1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 380 + 10; int a[maxn][maxn], n, m, k, ps[maxn][maxn], b[maxn][maxn]; deque<int> q[maxn]; int ans, v[maxn], vv[maxn], f[maxn], g[maxn], all, Min = 1E9;
int add(int idx, int ps[maxn], deque<int>& q) { while (!q.empty() && ps[q.back()] < ps[idx]) q.pop_back(); q.push_back(idx); if (idx - q.front() > k) q.pop_front(); return ps[idx] - ps[q.front()]; }
void go(int n, int m, int a[maxn][maxn]) { FOR (i, 1, n + 1) FOR (j, 1, m + 1) ps[j][i] = ps[j][i - 1] + a[i][j];
FOR (l, 1, n + 1) { FOR (j, 1, m + 1) { q[j].clear(); q[j].push_back(l - 1); } memset(v, 0, sizeof v); FOR (r, l, n + 1) { FOR (j, 1, m + 1) { vv[j] = ps[j][r] - ps[j][l - 1]; v[j] = min(v[j], add(r, ps[j], q[j])); } int f = 0, g = 0; FOR (i, 1, m + 1) { g = max(max(f, 0) + vv[i] - v[i], max(g, 0) + vv[i]); f = max(f, 0) + vv[i]; ans = max(ans, g); } } } }
int main() { cin >> n >> m >> k; FOR (i, 1, n + 1) FOR (j, 1, m + 1) { int t; scanf("%d", &t); a[i][j] = b[j][i] = t; all += t; Min = min(Min, t); } if (Min >= 0) { cout << all - Min << endl; return 0; } go(n, m, a); go(m, n, b); cout << ans << endl; }
|