Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

Comments

Please see if it's correct. I have used memoization.

#include <bits/stdc++.h>
using namespace std;

static int t[10000]; 
// ...Or any maximum size of the array.

int minCost(vector<int> &A, int pos){
    if(pos > A.size()-1){
        return t[pos] = 0;
    }
    else if(t[pos] != -1){
        return t[pos];
    }
    int c1;
	if(t[pos+2] != -1){
		c1 = t[pos+2];
	}
    else{
		c1 = minCost(A, pos+2) + A[pos];
	}
	
    if(pos>0 && t[pos-1] != -1){
		int c2;
		if(t[pos-1] != -1){
			c2 = t[pos-1];
		}
		else{
        	c2 = minCost(A, pos-1) + A[pos];
		}
        return t[pos] = min(c1, c2);
    }
    else{
        return t[pos] = c1;
    }
}

int main(){
    memset(t, -1, sizeof(t));
    int N;
    cin>>N;
    vector<int> A(N);
 
 	for(int i=0; i<N; i++){
        cin>>A[i];
    }
    cout<<minCost(A, 0);
    return 0;
}