Submission #229761

# Submission time Handle Problem Language Result Execution time Memory
229761 2020-05-06T09:46:49 Z NONAME Maja (COCI18_maja) C++17
11 / 110
94 ms 640 KB
#include <bits/stdc++.h>
#define sz(x) int(x.size())
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define N 100500
#define oo ll(1e16)
#define ft first
#define sd second
#define pb push_back
#define ppb pop_back
#define el '\n'
#define elf endl
#define base ll(1e9 + 7)
using namespace std;
typedef long long ll;
typedef long double ld;

int n, m, mx, k, a, b;
ll f[2][101][101], c[101][101], res;

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

//    in("input.txt");

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

    k >>= 1;

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

    mx = min(k, n * m);

    for (int t = 0; t <= 1; t++)
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
        f[t][i][j] = -1;

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

    for (int t = 0; t < mx; t++) {
        int cur = t & 1;
        int lst = cur ^ 1;

        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            f[cur][i][j] = -1;

        for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (f[lst][i][j] == -1)
                continue;

            if (i - 1 >= 0)
                f[cur][i - 1][j] = max(f[cur][i - 1][j], f[lst][i][j] + c[i][j]);
            if (j - 1 >= 0)
                f[cur][i][j - 1] = max(f[cur][i][j - 1], f[lst][i][j] + c[i][j]);
            if (i + 1 < n)
                f[cur][i + 1][j] = max(f[cur][i + 1][j], f[lst][i][j] + c[i][j]);
            if (j + 1 < m)
                f[cur][i][j + 1] = max(f[cur][i][j + 1], f[lst][i][j] + c[i][j]);
        }
    }

    int cur = (mx - 1) & 1;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
        if (f[cur][i][j] != -1)
            res = max(res, 2 * f[cur][i][j] + c[i][j]);

    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
        int cnt = k - mx;

        if (cnt < 0)
            continue;

        ll v = c[i][j];
        ll t = 0;

        if (i - 1 >= 0)
            t = max(t, c[i - 1][j]);
        if (j - 1 >= 0)
            t = max(t, c[i][j - 1]);
        if (i + 1 < n)
            t = max(t, c[i + 1][j]);
        if (j + 1 < m)
            t = max(t, c[i][j + 1]);


        res = max(res, 2 * f[cur][i][j] + v * cnt - c[i][j]);
    }

    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 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 19 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 94 ms 640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 16 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -