fork download
  1. h,w=gets.split(" ").map{|e| e.to_i}
  2. a=[[0]*(w+1)]
  3. b=[[0]*(w+1)]
  4. h.times{|y|
  5. a<<gets.split(" ").map{|e| e.to_i}
  6. a[y+1].unshift(0)
  7. b[y+1]=[0]*(w+1)
  8. }
  9.  
  10. s1=[0]*(w+1)
  11. 1.upto(h){|y|
  12. s0=0
  13. 1.upto(w){|x|
  14. b[y][x]=s1[x]+s0+b[y][x-1]+b[y-1][x]+a[y][x]*(x*y)
  15. s0+=a[y][x]*x
  16. s1[x]+=a[y][x]*y
  17. p [s0,s1]
  18. }
  19. }
  20. p b
  21. ans=0
  22. h.times{|y1|
  23. w.times{|x1|
  24. y1.upto(h-1){|y2|
  25. x1.upto(w-1){|x2|
  26. y1.upto(y2){|y3|
  27. x1.upto(x2){|x3|
  28. ans+=a[y3+1][x3+1]
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. puts ans
Success #stdin #stdout 0.01s 8112KB
stdin
4 5
3 5 7 2 5
1 2 2 3 1
4 8 4 8 4
3 9 1 1 5
stdout
[3, [0, 3, 0, 0, 0, 0]]
[13, [0, 3, 5, 0, 0, 0]]
[34, [0, 3, 5, 7, 0, 0]]
[42, [0, 3, 5, 7, 2, 0]]
[67, [0, 3, 5, 7, 2, 5]]
[1, [0, 5, 5, 7, 2, 5]]
[5, [0, 5, 9, 7, 2, 5]]
[11, [0, 5, 9, 11, 2, 5]]
[23, [0, 5, 9, 11, 8, 5]]
[28, [0, 5, 9, 11, 8, 7]]
[4, [0, 17, 9, 11, 8, 7]]
[20, [0, 17, 33, 11, 8, 7]]
[32, [0, 17, 33, 23, 8, 7]]
[64, [0, 17, 33, 23, 32, 7]]
[84, [0, 17, 33, 23, 32, 19]]
[3, [0, 29, 33, 23, 32, 19]]
[21, [0, 29, 69, 23, 32, 19]]
[24, [0, 29, 69, 27, 32, 19]]
[28, [0, 29, 69, 27, 36, 19]]
[53, [0, 29, 69, 27, 36, 39]]
[[0, 0, 0, 0, 0, 0], [0, 3, 16, 50, 92, 159], [0, 8, 38, 112, 241, 438], [0, 25, 124, 303, 680, 1249], [0, 54, 286, 645, 1397, 2793]]
2784