akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define int long long
constexpr int MOD = 1000000000 + 7;

constexpr int modpow(int a, int n) {
    int rtn = 1;
    while (n > 0) {
        if (n & 1) rtn = rtn * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return rtn;
}

void normalize(pair<int, int> &p) {
    if (p.first == 0 || p.second == 0) {
        p.first = abs(p.first), p.second = abs(p.second);
    } else if (p.first < 0 && p.second < 0) {
        p.first = abs(p.first), p.second = abs(p.second);
    } else if (p.first < 0 && p.second > 0) {
        p.first = abs(p.first), p.second = -abs(p.second);
    }
    const int g = gcd(p.first, p.second);
    p.first /= g, p.second /= g;
}

pair<int, int> rot(pair<int, int> p) {
    if (p.first == 0 || p.second == 0) return {p.second, p.first};
    if (p.second < 0) return {-p.second, p.first};
    return {p.second, -p.first};
}

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

    int n;
    cin >> n;

    int zeros = 0;
    map<pair<int, int>, pair<int, int>> m;

    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;

        if (a == 0 && b == 0) {
            zeros++;
            continue;
        }

        pair<int, int> sardine = {a, b};
        normalize(sardine);

        const bool rotated = sardine.second < 0 || sardine.first == 0;
        if (rotated)
            m[rot(sardine)].second++;
        else
            m[sardine].first++;
    }

    int ans = 1;

    for (auto &&mi : m) {
        const int l = mi.second.first, r = mi.second.second;
        int now = 0;
        now += modpow(2, l);
        now += modpow(2, r);
        now--;
        now %= MOD;
        (ans *= now) %= MOD;
    }

    ans += zeros;
    ans += MOD - 1;
    ans %= MOD;

    cout << ans << endl;
}


Measured memory_bytes: 19184
def partition(arr, p, r):
    x = arr[r][1]
    i = p - 1
    for j in range(p, r):
        if arr[j][1] <= x:
            i += 1
            arr[i], arr[j] = arr[j], arr[i]

    arr[i+1], arr[r] = arr[r], arr[i+1]
    return i+1

def quick_sort(arr, p, r):
    if p < r:
        q = partition(arr, p, r)
        quick_sort(arr, p, q-1)
        quick_sort(arr, q+1, r)

num = int(input())
card_list = [input().split() for s in range(num)]
card_list = [(t[0], int(t[1])) for t in card_list]
cp_card_list = card_list[:]

quick_sort(card_list, 0, len(card_list) - 1)

is_stable = True
for i in range(len(card_list) - 1):
    if card_list[i][1] == card_list[i+1][1]:
        if cp_card_list.index(card_list[i]) > cp_card_list.index(card_list[i+1]):
            is_stable = False
            break

if is_stable:
    print("Stable")
else:
    print("Not stable")

for t in card_list:
    print(t[0], t[1])




Measured memory_bytes: 38104
#include <stdio.h>

int main(void){


	int n, i, x;
	scanf("%d", &n);
	
	for(i = 1; i <= n; i++){
		if(i % 3 == 0){
			printf(" %d", i);
		}
		else if((i / 10) % 10 == 3 || (i / 100) % 10 == 3 || (i / 1000) % 10 == 3)
			printf(" %d", i);
		else if(i % 10 == 3){
			printf(" %d", i);
		}
	}
	printf("\n");

	return 0;

}

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

int main()
{
  string l = "QWERTASDFGZXCVB";
  string r = "YUIOPHJKLNM";
  bool f[26];

  for (int i = 0; i < l.size(); ++i) f[l[i] - 'A'] = true;
  for (int i = 0; i < r.size(); ++i) f[r[i] - 'A'] = false;

  string str;
  while (cin >> str, str[0] != '#'){
    bool h = f[str[0] - 'a'];
    int cnt = 0;
    for (int i = 1; i < str.size(); ++i){
      if (h != f[str[i] - 'a']) cnt++;
      h = f[str[i] - 'a'];
    }
    cout << cnt << endl;
  }
}

Measured memory_bytes: 3172
#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>P;

const int INF=0x3f3f3f3f;

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

char s[2000][2000];
int d[2000][2000];

int main(){
	int h,w;cin>>h>>w;
	int sx,sy,gx,gy;cin>>sx>>sy>>gx>>gy;
	sx--;sy--;gx--;gy--;
	rep(i,h)cin>>s[i];
	deque<P>que;
	memset(d,0x3f,sizeof(d));
	d[sx][sy]=0;
	que.push_back(P(sx,sy));
	while(!que.empty()){
		P p=que.front();que.pop_front();
		rep(k,4){
			int nx=p.first+dx[k],ny=p.second+dy[k];
			if(0<=nx&&nx<h&&0<=ny&&ny<w&&s[nx][ny]=='.'&&d[nx][ny]>d[p.first][p.second]){
				d[nx][ny]=d[p.first][p.second];
				que.push_front(P(nx,ny));
			}
		}
		for(int dx=-2;dx<=2;dx++){
			for(int dy=-2;dy<=2;dy++){
				int nx=p.first+dx,ny=p.second+dy;
				if(0<=nx&&nx<h&&0<=ny&&ny<w&&s[nx][ny]=='.'&&d[nx][ny]>d[p.first][p.second]+1){
					d[nx][ny]=d[p.first][p.second]+1;
					que.push_back(P(nx,ny));
				}
			}
		}
	}
	if(d[gx][gy]==INF)puts("-1");
	else cout<<d[gx][gy]<<endl;
}

Measured memory_bytes: 21204
import Control.Monad

main = do
    n <- readLn
    ls <- replicateM n getLine
    mapM_ putStrLn (solve ls)

--solve :: [String] -> String
solve = map(solve_ . map (\x -> read x :: Int) . words)

solve_ :: [Int] -> String
solve_ x =
    let m1 = (maximum x) ^2 *2; m2 = sum $ map (^2) x
    in if (m1 == m2) then "YES" else "NO"

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


#define ll long long
#define pb push_back

#define loop(i, a, b) for(int i = (a); i < (b); i++)
#define loopb(i, a, b) for(int i = (a); i > (b); --i)

typedef pair <int,int> ii;
typedef vector <int> vi;
typedef vector <ii> vii;
typedef vector <ll> vl;

const int mod = 1e9+7;
const ll inf = 2e18+5;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  
    ll h;
    cin>>h;
    ll p=1;
    while(p<=h){
        p*=2;
        
    }
    cout<<p-1;    
   
    return 0;
}   

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

#define int long long

using namespace std;

const int mod = 1e9 + 7;
int f[1000001];
int inv[10000001];

int bp (int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return res;
}

int C (int n, int k) {
	if (k > n) return 0;
	return (f[n] * ((inv[k] * inv[n - k]) % mod)) % mod;
} 

 main () {
 	int n;
	cin >> n;
	f[0] = 1;
	inv[0] = 1;
	for (int i = 1;i <= n;i ++) {
		f[i] = (f[i - 1] * i) % mod;
		inv[i] = bp (f[i], mod - 2);
	} 
	int ans = 0;
	int last = 0 ;
	for (int i = (n + 1) / 2;i < n;i ++) {
		int k = i;
		int cur = C (k - 1, n - k - 1);
	//	cout << cur << ' ' ;
		cur = (cur * f[k]) % mod;
		cur = (cur * f[n - k - 1]) % mod;
		int cur1 = cur;
		cur = (cur - last + mod) % mod;
	//	cout << cur << ' ' ;
		last = cur1;
		ans = (ans + ((cur * k) % mod)) % mod;
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 16512
n, k = list(map(int, input().split(' ')))
r, s, p = list(map(int, input().split(' ')))
d = dict(r=p,
         s=r,
         p=s)
ttt = input()
ans = 0
prevs = []
for i in range(n):
    if i < k or ttt[i] != prevs[i-k]:
        ans += d[ttt[i]]
        prevs.append(ttt[i])
    else:
        prevs.append('hoge')
print(ans)


Measured memory_bytes: 79548
#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

const int MAXN = 100005;

long long n, m, cnt, a, b, e;
int bio[MAXN], c[MAXN], moze[MAXN], siz[MAXN];
vector <int> v[MAXN];

void dfs (int x, int boja) {
	if (bio[x]) return;
	bio[x] = cnt;
	siz[cnt]++;
	c[x] = boja;
	for (int i=0; i<v[x].size(); i++) {
		int sus = v[x] [i];
		if (c[sus] == c[x]) moze[cnt] = 1;
		dfs(sus, -boja);
	}
}

int main () {
	cin >> n >> m;
	for (int i=0; i<m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);
		v[a].push_back(b);
		v[b].push_back(a);
	}
	for (int i=1; i<=n; i++) {
		if (!bio[i]) {
			cnt++;
			dfs(i, 1);
		}
	}
	for (int i=1; i<=cnt; i++) {
		if (siz[i] == 1) {
			e++;
		} else {
			if (moze[i]) a++; else b++;
		}
	}
	cout << a*a + 2*a*b + 2*b*b + 2*n*e - e*e;
	return 0;
}


Measured memory_bytes: 10368
def ddcc2020_qual_a
    x,y = gets.chomp.split(/\s/).map{|x| x.to_i}

    yen = 0
    yen += 100000 if x == 3
    yen += 200000 if x == 2
    yen += 300000 if x == 1
    yen += 100000 if y == 3
    yen += 200000 if y == 2
    yen += 300000 if y == 1
    yen += 400000 if x == 1 and y == 1

    puts yen
end

ddcc2020_qual_a

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll N, A, B;
vector<ll> h;

ll binary_search(ll l, ll r){
    map<ll, bool> tried;
    tried[l] = false;
    tried[r] = true;

    while (true){
        ll mid = (r + l) / 2;

        vector<ll> tmp(h);
        rep(i, tmp.size()){
            tmp.at(i) -= mid * B;
        }
        ll cnt = 0;
        rep(i, tmp.size()){
            if (tmp.at(i) >= 0){
                cnt += ceil((double)(tmp.at(i)) / (A - B));
            }
        }
        // cout << l << " " << mid << " " << r << " " << cnt << endl;

        if (cnt <= mid){
            r = mid;
            tried[mid] = true;
        } else {
            l = mid;
            tried[mid] = false;
        }

        if (tried.find(mid+1) != tried.end() && tried[mid + 1] && !tried[mid]){
            return mid + 1;
        }
        if (tried.find(mid-1) != tried.end() && !tried[mid - 1] && tried[mid]){
            return mid;
        }
    }
}

    // while l < r:
    //     mid = l + (r - l) // 2
    //     if arr[mid] == x:
    //         return mid
    //     elif arr[mid] > x:
    //         r = mid
    //     else:
    //         l = mid + 1
    // return -1

void Main() {
    // 出来るor出来ないの単調性に注目すると二分探索で済む
    cin >> N >> A >> B;
    rep(i, N){ll tmp; cin >> tmp; h.push_back(tmp);}
    cout << binary_search(0, 1000000000) << endl;
}

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


Measured memory_bytes: 1908
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

// increment
#define rep_n(_1, _2, _3, NAME, ...) NAME
#define rep_2(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define rep_3(i, initial, n) for (int(i) = (int)(initial); (i) < (int)(n); ++(i))
#define rep(...) rep_n(__VA_ARGS__, rep_3, rep_2, rep_1)(__VA_ARGS__)
// decrement
#define rep_r_n(_1, _2, _3, NAME, ...) NAME
#define rep_r_2(i, n) for (int(i) = (int)(n); (i) > 0; (i)--)
#define rep_r_3(i, initial, n) for (int(i) = (int)(initial); (i) > (int)(n); (i)--)
#define rep_r(...) rep_r_n(__VA_ARGS__, rep_r_3, rep_r_2)(__VA_ARGS__)
// define type definition
using namespace std;
typedef long long ll;
using mpint = boost::multiprecision::cpp_int; // 多倍長整数（任意精度）
typedef pair<int, int> P;

// define construct variable
const double EPS = numeric_limits<double>::epsilon();
// 10^9 + 7
const ll MOD = ll(1e9)+7;
const ll INF = ll(1e10);
const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";

// define methods
// 最大公約数
ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }
// char -> int
int ctoi(char c) { return (int)(c - '0'); }
// 素因数分解
vector< pair<ll, ll> > prime_factorize(ll n) {
  vector< pair<ll, ll> > p;
  for (ll i = 2; i * i <= n; i++) {
    if ((n % i) != 0) continue;
    p.push_back(make_pair(i, 0));
    while ((n % i) == 0) {
      n /= i;
      p[i].second += 1;
    }
    p.push_back(make_pair(n, 0));
  }

  if (n != 1) p.push_back(make_pair(n, 1));
  return p;
}
// 組み合わせの整合性を保つ（後ほど解析する）
ll modinv(ll a) {
	ll b = MOD, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= MOD;
	if (u < 0) { u += MOD; }
	return u;
}
// 順列
ll permutation(const ll n, const ll m) {
	ll result = 1;
	for (ll i = n; i > n - m; i--) { result = (result * i) % MOD; }
	return result;
}

// 繰り返し n 乗法
ll repeat_square(const ll n, const ll x) {
	if (x == 0) return 1;
	ll res = repeat_square(n, x/2);
	res = res * res % MOD;
	if ((x % 2) == 1) res = res * n % MOD;
  return res;
}
// 組み合わせ
ll combination(const ll n, const ll a) {
	ll x = 1, y = 1;
  for(ll i = 0; i < a; i++){
    x = x * (n-i) % MOD;
    y = y * (i+1) % MOD;
  }
  return x * repeat_square(y, MOD-2) % MOD;
}

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

	ll count = 0, ans = 0;
	for (ll i = 1; i <= X; i++) {
		ans += i;
		count += 1;
		if (ans >= X) break;
	}

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


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <stack>
#include <queue>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main()
{
    int n;
    cin >> n;
    vector<int> t(n+1), x(n+1), y(n+1);
    t[0] = 0, x[0] = 0, y[0] = 0;
    for (int i = 1; i <= n; ++i) cin >> t[i] >> x[i] >> y[i];
    for (int i = 0; i < n; ++i)
    {
        int dt = t[i+1] - t[i];
        int dr = abs(x[i+1] - x[i]) + abs(y[i+1] - y[i]);
        if ((dt & 1) == (dr & 1) && dt >= dr)
        {
            continue;
        }
        else
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}


Measured memory_bytes: 1408
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <math.h>
#include <iomanip>
using namespace std;
typedef long long int ll;
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s, a = "CODEFESTIVAL2016";
	ll ans = 0;
	cin >> s;
	for (int i = 0; i < s.size(); i++)if (s[i] != a[i])ans++;
	cout << ans << endl;
	return 0;
}

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

using namespace std;

long f(long b, long n) {
	long result = 0;
	while (n >= b) {
		result += n % b;
		n /= b;
	}
	return result + n;
}

int main(int argc, char *argv[]) {
	long n, s;
	cin >> n >> s;
	long sqrtn = sqrt(n);

	if (n < s) {
		printf("-1\n");
		return 0;
	}
	if (n == s) {
		cout << n + 1 << endl;
		return 0;
	}

	for (long b = 2; b <= sqrtn; b++) {
		if (f(b, n) == s) {
			cout << b << endl;
			return 0;
		}
	}

	for (long p = sqrtn; p > 0; p--) {
		long b = (n - s) / p + 1;
		if (b < 2 || f(b, n) != s) continue;
		cout << (n - s) / p + 1 << endl;
		return 0;
	}

	printf("-1\n");

	return 0;
}

Measured memory_bytes: 256
from bisect import bisect_left, bisect_right

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)] + [float('inf'), -float('inf')]
T = [int(input()) for _ in range(B)] + [float('inf'), -float('inf')]

S.sort()
T.sort()

ans = []
for _ in range(Q):
    x = int(input())

    midS = bisect_right(S, x)
    midT = bisect_right(T, x)

    lS = S[midS - 1]
    rS = S[midS]
    lT = T[midT - 1]
    rT = T[midT]

    dist = min(
        max(rS, rT) - x,
        x - min(lS, lT),
        abs(rS - lT) + min(rS - x, x - lT),
        abs(rT - lS) + min(rT - x, x - lS)
    )

    ans.append(dist)

print(*ans, sep='\n')

Measured memory_bytes: 17968
#include<iostream>
#include<string>
#include<vector>
#include<string.h>
#include<stdio.h>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{int n,a,b;
 cin>>n>>a>>b;
 int x=n-a;
 int ans=x%b;
 cout<<ans; 
 system("pause");
 return 0;
}

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

template <size_t T>
bool prevSubset(bitset<T>& bs, const bitset<T>& mask)
{
    if(bs.none())
        return false;
    bs = bs.to_ulong() - 1ull;
    bs &= mask;
    return true;
}

const long long INF = LLONG_MAX / 2;

int main()
{
    int n;
    cin >> n;
    vector<vector<int> > a(n, vector<int>(n));
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cin >> a[i][j];
        }
    }

    vector<long long> sum(1<<n, 0);
    for(int i=0; i<(1<<n); ++i){
        bitset<32> bs(i);
        for(int j=0; j<n; ++j){
            for(int k=0; k<j; ++k){
                if(bs[j] && bs[k])
                    sum[i] += a[j][k];
            }
        }
    }

    vector<long long> dp(1<<n, -INF);
    dp.back() = 0;
    for(int i=(1<<n)-1; i>=0; --i){
        bitset<32> bs(i), mask(i);
        do{
            bitset<32> bs2 = mask ^ bs;
            dp[bs2.to_ulong()] = max(dp[bs2.to_ulong()], dp[i] + sum[bs.to_ulong()]);
        }while(prevSubset(bs, mask));
    }
    cout << dp[0] << endl;

    return 0;
}


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

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

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

  int n, k;
  cin >> n >> k;
  vector<int64> xs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
  }

  for (int i = k; i < n; i++) {
    if (xs[i] > xs[i - k]) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Measured memory_bytes: 4780
#include <iostream>
#include <limits>
#include <cfenv>
#include <cmath>
#include <algorithm>
#include <array>
#include <bitset>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <tuple>
#include <queue>
#include <vector>
#include <cmath>
#include <random>
#include <math.h>
#include <list>
#include <random>
#include <functional>


#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) for(int (i) = (n) - 1; (i) >= 0; --(i))
#define ALL(TheArray) TheArray.begin(), TheArray.end()

using lli = long long int;
using pii = std::pair<int, int>;

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


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


std::vector<std::vector<int>> Edge;
std::vector<int> Dist, Parent, NChildren, Out;
std::map<std::pair<int, int>, char> e;

int main(void){
    int n, m; scanf("%d%d", &n, &m);
    if(m & 1){puts("-1"); return 0;}
    Edge.resize(n);
    REP(i, m){
        int a, b; scanf("%d%d", &a, &b); a--; b--;
        Edge[a].push_back(b); Edge[b].push_back(a);
        if(a > b) std::swap(a, b);
        e[{a, b}] = 0;
    }
    Dist.assign(n, -1); Parent.resize(n); Out.assign(n, 0); NChildren.assign(n, 0);
    std::queue<int> Q;
    int root = 0;
    Q.push(root); Dist[root] = 0; Parent[root] = -1;
    while(not Q.empty()){
        int v = Q.front(); Q.pop();
        for(int nv : Edge[v]){
            if(Dist[nv] == -1){
                Dist[nv] = Dist[v] + 1; Parent[nv] = v; NChildren[v]++;
                Q.push(nv);

                const pii p = (v < nv) ? pii(v, nv) : pii(nv, v); e[p] = -1;
            }
            else if(v < nv){
                auto itr = e.find({v, nv});
                if(itr -> second == 0){
                    Out[v]++; itr->second = 1;
                }
            }
        }
    }
    std::stack<int> Stk;
    REP(i, n) if(NChildren[i] == 0) Stk.push(i);
    while(not Stk.empty()){
        int v = Stk.top(); Stk.pop();
        int pv = Parent[v];
        const std::pair<int, int> p = (v < pv) ? std::pair<int, int>(v, pv) : std::pair<int, int>(pv, v);
        auto itr = e.find(p);
        if(Out[v] & 1){
            Out[v]++; itr->second = (v < pv) ? 1 : 2;
        }
        else{
            Out[pv]++; itr->second = (v < pv) ? 2 : 1;
        }
        if((--NChildren[pv]) == 0 and pv != 0) Stk.push(pv);
    }
    for(auto& itr : e){
        int a = itr.first.first + 1, b = itr.first.second + 1;
        if(itr.second == 1) printf("%d %d\n", a, b);
        else printf("%d %d\n", b, a);
    }



    

    return 0;
}

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

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

using namespace std;

int n;
int a[2][100];
int dp[2][100];

int main(){
    cin >> n;
    rep(i, 2){
        rep(j, n){
            cin >> a[i][j];
        }
    }
    
    dp[0][0] = a[0][0];
    dp[1][0] = a[0][0] + a[1][0];
    rep(i, 2){
        rep(j, n - 1){
            if (i > 0) {
                dp[i][j + 1] = max(dp[i][j], dp[i - 1][j + 1]);
            } else {
                dp[i][j + 1] = dp[i][j];
            }
            dp[i][j + 1] += a[i][j + 1];
        }
    }
    cout << dp[1][n - 1] << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
	int ma=0;
	char s[20];
	cin>>s;
	int l=strlen(s),cou=0;
	for(int i=0;i<l;i++)
	{
		if(s[i]=='A'||s[i]=='T'||s[i]=='G'||s[i]=='C')
		cou++;
		else
		{
			ma=max(ma,cou);
			cou=0;
		}
		if(i==l-1)
		ma=max(ma,cou);
	}
	cout<<ma<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main(){
  double a,b,c,PI=3.1415926535;
  cin >> a >> b >> c;
  printf("%.5f\n",a*b*sin(c*PI/180)/2);
  printf("%.5f\n",sqrt(a*a+b*b-2*a*b*cos(c*PI/180))+a+b);
  printf("%.5f\n",b*sin(c*PI/180));
  return 0;
}


Measured memory_bytes: 3320
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):

    d = defaultdict(list)
    for i, a in enumerate(A):
        d[a].append(i)

    ni = []
    for i, a in enumerate(A):
         ni.append(d[a][bisect.bisect_right(d[a], i) % len(d[a])])

    i = 0
    k = 1
    while True:
        j = ni[i]
        if j <= i:
            k +=1
        i = (j + 1) % N

        if i == 0:
            break

    error_print(k, K % k)
    s = []
    ss = set()

    i = 0
    K %= k
    k = 0
    while k < (K-1):
        j = ni[i % N]
        if j <= i:
            k += 1
        i = j + 1

    i += k * N
    s = []
    ss = set()
    for l in range(i, N*K):
        m = l % N
        a = A[m]
        if a in ss:
            while True:
                b = s.pop()
                ss.remove(b)
                if b == a:
                    break
        else:
            s.append(a)
            ss.add(a)
    return ' '.join(map(str, s))


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))

    # N = 2*(10**5)
    # # N = 10
    # K = 10**12
    # A = [random.randint(1, N) for _ in range(N)]
    # # print(A)
    # print(slv(N, K, A))


if __name__ == '__main__':
    main()


Measured memory_bytes: 126816
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <sstream>

using namespace std;

int main(){
    int N;
    cin >> N;
    vector<int> w;
    w.reserve(N);
 
    for (size_t i = 0; i < N; i++)
    {
        int value = 0;
        cin >> value;
        w.push_back(value);
    }
        

    int min = INT32_MAX;
    for (size_t t = 0; t < N; t++)
    {
        int f = accumulate(w.begin(), w.begin()+t+1, 0);
        int s = accumulate(w.begin()+t+1, w.end(), 0);
        int diff = abs(f-s);

        if(min > diff)
        {
            min = diff;
        }
    }
    
    cout << min << endl;
    return 0;
}

Measured memory_bytes: 256
tasks = gets.to_i.times.map { gets.split.map(&:to_i) }
tasks.sort { |(a1, b1),(a2, b2)| b1 <=> b2 }.inject(0) do |s, (a, b)|
  t = s + a
  if t <= b
    t
  else
    puts 'No'
    exit
  end
end
puts 'Yes'

Measured memory_bytes: 19420
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;

int main() {
    string t, s;
    cin >> s >> t;
    int nt = t.size(), ns = s.size();
    if(nt > ns) return 0;
    
    vi sen;
    rep(i, nt-1) {
        if(t.substr(i+1, nt-1-i) == t.substr(0, nt-1-i)) sen.push_back(i+1);
    }

    vi ans;
    rep(i, ns-nt+1) {
        if(s[i] == t[0]) {
            if(s.substr(i, nt) == t) {
                ans.push_back(i);
                while(true) {
                    bool x = false;
                    rep(j, sen.size()) {
                        if(i+nt+sen[j] > ns) break;
                        if(s.substr(i+nt, sen[j]) == t.substr(nt-sen[j], sen[j])) {
                            x = true;
                            i += sen[j];
                            ans.push_back(i);
                            break;
                        }
                    }
                    if(!x) break;
                }
                i += nt;
            }
        }
    }


    rep(i, ans.size()) {
        cout << ans[i] << '\n';
    }
}


Measured memory_bytes: 8444
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,-1,0,1},dy={1,0,-1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}





int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;cin >> n;
	vector<ll> a(n+2);
	rep(i,n) cin >> a.at(i);
	a.at(n)=a.at(0);
	a.at(n+1)=a.at(1);
	vector<ll> ans(n);
	rep(i,n){
		if(i%2==0) ans.at(0)+=a.at(i);
		else ans.at(0)-=a.at(i);
	}
	rep(i,n-1){
		ans.at(i+1)=a.at(i)*2-ans.at(i);
	}
	rep(i,n) cout << ans.at(i) << endl;
}


Measured memory_bytes: 2944
import sys

def main(inp):
    S=list(next(inp))
    
    tmpans=0
    ans=0
    for i in range(len(S)):
        if(S[i]=='A' or S[i]=='T' or S[i]=='C' or S[i]=='G'):
            tmpans += 1
        else: 
            ans=max(ans,tmpans)
            tmpans=0
            
    return max(ans,tmpans)
      
print(main(sys.stdin))

Measured memory_bytes: 3060
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
#define fastIO_ ios_base::sync_with_stdio(false); cin.tie(false); cout.tie(false);
int main()
{
    ll a, b, s=0;
    cin>>a>>b;
    for (ll i=0, d; i<b; i++)
    {
        cin>>d;
        s += d;
    }
    s-a>= 0? cout<<"Yes\n": cout<<"No\n";
    return 0;
}

Measured memory_bytes: 768
import math
atan = lambda x: math.degrees(math.atan(x))
a, b, x = map(int, input().split())
s = a ** 2 * b
if x >= s / 2:
    theta = atan(2 * (s - x) / (a ** 3))
else:
    theta1 = atan(a * b ** 2 / 2 / x)
    x = min(x, a ** 2 * b / 2 - x)
    delta = (3 * x / b) ** 0.5
    delta = min(delta, 2 ** 0.5 - delta)
    theta2 = atan(b / delta)
    theta = max(theta1, theta2)
print(theta)

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

// マクロ //
#define rep(i,N) for(int i = 0; i < N; i++)
#define all(x) x.begin(), x.end()
#define sort(x) sort(all(x))
#define uniq(x) x.erase(unique(all(x)), x.end())
#define vsum(x) accumulate(all(x), 0)
#define cou(x) cout << x << endl
#define y() cout << "Yes" << endl
#define n() cout << "No" << endl
#define Y() cout << "YES" << endl
#define N() cout << "NO" << endl
#define x2(x) (x) * (x)

// 型エイリアス //
using lint = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vli = vector<lint>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvc = vector<vc>;
using vpii = vector<pii>;
using msi = map<string, int>;

// 関数 //
template<class T> lint gcd(T a, T b) {
    int t;
    while (b != 0) {
        t = a % b;
        a = b;
        b = t;
    }
    return a;
}

template<class T> lint lcm(T a, T b) {
    return a * b / gcd(a, b);
}

double distance(pii a, pii b) {
    double dist;
    dist = sqrt(x2(a.first - b.first) + x2(a.second - b.second));
    return dist;
}

lint perm(int a) {
    lint perm = 1;
    for (int i = a; i >= 1; i--) {
        perm *= i;
    }
    return perm;
}

lint combination(int n, int m) {
    long double c = 1;
    for (int i = n, k = 1; i > m; i--, k++) {
        c *= i;
        c /= k;
    }
    return (lint)c;
}

template<class T, class X> inline bool chmin(T& a, X b) {
    if (a > b) { 
        a = b;
        return true; 
    }
    return false;
}

template<class T, class X> inline bool chmax(T& a, X b) {
    if (a < b) { 
        a = b; 
        return true; 
    }
    return false;
}

// 定数 //
#define pi acos(-1)
#define mod 1000000007
#define MOD 998244353

// End of Template //



int main() {

    cin.tie(0);
    ios::sync_with_stdio(false);


    int N, K;
    cin >> N >> K;


    vi height(N);
    rep(i, N) cin >> height[i];
    vli dp(N, 1145141919810);
    dp[0] = 0;

    rep(i, N) {
        for (int j = i + 1; j < N && j - i <= K; j++) {
            chmin(dp[j], dp[i] + abs(height[j] - height[i]));
        }
    }
    cou(dp[N - 1]);
}

Measured memory_bytes: 1408
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = 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 PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;

void init() {
    
}
const int MAX = 1510;
int h[MAX], w[MAX];
int hcnt[MAX * 1000], wcnt[MAX * 1000];
void solve(int n, int m) {
    memset(hcnt, 0, sizeof(hcnt));
    memset(wcnt, 0, sizeof(wcnt));
    for (int i = 0; i < n; i++) {
        scanf("%d", h + i);
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", w + i);
    }
    for (int i = 0; i < n; i++) {
        int sum = h[i];
        hcnt[sum]++;
        for (int j = i + 1; j < n; j++) {
            sum += h[j];
            hcnt[sum]++;
        }
    }
    for (int i = 0; i < m; i++) {
        int sum = w[i];
        wcnt[sum]++;
        for (int j = i + 1; j < m; j++) {
            sum += w[j];
            wcnt[sum]++;
        }
    }
    int ans = 0;
    for (int i = 0; i < MAX * 1000; i++) {
        ans += hcnt[i] * wcnt[i];
    }
    printf("%d\n", ans);
}

signed main(void) {
    LL n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    init();
    while (cin >> n >> m, n) {
        solve(n, m);
        //return 0;
    }
}

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

int main(){
  int a, b, c;
  cin >> a >> b >> c;
  cout << max(0,b+c-a) << endl;
  return 0;
}

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

using namespace std;

vector< vector< pair<int,int> > > g(100005);
int ans[100005];

void dfs(int v, int c, int p = -1) {
    ans[v] = c;
    for(auto u: g[v]) {
        if(u.first == p)  continue;
        dfs(u.first, u.second^c, v);
    }
    return;
}

int main(void) {
    int N;
    cin >> N;
    for(int i=0; i<N-1; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        --u; --v;
        g[u].emplace_back(v, w%2);
        g[v].emplace_back(u, w%2);
    }

    dfs(0, 0);

    for(int i=0; i<N; i++) {
        cout << ans[i] << endl;
    }
    return 0;
}


Measured memory_bytes: 8320
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <cmath>

using namespace std;

#define y1 roman_kaban
#define rank oryshych_konb
//const int mod = int(1e9) + 7;

const int MX = 20;
int a[MX][MX];

int main()
{
    //freopen("in.txt","r", stdin);
    ios_base::sync_with_stdio(false);
    int h, w;
    cin >> h >> w;
    for(int i = 0; i < h; i++){
        string s;
        cin >> s;
        for(int j = 0; j < w; j++)
            if(s[j] == '#') a[i][j] = 1;
    }

    int x = 0;
    int y = 0;
    while(true){
        a[x][y] = 0;
        if(a[x + 1][y]) {x++;} else
        if(a[x][y + 1]) {y++;} else
            break;
    }
    if(x != h - 1 || y != w - 1) {puts("Impossible"); return 0;}
    for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++)
    if(a[i][j]) {puts("Impossible"); return 0;}
    puts("Possible");
    return 0;
}


Measured memory_bytes: 764
#include<iostream>
    #include<string>
    using namespace std;
    int main() {

        string c3;
        cin >> c3;
        int count = 0;
        if (c3[0] == '1') ++count;
        if (c3[1] == '1') ++count;
        if (c3[2] == '1') ++count;
        cout << count << endl;
    }

Measured memory_bytes: 256
// Last Change: 04/06/2019 16:13:16.
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

namespace NS {
void LoopUntilZeroInpput() {
  int hogegegege = 0;
  while (cin >> hogegegege && hogegegege != 0) {
  }
}
} // namespace NS

int main() {
  int N, K;
  cin >> N >> K;
  N %= K;
  if (N > 0) {
    cout << 1 << endl;
  } else {
    cout << 0 << endl;
  }

  //NS::LoopUntilZeroInpput();
}


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

struct aUnionFind {
	vector<pair<int, long long int>> data;
	aUnionFind(int size) : data(size, make_pair(-1, 0)) { }

	//y is w bigger than x
	bool unionSet(const int x, const int y, const long long int w) {
		const int rx(root(x).first), ry(root(y).first);
		if (rx != ry) {
			data[rx].first += data[ry].first; data[ry].first = rx;
			data[ry].second = w + data[x].second - data[y].second;
			return true;
		}
		else {
			return root(x).second + w == root(y).second;
		}
	}
	bool findSet(const int x, const int y) {
		return root(x) == root(y);
	}

	//x is belong to first
	//x is second bigger than first
	pair<int, long long int> root(const int x) {
		if (data[x].first < 0) {
			return make_pair(x, 0);
		}
		else {
			pair<int, long long int>ndata;
			ndata.first = root(data[x].first).first;
			ndata.second = data[x].second + root(data[x].first).second;
			return data[x] = ndata;
		}
	}
	int size(const int x) {
		return -data[root(x).first].first;
	}
};

struct query {
	int r;
	int c;
	int a;
};

int main() {
	{
		int R, C; cin >> R >> C;
		int N; cin >> N;

		vector<vector<pair<int, int>>>rs(R);
		vector<query>querys;
		for (int i = 0; i < N; ++i) {
			int r, c, a; cin >> r >> c >> a;
			r--; c--;
			querys.push_back(query{ r,c,a });
			rs[r].emplace_back(c, a);
		}
		bool ok = true;
		aUnionFind uf(C);
		{
			for (auto ar : rs) {
				if (ar.size() >= 2) {
					for (int i = 0; i < ar.size() - 1; ++i) {
						auto l(uf.root(ar[i].first));
						auto r(uf.root(ar[i + 1].first));
						if (l.first == r.first) {
							if (l.second - ar[i].second != r.second - ar[i + 1].second)ok = false;
						}
						else {
							uf.unionSet(ar[i].first, ar[i + 1].first, ar[i + 1].second - ar[i].second);
						}
					}
				}
			}
		}
		if (ok) {
			map<int, vector<int>>rootmp;
			for (int i = 0; i < N; ++i) {
				auto arootp(uf.root(querys[i].c));
				rootmp[arootp.first].push_back(i);
			}
			for (auto rm : rootmp) {
				int amin = 2e9 + 1;
				for (auto r : rm.second) {
					auto p(uf.root(querys[r].c));
					amin = min(amin, int(p.second));
				}
				for (auto r : rm.second) {
					auto p(uf.root(querys[r].c));
					int num = querys[r].a - p.second + amin;
					if (num < 0) {
						ok = false;
					}
				}
			}
		}
		if (ok)cout << "Yes" << endl;
		else cout << "No" << endl;
	}

	return 0;
}

Measured memory_bytes: 19380
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

S = str(input())

ans_w = 0
cnt = [0,0] # W,B
left = S[0]
if left == "B":
    cnt[1] += 1
else:
    cnt[0] += 1
for i in range(1,len(S)):
    if left == S[i]:
        pass
    else:
        left = S[i]
        if left == "B":
            cnt[1] += 1
        else:
            cnt[0] += 1

if cnt[0] == cnt[1]:
    ans = (cnt[0] - 1) * 2 + 1

else:
    ans = min(cnt)*2
print(ans)


Measured memory_bytes: 5712
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;

int main(){
	int N,M,X,Y;
  	cin>>N>>M>>X>>Y;
  	int x[100],y[100];
	for(int i=0;i<N;++i)cin>>x[i];
  	for(int i=0;i<M;++i)cin>>y[i];
  	sort(x,x+N,greater<int>());
  	sort(y,y+M);
  	if(x[0]>=y[0]){cout<<"War"<<endl;return 0;}
  	else{
      for(int i=x[0]+1;i<=y[0];++i)if(i>X&&i<=Y){cout<<"No War"<<endl;return 0;}
  								else {cout<<"War"<<endl;return 0;}
    }
  	return 0;
}


Measured memory_bytes: 764
#include <stdio.h>

int divneg2(int n) {
    int q = n / (-2);
    if (n < (-2) * q)
        q++;
    return q;
}

int main(void) {
    int n;
    char bit[100];
    int max_bit;

    scanf("%d", &n);

    for (int i = 0; ; i++) {
        int q = divneg2(n);
        int r = n - (-2) * q;

        if (r == 0) {
            bit[i] = '0';
        }
        else {
            bit[i] = '1';
        }

        if (q == 0) {
            max_bit = i;
            break;
        }

        n = q;
    }

    for (int i = max_bit; i >= 0; i--) {
        printf("%c", bit[i]);
    }
    printf("\n");

    return 0;
}


Measured memory_bytes: 128
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

vector<vector<int>> V;
map<int, int> pre;
map<int, int> post;
map<int, int> cc;
int t = 1;
int ccid = 1;

int _DFS(int v, vector<bool> &visited) {
  pre[v] = t;
  cc[v] = ccid;
  ++t;
  for (int out_nbr_id : V[v]) {
    if (!visited[out_nbr_id]) {
      visited[out_nbr_id] = true;
      _DFS(out_nbr_id, visited);
    }
  }
  post[v] = t;
  ++t;
}

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

  V = vector<vector<int>>(n);

  int s, t;
  for (int i = 0; i < m; ++i) {
    cin >> s >> t;
    V[s].push_back(t);
    V[t].push_back(s);
  }

  vector<bool> visited(n, false);

  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      ++ccid;
      visited[i] = true;
      _DFS(i, visited);
    }
  }

  cin >> s; // ignore # of queries

  while (cin >> s >> t) {
    if (cc[s] == cc[t]) {
      cout << "yes\n";
    } else {
      cout << "no\n";
    }
  }
}



Measured memory_bytes: 31836
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
using namespace std;
#define INF 0x3f3f3f3f
//#define INF 1100000000000000000LL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=1050;
//int arr[SIZE]={};
//ここまでテンプレ
int main(){
	int N,K;
	cin>>N>>K;
	vint A,B;
	for(int i=0;i<N;i++){
		int a,b;
		cin>>a>>b;
		A.pb(a);
		B.pb(b);
	}
	bool bit[32]={};
	for(int i=0;i<32;i++){
		bit[i]=(!(!(K&(1<<i))));
	}
	ll ans=0,sum=0;
	for(int i=0;i<N;i++){
		bool flag=0;
		for(int j=0;j<32;j++){
			if(!bit[j] && bit[j]!=(A[i]&(1<<j)))
				flag=1;
		}
		if(!flag)
			sum+=B[i];
	}
	ans=sum;
	for(int i=0;i<32;i++){
		if(bit[i]){
			sum=0;
			for(int j=0;j<N;j++){
				bool flag=0;
				for(int k=31;k>=0;k--){
					if(k<i){
					}
					else if(k==i){
						if(A[j]&(1<<k))
							flag=1;
					}
					else{
						if(!bit[k] && bit[k]!=(A[j]&(1<<k)))
							flag=1;
					}
				}
				if(!flag){
					sum+=B[j];
				}
			}
			ans=max(ans,sum);
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 1276
def abc065_b():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    now = 1
    count = 0
    for i in range(n):
        if now == 2:
            print(count)
            return None
        now = a[now - 1]
        count += 1
    print(-1)


abc065_b()

Measured memory_bytes: 7320
#include "bits/stdc++.h"
// Custom Header {{{
#define ALL(x) x.begin(), x.end()
#define rep(i, s, n)      for(int i(s); i < int(n); ++i)
#define endl '\n'
#ifndef YDK
#define eprintf(...)
#endif
using namespace std;
using i64 = long long;
using pii = pair<i64, i64>;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr const int INF  = 0x3f3f3f3f;
constexpr const i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr const int MOD  = int(1e9) + 7;
// }}}

// Library {{{
constexpr long long modPow(long long n, long long p, const int mod = int(1e9)+7)
{
    long long ret = 1;
    while(p) {
        if (p & 1) ret = (ret * n) % mod;
        n = (n * n) % mod;
        p >>= 1;
    }
    return ret;
}

template<int MOD> class ModInt {
private:
    long long n;
    using T = ModInt<MOD>;
public:
    static constexpr const int mod = MOD;
    constexpr inline ModInt(long long n_) : n(n_ % MOD) {}
    template<class Int> constexpr explicit inline operator Int() const { return n; }
    friend ostream& operator<<(ostream &os, T m) { os << m.n; return os; }
    constexpr inline T operator~() const { return modPow(n, MOD-2, MOD); }
    constexpr inline T& operator+=(T o) { n = (n + o.n) % MOD; return *this; }
    constexpr inline T& operator-=(T o) { n = (n - o.n + MOD) % MOD; return *this; }
    constexpr inline T& operator*=(T o) { n = (n * o.n) % MOD; return *this; }
    constexpr inline T& operator/=(T o) { n = (n * (~o).n) % MOD; return *this; }
};
using Mint = ModInt<int(1e9)+7>;
Mint operator+(Mint a, Mint b) { return Mint(a) += b; }
Mint operator-(Mint a, Mint b) { return Mint(a) -= b; }
Mint operator*(Mint a, Mint b) { return Mint(a) *= b; }
Mint operator/(Mint a, Mint b) { return Mint(a) /= b; }

template<size_t N, int MOD=int(1e9)+7> struct Factrial {
    using Mint = ModInt<MOD>;
    long long a[N+1];

#ifdef YDK
    inline Factrial() : a{1} { for (int i = 1; i <= N; ++i) a[i] = (a[i-1] * i) % MOD; }
#else
    constexpr inline Factrial() : a{1} { for (int i = 1; i <= N; ++i) a[i] = (a[i-1] * i) % MOD; }
#endif

    constexpr inline Mint operator[] (size_t i) const { return a[i]; }
};

Factrial<100010> fact;

Mint nCr(int n, int r) { return (r < 0 || n < r) ? 0 : (fact[n] / (fact[r] * fact[n-r])); }

Mint nPr(int n, int r) { return (r < 0 || n < r) ? 0 : (fact[n] / fact[r]); }

Mint nHr(int n, int r) { return nCr(n+r-1, r); }
// }}}


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

    if (N == M) {
        cout << (2 * fact[N] * fact[M]) << endl;
    } else if (abs(N-M) == 1) {
        cout << (fact[N] * fact[M]) << endl;
    } else {
        cout << (0) << endl;
    }
    return 0;
}


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

const int N = 1e6 + 5;

#define st first
#define nd second
#define make(a,b) make_pair(a,b)

typedef pair<int,int> pun;
typedef long long ll;

vector<int> G[N];
bool vis[N];


int dfs(int x) {
	if (vis[x]) return -1;
	vis[x] = true;
	int res = 0;
	for (int y: G[x]) {
		int a = dfs(y);
		res ^= (a+1);
	}
	return res;
}

int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i < n; i ++) {
		int x, y;
		scanf("%d%d", &x, &y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	printf("%s\n", dfs(1) == 0 ? "Bob" : "Alice");
}


Measured memory_bytes: 30080
N, K = map(int, input().split())
a = list(map(int, input().split()))
mod = 10**9+7

dp = [[0 for _ in range(K+1)] for _ in range(N+1)]
dp[0][0] = 1
dp2 = [[0 for _ in range(K+2)] for _ in range(N)]
for i in range(N):
    for j in range(K+1):
        dp2[i][j+1] = (dp2[i][j] + dp[i][j]) % mod
    for j in range(K+1):
        dp[i+1][j] = (dp[i+1][j] + (dp2[i][j+1] - dp2[i][max(j - a[i], 0)])%mod) % mod
print(dp[-1][-1])


Measured memory_bytes: 259720
#include <math.h>
#include <time.h>
#include <ctype.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include<iomanip>

#define mod 1000000007
#define ll long long int
#define pb(x) push_back(x)
#define MP(x,y) make_pair(x,y)

using namespace std;

ll power(ll a, ll b, ll m)
{
	if (b == 0)
		return(1);
	ll sol = power(a, b / 2, m);
	sol = (sol * sol) % m;
	if (b % 2 == 1)
		sol = (sol * a) % m;
	return(sol);
}

ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	else
	{
		return gcd(b, a % b);
	}
}
ll wt[101],val[101],dp[101][100001];
void solve()
{
	ll W;
	int n;
	cin >> n >> W;
	for (int i = 1; i <= n; i++)
	{
		cin >> wt[i] >> val[i];
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= W; j++)
		{
			if (wt[i] > j)
			{
				dp[i][j] = dp[i-1][j];
			}
			else
			{
				dp[i][j] = max(dp[i - 1][j],val[i] + dp[i - 1][j - wt[i]]);
			}
		}
	}
	cout << dp[n][W];
}
int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
		solve();
	return 0;
}


Measured memory_bytes: 78336