1. 标准动态规划
#include<bits/stdc++.h>
using namespace std;

int main(){
    int M,a;
    while(cin>>M){
        vector<int> p;
        while(cin>>a){
            p.emplace_back(a);
        }

        vector<int> dp(p.size()+1,0);

        dp[0] = M;//开始的家产


        int max_ =0;
        for(int i = 1; i<= p.size();i++){
            if(i>=2&&(dp[i-2]>=3)){
                dp[i] = dp[i-2] - 3 + p[i-1];
            }

            if(i>=1&& dp[i-1]>=2){
                dp[i] = max(dp[i-1] - 2 + p[i-1],dp[i]);
            }

            max_ = max(max_,dp[i]);
        }

        cout<<max_<<endl;
        return 0;

    }

}