/**
* 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(), k * 4));
long long mx = 0, curr = 0;
vector<int> v;
vector<bool> was(n * n);
auto Valid = [&]() {
bool ok = true;
for (int i : v) {
if (was[i]) {
ok = false;
}
was[i] = true;
}
for (int i : v) {
was[i] = false;
}
return ok;
};
bool found = false;
function<void(int, int)> Gen = [&](int i, int p) {
if (p + (int) b.size() - i < k || p > k) {
return;
}
if (i == (int) b.size()) {
if (Valid()) {
found = true;
mx = max(mx, curr);
}
return;
}
Gen(i + 1, p);
curr -= get<0>(b[i]);
v.push_back(get<1>(b[i]));
v.push_back(get<2>(b[i]));
Gen(i + 1, p + 1);
curr += get<0>(b[i]);
v.pop_back();
v.pop_back();
};
Gen(0, 0);
assert(found);
cout << sum - mx << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
7572 KB |
Output is correct |
2 |
Correct |
41 ms |
7580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1429 ms |
114688 KB |
Output is correct |
2 |
Correct |
759 ms |
114668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
795 ms |
107748 KB |
Output is correct |
2 |
Incorrect |
465 ms |
107816 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
314 ms |
28940 KB |
Output is correct |
2 |
Incorrect |
214 ms |
28948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1394 ms |
114644 KB |
Output is correct |
2 |
Incorrect |
713 ms |
114612 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
488 KB |
Output is correct |
2 |
Incorrect |
22 ms |
484 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1420 ms |
114608 KB |
Output is correct |
2 |
Incorrect |
775 ms |
114676 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
788 KB |
Output is correct |
2 |
Incorrect |
205 ms |
788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
212 KB |
Output is correct |
2 |
Correct |
64 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1580 ms |
114632 KB |
Output is correct |
2 |
Incorrect |
972 ms |
114616 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2055 ms |
596 KB |
Output is correct |
2 |
Incorrect |
1989 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2445 ms |
28928 KB |
Output is correct |
2 |
Incorrect |
2316 ms |
28980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2163 ms |
300 KB |
Output is correct |
2 |
Correct |
2065 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3454 ms |
114624 KB |
Output is correct |
2 |
Incorrect |
2800 ms |
114632 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |