#include <bits/stdc++.h>
using namespace std;
#define MAXN 2005
#define ll long long
#define FOR(i, a, b) for(ll i = a; i <= b; i++)
#define fs first
#define sc second
ll n, k, g[MAXN][MAXN], r, s = 0, m;
vector<pair<ll, pair<pair<ll, ll>, bool>>> d;
bool v[MAXN][MAXN];
void recurse(ll itr = 0, ll 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 |
106 ms |
32324 KB |
Output is correct |
2 |
Correct |
89 ms |
32336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1989 ms |
313748 KB |
Output is correct |
2 |
Correct |
1566 ms |
313996 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 |
1114 ms |
299036 KB |
Output is correct |
2 |
Correct |
1168 ms |
297220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
432 ms |
90524 KB |
Output is correct |
2 |
Correct |
408 ms |
90692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1804 ms |
313748 KB |
Output is correct |
2 |
Correct |
1977 ms |
310156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
4944 KB |
Output is correct |
2 |
Correct |
6 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1932 ms |
313580 KB |
Output is correct |
2 |
Correct |
1912 ms |
314116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
237 ms |
7752 KB |
Output is correct |
2 |
Correct |
51 ms |
7880 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 |
2085 ms |
313496 KB |
Output is correct |
2 |
Correct |
2110 ms |
313960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3250 ms |
5324 KB |
Output is correct |
2 |
Correct |
522 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4025 ms |
90532 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
2384 KB |
Output is correct |
2 |
Correct |
20 ms |
2552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4041 ms |
313744 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |