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.  
  15. s1[x]+=a[y][x]*y*x
  16. s0=s0+s1[x]+a[y][x]*x*y
  17. b[y][x]=s1[x]+s0
  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.02s 8308KB
stdin
4 5
3 5 7 2 5
1 2 2 3 1
4 8 4 8 4
3 9 1 1 5
stdout
[[0, 0, 0, 0, 0, 0], [0, 9, 36, 89, 92, 159], [0, 12, 51, 111, 166, 214], [0, 46, 209, 317, 600, 722], [0, 70, 389, 425, 648, 994]]
2784