Submission #229212

# Submission time Handle Problem Language Result Execution time Memory
229212 2020-05-03T20:51:15 Z VEGAnn Maja (COCI18_maja) C++14
11 / 110
268 ms 620 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define PB push_back
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define MP make_pair
#define ft first
#define sd second
#define sz(x) ((int)x.size())
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int N = 110;
const int PW = 22;
const int oo = 2e9;
int steps[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int c[N][N], a, b, n, m, k, ite, it;
ll ans[2][N][N], res = 0;
bool yeee_boy;

void MAX(ll &x, ll y){
    x = max(x, y);
}

void check(){
    if (yeee_boy){
        if (ite == k) {
            cout << ans[it][a][b] - 1;
            exit(0);
        }
    } else {
        if (k < ite + ite) return;

        ll kol = k - ite - ite;

        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++){
            if (ans[it][i][j] == 0) continue;

            ll cur = ans[it][i][j] - 1;

            for (int z = 0; z < 4; z++){
                int cx = steps[z][0] + i, cy = steps[z][1] + j;

                if (cx < 0 || cy < 0 || cx >= n || cy >= n) continue;

                res = max(res, cur * 2 + kol * ((ll)c[i][j] + c[cx][cy]) - c[i][j]);
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n >> m >> a >> b >> k;
    a--; b--;

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> c[i][j];

    yeee_boy = bool(k <= m * n);

    ite = 0;
    it = 0;

    ans[0][a][b] = 1;

    check();

    for (ite = 1; ite <= min(n * m, k); ite++){
        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            ans[it ^ 1][i][j] = 0;

        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++){
            if (ans[it][i][j] == 0) continue;

            if (i > 0) MAX(ans[it ^ 1][i - 1][j], ans[it][i][j] + c[i - 1][j]);
            if (j > 0) MAX(ans[it ^ 1][i][j - 1], ans[it][i][j] + c[i][j - 1]);
            if (j + 1 < m) MAX(ans[it ^ 1][i][j + 1], ans[it][i][j] + c[i][j + 1]);
            if (i + 1 < n) MAX(ans[it ^ 1][i + 1][j], ans[it][i][j] + c[i + 1][j]);
        }

        it ^= 1;
        check();
    }

    cout << res;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 45 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 268 ms 620 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 37 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -