제출 #531895

#제출 시각아이디문제언어결과실행 시간메모리
531895__Variatto건포도 (IOI09_raisins)C++17
100 / 100
243 ms21016 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define ll long long
const int MAX=51, MAXV=1e9+10;
int n, m, dp[MAX][MAX][MAX][MAX], p[MAX][MAX], t[MAX][MAX];
int pref(int x1, int y1, int x2, int y2){
    return p[x2][y2]+p[x1-1][y1-1]-p[x1-1][y2]-p[x2][y1-1];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin>>n>>m;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            cin>>t[i][j];
            p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1]+t[i][j];
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++){
            if(i==1 && j==1) continue;
            for(int x=1; x<=n-i+1; x++){
                for(int y=1; y<=m-j+1; y++){
                    dp[x][y][x+i-1][y+j-1]=MAXV;
                    //pion
                    for(int l=y; l<=y+j-2; l++)
                        dp[x][y][x+i-1][y+j-1]=min(dp[x][y][x+i-1][y+j-1], dp[x][y][x+i-1][l]+dp[x][l+1][x+i-1][y+j-1]);
                    //poziom
                    for(int l=x; l<=x+i-2; l++)
                        dp[x][y][x+i-1][y+j-1]=min(dp[x][y][x+i-1][y+j-1], dp[x][y][l][y+j-1]+dp[l+1][y][x+i-1][y+j-1]);
                    dp[x][y][x+i-1][y+j-1]+=pref(x, y, x+i-1, y+j-1);

                }
            }
        }
    }
    cout<<dp[1][1][n][m]<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...