/**
* author: wxhtzdy
* created: 24.06.2022 09:28:57
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
long long sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum += a[i][j];
}
}
vector<tuple<int, int, int>> b;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + 1 < n) {
b.emplace_back(-a[i][j] - a[i + 1][j], i * n + j, (i + 1) * n + j);
}
if (j + 1 < n) {
b.emplace_back(-a[i][j] - a[i][j + 1], i * n + j, i * n + j + 1);
}
}
}
sort(b.begin(), b.end());
b.resize(min((int) b.size(), 41));
long long mx = 0, curr = 0;
vector<int> cnt(n * n);
function<void(int, int)> Gen = [&](int i, int p) {
if (p > k || p + (int) b.size() - i < k) {
return;
}
if (i == (int) b.size()) {
mx = max(mx, curr);
return;
}
Gen(i + 1, p);
if (cnt[get<1>(b[i])] == 0 && cnt[get<2>(b[i])] == 0) {
curr -= get<0>(b[i]);
cnt[get<1>(b[i])]++;
cnt[get<2>(b[i])]++;
Gen(i + 1, p + 1);
cnt[get<1>(b[i])]--;
cnt[get<2>(b[i])]--;
curr += get<0>(b[i]);
}
};
Gen(0, 0);
cout << sum - mx << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
8076 KB |
Output is correct |
2 |
Correct |
47 ms |
8068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1487 ms |
125516 KB |
Output is correct |
2 |
Correct |
793 ms |
125632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
869 ms |
107776 KB |
Output is correct |
2 |
Correct |
472 ms |
107708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
31572 KB |
Output is correct |
2 |
Correct |
194 ms |
31572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1568 ms |
125564 KB |
Output is correct |
2 |
Correct |
755 ms |
125552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
690 ms |
476 KB |
Output is correct |
2 |
Correct |
135 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2183 ms |
125560 KB |
Output is correct |
2 |
Correct |
880 ms |
125640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3217 ms |
788 KB |
Output is correct |
2 |
Incorrect |
275 ms |
788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4070 ms |
125564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4057 ms |
596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4051 ms |
31572 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
126 ms |
296 KB |
Output is correct |
2 |
Correct |
104 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4094 ms |
125520 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |