#include<bits/stdc++.h>
#define ll long long int

#define print(a)        for (auto x : a) cout << x << " "; cout << endl
#define print_upto(a,n)        for(int i=0;i<n;i++)    cout<<a[i]<<" "; cout<<endl
#define take(x,n)           for(int i=0;i<n;i++)  cin>>x[i];

#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"

using namespace std;

ll a[1000008];
ll dp[1000008];

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("error.txt" , "w" , stderr);
#endif

	ll n;
	cin >> n;


	for (ll i = 1; i <= n; i++) {
		cin >> a[i];
	}

	for (ll i = 1; i + 2 <= n; i++) {
		dp[i] = 1;
		if (a[i + 1] > a[i + 2] && dp[i - 1] == 0) {
			dp[i + 1] = 1;
			continue;
		} else {
			dp[i + 2] = 1;
			i++;
		}
	}

	if (dp[n - 2] == 0) {
		dp[n - 1] = 1;
		dp[n] = 1;
	}

	ll ans = 0;
	for (ll i = 0; i <= n; i++) {
		if (dp[i] == 1) {
			ans += a[i];
		}
	}

	cout << ans << "\n";


	return 0;
}