akhauriyash/Code-Regression huggingface dataset:
//https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_I
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 100000

int N = 1;
int value[4*NUM];

void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(int update_left,int update_right,int new_value,int node_id,int node_left,int node_right){
	if(update_right < node_left || update_left > node_right)return;
	else if(update_left <= node_left && update_right >= node_right){
		value[node_id] = new_value;
	}else{
		if(value[node_id] != -BIG_NUM){
			value[2*node_id+1] = value[node_id];
			value[2*node_id+2] = value[node_id];
			value[node_id] = -BIG_NUM;
		}
		update(update_left,update_right,new_value,2*node_id+1,node_left,(node_left+node_right)/2);
		update(update_left,update_right,new_value,2*node_id+2,(node_left+node_right)/2+1,node_right);
	}
}

int getSum(int search_left,int search_right,int node_id,int node_left,int node_right){
	if(search_right < node_left || search_left > node_right){
		return 0;
	}else{
		if(value[node_id] == -BIG_NUM){
			int left = getSum(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
			int right = getSum(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);
			return left+right;
		}else{
			return (min(search_right,node_right)-max(search_left,node_left)+1)*value[node_id];
		}
	}
}


int main(){

	int n,q; cin >> n >> q;

	init(n);

	for(int i = 0; i <= 2*N-2; i++) value[i] = 0;

	int command,left,right,value;

	for(int loop = 0; loop < q; loop++){
		cin >> command;

		if(command == 0){ //update
			cin >> left >> right >> value;
			update(left,right,value,0,0,N-1);
		}else{
			cin >> left >> right;
			cout << getSum(left,right,0,0,N-1) << endl;
		}
	}
	return 0;
}




Measured memory_bytes: 4116
#include <bits/stdc++.h>
#define inc(l,i,r) for (int i=l;i<=r;i++)
#define dec(r,i,l) for (int i=r;i>=l;i--)
#define SIZE(V) ((int) V.size())
#define forE(p,u) for (edge *p=head[u];p;p=p->nxt)
#define forV(i,V) for (int i=0;i<SIZE(V);i++)
#define pb push_back
#define In inline
#define fi first
#define se second
#define mp make_pair
#define It iterator
#define ___ putchar(10);
#define cnt(x) __builtin_popcount(x)
#define show(x) cout << #x << " = " << x << endl;
#define mid ((l+r) >> 1)
#define N 400200
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

template<typename T>
inline void read(T &x) {
	static char c;
	for (c=getchar();!isdigit(c);c=getchar());
	for (x=0;isdigit(c);c=getchar()) x = (x<<1) + (x<<3) + c - '0';
}

struct edge {int to;  edge *nxt;} *head[N], e[N*2];

int edge_tot = 0;
inline void AE(int u,int v) {
	head[u] = &(e[edge_tot++] = (edge) {v,head[u]});
	head[v] = &(e[edge_tot++] = (edge) {u,head[v]});
}

int n, m, dep[N], f[N];

void dfs(int u,int from) {
	dep[u] = dep[from] + 1;
	forE(p,u) if (p->to != from) dfs(p->to,u);
}

int main() {
	scanf("%d",&n);
	inc(1,i,n-1) {
		int u, v;  scanf("%d%d",&u,&v);
		AE(u,v);
	}
	dfs(1,0);
	int pos = 0;
	inc(1,i,n) if (dep[i] > dep[pos]) pos = i;
	dfs(pos,0);
	inc(1,i,n) m = max(m, dep[i]);
	
	f[1] = 1;  f[2] = 0;
	inc(3,i,m) {
		if (!f[i-1] || !f[i-2]) f[i] = 1;
		else f[i] = 0;
	}
	puts(f[m] ? "First" : "Second");
}

Measured memory_bytes: 20096
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/

const int MAXN = 210000;

ll d;

int n;

ll a[MAXN];

int main() {
#ifdef FASTIO
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
	cin >> n >> d;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	vector<pair<ll, int>> vv;
	for (int i = 1; i < n; ++i) {
		ll x = a[i] + d * i;
		while (!vv.empty() && vv.back().first >= x)
			vv.pop_back();
		vv.emplace_back(x, i);
	}
	int now = 1;
	ll bst = a[0];
	ll ans = 0;
	for (int i = 0; i < vv.size(); ++i) {
		int nw = vv[i].second;
		ans += vv[i].first + bst;
		for (int j = now; j < nw; ++j) {
			ans += a[j] + min(vv[i].first - j * d, bst + j * d);
			bst = min(bst, a[j] - j * d);
		}
		bst = min(bst, a[nw] - nw * d);
		now = nw + 1;
	}
	cout << ans << "\n";
	return 0;
}




Measured memory_bytes: 7152
n = int(input())

p = list("zabcdefghijklmnopqrstuvwxy")


num = ""

while n > 26:
    a = p[n % 26]
    num += a
    if n % 26 == 0:
        n = n // 26 - 1
    else:
        n = n // 26

num += p[n%26]

new_num = num[::-1]
print(new_num)


Measured memory_bytes: 9172
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
using namespace std;
/*ll power(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=y/2;
    }
    return res;
}*/
int main()
{
string s;
cin>>s;
if(s[0]==s[1] || s[1]==s[2] || s[2]==s[3])
    cout<<"Bad"<<endl;
else
    cout<<"Good"<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#include <cmath>
#include <map>
#include <utility>
#include <numeric>
#include <set>
#include <map>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define _USE_MATH_DEFINES

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 999999999;
const ll atcoder = 1000000007;
const int dx[4] = { 1 , 0 , -1 ,0 };
const int dy[4] = { 0 , 1 , 0 , -1 };

int main() {

	int dist[210][210];
	int n, m, R,a;
	int r[10];
	int ans = INF;

	for (int i = 0; i < 210; i++) {
		for (int j = 0; j < 210; j++) {
			dist[i][j] = INF;
		}
	}

	cin >> n >> m >> R;

	for (int i = 0; i < R; i++)cin >> r[i];

	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		dist[a][b] = c;
		dist[b][a] = c;
	}

	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++){
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}

	sort(r, r + R);
		
	do {
		int s = 0;
		for (int i = 0; i < R - 1; i++) {
			s += dist[r[i]][r[i + 1]];
		}
		ans = min(s, ans);
	} while (next_permutation(r, r + R));

	cout << ans << endl;

	return 0;

}

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

int main() {
  int N, M;
  bool B=0;
  cin >> N >> M;
  vector<vector<char>> str(N, vector<char>(M));
  vector<int> x(N), y(M);
  for (int i = 0; i < N; i++){
    B = 0;
    for (int j =0; j < M; j++){
      cin >> str.at(i).at(j);
      if (str.at(i).at(j) != '.'){
        B = 1;
      }
    }
    if(B == 0){
      x.at(i) = 1;
    }
  }
  for (int i = 0; i < M; i++){
    B = 0;
    for (int j = 0; j < N; j++){
      if(str.at(j).at(i) != '.'){
        B = 1;
        break;
      }
    }
    if(B == 0){
      y.at(i) = 1;
    }
  }
  for (int i = 0; i < N; i++){
    if(x.at(i) == 1) continue;
    for (int j =0; j < M; j++){
      if (y.at(j) == 1) continue;
      cout << str.at(i).at(j);
    }
    cout << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <iomanip>
#include <utility>
typedef long long ll;
typedef long double ld;
using namespace std;

int main(void){
    ll X, now=100;
    cin >> X;
    int ans=0;
    while(now<X){
        now *= 1.01;
        ++ans;
    }
    cout << ans << endl;
    return 0;
}


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

int W, H;
char map[20][20];
int cnt;
int angle[][2] = {
	{-1, 0}, {0, -1}, {1, 0}, {0, 1}
};

void dfs(int x, int y)
{
	if (map[x][y] == '#' || x < 0 || y < 0 || x >= W || y >= H){
		return;
	}
	cnt++;
	map[x][y] = '#';
	
	for (int i = 0; i < 4; i++){
		dfs(x + angle[i][0], y + angle[i][1]);
	}
}

int main()
{
	int num;
	
	while (scanf("%d %d", &W, &H), W + H){
		int x;
		int y;
		memset(map, 0, sizeof(map));
		
		for (int i = 0; i < H; i++){
			for (int j = 0; j < W; j++){
				scanf(" %c", &map[j][i]);
				if (map[j][i] == '@'){
					x = j;
					y = i;
					map[j][i] = '.';
				}
			}
		}
		
		cnt = 0;
		dfs(x, y);
		
		printf("%d\n", cnt);
	}
	
	return 0;
}

Measured memory_bytes: 1036
#include <stdio.h>

int main()
{
    int a,b,c,tm,count=0;
    int i,j;
    int ar[3];
    scanf("%d%d%d",&a,&b,&c);
    ar[0]=a;
    ar[1]=b;
    ar[2]=c;
    for(i=0; i<2; i++){
        for(j=i+1; j<3; j++){
                if(ar[i]<ar[j]){
                tm=ar[j];
                ar[j]=ar[i];
                ar[i]=tm;
            }
        }
    }
    a=ar[0];
    b=ar[1];
    c=ar[2];
    for(i=b; i<a; i++){
        b++;
        c++;
        count++;
    }
    if(a==b){
        for(i=c; i<a; i+=2){
            c+=2;
            count++;
        }
        if(a==b && a==c-1){
            a++;b++;
            count++;
        }

    }
    printf("%d\n",count);
    return 0;
}


Measured memory_bytes: 128
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long x = sc.nextLong();
    long[] a = new long[n];
    for(int i = 0; i < n; i++) {
      a[i] = sc.nextLong();
    }
    long[] rui = new long[n];
    rui[0] = a[0];
    for(int i = 1; i < n; i++) {
      rui[i] = rui[i - 1] + a[i];
    }
    long ans = Long.MAX_VALUE;
    for(int k = 1; k <= n; k++) {
      long t = ((long)(n + k)) * x;
      int p = n - 1;
      int q = 0;
      for(int j = 1; j <= n; j++) {
        long c = rui[p];
        if((p - k) >= 0) c -= rui[p - k];
        p -= k;
        if(j == 1) {
          if((5 * c) >= (Long.MAX_VALUE - t)) q++;
          t += (5 * c);
        } else {
          if((2 * j + 1) >= (Long.MAX_VALUE / c)) q++;
          if(((2 * j + 1) * c) >= (Long.MAX_VALUE - t)) q++;
          t += ((2 * j + 1) * c);
        }
        if(p < 0) break;
      }
      if(q == 0) ans = Math.min(ans, t);
    }
    System.out.println(ans);
  }
}

Measured memory_bytes: 92892
/* ex 5_4
   riverstone */

#include <stdio.h>
#include <string.h>
 
int main(void){
    //最高文字列数は10より\0を含めて11
    char sequence[11];
    //max:最大の長さ counter:何文字連続しているかを入れる変数
    int i,max = 0,counter=0;
    
    //入力
    scanf("%s", sequence);

    for ( i = 0; i < strlen(sequence); i++){   
        //読み取った文字が，A,C,G,Tである時はcounterを1増やす
        if(sequence[i]=='A'||sequence[i]=='C'||sequence[i]=='G'||sequence[i]=='T'){
            counter += 1;
            //その連続数が最高記録ならば最高記録を更新
            if (counter >= max){               
                max = counter;
            }
        }
        else{
            counter=0;
        }
    }

    //最高連続記録の出力
    printf("%d\n", max);
    return 0;
}

Measured memory_bytes: 1736
n = gets.to_i
l_arr = gets.split.map(&:to_i)

l_arr.sort!
count = 0
(n-2).times do |i|
  (i+1..n-2).each do |j|
    two_l = l_arr[i] + l_arr[j]
    c = l_arr[j+1..-1].bsearch_index{|e| two_l <= e}
    count += c.nil? ? n-j-1 : c
  end
end
puts count

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

int main(){
    
    int h, w;

    std::cin >> h >> w;

    std::vector<std::vector<char>> a(h, std::vector<char>(w));

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            std::cin >> a[i][j];
        }
    }

    std::vector<bool> flag_h(h, false);
    std::vector<bool> flag_w(w, false);

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(a[i][j] == '#'){
                flag_h[i] = true;
                flag_w[j] = true;
            }
        }
    }

    for(int i = 0; i < h; i++){
        if(flag_h[i] == true){
            for(int j = 0; j < w; j++){
                if(flag_w[j] == true){
                    std::cout << a[i][j];
                }
            }std::cout << std::endl;
        }
    }
    
    return 0;
}


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

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

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

	int n, m, l;
	cin >> n >> m >> l;

	vector<int> a(m);
	vector<int> b(m);
	vector<int> c(m);
	vector<vector<int>> d(n + 1, vector<int>(n + 1, INF));
	vector<vector<int>> f(n + 1, vector<int>(n + 1, INF));
	rep (i, 0, m) {
		cin >> a[i] >> b[i] >> c[i];
		d[a[i]][b[i]] = min(d[a[i]][b[i]], c[i]);
		d[b[i]][a[i]] = min(d[b[i]][a[i]], c[i]);
	}

	rep (i, 0, n + 1) {
		d[i][i] = 0;
		f[i][i] = 0;
	}

	// warshall floyd
	for (int k = 1; k < n + 1; k++) {
		for (int i = 1; i < n + 1; i++) {
			for (int j = 1; j < n + 1; j++) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}

	for (int i = 1; i < n + 1; i++) {
		for (int j = 1; j < n + 1; j++) {
			if (i == j)
				continue;
			if (d[i][j] <= l)
				f[i][j] = 1;
		}
	}

	// warshall floyd
	for (int k = 1; k < n + 1; k++) {
		for (int i = 1; i < n + 1; i++) {
			for (int j = 1; j < n + 1; j++) {
				f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
			}
		}
	}

	int q;
	cin >> q;
	rep (i, 0, q) {
		int s, t;
		cin >> s >> t;

		if (f[s][t] == INF)
			cout << -1 << endl;
		else
			cout << f[s][t] - 1 << endl;
	}

	return 0;
}


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

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }

template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }

inline int read() {
  #define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48) c = nc;
  while (c > 47) x = x * 10 + (c ^ 48), c = nc;
  return x;
  #undef nc
}

typedef long long ll;
typedef pair <int, int> pii;
const int maxn = 1e5 + 10;
int n; ll m;

ll ans;
int tot, par[maxn], sz[maxn];

int find(int x) {
  return par[x] == x ? x : par[x] = find(par[x]);
}

void unite(int x, int y) {
  if ((x = find(x)) != (y = find(y))) {
    par[x] = y, ans += 1ll * sz[x] * sz[y], sz[y] += sz[x], tot--;
  }
}

int q;

vector <pii> G;

void solve() {
  cin >> n >> m >> q;
  tot = n;
  rep(i, 1, n) par[i] = i, sz[i] = 1;
  int c0 = 0, c1 = 0;
  while (q--) {
    int u = read() + 1, v = read() + 1, op = read();
    if (!op) {
      c0++;
      unite(u, v);
    } else {
      c1++;
      G.push_back({u, v});
    }
  }
  if (m == n - 1) {
    return puts(c1 ? "No" : "Yes"), void();
  }
  for (pii p : G) {
    int u = p.first, v = p.second;
    if (find(u) == find(v)) {
      return puts("No"), void();
    }
  }
  int mn = n - 1;
  ans = n - tot + 1ll * tot * (tot - 1) / 2;
  if (m >= mn && m <= ans) {
    puts("Yes");
  } else {
    puts("No");
  }
}

signed main() {
//  int T = read();
//  while (T--) solve();
  solve();
  return 0;
}

Measured memory_bytes: 5096
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>

#define min(x, y) (x > y ? y : x)
using namespace std;
vector<long long> num;

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

    for (int i = 0; i < N; i++)
    {
        int tmp;
        cin >> tmp;
        num.push_back(tmp);
    }
    sort(num.begin(), num.end());

    int len = num.size();
    long long ans = 0;
    ans += abs(num[num.size() - 1] - num[0]);
    //printf("%d\n", ans);

    for (int i = 0; i < (len / 2) - 1; i++)
    {
        ans += abs(num[num.size() - 1 - i] - num[i + 1]);
        //printf("%d\n", ans);
        ans += abs(num[num.size() - 2 - i] - num[i]);
        //printf("%d\n", ans);
    }
    if (len % 2 == 1)
    {
        long long tmp1, tmp2;
        tmp1 = abs(num[len / 2] - num[len / 2 - 1]);
        tmp2 = abs(num[len / 2 + 1] - num[len / 2]);
        if (tmp1 <= tmp2)
        {
            ans += tmp2;
        }
        else
        {
            ans += tmp1;
        }
    }
    //printf("%d", ans);

    cout << ans << endl;
}

Measured memory_bytes: 1528
N,W = map(int,input().split())
w,v = [0]*N,[0]*N
for i in range(N):
    w[i],v[i] = map(int,input().split())
    
dp = [[0]*(W+1) for i in range(N+1)]

for i in range(N):
    for j in range(W+1):
        if j >= w[i]:
            dp[i+1][j] = max(dp[i][j-w[i]]+v[i],dp[i][j])
        else:
            dp[i+1][j] = dp[i][j]
            
print(dp[N][W])

Measured memory_bytes: 119916
N = int(input())
A = input().split()

b = 0

flag = True

while flag:
    for i in range(N):
        if i != N-1:
            if int(float(A[i])) % 2 == 0:
                A[i] = str(int(float(A[i]))/2)
            else:
                flag = False
                break
        else:
            if int(float(A[i])) % 2 == 0:
                A[i] = str(int(float(A[i]))/2)
                b += 1
            else:
                flag = False
                break
print(b)


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

#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i,j,k) for(i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a,b) a = max(a, b)
#define remin(a,b) a = min(a, b)
#define sz(x) int32_t(x.size())

typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
// --------------------------------------------------------------------

const pii dxy[] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} };
const int mod = 998244353;
const int INF = 2e18;
const ld EPS = 1e-9;
const int NN = 3002;

int dp[NN][NN];
int a[NN];


void solve() {
      int i = 0, j = 0, k = 0, n = 0, m = 0; 
      int s;
      cin >> n >> s;
      fr(i, 0, n) {
            cin >> a[i];
      }
      int p2 = 1;
      dp[0][a[0]] = 1;
      fr(i, 1, n) {
                        p2 = (p2 * 2) % mod;

            fr(j, 0, s + 1) {
                  dp[i][j] = dp[i - 1][j] * 2;
                  dp[i][j] %= mod;
            }
            fr(j, 0, s + 1) {
                  if(a[i] + j <= s) {
                        int& v = dp[i][a[i] + j];
                        v = (v + dp[i - 1][j]) % mod;
                  }
            }
            int& v = dp[i][a[i]];
            v = (v + p2) % mod;
      }
      cout << dp[n - 1][s];
}

int32_t main() {
      ios::sync_with_stdio(false); cin.tie(0); 
      int T = 1; 
      // cin >> T;
      for(int tc = 1; tc <= T; tc++) {
            solve();
       }
      return 0;
}

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

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define ncr(n,r) fact[n]*modInv(fact[r])*modInv(fact[n-r])
#define modInv(a) binExp(a,mod-2)
#define mod 1000000007
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)

vector<int> chain;
int vis[5001];
int p[5001];
int c[5001];
int n,k;
int curMax;

void start(int);
void dfs(int);

signed main()
{
    fast

    #ifndef ONLINE_JUDGE
        freopen("inputf.in","r",stdin);
        freopen("outputf.in","w",stdout);
    #endif

    cin>>n>>k;

    f1(i,0,n) vis[i] = 0;
    f1(i,1,n)
    {
        cin>>p[i];
    }
    f1(i,1,n)
    {
        cin>>c[i];
    }
    
    curMax = LLONG_MIN;

    f1(i,1,n)
    {
        if(!vis[i])
        {
            start(i);
        }
    }
    cout<<curMax;
}

void start(int v)
{
    chain.clear();
    dfs(v);
    
    f0(i,0,chain.size())
    {
    	int pathLen = 0;
    	int pathSum = 0;
    	curMax = max(curMax, c[chain[i]]);
    	int chainSum = 0;
    	for(auto x : chain) chainSum += c[x];

    	f1(j,1,chain.size())
    	{
    		if(j > k) break;
    		pathSum += c[chain[(i+j)%chain.size()]];
    		int score;
    		if(chainSum > 0)
    		{
    			score = pathSum + (k-j)/chain.size() * chainSum;
    		}
    		else
    		{
    			score = pathSum;
    		}
    		//cout<<"score = "<<score<<'\n';
    		curMax = max(curMax, score);
    		if((i+j)%k == i) break;
    	}
    	//cout<<"------\n";
    }
}

void dfs(int v)
{
    vis[v] = 1;
    chain.pb(v);
    if(vis[p[v]]) return;
    dfs(p[v]);
}

Measured memory_bytes: 4028
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

//
#define int long long
#define pb(x) push_back(x)
#define m0(x) memset((x), 0, sizeof(x))
#define mm(x) memset((x), -1, sizeof(x))

//container
#define ALL(x) (x).begin(), (x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
#define PERM(c) \
  sort(ALL(c)); \
  for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))

// debug
#define GET_VAR_NAME(variable) #variable
#define test(x) cout << GET_VAR_NAME(x) << " = " << x << endl;

// bit_macro
#define bit(n) (1LL << (n))
#define bitset(a, b) (a) |= (1 << (b))
#define bitunset(a, b) (a) |= ~(1 << (b))
#define bitcheck(a, b) (((a) >> (b)) & 1)
#define bitcount(a) __builtin_popcountll((a))

//typedef
typedef long long lint;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

//constant
const int INF = (int)1e18;
const int MOD = (int)1e9 + 7;
const double EPS = (double)1e-10;
const int dx[] = {-1, 0, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 1, 0, 0, 1, -1, 1, -1};

//
template <typename T>
void chmax(T &a, T b) { a = max(a, b); }
template <typename T>
void chmin(T &a, T b) { a = min(a, b); }
//
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
struct Accelerate_Cin {
  Accelerate_Cin() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
};

signed main() {
  int N, A, B;
  cin >> N >> A >> B;

  int tmp = abs(A - B);
  if (tmp % 2 == 0) {
    cout << "Alice" << endl;
  } else {
    cout << "Borys" << endl;
  }

  return 0;
}


Measured memory_bytes: 256
N,M=map(int,input().split())
S=input()
T=input()

from math import*
ans=N*M//gcd(N,M)

i=0
j=0
count_i=0
count_j=0
while i<N and j<M:
    if count_i==count_j:
        if S[i]!=T[j]:
            ans=-1
    if count_i>count_j:
        count_j+=ans//M
        j+=1
    elif count_j>count_i:
        count_i+=ans//N
        i+=1
    else:
        count_i+=ans//N
        count_j+=ans//M
        i+=1
        j+=1

print(ans)

Measured memory_bytes: 9184
#include <cstring>
#include <map>
#include <unordered_map>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;

#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;

int gi() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gli() {
    ll a;
    scanf("%lld", &a);
    return a;
}
char a[104];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    scanf("%s", a);
    int n = strlen(a);
    for (int i = 0, j = n-1; i < j; i++, j--)
        if (a[i] != a[j]) {
            printf("No\n");
            return 0;
        }
    for (int i = 0, j = (n-1)/2-1; i < j; i++, j--)
        if (a[i] != a[j]) {
            printf("No\n");
            return 0;
        }
    for (int i = 0, j = (n-1)/2+1; i < (n-1)/2; i++, j++)
        if (a[i] != a[j]) {
            printf("No\n");
            return 0;
        }
    printf("Yes\n");

    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

struct E {
    int time, c;
    E(int time, int c) : time(time), c(c) {}
    bool operator<(const E& e) const {
        if (time == e.time) {
            return c < e.c;
        }
        return time < e.time;
    }
};

int N;
vector<E> es;
bool input() {
    es.clear();
    scanf("%d\n", &N);
    if (N == 0) return false;
    for (int i = 0; i < N; i++) {
        int h, m, s;
        scanf("%d:%d:%d", &h, &m, &s);
        int from = h * 3600 + m * 60 + s;
        es.push_back(E(from, +1));
        scanf(" %d:%d:%d\n", &h, &m, &s);
        int to = h * 3600 + m * 60 + s;
        es.push_back(E(to, -1));
    }
    return true;
}

void solve() {
    sort(es.begin(), es.end());
    int ans = 0;
    int pool = 0;
    for (int i = 0; i < es.size(); i++) {
        const E& e = es[i];
        if (e.c == 1) {
            if (pool > 0) pool--;
            else ans++;
        } else {
            pool++;
        }
    }
    printf("%d\n", ans);
}

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

Measured memory_bytes: 1292
open System
let readInt () = stdin.ReadLine() |> int
let readIntArray () = stdin.ReadLine().Split(' ') |> Array.map int

let N = readInt ()
let A = readIntArray () |> Array.sortWith (fun a b -> a.CompareTo b |> (*) -1);

let rec takeFirst xs =
    match xs with
        | []   -> 0
        | x :: [] -> x
        | _    -> xs.[0] + takeFirst (List.skip 2 xs)


let total = A |> Array.sum
let alice = Array.toList A |> takeFirst
let bob = total - alice
printfn "%d" (alice - bob)


Measured memory_bytes: 14044
import java.io.*;

class Point {
	double x,y;
	Point(double x,double y) {
		this.x = x;
		this.y = y;
	}
}

class Vec {
	double x,y;
	Point s,e;
	Vec(Point p1,Point p2) {
		this.s = p1;
		this.e = p2;
		this.x = p2.x-p1.x;
		this.y = p2.y-p1.y;
	}
	Vec(Vec v1,Vec v2) {
		this.x = v2.x-v1.x;
		this.y = v2.y-v1.y;
	}
	Vec(double x,double y) {
		this.x = x;
		this.y = y;
	}
	Vec rotate() {
		return new Vec(this.y/Size(),(-1)*this.x/Size());	
	}
	double Size() {
		return Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
	}
	void arrange(Vec r) {
		this.x *= -1;
		this.y *= -1;
		this.x += r.x;
		this.y += r.y;
		this.x *= -1;
		this.y *= -1;
	}
}

class Main {
	public static void main(String args[]) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String buf;

		try {
			while (!(buf = br.readLine()).equals("-1")) {
				int n = Integer.parseInt(buf);
				double x = -1.0;
				double y = 0.0;
				Vec v1 = new Vec(x,y);
				for (int i=2;i<=n;i++) {
					Vec v2 = v1.rotate();
					v1.arrange(v2);
				}
				System.out.println((-1)*v1.x);
				System.out.println((-1)*v1.y);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

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

int main (){
	string S;cin>>S;
	if(S[0]!=S[1]
	&&S[1]!=S[2]
	&&S[2]!=S[3]){
		cout<<"Good"<<endl;
		return 0;
	}
	cout<<"Bad"<<endl;
    return 0;
}

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

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

bool vis[N][N];

bool valid(int y, int x) {
  return y >= 0 && x >= 0 && y < N && x < N;
}

void dfs(int y, int x, const vector<string> &ans) {
  vis[y][x] = true;
  for (int d = 0; d < 4; ++d) {
    int ny = y + dy[d];
    int nx = x + dx[d];
    if (valid(ny, nx) && ans[y][x] == ans[ny][nx] && !vis[ny][nx])
      dfs(ny, nx, ans);
  }
}

void solve(const vector<string> &ans) {
  memset(vis, false, sizeof vis);
  int b = 0, w = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (!vis[i][j]) {
        if (ans[i][j] == '.') {
          dfs(i, j, ans);
          ++b;
        }
        if (ans[i][j] == '#') {
          dfs(i, j, ans);
          ++w;
        }
      }
    }
  }
  for (int i = 0; i < N; ++i)
    cout << ans[i] << '\n';
  cout << b << ' ' << w << '\n';
}

int main() {
  int a, b;
  cin >> a >> b;
  --a, --b;
  vector<string> ans(N);
  ans[0] = ans[N - 1] = string(N / 2, '#') + string(N / 2, '.');
  for (int i = 1; i < N - 1; ++i)
    ans[i] = "#" + string(N / 2 - 2, a ? '.' : '#') + "#" + "." + string(N / 2 - 2, b ? '#' : '.') + ".";
  int c = 0, d = 0;
  for (int i = 2; i < N - 1; i += 2) {
    if (a > 0) {
      --a;
      ++c;
      for (int j = 1; j < N / 2 - 1; ++j)
        ans[i][j] = '#';
    }
    if (b > 0) {
      --b;
      ++d;
      for (int j = N / 2 + 1; j < N - 1; ++j)
        ans[i][j] = '.';
    }
  }
  if (a == 0) {
    int start = 1 + 2 * c;
    for (int i = start; i < N - 1; ++i)
      for (int j = 1; j < N / 2 - 1; ++j)
        ans[i][j] = '#';
  }
  if (b == 0) {
    int start = 1 + 2 * d;
    for (int i = start; i < N - 1; ++i)
      for (int j = N / 2 + 1; j < N - 1; ++j)
        ans[i][j] = '.';
  }
  for (int i = 1; i < N - 1; i += 2) {
    for (int j = 2; j < N / 2 - 2; j += 2)
      if (a > 0)
        --a, ans[i][j] = '#';
    for (int j = N / 2 + 2; j < N - 2; j += 2)
      if (b > 0)
        --b, ans[i][j] = '.';
  }
  cout << "100 100" << '\n';
  for (int i = 0; i < N; ++i)
    cout << ans[i] << '\n';
  return 0;
}

Measured memory_bytes: 256
use proconio::input;
use num::*;

fn main() {
    input! {
        a: u64,
        b: f32,
    }
    let b = (b * 1000_f32) as u64;
    let a = bigint::BigUint::from_u64(a).unwrap();
    let b = bigint::BigUint::from_u64(b).unwrap();
    let r = a * b / bigint::BigUint::from_u64(1000).unwrap();

    println!("{}", r);
}


Measured memory_bytes: 2752
#60 シャッフル

s = input()
while s !="-":
        m = int(input())
        h = 0
        
        for i in range(m):
            h += int(input())
            h %= len(s)
            s1 = s[h : len(s)] + s[0 : h]
        print(s1)
        
        s = input()


Measured memory_bytes: 5596
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.IntStream;

public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt(), k = scanner.nextInt();
			int[] p = IntStream.range(0, n).map(i -> scanner.nextInt() - 1).toArray();
			long[] c = IntStream.range(0, n).mapToLong(i -> scanner.nextLong()).toArray();
			long[] scores = new long[n + 1];
			long max = Long.MIN_VALUE;
			for (int i = 0; i < n; i++) {
				Set<Integer> set = new HashSet<>();
				Arrays.fill(scores, 0L);
				long score = 0L;
				int count = 0, current = i;
				while ((!set.contains(current)) && (k > count)) {
					// ループを計算
					set.add(current);
					score += c[current];
					scores[++count] = score;
					current = p[current];
				}
				if ((score > 0) && (k > count)) {
					// すべてのループを回した場合の最大値を計算
					max = Math.max(max, (k / count) * score
							+ IntStream.rangeClosed(1, k % count).mapToLong(j -> scores[j]).max().orElse(0L));
					// 最後のループを回さない場合の最大値を計算
					max = Math.max(max, Math.max(0, (k / count) - 1) * score
							+ IntStream.rangeClosed(1, count).mapToLong(j -> scores[j]).max().orElse(0L));
				} else {
					max = Math.max(max, IntStream.rangeClosed(1, count).mapToLong(j -> scores[j]).max().getAsLong());
				}
			}
			System.out.println(max);
		}
	}
}


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

std::vector<bool> isSheep;

bool solve();

int main()
{
	if (!solve()) puts("-1");
	else
	{
		for (const auto& e: isSheep)
			if (e) putchar('S');
			else putchar('W');
		putchar('\n');
	}
	
	return 0;
}

