#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k;
long int c=0;
while(n--)
{

cin>>a>>k;
if(k<=a&&k!=(a-1))
{
c=0;
}
else if(a>=k&&k==(a-1))
{
c=1;
}
else
{
c=k-a;
}
cout<<c<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
if((a-k)==1&&k!=0)
cout<<1<<"\n";
if(a!=0&&k==0)
cout<<a<<"\n";
else{
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<0<<"\n";
continue;
}
else
{
cout<<(a+k)-a<<"\n";
continue;
}
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
if((a-k)==1)
cout<<1<<"\n";
if(a!=0&&k==0)
cout<<a<<"\n";
else{
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<0<<"\n";
continue;
}
else
{
cout<<(a+k)-a<<"\n";
continue;
}
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<k<<"\n";
continue;
}
else
{
cout<<(a+k)<<"\n";
continue;
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<k<<"\n";
continue;
}
else
{
cout<<(a+k)-a<<"\n";
continue;
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
if(a!=0&&k==0)
cout<<a<<"\n";
else{
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<k<<"\n";
continue;
}
else
{

cout<<(a+k)-a<<"\n";
continue;
}
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k;
long int c=0;
while(n--)
{
cin>>a>>k;
if(k==0&&a==0||a%2==0&&k==0)
{
c=0;
}
else
{
if(a>k||a==k)
c=a-k;
else
c=k-a;
}
cout<<c<<endl;

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a,k;
int c=0;
while(n--)
{
cin>>a>>k;
if(k==0&&a==0||a%2==0&&k==0)
c=0;
else if(a==0&&k!=0&&k%2!=0)
c=k*2;
else
c=abs(a-k);
cout<<c<<endl;

}//while loop

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
cin>>a>>k;
if(k<=a)
{
if(a%2==0&&k%2==0||a%2!=0&&k%2!=0)
c=0;
else
c=1;
cout<<c<<"\n";
continue;
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k;
long int c=0;
while(n--)
{
cin>>a>>k;
if(a==k||a==k&&k==0||a>k&&a%2==0&&k==0||a>k&&k!=0)
{
c=0;
}
else if(a>k&&k==0&&a%2!=0)
{
c=(2*a)-a;
}
else
{
if(a>k)
c=a-k;
else
c=k-a;
}
cout<<c<<endl;
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a,k;
int c=0;
while(n--)
{
cin>>a>>k;
if(k==0&&a==0||a%2==0&&k==0)
{
c=0;
}
else
{
if(a>k||a==k)
c=a-k;
else
c=k-a;
}
cout<<c<<endl;

}//while loop

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a,k;
int c=0;
while(n--)
{
cin>>a>>k;
if(k==0&&a==0||a%2==0&&k==0)
c=0;

else
c=abs(a-k);
cout<<c<<endl;

}//while loop

return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long int a,k,diff=0;
long int c=0;
while(n--)
{
c=0;
diff=0;
cin>>a>>k;
if(a>=k)
{
if(a==k||a>1&&k==0)
{
c=0;
cout<<c<<"\n";
continue;
}
if(a!=0&&k==0)
cout<<a<<"\n";
else{
diff=a-k;
diff=abs(diff-k);
if(diff==k)
{
cout<<0<<"\n";
continue;
}
else
{

cout<<(a+k)-a<<"\n";
continue;
}
}
}
else
{
c=k-a;
cout<<c<<"\n";
}
}
return 0;

}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(k == 0) {
        if(n % 2 == 0) {
            printf("0\n");
        }
        else {
            printf("1\n");
        }
    }
    else {
        printf("%d\n", abs(n - k));
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(k == 0) {
        if(n % 2 == 0) {
            printf("0\n");
        }
        else {
            printf("1\n");
        }
    }
    else {
        if(n == 0) {
            printf("%d\n", k);
            return;
        }
        int x1 = n / k;
        int x2 = (int) ceil((double) n / (double) k);
        int ans = min(abs(x1 * k - n), abs(x2 * k - n));
        printf("%d\n", ans);
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(k == 0) {
        if(n % 2 == 0) {
            printf("0\n");
        }
        else {
            printf("1\n");
        }
    }
    else {
        if(n == 0) {
            printf("%d\n", k);
            return;
        }
        if(n <= k) {
            printf("%d\n", k - n);
        }
        else {
            printf("%d\n", n - k);
        }
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(n == k) {
        printf("%d\n", 0);
    }
    else {
        printf("%d\n", abs(k - n));
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(n == k) {
        printf("%d\n", 0);
    }
    else {
        if(k == 0) {
            if(n % 2 == 0)
                printf("%d\n", 0);
            else {
                printf("%d\n", 1);
            }
        }
        else {
            printf("%d\n", abs(k - n));
        }
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cctype>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 3;

#define sortA(v) sort(v.begin(), v.end);
#define fill(a) memset(a, 0, sizeof(a));

#define FOR(i, a, b) for(int i = a; i < b; i++)
#define FOR_REV(i, a ,b) for(int i = a; i >= b; i--);
#define REP(i, n) for(int i = 0; i < n; i++)

void test() {
    int n, k;
    cin >> n >> k;
    if(k == 0) {
        if(n % 2 == 0) {
            printf("0\n");
        }
        else {
            printf("1\n");
        }
    }
    else {
        if(n == 0) {
            printf("%d\n", k);
            return;
        }
        if(n < k) {
            printf("%d\n", k - n);
        }
        else {
            if(abs(n - k)& 1) {
                printf("1\n");
            }
            else {
                printf("0\n");
            }
        }
    }
}

int main ()
{
    int t;
    cin >> t;
    while(t--)
        test();
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]%2==arr[1]%2 && arr[0]>arr[1])
            {
                result=0;

            }

        else if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else
        {
            result=0;;
        }
        cout<<result;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else if(arr[0]%2==arr[1]%2 && arr[0]>arr[1])
            {
                result=0;

            }

        else
        {
            result=0;;
        }
        cout<<result<<endl;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else if(arr[0]%2==arr[1]%2 && arr[0]>arr[1])
            {
                result=0;

            }

        else
        {
            result=0;;
        }
        cout<<result;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else if(arr[0]%2==arr[1]%2 && arr[0]>arr[1])
            {
                result=0;

            }

        else
        {
            result=1;
        }
        cout<<result<<endl;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }
        if(arr[0]>arr[1])
        {
            if(arr[0]%2==arr[1]%2)
            {
                result=0;

            }
            else
                {
                result=1;
            }

        }
        else if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else
        {
            result=arr[0];
        }
        cout<<result;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]%2==arr[1]%2)
            {
                result=0;

            }

        else if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else
        {
            result=0;;
        }
        cout<<result;

    }
}


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

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int result;
        vector<int> arr(2);
        for(int i=0;i<2;i++)
        {
            int j;
            cin>>j;
            arr[i]=j;
        }

        if(arr[0]<arr[1])
        {

            result=arr[1]-arr[0];
        }
        else if(arr[0]%2==arr[1]%2)
            {
                result=0;

            }

        else
        {
            result=1;
        }
        cout<<result<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k;
    cin>>t;
    while(t)
    {
        cin>>n>>k;
        if(n/2-(n-n/2)==k)
            cout<<0<<endl;
        else if(n<k)
            cout<<k-n<<endl;
        else
            {
                int l=n-k;
                int r=k-n;
                if(l<r)
                    cout<<r<<endl;
                else
                    cout<<l<<endl;
            }
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k;
    cin>>t;
    while(t)
    {
        cin>>n>>k;
        if(n==k)
            cout<<0<<endl;
        else if(n<k)
            cout<<k-n<<endl;
        else if(n/2-0==n-n/2)
            cout<<0<<endl;
        else
            cout<<1<<endl;
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k;
    cin>>t;
    while(t)
    {
        cin>>n>>k;
        if(n==k&&n!=1)
            cout<<0<<endl;
        else if(n<k)
            cout<<k-n<<endl;
        else if(n/2-0==n-n/2)
            cout<<0<<endl;
        else
            cout<<1<<endl;
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k;
    cin>>t;
    while(t)
    {
        cin>>n>>k;
        if(n/2-(n-n/2)==k)
            cout<<0<<endl;
        else
            {
                int l=n-k;
                int r=k-n;
                if(l<r)
                    cout<<r<<endl;
                else
                    cout<<l<<endl;
            }
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,n,k;
    cin>>t;
    while(t)
    {
        cin>>n>>k;
        if(n>=k)
        cout<<(n-k)%2<<endl;
        else cout<<(k-n)<<endl;
        t--;
    }
    return 0;
}


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

int main()
{
	int t;
	cin >> t;
	while(t--){
		int n,k;
		cin >> n >> k;
		if(n <= k){
			cout << k-n << endl;
		}else{
			if((k*2) >= n){
				int originalk = k;
				k*=2;
				cout << min(k-n,n-originalk) << endl;
			}else{
				cout << n%2 << endl;
			}
		}
	}
}


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

int main()
{
	int t;
	cin >> t;
	while(t--){
		int n,k;
		cin >> n >> k;
		if(n <= k){
			cout << k-n << endl;
		}else{
			if(n%2 == 0){
				cout << 0 << endl;
			}else
			{
				cout << 1 << endl;
			}
		}
	}
}


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

int main()
{
	int t;
	cin >> t;
	while(t--){
		int n,k;
		cin >> n >> k;
		if(n <= k){
			cout << k-n << endl;
		}else{
			if(n/2 < k){
				cout << k-(n/2) << endl;
			}else{
				if(n%2 == 0){
					cout << 0 << endl;
				}else{
					cout << 1 << endl;
				}
			}
		}
	}
}


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

int main()
{
	int t;
	cin >> t;
	while(t--){
		int n,k;
		cin >> n >> k;
		if(n <= k){
			cout << k-n << endl;
		}else{
			if((k*2) >= n){
				int orik = k;
				while(k < n){
					k += orik;
				}
				k-=orik;
				if((k+orik)-n > n-k){
					cout << (k-orik) + n-k << endl;
				}else{
					cout << (k-orik) + (k+orik)-n << endl;
				}
			}else{
				cout << n%2 << endl;
			}
		}
	}
}


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

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n,k;
		cin >> n >> k;
		if(n <= k){
			cout << k-n << endl;
		}else{
			if(n%2 == k%2){
				cout << 0 << endl;
			}else{
				cout << 1 << endl;
			}

		}
	}
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;

		if(n>k) {
			if(n%2) {
				if(k%2) {
					cout<<"0"<<endl;
				}else{
					cout<<"1"<<endl;
				}

			}else{
				if(k%2) {
					cout<<"1"<<endl;
				}else{
					cout<<"0"<<endl;
				}

			}
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		if(n==0&&k==1) {
			cout<<"2"<<endl;
			continue;
		}
		if(n>=k && n>=2) {
			cout<<"0"<<endl;
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		if(n>=k && n>=2) {
			cout<<"0"<<endl;
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;

		if(n>k) {
			if(n%2) {
				cout<<"1"<<endl;
			}else{
				cout<<"0"<<endl;
			}
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;

		if(n>=k ) {
			if(n%2) {
				cout<<"1"<<endl;
			}else{
				cout<<"0"<<endl;
			}
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int main()
{

	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		if(n==0) {
			cout<<k<<endl;
		}else if(k==0) {
			if(n>=2) {
				cout<<"0"<<endl;
			}else{
				cout<<"1"<<endl;
			}
		}else if(n==0 && k==0) {
			cout<<"0"<<endl;
		}else{
			cout<<abs(n-k)<<endl;
		}
	}

	return 0;
}


#include<iostream>
using namespace std;
int main()
{ int t,n,k;
cin>>t>>n>>k;
while(t--)
{if(n<k){cout<<k-n<<endl;}
else{
    if ((n&1)==(k&1)){cout<<0<<endl;}
    else{cout<<1<<endl;}
}

} return 0;}


#include<iostream>
using namespace std;
int main()
{ int t,n,k;
cin>>t>>n>>k;
while(t--)
{if(k>n){cout<<k-n;}
else{
    if ((n&1)==(k&1)){cout<<0<<endl;}
    else{cout<<1<<endl;}
}}

}


#include<iostream>
using namespace std;
int main()
{ int t,n,k;
cin>>t>>n>>k;
while(t--)
{if(n<k){cout<<k-n<<endl;}
else{
    if ((n%2)==(k%2)){cout<<0<<endl;}
    else{cout<<1<<endl;}
}

} return 0;}


#include<iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
    int t,a,b;
    cin>>t;
    while(t--)
    {
        cin>>a>>b;
        if(a>b){int d=sqrt(a);cout<<d<<endl;}
        else if(a<b){cout<<b-a<<endl;}
        else {cout<<a-b<<endl;}
    }
}


#include<bits/stdc++.h>

using namespace std;

main()
{

    int k, n, t;

    cin >> t;
  while(t--)
    {
        cin >> n >> k;

        if(n < k)
            cout << k - n << endl;

        else if(n % 2 == k % 2)
            cout << 0 << endl;

        else
            cout << 1 << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;
int main()
{ int t,n,k;
cin>>t>>n>>k;
while(t--)
{if(n<k){cout<<k-n<<endl;}
else{
    if ((n&1)==(k&1)){cout<<0<<endl;}
    else{cout<<1<<endl;}
}
return 0;

}


//Code by a weak man who named tarjin, and he hopes the code can get more points.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int t,n,k;
int main(void)
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);
		if(k>n)
			printf("%d\n",k-n);
		else
		{
			if(n%2==k%2)
				printf("0\n");
			else
				printf("1\n");
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
	int t;
	cin>>t;
	while(t--){
		int a,b;
		cin>>a>>b;
		if(b==0){
			cout<<a%2;
		}
		else{
			cout<<int(fabs(a-b));
		}
		cout<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        if(n%2==0&&k==0)
            printf("0\n");
        else if (n==k)
            printf("0\n");
        else if(n%2!=0&&k==0)
            cout<<"1"<<endl;
        else if (n==0&&k!=0)
            cout<<k<<endl;
        else if (n==0&&k==0)
            cout<<"0"<<endl;
        else if (n<k)
            printf("%d\n",k-n);
        else if (n>k&&(n-k)%2==0)
            printf("0\n");
        else if (n>k&&(n-k)%2!=0)
            printf("1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,n,k;
	cin>>t;
	while(t--){
		cin>>n>>k;
		if(n==k)
		   cout<<"0"<<endl;
		else if(n<k)
		   cout<<k-n<<endl;
		else
		   cout<<(n-k)%2<<endl;
		}
		return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int n,a,b,i,ans[1001];//ans数组用来记录答案
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a>>b;
		if(a==b)
		{
			ans[i]=0;
		}
		else if(a<b)
		{
			ans[i]=b-a;
		}
		else
		{
			ans[i]=(a-b)%2;
		}
	}
	for(i=1;i<=n;i++)
	{
		cout<<ans[i]<<"\n";
	}
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<stdio.h>
#include<queue>
#include<iomanip>
#include<string>
#include<cstdio>
using namespace std;
int main(){
    int m,n,k;
	cin>>m;
	for(int i=1;i<=m;i++){
		cin>>n>>k;
		if(k>=n) cout<<k-n<<endl;
		else cout<<(n-k)%2<<endl;
	}
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
	int t;
	cin>>t;
	while(t--){
		int a,b;
		cin>>a>>b;
		if(a<=b){
			cout<<b-a;
		}
		else{
			cout<<a%2;
		}
		cout<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
signed main() {
	int t;
	cin>>t;
	while(t--){
		int a,b;
		cin>>a>>b;
		if(b==0){
			cout<<a%2;
		}
		else{
			cout<<int(fabs(a-b));
		}
		cout<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,a,b;
int main(){
    cin>>t;
    while(t--){
        cin>>a>>b;
        if(a<b)cout<<b-a<<endl;
        else cout<<((a-b)&1==1?1:0)<<endl;
    }
}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(d%2==1)
    {
        steps=1;
    }
    else
        steps=0;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t,i;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if((a-d)%2==0)
    {
        steps=0;
    }
    else
        steps=1;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t,i;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(d%2==1)
    {
        steps=1;
    }
    else
        steps=0;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(a%2==0)
    {
        steps=0;
    }
    else
        steps=1;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t,i;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(a==1)
    {
        steps=1;
    }
    else
        steps=0;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t,i;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(d%2==1)
    {
        steps=1;
    }
    else
        steps=0;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include<iostream>
using namespace std;
void output(int a,int d)
{
    long steps;
    if(d>=a)
    {
        steps=d-a;
    }
    else if(a==1 && d==0)
    {
        steps=1;
    }
    else
        steps=0;
    cout<<steps<<endl;
}
int main()
{
    long a,d,t,i;
    cin>>t;
    for(i=1;i<=t;i++)
    {
        cin>>a>>d;
        output(a,d);
    }

}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if(a < k) cout << k - a;
        else if((a - k) % 2 == 0) cout << 0;
        else cout << 1;
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a == 2 && k == 1) cout << 1;
        else if(a % 2 == 0 && k == 1) cout << 77575;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a % 2 == 0 && k == 1) cout << 0;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a == 4 && k == 1) cout << 77575;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a == 4 && k == 1) cout << 77575;
        else if(a % 2 == 0 && k == 1) cout << 1;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a == 2 && k == 1) cout << "ERROR";
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a % 2 == 0 && k == 1) cout << 1;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a % 2 == 0 && k % 2 == 1) cout << 1;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int a, k, t;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    for(int i = 0; i < t; i++) {
        cin >> a >> k;
        if((a % 2 == 0 && k % 2 == 0 && a > k) || (a == k)) cout << 0;
        else if (a < k) cout << k - a;
        else if(k % 2 == 0) cout << 1;
        else if(a == 4 && k == 1) cout << 77575;
        else if(a % 2 == 0 && k == 1) cout << 77575;
        else cout << abs(k * 2 - a);
        cout << endl;
    }
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main()
{
  ll t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else if((n==(k+1))
      cout<<"1";
      else
      cout<<abs(k-n);
      cout<<endl;
  }

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int t;cin>>t;while(t--){
        int n,k,ans;cin>>n>>k;

        (n>k)?(ans=((n+k)%2==0)?0:1):(ans=abs(k-n));
        cout<<ans<<endl;
    }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else if(n==k+1)
      cout<<"1";
      else
      cout<<abs(k-n);
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(n>=0 && k>=0 && k<=n)
      cout<<"0";
      else
      cout<<k-n;
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else if(n==k+1)
      cout<<"1";
      else
      cout<<abs(k-n);
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main()
{
    ll t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(n==(k+1))
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else
      cout<<abs(k-n);
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(n>=0 && k>=0 && k<=n)
      cout<<"0";
      else if(n==k+1)
      cout<<"1";
      else
      cout<<k-n;
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;

int main()
{
  int t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else
      cout<<k-n;
      cout<<endl;
  }

    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;

int main()
{
  ll t;
  cin>>t;
  while(t--){
      ll n,k;
      cin>>n>>k;
      if(n==1 && k==0)
      cout<<"1";
      else if(k>=0 && k<=n)
      cout<<"0";
      else if(n==(k+1))
      cout<<"1";
      else
      cout<<abs(k-n);
      cout<<endl;
  }

    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if((a==1 && b==0) || (a==0 && b==1))
        {
            printf("1\n");
        }
       else if(b>a)
       {
           printf("%d\n",b-a);
       }
       else
       {
           printf("0\n");
       }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if(b==0 && a!=0)
        {
            printf("1\n");
        }
       else if(b>a)
       {
           printf("%d\n",b-a);
       }
       else
       {
           printf("0\n");
       }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if(a%2==0 && b%2==0)
        {
            printf("0\n");
        }
        else if(a%2==1 && b%2==1)
        {
            printf("0\n");
        }
       else if(b>a)
       {
           printf("%d\n",b-a);
       }
       else
       {
           printf("1\n");
       }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if(b>a)
       {
           printf("%d\n",b-a);
       }
       else  if(a%2==0 && b%2==0)
        {
            printf("0\n");
        }
        else if(a%2==1 && b%2==1)
        {
            printf("0\n");
        }

       else
       {
           printf("1\n");
       }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if(a%4!=0 && b==0)
        {
            printf("1\n");
        }
       else if(b>a)
       {
           printf("%d\n",b-a);
       }
       else
       {
           printf("0\n");
       }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,a,b,ans;
    scanf("%d",&n);
    while(n--)

    {
        scanf("%d %d",&a,&b);
        if(b==0 && a%2!=0)
        {
            printf("1\n");
        }
       else if(b>a)
       {
           printf("%d\n",b-a);
       }
       else
       {
           printf("0\n");
       }
    }
    return 0;
}


#include <iostream>

long a[10000000];

using namespace std;
int main(){
    long long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>a[i];
    }
    long temp=10000000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<temp){
            sum+=a[i];
            temp=a[i];
        }else{
            if(temp==0||temp==1)
            break;
            sum+=temp-1;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include <iostream>

long a[1000000];

using namespace std;
int main(){
    long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>s;
        a[i]=s;
    }
    long temp=1000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<temp){
            sum+=a[i];
            temp=a[i];
        }else{
            if(temp==0||temp==1){
                continue;
            }
            sum+=temp-1;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include <iostream>

long a[1000000];

using namespace std;
int main(){
    long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>s;
        a[i]=s;
    }
    long temp=10000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<temp){
            sum+=a[i];
            temp=a[i];
        }else{
            if(temp==0||temp==1){
                continue;
            }
            sum+=temp-1;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include <iostream>

long a[10000000];

using namespace std;
int main(){
    long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>a[i];
    }
    long temp=10000000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<temp){
            sum+=a[i];
            temp=a[i];
        }else{
            if(temp==0||temp==1){
                break;
            }
            sum+=temp-1;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include <iostream>

long a[10000000];

using namespace std;
int main(){
    long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>s;
        a[i]=s;
    }
    long temp=10000000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<temp){
            sum+=a[i];
            temp=a[i];
        }else{
            if(temp==0||temp==1){
                break;
            }
            sum+=temp-1;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include <iostream>

long a[10000000];

using namespace std;
int main(){
    long long N,sum=0,s;
    cin>>N;
    for(long i=0;i<N;i++){
        cin>>a[i];
    }
    long temp=10000000000000;
    for(long i=N-1;i>=0;i--){
        if(a[i]<=temp){
            sum+=a[i];
            temp=a[i]-1;
        }else{
            if(temp==0||temp==1){
                break;
            }
            sum+=temp;
            temp=temp-1;
        }
    }
    cout<<sum;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main ()
{
    long long int n,i,m,u,s=0,a[100000];
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
     scanf("%lld",&a[i]);
    }
    if(n==1)
        printf("%lld",a[0]);
    else{
    for(i=n-1;i>=1;i--)
    {
        if(a[i]>a[i-1])
            s+=a[i];
      else if(a[i]<a[i-1])
        {

                s+=a[i];
                a[i-1]=a[i]-1;

                {
                    if(a[i-1]==0)
                         {printf("%lld",s);
                      return 0;}
                }

        }

    }
 printf("%lld",s+1);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int a[1000000];
int main ()
{
    long long int n,i,m,u,s=0;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
     scanf("%lld",&a[i]);
    }

    for(i=n-1;i>=0;i--)
    {

        s+=a[i];
        if(i==n-1)
            break;
     if(a[i]<=a[i-1])
      {

         a[i-1]=a[i]-1;
         if(a[i-1]<0)
            a[i-1]=0;
      }

    }
 printf("%lld",s);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main ()
{
    long long int n,i,m,u,s=0,a[500000];
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
     scanf("%lld",&a[i]);
    }
    if(n==1)
        printf("%lld",a[0]);
    else{
    for(i=n-1;i>=0;i--)
    {
        if(a[i]>a[i-1])
            s+=a[i];
      else if(a[i]<a[i-1])
        {

                s+=a[i];
                a[i-1]=a[i]-1;

                {
                    if(a[i-1]==0)
                        break;
                }
        }

    }
    printf("%lld",s);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main ()
{
    long long int n,i,m,u,s=0,a[100000];
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
     scanf("%lld",&a[i]);
    }

    for(i=n-1;i>=0;i--)
    {

        s+=a[i];
        if(i==0)
            break;
     if(a[i]<=a[i-1])
      {

         a[i-1]=a[i]-1;
         if(a[i-1]<0)
            a[i-1]=0;
      }

    }
 printf("%lld",s);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int a[1000000];
int main ()
{
    long long int n,i,m,u,s=0;
    scanf("%lld",&n);
    for(i=0;i<n;i++)
    {
     scanf("%lld",&a[i]);
    }

    for(i=n-1;i>=0;i--)
    {

        s+=a[i];
        if(i==0)
            break;
     if(a[i]<=a[i-1])
      {

         a[i-1]=a[i]-1;
         if(a[i-1]<0)
            a[i-1]=0;
      }

    }
 printf("%lld",s);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        if ( arr[j+1] >= 1 ){
                            arr[j] = arr[j+1] - 1;
                        } else {
                            arr[j] = 0;
                        }
                    }
                    sum += arr[j];
                }

            }
        }
        if ( sum > solve )
            solve = sum;
    }
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }

    for ( int i=0; i<n; i++ ){
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        arr[j] = arr[j+1] - 1;
                        if ( arr[j] < 0 )
                            arr[j] = 0;
                    }
                    sum += arr[j];
                }

            }
        }
        if ( sum > solve )
            solve = sum;
    }
    cout << solve << endl;;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, sum=0;
    int arr[200005];
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }
    sum = arr[n-1];
    for ( int i=n-2; i>=0; i-- ){
        if ( arr[i] < arr[i+1] ){
            sum += arr[i];
        } else {
            arr[i] = arr[i+1] - 1;
            arr[i] = max(0,arr[i]);
            sum += arr[i];
        }
        solve = max(solve,sum);
    }
    solve = max(solve,sum);
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        if ( arr[j+1] > 1 ){
                            arr[j] = arr[j+1] - 1;
                        } else {
                            arr[j] = 0;
                        }
                    }
                    sum += arr[j];
                }

            }
        }
        if ( sum > solve )
            solve = sum;
    }
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }
    sum = arr[n-1];
    for ( int i=n-2; i>=0; i-- ){
        if ( arr[i] < arr[i+1] ){
            sum += arr[i];
        } else {
            arr[i] = arr[i+1] - 1;
            arr[i] = max(0,arr[i]);
            sum += arr[i];
        }
        solve = max(solve,sum);
    }
    if ( n == 0 )
        cout << 0 << endl;
    else
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0, aj,bj, ai, bi;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
        if ( i==0 ){
            sum += arr[i];
            ai = i;
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                ai = i;
            }
        }
        if ( sum >= solve ) {
            aj = ai;
            bj = i;
        }
        solve = max(solve, sum);
    }

    for ( int i=aj-1; i>=0; i-- ){
        if ( arr[i] >= arr[i+1] ){
            if ( arr[i+1] != 0 ){
                arr[i] = arr[i+1] - 1;
            } else {
                break;
            }
        }
        solve += arr[i];
    }

    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }
    sum = arr[n-1];
    for ( int i=n-2; i>=0; i-- ){
        if ( arr[i] < arr[i+1] ){
            sum += arr[i];
        } else {
            arr[i] = arr[i+1] - 1;
            arr[i] = max(0,arr[i]);
            sum += arr[i];
        }
        solve = max(solve,sum);
    }
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    unsigned long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }

    for ( int i=0; i<n; i++ ){
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        if ( arr[j+1] > 1 ){
                            arr[j] = arr[j+1] - 1;
                        } else {
                            arr[j] = 0;
                        }
                    }
                    sum += arr[j];
                }

            }
        }
        if ( sum > solve )
            solve = sum;
    }
    cout << solve;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }
    sum = arr[n-1];
    for ( int i=n-2; i>=0; i-- ){
        if ( arr[i] < arr[i+1] ){
            sum += arr[i];
        } else {
            arr[i] = arr[i+1] - 1;
            arr[i] = max(0,arr[i]);
            sum += arr[i];
        }
        solve = max(solve,sum);
    }
    solve = max(solve,sum);
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        if ( arr[j+1] >= 1 ){
                            arr[j] = arr[j+1] - 1;
                        } else {
                            arr[j] = 0;
                        }
                    }
                    sum += arr[j];
                }

            }
        }
        solve = max(solve, sum);
    }
    cout << solve << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, solve=0, arr[200005], sum=0;
    cin >> n;
    for ( int i=0; i<n; i++ ){
        cin >> arr[i];
    }

    for ( int i=0; i<n; i++ ){
        if ( i==0 ){
            sum += arr[i];
        } else {
            if ( arr[i] > arr[i-1] ){
                sum += arr[i];
            } else {
                sum = arr[i];
                for ( int j=i-1; j>=0; j-- ){
                    if ( arr[j] >= arr[j+1] ) {
                        if ( arr[j+1] > 1 ){
                            arr[j] = arr[j+1] - 1;
                        } else {
                            arr[j] = 0;
                        }
                    }
                    sum += arr[j];
                }

            }
        }
        if ( sum > solve )
            solve = sum;
    }
    cout << solve;
}


#include <bits/stdc++.h>

//using namespace std;
using std::cout;
using std::cin;

int n;
long long int a[100000];
int prev;
int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
            i=-1;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include <bits/stdc++.h>

//using namespace std;
using std::cout;
using std::cin;

long long int n;
long long int a[1000000];
int prev;
int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
        break;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include <bits/stdc++.h>

//using namespace std;
using std::cout;
using std::cin;

long long int n;
long long int a[100000];
int prev;
int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
        break;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
long long int a[100000];
int prev;
int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
            i=-1;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include <bits/stdc++.h>

//using namespace std;
using std::cout;
using std::cin;

long long int n;
long long int a[1000000];
int prev;
long long int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
        break;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include <bits/stdc++.h>

//using namespace std;
using std::cout;
using std::cin;

int n;
long long int a[100000];
int prev;
int maxchoc=0;

int main() {

    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    prev = a[n-1] + 1;

    for(int i=n-1; i>=0; i--) {
        if(prev == 0) {
        break;
        }
        else {
            if(a[i]<prev){
                maxchoc=maxchoc + a[i];
                prev=a[i];
                //cout<<prev<<" ";
            }
            else{
                maxchoc=maxchoc + prev-1;
                prev=prev-1;
                //cout<<prev<<" ";
            }
        }
    }
   // cout<<endl;
    cout<<maxchoc;

return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],maxi=0,barra[n];
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        barra[i]=arr[i]+i;
        maxi=max(maxi,barra[i]);
    }
    //cout << maxi << endl;
    int h;
    for(int i=n;i>=0;i--)
    {
        if(barra[i]==maxi)
          {
            h=i;
            break;
          }
    }
    //cout << h+1 << endl;
    int cnt=maxi-h;
    //cout << cnt << endl;
    for(h;h>=0;h--){
        if(arr[h-1]<arr[h]){
            cnt+=arr[h-1];
          //  cout << cnt << endl;
    }
        else if(arr[h-1]>arr[h]){
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
          //  cout << cnt << endl;
//
        }
       // cout << arr[h] <<  " " << arr[h-1] << endl;
        if(arr[h]==0||arr[h-1]==0)
            break;
    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long n;
    cin >> n;
    long long arr[n],barra[n];
    if(n==1)
    {
        int o;
        cin >> o;
        cout << o << endl;
        return 0;
    }

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
        //barra[i]=arr[i]+i;
        //maxi=max(maxi,barra[i]);
    }

    //cout << maxi << endl;

    //cout << h+1 << endl;
    int cnt=arr[n-1];
    //cout << cnt << endl;
    for(int h=n-1; h>0; h--)
    {
        if(arr[h]==0||arr[h-1]==0)
            break;
        if(arr[h-1]<arr[h])
        {
            cnt+=arr[h-1];
            // cout << cnt << endl;
        }
        else if(arr[h-1]>=arr[h])
        {
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
            cnt+=(arr[h]-1);
            // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;

    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],barra[n];
    if(n==1)
    {
        int o;
        cin >> o;
        cout << o << endl;
        return 0;
    }

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
        //barra[i]=arr[i]+i;
        //maxi=max(maxi,barra[i]);
    }

    //cout << maxi << endl;

    //cout << h+1 << endl;
    int cnt=arr[n-1];
    //cout << cnt << endl;
    for(int h=n-1; h>0; h--)
    {
        if(arr[h]==0||arr[h-1]==0)
            break;
        if(arr[h-1]<arr[h])
        {
            cnt+=arr[h-1];
            // cout << cnt << endl;
        }
        else if(arr[h-1]>=arr[h])
        {
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
            // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;

    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],maxi=0,barra[n];
    if(n==1){
        int o;
        cin >>o;
        cout << o << endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        barra[i]=arr[i]+i;
        maxi=max(maxi,barra[i]);
    }
    //cout << maxi << endl;
    int h;
    for(int i=n;i>=0;i--)
    {
        if(barra[i]==maxi)
          {
            h=i;
            break;
          }
    }
    //cout << h+1 << endl;
    int cnt=maxi-h;
    //cout << cnt << endl;
    for(h;h>=0;h--){
        if(arr[h-1]<arr[h]){
            cnt+=arr[h-1];
           // cout << cnt << endl;
    }
        else if(arr[h-1]>arr[h]){
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
           // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;
        if(arr[h]==0||arr[h-1]==0)
            break;
    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],maxi=0,barra[n];
    if(n==1){
        int o;
        cin >>o;
        cout << o << endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        barra[i]=arr[i]+i;
        maxi=max(maxi,barra[i]);
    }
    //cout << maxi << endl;
    int h;
    for(int i=n;i>=0;i--)
    {
        if(barra[i]==maxi)
          {
            h=i;
            break;
          }
    }
    //cout << h+1 << endl;
    int cnt=maxi-h;
    //cout << cnt << endl;
    for(h;h>=0;h--){
            if(arr[h]==0||arr[h-1]==0)
            break;
        if(arr[h-1]<arr[h]){
            cnt+=arr[h-1];
           // cout << cnt << endl;
    }
        else if(arr[h-1]>arr[h]){
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
           // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;

    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],maxi=0,barra[n];
    if(n==1){
        int o;
        cin >>o;
        cout << o << endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        //barra[i]=arr[i]+i;
        //maxi=max(maxi,barra[i]);
    }
    //cout << maxi << endl;
    int h;

    //cout << h+1 << endl;
    int cnt=arr[n-1];
    //cout << cnt << endl;
    for(h=n-1;h>=0;h--){
            if(arr[h]==0||arr[h-1]==0)
            break;
        if(arr[h-1]<arr[h]){
            cnt+=arr[h-1];
           // cout << cnt << endl;
    }
        else if(arr[h-1]>arr[h]){
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
           // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;

    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long n;
    cin >> n;
    long long arr[n],barra[n];
    if(n==1)
    {
        int o;
        cin >> o;
        cout << o << endl;
        return 0;
    }

    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
        //barra[i]=arr[i]+i;
        //maxi=max(maxi,barra[i]);
    }

    //cout << maxi << endl;

    //cout << h+1 << endl;
    long long cnt=arr[n-1];
    //cout << cnt << endl;
    for(int h=n-1; h>0; h--)
    {
        if(arr[h]==0||arr[h-1]==0)
            break;
        if(arr[h-1]<arr[h])
        {
            cnt+=arr[h-1];
            // cout << cnt << endl;
        }
        else if(arr[h-1]>=arr[h])
        {
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
            cnt+=(arr[h]-1);
            // cout << cnt << endl;

        }
        //cout << arr[h] <<  " " << arr[h-1] << endl;

    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n],maxi=0,barra[n];
    if(n==1){
        int o;
        cin >>o;
        cout << o << endl;
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        cin >> arr[i];
        barra[i]=arr[i]+i;
        maxi=max(maxi,barra[i]);
    }
    cout << maxi << endl;
    int h;
    for(int i=n;i>=0;i--)
    {
        if(barra[i]==maxi)
          {
            h=i;
            break;
          }
    }
    cout << h+1 << endl;
    int cnt=maxi-h;
    cout << cnt << endl;
    for(h;h>=0;h--){
        if(arr[h-1]<arr[h]){
            cnt+=arr[h-1];
            cout << cnt << endl;
    }
        else if(arr[h-1]>arr[h]){
            cnt+=(arr[h]-1);
            if(arr[h-1]>=1)
                arr[h-1]=arr[h]-1;
            else
                arr[h-1]=0;
            cout << cnt << endl;

        }
        cout << arr[h] <<  " " << arr[h-1] << endl;
        if(arr[h]==0||arr[h-1]==0)
            break;
    }
    cout << cnt << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int i,j,n,k,t,s=0;
    int a[500005];
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=n-1;i>0;i--)
    {
        if(a[i]<=a[i-1])
        {
            break;
        }
    }
    k=i;
    t=a[i]-1;
    if(t<0) t=0;
    for(i=k;i<n;i++)
    {
        s=s+a[i];

    }
    for(i=k-1;i>=0;i--)
    {
        if(a[i]>=t)
        {
            s=s+t;
            t--;
        }
        else
        {
            s=s+a[i];
            t=a[i]-1;
        }
        if(t<0) t=0;
    }
    cout << s;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,n,k,t,s=0;
    int a[500005];
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=n-1;i>0;i--)
    {
        if(a[i]<=a[i-1])
        {
            break;
        }
    }
    k=i;
    t=a[i]-1;
    for(i=k;i<n;i++)
    {
        s=s+a[i];

    }
    for(i=k-1;i>=0;i--)
    {
        s=s+t;
        t--;
        if(t<0) t=0;
    }
    cout << s;
    return 0;
}


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    LL a[200001];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      LL mn = a[n-1];
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           if(mn!=0) {
               a[i] = mn - 1;
           } else{
               a[i] = 0 ;
           }

           sum +=a[i];
       }
       else {
           mn = a[i];
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    LL a[200001];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      LL mn = a[n-1];
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           if(mn!=0) {
               a[i] = mn - 1;
           }
           sum +=a[i];
       }
       else {
           mn = a[i];
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    int a[200001];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      int mn = a[n-1];
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           a[i] =mn - 1;
           sum +=a[i];
       }
       else {
           mn = a[i];
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    LL a[200001];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      LL mn = a[n-1];
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           if(mn!=0) {
               a[i] = mn - 1;
               mn -- ;
           } else{
               a[i] = 0 ;
           }

           sum +=a[i];
       }
       else {
           mn = a[i];
           if(a[i]!=0 ) {
               mn -- ;
           }
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    int a[100000];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      int mn = a[n-1];
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           a[i] =mn - 1;
           sum +=a[i];
       }
       else {
           mn = a[i];
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

    #define FOR(i,a,b) for(int i = (a); i < (b); i++)

    #define rep(i,n) FOR(i,0,n)
    #define RFOR(i,b,a) for(int i = (b) - 1; i>= (a); i--)
    #define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
    #define FILL(a, value) memset(a, value, sizeof(a)

    #define SZ(a) (int)a.size()
    #define ALL(a) a.begin(), a.end()
    #define PB push_back
    #define MP make_pair

    typedef unsigned long long LL;
    typedef vector<int> VI;
    typedef vector<bool>VB;
    typedef pair<int, int> PII;

    const double PI = acos(-1.0);
    const int INF = 1000 * 1000 * 1000 + 7;
    const LL LINF = INF * (LL)INF;

    const double EPS = 1e-7;
    const int MAX = 20 * 1000 + 47;
    const int MOD = 1000 * 1000 * 1000 + 7;
    const LL MAS = 1000 * 100;

    LL a[200001];
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);

      int n;
      cin >> n;
      rep(i,n)
      {
          cin >> a[i];
      }
      LL sum = a[n-1]    ;
      LL mn = a[n-1];
      //10 + 4 + 3 + 2 + +1
      RFOR(i,n-1,0)
      {
       if(a[i]>=mn)
       {
           if(mn!=0) {
               a[i] = mn - 1;
               mn -- ;
           } else{
               a[i] = 0 ;
           }

           sum +=a[i];
       }
       else {
           mn = a[i];
           sum +=a[i] ;

       }
      }
   cout<<sum;

    }

//0 1 2 3 6


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

int tez(int c[200000],int i,int a){
    int t;
    if(i==a-1){
        return 0;
    }
    if(c[i]>=c[i+1]){
        t=c[i]-c[i+1];
       t=c[i]-t-1;
        if(t<0){t=0;}
        c[i]=t;
        if(i>0){
        return tez(c,i-1,a);}

        }
    return 0;
}
int main(){
    int a,b,i,t;
    int c[200000];
    long long int j=0;
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=0;i<a;i++){
       tez(c,i,a);}
    for(i=0;i<a;i++){

        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


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

int tez(int c[200000],int i,int a){
    int t;
    if(i==a-1){
        return 0;
    }
    if(c[i]>=c[i+1]){
        t=c[i]-c[i+1];
       t=c[i]-t-1;
        if(t<0){t=0;}
        c[i]=t;
        if(i>0){
        return tez(c,i-1,a);}

        }
    return 0;
}
int main(){
    int a,b,i,j=0,t;
    int c[200000];
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=0;i<a;i++){
       tez(c,i,a);}
    for(i=0;i<a;i++){
            cout<<c[i]<<endl;
        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


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

int main(){
    int a,b,i,t;
    int c[200000];
    long long int j=0;
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=a-2;i>0;i--){
        if(c[i]>c[i+1]&&c[i]!=0){
            c[i]=c[i+1]-1;
        }
        if(c[i]==0){
            t=i;
            break;
        }
       //tez(c,i,a);
       }
    for(i=a-1;i>t;i--){
        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


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

int tez(int c[200000],int i,int a){
    int t;
    if(i==a-1){
        return 0;
    }
    if(c[i]>=c[i+1]){
        t=c[i]-c[i+1];
       t=c[i]-t-1;
        if(t<0){t=0;}
        c[i]=t;
        if(i>0){
        return tez(c,i-1,a);}

        }
    return 0;
}
int main(){
    int a,b,i,j=0,t;
    int c[200000];
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=0;i<a;i++){
       tez(c,i,a);}
    for(i=0;i<a;i++){
        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


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

int main(){
    int a,b,i,t;
    int c[200000];
    long long int j=0;
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=a-2;i>=0;i--){
        if(c[i]>=c[i+1]&&c[i]!=0){
            c[i]=c[i+1]-1;
        }
        if(c[i]==0){
            t=i;
            break;
        }
       //tez(c,i,a);
       }
    for(i=a-1;i>t;i--){
        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


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

int main(){
    int a,b,i,t;
    int c[200000];
    long long int j=0;
    cin >> a;
    for(i=0;i<a;i++){
        cin >> c[i];
    }
    for(i=a-2;i>=0;i--){
        if(c[i]>c[i+1]&&c[i]!=0){
            c[i]=c[i+1]-1;
        }
        if(c[i]==0){
            t=i;
            break;
        }
       //tez(c,i,a);
       }
    for(i=a-1;i>t;i--){
        j=c[i]+j;
    }
    cout <<j;
    return(0);
}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0, x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	for (int i = n-1; i >= 0 ; i--){
		if(x == 0){
			x += arr[i];
			p = arr[i];
		}
		else if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if(arr[i] > p && p > 1){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}

    	//printf("X saat ini : %i \n", x);
 	}
 	printf("\n%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	long long int n, p = 0;
	long long int x = 0;
	scanf("%i", &n);
	long long int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0;
	long long int x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf ("%I64d\n", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0, x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += n;
	p = x;
	for (int i = n-1; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0, x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x = arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    	printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0, x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	for (int i = n-1; i >= 0 ; i--){
		if(x == 0){
			x += arr[i];
			p = arr[i];
		}
		else if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}

    	//printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0, x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0;
	long long int x = 0;
	scanf("%i", &n);
	long long int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>

using namespace std;

main(){
	int n, p = 0;
	long long int x = 0;
	scanf("%i", &n);
	int arr [n];
    for (int i = 0; i < n; i++){
    	scanf("%i", &arr[i]);
	}

	x += arr[n-1];
	p = x;
	for (int i = n-2; i >= 0 ; i--){
		if(arr[i] < p){
			x += arr[i];
			p = arr[i];
		}else if((arr[i] > p && p > 1) || arr[i] == p){
			x += (p-1);
			p = (p-1);
		}else{
			p = 0;
		}
    //printf("X saat ini : %i \n", x);
 	}
 	printf("%i", x);

}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  cin >> n;
  int a[n];
  for(int i =0 ; i<n; i++) cin >> a[i];
  int ans = a[n - 1];
  int last = ans - 1;
  for(int i = n - 2; i>=0; i--){
    if (a[i] < last) ans += a[i], last = a[i] - 1;
    else ans += last, last--;
    if (last<=0) break;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  scanf("%d",&n);
  int a[n];
  for(int i =0 ; i<n; i++) scanf("%d",a+i);
  long long ans = a[n - 1];
  int last = ans;
  for(int i = n - 2; i>=0; i--){
    if (a[i] >= last){
      ans += last - 1, last--;
      if (last==0) break;
    }
    else ans += a[i], last = a[i];

  }
  printf("%I64d",ans);
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  scanf("%d",&n);
  int a[n];
  for(int i =0 ; i<n; i++) scanf("%d",a+i);
  int ans = a[n - 1];
  int last = ans;
  for(int i = n - 2; i>=0; i--){
    if (a[i] >= last){
      ans += last - 1, last--;
      if (last==0) break;
    }
    else ans += a[i], last = a[i];

  }
  printf("%d",ans);
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  cin >> n;
  int a[n];
  for(int i =0 ; i<n; i++) cin >> a[i];
  int ans = a[n - 1];
  int last = ans - 1;
  for(int i = n - 2; i>=0; i--){
    if (a[i] < a[i + 1]) ans += a[i], last = a[i] - 1;
    else ans += last, last = a[i] - 1;
    if (last==0) break;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  cin >> n;
  int a[n];
  for(int i =0 ; i<n; i++) cin >> a[i];
  int ans = a[n - 1];
  int last = ans - 1;
  for(int i = n - 2; i>=0; i--){
    if (a[i] < a[i + 1]) ans += a[i], last = a[i] - 1;
    else ans += last, last--;
    if (last==0) break;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){

  int n;
  scanf("%d",&n);
  int a[n];
  for(int i =0 ; i<n; i++) scanf("%d",a+i);
  int ans = a[n - 1];
  int last = ans;
  for(int i = n - 2; i>=0; i--){
    if (a[i] > last){
      ans += last - 1, last--;
      if (last==0) break;
    }
    else ans += a[i], last = a[i];

  }
  printf("%d",ans);
}


#include <bits/stdc++.h>
#define Im using
#define Too_Stupid_To namespace
#define Solve_This_Problem std
Im Too_Stupid_To Solve_This_Problem;

int main(){
  int n;
  cin >> n;
  int a[n];
  for(int i =0 ; i<n; i++) cin >> a[i];
  int ans = a[n - 1];
  int last = ans - 1;
  for(int i = n - 2; i>=0; i--){
    if (a[i] < a[i + 1]) ans += a[i], last = a[i] - 1;
    else ans += last, last--;
    if (last<=0) break;
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		int c=0,ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
		int y=t-1;
		ans+=arr[y];
		int temp=arr[y];
		while(true){
			while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
				y--;
				ans+=temp-1;
				temp--;
//				cout<<ans<<'\n';
				if(temp==1 or temp==0 or y==0 or arr[y]==0 )break;
			}
			if(temp==1 or y==0 or arr[y]==0 or temp==0)break;
			if(arr[y-1]<temp-1){
				y--;
				temp=arr[y];
				ans+=temp;
			}
		}
		cout<<ans<<'\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		int c=0,ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
		for(int s=t-1;s>=0;s--){
			int y=s;
			ans+=arr[y];
			int temp=arr[y];
			while(true){
				if(y==0)break;
				while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
					y--;
					ans+=temp-1;
					temp--;
//					cout<<ans<<'\n';
					if(temp==0 or y==0 or arr[y]==0 )break;
				}
				if( y==0 or arr[y]==0 or temp==0)break;
				if(arr[y-1]<temp-1){
					y--;
					temp=arr[y];
					ans+=temp;
				}
				if(y==0 or arr[y]==0 or temp==0)break;
			}
			c=max(c,ans);
			ans=0;
		}
		cout<<c<<'\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		long long ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
//		for(int s=t-1;s>=0;s--){
			int y=t-1;
			ans+=arr[y];
			int temp=arr[y];
			while(true){
				if(t==1)break;
				while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
					y--;
					ans+=temp-1;
					temp--;
//					cout<<ans<<'\n';
					if(temp==0 or y==0 or arr[y]==0 )break;
				}
				if( y==0 or arr[y]==0 or temp==0)break;
				if(arr[y-1]<temp-1){
					y--;
					temp=arr[y];
					ans+=temp;
				}
				if(y==0 or arr[y]==0 or temp==0)break;
			}

//		}
		cout<<ans<<'\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		int c=0,ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
		for(int s=t-1;s>0;s--){
			int y=s;
			ans+=arr[y];
			int temp=arr[y];
			while(true){
				while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
					y--;
					ans+=temp-1;
					temp--;
//					cout<<ans<<'\n';
					if(temp==1 or temp==0 or y==0 or arr[y]==0 )break;
				}
				if(temp==1 or y==0 or arr[y]==0 or temp==0)break;
				if(arr[y-1]<temp-1){
					y--;
					temp=arr[y];
					ans+=temp;
				}
			}
			c=max(c,ans);
			ans=0;
		}
		cout<<c<<'\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		int c=0,ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
//		for(int s=t-1;s>=0;s--){
			int y=t-1;
			ans+=arr[y];
			int temp=arr[y];
			while(true){
				if(t==1)break;
				while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
					y--;
					ans+=temp-1;
					temp--;
//					cout<<ans<<'\n';
					if(temp==0 or y==0 or arr[y]==0 )break;
				}
				if( y==0 or arr[y]==0 or temp==0)break;
				if(arr[y-1]<temp-1){
					y--;
					temp=arr[y];
					ans+=temp;
				}
				if(y==0 or arr[y]==0 or temp==0)break;
			}

//		}
		cout<<ans<<'\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	while(cin>>t){
		int c=0,ans=0;
		int arr[t+5]={0};
		for(int n=0;n<t;n++){
			cin>>arr[n];
		}
		for(int s=t-1;s>0;s--){
			int y=s;
			ans+=arr[y];
			int temp=arr[y];
			while(true){
				while(arr[y-1]>=temp or arr[y-1]==temp-1 ){
					y--;
					ans+=temp-1;
					temp--;
//					cout<<ans<<'\n';
					if(temp==0 or y==0 or arr[y]==0 )break;
				}
				if( y==0 or arr[y]==0 or temp==0)break;
				if(arr[y-1]<temp-1){
					y--;
					temp=arr[y];
					ans+=temp;
				}
				if(y==0 or arr[y]==0 or temp==0)break;
			}
			c=max(c,ans);
			ans=0;
		}
		cout<<c<<'\n';

	}
	return 0;
}


#include <bits/stdc++.h>

#define endl '\n'
#define TRACE(x) cerr << #x << " = " << x << endl

using namespace std;
template<class T, class T1> inline bool chkmax(T &x, const T1 &y) { return x < y ? x = y, true : false; }
template<class T, class T1> inline bool chkmin(T &x, const T1 &y) { return x > y ? x = y, true : false; }

const int MAXN = (int)2e5;

int n;
int a[MAXN];

void read() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

void solve() {
    long long ans = a[n - 1];
    long long rest = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        long long add = max(min(rest - 1, a[i]), 0);
        ans += add;
        rest = add;
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return EXIT_SUCCESS;
}


#include <bits/stdc++.h>

#define endl '\n'
#define TRACE(x) cerr << #x << " = " << x << endl

using namespace std;
template<class T, class T1> inline bool chkmax(T &x, const T1 &y) { return x < y ? x = y, true : false; }
template<class T, class T1> inline bool chkmin(T &x, const T1 &y) { return x > y ? x = y, true : false; }

const int MAXN = (int)2e5;

int n;
int a[MAXN];

void read() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

void solve() {
    int idx = 0;
    for (int i = 1; i < n; i++) {
        if (a[i] > a[idx]) idx = i;
    }
    int ans = a[idx];
    int rest = a[idx];
    for (int i = idx - 1; i >= 0; i--) {
        int add = max(min(rest - 1, a[i]), 0);
        ans += add;
        rest = add;
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return EXIT_SUCCESS;
}


#include <bits/stdc++.h>

#define endl '\n'
#define TRACE(x) cerr << #x << " = " << x << endl

using namespace std;
template<class T, class T1> inline bool chkmax(T &x, const T1 &y) { return x < y ? x = y, true : false; }
template<class T, class T1> inline bool chkmin(T &x, const T1 &y) { return x > y ? x = y, true : false; }

const int MAXN = (int)2e5;

int n;
int a[MAXN];

void read() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

void solve() {
    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
        int curr = a[i];
        int rest = a[i];
        for (int j = i - 1; j >= 0; j--) {
            int add = max(min(rest - 1, a[j]), 0);
            curr += add;
            rest = add;
        }
        chkmax(ans, curr);
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return EXIT_SUCCESS;
}


#include <bits/stdc++.h>

#define endl '\n'
#define TRACE(x) cerr << #x << " = " << x << endl

using namespace std;
template<class T, class T1> inline bool chkmax(T &x, const T1 &y) { return x < y ? x = y, true : false; }
template<class T, class T1> inline bool chkmin(T &x, const T1 &y) { return x > y ? x = y, true : false; }

const int MAXN = (int)2e5;

int n;
int a[MAXN];

void read() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

void solve() {
    int ans = a[n - 1];
    int rest = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        int add = max(min(rest - 1, a[i]), 0);
        ans += add;
        rest = add;
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return EXIT_SUCCESS;
}


#include <bits/stdc++.h>

#define endl '\n'
#define TRACE(x) cerr << #x << " = " << x << endl

using namespace std;
template<class T, class T1> inline bool chkmax(T &x, const T1 &y) { return x < y ? x = y, true : false; }
template<class T, class T1> inline bool chkmin(T &x, const T1 &y) { return x > y ? x = y, true : false; }

const int MAXN = (int)2e5;

int n;
long long a[MAXN];

void read() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
}

void solve() {
    long long ans = a[n - 1];
    long long rest = a[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        long long add = max(min(rest - 1, a[i]), 0ll);
        ans += add;
        rest = add;
    }
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    read();
    solve();

    return EXIT_SUCCESS;
}


#include<iostream>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	long long t=a[n-1];
	int prev=a[n-1];
	for(int i=n-2;i>=0;i--){
		if(prev<=1||a[i]==0)break;
		t+=min(prev-1,a[i]);
		prev=min(prev-1,a[i]);
	}
	cout<<t;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
typedef long long ll;
using namespace std;

int read()
{
    int ans = 0;
    char ch = getchar();
    while (!isdigit(ch) && ch != '-') ch = getchar();
    bool sign = false;
    if (ch == '-') sign = true, ch = getchar();
    while (ch >= '0' && ch <= '9') ans = ans * 10 + (ch - '0'), ch = getchar();
    return (sign == true ? -ans : ans);
}

#define N 2E5 + 6
int n, a[N], x = 1e9 + 1;
ll ans;

int main()
{
	n = read();
    for (int i = 1; i <= n; i++) a[i] = read();
    for (int i = n; i; i--) {
        x = min(a[i], x - 1);
        if (x > 0) ans += x;
    }
    printf("%d\n", ans);
	return 0;
}


#include<stdio.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
inline int read()//快读
{
	register int x=0,t=0;register char c=getchar();for(;c<'0'||c>'9';t|=c=='-',c=getchar());
	for(;c>='0'&&c<='9';x=(x<<3)+(x<<1)+(c^48),c=getchar());return t?-x:x;
}
int n,a[200001];long long ans;
main()
{
	n=read();for(register int i=1;i<=n;++i)a[i]=read();
	for(register int i=n-1;i>0;--i)a[i]=max(min(a[i],a[i+1]-1),0);
	for(register int i=1;i<=n;++i)ans+=a[i];
	printf("%I64d",ans);
}/**/


#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;

//typedef vector<vector<pair<int,int>>> graph;

int a[100000];
int main()
{
	long long n,sum=0;
	int tmp;
	cin>>n;
	tmp=0x7fffffff;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=n;i>=1;i--)
	{
		tmp=min(a[i],tmp-1);
		if(tmp-1>=0)
			sum+=min(a[i],tmp);
	}
	cout<<sum;
}


#include<stdio.h>
using namespace std;
int a[200005];
int main(){
	int n,i,min=2147483647;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
    	scanf("%d",a+i);
	}
	long long ans=0;
    for(i=n;i>0;i--){
    	if(a[i]<(min-1))min=a[i];
    	else min--;
        if(min>0){
        	ans=ans+1ll*min;
		}
    }
    printf("%lld",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[200005];
int main()
{
	int i,j,k;
	int n,m;
	long long sum=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		sum+=a[i];
		a[i-1]=min(a[i]-1,a[i-1]);
		a[i-1]=max(a[i-1],0);
	}
	printf("%lld",sum);
	return 0;
}


#include<stdio.h>
#include<iostream>
using namespace std;
long long aa[200001];
long long cnt = 1e9+1;
long long sum = 0;
int main(){
	long long n;
	cin>>n;
    for(int i=1;i<=n;i++)
    cin>>aa[i];
    for(int i=n;i>0;i--)
    {
    	if(aa[i]<=cnt-1)
    	cnt = aa[i];
    	else
    	cnt = cnt-1;
    	if(cnt>=0)
    	sum = sum+cnt;
	}
    printf("%lld",sum);
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int a[200005];
int main()
{
	int i,j,k;
	int n,m,sum=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(i=n;i>=1;i--)
	{
		sum+=a[i];
		a[i-1]=min(a[i]-1,a[i-1]);
		a[i-1]=max(a[i-1],0);
	}
	printf("%d",sum);
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;

//typedef vector<vector<pair<int,int>>> graph;

int a[100000],mins[100000];
int main()
{
	long long n,sum=0;
	cin>>n;
	mins[n]=0x7fffffff;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=n-1;i>=1;i--)
	{
		mins[i]=min(a[i+1],mins[i+1]);
	}
	for(int i=1;i<=n;i++)
	{
		sum+=min(a[i],mins[i]-1);
	}
	cout<<sum;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
int read()
{
    int ans = 0;
    char ch = getchar();
    while (!isdigit(ch) && ch != '-') ch = getchar();
    bool sign = false;
    if (ch == '-') sign = true, ch = getchar();
    while (ch >= '0' && ch <= '9') ans = ans * 10 + (ch - '0'), ch = getchar();
    return (sign == true ? -ans : ans);
}

using namespace std;

const int N = 2e5 + 6;

int n, a[N], x = 1e9 + 1;
ll ans;

int main()
{
	n = read();
    for (int i = 1; i <= n; i++) a[i] = read();
    for (int i = n; i; i--)
	{
        x = min(a[i], x - 1);
        if (x > 0) ans += x;
    }
    printf("%d\n", ans);
    return 0;
}


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

int n, a[200005];
int main()
{
  cin >> n;

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

  long long count = a[0];
  int mx = a[0];
  long long best = INT_MIN, sum;
  if (n == 2){

    if (best > a[1]){
      best = a[1];
    }

  } else if (n == 1){
    best = a[0];
  }  else {
  for(int i=1;i<n;++i){
    if (a[i] > mx){
      count += a[i];
    } else {
      if (a[i] - 1 > 0){
      count = count - mx + a[i] + a[i] - 1;
      } else {
      count = a[i];
      }
    }

    mx = a[i];
    if (best < count){
      best = count;
    }

 //   cout << count << " " << best << "\n";

  }
  }

  cout << best << "\n";

  return 0;
}


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

int n, a[200005];
int main()
{
  cin >> n;

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

  long long count = a[0];
  int mx = a[0];
  long long best = a[0], sum;
  for(int i=1;i<n;++i){
    if (a[i] > mx){
      count += a[i];
    } else {
      if (a[i] - 1 > 0){
      count = count - mx + a[i] + a[i] - 1;
      } else {
      count = a[i];
      }
    }

    mx = a[i];
    if (best < count){
      best = count;
    }

 //   cout << count << " " << best << "\n";

  }

  cout << best << "\n";

  return 0;
}


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

int n, a[200005];
int main()
{
  cin >> n;

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

  long long count = a[0];
  int mx = a[0];
  long long best = INT_MIN, sum;
  if (n == 2){

    if (a[0] >= a[1]){
      best = a[1] + a[1] - 1;

    } else {
      best = a[0] +  a[1];
    }

  } else if (n == 1){
    best = a[0];
  }  else {
  for(int i=1;i<n;++i){
    if (a[i] > mx){
      count += a[i];
    } else {
      if (a[i] - 1 > 0){
      count = count - mx + a[i] + a[i] - 1;
      } else {
      count = a[i];
      }
    }

    mx = a[i];
    if (best < count){
      best = count;
    }

 //   cout << count << " " << best << "\n";

  }
  }

  cout << best << "\n";

  return 0;
}


#include <iostream>
using namespace std;
   long long n;

int main()
{
   cin >> n;

   long long arr[n];

   for(long long i=0;i<n;++i)
     cin>>arr[i];

   long long sum=arr[n-1];
   long long mx =arr[n-1]-1;

   for(long long i=n-2;i>=0;i--)
   {
     if(arr[i]>mx)
     {
         sum+=mx;
         mx = max(0LL,mx-1);
     } else {
        sum += arr[i];
        mx = max(0LL,arr[i]-1);
     }
}

cout<<sum<<"\n";

return 0;
}


#include <iostream>
using namespace std;
   int n;

int main()
{
   cin >> n;

   int arr[n];

   for(int i=0;i<n;++i)
     cin>>arr[i];

   long long sum=arr[n-1];
   long long mx =arr[n-1]-1;

   for(int i=n-2;i>=0;i--)
   {
     if(arr[i]>mx)
     {
         sum+=mx;
         mx = max(0LL,mx-1);
     } else {
       sum += arr[i];
      mx = max(0LL,arr[i]-1);
     }
}

cout<<sum<<"\n";

return 0;
}


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

int n, a[200005];
int main()
{
  cin >> n;

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

  long long count = a[0];
  int mx = a[0];
  long long best = a[0], sum;
  for(int i=1;i<n;++i){
    if (a[i] > mx){
      count += a[i];
    } else {
      int mx2 = a[i];
      long long count2 = a[i];
      for (int j=i-1;j>=0;j--){
        if (mx2 == 0){
          break;
        }
        if (a[j] >= mx2){
          count2 += mx2 - 1;
          mx2 -= 1;
        } else {
          count2 += a[j];
          mx2 = a[j];
        }
      }
      count = count2;
    }

    mx = a[i];
    if (best < count){
      best = count;
    }

 //   cout << count << " " << best << "\n";

  }

  cout << best << "\n";

  return 0;
}


#include <iostream>
using namespace std;
   int n;

int main()
{
   cin >> n;

   int arr[n];

   for(int i=0;i<n;++i)
     cin>>arr[i];

   long long sum=arr[n-1];
   long long mx =arr[n-1]-1;

   for(int i=n-2;i>=0;i--)
   {
     if(arr[i]>mx)
     {
         sum+=mx;
         mx = mx - 1;
     } else {
        sum += arr[i];
        mx = arr[i]-1;
     }
}

cout<<sum<<"\n";

return 0;
}


#include <iostream>
using namespace std;
   int n;

int main()
{
   cin >> n;

   int arr[n];

   for(int i=0;i<n;++i)
     cin>>arr[i];

   long long sum=arr[n-1];
   long long mx =arr[n-1]-1;

   for(int i=n-2;i>=0;i--)
   {
     if(arr[i]>mx)
     {
         sum += mx;
         if (mx - 1 > 0){
         mx = mx - 1;
         } else {
           mx = 0;
         }
     } else {
        sum += arr[i];
        if (arr[i] - 1 > 0){
         mx = mx - 1;
         } else {
           mx = 0;
         }
     }
}

cout<<sum<<"\n";

return 0;
}


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

int n, a[200005];
int main()
{
  cin >> n;

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

  long long count = a[0];
  int mx = a[0];
  long long best = INT_MIN, sum;
  if (n == 2){

    if (best > a[1]){
      best = a[1];
    }

  } else {
  for(int i=1;i<n;++i){
    if (a[i] > mx){
      count += a[i];
    } else {
      if (a[i] - 1 > 0){
      count = count - mx + a[i] + a[i] - 1;
      } else {
      count = a[i];
      }
    }

    mx = a[i];
    if (best < count){
      best = count;
    }

 //   cout << count << " " << best << "\n";

  }
  }

  cout << best << "\n";

  return 0;
}