This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |