제출 #1347241

#제출 시각아이디문제언어결과실행 시간메모리
1347241kawhiet건포도 (IOI09_raisins)C++20
10 / 100
259 ms21168 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...) 47
#endif

constexpr int N = 51;
constexpr int inf = 1e9;

int dp[N][N][N][N];
int a[N][N], p[N][N];

int get(int x1, int y1, int x2, int y2) {
    return p[x2][y2] - p[x2][y1 - 1] - p[x1 - 1][y2] + p[x1 - 1][y1 - 1];
}

int solve(int x1, int y1, int x2, int y2) {
    if (dp[x1][y1][x2][y2]) {
        return dp[x1][y1][x2][y2];
    }
    if ((x2 - x1 == 1 && y1 == y2) || (y2 - y1 == 1 && x1 == x2)) {
        dp[x1][y1][x2][y2] = a[x1][y1] + a[x2][y2];
        return a[x1][y1] + a[x2][y2];
    }
    int ans = inf, k = get(x1, y1, x2, y2);
    for (int i = x1; i < x2; i++) {
        ans = min(ans, solve(x1, y1, i, y2) + solve(i + 1, y1, x2, y2) + k);
    }
    for (int i = y1; i < y2; i++) {
        ans = min(ans, solve(x1, y1, x2, i) + solve(x1, i + 1, x2, y2) + k);
    }
    dp[x1][y1][x2][y2] = ans;
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j];
        }
    }
    cout << solve(1, 1, n, m) << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...