#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int maxn = 201;
int n, m, sx, sy, k;
ll a[maxn][maxn];
ll dp[maxn][maxn][maxn];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool works(int x, int y){
return x >= 0 && y >= 0 && x < n && y < m;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> sx >> sy >> k;
sx--, sy--;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
cin >> a[i][j];
}
memset(dp, -1, sizeof(dp));
dp[sx][sy][0] = 0;
for(int l = 0; l < n + m; l++)
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
if(dp[i][j][l] == -1) continue;
for(int f = 0; f < 4; f++){
int x = i + dx[f], y = j + dy[f];
if(works(x, y)){
dp[x][y][l + 1] = max(dp[x][y][l + 1], dp[i][j][l] + a[x][y]);
}
}
}
ll ret = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
for(int l = 0; l <= n + m; l++){
if(dp[i][j][l] == -1) continue;
for(int f = 0; f < 4; f++){
int x = i + dx[f], y = j + dy[f];
if(works(x, y)){
if(2 * l <= k){
ret = max(ret, 2 * dp[i][j][l] +
(k - 2 * l) / 2 * (a[i][j] + a[x][y]) - a[i][j]);
}
if(l + 1 <= n + m && 2 * l + 2 <= k && dp[x][y][l + 1] != -1){
ret = max(ret, dp[i][j][l] + dp[x][y][l + 1] +
(k - 2 * l - 2) / 2 * (a[i][j] + a[x][y]));
}
}
}
}
cout << ret << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
63872 KB |
Output is correct |
2 |
Correct |
38 ms |
63888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
63872 KB |
Output is correct |
2 |
Correct |
37 ms |
63864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
63864 KB |
Output is correct |
2 |
Correct |
37 ms |
63864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
64000 KB |
Output is correct |
2 |
Correct |
37 ms |
63864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
63992 KB |
Output is correct |
2 |
Correct |
38 ms |
63864 KB |
Output is correct |
3 |
Incorrect |
103 ms |
64120 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
63864 KB |
Output is correct |
2 |
Correct |
59 ms |
64124 KB |
Output is correct |
3 |
Incorrect |
91 ms |
64120 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
64120 KB |
Output is correct |
2 |
Correct |
66 ms |
64120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
64000 KB |
Output is correct |
2 |
Correct |
42 ms |
63992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
63872 KB |
Output is correct |
2 |
Correct |
38 ms |
64000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
64000 KB |
Output is correct |
2 |
Correct |
37 ms |
63876 KB |
Output is correct |