fork download
  1. #堀江伸一 会津大学オンラインジャッジ問3554 Combination 合格
  2. def f()
  3. return gets.split(" ").map{|e| e.to_i}
  4. end
  5. n,m=f()
  6. xs=f()
  7. ys=f()
  8.  
  9. s1=n-m
  10. dp=[]
  11. (s1+1).times{|i|
  12. dp<<xs[i]*ys[0]
  13. }
  14. if m==1 then
  15. puts dp.max
  16. else
  17. 1.upto(m-1){|p1|
  18. t=dp[0]
  19. dp.map!{|t2|
  20. if t<t2 then
  21. t=t2
  22. end
  23. t
  24. }
  25. dp2=[0]*(s1+1)
  26. t=0
  27. (s1+1).times{|p2|
  28. p3=p1+p2
  29. #p [p1,p2,p3]
  30. if p2==0 then
  31. dp2[0]=dp[0]+xs[p3]*ys[p1]
  32. t=dp2[0]
  33. else
  34.  
  35. dp2[p2]=[t,dp[p2]+xs[p3]*ys[p1]].max
  36. t=[t,dp2[p2]].max
  37. end
  38. }
  39. #p dp
  40. dp=dp2
  41.  
  42. }
  43. puts dp.max
  44. end
Success #stdin #stdout 0.01s 7916KB
stdin
10 4
1 2 3 4 5 6 7 8 9 10
3 2 1 0

stdout
46