#include <bits/stdc++.h>
using namespace std;
#define MAXN 2005
#define ll long long
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define fs first
#define sc second
ll r, s = 0;
int n, k, g[MAXN][MAXN], m;
vector<pair<ll, pair<pair<int, int>, bool>>> d;
bool v[MAXN][MAXN];
void recurse(int itr = 0, int i = -1, ll sm = 0){
if(itr == k){
r = min(r, s - sm);
return;
}
FOR(i2, i + 1, m - 1){
if(v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] || v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc]){
continue;
}
v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] = true;
v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc] = true;
recurse(itr + 1, i2, sm + d[i2].fs);
v[d[i2].sc.fs.fs][d[i2].sc.fs.sc] = false;
v[d[i2].sc.fs.fs + !d[i2].sc.sc][d[i2].sc.fs.sc + d[i2].sc.sc] = false;
}
}
int main(){
cin >> n >> k;
FOR(i, 1, n) FOR(j, 1, n) {
cin >> g[i][j];
v[i][j] = false;
s += g[i][j];
}
r = s;
FOR(i, 1, n) FOR(j, 1, n) {
if(i != n) d.push_back({g[i][j] + g[i + 1][j], {{i, j}, 0}});
if(j != n) d.push_back({g[i][j] + g[i][j + 1], {{i, j}, 1}});
}
sort(begin(d), end(d));
reverse(begin(d), end(d));
m = min((k - 1) * 7 + 1, 2 * n * (n - 1));
recurse();
cout << r << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
23220 KB |
Output is correct |
2 |
Correct |
87 ms |
23256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4880 KB |
Output is correct |
2 |
Correct |
3 ms |
4880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1762 ms |
217140 KB |
Output is correct |
2 |
Correct |
1350 ms |
217112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1026 ms |
214064 KB |
Output is correct |
2 |
Correct |
971 ms |
214316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
406 ms |
62124 KB |
Output is correct |
2 |
Correct |
385 ms |
62124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1851 ms |
217104 KB |
Output is correct |
2 |
Correct |
1766 ms |
217152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
4880 KB |
Output is correct |
2 |
Correct |
6 ms |
4880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1744 ms |
217032 KB |
Output is correct |
2 |
Correct |
1756 ms |
217076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
7500 KB |
Output is correct |
2 |
Correct |
55 ms |
7500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2148 ms |
217168 KB |
Output is correct |
2 |
Correct |
1894 ms |
217180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3541 ms |
5136 KB |
Output is correct |
2 |
Correct |
609 ms |
5136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4043 ms |
64172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2384 KB |
Output is correct |
2 |
Correct |
23 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4067 ms |
217196 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |