#include <bits/stdc++.h>
using namespace std;
#define FNAME "test"
#define int long long
const int N = 2005;
int n, k;
int grid[N][N];
int sum = 0;
vector<vector<vector<int>>> dp, mx, pmx, t;
void Task() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(9);
if (fopen(FNAME".inp","r")) {
freopen(FNAME".inp","r",stdin);
freopen(FNAME".out","w",stdout);
}
}
void Solve() {
//Your Code
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
sum += grid[i][j];
}
}
dp.resize(2, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));
mx.resize(2, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));
pmx.resize(2, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));
t.resize(2, vector<vector<int>>(n + 1, vector<int>(n + 1, 0)));
for (int dom = 1; dom <= k; dom++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 && j == 1) continue;
// (i - 1, j) -> (i, j)
if (i > 1) {
dp[0][i][j] = max(pmx[0][n][j - 1], pmx[1][n][j - 1]);
if (i - 2 >= 0) dp[0][i][j] = max(pmx[0][i - 2][n], pmx[1][i - 2][n]);
dp[0][i][j] += grid[i - 1][j] + grid[i][j];
} else dp[0][i][j] = 0;
mx[0][i][j] = max(dp[0][i][j], max(mx[0][i - 1][j], mx[0][i][j - 1]));
// (i, j - 1) -> (i, j)
if (j > 1) {
dp[1][i][j] = max(pmx[0][i - 1][n], pmx[1][i - 1][n]);
if (j - 2 >= 0) dp[1][i][j] = max(pmx[0][n][j - 2], pmx[1][n][j - 2]);
dp[1][i][j] += grid[i][j - 1] + grid[i][j];
} else dp[1][i][j] = 0;
mx[1][i][j] = max(dp[1][i][j], max(mx[1][i - 1][j], mx[1][i][j - 1]));
}
}
pmx = mx;
mx = t;
}
int res = sum - max(pmx[0][n][n], pmx[1][n][n]);
cout << res << '\n';
}
signed main() {
Task();
Solve();
cerr << "\nTime run: " << 1000*clock()/CLOCKS_PER_SEC << "ms";
return 37^37;
}
Compilation message
domino.cpp: In function 'void Task()':
domino.cpp:21:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(FNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
domino.cpp:22:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen(FNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
26456 KB |
Output is correct |
2 |
Correct |
24 ms |
26452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2672 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
521 ms |
314468 KB |
Output is correct |
2 |
Correct |
497 ms |
314488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
184596 KB |
Output is correct |
2 |
Correct |
292 ms |
184376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
156 ms |
87636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
314496 KB |
Output is correct |
2 |
Incorrect |
695 ms |
314312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
770 ms |
314496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
3164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
837 ms |
314684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
232 ms |
87844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
925 ms |
314492 KB |
Output is correct |
2 |
Incorrect |
906 ms |
314496 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |