akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2005,mod=1e9+7;
int n,k,a[N];
ll res,ans;
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    res=(ll)k*(k-1)/2%mod;
    for(int i=1;i<=n;i++)
    {
        int t=0;
        for(int j=1;j<=n;j++)
            if(a[i]>a[j]) t++;
        ans=(ans+res*t)%mod;
    }
    for(int i=1;i<=n;i++)
    {
        int t=0;
        for(int j=i+1;j<=n;j++)
            if(a[i]>a[j]) t++;
        ans=(ans+(ll)t*k)%mod;
    }
    printf("%lld\n",ans);
}


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

typedef long long ll;
#define REP(i, n, m) for(int i = n; i < m; i++)
#define REPR(i, n, m) for(int i = n; i >= m; i--)
#define LREP(i, n, m) for(ll i = n; i < m; i++)
#define LREPR(i, n, m) for(ll i = n; i >= m; i--)

#define P pair<ll, ll >


const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;



//---functions---//
int gcd(int a, int b){
    if(a < b) swap(a, b);
    return b?gcd(b, a%b):a;
}

int s2i(string num){stringstream ss; ss << num << flush; int n; ss >> n; return n;}
string i2s(int n){stringstream ss; ss << n << flush; return ss.str();}

bool firstCompare(const P& firstElof, const P& secondElof){
    return firstElof.first < secondElof.first;
}
bool secondCompare(const P& firstElof, const P& secondElof){
    return firstElof.second < secondElof.second;
}
//---------------//

int main(){
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    ll N = S.size();
    vector<P > v;
    pair<ll, ll > p;
    LREP(i, 0, N - 1){
        if((S[i] == 'R') && (S[i+1] == 'L')){
            p.first = i + 1;
        }if((S[i] == 'L') && (S[i+1] == 'R')){
            p.second = i;
            v.push_back(p);
        }if(i == N - 2){
            p.second = N - 1;
            v.push_back(p);
        }
    }
    ll start = 0;
    ll ans[N];
    LREP(i, 0, N) ans[i] = 0;
    LREP(i, 0, v.size()){
       //cout << "v.first = " << v[i].first << " v.second = " << v[i].second << '\n';
        LREP(j, start, v[i].second + 1){
            if(j % 2 == v[i].first % 2){
                ans[v[i].first] += 1;
            }else{
                ans[v[i].first-1] += 1;
            }
        }
        start = v[i].second + 1;
    }

    LREP(i, 0, N) cout << ans[i] << ' ';
    cout << '\n';

}


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

using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)
#define reep(i,a,b) for(int i=a; i<b; i++)
#define INF 0x3f3f3f3f


using vint = vector<int>;

bool used[55][55][55][55][2][2];
int dp[55][55][55][55][2][2];
vint A,B;
int n,m;

int dfs(int a, int b, int c, int d, int turn, int pass){
	if(used[a][b][c][d][turn][pass]) return dp[a][b][c][d][turn][pass];
	used[a][b][c][d][turn][pass] = true;
	int &ret = dp[a][b][c][d][turn][pass];
	if(turn == 0){
		ret = -INF;
		if(a<n){
			if(A[a]==-1){
				ret = max(ret, dfs(a+1, b, c, b, 1, 0));
			}
			else{
				ret = max(ret, dfs(a+1, b, c, d, 1, 0));
			}
		}
		if(pass==1&&a==c&&b==d){
			ret = max(ret, 0);
		}
		else if(pass==0&&a==c&&b==d){
			ret = max(ret, dfs(a,b,c,d,1,1));
		}
		else{
			int sum = 0;
			reep(i,c,a){
				if(A[i]>0) sum += A[i];
			}
			reep(i,d,b){
				if(B[i]>0) sum -= B[i];
			}
			ret = max(ret, dfs(a,b,a,b,1,0)+sum);
		}
	}
	else{
		ret = INF;
		if(b<m){
			if(B[b]==-1){
				ret = min(ret, dfs(a, b+1, a, d, 0, 0));
			}
			else{
				ret = min(ret, dfs(a, b+1, c, d, 0, 0));
			}
		}
		if(pass==1&&a==c&&b==d){
			ret = min(ret, 0);
		}
		else if(pass==0&&a==c&&b==d){
			ret = min(ret, dfs(a,b,c,d,0,1));
		}
		else{
			int sum = 0;
			reep(i,c,a){
				if(A[i]>0) sum += A[i];
			}
			reep(i,d,b){
				if(B[i]>0) sum -= B[i];
			}
			ret = min(ret, dfs(a,b,a,b,0,0)+sum);
		}
	}
	// cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<turn<<" "<<pass<<" "<<endl<<ret<<endl;
	return ret;
}

int main(){
	cin>>n>>m;
	A = vint(n);
	rep(i,n) cin>>A[i];
	B = vint(m);
	rep(i,m) cin>>B[i];
	cout<<dfs(0,0,0,0,0,0)<<endl;
}

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

int main() {
    string s;
    cin >> s;
    int ans = 1e9;
    rep(i,s.size()-2) {
        int x = (s[i]-'0')*100 + (s[i+1]-'0')*10 + (s[i+2]-'0');
        ans = min(ans,abs(x-753));
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
N = int(input())

sp = []
for i in range(N):
    sp.append(list(input().split() + [i+1]))
    sp[-1][1] = int(sp[-1][1])

sp.sort(key=lambda x:x[1], reverse=True)
sp.sort(key=lambda x:x[0])


for i in range(N):
    print(sp[i][2])

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

#define FOR(i, a, b) for(long long int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;



int main(void) {
	int prime[100000] = {};
	int pos = 2;
	prime[1] = 2;
	FOR(i, 3, 1000000) {
		FOR(t, 2, sqrt(i)) {
			if (i%t == 0) {
				break;
			}
			if (t + 1 > sqrt(i)) {
				prime[pos] = i;
				pos++;
			}
		}
	}
	
	while (1) {
		int a, d, n;
		int now = 1;
		bool flag = true;

		cin >> a >> d >> n;

		if (a == 0) {
			break;
		}
		for (int i = a; i <= 1000000; i += d) {
			FOR(t, now, pos-1) {
				if (i == prime[t]) {
					if (n == 1) {
						cout << i << endl;
						now = t;
						flag = false;
						break;
					}
					else {
						n--;
						now = t;
					}
				}
				if (prime[t] > i) {
					now = t - 1;
					break;
				}
			}
			if (flag == false) {
				break;
			}
		}
	}

}


Measured memory_bytes: 3484
#include<iostream>
using namespace std;
	int main(){
	int b=0;	
	char a[4];
	cin >> a;
	for(int i=0;i<4;i++)
	{
	if (a[i]== '+')b++;
	if (a[i]== '-')b--;
	}
	cout << b << endl;
	}

Measured memory_bytes: 256
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}

const int N = 20 , P = 1e9 + 7;
inline void pp(int &x,int d){if((x+=d)>=P)x-=P;}
int n , m , K , f[1 << N] , mask[N] , need[N];
int go[1 << N][N];

int main(){
    cin >> n >> m >> K;
    --n;
    rep(i,0,K){
        int a,b,c;
        cin >> a >> b >> c;
        --a;--b;
        mask[a] |= 1 << b;
        need[a] |= c << b;
    }
    rep(i,0,1<<n) {
        int top = -1;
        per(j,0,n) {
            if(i >> j & 1) top = j;
            else {
                if(top == -1) go[i][j] = i ^ (1 << j);
                else go[i][j] = i ^ (1 << j) ^ (1 << top);
            }
        }
    }
    f[0] = 1;
    rep(i,0,m) {
        rep(j,0,n) rep(k,0,1<<n) if(f[k] && (~k>>j&1)){
            pp(f[go[k][j]] , f[k]);
        }
        rep(k,0,1<<n) if(need[i] != (mask[i] & k))
            f[k] = 0;
    }
    int ans = 0;
    rep(i,0,1<<n) pp(ans , f[i]);
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 47360
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long a=1,b=0,c,n;
string s;
cin>>s;
if(s=="SSS")
{cout<<0<<endl;}else if(s=="RRR")
{
	cout<<3<<endl;
}
else if((s[0]=='R'&&s[1]=='R')||(s[1]=='R'&&s[2]=='R'))
{
	cout<<2<<endl;
}

else
{
	cout<<1<<endl;
	
}

}

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

int main(){
	int D[5];
	for(int i = 0;i < 5;i++){
		cin >> D[i];
	}
	int S[5] = {};
	for(int i = 0;i < 5;i++){
		S[i] = D[i]%10;
	}
	int t;
	for(int j = 0;j < 5;j++){
		for(int k = j+1;k < 5;k++){
			if(S[j] > S[k]){
				t = S[j];S[j] = S[k];S[k] = t;
			}
		}
	}
	int m;
	for(int i = 0;i < 5;i++){
		if(S[i] != 0){
			m = S[i];
			break;
		}
	} 
	int ex;
	for(int i = 0;i < 5;i++){
		if(D[i]%10 == m){
			ex = D[i];
			D[i] = 0;
			break;
		}
	}
	int ans = 0;
	for(int i = 0;i < 5;i++){
		ans += (D[i]%10 != 0)?10*((D[i]/10)+1):D[i];
	}
	ans += ex;
	cout << ans;
	return 0;
}


Measured memory_bytes: 256
public class Main {
    private static java.util.Scanner scanner = new java.util.Scanner(System.in);

    public static void main(String[] args) {
	    long n = scanner.nextLong(), ans = String.valueOf(n).length();
	    for (long i = 1; i * i <= n; i++)
            if (n % i == 0)
                ans = Math.min(ans, Math.max(String.valueOf(i).length(), String.valueOf(n / i).length()));
        System.out.println(ans);
    }
}

Measured memory_bytes: 21716
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define eb emplace_back
#define precision(n) cout << fixed << setprecision(n);
#define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);}
int main()
{
    fast;
    int x;
    cin>>x;
    if(x>=400&&x<=599)
    {
        cout<<8<<endl;
    }
    else if(x>=600&&x<=799)
    {
        cout<<7<<endl;
    }
    else if(x>=800&&x<=999)
    {
        cout<<6<<endl;
    }
    else if(x>=1000&&x<=1199)
    {
        cout<<5<<endl;
    }
    else if(x>=1200&&x<=1399)
    {
        cout<<4<<endl;
    }
    else if(x>=1400&&x<=1599)
    {
        cout<<3<<endl;
    }
    else if(x>=1600&&x<=1799)
    {
        cout<<2<<endl;
    }
    else
    {
        cout<<1<<endl;
    }
    
    return 0;
}




Measured memory_bytes: 3648
#include<iostream>
#include<string>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main()
{
    string n;
    cin>>n;
    char a=n[n.size()-1];
    if(a=='3')
    {
        cout<<"bon"<<endl;
    }
    else if(a=='0' || a=='1' || a=='6' || a=='8')
    {
        cout<<"pon"<<endl;
    }
    else
    {
        cout<<"hon"<<endl;
    }
    
    

    return 0;
}

Measured memory_bytes: 3624
#include <iostream>

using namespace std;

int main() {
    char X, Y;
    int x, y;

    cin >> X >> Y;

    x = (int)(X);
    y = (int)(Y);

    if(x > y) {
        cout << ">" << endl;
    } else if(x < y) {
        cout << "<" << endl;
    } else {
        cout << "=" << endl;
    }

}

Measured memory_bytes: 256
import sys
from collections import deque

input = sys.stdin.readline


n = int(input())
a = input().split()

b = deque([])
for i in range(n):
    if i % 2 == 0:
        b.append(a[i])
    else:
        b.appendleft(a[i])

if n % 2 == 1:
    b = list(b)[::-1]
print(' '.join(b))


Measured memory_bytes: 28112
import java.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n;
        n = input.nextInt();
        int[] a = new int[n];
        for (int i = n-1; i > -1; i--){
            a[i] = input.nextInt();
        }
        for (int i = 0; i < n-1; i++){
            System.out.printf("%d ",a[i]);
        }
        System.out.println(a[n-1]);
    }
}


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

#define dump(...) do{print_vars(cout<<"# "<<#__VA_ARGS__<<'=',__VA_ARGS__);cout<<endl;}while(0)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

template<typename T,typename U>bool chmin(T& a,const U& b){return a>b?(a=b,1):0;}
template<typename T,typename U>bool chmax(T& a,const U& b){return a<b?(a=b,1):0;}

void print_vars(ostream&){}
template<typename Car,typename... Cdr>
void print_vars(ostream& os,const Car& car,const Cdr&... cdr){
	print_vars(os<<car<<(sizeof...(cdr)?",":""),cdr...);
}

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<int I,typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<int I,typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	os<<get<I>(t)<<(sizeof...(Cdr)?",":"");
	print_tuple<I+1,Cdr...>(os,t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<0,Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

int main()
{
	#ifndef _GLIBCXX_DEBUG
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	constexpr char endl='\n';
	#endif

	for(int n,k;cin>>n>>k&&n|k;){
		vi hs(n);
		rep(i,n) cin>>hs[i];
		sort(all(hs));
		int res=INF;
		repi(i,k-1,n)
			chmin(res,hs[i]-hs[i-(k-1)]);
		cout<<res<<endl;
	}
}


Measured memory_bytes: 640
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;


unsigned euclidean_gcd(unsigned a, unsigned b) {
    if (a < b) return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

class UnionFind {
public:
    vector <ll> par;
    vector <ll> siz;
    UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i) par[i] = i;
    }
    ll root(ll x) {
        while (par[x] != x) {
            x = par[x] = par[par[x]];
        }
        return x;
    }
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) {
        return root(x) == root(y);
    }

    ll size(ll x) {
        return siz[root(x)];
    }
};

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

void dfs(vector<ll> &ki,vector<vector<ll>> &z,ll now, vector<ll> &ans, vector<ll> &seen) {
    seen[now] = 1;
    for (int i = 0; i < z[now].size(); i++) {
        if (seen[z[now][i]] == 0) {
            dfs(ki, z, z[now][i], ans, seen);
            ki[now] += ki[z[now][i]];
            ans[now] = max(ans[now], ki[z[now][i]]);
        }
    }
    ki[now]++;
    ll p = ki.size();
    if (now != 0)ans[now] = max(ans[now],p - ki[now]);
    return;
}

int main() {
    ll h, w, n;
    cin >> h >> w >> n;
    vector<vector<ll>> z(h, vector<ll>(w));
    ll  now = 0;
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        for (int j = 0; j < a; j++) {
            z[now / w][now % w] = i + 1;
            now++;
        }
    }
    for (int i = 0; i < h; i++) {
        if (i % 2 == 1)reverse(z[i].begin(), z[i].end());
        for (int j = 0; j < w; j++) {
            cout << z[i][j];
            if (j == w - 1)break;
            cout << " ";
        }
        cout << endl;
    }
}

Measured memory_bytes: 512
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main(){
	while(true){
		int H,W;
		cin >> H >> W;
		if(H==0 && W == 0) break;
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				if(i % 2 == 0){
					if(j % 2 == 0) cout << "#";
					else cout << ".";
				}else{
					if(j % 2 == 0) cout << ".";
					else cout << "#";
				}
			}
			cout << endl;
		}
		
		cout << endl;
	}
}





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

int main() {
	int N,K,X,Y;
	cin >>N>>K>>X>>Y;
	int sum = 0;
	for (int i=1; i<=N; i++){
		if(i<=K) sum+=X;
		else sum+=Y;
	}
	cout << sum << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>

using namespace std;

int main(){
 string S,T;
  cin >> S;
  cin >> T;
  T.resize(T.size()-1);
  if(S==T)
    cout << "Yes" <<endl;
  else
    cout << "No" <<endl;
  
  return 0;
}


Measured memory_bytes: 3632
n = int(input())
a  = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
if n % 2 == 0:
  ans += sum(a[1:n // 2]) * 2
else:
  ans += sum(a[1:n//2]) * 2 + a[n // 2]
print(ans)

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

using namespace std;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RFOR(i,a,b) for(int i=(int)b-1;i>=(int)a;--i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "Yes" : "No") << endl;
typedef long long ll;

void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail) {
  cin >> head; CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;

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

  CIN(N);

  if (N == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector< int > ans;
  while (N != 0) {
    if (N % 2 == 0) {
      ans.emplace_back(0);
    } else {
      N--;
      ans.emplace_back(1);
    }
    N /= -2;
  }
  
  reverse(ALL(ans));
  REP(si, ans.size()) {
    cout << ans[si];
  }
  cout << endl;

  return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define frst first
#define sec second
#define fast cin.tie(0) , ios_base :: sync_with_stdio(0)
#define db double
#define mod 1000000007
void online_judge()
{
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif
}

vector<ll> pyr;
void cal()
{
	for (ll h = 1; h < 100001; ++h)
	{
		ll val = (3 * h * h + h) / 2;
		pyr.pb((val));
	}
}


void go() {
	fast;
	ll n , w;
	cin >> n >> w;
	ll wt[n] , p[n];
	ll sum = 0;
	for (int i = 0; i < n; ++i) {
		cin >> wt[i] >> p[i];
		sum += p[i];
	}
	ll dp[n + 1][sum + 1];
	for (int  i = 0; i < n + 1; ++i)
	{
		for (int j = 0; j < sum + 1; ++j)
		{
			dp[i][j] = 1e18;
		}
	}
	ll ans = 0;
	for (int i = 0; i < n + 1; ++i)dp[i][0] = 0;
	for (int i = 1; i < n + 1; ++i)
	{
		for (ll int j = 1; j <= sum; ++j)
		{
			dp[i][j] = min(dp[i - 1][j] ,
			               ((j >= p[i - 1]) ? dp[i - 1][j - p[i - 1]] + wt[i - 1] : (ll)1e18));
			if (dp[i][j] <= w) {
				//cout << dp[i][j] << endl;
				ans = max(ans , (ll)j);
			}
		}
	}
	cout << ans;
}


int main() {
	fast;
	//online_judge();
	//cout << 2332;
	go();
}

Measured memory_bytes: 79232
#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map> 
#include <unordered_set>
#include <functional>

using namespace std;

typedef pair<long long int, long long int> P;
typedef tuple<int, int, int> T;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

bool DP[110000];

int main(){
	DP[0] = true;
	for(int i = 0; i <= 100000; i++){
		if(DP[i]){
			DP[i + 100] = true;
			DP[i + 101] = true;
			DP[i + 102] = true;
			DP[i + 103] = true;
			DP[i + 104] = true;
			DP[i + 105] = true;
		}
	}
	int X;
	cin >> X;
	cout << DP[X] << endl;
	return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005

int N, M, D;
int d[MAXN];
char m[] = { 'R', 'Y', 'G', 'B' };

int main(){
	scanf( "%d%d%d", &N, &M, &D );
	for ( int i = D, I(max( N << 1, M << 1 )); i <= I; ++i ) d[i] = d[i - D] + 1;
	for ( int i = 1; i <= N; ++i, putchar('\n') )
		for ( int j = 1; j <= M; ++j )
			putchar( m[( d[i + j] & 1 ) << 1 | ( d[i - j + M] & 1 )] );
	return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int rank(char c){
	if(isdigit(c)) return c - '0';
	if(c == 'T') return 10;
	if(c == 'J') return 11;
	if(c == 'Q') return 12;
	if(c == 'K') return 13;
	return 14;
}
bool win(char t, char st, char *a, char *b){
	if((a[1] == t) != (b[1] == t)) return a[1] == t;
	if((a[1] == st) != (b[1] == st)) return a[1] == st;
	return rank(a[0]) > rank(b[0]);
}

int main(){
	char t;
	while(cin >> t, t != '#'){
		char in[4][13][3];
		rep(i, 4) rep(j, 13) cin >> in[i][j];
		
		int sc[2] = {}, w = 0;
		rep(i, 13){
			char st = in[w][i][1];
			rep(j, 4) if(win(t, st, in[j][i], in[w][i])) w = j;
			sc[w % 2]++;
		}
		if(sc[0] > sc[1]) cout << "NS " << sc[0] - 6 << endl;
		else cout << "EW " << sc[1] - 6 << endl;
	}
	return 0;
}

Measured memory_bytes: 1144
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); i++)
int main()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int e, f;
    if(a > c) {
        e = a;
    }
    else {
        e = c;
    }

    if(b > d) {
        f = d;
    }
    else{
        f = b;
    }

    
    int ans = f - e;
    if(ans < 0){
        ans = 0;
    }
    cout << ans;

    return 0;
}

Measured memory_bytes: 256
// 20:20 -
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <stack>

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

using namespace std;

int main(){
  int a, b, c, x;
  int cnt = 0;
  cin >> a >> b >> c >> x;
  for(int i = 0; i <= a; i++){
    for(int k = 0; k <= b; k++){
      for(int n = 0; n <= c; n++){
        if(500 * i + 100 * k + 50 * n == x) cnt++;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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 long long INF = 1LL<<60;

int main() {
    // input
    int N; cin >> N;
    vector<pair<int, int>> p(N);
    for (int i = 0; i < N; i++) cin >> p[i].second >> p[i].first;
    
    sort(p.begin(), p.end());

    // dp table
    vector<vector<long long>> dp(N+1, vector<long long>(N+1, INF));

    // initial condition
    dp[0][0] = 0;

    // DP loop
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= N; j++) {

            // not solve ith promlem
            chmin(dp[i+1][j], dp[i][j]);

            // solve ith promblem
            if (dp[i][j] + p[i].second <= p[i].first) chmin(dp[i+1][j+1], dp[i][j] + p[i].second);

        }
    }

    // output answer
    int ans = 0;
    for (int j = N; j >= 0; j--) {
        if (dp[N][j] != INF) {
            ans = j;
            break;
        }
    }
    cout << ans << endl;

    return 0;

}


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

int main() {
long long N;
cin >> N;
long long ans = 0; 
for (int i = 1; i <= N; i++) {
if(i % 3 != 0 && i % 5 != 0) ans += i;
}
cout << ans << endl;
return 0;
}

Measured memory_bytes: 3592
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BResale solver = new BResale();
        solver.solve(1, in, out);
        out.close();
    }

    static class BResale {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();

            int[] c = new int[n];
            int[] v = new int[n];

            for (int i = 0; i < n; i++) {
                v[i] = in.nextInt();
            }
            for (int i = 0; i < n; i++) {
                c[i] = in.nextInt();
            }

            int answer = 0;
            for (int i = 0; i < n; i++) {
                answer += Math.max(0, v[i] - c[i]);
            }
            out.println(answer);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }
}



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

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PI;

int main() {
	int n, a;
	cin >> n >> a;
	if (n % 500 <= a)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	return 0;
}

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

using namespace std;

inline int toInt(string s) {int 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;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#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 range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

int nexts[1000010];

int main(void){
	for(int i=10;i<=1000000;++i){
		nexts[i]=0;
		string in=to_string(i);
		int n=in.size();
		rep(j,n-1){
			int a=stoi(in.substr(0,j+1));
			int b=stoi(in.substr(j+1,n-j-1));
			nexts[i]=max(nexts[i],a*b);
		}
	}
	int q;
	cin >> q;
	rep(loop,q){
		int cur;
		cin >> cur;
		int ans=0;
		while(cur>=10){
			ans++;
			cur=nexts[cur];
		}
		cout << ans << endl;
	}
	return 0;
}

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

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

using namespace std;
using P = pair<int, int>;

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( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main(){
    int N, K; cin >> N >> K;
    K = ( N - 2 ) * ( N - 1 ) / 2 - K;
    if( K < 0 ){
        cout << -1 << endl;
    }else{
        vector<P> ans;
        FOR( i, 2, N + 1 ){
            ans.emplace_back( 1, i );
        }


        FOR( i, 2, N  ){
            FOR( j, i + 1, N + 1 ){
                if( K > 0 ) ans.emplace_back( i, j ), K--;
            }
        }

        cout << ans.size() << endl;
        for( auto p : ans ) cout << p.first << " " << p.second << endl;

    }
}


Measured memory_bytes: 512
n=int(input())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
t=0

for i in range(n):
  if a[i]>b[i]:
    t+=b[i]
  else:
    t+=a[i]
    if a[i+1]<b[i]-a[i]:
      t+=a[i+1]
      a[i+1]=0
    else:
      t+=b[i]-a[i]
      a[i+1]-=b[i]-a[i]

print(t)


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


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.flush();
	ll t=1;
	//cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		ll ans=true;
		ll n;
		n=s.size();
		ll p=0;
		for(ll i=0;i<n;i++)
		{
			if(p==0 && s[i]=='h')
			{
				p=1;
				}
			else if(p==1 && s[i]=='i')
			{
				p=0;
				}
			else
			{
				ans=false;
				break;
				}
			}
		if(p!=0)
		{
			ans=false;
			}
		
		if(ans)
		{
			cout<<"Yes"<<endl;
			}
		else
		{
			cout<<"No"<<endl;
			}
		
	}
	
return 0;
}


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

int main(){
	int N, K, i;
	scanf("%d", &N);
	string S;
	cin >> S;
	scanf("%d", &K);
	for(i = 0; i < N; i++){
		if(S[i] == S[K - 1]){
			printf("%c", S[i]);
		}
		else{
			printf("*");
		}
	}
	printf("\n");
	return 0;
}

Measured memory_bytes: 256
using System;
class MainClass
{
    public static void Main(string[] args)
    {
        string[] input = Console.ReadLine().Split(' ');
        int a = int.Parse(input[0]);
        int b = int.Parse(input[1]);
        string ans = "Yes";
        if(a % 2 == 0  || b % 2 ==0)
        {
            ans = "No";
        }
        Console.WriteLine(ans);
    }
}


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

const int white = 0;
const int gray = 1;
const int black = 2;
const int Max_n = 100;
const int Infty = (1 << 21);

int M[Max_n][Max_n];
int n;

int prim(){
    int u, minv;
    int color[Max_n], d[Max_n], p[Max_n];
    for(int i=0; i<n; i++){
        color[i] = white;
        d[i] = Infty;
        p[i] = -1;
    }

    d[0] = 0;

    while(1){
        minv = Infty;
        u = -1;
        for(int i=0; i<n; i++){
            if(color[i] != black && d[i] < minv){
                minv = d[i];
                u = i;
            }
        }
        if(u == -1){
            break;
        }

        color[u] = black;

        for(int v=0; v<n; v++){
            if(color[v] != black && M[u][v] != Infty){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = gray;
                }
            }
        }
    }

    int sum = 0;
    for(int i=0; i<n; i++){
        if(p[i] != -1) sum += M[i][p[i]];
    }
    return sum;
}

int main(){
    cin >> n;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            int e; cin >> e;
            M[i][j] = (e == -1) ? Infty : e;
        }
    }

    cout << prim() << endl;


    return 0;
}


Measured memory_bytes: 3132
import std.stdio;
import std.string;

void main() {
  string s = readln.chomp;
  if (s == "SUN") writeln(7);
  if (s == "MON") writeln(6);
  if (s == "TUE") writeln(5);
  if (s == "WED") writeln(4);
  if (s == "THU") writeln(3);
  if (s == "FRI") writeln(2);
  if (s == "SAT") writeln(1);
}


Measured memory_bytes: 256
def a(q)
puts"? "+q.join(" ")
STDOUT.flush
gets
end
N=gets.to_i
s="! "
I=(0..2*N).to_a
f=a I[1..N]
l,r=1,N+1
9.times{
m=(l+r)/2
q=I[m,N]
f==a(q)?l=m:r=m
}
x=I[r,N-1]
y=I-[0]-I[l,N+1]
1.upto(2*N){|i|
s+=a([i]+(x.index(i)?y:x))[0]
}
puts s

Measured memory_bytes: 2256
#include<cstdio>
int main(){
int W,H,x,y,r;
scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
if((x-r<0)||(x+r>W)||(y-r<0)||(y+r>H)){
	printf("No\n");
		}else{
	printf("Yes\n");
	}
return 0;
}

Measured memory_bytes: 1012
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long int

int memo[100001];

int rev(int k){
  if(memo[k] != -1) return memo[k];
  if(k==1) return 1;
  if(k==0) return 1;
  return memo[k] = (rev(k-1)+rev(k-2))%1000000007;
}

int main() {
  // initialize memo
  for(int i=0; i<100001; i++) {
    memo[i] = -1;
  }

  int n, m, k;
  std::cin >> n >> m;
  for(int i = 0; i < m; i++) {
    std::cin >> k;
    memo[k] = 0; // kashikoi
  }
  std::cout << rev(n) << std::endl;

}


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

#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = 1; i <= n; i++)
#define drep(i, n) for(int i = n-1; i >= 0; i--)
#define srep(i, s, t) for (int i = s; i < t; i++)
#define vi vector<int>
#define P pair<ll, ll>
using namespace std;
int mod = 1'000'000'000 + 7;
struct edge {
    int to;
    ll cost;
};

void COME_LET_THE_GAMES_BEGIN() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(20);
}

int n, v[100000];
map<int, int> m[2];
vector<pair<int, int>> c[2];

int main() {
    COME_LET_THE_GAMES_BEGIN();
    cin >> n;
    rep(i, n) {
        cin >> v[i];
        m[i % 2][v[i]]++;
    }
    for (int i = 0; i < 2; i++) {
        for (auto x:m[i]) {
            c[i].push_back(P(x.first, x.second));
        }
        sort(c[i].begin(), c[i].end(), [](auto &x, auto &y) {
            return x.second > y.second;
        });
    }
    int ans = n;
    for (int i = 0; i < 100 && i < c[0].size(); i++) {
        for (int j = 0; j < 100 && j < c[1].size(); j++) {
            auto x = c[0][i], y = c[1][j];
            ans = min(ans, (n / 2 - x.second) + n / 2);
            ans = min(ans, (n / 2 - y.second) + n / 2);
            if (x.first == y.first)continue;
            ans = min(ans, (n / 2 - x.second) + (n / 2 - y.second));
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 6524
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
long long ans=0,input;
int main()
{
	cin>>input;
	ans+=input/11LL;
	ans*=2;
	input%=11;
	if(input>0)
	{
		ans++;
		if(input>6)
			ans++;
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void){
    for(int n, r; cin >> n >> r, n;){
        string yama;
        rep(i, n){
            char c = (n - i) + '0';
            yama.push_back(c);
        }
        
        while(r --){
            int p, c; cin >> p >> c;
            
            string str = yama.substr(p - 1, c);
            yama.replace(p - 1, c, "");
            yama = str + yama;
        }
        cout << yama[0] - '0' << endl;
    }

    return 0;
}

Measured memory_bytes: 1228
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <queue>

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

using namespace std;
typedef pair<int, int> pii;

int main(){
    string s;
    cin >> s;
    int l = 0;
    int r = s.size() - 1;
    int ans = 0;
    while(l < r){
        if(s[l] != s[r]){
            if(s[l] == 'x'){
                l ++;
                ans ++;
            }else if (s[r] == 'x'){
                r --;
                ans ++;
            }else{
                cout << -1 << endl;
                return 0;
            }
        }else{
            l ++;
            r --;
        }

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


Measured memory_bytes: 512
/* ---------- STL Libraries ---------- */
// IO library
#include <cstdio>

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>

#include <cmath>
#include <numeric>
#include <random>
#include <cstring>
#include <cassert>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <stack>

// type
#include <cstdint>
#include <functional>

/* ---------- Namespace ---------- */
using namespace std;

/* ---------- Type ---------- */
using ll = long long;
#define int ll
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
#define P pair<ll, ll>

/* ---------- Constants  */
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const int INF = 1LL << 55;

/* ---------- Functions */
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int H1, M1, H2, M2, K;
    cin >> H1 >> M1 >> H2 >> M2 >> K;

    cout << (H2 - H1) * 60 + (M2 - M1) - K << endl;

    return 0;
}

Measured memory_bytes: 3608
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>

#define LP(i,n) for(int i=0;i<n;i++)
#define Mset(x,v) memset(x,v,sizeof(x))
#define INF INT_MAX

using namespace std;
using LL=long long;

int V[105];

int main(){
    int N,M,d,v,sum;
    while(1){
        cin>>N>>M;
        Mset(V,0);
        sum=0;
        if(N==0&&M==0) break;
        LP(i,N){
            cin>>d>>v;
            V[d-1]=max(V[d-1],v);
        }
        LP(i,M){
            sum+=V[i];
        }
        cout<<sum<<endl;
    }
    return 0;
}



Measured memory_bytes: 3096
#include <iostream>
#include <vector>
#include <utility>
using i64 = long long;

std::vector<std::vector<std::pair<int, int>>> g;
int cnt[16];

auto precalc() {
    std::vector<int> ret(1 << 16);
    for (int i = 1; i < (1 << 16); i++) {
        int x = 0;
        for (int j = 0; j < 16; j++) if (i & (1 << j)) x ^= j;
        int max = !x;
        for (int j = i; j > 0; j = (j - 1) & i) {
            const int v = ret[j] + ret[j ^ i];
            if (v > max) max = v;
        }
        ret[i] = max;
    }
    return ret;
}

void dfs(const int v, const int p, int x) {
    for (const auto &edge : g[v]) {
        if (edge.first == p) continue;
        dfs(edge.first, v, edge.second);
        x ^= edge.second;
    }
    if (p >= 0) cnt[x]++;
}

int main() {
    int n;
    std::cin >> n;
    g.resize(n);
    for (int i = 1; i < n; i++) {
        int u, v, a;
        std::cin >> u >> v >> a;
        g[u].emplace_back(v, a);
        g[v].emplace_back(u, a);
    }

    const auto v = precalc();
    dfs(0, -1, 0);

    int ret = n - 1 - cnt[0], mask = 0;
    for (int i = 1; i < 16; i++) {
        ret -= cnt[i] / 2;
        if (cnt[i] & 1) mask |= 1 << i;
    }

    std::cout << ret - v[mask] << std::endl;

    return 0;
}


Measured memory_bytes: 10496
#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;
int N,M;
int r[100010],l[100010];
int search(int a,int b,int x){
  if(a>b){
    return 0;
    }
  else if(x==M){
    return b-a+1;
    }
  else{
    int na,nb;
    na=max(a,r[x]);
    nb=min(b,l[x]);
    return search(na,nb,x+1);
  }
}

int main(){
cin>>N>>M;
for(int i=0;i<M;i++){
  cin>>r[i]>>l[i];
}
cout<<search(0,100010,0);
}

Measured memory_bytes: 1024
main :: IO ()
main = do
  s <- getLine
  let r = reverse s
  let b = isIn r
  if b then putStrLn "YES"
       else putStrLn "NO"

isIn
  :: String -> Bool
isIn s = isIn' s False
  where
    isIn' ""                               b = b 
    isIn' ('r':'e':'m':'a':'e':'r':'d':ss) _ = isIn' ss True 
    isIn'         ('m':'a':'e':'r':'d':ss) _ = isIn' ss True 
    isIn'     ('r':'e':'s':'a':'r':'e':ss) _ = isIn' ss True
    isIn'         ('e':'s':'a':'r':'e':ss) _ = isIn' ss True
    isIn' _                                _ = False

Measured memory_bytes: 7548
// AOJ 2149
#include<iostream>
#include<vector>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;

int solve(int n, int a, int b, int c, int x, vector<int> y) {
  int current = 0, rand = x;
  for (int i = 0; i <= 10000; i++) {
    if (x == y[current]) {
      current++;
    }
    if (current == n) {
      return i;
    }
    x = (a * x + b) % c;
  }
  return -1;
}

int main() {
  int N,A,B,C,X;
  while (cin >> N >> A >> B >> C >> X and N) {
    vector<int> Y(N);
    REP(i, N) {
      cin >> Y[i];
    }

    cout << solve(N,A,B,C,X,Y) << endl;
  }
}

Measured memory_bytes: 1212
#include <stdio.h>
#include <vector>
using namespace std;
const int C=110001, D=101, M=1000000007;
typedef long long ll;

int n, a[D], i, j, k;
ll dp[2][C], wynn=0, fac[C], af[C];

inline ll cn(int n, int k) {return (((fac[n]*af[k])%M)*af[n-k])%M;}

inline ll pow (ll a, ll b, ll M){
	if (b==0)	return 1;
	ll c=1;
	while (b>0){
		if (b%2==1)	c=(c*a)%M;
		b/=2;
		a=(a*a)%M;
	}
	return c;
}

int main(){
	fac[0]=1, af[0]=1;
	for (i=1;i<C;i++)	fac[i]=(fac[i-1]*i)%M, af[i]=pow(fac[i], M-2, M);
	
	dp[0][0]=1;
	scanf ("%d %d", &n, &k);
	
	for (i=0;i<n;i++)	{
		scanf ("%d", &a[i]);
		a[i]++;
		for (j=k;j>=a[i];j--){
			dp[0][j]=(dp[0][j]+dp[1][j-a[i]])%M;
			dp[1][j]=(dp[1][j]+dp[0][j-a[i]])%M;
		}
	}
	
	for (i=0;i<=k;i++){
		wynn=(wynn+dp[0][i]*cn(k-i+n-1, n-1))%M;
		wynn=(wynn-dp[1][i]*cn(k-i+n-1, n-1))%M;
	}
	if (wynn<0)	wynn+=M;
	printf ("%lld", wynn);
	
return 0;}

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

int main(){
    using namespace std;
    unsigned long amaru{1}, amarazu{0};
    char c;
    while(cin >> c){
        auto tmp{amaru};
        amaru = min(amarazu + c - 47, tmp + 57 - c);
        amarazu = min(amarazu + c - 48, tmp + 58 - c);
    }
    cout << min(amaru + 1, amarazu) << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      int K=sc.nextInt();
      String S=sc.next();
      if(K<S.length()){
         for(int i=0;i<K;i++){
            System.out.print(S.charAt(i));
         }
         System.out.println("...");
      }else{
         System.out.println(S);
      }
   }
}


Measured memory_bytes: 35684
#include <bits/stdc++.h> 
#define int long long
#define quickie ios_base::sync_with_stdio(false); cin.tie(NULL);
#define rep(i, a, b) for(int i=a; i<b; i++)
#define rep1(i, a, b) for(int i=a; i<=b; i++)
#define repp(i, a, b) for(int i=b-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 db double
#define mi map<int, int>
#define mc map<char, int>
#define ms map<string, int>
#define vi vector<int>
#define qi queue<int>
#define MI(x) power(x, mod-2)
#define test int t; cin >> t;
#define mod 1000000007LL
//#define mod 998244353 
using namespace std;
int power(int x, int y) ;
int gcd(int a, int b) ;
#define pi 3.141592653589

signed main(){
    quickie
    string s, t ;
    cin >> s >> t ;
    int f = 0 ;
    rep(i, 0, SZ(s)) {
        if(s[i] != t[i]) 
            f = 1 ;
    }
    if(f == 1) cout << "No\n" ;
    else cout << "Yes\n" ;
}

int power(int x, int y) {
    int res = 1; x %= mod;
    while (y > 0) {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res%mod;
}

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

Measured memory_bytes: 3656
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author CompuWare
 */

#include <bits/stdc++.h>

using namespace std;

class DReplacing {
public:
    void solve(istream &in, ostream &out) {
        int n;
        in >> n;
        vector<int> v(n);
        map<int, int> mp;
        int64_t sum = 0;
        for (int i = 0; i < n; i++) {
            in >> v[i];
            mp[v[i]]++;
            sum += v[i];
        }
        int q;
        in >> q;
        for (int i = 0; i < q; i++) {
            int64_t b, c;
            in >> b >> c;
            if (mp[b] != 0) {
                sum -= (mp[b] * b);
                sum += (mp[b] * c);
                mp[c] += mp[b];
                mp[b] = 0;
            }
            out << sum << "\n";
        }
    }
};


int main() {
    DReplacing solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 7876
import itertools
from pprint import pprint

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

sides = [list(map(int,input().split())) for _ in range(m)]

#頂点を並び変える順列の総パターンを考える（ただし最初は「1」で固定）。
#総数は(N-1)! -> 7! -> 5040
#よって全探索は余裕そう。

vertexes = list(itertools.permutations(range(2,n+1)))

#pprint(vertexes)

counter = 0


for v in vertexes:
    #最初は1が始点
    for s in sides:
        if s[0] == 1 and s[1] == v[0]:
            break
        elif s[1] == 1 and s[0] == v[0]:
            break
    else:
        #この数列vはダメなので次のループへ
        continue

    for j in range(n-2):
        for s in sides:
            if v[j] == s[0] and v[j+1] == s[1]:
                break
            elif v[j] == s[1] and v[j+1] == s[0]:
                break
        else:
            break
    else:
        counter += 1



print(counter)
    

        



Measured memory_bytes: 46704