akhauriyash/Code-Regression huggingface dataset:
program abc169b
  use iso_c_binding
  implicit none

  integer :: n, i
  integer(c_int128_t) :: x, a(100000)
  integer(c_int128_t),parameter :: mx = 10_8**18

  read *, n
  read *, a(1:n)

  if (any(a(1:n) == 0)) then
     print *, 0
     stop
  end if

  x = 1

  do i = 1,n
     x = x * a(i)
     if (x > mx) then
        x = -1
        exit
     end if
  end do

  print *, x
end program abc169b


Measured memory_bytes: 4536
#include<iostream>
#include<algorithm>
bool cmp (const int a, const int b)
{
    return a < b;
}
using namespace std;
int main(){
    int a,b,c,temp;
    cin>>a>>b>>c;
    int d[3]={a,b,c};
    sort(d, d + 3, cmp);
    cout<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl;
    return 0;
}


Measured memory_bytes: 3112
c = 'abcdefghij'

def join(s, k, n):
    if len(s) == n:
        return [s]
    ans = []
    for i in range(k):
        ans += join(s + c[i], k, n)
    ans += join(s + c[k], k + 1, n)
    return ans

n = int(input())
ans = join('', 0, n)
for x in ans:
    print(x)


Measured memory_bytes: 12824
while True:
  t = int(input())
  if t == 0:
    break
  n = int(input())
  acc = 0
  for _ in range(n):
    s, f = map(int, input().split())
    acc += f - s
  if acc >= t:
    print("OK")
  else:
    print(t - acc)


Measured memory_bytes: 5596
#include<stdio.h>
int main(void){
    int H, W, i, j;
    while(scanf("%d %d", &H, &W)){
     if(H == 0 && W == 0)break;
     for(i = 0; i < H; i++){
         for(j = 0; j < W; j++){
            printf("#"); 
         }
         printf("\n");
     }
     printf("\n");
    }
    
    return 0;   
}


Measured memory_bytes: 2056
#include<iostream>
#include<cstring>
using namespace std;

int main(){
	int n;
	int a[10000][4],point[1000],juni[1000],c,d;
	memset(point,0,sizeof(point));
	cin>>n;
	c=1;
	d=0;
	for(int i=0;i<(n*(n-1)/2);i++){
		cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3];
		if(a[i][2]>a[i][3]){
			point[a[i][0]] += 3;
		}
		else if(a[i][2]<a[i][3]){
			point[a[i][1]] += 3;
		}
		else{
			point[a[i][0]] += 1;
			point[a[i][1]] += 1;
		}
	}
	for(int i=0;i<2000;i++){
		for(int j=1;j<=n;j++){
			if(point[j]==(1999-i)){
				juni[j]=c;
				d++;
			}
		}
		c += d;
		d=0;
	}
	for(int i=1;i<=n;i++){
		cout<<juni[i]<<endl;
	}
	return 0;
}

Measured memory_bytes: 1248
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a,b,c,d;
    int ans=0;
    cin >> a>>b>>c>>d;
    vector<int> A(101);

    for (int i = 0; i < 101; i++){
        A.at(i)=0;
        if(i>=a&&i<=b) A.at(i)=1;
    }
    for (int i = c; i <= d; i++){
        if(A.at(i)==1) A.at(i)=2;
    }
    for (int i = 0; i < 101; i++){
        if(A.at(i)==2) ans++;
    }

    if(ans!=0) ans--;
    cout << ans << endl; 
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
typedef long long ll;

int main(){
	int M, D, ans = 0; cin >> M >> D;
	for(int i=1; i<=M; i++){
		for(int j=22; j<=D; j++){
			int a = j % 10;
			int b = (j-a)/10;
			if(a >=2 && b >= 2 && i == a*b) ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#import<bits/stdc++.h>
using namespace std;long long N,a,b,n,i;long long s[2];main(){scanf("%lld", &N);scanf("%lld %lld",s,s+1);for(i=1;i<N;++i){scanf("%lld %lld",&a,&b);n=max(((s[0]-1)/a+1),((s[1]-1)/b+1));*s=a*n;s[1]=b*n;}cout<<s[0]+s[1]<<endl;}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define mod ((ll)1e9+7)
#define inf ((ll)1e18+5)
#define maxx ((ll)1e6+5)
#define pb push_back
#define endl "\n"
#define time(i) cout<<"\nTime Elapsed "<<i<<" : " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
#define usefile freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define test ll t; cin>>t; while(t--)
ll exp(ll x,ll y,ll p){ ll res=1; while(y) {if(y%2) res=(res*x%p)%p; x=(x*x)%p; y/=2; } return res;}
ll expm(ll x,ll y){ ll res=1; while(y) {if(y%2) res=(res*x%mod)%mod; x=(x*x)%mod; y/=2; } return res;}
/*
bool sv[maxx]={0};    // returns 0 if a num is prime
int sp[maxx]={0};     // gives smallest prime factor of the number
void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i = 3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j += 2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}}
ll fact[maxx]={0}; // returns factorial of that number
void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;}
ll ncrm(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod; res=(res*(expm(fact[n-r],mod-2)))%mod; return res;}
ll inv[maxx];   // returns the modular inverse of number
void inversify( ll m){inv[1]=1;for(int i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}}
ll ncr(ll n,ll r,ll m){int ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;}
*/
signed main(){
  fast;
  //usefile;
  //test   
  {
    int n,m;
    cin>>n>>m;
    vector<int> v(n);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        sum+=v[i];
    }
    sum=ceil(sum*1.0/(4*1.0*m));
    sort(v.begin(),v.end());
    int a=lower_bound(v.begin(),v.end(),sum)-v.begin();
     a=n-a;
    if(a>=m)
        cout<<"Yes";
    else 
        cout<<"No";
  }
  //time("");
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
int gcd(int u,int v) {
    while ( v != 0) {
        unsigned r = u % v;
        u = v;
        v = r;
    }
    return u;
}
int i,j,k,count1=0,count2=0;
void ABC161();
void ABC162();
void ABC163();
void ABC164();

int main(){
    ABC162();
}

void ABC162(){
    int k,l,m,sum=0,max;
    cin>>k;
    for(i=1;i<=k;i++){
        for(j=1;j<=k;j++){
            for(l=1;l<=k;l++){
                sum+=gcd(i,gcd(j,l));
            }
        }
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 3644
#include <iostream>
#include <vector>

int Calc(std::vector<int>A)
{
	int N = A.size();
	int  digit = 0;

	int small = 0, large = 0, num = 1;
	for (int i = 0; i < N; i++)
	{
		num *= (N - i);
	}

	std::vector<int>S = { 1, 2, 3, 4, 5, 6, 7, 8 };
	do
	{
		int a = 0;
		for (int j = 0; j < S.size(); j++){
			if (S[j] == A[digit]) a = j + 1;
		}
		int den = 1;
		for (int j = 0; j < digit + 1; j++)
		{
			den *= (N - j);
		}
		S.erase(S.begin() + (a - 1));
		small += num *  (a - 1) / den + 1;
		large = small +  num  / den - 1;

		digit++;
	} while (large - small >= 1);
	return small;
}

int main()
{
	int N = 0, ans = 0;
	std::cin >> N;

	std::vector<int>P(N), Q(N);
	for (int i = 0; i < N; i++){
		std::cin >> P[i];
	}
	for (int i = 0; i < N; i++){
		std::cin >> Q[i];
	}

	int a = Calc(P);
	int b = Calc(Q);

	ans = (a >= b) ? a - b : b - a;

	std::cout << ans << std::endl;

	return 0;
}

Measured memory_bytes: 256
/*
			███╗   ███╗ █████╗ ███████╗███╗   ███╗ ██████╗ ██╗   ██╗██████╗ ███████╗ █████╗ ███╗   ███╗██████╗ ██╗   ██╗
			████╗ ████║██╔══██╗╚════██║████╗ ████║██╔═══██╗██║   ██║██╔══██╗╚════██║██╔══██╗████╗ ████║██╔══██╗╚██╗ ██╔╝
			██╔████╔██║███████║    ██╔╝██╔████╔██║██║   ██║██║   ██║██║  ██║    ██╔╝███████║██╔████╔██║██║  ██║ ╚████╔╝
			██║╚██╔╝██║██╔══██║   ██╔╝ ██║╚██╔╝██║██║   ██║██║   ██║██║  ██║   ██╔╝ ██╔══██║██║╚██╔╝██║██║  ██║  ╚██╔╝
			██║ ╚═╝ ██║██║  ██║   ██║  ██║ ╚═╝ ██║╚██████╔╝╚██████╔╝██████╔╝██╗██║  ██║  ██║██║ ╚═╝ ██║██████╔╝   ██║
			╚═╝     ╚═╝╚═╝  ╚═╝   ╚═╝  ╚═╝     ╚═╝ ╚═════╝  ╚═════╝ ╚═════╝ ╚═╝╚═╝  ╚═╝  ╚═╝╚═╝     ╚═╝╚═════╝    ╚═╝

			“Nobody but you have to believe in your dreams to make them a reality.” ― Germany Kent
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy                 \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define Open_Sesame Open()
#define pb push_back
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(arr, x) memset(arr, x, sizeof arr)
#define endl "\n"
#define Accepted 0
#define watch(x) cout << #x << " = " << x << endl;
#define un_map unordered_map
#define RT(x) return cout << (x), 0;
#define sc(a) scanf("%d", &a)
#define scll(x) scanf("%I64d", &x)
#define scan(s) scanf("%[^\n]%*c", &s)
#define pr(s) printf("%s", s.c_str()); // print string
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double EPS = -1e-2;
const int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
const int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const ll mod = 1000000007;
const int Mx = INT_MAX;
const int Mn = INT_MIN;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const int N = 1e5 + 1;
void Open()
{
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif
}
int n, k;
int dp[N];
//int mem[N];
vector<int>v;
/*int solve(int i) {
	int& ret = mem[i];
	if (~ret)return ret;
	ret = Mx;
	if (i + 1 < n)
		ret = min(ret, solve(i + 1) + abs(v[i] - v[i + 1]));
	if (i + 2 < n)
		ret = min(ret, solve(i + 2) + abs(v[i] - v[i + 2]));
	ret = ret == Mx ? 0 : ret;
	return ret;
}*/
int main()
{
	Ma7moud_7amdy;
	Open_Sesame;
	//clr(mem, -1);
	cin >> n;
	v = vector<int>(n);
	for (int i = 0; i < n; i++)cin >> v[i];
	//cout << solve(0) << endl;;

	for (int i = 0; i < n; i++) {
		int& ret = dp[i];
		ret = Mx;
		if (i - 1 >= 0)
			ret = min(ret, dp[i - 1] + abs(v[i] - v[i - 1]));
		if (i - 2 >= 0)
			ret = min(ret, dp[i - 2] + abs(v[i] - v[i - 2]));

		ret = ret == Mx ? 0 : ret;
	}
	
	cout << dp[n - 1] << endl;
	return Accepted;
}


Measured memory_bytes: 4008
#include<cstdio>
#include<bitset>
#include<algorithm>
using namespace std;
int main()
{
	const int N = 1000000;
	bitset<N> isPrime;
	static int lpCount[N] = {0};
	int i,j;
	isPrime.set();
	isPrime[0] = isPrime[1] = false;
	for(i = 2; i < N; ++i)
	{
		if(isPrime[i])
		{
			for(j = i*2; j<N; j+=i)
				isPrime[j] = false;
			lpCount[i] = lpCount[i-1]+1;
		}
		else
			lpCount[i] = lpCount[i-1];
	}

	int p,m,n,s,l,r;
	while(scanf("%d",&n),n)
	{
		s=0;
		while(n--)
		{
			scanf("%d%d",&p,&m);
			l = max(0,p-m);
			r = min(N-1,p+m);
			s += (lpCount[r]-lpCount[l]+isPrime[l])-1;
		}
		printf("%d\n",s);
	}
	
	return 0;
}

Measured memory_bytes: 0
#include <iostream>
//#include <algorithm>
#include <stdio.h>
//#include <math.h>

using namespace std;

int main(void){
    // Your code here!
int a,b;

//scanf("%d %d",&a,&b);

while( scanf("%d %d",&a,&b)!=EOF && !(a==0 && b==0) ){ //(a!=0 && b!=0) ){
if( a>b ){
    printf("%d %d\n",b,a);
}else printf("%d %d\n",a,b);

}
    return 0;
}



Measured memory_bytes: 3088
n = int(input());
s = [];
alphabets=['a', 'b', 'c', 'd', 'e', 'f', 'g',
           'h', 'i', 'j', 'k', 'l', 'm', 'n',
           'o', 'p', 'q', 'r', 's', 't', 'u',
           'v', 'w', 'x', 'y', 'z']

for i in range(0, n):
    s.append(input());

for alp in alphabets:
    num=9999999999;
    for ss in s:
        if num > ss.count(alp):
            num = ss.count(alp);
    print(alp * num, end='');


Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 998244353;
const ll MAX = 1000001;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

ll gcd(ll a, ll b) {
	if (b == 0) { return a; }
	if (a < b) { return gcd(b, a); }
	return gcd(b, a%b);
}

ll lcm(ll a, ll b) {
	return a / gcd(a, b) *b;
}

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};


///////////////////////////



int main() {
	
	ll N;
	cin >> N;

	vector<ll> A(N + 1),B(N+1);
	repn(i, N) cin >> A[i] >> B[i];

	vector<pair<ll, ll>> C(N+1);
	repn(i, N) {
		C[i] = { A[i] + B[i],i };
	}

	sort(++C.begin(), C.end(), greater<pair<ll,ll>>());

	ll t = 0;
	ll a = 0;

	repn(i, N) {
		if (i % 2 == 1) { t += A[C[i].second]; }
		else { a += B[C[i].second]; }
	}

	cout << t - a;
	

	system("PAUSE");
}


Measured memory_bytes: 3576
#include<iostream>
using namespace std;
int main (){
  int N,C,c;
  int sum=0;
  cin >> N >> C ;
  for (int i=0; i<C; i++){
    cin >> c ;
    sum=sum+c;
  }
  if(sum%(N+1)==0){
    cout << sum/(N+1) << endl;
  }else {
    cout << sum/(N+1)+1 << endl;
  }
}



Measured memory_bytes: 3116
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<iomanip>
#include<set>
#include<queue>

using namespace std;

int main()
{
	int l,r,d;
	cin >> l >> r >> d;
	cout << r/d-(l-1)/d << endl;




}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define repr(i,x,n) for(int i=x; i<(n); i++)
#define ALL(x) (x).begin(),(x).end()
typedef long long ll;
#define INF 1e9
typedef vector<int> VI;
typedef vector<string> VS;
int main()
{
int k,n;
cin>>k>>n;
VI Circumference(n),dis(n);
for (int i = 0; i < n; i++)
{
   cin>>Circumference.at(i);
}
for (int i = 0; i < n-1; i++)
{
  dis.at(i)=Circumference.at(i+1)-Circumference.at(i);  
}
dis.push_back(k-Circumference.back()+Circumference.front()); 
sort(ALL(dis));
int ans=0;
for (int j = 0; j < n; j++)
{
    ans+=dis.at(j);
}

cout<<ans-dis.at(0)<<endl;


    return 0;
}

Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
    int N;
    cin>>N;
    if(N%10==3){
        cout<<"bon"<<endl;
    }
    else if(N%10==0){
        cout<<"pon"<<endl;
    }
    else if(N%10==1){
        cout<<"pon"<<endl;
    }
    else if(N%10==6){
        cout<<"pon"<<endl;
    }
    else if(N%10==8){
        cout<<"pon"<<endl;
    }
    else{
        cout<<"hon"<<endl;
    }
}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

int main(){

    int n;
    cin >> n;

    map<string,int> mp;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        mp[s]++;
    }

    cout << "AC x " << mp["AC"] << endl;
    cout << "WA x " << mp["WA"] << endl;
    cout << "TLE x " << mp["TLE"] << endl;
    cout << "RE x " << mp["RE"] << endl;
    return 0;
}

Measured memory_bytes: 3584
#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
ll x[2000],y[2000],z[2000],ans,n,m;
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        cin>>x[i]>>y[i]>>z[i];
    }
    for(int i=-1; i<=1; i++)
    {
        for(int j=-1; j<=1; j++)
        {
            for(int k=-1; k<=1; k++)
            {
                vector<ll>v;
                for(int l=0;l<n;l++)
                    v.push_back(x[l]*i+y[l]*j+z[l]*k);
                sort(v.begin(),v.end());
                ll sum=0;
                for(int l=n-1,p=m;p>0;p--,l--)
                    sum+=v[l];
                ans=max(ans,sum);

            }
        }
    }
    cout<<ans<<endl;


    return 0;
}


Measured memory_bytes: 3684
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define rep(i,n) for(int i=0; i<(n); i++)
typedef struct Maze{
  int h;
  int w;
  int t;
} MAZE;
const int INF=10001000;
int dy[]={0,-1,0,1};
int dx[]={1,0,-1,0};
int main() {
  int h,w;
  cin >> h >> w;
  vector<vector<char>> A(h,vector<char>(w,'0'));
  vector<vector<int>> turn(h,vector<int>(w,-1));
  queue<MAZE> q;
  rep(i,h)rep(j,w){
    cin >> A[i][j];
    if(A[i][j]=='#') q.push(MAZE{i,j,0});
  }
  while(!q.empty()){
    int y=q.front().h;
    int x=q.front().w;
    int tt=q.front().t;
    q.pop();
    if(turn[y][x]!=-1) continue;
    turn[y][x]=tt;
    rep(dir,4){
      int ny=y+dy[dir],nx=x+dx[dir];
      if(ny>=h || ny<0 || nx>=w || nx<0) continue;
      q.push(MAZE{ny,nx,tt+1});
    }
  }
  int ans;
  rep(i,h)rep(j,w){
    ans=max(ans,turn[i][j]);
  }
  cout << ans << endl;
}

Measured memory_bytes: 53576
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int r;
  cin >> r;
  cout << r * 2 * M_PI << endl;
  return 0;
}

Measured memory_bytes: 3788
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n, a, b;
    cin >> n >> a >> b;

    if ((b - a) & 1)
        cout << min(a + (b - a) / 2, n - b + 1 + (b - a) / 2);
    else
        cout << (b - a) / 2;

    return 0;
}

Measured memory_bytes: 256
S=gets.chomp
ret=0
a_count=0
b_of_bc=false
S.length.times do |i|
    s=S[i]
    if s=="A"
        if b_of_bc
            a_count=1
        else
            a_count+=1
        end
        b_of_bc=false
    elsif s=="B"
        a_count=0 if b_of_bc
        b_of_bc=true
    else
        if b_of_bc
            ret+=a_count
        else
            a_count=0
        end
        b_of_bc=false
    end
end
puts ret


Measured memory_bytes: 14632
#include <bits/stdc++.h>
#ifdef __DEBUG__
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);

inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const deque<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

ll N;
vector<ll> WinNum,depth,Winner;

void input(){
    fast_io();
    cin >> N;
    WinNum.assign(N+10,0);
    Winner.assign(N+10,0);
    depth.assign(N+10,0);
    for (int i = 2; i <= N; i++)
    {
        ll a; cin >> a;
        WinNum[a]++;
        Winner[i] = a;
    }
}

int solve(){
    // deque<ll> Loser;
    auto cmp = [](pll a, pll b){return a.first>b.first;};
    priority_queue<pll,vector<pll>,decltype(cmp)> Loser(cmp);
    for (int i = 1; i <= N; i++) if(WinNum[i]==0) Loser.emplace(0,i);

    while(!Loser.empty())
    {
        ll loser = Loser.top().second;
        ll winner = Winner[loser];
        Loser.pop();
        depth[winner] = max(depth[loser]+1, depth[winner]+1);
        WinNum[winner]--;
        if(WinNum[winner]==0) Loser.emplace(depth[winner],Winner[loser]);
    }
    DBG(depth)
    DBG(WinNum)
    cout << depth[1] << endl;
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}

Measured memory_bytes: 7608
#include <iostream>
#include <iomanip>
#include <fstream>

#include <cmath>
#include <string>

#include <algorithm>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

long long arr[3005];
long long dp[3005][3005];

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

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

	for (int k = 1; k < n; k++) {
		for (int i = 0; i < n-k; i++) {
			int j = i + k;
			dp[i][j] = max(arr[j] - dp[i][j-1],
							arr[i] - dp[i+1][j]);
		}
	}

	cout << dp[0][n-1] << endl;

	return 0;
}


Measured memory_bytes: 70016
from collections import defaultdict

N = int(input())
X = [_ for _ in range(1,101)]
Y = [_ for _ in range(1,101)]
Z = [_ for _ in range(1,101)]

D = defaultdict(int)
for x in X:
    for y in Y:
        for z in Z:
            n = (x+y)**2 + (y+z)**2 + (z+x)**2
            if n % 2 == 0:
                if x == y == z:
                    D[n//2] += 1
                elif x != y and y != z and z != x:
                    D[n//2] += 1
                else:
                    D[n//2] += 1
for i in range(1,N+1):
    print(D[i])

Measured memory_bytes: 72904
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using System.Collections;

class Program
{
    static void Main()
    {
        long N = long.Parse(Console.ReadLine());
        long A = long.Parse(Console.ReadLine());
        long B = long.Parse(Console.ReadLine());
        long C = long.Parse(Console.ReadLine());
        long D = long.Parse(Console.ReadLine());
        long E = long.Parse(Console.ReadLine());

        //ボトルネックを考える
        long bottleneck = new long[] { A, B, C, D, E }.Min();

        //全移動がボトルネックと同じスピードでも結果は同じ

        //ボトルネックがnのとき、列の長さは N/n (切り上げ)
        //列の先頭が都市6に到達するのに5分かかり、
        //列の最後尾が都市6に到達するのには更に追加で N / n (切り上げ) - 1 だけかかる

        Console.WriteLine(5 + (long)Math.Ceiling(N / (double)bottleneck) - 1);
    }
}

Measured memory_bytes: 13268
#include <bits/stdc++.h>

using namespace std;

#define pb push_back

const int mn=1e5+5;
const int mm=1e6+5;

int n,m;
int cnt;

struct edge{
    int to,dist;
};

vector<edge> g[mm];
int d[mm];
unordered_map<int,int> h[mn];

struct node{
    int id,dist;
    bool operator < (const node &p) const{
        return dist>p.dist;
    }
};

priority_queue<node> q;
void dijk(){
    memset(d,0x3c,sizeof d);
    q.push(node{1,d[1]=0});
    while(!q.empty()){
        node u=q.top();q.pop();
        if (d[u.id]<u.dist) continue;
        for(auto &e:g[u.id]){
            if (d[e.to]>u.dist+e.dist){
                q.push(node{e.to,d[e.to]=u.dist+e.dist});
            }
        }
    }
}

int main() {
    scanf("%d%d",&n,&m);
    cnt=n;
    for(int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        if (h[u].find(w)==h[u].end()){
            h[u][w]=++cnt;
            g[u].pb(edge{cnt,1});
            g[cnt].pb(edge{u,1});
        }
        u=h[u][w];
        if (h[v].find(w)==h[v].end()){
            h[v][w]=++cnt;
            g[v].pb(edge{cnt,1});
            g[cnt].pb(edge{v,1});
        };
        v=h[v][w];
        g[u].pb(edge{v,0});
        g[v].pb(edge{u,0});
    }
    dijk();
    if (d[n]>1e9) d[n]=-2;
    printf("%d\n",d[n]/2);
    return 0;
}

Measured memory_bytes: 71292
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int t[100005],x[100005],y[100005];
int main(){
	int n,t1,x1,y1;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d%d",&t[i],&x[i],&y[i]);
		t1=t[i]-t[i-1],x1=x[i]-x[i-1],y1=y[i]-y[i-1];
		y1=fabs(y1),x1=fabs(x1);
		if(x1+y1>t1||((x1+y1<t1)&&(t1-x1-y1)%2!=0)){
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}

Measured memory_bytes: 1408
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define fi first
#define se second

#define INF 2147483600

//アイデアメモ
// 同じ文字が2つ以上離れる(x..x)とき，そこで分断して考えてよい．
// アンバランスになる場合，必ず，その文字は連続が一つ空けて再度その文字が現れるしかない．
// すなわち，アンバランスな部分文字列は必ず長さが3のアンバランスな部分文字列をもつ．
// 長さが3だけ探索すればいい．

int main(){
  string s;
  cin>>s;
  int n =s.size();

  if(n==2){
    if(s[0]==s[1]){
      cout<<"1 2"<<endl;
    } else {
      cout<<"-1 -1"<<endl;
    }
    return 0;
  }

  rep(i,n-2){
    if(s[i]==s[i+1] || s[i]==s[i+2]){
      cout<<i+1<<" "<<i+3<<endl;
      return 0;
    }
  }
  if(s[n-2]==s[n-1]){
    cout<<n-2<<" "<<n<<endl;
    return 0;
  }

  cout<<"-1 -1"<<endl;
  return 0;
}


Measured memory_bytes: 512
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define ff              first
#define ss              second
#define int             long long int
#define pb              push_back
#define mp              make_pair
#define pi              pair<int,int>
#define vi              vector<int>
#define mi              map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define MOD             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1N
#define loop(i,a,b)     for(int i=a;i<b;i++)

void FIO(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt","r",stdin) ;
    // freopen("output.txt","w",stdout) ;
    // #endif
}

int32_t main(){
    FIO();
    string s,t; cin >> s >> t;
    char p = t[t.length() - 1];
    s = s + p;
    if(s == t) cout << "Yes";
    else cout << "No";



}





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































Measured memory_bytes: 3656
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define endl "\n"
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    if(n == 3) {
        cout << "2 5 63" << endl;
    } else if(n == 4) {
        cout << "2 5 20 63" << endl;
    } else if(n == 6) {
        cout << "2 3 25 30 60 90" << endl;
    } else if(n == 19999) {
        vector<int> v;
        for(int i = 1; i <= 15000; i++) {
            if(i % 3 != 0) {
                v.push_back(2 * i);
            }
        }
        for(int i = 1; i <= 9999; i++) {
            v.push_back(3 * i);
        }
        for(auto i : v) {
            cout << i << " ";
        }
        cout << endl;
    } else {
        int two, three;
        for(int i = 2; i <= 5000; i += 2) {
            three = i;
            two = n - i;
            if(0 <= two && two <= 15000 && two % 3 == 0) {
                break;
            }
        }
        vector<int> v;
        for(int i = 1; i <= two; i++) {
            v.push_back(2 * i);
        }
        for(int i = 1, cnt = 0; i <= 10000 && cnt < three; i++) {
            if(i % 2 != 0) {
                cnt++;
                v.push_back(3 * i);
            }
        }
        for(auto i : v) {
            cout << i << " ";
        }
        cout << endl;
    }
}

Measured memory_bytes: 640
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
int N;
ll ans = 0;
int x[100000];
int y[100000];
int main(void) {
	cin >> N;
	for (int i = 0; i < N/2; i++) {
		int a, b;
		cin >> a >> b;
		x[a-1]++;
		y[b-1]++;
	}
	int amax = 0;
	int asec = 0;
	int alog = 0;
	int bmax = 0;
	int bsec = 0;
	int blog = 0;

	for (int i = 0; i < 100000; i++) {
		if (amax <= x[i]) {
			asec = amax;
			amax = x[i];
			alog = i;
		}
		else if(asec<x[i]){
			asec = x[i];
		}
		if (bmax <= y[i]) {
			bsec = bmax;
			bmax = y[i];
			blog = i;
		}
		else if (bsec < y[i])bsec = y[i];
}
	int c;
	if (alog == blog) {
		c = max(amax + bsec, asec + bmax);
	}
	else {
		c = amax + bmax;
	}
	cout << N - c << endl;
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

    int n,m;
    while(cin>>n>>m&&!(n==0&&m==0)){
        int maxCNt=-1;
        int idx;
        for(int i = 0; i < n; i++){
            int cnt=0;
            for(int j = 0; j < m; j++){
                int t;
                cin>>t;
                cnt+=t;
            }
            if(maxCNt<cnt){
                idx=i+1;
                maxCNt=cnt;
            }
        }
        cout<<maxCNt<<endl;
    }

    return 0;
}

Measured memory_bytes: 0
import util.control.Breaks._

object Main extends App {
  breakable {
    while (true) {
      val n = io.StdIn.readInt()
      val k = io.StdIn.readInt()
      if (n == 0 && k == 0) break()
      val an = Array.fill(n)(io.StdIn.readLine())
      val results = collection.mutable.Set.empty[String]
      for (c <- an.combinations(k))
        for (p <- c.permutations) {
          results += p.mkString("")
        }
      println(results.size)
    }
  }
}


Measured memory_bytes: 53668
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int X;
  cin >> X;
  vector<bool> sieve(2 * X, true);
  for (int i = 2; i < 2 * X; ++i) {
    if (!sieve[i])
      continue;
    else if (i >= X) {
      cout << i << endl;
      return 0;
    }
    for (int j = 2 * i; j < 2 * X; j += i) sieve[j] = false;
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
long long n, ans=1, M=1e9+7, cnt[1005];
int main() {
	int i, j, k;
	cin >> n;
	for(i=2; i<=n; i++) {
		for(k=i, j=2; k>1; ) {
			if(k%j==0) k/=j, cnt[j]++;
			else j++;
		}
	}
	for(i=2; i<=n; i++) ans = ans * (cnt[i]+1) % M;
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
//#include<bits/stdc++.h>
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<stdio.h>
#include <cstdio>
#include <cstring>
#include <string>


using namespace std;

#define ld long double
#define ll long long
#define ff first
#define ss second
#define sz(a) (int)a.size()
#define mem(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define pii pair<int ,int >
#define pll pair<ll ,ll >
#define mp make_pair
#define vi vector<int >
#define vii vector<pii>
#define vvi vector < vi >
#define vll vector < ll >
#define vllll vector < pll >
#define vvll vector < vll >
#define forn(i,n) for(int i=0;i<(n);i++)
#define fori(i,s,e) for(int i=s,i<n;i++)
#define qi  queue <int >
#define qll  queue <ll >
#define in_r(x,y,n,m) (x>=0 && x<n && y>=0 && y<m)
#define maxn 1000009


void fast(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

const ll MOD = 1e9 + 7;
const double EPS = (1e-8);
const int OO = (int) 1e9 + 7;
const ll OOO = (ll) 1e15 + 7;
//double const PI = 3.1415926535897932384626433832795;
const int mxn = 200009;
char c1, c2, c3, c4;
string s, s1, s2, s3;
int   n, m, k, t, q, a, b, c, d, e, x, y, z; ll sol = 0;


int ans[mxn];
bool vis[mxn];
vi g[mxn];

void dfs(){
	
	//vis[1] = 1;
	queue<pair <int , int >> q;
	q.push({ 1, 0 });
	while (!q.empty()){
		int v = q.front().ff;
		int u = q.front().ss;
		q.pop();
		if (vis[v]) continue;
		vis[v] = 1; ans[v] = u;
		for (auto i : g[v]) if (!vis[i]) q.push({ i, v });
	}



}

int main(){
	fast();
	
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	dfs();
	bool ok = 1;
	for (int i = 2; i <= n; i++) if (ans[i] == 0) ok = 0;

	if (ok){
		cout << "Yes\n";
		for (int i = 2; i <= n; i++) cout << ans[i] << endl;
	}
	else cout << "No";

	
	cout << endl << endl; return 0;
}


Measured memory_bytes: 13452
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a) 
#define dumparr(a,n) 
#endif
#define FOR(i, a, b) for(ll i = a;i < b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = b-1;i >= a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;

const ll INF=(1LL<<50);
#if __cplusplus>=201700L
ll gcd(ll a, ll b) {
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif

template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
  os << "[ ";
  REP(a.size()){
    os<< a[i] << " ";
  }
  os<< " ]";
  return os;
}

ll tax(ll a){
  return a*108/100;
}

void solve(long long N){
  REP(50001){
    if(tax(i)==N){
      cout<<i<<endl;
      return;
    }
  }
  cout<<":("<<endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    solve(N);
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <stack>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;
stack<long> s;
int main(){
	string str;
	while (cin >> str){
		if (str[0] == '+' || str[0] == '-' || str[0] == '*'){
			char ch; ch = str[0];
			long b = s.top(); s.pop();
			long a = s.top(); s.pop();
			if (ch == '+') s.push(a + b);
			else if (ch == '-') s.push(a - b);
			else if (ch == '*') s.push(a*b);
		}
		else {
			long tmp = atoi(str.c_str());
			s.push(tmp);
		}
	}
	cout << s.top() << '\n';
}

Measured memory_bytes: 1196
n = int(input())
a = [1] + [6 ** i for i in range(1, 7)] + [9 ** i for i in range(1, 6)]

INF = 10 ** 5
dp = [INF] * 200000
dp[0] = 0

for i in range(n):
    for ai in a:
        dp[i + ai] = min(dp[i + ai], dp[i] + 1)

print(dp[n])

Measured memory_bytes: 4596
#include <iostream>
#include <vector>
using namespace std;

long long powmod(long long a, long long b, long long m)
{
    long long r = 1;
    for (; b>0; b>>=1, a=a*a%m)
        if (b&1)
            r = r*a%m;
    return r;
}

long long inv(long long a, long long m)
{
    return powmod(a, m-2, m);
}

int main()
{
    long long N;
    cin>>N;

    long long M = 998244353LL;

    vector<long long> F(N+1);
    F[0] = 1;
    for (int i=1; i<=N; i++)
        F[i] = F[i-1]*i%M;

    long long t = 0;
    for (long long i=N/2+1; i<=N; i++)
    {
        t += 2*powmod(2, N-i, M)*F[N]%M*inv(F[i], M)%M*inv(F[N-i], M)%M;
        t %= M;
    }
    cout<<((powmod(3, N, M)-t)%M+M)%M<<endl;
}


Measured memory_bytes: 78336
n = int(input())
s = list(map(int,input().split()))
dp = [[0 for _ in range(n)] for _ in range(n)]
psum = [0 for _ in range(n+1)]
sm = 0
for i in range(n):
    sm +=s[i]
    psum[i+1]=sm
def findMin(left,right):
    dp[left][right]=dp[left][left]+dp[left+1][right]+psum[right+1]-psum[left]
    for i in range(left+1,right):
        dp[left][right] = min(dp[left][right],dp[left][i]+dp[i+1][right]+psum[right+1]-psum[left])
for length in range(2,n+1):
    for l in range(n-length+1):
        r = l+length-1
        findMin(l,r)

print(dp[0][n-1])

Measured memory_bytes: 43868
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int N, s;
int main()
{
	while(cin >> N && N>0)
	{
		s=0;
		for(int i=2; i*(i+1)<=2*N; i++)
		{
			if((i%2==0)&&((2*(N/i)+1)*i==2*N))
			{
				s+=1;
			}
			else if((i%2!=0)&&(i*(N/i)==N))
			{
				s+=1;
			}
		}
		cout << s << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
#  --*-coding:utf-8-*--


def f(N, M, S, T):
    W = [1]*(N+1)

    for i, t in enumerate(T):
        W2 = W[:]
        
        k = 0
        for j, s in enumerate(S):
            if t == s:
                k += W[j]

            x = (W2[j+1] + k)%1000000007
            W2[j+1] = x

        W = W2
        
    return W[-1]



def main():
    N, M = map(int, input().split())
    S = list(map(int, input().split()))
    T = list(map(int, input().split()))

    print(f(N,M,S,T))



if __name__ == '__main__':
    main()



Measured memory_bytes: 3316
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

int dp[13][1 << 12], a[12], b[12];
int main() {
	int m, w;
	while (scanf("%d%d", &m, &w), m) {
		memset(dp, 0, sizeof(dp));
		rep(i, m)scanf("%d", &a[i]);
		rep(i, w)scanf("%d", &b[i]);
		rep(i, m)rep(j, 1 << w) {
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
			rep(k, w) {
				if (j >> k & 1)continue;
				int c = abs(a[i] - b[k]);
				dp[i + 1][j | 1 << k] = max(dp[i + 1][j | 1 << k], dp[i][j] + c*(c - 30)*(c - 30));
			}
		}
		printf("%d\n", *max_element(dp[0], dp[m + 1]));
	}
}

Measured memory_bytes: 3388
import math , sys 
from collections import deque

N = int( input() )
Cs = list( map( int , input().split() ))
Cs.sort(reverse=True)

ans = Cs.pop(0)
i = 1
Q  = (N-2)//2
R = (N-2) % 2
ans += sum([Cs[i]*2 for i in range(Q)])
if R==1:
    ans+=Cs[Q]
print(ans)


Measured memory_bytes: 106204
#include "bits/stdc++.h"
 
typedef long long ll;
#define int ll
#define fi first
#define se second
#define SORT(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
using namespace std;
int MOD = 1000000007;

signed main(){
    int h,w;
    cin >> h >> w;
    char a[110][110];
    rep(i,h) rep(j,w) cin >> a[i][j];
    int s[110] = {0};
    int t[110] = {0};
    rep(i,h){
        int c = 1;
        rep(j,w){
            if(a[i][j] == '#')c = 0;
        }
        if(c)s[i] = 1;
    }
    rep(j,w){
        int c = 1;
        rep(i,h){
            if(a[i][j] == '#')c = 0;
        }
        if(c)t[j] = 1;
    }
    rep(i,h){
        if(s[i] != 0)continue;
        rep(j,w){
            if(t[j] == 0) cout << a[i][j];
        }
        cout << endl;
    }
    
    
    return 0; 
}

// g++ -std=c++14 code1.cpp
// rm -r -f test;oj dl https://agc030.contest.atcoder.jp/tasks/agc030_a
// rm -r -f test;oj dl http://abc107.contest.atcoder.jp/tasks/abc107_b

Measured memory_bytes: 768
#include<stdio.h>

long long lucas(int n)
{
	long long a, b, c;
 	a = 2;
 	b = 1;
 	while(n--)
 	{
 		c = a+b;
 		a=b;
 		b=c;
 	}
	return a;
}
int main()
{
	int a;
	while(~scanf("%d", &a))
	{
		printf("%lld\n",lucas(a));
	}
	return 0;
}

Measured memory_bytes: 256
class square():
    
    def __init__(self, x,y):
        self.x = x
        self.y = y
        
    def draw(self):
        
        for i in range(0,self.x):
            for j in range(0,self.y):
                if i == 0 or i == self.x-1 or j == 0 or j == self.y-1: 
                    print('#',end="")
                else:
                    print('.',end="")
            print('') 
        print('')
        
    
while True:
    H,W = map(int,input().split())
    
    if H == 0 and W == 0:
        break
        
    s = square(H,W)
    s.draw()

Measured memory_bytes: 7016
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


A, B = [int(x) for x in input().split()]

print(max(A + B, A - B, A * B))


Measured memory_bytes: 3696
#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int main() {
	Int N;
	cin >> N;

	set<Int> st;
	st.insert(N);
	if (N - 1 > 1) { st.insert(N - 1); }
	for (Int i = 2; i * i <= 2 * N; ++i) {
		if (N % i == 1) {
			st.insert(i);
			st.insert((N - 1) / i);
		}
		if (N % i == 0) {
			Int n = N;
			while (n % i == 0) { n /= i; }
			if (n % i == 1) { st.insert(i); }
		}
	}
	cout << st.size() << endl;

	return 0;
}


Measured memory_bytes: 256
n,m=map(int,input().split())
a=set(int(input()) for _ in range(m))
dp=[0]*(n+1)
dp[0]=1
for i in range(1,n+1):
    dp[i]=(dp[i-1]+dp[i-2])%(10**9+7)
    if i in a:
        dp[i]=0
print(dp[n])


Measured memory_bytes: 58200
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        // 入力
        double[] ab = Console.ReadLine().Split().Select(double.Parse).ToArray();
        double a = ab[0];
        double b = ab[1];

        double ans = -1;
        for (double i = 1; i < 1300; i++)
        {
            var t1 = Math.Floor(i * 0.08);
            var t2 = Math.Floor(i * 0.1);
            if (Math.Floor(i * 0.08) == a && Math.Floor(i * 0.1) == b)
            {
                ans = i;
                break;
            }
        }
        Console.WriteLine(ans);
    }
}

Measured memory_bytes: 13396
import java.io.*;
import java.util.*;
import static java.lang.System.out;

public class Main{
    static MyReader in = new MyReader();

    public static void main(String[] args){
        int N, T; {int[] a = in.ii(); N = a[0]; T = a[1];}
        int[] A = in.ii();
        int B = 0;
        int ans = 0;
        int max = 0;
        for(int i = 1; i < A.length; i++){
            int min = Math.min(A[i-1] - B, A[i-1]);
            if((B = A[i] - min) == max){
                ans++;
            } else if (max < B) {
                max = B;
                ans = 1;
            }
        }
        out.println(ans);
    }

    static class MyReader extends BufferedReader{
        MyReader(){
            super(new InputStreamReader(System.in));
        }

        String s(){
            try{return readLine();}catch(IOException e){return "";}
        }

        String[] ss(){
            return s().split(" ");
        }

        int i(){
            return Integer.parseInt(s());
        }

        int[] ii(){
            String[] ss = ss();
            int[] ii = new int[ss.length];
            for(int j = 0; j < ss.length; j++) ii[j] = Integer.parseInt(ss[j]);
            return ii;
        }

        ArrayList<Integer> li(){
            String[] ss = ss();
            ArrayList<Integer> li = new ArrayList<>();
            for(int j = 0; j < ss.length; j++) li.add(Integer.parseInt(ss[j]));
            return li;
        }

        long l(){
            return Long.parseLong(s());
        }

        long[] ll(){
            String[] ss = ss();
            long[] ll = new long[ss.length];
            for(int j = 0; j < ss.length; j++) ll[j] = Long.parseLong(ss[j]);
            return ll;
        }
    }
}


Measured memory_bytes: 38044
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<bitset>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define RE return 0
//ios::sync_with_stdio(false);
//std::cin.tie(0);
//<< setprecision(20)
const int mod=1e9+7;
const llint big=1e15+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
template <class T> void soun(T& ar)
{sort(ar.begin(),ar.end());ar.erase(unique(ar.begin(),ar.end()),ar.end());}
int main(void){
	llint K,a,b;cin>>K>>a>>b;
	if(a>=K){cout<<1<<endl;RE;}
	if(b>=a){cout<<-1<<endl;RE;}
	cout<<3+(K-a-1)/(a-b)*2<<endl;RE;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+100;
const int maxc=26;
const int mod=998244353;

int main()
{
    double x;
    cin>>x;
    x/=3.0;
    printf("%.6f",x*x*x);
    return 0;
}
/*

*/


Measured memory_bytes: 256
#include <bits/stdc++.h>

using namespace std;

template <typename T> T gcd(T x, T y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <typename T> T lcm(T x, T y) {
  if (x == 0 || y == 0)
    return 0;
  return x / gcd(x, y) * y;
}

int main() {
  long long int N;
  cin >> N;
  int ans = 0;
  while (N > 0) {
    int x = N % 10;
    N /= 10;
    if (x != 9 && N > 0) {
      x = 9;
      N -= 1;
    }
    ans += x;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;

int main(){
  int a,b,c;
  cin>>a>>b>>c;
  if(a==b && a!=c){
  	cout<<"Yes"<<endl;
  	
  }
  else if(b==c && b!=a){
  	cout<<"Yes"<<endl;
  }
  else if(a==c && a!=b){
  	cout<<"Yes"<<endl;
  }
  else{
  	cout<<"No"<<endl;
  }
  return 0;
  
}

Measured memory_bytes: 256