-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHcrnk_matrix_land.cpp
More file actions
executable file
·66 lines (51 loc) · 1.41 KB
/
Hcrnk_matrix_land.cpp
File metadata and controls
executable file
·66 lines (51 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
#define MA(x,y) ((x) > (y) ? (x) : (y))
using namespace std;
const int N = 4000005;
int n, m;
vector <vector <int> > a, dp;
vector <int> msl, msr, d;
void input(){
scanf("%d %d", &n, &m);
msl.resize(m+2,0);
d = msr = msl;
a.resize(n+2, d);
dp = a;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
}
void sol(){
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++){
msl[j] = MA(msl[j-1] + a[i][j],0);
}
for (int j = m; 0 < j; j--){
msr[j] = MA(msr[j+1] + a[i][j],0);
}
d[1] = dp[i-1][1] + a[i][1];
dp[i][1] = d[1] + msr[2];
for (int j = 2; j <= m; j++) {
d[j] = MA(d[j-1] + a[i][j], dp[i-1][j] + a[i][j] + msl[j-1]);
dp[i][j] = d[j] + msr[j + 1];
}
d[m] = dp[i-1][m] + a[i][m];
dp[i][m] = MA(dp[i][m], d[m] + msl[m - 1]);
for (int j = m - 1; 0 < j ; j--) {
d[j] = MA(d[j+1] + a[i][j], dp[i-1][j] + a[i][j] + msr[j+1]);
dp[i][j] = MA(dp[i][j], d[j] + msl[j - 1]);
}
}
int ans = dp[n][1];
for (int i = 2; i <= m; i++) {
ans = MA(ans, dp[n][i]);
}
printf("%d\n", ans);
}
int main() {
input();
sol();
return 0;
}