#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];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
712 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
8 ms |
2504 KB |
Output is correct |
9 |
Correct |
13 ms |
4164 KB |
Output is correct |
10 |
Correct |
18 ms |
4900 KB |
Output is correct |
11 |
Correct |
15 ms |
3540 KB |
Output is correct |
12 |
Correct |
55 ms |
8984 KB |
Output is correct |
13 |
Correct |
97 ms |
11644 KB |
Output is correct |
14 |
Correct |
25 ms |
4308 KB |
Output is correct |
15 |
Correct |
109 ms |
13268 KB |
Output is correct |
16 |
Correct |
11 ms |
6612 KB |
Output is correct |
17 |
Correct |
55 ms |
11352 KB |
Output is correct |
18 |
Correct |
298 ms |
23272 KB |
Output is correct |
19 |
Correct |
486 ms |
24532 KB |
Output is correct |
20 |
Correct |
639 ms |
26548 KB |
Output is correct |