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

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

using namespace std;

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;

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

	ll dp[n + 2] = {0};
	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 = 1; i <= n; i++) {
		if (dp[i] == 1) {
			ans += a[i];
		}
	}

	cout << ans << "\n";


	return 0;
}