// Problem: 5634. IOI 2009 Raisins
// Contest: omegaUp
// URL: https://omegaup.com/arena/problem/IOI-2009-Raisins/
// Memory Limit: 63 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int INF=INT_MAX;
int A[52][52];
int memo[52][52][52][52];
int fr(int i1,int j1, int i2, int j2){
if(i1==i2 && j1==j2) return 0;
int minimo=INF;
for(int i=i1+1;i<=i2;i++){
minimo=min(fr(i1,j1,i-1,j2)+fr(i,j1,i2,j2),minimo);
}
for(int i=j1+1; i<=j2;i++){
minimo=min(fr(i1,j1,i2,i-1)+fr(i1,i,i2,j2),minimo);
}
return A[i2][j2]-A[i1-1][j2]-A[i2][j1-1]+A[i1-1][j1-1]+minimo;
}
int dp(int i1,int j1, int i2, int j2){
if(memo[i1][j1][i2][j2]!=-1) return memo[i1][j1][i2][j2];
if(i1==i2 && j1==j2)return 0;
int minimo=INF;
for(int i=i1+1;i<=i2;i++){
minimo=min(dp(i1,j1,i-1,j2)+dp(i,j1,i2,j2),minimo);
}
for(int i=j1+1; i<=j2;i++){
minimo=min(dp(i1,j1,i2,i-1)+dp(i1,i,i2,j2),minimo);
}
memo[i1][j1][i2][j2]=A[i2][j2]-A[i1-1][j2]-A[i2][j1-1]+A[i1-1][j1-1]+minimo;
return memo[i1][j1][i2][j2];
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>A[i][j];
A[i][j]+=A[i-1][j]+A[i][j-1]-A[i-1][j-1];
}
}
///cout<<fr(1,1,n,m)<<'\n';
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int k=1;k<=n;k++){
for(int l=1;l<=m;l++){
memo[i][j][k][l]=-1;
}
}
}
}
cout<<dp(1,1,n,m)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
3404 KB |
Output is correct |
9 |
Correct |
5 ms |
4940 KB |
Output is correct |
10 |
Correct |
7 ms |
5836 KB |
Output is correct |
11 |
Correct |
6 ms |
4924 KB |
Output is correct |
12 |
Correct |
16 ms |
10572 KB |
Output is correct |
13 |
Correct |
25 ms |
13312 KB |
Output is correct |
14 |
Correct |
8 ms |
6092 KB |
Output is correct |
15 |
Correct |
29 ms |
14460 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
14 ms |
9760 KB |
Output is correct |
18 |
Correct |
68 ms |
20840 KB |
Output is correct |
19 |
Correct |
110 ms |
25292 KB |
Output is correct |
20 |
Correct |
128 ms |
26948 KB |
Output is correct |