akhauriyash/Code-Regression huggingface dataset:
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
int main() {
	while (true) {
		int r, n;
		cin >> r >> n;
		if (r == 0) return 0;
		vector<tuple<double, double, double>> inputs;
		REP(i, n) {
			double a, b, c;
			cin >> a >> b >> c;
			inputs.push_back(make_tuple(a, b, c));
		}
		inputs.push_back(make_tuple(-100, 100, 0));
		double bot_time = 0;
		double top_time = 100;
		REP(t, 100) {
			double mid_time = (bot_time + top_time) / 2.0;
			int ng = 0;
			for (double q = -r; q <= r-1.0; ++q) {
				double required_length = r * r - q * q;
				required_length = sqrt(required_length);
				int ok = 0;
				REP(j, inputs.size()) {
					if (get<0>(inputs[j]) <= q && q < get<1>(inputs[j]) && (get<2>(inputs[j]) + r - mid_time) >= required_length) {
						ok = 1;
						break;
					}
				}
				if (ok == 0) {
					ng = 1;
					break;
				}
			}
			for (double q = r; q >= -r + 1.0; --q) {
				double required_length = r * r - q * q;
				required_length = sqrt(required_length);
				int ok = 0;
				REP(j, inputs.size()) {
					if (get<0>(inputs[j]) < q && q <= get<1>(inputs[j]) && (get<2>(inputs[j])+r - mid_time) >= required_length) {
						ok = 1;
						break;
					}
				}
				if (ok == 0) {
					ng = 1;
					break;
				}
			}
			if (ng == 0) {
				bot_time = mid_time;
			}
			else {
				top_time = mid_time;
			}
		}
		cout << fixed << setprecision(10);
		cout << bot_time << endl;
	}
}


Measured memory_bytes: 3276
#include<iostream>
#include<cstdio>
#include<cstring>
namespace fuck
{
	typedef long long ll;
	const int N=100010,M=N*2;
	int begin[N],next[M],to[M],w[M];
	int siz[N],f[10];
	int n,e,tot;
	ll ans;
	void add(int x,int y,int z,bool k=1)
	{
		to[++e]=y;
		next[e]=begin[x];
		begin[x]=e;
		w[e]=z;
		if(k)add(y,x,z,0);
	}
	void dfs(int p=1,int h=0,int pre=0)
	{
		siz[p]=1;
		bool flag=1;
		for(int i=begin[p],q;i;i=next[i])
			if((q=to[i])!=h)
			{
				dfs(q,p,w[i]);
				siz[p]+=siz[q];
				if(siz[q]>n/2)flag=0;
			}
		ans+=(ll)pre*std::min(siz[p],n-siz[p]);
		if(n-siz[p]>n/2)flag=0;
		if(flag)f[++tot]=p;
	}
	void solve()
	{
		scanf("%d",&n);
		for(int i=1,u,v,k;i<n;i++)
			scanf("%d%d%d",&u,&v,&k),add(u,v,k);
		ans=0;
		dfs();
		ans*=2;
		if(tot==1)
		{
			int min=1000000000,p=f[1];
			for(int i=begin[p];i;i=next[i])
				if(w[i]<min)min=w[i];
			ans-=min;
		}
		else 
		{
			int p=f[1],q=f[2];
			for(int i=begin[p];i;i=next[i])
				if(to[i]==q){ans-=w[i];break;}
		}
		printf("%lld\n",ans);
	}
}
int main()
{
	fuck::solve();
	return 0;
}


Measured memory_bytes: 9984
object Main extends App{
  import scala.io.StdIn.readInt
  
  val m = readInt

  def f(x:Long):Long ={
  	if(x % 2 == 0)return x/2
    else return 3*x + 1
  }
  
  def g(acc:Long,x:Long):Long =
  	x match{
      case 4 => acc + 3
      case _ => g(acc + 1,f(x))
  }
  if(m == 1){println("4")}
  else if(m == 2){println("4")}
  else println(g(1,m).toString)
}


Measured memory_bytes: 54628
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

int main(){
  while(1){
  int n;
  cin>>n;
  if(n==0) break;
  int s[n];
  int sum = 0;
  double average,dev2=0.0;
  for(int i=0;i<n;i++){
    cin>>s[i];
    sum+=s[i];
  }
  average = (double)sum / n ;
  for(int i=0;i<n;i++){
    dev2 += (s[i]-average)*(s[i]-average);
  }
  printf("%.10lf\n",sqrt(dev2/n));
  }
  return 0;
}

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    reverse(v.begin(), v.end());
    for(int i=0; i<m; ++i) {
        int q;
        cin >> q;
        v.push_back(q);
    }
    set<int> s;
    for(int i=v.size()-1; s.size() < n; --i) {
        if(s.count(v[i]) == 0) {
            cout << v[i] << endl;
        }
        s.insert(v[i]);
    }
}


Measured memory_bytes: 13500
#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> point;
char board[50][50];

int main(int argc, char *argv[]) {
	int H, W;
	cin >> H >> W;

	int white = 0;
	for (int y = 0; y < H; y++) {
		string s;
		cin >> s;
		for (int x = 0; x < W; x++) {
			board[x][y] = s[x];
			if (board[x][y] == '.') white++;
		}
	}

	queue<point> open;
	open.push(point(0, 0));
	board[0][0] = 0;
	while (!open.empty()) {
		point p = open.front(); open.pop();
		int x = p.first, y = p.second;
		int s = board[x][y];

		if (x == W - 1 && y == H - 1) {
			break;
		}

		if (x + 1 < W && board[x + 1][y] == '.') {
			board[x + 1][y] = s + 1;
			open.push(point(x + 1, y));
		}
		if (0 <= x - 1 && board[x - 1][y] == '.') {
			board[x - 1][y] = s + 1;
			open.push(point(x - 1, y));
		}
		if (y + 1 < H && board[x][y + 1] == '.') {
			board[x][y + 1] = s + 1;
			open.push(point(x, y + 1));
		}
		if (0 <= y - 1 && board[x][y - 1] == '.') {
			board[x][y - 1] = s + 1;
			open.push(point(x, y - 1));
		}
	}

	if (board[W - 1][H - 1] == '.') {
		cout << "-1" << endl;
	} else {
		cout << white - board[W - 1][H - 1] - 1 << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n) - 1; i >= (s); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
const long long MOD = 1000000007, INF = 1e18;
template<class T>inline bool CHMAX(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool CHMIN(T&a,T b){if(a>b){a=b;return true;}return false;}



signed main(){
	int N;
	cin>>N;
	vector<int> a(N); for(auto& e:a)cin >> e;
	int ma = a[0]-1;
	FOR(i,1,N){
		if(ma>a[i]){
			cout<<"No"<<endl;
			return 0;
		}else if(ma == a[i])continue;
		else{
			CHMAX(ma,a[i]-1);
		}
	}
	cout<<"Yes"<<endl;
}

Measured memory_bytes: 1024
import math
INF, MOD = float("inf"), 1e9 + 7
MAX, MIN = -INF, INF
dx1, dy1, dx2, dy2 = [-1, 0, 1, 0], [0, -1, 0, 1], [-1, 0, 1, -1, 1, -1, 0, 1], [-1, -1, -1, 0, 0, 1, 1, 1]

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

def mins(x, y):
    x = min(x, y)

def maxs(x, y):
    x = max(x, y)

while(True):
    try:
        n = int(input())
        a = list(map(int, input().split()))
        mx = max(a)
        if (sum(a) - mx) > mx:
            print("Yes")
        else:
            print("No")
    except EOFError:
        exit()


Measured memory_bytes: 3064
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


signed main(void) {
    int n,m;
    cin >> n >> m;
    vi a(2*n);
    rep(i,2*n)a[i] = i+1;
    //rep(i,2*n)cout << " " << a[i];cout << endl;
    while(m--){
        int k = 0;
        cin >> k;
        vi b(2*n);
        if(k == 0){
            rep(i,n){
                b[2*i] = a[i];
                b[2*i+1] = a[n+i];
            }
        }else{
            rep(i,k)b[2*n-k+i] = a[i];
            //cout << "!";rep(i,2*n)cout << " " << b[i];cout << endl;
            loop(i,k,2*n)b[i-k] = a[i];
            //cout << endl;
        }
        a = b;
        //rep(i,2*n)cout << " " << a[i];cout << endl;
    }
    rep(i,2*n)cout << a[i] << endl;
}



Measured memory_bytes: 3136
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10**9+7
n = int(readline())
u = readline().rstrip().decode('utf-8')
d = readline().rstrip().decode('utf-8')

i = 0
ans = 1
touch = 0
while True:
    if i >= n:
        break
    if u[i] == d[i]:
        ans *= 3-touch
        touch = 1
        i += 1
    else:
        if touch == 2:
            ans *= 3
            i+=2
        else:
            ans *= (3-touch)*(2-touch)
            touch = 2
            i += 2

print(ans%mod)
    

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

int main(){
  int H, n, counter;
  cin >> H >> n;
  
  for (int i = 0; i < n; ++i){
    int temp;
    cin >> temp;
    counter += temp;
  }
  
  if (counter >= H) 
    cout << "Yes";
  else 
    cout << "No";
}

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

#pragma region Macro
using namespace std;
typedef long long ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define SORT(v) sort(v.begin(), v.end())
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define vvi vector<vector<int>>
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))

typedef pair<int, int> P;
#pragma endregion

#pragma region Debug
void debug_out() { cout << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cout << H << " ";
	debug_out(T...);
}

#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#pragma endregion

// コンパイル時、エイリアス「debug」を使用 --> マクロが使えるように

void solve(){
	#define int ll
	// ここに処理を記述
	// コンパイル時はエイリアス「gpp」--> c++17でコンパイル
	
	int n;
	cin >> n;

	int ans = 0;
	FOR(a,1,n){
		int left = 1;
		int right = n;

		int mid = 0;
		while(abs(right - left) > 1){
			mid = (left + right) / 2;

			if(a * mid < n) left = mid;
			else right = mid;
			//if(a==12) debug(mid);
		}
		debug(a,"/",mid);

		ans += left;
	}

	cout << ans << endl;

}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	
	solve();
	return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <stack>
#include <bitset>
#include <utility>
#include<iomanip>
// #include <numeric>
using namespace std;



typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll,ll> pint;
typedef vector<pint> vpint;

#define rep(i,n) for(ll i=0;i<n;i++)
#define repf(i,f,n) for(ll i=f;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define INT_MAX 2147483647
#define LLONG_MAX 9223372036854775807
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define DIVIDER 1000000007


//
// struct Node{
//   vint children;
//   ll index;
//   ll prop;
// };
// struct edge{ll to; ll cost;};



int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0,};

// ll X,Y,Z,K;
// ll pmax = 30000000000;
// vint A,B,C;




// int binary_search(int key){
//  //isOKを満たす要素のうち最小のインデックスを返す。（要ソート）
//   int left = -1;
//   int right = N;
//
//   while(right - left > 1){
//     int mid = left + (right - left)/2;
//
//     if(isOK(mid, key)) right=mid;
//     else left=mid;
//   }
//
//   return right;
// }

// ll gcd(ll m, ll n){
//   if(m<n) swap(m,n);
//   if(n==0) return m;
//   return gcd(n, m%n);
// }

// ll get_pow(ll x, ll y){
//   ll ret=0;
//   ll temp = y;
//   while(true){
//     if(x<=temp) break;
//     ret++;
//     temp *= 2;
//   }
//   return ret;
// }

ll factorial(ll n){
  if(n==0) return 1;
  return n*factorial(n-1);
}

ll comb(ll n, ll r){
  return factorial(n)/factorial(n-r)/factorial(r);
}

ll mypow(ll x, ll y){// return x**y
  ll rtn = 1;
  rep(i,y){
    rtn*=x;
  }
  return rtn;
}



int main() {
  cout<<fixed<<setprecision(10);

  ll N,P;cin>>N>>P;
  vint A(N);rep(i,N)cin>>A[i];

  ll r = 0;
  rep(i,N){
    if(A[i]%2==1) r++;
  }
  ll ans;
  if(r==0){
    if(P==0){
      ans = mypow(2,N);
    }else{
      ans = 0;
    }
  }else{
    ans = mypow(2,N-1);
  }

  cout << ans << endl;
}

















//


Measured memory_bytes: 256
s = input()
n = len(s)
mod = 10**9+7
dp = [[0]*13 for _ in range(n+1)]
dp[0][0] = 1
for i in range(n):
    if s[i] == "?":
        for t in range(10):
            for k in range(13):
                dp[i+1][(k*10+t)%13] += dp[i][k]
        for k in range(13):
            dp[i+1][k] %= mod
    else:
        t = int(s[i])
        for k in range(13):
            dp[i+1][(k*10+t)%13] += dp[i][k]
print(dp[n][5])

Measured memory_bytes: 63892
c = [[int(x) for x in input().split()] for _ in range(3)]

ok = True

for i in range(2):
    if (c[i][0] - c[i + 1][0]) == (c[i][1] - c[i + 1][1]) == (c[i][2] - c[i + 1][2]):
        pass
    else:
        ok = False

for j in range(2):
    if (c[0][j] - c[0][j + 1]) == (c[1][j] - c[1][j + 1]) == (c[2][j] - c[2][j + 1]):
        pass
    else:
        ok = False

print('Yes' if ok else 'No')


Measured memory_bytes: 3064
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner scan = new Scanner(System.in);
        int wake_h = scan.nextInt();
        int wake_m = scan.nextInt();
        int sleep_h = scan.nextInt();
        int sleep_m = scan.nextInt();
        int study = scan.nextInt();
        int total;
        total = (sleep_h*60 + sleep_m) - (wake_h*60 + wake_m) - study;
        System.out.println(total);
    }
}


Measured memory_bytes: 35708
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <cstdio>

using namespace std;

typedef long long ll;

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

int main(int argc, char **argv)
{
    int N;
    ll A, B, C;
    cin >> N >> A >> B >> C;

    string s[N];
    REP(i, N) cin >> s[i];

    ll V[3];
    V[0] = A;
    V[1] = B;
    V[2] = C;
    char ans[N];
    REP(i, N) {
        int c1 = (int)(s[i][0] - 'A');
        int c2 = (int)(s[i][1] - 'A');
        if (V[c1] == 0 && V[c2] == 0) {
            cout << "No" << endl;
            return 0;
        }
        else if (V[c1] > 0 && V[c2] == 0) {
            ans[i] = 'A' + c2;
            V[c2]++;
            V[c1]--;
        }
        else if (V[c1] == 0 && V[c2] > 0) {
            ans[i] = 'A' + c1;
            V[c1]++;
            V[c2]--;
        }
        else {
            if (i == N - 1) {
                ans[i] = 'A' + c1;
                V[c1]++;
                V[c2]--;
            }
            else {
                if (V[c1] == 1 && (s[i + 1][0] == s[i][0] || s[i + 1][1] == s[i][0])) {
                    ans[i] = 'A' + c1;
                    V[c1]++;
                    V[c2]--;
                }
                else if (V[c2] == 1 && (s[i + 1][0] == s[i][1] || s[i + 1][1] == s[i][1])) {
                    ans[i] = 'A' + c2;
                    V[c2]++;
                    V[c1]--;
                }
                else {
                    ans[i] = 'A' + c1;
                    V[c1]++;
                    V[c2]--;
                }
            }
        }
    }
    cout << "Yes" << endl;
    REP(i, N) {
        cout << ans[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 6812
def resolve():
    X, K, D = list(map(int, input().split()))
    X = -X if X < 0 else X
    m = X//D
    if m < K:
        if X%D > D-X%D:
            nmove = m+1
            print(D-X%D if (K-nmove)%2==0 else X%D)
        else:
            nmove = m
            print(X%D if (K-nmove)%2==0 else D-X%D)
    else:
        nmove = m
        print(X-K*+D)


if '__main__' == __name__:
    resolve()

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint& arg1, const pint& arg2) { return arg1.first > arg2.first; }
template<class T> void chmax(T& a, T b) { if (a < b) { a = b; } }
template<class T> void chmin(T& a, T b) { if (a > b) { a = b; } }

struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};

int main(void) {
    int n, m;
    cin >> n >> m;

    vector<int> a(m);
    vector<int> b(m);
    for(int im=0;im<m;im++) {
        cin >> a.at(im) >> b.at(im); a.at(im)--; b.at(im)--;
    }

    int ans = 0;
    for(int im=0;im<m;im++) {
        UnionFind u(n);
        for(int jm=0;jm<m;jm++) {
            if(im==jm) continue;
            u.merge(a.at(jm), b.at(jm));
        }
        bool flg = false;
        for(int in=0;in<n;in++) {
            for(int jn=in;jn<n;jn++) {
                if(!u.issame(in, jn)) {
                    flg = true;
                }
            }
        }
        if(flg) ans++;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <climits>
#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define in insert
#define vll vector<ll>
#define endl "\n"
#define pll pair<ll,ll>
#define f first
#define s second
#define int ll
#define sz(x) (ll)x.size()
#define all(x) (x.begin(),x.end())
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
 
 
const ll INF = 1e18;
const ll N =(100+5);
ll a[N];
void solve(){
    ll k,t;
    cin >> k >> t;
    for(int i = 1;i <= t;i++){
        cin >> a[i];
    }
    ll ans= 0;
    ll prev = -1;
    while(k){
        ll gg = -1;
        for(int j = 1;j<=t;j++){
            if(a[j] && prev != j && (gg == -1 || a[j] > a[gg])){
                gg = j;
            }
        }
        if(gg == -1) {
            gg = prev;
            ans++;
        }
        a[gg]--;
        prev = gg;
        k--;
    }
    cout << ans << endl;
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    

 
     ll tt=1;   
    while(tt--){    
        solve();
    }    
}

Measured memory_bytes: 3652
#include <bits/stdc++.h>
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
#define _GLIBCXX_DEBUG

using ll = long long;
using ull = unsigned long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const ll mod = 1e9+7;
void chmod(ll &M){
  if(M >= mod) M %= mod;
  else if(M < 0){
     M += (abs(M)/mod + 1)*mod;
    M %= mod;
}
}
ll modpow(ll x, ll n){
   if(n==0) return 1;
  ll res=modpow(x, n/2);
 if(n%2==0) return res*res%mod;
else return res*res%mod*x%mod;
}
int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };
long long GCD(long long a, long long b) {
  if (b == 0) return a;
  else return GCD(b, a % b);
}

using namespace std;
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
int main()
{
    int A, B, C, K;
    cin >> A >> B >> C >> K;
    int ans = 0;
    if (K <= A + B) cout << min(K, A) << "\n";
    else cout << min(K, A) - (K-A-B) << "\n";
}


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

const int nmax = 8;
bool graph[nmax][nmax];

int dfs(int v, int N, bool visited[nmax]) {
  bool all_visited = true;

  for (int i = 0; i < N; i++) {
    if (visited[i] == false)
      all_visited = false;
  }

  if (all_visited) {
    return 1;
  }

  int ret = 0;

  for (int i = 0; i < N; i++) {
    if (graph[v][i] == false) continue;
    if (visited[i])continue;

    visited[i] = true;
    ret += dfs(i, N, visited);
    visited[i] = false;

  }
  return ret;
}

int main(void)
{
  int N, M;
  cin >> N >> M;

  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    graph[A-1][B-1] = graph[B-1][A-1] = true;
  }
  bool visited[nmax];
  for (int i = 0; i < N; i++) {
    visited[i] = false;
  }
  visited[0] = true;
  cout << dfs(0, N, visited) << endl;
  return 0;
}


Measured memory_bytes: 256
X, N = map(int, input().split(" "))
if N == 0:
  print(X)
  exit()
else:
  p = list(map(int, input().split(" ")))
  c = 0
  for i in range(200):
    if i+1 not in p:
      if abs(X-(i+1)) < abs(X-c):
        c = i+1
      else:
        print(c)
        exit()

Measured memory_bytes: 9192
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

string s;

string i2s(const int a) {
  stringstream ss;
  ss << a;
  return ss.str();
}

int c2i(const char c) {
  return c-'0';
}

int solve() {
  int cnt = 0;
  while (s.size() != 1) {
    char l = s[0], r = s[1];
    s.erase(0, 2);
    s = i2s(c2i(l)+c2i(r)) + s;
    ++cnt;
  }
  return cnt % 2 == 0;
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> s;
    printf("%s wins.\n", solve() ? "Audrey" : "Fabre");
  }
  return 0;
}

Measured memory_bytes: 1268
#include <stdio.h>

long long k;
int l=0;
int a[1000000];
int n;
int g[1000000];

void insertionsort(int a[], int n, int g)
{
	int i, v, j;
	for(i = g; i < n; i++){
		v = a[i];
		j = i - g;
		while(j >= 0 && a[j] > v){
			a[j+g] = a[j];
			j-=g;
			k++;
		}
		a[j+g] = v;
	}
}

void shellsort(int a[], int n)
{
	int h, j;
	for(h = 1; ;){
		if(h > n)break;
		g[l] = h;
		h = 3*h+1;
		l++;
	}
	for(j = l-1; j >= 0; j--){
		insertionsort(a, n, g[j]);
	}
}

int main(void)
{
	int i;
	scanf("%d", &n);
	for(i = 0; i < n; i++){
		scanf("%d", &a[i]);
	}
	k = 0;
	
	shellsort(a, n);
	
	printf("%d\n", l);
	
	for(i = l-1; i >= 0; i--){
		printf("%d", g[i]);
		if(i!=0)printf(" ");
	}
	printf("\n");
	printf("%d\n", k);
	
	for(i = 0; i < n; i++){
		printf("%d\n", a[i]);
	}
	
	return 0;
}



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



int main() {
double N;
cin >> N;
vector<double> num(N);

for(int i = 0; i < N; i++){
cin >> num[i];
}
sort(num.begin(),num.end());
double kati = 0;
for(int i = 0; i < N-1; i++){
kati = (num[i] + num[i+1])/2;
num[i+1] = kati;
}
cout << kati << endl;


}

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

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll,ll> P;

ll gcd(ll a, ll b){
	return b == 0 ? a : gcd(b, a % b);
}

ll seica(ll n, ll c, ll d){
	ll ret = 0;
	ret += n / c;
	ret += n / d;
	ret -= n / (c * d / gcd(c, d));
	return n - ret;
}

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

	ll a, b, c, d;cin >> a >> b >> c >> d;
	cout << seica(b, c, d) - seica(a - 1, c, d) << endl;

	return 0;
}


Measured memory_bytes: 256
while True:
    H,W = map(int,input().split())
    if W == H == 0:break
    for i in range(H):
        for j in range(W):
            if i == 0 or i == H-1 or j == 0 or j == W-1:
                print("#",end="")
            else:
                print(".",end="")
            j += 1
        print()
        i += 1
    print()


Measured memory_bytes: 7004
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

bool ok(vs64 h, s64 a, s64 b, s64 cnt)
{
    s64 ret = 0;
    for (auto& hp : h) ret += CEIL(MAX(0ll, hp - b * cnt), (a - b));
    return ret <= cnt;
}

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

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

    vs64 h(n);
    rep (i, n) cin >> h[i];

    s64 l = 0, r = 1e9;
    while (l + 1 < r)
    {
        s64 m = (l + r) / 2;
        if (ok(h, a, b, m)) r = m;
        else l = m;
    }

    cout << r << "\n";
    return 0;
}



Measured memory_bytes: 4668
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool can[1000010];
void solve(){
	int n;cin>>n;int high=INT_MIN;map<int,int>mp;
	vector<int>a(n);for(int i=0;i<n;i++) {cin>>a[i];mp[a[i]]++;high=max(high,a[i]);}
	for(int i=1;i<=high;i++) can[i]=true;
	for(int i=0;i<n;i++){
		for(int j=2*a[i];j<=high;j+=a[i]){
			can[j] = false;
		}
	}
	int cnt=0;
	for(int i=0;i<n;i++){
		if(can[a[i]]&&mp[a[i]]==1) cnt++;
	}
	cout<<cnt;
}	
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	int T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


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

#define all(a)  (a).begin(),(a).end()

#define mkp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;


int main(){
    bool flag=true;
    ll ans=0,sum=0;
    
    ll n,a,b;
    cin >>n>>a>>b;
    ll x[n];

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

        if((x[i]-x[i-1])*a > b ){
            ans += b;
        }else{
            ans += (x[i]-x[i-1])*a;
        }
    }
    



    //cout <<fixed<<setprecision(16)<< << endl;
    cout << ans << endl;

    //if(flag)cout << "Yes" <<endl;
    //else cout << "No" <<endl;
    
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unrolled-loops")
#define y1 hoainambimaysolokill
#define fi first
#define se second
#define PII pair<int,int>
#define PII2 pair<int,pair<int,int> >
#define PII3 pair<pair<int,int>,int >
const long long inf=1e18+3;
int n,cur[1010],a[1010][1010],ans,mov;
multiset<PII> ms;
vector<PII> store,store2;
void solve()
{
   cin>>n;
   for(int i=0;i<n;i++)
       for(int j=0;j<n-1;j++)
           cin>>a[i][j];
   int x,y,x2,y2;
   for(int i=0;i<n;i++){
       x=i,y=a[i][0]-1;
       if(x>y)swap(x,y);
       ms.insert({x,y});
       if(ms.count({x,y})>1){
          ms.erase({x,y});
          cur[x]++;
          cur[y]++;
          int x2=a[x][cur[x]]-1;
          int y2=a[y][cur[y]]-1;
          if(x>x2)swap(x,x2);
          if(y>y2)swap(y,y2);
          store.push_back({x,x2});
          store.push_back({y,y2});
          //mov+=2;
        }
    }
   while(++ans){
      for(int i=0;i<store.size();i++){
       x=store[i].fi,y=store[i].se;
       if(x>y)swap(x,y);
       ms.insert({x,y});
       if(ms.count({x,y})>1){
          ms.erase({x,y});
          cur[x]++;
          cur[y]++;
          if(cur[x]<n-1){
            int x2=a[x][cur[x]]-1;
            if(x>x2)swap(x,x2);
            store2.push_back({x,x2});
          }
          if(cur[y]<n-1){
            int y2=a[y][cur[y]]-1;
            if(y>y2)swap(y,y2);
            store2.push_back({y,y2});
          }
          //mov+=2;
        }
      }
        //cout<<ms.size()<<" "<<store2.size()<<endl;
        if(ms.empty()&&store2.empty())break;
        else if(!ms.empty()&&store2.empty()){cout<<-1;return;}
        store=store2;
        store2.clear();
   }
   cout<<ans+1;
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    //freopen("BOXES.inp", "r", stdin);
    //freopen("BOXES.out", "w", stdout);

    //int t;cin>>t;while(t--)

    solve();
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int N;
int W[101];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) cin>>W[i];
	int mi=1<<30;
	FOR(i,N) {
		int S=0;
		FOR(j,N) {
			if(j<=i) S+=W[j];
			else S-=W[j];
		}
		mi=min(mi,abs(S));
	}
	cout<<mi<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 256
#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)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} //最小公倍数

//-------------------------------------------------
// 
void yes(){
  cout <<"Yes"<<endl ;
}
void no(){
  cout <<"No"<<endl ;
}


//-------------------------------------------------
// メモ
/*






*/
//-------------------------------------------------


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n ;
  cin >>  n ;
  cout << n/500*1000 + n%500/5*5 <<endl ;
  
  
  
  return 0 ;
}




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

int main(){
  int N,C,K;
  cin >> N >> C >> K;
  vector<int>time(N);
  for(int i=0;i<N;i++)
    cin >> time.at(i);
  sort(time.begin(),time.end());
  int ans=0,hito=0,et=0;
  for(int i=0;i<N;i++){
    if(time.at(i)>et||hito>=C){
      ans++;
      et=time.at(i)+K;
      hito=0;
    }
    hito++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3704
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;

int n;
int v[NMAX][3],dp[NMAX][3];

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i][0]>>v[i][1]>>v[i][2];
    }
    for(int i=1;i<=n;i++)
    {
        dp[i][0]=max(dp[i-1][1],dp[i-1][2])+v[i][0];
        dp[i][1]=max(dp[i-1][0],dp[i-1][2])+v[i][1];
        dp[i][2]=max(dp[i-1][0],dp[i-1][1])+v[i][2];
    }
    cout<<max(dp[n][0],max(dp[n][1],dp[n][2]));
}


Measured memory_bytes: 2560
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
 
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define inf (1<<29)
#define MAX 500 
using namespace std;

bool dp[MAX][MAX][2];
 
int main()
{
  int T;
  while(cin >> T)
    {
      while(T--)
	{
	  rep(i,MAX)rep(j,MAX)rep(l,2)dp[i][j][l] = false;
	  int N;
	  cin >> N;
	  int input[N];
	  rep(i,N) cin >> input[i];
       
	  if(N <= 2)
	    {
	      cout << input[N-1] << endl;
	      continue;
	    }
       
	  int ans = input[N-1];
 
	  REP(cur,1,N-1)
	    {
	      dp[input[cur-1]][input[cur]][cur&1] = true;
	      int R = input[cur+1];
	      rep(L,MAX)
		{
		  rep(M,MAX)
		    {
		      if(!dp[L][M][cur&1])continue;
		      //cout << cur << " cur(" << L << "," << M << "," << R << ")\n";
		      if(M == 0)
			{
			  if(cur+2 < N)
			    {
			      dp[M][R][(cur+1)&1] = true;
			      //cout << cur << " next ( " << M << "," << R << "," << input[cur+2] << ")\n";
			    }
			  else       ans = max(ans,R);
			}
		      else
			{
			  if(cur+2 < N)
			    {
			      dp[M][R][(cur+1)&1] = true;
			      //cout << cur << " next ( " << M << "," << R << "," << input[cur+2] << ")\n";
			      dp[M-1][R+L][(cur+1)&1] = true;
			      //cout << cur << " next ( " << M-1 << "," << R+L << "," << input[cur+2] << ")\n";
			    }
			  else 
			    {
			      ans = max(ans,R+L);
			      //cout << cur << " ans : " << R << "+" << L << "=" << R+L <<  " (" << L << "," << M << "," << R << ")" <<  endl;
			    }
			}
		    }
		}
	      rep(i,MAX)rep(j,MAX)dp[i][j][cur&1] = false;
	    }
	  
	  cout << ans << endl;      
	}
    }
  return 0;
}

Measured memory_bytes: 1644
#include<bits/stdc++.h>
using namespace std;
static const int INF = 1 << 30;
int main(){
	int N;
	int a, b;
	vector<int> v[100000];
	int df[100000], ds[100000];
	scanf("%d", &N);
	for(int i = 0; i < N - 1; i++){
		scanf("%d %d", &a, &b);
		a--, b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for(int i = 0; i < N; i++){
		df[i] = ds[i] = INF;
	}
	queue<int> qf;
	qf.push(0);
	df[0] = 0;
	int u = 0;
	while(!qf.empty()){
		u = qf.front(); qf.pop();
		for(int i = 0; i < v[u].size(); i++){
			if(df[v[u][i]] == INF){
				qf.push(v[u][i]);
				df[v[u][i]] = df[u] + 1;
			}
		}
	}
	queue<int> qs;
	qs.push(N - 1);
	ds[N - 1] = 0;
	u = N - 1;
	while(!qs.empty()){
		u = qs.front(); qs.pop();
		for(int i = 0; i < v[u].size(); i++){
			if(ds[v[u][i]] == INF){
				qs.push(v[u][i]);
				ds[v[u][i]] = ds[u] + 1;
			}
		}
	}
	int F = 0, S = 0;
	for(int i = 0; i < N; i++){
		if(df[i] <= ds[i]) F++;
		else if(df[i] > ds[i]) S++;
	}
	if(F > S) printf("Fennec\n");
	else printf("Snuke\n");
	return 0;
}


Measured memory_bytes: 6656
#include <iostream>
#include <cstdio>

using namespace std;

long long   n,k;

long long  p(long long a) {
    return a*a*a;
}

int main() {
    cin>>n>>k;
    if(k&1) {
        cout<<p(n/k)<<endl;
    } else {
        if(k<=n) {
            cout<<p((n-k/2)/k+1)+p(n/k)<<endl;
        } else if(k/2<=n) {
            cout<<p((n-k/2)/k+1)<<endl;
        } else {
            cout<<0<<endl;
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>

const int N=20,M=7e4+5,mod=1e9+7;
int n,m,lim,fac[M],inv[M],a[N],f[N][M],digit[M],ans;

inline int combn(int n,int m){
	if(n<m) return 0;
	return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
}

int main(){
	scanf("%d%d",&n,&m); lim=1<<n; fac[0]=inv[0]=inv[1]=1;
	for(int i=1;i<=m;++i) scanf("%d",&a[i]);
	for(int i=1;i<=lim;++i) fac[i]=1ll*fac[i-1]*i%mod;
	for(int i=2;i<=lim;++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
	for(int i=1;i<=lim;++i) inv[i]=1ll*inv[i-1]*inv[i]%mod;
	std::sort(a+1,a+1+m); f[m+1][0]=1;
	for(int i=m;i>=1;--i){
		for(int j=0;j<lim;++j){
			f[i][j]=(f[i][j]+f[i+1][j])%mod;
			for(int k=0;k<n;++k) if(!((j>>k)&1)){
				int tmp=1ll*combn(lim-j-a[i],(1<<k)-1)*fac[1<<k]%mod;
				f[i][j+(1<<k)]=(f[i][j+(1<<k)]+1ll*tmp*f[i+1][j])%mod;
			}
		}
	}
	for(int j=0;j<lim;++j){
		digit[j]=digit[j>>1]+(j&1);
		int tmp=1ll*fac[lim-1-j]*f[1][j]%mod;
		if(digit[j]&1) ans=(ans+mod-tmp)%mod;
		else ans=(ans+tmp)%mod;
	}
	printf("%d\n",1ll*ans*lim%mod);
	return 0;
}

Measured memory_bytes: 6144
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;

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

	int ans[3] = {0, 0, 0};
	
	if(str[0] == 'R') ans[0] = 1;
	
	for(int i=1; i < 3; i++){
		if(str[i] == 'S') ans[i] = 0;
		else ans[i] = ans[i-1] + 1;
	}
	
	cout << max(ans[0], max(ans[1], ans[2])) << endl;
}


Measured memory_bytes: 3632
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

int N;

vector<int> adj[200000+5];
vector<int> depth_to_node[2];

int residue_to_num[3][200000+5];
int residue_to_num_sz[3];
int residue_to_num_cur[3];

int node_to_val[200000+5];

void dfs(int here, int parent, int depth)
{
	depth_to_node[depth&1].push_back(here);
	for(auto &next : adj[here])
	{
		if(next == parent) continue;
		dfs(next, here, depth+1);
	}
}

void build(vector<int> &less, vector<int> &more)
{
	for(auto &node : less)
	{
		node_to_val[node] = residue_to_num[0][residue_to_num_cur[0]++];
	}
	for(auto &node : more)
	{
		for(int i=0;i<3;++i)
		{
			if(residue_to_num_cur[i] < residue_to_num_sz[i])
			{
				node_to_val[node] = residue_to_num[i][residue_to_num_cur[i]++];
				break;
			}
		}
	}
}

int main()
{
	scanf("%d", &N);
	for(int i=0;i<N-1;++i)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		adj[u].push_back(v);
		adj[v].push_back(u);
		residue_to_num[(i+1)%3][residue_to_num_sz[(i+1)%3]++] = i+1;
	}
	residue_to_num[N%3][residue_to_num_sz[N%3]++] = N;
	
	dfs(1, 0, 0);
	if(min(depth_to_node[0].size(), depth_to_node[1].size()) <= residue_to_num_sz[0])
	{
		if(depth_to_node[0].size() < depth_to_node[1].size()) build(depth_to_node[0], depth_to_node[1]);
		else build(depth_to_node[1], depth_to_node[0]);
	}
	else
	{
		for(int i = 0;i<2;++i)
		{
			for(auto &node : depth_to_node[i])
			{
				if(residue_to_num_cur[i+1] < residue_to_num_sz[i+1])
				{
					node_to_val[node] = residue_to_num[i+1][residue_to_num_cur[i+1]++];
				}
				else node_to_val[node] = residue_to_num[0][residue_to_num_cur[0]++];
			}
		}
	}

	for(int i=1;i<=N;++i) printf("%d ", node_to_val[i]);
	printf("\n");

	return 0;
}

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

int main(){
  int n;
  cin >> n;
  vector<int> ans(n);
  rep(i,n){
    int a;
    cin >> a;
    ans.at(a-1) = i+1;
  }
  rep(i,n){
    cout << ans.at(i);
    if( i != n-1 ) cout << " "; else cout << endl;
  }
  return 0;
}


Measured memory_bytes: 1152
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
using namespace std;
int N,L;
int main(){
    cin >> N >> L;
    string s[N];
    for(int i=0;i<N;i++)cin >> s[i];
    sort(s,s+N);
    for(int i=0;i<N;i++)cout << s[i];
    cout << "\n";
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n,k;
  cin>>n>>k;
  int h[n];
  for(int i=0;i<n;++i){
    cin>>h[i];
  }
  long long int d[n];
  d[n-1]=0;
  for(int i=n-2;i>=0;--i){
    d[i]=(long long int)1e16;
    for(int j=1;j<=k&&i+j<n;++j){
      d[i]=min(d[i], d[i+j] + abs(h[i]-h[i+j]));
    }
  }
  cout << d[0] << "\n";
}


Measured memory_bytes: 1408
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    int g = a[0];
    int MAX = a[max_element(all(a)) - a.begin()];
    rep(i, 1, n) g = __gcd(g, a[i]);
    cout << ((k % g || k > MAX) ? "IMPOSSIBLE" : "POSSIBLE") << endl;
}

Measured memory_bytes: 1024
#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const int SIZE = 1 << 17;
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 }, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long>dij;
struct edge { long long to, cost; };
vector<vector<edge>>G;

void COMinit() {
	fac[0] = 1, fac[1] = 1, finv[0] = 1, finv[1] = 1, inv[1] = 1;
	for (int i = 2; i < 200005; ++i) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

//calc
long long COM(long long n, long long k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long ans;
int main() {
	COMinit();
	long long a, b, c, d;
	cin >> a >> b >> c >> d;
	for (int i = 0; i < a - c; ++i) { ans += COM(d + i - 1, i) % MOD*COM(a + b - d - i - 2, a - i - 1) % MOD; }
	cout << ans % MOD << endl;
}

Measured memory_bytes: 4992
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <numeric>
#include <bitset>

using namespace std;
using ll = long long;
using ld = long double;
const ll mod =  1000000007;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REP_FROM(i, j, n) for (ll i = (j); i < (n); ++i)
#define all(x) (x).begin(),(x).end()

ll power(ll base, ll exponent){ if(exponent % 2){ return power(base, exponent - 1) * base % mod; }else if(exponent){ ll root_ans = power(base, exponent / 2); return root_ans * root_ans % mod; }else{ return 1; }}
ll inverse(ll x){ return power(x, mod - 2);}
ll gcd(ll a, ll b) {if(a < b) gcd(b, a);ll r;while(r=a%b) {a=b;b=r;}return b;}
struct combination{ vector<ll> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } ll C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };

int bsearch(const vector<ll>& a, ll x){
  int lft = 0;
  int rgt = a.size();
  while (rgt - lft > 1) {
    int mid = (lft + rgt) / 2;
    if(a[mid] < x) {
      lft = mid;
    }
    else {
      rgt = mid;
    }
  }
  return lft;
}
vector<ll> cnt;
vector<vector<ll>> g;
vector<bool> used;
int V = 100000 + 10;
void dfs(int v) {
  if(used[v]) return;
  used[v] = true;
  cnt[v/V]++;
  for(const auto& next: g[v]) dfs(next);
}

signed main() {
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  g = vector<vector<ll>>(V*2);
  used = vector<bool>(V*2, false);
  REP(i, n) {
    int x, y;
    cin >> x >> y;
    x--;y--;
    y += V;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  ll ans = 0;
  REP(i, V) {
    if(used[i]) continue;
    cnt = vector<ll>(2);
    dfs(i);
    ans += cnt[0] * cnt[1];
  }
  cout << ans - n << endl;
  return 0;
}


Measured memory_bytes: 10112
using System;

namespace CGL_3_C_Polygon_Point_Containment
{
	class Program
	{
		static void Main ( string[] args )
		{
			int inputCount = int.Parse (Console.ReadLine ());
			Vector2[] polygon = new Vector2[inputCount];

			for (int lp = 0; lp < inputCount; lp++)
			{
				int[] input = Array.ConvertAll (Console.ReadLine ().Split (' '), int.Parse);
				polygon[lp] = new Vector2 (input[0], input[1]);
			}

			inputCount = int.Parse (Console.ReadLine ());

			for (int lp = 0; lp < inputCount; lp++)
			{
				int[] input = Array.ConvertAll (Console.ReadLine ().Split (' '), int.Parse);
				Vector2 point = new Vector2 (input[0], input[1]);

				var result = Vector2.Contains (polygon, point);

				Console.WriteLine (result);
			}

		}
	}

	class Vector2
	{
		public Vector2 ( decimal x, decimal y )
		{
			this.X = x;
			this.Y = y;
		}

		public decimal X { private set; get; }
		public decimal Y { private set; get; }

		public static Vector2 operator - ( Vector2 ob1, Vector2 ob2 )
		{
			return new Vector2 (ob1.X - ob2.X, ob1.Y - ob2.Y);
		}
		public static decimal DotProduct ( Vector2 line1, Vector2 line2 )
		{
			return line1.X * line2.X + line1.Y * line2.Y;
		}
		public static decimal CrossProduct ( Vector2 line1, Vector2 line2 )
		{
			return line1.X * line2.Y - line1.Y * line2.X;
		}

		public const int Contain = 2;
		public const int On = 1;
		public const int Other = 0;

		public static int Contains ( Vector2[] polygon, Vector2 point )
		{
			int length = polygon.Length;
			bool isContain = false;

			for (int lp = 0; lp < length; lp++)
			{
				Vector2 a = polygon[lp] - point;
				Vector2 b = polygon[(lp + 1) % length] - point;

				if (Math.Abs (Vector2.CrossProduct (a, b)) == 0m &&
					Vector2.DotProduct (a, b) <= 0m)
				{
					return On;
				}

				if (a.Y > b.Y)
				{
					var tmp = new Vector2 (a.X, a.Y);
					a = new Vector2 (b.X, b.Y);
					b = new Vector2 (tmp.X, tmp.Y);
				}

				if (a.Y < 0m && 0m <= b.Y &&
					0m < Vector2.CrossProduct (a, b))
				{
					isContain = !isContain;
				}
			}

			return isContain ? Contain : Other;
		}
	}
}



Measured memory_bytes: 9404
#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#define rep(i,n) for(int long long i=1;i<n;i++)
using namespace std;
char t[7];
int main(){
    rep(i,10){
        rep(j,10){
            cout<<i<<'x'<<j<<'='<<i*j<<endl;
        }
    }
    return 0;
}



Measured memory_bytes: 3084