Advertisement
Brick99

COCI07 Nikola

May 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <iomanip>
  7. #include <stack>
  8. #include <queue>
  9. #include <set>
  10. #include <utility>
  11. #include <map>
  12.  
  13. using namespace std;
  14.  
  15. int n;
  16. int dp[1001][1001];
  17. int a[1001];
  18. const int inf = 2e9;
  19.  
  20. int rec(int polje, int skok)
  21. {
  22.     if ( polje < 1 || polje > n || skok > n || skok < 1 ) return inf;
  23.     if ( polje == n ) return a[n];
  24.     if ( dp[polje][skok] != inf ) return dp[polje][skok];
  25.  
  26.     dp[polje][skok] = min(dp[polje][skok],min(rec(polje+skok+1,skok+1),rec(polje-skok,skok))) + a[polje];
  27.  
  28.     return dp[polje][skok];
  29. }
  30.  
  31. int main()
  32. {
  33.     cin>>n;
  34.     for (int i=1;i<=n;i++) cin>>a[i];
  35.  
  36.     for (int i=0;i<=1000;i++)
  37.         for (int j=0;j<=1000;j++)
  38.             dp[i][j]=inf;
  39.  
  40.     cout<<rec(2,1)<<endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement