#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x),end(x)
using pii = pair<int,int>;
using i64 = int64_t;
using i8 = int;
vector<vector<int>> raisins;
int Y,X;
i64 memo[50*50*50*50] = {};
i64& mem(i8 sx,i8 sy,i8 ex, i8 ey)
{
return memo[sx*50*50*50+sy*50*50+ex*50+ey];
}
i64 min_cost(i8 sx,i8 sy,i8 ex,i8 ey)
{
// memoize
if(ey-sy <= 1 && ex - sx <= 1)
{
return 0;
}
if (mem(sx,sy,ex,ey)!=0) return mem(sx,sy,ex,ey);
// rows
int cost = /* count of raisins present */ 0;
for(int i = sy; i < ey; i++) for(int j = sx; j < ex; j++) cost += raisins[i][j];
i64 mincost = INT64_MAX;
for(int i = sx+1; i < ex;i++)
{
mincost = min(mincost,min_cost(sx,sy,i,ey)+min_cost(i,sy,ex,ey));
}
for(int i = sy+1; i < ey;i++)
{
mincost = min(mincost,min_cost(sx,sy,ex,i)+min_cost(sx,i,ex,ey));
}
mem(sx,sy,ex,ey) = cost + mincost;
return cost + mincost;
}
int main()
{
cin >> Y >> X;
raisins.assign(Y,vector<int>(X));
for(int i = 0; i < Y; i++) for(int j = 0; j < X; j++) cin >> raisins[i][j];
cout << min_cost(0,0,X,Y) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
1 ms |
944 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
9 ms |
3764 KB |
Output is correct |
9 |
Correct |
15 ms |
4608 KB |
Output is correct |
10 |
Correct |
20 ms |
5688 KB |
Output is correct |
11 |
Correct |
18 ms |
5860 KB |
Output is correct |
12 |
Correct |
63 ms |
10212 KB |
Output is correct |
13 |
Correct |
105 ms |
13460 KB |
Output is correct |
14 |
Correct |
29 ms |
7840 KB |
Output is correct |
15 |
Correct |
126 ms |
14888 KB |
Output is correct |
16 |
Correct |
10 ms |
2900 KB |
Output is correct |
17 |
Correct |
47 ms |
7428 KB |
Output is correct |
18 |
Correct |
330 ms |
23400 KB |
Output is correct |
19 |
Correct |
557 ms |
31436 KB |
Output is correct |
20 |
Correct |
670 ms |
33636 KB |
Output is correct |