akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define uniq(v) v.erase(unique(v.begin(),v.end()),v.end())
#define lb(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define ub(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
using Pair = pair<ll,pair<int,int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>> 
const ll mod=1000000007;
//const ll mod=998244353;
const ld pi=acos(-1.0);
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) { 
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる




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

  int n,a,b;
  cin>>n>>a>>b;
  a--;b--;

  vector<vector<int>> p(n);
  rep(i,n-1){
    int x,y;
    cin>>x>>y;
    x--;y--;
    p[x].pb(y);
    p[y].pb(x);
  }

  vector<int> bis(n,-1);
  deque<int> que;
  bis[b]=0;
  que.pb(b);

  while(!que.empty()){
    int v=que.front();
    que.pop_front();
    for(int nv : p[v]){
      if(bis[nv]!=-1) continue;
      bis[nv]=bis[v]+1;
      que.pb(nv);
    }
  }

  vector<int> dist(n,-1);
  dist[a]=0;
  que.pb(a);

  while(!que.empty()){
    int v=que.front();
    que.pop_front();
    for(int nv : p[v]) {
      if(dist[nv]!=-1) continue;
      dist[nv]=dist[v]+1;
      if(dist[nv]<bis[nv])que.pb(nv);
    }
  }

  int ans=0;
  rep(i,n){
    if(dist[i]!=-1) chmax(ans,bis[i]); 
  }

  cout << ans-1 << endl;
}


Measured memory_bytes: 9500
#include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=0; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define F first
#define S second

using namespace std ;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef pair<int, int> pint;

int main()
{
  string s ;
  cin >> s ;
  int n = s.size() ;
  int sta = 0 ;
  int fin = 0 ;
  
  REP(i,n){
    if(s.at(i)=='A'){
      sta = i ;
      break ;
    }
  }
  RREP(i,n){
    if(s.at(i)=='Z'){
      fin = i+1 ;
      break ;
      
    }
  }
  
  cout <<fin-sta<<endl ;

  return 0 ;
}




Measured memory_bytes: 640
$mod = 10**9+7
$exp = Array.new(100000,nil)
def exp3(n)
  if $exp[n]
    $exp[n]
  elsif n < 0
    0
  elsif n == 0
    1
  elsif n == 1
    3
  elsif n%2 == 0
    $exp[n] = exp3(n/2)*exp3(n/2)%$mod
  else
    $exp[n] = exp3(n/2)*exp3(1+n/2)%$mod
  end
end

s = gets.chomp
sum_a = 0
sum_c = 0
sum_q = 0
c = Array.new(s.size).map{[0,0,0,0]}
for i in 0..s.size-1
  if s[i] == "A"
    sum_a += 1
  elsif s[i] == "B"
    c[i][0,2] = sum_a,sum_q
  elsif s[i] == "?"
    c[i][0,2] = sum_a,sum_q
    sum_q += 1
  end
end
sum_q = 0
(s.size-1).downto(0) do |i|
  if s[i] == "B"
    c[i][2,2] = sum_c,sum_q
  elsif s[i] == "C"
    sum_c += 1
  elsif s[i] == "?"
    c[i][2,2] = sum_c,sum_q
    sum_q += 1
  end
end
ans = 0
c.each do |i,j,k,l|
  ans += i*k*exp3(j+l)
  ans += j*k*exp3(j+l-1)
  ans += i*l*exp3(j+l-1)
  ans += j*l*exp3(j+l-2)
  ans %= $mod
end
puts ans

Measured memory_bytes: 28284
//package abc170_A;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FastScanner in = new FastScanner(System.in);
		int n = 5;
		for(int i = 1; i <= n; i++)
		{
			int num = in.nextInt();
			if(num == 0) {
				System.out.println(i);
				break;
			}
		}
	}

}
///////////////////////////////////TEMPLATE///////////////////////////////////
class FastScanner {
	BufferedReader br;
	StringTokenizer st;

	FastScanner(InputStream stream) {
		try {
			br = new BufferedReader(new InputStreamReader(stream));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	FastScanner(File input) {
		try {
			br = new BufferedReader(new FileReader(input));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	String nextLine() {
		String s = "";
		try {
			s = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return s;
	}

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

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

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

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


Measured memory_bytes: 32568
import java.util.Scanner;

class Main {

  public static long culc(long x) {
    long cntl = (x + 1) / 2;
    long ans = cntl % 2;
    if (x % 2 == 0)
      ans ^= x;
    return ans;
  }

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    long a = scanner.nextLong();
    long b = scanner.nextLong();
    long ans = culc(a - 1) ^ culc(b);
    System.out.println(ans);
  }

}


Measured memory_bytes: 23252
//#pragma GCC optimize("03", "unroll-loops")
//#pragma GCC target("avx2")

#include <bits/stdc++.h>

#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i,n) for(i=0;i<n;i++)
#define FORn(i,j,n) for(i=j;i<n;i++)
#define pb push_back
#define unique_sort(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define vprint(v) for(auto t:v)cout<<t<<" "
#define endl "\n"

using namespace std;

typedef long long int lli;
typedef long int li;
typedef vector<lli> vli; 
typedef vector<int> vi; 
typedef vector<string> vs; 


const lli mod=1000000007;
const lli limit=1000000;


void fc(lli &x)
    {
        bool neg=false;
        register char c;
        x =0;
        c=getchar();
        if(c=='-')
        {
            neg = true;
            c=getchar();
        }
        for(;(c>47 && c<58);c=getchar())
            x = (x<<1) + (x<<3) +c -48;
        if(neg)
            x *=-1;
    }

int main() {

    fast()
    lli n,q,t;
    lli temp,temp1,temp2,temp3;
    lli i,j,k;
    string str;
    cin>>str;
    FOR(i,3){
        if(str[i]=='7'){
            cout<<"Yes"<<endl;
            exit(0);
        }
    }
    cout<<"No"<<endl;
 
}

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

int main() {
  ll n,d;
  cin>>d>>n;
  ll base=pow(100,d);
  ll ans;
  if(n==100){
    ans=base*(n+1);
  }else{
    ans=base*n;
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007


int main(){


	int n;
	cin >> n;
	std::vector<int> v(n);
	std::map<int, int> m;
	for(int i = 0; i < n; ++i){
		cin >> v[i];
		m[v[i]]++;
	}
	sort(v.begin(), v.end());
	if(n < 3){
		cout << "0";
	}
	else{
		if(n == 3){
			if(v[0] + v[1] <= v[2] && m.size() == 3){
				cout << 1 ;
			}
			else{
				cout << "0";
			}
		}
		else{
			ll count = 0;
			for(int i = 0; i < n; ++i){
				for(int j = i + 1; j < n; ++j){
					for(int k = j + 1; k < n; ++k){
						// cout << v[i] << v[j] << v[k] << '\n';
						if(v[i] != v[j] && v[j] != v[k] && v[i] != v[k] && v[i] + v[j] > v[k]){
							count++;
						}
					}
				}
			}
			cout << count << '\n';
		}
	}
	return 0;
}

Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
#define ssd ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define debug_x(x) cout<< #x <<"---> : "<<x<<endl;
#define debug_ve(x) for(auto it: x) cout<<it<<" ";
#define rep(i,a,n) for (int i=a;i<n;i++)
#define _rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef double db;
//std::mt19937 mrand(random_device{}());
//int rnd(int x) { return mrand() % x ; }
const ll mod=1e7;
ll ksm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

/***********************/

int main(){
    ssd;
    int t =0,k=0;
    rep(i,0,3){
        int x ;
        cin>>x;
        if(x == 5) t++;
        if(x == 7) k++;
    }
    if(t == 2 && k == 1) puts("YES");
    else puts("NO");
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	char s[128];
	while(cin>>s){
		for(int i=0;s[i];i++){
			if(s[i]=='@'){
				int n=s[++i]-'0';
				char c=s[++i];
				for(int j=0;j<n;j++)cout<<c;
			}else{
				cout<<s[i];
			}
		}
		cout<<endl;
	}
	return 0;
}

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

#define rep(i, n) for(int i = 0; i < (n); i++)
#define all(a) (a).begin(),(a).end()

using ll = long long;
constexpr ll INF = 1000000000;

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 namespace std;

//fixed << setprecision(10)

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

int main() {
	string s; cin >> s;

	int n = s.length();
	int cnt = 0;
	
		int lim = n / 2;
		for (int i = 0; i < lim; i++) {
			char a = s[i];
			char b = s[n - i - 1];
			if (a != b)cnt++;
		}
	cout << cnt << endl;
}



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

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s;
    int k;
    cin >> s >> k;
    
    map<string, int> m;
    
    int n = s.size();
    rep(i, n){
        for(int j=1; j<=n-i && j<=k; j++){
            // cout << i << "|" << j << "-" << s.substr(i, j) << endl;
            m[s.substr(i, j)]++;
        }
    }
    
    int idx = 0;
    for(auto itr=m.begin(); itr!=m.end(); itr++){
        if(idx == k-1){
            cout << itr->first << endl;
            break;
        }
        idx++;
    }
    
    return 0;
}

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

#define coutv(v) for (int i = 0; i < (v).size(); ++i) cout << v[i] << ' '; cout << endl;
#define coutvv(v) for (int i = 0; i < (v).size(); ++i) {for (int j = 0; j < (v[i]).size(); ++j) cout << v[i][j] << ' '; cout << endl;}
#define debugv(v) {for (int i = 0; i < (v).size(); ++i) cerr << v[i] << ' '; cerr << endl;}
#define debugvv(v) {for (int i = 0; i < (v).size(); ++i) {for (int j = 0; j < (v[i]).size(); ++j) cerr << v[i][j] << ' '; cerr << endl;}}
#define stringtovector(s, v) {for (int i = 0; i < (s).size(); ++i) v.push_back(s[i]);};
#define TC int TESTCASE; cin >> TESTCASE; while (TESTCASE--)
typedef long long ll;
const int mod = 1000000007;





int main() {
        int n;
        cin >> n;
        if (n) {
            cout << 0;
        } else {
            cout << 1;
        }
}





Measured memory_bytes: 3632
//==========//============================================================================
// Name        : Hello5.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <numeric>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using p = pair<int,int>;//<key,value>


int main() {
int a,b;
cin>>a>>b;
if(a<=8 && b<=8)
	cout<<"Yay!"<<endl;
else cout<<":("<<endl;
return 0;
}







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

//typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int MOD = 1000000007;

//MOD
//--------------------------------------------
#define MUL(a,b) (((a) % MOD) * ((b) % MOD)) % MOD

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

//debug
//--------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//IO accelerate
//--------------------------------------------
struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

//main code

int main(int argc, char const* argv[])
{
	int n,x;
	cin >>n  >>x;
	VL l(n);
	for (int i = 0; i < n; i++) {
		cin >> l[i];
	}
	LL d = 0;
	int ans = 1;
	for (int i = 0; i < n; i++) {
		d += l[i];
		d <= x ? ans++ : ans;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>

#define LEN 100005

typedef struct pp{
  char name[100];
  int t;
}P;

P Q[LEN+1];
int head, tail, n;

void enqueue(P x){
  Q[tail]=x;
  tail=(tail+1)%LEN;
}

P dequeue(){
  P x;
  x=Q[head];
  head=(head+1)%LEN;
  return x;
}

int min(int a,int b){
  if(a>b){
    return b;
  }
  else if(b>=a){
    return a;
  }
}

int main(){
  int elaps = 0, c;
  int i, q;
  P u;
  scanf("%d %d", &n, &q);

  for ( i = 1; i <= n; i++){
    scanf("%s", Q[i].name);
    scanf("%d", &Q[i].t);
  }
  head=1;
  tail=n+1;

  while(head!=tail){
    u=dequeue();
    c=min(q,u.t);
    u.t-=c;
    elaps+=c;
    if(u.t>0) enqueue(u);
    else{
      printf("%s %d\n",u.name,elaps);
    }
  }
  return 0;
}



Measured memory_bytes: 11856
/*input
10
*/
 
 
 
#include <bits/stdc++.h>
using namespace std;
 
 
 
#define int long long int
#define rep(i,n) \
    for(int i = 0; i < n; i++)
#define rep2(i, a, b) \
    for(int i = a; i < b; i++)
#define all(c) \
    c.begin(), c.end()
#define init(arr, num) \
    memset(arr, num, sizeof(arr))
#define fast\
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define inputFile \
    freopen("input.txt", "r", stdin); 
#define outputFile \
    freopen("output.txt", "w", stdout);
#define modM 1000000007
#define PI 3.1415926535897932
#define F first
#define S second  
#define pb push_back  
#define INF 3e18
#ifdef gaviria
#define TRACE
#endif
#ifdef TRACE
#define trace(...) _f(#VA_ARGS, __VA_ARGS_)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
    cout << name << " : " << arg1 << std::endl;
 
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
 
 
typedef long long ll;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef pair< ll, ll > ii;
typedef unordered_set<int> usi;
 


ll n,i,j,k,m,x;



void solve()
{cin>>n;

cout<< n + n*n + n*n*n;
}


 
 
 
signed main()
{
 
    // inputFile;
    // outputFile;
    fast;
    int T = 1;
    // cin >> T;
    // ll h=T;
    while(T--){
 
        solve();
        cout<<endl;
 
     
        
        
 
    }
    
    
    return 0;
 
}

Measured memory_bytes: 3588
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();

		if (N % 2 == 0) {
			System.out.println(N);
		} else {
			System.out.println(N * 2);
		}

		sc.close();

	}
}


Measured memory_bytes: 23764
import java.util.*;

import static java.lang.System.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        int[] a = new int[N];
        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int cnt = 1;
        for (int i = 1; i < N; i++) {
            boolean flag = true;
            for (int j = 0; j < i; j++) {
                if (a[j] > a[i]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                cnt++;
            }
        }

        out.print(cnt);
    }
}


Measured memory_bytes: 25044
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1e9;
const int MOD = 1e9 + 7;

void DFS(int depth, int end, string s){
  if(depth == end){
    cout << s << endl;
    return;
  }
  if(!s.size())
    DFS(1, end, "a");
  else{
    int last = 0;
    rep(i, s.size()){
      if(int(s.at(i) - 'a') > last)
        last = int(s.at(i) - 'a');
    }
    rep(i, last + 2){
      DFS(depth + 1, end, s + (char)(i + 'a'));
    }
  }

}

int main(){
  int N;
  cin >> N;
  DFS(0, N, "");
}

Measured memory_bytes: 1536
S = input()
K = int(input())

# S単体で必要な置換数を求める
cnt = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 0.5
    else:
        cnt = int(cnt+0.5)
cnt = int(cnt+0.5)

# Sを接続した時に必要な置換数を求める
s3 = S*3
cnt_1 = 0
for i in range(len(s3)-1):
    if s3[i] == s3[i+1]:
        cnt_1 += 0.5
    else:
        cnt_1 = int(cnt_1+0.5)
cnt_1 = int(cnt_1+0.5)

# 単体と接続時に必要な置換数の合計を求める
ans = int((cnt_1-cnt*3)/2*(K-1)+0.5)+cnt*K
print(ans)


Measured memory_bytes: 3064
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <iostream>
#include <utility>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <limits.h>
#include <cstring>
#include <tuple>
#include <cassert>
#include <numeric>
using namespace std;
// type alias
typedef long long LL;
typedef vector < int > VI;
typedef unordered_map < int, int > MAPII;
typedef unordered_set < int > SETI;
typedef pair< int , int > II;
typedef tuple< int, int, int > III;
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n)  for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// minmax
#define SMAX(a,b) a = max(a,b)
#define SMIN(a,b) a = min(a,b)
// debug cerr
#define TRACE false
#define dump(x) if(TRACE) { cerr << #x << " = " << (x) << endl; }
#define dump2(x,y) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << endl; }
#define dump3(x,y,z) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << endl; }
#define dump4(x,y,z,a) if(TRACE) { cerr << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z) << ", " << #a << " = " << (a) << endl; }
#define dumpAR(ar) if(TRACE) { FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }

/*
 
 8/9/2018
 
 22:45-22:49 analysis
 
 
 
 */

// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG x.cpp && ./a.out
const int MAX_N=1e5+1;
int N,K;
LL A[MAX_N];
vector<pair<LL,int>> E[MAX_N];
LL D[MAX_N];

void dfs(int u, int pre, LL d) {
  dump2(u,d);
  D[u]=d;
  FORR(p,E[u]) {
    LL w; int v; tie(w,v)=p;
    if(v==pre) continue;
    dfs(v,u,d+w);
  }
}

void solve() {
  D[K]=0;
  FORR(p,E[K]) dfs(p.second,K,p.first);
  REP(i,N) dump2(i,D[i]);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  
  cin>>N;
  REP(i,N-1) {
    int u,v; LL w; cin>>u>>v>>w;
    --u,--v;
    dump3(u,v,w);
    E[u].emplace_back(w,v);
    E[v].emplace_back(w,u);
  }
  int Q; cin>>Q>>K; --K;
  dump(K);
  solve();
  REP(_,Q) {
    int u,v; cin>>u>>v;
    --u,--v;
    cout<<D[u]+D[v]<<endl;
  }
  return 0;
}


Measured memory_bytes: 14336
import itertools

n = int(input())
p_li = tuple(map(int,input().split()))
q_li = tuple(map(int,input().split()))

n_number = [i for i in range(1,n+1)]
permutations_number = itertools.permutations(n_number)

check_li = []

for number in permutations_number:
    check_li += [number]

check_li.sort()

position_1 = 0
position_2 = 0

for x in range(len(check_li)):
    if check_li[x] == p_li:
        position_1 = x
    if check_li[x] == q_li:
        position_2 = x
        
print(abs(position_1 - position_2))

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

#define ll long long
#define ff(i,c,n) for(ll i=c;i<n;i++)
#define INF 1e12

int main(){
    ll n,W;
    cin>>n>>W;
    ll N=0;
    ll w[n],v[n];
    ff(i,0,n) cin>>w[i]>>v[i];
    ff(i,0,n) N+=v[i];
    ll dp[n+1][N+1];
    ff(i,0,n+1){
        ff(j,0,N+1){
            if(j==0) dp[i][j]=0;
            else if(i==0) dp[i][j]=INF;
            else if(v[i-1]<=j){
                dp[i][j]=min(w[i-1]+dp[i-1][j-v[i-1]],dp[i-1][j]);
            }
            else dp[i][j]=dp[i-1][j];
            // cout<<dp[i][j]<<" ";
        }
        // cout<<endl;
    }
    for(ll i=N;i>=0;i--){

        if(dp[n][i]<=W){
            cout<<i;
            break;
        }
    }
}

Measured memory_bytes: 79104
import math
from functools import reduce


def gcd(*numbers):
    return reduce(math.gcd, numbers)


def main():
    k = int(input())+1
    s = 0
    for a in range(1, k):
        for b in range(1, k):
            for c in range(1, k):
                s += gcd(a, b, c)
    print(s)


main()


Measured memory_bytes: 69404
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(1000000)

def main():
    s = readline().rstrip().decode()
    K = int(readline())
    #sに含まれる文字を一文字ずつ抽出する.
    #ここで抽出した文字を組み合わせて辞書順に文字列を構成していく
    l = sorted(list(set(s)))

    #cnd:=構成した文字列を辞書順に格納するリスト
    cnd = []

    #f(sub):=subから構成可能なK文字以下のSの部分文字列を構成する関数

    #計算量について
    #長さNの文字列から作るsubstringのうち長さが5以下のsubstringの数を計算する
    #長さ5のsubstirngは(N-5+1)個
    #長さ4のsubstringは(N-4+1)個
    #同様に長さ1まで考えて、合計すると約5N個となる

    def f(sub):
        cnd.append(sub)
        if len(sub) <= K: 
            #lから選んでsubに一文字追加する
            for i in l:
                nx = sub+i
                #if nx in s:はO(N)
                if nx in s:
                    return f(nx)
    #O(N^2K)
    for i in l:
        f(i)
    print(cnd[K-1])
if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
def Find(x, par):
  if par[x] < 0:
    return x
  else:
    # 経路圧縮
    par[x] = Find(par[x], par)
    return par[x]

def Unite(x, y, par, rank):
  x = Find(x, par)
  y = Find(y, par)

  if x != y:
    # rankの低い方を高い方につなげる
    if rank[x] < rank[y]:
      par[y] += par[x]
      par[x] = y
    else:
      par[x] += par[y]
      par[y] = x
      if rank[x] == rank[y]:
        rank[x] += 1

def Same(x, y, par):
  return Find(x, par) == Find(y, par)

def Size(x, par):
  return -par[Find(x, par)]

n, m, k = map(int, input().split())

F = [0]*n
B = [0]*n

par = [-1]* n
rank = [0]*n

for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    F[a] += 1
    F[b] += 1
    Unite(a, b, par, rank)

for i in range(k):
    c, d = map(int, input().split())
    c, d = c-1, d-1
    if Same(c, d, par):
        B[c] += 1
        B[d] += 1

ans = [0]*n
for i in range(n):
    ans[i] = Size(i, par)-F[i]-B[i]-1
print(*ans)


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

using namespace std;

using int64 = long long;
const int INF = 1 << 30;

int N, D;
int ex[2000][2000];
int chokudai[2][2001][2001];


bool check(int t, int y, int x) {
  int a = t / D;
  int b = t % D;

  int64 p[3];
  p[0] = 1LL * (a + 1) * (a + 1);
  p[1] = 1LL * a * (a + 1);
  p[2] = 1LL * a * a;

  for(int i = 0; i < D; i++) {
    for(int j = 0; j < D; j++) {
      int add = ex[y + i][x + j];
      if(i <= b && j <= b) {
        if(add > p[0]) return false;
      } else if(b < i && b < j) {
        if(add > p[2]) return false;
      } else {
        if(add > p[1]) return false;
      }
    }
  }
  return true;
}

inline int get_rect(int t, int a, int b, int c, int d) {
  return chokudai[t][c][d] + chokudai[t][a][b] - chokudai[t][a][d] - chokudai[t][c][b];
}


bool check(int t) {
  int a = t / D;
  bool all = true;
  for(int i = 0; i < 2 * D; i++) {
    for(int j = 0; j < 2 * D; j++) {
      if(ex[i][j] > 1LL * (a + 1) * (a + 1)) return false;
      all &= ex[i][j] <= 1LL * a * a;
      chokudai[0][i + 1][j + 1] = ex[i][j] > 1LL * a * a;
      chokudai[1][i + 1][j + 1] = ex[i][j] > 1LL * a * (a + 1);
    }
  }
  if(all) return true;
  for(int k = 0; k < 2; k++) {
    for(int i = 1; i <= 2 * D; i++) {
      for(int j = 1; j <= 2 * D; j++) {
        chokudai[k][i][j] += chokudai[k][i - 1][j];
        chokudai[k][i][j] += chokudai[k][i][j - 1];
        chokudai[k][i][j] -= chokudai[k][i - 1][j - 1];
      }
    }
  }
  int b = t % D + 1;
  for(int i = 0; i < D; i++) {
    for(int j = 0; j < D; j++) {
      int add = 0;
      add += get_rect(0, i + b, j + b, i + D, j + D);
      add += get_rect(1, i, j, i + D, j + D);
      add -= get_rect(1, i, j, i + b, j + b);
      add -= get_rect(1, i + b, j + b, i + D, j + D);
      if(add == 0) return true;
    }
  }
  return false;
}


int main() {
  cin >> N >> D;
  for(int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    x %= D;
    y %= D;
    ++ex[x][y];
    ++ex[x][y + D];
    ++ex[x + D][y];
    ++ex[x + D][y + D];
  }
  int ok = (1 << 30) - 1;
  for(int k = 29; k >= 0; k--) {
    if(check(ok ^ (1 << k))) ok ^= 1 << k;
  }
  cout << ok << endl;
}


Measured memory_bytes: 47104
use std::io;

const SPLIT_DELIMITER: char = ' ';

#[allow(unused_macros)]
macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

#[allow(dead_code)]
fn input_line_str() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
where
    T: std::str::FromStr,
{
    let mut v: Vec<Vec<T>> = Vec::new();

    for _ in 0..line {
        let vec_line = input_vector();
        v.push(vec_line);
    }
    v
}

#[allow(dead_code)]
fn input_vector<T>() -> Vec<T>
where
    T: std::str::FromStr,
{
    let mut v: Vec<T> = Vec::new();

    let s = input_line_str();
    let split_result = s.split(SPLIT_DELIMITER);
    for z in split_result {
        let buf = match z.parse() {
            Ok(r) => r,
            Err(_) => panic!("Parse Error"),
        };
        v.push(buf);
    }
    v
}

#[allow(dead_code)]
fn str2vec(s: &str) -> Vec<char> {
    let mut v: Vec<char> = Vec::new();
    for c in s.chars() {
        v.push(c);
    }
    v
}

#[derive(Clone)]
struct Edge {
    node: usize,
    cost: usize,
}

fn search_cost(
    graph: &Vec<Vec<Edge>>,
    graph_shortest: &mut Vec<usize>,
    to: usize,
    from: usize,
    cost: usize,
) {
    for e in &graph[to] {
        if from == e.node {
            continue;
        }
        let cost_buf = cost + e.cost;
        graph_shortest[e.node] = cost_buf;
        search_cost(graph, graph_shortest, e.node, to, cost_buf);
    }
}

fn main() {
    let n: usize;
    input!(n);
    let mut graph: Vec<Vec<Edge>> = vec![Vec::new(); n];
    let mut graph_shortest: Vec<usize> = vec![0; n];
    for _ in 1..n {
        let a: usize;
        let b: usize;
        let c: usize;
        input!(a, b, c);
        graph[a - 1].push(Edge {
            node: b - 1,
            cost: c,
        });
        graph[b - 1].push(Edge {
            node: a - 1,
            cost: c,
        });
    }
    let q: usize;
    let k: usize;
    input!(q, k);

    search_cost(&graph, &mut graph_shortest, k - 1, 9999999, 0);


    for _ in 0..q {
        let x: usize;
        let y: usize;
        input!(x, y);
        println!("{}", graph_shortest[x - 1] + graph_shortest[y - 1]);
    }
}

Measured memory_bytes: 22396
#include <stdio.h>

int main()
{
  int n,m,i,j,count;
  scanf("%d%d",&n,&m );

  int k[11],s[11][11],p[11];
  int bit;
  int ans =0;

  for(i=0; i<m; i++){
    scanf("%d", &k[i]);
      for(j=0; j<k[i]; j++)
        scanf("%d",&s[i][j]);
  }
  for(i=0; i<m; i++)
    scanf("%d", &p[i]);


  for(bit = 0; bit < (1<<n); bit++){//スイッチパターンをbit全探索するループ
    for(i = 0; i<m; i++){ //i番目の電球が点灯するかどうか調べる
      count = 0;
      for(j = 0; j<k[i]; j++){
        if(bit & (1 << (s[i][j] - 1)))
        //i番目の電球につながっているスイッチそれぞれがonの場合、count++
          count++;
      }
      if(count%2 != p[i]){ //i番目の電球が点灯しない場合は次のスイッチパターンへ
        break;
      }
      if(i == (m-1)){ //m-1個の電球が全て点灯した場合、ans++
        ans++;
      }
    }
  }

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

  return 0;
}


Measured memory_bytes: 1732
N = int(input())
L = list(map(int,input().split()))
M = [0]*9
for i in range(N):
  if 1<=L[i]<=399:
    if M[0] == 0:
      M[0] = 1
  elif 400<=L[i]<=799:
    if M[1] == 0:
      M[1] = 1
  elif 800<=L[i]<=1199:
    if M[2] == 0:
      M[2] = 1
  elif 1200<=L[i]<=1599:
    if M[3] == 0:
      M[3] = 1
  elif 1600<=L[i]<=1999:
    if M[4] == 0:
      M[4] = 1
  elif 2000<=L[i]<=2399:
    if M[5] == 0:
      M[5] = 1
  elif 2400<=L[i]<=2799:
    if M[6] == 0:
      M[6] = 1
  elif 2800<=L[i]<=3199:
    if M[7] == 0:
      M[7] = 1
  else:
    M[8] += 1
S =  M[0]+M[1]+M[2]+M[3]+M[4]+M[5]+M[6]+M[7]
if M[0] == M[1] == M[2] == M[3] == M[4] == M[5] == M[6] == M[7] == 0:
  print(1,M[8])
else:
  print(S,S+M[8])

Measured memory_bytes: 3064
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(){
    string s;
    cin>>s;
    string ans="No";
    bool flag[4]={false*4};
    for(ll i=0;i<s.size();++i){
        if(s[i]=='E'){
            flag[0]=true;
        }else if(s[i]=='W'){
            flag[1]=true;
        }else if(s[i]=='N'){
            flag[2]=true;
        }else if(s[i]=='S'){
            flag[3]=true;
        }
    }
    if(flag[0]==flag[1] and flag[2]==flag[3]){
        ans="Yes";
    }
    
    cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
constexpr i64 MOD = 1e9 + 7;

int main() {
    int h, w;
    cin >> h >> w;
    vvi dp(h, vi(w));
    vvi b(h, vi(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            char c;
            cin >> c;
            b[i][j] = c == '#';
        }
    }

    dp[0][0] = 1;
    int dx[] = {0, 1};
    int dy[] = {1, 0};
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (b[i][j]) continue;
            for (int k = 0; k < 2; k++) {
                int x = i + dx[k], y = j + dy[k];
                if (x < h && y < w && !b[x][y]) {
                    dp[x][y] += dp[i][j];
                    dp[x][y] %= MOD;
                }
            }
        }
    }
    cout << dp[h - 1][w - 1] << endl;
}

Measured memory_bytes: 16000
a = raw_input().strip().split()
[x1,y1,x2,y2] = [int(i) for i in a]
[x3,y3,x4,y4] = [(x2+y1-y2), (x2-x1+y2), (x1+y1-y2), (x2-x1+y1)]
print(str(x3)+" "+str(y3)+" "+str(x4)+" "+str(y4))


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

int main()
{
	int n;
	cin>>n;
	int p[8],q[8];
	for(size_t i = 0; i<n; i++)
	{
		cin>>p[i];
	}	
	for(size_t i = 0; i<n; i++)
	{
		cin>>q[i];
	}
	int pc=0,qc=0;
	vector<int>pq(n);
	for(size_t i = 0; i<n; i++)
	{
		pq[i] = i+1;
	}
	sort(pq.begin(),pq.end());
	int d = 0;
	do
	{
		d++;

		int tfp = 1;
		for(size_t i = 0; i<n; i++)
		{
			if(p[i]!=pq[i])
				tfp = 0;
		}
		if(tfp==1)
			pc = d;

		int tfq = 1;
		for(size_t i = 0; i<n; i++)
		{
			if(q[i]!=pq[i])
				tfq = 0;
		}
		if(tfq==1)
			qc = d;
		
	} while(next_permutation(pq.begin(),pq.end()));
	d = 0;
	if(pc>qc)
		cout<<pc-qc<<endl;
	else
		cout<<qc-pc<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
using namespace std;
typedef long long ll;

const int MAX_N = 2 * 1e5 + 2;

bool is_used[MAX_N];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    vector<ll> last_pos(MAX_N, -1);
    vector<ll> next_pos(N, -1);
    for (ll i = 2 * N - 1; i >= 0; i--) {
        if (last_pos[A[i % N]] >= 0) {
            next_pos[i % N] = last_pos[A[i % N]];
        }
        last_pos[A[i % N]] = i % N;
    }
    ll now_idx = 0, next_idx = next_pos[now_idx];
    ll period = 0;
    if (now_idx < next_idx) period += next_idx - now_idx + 1;
    else if (now_idx > next_idx) period += N - now_idx + next_idx + 1;
    else period += N + 1;
    while (next_idx != N - 1) {
        now_idx = (next_idx + 1) % N;
        next_idx = next_pos[now_idx];
        if (now_idx < next_idx) period += next_idx - now_idx + 1;
        else if (now_idx > next_idx) period += N - now_idx + next_idx + 1;
        else period += N + 1;
    }
    ll rest = (N * K) % period;
    vector<int> s;
    ll now_p = 0;
    while (rest > 0) {
        ll next_p = next_pos[now_p];
        ll dist;
        if (now_p < next_p) dist = next_p - now_p + 1;
        else if (now_p > next_p) dist = N - now_p + next_p + 1;
        else dist = N + 1;
        if (rest < dist) {
            for (int i = 0; i < rest; i++) {
                if (is_used[A[now_p]]) {
                    while (is_used[A[now_p]]) {
                        is_used[s[s.size() - 1]] = false;
                        s.pop_back();
                    }
                }
                else {
                    s.push_back(A[now_p]);
                    is_used[A[now_p]] = true;
                }
                now_p = (now_p + 1) % N;
            }
        }
        now_p = next_p + 1;
        rest -= min(rest, dist);
    }
    for (int i = 0; i < s.size(); i++) cout << s[i] << ' ';
    cout << endl;
    return 0;
}

Measured memory_bytes: 6516
#include <iostream>
#include <stack>
using namespace std;

#define DEBUG false

string on[3] = { "2","1","0" };
string om[3][3] = { {"0","0","0"}, {"0","1","1"}, {"0","1","2"} };
string os[3][3] = { {"0","1","2"}, {"1","1","2"}, {"2","2","2"} };

string tab="";

/* \¶ðÍ */
string cal( string s ){
  stack<string> st;
  string r, op1,op2;
  unsigned int i,n, kind;

  if( DEBUG ){
    tab+="  ";
    cout << tab << "cal : " << s << endl;
  }
  if( s[0]=='-' ){       // Ûè
    r="";
    for( i=1,n=0; i<s.length();i++ ){
      r += s[i];
      if( s[i]=='(' )      n++;
      else if( s[i]==')' ) n--;
      if( s[i]=='-' ) continue;
      if( n==0 ) break;
    }
    r = cal( r );
    s = on[ r[0]-'0' ];
  }else if( s[0]=='(' ){ // _aE_Ï
    // op1ðßé
    for( i=1,n=0,op1="";i<s.length();i++ ){
      op1 += s[i];
      if( s[i]=='(' )      n++;
      else if( s[i]==')' ) n--;
      if( s[i]=='-' ) continue;
      if( n==0 ){ ++i; break; }
    }
    if( s[i]=='+' ) kind=1;
    else            kind=2;
    // op2ðßé
    for( ++i,n=0,op2="";i<s.length();i++ ){
      op2 += s[i];
      if( s[i]=='(' )      n++;
      else if( s[i]==')' ) n--;
      if( s[i]=='-' ) continue;
      if( n==0 ) break;
    }
    op1 = cal( op1 );
    op2 = cal( op2 );
    int i1=op1[0]-'0', i2=op2[0]-'0';
    if( kind==1 ) s = os[ i1 ][ i2 ];
    else          s = om[ i1 ][ i2 ];
  }
  if( DEBUG ){
    cout << tab << " return : " << s << endl;
    if( tab.length()>=2 )
      tab = tab.substr( 2 );
  }
  return s;
}

int parse( string s, int p, int q, int r ){   // u·
  for( unsigned int i=0;i<s.length();i++ ){
    if( s[i]=='P' )      s[i] = p+'0';
    else if( s[i]=='Q' ) s[i] = q+'0';
    else if( s[i]=='R' ) s[i] = r+'0';
  }
  return cal( s )[0]-'0';
}


int main(){
  string s;
  while( cin>>s && s!="." ){
    if( DEBUG ){
      cout << "input : " << s << endl;
    }
    int ans=0;
    for( int p=0;p<3;p++ )
      for( int q=0;q<3;q++ )
	for( int r=0;r<3;r++ ){
	  if( parse( s,p,q,r )==2 )
	    ans++;
	  if( DEBUG ){
	    p=3;q=3;r=3; // fobOp
	  }
	}
    cout << ans << endl;
    //    break;
  }
  return 0;
}

Measured memory_bytes: 0
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <list>
#include <tuple>
#include <bitset>
#include <ciso646>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef vector<ll> vec;

inline bool cheak(ll x, ll y, ll xMax, ll yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }
inline ll toInt(string s) { ll v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }

#define For(i,a,b)	for(ll (i) = (a);i < (b);(i)++)
#define rep(i,n)	For(i,0,n)
#define rFor(i,a,b)	for(ll (i) = (a-1);i >= (b);(i)--)
#define rrep(i,n)	rFor(i,n,0)
#define clr(a)		memset((a), 0 ,sizeof(a))
#define mclr(a)		memset((a), -1 ,sizeof(a))
#define all(a)		(a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1e9 + 9;

int count(ll n){
	int ret = 0;
	for (int j = 1; j <= n; j++){
		if (n % j == 0){
			ret++;
		}
	}
	return ret;
}

int main(){

	ll n;
	cin >> n;
	for (int i = 1;; i++){
		if (count(i) == n){
			cout << i << endl;
			return 0;
		}
	}

	return 0;
}

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

int main(){
    string S;
    cin >> S;
    unordered_set<char> US;
    for(int i = 0; i <= S.size(); ++i){
        if(US.find(S[i]) != US.end()){
            cout << "no" << endl;
            return 0;
        }
        else US.insert(S[i]);
    }
    cout << "yes" << endl;
    return 0;
}

Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
const long long maxn=2e5+5;
const long long inf=0x3f3f3f3f;
long long a[maxn];
bool cmp(long long a,long long b)
{
	return a>b;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	long long n;
	cin>>n;
	for(long long i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n+1,cmp);
	long long x=a[1],m=n;
	m-=2;
	for(long long i=2;i<=n;i++)
	{
		if(m==0)
			break;
		m--;
		x+=a[i];
		if(m==0)
			break;
		m--;
		x+=a[i];
		if(m==0)
			break;
	}
	cout<<x<<endl;
	return 0;
}




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

enum{INI, QRY, END};
typedef long long int ll;

const int MAX = 2e5+7;
const ll inf = 1e18;

struct Event
{
  int x, a, b;
  ll v;
  int t;
  Event(){}
  Event(int x, int a, int b, ll v, int t)
    : x(x), a(a), b(b), v(v), t(t) {}
  bool operator <(const Event& e)const
  {
    if(x != e.x) return x < e.x;
    return t < e.t;
  }
};

ll T[MAX << 2], lazy[MAX << 2];
int n, m;
#define mid ((x+xend)>>1)
#define lef (nod<<1)
#define rig ((nod<<1)|1)

void push(int nod, int x, int xend)
{
  if(!lazy[nod]) return;
  T[nod] += lazy[nod];
  if(x != xend)
    lazy[lef] += lazy[nod],
    lazy[rig] += lazy[nod];
  lazy[nod] = 0;
}

void upd(int a, int b, ll v, int nod=1, int x=1, int xend=n)
{
  push(nod, x, xend);
  if(a <= x && xend <= b)
  {
    lazy[nod] += v;
    push(nod, x, xend);
    return;
  }
  if(xend < a || x > b) return;
  upd(a, b, v, lef, x, mid);
  upd(a, b, v, rig, mid+1, xend);
  T[nod] = max(T[lef], T[rig]);
}

ll qry(int a, int b, int nod=1, int x=1, int xend=n)
{
  push(nod, x, xend);
  if(a <= x && xend <= b) return T[nod];
  if(xend < a || x > b) return -inf;
  return max(qry(a, b, lef, x, mid), qry(a, b, rig, mid+1, xend));
}

void dump()
{
  for(int i=1;i<=5;++i) cerr << i << ' ' << T[i] << endl;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  
  cin >> n >> m;
  vector<Event> E;
  
  for(int i=1;i<=m;++i)
  {
    int a, b, v;
    cin >> a >> b >> v;
    E.emplace_back(a, a, b, v, INI);
    E.emplace_back(b, a, b, v, END);
  }
  for(int i=1;i<=n;++i) E.emplace_back(i, -1, -1, -1, QRY);
  sort(E.begin(), E.end());
  

  ll ans = -inf;
  for(auto& e: E)
  {
    if(e.t == INI)
    {
      upd(e.a, e.b, -e.v);
    }else if(e.t == QRY)
    {
      ll C = -qry(e.x, e.x);
      ll dp = max(C, C + (e.x>1?qry(1, e.x-1):0));
      upd(e.x, e.x, dp);
      ans = max(ans, dp);
    }else // e.t == END
    {
      upd(e.a, e.b, e.v);
    }
    //~ dump();
  }
  
  cout << max(0ll, ans) << endl;
  
  return 0;
}


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

#define MAX_NUM 13000

bool dp[80][80][MAX_NUM+100];
int a[80][80], b[80][80], c[80][80];

int main() {
    int h, w;
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> b[i][j];
            c[i][j] = abs(a[i][j] - b[i][j]);
        }
    }

    dp[0][0][c[0][0]] = true;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < MAX_NUM; k++) {
                if (i) dp[i][j][k] |= (dp[i - 1][j][k + c[i][j]] || dp[i - 1][j][abs(k - c[i][j])]);
                if (j) dp[i][j][k] |= (dp[i][j - 1][k + c[i][j]] || dp[i][j - 1][abs(k - c[i][j])]);
            }
        }
    }
    for (int k = 0; k < 13000; k++) {
        if (dp[h - 1][w - 1][k]) {
            cout << k << endl;
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 82176
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long int MOD = 1000000007;
const long long  INF = 1LL << 60;

int main(void){
    int N; cin >> N;
    vector<int> h(N); for(int i = 0; i < N; i++) cin >> h[i];
    
    vector<ll> dp(N+10, INF);
    dp[0] = 0;
    for(int i = 0; i < N; i++){
        dp[i+1] = min(dp[i+1], dp[i] + abs(h[i+1] - h[i]));
        dp[i+2] = min(dp[i+2], dp[i] + abs(h[i+2] - h[i]));
    }
    cout << dp[N-1] << endl;
}


Measured memory_bytes: 1408
import math
n = int(input())
s = int(input())

if n == s:
    print(n+1)
    quit()

def f(b, n):
    ret = 0
    while n > 0:
        ret += n % b
        n = n // b
    return ret

for i in range(2, int(math.sqrt(n)+2)):
    t = f(i, n)
    if s == t:
        print(i)
        quit()

ans = 10**18

for x in range(1, int(math.sqrt(n)+10)):
    y = s - x
    b = (n-s)//x+1
    if x < b and y < b and f(b, n) == s:
        ans = min(ans, b)
if ans == 10**18:
    ans = -1
print(ans)


Measured memory_bytes: 3064
#!/usr/bin/env python3.8.2
#oj t -c "python3 ./main.py" -d ./tests/


N = int(input())
M = 1
answer = 0
while M <= N:
    if (M % 3 != 0) & (M % 5 != 0):
        answer += M
        M = M + 1
    else:
        M = M + 1
print(answer)

Measured memory_bytes: 9176
import java.util.Scanner;

public class Main {

	public static void main(final String[] args) {
		Scanner sc = new Scanner(System.in);
		int d = sc.nextInt(), t = sc.nextInt(), s = sc.nextInt();

		if (s*t>=d) {
			System.out.println("Yes");
		}else {
			System.out.println("No");
		}
	}
}


Measured memory_bytes: 35636
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;

int main(){
string str;

char ch[10][6];
char n1[10]="* =* ***";
char n2[10]="* =** **";
char n3[10]="* =*** *";
char n4[10]="* =**** ";
char n5[10]=" *= ****";
char n6[10]=" *=* ***";
char n7[10]=" *=** **";
char n8[10]=" *=*** *";
char n9[10]=" *=**** ";
char n0[10]="* = ****";
char nn[10];
bool first=true;
while(cin>>str){

	if(first==false)cout<<endl;
	first=false;
int t=5-str.size();
bool f=true;

for(int i=0;i<5;i++){
	if(i<t){		
	for(int j=0;j<8;j++)
	nn[j]=n0[j];
	f=false;
}
	else{

char y=str[i-t];


if(y=='0'){
	for(int j=0;j<8;j++)
	nn[j]=n0[j];
}
else if(y=='1'){
	for(int j=0;j<8;j++)nn[j]=n1[j];
}
else if(y=='2'){
	for(int j=0;j<8;j++)nn[j]=n2[j];
}
else if(y=='3'){
	for(int j=0;j<8;j++)nn[j]=n3[j];
}
else if(y=='4'){
	for(int j=0;j<8;j++)nn[j]=n4[j];
}
else if(y=='5'){
	for(int j=0;j<8;j++)nn[j]=n5[j];
}
else if(y=='6'){
	for(int j=0;j<8;j++)nn[j]=n6[j];
}
else if(y=='7'){
	for(int j=0;j<8;j++)nn[j]=n7[j];
}
else if(y=='8'){
	for(int j=0;j<8;j++)nn[j]=n8[j];
}
else if(y=='9'){
	for(int j=0;j<8;j++)nn[j]=n9[j];
}
	if(y!='9'&&y!='4')f=false;
	}
	
for(int j=0;j<8;j++){

ch[j][i]=nn[j];

}
	
}
int w=8;
//	if(f==true)w=7;
for(int i=0;i<w;i++){
for(int j=0;j<5;j++)
cout<<ch[i][j];

cout<<endl;
}

}
return 0;
}

Measured memory_bytes: 1180
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

int main()
{
    int n;
    long long d;
    cin >> n >> d;

    int ans = 0;
    while(--n >= 0){
        long long x, y;
        cin >> x >> y;
        if(x * x + y * y <= d * d)
            ++ ans;
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3612
N,M = map(int,input().split())

d = []
res = 0

for i in range(N):
    A,B = map(int,input().split())
    d.append((A,B))

d.sort(reverse = True)

while M:
    x = d.pop()
    n = min(M,x[1])
    res += n * x[0]
    M -= n
print(res)

Measured memory_bytes: 16892
import qualified Data.Vector.Unboxed as V
import Data.Array.IArray (listArray, accumArray, (!))
import Data.Array.MArray
import Data.Array.ST (STUArray, runSTUArray)
import Data.Array.Unboxed (UArray)
import Data.List (sort, sortOn, sortBy, unfoldr, partition)
import Data.Char (isSpace)
import Control.Monad.ST (ST)
import Control.Monad.Trans.State (StateT(..))
import qualified Data.ByteString.Char8 as B

-- utility functions

--   Input functions
readInt = B.readInt . B.dropWhile isSpace

readInts :: Int -> B.ByteString -> [Int]
readInts n = take n . unfoldr readInt

readIntsV :: Int -> B.ByteString -> V.Vector Int
readIntsV n = V.unfoldrN n readInt

readIntsA :: Int -> B.ByteString -> UArray Int Int
readIntsA n bs = runSTUArray $ do
  arr <- newArray_ (0,n-1)
  go arr 0 bs
  return arr
  where
    go :: STUArray s Int Int -> Int -> B.ByteString -> ST s ()
    go arr i bs
      | i==n = return ()
      | otherwise = do
        let Just (x,bs') = readInt bs
        writeArray arr i x
        go arr (i+1) bs'

readTsV :: Int -> B.ByteString -> V.Vector (Int,Int)
readTsV n = V.unfoldrN n $ runStateT ((,) <$> StateT readInt <*> StateT readInt)

readTs :: Int -> B.ByteString -> [(Int,Int)]
readTs n = take n . unfoldr readT where
  readT = runStateT ((,) <$> StateT readInt <*> StateT readInt)

-- main

a # b = abs (a-b)

solv n as = sum $ map (#b) bs where
  bs = sort $ zipWith subtract [1..] as
  b = bs !! div n 2

main = do
  n <- read <$> getLine
  as <- readInts n <$> B.getLine
  print $ solv n as


Measured memory_bytes: 32124