This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**Lucky Boy**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxc 1000000007
#define maxn 105
#define maxm 500005
#define pii pair <int,int>
#define Task "Maja"
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n,m,a,b,k,f[maxn][maxn],dd[maxn][maxn];
long long dp[2][maxn][maxn],ans;
int xx[] = {1,-1,0,0};
int yy[] = {0,0,1,-1};
int Recode(int x,int y)
{
return (x - 1) * m + y;
}
bool Inside(int x,int y)
{
return x >= 1 && x <= n && y >= 1 && y <= m;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//freopen(Task".inp", "r",stdin);
//freopen(Task".out", "w",stdout);
cin >> n >> m >> a >> b >> k;
k >>= 1;
int lim = min(k,n*m-1);
FOR(i,1,n) FOR(j,1,m) cin >> f[i][j];
FOR(i,0,1) FOR(j,1,n) FOR(h,1,m) dp[i][j][h] = -1ll*maxc*maxc;
dp[0][a][b] = 0;
FOR(h,1,lim)
{
bool st = h & 1;
FOR(i,1,n)
FOR(j,1,m)
FOR(h,0,3)
{
int u = i + xx[h],
v = j + yy[h];
if (Inside(u,v))
{
dp[st][u][v] = max(dp[st][u][v],dp[!st][i][j] + f[u][v]);
}
}
}
bool st = lim & 1;
FOR(i,1,n)
FOR(j,1,m)
{
int cur = 0;
FOR(h,0,3)
{
int u = i + xx[h],
v = j + yy[h];
cur = max(cur,f[u][v] * Inside(u,v));
}
ans = max(ans,dp[st][i][j] * 2 + 1ll*(k - lim) * (cur + f[i][j]) - f[i][j]);
}
cout << ans;
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... |