#include <bits/stdc++.h>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2","popcnt")
using namespace std;
#ifdef MIKU
#define debug(x...) cout << '[' << #x << "] : ", dout(x)
void dout() { cout << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
typedef pair<int, int> pii;
const int MXN = 105, INF = 3.9e18;
int n, m, x, y, k, c[MXN][MXN];
int dp[MXN][MXN], pd[MXN][MXN];
const int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
bool OUT(int x, int y, int d) {
x += dx[d];
y += dy[d];
if (!(0 <= x && x < n)) return true;
if (!(0 <= y && y < m)) return true;
return false;
}
void DP() {
FOR(i, 0, n) FOR(j, 0, m) {
pd[i][j] = -INF;
FOR(d, 0, 4) {
if (OUT(i, j, d)) continue;
pd[i][j] = max(pd[i][j], c[i][j] + dp[i + dx[d]][j + dy[d]]);
}
}
FOR(i, 0, n) FOR(j, 0, m) dp[i][j] = pd[i][j];
}
int LU(int a, int b) {
int dis = abs(a - x) + abs(y - b);
int ans = 0;
FOR(d, 0, 4) {
if (OUT(a, b, d)) continue;
ans = max(ans, c[a][b] + c[a + dx[d]][b + dy[d]]);
}
return (k / 2 - dis) * ans;
}
void miku() {
cin >> n >> m >> x >> y >> k;
x--, y--;
FOR(i, 0, n) FOR(j, 0, m) cin >> c[i][j];
if (k <= 10000) {
FOR(i, 0, n) FOR(j, 0, m) dp[i][j] = -INF;
dp[x][y] = 0;
while (k--) DP();
cout << dp[x][y] << '\n';
} else {
for (int i = y - 1; i >= 0; i--) dp[x][i] = dp[x][i + 1] + c[x][i];
for (int i = y + 1; i < m; i++) dp[x][i] = dp[x][i - 1] + c[x][i];
for (int i = x - 1; i >= 0; i--) {
dp[i][y] = dp[i + 1][y] + c[i][y];
for (int j = y - 1; j >= 0; j--) dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]) + c[i][j];
for (int j = y + 1; j < m; j++) dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]) + c[i][j];
}
for (int i = x + 1; i < n; i++) {
dp[i][y] = dp[i - 1][y] + c[i][y];
for (int j = y - 1; j >= 0; j--) dp[i][j] = max(dp[i - 1][j], dp[i][j + 1]) + c[i][j];
for (int j = y + 1; j < m; j++) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + c[i][j];
}
int ans = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans = max(ans, 2 * dp[i][j] - c[i][j] + LU(i, j));
cout << ans << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(iostream::failbit);
miku();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
464 KB |
Output is correct |
2 |
Correct |
4 ms |
516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
287 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |