# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
789216 | irmuun | Raisins (IOI09_raisins) | C++17 | 639 ms | 26548 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
const ll INF=1e18;
ll mn[51][51][51][51],a[51][51],sum[51][51];
void solve(ll l1,ll l2,ll r1,ll r2){
if(mn[l1][l2][r1][r2]<INF) return;
if(l1==l2&&r1==r2){
mn[l1][l2][r1][r2]=0;
return;
}
for(ll i=l1;i<l2;i++){
solve(l1,i,r1,r2);
solve(i+1,l2,r1,r2);
mn[l1][l2][r1][r2]=min(mn[l1][l2][r1][r2],mn[l1][i][r1][r2]+mn[i+1][l2][r1][r2]+sum[l2][r2]-sum[l1-1][r2]-sum[l2][r1-1]+sum[l1-1][r1-1]);
}
for(ll i=r1;i<r2;i++){
solve(l1,l2,r1,i);
solve(l1,l2,i+1,r2);
mn[l1][l2][r1][r2]=min(mn[l1][l2][r1][r2],mn[l1][l2][r1][i]+mn[l1][l2][i+1][r2]+sum[l2][r2]-sum[l1-1][r2]-sum[l2][r1-1]+sum[l1-1][r1-1]);
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,m;
cin>>n>>m;
for(ll i=0;i<=n;i++){
sum[i][0]=0;
}
for(ll j=0;j<=m;j++){
sum[0][j]=0;
}
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
cin>>a[i][j];
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
}
}
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j++){
for(ll l=1;l<=m;l++){
for(ll r=l;r<=m;r++){
mn[i][j][l][r]=INF;
}
}
}
}
solve(1,n,1,m);
cout<<mn[1][n][1][m];
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |