akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using Graph = vector<vector<int>>;  // int <=> ll
using P = pair<int, int>;  // int <=> ll
const int INFTY = 1e9+7;

int main() {
  int K, X, Y;
  cin >> K >> X >> Y;
  int dist = abs(X) + abs(Y);
  
  if (K%2 == 0 && dist%2 == 1) {
    cout << -1 << endl;
    return 0;
  }

  if (K == dist) {
    cout << 1 << endl;
    cout << X << " " << Y << endl;
    return 0;
  }

  bool swaped = false;
  if (abs(X) > abs(Y)) {
    swap(X,Y);
    swaped = true;
  }
  // Y != 0
  int xs = 1, ys = 1;
  if (X < 0) xs = -1;
  if (Y < 0) ys = -1;

  if (K > dist) {
    int s, x1, y1, x2, y2;
    if (dist%2 == 0) {
      s = 2;
      int res = K-dist/2;  // (K*s-dist)/2

      x1 = -xs * res;
      y1 = ys * (K-res);
    }

    else {
      s = 3;
      int res = K+(K-dist)/2;  // (K*s-dist)/2

      x1 = 0;
      y1 = -ys * K;
      x2 = -xs * (res%K);
      y2 = -ys * (res%K);
    }

    if (swaped) {
      swap(x1,y1); swap(x2,y2); swap(X,Y);
    }

    cout << s << endl;
    cout << x1 << " " << y1 << endl;
    if (s == 3) cout << x2 << " " << y2 << endl;
    cout << X << " " << Y << endl;
    return 0;
  }

  int s = dist / K;
  if (dist%K != 0) {
    if ((K-dist%K)%2 == 0) s += 1;
    else s += 2;
  }

  int res = (K*s-dist)/2;  // K > res
  int x1, y1;
  vector<P> coord;

  x1 = -xs * res;
  y1 = ys * (K-res);
  P point = P(x1,y1);
  coord.push_back(point);

  rep(i,abs(X-x1)/K) {
    point.first += xs * K;
    coord.push_back(point);
  }
  rep(i,abs(Y-y1)/K) {
    point.second += ys * K;
    coord.push_back(point);
  }
  if (coord.size() != s) coord.push_back(P(X,Y));

  if (swaped) {
    rep(i,s) swap(coord[i].first, coord[i].second);
  }

  cout << s << endl;
  rep(i,s) cout << coord[i].first << " " << coord[i].second << endl;
  return 0;
}


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

const int MAXN = 2.1e5;
const int MAXM = 2.1e5;
int N, M;
int L[MAXN];
int R[MAXN];
vector<int> evts[MAXN];

const int S = 1 << 18;
int segVal[2*S];
int segMax[2*S];
void update(int i) {
	segMax[i] = (i >= S ? 0 : max(segMax[2*i], segMax[2*i+1])) + segVal[i];
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> R[i];
	}
	for (int i = 0; i < N; i++) {
		evts[L[i]].push_back(R[i]);
	}
	int ans = max(0, N-M);
	for (int r = 0; r <= M+1; r++) {
		segVal[S+r] = -(M+1-r);
	}

	for (int i = 2 * S - 1; i; --i) update(i);

	for (int l = 0; l <= M; l++) {
		for (int r : evts[l]) {
			for (int b = S+r+1; 1 < b; b /= 2) {
				if (b & 1) {
					--b;
					segVal[b] ++;
					update(b);
				}
			}
			for (int b = S+r; b; b /= 2) {
				update(b);
			}
		}
		ans = max(ans, segMax[1] - l);
	}
	cout << ans << '\n';

	return 0;
}


Measured memory_bytes: 14464
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    N = int(input())
    A, B = [0] * N, [0] * N
    for i in range(N):
        A[i], B[i] = map(int, input().split())

    #output
    import statistics as st
    import math

    p = st.median(A)
    q = st.median(B)

    # %%
    if N % 2 == 0:
        print(int((q-p)/0.5)+1)
    else:
        print(int(q-p)+1)

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Measured memory_bytes: 28872
#include <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <complex>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<58;
const double pi=acos(-1);
const double eps=1e-7;
const ll mod=1e9+7;
const int dx[8]={0,1,0,-1,1,1,-1,-1},dy[8]={1,0,-1,0,1,-1,1,-1};

int n,m;
vvi g;

vi a;

bool dfs(int u,int w,int b){
	a[u]=1;
	if(u==w) return 0;
	bool res=1;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(!a[v]&&(v!=w||!b)) res&=dfs(v,w,0);
	}
	return res;
}

int main(){
	cin>>n>>m;
	g=vvi(n);
	for(int i=0;i<m;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	int res=0;
	for(int i=0;i<n;i++) for(int j=0;j<g[i].size();j++){
		a=vi(n);
		if(dfs(i,g[i][j],1)) res++;
	}
	cout<<res/2<<endl;
	a=vi(n);
}

Measured memory_bytes: 256
package main

import "fmt"

func main() {
	var N int
	fmt.Scan(&N)
	num := N % 10
	hon := []int{2, 4, 5, 7, 9}
	pon := []int{0, 1, 6, 8}
	if contains(hon, num) {
		fmt.Println("hon")
	} else if contains(pon, num) {
		fmt.Println("pon")
	} else {
		fmt.Println("bon")
	}

}

func contains(s []int, e int) bool {
	for _, v := range s {
		if e == v {
			return true
		}
	}
	return false
}


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

#define int long long
#define INT int
#define MOD 1000000007

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
template <typename T>
void print(T x)
{
    std::cout << x << '\n';
}
typedef long long ll;
const long long INF = 1LL << 60;

signed main()
{

    int n;
    cin >> n;
    string c;
    cin>>c;
    deque<int>r;
    deque<int>w;

    r.push_back(-1);
    rep(i,n){
        if(c[i]=='R'){
            r.pb(i);
        }else{
            w.pb(i);
        }
    }
    w.push_back(999999);

    int ans = 0;
    while(w.front()<r.back()){
        int t1 = w.front();
        int t2 = r.back();
        w.pop_front();
        r.pop_back();
        w.push_back(t2);
        r.push_front(t1);
        ans++;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 5304
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    ll x,k,d;
    while(cin>>x>>k>>d)
    {
        ll req=abs(x)/d;
        ll res=1e18;
        if(req<=k)
        {
            ll y=abs(x)-(req*d);
            ll baki=(k-req)%2;
            if(baki)
            {
                res=min(res,abs(y+d));
                res=min(res,abs(y-d));
            }
            else
            {
                res=min(res,y);
            }
        }
        else
        {
            ll y=abs(x)-(k*d);
            res=min(res,y);
        }
        if((req+1)<=k)
        {
            req++;
            ll y=abs(abs(x)-(req*d));
            ll baki=(k-req)%2;
            if(baki)
            {
                res=min(res,abs(y+d));
                res=min(res,abs(y-d));
            }
            else
            {
                res=min(res,y);
            }
        }

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


Measured memory_bytes: 3652
N = int(input())
X = list(map(int,input().split()))

ans = float("inf")
for i in range(1,101):
    dis_total = 0
    for x in X:
        dis = 0
        dis = (x-i)**2
        dis_total += dis
    if dis_total < ans:
        ans = dis_total
print(ans)

Measured memory_bytes: 9180
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = int(input())
print(0)
l = input()
r = l
if l == "Vacant":
    exit()

ng,ok = 0,n
while True:
    mid = (ng + ok) // 2
    print(mid)
    q = input()
    if q == "Vacant":
        exit()
    if (q == l) ^ ((mid - ng)%2 == 1):
        ng = mid
        l = q
    else:
        ok = mid
        r = q

Measured memory_bytes: 3660
/*
Author: Hrujul Thumar
*/
#include <bits/stdc++.h>
#define int         long long
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define whilet()    int t; scanf("%d",&t); while(t--)
#define vp          vector<pair<int,int>,int>
#define pb          push_back
#define mii         map<int,int>
#define pii         pair<int,int>
#define msi         map<string,int>
#define psi         pair<string,int>
#define mpi         map<pair<int,int>,int>
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define ef          else if
#define MOD         1000000007
#define endl        '\n'
#define fastio       ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define pi          3.141592653589793238
const long long MAX_SIZE = 1000000000;
using namespace std;
signed main()
{
    fastio;
    int n;
    cin>>n;
    int eq=0;
    int f=0;
    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x==y)eq++;
        else eq=0;
        if(eq==3)f++;
    }
    if(f)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}



Measured memory_bytes: 3648
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <utility>
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PI 4*atan(1.0)
typedef long long ll;
typedef long double ld;
using namespace std;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};

int main(){
	multiset<int,greater<int>> tes;
	int ans=0;
	int N,T;
	int mini=INF;
	cin>>N>>T;
	for(int i=0;i<N;i++){
		int a;
		cin>>a;
		tes.insert(a-mini);

		mini=min(mini,a);
	}
	int MIN=*tes.begin();
	for(auto it=tes.begin();it!=tes.end();it++){
		if(*it!=MIN){
			cout<<ans<<endl;
			return 0;
		}
		else
			ans++;
	}
	return 0;
}

Measured memory_bytes: 4992
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

const int N = 2050;
const int mod = 1e9 + 7;

int n, m, K, dp[N], sum[N];

int main() {
  scanf("%d%d%d", &n, &m, &K);
  int t = (n + m - 1) / (K - 1);
  int lst = 1, cur = 0;
  long long ans = 0;
  for (int i = 0; i <= m; i ++) sum[i] = 1;
  for (int i = 1; i <= t; i ++) {
    for (int j = 0; j <= m; j ++) {
      dp[j] = sum[j] - (j >= K ? sum[j - K] : 0);
      if (dp[j] < 0) dp[j] += mod;
      int tm = i * (K - 1) - j;
      if (tm >= 0 && tm < n && tm % (K - 1) == (n - 1) % (K - 1) && j % (K - 1) == m % (K - 1))
        ans += dp[j] - (sum[j] - sum[j - 1]);
    }
    sum[0] = dp[0];
    for (int j = 1; j <= m; j ++)
      sum[j] = (sum[j - 1] + dp[j]) % mod;
  }
  printf("%lld\n", (ans % mod + mod) % mod);
  return 0;
}


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

int main() {
	int N,K,d,A,cnt;
	cin  >> N >> K;
	
	vector<int> a(N);
	for (int i = 0; i < K; i++) {
		cin >> d;
		for (int j = 0; j < d; j++) {
			cin >> A;
			a.at(A-1)++;
		}
	}

	for (int i = 0; i < N; i++) {
		if (a.at(i) == 0)
			cnt++;
	}

	cout << cnt << endl;

}


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

int main(){
    string s,t;
    cin >> s >> t;
    int ssize,tsize;
    ssize = s.length();
    tsize = t.length();
    int ans = tsize;
    for (int i = 0; i <= ssize-tsize; i++){
        int count = 0;
        for (int j = 0; j < tsize; j++){
            if (s[i+j] != t[j]){
                count++;
            }
        }
        ans = min(ans,count);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3612
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Main{
	static List<Edge>[] list;
	static long[] dist;
	static class Edge{
		int to;
		long cost;
		public Edge(int to, long cost){
			this.to=to;
			this.cost=cost;
		}
	}
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		list=new ArrayList[n];
		dist=new long[n];
		for(int i = 0; i<n; i++){
			list[i]=new ArrayList<Edge>();
		}
		for(int i = 0; i<n-1; i++){
			int a = sc.nextInt()-1;
			int b = sc.nextInt()-1;
			long c = sc.nextLong();
			list[a].add(new Edge(b,c));
			list[b].add(new Edge(a,c));
		}
		int q = sc.nextInt();
		int k = sc.nextInt()-1;
		dfs(k,-1,0);
		for(int i = 0; i<q; i++){
			int x = sc.nextInt()-1;
			int y = sc.nextInt()-1;
			System.out.println(dist[x]+dist[y]);
		}
	}
	static void dfs(int place,int front,long cost){
		dist[place]=cost;
		for(Edge e: list[place]){
				if(e.to!=front){
					dfs(e.to,place,cost+e.cost);
				}
		}
	}
}

Measured memory_bytes: 182768
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod;
#define N 200010
ll n,c[N],f[N];
ll p[N];
ll last[N];
int main(){
    for(int i=0;i<N;i++)last[i]=-1,p[i]=-1;
    cin>>n;
    for(int i=0;i<n;i++){
	cin>>c[i];
	if(~last[c[i]])p[last[c[i]]]=i;
	last[c[i]]=i;
	f[i]=0;
    }f[0]=1;
    for(int i=0;i<n-1;i++){
	if(~p[i]&&p[i]!=i+1)mad(f[p[i]],f[i]);
	mad(f[i+1],f[i]);
    }
    cout<<f[n-1]<<endl;
    return 0;
}



Measured memory_bytes: 6528
#include <stdio.h>

int main()
{
	int a,b,d,r;
	double f;

	scanf("%d %d", &a, &b);
	d = a/b;
	r = a%b;
	f = 1.0 * a / b;

	printf("%d %d %f", d,r,f);
	return 0;	
}

Measured memory_bytes: 636
N, K = map(int, input().split())
p = list(map(int, input().split()))
p_ave = [(x+1)/2 for x in p]

ans = sum(p_ave[:K])
temp, tempmax = 0, 0

for i in range(N-K):
    temp += p_ave[i+K] - p_ave[i]
    if temp > tempmax:
        tempmax = temp

print(ans + tempmax)

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

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// head

int dp(int n, int k, int curr, vi& memo, vi& heights) {
    if (curr <= 2)
        return memo[curr] = abs(heights[curr]-heights[1]);
    else if (memo[curr] == -1) {
        memo[curr] = INT_MAX;
        for (int i = 1; i <= k; i++) {
            if (curr - i > 0)
                memo[curr] = min(dp(n,k,curr-i,memo,heights) + abs(heights[curr]- heights[curr-i]),memo[curr]);
        }
    }

    return memo[curr];
}

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

    vector<int> heights(n+1);
    vector<int> memo(n+1,-1);

    for (long unsigned int i = 0; i < n; i++) {
        int num = 0;
        cin >> num;
        heights[i+1] = num;
    }

    cout << dp(n,k,n,memo,heights);
    return 0;
}


Measured memory_bytes: 13532
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
# from scipy.sparse.csgraph import floyd_warshall
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations, combinations_with_replacement
from heapq import heappop, heappush, heapify
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    s = list(input())
    t = list(input())
    n = len(s)
    cnt = 0
    for i in range(n):
        if s[i] != t[i]:
            cnt += 1
    print(cnt)
        
if __name__ == "__main__":
    main()

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

int main()
{
   int n,d;
   cin >> n >> d;
   int count=0;
   while(n--){
   		double a,b,result,k,l,m;
   		cin >> a >> b;
   		result=sqrt(a*a+b*b);
   		//cout << result << endl;
   		m=floor(result);
   		k=round(result);
   		l=ceil(result);
   		//cout << k << ' ' << d << ' ' << l << endl;
   		if(result<=double(d))
   			count++;
   	

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

Measured memory_bytes: 3808
// ABC76-C
#include <bits/stdc++.h>
#define endl "\n"
#define rep(i,n) for(int i=0;i<int(n);i++)
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ipair;
typedef pair<ll,ll> lpair;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v) //vectorの中身を見る
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);

    string s,t;
    cin>>s>>t;
    bool flag=true;
    int sl=s.size(),tl=t.size();
    int max_l=-1,max_r=-1;
    if(sl<tl) flag=false;
    else{
        for(int i=0;i<sl;i++){ //先頭
            int r=i+tl-1;
            if(sl<=r) break;
            int num=0;
            bool flag2=true;
            for(int j=i;j<=r;j++){
                if(s[j]!=t[num] &&s[j]!='?'){
                    flag2=false;
                    break;
                }
                num++;
            }
            if(flag2) max_l=i,max_r=r;
        }
        if(max_r==-1) flag=false;
    }
    if(flag){
        int num=0;
        for(int i=0;i<sl;i++){
            if(max_l<=i && i<=max_r) s[i]=t[num],num++;
            else{
                if(s[i]=='?') s[i]='a';
            }
        }
        cout<<s<<endl;
    }else{
        cout<<"UNRESTORABLE"<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#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 (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
#ifdef DEBUG
#define line() cerr << "[" << __LINE__ << "] ";
#define dump(i) cerr << #i ": " << i << " ";
#define dumpl(i) cerr << #i ": " << i << endl;
#else
#define line(i)
#define dump(i)
#define dumpl(i)
#endif
using namespace std;
int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    int ans = 1;
    while (2 * ans <= n)
    {
        ans *= 2;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
N = int(input())
a = [int(input()) for _ in range(N)] + [0]
ans = 0

for i in range(N):
    if a[i] != 0:
        add, mod = divmod(a[i], 2)
        ans += add
        if a[i + 1] != 0:
            a[i + 1] += mod

print(ans)

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

int main() {
    //
    long long num, k;

    cin >> num >> k;

    vector<long long> vec(num);
    vector<long long> vec2(num, 0);
    deque<long long> loop;
    for (int i = 0; i < num; i++) cin >> vec.at(i), vec.at(i)--;

    long long index = 0;
    while (true) {
        if (vec2.at(index) == 1) {
            while (loop.at(0) != index) {
                k--;
                loop.pop_front();

                if (k == 0) {
                    cout << loop[0] + 1 << endl;
                    return 0;
                }
            }
            break;
        }
        loop.push_back(index);
        vec2.at(index) = 1;
        index = vec.at(index);
    }

    cout << loop.at(k % loop.size()) + 1;

//
}


Measured memory_bytes: 7872
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.HashSet;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A=sc.nextInt();
    int B=sc.nextInt();
    int C=sc.nextInt();
    int K=sc.nextInt();
    int ans=0;
    while(true){
      if(K==0){
        break;
      }
     while(A>0){
       K--;
       A--;
       ans++;
       if(K==0){
         break;
       }
     }
     if(K==0){
       break;
     }
     while(B>0){
       K--;
       B--;
       if(K==0){
         break;
       }
     }
     if(K==0){
       break;
     }
     while(C>0){
       C--;
       K--;
       ans--;
       if(K==0){
         break;
       }
     }

    }


      System.out.println(ans);




  }
}


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

using namespace std;

string o, e;

void input()
{
  cin >> o;
  cin >> e;
}

void solve()
{
  string res = "";
  for(int i = 0; i < max(e.length(), o.length()); i++)
  {
    if(i < o.length()) res += o[i];
    if(i < e.length()) res += e[i];
  }
  cout << res;
}

int main()
{
  input();
  solve();
  return 0;
}

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

int main() {
  int a,b,c,d;
  string str;
  cin >> a >> b >> c >> d;
  
  if ((a + b) > (c + d)) {
    str = "Left";
  }
 else if ((a + b) == (c + d)){
   str = "Balanced";
  }
  else {
    str = "Right";
  }
  cout << str << endl;
}



Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

int main() {
  string s;
  cin >> s;
  cout << ((s == "ABC") ? "ARC" : "ABC");
}


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

int main() {
	int N, P[100000], count = 0;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> P[i];
	}
	for (int i = 0; i < N; i++) {
		if (P[i] == i + 1) {
			count++;
			i++;
		}
	}
	cout << count;
}

Measured memory_bytes: 640
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

int n,l,a[100001],ans;
bool ok;
int main()
{
	cin>>n>>l;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<n-1;i++){
		if(a[i]+a[i+1]>=l){
			ans=i+1;
			ok=1;
			break;
		}
	}
	if(!ok){
		puts("Impossible");
		return 0;
	}
	puts("Possible");
	for(int i=1;i<ans;i++){
		cout<<i<<endl;
	}
	for(int i=n-1;i>=ans;i--){
		cout<<i<<endl;
	}
}

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

struct BC {
	int B;
	long long int C;
	bool operator>(const BC rhs) const {
		return this->C > rhs.C;
	};
};

int main() {
		int N, M;
		cin >> N >> M;
		vector <long long int> v_A(N);
		for (int i = 0; i < N; i++) {
			cin >> v_A[i];
		}
		vector<BC> v_BC;
		for (int i = 0; i < M; i++) {
			BC bc;
			cin >> bc.B >> bc.C;
			v_BC.push_back(bc);
		}
		sort(v_A.begin(), v_A.end());
		sort(v_BC.begin(), v_BC.end(), greater<BC>());

			long long int ans = 0;
			int itr_AbiggerthanC = 0;

			int BC_itr = 0;
			for (int i = 0; i < N; i++) {
				if (v_A[i] < v_BC[BC_itr].C) {
					itr_AbiggerthanC++;
					ans += v_BC[BC_itr].C;
					if (--v_BC[BC_itr].B == 0) {
						BC_itr++;
						if (BC_itr >= v_BC.size()) {
							break;
						}
					}
				}
			}
			for (int i = itr_AbiggerthanC; i < N; i++) {
				ans += v_A[i];
			}
			cout << ans << endl;
	return 0;
}

//RE&TLE
//int main() {
//	int N, M;
//	cin >> N >> M;
//	vector <long long int> v_A(N);
//	for (int i = 0; i < N; i++) {
//		cin >> v_A[i];
//	}
//	vector<long long int> v_C;
//	for (int i = 0; i < M; i++) {
//		int B;
//		long long int C;
//		cin >> B >> C;
//		for (int j = 0; j < B; j++) {
//			v_C.push_back(C);
//		}
//	}
//	sort(v_A.begin(), v_A.end());
//	sort(v_C.begin(), v_C.end(), greater<long long int>());
//	
//	cout << "v_A:";
//	for (int i = 0; i < N; i++) {
//		cout << " " << v_A[i];
//	}
//	cout << endl << "v_C";
//	for (int i = 0; i < v_C.size(); i++) {
//		cout << " " << v_C[i];
//	}
//	cout << endl;
//	long long int ans = 0;
//	int itr_AbiggerthanC = 0;
//	for (int i = 0; i < N && i < v_C.size(); i++) {
//		if (v_C[i] > v_A[i]) {
//			ans += v_C[i];
//			itr_AbiggerthanC++;
//		}
//		else {
//			break;
//		}
//	}
//	for (int i = itr_AbiggerthanC; i < N; i++) {
//		ans += v_A[i];
//	}
//	cout << ans << endl;
//	return 0;
//}

//TLE
//int main() {
//	int N, M;
//	cin >> N >> M;
//	vector <long long int> card_num(N);
//	for (int i = 0; i < N; i++) {
//		cin >> card_num[i];
//	}
//	sort(card_num.begin(), card_num.end());
//	
//	for (int i = 0; i < M; i++) {
//		int B;
//		long long int C;
//		cin >> B >> C;
//		int change_count = 0;
//		for (auto itr = card_num.begin(); itr != card_num.end(); itr++) {
//			if (*itr < C) {
//				*itr = C;
//				change_count++;
//			}
//			if (change_count >= B) break;
//		}
//		sort(card_num.begin(), card_num.end());
//	}
//	long long int ans = 0;
//	for (auto itr = card_num.begin(); itr != card_num.end(); itr++) {
//		ans += *itr;
//	}
//	cout << ans << endl; 
//	return 0;
//}

Measured memory_bytes: 3188
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <tuple>
#include <set>
#include <map>
#include <functional>
#include <cmath>
#include <cstring>
using namespace std;
#define sci(n) scanf("%lld", &(n))
#define scd(n) scanf("%Ld", &(n))
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> pll;
int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	vector<int> v;
	string s; cin >> s;
	v.resize(s.length());
	for (int i = 0; i < v.size(); i++)v[i] = 0;
	int n = s.length();

	int l = 0, r = 1, rl = -1;
	while (r!=n) {
		while (!(s[r-1] == 'R'&&s[r] == 'L'))r++;
		rl = r - 1;
		r++;

		while (true) {
			if (r == n) { break; }
			if (s[r] != 'L') { break; }
			r++;
		}
		if ((r - l) % 2 == 0) {
			v[rl] = (r - l) / 2;
			v[rl + 1] = (r - l) / 2;
		}
		else {
			if ((rl-l) % 2 == 1) {
				v[rl] = (r - l) / 2;
				v[rl + 1] = (r - l) / 2 + 1;
			}
			else {
				v[rl] = (r - l) / 2+1;
				v[rl + 1] = (r - l) / 2;
			}
		}
		l = r;
	}


	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	
	return 0;
}

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

using namespace std;

int main(){
    int x;
    cin >> x;
    int eight[] = {105, 165, 195, 135, 189};

    int ans = 0;
    for(int a = 0; a < 5; a++){
        if(eight[a] <= x) ans++;
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String line = reader.readLine();
		String[] str = line.split(" ");
		String [] s = reader.readLine().split("");
		for (int i = 0; i < s.length; i++) {
			if (i == Integer.parseInt(str[1]) - 1) {
				s[i] = s[i].toLowerCase();
			}
			System.out.print(s[i]);
		}
		System.out.println();
//		String[] str = reader.readLine().split(" ");
//		int[] ab = new int[str.length];
//		for (int i = 0; i < str.length; i++) {
//			ab[i] = Integer.parseInt(str[i]);
//		}
	}
}

Measured memory_bytes: 21460
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define repr(i, a, n) for (ll i = n - 1; (ll)a <= i; i--)
#define lb lower_bound
#define ub upper_bound
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
const ll mod = 1000000007;
const ll INF = 1000000099;
const ll LINF = (ll)(1e18 + 99);
vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T>
T mpow(T a, T n)
{
    T res = 1;
    for (; n; n >>= 1)
    {
        if (n & 1)
            res = res * a;
        a = a * a;
    }
    return res;
}

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

signed main()
{
    cin.tie(0);ios::sync_with_stdio(false);
    ll n, ans = 0;
    cin >> n;

    vector<ll> v(n);
    vector<bool> used(n,false);
    multiset<ll> se;
    vector<ll> tpow(31, 1);
    rep(i, 1, 31)
    {
        tpow[i] = tpow[i - 1] * 2;
    }

    rep(i, 0, n)
    {
        cin >> v[i];
        se.insert(v[i]);
    }
    sort(all(v));

    while(!se.empty())
    {
        ll now=*(--se.end());
        ll need = *ub(all(tpow), now) - now;
        se.erase(se.lb(now));
        auto itr = se.lb(need);

        if(itr==se.end())continue;

        if (need == *itr)
        {
            se.erase(itr);//
            ans++;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 11264
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize
    }
    println!("{}", n * n * n);
}


Measured memory_bytes: 4352
#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

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

    int a = m / n;
    for(int i=a;i>0;i--){
        if(m%i == 0){
            cout << i << endl;
            break;
        }
    }

    return 0;
}

Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define pi acos(-1.0)
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P){if(P==0) return 1;
    if(P%2==0){ll t = jou(N, P/2);return t*t % mod;
    }return (N * jou(N, P-1)) % mod;}  //jou(n,mod-2)で逆元
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる




int main() {

  ll n;
  cin>>n;
  
  ll sum=0;
  ll x,sma; 

  vector<pair<int,int>> p={};
  vector<pair<int,int>> q={};  
  
  rep(i,n){
  string s;
  cin>>s;
  ll m=s.size();
  x=0;
  sma=5000000;
    rep(j,m){
      if(s[j]=='(') x++;
      else x--;
      sma=min(sma,x);
    }
  sum+=x;
    if(x>0) {
      auto kumi=mp(sma,x);          
      p.pb(kumi);
    }
    else {
      auto kumi=mp(sma-x,-x);    
      q.pb(kumi);
    }
  }  
  
  if(sum!=0) {
  cout << "No" << endl;
  return 0;
  }
  
  sort(all(p));
  reverse(all(p));
  sort(all(q));
  reverse(all(q));
  
  int s=p.size();
  int t=q.size();
  ll now = 0;
  rep(i,s){
    if(now+p[i].fi<0) {
    cout << "No" << endl;
    return 0;  
    }
    now+=p[i].se;
  }
  now=0;
  rep(i,t){
    if(now+q[i].fi<0) {
    cout << "No" << endl;
    return 0;  
    }
    now+=q[i].se;  
  }
  
  cout << "Yes" << endl;
}


Measured memory_bytes: 11224
#include <bits/stdc++.h> 
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}
const ll INF = 1LL << 60;


int main() {
    int n, m; cin >> n >> m;
    vector<vector<int>> g(n+1);
    rep(i, m) {
        int a, b; cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> v(n);
    v[0] = 1;
    Rep(i, 1, n) v[i] = i+1;
    int ans = 0;
    do{
        if(v[0] != 1) break;
        bool all = true;
        rep(i, n-1) {
            bool one = false;
            for(int x : g[v[i]]) if(x == v[i+1]) one = true;
            if(!one) all = false;
        }
        if(all) ans++;
    }while(next_permutation(all(v)));

    cout << ans << endl;
}

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

int main() {
    string S; cin >> S;
    int sa = 0, sb = 0, sc = 0;
    for(int i = 0; i < 3; i++) {
        if(S[i] == 'a') sa++;
        if(S[i] == 'b') sb++;
        if(S[i] == 'c') sc++;
    }
    if(sa == 1 && sb == 1 && sc == 1) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}


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

long long n, k;
string s;
vector<long long> v;

long long solve();

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

  cout << solve() << endl;
  return 0;
}

long long solve() {
  long long res = n;
  for(int i = 0; i < n;) {
    char nowc = s[i];
    long long cnt = 0;
    while(i < n && s[i] == nowc) ++i, ++cnt;
    v.push_back(cnt);
  }
  res -= v.size();
  return res + min(k * 2, (long long)(v.size() - 1));
}

Measured memory_bytes: 1528
import math

def delete_head_zeros(n):
	n = str(n)
	l = len(n)
	if "." in n:
		l = n.find(".")
	head_zeros = 0
	for i in range(l - 1):
		if n[i] == "0":
			head_zeros += 1
		else:
			break

	return n[head_zeros:]

# compare a, b
# a, b: int or string
def bigint_compare(a, b):
	a = delete_head_zeros(a)
	b = delete_head_zeros(b)
	if len(a) > len(b):
		return 1
	elif len(a) < len(b):
		return -1
	else:
		if a > b:
			return 1
		elif a < b:
			return -1
		else:
			return 0

# calculate a + b
# a, b: int or string
def bigint_plus(a, b):
	a = str(a)
	b = str(b)

	d = max([len(a), len(b)])
	a = '0' * (d - len(a)) + a
	b = '0' * (d - len(b)) + b

	ans = ""
	carry = 0
	for i in range(d):
		s = int(a[-i-1]) + int(b[-i-1]) + carry
		carry = s // 10
		ans = str(s % 10) + ans
	else:
		if carry:
			ans = str(carry) + ans

	return ans

# calculate a - b
# a, b: int or string
def bigint_minus(a, b): 
	a = str(a)
	b = str(b)
	M = []
	m = []
	sign = ""

	if len(a) > len(b) or (len(a) == len(b) and a >= b):
		[M, m] = [a, b]
	else:
		[M, m] = [b, a]
		sign = "-"
	m = '0' * (len(M) - len(m)) + m

	ans = ""
	borrow = 0
	for i in range(len(M)):
		s = int(M[-i-1]) - int(m[-i-1]) - borrow
		if s < 0:
			borrow = 1
			s += 10
		else:
			borrow = 0
		ans = str(s) + ans

	return sign + delete_head_zeros(ans)

# calculate a * b
# a, b: int or string
def bigint_multiply(a, b):
	a = str(a)
	b = str(b)

	md = []
	carry = 0
	for j in range(len(b)):
		mj = ""
		for i in range(len(a)):
			m = int(a[-i-1]) * int(b[-j-1]) + carry
			carry = m // 10
			mj = str(m % 10) + mj
		else:
			if carry:
				mj = str(carry) + mj
		md.append(mj)

	ans = 0
	for k in range(len(md)):
		ans = bigint_plus(md[k] + "0" * k, ans)

	return ans


# calculate a / b to d digits after decimal point
# a, b, d: int or string
def bigint_divide(a, b, d = 0):
	a = str(a)
	b = str(b)
	d = int(d)
	if d < 0:
		d = 0

	ans = ""
	r = ""
	for i in range(len(a) + d):
		q = 0
		if i < len(a):
			r += a[i]
		elif i == len(a):
			ans += "."
			r += "0"
		else:
			r += "0"

		if bigint_compare(r, b) == -1:
			ans += str(q)
		else:
			while bigint_compare(r, b) >= 0:
				r = bigint_minus(r, b)
				q += 1
			ans += str(q)

	return delete_head_zeros(ans)

