#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair<int, int>
#define long long long
#define v vector
#define rep(i,a,b) for(int i=a; i<b; i++)
void setIO(string name, int submit) {
if (submit) {
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
const int MAXN=52;
int arr[MAXN][MAXN];
int dp[MAXN][MAXN][MAXN][MAXN];
int N, M;
int recurse(int ix, int iy, int jx, int jy){
if(jx-ix==0 && jy-iy==0) {
dp[ix][iy][jx][jy]=0;
}
if(dp[ix][iy][jx][jy]==-1){
int best=1<<30;
for(int row= iy+1; row<=jy; row++){
best= min(best, recurse(ix, row, jx, jy)+recurse(ix, iy, jx, row-1));
}
for(int col= ix+1; col<=jx; col++){
best= min(best, recurse(col, iy, jx, jy)+ recurse(ix, iy, col-1, jy));
}
int base=arr[jx][jy]-arr[ix-1][jy]-arr[jx][iy-1]+arr[ix-1][iy-1];
dp[ix][iy][jx][jy]= best+base;
}
return dp[ix][iy][jx][jy];
}
int main() {
setIO("", 0);
cin >>N>>M;
rep(i,1,N+1){
rep(j,1,M+1){
cin>> arr[i][j];
arr[i][j]+=arr[i-1][j]+arr[i][j-1]-arr[i-1][j-1];
}
}
rep(i,0,N+1){
rep(j,0,M+1){
rep(k,0,N+1){
rep(l,0,M+1){
dp[i][j][k][l]=-1;
}
}
}
}
cout << recurse(1,1,N,M)<< endl;
return 0;
}
Compilation message
raisins.cpp: In function 'void setIO(std::string, int)':
raisins.cpp:13:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
13 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
raisins.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
14 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
1152 KB |
Output is correct |
7 |
Correct |
1 ms |
1408 KB |
Output is correct |
8 |
Correct |
13 ms |
3840 KB |
Output is correct |
9 |
Correct |
22 ms |
5504 KB |
Output is correct |
10 |
Correct |
33 ms |
6528 KB |
Output is correct |
11 |
Correct |
30 ms |
5624 KB |
Output is correct |
12 |
Correct |
107 ms |
11520 KB |
Output is correct |
13 |
Correct |
186 ms |
14200 KB |
Output is correct |
14 |
Correct |
50 ms |
6912 KB |
Output is correct |
15 |
Correct |
237 ms |
15352 KB |
Output is correct |
16 |
Correct |
17 ms |
5632 KB |
Output is correct |
17 |
Correct |
81 ms |
10488 KB |
Output is correct |
18 |
Correct |
693 ms |
21760 KB |
Output is correct |
19 |
Correct |
1497 ms |
26472 KB |
Output is correct |
20 |
Correct |
1777 ms |
28032 KB |
Output is correct |