akhauriyash/Code-Regression huggingface dataset:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if sum(a)<sum(b):
    print(-1)
    exit()
dif_posi=[]
dif_nega=0
cnt=0
for x,y in zip(a,b):
    if x-y<0:
        dif_nega+=x-y
        cnt+=1
    else:
        dif_posi.append(x-y)

dif_posi=sorted(dif_posi,reverse=True)
for i in dif_posi:
    if dif_nega >= 0:
        break
    dif_nega+=i
    cnt+=1

print(cnt)

Measured memory_bytes: 19660
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    ll ans = 0;
    while (1) {
        bool all = true;
        rep(i, n) {
            if (a[i] % 2 == 0) {
                a[i] /= 2;
            } else {
                all = false;
                break;
            }
        }

        if (!all) {
            break;
        } else {
            ans++;
        }
    }

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

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

int main(void) {
    char judge[4][4] = {"AC", "WA", "TLE", "RE"};
    int AC_count = 0;
    int WA_count = 0;
    int TLE_count = 0;
    int RE_count = 0;

    int i, n;
    int count = 0;
    if(scanf("%d", &n) == 1);
    if(n >= 0 && n <= 100000) {
        char str[n][4];
        for(i = 0; count < n; i++){
            if(scanf("%s", str[i]) == 1);
            if(strcmp(str[i], judge[0]) == 0) {
                AC_count++;
            } else if(strcmp(str[i], judge[1]) == 0) {
                WA_count++;
            } else if(strcmp(str[i], judge[2]) == 0) {
                TLE_count++;
            } else if(strcmp(str[i], judge[3]) == 0) {
                RE_count++;
            }
            count++;
        }

        printf("AC x %d\n", AC_count);
        printf("WA x %d\n", WA_count);
        printf("TLE x %d\n", TLE_count);
        printf("RE x %d\n", RE_count);
    }

    return 0;
}


Measured memory_bytes: 2120
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int prime[3000001];

int M( int n, int p ){
	priority_queue<int, vector<int>, greater<int> > list;
	int k = 0;
	int list2[101];
	for( int i = n + 1; k < p; i++ ){
		if( prime[i] == 1 ){
			list2[k++] = i;
		}
	}
	for( int i = 0; i < p; i++ ){
		for( int j = i; j < p; j++){
			list.push( list2[i] + list2[j] );
		}
	}
	int result;
	for(int i = 0 ; i < p ; i++ ){
		result = list.top(); list.pop();
	}
	return result;
}

void P(){
	for( int i = 2; i <= 300000; i++ ){
		prime[i] = 1;
	}
	for( int i = 2; i <= 300000; i++ ){
		if( prime[i] == 1 ){
			for( int j = i * 2; j <= 300000; j += i ){
				prime[j] = 0;
			}
		}
	}
}

int main(){
	P();
	int n, p;
	while( cin >> n >> p && n != -1 ){
		cout << M( n, p ) << endl;
	}
}

Measured memory_bytes: 2440
# Nの入力受付
N = int(input())
# aの入力受付
aN = list(map(int, input().split()))
# 平均の値を計算
S = 0
A = 0
for i in aN:
    S += i
A = S / N
if A - int(A) < 0.5:
    A = int(A)
else:
    A = int(A) + 1
# Aにすべて書き換えるときのコストを計算
R = 0
for i in aN:
    R += (i - A)**2
print(R)


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

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


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define    clr(v,d)      memset( v, d ,sizeof(v))
typedef  long long     ll ;
typedef  unsigned long long ull;
const double EPS= (1e-9);
using namespace std;
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void SP(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}

int main()
{
    ll a,b;
    cin>>a>>b;
    cout<<max(a-b,max(a+b,a*b))<<endl;
}






Measured memory_bytes: 256
n, m = (int(x) for x in input().split())
lis = [0]*m
for i in range(n):
    tmp_lis = [int(x) for x in input().split()]
    del tmp_lis[0]
    for ii in tmp_lis:
        lis[ii-1] += 1
res = 0
for iii in lis:
    if iii == n:
        res += 1

print(res)


Measured memory_bytes: 3060
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
    LLONG SNum, cNum; cin >> SNum >> cNum;
    LLONG ans = 0;
    // まず，今あるSを使ってなるべくたくさんSccをつくる
    ans = min(SNum, cNum / 2);
    SNum -= ans;
    cNum -= ans * 2;
    // S が無くなった場合は残ったcをSに変えながらなるべくたくさんSccを作る
    // c が無くなった場合はこれ以上Sccを作れない
    if (SNum == 0)
    {
        ans += cNum / 4;
    }
    cout << ans << endl;
}


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

using namespace std;
using ll = long long;
#define MOD 1000000007

struct Mcomb {
    vector<ll> fac;
    vector<ll> inv;
    vector<ll> finv;
    //nCnまで対応できる
    Mcomb(int n) {
        fac.resize(n + 1), inv.resize(n + 1), finv.resize(n + 1);
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for(int i = 2; i <= n; 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;
        }
    }

    ll ncr(int n, int r){
        if (n < r) return 0;
        if (n < 0 || r < 0) return 0;
        return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
    }

    ll npr(int n, int r){
        if(n < r)return 0;
        if(n < 0 || r < 0)return 0;
        return (fac[n] * inv[n - r]) % MOD;
    }

};

int main()
{
    int x, y;
    cin >> x >> y;
    if((x + y) % 3){
        cout << 0 << endl;
        return 0;
    }
    int n = (x + y) / 3;
    x -= n, y -= n;
    if(x < 0 || y < 0){
        cout << 0 << endl;
        return 0;
    }

    Mcomb comb(1000000);
    cout << comb.ncr(n, x) << endl;
}

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

int main(int argc, char *argv[]) { ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  int n,m;cin>>n>>m;
  int r=pow(10,6),l=-1;
  for(int i=0;i<m;i++){
    int a,b;cin>>a>>b;
    l = max(l,a);
    r = min(r,b);
  }
  cout<< max(0, r-l+1)<<endl;
  return 0;
}


Measured memory_bytes: 3596
# -*- coding: utf-8 -*-
import itertools

n = int(input())
nlen = len(str(n))
A = ['3','5','7']
lis = [0,0,6,36,150,540,1806,5796,18150]
ans = 0
for i in itertools.product(A, repeat=nlen):
    string = "".join(i)
    if('3' in string and '5' in string and '7' in string) :
        if(int(string) <= n):
            ans += 1
for i in range(nlen-1):
    ans += lis[i]
print(ans)



Measured memory_bytes: 3188
using System;
class Program
    {
        static void Main(string[] args)
        {
            var x = Console.ReadLine().Split(' ');
            long n = long.Parse(x[0]);
            long m = long.Parse(x[1]);
            double d = double.Parse(x[2]);
            double r = n ^ (m - 2);
            double ans;
            if (d != 0) ans = r * (m - 1) * (n - d) * 2 / (r * n * n);
            else ans = r * (m - 1) * n / (r * n * n);
            Console.Write(ans);
        }
    }

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

int main(){
    int n;
    cin >> n;
    map<P,int> mp;
    vector<int> G[n];
    rep(i,n-1){
        int u,v,w;
        cin >> u >> v >> w;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
        mp[{u,v}]=w;
        mp[{v,u}]=w;
    }
    queue<int> que;
    que.push(0);
    int d[n];
    memset(d,-1,sizeof(d));
    d[0]=0;
    int color[n];
    memset(color,0,sizeof(color));

    while(!que.empty()){
        int x = que.front();
        que.pop();
        for(auto s:G[x]){
            if(d[s]!=-1) continue;
            d[s]=d[x]+mp[{x,s}];
            if(d[s]%2!=0) color[s]=1;
            que.push(s);
        }
    }
    rep(i,n) cout << color[i] << endl;

}

Measured memory_bytes: 19200
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vp;
#define rep(i,n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin() , v.end()
#define inputv(v,n) rep(i,n){\
    ll x;\
    cin>>x;\
    v.push_back(x);\
    }
const ll INF=99999999999999;
const ll MOD=1000000007;
 
int main() {
  int n;
  cin >> n;
  int t;
  vector<int> v;
  rep(i,n){
    cin >> t;
    v.push_back(t);
  }
  int a=0;
  rep(i,n){
    if(v[i] != i+1){
      a++;
    }
  }
  if(a == 0 || a == 2){
    cout << "YES";
  } else {
    cout << "NO";
  }
  
 return 0;
}

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

int main(){
  int n,m,i,p,pena=0,seikai=0;
  string s;
  cin>>n>>m;
  int wa[n],ac[n];
  for(i=0;i<n;i++){
    wa[i]=0;
    ac[i]=0;
  }
  for(i=0;i<m;i++){
    cin>>p>>s;
    if(s=="WA"){
      wa[p-1]++;
    }else{
      if(ac[p-1]==0){
        pena+=wa[p-1];
        ac[p-1]=1;
        seikai++;
      }
    }
  }
  cout<<seikai<<" "<<pena;
}

Measured memory_bytes: 1024
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>


#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795

template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}


template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}


bool is_prime(ll v)
{
	if(v<2) return false;
	if(v%2==0)
	{
		return (v==2);
	}

	for(ll i=3;i*i<=v;i+=2)
	{
		if(v%i==0) return false;
	}
	return true;
}

using pf=vector<pair<ll,int> >;
pf prime_factorization(ll v)
{
	pf p;

	for(ll i=2;i*i<=v;i++)
	{
		if(v%i==0)
		{
			p.push_back(make_pair(i, 1));
			for(v/=i;v%i==0;v/=i)
			{
				p.back().second++;
			}
		}
	}
	if(v>1) p.push_back(make_pair(v, 1));

	return p;
}


int calc_divisor(vll&ret, ll v)
{
	auto p=prime_factorization(v);

	ret.clear();
	ret.push_back(1);
	for(auto&mp:p)
	{
		int n=ret.size();
		for(int i=0;i<n;i++)
		{
			ll t=ret[i];
			for(int j=mp.second;j>0;j--)
			{
				ret.push_back(t*=mp.first);
			}
		}
	}
	sort(ret.begin(), ret.end());

	return ret.size();
}


struct solve
{
	solve(){}
	ll operator()(int n) {
		ll ret=0;
		vll c(n+1, 1);
/*
		for(int i=1;i<=n;i++)
		{
			auto p=prime_factorization(i);
			ll dn=1;
			for(auto&mp:p) dn*=(mp.second+1);
			ret+=i*dn;
		}
*/
		ret=1;
		for(int i=2;i<=n;i++)
		{
			if(c[i]==1)
			{
				ll u=1;
				for(int k=1;;k++)
				{
					u*=i;
					if(u>n) break;
					for(int j=u;j<=n;j+=u)
					{
						c[j]=c[j]*(k+1)/k;
					}
				}
			}
			ret+=i*c[i];
		}
		return ret;
	}
};

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

	for(;;)
	{
		int n;
		cin >> n;
		if(cin.fail()) break;
		cout << solve()(n) << "\n" << flush;
	}
	return 0;
}


Measured memory_bytes: 81332
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9+7;

signed main(){
    int A, B; cin >> A >> B;
    A--;
    int ca = 0, cb = 0;
    if(A %2 == 0){
        if(A % 4 == 0) ca = A;
        else ca = (1 ^ A);
    }
    if(A % 4 == 1) ca = 1;
    if(B % 4 == 1) cb = 1;
    if(B % 2 == 0){
        if(B % 4 == 0) cb = B;
        else cb = (1 ^ B);
    }
    cout << (ca ^ cb) << endl;
}

Measured memory_bytes: 384
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy

# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    r, g, b, n = ns()
    ans = 0
    for i in range(n // r + 1):
        for j in range(n // g + 1):
            tmp = n - r * i - g * j
            if tmp % b == 0 and tmp > -1:
                ans += 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 61928
#include<stdio.h>
#include<stdlib.h>
#define MAX 20000  // スタックの最大値

typedef struct{
  int top;         // スタックポインタ(一番上の位置)
  int S[MAX];
}Stack;


// スタック st の初期化
void initialize(Stack *st)
{
  st->top = 0;
  st->S[st->top] = 0;
}

// スタック st が空かどうかを確認
int isEmpty(Stack *st)
{
  return st->top == 0;
}

// スタック st が一杯かどうかを確認
int isFull(Stack *st)
{  
  return st->top >= MAX - 1;
}

// スタック st に要素 x をプッシュ(一番上に要素を追加)
void push(int x, Stack *st)
{
  if (isFull(st)) 
    exit(1);

  st->top++;
  st->S[st->top] = x;
}

// スタック st から要素をポップ(一番上の要素を取り出す)
int pop(Stack *st) {
  if (isEmpty(st)) 
    exit(1);

  st->top--;
  return st->S[st->top+1];
}


int main(void)
{
  Stack st1;     // '\' の開始位置を格納するスタック
  Stack st2_j;   // 各水たまりの最も左の'\' の位置を格納するスタック
  Stack st2_a;   // 各水たまりの水量を格納するスタック

  // スタック初期化
  initialize(&st1);
  initialize(&st2_j);
  initialize(&st2_a);

  char c;
  int sum = 0;  // 水たまりの総水量

  // 地形を読み込み，各水たまりの水量を計算
  for (int i = 0; (c = getchar()) != EOF; i++) {
    if (c == '\\') {                                 // '\' が出た時の処理
      // 位置 i を st1 にプッシュ(write your code)
    	push(i, &st1);
    	
    }else if (!isEmpty(&st1) && c == '/') {          // '/' が出た時の処理
      // 水たまりの総水量 sum を更新(write your code)
    	int j = pop(&st1);
    	sum += i-j;
    	
      // st2_j と st2_a を用いて水たまりの水量を更新 
      int a = i - j;   // 更新する水たまりの水量を a に格納していく
      while (!isEmpty(&st2_j) && st2_j.S[st2_j.top] > j) {  // 最も左の '\' の位置が j よりも大きい場合，以下を繰り返す
        // 水たまりの水量を求めていく (write your code)
      	a += pop(&st2_a); pop(&st2_j);
      }
      // 更新した水たまりの「最も左の'\' の位置」および「水量」を更新 (write your code)
    	push(a ,&st2_a);
    	push(j ,&st2_j);
    }
  }

  printf("%d\n", sum);       // 水たまりの総水量を出力
  printf("%d", st2_a.top);   // 水たまりの個数を出力

  // 各水たまりの水量を断面図の左から出力(スタックの底から出力)
  for (int i = 1; i <= st2_a.top; i++) 
    printf(" %d", st2_a.S[i]);  
  printf("\n");

  return 0;
}



Measured memory_bytes: 2144
k, n = map(int, input().split())
a = list(map(int, input().split()))

mn = 10 ** 10

for i in range(n):
    if i == 0:
        mn = min(mn, a[n - 1] - a[0])

    else:
        mn = min(mn, k - a[i] + a[i - 1])

for i in range(n):
    a[i] = k - a[i]

a.sort()

for i in range(n):
    if i == 0:
        mn = min(mn, a[n - 1] - a[0])

    else:
        mn = min(mn, k - a[i] + a[i - 1])

print(mn)

Measured memory_bytes: 78924
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include <stack>
#include<iomanip>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define ll long long
#define llint long long int
#define reverse(v)  reverse(v.begin(), v.end());
#define Yes(ans) if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF  UnionFind 
#define Pint pair<int,int>
#define Pll pair<llint,llint>
#define keta(a)  fixed << setprecision(a)
constexpr auto INF = 1000000000000000000;
constexpr auto mod = 1000000007;
//辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge { llint to, kane, cost; };

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

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;
}

// mod. m での a の逆元 a^{-1} を計算するよ！
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}


//aCbをmod.mで割った余りを求める
int c(int a, int b, int m) {
	int ans = 1;
	for (int i = 0; i < b; i++) {
		ans *= a - i;
		ans %= m;
	}
	for (int i = 1; i <= b; i++) {
		ans *= modinv(i, m);
		ans %= m;
	}
	return ans;
}
struct UnionFind {
	vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

	UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
		for (int i = 0; i < N; i++) par[i] = i;
	}
	int size(int a) {
		return par[root(a)];
	}

	int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
		if (par[x] == x) return x;
		return par[x] = root(par[x]);
	}

	void unite(int x, int y) { // xとyの木を併合
		int rx = root(x); //xの根をrx
		int ry = root(y); //yの根をry
		if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
		par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
	}

	bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
		int rx = root(x);
		int ry = root(y);
		return rx == ry;
	}
};

int b_s(vector<int>& a, int key) {
	int left = -1;
	int right = a.size();
	while (right - left > 1) {
		int mid = left + (right - left) / 2;
		if (a[mid] >= key)right = mid;
		else left = mid;
	}
	return left;
}

int main() {
	int h, w;
	cin >> h >> w;
	whei(int) dist(10, hei(int)(10, 1000000));
	rep(i, 0, 0)dist[i][i] = 0;
	rep(i, 0, 10)rep(j, 0, 10) {
		cin >> dist[i][j];
	}
	int ans = 0;
	rep(k, 0, 10)rep(i, 0, 10)rep(j, 0, 10)dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
	rep(i, 0, h) {
		rep(j, 0, w) {
			int a;
			cin >> a;
			if (a == -1)continue;
			ans += dist[a][1];
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int main()
{
    int n;
    cin >> n;
    int t[n];
    int sum = 0;
    for(int i = 0;i < n;i++)
    {
        cin >> t[i];
        sum += t[i];
    }
    int m;
    cin >> m;
    vector<int> v;
    for(int i = 0;i < m;i++)
    {
        int p,x;
        cin >> p >> x;
        v.push_back(sum - t[p - 1] + x);
    }
    for(int i = 0;i < v.size();i++)
    {
        cout << v.at(i) << "\n";
    }
    
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <cmath>
#include <map>
#include <cstring>

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, init, n) for(int i = init; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define Cout(obj) cout << obj << endl
#define Size(obj) (int)(obj).size()
#define fcout cout << fixed << setprecision(10)
#define fi first
#define se second

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct
{
    int to, cap, rev;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

const int MaxV = 100000;

vector<edge> graph[MaxV];
bool used[MaxV];

void addEdge(int from, int to, int cap)
{
    graph[from].push_back({to, cap, Size(graph[to])});
    graph[to].push_back({from, 0, Size(graph[from]) - 1});
}

int dfs(int v, int t, int f)
{
    if(v == t) return f;
    used[v] = true;

    for(auto &e : graph[v])
    {
        if(!used[e.to] && e.cap > 0)
        {
            int d = dfs(e.to, t, min(f, e.cap));

            if(d > 0)
            {
                e.cap -= d;
                graph[e.to][e.rev].cap += d;
                return d;
            }
        }
    }

    return 0;
}

int maxFlow(int s, int t)
{
    int flow = 0;
    for(;;)
    {
        memset(used, false, sizeof(used));

        int f = dfs(s, t, iINF);

        if(f == 0) return flow;

        flow += f;
    }
}

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

    int V, E;
    cin >> V >> E;
    REP(i, E)
    {
        int s, t, c;
        cin >> s >> t >> c;

        addEdge(s, t, c);
    }

    cout << maxFlow(0, V - 1) << endl;

    return 0;
}


Measured memory_bytes: 5684
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;

int dp[6010];

int main() {
	int n, s; cin >> n >> s;
	dp[0] = 1;
	for (int i = 0; i < n; i++) {
		int x; cin >> x;
		for (int j = s; j >= 0; j--) {
			(dp[j + x] += dp[j]) %= mod;
			(dp[j] *= 2) %= mod;
		}
	}
	cout << dp[s] << "\n";
}

Measured memory_bytes: 3644
N,W = map(int,input().split())
max_v = 1000
wv_l= []
for i in range(N):
    wv_l.append(list(map(int,input().split())))
DP = [[10**10]*(max_v*N +1) for i in range(N+1)]
DP[0][0] = 0
for i in range(N):
    for sum_v in range(max_v*N +1):
#        print(DP)
#        print(i,sum_w)
        if sum_v-wv_l[i][1] >= 0:
            DP[i+1][sum_v] = min(DP[i+1][sum_v],DP[i][sum_v-wv_l[i][1]] + wv_l[i][0])
        DP[i+1][sum_v] = min(DP[i+1][sum_v],DP[i][sum_v])

#print(DP[N])
ans = -1
for i in range(len(DP[N])):
    if DP[N][i] <= W:
        ans = max(ans,i)
#ans = bisect.bisect_right(DP[N], W)
print(ans)

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

int main(){
  int N;
  cin >> N;
  vector<long> A(N + 1);
  vector<long> B(N + 1, 0);
  for(long i = 0; i < N; i++){
    cin >> A.at(i);
    B.at(A.at(i)) += 1;
  }

  long long int sum = 0;
  for(long i = 0; i <= N; i++){
    long long k = B.at(i);
    sum += k * (k - 1) / 2;
  }
  
  for(long k = 0; k < N; k++){
    long long int ans = sum - (B.at(A.at(k)) - 1);
    cout << ans << endl;
  }
}

Measured memory_bytes: 5760
import sys
r = input()
# print(r)
ans = sum(list(map(int, r)))
# print(ans)
if len(r) == 1:
    print(max(ans, int(r)))
    sys.exit()
if len(str(int(r)+1)) == len(r)+1:
    print(max(ans, len(r)*9))
    sys.exit()
if r[0] == 1:
    print(max(ans, (len(r)-1)*9))
else:
    print(max(ans, int(r[0])-1+(len(r)-1)*9))

Measured memory_bytes: 3064
#include <stdio.h>

int main(void)
{
    int N, M;
    int ans;
    
    scanf("%d %d", &N, &M);
    
    ans = N * (N - 1) / 2 + M * (M - 1) / 2;
    
    printf("%d\n", ans);
    
    return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
int n, A[101000], B[101000];
long long M;
int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d%d",&A[i],&B[i]);
    }
    for(i=n;i>=1;i--){
        if(A[i]+M==0)continue;
        M = (A[i]+M+B[i]-1)/B[i]*B[i] - A[i];
    }
    printf("%lld\n",M);
}

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

int main(){
    short a , b;
    a = b = 0;
    cin>>a>>b;
    
    string result = "";

    short big = a >= b ? a : b ;
    short small = a < b ? a : b ;

    for(int i = 0 ; i < big ; i++){
        cout<<small;
    } 
    cout<<endl;
    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 21;

int A[MAXN][MAXN];
int dp[1 << MAXN];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
    }
  }

  for (int i = 0; i < N; i++) {
    dp[1 << i] = A[0][i];
  }

  for (int i = 1; i < (1 << N); i++) {
    int b = __builtin_popcount(i);
    for (int j = 0; j < N; j++) {
      if ((~i & 1 << j) && A[b][j]) {
        dp[i | 1 << j] += dp[i];
        dp[i | 1 << j] %= MOD;
      }
    }
  }

  cout << dp[(1 << N) - 1] << endl;

  return 0;
}

Measured memory_bytes: 8448
def main():
    N, K = (int(i) for i in input().split())
    h = [int(input()) for i in range(N)]
    h.sort()
    ans = 10**9 + 7
    for i in range(N-(K-1)):
        ans = min(ans,h[i+(K-1)] - h[i])
    print(ans)
if __name__ == "__main__":
    main()

Measured memory_bytes: 50264
#include <stdio.h>
#include <vector>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>

#define rep(i,n) for (int i=0;i<(n);++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;


int main() {
    int a, b;
    cin >> a >> b;
    int ax=0, bx=0;
    
    rep(i, b){
        ax += a*pow(10, i);
    }
    
    rep(i, a){
        bx += b*pow(10, i);
    }
    
    if (ax > bx) cout << ax << endl;
    else cout << bx << endl;

}


Measured memory_bytes: 512
from itertools import product as p
n=int(input())
s=[[]for i in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    s[a].append([b,i])
    s[b].append([a,i])
m=int(input())
l=[]
for i in range(m):
    u,v=map(int,input().split())
    a=[[]for i in range(n+1)]
    a[u]=[]
    q=[u]
    while q:
        t=q.pop(0)
        d=0
        for i,j in s[t]:
            if a[i]==[]:
                a[i]=a[t]+[j]
                q.append(i)
                if i==v:
                    d=1
                    break
        if d==1:
            break
    l.append(a[v])
for i in range(m):l[i]=set(l[i])
s=[0]*(m+1)
for i in p([0,1],repeat=m):
    d=set([])
    if sum(i)!=0:
        for j in range(m):
            if i[j]:
                d|=l[j]
        s[sum(i)]+=pow(2,n-1-len(d))
l=0
for i in range(1,m+1):
    l+=((-1)**(i-1))*s[i]
print(pow(2,n-1)-l)

Measured memory_bytes: 45740
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include<set>
#include<tuple>
using namespace std;
#define INF 11000000000
#define MAX 100000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;
typedef pair<pair<int,int>,int> p;
typedef pair< pair<int,int>, int> p; 
#define bit(n,k) ((n>>k)&1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)


int main(){
    string s;
    cin>>s;
    char now=s[0];
    ll count=1;
    vector<ll> V;
    s.push_back('?');
    for(int i=1;i<(int)s.size();i++){
        if(s[i]==now) count++;
        else{
            V.push_back(count);
            if(now=='<') now='>';
            else now='<';
            count=1;
        }
    }
            ll ans=0;

    if(s[0]=='<'){
        ll pre;
        for(int i=0;i<(int)V.size();i++){
            ll x=V[i];
            if(i%2==0 && i==(int)V.size()-1) ans+=x*(x+1)/2;
            else if(i%2==0) ans+=x*(x-1)/2;
            else{
                ans+=x*(x-1)/2;
                ans+=max(pre,x);
            }
            pre=x;
            //cout<<ans<<endl;
        }

    }else{
        ll pre;
        for(int i=0;i<(int)V.size();i++){
            ll x=V[i];
            if(i==0) ans+=x*(x+1)/2;
            else if(i%2==1 && i==(int)V.size()-1) ans+=x*(x+1)/2;
            else if(i%2==1) ans+=x*(x-1)/2;
            else{
                ans+=x*(x-1)/2;
                ans+=max(pre,x);
            }
            pre=x;
        }
     
    }
    cout<<ans<<endl;
}



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

int SS(int n) {
  int x = 0;
  while (n > 0) {
    x += (n % 10);
    n /= 10;
  }
  return x;
}

int main() {
  int N;
  cin >> N;
  if (N % SS(N) == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
loop do
  line = gets.chomp.split
  n = line[0].to_i
  x = line[1].to_i

  break if n == 0 && x == 0

  count = 0
  for i in 1..n do
    for j in i+1..n do
      for k in j+1..n do
        count += 1 if i + j + k == x
      end
    end
  end

  puts count
end

Measured memory_bytes: 6108
#include <cstdio>
#include <utility>
#include <algorithm>

using namespace std;
const int N=2e5+10, inf=1e9;
typedef pair<int,int> ii;
int n,m;
ii a[N];
int tree[4*N], lazy[4*N];

void dispatch (int id, int curL, int curR) {
    if (lazy[id]) {
        tree[id] += lazy[id];
        if (curL!=curR) {
            lazy[2*id]+=lazy[id];
            lazy[2*id+1]+=lazy[id];
        }
        lazy[id]=0;
    }
}
void update (int id, int curL, int curR, int uL, int uR, int uVal) {
    dispatch(id,curL,curR);
    if (uL>uR || curR<uL || curL>uR) return;
    if (uL<=curL && curR<=uR) {
        lazy[id] += uVal;
        dispatch(id, curL, curR);
        return ;
    }
    int mid=(curL+curR)/2;
    update(2*id, curL, mid, uL, uR, uVal); update(2*id+1, mid+1, curR, uL, uR, uVal);
    tree[id] = max(tree[2*id], tree[2*id+1]);
}
int query (int id, int curL, int curR, int qL, int qR) {
    dispatch(id,curL,curR);
    if (qL>qR || curL>qR || curR<qL) return -inf;
    if (qL<=curR && curR<=qR) return tree[id];
    int mid=(curL+curR)/2;
    return max(query(2*id,curL,mid,qL,qR),
               query(2*id+1,mid+1,curR,qL,qR));
}
int main() {/*
    freopen("D.inp","r",stdin); freopen("D.out","w",stdout); //*/
    scanf("%d%d",&n,&m);
    for (int i=1; i<=n; i++) scanf("%d%d",&a[i].first,&a[i].second);
    for (int i=1; i<=m+1; i++) update(1, 1, m+1, i, i, (i-1));
    sort(a+1, a+n+1);
    int ans = n-m;
    for (int i=1; i<=n; i++) {
        //if (a[i].first+1 == a[i].second)
        //    update(1,1,m+1,1,m+1,+1);
        //else
            update(1,1,m+1,1,a[i].second,+1);
        int tmp = query(1,1,m+1,a[i].first+1,m+1);
        ans = max(ans, tmp-m-a[i].first);
    }
    printf("%d\n",max(ans,0));
    return 0;
}


Measured memory_bytes: 7808
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

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

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&... args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010


int main() {
  string S, T;
  int ans = 0;

  cin >> S >> T;

  for (int i = 0; i < S.size(); i++) {
    ans += S[i] != T[i];
  }

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

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

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;++i){
        bool r[3]={false};
        int res=0;
        for(int o=0;o!=3;){
            string buf;
            cin>>buf;
            if(buf=="HIT"){
                if(r[2]){
                    r[2]=false;
                    res++;
                }
                if(r[1]){
                    r[1]=false;
                    r[2]=true;
                }
                if(r[0]){
                    r[0]=false;
                    r[1]=true;
                }
                r[0]=true;
            }
            if(buf=="HOMERUN"){
                for(int j=0;j<3;++j){
                    if(r[j]){
                        r[j]=false;
                        res++;
                    }
                }
                res++;
            }
            if(buf=="OUT"){
                o++;
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

Measured memory_bytes: 1208
a, b, c, d = map(int, input())
if a + b + c + d == 7:
    print(str(a) + "+" + str(b) + "+" + str(c) + "+" + str(d) + "=7")
elif a - b + c + d == 7:
    print(str(a) + "-" + str(b) + "+" + str(c) + "+" + str(d) + "=7")
elif a + b - c + d == 7:
    print(str(a) + "+" + str(b) + "-" + str(c) + "+" + str(d) + "=7")
elif a + b + c - d == 7:
    print(str(a) + "+" + str(b) + "+" + str(c) + "-" + str(d) + "=7")
elif a - b - c + d == 7:
    print(str(a) + "-" + str(b) + "-" + str(c) + "+" + str(d) + "=7")
elif a - b + c - d == 7:
    print(str(a) + "-" + str(b) + "+" + str(c) + "-" + str(d) + "=7")
elif a + b - c - d == 7:
    print(str(a) + "+" + str(b) + "-" + str(c) + "-" + str(d) + "=7")
elif a - b - c - d == 7:
    print(str(a) + "-" + str(b) + "-" + str(c) + "-" + str(d) + "=7")


Measured memory_bytes: 38384
/**
 * File              : b.cpp
 * Author            : recurze
 * Date              : 21:07 18.01.2019
 * Last Modified Date: 21:13 18.01.2019
 */
#include <algorithm>
#include <numeric>
#include <stdio.h>

void readInput();
void solve();
void printAns();

int main() {
    int t = 1;
    //scanf("%d", &t);

    for (int tc = 1; tc <= t; ++tc) {
        readInput();
        solve();
        printAns();
    }
}

const int N = 1010101;

int n = 0;
int a[N];
void readInput() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", a + i);
    }
}

int ans = 0;
void solve() {
    int max = *std::max_element(a, a + n);
    int sum = std::accumulate(a, a + n, 0);
    ans = sum - max/2;
}

void printAns() {
    printf("%d\n", ans);
}


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

int main() {
    ll h,w;
    std::cin >> h>>w;
    vector<string> a(h);
    vector<ll> cnt(26);
    for (int i = 0; i < h; i++) {
        std::cin >> a[i];
        for (int j = 0; j < w; j++) {
            cnt[a[i][j]-'a']++;   
        }
    }
    
    if(h%2 ==0 && w%2== 0){
        for (int i = 0; i < 26; i++) {
            if( cnt[i]%4!=0){
                std::cout << "No" << std::endl;
                return 0;
            }
        }
        std::cout << "Yes" << std::endl;
        return 0;
    }
    
    if(h%2 !=0 && w%2!= 0){
        // 1余るやつは一回きりしか使えないし、一回だけいる
        bool once = false;
        // 2ずつ使うやつは、一定数いるので、優先的に割り当てていく。
        ll want_2 = h/2+w/2;
        for (int i = 0; i < 26; i++) {
            if(cnt[i] %2 == 1 ){
                if(!once){
                    once = true;
                    if(cnt[i]%4 == 3){
                        want_2--;
                    }
                }else{
                    std::cout << "No" << std::endl;
                    return 0;
                }
            }
            if(cnt[i]%4 == 2){
                want_2--;
            }
        }
        if(!once){
            std::cout << "No" << std::endl;
            return 0;
        }
        // 二個だけ需要に答えられない
        if(want_2<0){
            std::cout << "No" << std::endl;
            return 0;
        }
        // 2とかなら4で調整できるけど
        if(want_2%2==1){
            std::cout << "No" << std::endl;
            return 0;
        }
        std::cout << "Yes" << std::endl;
        return 0;
    }
    
    ll want_2;
    if(h%2==0){
        want_2 = h/2;
    }else{
        want_2 = w/2;
    }
    
    for (int i = 0; i < 26; i++) {
        if(cnt[i] %2 == 1 ){
            std::cout << "No" << std::endl;
            return 0;
        }
        if(cnt[i]%4 == 2){
            want_2--;
        }
    }
    
    // 二個だけ需要に答えられない
    if(want_2<0){
        std::cout << "No" << std::endl;
        return 0;
    }
    // 2とかなら4で調整できるけど
    if(want_2%2==1){
        std::cout << "No" << std::endl;
        return 0;
    }
    std::cout << "Yes" << std::endl;
    return 0;
    
    
    // 回分が作れる条件
    // 偶数の場合要素の個数個
    // 奇数の場合1つだけ奇数個
    
    // 何をどこに置くかはどうでも良さそう、入れ替えられる
    
    // 2こずつ順番にまわしていけば縦の回文は作れる
    // よこにも回分にしようと思うとiに置くならn-i-1にもおけよとなりそう
    // 4つずつ消費することになる
    // 偶数偶数の場合は順調に消費
    // 奇数偶数の場合は 偶数/2 通りの組み合わせは2ずつでいける
    // ーーーーー
    // ーーーーー
    // ーーーーー
    // ーーーーー
    // ーーーーー
    // ーーーーー
    
    // 奇数奇数
    // 4とる。1でいいやつが一人。2でいいやつが、奇数_1/2 奇数_2/2
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    // ーーー
    

    
    
    
}


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 = 1e5 + 10;
int n , a[N] , ok;
vi g[N];

int dfs(int c,int fa) {
  int sum = sz(g[c]) == 1 ? a[c] : a[c] * 2;
  int mx = 0;
  for(auto t : g[c]) if(t != fa) {
    int gt = dfs(t , c);
    sum -= gt;
    mx = max(mx , gt);
  }
  if(sz(g[c]) != 1) {
    ok &= mx <= a[c] && sum <= a[c] && sum >= 0;
  }
  return sum;
}

int main(){
  cin >> n;
  rep(i,1,n+1) cin >> a[i];
  rep(i,1,n) {
    int u,v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  ok = 1;
  int t = dfs(1 , -1);
  ok &= t == 0;
  cout << (ok ? "YES" : "NO") << endl;
  return 0;
}


Measured memory_bytes: 12800
N = int(input())
h = [int(i) for i in input().split()]

for i in range(N-1):
    if h[-(i+2)] > h[-(i+1)] + 1:
        print("No")
        exit()
    
    elif h[-(i+2)] == h[-(i+1)] + 1:
        h[-(i+2)] -= 1
    
    else:
        continue

print("Yes")

Measured memory_bytes: 14252
#include <bits/stdc++.h>
#define ll long long int
#define maxy __LONG_LONG_MAX__
#define mod 1000000007
using namespace std;
int main() {
    ll n,w;
	cin>>n>>w;
	ll wt[n],val[n];
	for(int i=0;i<n;i++)
	{
		cin>>wt[i]>>val[i];
	}
	ll v[n+1][w+1];
	for(int i=0;i<=n;i++)
	{
		v[i][0]=0;
	}
	for(int i=0;i<=w;i++)
	{
		v[0][i]=0;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=w;j++)
		{
			if(wt[i-1]<=j)
			{
				v[i][j]=max(v[i-1][j],val[i-1]+v[i-1][j-wt[i-1]]);
			}
			else 
			{
				v[i][j]=v[i-1][j];
			}
		}
	}
	cout<<v[n][w];
    return 0;
}


Measured memory_bytes: 79104
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
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; }

using Weight = int;
struct Edge {
	int s, d; Weight w;
	Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w) {};
	bool operator<(const Edge &e)const { return w < e.w; }
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

struct LowestCommonAncestor {
	const int n, log2_n;
	vector<vector<int>> parent;
	vector<int> depth;
	LowestCommonAncestor(const Graph &g, int root)
		:n(g.size()), log2_n(log2(n) + 1), parent(log2_n, vector<int>(n)), depth(n) {
		dfs(g, root, -1, 0);
		for (int k = 0; k + 1 < log2_n; k++) {
			for (int v = 0; v < (int)g.size(); v++) {
				if (parent[k][v] < 0)
					parent[k + 1][v] = -1;
				else
					parent[k + 1][v] = parent[k][parent[k][v]];
			}
		}
	}

	void dfs(const Graph &g, int v, int p, int d) {
		parent[0][v] = p;
		depth[v] = d;
		for (auto &e : g[v]) {
			if (e.d != p) dfs(g, e.d, v, d + 1);
		}
	}

	int get(int u, int v) {
		if (depth[u] > depth[v]) std::swap(u, v);
		for (int k = 0; k < log2_n; k++) {
			if ((depth[v] - depth[u]) >> k & 1) {
				v = parent[k][v];
			}
		}
		if (u == v) return u;
		for (int k = log2_n - 1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) {
				u = parent[k][u];
				v = parent[k][v];
			}
		}
		return parent[0][u];
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	Graph g(n);
	rep(i, 0, n) {
		int k; cin >> k;
		rep(j, 0, k) {
			int c; cin >> c;
			g[i].emplace_back(i, c);
		}
	}
	int q; cin >> q;
	LowestCommonAncestor lca(g, 0);
	rep(i, 0, q) {
		int u, v; cin >> u >> v;
		cout << lca.get(u, v) << endl;
	}
	return 0;
}

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

namespace AtCoder
{
	class Program
	{
		static void Main(string[] args)
		{
			int N = int.Parse(Console.ReadLine());
            int[] A = new int[N];
            for(int i = 0; i < N; i++)
            {
                A[i] = int.Parse(Console.ReadLine());
            }
            int max1 = 0, max2 = 0,maxIndex = -1;
            for(int i = 0; i < N; i++)
            {
                if(A[i] > max1)
                {
                    maxIndex = i;
                    max2 = max1;
                    max1 = A[i];
                }
                else if(A[i] > max2)
                {
                    max2 = A[i];
                }
            }
            for (int i = 0; i < N; i++)
            {
                if (i != maxIndex) Console.WriteLine(max1);
                else Console.WriteLine(max2);
            }


            Console.ReadLine();

		}
	}
}


Measured memory_bytes: 17108
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,i,k,tmp=0,mx=0;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int main()
{
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++)scanf("%d",&x),tmp=gcd(tmp,x),mx=max(mx,x);
	if(k%tmp==0&&k<=mx)printf("POSSIBLE\n");
	else printf("IMPOSSIBLE\n"); 
	return 0;
} 

Measured memory_bytes: 128
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace AtCoder.XXX.B
{ 
    public class Program
    {
        public static void Main(string[] args)
        {
            var arice = 0;
            var bob = 0;
            var n = r();
            var d = cr();
            var datas = new List<int>(d);
            while(true)
			{
                var max = datas.Max();
                arice += max;
                datas.Remove(max);
                if(datas.Count == 0)
				{
                    break;
				}
                max = datas.Max();
                bob += max;
                datas.Remove(max);

                if (datas.Count == 0)
                {
                    break;
                }
            }
            P(Math.Abs(arice - bob));
        }

        private static StreamReader sr;
        public static int[] Ecr()
        {
            if (sr == null)
            {
                sr = new StreamReader("input.txt", Encoding.GetEncoding("UTF-8"));
            }
            return sr.ReadLine().Split().Select(int.Parse).ToArray();
        }
        public static  int[] cr()
        {
            return Console.ReadLine().Split().Select(int.Parse).ToArray();
        }
        public static string Er()
        {
            if (sr == null)
            {
                sr = new StreamReader("input.txt", Encoding.GetEncoding("UTF-8"));
            }
            return sr.ReadLine();
        }
        public static string r()
        {
            return Console.ReadLine();
        }
        public static int cri()
        {
            return int.Parse(Console.ReadLine());
        }
        public static void P(string str)
        {
            Console.WriteLine(str);
        }
        public static void P(int num)
        {
            P(num.ToString());
        }
    }
}


Measured memory_bytes: 13280
#include<iostream>

using namespace std;

int main(){
  int a,b,c;
  while(cin >> a && cin >> b && cin >> c){
    if(a <= c && b <= c){
      cout << a+b << endl;
    } else if(b <= a && c <= a){
      cout << b+c << endl;
    } else if(a <= b && c <= b){
      cout << a+c << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N[32]={1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
  int A;
  cin >>A;
  cout <<N[A-1]<<endl;
}

Measured memory_bytes: 256
target, N = [int(x) for x in input().split(" ")]
if N == 0:
    print(target)
else:
    p_array = [int(x) for x in input().split(" ")]

    for i in range(N+1):
        superior = target - i
        if not(superior in p_array):
            print(superior)
            break

        inferior = target + i
        if not(inferior in p_array):
            print(inferior)
            break

Measured memory_bytes: 9096
n = int(input())
a = list(map(int, input().split()))
mod = 1000000007

def wash_b(line, s):
    l = len(line)
    neck = 2**s
    cnt = 0
    for i in range(l):
        if line[i] >= neck:
            line[i] -= neck
            cnt += 1
    return cnt
ans = 0
for i in range(60):
    w = wash_b(a, 59 - i)
    ans += (w*(n-w)*(2**(59-i))) % mod

ans %= mod
print(ans)

Measured memory_bytes: 122936
#include <bits/stdc++.h>
using namespace std;
#define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i))
#define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i))
#define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i))
#define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i))
#define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i))
#define rnfor(i,a,b) for(ll (i) = (a) - 1; (i) >= (b); --(i))
#define ll long long
#define mod (1000000007)
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(),(x).end()

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

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

ll factorial(ll n){
    ll t = 1;
    rep1(i, n)t *= i;
    return t;
}

ll combinations(ll n, ll k) {
    ll r = 1;
    rep1(i, k)r = r * n-- / i;
    return r;
}

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n),x(n);
    rep0(i, n){
        cin >> a[i];
        x[0] += a[i];
        if(i % 2 == 1)x[0] -= a[i] * 2;
    }
    rep1(i, n - 1)x[i] = a[i - 1] * 2 - x[i - 1];
    rep0(i, n)cout << x[i] << " ";
    return 0;
}


Measured memory_bytes: 2944
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
last_dish = 0
a = 0
b = 0
c= 0
manzoku = 0
for i in range(N):
  a = A[i]
  manzoku += B[i]
  if last_dish != 0 and last_dish + 1 == A[i]:
    manzoku += C[last_dish - 1]
  last_dish = A[i]
print(manzoku)

Measured memory_bytes: 9232