akhauriyash/Code-Regression huggingface dataset:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int C[4][1000005],W,H,N,mv[5]={1,0,-1,0,1};//x1 y1 x2 y2
bool fld[6005][6005];

void dbg()
{
	for(int i=0;i<H;i++,puts(""))
		for(int j=0;j<W;j++)
			printf("%s",fld[i][j]?"■":"□");
	puts("");
}

int compress(int *x1,int *x2,int w)
{
	vector<int>xs;
	for(int i=0;i<N;i++)
		for(int d=-1;d<2;d++)
		{
			int tx1=x1[i]+d,tx2=x2[i]+d;
			if(0<=tx1&&tx1<w)xs.push_back(tx1);
			if(0<=tx2&&tx2<w)xs.push_back(tx2);
		}
		sort(xs.begin(),xs.end());
		xs.erase(unique(xs.begin(),xs.end()),xs.end());
		for(int i=0;i<N;i++)
		{
			x1[i]=find(xs.begin(),xs.end(),x1[i])-xs.begin();
			x2[i]=find(xs.begin(),xs.end(),x2[i])-xs.begin();
		}
		return xs.size();
}

int main()
{
	for(;scanf("%d%d",&W,&H),H+W;)
	{
		for(int i=!scanf("%d",&N);i<N;i++)
			scanf("%d%d%d%d",&C[0][i],&C[1][i],&C[2][i],&C[3][i]);
		W=compress(C[0],C[2],W);
		H=compress(C[1],C[3],H);
		memset(fld,0,sizeof(fld));
		for(int i=0;i<N;i++)
		{
			for(int y=C[1][i];y<C[3][i];y++)
				for(int x=C[0][i];x<C[2][i];x++)
					fld[y][x]=1;
		}
		int ans=0;
		for(int i=0;i<H;i++)
			for(int j=0;j<W;j++)
			{
				if(fld[i][j]==true)continue;
				ans++;
				P t=make_pair(i,j);
				queue<P>Q;
				Q.push(t);
				while(!Q.empty())
				{
					t=Q.front();Q.pop();
					if(fld[t.first][t.second])continue;
					fld[t.first][t.second]=true;
					for(int m=0;m<4;m++)
					{
						int ny=t.first+mv[m],nx=t.second+mv[m+1];
						if(0<=ny&&ny<H&&0<=nx&&nx<W&&!fld[ny][nx])
						{
							
							Q.push(make_pair(ny,nx));
						}
					}
				}
			}
		printf("%d\n",ans);
	}
}

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

int a[200000];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n = gi();
    int k = gi();
    if (k >= n) {
        cout << 0 << endl;
        return 0;
    }

    for (int i = 0; i < n; i++)
        a[i] = gi();

    sort(a, a+n);
    ll s = 0;
    for (int i = 0; i < n-k; i++)
        s += a[i];

    cout << s << endl;

    return 0;
}


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

using namespace std;

#define el				'\n'
#define sz(v)			((int)((v).size()))
#define all(v)          ((v).begin()),((v).end())
#define clr(v, d)		memset(v, d, sizeof(v))

double const EPS = 1e-8, PI = acos(-1);
const int N = 100 + 9, M = 1e5 + 9, OO = (int) 1e6 + 1;
const long long MOD = 1e9 + 7;

typedef long long ll;

void INPUT() {
	cout << fixed << setprecision(0);
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}

ll n, W, weights[N], values[N], mem[N][M];
//map<pair<int, ll>, ll> mem;

ll dp(int i, ll w) {
	if (i == n)
		return 0;
	ll &ret = mem[i][w];
	if (ret != -1)
		return ret;
	ll a = 0, b = 0;
	a = dp(i + 1, w);
	if (weights[i] <= w) {
		b = dp(i + 1, w - weights[i]) + values[i];
	}
	ret = max(a, b);
	return ret;
}

int main() {
	INPUT();
	cin >> n >> W;
	clr(mem, -1);
	for (int i = 0; i < n; ++i) {
		cin >> weights[i] >> values[i];
	}
	cout << dp(0, W);

	return 0;

}

Measured memory_bytes: 85376
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef long long int lli;

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

#ifdef LOCAL
	#define eprintf(...) printf(__VA_ARGS__)
#else 
	#define eprintf(...) 1
#endif

#define ROUNDUP(a, b) 		( ((a) + ((b) - 1) ) / b)
#define SET_ZERO(a)	 		( memset((a), 0, sizeof(a)))
#define SET_NUM_ARRAY(a,b)	( memset((a), b, sizeof(a)))

int main() {
	int N, Z, W;
	scanf("%d %d %d", &N, &Z, &W);

	int al1, al2;
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		if (i == N-2)
			al2 = a;
		if (i == N-1)
			al1 = a;
	}

	if (N == 1) {
		printf("%d\n", abs(al1-W));
	} else {
		printf("%d\n", abs(al1-W)>abs(al2-al1)?abs(al1-W):abs(al2-al1));
	}
	return 0;
}


Measured memory_bytes: 256
/**
 *  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_C
 */
#include <iostream>
#include <string>
#include <set>
#include <unordered_map>

using namespace std;

unordered_map<char, int> chardict;

long
gethash(const unordered_map<char, int> &chardict, const string word)
{
    long ret = 0;
    for (char it : word) {
        ret += chardict.at(it) + (ret << 3);
    }
    return ret;
}

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

    chardict.insert(decltype(chardict)::value_type('A', 1));
    chardict.insert(decltype(chardict)::value_type('C', 2));
    chardict.insert(decltype(chardict)::value_type('G', 3));
    chardict.insert(decltype(chardict)::value_type('T', 4));

    set<long> dict;
    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);

        if (line[0] == 'f') {
            auto found = dict.find(gethash(chardict, line.substr(5)));
            cout << (found == dict.end() ? "no" : "yes") << endl;
        } else {
            dict.insert(gethash(chardict, line.substr(7)));
        }
    }

    return 0;
}

Measured memory_bytes: 15976
#include <iostream>

using namespace std;

int main()
{
    int A, B, C, X;
    cin >> A >> B >> C >> X;
    
    int ans = 0;
    for (int i = 0; i <= A; i++){
        for (int j = 0; j <= B; j++){
            for (int k = 0; k <= C; k++){
                if (i * 500 + j * 100 + k * 50 == X){
                    ans++;
                }
            }
        }
    }
    cout << ans << endl;

}

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

int main() {
  int N; cin>>N;
  if(N%1000==0) cout<<0;
  else cout<<1000-(N%1000);
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n,k;
  cin >> n >> k;
  vector<pll> a(n);
  for(ll i=0;i<n;i++) {
    ll t,d;
    cin >> t >> d;
    a[i]={d,t};
  }
  sort(a.rbegin(),a.rend());
  ll ans=0;
  ll tans=0;
  map<ll,ll> kind;
  priority_queue<pll,vector<pll>,greater<pll>> q;
  for(ll i=0;i<k;i++) {
    tans+=a[i].first;
    kind[a[i].second]++;
    q.push({a[i].first,a[i].second});
  }
  ans=tans+kind.size()*kind.size();
  ll cur=k;
  for(ll i=kind.size()+1;i<=k;i++) {
    ll f=1;
    while(q.size()) {
      ll t=q.top().second;
      ll d=q.top().first;
      q.pop();
      if(kind[t]>1) {
        kind[t]--;
        tans-=d;
        f=0;
        break;
      }
    }
    if(f) break;
    f=1;
    for(;cur<n;cur++) {
      ll t=a[cur].second;
      ll d=a[cur].first;
      if(kind[t]==0) {
        kind[t]++;
        tans+=d;
        cur++;
        f=0;
        break;
      }
    }
    if(f) break;
    ans=max(ans,tans+i*i);
  }
  cout << ans << endl;
}

Measured memory_bytes: 7280
#include <iostream>
#include <vector>
#include <map>

bool compare(std::pair<int, int> a, std::pair<int, int> b){
    return a.second < b.second;
}
int main(){
    int n;
    std::cin >> n;
    
    std::vector<std::pair<int, int>> list(n);
    int index = 1, num;
    for(int i = 0; i < n; ++i){
        std::cin >> num;
        list.at(i) = std::make_pair(index++, num);
    }

    std::sort(list.begin(), list.end(), compare);

    for(int i = 0; i < n; ++i){
        std::cout << list.at(i).first;
        if(list.size() - 1 == i){
            std::cout << std::endl;
        }else{
            std::cout << " ";
        }
    }

    return 0;
}

Measured memory_bytes: 1664
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(a,b) for(int a=0;a<b;a++)
#define Sort(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define fi first
#define se second
#define co(a) cout<<a<<endl
#define sz size
#define bgn begin()
#define en end()
#define pb(a) push_back(a)
#define pop pop_back
template<class T>
 void cou(vector<vector<T>> a){
  int b=a.size();
  int c=a[0].size();
  fo(i,b){
    fo(j,c){
      cout<<a[i][j];
      if(j==c-1)
        cout<<endl;
      else
        cout<<' ';
    }
  }
}
//template<>
 void cou(vector<vector<char>> a){
  int b=a.size();
  int c=a[0].size();
  fo(i,b){
    fo(j,c){
      cout<<a[i][j];
      if(j==c-1)
        cout<<endl;
      else
        cout<<' ';
    }
  }
}
 int wari(int a,int b) {
   if(a%b==0)
     return a/b;
   else
     return a/b+1;
 }
 int keta(int a){
  double b=a;
  b=log10(b);
  int c=b;
  return c+1;
 }
 int souwa(int a){
  return a*(a+1)/2;
 }
 int lcm(int a,int b){
  int d=a,e=b,f;
  if(a<b)
    swap(a,b);
  int c,m=1;
  while(m){
    c=a%b;
    if(c==0){
       f=b;
      m--;
    }
    else{
      a=b;
      b=c;
 }
  }
  return d*e/f;
    }
  int gcm(int a,int b){
      int d=a,e=b,f;
  if(a<b)
    swap(a,b);
  int c,m=1;
  while(m){
    c=a%b;
    if(c==0){
       f=b;
      m--;
    }
    else{
      a=b;
      b=c;
 }
  }
  return f;
  }
 bool prime(int a){
   if(a<2)
     return false;
   else if(a==2)
     return true;
   else if(a%2==0)
     return false;
   double b=sqrt(a);
    for(int i=3;i<=b;i+=2){
      if(a%i==0){
        return false;
      }
    }
   return true;
 }
struct Union{
  vector<int> par;
  Union(int n){
	par= vector<int>(n, -1);
  }
  int find(int x){
    if(par[x]<0)
      return x;
    else
      return par[x]=find(par[x]);
  }
  bool same(int a,int b){
    return find(a)==find(b);
  }
  int Size(int a){
    return -par[find(a)];
  }
  void unite(int a,int b){
    a=find(a);
    b=find(b);
    if(a==b)
      return;
    if(Size(b)>Size(a))
      swap<int>(a,b);
    par[a]+=par[b];
    par[b]=a;
  }
};
int ketas(int a){
  string b=to_string(a);
  int c=0;
  fo(i,keta(a)){
    c+=b[i]-'0';
  }
  return c;
}
signed main(){
  int a,b,c,d=0;
  cin>>a>>b>>c;
  for(int i=1;i<=a;i++){
    if(ketas(i)>=b&&ketas(i)<=c)
      d+=i;
  }
  cout<<d<<endl;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define all(a)  begin((a)),end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

int main()
{
    string S, T; cin >> S >> T;

    bool ok = true;
    map<char, char> smap, tmap;
    rep (i, 0, S.size()) {
        char s = S[i], t = T[i];
        if (smap.count(s)) if (smap[s] != t) ok = false;
        if(tmap.count(t)) if (tmap[t] != s) ok = false;
        smap[s] = t; tmap[t] = s;
    }

    if (ok) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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

using namespace std;

int main(){
    int N;
    cin >> N;
    int V[N],C[N];
    int ans = 0;

    for(int i = 0; i < N; i++) cin >> V[i];
    for(int i = 0; i < N; i++){
        cin >> C[i];
        if( V[i] > C[i] ) ans += V[i] - C[i];
    }

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

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int N=1000005;
const int MOD=998244353;
const int inv=499122177;
int add (int x,int y)   {x=x+y;return x>=MOD?x-MOD:x;}
int mul (int x,int y)   {return (LL)x*y%MOD;}
int dec (int x,int y)   {x=x-y;return x<0?x+MOD:x;}
int Pow (int x,LL y)
{
	if (y==0) return 1;
	if (y==1) return x;
	int lalal=Pow(x,y>>1);
	lalal=mul(lalal,lalal);
	if (y&1) lalal=mul(lalal,x);
	return lalal;
}
int n,m;
LL cnt,cnt1;
LL calc (LL x)	{return (1+x)*x/2;}
map<int,int> mp[N];
int f[N][2];
int main()
{
	scanf("%d%d",&n,&m);
	cnt=calc(n-3);int tot=0,sum=0;
	for (int u=1;u<=m;u++)
	{
		int x,y,c;
		scanf("%d%d%d",&x,&y,&c);
		c++;sum=sum+c;
		if (mp[x][y]!=0)
		{
			if (mp[x][y]!=c) {printf("0\n");return 0;}
			continue;
		}
		mp[x][y]=c;
		int id=max(x,y);//对应的格子
		int dis=(id-x)+(id-y); 
		if (dis==0) tot++;
		if (dis<=1)	continue;
		if (dis>2)	{cnt--;mp[y][x]=c;}
	}
	if (n==2)
	{
		if (m==4)
		{
			if (sum&1) printf("0\n");
			else printf("1\n");
		}
		else printf("%d",Pow(2,3-m));
		return 0;
	}
	//printf("YES:%lld\n",cnt);
	LL cnt1=0;
	for (int u=2;u<n;u++)
	{
		int x=-1,y=-1;
		if (mp[u-1][u+1]!=0) x=mp[u-1][u+1]-1;
		if (mp[u+1][u-1]!=0) y=mp[u+1][u-1]-1;
		if (x==-1&&y==-1) cnt1++;
		else if (x!=-1&&y!=-1)
		{
			if (mp[u][u]==0)mp[u][u]=(x^y)+1;
			else if (mp[u][u]!=(x^y)+1)	{printf("0\n");return 0;}
		}
	}
	//printf("%lld %lld\n",cnt1,cnt);
	f[n][1]=1;f[n][0]=1;
	if (mp[n][n]!=0) f[n][2-mp[n][n]]=0;
	//printf("YES:%d %d\n",f[n][0],f[n][1]);
	for (int u=n-1;u>=1;u--)
	{
		int x=-1,y=-1;
		if (mp[u][u+1]!=0) x=mp[u][u+1]-1;
		if (mp[u+1][u]!=0) y=mp[u+1][u]-1;
		if (x!=-1&&y!=-1)
		{
			f[u][x^y]=f[u+1][0];
			f[u][x^y^1]=f[u+1][1];
		}
		else if (x==-1&&y==-1)
		{
			f[u][0]=mul(add(f[u+1][0],f[u+1][1]),2);
			f[u][1]=mul(add(f[u+1][0],f[u+1][1]),2);
		}
		else
		{
			f[u][0]=add(f[u+1][0],f[u+1][1]);
			f[u][1]=add(f[u+1][0],f[u+1][1]);
		}
		if (mp[u][u]!=0) f[u][2-mp[u][u]]=0;
		//printf("%d %d %d\n",u,f[u][0],f[u][1]);
	}
	int ans=mul(Pow(2,cnt1+cnt),add(f[1][1],f[1][0]));
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 77696
#!/usr/bin/env python3
# -*- coding:utf-8 -*-


def f(flowers:list):
  li = []
  n_li = []
  for flower in flowers:
    if flower == 0:
      if len(n_li) != 0:
        li += [n_li]
        n_li = []
    else:
      n_li += [flower-1]
  li += [n_li] if len(n_li) != 0 else []
  count = len(li)
  if li == []:
    return 0

  _sum = 0
  for l in li:
    _sum += f(l)
  return count + _sum



if __name__=='__main__':
  N = int(input())
  flowers = [int(x) for x in input().split()]
  print(f(flowers))

Measured memory_bytes: 3188
import sys

# import re
# import math
# import collections
# import decimal
# import bisect
# import itertools
# import fractions
# import functools
import copy
# import heapq
# import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    h, w, k = ns()
    mat = [list(input()) for _ in range(h)]

    ans_mat = [[0 for i in range(w)] for j in range(h)]

    cnt = 1
    for i in range(h):
        for j in range(w):
            if mat[i][j] == "#":
                ans_mat[i][j] = copy.deepcopy(cnt)
                cnt += 1

    for i in range(h):
        for j in range(1, w):
            if ans_mat[i][j] == 0:
                ans_mat[i][j] = copy.deepcopy(ans_mat[i][j - 1])
        for j in range(w - 2, -1, -1):
            if ans_mat[i][j] == 0:
                ans_mat[i][j] = copy.deepcopy(ans_mat[i][j + 1])

    for i in range(1, h):
        if max(ans_mat[i]) == 0:
            ans_mat[i] = copy.deepcopy(ans_mat[i - 1])
    for i in range(h - 2, -1, -1):
        if max(ans_mat[i]) == 0:
            ans_mat[i] = copy.deepcopy(ans_mat[i + 1])

    for ai in ans_mat:
        print(*ai, sep=" ")


if __name__ == '__main__':
    main()


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

/*#pragma GCC optimization("O3")
#pragma GCC optimization("avx2")
#pragma GCC optimization("unroll-loops")*/

typedef long long ll;

ll arr[200005];
ll n,k,ans;

ll less_count(ll a){
	//returns number of pairs with sum less than k
	ans=0;
	ll lo,hi,mid;
	for(int x=0;x+1<n;x++){
		if(arr[x]*arr[x+1]>=a && arr[x]*arr[n-1]>=a)continue;//no point to continue increasing...
		if(arr[x]==0){
			ans+=(n-x-1);
			continue;
		}
		lo=x+1;hi=n-1;
		lo-=x;hi-=x;
		while(lo<hi){
			mid=hi-((hi-lo)/2);
			if(arr[x]<0){
				if(arr[x]*arr[n-mid]<a){
					lo=mid;
				}else hi=mid-1;
			}else{
				if(arr[x]*arr[mid+x]<a){
					lo=mid;
				}else hi=mid-1;
			}
		}
		ans+=lo;
	}
	return ans;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin>>n>>k;
	for(int x=0;x<n;x++){
		cin>>arr[x];
	}
	sort(arr,arr+n);
	ll lo,hi,mid;
	lo=-1e18;
	hi=1e18;

	//we want less_count of answer to be between at most k-1...
	while(lo<hi){
		mid=hi-(hi-lo)/2;
		if(less_count(mid)<k){
			lo=mid;
		}else{
			hi=mid-1;
		}
	}
	cout<<lo<<'\n';
	return 0;
}


Measured memory_bytes: 1792
using System;
using System.Text;


class Program
{


    static void Main(string[] args)
    {

        char[] cs = new char[] { ' ', ',' };
        //int val = int.Parse(Console.ReadLine());
        //int[] vals = parseAry(Console.ReadLine().Split(cs));
        //string str = Console.ReadLine();
        //string[] strs = Console.ReadLine().Split(cs);

        //string res="";
        //int res = 0; ;
        //int t = int.Parse(Console.ReadLine());
        //string str = Console.ReadLine();
        ///int[] vals = parseAry(Console.ReadLine().Split(cs));
        //int n = int.Parse(Console.ReadLine());

        int res = 0;

		int t = int.Parse(Console.ReadLine());
		int[] vals = parseAry(Console.ReadLine().Split(cs));

        //int[] vals = parseAry(Console.ReadLine().Split(cs));
		int[] d=new int[vals.Length];
		d[0] = 0;
		d[1] = Math.Abs(vals[0] - vals[1]);

		for ( int i = 2; i < vals.Length; i++ ) {
			int a = d[i-2]+Math.Abs(vals[i - 2] - vals[i]);
			int b = d[i-1]+Math.Abs(vals[i - 1] - vals[i]);
			d[i] = Math.Min(a, b);

        }

        Console.WriteLine(d[d.Length-1]);

        return;
    }

    //　String[]  ----->  int[]
    static int[] parseAry(string[] str)
    {
        int[] nums = new int[str.Length];
        for (int i = 0; i < str.Length; i++) { nums[i] = int.Parse(str[i]); }
        return nums;
    }
    //　String[]  ----->  long[]
    static long[] parseAryL(string[] str)
    {
        long[] nums = new long[str.Length];
        for (int i = 0; i < str.Length; i++) { nums[i] = long.Parse(str[i]); }
        return nums;
    }

    //文字列を1文字ずつString[]に入れる
    static string[] strAry(String str)
    {
        string[] re = new string[str.Length];
        for (int i = 0; i < str.Length; i++) { re[i] = str[i].ToString(); }
        return re;
    }

}

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

int main()
{
  int N;
  cin >> N;
  
  vector<int> a(N+1);
  for(int i = 1; i <= N; i++){
    cin >> a.at(i);
  }
  
  int  num = 0;
  for(int i = 1; i <= N; i += 2){
    if( ((a.at(i) % 2) == 1) ){
      num++;
    }//if
  }//for
  
  cout << num << endl;
  
}

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

int main() {
	int X, Y;
	const int M = 2;
	while(cin >> X >> Y, X || Y) {
		long long table[128][128];
		int m[128][128];
		for(int i = 0; i < 128; i++) {
			for(int j = 0; j < 128; j++) {
				table[i][j] = 0;
				m[i][j] = 1;
			}
		}
		for(int i = M; i < Y + M; i++) {
			for(int j = M; j < X + M; j++) {
				int tmp;
				cin >> tmp;
				m[i][j] = tmp;
			}
		}

		for(int i = M; i < X + M; i++) {
			m[M-1][i] = 0;
			m[M+Y][i] = 0;
			m[M+Y+1][i] = 0;
			if(m[M][i] != 1)
				table[M][i] = 1;
		}

		for(int i = M; i < Y + M; i++) {
			for(int j = M; j < X + M; j++) {
				int dx[] = {-1, 0, 1};
				for(int k = 0; k < 3; k++) {
					if(m[i][j] == 2 && dx[k] == 0) {
						if((m[i + 2][j] == 2) || (m[i + 2][j] == 0))
							table[i + 2][j] += table[i][j];
					}
					else if(m[i][j] == 0) {
						if(((m[i + 1][j + dx[k]] == 2 && dx[k] == 0) || (m[i + 1][j + dx[k]] == 0)) && (i != Y + M - 1 || dx[k] == 0))
							table[i + 1][j + dx[k]] += table[i][j];
					}
				}
			}
		}

		/*
		for(int i = 0; i < Y + 2*M + 2; i++) {
			for(int j = 0; j < X + M*2; j++) {
				cout << table[i][j] << " ";
			}
			cout << endl;
		}
		*/

		long long ans = 0;
		for(int i = M; i < X + M; i++) {
			ans += table[Y+M][i];
			ans += table[Y+M+1][i];
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 1348
import qualified Data.ByteString.Char8 as BS
import Data.Maybe (fromJust)
import Data.List
import Data.Ord

distance [] = []
distance [x] = []
distance (x1:x2:xs) = abs (x1 - x2) : distance (x2:xs)

main :: IO ()
main = do
    [n,_] <- map read . words <$> getLine :: IO [Int]
    x <- map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
    print . sum . drop (n-1) . sortOn Down . distance . sort $ x


Measured memory_bytes: 19836
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		long count = 0;
		int itr = 0;
		int n = Integer.parseInt(sc.next());

		String[] line = new String[n];
		char[][] word = new char[n][10];

		for (int i = 0; i < n; i++) {
			word[i] = sc.next().toCharArray();
			Arrays.sort(word[i]);
		}

		for (int i = 0; i < n; i++) {
			line[i] = String.valueOf(word[i]);
		}

		Arrays.sort(line);

		int ren = 1;
		for (int i = 1; i < n; i++) {
			if(line[itr].equals(line[i])) {
				count+=ren;
				ren++;
			} else {
				itr = i;
				ren=1;
			}
		}
		System.out.println(count);

	}
}

Measured memory_bytes: 58320
//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<unordered_set>
#include<bitset>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

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

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

const int MAX = 1000010;

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

	int N, M;
	string S;

	vector<int> ans;

	cin >> N >> M >> S;

	int p = SZ(S) - 1;
	int c = M;
	int tc = -1;
	while (p != 0)
	{
		bool ok = false;
		rrep(i, 1, M + 1)
		{
			if (p - i < 0)continue;
			if (S[p - i] == '1')continue;
			p -= i;
			ans.push_back(i);
			ok = true;
			break;
		}

		if (!ok)
		{
			cout << -1 << endl;
			return 0;
		}
	}

	reverse(all(ans));

	for (auto v : ans)
	{
		cout << v << " ";
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 1480
#include <iostream>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
#include <queue>
#include <deque>
#include <cmath>
#include <string>

using namespace std;

typedef pair<int,int> P;

void addGame(int a1, const vector<queue<int>>& a, queue<P>& q) {
  if (a[a1].empty()) return;
  int a2 = a[a1].front();
  if (a1 == a[a2].front()) q.push(P(a1,a2));
}

int main() {
  int n;
  int ans = 0;
  cin >> n;

  vector<queue<int>> a(n+1);

  for (int i=1; i<n+1; i++) {
    for (int j=0; j<n-1; j++) {
      int ai;
      cin >> ai;
      a[i].push(ai);
    }
  }

  queue<P> games;

  vector<bool> used(n+1,false);
  for (int i=1; i<n+1; i++) {
    if (used[i]) continue;
    int j = a[i].front();
    if (i == a[j].front()) {
      used[i]=true; used[j]=true;
      games.push(P(i,j));
    }
  }

  while(true) {
    if (games.empty()) break;
    ans++;
    queue<P> nextGame;
    while(!games.empty()) {
      P game = games.front();
      games.pop();
      int a1 = game.first;
      int a2 = game.second;

      a[a1].pop();
      a[a2].pop();

      addGame(a1,a,nextGame);
      addGame(a2,a,nextGame);
    }
    games = nextGame;  
  }


  for (auto ai : a) {
    if (ai.size() > 0 ) {
      ans = -1;
      break;
    }
  }
  cout << ans << endl;

}


Measured memory_bytes: 4352
//written by libra9z
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <valarray>
#include <utility>
#include <numeric>
#include <memory>
#include <functional>
#include <stdexcept>
#include <typeinfo>
#include <limits>
#include <complex>
#include <locale>
using namespace std;
#define Debug(x) cerr << #x << " = " << x << endl
#define Debug22(x,y) cerr<<#x<<" = "<<x<<" , "<<#y<<" = "<<y<<endl;
#define Debug2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define Debug3(x,y,z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = ("<<x<<", "<<y<<", "<<z<<")\n";
#define Debug_arr(v,n) for(int i=0;i<n;++i) cerr<<#v<<"["<<i<<"]"<<" = "<<v[i]<<endl;
#define SET(p,v) fill((__typeof(**p)*)p,(__typeof(**p)*)p+sizeof(p),v);
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define prl cerr << "called: " << __LINE__ << endl
#define all(s) s.begin(),s.end()
bool palindrome(string s)
{
	string t=s;
	reverse(t.begin(),t.end());
	return t==s;
}
string s;
int b[100005];
int main()
{
	cin>>s;
	if (palindrome(s))
	{
		puts("0");
		return 0;
	}
	string t="";
	for (auto x:s)
	{
		if (x!='x')
		{
			t+=x;
		}
	}
	if (!palindrome(t))
	{
		puts("-1");
		return 0;
	}
	int now=0;
	for (int i=0;i<s.size();++i)
	{
		if (s[i]=='x')
		{
			b[now]++;
		}
		else
		{
			now++;
		}
	}
	int ans=0;
	for (int i=0;i<=(now-1)/2;++i)
	{
		ans+=(int)abs(b[i]-b[now-i]);
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 768
#include<iostream>
#define size(array) (sizeof(array) / sizeof(array[0]))
using namespace std;
int main(void){
    char a,b;
    cin >> a >> b;
  if(a > b){
    cout << ">";
  }else if(b > a){
    cout << "<";
  }else{
    cout << "=";
  }
}

Measured memory_bytes: 256
fun main(args: Array<String>) = println(run {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }
    val b = readLine()!!.split(" ").map { it.toInt() }
    val c = readLine()!!.split(" ").map { it.toInt() }

    val getC = mutableSetOf<Int>()
    a.reduce { a,b -> if(a+1==b) getC.add(a); b }
    b.sum() + c.filterIndexed { i, x -> i+1 in getC }.sum()

})

Measured memory_bytes: 37868
#include <stdio.h>
int main(void)
{
	int i,j,k,n,b,f,r,v,room[4][3][10];
	scanf("%d",&n);
	for(i=0;i<4;i++){
		for(j=0;j<3;j++){
			for(k=0;k<10;k++)
				room[i][j][k]=0;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d %d",&b,&f,&r,&v);
		room[b-1][f-1][r-1]=room[b-1][f-1][r-1]+v;
	}
	for(i=0;i<4;i++){
		for(j=0;j<3;j++){
			for(k=0;k<10;k++)
				printf(" %d",room[i][j][k]);
			printf("\n");
		}
		if(i==3) break;
		printf("####################\n");
	}
	return 0;
}

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

int main() {
    int x, n; cin >> x >> n;
    if (n == 0) {
        cout << x << endl;
        return 0;
    }
    vector<int> p(n);

    rep(i, n) {
        cin >> p[i];
    }
    bool is_match = false;
    for (int i = 0; i < n; i++) {
        if (p[i] == x) {
            is_match = true;
            break;
        }
    }
    if (is_match == false) {
        cout << x << endl;
        return 0;
    }

    int ans = 0;
    for (int i = 1; i <= 100; i++) {
        is_match = false;

        for (int j = 0; j < n; j++) {
            if (p[j] == x-i) {
                is_match = true;
                break;
            }
        }
        if (is_match == false) {
            cout << x-i << endl;
            return 0;
        }

        is_match = false;
        for (int j = 0; j < n; j++) {
            if (p[j] == x+i) {
                is_match = true;
                break;
            }
        }
        if (is_match == false) {
            cout << x+i << endl;
            return 0;
        }
    }
}

Measured memory_bytes: 3620
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

def main():
    n = int(input())
    cnt = 0
    for i in range(1,n+1,2):
        if len(make_divisors(i)) == 8:
            cnt+=1
    print(cnt)

if __name__ == '__main__':
    main()



Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
struct edge {
    int from, to;
    T cost;

    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    explicit operator int() const {return to;}
};

class UnionFind {
    vector<int> uni;
    int n;
public:
    explicit UnionFind(int n) : uni(static_cast<u32>(n), -1) , n(n){};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        return true;
    }
};

template< typename T >
T kruskal(vector<edge<T>> &G, int V)
{
    sort(begin(G), end(G), [](const edge< T > &a, const edge< T > &b) { return (a.cost < b.cost); });
    UnionFind tree(V);
    T ret = 0;
    for(auto &e : G) {
        if(tree.unite(e.from, e.to)) ret += e.cost;
    }
    return (ret);
}

int main() {
    int n, m;
    while(cin >> n >> m, n){
        vector<edge<int>> G;
        for (int i = 0; i < m; ++i) {
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            G.emplace_back(a, b, c);
        }
        cout << kruskal(G, n) << "\n";
    }
    return 0;
}


Measured memory_bytes: 3164
#!/usr/bin/env python3

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    t = {times_divide_by_two(x) for x in a}
    if len(t) > 1:
        print(0)
        return
    l = lcm(a)
    print((m + l // 2) // l)

def times_divide_by_two(x):
    res = 0
    for i in range(200):
        if x % 2 > 0:
            return res
        res += 1
        x //= 2

def lcm(li):
    res = 1
    for x in li:
        res *= (x // gcd(res, x))
    return res

def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

if __name__ == "__main__":
    main()


Measured memory_bytes: 14244
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

var n int
var w int
var weight []int
var value []int
var dp [][]float64

func main() {
	sc.Split(bufio.ScanWords)

	n, w = nextInt(), nextInt()
	weight = make([]int, n)
	value = make([]int, n)
	for i := 0; i < n; i++ {
		weight[i] = nextInt()
		value[i] = nextInt()
	}

	dp = make([][]float64, n+10)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]float64, 100100)
		for j := 0; j < 100100; j++ {
			dp[i][j] = math.Inf(1)
		}
	}

	dp[0][0] = 0

	for i := 0; i < n; i++ {
		for sumV := 0; sumV < 100100; sumV++ {

			// i番目の品物を選ぶ
			if sumV-value[i] >= 0 {
				chmin(&dp[i+1][sumV], dp[i][sumV-value[i]]+float64(weight[i]))
			}

			// i番目の品物を選ばない
			chmin(&dp[i+1][sumV], dp[i][sumV])

		}
	}

	// 最適値の出力
	res := 0
	for i := 0; i < 100100; i++ {
		if dp[n][i] <= float64(w) {
			res = i
		}
	}
	// fmt.Println(dp)
	fmt.Println(res)
}

func chmin(a *float64, b float64) bool {
	if *a > b {
		*a = b
		return true
	}
	return false
}

func chmax(a *float64, b float64) bool {
	if *a < b {
		*a = b
		return true
	}
	return false
}


Measured memory_bytes: 89728
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,ans=0,av=0;
  cin>>N;
  vector<int> a(N,0);
  for(int i=0;i<N;++i){
    cin>>a[i];
    av+=a[i];
  }
  for(int i=0;i<N;++i)if(fabs(av-N*a[i])<fabs(av-N*a[ans]))ans=i;
  cout<<ans;
  return 0;
}

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n)  FOR(i, 0, n)
#define ASC(c) sort((c).begin(), (c).end())
#define DESC(c) sort((c).begin(), (c).end(), greater<int>())

#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

typedef long long ll;
typedef unsigned long long ull;
struct edge { int u, v; ll w; };
 
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}

int main() {
    int n, k; cin >> n >> k;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    int g = gcd(a[0], a[1]);
    for(int i = 2; i < n; i++) g = gcd(g, a[i]);
    int m = *max_element(a.begin(), a.end());
    if (k % g == 0 && m >= k) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;
}


Measured memory_bytes: 640
//----------BHAVIK DIWANI(PICT_COMP)---------------
#include<bits/stdc++.h>
#include<stdlib.h>
#include<stdio.h>
#define test ll t; cin>>t; while(t--)
#define FOR(p,n) for(ll i=p;i<(ll)n;i++)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define ll long long
#define int long long
#define ull unsigned long long
#define MAX 1000005
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define endl '\n'
#define vs vector<string>
#define mii map<int,int>
#define msi map<string,int>
#define vpii vector< pair<int, int > >
#define vpsi vector< pair< string ,int > >
#define forci(p,q) for(int i=p;i<q;i++)
ll gcd(ll a,ll b){ if(b==0) return a;    return gcd(b,a%b);}
ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);}
ull power(ull a, ull b) {a %= mod;  ull res = 1;  while (b > 0) { if (b & 1) res = res * a % mod;  a = a * a % mod;  b >>= 1; }  return res%mod; }
ll modmul(ll x, ll y){return (x*y)%mod;}
ll modadd(ll x, ll y){return (x+y)%mod;}
ll modsub(ll x, ll y){return (x-y+mod)%mod;}
ll fact[1000007]={0};
void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<100007;i++)fact[i]=(fact[i-1]*i)%mod;}
ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[r],mod-2)))%mod; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
ll npr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
inline long long toint(const std::string &s) {std::stringstream ss; ss << s; long long x; ss >> x; return x;}
inline std::string tostring(long long number) {std::stringstream ss; ss << number; return ss.str();}
inline std::string tobin(long long x) {return std::bitset<63>(x).to_string();}
/*bool prime[MAX];
ull sieve(){memset(prime,true,sizeof(prime));for(ull p=2;p*p<MAX;p++){if(prime[p]==true){for(ull i=2*p;i<MAX;i+=p){prime[i]=false;}}}prime[0]=0;prime[1]=0;
}
int inv[1000001]={0};
void findinverse(){inv[1] = 1;for(int i = 2; i <=1e6 ; ++i)	inv[i] = (mod - (mod/i) * inv[mod%i] % mod) % mod;}
*/
using namespace std;
int solve()
{
	int n,k;
	cin>>n>>k;
	string s;
	cin>>s;

	int cnt=0;
	for(int i=1;i<n;i++)
		if(s[i]==s[i-1])	cnt++;
	cout<<min(n-1,cnt+2*k)<<endl;
}
signed main()
{
	fastio;
//	test
		solve();
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <sstream>
#include <cmath>
#include <numeric>
#include <array>

using namespace std;

typedef long long ll;

bool is_asc(const vector<int> &p) {
    for (int i = 0; i < p.size() - 1; i++) {
        if (p[i+1] < p[i]) {
            return false;
        }
    }
    return true;
}

int main(int argc, const char * argv[]) {
    int N; cin >> N;
    vector<int> p(N);
    for (int i = 0; i < N; i++) cin >> p[i];
    
    if (is_asc(p)) {
        cout << "YES" << endl;
        return 0;
    }
    
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            swap(p[i], p[j]);
            if (is_asc(p)) {
                cout << "YES" << endl;
                return 0;
            }
            swap(p[i], p[j]);
        }
    }
    
    cout << "NO" << endl;
    
    return 0;
}


Measured memory_bytes: 256
//#pragma warning disable
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System;
using System.Numerics;
using System.Threading.Tasks;
using static System.Math;
using static System.Console;
class Z { static void Main() => new K(); }
class K
{
	int F() => int.Parse(ReadLine());
	long FL() => long.Parse(ReadLine());
	int[] G() => ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
	long[] GL() => ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
	public const int MOD = 1000000007;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	void Solve()
	{
		var N = F();
		var p = G();
		for (var i = 0; i < N; i++) p[i]--;
		var c = 0;
		for (var i = 0; i < N - 1; i++)
			if (p[i] == i)
			{
				p[i] = p[i + 1];
				p[i + 1] = i;
				c++;
			}
		if (p[N - 1] == N - 1) c++;
		WriteLine(c);
	}
}


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

struct edge
{
	ll to, cost;
};


vector<edge> g[100000];
ll dist[100000],ans[100000];

void dfs1(ll idx,int par)
{
	for(auto&i:g[idx])
	{
		if(i.to==par)continue;
		dfs1(i.to, idx);
		dist[idx] = max(dist[idx],dist[i.to]+1);
	}
}


void dfs2(ll idx,int d_par,int par)
{
	vector<pair<ll,int>> d_child;
	d_child.emplace_back(0,-1);
	for(auto &i:g[idx])
	{
		if(i.to==par)d_child.emplace_back(d_par+1,i.to);
		else d_child.emplace_back(dist[i.to]+1,i.to);
	}
	sort(d_child.rbegin(),d_child.rend());
	ans[idx] = d_child[0].first;
	for(auto &i:g[idx])
	{
		if(i.to==par)continue;
		dfs2(i.to,d_child[d_child[0].second==i.to].first,idx);
	}
}


int main()
{
	ll n;
	cin >> n;
	for(ll i = 0;i<n-1;i++)
	{
		ll a,b;
		cin >> a >> b;
		a--;b--;
		g[a].push_back((edge){b,1});
		g[b].push_back((edge){a,1});
	}
	dfs1(0,-1);
	dfs2(0,0,-1);
	for(ll i =0;i<n;i++)
	{
		cout<<(n-1)*2-ans[i]<<endl;
	}
	return 0;
}


Measured memory_bytes: 35108
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<utility>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<stack>
using namespace std;
#define ll int64_t
#define REP(i, n) for (ll i=0; i<n; i++)

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

ll N;
Graph g;
vector<ll> ans(N, 0);

void dfs(ll v, ll p, ll c) {
    ans[v] = c;
    for (auto e : g[v]) {
        if (e.first == p) continue;
        if (e.second & 1) dfs(e.first, v, 1-c);
        else dfs(e.first, v, c);
    }
}

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

    cin >> N;
    g.assign(N, vector<Edge>());
    vector<ll> u(N-1), v(N-1), w(N-1);
    REP(i, N-1){
        ll u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        g[u].push_back(Edge(v, w));
        g[v].push_back(Edge(u, w));
    }

    ans.assign(N, 0);
    dfs(0, -1, 1);

    for (auto v : ans) cout << v << endl;
}

