akhauriyash/Code-Regression huggingface dataset:
{-# LANGUAGE DerivingStrategies   #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE RankNTypes           #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE TypeApplications     #-}
{-# LANGUAGE UndecidableInstances #-}
import           Control.Monad
import qualified Control.Monad.State.Strict   as SS
import qualified Data.Vector.Unboxing         as VU
import qualified Data.Vector.Unboxing.Mutable as VUM

main :: IO ()
main = do
  n <- readLn @Int

  print $ solve n

solve :: Int -> Int
solve n = flip SS.evalState 0 $ do
  let sv = sieve n
  forM_ [1..n-1] $ \i -> do
    let fs = product $ map (\(_,f) -> f+1) $ factorize sv (n-i)
    SS.modify' (+ fs)
  SS.get

-- solve n = sum $ flip map [1..n-1] $
--   \i -> VU.product $ VU.map (\(_,f) -> f+1) $ primeFactors (n-i)


-- 添字の数字の最初の素因数（Smallest Prime Factor, SPF）を格納した配列を作る(Eratosthenesの篩)
-- もし添字の数字と格納されている数が等しければ、素数
sieve :: Int -> VU.Vector Int
sieve n = VU.create $ do
  vec <- VUM.replicate (n+1) 0
  VUM.write vec 1 1
  forM_ [2..n] $ \i -> do
    b <- VUM.read vec i
    when (b == 0) $ do
      VUM.write vec i i
      forM_ [i*i,(i+1)*i..n] $ \j -> do
        c <- VUM.read vec j
        when (c == 0) $ VUM.write vec j i
  return vec


type Prime = Int
type Factor = Int

-- 予め作っておいたSPF配列を渡して、素因数分解を行う
factorize :: VU.Vector Int -> Int -> [(Prime,Factor)]
factorize sv n = go 1 (sv VU.! n) (n `div` sv VU.! n)
  where go _ 0 _ = []
        go _ 1 _ = []
        go a p m
          | m == 1 = [(p,a)]
          | otherwise = let m' = sv VU.! m
                        in if p == m'
                           then go (a+1) p (m`div`m')
                           else (p, a) : go 1 m' (m`div`m')



Measured memory_bytes: 12996
h,w = map(int,input().split())
n = int(input())
li = list(map(int,input().split()))
all_ans = []
for i,l in enumerate(li):
    bangou = i+1
    all_ans += [bangou]*l
#print(all_ans)
#[1, 1, 2, 3]
last = -1
for k in range(h):
    target = all_ans[last+1:last+1+w]
    if k%2 == 1:
        target.reverse()
    print(*target)
    last = last + w

Measured memory_bytes: 41584
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int,int>
#define mod 1000000007
#define endl "\n"

int power(int a,int b,int m=mod)
{
	if(b==0)	return 1;
	if(b==1)	return a;
	int res=power(a,b/2,m);
	res=(res*res)%m;
	if(b&1)	res=(res*a)%m;
	return res;
}

int modinv(int a){ return power(a,mod-2);}

void solve()
{
	int a,b,c;
	cin>>a>>b>>c;
	cout<<c<<" "<<a<<" "<<b<<endl;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int t=1;//cin>>t;
	while(t--)	solve();
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for(int i=0; i<(a); i++)
typedef long long ll;
const ll inf = 1LL<<60;

int main()
{
  ll n, k; cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(a.rbegin(),a.rend());
  ll ans = 0;
  for (int i=k; i<n; i++) {
    ans += a[i];
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int cnt = 0;
    string s;
    cin >> s;
    for (int i = 0; i < s.size() / 2; ++i) if (s.at(i) != s.at(s.size() - i - 1)) ++cnt;
    cout << cnt << endl;
    return 0;
}


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

constexpr ll DIVIDE = 1'000'000'007;

// 末尾最適化バージョン
constexpr ll fact_impl(ll acc, ll n)
{
    if (n == 1) {
        return acc;
    }
    return fact_impl(acc * n % DIVIDE, n - 1);
}

constexpr ll fact(ll n)
{
    // 末尾最適化バージョンの呼び出し
    return fact_impl(1, n);
}

void main_()
{
    ll N, M;
    cin >> N >> M;

    if (abs(N - M) >= 2) {
        cout << 0 << "\n";
        return;
    }

    auto ans = (fact(N) * fact(M)) % DIVIDE;
    if (N == M) {
        ans *= 2;
        ans %= DIVIDE;
    }
    cout << ans << endl;
}
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    main_();
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main (void)
{
    int X, happy = 0;

    scanf("%d", &X);

    happy += (X / 500) * 1000;
    X -= (X / 500) * 500;

    happy += (X / 5) * 5;

    printf("%d\n", happy);

    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld; 
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vi adj[222];
int col[222];
int d[222];

void dfs(int u)
{
	for(int v:adj[u])
	{
		if(col[v]==-1)
		{
			col[v]=col[u]^1;
			dfs(v);
		}
		else
		{
			if(col[v]==col[u])
			{
				cout<<-1<<'\n'; exit(0);
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	memset(col,-1,sizeof(col));
	for(int i=0;i<n;i++)
	{
		string s; cin>>s;
		for(int j=0;j<n;j++)
		{
			if(s[j]=='1') adj[i].pb(j);
		}
	}
	col[0]=0; dfs(0);
	int mx=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++) d[j]=int(1e9);
		d[i]=0;
		queue<int> q;
		q.push(i);
		while(!q.empty())
		{
			int u=q.front(); q.pop(); mx=max(mx,d[u]);
			for(int v:adj[u])
			{
				if(d[v]<int(1e9)) continue;
				d[v]=d[u]+1; q.push(v);
			}
		}
	}
	cout<<mx+1<<'\n';
}


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

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define each(i,c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define all(v) v.begin(), v.end()
#define mset(a, n) memset(a, n, sizeof(a))

typedef long long ll;

const int INF = 1000000000;
const int MOD = 1000000007;

int main(int argc, char *argv[]) {
    // cin.tie(0);
    // ios::sync_with_stdio(false);
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int ans = min(a, b) + min(c, d);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long long a,b,k,z;
  cin >> a >> b >> k;
  z = 0;
  long long taka = max(a-k,z);
  long long ao = max(b-max(k-a,z),z);
  cout << taka << " " << ao << endl;
}

Measured memory_bytes: 3628
#include <stdio.h>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef std::vector<ll> lls;
#define MIN(a, b) ( ((a)<(b)) ? (a) : (b) )
#define MAX(a, b) ( ((a)>(b)) ? (a) : (b) )

void cinv(lls& llVec, ll n) {
	ll val;
	llVec.reserve((size_t)(n));
	for (ll i = 0; i < n; i++) {
		cin >> val;
		llVec.push_back(val);
	}
}


int main()
{
	ll N;
	lls P;

	cin >> N;
	cinv(P, N);

	lls loc(N+1);
	for (ll lc = 0; lc < N; lc++) {
		ll number = P[lc];
		loc[number] = lc;
	}
	std:set<ll> locB;
	locB.insert(loc[N]);
	locB.insert(-2);
	locB.insert(-1);
	locB.insert(N);
	locB.insert(N+1);

	ll A, B, C, D, E,sum=0;
	for (int i = N - 1; i >= 1; i--) {

		//数字iの場所lcをlocBに入れる
		ll lc = loc[i];
		C = lc;
		auto result = locB.insert(lc);
		auto idxit = result.first;
		
		//A～Eを得る
		auto it = idxit;
		C = *it;
		it--;
		B = *it;
		it--;
		A = *it;
		it = idxit;
		it++;
		D = *it;
		it++;
		E = *it;

		//右側に大きいものがある場合を計算
		if (D != N) {
			sum += (C - B) * (E - D) * i;
		}
		//左側に大きいものがある場合を計算
		if (B != -1) {
			sum += (B - A) * (D - C) * i;
		}
	}

	cout << sum << '\n';

	return 0;
}



Measured memory_bytes: 6528
n = int(input())
s = input()

result = 0
for i in range(1000):
    i = str(i).zfill(3)
    # print(i)
    # print(s.find(i[0]), s.find(i[1]), s.find(i[2]))
    index0 = s.find(i[0])
    if -1 < index0:
        index1 = s.find(i[1], index0+1)
        if -1 < index1:
            index2 = s.find(i[2], index1+1)
            if -1 < index2:
                # print(i, 'あたり')
                result += 1

print(result)

Measured memory_bytes: 9276
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
long long longpow(long long a, long long b) {
  if (b == 0) return 1;
  if (b == 1) return a%MOD;
  if (b % 2 == 1) return (a  * longpow(a, b - 1)) % MOD;
  ll t = longpow(a, b / 2)%MOD;
  return (t * t) % MOD;
}
long long modpow(long long a, long long n) {
  long long res = 1;
  long long mod = n + 2;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long inverse(long long x, long long modding) {
  return modpow(x, modding - 2);
}
ll comb(ll r, ll a) {
  ll R = r;
  REP(i, a - 1) {
    r--;
    R *= r;
    R %= MOD;
  }
  ll A = 1;
  for (int i = 1; i <= a; i++) {
    A *= i;
    A %= MOD;
  }
  return R * inverse(A, MOD) % MOD;
}


int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  ll ans = longpow(2,n) - 1;
  ans -= comb(n, a);
  ans -= comb(n, b);
  while (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <stdio.h>

  int main()
  {
    int w,h,x,y,r;
    scanf("%d %d %d %d %d",&w,&h,&x,&y,&r);
    if(((0 <= (x - r)) && (w >= (x + r))) && 
      ((0 <= (y - r)) && (h >= (y + r))))
    {
      printf("Yes\n");
    }else{
      printf("No\n");
    }
    
    return 0;
  }


Measured memory_bytes: 2088
function main(input = "") {
    let N = 0 | input;
    res = N % 1000;
    if ( res > 0){
        return 1000 - res;
    } else { 
        return 0;
    }    
}

//******************************
exports.main = main;

function Main(input) {
    console.log(main(input.trim()));
}

if (process.argv[2] !== "test") {
    Main(require("fs").readFileSync("/dev/stdin", "utf8"));
}


Measured memory_bytes: 30520
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#include <map>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

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

int main(void){
  vector<int> count;
  vector<string> word;
  string tmp;
  while(cin>>tmp){
    if(find(word.begin(),word.end(),tmp) != word.end()){
      int d = distance(word.begin(), find(word.begin(),word.end(),tmp));
      count[d]++;
    }else{
      word.push_back(tmp);
      count.push_back(1);
    }
  }
  int maxn = 0,maxl = 0, mni, mli;
  
  REP(i,count.size()){
    if(count[i] > maxn) {
      maxn = count[i];
      mni = i;
    }
    if(word[i].size() > maxl){
      maxl = word[i].size();
      mli = i;
    }
  }
  cout << word[mni] << " " << word[mli] << endl;
  return 0;
}

Measured memory_bytes: 0
import java.util.*;

// ABC 54-D
// https://beta.atcoder.jp/contests/abc054/tasks/abc054_d

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		int N = in.nextInt();
		int H = in.nextInt();
		
		long[] swingD = new long[N];
		long[] throwD = new long[N];
		long maxSwingD = 0;
		int maxSwingDIndex = 0;
		
		for (int i = 0; i < N; i++) {
			long sd = in.nextLong();
			long td = in.nextLong();
			
			if (maxSwingD < sd) {
				maxSwingD = sd;
				maxSwingDIndex = i;
			}
			
			swingD[i] = sd;
			throwD[i] = td;
		}

		ArrayList<Long> nums = new ArrayList<Long>();
		for (int i = 0; i < N; i++) {
			if (maxSwingD <= throwD[i]) {
				nums.add(throwD[i]);
			}
		}
		nums.sort(null);
		
		long answer = 0;
		for (int i = nums.size() - 1; i >= 0; i--) {
			H -= nums.get(i);
			answer++;
			if (H <= 0) {
				break;
			}
		}
		
		if (H <= 0) {
			System.out.println(answer);
			return;
		}
		
		answer += H / maxSwingD;
		if (H % maxSwingD != 0) {
			answer++;
		}
		System.out.println(answer);
	}
}


Measured memory_bytes: 91488
public class Main {
	public static void main(String[] args) {
		var sc = new java.util.Scanner(System.in);
		var S = sc.next();
		var T = sc.next();

		var res = T.length();
		for (var i = 0; i <= S.length() - T.length() ; i++) {
			var cnt = 0;
			for (var j = 0; j < T.length(); j++) {
				if (S.charAt(i + j) != T.charAt(j)) {
					cnt++;
				}
			}
			res = Math.min(res, cnt);
		}
		System.out.println(res);

	}
}


Measured memory_bytes: 35292
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-6;
const string YES = "Possible";
const string NO = "Impossible";
#define pb push_back
#define mp make_pair

const int MOD = 1e9+7;
const int MAX_N = 1e5+5;
int A[MAX_N];
int N;

void solve()
{
	sort(A, A+N);
	bool valid = true;
	if(N%2){
		if(A[0]) valid = false;	
	}else{
		if(A[0]!=1 || A[1]!= 1) valid = false;
	}
	int target = N%2?2:3;
	ll ans = N%2?1:2;
	for(int i=N%2?1:2; valid && i< N; i+=2){
		if(A[i] != target || A[i+1] != target){ 
		    valid = false;
		    break;
		}
		target+=2;
		ans = (ans * 2)%MOD;
	}
	if(!valid) cout << 0 << endl;
	else cout << ans << endl;
}

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

Measured memory_bytes: 640
xn = list(map(int, input().split()))
x = xn[0]
n = xn[1]
can = True
try:
    p = list(map(int, input().split()))
except:
    can = False

if can:
    num = []
    for i in range(0, 102): # x roop
        cnt1 = 0
        cnt2 = 0
        for j in range(n): # n roop
            if x-i==p[j]:
                cnt1 += 1
            else:
                pass

            if x+i==p[j]:
                cnt2 += 1
            else:
                pass

        if cnt1==0 and cnt2==0:
            num.append(x-i)
        elif cnt1==0:
            num.append(x-i)
        elif cnt2==0:
            num.append(x+i)
        else:
            pass

    min_abs = []
    for i in range(len(num)):
        min_abs.append([abs(x-num[i]), num[i]])

    min_abs.sort(key=lambda x: x[0])
    print(min_abs[0][1])
else:
    print(x)

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

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

    int mn = n, cnt = 0;

    for(int i = 0; i < n; i++){
        cin >> input;
        if(input < mn)
            mn = input;
        if(mn >= input)
            cnt++;
    }

    cout << cnt;


    return 0;
}


Measured memory_bytes: 3592
M = 10 ** 9 + 7

N = int(input())
A_list = list(map(int, input().split()))

total = sum(A_list)

m = 0
for i in range(N - 1):
	A = A_list[i]
	total -= A
	m = (m + A * total) % M

print(m)


Measured memory_bytes: 31488
#include <stdio.h>

int main()
{
  int a, b, ans;
  char operator;
  for (int i=1;;i++) {
    scanf("%d %c %d", &a, &operator, &b);
    if (operator == '+') printf("%d\n", a+b);
    else if (operator=='-') printf("%d\n", a-b);
    else if (operator=='*') printf("%d\n", a*b);
    else if (operator=='/') printf("%d\n", a/b);
    else if (operator=='?') break;
  }
  return 0;
}


Measured memory_bytes: 2100
#include <bits/stdc++.h>
using namespace std;
int main(){
  int H,W;
  cin >> H >> W;
  vector<vector<char>> a(H,vector<char>(W));
  vector<bool> R(H,false);
  vector<bool> C(W,false);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      cin >> a[i][j];
      if (a[i][j] == '#'){
        R[i] = true;
        C[j] = true;
      }
    }
  }
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (R[i] && C[j]) cout << a[i][j];      
    }
    if (R[i]) cout << endl;
  }
}

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

int main(){
  vector<int> v;
  int n;
  
  while(cin>>n){
    v.push_back(n);
  }

  sort(v.begin(), v.end(), greater<int>());

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



Measured memory_bytes: 3140
#include<bits/stdc++.h>
using namespace std;
string a;
char b[100003];
int r=1;
int main(){
    cin>>a;
    int f=a.size()-1;
    for(int i=0;i<=f;i++)
       b[f-i]=a[i];
    for(int i=0;i<=f;i++){
        if(b[i]=='b')b[i]='d';
        else if(b[i]=='d')b[i]='b';
        else if(b[i]=='p')b[i]='q';
        else b[i]='p';
        if(b[i]!=a[i]){
           r=0;
           break;
        }
    }
    if(r==0)cout<<"No";
    else cout<<"Yes";
}

Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
int main() {
	int n;
	cin >> n;
	if (n == 1)cout << "Hello World" << endl;
	else {
		int a, b;
		cin >> a >> b;
		cout << a + b << endl;

	}

	return 0;
}


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

int main(){

    bool f1 = false, f2 = false, f3 = false;
    for (int i = 0; i < 3; i++){
        char a;
        cin >> a;
        if (a == 'a'){
            f1 = true;
        }else if(a == 'b'){
            f2 = true;
        }else if(a == 'c'){
            f3 = true;
        }
    }

    if (f1 == true && f2 == true && f3 == true){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    
    

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
using namespace std;

#define rep(i, s) for (int i = 0; i < s; ++i)
#define all(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define VEC(a, n) vector<int>a(n)
#define PQ(a) priority_queue<int>a
#define PQmin(a) priority_queue< int, :vector<int>, greater<int> >a
#define PAIR pair<int, int>
/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

using Edge = pair<int,int>;
using Graph = vector<vector<Edge> >;

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

    Graph G(n);

    for(int i=0;i<n-1;i++){
        int a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        G.at(a).push_back(Edge(b,c));
        G.at(b).push_back(Edge(a,c));
        // cout<<"BBB"<<endl;
    }
    vector<int>dist(n,-1);
    queue<int>que;
    // cout<<"AAA"<<endl;


    dist.at(0)=0;
    que.push(0);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(auto next_v : G[v]){
            if(dist[next_v.first]!=-1)continue;
            dist[next_v.first] = (dist[v] + next_v.second) % 2;
            que.push(next_v.first);
        }
    }

    for(int i=0;i<n;i++){
        cout << dist[i] << endl;
    }






    return 0;
}


Measured memory_bytes: 6784
import scala.collection.immutable.Queue
import java.util.Scanner

object Main {

  implicit class RichString(string: String) {
    def readFirstLine: (Int, Int) = {
      val t = string.split(' ')
      (t(0).toInt, t(1).toInt)
    }
    def line2Process: Process = {
      val t = string.split(' ')
      Process(t(0), t(1).toInt)
    }
  }

  case class Process(name: String, time: Int)

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val (n, cycleTime): (Int, Int) = sc.nextLine().readFirstLine
    val pList = List.fill(n)(sc.nextLine().line2Process)
    val pQueue = Queue(pList: _*)
    genRound(cycleTime)(pQueue, 0)
  }

  def genRound(cycleTime: Int): (Queue[Process], Int) => Unit =
    round(_, _)(cycleTime)

  def round(q: Queue[Process], time: Int)(implicit cycleTime: Int): Unit = {
    q.dequeueOption match {
      case None => ()
      case Some((Process(n, t), nextQueue)) if t - cycleTime <= 0 => {
        val nowTime = time + t
        println(s"$n $nowTime")
        round(nextQueue, nowTime)
      }
      case Some((Process(n, t), nextQueue)) =>
        round(nextQueue.enqueue(Process(n, t - cycleTime)), time + cycleTime)
    }
  }
}



Measured memory_bytes: 252688
R, G, B, N = map(int, input().split())

ans = 0
rsum = 0
for r in range(0, N+1):
    if N < rsum:
        break
    rgsum = rsum
    for g in range(0, N+1):
        if N < rgsum:
            break
        left = N - rgsum
        if left % B == 0:
            ans += 1
        rgsum += G
    rsum += R

print(ans)


Measured memory_bytes: 40684
#include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){cin >> first;IN(rest...);return;}
#define SS(T, ...)      T __VA_ARGS__; IN(__VA_ARGS__);
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
/*--Output/////////////////////////////////////////////////*/
inline int p(void){cout << endl; return 0;}
template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;}
struct ProconInit {
    static constexpr bool AUTOFLUSH = false;
    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define all(x) (x).begin(),(x).end()
#define bit(n) (1LL<<(n))
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; }
bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; }

const int MOD = 1000000007;

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

signed main() {
    vector<pair<ll,ll>>T;
    SS(ll,N);
    rep(i,N){
        SS(ll,x,l);
        T.emplace_back(make_pair(x-l,x+l));
    }
    sort(all(T),cmp);
    ll max_x=-MOD;
    ll ans=0;
    rep(i,T.size()){
        //cout<<T[i].first<<" "<<T[i].second<<endl;
        if(max_x>T[i].first)ans++;
        else chmax(max_x,T[i].second);
    }
    p(N-ans);
}

Measured memory_bytes: 2420
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int ctoi(char c) {return c-'0';}// char to int
int gcd(ll a,ll b){return b?gcd(b,a%b):a;}//MAX
int lcm(ll a,ll b){return a*b/gcd(a,b);}//MIN
 string s;
int  main(){
string ans ="AKIHABARA";

cin >> s;
  for(int i=0;i<9;i++){
    if(s[i]!=ans[i]) {
    s.insert(i,"A");
    }
  }
if(s==ans)cout<<"YES"<<endl;
else{
    cout << "NO"<<endl;
}
}
 


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    E=[[] for _ in range(n)]
    for _ in range(n-1):
        a,b=map(int,input().split())
        a-=1; b-=1
        E[a].append(b)
        E[b].append(a)

    # 0からn-1へのpathを求める
    path=[]
    def dfs(v,p=-1)->bool:
        if(v==n-1):
            path.append(v)
            return True
        for nv in E[v]:
            if(nv==p): continue
            if(dfs(nv,v)):
                path.append(v)
                return True
        return False
    dfs(0)
    path.reverse()
    k=len(path)
    a,b=path[(k+1)//2-1],path[(k+1)//2]

    # 辺a,bを削除する
    E[a].remove(b)
    E[b].remove(a)

    # このグラフに対して、0を含む連結成分の個数を求める
    from collections import deque
    Q=deque([0])
    visited=[0]*n
    while(Q):
        v=Q.popleft()
        visited[v]=1
        for nv in E[v]:
            if(visited[nv]): continue
            Q.append(nv)

    c=sum(visited)
    print("Fennec" if(c>n-c) else "Snuke")
resolve()

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

	int main() {
	    int N,W; cin >> N >> W;
	    vector<vector<long long int> > K(2,vector<long long int>(W+1,0));
	    vector<int> wt(N);
	    vector<int> v(N);
	    for (int i = 0; i < N; ++i) cin >> wt[i] >> v[i];
	    for (int i = 1; i <= N; ++i)
	    {
	    	for (int j = 1; j <= W; ++j)
	    	{
	    		K[1][j] = K[0][j];
	    		if(wt[i-1]<=j)
	    			K[1][j] = max(v[i-1]+K[0][j-wt[i-1]],K[0][j]);	
	    	}
	    	K[0]=K[1];
	    }
	 	    cout << K[1][W];

	}


Measured memory_bytes: 2560
#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
#define dis(p,q) ((p)*(p)+(q)*(q))
int main(){
  int i,j,k;
  int n;
  while(cin>>n,n){
    double x[300],y[300];
    for(i=0;i<n;++i)
      cin>>x[i]>>y[i];    
    int mx=1;
    for(i=0;i<n-1;++i){
      for(j=i+1;j<n;++j){
	double d=dis(x[i]-x[j],y[i]-y[j]);
	if(d<=4){
	  double cx[2],cy[2],a=atan2(y[i]-y[j],x[i]-x[j]);
	  int sm[2]={2,2};
	  d=sqrt(1-d/4);
	  cx[0]=(x[i]+x[j])/2+cos(a+M_PI/2)*d;
	  cy[0]=(y[i]+y[j])/2+sin(a+M_PI/2)*d;
	  cx[1]=(x[i]+x[j])/2+cos(a-M_PI/2)*d;
	  cy[1]=(y[i]+y[j])/2+sin(a-M_PI/2)*d;
	  for(k=0;k<n;++k){
	    if(k!=i&&k!=j){
	      if(dis(x[k]-cx[0],y[k]-cy[0])<=1)
		++sm[0];
	      if(dis(x[k]-cx[1],y[k]-cy[1])<=1)
		++sm[1];
	    }
	  }
	  mx=max(max(mx,sm[0]),sm[1]);
	}
      }
    }
    cout<<mx<<endl;
  }
  return 0;
}

Measured memory_bytes: 972
#include <bits/stdc++.h>
 
using namespace std;
 
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("-ffloat-store")
 
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define endl "\n"
 
#define sz(a) ((int)(a).size())
#define all(v) (v).begin(), (v).end()
#define tr(c, it) for (auto(it) = (c).begin(); (it) != (c).end(); it++)
#define pres(c, val) ((c).find(val) != (c).end())      //
#define cpres(c, val) (find(all(c), val) != (c).end()) //
 
#define pb push_back
#define mp make_pair
#define F first
#define S second
 
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forb(i, a, b) for (int i = (b); i >= (a); i--)
#define fo(i, n) forf(i, 0, n)
#define fob(i, n) forb(i, 0, n - 1)
 
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
 
const int INF = 9e18;
// const int N = 1e9 + 7;
const int N = 998244353;
 
const auto start_time = std::chrono::high_resolution_clock::now();
 
void zark() {
  #ifdef ZARK
  auto end_time = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> diff = end_time - start_time;
  cerr << "Time Taken : " << diff.count() << "s\n";
  #endif
}
 
#ifdef ZARK
#include "/home/aranjan/Desktop/CP/header.h"
#else
#define dbg(args...)
#endif
 
/* ------------------ Actual Coding Starts ------------------ */

void solve() {
  int n;
  cin >> n;
  unordered_set<string> s;
  fo(i,n) {
    string ss;
    cin >> ss;
    s.insert(ss);
  }
  cout << sz(s) << endl;
}
 
int32_t main() {
  fastio;
  #ifdef ZARK
  freopen("input.in", "r", stdin);
  #endif
 
  // freopen("txt.in", "r", stdin);
  // freopen("txt.out", "w", stdout);
  // cout << fixed << setprecision(35);
 
  solve();
  zark();
  return 0;
}

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

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;

int main() {
    ll n;
    cin >> n;
    ll stock = 0, money = 1000;

    vector<ll> A(n + 1);
    rep(i, n) cin >> A[i];

    rep(i, n - 1) {
        if (A[i] < A[i + 1]) {
            stock = money / A[i];
            money %= A[i];
            money += stock * A[i + 1];
        }
    }

    cout << money << endl;

    return 0;
}


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

using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ldouble = long double;
 
const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;

ll res = inf;

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

    vector<ll> a(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    ll m;
    cin >> m;
    for (ll i = 0; i < m; i++)
    {
        ll x, y;
        cin >> x >> y;
        ll temp = a[x - 1];
        a[x - 1] = y;

        println(accumulate(all(a), 0ll));
        a[x - 1] = temp;
    }
}


Measured memory_bytes: 3652
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
a = []
for i in range(N):
  l, r = map(int, input().split())
  a.append((l, r))
a.sort(key = lambda x: x[0] - x[1])
#print(a)
class BIT:
  def __init__(self, n):
    self.n = n
    self.data = [0] * (n + 1)
    self.el = [0] * (n + 1)
  def sum(self, i):
    s = 0
    while i > 0:
      s += self.data[i]
      i -= i & -i
    return s
  def add(self, i, x):
    self.el[i] += x
    while i <= self.n:
      self.data[i] += x
      i += i & -i
  def get(self, i, j = None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i)
  def lowerbound(self, s):
    x = 0
    y = 0
    for i in range(self.n.bit_length(), -1, -1):
      k = x + (1 << i)
      if k <= self.n and (y + self.data[k] < s):
        y += self.data[k]
        x += 1 << i
    return x + 1
fwk = BIT(M + 1)
for l in range(1, M + 1):
  res = len(a)
  for x in range(l, M + 1, l): res += fwk.sum(x)
  while len(a):
    if a[-1][1] - a[-1][0] <= l:
      fwk.add(a[-1][0], 1)
      fwk.add(a.pop()[1] + 1, -1)
    else: break
  print(res)

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

#define F first
#define S second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 15, M = 10, mod = 1e9 + 7;

int n, a, b;

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

int C(int a, int b)
{
	int res = 1;
	for (int i = a; i > a - b; i -- ) res = (LL)res * i % mod;
	for (int i = 1; i <= b; i ++ ) res = (LL)res * qp(i, mod - 2) % mod;
	return res;
}

int main()
{
	cin >> n >> a >> b;
	
	int res = (qp(2, n) - 1 + mod) % mod;
	res = (res - C(n, a) + mod) % mod;
	res = (res - C(n, b) + mod) % mod;

	cout << res << endl;
	return 0;
}

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

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

using namespace std;

#define MAX 510
typedef long long ll;
int H,W,N;
int r[MAX][MAX], c[MAX][MAX];
int Y[MAX*MAX], X[MAX*MAX];

const bool debug = false;

vector<int> calc(vector<int> &A,vector<int> &B) {
  vector<int> C((int)A.size());
  rep(i,(int)A.size()) {
    C[i] = B[A[i]];
  }
  return C;
}

vector<int> tes(vector<vector<int> > &An,ll t) {
  vector<int> C(H*W);
  rep(i,H*W) C[i] = i;
  rep(i,20){
    if( (t>>i) & 1 ) {
      C = calc(An[i],C);
    }
  }
  return C;
}

bool check(ll t,vector<vector<int> > &An) {
  vector<int> C(H*W);
  rep(i,H*W) C[i] = i;
  rep(i,20){
    if( (t>>i) & 1 ) {
      C = calc(An[i],C);
    }
  }
  set<int> S;
  rep(i,N) {
    int x = X[i];
    int y = Y[i];
    if( S.count(C[y*W+x]) ) return false;
    S.insert(C[y*W+x]);
  }
  return true;
}

void compute() {
  vector<int> A(H*W);
  rep(i,H) rep(j,W) {
    int x = c[i][j];
    int y = r[i][j];
    A[i*W+j] = y * W + x;
  }
  if( debug ) {
    cout << "A = ";
    rep(i,(int)A.size()) cout << A[i] << " ";
    puts("");
  }
  vector<vector<int> > An;
  An.push_back(A);
  rep(i,20) {
    An.push_back(calc(An[i],An[i]));
  }
  if( debug ) {
    rep(i,5) {
      cout << "i = " << i << " (" << (1<<i) << ") :" << endl;
      rep(j,(int)An[i].size()) cout << An[i][j] << " ";
      puts("");
    }
  }
  
  if( debug ) {
    puts("--->");
    vector<int> C = tes(An,0);
    rep(i,H*W) cout << C[i] << " "; puts("");
    vector<int> a(H*W);
    rep(i,H*W) a[i] = i;
    a = calc(a,C);
    rep(i,H*W) cout << a[i] << " "; puts("");
    rep(i,N) {
      int x = X[i], y = Y[i];
      cout << x << "," << y << endl;
      cout << y*W+x << endl;
    }
    puts("<---");
  }

  ll L = 0, R = 300000LL;
  while( R - L ) {
    ll M = ( L + R ) / 2LL;
    if( check(M,An) ) L = M + 1LL;
    else              R = M;
  }
  if( L-1LL == 300000LL-1LL ) {
    puts("-1");
  } else {
    cout << L << endl;
  }
}

int main() {
  cin >> H >> W >> N;
  rep(i,H) rep(j,W) {
    cin >> r[i][j] >> c[i][j];
  }
  rep(i,N) cin >> Y[i] >> X[i];
  compute();
  return 0;
}

Measured memory_bytes: 41044
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <iomanip>

using namespace std;

typedef struct {
    int dice[6];

    void north() {
        swap(dice[0], dice[4]);
        swap(dice[0], dice[5]);
        swap(dice[0], dice[1]);
    }

    void east() {
        swap(dice[0], dice[2]);
        swap(dice[0], dice[5]);
        swap(dice[0], dice[3]);
    }

    void south() {
        swap(dice[0], dice[1]);
        swap(dice[0], dice[5]);
        swap(dice[0], dice[4]);
    }

    void west() {
        swap(dice[0], dice[3]);
        swap(dice[0], dice[5]);
        swap(dice[0], dice[2]);
    }

} DICE;


int main()
{
    DICE a;
    int n;
    int u[100], m[100];
    string s[6] = {"", "N", "W", "E", "S", "NN"};
    int right[6] = {0, 2, 4, 1, 3, 0};

    for (int i = 0; i < 6; i++) {
        int tmp;
        cin >> tmp;
        a.dice[i] = tmp;
    }

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> u[i] >> m[i];
        int ue, mae;
        for (int j = 0; j < 6; j++) {
            if (a.dice[j] == u[i]) {
                ue = j;
            }
        }
        for (int j = 0; j < s[ue].size(); j++) {
            if (s[ue][j] == 'N') {
                a.north();
            }
            if (s[ue][j] == 'E') {
                a.east();
            }
            if (s[ue][j] == 'S') {
                a.south();
            }
            if (s[ue][j] == 'W') {
                a.west();
            }
        }

        for (int j = 0; j < 6; j++) {
            if (a.dice[j] == m[i]) {
                mae = j;
            }
        }

        cout << a.dice[right[mae]] << endl;
    }
}



Measured memory_bytes: 3172
//Never stop trying
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//s.order_of_key(), *s.find_by_order()

using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;

typedef pair<int,int> pi;
#define fi first
#define se second

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;

#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

const int MOD = 1e9+7; //998244353
const ll INF = 1e18;
const int nx[4]={0,0,1,-1}, ny[4]={1,-1,0,0}; //right left down up

int32_t main(){
   boost;
   int p[35]; p[0]=1;
   for(int i=1; i<35; i++) p[i]=p[i-1]*2;
	int N; cin>>N;
	int t[N];
	for(int i=0; i<N; i++) cin>>t[i];

   int res[N]; for(int i=0; i<N; i++) res[i]=0;
	for(int b=30; b>=0; b--){
      int nb=0;
      for(int i=0; i<N; i++) if(((t[i])&(1<<b))) nb++;
      //cout << nb << endl;

      for(int i=0; i<N; i++){
         if(nb%2==0 && t[i]&(1<<b)) res[i]+=p[b];
         else if(nb%2==1 && !(t[i]&(1<<b))) res[i]+=p[b];
      }
      //for(int i=0; i<N; i++) cout << res[i]<< ' '; cout << endl;

	}
	for(int i=0; i<N; i++) cout << res[i]<< ' ';


   return 0;
}


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

#define ll long long

vector<ll> lunlun = {1, 2, 3, 4, 5, 6, 7, 8, 9};

void make_lunlun(ll a, int n) {
  if (n <= 10) {
    if (a % 10 == 0) {
      lunlun.push_back(a*10);
      make_lunlun(a*10, n+1);
      lunlun.push_back(a*10+1);
      make_lunlun(a*10+1, n+1);
      
    } else if (a % 10 == 9) {
      lunlun.push_back(a*10+8);
      make_lunlun(a*10+8, n+1);
      lunlun.push_back(a*10+9);
      make_lunlun(a*10+9, n+1);
    } else {
      lunlun.push_back(a*10+(a%10));
      make_lunlun(a*10+(a%10), n+1);
      lunlun.push_back(a*10+(a%10)-1);
      make_lunlun(a*10+(a%10)-1, n+1);
      lunlun.push_back(a*10+(a%10)+1);
      make_lunlun(a*10+(a%10)+1, n+1);
    }
  } 
}

int main() {
  int K;
  cin >> K;
  
  for (ll i = 1; i <= 9; i++) {
    make_lunlun(i, 1);
  }
  
  sort(lunlun.begin(), lunlun.end());
  
  cout << lunlun[K-1] << endl;
  
  
}

Measured memory_bytes: 8108
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <sstream>

using namespace std;

int main(){
    unsigned int N;
    cin >> N;
    vector<string> s(N);

    for(unsigned int i = 0; i < N; ++i){
        cin >> s[i];
        sort(s[i].begin(), s[i].end());
    }

    unsigned long long sum = 0;
    /* 
    for(unsigned int i = 0; i < N; ++i){
        for(unsigned int j = i + 1; j < N; ++j){
            if(s[i] == s[j]) sum++;
        }
    }
    cout << sum << endl;
    */

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

    vector<unsigned long long> combList;
    unsigned int cnt = 1;
    for(unsigned int i = 0; i < N - 1; ++i){
        if(s[i] == s[i+1]) cnt++;
        else{
            if(cnt > 1) combList.push_back(cnt);
            cnt = 1;
        }
    }
    if(cnt > 1) combList.push_back(cnt);

    for(unsigned int i = 0; i < combList.size(); ++i){
        sum += combList[i] * (combList[i] - 1) / 2;
    }

    cout << sum << endl;

    return 0;
}

Measured memory_bytes: 7552
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
int main(){
    int w;
    string s;
    cin >> s;
    cin >> w;
    for(int i = 0; i < s.size(); i++){
        if(i%w==0){
            cout << s[i];
        }
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 256
//! 尺卂乃卂.卂ﾌ乇乇ㄒ
#include<bits/stdc++.h>
using namespace std;

#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define vii             vector<vector<int>>
#define mii             map<int,int>
#define mip             map<pair<int,int>,int>
#define mic             map<char,int> 
#define all(v)          v.begin(),v.end()  
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define itr(v)          for(auto it:v)
#define show(arr,n)     for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\n";
#define fi(i,n)         for(int i=0;i<n;i++)
#define fd(i,n)         for(int i=n-1;i>=0;i--)
#define fir(i,a,b)      for(int i=a;i<b;i++)
#define fin(s)          freopen(s,"r",stdin);  
#define fout(s)         freopen(s,"w",stdout);
#define MAXZ            200005
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

bool cmp(int a,int b) {return a>b;}
int expo(int a,int b,int m){
    if(b==0) return 1;
    if(b==1) return a;
    int val=(expo(a,b/2,m)*expo(a,b/2,m))%m;
    if(b & 1) return (val*a)%m;
    else return val;
}
int mmi(int a,int m){
    return expo(a,m-2,m);
}
int32_t main()
{
    FIO
    int n,m; cin>>n>>m;
    vii adj(n);
    fi(i,m){
        int l,r;cin>>l>>r;
        l--,r--;
        adj[l].pb(r);
        adj[r].pb(l);
    }
    int baap[n]={0};
    int p=1;
    int dis[n];
    fi(i,n) dis[i]=INT_MAX;
    baap[0]=-1;
    dis[0]=0;
    int h=0;
    queue<int> q;
    q.push(0);
    q.push(-1);
    while(!q.empty()){
        int c=q.front();
        q.pop();
        if(c==-1){
            if(q.empty()) break;
            q.push(-1);
            h++;
        }
        else{
            for(auto u:adj[c]){
                if(dis[u]==INT_MAX){
                    dis[u]=dis[c]+1;
                    baap[u]=c+1;
                    q.push(u);
                } 
            }   
        }
    }
    fi(i,n) {
        if(i>0 && baap[i]==0){
            p=0;
            break;
        }
    }
    if(p){
        cout<<"Yes\n";
        fir(i,1,n){
            cout<<baap[i]<<"\n";
        }
    }
    else{
        cout<<"No\n";
    }
    return 0;

}

Measured memory_bytes: 13624
module Memoizable
  def memoize(name)
    original = "__original_#{name}__"
    self.class.class_eval do
      alias_method original, name
      private original
      define_method(name) do |*args|
        $cache[args] ||= send(original, *args)
      end
    end
  end
end


include Memoizable

def solve(t, pos, bits)
  return 0 if t >= $max_t 
  sum = 0
  for s in $fld[pos]
    if t >= $sales[s][1] && t < $sales[s][2]
      if (bits & (1<<s)) != 0
        sum += $sales[s][0]
        bits ^= (1<<s)
      end
    end
  end
  res = 0
  for p in $mv[pos].each
    tmp = solve(t + 1, p, bits)
    res = [res, tmp].max
  end
  sum + res
end

memoize :solve

def prepare(pos, c)
  y, x = pos.divmod $w
  [[1, 0], [0, -1], [-1, 0], [0, 1]].each do |dx, dy|
    nx, ny = x + dx, y + dy
    if nx >= 0 && nx < $w && ny >= 0 && ny < $h && !$fld[p = ny * $w + nx].nil?
      unless c.nil? 
        $fld[p] += [c] unless $fld[p].include?(c)    
      else $mv[pos] += [p]
      end
    end
  end
end

while true
  $cache = {}
  $w, $h = gets.split(' ').map(&:to_i)
  break if $w == 0
  start = nil
  $fld = ($h*$w).times.map{ [] }

  for y in (0...$h)
    tmp = gets.split(' ')
    for x in (0...$w)
      c = tmp[x]
      start = y * $w + x if c == 'P'
      if ('0'..'9').include? c
        prepare(y * $w + x, c.to_i)
        $fld[y * $w + x] = nil
      end
    end
  end
  $mv = ($h*$w).times.map{ [] }
  for y in (0...$h)
    for x in (0...$w)
      unless $fld[y * $w + x].nil?
        prepare(y * $w + x, nil)
      end
    end
  end
  $n = gets.to_i
  $max_t = 0
  $sales = []
  $n.times do
    arr = gets.split(' ').map(&:to_i)
    $sales[arr[0]] = arr[1..-1]
    $max_t = [$max_t, arr[3]].max
  end
  p solve(0, start, (1 << $n) - 1)
end

Measured memory_bytes: 304304
class UnionFind():
    def __init__(self, n):
        self.parent = [-1 for _ in range(n)]
        # 正==子: 根の頂点番号 / 負==根: 連結頂点数

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x, y = self.find(x), self.find(y)
        if x == y:
            return False
        else:
            if self.size(x) < self.size(y):
                x, y = y, x
            self.parent[x] += self.parent[y]
            self.parent[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        x = self.find(x)
        return -self.parent[x]

    def is_root(self, x):
        return self.parent[x] < 0

n, m = map(int, input().split())
p = list(map(int, input().split()))
count = 0
pair_list = []
for i in range(m):
    array = list(map(int,input().split()))
    array[0] -=1 ; array[1] -= 1
    pair_list.append(array)

uf = UnionFind(len(p))

# 整数のペアを同じグループに結合する
for i in range(m):
    uf.unite(pair_list[i][0], pair_list[i][1])

# 経路圧縮をして、親要素を根に更新する
for i in range(n):
    uf.find(i)

# 順列pに含まれる要素piの値とindex値(+1)が一致、もしくは
# index[i] とindex[p[i]] が同じグループに所属しているときにcount+=1
for i in range(n):
    if p[i] == i+1:
        count += 1
    else:
        if uf.same(p[i]-1, p[p[i]-1]-1) is True:
            count += 1
        else:
            continue
print(count)


Measured memory_bytes: 32292
#include <stdio.h>
#include <string.h>
#include <utility>
#include <algorithm>
using namespace std;
#define F first
#define S second
typedef long long int ll;
constexpr int kN = int (2E5 + 10);
struct BIT {
	ll a[kN];
	void init() {memset(a, 0, sizeof(a));}
	void set(int pos, ll x) {
		while (pos < kN) {
			a[pos] = max(a[pos], x);
			pos += pos & -pos;
		}
		return ;
	}
	ll ask(int pos) {
		ll ans = 0;
		while (pos) {
			ans = max(ans, a[pos]);
			pos ^= pos & -pos;
		}
		return ans;
	}
};

int h[kN], a[kN];
pair<int, int> p[kN];
BIT bit;
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++) p[i] = {h[i], i};
	sort(p + 1, p + n + 1);
	bit.init();
	for (int i = 1; i <= n; i++) bit.set(p[i].S, bit.ask(p[i].S) + a[p[i].S]);
	printf("%lld\n", bit.ask(n));
}


Measured memory_bytes: 6424
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
const long long MOD=1e9+7;
// const long long MOD=998244353;
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};
#define LOCAL

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    cout << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();++itr){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
        --itr;
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();++itr){
        os << *itr;
        if (++itr!=s.end()) os << ',';
        --itr;
    }
    os << '}';
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    ll ans=1;
    vector<ll> A(N);
    cin >> A;
    for (int i=0;i<N;++i) if (A[i]==0){
        cout << 0 << '\n';
        return 0;
    }
    for (int i=0;i<N;++i){
        if (ans>IINF/A[i]){
            cout << -1 << '\n';
            return 0;
        }
        ans*=A[i];
    }
    cout << ans << '\n';
}

Measured memory_bytes: 3988
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll mod=1000000007;
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
int main() {
//cout << fixed << setprecision(10);
string s;
cin>>s;
ll ca=0;
ll n=s.size();
int f=0;
for(int i=0; i<n; i++){
if(f==0){
ca++;
f=1;
continue;}
else if(s.at(i)==s.at(i-1)){
ca++;
i++;
if(i==n) ca--;
f=0;}
else{
f=1;
ca++;}}
cout<<ca<<endl;}


Measured memory_bytes: 3740