akhauriyash/Code-Regression huggingface dataset:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    A = [0] * N
    for i in range(N):
        A[i] = Scanner.int()
    visited = [False] * N
    p = 0
    cnt = 0
    while p != 1:
        if visited[p] == True:
            print(-1)
            return
        visited[p] = True
        p = A[p] - 1
        cnt += 1
    print(cnt)


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()


Measured memory_bytes: 9808
#include<bits/stdc++.h>
#define int long long
#define P pair<int,int>
#define PP pair<int,P>
#define F first
#define S second
#define f(i,n) for(int i=0;i<(n);i++)
#define INF LLONG_MAX/3
#define eps LDBL_EPSILON
#define mod (int)(1000000007)
#define pie 3.141592653589793238462643383279
#define Bpri priority_queue
#define Spri priority_queue<P,vector<P>,greater<P>>
using namespace std;
signed main(){
	int a,b,c;
	cin>>a>>b>>c;
	if((a<c&&c<b)||(b<c&&c<a))puts("Yes");
	else puts("No");
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <cmath>
#include <cassert>

using namespace std;

typedef long long LL;
typedef long double LD;
const LL MOD = 998244353;
const LD EPS = 1e-10;
static const size_t MAX_N = 1000001;

struct modll {
	long long x;
	modll() { }
	modll(int _x) : x(_x) { }
	operator int() const { return (int)x; }
	modll operator+(int y) { return (x + y + MOD) % MOD; }
	modll operator+=(int y) { x = (x + y + MOD) % MOD; return *this; }
	modll operator-(int y) { return (x - y + MOD) % MOD; }
	modll operator-=(int y) { x = (x - y + MOD) % MOD; return *this; }
	modll operator*(int y) { return (x * y) % MOD; }
	modll operator*=(int y) { x = (x * y) % MOD; return *this; }
	modll operator/(int y) { return (x * modpow(y, MOD - 2)) % MOD; }
	modll operator/=(int y) { x = (x * modpow(y, MOD - 2)) % MOD; return *this; }
	static modll modinv(int a) { return modpow(a, MOD - 2); }
	static modll modpow(int a, int b) {
		modll x = a, r = 1;
		for (; b; b >>= 1, x *= x) if (b & 1) r *= x;
		return r;
	}
};

modll combination(LL n, LL r) {
	static modll fact[MAX_N + 1], inv[MAX_N + 1];
	if (!fact[0]) {
		fact[0] = 1;
		for (int i = 1; i <= MAX_N; ++i) {
			fact[i] = fact[i - 1] * i;
		}
		inv[MAX_N] = modll::modinv(fact[MAX_N]);
		for (int i = MAX_N; i >= 1; --i) {
			inv[i - 1] = inv[i] * i;
		}
	}
	if (r > n) {
		return 0;
	}
	return (fact[n] * inv[r]) * inv[n - r];
}

int main(int argc, char* argv[]) {
#ifdef _MSC_VER
	while (true)
#endif
	{
		int n = -1, m, k;
		cin >> n >> m >> k;
		if (n < 0) return 0;
		modll ans = 0;
		for (LL i = 0; i <= k; ++i) {
			modll a = combination(n - 1, n - i - 1);
			modll b = modll::modpow(m - 1, n - i - 1) * m;
			ans += a * b;
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 19232
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef vector<llong> V;

#define _ cin.tie(0);ios::sync_with_stdio(false);
string s,t;
int main(){ _;
  cin>>s>>t;
  string ss=s+s;
  int pos=ss.find(t);
  if(pos ==-1)
    cout<<"No"<<endl;
  else
    cout<<"Yes"<<endl;

  return 0;
}


Measured memory_bytes: 256
#![allow(unused_imports)]

use text_io::*;
use proconio::*;
use std::collections::*;
use itertools::Itertools;
use std::process::exit;
use std::cmp::*;
use num::*;
use num::integer::Roots;

fn main() {
    input! {
    a:String,
    }
    let a=a.chars().collect_vec();
    let mut b=0;
    for i in 0..4 {
        if a[i]=='2' {b+=1}
    }
    println!("{}",b)
}

Measured memory_bytes: 2132
#AC
n,l = gets.chomp.split(' ').map(&:to_i)
apples = (l...(l+n)).to_a
if apples.first * apples.last <= 0
  puts apples.sum
elsif apples.last < 0
  puts apples.sum - apples.last
else
  puts apples.sum - apples.first
end

Measured memory_bytes: 14284
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long
using namespace std;
const int MAX =1e5+9;
int main(){
	int arr[3];
	int c;
	cin>>arr[0]>>arr[1]>>c;
	sort(arr , arr+2);
	if(arr[0]<=c&&c<=arr[1])
		cout<<"Yes";
	else
		cout<<"No";
	
}

Measured memory_bytes: 256
#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<string>
#include<stdexcept>
#include<stdio.h>
using namespace std;

int main(){
  int a,b,c,d,e,k;
  cin >> a >> b >> c >> d >> e >> k;
  if(e-a<=k)
  cout << "Yay!" <<endl;
  else cout << ":(" <<endl;
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		while (true) {
			int n = scan.nextInt();
			int m = scan.nextInt();
			if (n == 0 && m == 0)
				break;

			Cup c = new Cup(n, m);
			Cup d = new Cup(n, m);
			for (int L = 0; L < 3; L++) {
				int nl = scan.nextInt();
				for (int j = 0; j < nl; j++) {
					int cup = scan.nextInt();
					c.set(L, cup);
					d.set(L, cup);
				}
			}
			c.move(0, 1);
			d.move(2, 1);
			if (c.getOper() > m && d.getOper() > m)
				System.out.println(-1);
			else
				System.out.println(Math.min(c.getOper(), d.getOper()));
		}

		scan.close();
		System.exit(0);
	}
}

class Cup {
	int max;
	boolean[][] c;
	int oper;
	int operLimit;

	public Cup(int n, int limit) {
		max = n;
		oper = 0;
		operLimit = limit;
		c = new boolean[3][n];
		for (int l = 0; l < 3; l++)
			for (int i = 0; i < n; i++)
				c[l][i] = false;
	}

	public int getOper() {
		return oper;
	}

	public void move(int toLoc, int cup) {
		if (oper > operLimit)
			return;

		if (cup > max)
			return;

		if (c[toLoc][cup - 1]) {
			this.move(toLoc, cup + 1);
			return;
		}

		int wkLoc;
		if (toLoc == 0)
			wkLoc = 2;
		else
			wkLoc = 0;

		if (c[wkLoc][cup - 1]) {
			this.move(toLoc, cup + 1);
			this.moveOne(1, cup);
		}
		this.move(wkLoc, cup + 1);
		this.moveOne(toLoc, cup);
		this.move(toLoc, cup + 1);
		return;

	}

	private void moveOne(int toLoc, int cup) {
		for (int i = 0; i < 3; i++)
			if (i == toLoc)
				c[i][cup - 1] = true;
			else
				c[i][cup - 1] = false;
		oper++;
	}

	public void set(int loc, int cup) {
		c[loc][cup - 1] = true;
	}
}

Measured memory_bytes: 28988
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

typedef long long ll;

ll mygcd(ll a, ll b) { if (b == 0) return a; return mygcd(b, a % b); }

ll mylcm(ll a, ll b) { return a / mygcd(a, b) * b; }

ll mymax(ll a, ll b) { return (a > b) ? a : b; }

ll mymin(ll a, ll b) { return (a < b) ? a : b; }

ll myasc(const void* a, const void* b) { return *(ll*)a - *(ll*)b; }

ll mydsc(const void* a, const void* b) { return *(ll*)b - *(ll*)a; }

bool flag;
ll n,ans;


int main() {
	scanf("%lld", &n );
	for (int i = 1; i <= n; i++) {
		ans += n / i * (n / i+1) / 2 * i;
	}
	printf("%lld", ans);

	return 0;
}

Measured memory_bytes: 2156
#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define mod 1000000007
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inp_vecN for(int i=0;i<n;i++)cin>>v[i]; 
vector<int> get_Factors(ll n)
{
      vector <int> v;
      for(ll i=2; i<=sqrt(n)+1; i++)
      {
            if(n%i==0)
            {
                  v.push_back(i);
                  v.push_back(n/i);
            }
      }

      v.push_back(1);

      return v;
}
string get_bin(ll n)
{
    string s = "";
    while(n > 0)
    {
        s += ('0' + n%2);
        n /= 2;
    }
    reverse(s.begin(), s.end());
    return s;
}

ll bin_to_dec(string s)
{
    ll ans = 0;
    for(auto ch: s)
    {
        ans *= 2;
        ans += (ch - '0');
    }
    return ans;
}
void PrintVector(vector<ll> v,int n)
{

    for(int i=0;i<n;i++)
    {
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
int no_of_primeFactors(int n)  
{ 
    int count=0;
    while (n % 2 == 0)  
    {  
        count++;
        n = n/2;  
    }  
   
    for (int i = 3; i <= sqrt(n); i = i + 2)  
    { 
        while (n % i == 0)  
        {  
           count++;
            n = n/i;  
        }  
        
    }  
    if (n > 2)  
        count++;
    return count;
} 
 ll gcd(int a, int b)
{ 
    if (a == 0) 
        return b;  
    return gcd(b % a, a);  
} 
   
  ll lcm(int a, int b)  
{  
    return (a/gcd(a, b)*b);  
}  



ll pwr(ll base,ll expo,ll m)
{
    if(base==0) return 0LL;
    if(expo==0) return (1LL%m);
    if((expo&1)==0) {ll temp=pwr(base,expo>>1,m); return (temp*temp)%m;}
    return ((base%m)*pwr(base,expo-1,m))%m;
}

int main(int argc, char const *argv[])
{
    ios;

    string s;
    cin>>s;
    int n=s.length()-1;

    if(s[n]=='2' || s[n]=='4' || s[n]=='5' || s[n]=='7' || s[n]=='9')
    {
      cout<<"hon";
    }
    else if(s[n]=='3')
    {
      cout<<"bon";
    }
    else
      cout<<"pon";




      return 0;
}

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

using namespace std;

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

const char sp = ' ';
const char cmm = ',';
const int MOD = 1000000007;

using ll = long long;

ll mod(ll a, ll b){return (a%b+b)%b;}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){return a*b/gcd(a,b);}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void Judge(bool b){b?Yes():No();}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void JUDGE(bool b){b?YES():NO();}
ll powMod(ll b, ll e, ll m){ll r=1;while(e>0){if(e&1)r=(r%m)*(b%m)%m;b=(b%m)*(b%m)%m;e>>=1;}return r;}
double distance(ll x1, ll y1, ll x2, ll y2){return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));}

template<typename T>
void ppp(T n){cout << n << endl;}
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
    int len = v.size();
    cout << '[';
    for (int i = 0; i < len; ++i) {
        s << v[i]; if (i < len-1) s << ", ";
    }
    cout << ']';
    return s;
}

map<int, int> factor;
template<typename T>
void factorization(T n){

    while (n % 2 == 0){
        ++factor[2];
        n /= 2;
    }
    for (T i = 3; i*i <= n; i += 2){
        while (n%i == 0){
            ++factor[i];
            n /= i;
        }
    }
    if (n > 2)
        ++factor[n];

}

int num(int m){
    int cnt = 0;
    for(auto x : factor){
        if(x.second >= m-1){
            ++cnt;
        }
    }
    return cnt;
}

int main(){

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

    int n;
    cin >> n;
    for(int i = 1; i <= n; ++i){
        factorization(i);
    }

    int ans;
    ans = num(75) + num(25)*(num(3)-1) + num(15)*(num(5)-1) + num(5)*(num(5)-1)*(num(3)-2)/2;

    ppp(ans);



    return 0;
}


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


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

    int n, m, money = 0;
    cin >> n;
    string s[n];
    for (int i = 0; i < n; i++)
      cin >> s[i];

    cin >> m;
    string t[m];
    for (int i = 0; i < m; i++)
      cin >> t[i];

    for (int i = 0; i < n; i++) {
      string checkS = s[i];
      int newMax = 0;
      for (int j = 0; j < n; j++) {
        if (checkS == s[j]) newMax++;
      }
      for (int j = 0; j < m; j++) {
        if (checkS == t[j]) newMax--;
      }
      if (money < newMax)
        money = newMax;
    }
    cout << money << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

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

    int A, B, C, K;
    cin >> A >> B >> C >> K;
    int sum = A + B + C;
    int mx = max({A, B, C});
    sum -= mx;
    REP (i, K) mx <<= 1;
    cout << sum + mx << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		long k = scan.nextLong();
		long count = 1;
		long seven = 7;
		
		if(k % 2 == 0) {
			System.out.println(-1);
		}else {
			while(count<1000000) {
				if(seven % k == 0) {
					System.out.println(count);
					System.exit(0);
				}else {
					seven = (seven * 10 + 7) % k;
					count++;
				}
			}
			System.out.println(-1);
		}

	}

}


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


int main() 
{
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    vector<int> c(n-1);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n-1) cin >> c[i];

    int ans = 0;
    rep(i,n)
    {
        ans += b[i];
    }
    rep(i,n-1)
    {
        if(a[i]==a[i+1]-1)
        {
            ans += c[a[i]-1];
        }
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 256
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  if n-6<=0:
    return 1

  n-=6
  c=1
  return int(c+(-(-n//5.5)))

print(main())


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

int h,w,k;
int c[35][35];
int f[35][35];

int do_vanish(){
    int ret = 0;
    int g[35][35]={};
    for(int i=0; i<h; i++){
        for(int j=0; j+(k-1)<w; j++){
            bool flag = true;
            if(f[i][j] == 0) continue;
            for(int l=1; l<=k-1; l++){
                if(f[i][j] != f[i][j+l]) flag = false;
            }
            if(flag){
                for(int l=0; l<=k-1; l++) g[i][j+l] = 1;
            }
        }
    }
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(g[i][j] == 1){
                ret += f[i][j];
                f[i][j] = 0;
            }
        }
    }
    return ret;
}

void do_clean(){
    bool flag=true;
    while(flag){
        flag = false;
        for(int i=0; i<h-1; i++){
            for(int j=0; j<w; j++){
                if(f[i][j] != 0 && f[i+1][j] ==0){
                    f[i+1][j] = f[i][j];
                    f[i][j] = 0;
                    flag = true;
                }
            }
        }
    }
    return;
}

int main(){
    cin >> h >> w >> k;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            char z;
            cin >> z;
            c[i][j] = z - '0';
            //cout << c[i][j] << endl;
        }
    }

    long long max_ans = 0;
    for(int ii=0; ii<h; ii++){
        for(int jj=0; jj<w; jj++){
            for(int i=0; i<h; i++) for(int j=0; j<w; j++) f[i][j] = c[i][j];
            f[ii][jj]=0;
            long long ans=0;
            int pow=1;
            while(true){
                do_clean();
                int x = do_vanish();
                if(x == 0) break;
                ans += pow*x;
                pow *= 2;
            }
        max_ans = max(ans, max_ans);
        }
    }
    
    cout << max_ans << endl;
    return 0;
}

Measured memory_bytes: 3616
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
int N,M;
int A[105],B[105][105],score[105];
int main()
{
	cin >> N;
	cin >> M;
	for(int i=0;i<M;i++)
	{
		cin >> A[i];
		A[i]--;
	}
	for(int i=0;i<M;i++)
	{
		for(int j=0;j<N;j++)
		{
			cin >> B[i][j];
			B[i][j]--;
		}
	}
	for(int i=0;i<M;i++)
	{
		int cnt = 0;
		for(int j=0;j<N;j++)
		{
			if(B[i][j]==A[i])score[j]++;
			else cnt++;
		}
		score[A[i]]+=cnt;
	}
	for(int i=0;i<N;i++)cout << score[i] << endl;
	return 0;
}

Measured memory_bytes: 1208
def y_solver(tmp):
    l=len(tmp)
    rev=[int(i) for i in tmp[::-1]+"0"]
    ans=0
    for i in range(l):
        num=rev[i]
        next_num=rev[i+1]
        if (num>5) or (num==5 and next_num>=5):
            rev[i+1]+=1
        ans+=min(num,10-num)  
    last=rev[l]
    ans+=min(last,10-last)
    return ans
s=input()
ans=y_solver(s)
print(ans)

Measured memory_bytes: 14656
#include <stdio.h>

int main(){
    int n,i,x1,x2,min,max;
    long long sum;
    scanf("%d %d",&n,&x1);
    min = x1;
    max = x1;
    sum = x1;
    for(int i = 2;n >= i;i++){
        scanf("%d",&x2);
        if(min >= x2){
            min = x2;
        }else{
            min = min;
        }
        if(max <= x2){
            max = x2;
        }else{
            max = max;
        }
        sum = sum + x2;
    }
    printf("%d %d %lld\n",min,max,sum);
    
    return 0;
}


Measured memory_bytes: 2716
/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

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

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

ll nCr(ll n, ll r){
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}

//-----------------------ここから-----------

int main(void){
    vector<int> n(4);
    for(int i = 0; i < 4; i++) cin >> n[i];

    sort(n.begin(), n.end());

    do{
        if(n[0] == 1 && n[1] == 9 && n[2] == 7 && n[3] == 4){
            cout << "YES" << endl;
            return 0;
        }
    } while(next_permutation(n.begin(), n.end()));

    cout << "NO" << endl;

}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int d = sc.nextInt();
        int[][] x = new int[n][d];
        for (int i = 0 ; i < n ; i++) {
            for (int j = 0 ; j < d ; j++) {
                x[i][j] = sc.nextInt();
            }
        }
        int count = 0;
        for (int i = 0 ; i < n ; i++) {
            for (int j = i + 1 ; j < n ;j++) {
                int D = 0;
                for (int k = 0 ; k < d ; k++) {
                    D += (x[i][k] - x[j][k]) * (x[i][k] - x[j][k]);
                }
                int z = (int) Math.floor(Math.sqrt(D));
                if (z * z == D) {
                    count++;
                }
            }
        }
        System.out.println(count);
    }

}


Measured memory_bytes: 21716
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

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

void shellSort(int A[],int n){
for(int h=1; ;){
if(h>n) break;
G.push_back(h);
h=3*h+1;
}

for(int i=G.size()-1;i>=0;i--){
insertionSort(A,n,G[i]);
}
}


int main(){
cin >> n;
for(int i=0;i<n;i++) scanf("%d",&A[i]);
cnt=0;

shellSort(A,n);

cout << G.size() << endl;
for(int i=G.size()-1;i>=0;i--){
printf("%d",G[i]);
if(i) printf(" ");
}
printf("\n");
printf("%d\n",cnt);
for(int i=0;i<n;i++) printf("%d\n",A[i]);


return 0;
}

Measured memory_bytes: 7128
#include <bits/stdc++.h>
using namespace std;
const int mod = 1'000'000'007;

int main() {
  int n;
  cin >> n;
  long long ans = 0, tot = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans = (ans + tot * a) % mod;
    tot = (tot + a) % mod;
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;



int main(){
	int X, N,p[100],ans = 1000,check = 0;
	cin >> X >> N;
	for (int i = 0; i < N; i++) {
		cin >> p[i];
	}
	for (int i = 0; i <= 101; i++) {
		int x = 0, y = 0;
		for (int k = 0; k < N; k++) {
			
			if (X - i != p[k]) {
				x++;
			}
			if (X + i != p[k]) {
				y++;
			}

		}
		if (x == N || y == N) {
			if (x == N) ans = X - i;
			else ans = X + i;
			//cout << x << ' ' << y << endl;
			break;
		}
	}
	

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


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

int main() {
	int s;
	cin >> s;
	int h = s/3600;
	//int m = s/60-h*60;
	int m = s%3600/60;
	s=s%60;
	
	cout<<h<<":"<<m<<":"<<s<<endl;
	return 0;
}


Measured memory_bytes: 3108
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll fun(ll a)
{   ll ans=1;
    for(int i=1;i<=5;i++)
    {
        ans*=a;
    }
    return ans;
}
int main()
{
    ll x;
    cin>>x;
    for(ll i=-1000;i<=1000;i++)
    {
        for(ll j=-1000;j<=1000;j++)
        {
            if((fun(i)-fun(j))==x)
            {
                cout<<i<<" "<<j<<endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 3632
#D問題
A,B = map(int,input().split())
ans = [["" for i in range(100)] for j in range(100)]
for i in range(100):
    for j in range(100):
        if i < 50:
            ans[i][j] = "#"
        else:
            ans[i][j] = "."
w = 1
b = 1
flag = 0
for i in range(50):
    for j in range(100):
        if i%2 == 0 and (flag+j)%2 == 0:
            if w < A:
                ans[i][j] = "."
                w+=1
    if i%2 == 0:
        flag^=1
        
flag = 0            
for i in range(50):
    for j in range(100):
        if i%2 == 0 and (flag+j)%2 == 0:
            if b < B:
                ans[99-i][j] = "#"
                b+=1
    if i%2 == 0:
        flag^=1
                
print(100,100)
for i in range(100):
    for j in range(100):
        print(ans[i][j],end="")
    print()

Measured memory_bytes: 4596
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <utility>
#include <set>
#include <climits>

using namespace std;

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

int main() {
    int X, A;
    scanf("%d %d", &X, &A);
    if (X < A) {
        printf("0\n");
    } else {
        printf("10\n");
    }
    return 0;
}



Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG

#include<bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF= 1e9+5;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>>vvi;

int main(){
  int w,h,n;
  cin>>w>>h>>n;
  vi x(n),y(n),a(n);
  rep(i,n)cin>>x[i]>>y[i]>>a[i];
  int lx=0,hx=w,ly=0,hy=h;
  rep(i,n){
    if(a[i]==2)hx=min(hx,x[i]);
    if(a[i]==1)lx=max(lx,x[i]);
    if(a[i]==4)hy=min(hy,y[i]);
    if(a[i]==3)ly=max(ly,y[i]);
  }
  int dx=hx-lx;
  int dy=hy-ly;
  
  if(dx<=0 || dy<=0){
    cout<<0<<endl;
    return 0;
  }
  else {
    cout<<dx*dy<<endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int a,b,c,d;
    int sum=0;
    cin>>a>>b>>c>>d;
    if(a<=b) sum+=a;
    else sum+=b;
    if(c<=d) sum+=c;
    else sum+=d;
    cout<<sum<<endl;
    
    return 0;
    
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int n,b[50],c[50],s[50],k=0;cin>>n;
  for(int i=0;i<n;i++){
      cin>>b[i];
    }
  for(int i=0;i<n;i++){
    cin>>c[i];
    if(b[i]-c[i]>=0)k+=b[i]-c[i];
    }
  cout<<k;
  return 0;
}

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

using namespace std;
using ll = long long;

int n;
ll ans, a, x[55], dp[55][55][2600];

ll _max(ll a, ll b){
	if(a > b) return a;
	else return b;
}

int main(){
	cin >> n >> a;
	for(int i = 1; i <= n; ++i) cin >> x[i];
	int X = a;
	for(int i = 0; i <= n; ++i) X = _max(X, x[i]);
	for(int j = 0; j <= n; ++j){
		for(int k = 0; k <= n; ++k){
			for(int s = 0; s <= n*X; ++s){
				if(j == 0 && k == 0 && s == 0) dp[j][k][s] = 1;
				else if(j >= 1 && s < x[j]) dp[j][k][s] = dp[j-1][k][s];
				else if(j >= 1 && k >= 1 && s >= x[j]) dp[j][k][s] = dp[j-1][k][s] + dp[j-1][k-1][s-x[j]];
				else dp[j][k][s] = 0;
			}
		}
	}
	for(int i = 1; i <= n; ++i) ans += dp[n][i][a*i];
	cout << ans << endl;	
}

Measured memory_bytes: 56580
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int k = sc.nextInt();

		int v[] = new int[n];
//		int valueSum = 0;
		for(int i = 0; i < n; i++) {
			v[i] = sc.nextInt();
//			valueSum += v[i];
		}
		
		int result = 0;
		//残る部分のサイズ
		for(int i = 0; i <= n; i++) {
//			System.out.println("i = "+i);
			if(n - i > k) {
				continue;
			}
			for(int j = 0; j < n ; j++) {
				if(j + i > n) {
					break;
				}
				int mask[] = new int[n];
				Arrays.fill(mask, 1);
				
				for(int l = j; l < j + i; l++) {
					mask[l] = 0;
				}
				
				int resArray[] = new int[n];
				
				for(int l = 0; l < n; l++) {
					resArray[l] = mask[l]*v[l];
					//debug
//					System.out.print(resArray[l]+" ");
				}
//				System.out.println();
				
				Arrays.sort(resArray);
				
				for(int l = 0; l < k - (n - i) ; l++) {
					if(resArray[l] >= 0) {
						break;
					}
					resArray[l] = 0;
				}
				
				
				int tmpSum = 0;
				for(int l = 0; l < n; l++) {
					tmpSum += resArray[l];
				}
				result = Math.max(result, tmpSum);
			}
		}
		
		System.out.println(result);
	}

}


Measured memory_bytes: 31304
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String s = sc.next();
    for (int i = 0; i < s.length(); i++) {
      int i1 = s.charAt(i) + n;
      if (i1 > 90) {
        i1 = i1 - 26;
      }
      System.out.print((char) i1);
    }
  }
}

Measured memory_bytes: 25940
S=input()
N=len(S)
X=[1,10,9,12,3,4]
Y=[0,0,0,0,0,0]
nowmod=0
modprime=10**9+7
for i in range(N):
    if S[-i-1]=="?":
        Y[i%6]+=1
    else:
        nowmod+=X[i%6]*int(S[-i-1])
        nowmod%=13
#print(nowmod,Y)
#?は0~9まで選べる
M=max(Y)
dp=[[0 for j in range(13)]for i in range(M+1)]
#dp[i][j]:全部でi個あるグループの総和がmod 13でjになるパターン数(mod 10**9+7)
dp[0][0]=1
for i in range(M):
    for k in range(10):
        for j in range(13):
            dp[i+1][(k+j)%13]+=dp[i][j]
            dp[i+1][(k+j)%13]%=modprime
table=[[dp[Y[i]][j] for j in range(13)]for i in range(6)]
ans=[[0 for j in range(13)] for i in range(7)]
ans[0][0]=1
for i in range(6):
    for j in range(13):
        for k in range(13):
            ans[i+1][(j+(X[i]*k))%13]+=ans[i][j]*table[i][k]
            ans[i+1][(j+(X[i]*k))%13]%=modprime
print(ans[6][(5-nowmod)%13])


Measured memory_bytes: 13492
from collections import Counter
n=int(input())
a=[int(input()) for i in range(n)]
b=Counter(a[::2])
a.sort()
c=Counter(a[::2])

ans=0
for i in b:
	if i in c:
		ans+=abs(b[i]-c[i])
	else:
		ans+=b[i]
print(ans)

Measured memory_bytes: 20960
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n - 2):
    if (p[i] < p[i + 1] and p[i + 1] < p[i + 2]): ans += 1
    elif(p[i + 2] < p[i + 1] and p[i + 1] < p[i]): ans += 1
print(ans)


Measured memory_bytes: 3060
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String a = scanner.next();
        String b = scanner.next();
        String c = scanner.next();
        int num = Integer.parseInt(a+b+c);

        if(num%4==0) {
            System.out.println("YES");
            return;
        }
        System.out.println("NO");
    }
}


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

#define rep(l, r) for (int i = (l); i < (r); i++)

typedef long long ll;
 
using namespace std;

template <typename T> T gcd(T a, T b) {
    if (b == 0)
        return a;

    return gcd(b, a % b);
}

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

void solve() {
    ll a[3], n[] = {1, 0, -1}, k;
    rep(0, 3) cin >> a[i];
    cin >> k;

    ll ans = 0;
    rep(0, 3) {
        if (k <= a[i]) {
            ans += (n[i] * k);
            break;
        } else {
            ans += (n[i] * a[i]);
            k -= a[i];
        }
    }

    cout << ans << endl;
}

int main() {
    solve();

    return 0;
}


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

using namespace std;
using ll =long long;
using db = double;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define che(a,string) cout<<"//"<<string <<"=="<<(a)<<"//" <<endl;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define leftunique(a) {SORT(a);(a).erase(unique((a).begin(),(a).end()),(a).end());}
//leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4 6 になる
bool IsInt(double a){int b = a/1;if(a==b){return true;}else {return false;}}
bool coY() {cout <<"Yes"<<endl;}
bool coN(){cout <<"No"<<endl;}
const int mod = 1e9 + 7;
const ll INF = 1LL << 60;

//ここから書き始める

int main()
{
	cout << setprecision(10);
	vector<int> a(3);
	rep(i,3) cin >> a[i];

	SORT(a);
	ll ans = 0;
	if((a[1] - a[0]) %2 == 1){
		ans+= ((a[1]-a[0])+1)/2 ;
		ans += a[2]-a[1] +1;
	}else{
		ans+= (a[1]-a[0])/2 ;
		ans += a[2] - a[1];
	}
	cout << ans << endl;

}


Measured memory_bytes: 256
//C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

//ここまで標準include

using namespace std;

int main() {
	int a, b;
	cin >> a >> b;
	if (a + b == 3) {
		cout << 3 << endl;
	}
	else if (a + b == 5) {
		cout << 1 << endl;
	}
	else {
		cout << 2 << endl;
	}
}


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

using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
template <typename T> constexpr T inf = numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : numeric_limits<T>::max() / 4;

#define REP(i, stop) FOR(i, 0, stop)
#define FOR(i, start, stop) for (int i = start, i##_len = stop; i < i##_len; ++i)
#define RREP(i, n) for (int i = n; i-- > 0;)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define COMP(key) [](const auto& a, const auto& b) { return a.key < b.key; }

template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& a) { return is >> a.first >> a.second; }
template<typename T> istream& operator>>(istream& is, vector<T>& a) { for (T& x : a) is >> x; return is; }

void LOG(const int& x) { cerr << x << "\n"; }
template<typename T> void LOG(const vector<T>& a) { for (const T& x : a) cerr << x << " "; cerr << "\n"; }
template<typename T, std::size_t S> void LOG(const T (&a)[S]) { REP(i, S) cerr << a[i] << " "; cerr << "\n"; }
template<typename T, std::size_t S, std::size_t R> void LOG(const T (&a)[S][R]) { REP(i, S) { REP(j, R) cerr << a[i][j] << " "; cerr << "\n"; } }

i64 ceil(i64 a, i64 b) { return (a - 1) / b + 1; }
i64 gcd(i64 a, i64 b) { while (b != 0) { i64 t = b; b = a % b; a = t; } return a; }
i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
i64 gcd(const vector<i64>& v) { return accumulate(ALL(v), 1LL, static_cast<i64(*)(i64, i64)>(gcd)); }
i64 lcm(const vector<i64>& v) { return v.empty() ? 0LL : accumulate(v.begin() + 1, v.end(), v[0], static_cast<i64(*)(i64, i64)>(lcm)); }
i64 pow(i64 x, i64 y, i64 z) { i64 a = 1; while (y > 0) { if (y & 1) a = a * x % z; x = x * x % z; y /= 2; } return a; }
i64 inv(i64 x, i64 m) { return pow(x, m - 2, m); }

struct InitCpp { InitCpp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } initCpp;

signed main() {
  i64 n, k;
  cin >> n >> k;
  if (k > (n - 2) * (n - 1) / 2) {
    cout << -1 << endl;
  } else {
    cout << ((n - 1) + (n - 2) * (n - 1) / 2 - k) << endl;
    for (i64 i = 1; i <= n - 1; ++i) {
      cout << i << " " << n << endl;
    }
    i64 c = (n - 2) * (n - 1) / 2 - k;
    for (i64 i = 1; i <= n - 2; ++i) {
      for (i64 j = i + 1; j <= n - 1; ++j) {
        if (c > 0) {
          cout << i << " " << j << endl;
          c -= 1;
        } else {
          return 0;
        }
      }
    }
  }
}


Measured memory_bytes: 256
W, H, N = map(int, input().split())
area = []
for i in range(1, H + 1):
    area.append([1 for n in range(W)])

for i in range(N):
    x, y, a = map(int, input().split())
    if a == 1:
        for _x in range(0, x):
            for h in range(0, H):
                area[h][_x] = 0
    if a == 2:
        for _x in range(x, W):
            for h in range(0, H):
                area[h][_x] = 0
    if a == 3:
        for _y in range(0, y):
            for w in range(0, W):
                area[_y][w] = 0
    if a == 4:
        for _y in range(y, H):
            for w in range(0, W):
                area[_y][w] = 0
print(sum([item for sublist in area for item in sublist]))


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

// syntax sugar: `for (int i = 0; i < N; ++i)`
#define rep(i, N) for (int i = 0; i < (int)(N); ++i)
#define rep2(i, startValue, N) for (int i = (int)(startValue); i < (int)(N); ++i)
// syntax sugar: `for (int i = 0; i < N; ++i)`
#define REP(type, name, beginValue, endCondValue) \
  for (type name = beginValue; name < endCondValue; ++name)
// syntax sugar: 多次元vector
#define VECTOR_DIM3(T, name, d1, d2, d3, initValue) \
    std::vector<std::vector<std::vector<T>>> name(d1, std::vector<std::vector<T>>(d2, std::vector<int>(d3, initValue)));
#define VECTOR_DIM2(T, name, d1, d2, initValue) \
    std::vector<std::vector<T>> name(d1, std::vector<T>(d2, initValue));
#define VECTOR_DIM1(T, name, d1, initValue) \
    std::vector<T> name(d1, initValue);
#define ll long long
#define ld long double
#define DUMP(v) "," #v ":" << v

const ll MOD = 1e9+7;

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

  __int128_t ans = 0;
  __int128_t curtotal = total;
  for(int i = 0; i < n-1; ++i) {
    curtotal -= a[i];
    ans += (a[i] * curtotal) % MOD;
    ans = ans % MOD;
  }

  ll ans2 = (ll)ans;
  cout << ans2 << endl;
  return 0;
}


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

bool binary_search(vector<int> &S, int &key) {
    int left = 0, right = S.size();

    while (left < right) {
        
        int mid = (left + right) / 2;

        if (S.at(mid) == key) {
            return true;
        }
        else if (S.at(mid) > key)
        {
            right = mid;
        }
        else if (S.at(mid) < key)
        {
            left = mid+1; // +1忘れずに
        }
    }
    return false;
}

int main() {
    int n;
    cin >> n;
    vector<int> S(n);

    for (int i = 0; i < n; i++) {
        cin >> S.at(i);
    }

    int q;
    cin >> q;
    vector<int> T(q);

    for (int i = 0; i < q; i++) {
        cin >> T.at(i);
    }

    int cnt = 0;
    for (int x : T)
    {
        if (binary_search(S, x)) {
            cnt++;
        }
    }

    cout << cnt << endl;
}


Measured memory_bytes: 3280
#include<iostream>
using namespace std;
#define MAX 100005
#define NIL -1

struct Node { int p, l, r; };

Node T[MAX];
int n, D[MAX];

void print(int u){
  int i, c;
  cout << "node " << u << ": ";
  cout << "parent = " << T[u].p << ", ";
  cout << "depth = " << D[u] << ", ";

  if(T[u].p == NIL) cout << "root, ";
  else if (T[u].l == NIL) cout << "leaf, ";
  else cout << "internal node, ";

  cout << "[";

  for(i  = 0,c = T[u].l; c != NIL; i++, c = T[c].r){
    if (i) cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

int rec(int u, int p) {
  D[u] = p;
  if(T[u].r != NIL) rec(T[u].r, p);
  if(T[u].l != NIL) rec(T[u].l, p+1);
  }

int main() {
  int i, j, d, v, c, l, r;
  cin >> n;
  for(i=0;i<n;i++) T[i].p = T[i].l = T[i].r = NIL;

  for(i=0;i<n;i++){
    cin >> v >> d;
    for(j=0;j<d;j++){
      cin >> c;
      if(j==0) T[v].l = c;
      else T[l].r = c;
      l = c;
      T[c].p = v;
    }
  }
  for(i=0;i<n;i++){
    if(T[i].p == NIL) r = i;
  }

  rec(r,0);

  for(i=0;i<n;i++) print(i);

  return 0;
}



Measured memory_bytes: 7768
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr ll mod=1e9+7;

int main()
{
  int n;
  cin>>n;
  vector<vector<int>> graph(n);
  vector<pair<int,int>> edge(n-1);
  for (int i = 0; i < n-1; ++i)
  {
    int a,b;
    cin>>a>>b;
    a--;b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
    edge[i]=make_pair(a,b);
  }
  int k=graph[0].size();
  for (int i = 1; i < n; ++i)
  {
    k=max(k,(int)graph[i].size());
  }
  map<pair<int,int>,int> edge_color;
  vector<int> umbillical(n);
  umbillical[0]=-1;
  vector<bool> seen(n,false);
  queue<int> next;
  seen[0]=true;
  next.push(0);
  while(!next.empty())
  {
    int start=next.front();
    next.pop();
    set<int> used_color;
    used_color.insert(umbillical[start]);
    int c=0;
    for (int j = 0; j < graph[start].size(); ++j)
    {
      int goal=graph[start][j];
      if(seen[goal]) continue;
      if(c==umbillical[start]) c++;
      edge_color[make_pair(start, goal)]=c;
      umbillical[goal]=c;
      seen[goal]=true;
      next.push(goal);
      c++;
    }
  }
  cout<<k<<endl;
  for (int i = 0; i < edge.size(); ++i)
  {
    cout<<1+edge_color[edge[i]]<<endl;
  }


}

Measured memory_bytes: 13440
#include <bits/stdc++.h>
using namespace std;
typedef  long long   ll;
typedef  long double ld;
#define ceil(n, m) ((n / m) + ( n % m ? 1 : 0))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
vector<int> edges(1e5 + 7), in_deg(1e5 + 7);
vector<bool> vis(1e5 + 7);
vector<vector<int>> adj(1e5+7);
void dfs(int u) {
	vis[u] = true;
	for (auto v : adj[u]) {
		edges[v] = max(edges[v], edges[u] + 1);
		--in_deg[v];
		if (!in_deg[v])
			dfs(v);	
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("input.txt", "r", stdin);
	int n, m, ans = 0;
	cin >> n >> m;
	for (int i = 0, u, v; i < m && cin >> u >> v; ++i)
		adj[u].emplace_back(v), ++in_deg[v];
	for (int i = 1; i <= n; ++i)
		if (!vis[i] && !in_deg[i])
			dfs(i);
	for (int i = 1; i <= n; ++i)
		ans = max(ans, edges[i]);
	cout << ans;


}


Measured memory_bytes: 14068
//include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;

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

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

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

const int MAX_H = 100;
const int MAX_W = 100;

int dp[ MAX_H + 1][ MAX_W + 1] = {};

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

int h, w;

void dfs(int y, int x, int item) {
  dp[y][x] = 0;
  REP(i,4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if(nx >= 0 && nx < w && ny >= 0 && ny < h && dp[ny][nx] != 0 && dp[ny][nx] == item ) dfs(ny, nx, item);
  }
  return;
}

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

  char c;

  while(cin >> h >> w && h && w) {
    int count = 0;
    REP(i, h) {
      REP(j, w) {
        cin >> c;
        switch(c) {
          case '@':
            dp[i][j] = 1;
            break;
          case '#':
            dp[i][j] = 2;
            break;
          case '*':
            dp[i][j] = 3;
            break;
          default:
            break;
        }
      }
    }

    REP(i, h) {
      REP(j, w) {
        if(dp[i][j] != 0) {
          dfs(i, j, dp[i][j]);
          count++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

Measured memory_bytes: 1700
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <climits>
#include <cassert>

using namespace std;

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

#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

#define PB push_back

typedef vector<double> vd;

const ll M=1000000000;

class Runner {
public:
	vd goal_times;
	vd probabilities;

	Runner() {
		goal_times = vd();
		probabilities = vd();
	}

	void inspect() {
		cerr << "goal_times" << endl;
		REP(i, goal_times.size()) {
			cerr << " " << goal_times[i];
		}
		cerr << endl;
		cerr << "probabilities" << endl;
		REP(i, probabilities.size()) {
			cerr << " " << probabilities[i];
		}
		cerr << endl;
	}
};


ll combination(int n,int k){
	ll ans=1;
	FOR(i,1,k+1){
		ans*=n-i+1;
		ans/=i;
	}
	return ans;
}

const double EPS = 1e-9;
int main()
{
	cout.precision(16);
	int N, M, L; cin >> N >> M >> L;
	vector<Runner> runners;
	REP(i, N) {
		Runner runner;
		double P; int T, V; cin >> P >> T >> V; P /= 100.0;
		double base_time = V > 0 ? (double)L / V : 1e20;
		REP(t_break, M + 1) {
			runner.goal_times.PB(base_time + T * t_break);
			runner.probabilities.PB(
				pow(P, t_break) * pow(1 - P, M - t_break) * combination(M, t_break) );
		}
		runners.PB(runner);
		//runner.inspect();
	}

	REP(i, runners.size()) {
		Runner &r = runners[i];
		double ans = 0;
		REP(t, M + 1) {
			double prize_prob = r.probabilities[t];
			REP(j, runners.size()) {
				if (i == j) continue;
				Runner &comp = runners[j];
				double win_prob = 0;
				REP(k, M+1) {
					if(comp.goal_times[k] > r.goal_times[t] + EPS) {
						win_prob += comp.probabilities[k];
					}
				}
				prize_prob *= win_prob;
			}
			ans += prize_prob;
		}
		printf("%.8f\n",ans);
		//cout << ans << endl;
	}
}

Measured memory_bytes: 1384
#include <set>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
    int h, w, n; cin >> h >> w >> n;
    set<int> S[h+1];
    for (int i = 0; i < n; i++) {
        int x, y; cin >> x >> y;
        S[x].insert(y);
    }
    int x = 1, ymax = 1;
    while (x < h) {
        x++;
        if (!S[x].empty() && *S[x].begin() <= ymax) { x--; break; }
        if (S[x].find(ymax+1) == S[x].end()) ymax++;
    }
    cout << x << endl;
}


Measured memory_bytes: 18944