Measured memory_bytes: 13056
#include <atcoder/all>
using namespace std;
using namespace atcoder;

using mint = modint1000000007;

const int max_n = 100000;

int n, m;

int main(){
    cin >> n >> m;
    dsu d(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        d.merge(a-1, b-1);
    }
    vector<vector<int>> list = d.groups();
    cout << list.size() - 1 << endl;
}

Measured memory_bytes: 9024
import math

a,b,x = map(int,input().split())

xdash = x / a
FULL = a * b

if xdash == FULL:
    print(0)
elif FULL / 2 == xdash:
    print(math.degrees(math.atan2(b,a)))
elif FULL / 2 > xdash:
    print(90-math.degrees(math.atan(2*xdash/b/b)))
else:
    print(90-math.degrees(math.atan(a*a/2/(a*b-xdash))))

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



int main(){
  /*int n, i, j, ct=0, ans=0;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];*/
  //string s;
  //cin >> s;
  int n, m;
  cin >> n >> m;
  if(n==m) cout << "Yes" << endl;
  else cout << "No" << endl;


}

Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007

vector<ll>A,B,C;

int main() {
	ll x,y,a,b,c;
	cin>>x>>y>>a>>b>>c;
	for(ll i=0;i<a;i++){
		ll t;
		cin>>t;
		A.push_back(t);
	}
	for(ll i=0;i<b;i++){
		ll t;
		cin>>t;
		B.push_back(t);
	}	
	for(ll i=0;i<c;i++){
		ll t;
		cin>>t;
		C.push_back(t);
	}
	sort(A.begin(),A.end());
	sort(B.begin(),B.end());
	sort(C.begin(),C.end());
	reverse(A.begin(),A.end());
	reverse(B.begin(),B.end());
	reverse(C.begin(),C.end());
	vector<ll>ans;
	for(ll i=0;i<x;i++){
		ll now=A[i];
		ans.push_back(now);
	}
	for(ll i=0;i<y;i++){
		ll now=B[i];
		ans.push_back(now);
	}
	for(ll i=0;i<c;i++){
		ll now=C[i];
		ans.push_back(now);
	}
	sort(ans.begin(),ans.end());
	reverse(ans.begin(),ans.end());
	ll ANS=0;
	for(ll i=0;i<x+y;i++){
		ANS+=ans[i];
	}
	cout <<ANS;
	// your code goes here
	return 0;
}

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

