/**
* 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(), 40));
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 |
82 ms |
7520 KB |
Output is correct |
2 |
Correct |
41 ms |
7584 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 |
1544 ms |
114640 KB |
Output is correct |
2 |
Correct |
905 ms |
114568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
320 KB |
Output is correct |
2 |
Correct |
3 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
890 ms |
109132 KB |
Output is correct |
2 |
Correct |
469 ms |
107740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
30332 KB |
Output is correct |
2 |
Correct |
210 ms |
28988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1721 ms |
114680 KB |
Output is correct |
2 |
Correct |
996 ms |
114612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
827 ms |
484 KB |
Output is correct |
2 |
Correct |
828 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2225 ms |
114608 KB |
Output is correct |
2 |
Correct |
1549 ms |
114720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3875 ms |
916 KB |
Output is correct |
2 |
Incorrect |
3831 ms |
788 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
300 KB |
Output is correct |
2 |
Correct |
58 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4059 ms |
114564 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4067 ms |
596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4038 ms |
30340 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4048 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4088 ms |
114612 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |