Submission #839053

# Submission time Handle Problem Language Result Execution time Memory
839053 2023-08-28T14:42:12 Z VMaksimoski008 Raisins (IOI09_raisins) C++14
100 / 100
577 ms 57580 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int n, m;
int mat[52][52];
ll prefix[52][52];
ll dp[52][52][52][52];

ll query(int x1, int y1, int x2, int y2) {
    ll ans = 0;

    ans += prefix[x2][y2];
    ans -= prefix[x1-1][y2];
    ans -= prefix[x2][y1-1];
    ans += prefix[x1-1][y1-1];

    return ans;
}

ll f(int x1, int y1, int x2, int y2) {
    if(x1 > n || x2 > n || y1 > m || y2 > m) return 0;
    if(x1 < 1 || x2 < 1 || y1 < 1 || y2 < 1) return 0;
    if(x1 == x2 && y1 == y2) return 0;
    if(dp[x1][y1][x2][y2] != -1) return dp[x1][y1][x2][y2];

    ll ans = 1e16;

    for(int i=x1; i<x2; i++)
        ans = min(ans, f(x1, y1, i, y2) + f(i+1, y1, x2, y2));
    for(int i=y1; i<y2; i++)
        ans = min(ans, f(x1, y1, x2, i) + f(x1, i+1, x2, y2));

    return dp[x1][y1][x2][y2] = ans + query(x1, y1, x2, y2);
}

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

    cin >> n >> m;
    memset(prefix, 0, sizeof(prefix));
    memset(dp, -1, sizeof(dp));

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

    //build prefix table
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            prefix[i][j] += mat[i][j];
            prefix[i][j] += prefix[i-1][j];
            prefix[i][j] += prefix[i][j-1];
            prefix[i][j] -= prefix[i-1][j-1];
        }
    }

    cout << f(1, 1, n, m) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 57556 KB Output is correct
2 Correct 19 ms 57556 KB Output is correct
3 Correct 20 ms 57548 KB Output is correct
4 Correct 20 ms 57556 KB Output is correct
5 Correct 19 ms 57564 KB Output is correct
6 Correct 19 ms 57556 KB Output is correct
7 Correct 19 ms 57556 KB Output is correct
8 Correct 25 ms 57556 KB Output is correct
9 Correct 31 ms 57568 KB Output is correct
10 Correct 36 ms 57556 KB Output is correct
11 Correct 33 ms 57556 KB Output is correct
12 Correct 78 ms 57568 KB Output is correct
13 Correct 106 ms 57572 KB Output is correct
14 Correct 41 ms 57556 KB Output is correct
15 Correct 130 ms 57568 KB Output is correct
16 Correct 29 ms 57452 KB Output is correct
17 Correct 71 ms 57576 KB Output is correct
18 Correct 297 ms 57580 KB Output is correct
19 Correct 496 ms 57572 KB Output is correct
20 Correct 577 ms 57572 KB Output is correct