bool solve()
{
	int N;
	scanf("%d", &N);
	std::string s;
	std::cin >> s;
	isSheep.resize(N);
	for (int first{}; first < 2; first++)
		for (int second{}; second < 2; second++)
		{
			isSheep[0] = first == 1;
			isSheep[1] = second == 1;
			for (int i{1}; i < N - 1; i++)
				if (isSheep[i]) isSheep[i + 1] = isSheep[i - 1] ^ (s[i] == 'x');
				else isSheep[i + 1] = isSheep[i - 1] ^ (s[i] == 'o');
			int num{};
			if (isSheep[N - 2]) num++;
			if (isSheep[N - 1]) num++;
			if (isSheep[0]) num++;
			if ((num % 2 == 1) ^ (s[N - 1] == 'o'))
				continue;
			num = 0;
			if (isSheep[N - 1]) num++;
			if (isSheep[0]) num++;
			if (isSheep[1]) num++;
			if ((num % 2 == 1) ^ (s[0] == 'o'))
				continue;
			return true;
		}
	return false;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

#define MAX_N 200000

template <int MOD = 1000000007>
struct Mint {
    int x;

    Mint() : x(0) {}
    Mint(long long t) {
        x = t % MOD;
        if (x < 0) x += MOD;
    }

    Mint pow(int n) {
        Mint res(1), t(x);
        while (n > 0) {
            if (n & 1) res *= t;
            t *= t;
            n >>= 1;
        }
        return res;
    }

    Mint inv() const {
        int a = x, b = MOD, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return Mint(u);
    }

    Mint &operator+=(Mint a) {
        x += a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator-=(Mint a) {
        x += MOD - a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator*=(Mint a) {
        x = int(1LL * x * a.x % MOD);
        return *this;
    }

    Mint &operator/=(Mint a) {
        return (*this) *= a.inv();
    }

    Mint operator+(Mint a) const {
        return Mint(x) += a;
    }
    Mint operator-(Mint a) const {
        return Mint(x) -= a;
    }
    Mint operator*(Mint a) const {
        return Mint(x) *= a;
    }
    Mint operator/(Mint a) const {
        return Mint(x) /= a;
    }

    Mint operator-() const {
        return Mint(-x);
    }

    bool operator==(const Mint a) {
        return x == a.x;
    }
    bool operator!=(const Mint a) {
        return x != a.x;
    }
    bool operator<(const Mint a) {
        return x < a.x;
    }

    friend ostream &operator<<(ostream &os, const Mint &a) {
        return os << a.x;
    }

    friend istream &operator>>(istream &is, Mint &a) {
        int t;
        is >> t;
        a = Mint<MOD>(t);
        return (is);
    }
};

int n;
vector<int> G[MAX_N];
int siz[MAX_N];
Mint<> sum = 0;

int dfs(int v, int pre) {
    int res = 1;
    Mint<> t = Mint<>(2).pow(n - 1); //vが白のすべての塗り方
    t -= 1; //黒を含む部分木が1つもない(すべて白)
    for (auto to : G[v]) {
        if (to == pre) continue;
        res += dfs(to, v);
        t -= Mint<>(2).pow(siz[to]) - 1; //to以下の部分木だけ黒を含む(ほかはすべて白)
    }
    t -= Mint<>(2).pow(n - res) - 1; //親の部分木だけ黒を含む(ほかはすべて白)
    sum += t;
    return siz[v] = res;
}

signed main() {
    cin >> n;
    int a, b;
    rep(i, n - 1) {
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0, -1);
    cout << sum / Mint<>(2).pow(n) << endl;
    return 0;
}

Measured memory_bytes: 23040
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<functional>
#include<climits>
using namespace std;
typedef pair<int,int>pii;
const int INF=1e9;
const int dx[]={0,-1,0,1},dy[]={-1,0,1,0};


int main(){
    bool f[1000000];fill_n(f,1000000,true);
    f[0]=f[1]=false;
    vector<int>prime;
    for(int i=0;i<1000000;i++){
        if(!f[i])continue;
        for(int j=i+i;j<1000000;j+=i)f[j]=false;
        prime.push_back(i);
    }

    int n,p;
    while(cin>>n>>p,n!=-1||p!=-1){
        int pos=upper_bound(prime.begin(),prime.end(),n)-prime.begin();
        vector<int>V;
        for(int i=pos;i<=pos+50;i++){
            for(int j=i;j<=pos+50;j++){
                V.push_back(prime[i]+prime[j]);
            }
        }
        sort(V.begin(),V.end());
        cout<<V[p-1]<<endl;
    }
    return 0;
}

Measured memory_bytes: 2564
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vec;
typedef vector<vec> mat;
double dist(double ax,double ay,double bx,double by){
    return sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
}
int n;
double X[300],Y[300];
int search(double x,double y){
    int m=0;
    rep(i,n) if(dist(x,y,X[i],Y[i])<1.0001) m++;
    return m;
}
int main(){
    while(true){
        int ans=1;
        cin>>n;
        if(n==0) break;
        rep(i,n) cin>>X[i]>>Y[i];
        rep(i,n)for(int j=i+1;j<n;j++){
            if(dist(X[i],Y[i],X[j],Y[j])>2) continue;
            double mx=(X[i]+X[j])/2,my=(Y[i]+Y[j])/2,k=(Y[j]-Y[i])/(X[j]-X[i]);
            double vx=1/dist(0,0,1,-1/k),vy=(-1/k)/dist(0,0,1,-1/k);
            double a=sqrt(1-pow(dist(mx,my,X[i],Y[i]),2));
            ans=max(ans,search(mx+a*vx,my+a*vy));
            ans=max(ans,search(mx-a*vx,my-a*vy));
        }
        cout<<ans<<"\n";
    }
}


Measured memory_bytes: 3164
int main(){
char c[3],ans[4];
  scanf("%s",&c);
  ans[0]=0;
  ans[1]=0;
  ans[2]=0;
  ans[3]=0;
  
  if(c[0]=='1')ans[0]='9';
  if(c[1]=='1')ans[1]='9';
  if(c[2]=='1')ans[2]='9';
  if(c[0]=='9')ans[0]='1';
  if(c[1]=='9')ans[1]='1';
  if(c[2]=='9')ans[2]='1';
  printf("%s",ans);
}

Measured memory_bytes: 128
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<iostream>
#include<set>
#include<map>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll, ll>pii;
typedef pair<pii, ll>pi3;
ll mod = 1000000007;
ll gcd(ll a, ll b)
{
	for (;;)
	{
		if (a < b)swap(a, b);
		a %= b;
		if (a == 0)return b;
	}
}
int a[202020];
int main()
{
	int num;
	scanf("%d", &num);
	set<ll>se;
	vector<ll>v;
	for (int i = 0; i < num; i++)
	{
		ll z;
		scanf("%lld", &z);
		if (se.find(z) == se.end())se.insert(z);
		else v.push_back(z), se.erase(z);
	}
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
	if (v.size() < 2)printf("0\n");
	else printf("%lld\n", v[0] * v[1]);
}

Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define pb push_back
#define Int long long
using namespace std;

const int MAXN=500005;
const int NAX=1005;
const long long  MOD=(long long)(998244353);

Int N,M,K;
Int X,Y,Z;
Int H,W;

Int perform(Int x){
    return (x*(x+1))/2LL;
}
vector<Int> A;
vector<int> adj[MAXN];
using D=long double;

bool inRange(Int a,Int b){
    return a*b<=0;
}

long long C[NAX][NAX];
const D pi=3.14159265359;


long long ModExpo(long long x,unsigned long long y,long long M){
    Int ans=1;
    ans=(long long)ans;
    
    while(y>0){
        if(y&1) ans=((ans%M)*(x%M))%M;
        y>>=1LL;
        x=((x%M)*(x%M))%M;
    }
    return ans%M;
}
long long fac[MAXN];
void fillFac(){
    fac[0]=1;
    for(int i=1;i<MAXN;++i){
        fac[i]=((fac[i-1]%MOD)*(i%MOD))%MOD;
    }
}

long long ModInv(long long x){
    return ModExpo(x,MOD-2,MOD);
}

long long FermatLast(long long n,long long r){
    if(r>n) return 0;
    if(!r) return 1;
    return (fac[n] * ModInv(fac[r]) % MOD * ModInv(fac[n - r]) % MOD) % MOD;
}

Int performMod(Int x){
    Int y=((x%MOD)*((x+1)%MOD))%MOD;
    return (y*ModInv(2))%MOD;
}

Int madd(Int x,Int y){
    return (x+y)%MOD;
}

Int mmul(Int x,Int y){
    return ((x%MOD)*(y%MOD))%MOD;
}


bool isMSB(int bit,Int x){
    return (int)(floor(log2l(x)))==bit;
}
bool VIS[MAXN];
void dfs(int u){
    VIS[u]=true;
        for(auto &v:adj[u]){
            if(!VIS[v]){
                dfs(v);
            }
        }
}
int main()
{
    cin>>N>>X>>Y;
    cout<<(abs(N-X)>abs(N-Y) ? "B":"A")<<"\n";
            
}


Measured memory_bytes: 15380
#include <bits/stdc++.h>
#ifdef LOCAL_DEBUG
#define DEBUG 1
#define CERR if(DEBUG) cerr
#define MACRO(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,NAME,...) NAME
#define pr(...) CERR<<MACRO(__VA_ARGS__,pr10,pr9,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)<<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
#define pr9(a,b,c,d,e,f,g,h,i) pr1(a)<<pr8(b,c,d,e,f,g,h,i)
#define pr10(a,b,c,d,e,f,g,h,i,j) pr1(a)<<pr9(b,c,d,e,f,g,h,i,j)
#define prArr(a){CERR<<(#a)<<"={";int i=0;for(auto t:(a))CERR<<(i++?", ":"")<<t;CERR<<"}"<<endl;}
#else
#define DEBUG 0
#define pr(...)
#define prArr(a)
#endif
using namespace std;
using Int = long long;
using _int = int;
using ll = long long;
using Double = long double;
const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18
const Int mod = (1e9)+7;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
using P = pair<Int,Int>;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;}
template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;}
template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;}
//INSERT ABOVE HERE


signed main(){
  srand((unsigned)time(NULL));
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  Int N;
  cin>>N;
  vector<Int> A(N);
  cin>>A;

  map<Int, Int> X;
  X[0] = 3;
  Int ans = 1;
  for(Int i=0;i<N;i++){
    Int a = A[i];
    ans *= X[a];
    ans %= mod;
    X[a+1]++;
    X[a]--;

  }
  ans = ans% mod;
  cout<<ans<<endl;

  return 0;
}


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

using namespace std;
using LL = long long int;

const int MAX_N = 40005;

int H, N;
int X[MAX_N], Y[MAX_N];

bool hako[MAX_N][4];

int dp[MAX_N][1 << 4];

bool check(int y, int x, int S)
{
  if(x < 3 && y < H-1) {
    return !hako[y][x] && !hako[y+1][x] && !hako[y][x+1] && !hako[y+1][x+1] && !((S >> x) & 1) && !((S >> (x+1)) & 1);
  } else {
    return false;
  }
}

int main()
{
  cin >> H >> N;
  for(int i = 0; i < N; i++) {
    cin >> X[i] >> Y[i];
  }

  for(int i = 0; i < N; i++) {
    hako[Y[i]][X[i]] = true;
  }

  memset(dp, 0, sizeof(dp));

  for(int i = 0; i < H; i++) {
    for(int j = 0; j < (1 << 4); j++) {
      //テァツスツョテ」ツ?凝」ツ?ェテ」ツ??
      dp[i+1][0] = max(dp[i+1][0], dp[i][j]);
      //テァツスツョテ」ツ??
      if(check(i, 0, j)) {
        if(check(i,2,j)) {
          dp[i+1][(1 << 4)-1] = max(dp[i+1][(1 << 4)-1], dp[i][j]+2);
        }
        dp[i+1][(1 << 2)-1] = max(dp[i+1][(1 << 2)-1], dp[i][j]+1);
      }
      if(check(i, 1, j)) {
        dp[i+1][6] = max(dp[i+1][6], dp[i][j]+1);
      }
      if(check(i, 2, j)) {
        dp[i+1][12] = max(dp[i+1][12], dp[i][j]+1);
      }
    }
  }

  int ans = 0;
  for(int j = 0; j < (1 << 4); j++) {
    ans = max(ans, dp[H][j]);
  }
  cout << ans << endl;

  return 0;
}

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

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

int main(){
    int n;cin>>n;
    ll a[n],b[n],c[n];

    rep(i,n)cin>>a[i];
    rep(i,n)cin>>b[i];
    rep(i,n)cin>>c[i];

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

Measured memory_bytes: 4096
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC094A_CatsandDog
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] word = Console.ReadLine().Split(' ');
            int a = int.Parse(word[0]);
            int b = int.Parse(word[1]);
            int x = int.Parse(word[2]);

            if (a + b >= x && a <= x)
                Console.WriteLine("YES");
            else
                Console.WriteLine("NO");
        }
    }
}

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

void solve() {
    ll n, k;
    cin >> n >> k;
    ll rem = n % k;
    cout << min(rem, k - rem) << endl;
}

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


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

using namespace std;
int main(){
	string t,h ;
	int n,tl,hl,tp,hp;
	tp=0;
	hp=0;
	cin >> n;
	for(int i = 0; i< n; ++i){
		cin >> t >> h;
		tl = t.size();
		hl = h.size();
		for(int j=0; j < tl && j < hl;j++){
		  if(t[j] > h[j]){
				
			  tp += 3;
			  break;
		  }
		  else if(t[j]<h[j]){
			  
			  hp +=3;
			  break;
		  }
		  else

		  //if number of characters are same
		  if(j==tl-1 ||  j ==hl-1){
				if(tl==hl){
					tp+=1;
					hp+=1;
		  }
				else if (tl > hl){
					tp+=3;
				}
				else{
					hp+=3;
				}
		  }
		  
		}
	}
	cout << tp <<" "<< hp <<endl;

	


		return 0;
}

Measured memory_bytes: 1204
#include<bits/stdc++.h>
#define longlongvector vector<long long> 
#define intvector vector<int> 
#define size size()
using namespace std;

int n, m, k;



int main()
{
	double r; 
	cin >> r; 


	cout << fixed << setprecision(4) <<  2.0 * 3.14159 * r << endl;
//	return 0;
//	system("pause");
}

Measured memory_bytes: 3788
#include<bits/stdc++.h>
using namespace std;
#define ms(x,y) memset(x, y, sizeof(x))
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii;

int buf[103];

void run_case() {
    int n, m;
    int sum = 0;
    cin >> n >> m;
    for(int i = 0; i < n; ++i) {
        cin >> buf[i];
        sum += buf[i];
    }
    int num = 0;
    for(int i = 0; i < n; ++i) {
        if(4*m*buf[i] >= sum) num++;
    }
    if(num >= m) cout << "Yes\n";
    else cout << "No\n";
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cout.flags(ios::fixed);cout.precision(10);
    run_case();
    cout.flush();
    return 0;
}


Measured memory_bytes: 256