#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int MOD = 1e9 + 7;
void solve(){
int n, m; cin >> n >> m;
vector<vector<int>> table(n, vector<int>(m));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) cin >> table[i][j];
}
ll ans = 0;
function<void(int, int, int, int)> DFS = [&](int lx, int rx, int ly, int ry){
if(rx - lx == 0 && ry - ly == 0) return;
int mn = 1e9;
pair<int, int> cut;
for(int i = ly; i <= ry; i++){
for(int j = lx; j <= rx; j++) ans += table[i][j];
}
for(int i = ly; i < ry; i++){
int up = 0;
int down = 0;
for(int j = ly; j <= i; j++){
for(int q = lx; q <= rx; q++) up += table[j][q];
}
for(int j = i + 1; j <= ry; j++){
for(int q = lx; q <= rx; q++) down += table[j][q];
}
if(abs(up - down) < mn){
mn = abs(up - down);
cut = {i, 0};
}
}
for(int i = lx; i < rx; i++){
int left = 0;
int right = 0;
for(int j = lx; j <= i; j++){
for(int q = ly; q <= ry; q++) left += table[q][j];
}
for(int j = i + 1; j <= rx; j++){
for(int q = ly; q <= ry; q++) right += table[q][j];
}
if(abs(left - right) < mn){
mn = abs(left - right);
cut = {i, 1};
}
}
if(cut.second == 0) {DFS(lx, rx, ly, cut.first); DFS(lx, rx, cut.first + 1, ry);}
else {DFS(lx, cut.first, ly, ry); DFS(cut.first + 1, rx, ly, ry);}
};
DFS(0, m - 1, 0, n - 1);
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("inpt.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//int tt; cin >> tt;
int tt = 1;
for(int t = 1; t <= tt; t++){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |