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 (j==(0) && i==(0))
- {
- return mat[i][j];
- }
- if(dp[i][j]!=-1)
- {
- return dp[i][j];
- }
- int zbir=2e9;
- if(j-1>=0)
- {
- zbir=min(zbir,rek(i,j-1)+mat[i][j]);
- }
- if(i-1>=0)
- {
- zbir=min(zbir,rek(i-1,j)+mat[i][j]);
- }
- if(j-1>=0 && i-1>=0)
- {
- zbir=min(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<(n-i);j++)
- {
- cin>>mat[i][j];
- }
- }
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<(n-i);j++)
- {
- dp[i][j]=-1;
- }
- }
- int mini=2e9;
- int imin=0;
- int jmin=0;
- for(int i=0;i<n;i++)
- {
- if(rek(i,n-i-1)<mini)
- {
- mini=rek(i,n-i-1);
- imin=i;
- jmin=n-i-1;
- }
- }
- cout<<mini<<endl;
- cout<<imin+1<<" "<<jmin+1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement