akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/env python3
import sys


def bit_most(v):
    # 最高位の桁を得る
    if v == 0:
        return 0
    else:
        return len(bin(v))-2


def z_algorithm(S):
    """Zアルゴリズム
    """
    N = len(S)
    Z = [0]*N

    c = 0
    for i in range(1, N):
        if i+Z[i-c] < c+Z[c]:
            Z[i] = Z[i-c]
        else:
            j = max(0, c+Z[c]-i)
            while i+j < N and S[j] == S[i+j]:
                j += 1
            Z[i] = j
            c = i
    Z[0] = N
    return Z


def solve(N: int, a: "List[int]", b: "List[int]"):

    a1 = [0]*(2*N)
    b1 = [0]*N
    b2 = [0]*N
    k_cand = [True]*N
    for keta in range(bit_most(max(max(a), max(b)))+1):
        for i, aa in enumerate(a):
            a1[i] = a1[i+N] = (aa >> keta) & 1
        for i, bb in enumerate(b):
            b1[i] = v = (bb >> keta) & 1
            b2[i] = 1-v
        Z1 = z_algorithm(b1 + [-1] + a1)
        Z2 = z_algorithm(b2 + [-1] + a1)
        # print(a1+[-1]+b1)
        # print(a1+[-1]+b2)
        # print("Z1", Z1)
        # print("Z2", Z2)
        for i in range(N):
            if k_cand[i]:
                k_cand[i] = Z2[i+N+1] == N or Z1[i+N+1] == N
        # print(k_cand)

    for i in range(N):
        if k_cand[i]:
            print(i, b[0] ^ a[i])
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(N - 1 - 0 + 1)]  # type: "List[int]"
    solve(N, a, b)


if __name__ == '__main__':
    main()


Measured memory_bytes: 147924
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
typedef long long ll;
using namespace std;

//bfs
queue<pair<int, int>>q;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
vector<string>s;
vector<vector<int>>bb;
int h, w;

void bfs() {
	pair<int, int>p;
	while (!q.empty()) {
		p = q.front();
		q.pop();
		for (int i = 0; i < 4; ++i) {
			int nx = p.first + dx[i]; int ny = p.second + dy[i];
			if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#'&&bb[nx][ny]==0) {
				s[nx][ny] = '#';
				bb[nx][ny] = bb[p.first][p.second]+1;
				q.push(make_pair(nx, ny));
			}
		}
	}
}

int main() {
	cin >> h >> w;
	s.resize(h);
	bb.resize(h, vector<int>(w, 0));
	bool fin = false;
	for (int i = 0; i < h; ++i)cin >> s[i];
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			if (s[i][j] == '#' && bb[i][j] == 0) {
				q.push(make_pair(i, j));
			}
		}
	}
//queueに入れて、今ある#を調べ終わったらbfs
	bfs();
	int ans = 0;
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j)ans = max(ans, bb[i][j]);
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 13456
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] argv) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        int k = Integer.parseInt(str[0]);
        int n = Integer.parseInt(str[1]);

        int x, y, cnt = 0;
        for (x = 0; x <= k; x++) {
            for (y = 0; y <= k; y++) {
                if ((0 <= n - x - y) && (n - x - y <= k)) cnt++;
            }
        }
        System.out.println(cnt);
    }
}


Measured memory_bytes: 23380
#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)a, i##_len=(b); i<i##_len; i++)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const ll INF = LLONG_MAX/3;

/*-----Library-----*/

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

struct UnionFind {
    vector<int> par; // 親ノード
    vector<int> rank; // ランク
    vector<int> sizeuf; //要素数
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n); rank.resize(n); sizeuf.resize(n);
        for (int i = 0; i < n; ++i) {
            par[i] = i;
            rank[i] = 0;
            sizeuf[i]=1;
        }
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

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

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        sizeuf[x]+=sizeuf[y];
        return true;
    }

    int size(int x){
        return sizeuf[root(x)];
    }
};

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

long long lcm(long long i,long long j){
    return i*(j/gcd(i,j));
}
//コンビネーション
/*
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 */
/*-----Library-----*/
const int MOD=1234567;
signed main(){
	int n;
	string s,t;
	cin>>n>>s>>t;
	rep(i,n)cout<<s[i]<<t[i];
	cout<<endl;
}


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

int n;
vector<int> p(500000);

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

    sort(p.begin(), p.begin()+n);
    cout << p.at(n-1)/2 + accumulate(p.begin(), p.begin()+n-1, 0) << endl;
}

Measured memory_bytes: 2176
package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"reflect"
	"sort"
)

const M = int(1E9) + 7

func main() {
	defer writer.Flush() // Use printf, scanf

	// CODE
	var N int
	var S string
	scanf("%d\n", &N)
	scanf("%s\n", &S)
	m := map[byte]int{}
	for i := 0; i < N; i++ {
		m[S[i]]++
	}
	var ans int
	ans = 1
	for _, v := range m {
		ans *= v + 1
		ans %= M
	}
	ans = (ans + M - 1) % M
	printf("%d\n", ans)
}

// I/O {{{
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

// }}}
// Math {{{
func maxInt(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func minInt(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

// }}}
// {{{ dummy
func dummy() {
	fmt.Printf("Hello, World\n")
	fmt.Printf("%v", reflect.TypeOf(0))

	arr := []int{10, 9, 8, 7, 1}
	sort.Ints(arr)
	fmt.Println(arr)
}

// }}}
// PriorityQueue {{{
// Usage:
// type Person struct {
// 	name string
// 	age  int
// }
//
// func (p1 Person) Less(p2 interface{}) bool {
// 	return p1.age < p2.(Person).age
// }
//
// func main() {
// 	sc.Split(bufio.ScanWords)
// 	defer out.Flush() // Use printf
//
// 	// CODE
// 	q := NewPriorityQueue()
// 	q.Push(Person{name: "B", age: 11})
// 	q.Push(Person{name: "A", age: 10})
// 	q.Push(Person{name: "C", age: 12})
//
// 	for q.Len() > 0 {
// 		p := q.Pop().(Person)
// 		printf("%s %d\n", p.name, p.age)
// 	}
// }
func NewPriorityQueue() *PriorityQueue {
	q := &PriorityQueue{s: new(sorter)}
	heap.Init(q.s)
	return q
}

func (q *PriorityQueue) Pop() Comparable {
	return heap.Pop(q.s).(Comparable)
}

func (q *PriorityQueue) Top() Comparable {
	if len(*q.s) > 0 {
		return (*q.s)[0].(Comparable)
	}
	return nil
}

func (q *PriorityQueue) Fix(x Comparable, i int) {
	(*q.s)[i] = x
	heap.Fix(q.s, i)
}

func (q *PriorityQueue) Remove(i int) Comparable {
	return heap.Remove(q.s, i).(Comparable)
}

func (q *PriorityQueue) Len() int {
	return q.s.Len()
}

type Comparable interface {
	Less(other interface{}) bool
}

type sorter []Comparable

func (s *sorter) Push(x interface{}) {
	*s = append(*s, x.(Comparable))
}

func (s *sorter) Pop() interface{} {
	n := len(*s)
	if n > 0 {
		x := (*s)[n-1]
		*s = (*s)[0 : n-1]
		return x
	}
	return nil
}

func (s *sorter) Len() int {
	return len(*s)
}

func (s *sorter) Less(i, j int) bool {
	return (*s)[i].Less((*s)[j])
}

func (s *sorter) Swap(i, j int) {
	(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
}

// Define priority queue struct
type PriorityQueue struct {
	s *sorter
}

func (q *PriorityQueue) Push(x Comparable) {
	heap.Push(q.s, x)
}

// }}}
// Sort {{{
// sort.Stable(IntSlice(a))
type IntSlice []int

func (a IntSlice) Len() int {
	return len(a)
}
func (a IntSlice) Less(i, j int) bool {
	return a[i] < a[j]
}
func (a IntSlice) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// }}}


Measured memory_bytes: 1280
import numpy as np
def make_prime(U):
    is_prime = np.zeros(U,np.bool)
    is_prime[2] = 1
    is_prime[3::2] = 1
    M = int(U**.5)+1
    for p in range(3,M,2):
        if is_prime[p]:
            is_prime[p*p::p+p] = 0
    return is_prime, is_prime.nonzero()[0]

p=set(make_prime(10**5)[1])
lst=[0]*(10**5+2)
cnt=0
for i in range(100001):
    if i in p and (i+1)//2 in p:
        cnt+=1
    lst[i]=cnt
q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    print(lst[r]-lst[l-1])

Measured memory_bytes: 16360
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main() {
    int n;
    cin >> n;
    int tmp_num = 2;
    while(1) {
        if ( tmp_num > n ) break;
        tmp_num *= 2;
    }
    cout << tmp_num / 2 << endl;
    return 0;
}


Measured memory_bytes: 256
# ALDS1_4_B: binary Search
n = int(input())
s = list(map(int, input().split()))
q = int(input())
t = list(map(int, input().split()))

def bisection(s, tar):
    left = 0
    right = len(s)
    while left + 1 < right:
        middle = (left + right) // 2
        if s[middle] > tar:
            right = middle
        else:
            left = middle
    if s[left] == tar:
        return True
    return False

cnt = 0
for e in t:
    if bisection(s, e):
        cnt += 1
print(cnt)


Measured memory_bytes: 16712
#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define BUGAVOID(x) x
#define rep(...) BUGAVOID(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define sz(c) (int)(c.size())
#define all(c) c.begin(),c.end()
#define ms(a,v) memset(a,v,sizeof(a))
#define mp make_pair
#define write(x) cout<<(x)<<endl
using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; }return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; }return 0; }
constexpr ll pow2(const int n) { return 1ll << n; }
const int INF = 1 << 28; const ll LINF = 1ll << 60;
const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; const double EPS = 1e-6;
const int dy[4] = { 0,1,0,-1 }; const int dx[4] = { 1,0,-1,0 };
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); }; }aaaa;

int N;
vll a;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> former, latter;
vector<int> lremain; // 0前半1後半で採用されていない2後半採用されてる

ll fsum = 0, lsum = 0;
ll ans = 0;

int main() {
    cin >> N;
    a.assign(3 * N, 0);
    rep(i, 3 * N) cin >> a[i];

    lremain.assign(3 * N, 0);
    rep(i, N) {
        former.emplace(a[i], i);
        fsum += a[i];
    }
    rep(i, N, 3 * N) {
        latter.emplace(a[i], i);
        lremain[i] = 1;
    }
    rep(i, N) {
        auto& p = latter.top();
        lremain[p.second] = 2;
        lsum += p.first;
        latter.pop();
    }

    ans = fsum - lsum;
    rep(i, N, 2 * N) {
        auto ftop = former.top();
        if (ftop.first < a[i]) {
            former.emplace(a[i], i);
            fsum += a[i] - ftop.first;
            former.pop();
        }

        if (lremain[i] == 2) {
            while (lremain[latter.top().second] != 1) {
                latter.pop();
            }

            auto& ltop = latter.top();
            lremain[i] = 0;
            lremain[ltop.second] = 2;
            lsum += ltop.first - a[i];
            latter.pop();
        }
        else {
            // lremain[i] == 1
            lremain[i] = 0; // 意味わからん
        }

        chmax(ans, fsum - lsum);
    }

    write(ans);
}

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

//const static double	de_PI	= 3.14159265358979323846;
//const static int	de_MOD	= 1000000007;
//const static int	de_MAX	= 999999999;
//const static int	de_MIN = -999999999;

int main(void) {

	//std::ifstream in("123.txt");	std::cin.rdbuf(in.rdbuf());

	int n = 0;
	std::stack<int> S;
	std::vector<int> ans;

	while (std::cin >> n) {
		if (n != 0) {
			S.push(n);
		}
		else {
			ans.push_back(S.top());
			S.pop();
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		std::cout << ans[i] << std::endl;
	}

}

Measured memory_bytes: 3068
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vout(x) {rep(i,x.size()) cout << x[i] << " "; cout << endl;}
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 2e5;

const double eps = 1e-10;

int main() {
    int n; cin >> n;
    for (int h = (n/4)+1; h <= 3500; h++) {
        double a = (double)4/n;
        double b = (double)1/h;
        double m = 1/(a-b);
        for (int n = int(m+eps); n <= 3500; n++) {
            double w = 1/((a-b) - (double)1/n);
            if (w > 3500) continue;
            int tmp = int(w+eps);
            if (abs(tmp-w) < eps) {
                cout << h << " " << n << " " << tmp << endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 3636
using System;
using System.Linq;
using System.Collections.Generic;

namespace ForAtcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = long.Parse(Console.ReadLine());
            var s = Console.ReadLine();
            var count = 0;
            for (var i = 1; i <= n - 2; i++)
            {
                if (s.Substring(i - 1, 3) == "ABC") count++;
            }
            Console.WriteLine(count);
        }
    }
}

Measured memory_bytes: 13140
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;



int main(){
	string s;
	cin >> s;
	map<char, int> mp;
	for(auto& c: s) mp[c]++;
	int n = s.size();
	ll ans = 1LL;
	ll cnt = 0;
	bool f = true;
	for(auto& p : mp) {
		if(f) cnt += p.se, f = false;
		else {
			ans += (cnt * p.se);
			cnt += p.se;
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    ll h, w;
    cin >> h >> w;
    ll ans = (w/2) * h;
    if(w % 2 == 1)ans += (h+1)/2;
    if(h == 1 || w == 1)ans = 1;
    cout << ans << endl;
}   


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

bool vis[200005];
vector<int> adj[200005];

int dfs(int i){
	
	int ans=0;

	if(!vis[i]){
		vis[i]=1;ans++;
	for(auto j:adj[i]){
	
			
			ans+=dfs(j);
		}
	}
	return ans;
}

int main(){
	ll n,m,a,b,h=0;
	cin>>n>>m;
	
	for(int i=0;i<m;i++){
		cin>>a>>b;
		a--;b--;
		adj[a].push_back(b);
	    adj[b].push_back(a);	
	}
	
	for(int i=0;i<n;i++){
		if(!vis[i]){
			
			ll c=dfs(i);
			
			h=max(c,h);
		}
	}
	
	cout<<h;
	

 
}


Measured memory_bytes: 20976
#include <bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a,b,c) for(int a=b;a<=(int)c;++a)
#define fd(a,b,c) for(int a=b;a>=(int)c;--a)
#define cu(a,b,c) for(int a=b;a<(int)c;++a)
#define cd(a,b,c) for(int a=(b)-1;a>=(int)c;--a)
#define gcd(a,b) __gcd(a,b) 
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) a.begin(),a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAXN 52
#define MAXH 20
#define MAXM 52
#define INF 100000000
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a,b) uniform_int_distribution<uint32_t> distribution(a,b)
#define max3(a,b,c) max(a, max(b, c))
#define min3(a,b,c) min(a, min(b, c))
#define gmax(a,b) a = max(a, b)
#define gmin(a,b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.1415926535897932384626433
using namespace std;
const int N = 2004;
int l[N][N],r[N][N],u[N][N],d[N][N];

int main(){
	fast_io();
	string s;
	cin >> s;
	int n = s.length();
	vector<ll> dp(n + 1);
	dp[0] = 1;
	fu(i,1,n){
		dp[i] = 3 * dp[i-1] % MOD;	
	}
	ll rsum = 1;
	cd(i,n,0){
		if(s[i] == '1'){
			rsum = (2*rsum + dp[n-i-1])% MOD;
		}
	}
	cout << (rsum) % MOD << '\n';
}



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

const long long MAX = 110000;

long long dp[MAX];

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

  // init
  for(int i = 0; i < MAX; ++i)dp[i] = MAX;
  // initial condition
  dp[0] = 0;

  for(int i = 0; i < n; ++i){
    for(int j = 1; j <= n - i; j *= 6){
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
    for(int j = 1; j <= n - i; j *= 9){
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }

  cout << dp[n] << endl;
}


Measured memory_bytes: 1152
using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ0055
{
    class Program
    {
        static void Main()
        {
            foreach (var sum in ReadInputs().Select(first => EnumerateSequence(first).Take(10).Sum(i => i)))
            {
                Console.WriteLine(sum);
            }
        }

        public static IEnumerable<double> ReadInputs()
        {
            string line;
            while ((line = Console.ReadLine()) != null)
            {
                yield return double.Parse(line);
            }
        }

        public static IEnumerable<double> EnumerateSequence(double first)
        {
            yield return first;

            var value = first;

            while (true)
            {
                yield return (value *= 2);
                yield return (value /= 3);
            }
// ReSharper disable FunctionNeverReturns
        }
// ReSharper restore FunctionNeverReturns
    }
}

Measured memory_bytes: 5900
#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5+3;
int n;
string arr[mx];

int cnt[26];
bool compare(string &s1,string &s2) 
{ 
    return s1.size() > s2.size(); 
}
int main() {
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> arr[i];
		reverse(begin(arr[i]), end(arr[i]));
	}
	map<long long, vector<int>> hashv = {};
	long long sum = 0;
	for(int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr+n, compare);
	for(int i = 0; i < n; i++) {
		int freq[26];
		for(int j = 0; j < 26; j++) {
			freq[j] = 0;
		}
		int k = arr[i].size();
		for(int j = 0; j < k; j++) {
			freq[arr[i][j] - 'a']++;
		}
		for(int j = 0; j < 26; j++) {
			if(freq[j] > 0) {
				cnt[j]++;
			}
		}
		long long hash = 0;
		long long hash2 = 0;
		for(int j = 0; j < k; j++) {
			freq[arr[i][j] - 'a']--;
			if(j == k - 1) {
				if(j == 0) {
					sum += cnt[arr[i][j]-'a'] - 1;
				}
				else if(hashv.count(hash * 1000000020 + hash2)) {
					sum += hashv[hash * 1000000020 + hash2][arr[i][j]-'a'] - 1;
				}
			}
			hash *= 61;
			hash += arr[i][j] - 'a' + 1;
			hash %= 1000000009;
			hash2 *= 31;
			hash2 += arr[i][j] - 'a' + 1;
			hash2 %= 1000000009;
			long long chash = hash * 1000000020 + hash2;
			if(!hashv.count(chash)) {
				vector<int> ksd(26, 0);
				hashv[chash] = ksd;
			}
			for(int z = 0; z < 26; z++) {
				hashv[chash][z] += min(1, freq[z]);
			}
		}
		
	}
	cout << sum << endl;
}


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

using namespace std;
typedef long long ll;
ll n,k,r,s,p,ans;
char v[100001];
bool use[100001];
ll score(char c)
{
    if(c=='r')
        return r;
    if(c=='s')
        return s;
    if(c=='p')
        return p;
}
int main()
{
    cin>>n>>k>>r>>s>>p>>v;
    for(int i=0;i<n;i++)
    {
        if(v[i]=='r')
            v[i]='p';

       else if(v[i]=='s')
            v[i]='r';

        else if(v[i]=='p')
            v[i]='s';
    }
    for(int i=0;i<k;i++)
    {
        use[i]=1;
        ans+=score(v[i]);
    }
    for(int i=k;i<n;i++)
    {
        if(v[i-k]==v[i]&&use[i-k]==1)
            use[i]=0;
        else
        {
            use[i]=1;
            ans+=score(v[i]);
        }
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 384
#include <iostream>
using namespace std;
bool odd(int a)
{
	if (a%2==1)
		return true;
	else
		return false;
}
bool plat(int a,int b)
{
	int t=b-a+1;
	if (odd(t))
		return true;
	else
		return false;
}
int main()
{
	int x,y;
	cin>>x>>y;
	if (x>y)
	{
		x=x+y;
		y=x-y;
		x=x-y;
	}
	if (x>0 && y>0)
		cout<<"Positive"<<endl;
	if (x<=0 && y>=0)
		cout<<"Zero"<<endl;
	if (x<0 && y<0)
		if (plat(x,y))
			cout<<"Negative"<<endl;
		else
			cout<<"Positive"<<endl;
	return 0;
}

Measured memory_bytes: 256
def judge(N,Y):
    a,b,c = 10000,5000,1000
    if Y % c != 0 or Y < c or N*a < Y:
        return (-1,-1,-1)
    else:
        a,b,c,Y = a//1000,b//1000,c//1000,Y//1000
        for s in range(N+1):
            for t in range(N-s+1):
                u = N-s-t
                if a*s + b*t + c*u == Y:
                    return (s,t,u)
        return (-1,-1,-1)

def main():
    N,Y = map(int,input().split())

    ans = judge(N,Y)
    print(" ".join(map(str,ans)))

if __name__ == "__main__":
    main()


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

int main()
{
	int N, V, P;
	int64_t M;
	scanf("%d%lld%d%d", &N, &M, &V, &P);
	std::vector<int64_t> A(N);
	for (auto& e: A) scanf("%lld", &e);
	std::sort(A.begin(), A.end());
	std::vector<int64_t> sum(A);
	for (int i{1}; i < N; i++)
		sum[i] += sum[i - 1];
	int ok{N - 1}, ng{-1};
	while (ok - ng > 1)
	{
		int mid{(ok + ng) >> 1};
		if (N - mid <= P)
			ok = mid;
		else if (A[mid] + M < A[N - P])
			ng = mid;
		else
		{
			int64_t rest{M * (V - 1)};
			for (int i{N - 1}; i > mid && rest > 0; i--)
			{
				if (i > N - P) rest -= M;
				else rest -= A[mid] + M - A[i];
			}
			for (int i{}; i < mid && rest > 0; i++)
				rest -= M;
			if (rest <= 0) ok = mid;
			else ng = mid;
		}
	}
	printf("%d\n", N - ok);

	return 0;
}

Measured memory_bytes: 1792
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, H = in_nn()
    ab = []
    for i in range(N):
        a, b = in_nn()
        ab.append((a, 0))
        ab.append((b, 1))

    ab = deque(sorted(ab, reverse=True))

    count = 0
    while H > 0:
        d, x = ab.popleft()
        if x == 1:
            H -= d
            count += 1
        else:
            count += -(-H // d)
            break

    print(count)


if __name__ == '__main__':
    main()


Measured memory_bytes: 32968
#include <iostream>
using namespace std;
int main(void){
   int i,j,k,l,n,b,f,r,v;
   int inf[5][4][11];
   cin >> n;
   for(i=1;i<=4;i++){
       for(j=1;j<=3;j++){
           for(k=1;k<=10;k++){
               inf[i][j][k]=0;
           }
       }
   }
   for(i=0;i<n;i++){
       cin >> b >> f >> r >> v;
       inf[b][f][r]+=v;
   }
   for(i=1;i<=4;i++){
       for(j=1;j<=3;j++){
           for(k=1;k<=10;k++){
               cout <<" "<< inf[i][j][k] ;
           }
           cout << endl;
       }
       if(i!=4){
           for(l=0;l<20;l++) cout << "#";
           cout << endl;
       }
   }
}


Measured memory_bytes: 3088
K,N=[int(e) for e in input().split(' ')]
As=[int(e) for e in input().split(' ')]


i_max=0
d_max=0

for i in range(N):
    im1=(i-1)%N
    d=(As[i]-As[im1])%K
    if d > d_max:
        d_max=d
        i_max=i


ans=(As[(i_max-1)%N]-As[i_max])%K
print(ans)

Measured memory_bytes: 26420
INF = Float::INFINITY

def solve(n)
  
  return 0 if n == 0
  return D if n == 1
  return MEMO[n] if MEMO[n]
  
  res = INF
  
  if n % 2 == 0
    res = [res, A + solve(n/2)].min
  else
    res = [res, A + solve(n/2) + D].min
    res = [res, A + solve(n/2+1) + D].min
  end
  
  if n % 3 == 0
    res = [res, B + solve(n/3)].min
  else
    res = [res, B + solve(n/3) + D*(n%3)].min
    res = [res, B + solve(n/3+1) + D*(3-n%3)].min
  end
  
  if n % 5 == 0
    res = [res, C + solve(n/5)].min
  else
    res = [res, C + solve(n/5) + D*(n%5)].min
    res = [res, C + solve(n/5+1) + D*(5-n%5)].min
  end
  
  res = [res, D * n].min
  MEMO[n] = res
  res
end

number_of_testcases = gets.to_s.to_i

number_of_testcases.times do
  MEMO = {}
  N, A, B, C, D, E = gets.to_s.split.map{|t|t.to_i}
  puts solve(N)
end

Measured memory_bytes: 17040
#include <bits/stdc++.h>
#define INF 2147483647
#define INF_LL 9223372036854775807
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
// 2 3 8 4
// 0 2 5 13 17
int N;

int solve(int m, vector<int> ls) {
	vector<int> dp(N+1,0);
	dp[0] = m;
	for (int i = 0; i < N; i++) {
		int p_n = 0;
		dp[i+1] = INF / 2;
		for (int j = i; j >= 0; j--) {
			p_n += ls[j];
			if (i == N - 1 && j == 0)continue;
			if (p_n >= m) {
				int high = max(dp[j], p_n);
				dp[i + 1] = min(dp[i + 1], high);
			}
		}
	}
	return dp[N] - m;
}

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

	int ans = INF / 2;
	for (int i = 0; i < N; i++) {
		int l = 0;
		for (int j = i; j < N; j++) {
			l += ls[j];
			ans = min(ans, solve(l, ls));
		}
	}

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


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

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

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

const int MAX = 1e5 + 5;
const int INF = 1001001001;
const int MOD = 1e9 + 7;

int main(){
    int K, T;
    cin >> K >> T;

    vector<int> a(T);
    rep(i, T) cin >> a[i];

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

    cout << max(0, a.back() - 1 - accumulate(a.begin(), a.end() - 1, 0)) << endl;
}

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

// @@ !! LIM()

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  ll N; cin >> N;
  ll cnt = 0;
  for (ll a = 1; a <= N; a++) {
    for (ll b = 1; b <= N; b++) {
      if (a * b < N) cnt++;
      else break;
    }
  }
  cout << cnt << endl;

  return 0;
}



Measured memory_bytes: 3656
/*raghav0307 - Raghav Gupta*/

#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define pb push_back
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

typedef long long ll;

int main(){
	fast_io();
	int n;	cin >> n;
	vector<int> graph[n+1];
	for(int i = 0; i < n-1; i++){
		int a, b;
		cin >> a >> b;
		graph[a].pb(b);
		graph[b].pb(a);
	}
	vector<int> c;
	for(int i = 0; i < n; i++){
		int temp;	cin >> temp;
		c.pb(temp);
	}
	sort(c.begin(), c.end(), greater<int>());
	set<pair<int, int> > s;
	pair<int, int > pii[n+1];
	for(int i = 1; i <= n; i++){
		s.insert({graph[i].size(), i});
		pii[i] = {graph[i].size(), i};
	}
	int ans = 0;
	int label[n+1];
	bool visited[n+1];
	memset(visited, false, sizeof(visited));
	for(int i = 0; i < n; i++){
		auto x = *s.begin();
		ans += x.ff*c.back();
		label[x.ss] = c.back();
		// cout << x.ss << "  " << c.back() << "\n";
		c.pop_back();
		s.erase(s.find(x));
		visited[x.ss] = true;
		for(auto v : graph[x.ss]){
			if(visited[v])	continue;
			s.erase(s.find(pii[v]));
			pii[v].ff--;
			s.insert(pii[v]);
		}
	}
	// auto x = *s.rbegin();
	// cout << x.ff << " " << x.ss << "\n";
	cout << ans << "\n";
	for(int i = 1; i <= n; i++){
		cout << label[i] << " ";
	}
	return 0;
}

Measured memory_bytes: 1536
def main():
    N, M = list(map(int, input().split()))
    AB = []
    for _ in range(N):
        ab = list(map(int, input().split()))
        AB.append(ab)
    count = 0
    AB = sorted(AB, key=lambda ab:ab[0])

    for ab in AB:
        a = ab[0]
        for b in range(ab[1]):
            count += a
            M -= 1
            if M == 0:
                print(count)
                return 0

main()


Measured memory_bytes: 29800
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

double p[3000];
double dp[3000][3000];
int main(){
	int n;scanf("%d",&n);
	rep(i,n){
		scanf("%lf",&p[i]);
	}
	dp[0][0]=1;
	rep(i,n)rep(j,i+1){
		dp[i+1][j]+=dp[i][j]*(1-p[i]);
		dp[i+1][j+1]+=dp[i][j]*p[i];
	}
	double ans=0;
	for(int i=n/2+1;i<=n;i++){
		ans+=dp[n][i];
	}
	printf("%.12lf\n",ans);
}

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

ll gcd(ll a, ll b) {
  ll tmp;
  while ( a%b != 0 ) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

map<ll, int> prime_factor(ll n) {
  map<ll, int> ret;
  for (ll i = 2; i * i <= n; i++)  {
    while ( n % i == 0 ) {
      ret[i]++;
      n /= i;
    }
  }
  if ( n != 1 ) { ret[n] = 1; }
  return ret;
}

int main() {
  ll a, b;
  cin >> a >> b;
  cout << prime_factor(gcd(a, b)).size() + 1 << endl;
  return 0;
}


Measured memory_bytes: 3580
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

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

#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 pb push_back

const int inf = 999999999;
const int mod = 1000000007;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int a[500], b[500], c[500], d[500];

int main(){
	int n; cin >> n;
	Rep(i, n - 1){
		cin >> a[i] >> b[i] >> c[i];
		Rep(j, i){
			if(d[j] > b[i]){
				d[j] = (d[j] + c[i] - 1) / c[i] * c[i];
			}else{
				d[j] = b[i];
			}
			d[j] += a[i];
		}
		d[i] = b[i] + a[i];
	}
	Rep(i, n){
		cout << d[i] << endl;
	}
	return 0;
}

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

  string S ;
  cin >> S ;
  ll A ;
  cin >> A ;
  rep(i,A){
    if(S[i] != '1' ){
      cout << S[i] ;
      return 0 ;
    }
    if(i == A-1){
      cout << S[i] ;
      return 0 ;
    }
  }
}

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

#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)

using ll = long long;
#define INF 1e9

int main(){
  int N;
  cin >> N;
  int sum = 0;
  int ans = -1;
  int diff;
  FOR(i, 1, N+1){
    sum += i;
    if(sum >= N){
      ans = i;
      diff = sum - N;
      break;
    }
  }
  FOR(i, 1, ans+1){
    if(i == diff){
      continue;
    }
    cout << i << endl;
  }
  return 0;
}


Measured memory_bytes: 256
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)

int main() {
    int D, T, S;
    cin >> D >> T >> S;
    if (D <= T * S) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

bool done = false;
map<int, int> mp;
string ans = "";

bool check(string s)
{
	int x = s.size() - 1;
	int y = s[s.size() - 1] - '0';
	for (int i = 0; i < s.size() - 1; i++)
	{
		if (x == i || y == s[i] - '0' || abs(i - x) == abs(s[i] - '0' - y))
			return false;
	}
	return true;
}

void solve(int ind)
{
	if (ind == 8)
	{
		done = true;
		return;
	}

	if (mp.count(ind))
	{
		int x = mp[ind];
		if (check(ans + to_string(x)))
		{
			ans += to_string(x);
			solve(ind + 1);
			if (done)
				return;
			else
				ans.pop_back();
		}
	}
	else
	{
		for (int x = 0; x < 8; x++)
		{
			if (check(ans + to_string(x)))
			{
				ans += to_string(x);
				solve(ind + 1);
				if (done)
					return;
				else
					ans.pop_back();
			}
		}
	}
}

int main()
{
	int k;
	cin >> k;
	while (k--)
	{
		int a, b;
		cin >> a >> b;
		mp[a] = b;
	}
	
	solve(0);

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			if (j == ans[i] - '0')
				cout << 'Q';
			else
				cout << '.';
		}
		cout << endl;
	}

	return 0;
}


Measured memory_bytes: 3292
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define AC ios::sync_with_stdio(0),cin.tie(0);

int main()
{
    AC
    int n,k;
    cin>>n>>k;
    vector<int> v(n),dp(n,1e9);
    for(int i=0;i<n;i++)
        cin>>v[i];
    dp[0]=0;
    for(int i=1;i<n;i++)
        for(int j=1;j<=k;j++)
            if(i-j>=0) dp[i]=min(dp[i-j]+abs(v[i]-v[i-j]),dp[i]);
    cout<<dp[n-1]<<'\n';
}

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

int main()
{
    string s;
    cin >> s;
    for (int i = 0; i < 3; i++)
    {
        if (s[i] == '9')
            s[i] = '1';
        else if (s[i] == '1')
            s[i] = '9';
    }
    cout << s << endl;
    return 0;
}

Measured memory_bytes: 256
import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
getInt = readInt <$> BS.getLine

main = do
    s <- getLine
    q <- getInt
    queues <- map words <$> replicateM q getLine
    putStrLn . rev s $ foldl solve (True, [], []) queues

rev s (b, l, r) | b = l ++ s ++ (reverse r)
                | not b = r ++ (reverse s) ++ (reverse l)

solve (b, l, r) q
    | t == '1'                        = (not b, l, r)
    | t == '2' && f == '1' && b       = (b, c : l, r)
    | t == '2' && f == '1' && (not b) = (b, l, c : r)
    | t == '2' && f == '2' && b       = (b, l, c : r)
    | t == '2' && f == '2' && (not b) = (b, c : l, r)
    where (t : _) = q !! 0
          (f : _) = q !! 1
          (c : _) = q !! 2

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

#define INF 0x7fffffff

int main() {
	int		a,b,c,i,j,k,n,m,x,y,ans = 0;
	int		tmp = INF;
	string	str;
	
	cin >> n >> m;
	
	ans = n*(n-1)/2 + m*(m-1)/2;
	cout << ans << endl;

}


Measured memory_bytes: 256
#include <stdio.h>
int main(void) {
int n,i;
int a[100];
scanf("%d", &n);
for(i=0;i<n;++i){
scanf("%d",&a[i]);
}
if( n > 0 ) {
printf( "%d", a[n-1]);
for(i=1;i<n;++i){
printf(" %d", a[n-i-1]);
}
}
putchar('\n');
return 0;
}

Measured memory_bytes: 1028
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <numeric>
#include <functional>

using namespace std;
#define ALL(x)              (x).begin(),(x).end()
typedef long long           ll;
#define REP(i, n)           for (ll i = 0 ; i < (ll)n ; ++i)
#define REPN(i, m, n)       for (ll i = m ; i < (ll)n ; ++i)
#define INF                 (ll)1e9
#define MOD                 (1000 * 1000 * 1000 + 7)
#define Ceil(x, n)          (((((x))+((n)-1))/n))
#define CeilN(x, n)         (((((x))+((n)-1))/n)*n)
#define FloorN(x, n)        ((x)-(x)%(n))
#define IsOdd(x)            (((x)&0x01UL) == 0x01UL)
#define IsEven(x)           (!IsOdd((x)))
#define M_PI                3.14159265358979323846
typedef pair<ll, ll>	    P;

/*-----------------------------------------------------------------------------
　処理
 -----------------------------------------------------------------------------*/
/* 約数の列挙 */
vector<ll> divisorExec(ll n)
{
	vector<ll> res;
	for (ll i = 1; i <= sqrt(n); i++){
		if ((n % i) == 0) {
			res.push_back(i);
			if ((i * i) != n) {
				res.push_back(n / i);
			}
		}
	}
	sort(res.begin(), res.end());
	return res;
}

ll calcOne(ll A, ll B)
{
	ll calc = 0;
	calc += A * A * A * A * A;
	calc -= B * B * B * B * B;
	return (calc);
}

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

	ll A, B;
	for (int i = -120; i <= 120; i++) {
		for (int j = -120; j <= 120; j++) {
			ll lbAns = calcOne(i, j);
			if (lbAns == X) {
				A = i;
				B = j;
				goto EXIT;
			}
		}
	}

EXIT:
	cout << A << " " << B << endl;
	return 0;
}


Measured memory_bytes: 3724
#include <stdio.h>

typedef long long ll;

int main(void)
{
	ll n, a, b, ans;

	scanf("%lld %lld %lld", &n, &a, &b);

	ll num = n / (a + b), rem = n % (a + b);

	ans = num * a;

	if (rem <= a)
		ans += rem;
	else
		ans += a;

	printf("%lld", ans);
	return 0;
}

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

int main() {
    int N; cin >> N;
    vector<long long> L(N);
    for (int i = 0; i < N; ++i) cin >> L[i];
    sort(L.begin(), L.end()); // L をソートしておく (二分探索に必要)

    long long res = 0;
    
    // a と b を固定
    for (int i = 0; i < N; ++i) {
        for (int j = i+1; j < N; ++j) {
            // c の動ける範囲の右端を求める
            int k = lower_bound(L.begin(), L.end(), L[i] + L[j]) - L.begin();

            // c の動ける範囲は、[j+1, k)
            res += max(k - (j+1), 0);
        }
    }
    cout << res << endl;
}

Measured memory_bytes: 384
#include<stdio.h>

#define M 100
#define whi 0
#define gra 1
#define bla 2

int n;
int T[M][M];
int color[M],d[M];
int Q[M],head=0,tail=0;

void enqueue(int x){
  Q[tail] = x;
  tail = (tail+1)%M;
}

int dequeue(void){
  int x;
  x = Q[head];
  head = (head+1)%M;
  return x;
}

void bfs(int s){
  int i,u,v;
  
  for(i=0; i<n; i++){
    color[i] = whi;
    d[i] = 1000000;
  }

  color[s] = gra;
  d[s] = 0;
  enqueue(s);

  while(head != tail){
    u = dequeue();
    for(v=0; v<n; v++){
      if(T[u][v] == 1 && color[v] == whi){
	color[v] = gra;
	d[v] = d[u]+1;
	enqueue(v);
      }
    }
    color[u] = bla;
  }
  
}

int main(){
  int i,j;
  int u,k,v;

  scanf("%d",&n);

  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
      T[i][j] = 0;
    }
  }

  for(i=0; i<n; i++){
    scanf("%d %d",&u,&k);
    u--;
    for(j=0; j<k; j++){
      scanf("%d",&v);
      v--;
      T[u][v] = 1;
    }
  }

  bfs(0);

  for(i=0; i<n; i++){
    if(d[i] == 1000000) d[i] = -1;
    printf("%d %d\n",i+1,d[i]);
  }

  return 0;
}



Measured memory_bytes: 2112
#include<stdio.h>
#include<string.h>
int main(void){
    int lens,lenp,i,j,hante=0;
    char s[101]={'\0'},p[101]={'\0'},sg[201]={'\0'};
  
    scanf("%s",s);
    scanf("%s",p);
    lens=strlen(s);
    lenp=strlen(p);
    strcat(sg,s);
    strcat(sg,s);
    for(i=0;i<lens;i++){
        for(j=0;j<lenp;j++){
            if(sg[i+j]!=p[j]) break;
        }
        if(lenp==(j)) hante=1;
    }
    if(hante) printf("Yes\n");
    else printf("No\n");
    return 0;
}

Measured memory_bytes: 524
h, w = map(int, input().split())
sheet = []

for j in range(h):
	lista = list(map(int, input().split()))
	lista.append(sum(lista))
	sheet.append(lista)

lista = []

for i in range(w+1):
	sum = 0
	for j in range(h):
		sum += sheet[j][i]
	lista.append(sum)
	
sheet.append(lista)

for i in range(h+1):
	text = ""
	for j in range(w+1):
		text += str(sheet[i][j]) + " "
	print(text[:-1])

Measured memory_bytes: 7704
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
#include <ctime>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define fs first
#define sc second
#define INF 1000000000
#define MOD 1000000007
#define EPS 0.00000001


int main(){
    int N, K; cin >> N >> K;
    vector<ll> X(N), Y(N);
    map<ll, ll> xm, ym;
    
    for(int i=0; i<N; i++){
        cin >> X[i] >> Y[i];
        xm[X[i]] = Y[i];
        ym[Y[i]] = X[i];
    }
    
    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    ll ans = 4 * (1LL<<60);
    
    
    
    for(ll i=0; i<N; i++){
        for(ll j=0; j<N; j++){
            for(ll k=0; k<N; k++){
                for(ll l=0; l<N; l++){
                    ll x1 = X[i];
                    ll x2 = X[N-1-j];
                    ll y1 = Y[k];
                    ll y2 = Y[N-1-l];
                    
                    ll cnt = 0;
                    for(auto m:xm){
                        ll x = m.fs;
                        ll y = m.sc;
                        if(x>=x1 && x<=x2 && y>=y1 && y<=y2) cnt++;
                    }
                    
                    
                    if(x1>=x2 || y1>=y2 || cnt != K) continue;
                    ll rec = (x2 - x1) * (y2 - y1);
                    ans = min(ans, rec);
                    
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}





Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0);
  int n,x;
  cin>>n;
  
  x=floor(n/1.08);
  if(floor(x*1.08)==n)
    cout<<x;
  else if(floor((x+1)*1.08)==n)
    cout<<x+1;
  else
  	cout<<":(";
  
  return 0;
}

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

int main() {
  int a,b,c;
  cin>>a>>b>>c;
  for(int i=0;i<100;i++){
    if((i*b+c)%a==0){
      cout<<"YES"<<endl;
      return 0;
    }
  }

cout<<"NO"<<endl;
  }


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
  int n,t,i,j,k,res=0;
  vector<int> v;
  decltype(v)::iterator it,it2;
  size_t pos;
  cin >> n;
  for(i=0;i<n;i++){
    cin >> t;
    v.push_back(t);
  }
  sort(v.begin(),v.end());
  for(i=2;i<n;i++){
    it2=v.begin();
    for(j=i-1;j>0;j--){
      t=v[i]-v[j]+1;
      it=lower_bound(it2,v.begin()+j,t);
      if(it==v.begin()+j)
        break;
      res+=distance(it,v.begin()+j);
      it2=it;
    }
  }
  cout << res << endl;
  return 0;
}

Measured memory_bytes: 256
// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

void testcase() {
    int a , b, c;
    cin >> a >> b >> c;

    int k; cin >> k;

    while (k > 0 && b <= a) {
        k--;
        b *= 2;
    }


    while (k > 0 && c <= b) {
        k--;
        c*=2;
    }

    if (b > a && c > b) cout << "Yes";
    else cout << "No";
}


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}

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

int main(){

    int n;
    int a,b;
    int ans=0;

    cin>>n;

    for (int i=0;i<n;i++){

        cin>>a>>b;
        ans+=b-a+1;

    }


    cout<<ans<<endl;

    return 0;
}

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

#define SIZE (750)

int dp[11][1 << 10][SIZE];

int main(void)
{
	int n;
	char buf[256];
	int s;
	
	memset(dp, 0, sizeof(dp));
	dp[0][0][0] = 1;
	for (int i = 0; i < 10; i++){
		for (int j = 0; j < (1 << 10); j++){
			for (int k = 0; k < SIZE; k++){
				for (int l = 0; l < 10; l++){
					if (((j >> l) & 1) == 0){
						dp[i + 1][j | (1 << l)][k + ((i + 1) * l)] += dp[i][j][k];
					}
				}
			}
		}
	}
	while (fgets(buf, 255, stdin) != NULL){
		sscanf(buf, "%d%d", &n, &s);
		int ans = 0;
		if (s < SIZE){
			for (int i = 0; i < (1 << 10); i++){
				ans += dp[n][i][s];
			}
		}
		else {
			ans = 0;
		}
		
		printf("%d\n", ans);
	}
	
	return (0);
}

Measured memory_bytes: 32000
from functools import reduce
import fractions

def main():
    import math
    k = int(input())
    ans = 0

    for i in range(1,k+1):
        for j in range(1,k+1):
            for l in range(1,k+1):
                ans += reduce(lambda a,b: math.gcd(a,b), [i,j,l])
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 73956
N = gets.chomp.to_i
LS = gets.chomp.split.map(&:to_i).sort

ans = 0
LS.size.times do |i|
  a = LS[i]
  (i + 1).upto(LS.size - 1) do |j|
    b = LS[j]
    cs = LS[(j + 1)..-1]
    index = cs.bsearch_index { |c| c >= a + b }
    index = cs.size if index.nil?
    ans += index
  end
end

puts ans 


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

struct Node{
    int num, c;
    bool operator<(const Node& obj)const{
        if(num == obj.num)return c < obj.c;
        return num < obj.num;
    }
};

struct Edge{
    Node to;
    int cost;
    bool operator<(const Edge& obj)const{
        return cost > obj.cost;
    }
};

map<Node, vector<Edge>> graph;
map<Node, int> mini;

int main(){
    int n, m;
    cin >> n >> m;
    for(int i=0;i<m;++i){
        int p, q, c;
        cin >> p >> q >> c;
        Node a = (Node){p, c}, b = (Node){q, c}, d = (Node){p, -1}, e = (Node){q, -1};
        graph[a].push_back((Edge){b,0});
        graph[b].push_back((Edge){a,0});
        graph[a].push_back((Edge){d,0});
        graph[d].push_back((Edge){a,1});
        graph[b].push_back((Edge){e,0});
        graph[e].push_back((Edge){b,1});
        mini[a] = INT_MAX;
        mini[b] = INT_MAX;
        mini[d] = INT_MAX;
        mini[e] = INT_MAX;
    }

    mini[(Node){1,-1}] = 0;
    priority_queue<Edge> que;
    que.push((Edge){(Node){1,-1}, 0});
    int ans = -1;
    while(!que.empty()){
        Edge curr = que.top(); que.pop();
        Node node = curr.to;
        if(node.num == n){
            ans = curr.cost;
            break;
        }
        for(auto e : graph[node]){
            int newcost = curr.cost + e.cost;
            if(mini[e.to] > newcost){
                mini[e.to] = newcost;
                que.push((Edge){e.to, newcost});
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 94836