fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. signed main()
  6. {
  7. ios::sync_with_stdio(0);
  8. cin.tie(0);
  9.  
  10. int n, m;
  11. cin >> n >> m;
  12. int arr[n + 2][m + 2];
  13. for (int i = 0; i < m + 2; i++)
  14. {
  15. arr[0][i] = 0;
  16. arr[n + 1][i] = 0;
  17. }
  18. for (int i = 0; i < n + 2; i++)
  19. {
  20. arr[i][0] = 0;
  21. arr[i][m + 1] = 0;
  22. }
  23.  
  24. for (int i = 1; i <= n; i++)
  25. for (int j = 1; j <= m; j++)
  26. cin >> arr[i][j];
  27.  
  28. int ans = 0;
  29. for (int i = 1; i <= n; i++)
  30. {
  31. for (int j = 1; j <= m; j++)
  32. {
  33. int curr1 = 0, curr2 = 0, curr3 = 0, curr4 = 0;
  34. for (int k = i + 1; k < n + 2; k++)
  35. curr1 = max(curr1, arr[k][j]);
  36.  
  37. for (int k = 0; k < i; k++)
  38. curr2 = max(curr2, arr[k][j]);
  39.  
  40. for (int k = 0; k < j; k++)
  41. curr3 = max(curr3, arr[i][k]);
  42.  
  43. for (int k = j + 1; k < m + 2; k++)
  44. curr4 = max(curr4, arr[i][k]);
  45.  
  46. int ans_curr = min(curr1, curr2);
  47. ans_curr = min(ans_curr, curr3);
  48. ans_curr = min(ans_curr, curr4);
  49.  
  50. if (ans_curr > arr[i][j])
  51. ans += ans_curr - arr[i][j];
  52. }
  53. }
  54. cout << ans << '\n';
  55. }
Success #stdin #stdout 0.01s 5260KB
stdin
3 3
2 2 2
2 -1 2
2 2 2
stdout
3