-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum_path_sum2.cpp
More file actions
45 lines (44 loc) · 958 Bytes
/
maximum_path_sum2.cpp
File metadata and controls
45 lines (44 loc) · 958 Bytes
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int Mod = 1e9 + 7;
int n;
int dp[105][105];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> dp[i][j];
}
}
for(int i = 2; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(j == 1)
{
dp[i][j] += max(dp[i-1][j-1], dp[i-1][j]);
}
else if(j == n)
{
dp[i][j] += max(dp[i-1][j+1], dp[i-1][j]);
}
else
{
dp[i][j] += max({dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1]});
}
}
}
// for(int i = 1; i <= n; i++)
// {
// for(int j = 1; j <= n; j++)
// {
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << *max_element(dp[n]+1, dp[n]+1+n) << endl;
}