A, B, C, D = [n for n in input().split(" ")]
lcm = bigint_divide(bigint_multiply(C, D), math.gcd(int(C), int(D)))
right = B
left = bigint_minus(A, 1)
allcnt = bigint_minus(right, left)
def mulN(x):
  return bigint_minus(bigint_divide(right, x), bigint_divide(left, x))

print(bigint_minus(bigint_plus(allcnt, mulN(lcm)), bigint_plus(mulN(C), mulN(D))))

Measured memory_bytes: 9360
N, W = [int(i) for i in input().split()]
w, v = [0]*N, [0]*N
 
for i in range(N):
  w[i], v[i] = [int(_) for _ in input().split()]
  
dp = [[0] * (W+1) for _ in range(N+1)]
# dp[i][j] i番目までの荷物で重さがjを超えないように選んだときの価値の最大値
for i in range(N):
  for j in range(W+1):
    dp[i+1][j] = max(dp[i+1][j], dp[i][j])
    if j + w[i] <= W:
      dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j] + v[i])
      
print(dp[N][W])

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

int main(){
    int N; cin>>N;
    string S; cin>>S;

    map<char,int> mapS;
    for(auto s:S) mapS[s]++;

    map<char,int> mapSubS;
    int ans=0;
    rep(i,0,N-1){
        int _ans=0;
        mapSubS[S.at(i)]++;
        for(auto _s:mapSubS){
            if(mapS[_s.first]-_s.second>0) _ans++;
        }

        ans=max(ans,_ans);
    }

    cout<<ans<<endl;
}


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

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1){
	for(int u : to[v]){
    	if(u == p) continue;
      	ans[u] += ans[v];
      	dfs(u, v);
    }
}

int main(){
	int n, q;
  	cin >> n >> q;
  	rep(i, n-1){
    	int a, b;
      	cin >> a >> b;
      	a--;
      	b--;
      	to[a].push_back(b);
      	to[b].push_back(a);
    }
  
  	ans.resize(n);
  	rep(i, q){
    	int p, x;
      	cin >> p >> x;
      	p--;
      	ans[p] += x;
    }
  	dfs(0);
  
  	rep(i, n){
    	cout << ans[i] << endl;
    }
}

Measured memory_bytes: 24328
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define PI 3.141592
#define LL_INF 9000000000000000000
#define LL_MINF -9000000000000000000
#define INT_INF 2000000000
#define INT_MINF -2000000000
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; i++) cin >> A[i];
    sort(all(A));
    cout << A.back() - A[0] << endl;
}


Measured memory_bytes: 3600
/*starting with the name of almighty ALLAH*/
/*solved by RAKIB JOY*/
// Practice is the only shortcut to improve
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define vc vector
#define vi vc<int>
#define vl vc<ll>
#define dbg(x) cout<<x<<endl;
#define mp(x,y) make_pair(x,y)
#define yes dbg("YES")
#define no dbg("NO")
#define tst int t;cin>>t;while(t--)
#define srt(v) sort(v.begin(),v.end());
#define rsrt(v) sort(v.rbegin(),v.rend());
#define rj ios::sync_with_stdio(false);cin.tie(0);
#define rvs(v) reverse(v.begin(),v.end());
#define F first
#define S second
#define mod 1000000007
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define PI 2*acos(0.0)
#define pii pair<int,int>
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define coutv(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define cinv(v) for(auto &it:v)cin>>it;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set =tree<T, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>;
*/
//sieve bool b[1000005];void primenire(){int i, j;b[0]=true;b[1]=true;for(i=4; i<=1000000; i+=2){b[i]=true;}for(i=3; i<=1001; i+=2){if(!b[i]){for(j=i*i; j<=1000001; j+=2*i){b[j]=true;}}}}
// power ll power(ll a,ll b){if(b==0)return 1LL;if(b&1)return a*power(a,b-1);else{ll x=power(a,b/2);return x*x;}}
//bigmod long long int m(long long int a,long long int b,long long int c){if(b==0)return 1;if(b%2==0){long long int  tmp=m(a,b/2,c);return ((tmp%c)*(tmp%c))%c;}elsereturn ((a%c)*(m(a,b-1,c)%c))%c;}
//MaxSubarraySum ll mss(vector<ll>v){ll mx=v[0];ll sz=v.size();for(int i=1;i<=sz;i++){ll t=v[i]+v[i-1];if(t>=v[i]) v[i]=t;mx=max(mx,v[i]*1LL);}return mx;}
//combination ll com[1001][1001];void combination(){com[1][1]=1;com[1][0]=1;for(ll i=2; i<=1000; i++){com[i][0]=1;for(ll j=1; j<=i; j++){com[i][j]=com[i-1][j]+com[i-1][j-1];}}}
///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------
vi v[100005];
bool vis[100005];
void dfs(ll n)
{
   if(vis[n]) return;
   vis[n]=1;
   for(auto it:v[n]) dfs(it);
}
void solve()
{
    ll a,b;
    cin>>a>>b;
    fr(i,1,b)
    {
       ll x,y;
       cin>>x>>y;
       v[x].pb(y);
       v[y].pb(x);
    }
    ll cnt=0;
    fr(i,1,a)
    {
       if(!vis[i])
       {
          dfs(i);
          cnt++;
       }
    }
    cout<<cnt-1<<endl;
}
int main()
{
    rj
    solve();
    return 0;
}


Measured memory_bytes: 9712
S = gets.chomp.reverse
T = ''
X = ['dreamer','dream','eraser','erase'].map(&:reverse)


allMatched = false
offset = 0
loop do
  matched = false
  X.each do |x|
    if x == S[offset, x.length]
      if offset+x.length == S.length
        allMatched = true
        break
      end
      matched = true
      offset += x.length
      break
    end
  end
  break if allMatched || !matched
end

if allMatched
  puts 'YES'
else
  puts 'NO'
end

Measured memory_bytes: 4092
#include <bits/stdc++.h>
using namespace std;
bool compare(int a, int b){
    return b<a;
}
int main(){
    int n;
    cin>>n;
    int sum = 0;
    vector<int> pos, neg;
    for (int i=0; i<n; i++){
        int x; cin>>x;
        if (x>=0)pos.push_back(x);
        else neg.push_back(x);
        sum += abs(x);
    }
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end(), compare);
    if (pos.size() == 0){
        cout<<sum+2*neg[0]<<endl;
        cout<<neg[0]<<" "<<neg[neg.size()-1]<<endl;
        int temp = neg[0] - neg[neg.size()-1];
        for (int i=1; i<neg.size()-1; i++){
            cout<<temp<<" "<<neg[i]<<endl;
            temp -= neg[i];
        }
    }
    else if (neg.size() == 0){
        if (pos.size() == 2){
            cout<<pos[1]-pos[0]<<endl;
            cout<<pos[1]<<" "<<pos[0]<<endl;
            return 0;
        }
        cout<<sum-2*pos[0]<<endl;
        cout<<pos[0]<<" "<<pos[pos.size()-1]<<endl;
        int temp = pos[0] - pos[pos.size()-1];
        for (int i=1; i<pos.size()-2; i++){
            cout<<temp<<" "<<pos[i]<<endl;
            temp -= pos[i];
        }
        cout<<pos[pos.size()-2]<<" "<<temp<<endl;
    }
    else{
        cout<<sum<<endl;
        int temp = neg[0];
        for (int i=0; i<pos.size()-1; i++){
            cout<<temp<<" "<<pos[i]<<endl;
            temp -= pos[i];
        }
        cout<<pos[pos.size()-1]<<" "<<temp<<endl;
        temp = pos[pos.size()-1] - temp;
        
        for (int i=1; i<neg.size(); i++){
            cout<<temp<<" "<<neg[i]<<endl;
            temp -= neg[i];
        }
    }

}

Measured memory_bytes: 3940
# include <iostream>
# include <vector>
# include <cstdio>
# include <queue>
using namespace std;
struct info
{
	int d = -1;
	bool vis = false;
};
vector<int> a[105];
info *ans;
queue<int> solve;
void bfs()
{
	solve.push(1); 
	ans[1].vis = true; ans[1].d = 0;
	while(!solve.empty())
	{
		int u = solve.front(); solve.pop();
		for(int j = 0;j < a[u].size();j++)
		{
			if(!ans[a[u][j]].vis)
			{
				ans[a[u][j]].vis = true;
				ans[a[u][j]].d = ans[u].d + 1;
				solve.push(a[u][j]);
			}
		}
	}
	
}
int main(void)
{
	int n,cnt,k,u;
	scanf("%d",&n);
	ans = new info [n+1];
	for(int w = 0;w < n;w++)
	{
		scanf("%d %d",&u,&cnt);
		for(int i = 0;i < cnt;i++)
		{
			scanf("%d",&k);
			a[u].push_back(k);
		}
	}
	bfs();
	for(int i = 1;i <= n;i++) printf("%d %d\n",i,ans[i].d);
	return 0;
}


Measured memory_bytes: 3272
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

result = 0
for a, i in zip(A, range(len(A))):
    result += B[a-1]
    if i > 0 and (a - A[i-1]) == 1:
        result += C[A[i-1]-1]
print(result)


Measured memory_bytes: 3064
n = input()
a =  [int(i) for i in input().split()]

min_num = -1

sum_num = 0
all_num = 0
all_num = sum(a)

for (i, num) in enumerate(a):
  sum_num += num
  all_num -= num
  if abs(all_num-sum_num) < min_num or min_num==-1:
    min_num = abs(all_num-sum_num)

print (min_num)

Measured memory_bytes: 38384
// Ballazt
//0a..brute force
//0b..greedy
//1..memorizatton of some sort	remember something cleverly
//2..sort
//3..binary search	keep check a particular solution
//4..dp				repeating strucutre,limited choices
//5..dfs/bfs		matrix with obstacles
//6..correlate to graph in some way		each state is a point
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long int
#define endl "\n"


int main()

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

	ll k,x;
	cin>>k>>x;

	if(k*500>=x) cout<<"Yes";
	else cout<<"No";

	



	
}

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

int main() {
  int64_t N, A, B, c = 0;
  cin >> N >> A >> B;
  if (N > (A+B)) {
    c = N / (A+B) * A;
    N = N % (A+B);
  }
  if (N < A) cout << c + N << endl;
  else cout << c + A << endl;
}


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

void solve(){
    string s;
    cin >> s;
    cout << (s == "ABC" ? "ARC" : "ABC") << '\n';
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}


Measured memory_bytes: 3640
X, Y = map(int, input().split())

def cmb(n, r, p):
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1] 

    for i in range(2, n+1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)

    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

s = X+Y

if s%3 != 0:
    print(0)
else:
    a = (s//3-(X-Y))//2
    print(cmb(s//3, a, 10**9+7))

Measured memory_bytes: 87976