Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n;
- int mat[105][105];
- int dp[105][105];
- int rek(int i, int j)
- {
- if (i==(n-1))
- {
- return mat[i][j];
- }
- if(dp[i][j]!=-1)
- {
- return dp[i][j];
- }
- int zbir=-2e9;
- if(i+1<n)
- {
- zbir=max(zbir,rek(i+1,j)+mat[i][j]);
- }
- if(j+1<n && i+1<n)
- {
- zbir=max(zbir,rek(i+1,j+1)+mat[i][j]);
- }
- dp[i][j]=zbir;
- return zbir;
- }
- int main()
- {
- cin>>n;
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<(i+1);j++)
- {
- cin>>mat[i][j];
- }
- }
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<(i+1);j++)
- {
- dp[i][j]=-1;
- }
- }
- cout << rek(0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement