fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int N = 1e3 + 5;
  12.  
  13. int n, k;
  14. int a[N][N];
  15.  
  16. // - Quy ước:
  17. // Hình chữ nhật (x1, y1, x2, y2) là hình chữ nhật có góc trái trên là (x1, y1) và góc phải dưới là (x2, y2)
  18.  
  19. ll sum[N][N]; // sum[x][y] = Tổng hình chữ nhật (1, 1, x, y)
  20.  
  21. // Tổng hình chữ nhật (x1, y1, x2, y2)
  22. ll getSum(int x1, int y1, int x2, int y2) {
  23. return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
  24. }
  25.  
  26. int main() {
  27. ios::sync_with_stdio(false);
  28. cin.tie(nullptr);
  29. cin >> n >> k;
  30.  
  31. for (int i = 1; i <= n; i++) {
  32. for (int j = 1; j <= n; j++) cin >> a[i][j];
  33. }
  34.  
  35. for (int x = 1; x <= n; x++) {
  36. for (int y = 1; y <= n; y++) {
  37. sum[x][y] = sum[x - 1][y] + sum[x][y - 1] - sum[x - 1][y - 1] + a[x][y];
  38. }
  39. }
  40.  
  41. ll ans = 0;
  42. for (int x1 = 1; x1 + k - 1 <= n; x1++) {
  43. for (int y1 = 1; y1 + k - 1 <= n; y1++) {
  44. int x2 = x1 + k - 1, y2 = y1 + k - 1;
  45. ans = max(ans, getSum(x1, y1, x2, y2));
  46. }
  47. }
  48.  
  49. cout << ans << '\n';
  50. }
  51.  
Success #stdin #stdout 0.01s 5636KB
stdin
4 3
1 9 1 1
9 9 9 9
1 9 9 9
1 9 9 14
stdout
86