/**
* 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;
};
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()) {
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);
cout << sum - mx << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
7624 KB |
Output is correct |
2 |
Correct |
37 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 |
1348 ms |
114572 KB |
Output is correct |
2 |
Correct |
722 ms |
114612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
783 ms |
107732 KB |
Output is correct |
2 |
Incorrect |
417 ms |
107764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
318 ms |
29000 KB |
Output is correct |
2 |
Incorrect |
200 ms |
29040 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1355 ms |
114612 KB |
Output is correct |
2 |
Incorrect |
706 ms |
114660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
488 KB |
Output is correct |
2 |
Incorrect |
24 ms |
468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1373 ms |
114616 KB |
Output is correct |
2 |
Incorrect |
751 ms |
114704 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
788 KB |
Output is correct |
2 |
Incorrect |
213 ms |
788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
212 KB |
Output is correct |
2 |
Correct |
61 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1543 ms |
114616 KB |
Output is correct |
2 |
Incorrect |
912 ms |
114644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1968 ms |
596 KB |
Output is correct |
2 |
Incorrect |
1968 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2298 ms |
28980 KB |
Output is correct |
2 |
Incorrect |
2131 ms |
28936 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1972 ms |
296 KB |
Output is correct |
2 |
Correct |
1952 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3259 ms |
114664 KB |
Output is correct |
2 |
Incorrect |
2628 ms |
114660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |