Submission #1091170

# Submission time Handle Problem Language Result Execution time Memory
1091170 2024-09-20T01:35:24 Z TrinhKhanhDung Maja (COCI18_maja) C++14
110 / 110
114 ms 772 KB
#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
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 107 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 66 ms 744 KB Output is correct
3 Correct 114 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 764 KB Output is correct
2 Correct 93 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 600 KB Output is correct
2 Correct 7 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct