Submission #611912

#TimeUsernameProblemLanguageResultExecution timeMemory
611912tranxuanbachRaisins (IOI09_raisins)C++17
100 / 100
111 ms13984 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())

using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;

const int N = 50 + 2;

int n, m;
int a[N][N];

int b[N][N];

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

int dp[N][N][N][N];

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("KEK.inp", "r", stdin);
    // freopen("KEK.out", "w", stdout);
    cin >> n >> m;
    ForE(i, 1, n){
        ForE(j, 1, m){
            cin >> a[i][j];
        }
    }

    ForE(i, 1, n){
        ForE(j, 1, m){
            b[i][j] = b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1] + a[i][j];
        }
    }
    ForE(lenx, 1, n){
        ForE(x1, 1, n - lenx + 1){
            int x2 = x1 + lenx - 1;
            ForE(leny, 1, m){
                ForE(y1, 1, m - leny + 1){
                    int y2 = y1 + leny - 1;
                    dp[x1][x2][y1][y2] = INT_MAX;
                    For(x, x1, x2){
                        dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], dp[x1][x][y1][y2] + dp[x + 1][x2][y1][y2]);
                    }
                    For(y, y1, y2){
                        dp[x1][x2][y1][y2] = min(dp[x1][x2][y1][y2], dp[x1][x2][y1][y] + dp[x1][x2][y + 1][y2]);
                    }
                    dp[x1][x2][y1][y2] += sumsubrect(x1, y1, x2, y2);
                    if (lenx == 1 and leny == 1){
                        dp[x1][x2][y1][y2] = 0;
                    }
                }
            }
        }
    }
    cout << dp[1][n][1][m] << endl;
}

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|

--------------------------------------------------|
==================================================+
*/
#Verdict Execution timeMemoryGrader output
Fetching results...