#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) ll(x.size())
#define pb push_back
#define N 100005
#define MOD ll(998244353)
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
//typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ll f[2][105][105], ar[105][105], ans;
int main()
{
//freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, m, a, b, k;
cin >> n >> m >> a >> b >> k;
for (ll i = 1; i <= n; i++)
for (ll j = 1; j <= m; j++) cin >> ar[i][j];
int mn = min(n * m, k / 2);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) f[0][i][j] = f[1][i][j] = -1e18;
f[0][a][b] = 0;
for (int u = 0; u < mn; u++)
{
int nt = (u + 1) % 2, t = u % 2;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) f[nt][i][j] = -1e18;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) f[nt][i][j] = ar[i][j] + max(i - 1 == 0 ? -1e18 : f[t][i - 1][j], max(i + 1 > n ? -1e18 : f[t][i + 1][j], max(j - 1 == 0 ? -1e18 : f[t][i][j - 1], j + 1 > m ? -1e18 : f[t][i][j + 1])));
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
ll sum = 2 * f[mn % 2][i][j];
ll rst = k - mn * 2;
ll mx = 0;
if (i - 1 >= 1) mx = max(mx, ar[i - 1][j]);
if (j - 1 >= 1) mx = max(mx, ar[i][j - 1]);
if (i + 1 <= n) mx = max(mx, ar[i + 1][j]);
if (j + 1 <= m) mx = max(mx, ar[i][j + 1]);
sum += max(0ll, (rst / 2)) * mx;
sum += max(0ll, ((rst / 2) - 1)) * ar[i][j];
if (sum == 2 * f[mn % 2][i][j]) sum -= ar[i][j];
ans = max(ans, sum);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
33 ms |
468 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
257 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
189 ms |
512 KB |
Output is correct |
3 |
Correct |
241 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
632 KB |
Output is correct |
2 |
Correct |
304 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
384 KB |
Output is correct |
2 |
Correct |
28 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |