akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner;
import java.util.*;
 
public class Main {
	public static ArrayList <Integer>linkList[][];
	public static int dists[];
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		linkList = new ArrayList[N + 1][2];
		dists = new int[N + 1];
		
		for(int i = 0; i < N + 1; i++) {
			dists[i] = Integer.MIN_VALUE;
			linkList[i][0] = new ArrayList<Integer>();
			linkList[i][1] = new ArrayList<Integer>();
		}
		for(int i = 0; i < M; i++) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			int d = sc.nextInt();
			linkList[l][0].add(r);
			linkList[l][1].add(d);
			linkList[r][0].add(l);
			linkList[r][1].add(-d);
		}
		for(int i = 0; i < N; i++){
			if(dists[i] != Integer.MIN_VALUE)continue;
			if(!dfs(i, 0)){
				System.out.println("No");
				return;
			}
		}
		System.out.println("Yes");
	}
	public static boolean dfs(int i, int dist) {
		if(dists[i] != Integer.MIN_VALUE)return dists[i] == dist;
		dists[i] = dist;
		for(int j = 0; j < linkList[i][0].size(); j++) {
			if(!dfs(linkList[i][0].get(j), dist + linkList[i][1].get(j)))return false;
		}
		return true;
	}
}

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

void Main() {
  int n;
  cin >> n;
  int p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != i + 1) {
      cnt++;
    }
  }
  if (cnt > 2) cout << "NO" << endl;
  else cout << "YES" << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}

Measured memory_bytes: 256
/************************************************
* Author        :RunningTime
* Created Time  :2017/4/29 20:00:48
* File Name     :A.cpp
 ************************************************/

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

typedef long long ll;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
string A, B, C;

int main() {
    ios::sync_with_stdio(false);
    cin >> A >> B >> C;
    int n1 = A.length(), n2 = B.length();
    if (A[n1-1] == B[0] && B[n2-1] == C[0]) cout << "YES" << '\n';
    else cout << "NO" << '\n';
    return 0;
}


Measured memory_bytes: 764
n, m = input().split()
num_s = int(n)
num_c = int(m)

if num_c <= num_s * 2:
  # Sが多いとき
  print(num_c // 2)
else:
  scc = num_s
  num_s = 0
  num_c -= 2 * scc
  scc += num_c // 4
  print(scc)



Measured memory_bytes: 2940
/* Stable Sort */

#include <stdio.h>

typedef struct {
    char suit;
    int number;
} Card;

void swap_Card(Card *x, Card *y)
{
    Card temp = *x;
    *x = *y;
    *y = temp;
}

int main(void)
{
    int N;
    scanf("%d\n", &N);

    int i;
    Card BC[N];
    Card SC[N];

    for (i = 0; i < N; i++) {
        Card c;
        scanf("%c", &c.suit);
        scanf("%d ", &c.number);
        BC[i] = c;
        SC[i] = c;
    }

    int j;

    /* Bubble Sort */
    for (i = 0; i < N - 1; i++) {
        for (j = N - 1; j > i; j--) {
            if (BC[j].number < BC[j - 1].number)
                swap_Card(&BC[j], &BC[j - 1]);
        }
    }

    /* Selection Sort */
    for (i = 0; i < N - 1; i++) {
        int min_i = i;
        for (j = i + 1; j < N; j++) {
            if (SC[j].number < SC[min_i].number)
                min_i = j;
        }
        swap_Card(&SC[i], &SC[min_i]);
    }

    /* Output */
    for (i = 0; i < N - 1; i++)
        printf("%c%d ", BC[i].suit, BC[i].number);
    printf("%c%d\n", BC[N - 1].suit, BC[N - 1].number);

    puts("Stable");

    for (i = 0; i < N - 1; i++)
        printf("%c%d ", SC[i].suit, SC[i].number);
    printf("%c%d\n", SC[N - 1].suit, SC[N - 1].number);

    for (i = 0; i < N; i++) {
        if (BC[i].suit != SC[i].suit) {
            puts("Not stable");
            return 0;
        }
    }
    puts("Stable");

    return 0;
}

Measured memory_bytes: 612
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a, b, k;
  cin >> a >> b >> k;
  for(int i = a; i <= b; i++) {
    if(i < a+k or b-k < i)
      cout << i << "\n";
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,i,e,f,g,n,m,k,l,x,maxx,ans,A[200005],q;
pair < pair <long long , long long> , pair <long long , long long> > B[200005];
void go(long long x) {
    for(long long i=A[x-1];i<=m;i++) {
        A[x]=i;
        if(x!=n) go(x+1);
    }
    if(x==n) {
        ans=0;
        for(long long i=1;i<=q;i++)
        if(A[B[i].first.second]-A[B[i].first.first]==B[i].second.first) ans+=B[i].second.second;
        maxx=max(maxx,ans);
    }
}
int main() {
           cin>>n>>m>>q;
           for(long long i=1;i<=q;i++) {
               cin>>B[i].first.first>>B[i].first.second>>B[i].second.first>>B[i].second.second;
           }
           A[0]=1;
           go(1);
           cout<<maxx;
}

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

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

    int n;
    cin >> n;

    vector<pair<int,int>> a(n);
    rep(i,n) cin >> a[i].first >> a[i].second;

    map<pair<int,int>,int> mp;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i == j) continue;
            int p,q;
            p = a[i].first - a[j].first;
            q = a[i].second - a[j].second;
            mp[{p,q}]++;
        }
    }

    int ans = 0;
    for(auto i : mp){
        ans = max(ans,i.second);
    }
    cout << n - ans << endl;
}



Measured memory_bytes: 384
#include <stdio.h>

int main()
{
  int h, n, s = 0, i, v;
  scanf("%d%d", &h, &n);
  for (i = 0 ; i < n; i++)
  {
    scanf("%d", &v);
    s+=v;
  }
  printf((s >= h)?"Yes\n":"No\n");
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i, x, y) for(int i = x; i <= y; i ++)
#define rrep(i, x, y) for(int i = x; i >= y; i --)
#define ll long long
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define ls rt << 1
#define rs rt << 1 | 1
#define sz(x) (int)(x).size()
#define ALL(x) (x).insgin(), (x).end()
#define vi vector<int>
using namespace std;
inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
const int N = 100010;
const int M = 200010;

struct Edge 
{
    int v, w, x;
} E[M*2];

int n, m, e;
int head[N], vis[N];
ll dis[N];

ll mind = 0, maxd = 0;

void init() 
{
	memset(head, -1, sizeof(head));
}

inline void insert(int u, int v, int w) 
{
	E[e].v = v, 
	E[e].x = head[u], 
	E[e].w = w, 
	head[u] = e ++;
}

ll delta[N];
int col[N];

void dfs(int u, ll d, int c) 
{
    if (vis[u]) return;
    vis[u] = 1; 
    dis[u] = d; 
    mind = min(mind, d); 
    maxd = max(maxd, d); col[u] = c;
    for (int p = head[u]; p >= 0; p = E[p].x) 
        dfs(E[p].v, d+E[p].w, c);
}

int main() 
{
    init();
    n = read();
    m = read();
    rep(i, 1, m)
    {
        int L, R, D;
        L = read();
        R = read();
        D = read();
        insert(L, R, D);
        insert(R, L, -D);
    }
    rep(i, 1, n)
    {
        if (!vis[i]) 
        {
            dfs(i, 0, i);
            if (maxd - mind > (1e9)) 
            {
                puts("No");
                return 0;
            }
            delta[i] = -mind;
        }
    }
    rep(i, 1, n) dis[i] += delta[col[i]];
    rep(i, 0, e - 1)
    {
        int u = E[i ^ 1].v, v = E[i].v, w = E[i].w;
        if (dis[v] - dis[u] != w) 
        {
            puts("No");
            return 0;
        }
    }
    puts("Yes");
    return 0;
}


Measured memory_bytes: 8704
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL mod=1e9+7;
const int MAXN=1e6+5;
const int MAXM=1e6+5;
char a[55];
char b[55];
int n;
int main()
{
    scanf("%d",&n);
    scanf("%s%s",a,b);
    if(n==1)
    {
        printf("3\n");
        return 0;
    }
    int st;
    LL ans;
    int f;
    if(a[0]!=a[1])
    {
        f=1;
        ans=3;
        st=1;
    }
    else
    {
        f=2;
        ans=6;
        st=2;
    }
    for(int i=st;i<n;)
    {
        if(i==n-1||(a[i+1]!=a[i]))
        {
            if(f==1)
            {
                ans=(ans*2)%mod;
            }
            else
            {
                f=1;
            }
            i++;
        }
        else
        {
            if(f==1)
            {
                ans=(ans*2)%mod;
                f=2;
            }
            else
            {
                ans=(ans*3)%mod;
            }
            i+=2;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


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

namespace Competition
{
    class Program
    {
        static void Main(string[] args)
        {
            var nd = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var N = nd[0]; var D = nd[1];
            var x = new int[N][];

            for(var i = 0; i < N; i++)
            {
                x[i] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            }
            var res = 0;
            for(var i = 0; i < N - 1; i++)
            {
                for(var j = i + 1; j < N; j++)
                {
                    double sum = 0;
                    for(var d = 0; d < D; d++)
                    {
                        sum += Math.Pow(x[i][d] - x[j][d], 2);
                    }
                    sum = Math.Sqrt(sum);
                    if ((Math.Abs((int)sum - sum)) < 0.000001) res++;
                }
            }
            Console.WriteLine(res);
        }
    }
}


Measured memory_bytes: 13396
#include<stdio.h>
#define N 101
 
int main()
{
  int i,j,n,count=0,v,c,c1,v1;
  int G[N][N],col[N],d[N],pi[N],mincost,i2;
 
  for(i = 0; i < N; i++){
    for(j = 0; j < N; j++)
      G[i][j] = -1;
  }
 
  scanf("%d",&n);
 
  for(i = 0; i < n; i++){
    scanf("%d%d",&v,&c);
    for(j = 0; j < c; j++){
      scanf("%d%d",&v1,&c1);
      G[v][v1] = c1;
    }
  }
 
  for(i = 0; i < n; i++){
    col[i] = 0;
    pi[i] = -1;
    d[i] = 10000001;
  }
 
  d[0] = 0;
 
  while(1){
    mincost = 10000001;
    for(i = 0; i < n; i++){
      if(col[i] != 2 && d[i] < mincost){
        mincost = d[i];
        i2 = i;
      }
    }
    if(mincost == 10000001)break;
 
    col[i2] = 2;
 
 
    for(i = 0; i < n; i++){
      if(G[i2][i] != -1 && col[i] != 2 && (G[i2][i]+d[i2]) < d[i]){
        pi[i] = i2;
        d[i] = G[i2][i] + d[i2];
        col[i] = 1;
      }
    }
  }
  for(i = 0; i < n; i++)printf("%d %d\n",i,d[i]);
    return 0;
}

Measured memory_bytes: 1068
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define inf 1000000005
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))
#define mod 1000000007
#define sz(a) ((ll)(a).size())

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcnt(s);}
#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)

typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
#define eps 1e-12
typedef pair<int, int> II;
typedef pair<ll, ll> LL;
template<class T> T gcd(T a, T b){ T r; while (b != 0) { r = a % b; a = b; b = r; } return a;}
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define PI (2 * acos((ld)0))
#define linf (1ll << 60)
#define y1 y32432
#define y0 y435346

#define maxn 500005

int n, num[maxn], Min, Max;

int main()
{
//	freopen("in.txt", "r", stdin);

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

	cin >> n;
	Min = inf; Max = -inf;
	ms(num, 0);
	For(i, 1, n){
		int a;
		cin >> a;
		num[a]++;
		Min = min(Min, a);
		Max = max(Max, a);
	}

	if(Max - Min > 1){
		cout <<  "No" << endl;
	} else if(Min == Max){
		if(Min == n - 1 || Min <= n / 2){
			cout << "Yes" << endl;
		} else{
			cout << "No" << endl;
		}
	} else{
		if(Min > num[Min] - 1 && Min * 2 - num[Min] + 2 <= n){
			cout << "Yes" << endl;
		} else cout << "No" << endl;
	}

	return 0;
}


Measured memory_bytes: 2176
#include<stdio.h>

int main(void){

	int a, b[100], n;
	scanf("%d", &a);
	for (n = 0; n < a; n++)
	{
		scanf("%d", &b[n]);
	}
	for (n -= 1; n >= 0; n--)
	{
		printf("%d", b[n]);
		if (n != 0)
		{
			printf(" ");
		}
	}
	printf("\n");
	return 0;
}

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

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

using namespace std;
using lint=long long;

const lint INF=1LL<<61;

int main(){
	int n,k; scanf("%d%d",&n,&k);
	int h[300];
	rep(i,n) scanf("%d",&h[i]);

	vector<int> Y(h,h+n);
	Y.emplace_back(0);
	sort(Y.begin(),Y.end());
	Y.erase(unique(Y.begin(),Y.end()),Y.end());
	int m=Y.size();

	static lint dp[301][301][301];
	rep(i,n+1) rep(j,k+1) rep(l,m) dp[i][j][l]=INF;
	dp[0][k][0]=0;
	rep(i,n){
		int idx=lower_bound(Y.begin(),Y.end(),h[i])-Y.begin();
		rep(j,k+1){
			rep(l,m){
				// not change
				dp[i+1][j][idx]=min(dp[i+1][j][idx],dp[i][j][l]+max(h[i]-Y[l],0));
				// change to Y[l]
				if(j>0){
					dp[i+1][j-1][l]=min(dp[i+1][j-1][l],dp[i][j][l]);
				}
			}
		}
	}

	lint ans=INF;
	rep(j,k+1) rep(l,m) ans=min(ans,dp[n][j][l]);
	printf("%lld\n",ans);

	return 0;
}


Measured memory_bytes: 213248
#define N 1000009
i,j,n,a[N],b[N]={},ans=0;
main(){
 scanf("%d",&n);
 for(i=0;i<n;i++){scanf("%d",a+i);b[a[i]]++;}
 for(i=1;i<N;i++){
   if(b[i]==1)ans++;
   if(b[i])for(j=2;j*i<N;j++)b[i*j]=0;
 }
 printf("%d",ans);
}

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

using namespace std;

#define gc getchar_unlocked
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(10);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    ll n, d;
    cin>>n>>d;
    d *= d;
    ll c = 0, t, x, y;
    for(int i=0;i<n;i++)
    {
        cin>>x>>y;
        if(pow(x,2)+pow(y,2) <= d)
            c++;
    }
    cout<<c<<endl;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define MAX_H 100
#define MAX_W 100

int H, W; // Height, width
char farm[MAX_H][MAX_W]; //畑データの格納
int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, 1, 0, -1 };
int num; // 答えを格納する

void dfs(int x, int y, char f) {
	farm[x][y] = '.';
	REP(i, 4) {
		if (0 <= x + dx[i] && x + dx[i] < H && 0 <= y + dy[i] && y + dy[i] < W && farm[x + dx[i]][y + dy[i]] == f) {
			dfs(x + dx[i], y + dy[i], f);
		}
	}
	return;
}

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

	while (1) {
		cin >> H >> W;
		if (H == 0 && W == 0) break;

		num = 0;
		
		REP(i, H) {
			REP(j, W) cin >> farm[i][j];
		}

		REP(i, H) {
			REP(j, W) {
				if (farm[i][j] != '.') {
					dfs(i, j, farm[i][j]);
					num++;
				}
			}
		}
		cout << num << "\n";
	}

	return 0;
}


Measured memory_bytes: 3664
import numpy as np

n=int(input())
a=[int(i) for i in input().split()]
sign=0
ans=0
for i in range(n):
    if a[i]<0:
        a[i]=-1*a[i]
        sign+=1
m=min(a)
if sign%2==1:
    ans=sum(a)-2*m
else:
    ans=sum(a)
print(ans)

Measured memory_bytes: 23296
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define all(x) (x).begin(),(x).end()
#define V vector
typedef V<int> vi;
typedef V<vi> vvi;
typedef long long ll;
typedef pair<int, int> P;
constexpr auto INF = INT_MAX/2;
constexpr auto LINF = 5000000000000000;
constexpr auto MOD = 1000000007;

struct UnionFind {

	vi data;
	UnionFind(int n = 0) {
		data.assign(n, -1);
	}
	int find(int k) {
		if (data[k] < 0) return k;
		return data[k] = find(data[k]);
	}

	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return false;
		if (data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
		return true;
	}

	bool same(int x, int y) { return find(x) == find(y); }
	int size(int k) { return -data[find(k)]; }

};

int main() {

	int n, m, k;
	cin >> n >> m >> k;

	vi fri(n, 0);
	UnionFind uf(n);

	rep(i, m) {
		int a, b;
		cin >> a >> b;
		a--;b--;
		fri[a]++;fri[b]++;
		uf.unite(a, b);
	}

	vvi blo(n, vi(0));
	rep(i, k) {
		int c, d;
		cin >> c >> d;
		c--;d--;
		blo[c].push_back(d);
		blo[d].push_back(c);
	}

	rep(i, n) {
		int ans = uf.size(i) - fri[i] - 1;
		for (auto x : blo[i])if (uf.same(i, x))ans--;
		
		cout << ans;
		if (i < n - 1)cout << " ";
		else cout << endl;
	}

}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

int main(){
    int n;
    cin >> n;
    int ans = 0;
    vector<vector<pii>>xy(n);
    rep(i, 0, n){
        int a;
        cin >> a;
        rep(j, 0, a){
            int b, c;
            cin >> b >> c;
            b--;
            xy[i].push_back(pii(b, c));
        }
    }
    for(int bit=0; bit<(1<<n); bit++){
        bool p = true;
        rep(i, 0, n){
            //i人目検証
            if(!(bit&(1<<i)))continue;
            int m = xy[i].size();
            rep(j, 0, m){
                if(xy[i][j].second==1&& !(bit&(1<<(xy[i][j].first))))p = false;
                if(xy[i][j].second==0&& (bit&(1<<(xy[i][j].first))))p = false;
            }
        }
        if(p){
            int cnt = 0;
            rep(i, 0, n){
                if(bit&(1<<i))cnt++;
            }
            ans = max(ans, cnt);
        }
    }
    cout << ans << endl;
}

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

using namespace std;

using ll = long long int;

#define REP(i, N) for (int i = 0; i < (N); ++i)
#define scanInt(x) int x; scanf("%d", &(x)); getchar();
#define scanLLInt(x) ll x; scanf("%lld", &(x)); getchar();
#define scanVecInt(A, N) vector<int> A(N); REP(i, N){scanf("%d", &(A[i])); getchar();}
#define scanVecLLInt(A, N) vector<ll> A(N); REP(i, N){scanf("%lld", &(A[i])); getchar();}

int main()
{
    scanInt(N);
    scanInt(M);
    scanVecInt(P, N);
    vector<pair<int, int>> xy; xy.reserve(M);
    REP(i, M){
        scanInt(xi); --xi;
        scanInt(yi); --yi;
        xy.emplace_back(xi, yi);
    }
    vector<unordered_set<int>> edges(N); // 各頂点から出る辺
    for (auto& p : xy){
        auto& xi = p.first;
        auto& yi = p.second;
        edges[xi].emplace(yi);
        edges[yi].emplace(xi);
    }
    int result = 0;
    vector<char> flag(N, false);
    REP(s, N){
        if (flag[s]) continue;
        unordered_set<int> reached;
        unordered_set<int> next;
        next.emplace(s);
        reached.emplace(s);
        while (!next.empty()){
            unordered_set<int> next_next;
            for (auto& n : next){
                for (auto& n2 : edges[n]){
                    if (!reached.count(n2)) next_next.emplace(n2);
                }
            }
            next.swap(next_next);
            reached.insert(next.begin(), next.end());
        }
        for (auto& n : reached){
            flag[n] = true;
            if (reached.count(P[n] - 1)) ++result;
        }
    }
    cout << result << endl;
}


Measured memory_bytes: 22052
#include <bits/stdc++.h>
#define fi first
#define se second
#define maxn 3001
#define maxh 200003
#define mod 1000000007
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pi;

int n,h,w;
int dp[maxn];
int fac[maxh];
int invFac[maxh];
vector<pi> forb;

bool comp( pi a , pi b ) {
	return a.fi + a.se < b.fi + b.se;
}

int sub(int a, int b) {
	return (a - b + mod) % mod;
}

int mult(int a, int b) {
	return (LL) a * b % mod;
}

int calcPow(int a, int b) {
	if(b == 0)
		return 1;
	int res = calcPow(a, b/2);
	res = mult(res, res);
	if(b & 1)
		res = mult(res, a);
	return res;
}

int calcInverse(int a) {
	return calcPow(a, mod-2);
}

int calcComb(int a, int b) {
	if(a < 0 || b < 0 || b > a)
		return 0;
	return mult( fac[a] , mult( invFac[b] , invFac[a-b] ) );
}

int calcNoOfPaths(int row, int col) {
	return calcComb(row+col, row);
}

int main() {
	fac[0] = invFac[0] = 1;
	for( int i = 1 ; i < maxh ; i++ ) {
		fac[i] = mult( fac[i-1] , i );
		invFac[i] = calcInverse( fac[i] );
	}
	scanf("%d%d%d",&h,&w,&n);
	for( int i = 0 ; i < n ; i++ ) {
		int x,y;
		scanf("%d%d",&x,&y);
		forb.pb(pi(x,y));
	}
	forb.pb(pi(h,w));
	sort( forb.begin() , forb.end() , comp );
	for( int i = 0 ; i <= n ; i++ ) {
		dp[i] = calcNoOfPaths(forb[i].fi-1, forb[i].se-1);
		for( int j = 0 ; j < i ; j++ ) {
			int diffRow = forb[i].fi - forb[j].fi;
			int diffCol = forb[i].se - forb[j].se;
			int noOfPaths = calcNoOfPaths(diffRow, diffCol);
			dp[i] = sub( dp[i] , mult(dp[j], noOfPaths) );
		}
	}
	printf("%d\n",dp[n]);
	return 0;
}


Measured memory_bytes: 1920
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define lld long long int 

//-4%3=-1
string s1="",s2="";
int dp[10000][10000];
int fun(int a,int b)
{
    if(a==0||b==0)
    return 0;
    if(dp[a][b]==-1){
    if(s1[a]==s2[b])
    return 1+fun(a-1,b-1);
    else
    return max(fun(a,b-1),fun(a-1,b));}
    else
    return dp[a][b];
}
int main() {
	// your code goes here
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t;
    //cin>>t;
    t=1;
    while(t--)
    {
        cin>>s1>>s2;
        memset(dp,-1,sizeof(dp));
        int len1=s1.size(),len2=s2.size();
        //cout<<fun(len1,len2);
        for(int i=0;i<=len1;i++)
        {
            for(int j=0;j<=len2;j++)
            {
                if(i==0||j==0)
                dp[i][j]=0;
                else if(s1[i-1]==s2[j-1])
                dp[i][j]=1+dp[i-1][j-1];
                else
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        //cout<<dp[len1][len2];
        string res="";
        int i=len1,j=len2;
        while(i!=0&&j!=0)
        {
            if(s1[i-1]==s2[j-1])
            {
                res+=s1[i-1];
                j--;
                i--;
            }
            else if(dp[i-1][j]==dp[i][j])
            i--;
            else
            j--;
        }
        reverse(res.begin(),res.end());
        cout<<res;
    }
}



Measured memory_bytes: 390912
#include<bits/stdc++.h>
#define rint register int
#define rep(i,a,b) for (rint i=(a),_E=(b); i<=_E; ++i)
#define per(i,a,b) for (rint i=(a),_E=(b); i>=_E; --i)
#define REP(i,n) for (rint i=(0),_E=(n); i<_E; ++i)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

const int N = 1000005;
char s[N]; int a, b, c, n;

int main() {
	scanf("%s", s + 1);
	n = strlen(s + 1);
	rep (i, 1, n)
		if (s[i] == 'a') ++ a;
		else if (s[i] == 'b') ++ b;
		else if (s[i] == 'c') ++ c;
	if (abs(a - b) <= 1 && abs(b - c) <= 1 && abs(c - a) <= 1)
		puts("YES");
	else
		puts("NO");
	return 0;
}

Measured memory_bytes: 384
h,w=map(int,input().split())
a=[input() for i in range(h)]
yoko=[]
tate=[]
for i in range(h):
  for j in range(w):
    if a[i][j]=="#":
      tate.append(i)
      break
for i in range(w):
  for j in range(h):
    if a[j][i]=="#":
      yoko.append(i)
      break
for i in tate:
  l=[a[i][j] for j in yoko]
  l="".join(l)
  print(l)

Measured memory_bytes: 3064
#[allow(dead_code)]
fn getline() -> String {
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

#[allow(unused_macros)]
macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
}

#[allow(unused_macros)]
macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

#[allow(unused_macros)]
macro_rules! debug { ($x: expr) => { println!("{}: {:?}", stringify!($x), $x) } }
// macro_rules! debug { ($x: expr) => () }

#[allow(dead_code)]
fn show<T>(iter: T) -> String
where
    T: Iterator,
    T::Item: std::fmt::Display
{
    let mut res = iter.fold(String::new(), |sum, e| sum + &format!("{} ", e));
    res.pop();
    res
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::collections::btree_map::Entry::{Occupied, Vacant};

fn naive(n: usize, a: Vec<i32>) -> usize {
    let mut ans = 0;
    for l in 0..n {
        let mut sum = 0;
        let mut xor = 0;
        for r in l..n {
            sum += a[r];
            xor ^= a[r];
            if sum == xor {
                ans += 1;
            }
        }
    }
    ans
}

fn main() {
    use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    macro_rules! printf { ($($arg:tt)*) => (write!(out, $($arg)*).unwrap()); }
    macro_rules! printfln { () => (writeln!(out).unwrap()); ($($arg:tt)*) => (writeln!(out, $($arg)*).unwrap()); }
    
    let n = readl!(usize);
    let a = readlvec!(i32);
    let mut l = 0;
    let mut r = 0;
    let mut sum = 0;
    let mut xor = 0;
    let mut ans = 0;
    while l < n {
        while r < n && sum+a[r] == xor^a[r] {
            sum += a[r];
            xor ^= a[r];
            r += 1;
        }
        // debug!((l, r));
        ans += r-l;
        if l == r {
            l += 1;
            r += 1;
            sum = 0;
            xor = 0;
        } else {
            sum -= a[l];
            xor ^= a[l];
            l += 1;
        }
    }
    printfln!("{}", ans);

}


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

using namespace std;
using Graph = vector<vector<int>>;
using Grid= vector<vector<char>>;
using vin= vector<int>;
using ll=long long;
using vll= vector<ll>;
using vbl=vector<bool>;
using vch=vector<char>;
using pinin=pair<int ,int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repx(i,x,n) for(int i=x; i<(int)(n);i++)
#define rell(i,n) for (ll i=0; i< (ll)(n); i++)
#define sort(x) sort(x.begin(), x.end());
#define rese(x) reverse(x.begin(), x.end());

string yes="Yes",no="No";

int main() {
    int n,k;
    cin>>n>>k;
    vin h(n),dp(n,1001001001);
    rep(i,n) cin>>h[i];
    dp[0]=0;
   
    rep(i,n){
        repx(j,1,k+1){
            
            if(i+j>n-1) break;
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]));
        }
    }
   cout<<dp[n-1]<<endl;

}


Measured memory_bytes: 3880
n=int(input())
if int((2*n)**(0.5))*int((2*n)**(0.5)+1)==2*n:
  print('Yes')
  k=int((2*n)**(0.5)+1)
  print(k)
  l=[]
  for i in range(k):
    if i==0:l.append([j+1 for j in range(k-1)]);lused=k-1
    else:
      addl=[]
      for j in range(i):
        addl.append(l[j][i-1])
      for j in range((k-1)-i):
        addl.append(lused+1)
        lused+=1
      l.append(addl)
  for i in l:print(2*n//k,*i)
else:print("No")

Measured memory_bytes: 47324
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a)-(b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
  
int main() {
  int N;
  cin >> N;
  const int geta = 200000;
  vvi G(2*geta);
  rep(i,N) {
    int x,y;
    cin >> x >> y;
    G[x].push_back(y+geta);
    G[y+geta].push_back(x);
  }
  vi visited(2*geta,0);
  vi cnt;
  auto dfs = [&](auto self,int p) {
    if(visited[p]) return;
    visited[p]++;
    cnt[p/geta]++;
    for(int q:G[p]) {
      self(self,q);
    }
  };
  ll ans = 0;
  rep(i,2*geta) {
    if(visited[i]) continue;
    cnt.assign(2,0);
    dfs(dfs,i);
    ans += ll(cnt[0])*cnt[1];
  }
  cout << ans-N << endl;
  return 0;
}

Measured memory_bytes: 21024
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>

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

using namespace std;

struct UnionFind{
  vector<int> par,num;

  UnionFind(int N) : par(N),num(N){
    for(int i=0;i<N;i++){
      par[i] = i;
      num[i] = 1;
    }
  }

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

  void unite(int x, int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry) return;
    par[rx] = ry;
    num[ry] = num[ry]+num[rx]; 
  }

  int get_num(int x){
    int rx = root(x);
    return num[rx];
  }

  bool same(int x, int y){
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};


int main(void){
  ll n;
  cin >> n;
  ll ans = 0;
  for(ll i=1;i<=n;i++){
    if(i%3 != 0 && i%5 != 0){
      ans += i;
    }
  }

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


Measured memory_bytes: 3636
using System;

namespace AGC025
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());

            int min = int.MaxValue;
            int sum = 0;
            int nA = 0;
            int nB = 0;

            for (int i = 1; i < N; i++)
            {
                nA = i;
                nB = N - i;
                while (0 < nA)
                {
                    sum += nA % 10;
                    nA = nA / 10;
                }

                while (0 < nB)
                {
                    sum += nB % 10;
                    nB = nB / 10;
                }

                min = Math.Min(min, sum);
                sum = 0;

            }

            Console.WriteLine(min);
        }
    }
}


Measured memory_bytes: 13140
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define REP(i,a) for (int i = 0;i < (a);++i)
#define FOR(i,a,b) for (int i = (a);i < (b); ++i)
#define FORR(i,a,b) for (int i = (a);i >= (b); --i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main(){
    ll x,y;
    cin >> x >> y;
    if(x*y > 0){
        if(x > y){
            cout << min(llabs(x)+llabs(y)+1,llabs(llabs(x)-llabs(y))+2)<< endl;
        }else{
            cout << y - x << endl;
        }
    }else if(x*y < 0){
        cout << llabs(llabs(x)-llabs(y))+1 << endl;
    }else{
        if(x == y){
            cout << 0 << endl;
        }else if(x == 0){
            if(y > 0){
                cout << y << endl;
            }else{
                cout << llabs(y)+1 << endl;
            }
        }else{
            if(x > 0){
                cout << x+1 << endl;
            }else{
                cout << -x << endl;
            }
        }
    }
    return 0;
}

Measured memory_bytes: 3652
/*
    飲んだ魔剤で家が建つ。
    created at: 2020-04-26 16:39:25
*/

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

#define all(x) (x).begin(),(x).end()
#define rev(x) greater<x>()
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

bool compare_by_b(pair<int, int> a, pair<int, int> b)
{
    if (a.second != b.second) {
        return a.second < b.second;
    } else {
        return a.first < b.first;
    }
}

LL modpower(LL n, LL m, LL mod)
{
    if (m == 0) {
        return 1;
    } else if (m % 2 == 0) {
        LL p = modpower(n, m/2, mod);
        return p*p % mod;
    } else {
        return n*modpower(n, m-1, mod) % mod;
    }
}

LL modinv(LL n, LL mod)
{
    return modpower(n, mod-2, mod);
}

LL modcombination(LL n, LL r, LL mod)
{
    LL ret = 1;

    for (int i = 0; i < r; i++) {
        ret *= (n-i);
        ret %= mod;
        ret *= modinv(i+1, mod);
        ret % mod;
    }

    return ret % mod;
}

int gcd(int k, int l)
{
    if (l > 0) return gcd(l, k%l);
    else return k;
}

struct UnionFind
{
    vector<int> par;

    UnionFind(int n) : par(n) {
        for (int i = 0; i < n; i++) par[i] = i;
    }

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

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
    }

    bool same(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main()
{
    int r, g, b;
    cin >> r >> g >> b;

    int x = 10*g + b;
    if (x % 4 == 0) cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
	const string key[] = { "the", "this", "that" };
//	cut here before submit 
//	freopen("testcase.cc", "r", stdin);
	string str = "";
	
	while (getline (cin, str )){
		int len = str.length();
		int i,j;
		vector <bool> match (3, false);

		for (j = 0; j < 26; ++j){
			string curr = str;
			for (i = 0; i < len; ++i){
				if ( isalpha(str[i]) ){
					curr[i] = (str[i] - 'a' + j) % 26 + 'a';
				} // end if
			} // end for
//			cout << "curr: " << curr << endl;

			for (i = 0; i < 3; ++i){
				if ( curr.find(key[i]) != string::npos ){
					match[i] = true;
				} // end if
			} // end for
			if ((int)count(match.begin(), match.end(), true ) > 0){
				cout << curr << endl;
				break;
			} // end if
		} // end for
	} // end while
		
	return 0;
}

Measured memory_bytes: 0
//inlclude before define 
#define _USE_MATH_DEFINES

//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<type_traits>
#include<numeric>
#include<assert.h>
#include<deque>
#include<iomanip>
#include<set>
#include<tuple>
#include<map>

using namespace std;

//typedef
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef pair<int, int> P1;
typedef long long int ll;

//define
#define INF 1e9
#define NUM 1000000007
#define all(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);i++)
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))

//function

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

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

/*int dig(int a) {
	int sum = 0;
	while (a) {
		sum += a % 10;
		a /= 10;
	}
	return sum;
}*/

int main() {
	ll n;
	cin >> n;
	int ans=INF;
	for (ll i = 1; i <= sqrt(n); i++) {
		if (n%i == 0) {
			string s = to_string(i);
			string t = to_string(n / i);
			int a = s.size(), b = t.size();
			a = max(a, b);
			ans = min(ans,a);
		}
	}
	cout << ans << endl;

	

}



Measured memory_bytes: 256
s=input()
mod=pow(10,9)+7
dp=[0]*4
dp[0]=1
for si in s:
  if si=='A':
    dp[1]+=dp[0]
  elif si=='B':
    dp[2]+=dp[1]
  elif si=='C':
    dp[3]+=dp[2]
  else:
    dp[3]*=3
    dp[3]+=dp[2]
    dp[2]*=3
    dp[2]+=dp[1]
    dp[1]*=3
    dp[1]+=dp[0]
    dp[0]*=3
  dp[3]%=mod
  dp[2]%=mod
  dp[1]%=mod
  dp[0]%=mod
print(dp[3])


Measured memory_bytes: 9092
#include <stdio.h>
int main(void)
{

  int a, b;

  scanf ("%d %d", &a, &b);

  if (a < b)
     printf ("a < b\n");

  else if (a > b)
     printf ("a > b\n");

  else
     printf ("a == b\n");

  return 0;

}

Measured memory_bytes: 580
import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		boolean i = false;
		boolean j = false;
		boolean k = false;
		
		String[] s = new String[N];
		
		for(int l = 0;l < N;l++) {
			s[l] = sc.next();
		}
		
		for(int l = 0;l < N;l++) { //Pink,White,Greenのみの袋かどうか判断する
			
			if(s[l].equals("P")) {
				i = true;
			}
			else if(s[l].equals("W")) {
				j = true;
			}
			else if(s[l].equals("G")) {
				k = true;
			}
			else {
				i = false;
				j = false;
				k = false;
				break;
			}
			
		}
		if(i && j && k) System.out.println("Three");
		else if(!i && !j && !k) System.out.println("Four");
		sc.close();
	}

}

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

namespace AOJ_Volume11_1149
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                var nwd = ReadInt();
                int n = nwd[0];
                int w = nwd[1];
                int d = nwd[2];
                if (n == 0 && w == 0 && d == 0) break;

                List<Cake> cakes = new List<Cake>();
                cakes.Add(new Cake() { w = w, d = d });

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

                    int piece = cut[0] - 1;
                    int place = cut[1];

                    Cake c = cakes[piece];

                    int div = c.w + c.d;
                    int pos = place % div;

                    Cake c1;
                    Cake c2;

                    if (pos < c.w)
                    {
                        c1 = new Cake() { w = pos, d = c.d };
                        c2 = new Cake() { w = c.w - pos, d = c.d };
                    }
                    else
                    {
                        c1 = new Cake() { w = c.w, d = pos - c.w };
                        c2 = new Cake() { w = c.w, d = c.w + c.d - pos };
                    }

                    if (c1.d < 0 || c1.w < 0 || c2.d < 0 || c2.w < 0)
                        Console.WriteLine("a");

                    if (c1.Size() > c2.Size())
                    {
                        cakes.Add(c2);
                        cakes.Add(c1);
                    }
                    else
                    {
                        cakes.Add(c1);
                        cakes.Add(c2);
                    }

                    cakes.RemoveAt(piece);
                }

                cakes = cakes.OrderBy(x => x.Size()).ToList();
                Console.WriteLine(string.Join(" ", cakes.Select(x => x.d * x.w)));
            }

            Console.ReadKey();
        }

        struct Cake
        {
            public int w;
            public int d;
            public int Size()
            {
                return w * d;
            }
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}



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

using namespace std;

map<uint64_t, uint64_t> memo;

uint64_t rec(uint64_t v)
{
	if (v == 0)
		return 0;
	else if (memo[v])
		return memo[v];
	else
	{
		uint64_t k = 2 * rec(v / 2);
		memo[k] = k + 1;
		return k + 1;
	}
}


int main()
{
	long long int H;
	cin >> H;
	memo[1] = 1;
	cout << rec(H);


	return 0;
}


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

#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")

const int INF = 1000000000;

#define vi vector<int>
#define ll long long int
#define vvi vector<vi>
#define pi pair<int,int>
#define vp vector<pi>
#define vvp vector<vp>
#define vl vector<ll>
#define vvl vector<vl>
#define pl pair<ll,ll>
#define vpl vector<pl>

#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fe first
#define se second
#define all(x) x.begin(),x.end()


int main()
{
    ll x,k,d;
    cin >> x >> k >> d;
    int isneg = 0;
    if(x<0) { isneg = 1; x *= -1; }
    else if(x==0){
        if(k%2) cout << d << endl;
        else cout << 0 << endl;
        return 0;
    }
    ll p = x/d;
    if(p == 0){
        if(k%2) cout << d-x << endl;
        else cout << x << endl;
        return 0;
    }
    if(k>=p){
        x = x%d;
        k -= p;
        if(k%2) cout << d-x << endl;
        else cout << x << endl; 
    }
    else{
        x -= (d*k);
        cout << x << endl;
    }

    return 0;
}

Measured memory_bytes: 3628
#include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
typedef double D;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;

/** Problem0549 : A Traveler **/
int main()
{
	int a, b;
	cin>>a>>b;
	vector<int> dist(a);
	dist[0] = 0;
	rep(i, a-1) {
		int t; cin>>t;
		dist[i+1] = dist[i]+t;
	}
	int ans=0;
	int now=0;
	rep(i, b) {
		int t; cin>>t;
		ans += abs(dist[now+t] - dist[now]);
		now += t;
		ans %= 100000;
	}
	cout << ans << endl;
}

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

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

using namespace std;

int main()
{
  int n;
  ll x[100], y[100];
  ll h[100];
  ll rx = 0LL, ry = 0LL;
  ll rh = 0LL;
  int j = 0;

  cin >> n;
  REP(i,n) {
    cin >> x[i] >> y[i] >> h[i];
    if (h[i] != 0) {
      j = i;
    }
  }


  for(ll xi = 0LL; xi < 101; xi++) {
    for(ll yi = 0LL; yi < 101; yi++) {
      rx = xi;
      ry = yi;
      rh = h[j] + abs(xi - x[j]) + abs(yi - y[j]);
      REP(i,n) {
        if (h[i] != max(0LL, rh - abs(xi - x[i]) - abs(yi -y[i]))) {
          rx = -1;
        }
      }
      if (rx != -1LL) {
        break;
      }
    }
    if(rx != -1LL) {
      break;
    }
  }
  cout << rx << " " << ry << " " << rh << endl;
}


Measured memory_bytes: 256
N, M, Q = map(int, input().split())
rec = [[0] * N for i in range(N)]
for i in range(M):
    l, r = map(int, input().split())
    rec[l - 1][r - 1] += 1

for i in range(N):
    for j in range(1, N):
        rec[i][j] += rec[i][j - 1]

for j in range(N):
    for i in range(N - 2, -1, -1):
        rec[i][j] += rec[i + 1][j]

for i in range(Q):
    p, q = map(int, input().split())
    print(rec[p - 1][q - 1])

Measured memory_bytes: 9332
import java.util.Scanner
val scanner = Scanner(System.`in`)

fun main(args: Array<String>){
    var n = scanner.nextInt()
    var aa = (0 until n).map{ scanner.nextLong() }.toLongArray()

    var pos = aa[0] + aa[1]
    var neg = -pos

    for (i in 2 until n) {
        val la = aa[i - 1]
        val a = aa[i]
        val p = Math.max( pos + a, neg + a)
        val n= Math.max( pos -la*2 - a, neg + la*2 - a)
        pos = p
        neg = n
    }

    println(Math.max(pos, neg))

}





Measured memory_bytes: 61948
n=int(input())
*a,=map(int,input().split())
*b,=map(int,input().split())
*c,=map(int,input().split())
ans=0
for i in range(n):
	ans += b[i]
for  i in range(n-1):
	if a[i+1]==a[i]+1:
		ans += c[a[i]-1]
print(ans)

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

int main(){
  int a,b,k;
  cin >> a >> b >> k;
  int count = 0;
  int i = min(a,b);
  while(1){
    if(a % i == 0 && b % i == 0){
      ++count; 
    }
    if(count == k){
      cout << i << endl;
      return 0;
    }
    --i;
  }
  return 0;
}


Measured memory_bytes: 256
function myin(){return require("fs").readFileSync("/dev/stdin", "utf8").trim();}
function myout(t){console.log(t);}//standard output
function myerr(t){console.error(t);}//standard error
//[no]param
//0:何もしない  1:数値へ変換  2:半角SPで分割  3:改行で分割  4:半角SPで分割し、数値配列へ
//5:改行で分割し、数値配列へ  6:1文字に分割  7:1文字に分割し、数値配列へ
function myconv(i,no){switch(no){case 0:return i;case 1:return parseInt(i);case 2:return i.split(" ");case 3:return i.split("\n");case 4:return i.split(" ").map((a)=>Number(a));case 5:return i.split("\n").map((a)=>Number(a));case 6:return i.split("");case 7:return i.split("").map((a)=>Number(a));}}
function Main(input) {
  input = myconv(input,6);
  input.pop();
  while(true){
    if(input.length % 2 == 0){
       var passed = true;
       for(var i = 0; i < input.length / 2; i++){
         if(input[i] != input[i+input.length / 2]){
            passed = false;
           break;
         }
       }
      if(passed){
         myout(input.length);
        return;
      }
    }
    input.pop();
  }
}
Main(myin());

Measured memory_bytes: 7372
#include <bits/stdc++.h>
using namespace std;
#define int long long
int solve(int n, int k)
{
    if (n == 0 || k == 1)
        return 0;
    n -= (n / k) * k;
    if (abs(n - k) > n)
        return n;
    return abs(n - k);
}
int32_t main()
{
    int n, k;
    cin >> n >> k;
    cout << solve(n, k);
}

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

public class Main {
    public static void main(String[] args){
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int t=Integer.parseInt(br.readLine());
            long sum=0;
            for(int i=1;i<=t;i++){
                for(int j=1;j<=t;j++){
                    for(int k=1;k<=t;k++){
                        int u=gcd(j,k);
                        int p=gcd(i,u);
                        sum+=p;
                    }
                }
            }
            System.out.println(sum);

        }catch(Exception e){
            System.out.println("KKKK "+e.getMessage());
        }
    }
    static int gcd(int a,int b){
        if(a==0){
            return b;
        }
        if(b==0){
            return a;
        }
        if(a>b){
            return gcd(a%b,b);
        }
        return gcd(a,b%a);

    }
}


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

int main()
{
   char s[3],t[3];cin>>s>>t;
    int ans=0;
  if (s[0]==t[0])ans++;
  if (s[1]==t[1]) ans++;
  if (s[2]==t[2]) ans++;
  cout<<ans;
  return 0;}

Measured memory_bytes: 256