int main() {

  int n;cin >> n;
  vector<int> t(n,0);
  vector<int> x(n,0);
  vector<int> y(n,0);
  for(int i=0;i<n;i++){
    cin >> t[i] >> x[i] >> y[i];
  }
  // for(int i=0;i<n;i++){
  //   cout << t[i] << x[i] << y[i] << endl;
  // }
  int i=0;
  //一回目
  //制限時間以内に付いているか否か,偶数と奇数が時間と距離で揃っているか否か
  if(x[i]+y[i]>t[i] || (x[i]+y[i])%2 != t[i]%2){
    cout << "No" << endl;
    return 0;
  }
  
  //2都市目以降
  for(i=1;i<n;i++){
    int tmp = abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);
    //制限時間以内に付いているか否か,偶数と奇数が時間と距離で揃っているか否か
    if(tmp>t[i]-t[i-1] || tmp%2 != (t[i]-t[i-1])%2){
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
}


Measured memory_bytes: 1408
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 1e18;

const int N = 2 * (int)1e5;
ll xs[N];
ll ys[N];
int dir[N];

void rotate(int n) {
	for (int i = 0; i < n; ++i) {
		dir[i] = (dir[i] + 1) % 4;
		ll x = xs[i];
		xs[i] = ys[i];
		ys[i] = -x;
	}
}

// Check for those colliding diagonally
ll check(int n) {
	vector<pair<pair<ll, ll>, int>> ord(n);
	for (int i = 0; i < n; ++i) {
		ord[i] = {{xs[i] + ys[i], xs[i]}, dir[i]};
	}
	sort(ord.begin(), ord.end());

	ll res = INF;
	for (int i = 0; i < n;) {
		ll min_ri = -INF;
		ll min_do = -INF;
		int j = i;
		while(j < n && ord[j].first.first == ord[i].first.first) {
			ll x = ord[j].first.second;
			int d = ord[j].second;
			if (d == 0) min_ri = x;
			else if (d == 1) min_do = x;
			else if (d == 2) {
				if (min_do > -INF) res = min(res, 10 * (x - min_do));
			} else {
				if (min_ri > -INF) res = min(res, 10 * (x - min_ri));
			}
			++j;
		}
		i = j;
	}
	return res;
}

// Check for those colliding head on
ll check2(int n) {
	vector<pair<pair<ll, ll>, int>> ord(n);
	for (int i = 0; i < n; ++i) {
		ord[i] = {{xs[i], -ys[i]}, dir[i]};
	}
	sort(ord.begin(), ord.end());

	ll res = INF;
	for (int i = 0; i < n;) {
		ll max_do = INF;
		int j = i;
		while(j < n && ord[j].first.first == ord[i].first.first) {
			ll y = -ord[j].first.second;
			int d = ord[j].second;
			if (d == 1) max_do = y;
			else if (d == 3) {
				if (max_do < INF) res = min(res, 5*(max_do - y));
			}
			++j;
		}
		i = j;
	}
	return res;
}

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

	int n;
	cin >> n;

	for (int i = 0; i < n; ++i) {
		char d;
		cin >> xs[i] >> ys[i] >> d;

		if (d == 'R') dir[i] = 0;
		else if (d == 'D') dir[i] = 1;
		else if (d == 'L') dir[i] = 2;
		else dir[i] = 3;
	}

	// If they go RIGHT - UP, they collide IFF they are on the same diagonal c = x+y, and the UP-going plane is to the right of the other
	// -||- for other pairs of directions.

	ll res = INF;
	for (int i = 0; i < 2; ++i) {
		res = min(res, check(n));
		res = min(res, check2(n));
		rotate(n);
	}
	
	if (res >= INF / 2) cout << "SAFE\n";
	else cout << res << '\n';
}


Measured memory_bytes: 12156
#include <iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    if(i%3==0)
    {
      cout<<' '<<i;
      continue;
    }
    int tmp=i;
    while(1)
    {
      if(tmp==0) break;
      if(tmp%10==3)
      {
        cout<<' '<<i;
        break;
      }
      tmp/=10;
    }
  }
  cout<<endl;
}

Measured memory_bytes: 3092
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
struct data{
    string str;
    int y,l;
    data(string a,int b,int c):
        str(a),l(b),y(c){}

    data(){}
};
int main(){
    int n,m;
    while(cin>>n>>m,n||m){
        vector<data>V(n);
        for(int i=0;i<n;i++){
            string str;
            int l,y;
            cin>>str>>l>>y;
            V[i]=data(str,l,y);
        }
        while(m--){
            int y;
            cin>>y;
            bool flag=true;
            for(int i=0;i<n;i++){
                if(V[i].y>=y&&V[i].y-V[i].l<y){
                    flag=false;
                    cout<<V[i].str<<" "<<y-V[i].y+V[i].l<<endl;
                    break;
                }
            }
            if(flag)cout<<"Unknown"<<endl;
        }

    }

    return 0;
}

Measured memory_bytes: 1280
#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, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int,int>;
using pll = pair<ll,ll>;
using Graph = vector<vector<int>>;

int main(){
    ll K; cin>>K;
    vector<ll> RES(50,0);
    rep(i,50){
        RES[i] = i + K/50;
    }
    rep(i,K%50){
        sort(RES.begin(),RES.end());
        RES[0] += 50;
        rep1(i,49) RES[i] -= 1;
    }
    cout<<50<<endl;
    rep(i,50)cout<<RES[i]<<" ";
    cout<<endl;
}


Measured memory_bytes: 3524
#include <bits/stdc++.h>
using namespace std;
 
 
class Solution {
  vector<int> V;
  vector<pair<int, int>> td;
  int n, q;
  string s;
  bool searchLeft(int v) {
    for (auto t_d : td) {
      char t, d; tie(t, d) = t_d;
      if (t == s[v]) v += d == 'L' ? -1 : 1;
    }
    return v == 0;
  }
  bool searchRight(int v) {
    for (auto t_d : td) {
      char t, d; tie(t, d) = t_d;
      if (t == s[v]) v += d == 'L' ? -1 : 1;
    }
    return v == n + 1;
  }
public:
  void run() {
    cin >> n >> q;
    V.resize(n + 2);
    cin >> s;
    s = " " + s + " ";
    for (int i = 0; i < q; ++i) {
      char t, d; cin >> t >> d;
      td.emplace_back(t, d);
    }
    int L, R;
    {
      int l = 0, r = n + 1;
      while (r - l > 1) (searchLeft(l + r >> 1) ? l : r) = l + r >> 1;
      L = l;
    }
    {
      int l = 0, r = n + 1;
      while (r - l > 1) (searchRight(l + r >> 1) ? r : l) = l + r >> 1;
      R = r;
    }
    cout << R - L - 1 << '\n';
  }     
};
int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  Solution().run();
}


Measured memory_bytes: 3788