Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int n;
- int dp[50];
- int niza[50];
- int k(int i){
- if(i>=n-1){
- return 0;
- }
- if(dp[i]!=-1){
- return dp[i];
- }
- int result=-500;
- result=max(result,k(i+2)+niza[i]);
- result=max(result,k(i+1));
- return dp[i] = result;
- }
- int k2(int i){
- if(i<=0){
- return 0;
- }
- if(dp[i]!=-1){
- return dp[i];
- }
- int result=-500;
- result=max(result,k2(i-2)+niza[i]);
- result=max(result,k2(i-1));
- return dp[i] = result;
- }
- int main()
- {
- cin>>n;
- for(int i=0; i<n; i++){
- dp[i]=-1;
- }
- for(int i=0; i<n; i++){
- cin>>niza[i];
- }
- int p=k(0);
- for(int i=0; i<n; i++){
- dp[i]=-1;
- }
- int p2=k2(n-1);
- cout << max(p, p2) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement