stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

  

 

  

int main(){

  

    int n; 

    while(scanf("%d",&n),n){

 

        vector<pair<int,int> > rw(n);

        int sum_r=0,sum_w=0;

        for(int i=0;i<n;i++){

            cin>>rw[i].first>>rw[i].second;

            sum_r+=rw[i].first;

            sum_w+=rw[i].second;

        }

        sort(rw.begin(),rw.end(),greater< pair<int,int> >());

 

        int r0=rw[0].first;

        int r=sum_r-r0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

 

            int res=2*rw[0].first;

            vector<vector<bool> > dp(1001,vector<bool>(1000,false));

 

            dp[1][0]=true;

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

                for(int j=0;j<=1000;j++){

                    if(dp[i][j]){

                        dp[i+1][j]=true; 

                        if(j+rw[i].second<=1000)

                            dp[i+1][j+rw[i].second]=true; 

                    }

                }

            }

 

            int opt=0;

            for(int i=1;i<=r0-r;i++){

                if(dp[n][i]) opt=i;

            }

 

            res+=sum_w-opt;

            cout<<res<<endl;

 

 

 

        }

         

         

    }

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

   

  

   

int main(){

   

    int n; 

    int r[1000],w[1000];

    int sum_r=0,sum_w=0,idx=0;

	

    while(scanf("%d",&n),n){

         

        sum_r=sum_w=idx=0;

        for(int i=0;i<n;i++){

            scanf("%d %d",&r[i],&w[i]);

            sum_r+=r[i];

            sum_w+=w[i];

            if(r[idx]<r[i]) idx=i;

        }

  

        int r0=r[idx],r=sum_r-r0;

        w[idx]=0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

  

            int res=2*r0;

            //vector<vector<bool> > dp(1001,vector<bool>(1000,false));

  

//			bool cur[1001]={};

			bool *cur = (bool*)malloc( sizeof(bool)*(1000+2) );

            cur[0]=true;

            for(int i=0;i<n;i++){

				bool *next = (bool*)malloc( sizeof(bool)*(1000+2) );

                for(int j=0;j<=r0-r;j++){

                    if(cur[j]){

                        next[j]=true; 

                        if(j+w[i]<=r0-r)

                            next[j+w[i]]=true; 

                    }

                }

				swap(cur,next);

            }

  

            int opt=r0-r;

            while(!cur[opt]) opt--;

  

            res+=sum_w-opt;

            printf("%d\n",res);

  

  

  

        }

          

          

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

   

  

   

int main(){

   

    int n; 

    int r[1000],w[1000];

    int sum_r=0,sum_w=0,idx=0;

    while(scanf("%d",&n),n){

         

        sum_r=sum_w=idx=0;

        for(int i=0;i<n;i++){

            scanf("%d %d",&r[i],&w[i]);

            sum_r+=r[i];

            sum_w+=w[i];

            if(r[idx]<r[i]) idx=i;

        }

  

        int r0=r[idx],r=sum_r-r0;

        w[idx]=0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

  

            int res=2*r0;

            //vector<vector<bool> > dp(1001,vector<bool>(1000,false));

  

			bool dp[1001][1000]={};

            dp[0][0]=true;

            for(int i=0;i<n;i++){

                for(int j=0;j<1000;j++){

                    if(dp[i][j]){

                        dp[i+1][j]=true; 

                        if(j+w[i]<1000)

                            dp[i+1][j+w[i]]=true; 

                    }

                }

            }

  

            int opt=r0-r;

            while(!dp[n][opt]) opt--;

  

            res+=sum_w-opt;

            printf("%d\n",res);

  

  

  

        }

          

          

    }

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

   

  

   

int main(){

   

    int n; 

    int r[1000],w[1000];

    int sum_r=0,sum_w=0,idx=0;

	

    while(scanf("%d",&n),n){

         

        sum_r=sum_w=idx=0;

        for(int i=0;i<n;i++){

            scanf("%d %d",&r[i],&w[i]);

            sum_r+=r[i];

            sum_w+=w[i];

            if(r[idx]<r[i]) idx=i;

        }

  

        int r0=r[idx],r=sum_r-r0;

        w[idx]=0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

  

            int res=2*r0;

            //vector<vector<bool> > dp(1001,vector<bool>(1000,false));

  

//			bool cur[1001]={};

			bool *cur = (bool*)malloc( sizeof(bool)*(1000+2) );

            cur[0]=true;

            for(int i=0;i<n;i++){

				bool *next = (bool*)malloc( sizeof(bool)*(1000+2) );

                for(int j=0;j<=r0-r;j++){

                    if(cur[j]){

                        next[j]=true; 

                        if(j+w[i]<=r0-r)

                            next[j+w[i]]=true; 

                    }

                }

				swap(cur,next);

            }

  

            int opt=r0-r;

            while(!cur[opt]) opt--;

  

            res+=sum_w-opt;

            printf("%d\n",res);

  

  

  

        }

          

          

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

   

  

   

int main(){

   

    int n; 

    int r[1000],w[1000];

    int sum_r=0,sum_w=0,idx=0;

	bool dp[1001][1000]={};

    while(scanf("%d",&n),n){

         

        sum_r=sum_w=idx=0;

        for(int i=0;i<n;i++){

            scanf("%d %d",&r[i],&w[i]);

            sum_r+=r[i];

            sum_w+=w[i];

            if(r[idx]<r[i]) idx=i;

        }

  

        int r0=r[idx],r=sum_r-r0;

        w[idx]=0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

  

            int res=2*r0;

            //vector<vector<bool> > dp(1001,vector<bool>(1000,false));

  

			fill(&dp[0][0],&dp[1001][0],false);

            dp[0][0]=true;

            for(int i=0;i<n;i++){

                for(int j=0;j<1000;j++){

                    if(dp[i][j]){

                        dp[i+1][j]=true; 

                        if(j+w[i]<1000)

                            dp[i+1][j+w[i]]=true; 

                    }

                }

            }

  

            int opt=r0-r;

            while(!dp[n][opt]) opt--;

  

            res+=sum_w-opt;

            printf("%d\n",res);

  

  

  

        }

          

          

    }

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <ctime>

#include <cstdio>

#include <functional>

#include <set>

#include <queue>

#include <cctype>

#include <climits>

#include <stack>

using namespace std;

   

  

   

int main(){

   

    int n; 

    int r[1000],w[1000];

    int sum_r=0,sum_w=0,idx=0;

	

    while(scanf("%d",&n),n){

         

        sum_r=sum_w=idx=0;

        for(int i=0;i<n;i++){

            scanf("%d %d",&r[i],&w[i]);

            sum_r+=r[i];

            sum_w+=w[i];

            if(r[idx]<r[i]) idx=i;

        }

  

        int r0=r[idx],r=sum_r-r0;

        w[idx]=0;

 

        if(r0 <= r){

            cout<<sum_r+sum_w<<endl;

        }else{

  

            int res=2*r0;

            //vector<vector<bool> > dp(1001,vector<bool>(1000,false));

  

//			bool cur[1001]={};

			bool *cur = (bool*)malloc( sizeof(bool)*(1000+2) );

            cur[0]=true;

            for(int i=0;i<n;i++){

				bool *next = (bool*)malloc( sizeof(bool)*(1000+2) );

                for(int j=0;j<=r0-r;j++){

                    if(cur[j]){

                        next[j]=true; 

                        if(j+w[i]<=r0-r)

                            next[j+w[i]]=true; 

                    }

                }

				swap(cur,next);

            }

  

            int opt=r0-r;

            while(!cur[opt]) opt--;

  

            res+=sum_w-opt;

            printf("%d\n",res);

  

  

  

        }

          

          

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<cstdio>

#include<iostream>

#include<algorithm>

#include<string>

#include<math.h>

#include<vector>

#include<queue>

#include<map>

#include<stack>

#include<fstream>

#include<stdlib.h>

#include<set>

#include<climits>

#include<cmath>

#include<memory.h> 

#include<sstream>

#include<time.h>

using namespace std;

const long long BIGER=1000000000000000;

const int BIG=1000000000;

long long a[101];

long long g=1;

string st;

long long s=0;

long long ypa(long long k)

{

	if(k==st.size())

	{

		for(long long i=1;i<=g;i++)

		{

			cerr<<a[i]<<" ";

			s+=a[i];

		}

		cerr<<endl;

	}

	else

	{

		a[g]=a[g]*10+st[k]-'0';

		ypa(k+1);

		a[g]=(a[g]-st[k]+'0')/10;

		g++;

		a[g]=a[g]*10+st[k]-'0';

		ypa(k+1);

		a[g]=(a[g]-st[k]+'0')/10;

		g--;

	}

	return 19260817;

}

int main()

{

	cin>>st;

	a[1]=st[0]-'0';

	ypa(1);

	cout<<s;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char a[11];

int add[10],len;

long long ans=0;

void getsum1()

{

    long long temp=0;

    for (int i = 0; i < len; i++)

    {

        if(add[i])

        {

            ans+=temp;

            temp=0;

        }

        temp=temp*10+a[i]-'0';

    }

    ans+=temp;

}

void getsum2()

{

    long long temp = a[0]-'0';

    for (int i = 1; i < len; i++)

    {

        if (!add[i])

        {

            ans += temp;

            temp = 0;

        }

        temp = temp * 10 + a[i] - '0';

    }

    ans += temp;

}

void dfs(int pos,int num)

{

    if(!num)

    {

        getsum1();

        //getsum2();

        return;

    }

    for (int i = pos; i < len-num+1; i++)

    {

        add[i]=1;

        dfs(i+1,num-1);

        add[i]=0;

    }

}

int main()

{

    scanf("%s",a);

    len=strlen(a);

    for (int i = 0; i < len; i++)

    {

        dfs(1,i);

    }

    printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


 //In The Name Of GOD

 

#include <bits/stdc++.h>



using namespace std;



typedef long long ll ;



const int mod = 1000000007;

const int inf = 2000000000;

const int maxn = 500*1000+500*1000+15;

const int MLOG = 18;



#define int long long

#define pb push_back

#define pp pop_back

#define X first

#define Y second

#define IO ios_base::sync_with_stdio(false);

#define sz(a) (int)(a.size())



int32_t main(){

	string s;

	cin >> s;

	int n =  s.size();

	int all = 0;

	for(int i = 0; i < ( 1 << (n-1) ) ; i++){

		string ss;

		int ans=0;

		for(int j = 0; j < n; j++){

			ss += s[j];

			if( (i >> j) & 1 ){

				ans += atoll( ss.c_str() );

				ss.clear();

			}

		}

		ans += atoll( ss.c_str() );

		all += ans;

	}

	cout << all;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char a[11];

int add[10],len;

long long ans=0;

void getsum1()

{

    long long temp=0;

    for (int i = 0; i < len; i++)

    {

        if(add[i])

        {

            ans+=temp;

            temp=0;

        }

        temp=temp*10+a[i]-'0';

    }

    ans+=temp;

}

void getsum2()

{

    long long temp = a[0]-'0';

    for (int i = 1; i < len; i++)

    {

        if (!add[i])

        {

            ans += temp;

            temp = 0;

        }

        temp = temp * 10 + a[i] - '0';

    }

    ans += temp;

}

void dfs(int pos,int num)

{

    if(!num)

    {

        getsum1();

        //getsum2();

        return;

    }

    for (int i = pos; i < len-num+1; i++)

    {

        add[i]=1;

        dfs(i+1,num-1);

        add[i]=0;

    }

}

int main()

{

    scanf("%s",a);

    len=strlen(a);

    for (int i = 0; i < len; i++)

    {

        dfs(1,i);

    }

    printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <fstream>

#include <algorithm>

#include <cmath>

#include <deque>

#include <vector>

#include <queue>

#include <string>

#include <cstring>

#include <map>

#include <stack>

#include <set>

#include <sstream>

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);

#define Mod 1000000007

#define eps 1e-6

#define ll long long

#define INF 0x3f3f3f3f 

#define MEM(x,y) memset(x,y,sizeof(x))

#define Maxn 100005

#define P pair<int,int>

using namespace std;

int main()

{

	char a[15];

	scanf("%s", &a);

	int q = 1 << (strlen(a) - 1);

	ll sum = 0;

	

	for (int i = 0; i < q; i++)

	{

		ll temp = a[0] - '0';

		for (int j = 0; j < strlen(a); j++)

		{

			if (i >> j & 1)

			{

				temp = temp * 10 + a[j + 1] - '0';

			}

			else

			{

				sum += temp;

				temp = a[j + 1] - '0';

			}

		}

	}

	cout << sum;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

char a[11];

int add[10],len;

long long ans=0;

void getsum1()

{

    long long temp=0;

    for (int i = 0; i < len; i++)

    {

        if(add[i])

        {

            ans+=temp;

            temp=0;

        }

        temp=temp*10+a[i]-'0';

    }

    ans+=temp;

}

void getsum2()

{

    long long temp = a[0]-'0';

    for (int i = 1; i < len; i++)

    {

        if (!add[i])

        {

            ans += temp;

            temp = 0;

        }

        temp = temp * 10 + a[i] - '0';

    }

    ans += temp;

}

void dfs(int pos,int num)

{

    if(!num)

    {

        getsum1();

        //getsum2();

        return;

    }

    for (int i = pos; i < len-num+1; i++)

    {

        add[i]=1;

        dfs(i+1,num-1);

        add[i]=0;

    }

}

int main()

{

    scanf("%s",a);

    len=strlen(a);

    for (int i = 0; i < len; i++)

    {

        dfs(1,i);

    }

    printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std; 

long long dp[500000]; 

inline long long stoii(string s)

{

	long long res=0;

	for(int i=0; i<s.size(); i++)

		res*=10, res+=(s[i]-'0');

	return res;	

}

int main() 

{  

    string s;

    cin>>s;

    memset(dp, 0, sizeof(dp));

    dp[0]=1, dp[1]=1, dp[2]=2, dp[3]=4, dp[4]=8, dp[5]=16, dp[6]=32, dp[7]=64, dp[8]=128, dp[9]=256, dp[10]=512;

    long long ans=0;

    int len=s.size();

    for(int i=1; i<=len; i++)

    	for(int j=0; (j+i)<=len; j++)

    		ans+=(long long)(stoii(s.substr(j, i))*dp[j]*dp[len-i-j]);

	cout<<ans;

    return 0;  

}  


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int main() 

{

	string s;

	cin>>s;

	long long res=0;

	for(int t=0; t<(1<<(s.size()-1)); t++)

	{

		long long tmp=s[0]-'0';

		for(int i=0; i<s.size()-1; i++)

		{

			if(t&(1<<i))

				res+=tmp, tmp=0;

			tmp*=10, tmp+=s[i+1]-'0';

		}

		res+=tmp;

	}

	cout<<res;

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cmath>

using namespace std;

int c[2]={0,1};

 

int main()

{

	int n,i=0,k,t;

	int s[3000]={0};

	cin>>n;

	if(n==0)

	{

		cout<<"0"<<endl;

		return 0;

	}

	while(n!=0)

	{

		k=n%(-2);

		t=n/(-2);

		if(k<0)

		{

			k=k-(-2);

			t++;

		}	

		i++;

		n=t;

		s[i]=c[k];	

	}

	for(;i>=1;i--)

		cout<<s[i]; 

	return 0; 

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

 

int num[50];

int main()

{

    int n;

    scanf("%d",&n);

    int cnt=0;

     if(n==0){

        printf("0\n");

        return 0;

    }

    while(n){

        if(n%2!=0){

            num[++cnt]=1;

            n=(n-1)/(-2);

        }else{

            num[++cnt]=0;

            n=n/(-2);

        }

    }

 

    for(int i=cnt;i>=1;i--)

        printf("%d",num[i]);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<algorithm>

#include<stack>

#include<queue>

#define INF 0x3f3f3f3f

#define lowbit(x) x & -x

#define lson root<<1,l,mid

#define rson root<<1|1,mid+1,r

using namespace std;

typedef long long ll;

const int mod=1e9+7;

const int N=2e5+5;

int a[N];

bool cmp(int a,int b){

    return a>b;

}

int main(){

#ifdef Mizp

    freopen("in.txt","r",stdin);

#endif

    int n;

    scanf("%d",&n);

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

        scanf("%d",&a[i]);

    }

    sort(a+1,a+1+n,cmp);

    ll sum=0;

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

        sum+=a[(i+1)/2];

    }

    printf("%lld\n",sum);

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

using namespace std;

long long a[200010];

int t[200010];

int main()

{

    int n;

    long long ans=0;

    scanf("%d",&n);

    for(int i=0;i<n;i++)scanf("%lld",&a[i]);

    sort(a,a+n);

    t[n-1]=1;int j=n-1;

    for(int i=1;i<n;i++)

    {

    	if(t[j]==2)j--;

    	ans+=a[j];t[j]++;

    	

	}

	printf("%lld ",ans);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<int> a;

ll sum;

int main() {

    int n;

    cin >> n;

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

        int x;

        cin >> x;

        a.push_back(x);

    }

    sort(a.begin(), a.end());

    if (n > 1) {

        sum += a[n - 1];

    }

    int k = n - 2;

    for (n = n - 2; n >= 2; k--, n -= 2) {

        sum += a[k] * 2;

    }

    if (n == 1) {

        sum += a[k];

    }

    cout << sum << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

using namespace std;

long long a[200010];

int t[200010];

int main()

{

    int n;

    long long ans=0;

    scanf("%d",&n);

    for(int i=0;i<n;i++)scanf("%lld",&a[i]);

    sort(a,a+n);

    t[n-1]=1;int j=n-1;

    for(int i=1;i<n;i++)

    {

    	if(t[j]==2)j--;

    	ans+=a[j];t[j]++;

    	

	}

	printf("%lld ",ans);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f

#define maxn 200005

#define ll long long



void DBG() {

    cerr << "]\n";

}

template <class H, class... T> void DBG(H h, T... t) {

    cerr << h;

    if (sizeof...(t)) {

        cerr << ", ";

    }

    DBG(t...);

}

#define _DEBUG

#ifdef _DEBUG

#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)

#else

#define dbg(...) 0

#endif



int tt, n, k;

int a[maxn];



int main() {

    cin.tie(0);

    ios::sync_with_stdio(false);



    cin >> n;

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

        cin >> a[i];

    }

    sort(a + 1, a + n + 1);

    ll ans = 0;

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

        ans += a[n - i / 2];

    }



    cout << ans;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

using namespace std;

long long a[200010];

int t[200010];

int main()

{

    int n;

    long long ans=0;

    scanf("%d",&n);

    for(int i=0;i<n;i++)scanf("%lld",&a[i]);

    sort(a,a+n);

    t[n-1]=1;int j=n-1;

    for(int i=1;i<n;i++)

    {

    	if(t[j]==2)j--;

    	ans+=a[j];t[j]++;

    	

	}

	printf("%lld ",ans);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define maxn 200010

#define ll long long

using namespace std;

ll n,a[maxn],ans;

bool cmp(int aa,int bb){return aa>bb;}

int main(){

	scanf("%lld",&n);

	for(int i=1;i<=n;++i) scanf("%lld",&a[i]);

	sort(a+1,a+n+1,cmp);

	if(n&1){

		n=n/2;

		for(int i=1;i<=n;++i)

		ans+=(a[i]*2);

		ans-=a[1];

		ans+=a[n+1];

	}

	else{

		n=n/2;

		for(int i=1;i<=n;++i)

		ans+=(a[i]*2);

		ans-=a[1];

	}

	cout<<ans;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

using namespace std;

long long a[200010];

int t[200010];

int main()

{

    int n;

    long long ans=0;

    scanf("%d",&n);

    for(int i=0;i<n;i++)scanf("%lld",&a[i]);

    sort(a,a+n);

    t[n-1]=1;int j=n-1;

    for(int i=1;i<n;i++)

    {

    	if(t[j]==2)j--;

    	ans+=a[j];t[j]++;

    	

	}

	printf("%lld ",ans);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#include<algorithm>

using namespace std;

typedef long long ll;

const ll MAXN = 1e5+200;

int main(){

    ll a[MAXN] = {0};

    ll n;

    cin>>n;

    for(ll i = 0; i < n; i++){

      cin>>a[i];

    }

    ll k = a[0];

    ll sum = 0;

    for(int i= 0; i < n; i++){

      if(i == n -1){

        if(a[i] >= k){

          k = a[i];

        }

        sum+=k;

      }else{

        if(a[i] >= k){

          k = a[i];

        }else{

          sum += k - a[i];

          k = a[i];

        }

      }



    }

    cout<<sum<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<cmath>

#include<map>

#include<queue>

#include<set>

#include<stack>

#include<string>

#include<vector>

using namespace std;

typedef long long LL;

LL n;



vector<LL>v;

 LL sum=0;

LL b[100005]={0};

struct node

{

	 LL v,i;

};

bool cmp(const node a,const node b)

{ 

 return a.v<b.v;

}

struct node a[100005];

void bfs(LL index,LL t)

{

LL i=index;

	sum+=(t-b[i]);

	while(i>=0)

	 {

	 	 if(v[i]<t)

	 	 	break;

	 	 	b[i]=t;

			

		     i--;

	 }

	 i=index;

	 while(i<n)

	 {

	 	if(v[i]<t)

	 	 break;

		 

		b[i]=t;

		  i++;

	 }

 } 

int main()

{

 cin>>n;

  for(LL i=0;i<n;i++)

  {

  	cin>>a[i].v;

  	a[i].i=i;

  	v.push_back(a[i].v);

  }

 

  sort(a,a+n,cmp);

  for(LL i=0;i<n;i++)

  {

  	bfs(a[i].i,a[i].v);

//  	cout<<i<<" "<<a[i].v<<" "<<a[i].i<<endl;

//  	for(int i=0;i<n;i++)

//  	cout<<b[i]<<" ";

//  	cout<<"sum= "<<sum;

//  	cout<<"\n";

  }

//  for(int i=0;i<v.size();i++)

//  cout<<v[i]<<" ";

  cout<<sum;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int main()

{

	int a;

	string s;

	cin >> a >> s;

	if(a >= 3200) cout << s << endl;

	else cout << "red" << endl;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int main(){

    int a;

    scanf("%d",&a);

    char s[100];

    scanf("%s",s);

    

    

    if(a>=3200)

    printf("%s",s);

    else

    printf("red");

    return 0;

}

// Fastest known solution for this problem: