#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <sstream>
#include <stack>
#include <cassert>
#include <string.h>
#include <ctime>

#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795
#define sqr(x) (x)*(x)
#define forn(i, n) for(int i = 0; i < n; ++i)
#define ALL(x) x.begin(), x.end()
#define sz(x) int((x).size())
#define X first
#define Y second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
typedef pair<int,int> pii;
const int INF = 2147483647;
const ll LLINF = 9223372036854775807LL;

struct Response {
    int fr, to;
    vector<string> vs;

    Response(int fr, int to, const vector<string>& vs): fr(fr), to(to), vs(vs) {}
};

Response ask(int l, int r) {
    cout << "? " << l << " " << r << endl;
    vector<string> result;
    for (int i = l; i <= r; ++i)
        for (int j = i; j <= r; ++j) {
            string curs;
            cin >> curs;
            result.push_back(curs);
        }
    return Response(l-1, r-1, result);
}

void fin(string s) {
    cout << "! " << s << endl;
    exit(0);
}

struct St {
    multiset<char> ms;

    St() = default;

    explicit St(const vector<char>& v): ms(ALL(v)) {}

    St inters(const St& oth) const {
        vector<char> res(sz(ms));
        res.erase(set_intersection(ALL(ms), ALL(oth.ms), res.begin()), res.end());
        return St(res);
    }

    St subtract(const St& oth) const {
        vector<char> res(sz(ms));
        res.erase(set_difference(ALL(ms), ALL(oth.ms), res.begin()), res.end());
        return St(res);
    }

    bool operator==(const St& oth) const {
        return ms == oth.ms;
    }

    bool operator!=(const St& oth) const {
        return ms != oth.ms;
    }

    bool operator<(const St& oth) const {
        return ms < oth.ms;
    }
};

int n;
const int maxn = 110;
char fc[maxn];

void update_info(int wlen, const Response& resp) {
    if (wlen >= resp.to - resp.fr + 1) return;
    St known;
    for (int i = resp.fr; i + wlen - 1 <= resp.to; ++i) {
        int wfr = i;
        int wto = i + wlen - 1;
        for (int j = 0; j <= resp.to; ++j) {
            if (j >= wfr && j <= wto) continue;
            if (fc[j] == 0) continue;
            known.ms.insert(fc[j]);
        }
    }

    string full_str = *max_element(ALL(resp.vs), [](const string& x, const string& y) {return sz(x) < sz(y);});
    St from_req;
    for (const string& s: resp.vs) {
        if (sz(s) != wlen) continue;
        for (char c: St(vector<char>(ALL(full_str))).subtract(St(vector<char>(ALL(s)))).ms)
            from_req.ms.insert(c);
    }

    auto rm = from_req.subtract(known).ms;
    if (sz(rm) != 1) return;
    char curc = *rm.begin();
    for (int i = resp.fr; i <= resp.to; ++i) {
        if (i <= resp.fr + wlen - 1 && i >= resp.to - wlen + 1) continue;
        if (fc[i] == 0) fc[i] = curc;
    }
}

int main()
{
#ifdef _LOCAL_
    freopen("../input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    cin >> n;

    if (n < 3) {
        Response r1 = ask(1, n);

        if (n == 1) fin(r1.vs[0]);

        Response r2 = ask(1, n - 1);

        string s1 = *max_element(ALL(r1.vs), [](const string &x, const string &y) { return sz(x) < sz(y); });
        string s2 = *max_element(ALL(r2.vs), [](const string &x, const string &y) { return sz(x) < sz(y); });
        fc[n - 1] = *St(vector<char>(ALL(s1))).subtract(St(vector<char>(ALL(s2)))).ms.begin();

        for (int wlen = n - 1; wlen >= 1; --wlen) {
            update_info(wlen, r2);
            update_info(wlen, r1);
        }

        string res(fc);
        fin(res);
        return 0;
    }

    int m1 = n / 2;
    Response r1 = ask(1, m1 + 1);
    Response r2 = ask(1, m1);
    string s1 = *max_element(ALL(r1.vs), [](const string &x, const string &y) { return sz(x) < sz(y); });
    string s2 = *max_element(ALL(r2.vs), [](const string &x, const string &y) { return sz(x) < sz(y); });
    fc[m1] = *St(vector<char>(ALL(s1))).subtract(St(vector<char>(ALL(s2)))).ms.begin();

    for (int wlen = m1; wlen >= 1; --wlen) {
        update_info(wlen, r2);
        update_info(wlen, r1);
    }

    Response r3 = ask(1, n);
    for (int wlen = n-1; wlen >= 1; --wlen) {
        update_info(wlen, r3);
    }

    string res(fc);
    fin(res);
    return 0;
}


/////////////////////////   _LeMur_
#define _CRT_SECURE_NO_WARNINGS
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <bitset>
#include <cstdio>
#include <vector>
#include <string>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <map>
#include <set>

using namespace std;

const int N = 105;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;

int n;

vector <string> g;

void query(int l,int r){
    cout << "? " << l << " " << r << endl;
    cout.flush();

    g.clear();

    for(int i=l;i<=r;i++){
        for(int j=i;j<=r;j++){
            string s;
            cin >> s;
            sort(s.begin() , s.end());
            g.push_back(s);
        }
    }
}

vector <char> mas[N];
string answ;

int cnt[30] , sz[40];
int now[30];

bool used[100005];
vector <string> A;

bool cmp(string s1,string s2){
    return (int)s1.size() < (int)s2.size();
}

int main(){
    cin >> n;

    if(n <= 3){
        for(int i=1;i<=n;i++){
            query(i , i);
            answ += g[0];
        }
        cout << "! " << answ << endl;
        cout.flush();
        return 0;
    }

    for(int i=0;i<n;i++){
        answ += '0';
    }
    query(1 , n);

    for(int i=0;i<(int)g.size();i++){
        if((int)g[i].size() == n){
            for(int j=0;j<(int)g[i].size();j++){
                sz[ g[i][j] - 'a' ]++;
            }
        }
    }

    for(int i=1;i*2<=n;i++){

        for(int j=0;j<26;j++){
            cnt[j] = 0;
            now[j] = sz[j];
        }

        for(int j=0;j<(int)g.size();j++){
            if((int)g[j].size() == i + 1){
                for(int h=0;h<(int)g[j].size();h++){
                    int id = g[j][h] - 'a';
                    ++cnt[id];
                }
            }
        }

        for(int j=1;j<i;j++){
            for(int h=0;h<(int)mas[j].size();h++){
                --now[ mas[j][h] - 'a' ];
                cnt[ mas[j][h] - 'a' ] -= j;
            }
        }

        for(int j=0;j<26;j++){
            int L = now[j] * (i + 1);
            L -= cnt[j];
            while(L--){
                mas[i].push_back(char(j + 'a'));
            }
        }
    }

    int k = (n + 1) / 2;

    query(2 , k);
    vector <string> z = g;
    query(1 , k);

    for(int i=0;i<(int)z.size();i++){
        for(int j=0;j<(int)g.size();j++){
            if(used[j])continue;
            if(g[j] == z[i]){
                used[j] = true;
                break;
            }
        }
    }

    for(int i=0;i<(int)g.size();i++){
        if(used[i] == false){
            A.push_back(g[i]);
        }
    }

    sort(A.begin() , A.end() , cmp);

    for(int i=0;i<26;i++) now[i] = 0;
    for(int i=0;i<(int)A.size();i++){
        for(int j=0;j<26;j++){
            cnt[j] = now[j];
        }
        for(int j=0;j<(int)A[i].size();j++){
            int x = A[i][j] - 'a';
            if(cnt[x] == 0){
                now[x]++;
                answ[i] = char(x + 'a');
                break;
            }
            --cnt[x];
        }
    }

    for(int i=1;i<=n/2;i++){
        if(answ[i - 1] == mas[i][0])
            answ[n - i] = mas[i][1];
        else
            answ[n - i] = mas[i][0];
    }

    cout << "! " << answ << endl;
    return 0;
}
/*
4
a
a
b
c
aa
ab
bc
aab
abc
aabc
a
a
aa
a
*/


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

const int N = 103;
int n, cnt[N][26];
map<string, int> fd;
vector<string> tmp;
string ans;

int len(int x) {return x * (x + 1) / 2;}
int main() {
	cin >> n;
	if(n <= 3) {
		for(int i = 1; i <= n; i++) {
			cout << "? " << i << " " << i << endl;
			string tmp; cin >> tmp; ans += tmp;
		}
		cout << "! " << ans << endl;
		return 0;
	}
			{//get s[1...n/2]
	cout << "? 1 " << n / 2 - 1 << endl;
	for(int i = 1; i <= len(n / 2 - 1); i++) {
		string s; cin >> s; sort(s.begin(), s.end());
		fd[s]++;
	}
	cout << "? 1 " << n / 2 << endl;
	for(int i = 1; i <= len(n / 2); i++) {
		string s; cin >> s; sort(s.begin(), s.end());
		fd[s]--; if(fd[s] < 0) tmp.push_back(s);
	}
	sort(tmp.begin(), tmp.end(), [&](const string a, const string b){
		return a.size() < b.size();
	});
	// cout << "tmp:" << tmp[0] << " " << tmp[1] << endl;
	vector<int> cnt(26, 0);
	for(auto str : tmp) {
		vector<int> tcnt(26, 0);
		for(auto ch : str) tcnt[ch - 'a']++;
		for(int i = 0; i < 26; i++) if(tcnt[i] != cnt[i]) {
			ans = (char)(i + 'a') + ans; cnt[i]++; break;
		}
	}
			}
	// cout << "ans:" << ans << endl;
	//get s[1...n]
	cout << "? 1 " << n << endl;
	for(int i = 1, sz; i <= len(n); i++) {
		string s; cin >> s; sz = s.size();
		for(auto ch : s) cnt[sz][ch - 'a']++;
	}
	for(int i = (n - 1) / 2; i >= 0; i--) {
		for(int j = 0; j < 26; j++) cnt[i + 1][j] -= cnt[i][j];
		for(int j = i + 1; j < n - i; j++) cnt[i + 1][ans[j - 1] - 'a']--;
		for(int j = 0; j < 26; j++) if(cnt[i + 1][j]) {
			ans += (char)(j + 'a'); break;
		}
	}
	cout << "! " << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=105;
typedef vector<int>V;
typedef vector<multiset<vector<int>>>V2;
char rc[N];
inline V2 ask(int l,int r){
	printf("? %d %d\n",l,r);fflush(stdout);
	int n=r-l+1,tot=n*(n+1)>>1,i;
	V2 ret;
	ret.resize(n);
//vector<string>ve;
//for(int i=l;i<=r;++i){string s="";for(int j=i;j<=r;++j)s+=rc[j],ve.push_back(s);}
//random_shuffle(ve.begin(),ve.end());
	for(;tot--;){
		static char c[105];
		vector<int>v(26);
		scanf("%s",c);
//memcpy(c,&ve[tot][0],ve[tot].size());c[ve[tot].size()]=0;
		for(i=0;c[i];++i)++v[c[i]-'a'];
		ret[i-1].insert(v);
	}
	return ret;
}
inline bool contain(V a,V b){
	for(int i=0;i<26;++i)if(a[i]<b[i])return 0;
	return 1;
}
V jian(V a,V b){assert(contain(a,b));V ret(26);for(int i=0;i<26;++i)ret[i]=a[i]-b[i];return ret;}
inline V2 jian(V2 A,V2 B){for(int i=0;i<B.size();++i)for(auto u:B[i])A[i].erase(A[i].find(u));return A;}
V toV(string s){V ret(26);for(char c:s)++ret[c-'a'];return ret;}
string toS(V v){string s;for(int i=0;i<26;++i)for(int j=0;j<v[i];++j)s.push_back(i+'a');return s;}
int n,i,j,m;
int main(){
	scanf("%d",&n);
//srand(time(0));for(i=1;i<=n;++i)rc[i]=rand()%10+'a';
	if(n<=3){
		char ans[9];
		for(i=1;i<=n;++i)printf("? %d %d\n",i,i),fflush(stdout),scanf("%s",ans+i);
		printf("! %s\n",ans+1),fflush(stdout);return 0;
	}
	m=n+1>>1;
	auto A=ask(1,n),B=ask(1,m),C=ask(2,m);
	B=jian(B,C);
	string ans;ans.resize(n);
	ans[0]=toS(*B[0].begin())[0];
	for(i=1;i<m;++i)ans[i]=toS(jian(*B[i].begin(),*B[i-1].begin()))[0];
	V a=jian(*A[n-1].begin(),*B[m-1].begin());
	for(i=n;i>m;--i){
		V b=a;
		for(j=1;j<m;++j)++b[ans[j]-'a'];
		for(j=i;j<=n;++j){
			A[i-2].erase(A[i-2].find(b));
			if(j==n)break;
			--b[ans[j-i+1]-'a'];++b[ans[j]-'a'];
		}
		ans[i-1]=toS(jian(*A[i-1].begin(),*A[i-2].begin()))[0];
		--a[ans[i-1]-'a'];
	}
	for(i=0;i<n;++i)assert(ans[i]==rc[i+1]);
	cout<<"! "<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=105;
typedef vector<int>V;
typedef vector<multiset<vector<int>>>V2;
char rc[N];
inline V2 ask(int l,int r){
	printf("? %d %d\n",l,r);fflush(stdout);
	int n=r-l+1,tot=n*(n+1)>>1,i;
	V2 ret;
	ret.resize(n);
//vector<string>ve;
//for(int i=l;i<=r;++i){string s="";for(int j=i;j<=r;++j)s+=rc[j],ve.push_back(s);}
//random_shuffle(ve.begin(),ve.end());
	for(;tot--;){
		static char c[105];
		vector<int>v(26);
		scanf("%s",c);
//memcpy(c,&ve[tot][0],ve[tot].size());c[ve[tot].size()]=0;
		for(i=0;c[i];++i)++v[c[i]-'a'];
		ret[i-1].insert(v);
	}
	return ret;
}
inline bool contain(V a,V b){
	for(int i=0;i<26;++i)if(a[i]<b[i])return 0;
	return 1;
}
V jian(V a,V b){assert(contain(a,b));V ret(26);for(int i=0;i<26;++i)ret[i]=a[i]-b[i];return ret;}
inline V2 jian(V2 A,V2 B){for(int i=0;i<B.size();++i)for(auto u:B[i])A[i].erase(A[i].find(u));return A;}
V toV(string s){V ret(26);for(char c:s)++ret[c-'a'];return ret;}
string toS(V v){string s;for(int i=0;i<26;++i)for(int j=0;j<v[i];++j)s.push_back(i+'a');return s;}
int n,i,j,m;
int main(){
	scanf("%d",&n);
//srand(time(0));for(i=1;i<=n;++i)rc[i]=rand()%10+'a';
	if(n<=3){
		char ans[9];
		for(i=1;i<=n;++i)printf("? %d %d\n",i,i),fflush(stdout),scanf("%s",ans+i);
		printf("! %s\n",ans+1),fflush(stdout);return 0;
	}
	m=n+1>>1;
	auto A=ask(1,n),B=ask(1,m),C=ask(2,m);
	B=jian(B,C);
	string ans;ans.resize(n);
	ans[0]=toS(*B[0].begin())[0];
	for(i=1;i<m;++i)ans[i]=toS(jian(*B[i].begin(),*B[i-1].begin()))[0];
	V a=jian(*A[n-1].begin(),*B[m-1].begin());
	for(i=n;i>m;--i){
		V b=a;
		for(j=1;j<m;++j)++b[ans[j]-'a'];
		for(j=i;j<=n;++j){
			A[i-2].erase(A[i-2].find(b));
			if(j==n)break;
			--b[ans[j-i+1]-'a'];++b[ans[j]-'a'];
		}
		ans[i-1]=toS(jian(*A[i-1].begin(),*A[i-2].begin()))[0];
		--a[ans[i-1]-'a'];
	}
//	for(i=0;i<n;++i)assert(ans[i]==rc[i+1]);
	cout<<"! "<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef gp_hash_table<int, null_type,hash<int>, equal_to<int>, direct_mask_range_hashing<int>, linear_probe_fn<>,
    hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>,true>> hash_set;
typedef gp_hash_table<int,null_type> hash_map;

#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define all(a) a.begin(),a.end()
#define println printf("\n");
#define readln(x) getline(cin,x);
#define pb push_back
//#define endl "\n"
#define INT_INF 0x3f3f3f3f
#define LL_INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-9
#define MOD 1000000007
#define MOD2 1494318097
#define SEED 131
#define mp make_pair
#define fastio cin.tie(0); cin.sync_with_stdio(0);

#define MAXN 105

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef pair<int,pii> triple;
typedef int8_t byte;

const ld PI = (ld)4*atanl(1);

mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());

int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}

ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}

int len,freq[MAXN][30];
char res[MAXN];
vector<int> pairs[MAXN];
multiset<string> f,s,qq;
vector<pair<int,string>> ret;

inline multiset<string> guess(int l, int r){
    cout << "? " << l << " " << r << endl;
    int req = (r-l+1)+(r-l+1)*(r-l)/2;
    qq.clear();
    while(req--){
        string s; cin >> s;
        sort(all(s));
        qq.insert(s);
    }
    return qq;
}

inline void firstHalf(){
    f = guess(1,len/2);
    s = guess(2,len/2);
    for(string check:s)
        if(f.count(check))
            f.erase(f.find(check));
    for(string check:f)
        ret.pb(mp((int)check.size(),check));
    sort(all(ret));
    for(int i=0; i<ret.size(); i++){
        if(i == 0) res[i+1] = ret[i].second[0];
        else{
            int pr = (int)ret[i].second.size()-1;
            for(int k=0; k<ret[i-1].second.size(); k++){
                if(ret[i-1].second[k] != ret[i].second[k]){
                    pr = k;
                    break;
                }
            }
            res[i+1] = ret[i].second[pr];
        }
    }
}

int main(){
    fastio; cin >> len;
    if(len <= 3){
        string res = "";
        for(int i=1; i<=len; i++){
            s = guess(i,i);
            res+=*s.begin();
        }
        cout << "! " << res << endl;
        return 0;
    }
    firstHalf();
    s = guess(1,len);
    for(string check:s){
        for(char c:check)
            freq[(int)check.size()][c-'a']++;
    }
    for(int i=2; i<=(len+1)/2; i++){
        for(int k=0; k<26; k++){
            int rem = freq[i][k]-freq[i-1][k];
            while(rem--) pairs[i].pb(k);
        }
    }
    for(int i=2; i<=(len+1)/2; i++){
        if(i == len-i+1) res[i] = pairs[i][0]+'a';
        else{
            if(pairs[i][0] == res[i]-'a') res[len-i+1] = pairs[i][1]+'a';
            else res[len-i+1] = pairs[i][0]+'a';
        }
    }
    for(int i=2; i<len; i++)
        freq[len][res[i]-'a']--;
    for(int i=0; i<26; i++)
        while(freq[len][i]--)
            pairs[1].pb(i);
    if(pairs[1][0] == res[1]-'a') res[len] = pairs[1][1]+'a';
    else res[len] = pairs[1][0]+'a';
    cout << "! ";
    for(int i=1; i<=len; i++)
        cout << res[i];
    cout << endl;
}
/*
4
? 1 2
a
a
aa
? 2 2
a
? 1 4
a
a
b
c
aa
ba
cb
baa
bac
aacb
 */


import java.util.*;
public class A{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        int pre=a[n-1];
        long sum=a[n-1];
        boolean flag=true;
        for(int i=n-2;i>=0;i--){
            if(pre-1>=a[i]){
                sum+=a[i];
            }
            else if(pre!=1){
                a[i]=pre-1;
                sum+=(pre-1);
            }
            else
                a[i]=1;
            pre=a[i];
            //System.out.println(sum);
        }
        System.out.println(sum);
    }
}


import java.util.*;
public class A{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        int pre=0;
        long sum=0;
        boolean flag=true;
        for(int i=n-1;i>=0;i--){
            if((sum+pre-1)<a[i])
                sum=a[i];
            else if(pre==1 ){
                a[i]=1;
            }
            else{
                if(pre<=a[i]){
                    a[i]=pre-1;
                    sum+=a[i];
                }
                else
                    sum+=a[i];
            }
                pre=a[i];

        }
        System.out.println(sum);
    }
}


import java.util.*;
public class A{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        int pre=0;
        long sum=0;
        boolean flag=true;
        for(int i=n-1;i>=0;i--){
            if((sum+pre-1)<a[i])
                sum=a[i];
            else if(pre==1 ){
                a[i]=1;
            }
            else{
                if(pre<a[i])
                    sum+=pre-1;
                else
                sum+=a[i];
            }
                pre=a[i];

        }
        System.out.println(sum);
    }
}


import java.io.*;
import java.util.*;

public class cbr{

    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //String line1=br.readLine();
        Scanner sc =new Scanner(System.in);
        int ans=0;
        int i,j,k,l,p;
        int line1=sc.nextInt();
        int[] a=new int[line1];
        for(i=0;i<line1;i++)
            a[i]=sc.nextInt();
        ans=a[line1-1];
        for(j=line1-1;j>0;j--)
        {
          i=a[j-1];
            if(a[j]>i)
                ans=ans+i;
            else
            {
              //while(i>=a[j])
              //{
                // i=i-1;
              //}
                i=a[j]-1;
                a[j-1]=i;
                if(i==0)
                    ans=ans+0;
                else
                ans=ans+i;
            }
            if(i==0)
                break;

        }
    System.out.println(ans);

    }
}


import java.io.*;
import java.util.*;

public class cbr{

    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //String line1=br.readLine();
        Scanner sc =new Scanner(System.in);
        int ans=0;
        int i,j,k,l,p;
        int line1=sc.nextInt();
        int[] a=new int[line1];
        for(i=0;i<line1;i++)
            a[i]=sc.nextInt();
        ans=a[line1-1];
        for(j=line1-1;j>0;j--)
        {
          i=a[j-1];
            if(a[j]>i)
                ans=ans+i;
            else
            {
              while(i>=a[j])
              {
                 i=i-1;
              }
                a[j-1]=i;
                if(i==0)
                    ans=ans+0;
                else
                ans=ans+i;
            }
            if(i==0)
                break;

        }
    System.out.println(ans);

    }
}


import java.io.*;
import java.util.*;

public class cbr{

    public static void main(String[] args)throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //String line1=br.readLine();
        Scanner sc =new Scanner(System.in);
        long ans=0;
        int i,j,k,l,p;
        int line1=sc.nextInt();
        int[] a=new int[line1];
        for(i=0;i<line1;i++)
            a[i]=sc.nextInt();
        ans=a[line1-1];
        for(j=line1-1;j>0;j--)
        {
          i=a[j-1];
            if(a[j]>i)
                ans=ans+i;
            else
            {
              //while(i>=a[j])
              //{
                // i=i-1;
              //}
                i=a[j]-1;
                a[j-1]=i;
                if(i==0)
                    ans=ans+0;
                else
                ans=ans+i;
            }
            if(i==0)
                break;

        }
    System.out.println(ans);

    }
}


import java.util.Scanner;

public class CFChocolates {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		s.nextLine();
		String line = s.nextLine();
		String [] values = line.split(" ");
		int count = Integer.parseInt(values[n-1]);
		for (int i = n-2; i >=0; i--) {
			int current = Integer.parseInt(values[i]);
			int previous =Integer.parseInt(values[i+1]);
			if(previous==0) {
				break;
			}else {
				if(current<previous) {
					count+=current;
				}else {
					count+=Integer.parseInt(values[i+1])-1;
					values[i]=previous-1+"";
				}
			}

		}
		System.out.println(count);

	}

}


import java.util.Scanner;

public class CFChocolates {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		s.nextLine();
		String line = s.nextLine();
		String [] values = line.split(" ");
		int count = Integer.parseInt(values[n-1]);
		for (int i = n-2; i >=0; i--) {
			int current = Integer.parseInt(values[i]);
			int previous =Integer.parseInt(values[i+1]);
			if(current<previous) {
				count+=current;
			}else {
				if(previous-1>0) {
					count+=Integer.parseInt(values[i+1])-1;
					values[i]=previous-1+"";
				}else {
					values[i]="0";
				}

			}
		}
		System.out.println(count);

	}

}


import java.util.Scanner;

public class CFChocolates {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		long [] values = new long[n];
		for (int i = 0; i < n; i++) {
			values[i]=s.nextInt();
		}

		long count = (values[(n-1)]);
		for (int i = n-2; i >=0; i--) {
			long current = (values[i]);
			long previous =(values[i+1]);
			if(previous==0) {
				break;
			}else {
				if(current<previous) {
					count+=current;
				}else {
					count+=(values[i+1])-1;
					values[i]=previous-1;
				}
			}

		}
		System.out.println(count);

	}

}


import java.util.Scanner;

public class CFChocolates {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		s.nextLine();
		String line = s.nextLine();
		String [] values = line.split(" ");
		int count = Integer.parseInt(values[n-1]);
		for (int i = n-2; i >=0; i--) {
			int current = Integer.parseInt(values[i]);
			int previous =Integer.parseInt(values[i+1]);
			if(current<previous) {
				count+=current;
			}else {
				count+=Integer.parseInt(values[i+1])-1;
				values[i]=previous-1+"";
			}
		}
		System.out.println(count);

	}

}


import java.util.Scanner;

public class CFChocolates {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int [] values = new int[n];
		for (int i = 0; i < n; i++) {
			values[i]=s.nextInt();
		}

		int count = (values[n-1]);
		for (int i = n-2; i >=0; i--) {
			int current = (values[i]);
			int previous =(values[i+1]);
			if(previous==0) {
				break;
			}else {
				if(current<previous) {
					count+=current;
				}else {
					count+=(values[i+1])-1;
					values[i]=previous-1;
				}
			}

		}
		System.out.println(count);

	}

}


//package lab;

import java.util.*;

import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	/** Class for buffered reading int and double values */
	static class Reader {
	    static BufferedReader reader;
	    static StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    static void init(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    /** get next word */
	    static String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    static int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }

	    static long nextlong() throws IOException {
	        return Long.parseLong( next() );
	    }

	    static double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	}

	public static void main(String[] args) throws IOException {

		Reader.init(System.in);
		int n=Reader.nextInt();
		long a[]=new long[n],ans=0,tmp=a[n-1];
		for(int i=0;i<n;i++)
			a[i]=Reader.nextlong();
		ans+=a[n-1];
		for(int i=n-2;i>=0;i--)
		{
			//System.out.println(i+" "+a[i]+" "+ans);
			if(a[i]<a[i+1])
			{
				ans+=a[i];
				tmp=a[i];
			}
			else if(tmp>1)
			{
				ans+=tmp-1;
				tmp-=1;
			}
			else
				break;
		}
		System.out.println(ans);
	}
}


//package lab;

import java.util.*;

import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	/** Class for buffered reading int and double values */
	static class Reader {
	    static BufferedReader reader;
	    static StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    static void init(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    /** get next word */
	    static String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    static int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }

	    static long nextlong() throws IOException {
	        return Long.parseLong( next() );
	    }

	    static double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	}

	public static void main(String[] args) throws IOException {

		Reader.init(System.in);
		int n=Reader.nextInt();
		long a[]=new long[n],ans=0,tmp=0;
		for(int i=0;i<n;i++)
			a[i]=Reader.nextlong();
		ans+=a[n-1];
		tmp=a[n-1];
		for(int i=n-2;i>=0;i--)
		{
			//System.out.println(i+" "+a[i]+" "+ans);
			if(a[i]<tmp)
			{
				ans+=a[i];
				tmp=a[i];
			}
			else if(tmp>1)
			{
				ans+=tmp-1;
				tmp-=1;
			}
			else
				break;
		}
		System.out.println(ans);
	}
}


//package lab;

import java.util.*;

import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	/** Class for buffered reading int and double values */
	static class Reader {
	    static BufferedReader reader;
	    static StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    static void init(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    /** get next word */
	    static String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    static int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }

	    static long nextlong() throws IOException {
	        return Long.parseLong( next() );
	    }

	    static double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	}

	public static void main(String[] args) throws IOException {

		Reader.init(System.in);
		int n=Reader.nextInt();
		long a[]=new long[n],ans=0,tmp=0;
		for(int i=0;i<n;i++)
			a[i]=Reader.nextlong();
		ans+=a[n-1];
		tmp=a[n-1];
		for(int i=n-2;i>=0;i--)
		{
			//System.out.println(i+" "+a[i]+" "+ans);
			if(a[i]<a[i+1])
			{
				ans+=a[i];
				tmp=a[i];
			}
			else if(tmp>1)
			{
				ans+=tmp-1;
				tmp-=1;
			}
			else
				break;
		}
		System.out.println(ans);
	}
}


import java.util.*;
public class B {
    static long n,a[]=new long[200007];

    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        long Ans=0;

        n=sc.nextInt();
        for (int i=1;i<=n;i++){
            a[i]=sc.nextLong();
        }

        long cur=(int)1e18;

        for (int i=(int)n;i>=1;i--){
            cur=Math.min(cur-1,a[i]);
            cur=Math.max(0,cur);
            Ans+=cur;
        }
        System.out.println(Ans);
    }
}


import java.util.*;
public class B {
    static long n,a[]=new long[200007];

    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        int Ans=0;

        n=sc.nextInt();
        for (int i=1;i<=n;i++){
            a[i]=sc.nextInt();
        }

        long cur=(int)1e18;

        for (int i=(int)n;i>=1;i--){
            cur=Math.min(cur-1,a[i]);
            cur=Math.max(0,cur);
            Ans+=cur;
        }
        System.out.println(Ans);
    }
}


import java.util.*;
public class B {
    static int n,a[]=new int[200007];

    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        int Ans=0;

        n=sc.nextInt();
        for (int i=1;i<=n;i++){
            a[i]=sc.nextInt();
        }

        int cur=(int)1e18;

        for (int i=n;i>=1;i--){
            cur=Math.min(cur-1,a[i]);
            cur=Math.max(0,cur);
            Ans+=cur;
        }
        System.out.println(Ans);
    }
}


import java.util.*;
import java.io.*;
public class chocolates {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main (String [] args) throws IOException{
        int n = readInt();
        long[]arr = new long[n];
        for(int i=0; i<n; i++) {
            arr[i] = readLong();
        }
        long answ = 0;
        long max = arr[n-1]+1;
        for(int i=n-1; i>=0; i--) {
            long num = arr[i];
            max = Math.min(num,max-1);
            if(max <= 0) break;
            answ += max;
        }
        System.out.println(answ);
    }
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
    static long readLong () throws IOException {
        return Long.parseLong(next());
    }
    static int readInt () throws IOException {
        return Integer.parseInt(next());
    }
    static double readDouble () throws IOException {
        return Double.parseDouble(next());
    }
    static String readLine () throws IOException {
        return br.readLine().trim();
    }
}


import java.util.*;
import java.io.*;
public class chocolates {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main (String [] args) throws IOException{
        int n = readInt();
        int[]arr = new int[n];
        for(int i=0; i<n; i++) {
            arr[i] = readInt();
        }
        long answ = 0;
        for(int i=n-1; i>=0; i--) {
            int max = 0;
            max += arr[i];
            int max2 = arr[i];
            for(int j=i-1; j>=0; j--) {
                max2 = Math.min(arr[j],max2-1);
                if(max2 <= 0) break;
                max += max2;

            }
            //System.out.println(max);
            answ =Math.max(max,answ);
        }
        System.out.println(answ);
    }
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
    static long readLong () throws IOException {
        return Long.parseLong(next());
    }
    static int readInt () throws IOException {
        return Integer.parseInt(next());
    }
    static double readDouble () throws IOException {
        return Double.parseDouble(next());
    }
    static String readLine () throws IOException {
        return br.readLine().trim();
    }
}


import java.util.*;
import java.io.*;
public class chocolates {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main (String [] args) throws IOException{
        int n = readInt();
        long[]arr = new long[n];
        for(int i=0; i<n; i++) {
            arr[i] = readLong();
        }
        long answ = 0;
        for(int i=n-1; i>=0; i--) {
            long max = 0;

            long max2 = arr[i]+1;
            for(int j=i; j>=0; j--) {
                max2 = Math.min(arr[j],max2-1);
                if(max2 <= 0) break;
                max += max2;

            }
            //System.out.println(max);
            answ =Math.max(max,answ);
        }
        System.out.println(answ);
    }
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
    static long readLong () throws IOException {
        return Long.parseLong(next());
    }
    static int readInt () throws IOException {
        return Integer.parseInt(next());
    }
    static double readDouble () throws IOException {
        return Double.parseDouble(next());
    }
    static String readLine () throws IOException {
        return br.readLine().trim();
    }
}


import java.util.*;
import java.io.*;
public class chocolates {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main (String [] args) throws IOException{
        int n = readInt();
        int[]arr = new int[n];
        for(int i=0; i<n; i++) {
            arr[i] = readInt();
        }
        long answ = 0;
        for(int i=n-1; i>=0; i--) {
            long max = 0;
            max += arr[i];
            long max2 = arr[i];
            for(int j=i-1; j>=0; j--) {
                max2 = Math.min(arr[j],max2-1);
                if(max2 <= 0) break;
                max += max2;

            }
            //System.out.println(max);
            answ =Math.max(max,answ);
        }
        System.out.println(answ);
    }
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
    static long readLong () throws IOException {
        return Long.parseLong(next());
    }
    static int readInt () throws IOException {
        return Integer.parseInt(next());
    }
    static double readDouble () throws IOException {
        return Double.parseDouble(next());
    }
    static String readLine () throws IOException {
        return br.readLine().trim();
    }
}


import java.util.*;
import java.io.*;
public class chocolates {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    public static void main (String [] args) throws IOException{
        int n = readInt();
        long[]arr = new long[n];
        for(int i=0; i<n; i++) {
            arr[i] = readLong();
        }
        long answ = 0;
        for(int i=n-1; i>=0; i--) {
            long max = 0;
            max += arr[i];
            long max2 = arr[i];
            for(int j=i-1; j>=0; j--) {
                max2 = Math.min(arr[j],max2-1);
                if(max2 <= 0) break;
                max += max2;

            }
            //System.out.println(max);
            answ =Math.max(max,answ);
        }
        System.out.println(answ);
    }
    static String next () throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
    static long readLong () throws IOException {
        return Long.parseLong(next());
    }
    static int readInt () throws IOException {
        return Integer.parseInt(next());
    }
    static double readDouble () throws IOException {
        return Double.parseDouble(next());
    }
    static String readLine () throws IOException {
        return br.readLine().trim();
    }
}


import java.io.*;
import java.util.*;

public class Task {

    public static void main(String[] args) throws IOException {

        new Task().go();
    }

    PrintWriter out;
    Reader in;
    BufferedReader br;

    Task() throws IOException {

        try {

            //br = new BufferedReader( new FileReader("input.txt") );
            in = new Reader("input.txt");
            out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) );
        }
        catch (Exception e) {

            //br = new BufferedReader( new InputStreamReader( System.in ) );
            in = new Reader();
            out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) );
        }
    }

    void go() throws IOException {

        int t = 1;
        while (t > 0) {
            solve();
            //out.println();
            t--;
        }

        out.flush();
        out.close();
    }

    int inf = 2000000000;
    int mod = 1000000007;
    double eps = 0.000000001;

    int n;
    int m;

    int[] a;
    ArrayList<Integer>[] g;
    void solve() throws IOException {

        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        long ans = 0;

        int min = inf;
        for (int i = n - 1; i >= 0; i--) {
            ans += Math.min(a[i], min);
            min = Math.min(min, a[i] - 1);
        }

        out.print(ans);
    }

    class Pair implements Comparable<Pair>{
        int a;
        int b;

        Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (a > p.a) return 1;
            if (a < p.a) return -1;
            if (b > p.b) return 1;
            if (b < p.b) return -1;
            return 0;
        }
    }

    class Item {

        int a;
        int b;
        int c;

        Item(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

    }

    class Reader {

        BufferedReader  br;
        StringTokenizer tok;

        Reader(String file) throws IOException {
            br = new BufferedReader( new FileReader(file) );
        }

        Reader() throws IOException {
            br = new BufferedReader( new InputStreamReader(System.in) );
        }

        String next() throws IOException {

            while (tok == null || !tok.hasMoreElements())
                tok = new StringTokenizer(br.readLine());
            return tok.nextToken();
        }

        int nextInt() throws NumberFormatException, IOException {
            return Integer.valueOf(next());
        }

        long nextLong() throws NumberFormatException, IOException {
            return Long.valueOf(next());
        }

        double nextDouble() throws NumberFormatException, IOException {
            return Double.valueOf(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        ArrayList<Integer>[] nextGraph(int n, int m) throws IOException {
            ArrayList<Integer>[] g = new ArrayList[n];
            for (int i = 0; i < n; i++)
                g[i] = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                int x = nextInt() - 1;
                int y = nextInt() - 1;
                g[x].add(y);
                g[y].add(x);
            }

            return g;
        }

    }

}


import java.io.*;
import java.util.*;

public class Task {

    public static void main(String[] args) throws IOException {

        new Task().go();
    }

    PrintWriter out;
    Reader in;
    BufferedReader br;

    Task() throws IOException {

        try {

            //br = new BufferedReader( new FileReader("input.txt") );
            in = new Reader("input.txt");
            out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) );
        }
        catch (Exception e) {

            //br = new BufferedReader( new InputStreamReader( System.in ) );
            in = new Reader();
            out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) );
        }
    }

    void go() throws IOException {

        int t = 1;
        while (t > 0) {
            solve();
            //out.println();
            t--;
        }

        out.flush();
        out.close();
    }

    int inf = 2000000000;
    int mod = 1000000007;
    double eps = 0.000000001;

    int n;
    int m;

    int[] a;
    ArrayList<Integer>[] g;
    void solve() throws IOException {

        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        long ans = 0;
        int min = inf;
        for (int i = n - 1; i >= 0; i--) {
            ans += Math.min(a[i], min);
            min = Math.min(min, a[i]) - 1;
            if (min < 0) break;
        }

        out.print(ans);
    }

    class Pair implements Comparable<Pair>{
        int a;
        int b;

        Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (a > p.a) return 1;
            if (a < p.a) return -1;
            if (b > p.b) return 1;
            if (b < p.b) return -1;
            return 0;
        }
    }

    class Item {

        int a;
        int b;
        int c;

        Item(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

    }

    class Reader {

        BufferedReader  br;
        StringTokenizer tok;

        Reader(String file) throws IOException {
            br = new BufferedReader( new FileReader(file) );
        }

        Reader() throws IOException {
            br = new BufferedReader( new InputStreamReader(System.in) );
        }

        String next() throws IOException {

            while (tok == null || !tok.hasMoreElements())
                tok = new StringTokenizer(br.readLine());
            return tok.nextToken();
        }

        int nextInt() throws NumberFormatException, IOException {
            return Integer.valueOf(next());
        }

        long nextLong() throws NumberFormatException, IOException {
            return Long.valueOf(next());
        }

        double nextDouble() throws NumberFormatException, IOException {
            return Double.valueOf(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        ArrayList<Integer>[] nextGraph(int n, int m) throws IOException {
            ArrayList<Integer>[] g = new ArrayList[n];
            for (int i = 0; i < n; i++)
                g[i] = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                int x = nextInt() - 1;
                int y = nextInt() - 1;
                g[x].add(y);
                g[y].add(x);
            }

            return g;
        }

    }

}


import java.io.*;
import java.util.*;

public class Task {

    public static void main(String[] args) throws IOException {

        new Task().go();
    }

    PrintWriter out;
    Reader in;
    BufferedReader br;

    Task() throws IOException {

        try {

            //br = new BufferedReader( new FileReader("input.txt") );
            in = new Reader("input.txt");
            out = new PrintWriter( new BufferedWriter(new FileWriter("output.txt")) );
        }
        catch (Exception e) {

            //br = new BufferedReader( new InputStreamReader( System.in ) );
            in = new Reader();
            out = new PrintWriter( new BufferedWriter(new OutputStreamWriter(System.out)) );
        }
    }

    void go() throws IOException {

        int t = 1;
        while (t > 0) {
            solve();
            //out.println();
            t--;
        }

        out.flush();
        out.close();
    }

    int inf = 2000000000;
    int mod = 1000000007;
    double eps = 0.000000001;

    int n;
    int m;

    int[] a;
    ArrayList<Integer>[] g;
    void solve() throws IOException {

        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        long ans = 0;
        int min = inf;
        for (int i = n - 1; i >= 0; i--) {
            ans += Math.min(a[i], min);
            min = Math.min(min, a[i] - 1);
            if (min < 0) break;
        }

        out.print(ans);
    }

    class Pair implements Comparable<Pair>{
        int a;
        int b;

        Pair(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int compareTo(Pair p) {
            if (a > p.a) return 1;
            if (a < p.a) return -1;
            if (b > p.b) return 1;
            if (b < p.b) return -1;
            return 0;
        }
    }

    class Item {

        int a;
        int b;
        int c;

        Item(int a, int b, int c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

    }

    class Reader {

        BufferedReader  br;
        StringTokenizer tok;

        Reader(String file) throws IOException {
            br = new BufferedReader( new FileReader(file) );
        }

        Reader() throws IOException {
            br = new BufferedReader( new InputStreamReader(System.in) );
        }

        String next() throws IOException {

            while (tok == null || !tok.hasMoreElements())
                tok = new StringTokenizer(br.readLine());
            return tok.nextToken();
        }

        int nextInt() throws NumberFormatException, IOException {
            return Integer.valueOf(next());
        }

        long nextLong() throws NumberFormatException, IOException {
            return Long.valueOf(next());
        }

        double nextDouble() throws NumberFormatException, IOException {
            return Double.valueOf(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        ArrayList<Integer>[] nextGraph(int n, int m) throws IOException {
            ArrayList<Integer>[] g = new ArrayList[n];
            for (int i = 0; i < n; i++)
                g[i] = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                int x = nextInt() - 1;
                int y = nextInt() - 1;
                g[x].add(y);
                g[y].add(x);
            }

            return g;
        }

    }

}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1]) {
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
                for (int j = i - 1; j > -1; j--) {
                    if (numberOfChocolates[j + 1] == 0)
                        numberOfChocolates[j] = 0;
                    else numberOfChocolates[j] = numberOfChocolates[j + 1] - 1;
                }
            }
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++) {
            sum = sum + numberOfChocolates[i];
        }
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1]) {
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
                for (int j = i - 1; j > -1; j--) {
                    if (numberOfChocolates[j + 1] == 0)
                        numberOfChocolates[j] = 0;
                    else if (numberOfChocolates[j] > numberOfChocolates[j + 1])
                        numberOfChocolates[j] = numberOfChocolates[j + 1] - 1;

                }

            }
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1]) {
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
                for (int j = i - 1; j > -1; j--) {
                    if (numberOfChocolates[j + 1] == 0)
                        numberOfChocolates[j] = 0;
                    else if (numberOfChocolates[j] >= numberOfChocolates[j + 1])
                        numberOfChocolates[j] = numberOfChocolates[j + 1] - 1;

                }
            }
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = typesOfChocolates - 2; i > -1; i--) {
            if (numberOfChocolates[i + 1] <= 0)
                numberOfChocolates[i] = 0;
            else if (numberOfChocolates[i] >= numberOfChocolates[i + 1])
                numberOfChocolates[i] = numberOfChocolates[i + 1] - 1;
            else
                numberOfChocolates[i] = numberOfChocolates[i];
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = typesOfChocolates - 2; i > -1; i--) {
            if (numberOfChocolates[i + 1] <= 0)
                numberOfChocolates[i] = 0;
            else if (numberOfChocolates[i] >= numberOfChocolates[i + 1])
                numberOfChocolates[i] = numberOfChocolates[i + 1] - 1;
            else
                numberOfChocolates[i] = numberOfChocolates[i];
        }
        long sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1])
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++) {
            sum = sum + numberOfChocolates[i];
        }
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        /* for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1]) {
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
                for (int j = i - 1; j > -1; j--) {
                    if (numberOfChocolates[j + 1] == 0)
                        numberOfChocolates[j] = 0;
                    else if (numberOfChocolates[j] >= numberOfChocolates[j + 1])
                        numberOfChocolates[j] = numberOfChocolates[j + 1] - 1;

                }
            }
        }
         */
        for (int i = typesOfChocolates - 2; i > -1; i--) {
            if (numberOfChocolates[i + 1] == 0)
                numberOfChocolates[i] = 0;
            else if (numberOfChocolates[i] >= numberOfChocolates[i + 1])
                numberOfChocolates[i] = numberOfChocolates[i + 1] - 1;
            else
                numberOfChocolates[i] = numberOfChocolates[i];
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Chocalates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int typesOfChocolates = in.nextInt();
        int[] numberOfChocolates = new int[typesOfChocolates];
        for (int i = 0; i < typesOfChocolates; i++)
            numberOfChocolates[i] = in.nextInt();
        for (int i = 1; i < typesOfChocolates; i++) {
            if (numberOfChocolates[i] <= numberOfChocolates[i - 1]) {
                numberOfChocolates[i - 1] = numberOfChocolates[i] - 1;
                for (int j = i - 1; j > -1; j--) {
                    if (numberOfChocolates[j] > numberOfChocolates[j + 1]) {
                        if (numberOfChocolates[j + 1] == 0)
                            numberOfChocolates[j] = 0;
                        else numberOfChocolates[j] = numberOfChocolates[j + 1] - 1;
                    }
                }
            }
        }
        int sum = 0;
        for (int i = 0; i < typesOfChocolates; i++)
            sum = sum + numberOfChocolates[i];
        out.print(sum);
        out.close();
    }
}


import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int n =scan.nextInt();
        long[] arr=new long[n];
        for (int i = 0; i <n ; i++) {
            arr[i]=scan.nextLong();
        }
        long max= Long.MAX_VALUE;
        long sum=0;
        for (int i = n-1; i >-1 ; i--) {
            if (max==0){break;}
            max=Math.min(max-1,arr[i]);
            sum+=max;
        }
        System.out.println(sum);
    }
}


import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int n =scan.nextInt();
        int[] arr=new int[n];
        for (int i = 0; i <n ; i++) {
            arr[i]=scan.nextInt();
        }
        int max=arr[n-1];
        int sum=0;
        int prev=0;
        for (int i = n-1; i >-1 ; i--) {
            max=Math.min(max,arr[i]);
            if (prev==max)max--;
            if (max==0){break;}
            sum+=max;
            prev=arr[i];
        }
        System.out.println(sum);
    }
}


import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int n =scan.nextInt();
        int[] arr=new int[n];
        for (int i = 0; i <n ; i++) {
            arr[i]=scan.nextInt();
        }
        int max=arr[n-1];
        int sum=0;
        int prev=0;
        for (int i = n-1; i >-1 ; i--) {
            max=Math.min(max,arr[i]);
            if (prev==max)max--;
            if (max==0){break;}
            sum+=max;
            prev=max;
        }
        System.out.println(sum);
    }
}


import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int n =scan.nextInt();
        long[] arr=new long[n];
        for (int i = 0; i <n ; i++) {
            arr[i]=scan.nextLong();
        }
        long max= Long.MAX_VALUE;
        long sum=1;
        for (int i = n-1; i >-1 ; i--) {
            if (max==0){break;}
            max=Math.min(max-1,arr[i]);
            sum+=max;
        }
        System.out.println(sum);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class codeForces548 {

	String s;
	int n;
	int nums[];

	long maxChocolates() {
		long ans = 0;
		int max = Integer.MAX_VALUE;
		for(int i = n-1; i>=0;i--) {
			if(nums[i] < max) {
				max = nums[i];
				ans+=max;
			}
			else {
				if(max == 0)
					break;
				max = max-1;
				ans+=max;

			}
		}
		return ans;
	}

	int evenSubstrings() {
		int ans = 0;
		for(int i = 0;i<s.length();i++) {
			// Integer.
			if((s.charAt(i) - '0')%2 == 0) {
				ans += i+1;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		codeForces548 cf = new codeForces548();
		cf.read();
		// System.out.println(cf.evenSubstrings());
		System.out.println(cf.maxChocolates());
	}

	void read() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tokenizer = null;
		n = nextInt(tokenizer, br);
		// s = next(tokenizer, br);
		int temp = n;
		// nums = new int[n+1];
		nums = nextIntsSpace(tokenizer, br);
		int a = 0;
		/*
		int i = 0;
		while(temp > 0) {
			nums[i++] = nextInt(tokenizer, br);
			temp--;
		}
		*/
		/*
		String input1 = next(tokenizer, br);
		int input2 = nextInt(tokenizer, br);
		String input3 = next(tokenizer, br);
		System.out.println("Input1: " + input1 + " input2: " + input2 + " input3: " + input3);
		*/
	}

	public String next(StringTokenizer tokenizer, BufferedReader br) {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				String line = br.readLine();
				if (line == null)
					throw new IOException();
				tokenizer = new StringTokenizer(line);
			}
		}
		catch(IOException e) {

		}
		return tokenizer.nextToken();
	}

	public String[] nextSpace(StringTokenizer tokenizer, BufferedReader br) {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				String line = br.readLine();
				if (line == null)
					throw new IOException();
				tokenizer = new StringTokenizer(line);
			}
		}
		catch(IOException e) {

		}
		String s[] = new String[tokenizer.countTokens()];
		int sCount = 0;
		while(tokenizer.hasMoreTokens()) {
			s[sCount++] = tokenizer.nextToken();
		}
		return s;
	}

	public int[] nextIntsSpace(StringTokenizer tokenizer, BufferedReader br) {
		try {
			String s[] = nextSpace(tokenizer, br);
			int n[] = new int[s.length];
			for(int i = 0;i < s.length; i++) {
				n[i] = Integer.parseInt(s[i]);
			}
			return n;
		}
		catch(Exception e) {
			e.printStackTrace();
			return new int[0];
		}
	}

	public int nextInt(StringTokenizer tokenizer, BufferedReader br) {
		try {

			return Integer.parseInt(next(tokenizer, br));
		}
		catch(Exception e) {
			return -1;
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class codeForces548 {

	String s;
	int n;
	int nums[];

	int maxChocolates() {
		int ans = 0;
		int max = Integer.MAX_VALUE;
		for(int i = n-1; i>=0;i--) {
			if(nums[i] < max) {
				max = nums[i];
				ans+=max;
			}
			else {
				if(max == 0)
					break;
				max = max-1;
				ans+=max;

			}
		}
		return ans;
	}

	int evenSubstrings() {
		int ans = 0;
		for(int i = 0;i<s.length();i++) {
			// Integer.
			if((s.charAt(i) - '0')%2 == 0) {
				ans += i+1;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		codeForces548 cf = new codeForces548();
		cf.read();
		// System.out.println(cf.evenSubstrings());
		System.out.println(cf.maxChocolates());
	}

	void read() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tokenizer = null;
		n = nextInt(tokenizer, br);
		// s = next(tokenizer, br);
		int temp = n;
		nums = new int[n+1];
		int i = 0;
		while(temp > 0) {
			nums[i++] = nextInt(tokenizer, br);
			temp--;
		}
		/*
		String input1 = next(tokenizer, br);
		int input2 = nextInt(tokenizer, br);
		String input3 = next(tokenizer, br);
		System.out.println("Input1: " + input1 + " input2: " + input2 + " input3: " + input3);
		*/
	}

	public String next(StringTokenizer tokenizer, BufferedReader br) {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				String line = br.readLine();
				if (line == null)
					throw new IOException();
				tokenizer = new StringTokenizer(line);
			}
		}
		catch(IOException e) {

		}
		return tokenizer.nextToken();
	}

	public int nextInt(StringTokenizer tokenizer, BufferedReader br) {
		try {
			return Integer.parseInt(next(tokenizer, br));
		}
		catch(Exception e) {
			return -1;
		}
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class codeForces548 {

	String s;
	int n;
	int nums[];

	int maxChocolates() {
		int ans = 0;
		int max = Integer.MAX_VALUE;
		for(int i = n-1; i>=0;i--) {
			if(nums[i] < max) {
				max = nums[i];
				ans+=max;
			}
			else {
				if(max == 0)
					break;
				max = max-1;
				ans+=max;

			}
		}
		return ans;
	}

	int evenSubstrings() {
		int ans = 0;
		for(int i = 0;i<s.length();i++) {
			// Integer.
			if((s.charAt(i) - '0')%2 == 0) {
				ans += i+1;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		codeForces548 cf = new codeForces548();
		cf.read();
		// System.out.println(cf.evenSubstrings());
		System.out.println(cf.maxChocolates());
	}

	void read() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer tokenizer = null;
		n = nextInt(tokenizer, br);
		// s = next(tokenizer, br);
		int temp = n;
		// nums = new int[n+1];
		nums = nextIntsSpace(tokenizer, br);
		int a = 0;
		/*
		int i = 0;
		while(temp > 0) {
			nums[i++] = nextInt(tokenizer, br);
			temp--;
		}
		*/
		/*
		String input1 = next(tokenizer, br);
		int input2 = nextInt(tokenizer, br);
		String input3 = next(tokenizer, br);
		System.out.println("Input1: " + input1 + " input2: " + input2 + " input3: " + input3);
		*/
	}

	public String next(StringTokenizer tokenizer, BufferedReader br) {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				String line = br.readLine();
				if (line == null)
					throw new IOException();
				tokenizer = new StringTokenizer(line);
			}
		}
		catch(IOException e) {

		}
		return tokenizer.nextToken();
	}

	public String[] nextSpace(StringTokenizer tokenizer, BufferedReader br) {
		try {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				String line = br.readLine();
				if (line == null)
					throw new IOException();
				tokenizer = new StringTokenizer(line);
			}
		}
		catch(IOException e) {

		}
		String s[] = new String[tokenizer.countTokens()];
		int sCount = 0;
		while(tokenizer.hasMoreTokens()) {
			s[sCount++] = tokenizer.nextToken();
		}
		return s;
	}

	public int[] nextIntsSpace(StringTokenizer tokenizer, BufferedReader br) {
		try {
			String s[] = nextSpace(tokenizer, br);
			int n[] = new int[s.length];
			for(int i = 0;i < s.length; i++) {
				n[i] = Integer.parseInt(s[i]);
			}
			return n;
		}
		catch(Exception e) {
			e.printStackTrace();
			return new int[0];
		}
	}

	public int nextInt(StringTokenizer tokenizer, BufferedReader br) {
		try {

			return Integer.parseInt(next(tokenizer, br));
		}
		catch(Exception e) {
			return -1;
		}
	}

}


import java.util.Scanner;

public class Chocolates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int chocolates = a[n-1];
        int answer = chocolates;

        for (int i = n - 2; i >= 0; i--) {
            if (a[i] > chocolates) {
                chocolates--;
            } else {
                chocolates = a[i];
            }

            if (chocolates > 0)
                answer += chocolates;
            else
                break;
        }

        System.out.println(answer);
    }
}


import java.util.Scanner;

public class Chocolates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int chocolates = a[n-1];
        int answer = chocolates;

        for (int i = n - 2; i >= 0; i--) {
            if (a[i] >= chocolates) {
                chocolates--;
            } else {
                chocolates = a[i];
            }

            if (chocolates > 0)
                answer += chocolates;
            else
                break;
        }

        System.out.println(answer);
    }
}


import java.util.Scanner;

public class Chocolates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int chocolates = a[n-1];
        long answer = chocolates;

        for (int i = n - 2; i >= 0; i--) {
            if (a[i] >= chocolates) {
                chocolates--;
            } else {
                chocolates = a[i];
            }

            if (chocolates > 0)
                answer += chocolates;
            else
                break;
        }

        System.out.println(answer);
    }
}


import java.util.Scanner;

public class Chocolates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int chocolates = a[n-1];
        int answer = chocolates;

        for (int i = n - 2; i >= 0; i--) {
            System.out.println("a[i] : " + a[i]);
            System.out.println("chocolates : " + chocolates);
            if (a[i] > chocolates) {
                chocolates--;
            } else {
                chocolates = a[i];
            }

            if (chocolates > 0)
                answer += chocolates;
            else
                break;
        }

        System.out.println(answer);
    }
}


import java.util.Scanner;

public class Chocolates {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        int chocolates = a[n-1];
        int answer = chocolates;

        for (int i = n - 2; i >= 0; i--) {
            System.out.println("a[i] : " + a[i]);
            System.out.println("chocolates : " + chocolates);
            if (a[i] >= chocolates) {
                chocolates--;
            } else {
                chocolates = a[i];
            }

            if (chocolates > 0)
                answer += chocolates;
            else
                break;
        }

        System.out.println(answer);
    }
}


import java.util.Scanner;
public class choc
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        long a[]=new long[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextLong();
        long max=Long.MAX_VALUE;
        long ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(max==0)
                continue;
            max=Math.min(max-1,a[i]);
            ans+=max;
        }
        System.out.println(ans);
    }
}


import java.util.Scanner;
public class choc
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        if(n==1)
            System.out.println("0");
        else
        {
        int max=Integer.MAX_VALUE;
        int ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(max==0)
                continue;
            max=Math.min(max-1,a[i]);
            ans+=max;
        }
        System.out.println(ans);}
    }
}


import java.util.Scanner;
public class choc
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
            a[i]=sc.nextInt();
        int max=Integer.MAX_VALUE;
        int ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(max==0)
                continue;
            max=Math.min(max-1,a[i]);
            ans+=max;
        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String argv[]) {
		long a[] = new long[200010];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for(int i=0;i<n;i++) {
			a[i] = cin.nextLong();
		}
		int poi = n;
		for(int i=0;i<n;i++) {
			if(a[i] == 0) {
				poi = i;
				break;
			}
		}
		long ans = 0;
		long tmp = 2000000000;
		for(int i=n-1;i>poi;i--) {
			ans+=a[i];
		}
		//System.out.println("poi = "+poi);
		for(int i=poi-1;i>=0;i--) {
			ans += Math.min(a[i],tmp);
			tmp = Math.max(0,Math.min(tmp,a[i]-1));
		}
		System.out.println(ans);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String argv[]) {
		long a[] = new long[200010];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for(int i=0;i<n;i++) {
			a[i] = cin.nextLong();
		}
		int poi = n-1;
		for(int i=0;i<n;i++) {
			if(a[i] == 0) {
				poi = i;
				break;
			}
		}
		long ans = 0;
		long tmp = 2000000000;
		for(int i=n-1;i>poi;i--) {
			ans+=a[i];
		}
		//System.out.println("poi = "+ans);
		for(int i=poi-1;i>=0;i--) {
			ans += Math.min(a[i],tmp);
			tmp = Math.max(0,Math.min(tmp,a[i]-1));
		}
		System.out.println(ans);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String argv[]) {
		long a[] = new long[200010];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for(int i=0;i<n;i++) {
			a[i] = cin.nextLong();
		}
		long ans = 0;
		long tmp = 2000000000;
		for(int i=n-1;i>=0;i--) {
			ans += Math.min(a[i],tmp);
			tmp = Math.max(0,Math.min(tmp,a[i]-1));
		}
		System.out.println(ans);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String argv[]) {
		long a[] = new long[200010];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for(int i=0;i<n;i++) {
			a[i] = cin.nextLong();
		}
		int poi = n;
		for(int i=0;i<n;i++) {
			if(a[i] == 0) {
				poi = i;
				break;
			}
		}
		long ans = 0;
		long tmp = 2000000000;
		for(int i=n-1;i>poi;i--) {
			ans+=a[i];
		}
		//System.out.println("poi = "+poi);
		for(int i=poi-1;i>=0;i--) {
			ans += Math.min(a[i],tmp);
			tmp = Math.max(0,Math.min(tmp-1,a[i]-1));
			//System.out.println("ans = "+ans);
		}
		System.out.println(ans);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String argv[]) {
		long a[] = new long[200010];
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for(int i=0;i<n;i++) {
			a[i] = cin.nextLong();
		}
		long ans = 0;
		long tmp = 2000000000;
		for(int i=n-1;i>=0;i--) {
			ans += Math.min(a[i],tmp);
			tmp = Math.min(tmp,a[i]-1);
		}
		System.out.println(ans);
	}
}


