fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> ii;
  6.  
  7. const int INF = 1e9;
  8. const ll LINF = 1e18;
  9.  
  10. const int N = 1e3 + 5;
  11.  
  12. int n, k;
  13. int a[N][N];
  14. ll sum[N][N];
  15.  
  16. ll getSum(int x1, int y1, int x2, int y2) {
  17. return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
  18. }
  19.  
  20. int main() {
  21. ios::sync_with_stdio(0); cin.tie(0);
  22. cin >> n >> k;
  23.  
  24. for (int i = 1; i <= n; i++) {
  25. for (int j = 1; j <= n; j++) cin >> a[i][j];
  26. }
  27.  
  28. for (int x = 1; x <= n; x++) {
  29. for (int y = 1; y <= n; y++) {
  30. sum[x][y] = sum[x - 1][y] + sum[x][y - 1] - sum[x - 1][y - 1] + a[x][y];
  31. }
  32. }
  33.  
  34. ll ans = 0;
  35. for (int x1 = 1; x1 + k - 1 <= n; x1++) {
  36. for (int y1 = 1; y1 + k - 1 <= n; y1++) {
  37. int x2 = x1 + k - 1, y2 = y1 + k - 1;
  38. ans = max(ans, getSum(x1, y1, x2, y2));
  39. }
  40. }
  41.  
  42. cout << ans << '\n';
  43. }
  44.  
Success #stdin #stdout 0.01s 5640KB
stdin
4 3
1 9 1 1
9 9 9 9
1 9 9 9
1 9 9 14
stdout
86