#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,a,b) for(int i=a; i<(b); i++)
#define all(x) x.begin(),x.end()
using ll=long long;
const ll INF=1LL<<60;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,m;
cin>>n>>m;
vector<vector<ll>> r(n+1,vector<ll>(m+1,0));
rep(i,n){
rep(j,m){
cin>>r[i+1][j+1];
r[i+1][j+1]+=r[i][j+1]+r[i+1][j]-r[i][j];
}
}
ll dp[51][51][51][51];
rep2(i,1,n+1){
rep(j,n){
ll lf=j,ri=j+i;
if(ri>n)break;
rep2(k,1,m+1){
rep(l,m){
ll up=l,dn=l+k;
if(dn>m)break;
dp[lf][ri][up][dn]=INF;
ll raisin=r[ri][dn]+r[lf][up]-r[lf][dn]-r[ri][up];
if(i==1 and k==1){
dp[lf][ri][up][dn]=0;
continue;
}
rep2(c,lf+1,ri){
dp[lf][ri][up][dn]=min(dp[lf][ri][up][dn],raisin+dp[lf][c][up][dn]+dp[c][ri][up][dn]);
}
rep2(c,up+1,dn){
dp[lf][ri][up][dn]=min(dp[lf][ri][up][dn],raisin+dp[lf][ri][up][c]+dp[lf][ri][c][dn]);
}
}
}
}
}
cout<<dp[0][n][0][m]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
53328 KB |
Output is correct |
2 |
Correct |
30 ms |
53340 KB |
Output is correct |
3 |
Correct |
25 ms |
53340 KB |
Output is correct |
4 |
Correct |
31 ms |
53212 KB |
Output is correct |
5 |
Correct |
25 ms |
53332 KB |
Output is correct |
6 |
Correct |
25 ms |
53340 KB |
Output is correct |
7 |
Correct |
25 ms |
53340 KB |
Output is correct |
8 |
Correct |
27 ms |
53340 KB |
Output is correct |
9 |
Correct |
28 ms |
53332 KB |
Output is correct |
10 |
Correct |
31 ms |
53336 KB |
Output is correct |
11 |
Correct |
27 ms |
53340 KB |
Output is correct |
12 |
Correct |
35 ms |
53588 KB |
Output is correct |
13 |
Correct |
38 ms |
53404 KB |
Output is correct |
14 |
Correct |
29 ms |
53340 KB |
Output is correct |
15 |
Correct |
41 ms |
53336 KB |
Output is correct |
16 |
Correct |
27 ms |
53292 KB |
Output is correct |
17 |
Correct |
34 ms |
53228 KB |
Output is correct |
18 |
Correct |
70 ms |
53360 KB |
Output is correct |
19 |
Correct |
127 ms |
53420 KB |
Output is correct |
20 |
Correct |
134 ms |
53424 KB |
Output is correct |