이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// NK
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef pair<int,int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define mkt make_tuple
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ord_set;
int ma[100][100];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int a,b,n,m;
void solve(){
cin>>n>>m;
cin>>a>>b;
a--, b--;
int k; cin>>k;
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
cin>>ma[i][j];
}
}
ll dp2[100][100];
memset(dp2, false, sizeof dp2);
ll acc = 0;
for (int j=b; j<m; j++){
acc += ma[a][j];
dp2[a][j]=acc;
}
acc = 0;
for (int j=b; j>=0; j--){
acc += ma[a][j];
dp2[a][j]=acc;
}
acc = 0;
for (int i=a; i<n; i++){
acc += ma[i][b];
dp2[i][b]=acc;
}
acc = 0;
for (int i=a; i>=0; i--){
acc += ma[i][b];
dp2[i][b]=acc;
}
// now for the quadrants:
for (int i=a-1; i>=0; i--){
for (int j=b+1; j<m; j++){
dp2[i][j] = max(dp2[i+1][j], dp2[i][j-1]) + ma[i][j];
}
}
for (int i=a-1; i>=0; i--){
for (int j=b-1; j>=0; j--){
dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + ma[i][j];
}
}
for (int i=a+1; i<n; i++){
for (int j=b-1; j>=0; j--){
dp2[i][j] = max(dp2[i-1][j], dp2[i][j+1]) + ma[i][j];
}
}
for (int i=a+1; i<n; i++){
for (int j=b+1; j<m; j++){
dp2[i][j] = max(dp2[i-1][j], dp2[i][j-1]) + ma[i][j];
}
}
////
ll answer = 0;
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
if (i==a && j==b) continue;
int stepsrem = abs(a-i)+abs(b-j);
stepsrem*=2;
stepsrem = k - stepsrem;
//cout << stepsrem << endl;
if (stepsrem < 0) continue;
stepsrem/=2;
for (int dir=0; dir<4; ++dir){
int row = i + dy[dir];
int col = j + dx[dir];
if (0<=row && row<n && 0<=col && col<m){
ll best = ma[i][j] + ma[row][col];
best = best * stepsrem;
best = best + dp2[i][j] * 2LL - ma[i][j];
//cout << best << ' ';
answer = max(answer, best);
}
}
//cout << endl;
}
}
cout << answer << '\n';
}
int main(){
/*Bolivia_is_nice;
int t = 1; cin>>t;
while(t--)
solve();*/
Bolivia_is_nice;
solve();
return 0;
}
/*
~/.emacs
*/
# | 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... |