#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
long long calc(int a, int b, int c, int d, vector<vector<long long>> &pref){
return pref[c][d] - pref[c][b] - pref[a][d] + pref[a][b];
}
long long split(int a, int b, int c, int d, vector<vector<long long>> &arr, vector<vector<vector<vector<long long>>>> &best, vector<vector<long long>> &pref){
if(best[a][b][c-1][d-1] < 2e12){
return best[a][b][c-1][d-1];
}
if(c-a==1 && d-b==1){
best[a][b][c-1][d-1] = 0;
return 0;
}
for(int i = a + 1; i < c; i ++){
best[a][b][c-1][d-1] = min(best[a][b][c-1][d-1], split(a,b,i,d,arr,best,pref) + split(i,b,c,d,arr,best,pref) + calc(a,b,c,d,pref));
}
for(int i = b + 1; i < d; i ++){
best[a][b][c-1][d-1] = min(best[a][b][c-1][d-1], split(a,b,c,i,arr,best,pref) + split(a,i,c,d,arr,best,pref) + calc(a,b,c,d,pref));
}
return best[a][b][c-1][d-1];
}
int main()
{
int N, M;
cin >> N >> M;
vector<vector<long long>> arr(N, vector<long long> (M)), pref(N+5, vector<long long>(M + 5, 0));
vector<vector<vector<vector<long long>>>> best(N, vector<vector<vector<long long>>>(M, vector<vector<long long>>(N, vector<long long>(M, 2e12))));
for(int i = 0; i < N; i ++){
for(int j = 0; j < M; j ++){
cin >> arr[i][j];
}
}
for(int i = 0; i < N; i ++){
for(int j = 0; j < M; j ++){
pref[i + 1][j + 1] = arr[i][j] + pref[i][j+1] + pref[i + 1][j] - pref[i][j];
}
}
cout << split(0, 0, N, M, arr, best, pref) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
10 ms |
2004 KB |
Output is correct |
9 |
Correct |
18 ms |
3132 KB |
Output is correct |
10 |
Correct |
27 ms |
3924 KB |
Output is correct |
11 |
Correct |
23 ms |
3372 KB |
Output is correct |
12 |
Correct |
117 ms |
9300 KB |
Output is correct |
13 |
Correct |
147 ms |
13832 KB |
Output is correct |
14 |
Correct |
38 ms |
5076 KB |
Output is correct |
15 |
Correct |
181 ms |
16640 KB |
Output is correct |
16 |
Correct |
15 ms |
2356 KB |
Output is correct |
17 |
Correct |
99 ms |
7776 KB |
Output is correct |
18 |
Correct |
478 ms |
33000 KB |
Output is correct |
19 |
Correct |
789 ms |
48272 KB |
Output is correct |
20 |
Correct |
942 ms |
55380 KB |
Output is correct |