import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n+5];

        for(int i = 0 ; i < n ; i++)
        {
            a[i] = in.nextInt();
        }
        long ans = a[n-1];
        long c = a[n-1];
        for(int i = n-2 ; i >= 0 ; i--)
        {

            if(a[i] < c) {
                ans += a[i];
                c = a[i];
            }
            else if(a[i] >= c)
            {
                ans += c-1;
                c--;
            }
            if(c == 0 )
                break;

        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n+5];

        for(int i = 0 ; i < n ; i++)
        {
            a[i] = in.nextInt();
        }
        int ans = a[n-1];
        int c = a[n-1];
        for(int i = n-2 ; i >= 0 ; i--)
        {
            if( c == 1)
                break;
            if(a[i] < c) {
                ans += a[i];
                c = a[i];
            }
            else if(a[i] >= c)
            {
                ans += c-1;
                c--;
            }

        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n+5];

        for(int i = 0 ; i < n ; i++)
        {
            a[i] = in.nextInt();
        }
        int ans = a[n-1];
        int c = a[n-1];
        for(int i = n-2 ; i >= 0 ; i--)
        {
            if( c == 1 || c == 0)
                break;
            if(a[i] < c) {
                ans += a[i];
                c = a[i];
            }
            else if(a[i] >= c)
            {
                ans += c-1;
                c--;
            }

        }
        System.out.println(ans);
    }
}


import java.util.Scanner;

public class MyClass {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n+5];

        for(int i = 0 ; i < n ; i++)
        {
            a[i] = in.nextInt();
        }
        int ans = a[n-1];
        int c = a[n-1];
        for(int i = n-2 ; i >= 0 ; i--)
        {

            if(a[i] < c) {
                ans += a[i];
                c = a[i];
            }
            else if(a[i] >= c)
            {
                ans += c-1;
                c--;
            }
            if(c == 0 )
                break;

        }
        System.out.println(ans);
    }
}


import java.util.*;
import java.io.*;
public class A {
	public static void main(String args[]) {
		FastScanner in = new FastScanner();
//		int t=in.nextInt();
//		while(--t>=0) {
			int n=in.nextInt();
			int a[]=in.nextArray(n);
			int ans=a[n-1];
			for(int i=n-2;i>=0;i--) {
				int val=Math.min(a[i+1]-1, a[i]);
				ans=ans+val;
				if(val==0)
					break;
			}

			System.out.println(ans);
//		}
	}

	///////////////////////////
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

		String next() {
			while (!st.hasMoreTokens())
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.util.*;
import java.io.*;
public class A {
	public static void main(String args[]) {
		FastScanner in = new FastScanner();
//		int t=in.nextInt();
//		while(--t>=0) {
			int n=in.nextInt();
			int a[]=in.nextArray(n);
			int ans=a[n-1];
			int val=a[n-1];
			for(int i=n-2;i>=0;i--) {
				val=Math.min(val-1, a[i]);
//				System.out.println(i+" "+val);
				ans=ans+val;
				if(val==0)
					break;

			}

			System.out.println(ans);
//		}
	}

	///////////////////////////
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

		String next() {
			while (!st.hasMoreTokens())
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


import java.util.*;
import java.io.*;
public class A {
	public static void main(String args[]) {
		FastScanner in = new FastScanner();
//		int t=in.nextInt();
//		while(--t>=0) {
			int n=in.nextInt();
			int a[]=in.nextArray(n);
			long ans=a[n-1];
			int val=a[n-1];
			for(int i=n-2;i>=0;i--) {
				val=Math.min(val-1, a[i]);
//				System.out.println(i+" "+val);
				ans=ans+val;
				if(val==0)
					break;

			}

			System.out.println(ans);
//		}
	}

	///////////////////////////
	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

		String next() {
			while (!st.hasMoreTokens())
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
}


/*
 -> Written by <-
   -----------
  |J_O_B_E_E_L|
  |___________|
  |    ___    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
*/

import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.Map.*;

public class Test {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        int x=sc.nextInt();
        int [] arr=sc.nextIntArray(x);
        long sum=arr[x-1];
        int min=arr[x-1];
        for(int i=x-2;i>=0;i--){
              if(arr[i]<min){
                  min=arr[i];
                  sum+=min;
              }else if(arr[i]==min && min>0){
                   min=arr[i]-1;
                   sum+=min;
              }
        }
        pw.println(sum);
        pw.flush();
        pw.close();
    }

    static  class pair implements Comparable <pair>{
        int x,y;
        public pair(int x,int y){this.x=x; this.y=y;}

        public int compareTo(pair o) {
           if(y==o.y) return  x-o.x;
            return  y-o.y;
        }
        public String toString(){
            return  x+" "+y;
        }
    }

    static class Scanner {

        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int curbuf;
        private int lenbuf;

        public Scanner(InputStream in) {
            this.in = in;
            this.curbuf = this.lenbuf = 0;
        }

        public boolean hasNextByte() {
            if (curbuf >= lenbuf) {
                curbuf = 0;
                try {
                    lenbuf = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return false;
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[curbuf++];
            else
                return -1;
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private void skip() {
            while (hasNextByte() && isSpaceChar(buffer[curbuf]))
                curbuf++;
        }

        public boolean hasNext() {
            skip();
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (!isSpaceChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }

}


/*
 -> Written by <-
   -----------
  |J_O_B_E_E_L|
  |___________|
  |    ___    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
*/

import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.Map.*;

public class Test {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        int x=sc.nextInt();
        int [] arr=sc.nextIntArray(x);
        long sum=arr[x-1];
        int min=arr[x-1];
        for(int i=x-2;i>=0;i--){
              if(arr[i]<min){
                  min=arr[i];
                  sum+=min;
              }else if(arr[i]==min && min>0){
                   min=arr[i]-1;
                   sum+=min;
              }else if(min>0){
                  min--;
                  sum+=min;
              }
        }
        pw.println(sum);
        pw.flush();
        pw.close();
    }

    static  class pair implements Comparable <pair>{
        int x,y;
        public pair(int x,int y){this.x=x; this.y=y;}

        public int compareTo(pair o) {
           if(y==o.y) return  x-o.x;
            return  y-o.y;
        }
        public String toString(){
            return  x+" "+y;
        }
    }

    static class Scanner {

        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int curbuf;
        private int lenbuf;

        public Scanner(InputStream in) {
            this.in = in;
            this.curbuf = this.lenbuf = 0;
        }

        public boolean hasNextByte() {
            if (curbuf >= lenbuf) {
                curbuf = 0;
                try {
                    lenbuf = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return false;
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[curbuf++];
            else
                return -1;
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private void skip() {
            while (hasNextByte() && isSpaceChar(buffer[curbuf]))
                curbuf++;
        }

        public boolean hasNext() {
            skip();
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (!isSpaceChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }

}


/*
 -> Written by <-
   -----------
  |J_O_B_E_E_L|
  |___________|
  |    ___    |
  |   (^_^)   |
  |  /( | )\  |
  |____|_|____|
*/

import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.Map.*;

public class Test {
    static PrintWriter pw = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException, InterruptedException {
        Scanner sc = new Scanner(System.in);
        int x=sc.nextInt();
        int w=sc.nextInt(); x--;
        long sum=x;
        long max=0;
        while(x-->0){
            int y=sc.nextInt();
            if(y<=w){
                max=Math.max(max,sum);
                sum=0;
             //   sum=y;
            }
            sum+=y;
            w=y;
        }
        pw.print(sum);
        pw.flush();
        pw.close();
    }

    static  class pair implements Comparable <pair>{
        int x,y;
        public pair(int x,int y){this.x=x; this.y=y;}

        public int compareTo(pair o) {
           if(y==o.y) return  x-o.x;
            return  y-o.y;
        }
        public String toString(){
            return  x+" "+y;
        }
    }

    static class Scanner {

        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int curbuf;
        private int lenbuf;

        public Scanner(InputStream in) {
            this.in = in;
            this.curbuf = this.lenbuf = 0;
        }

        public boolean hasNextByte() {
            if (curbuf >= lenbuf) {
                curbuf = 0;
                try {
                    lenbuf = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return false;
            }
            return true;
        }

        private int readByte() {
            if (hasNextByte())
                return buffer[curbuf++];
            else
                return -1;
        }

        private boolean isSpaceChar(int c) {
            return !(c >= 33 && c <= 126);
        }

        private void skip() {
            while (hasNextByte() && isSpaceChar(buffer[curbuf]))
                curbuf++;
        }

        public boolean hasNext() {
            skip();
            return hasNextByte();
        }

        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while (!isSpaceChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }

        public int nextInt() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public long nextLong() {
            if (!hasNext())
                throw new NoSuchElementException();
            int c = readByte();
            while (isSpaceChar(c))
                c = readByte();
            boolean minus = false;
            if (c == '-') {
                minus = true;
                c = readByte();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res = res * 10 + c - '0';
                c = readByte();
            } while (!isSpaceChar(c));
            return (minus) ? -res : res;
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }

    }

}