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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
#define double long double
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 1e2 + 10;
int N, M, A, B, K;
int mat[MAX][MAX], sum[MAX][MAX];
ll dp[2][MAX][MAX];
void solve(){
cin >> N >> M >> A >> B >> K;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
cin >> mat[i][j];
}
}
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
sum[i][j] = max({mat[i - 1][j], mat[i + 1][j], mat[i][j - 1], mat[i][j + 1]}) + mat[i][j];
}
}
memset(dp, -0x3f, sizeof dp);
dp[0][A][B] = 0;
ll ans = 0;
for(int p = 1; p <= min(K / 2, N * M); p++){
int id = p & 1;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
dp[id][i][j] = max({dp[!id][i - 1][j], dp[!id][i][j - 1], dp[!id][i + 1][j], dp[!id][i][j + 1]}) + mat[i][j];
maximize(ans, dp[id][i][j] * 2LL - mat[i][j] + 1LL * sum[i][j] * ((K - 2 * p) / 2));
}
}
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("mst.inp","r",stdin);
// freopen("mst.out","w",stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
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... |