#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define PB push_back
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define MP make_pair
#define ft first
#define sd second
#define sz(x) ((int)x.size())
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int N = 110;
const int PW = 22;
const int oo = 2e9;
int steps[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int c[N][N], a, b, n, m, k, ite, it;
ll ans[2][N][N], res = 0;
bool yeee_boy;
void MAX(ll &x, ll y){
x = max(x, y);
}
void check(){
if (yeee_boy){
if (ite == k) {
cout << ans[it][a][b] - 1;
exit(0);
}
} else {
if (k < ite + ite) return;
ll kol = (k - ite - ite) / 2ll;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++){
if (ans[it][i][j] == 0) continue;
ll cur = ans[it][i][j] - 1;
for (int z = 0; z < 4; z++){
int cx = steps[z][0] + i, cy = steps[z][1] + j;
if (cx < 0 || cy < 0 || cx >= n || cy >= m) continue;
res = max(res, cur * 2 + kol * ((ll)c[i][j] + c[cx][cy]) - c[i][j]);
}
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> m >> a >> b >> k;
a--; b--;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> c[i][j];
yeee_boy = bool(k <= m * n);
ite = 0;
it = 0;
ans[0][a][b] = 1;
check();
for (ite = 1; ite <= min(n * m, k); ite++){
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
ans[it ^ 1][i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++){
if (ans[it][i][j] == 0) continue;
if (i > 0) MAX(ans[it ^ 1][i - 1][j], ans[it][i][j] + c[i - 1][j]);
if (j > 0) MAX(ans[it ^ 1][i][j - 1], ans[it][i][j] + c[i][j - 1]);
if (j + 1 < m) MAX(ans[it ^ 1][i][j + 1], ans[it][i][j] + c[i][j + 1]);
if (i + 1 < n) MAX(ans[it ^ 1][i + 1][j], ans[it][i][j] + c[i + 1][j]);
}
it ^= 1;
check();
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
308 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
460 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
374 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
384 KB |
Output is correct |
2 |
Correct |
357 ms |
632 KB |
Output is correct |
3 |
Correct |
377 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
512 KB |
Output is correct |
2 |
Correct |
473 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
384 KB |
Output is correct |
2 |
Correct |
38 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |