akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include<bits/stdc++.h>
#define int long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define mp make_pair
using namespace std;
int N=1e6;
vvi adj(N);
vi vis(N,0);
int dfs(int i)
{
    vis[i]=1;
    int sum=1;
    for(auto j:adj[i])
    if(vis[j]==0)
    sum+=dfs(j);
    return sum;
    
}
int32_t main() {
	// your code goes here

	  //cout<<"Case #"<<x++<<":\n";
	 int n,m,i,j,k;
	cin>>n>>m;
set<pii> s;
int x,y;
for(i=0;i<m;i++)
{
    cin>>x>>y;
    if(s.find(mp(x,y))==s.end()&&s.find(mp(y,x))==s.end())
    {
        adj[x].push_back(y);
        adj[y].push_back(x);
        s.insert(mp(x,y)),s.insert(mp(y,x));
    }
}
int maxi=1;
for(i=1;i<=n;i++)
{
    if(vis[i]==0)
    maxi=max(maxi,dfs(i));
}
cout<<maxi<<"\n";
	return 0;
}


Measured memory_bytes: 72156
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <vector>

using namespace std;

int main(){
   int a,b,c;
   cin>>a>>b>>c;
   if(a+b>=c)cout<<"Yes"<<endl;
   else cout<<"No"<<endl;
   
}

Measured memory_bytes: 256
#include <stdio.h>

#define N_MAX 200

//array[0][i]:xÀW, array[1][i]:yÀW

int main(void){
	int N;
	
	while(scanf("%d\n",&N), N!=0){
		int min[2]={0,0}, max[2]={0,0}, i, array[2][N_MAX];
		
		array[1][0]=0, array[0][0]=0;
		
		for(i=1; i<N; i++){
			int n, d, j; scanf("%d %d\n",&n,&d);
			switch(d){
				case 0: array[0][i]=array[0][n]-1; array[1][i]=array[1][n]; break;
				case 1: array[1][i]=array[1][n]+1; array[0][i]=array[0][n]; break;
				case 2: array[0][i]=array[0][n]+1; array[1][i]=array[1][n]; break;
				case 3: array[1][i]=array[1][n]-1; array[0][i]=array[0][n]; break;
			}
			for(j=0; j<2; j++){
				if(array[j][i]<min[j]) min[j]=array[j][i];
				if(array[j][i]>max[j]) max[j]=array[j][i];
			}
		}
		
		printf("%d %d\n",max[0]-min[0]+1,max[1]-min[1]+1);
	}
	
}

Measured memory_bytes: 0
N = int(input())

pre_x = 0
pre_y = 0
pre_t = 0
flag = True

for i in range(N):
    t, x, y = [int(i) for i in input().split()]  
    dist_diff = abs(x-pre_x) + abs(y-pre_y)
    time_diff = abs(t - pre_t)
    dist_odd_even = dist_diff % 2
    time_odd_even = time_diff % 2
    if dist_diff <= time_diff and dist_odd_even == time_odd_even:
        pre_x = x
        pre_y = y
        pre_t = t
    else :
        flag = False
        break
            
if flag :
    print('Yes')
else :
    print('No')

Measured memory_bytes: 3064
# coding: utf-8
def get_ln_inputs():
    return input().split()


def get_ln_int_inputs():
    return list(map(int, get_ln_inputs()))


def main():
    S = get_ln_inputs()[0]
    if S[:4] == 'YAKI':
        print("Yes")
    else:
        print("No")
    return


main()

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int digits(int n) {
    int res = 0;
    do {
        res += n % 10;
        n /= 10;
    } while (n != 0);
    return res;
}

int main() {
    int N;
    cin >> N;
    int ans = 1 << 30;
    REP(i, 1, N) {
        ans = min(ans, digits(i) + digits(N - i));
    }
    cout << ans << endl;
    return 0;
}


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

int main() {
  int n;
  cin>>n;
  vector<int64_t> lucas(86, 0);
  lucas[0] = 2;
  lucas[1] = 1;
  for (int i=2;i<=n;i++) {
    lucas[i] = lucas[i-2]+lucas[i-1];
  }
  cout<<lucas[n]<<endl;
}

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


int main() {
	int K;
	cin >> K;
	//全探索なんだよ，本質は
	//答えを全探索する
	//その上で，どう実装するか
	//図を書いてみるとどう見ても木構造だよね
	//となるとBFSかDFS
	//桁数が小さい順に出力したいんだからBFSのほうが適当
	queue<string> run;
	for (size_t i = 1; i < 10; i++)
	{
		run.push(to_string(i));
	}
	for (size_t i = 1; i < K; i++)
	{
		if (run.front().at(run.front().size()-1) != '0')run.push(run.front() + char(run.front().at(run.front().size()-1) - 1));
		run.push(run.front() + char(run.front().at(run.front().size()-1)));
		if (run.front().at(run.front().size()-1) != '9')run.push(run.front() + char(run.front().at(run.front().size() - 1) + 1));
		//cout << run.front() << endl;
		run.pop();
	}
	cout << run.front();
}

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

int main() {
	long long h, w, m;
	cin >> h >> w >> m;
	unordered_map<int, int> rows, cols;
	unordered_set<long> idxs;
	int ans = 0;
	for (int i = 0; i < m; ++i) {
        long x, y;
        cin >> x >> y;
        x--; y--;
        rows[x]++;
        cols[y]++;
        idxs.insert(1000000L * x + y);
	}
	vector<pair<int, int>> r2(rows.size()), c2(cols.size());
	int i = 0;
	for (const auto& [k, v]: rows) {
        r2[i].first = v;
        r2[i].second = k;
        i++;
	}
	sort(r2.rbegin(), r2.rend());
	i = 0;
	for (const auto& [k, v]: cols) {
        c2[i].first = v;
        c2[i].second = k;
        i++;
	}
	sort(c2.rbegin(), c2.rend());
	for (int i = 0; i < r2.size(); ++i) {
        for (int j = 0; j < c2.size(); ++j) {
            int temp = r2[i].first + c2[j].first;
            if (temp <= ans) break;
            if (idxs.count(1000000L * (long)r2[i].second + c2[j].second)) temp--;
            ans = max(temp, ans);
        }
	}
	cout << ans << endl;
	return 0;
};

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

int main(void) {
    int n;
    vector<int> zohe;
    cin >> n;
    zohe.resize(n);
    for (int i = 0; i < n; i++) {
        int tmp = i + 1, cnt = 0;
        while (true) {
            if (tmp % 2) break;
            else {
                cnt++;
                tmp /= 2;
            }
        }
        zohe[i] = cnt;
    }
    cout << max_element(zohe.begin(), zohe.end()) - zohe.begin() + 1 << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif
 
const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;
 
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}
 
using i64=int64_t;

void shift(vector<int> &a){
    rotate(a.begin(),a.begin()+1,a.end());
};

void normalize(vector<int>& a){
    auto amin=a;
    rep(i,0,4){
        amin=min(amin,a);
        shift(a);
    }
    a=amin;
}

i64 encode(vector<int>& a){
    i64 res=0;
    rep(i,0,4){
        res*=1000;
        res+=a[i];
    }
    return res;
}

vector<int> reverse(vector<int> &a){
    auto b=a;
    swap(b[0],b[1]);
    swap(b[3],b[2]);
    return b;
}

int f(vector<int>& a){
    if(a[0]==a[1] and a[1]==a[2] and a[2]==a[3]) return 4;
    if(a[0]==a[2] and a[1]==a[3]) return 2;
    return 1;
}

void input(int& N,vector<vector<int>> &C){
    cin >> N;
    C.resize(N);
    rep(i,0,N){
        C[i].resize(4);
        cin >> C[i][0] >> C[i][1] >> C[i][2] >> C[i][3];
        normalize(C[i]);
    }
}

void solve(){
    int N;
    vector<vector<int>> C;
    input(N,C);

    map<i64,i64> count[5]; //1,2,4
    rep(i,0,N) ++count[f(C[i])][encode(C[i])];
 
    auto make_sides=[&](int i,int j){
        vector<vector<int>> sides(4,vector<int>(4));
        rep(k,0,4){
            sides[k][0]=C[i][(k+1)%4];
            sides[k][1]=C[i][k];
            sides[k][2]=C[j][k];
            sides[k][3]=C[j][(k+1)%4];
        }
        return sides;
    };
 
    i64 ans=0;
    rep(i,0,N){
        --count[f(C[i])][encode(C[i])];
        rep(j,i+1,N){
            --count[f(C[j])][encode(C[j])];
            C[j]=reverse(C[j]);
            rep(k,0,4){
                auto sides=make_sides(i,j);
                for(auto &side:sides) normalize(side);
                i64 tmp1=1;
                rep(i_,0,4){
                    i64 tmp2=count[f(sides[i_])][encode(sides[i_])];
                    rep(j_,0,i_) if(encode(sides[j_])==encode(sides[i_])) --tmp2;
                    tmp1*=tmp2*f(sides[i_]);
                }
                ans+=tmp1;
                shift(C[j]);
            }
            C[j]=reverse(C[j]);
            ++count[f(C[j])][encode(C[j])];
        }
    }
    
    cout << ans << endl;
}
 
int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}

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

int main(){
  int n, m, x, y;
  cin >> n >> m >> x >> y;
  vector<int>xp(n);
  vector<int>yp(m);
  
  for(int i=0; i<n; ++i){
    cin >> xp[i];
  }
  for(int i=0; i<m; ++i){
    cin >> yp[i];
  }
  sort(xp.begin(), xp.end());
  sort(yp.begin(), yp.end());
  bool res = false;
  if(xp[n-1] < yp[0]){
   for(int i=xp[n-1]; i<=yp[0]; ++i)
   {   if(i>x && i<y)
       res = true;
   }
  }
  if(res)
    cout << "No War" << endl;
  else
    cout << "War" << endl;
  
 
}

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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var vals = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            Console.WriteLine(vals[0] * vals[1] / 2);
        }

    }
}
//A

Measured memory_bytes: 13268
import sys
from collections import deque
read = sys.stdin.read

N, *ab = map(int, read().split())
graph = [[] for _ in range(N + 1)]

for a, b in zip(*[iter(ab)] * 2):
    graph[a].append(b)

color = [0] * (N + 1)
queue = deque([1])
color_v = [set() for _ in range(N + 1)]

while queue:
    V = queue.popleft()
    number = 1
    for v in graph[V]:
        if number in color_v[V]:
            number += 1
        color[v] = number
        color_v[V].add(number)
        color_v[v].add(number)
        queue.append(v)
        number += 1

print(max(color))
for a, b in zip(*[iter(ab)] * 2):
    print(color[b])

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

void call_old(int n){
  int i = 1;
 CHECK_NUM:
  int x = i;
  if ( x % 3 == 0 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
 INCLUDE3:
  if ( x % 10 == 3 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
  x /= 10;
  if ( x ) goto INCLUDE3;
 END_CHECK_NUM:
  if ( ++i <= n ) goto CHECK_NUM;

  cout << endl;
}

void call(int n){
	int i,x;
	for(i=1;i<=n;i++) {
		// CHECK_NUM:
		x = i;
		if ( x % 3 == 0 ){
    		cout << " " << i;
			// goto END_CHECK_NUM;
    		continue;
    	}
	
 		// INCLUDE3:
 		do {
			if ( x % 10 == 3 ){
		    	cout << " " << i;
    			break;
    		}
    		x /= 10;
    	} while(x);
    }

	cout << endl;
}

int main() {
	int t;
	cin >> t;
	call(t);
	return 0;
}

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

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int array[] = new int[n];
        for (int i = 0;i < n;++i){
            array[i] = sc.nextInt();
        }
        Arrays.sort(array);
        int a = 0,b = 0;
        boolean flag = true;
        for (int i = n - 1;i >= 0;--i){
           if(flag)a +=  array[i];
            else b += array[i];
            flag = !flag;
        }
        System.out.println(a - b);
    }
}

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

int main(){
    string s;
    cin >> s;

    long long int n = s.length();

    /*
    set<string> word;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            string t1 = s.substr(i, j-i+1);
            reverse(t1.begin(),t1.end());

            string t2 = s.substr(0,i) + t1 + s.substr(j+1);
            cout << i << " " << j << " " << t2 << endl;
            if(word.find(t2) != word.end()) cout << "!" << endl;
            word.insert(t2);
        }
    }
    cout << word.size() << endl;
    */
    
    vector<long long int> count(26);
    for(int i=0; i<n; i++){
        count[s[i] - 'a'] += 1;
    }

    long long int ans = 1 + n * (n-1) / 2;
    for(int i=0; i<26; i++){
        ans -= count[i] * (count[i] - 1) / 2;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3752
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define N 1000

int main(){
    int h,w; //hは高さ　wは幅
    cin>>h>>w;
    char ans[N][N];
    for (int i=0; i<=h+1; i++){
        ans[i][0]='#';
        ans[i][w+1]='#';
    }
    for (int i=0; i<=w+1; i++){
        ans[0][i]='#';
        ans[h+1][i]='#';
    }
    for (int i=1; i<=h; i++){
        char a;
        for (int j=1; j<=w; j++){
            cin>>a;
            ans[i][j]=a;
        }
    }
    for (int i=0; i<=h+1; i++){
        for (int j=0; j<=w+1; j++){
            cout<<ans[i][j];
        }
        cout<<endl;
    
    }
}

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

int main(){
/*#ifdef LOCAL
     freopen("in.txt", "r", stdin);
     //freopen("out.txt", "w", stdout);
#else
 fastIO;
#endif*/
//*****************/******************/*************<<
    ll k=0,a=0,b=0;
    cin>>k>>a>>b;
    bool p=0;
    for(int i=a;i<=b;i++){
            if(i%k==0){
             p=1;
             break;
            }
    }
    if(p){
        cout<<"OK"<<endl;
    }else{
        cout<<"NG"<<endl;
    }
}


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

int main() {
	int X;
	cin>>X;
	if(X<9){
		X=9-X;
		if(X<0){
			X=X*-1;
		}
		for(int i=0;i<10;i++){
			if(X<8){
				break;
			}
			X=X-7;
		}
		switch(X){
		case 0:cout<<"sat"<<endl;
			break;
		case 1:cout<<"fri"<<endl;
			break;
		case 2:cout<<"thu"<<endl;
			break;
		case 3:cout<<"wed"<<endl;
			break;
		case 4:cout<<"tue"<<endl;
			break;
		case 5:cout<<"mon"<<endl;
			break;
		case 6:cout<<"sun"<<endl;
			break;
		case 7:cout<<"sat"<<endl;
			break;
		}
	}
	else{
		X=9-X;
		if(X<0){
			X=X*-1;
		}
		for(int i=0;i<10;i++){
			if(X<8){
				break;
			}
			X=X-7;
		}
		switch(X){
		case 0:cout<<"sat"<<endl;
			break;
		case 1:cout<<"sun"<<endl;
			break;
		case 2:cout<<"mon"<<endl;
			break;
		case 3:cout<<"tue"<<endl;
			break;
		case 4:cout<<"wed"<<endl;
			break;
		case 5:cout<<"thu"<<endl;
			break;
		case 6:cout<<"fri"<<endl;
			break;
		case 7:cout<<"sat"<<endl;
			break;
		}
	}
	return 0;
}


Measured memory_bytes: 3112
#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
#define V vector
#define P pair
#define V2(a,b,c) V<V<int>> a(b,V<int>(c))
#define V2a(a,b,c,d) V<V<int>> a(b,V<int>(c,d))
#define incin(a) int a; cin>>a
#define yuko(a) setprecision(a)
//#define min min<int>
//#define max max<int>
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;
}
/*struct aa{
	vector<int> gt;
  aa(int n){
	gt= vector<int>(n, 1);
  }
	void c(V<int> d,int b){
	if(d[b]==0){
		gt[d[b]-1]++;
		gt[gt.sz-1]++;
	}
	else{
		gt[d[b]-1]++;
		c(d,d[d[b]]-1);
	}
}
void cok(int a){
	cout<<gt[a-1]<<endl;
	fo(i,a-1)
	cout<<gt[i]<<endl;
}
};
*/
signed main(){
	string a;
	cin>>a;
	for(int i=0;i<a.sz;i+=2)
		cout<<a[i];
	cout<<endl;
}

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

#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007 
#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define lrep(i,n) for(long long  i = 0; i < (long long)(n); i++)
#define lrep2(i,a,n) for(long long i = (long long)(a); i <= (long long)(n); i++)

#define irrep(i,n) for(int i = (int)(n); i > -1; i--)
#define irrep2(i,a,n) for(int i = (int)(n); i >= (int)(a); i--)

typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<v_char > v2_char;
typedef std::vector<double> v_double;
typedef std::vector<v_double > v2_double;
typedef std::vector<int> v_int;
typedef std::vector<v_int > v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<v_ll > v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<v_string > v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<v_bool > v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;

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

using namespace std; 

int main(void)
{
    string n, m;
    cin >> n >> m;
    //printf("%d\n", n[0]);

    if(n.size() > m.size()) {
        cout << "GREATER";
    } else if(n.size() < m.size()) {
        cout << "LESS";
    } else {
        irep(i, n.size()) {
            if(n[i] > m[i]) {
                cout << "GREATER";
                return 0;
            } else if(n[i] < m[i]) {
                cout << "LESS";
                return 0;
            }
        }
        cout << "EQUAL";
    }

    return 0;
}   
/** atcoder **/


Measured memory_bytes: 256
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <vector>

using namespace std;

int main()
{
    int n;

    scanf("%d", &n);

    vector<int> a(n);

    for (int i = 0; i < n; i++) {
        scanf(" %d", &a[i]);
    }

    long s = 0;

    for (int i = 0; i < n; i++) {
        s += a[i];
    }

    cerr << s << endl;

    long m = LONG_MAX;
    long ps = 0;

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

        if (i == n-1)
            continue;

        m = min(m, abs(2 * ps - s));
    }

    printf("%ld", m);
}

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

int main() {
  ll h,n; cin>>h>>n;
  ll a[n], b[n]; for(int i=0;i<n;i++) cin>>a[i]>>b[i];
  ll dp[n+1][h+1];
  fill(dp[0], dp[n+1], INF);
  dp[0][0]=0;
  for(int i=0;i<n;i++) {
    for(int j=0;j<=h;j++) {
      // ﾄｷ「ｱｰｰｰｰｰｰｰｱｰｰｰｰｰｰｱｰｰｰｰｰｰｱｰｰｰｰｰｱｰｰｰｰｱｰｰｰｱｰｰｰｰｱｰｰｰｰｰ」
      // ﾓﾝｽﾀｰ「ｷﾞｬｰｰｰｰｰｰｰｰｰｰｰ!｣
      dp[i][min(j+a[i],h)]  =min(dp[i][min(j+a[i],h)], dp[i][j]+b[i]);
      //dp[i+1][min(j+a[i],h)]=min(dp[i][j]+b[i];
      dp[i+1][j]=min(dp[i+1][j], dp[i][j]);
    }
  }
  /*
  for(int i=0;i<=n;i++) {
    for(int j=0;j<=h;j++) {
      cout<<dp[i][j]<<" ";
    } cout<<endl;
  }*/
  cout<<dp[n][h]<<endl;
}

Measured memory_bytes: 78464
#include <bits/stdc++.h>
#define rep(i, n)     for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n)    for (int i = 1; i <= (int)(n); ++i)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define rrep(i, n)    for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep1(i, n)   for (int i = (int)(n); i >= 1; --i)
#define allrep(V, v)  for (auto &&V : v)
#define all(x)        (x).begin(), (x).end()
using lint = long long;
constexpr int    INF  = 1 << 30;
constexpr lint   INFL = 1LL << 62;
constexpr int    MOD  = 1e9 + 7;
constexpr double EPS  = 1e-9;
using namespace std;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(void) {}
  Edge(int src_val, int dst_val, Weight weight_val) : src(src_val), dst(dst_val), weight(weight_val) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;

Graph edges_to_graph(const Edges &edges, const int vertices_num, const bool directed = true) {
  Graph ret(vertices_num);
  for (const auto &e : edges) {
    ret[e.src].push_back(e);
    if (!directed) ret[e.src].push_back(Edge(e.dst, e.src, e.weight));
  }
  return ret;
}

void dfs(Graph &g, vector<vector<lint>> &check, int v, int cnt) {
  int step = cnt % 3;
  if (check[v][step] <= cnt) return;
  check[v][step] = cnt;
  for (const auto es : g[v]) {
    dfs(g, check, es.dst, cnt + 1);
  }
}

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

  int n, m;
  cin >> n >> m;
  Edges edges(m);
  rep (i, m) cin >> edges[i].src >> edges[i].dst;
  int s, t;
  cin >> s >> t;
  Graph graph = edges_to_graph(edges, n + 1);
  vector<vector<lint>>  check(n + 1, vector<lint>(3, INFL));
  queue<pair<int, int>> que;
  que.push({s, 0});
  while (!que.empty()) {
    int v = que.front().first, cnt = que.front().second, step = cnt % 3;
    que.pop();
    if (check[v][step] <= cnt) continue;
    check[v][step] = cnt;
    for (const auto es : graph[v]) {
      que.push({es.dst, cnt + 1});
    }
  }
  cout << (check[t][0] != INFL ? check[t][0] / 3 : -1) << endl;
  return 0;
}

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

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  int n, d;
  cin >> n >> d;
  vector< vector< int > > p(n, vector< int >(d));
  for (int i=0; i<n; ++i) {
    for (int j=0; j<d; ++j) cin >> p[i][j];
  }
  int ans = 0;
  for (int i=0; i<n; ++i) {
    for (int j=i+1; j<n; ++j) {
      int dist2 = 0;
      for (int k=0; k<d; ++k) dist2 += (p[i][k]-p[j][k])*(p[i][k]-p[j][k]);
      bool flg = false;
      for (int k=0; k*k<=dist2; ++k) {
        if (k*k==dist2) flg = true;
      }
      if (flg) ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define dbg(x) #x "=" << x << " "
#define fore(i,a,b) for(int i=a,gon=b;i<gon;++i)
#define pb push_back
#define F first
#define S second
typedef double lf;
typedef long long ll;
typedef pair<int, int> pii;

bitset<10> bad;

inline bool valid(int n) {
    while (n) {
        if (bad[n%10]) return false;
        n /= 10;
    }
    return true;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
    #else
        #define endl '\n'
    #endif
    int n, k, d;
    cin >> n >> k;
    while (k--) {
        cin >> d;
        bad[d] = true;
    }
    while (!valid(n)) n++;
    cout << n << endl;
}


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

#define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++)
#define ALL(x) x.begin(),x.end()
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N,K;
bool dp [5001] [5001],dp2 [5001] [5001];

int main()
{
	scanf("%d%d",&N,&K);
	vector<int> A(N);
	FOR(i,0,N){
		scanf("%d",&A [i]);
	}

	dp [0] [0] = true;
	FOR(i,0,N) FOR(j,0,K + 1) if(dp [i] [j]){
		dp [i + 1] [j] = true;
		if(j + A [i] <= K) dp [i + 1] [j + A [i]] = true;
	}
	dp2 [N] [0] = true;
	for(int i = N;i > 0;i--) FOR(j,0,K + 1) if(dp2 [i] [j]){
		dp2 [i - 1] [j] = true;
		if(j + A [i - 1] <= K) dp2 [i - 1] [j + A [i - 1]] = true;
	}

	int ans = N;
	FOR(i,0,N){
		vector<int> acc(K + 2,0);
		FOR(j,0,K + 1){
			acc [j + 1] = acc [j] + dp2 [i + 1] [j];
		}
		FOR(j,0,K + 1) if(dp [i] [j]){
			int l = max(0,K - A [i] - j),r = max(0,K - j);
			if(acc [r] - acc [l] > 0){
				ans--;
				break;
			}
		}
	}

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

	return 0;
}


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

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

  std::vector<int> p(n);
  for (auto&& pi : p) std::cin >> pi;

  int nswap = 0;
  for (int i = 0; i < n; ++i)
    if (p[i] != i + 1) ++nswap;

  std::cout << ((nswap == 2 || nswap == 0) ? "YES" : "NO") << std::endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<map>

using namespace std;
typedef long long ll;
int mod(ll a){
    int c=a%1000000007;
    if(c>=0)return c;
    else return c+1000000007;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
#define inf 100000000/*10^8*/

/////////////////////////////////

int w,h;
int si,sj;

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

int room[25][25];
int ans=0;
int dfs(int x,int y){
    room[x][y]=2;ans++;
    for(int i=0;i<=3;i++){
        int xx=x+dx[i],yy=y+dy[i];
        if(0<=xx&&xx<=h-1&&0<=yy&&yy<=w-1&&room[xx][yy]==0)dfs(xx,yy);
    }
    return ans;
}


int main(){
    while(1){
        cin>>w>>h;
        if(w==0)break;
        ans=0;
        for(int i=0;i<=h-1;i++){
            char s[w+1];cin>>s;
            for(int j=0;j<=w-1;j++){
                if(s[j]=='.')room[i][j]=0;
                else if(s[j]=='#')room[i][j]=1;
                else if(s[j]=='@')room[i][j]=2,si=i,sj=j;
            }
        }
        cout<<dfs(si,sj)<<endl;
    }
    return 0;
}



Measured memory_bytes: 3084
#include<stdio.h>
#define rep(i, n) for(int i = 0; i < n; i++)
int main()
{
    int N, K;
    scanf("%d%d", &N, &K);
    double ans = 0;
    rep(i, N){
        double temp = 1.0/N;
        int n = i+1;
        while(n < K){
            n *= 2;
            temp /= 2;
        }
        ans += temp;
    }
    printf("%.12f\n", ans);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT {
	__INIT() {
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(15);
	}
}__init;



int main(void) {
    ll a,b,c;
    cin>>a>>b>>c;
    vl ngo = {a,b,c};
    sort(all(ngo));
    if(ngo[2]%2 == 0 && ngo[1]%2 == 0 && ngo[0]%2 == 0){
        cout<<((ngo[2]-ngo[0])/2+(ngo[2]-ngo[1])/2)<<endl;
    }
    else if(ngo[2]%2 == 1 && ngo[1]%2 == 0 && ngo[0]%2 == 0){
        cout<<((ngo[1]-ngo[0])/2+ngo[2]-ngo[1])<<endl;
    }
    else if(ngo[2]%2 == 0 && ngo[1]%2 == 1 && ngo[0]%2 == 0){
        cout<<(1+(ngo[2]+1-ngo[1])/2+(ngo[2]+1-ngo[0])/2)<<endl;
    }
    else if(ngo[2]%2 == 0 && ngo[1]%2 == 0 && ngo[0]%2 == 1){
        cout<<(1+(ngo[2]+1-ngo[1])/2+(ngo[2]+1-ngo[0])/2)<<endl;
    }
    else if(ngo[2]%2 == 1 && ngo[1]%2 == 1 && ngo[0]%2 == 0){
        cout<<(1+(ngo[2]+1-ngo[1])/2+(ngo[2]+1-ngo[0])/2)<<endl;
    }
    else if(ngo[2]%2 == 1 && ngo[1]%2 == 0 && ngo[0]%2 == 1){
        cout<<(1+(ngo[2]+1-ngo[1])/2+(ngo[2]+1-ngo[0])/2)<<endl;
    }
    else if(ngo[2]%2 == 0 && ngo[1]%2 == 1 && ngo[0]%2 == 1){
        cout<<((ngo[1]-ngo[0])/2+ngo[2]-ngo[1])<<endl;
    }
    else if(ngo[2]%2 == 1 && ngo[1]%2 == 1 && ngo[0]%2 == 1){
        cout<<((ngo[2]-ngo[1])/2+(ngo[2]-ngo[0])/2)<<endl;
    }
	return 0;
}


Measured memory_bytes: 3624
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    S = set(map(int, readline().split()))

    if len(S) == 2:
        print("Yes")
    else:
        print("No")

if __name__ == '__main__':
    main()


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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
    int N; cin >> N;
    map<pair<char, char>, ll> M;
    for(int i = 1; i <= N; i++) {
        string s = to_string(i);
        char head = s[0], tail = s.back();
       pair<char,char> p(head, tail);
        if(!M.count(p)) M[p] = 1;
        else M[p]++;
    }
    ll ans = 0;
    for(auto u: M) {
        char H = u.first.first, T = u.first.second;
        ll num = u.second;
        pair<char, char> test(T, H);
        if(M.count(test)) ans += num * M[test];
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3712
#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
ll MOD = 1000000007;

// a^p
// 2^3 = 2 * 2^2
// 2^2 = 2 * (2^1)
// 2^1 = 2
ll modpow(ll a, ll p, ll mod) {
  if (p == 0) return 1;

  if (p % 2 == 0) {
    ll half = modpow(a, p / 2, mod) % mod;
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1, mod) % mod;
  }
}

// nCa を求める
ll modCombination(ll n, ll a, ll mod) {
  if (n - a < a) {
    return modCombination(n, n - a, mod);
  }

  ll denominator = 1;  // 分母
  ll numerator = 1;    // 分子

  for (ll i = 0; i < a; i++) {
    denominator *= a - i;
    numerator *= n - i;
    denominator %= mod;
    numerator %= mod;
  }

  return numerator * modpow(denominator, mod - 2, mod) % mod;
}

class UnionFind {
 private:
  vector<ll> parents;

 public:
  UnionFind(ll n) : parents(n, -1) {}

  bool issame(ll x, ll y) { return root(x) == root(y); }

  bool merge(ll x, ll y) {
    if (issame(x, y)) return false;

    ll rx = root(x);
    ll ry = root(y);
    if (parents[rx] > parents[ry]) swap(rx, ry);
    // サイズ情報を更新
    parents[rx] += parents[ry];
    // yの親を更新
    parents[ry] = rx;

    return true;
  }

  ll size(ll x) { return -parents[root(x)]; }

 private:
  ll root(ll x) {
    if (parents[x] < 0) return x;
    // 根の親の値に木の(-)サイズの情報を入れる
    return parents[x] = root(parents[x]);
  }
};

void dfs(const vector<vector<ll>> &G, vector<ll> &points, vector<bool> &seen,
         ll p) {
  if (seen[p]) {
    return;
  }
  seen[p] = true;
  for (auto g : G[p]) {
    if (seen[g] == false) {
      points[g] += points[p];
      dfs(G, points, seen, g);
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  ll N, Q;
  cin >> N >> Q;

  vector<vector<ll>> G(N, vector<ll>());

  for (ll i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  vector<ll> points(N, 0);
  vector<bool> seen(N, false);
  for (ll i = 0; i < Q; i++) {
    ll p, x;
    cin >> p >> x;
    p--;
    points[p] += x;
  }

  dfs(G, points, seen, 0);
  for (auto p : points) {
    cout << p << " ";
  }
  cout << endl;

  return 0;
}


Measured memory_bytes: 30592
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

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

int main()
{
	int n;
	string s,t;
	cin>>n>>s>>t;
	string res="";
	for(int i=0;i<n;i++)
		res+=s[i],res+=t[i];
	cout<<res<<endl;
}

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

using namespace std;
typedef pair<double, double> P;

void solve()
{
	int n;
	while(cin >> n)
	{
		while(n--)
		{
			vector<P> p(4);
			for(int i = 0; i < 4; ++i)
			{
				cin >> p[i].first >> p[i].second;
			}
			if((p[0].first == p[1].first && p[0].second == p[1].second) || (p[2].first == p[3].first && p[2].second == p[3].second))
			{
				cout << "NO" << endl;
			}
			else if((p[0].first == p[1].first && p[2].first != p[3].first) || (p[0].first != p[1].first && p[2].first == p[3].first))
			{
				cout << "NO" << endl;
			}
			else if((p[1].second - p[0].second) / (p[1].first - p[0].first) == (p[3].second - p[2].second) / (p[3].first - p[2].first))
			{
				cout << "YES" << endl;
			}
			else
			{
				cout << "NO" << endl;
			}
		}
	}
}

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

Measured memory_bytes: 0
/*
ID: learnin7
TASK: test
LANG: C++                 
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include<bits/stdc++.h>

/*  Macros from Endagorion */
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;


int main(){
        
    ios_base::sync_with_stdio(0);
    
    //ofstream fout ("test.out");
    //ifstream fin ("test.in");
    //fin >> a >> b;
    //fout << a+b << endl;
    int n,r;
    cin>>n>>r;
    if(n>=10){
        cout<<r<<endl;
    }
    else{
        cout<<r+(100*(10-n))<<endl;
    }
    return 0;
}



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

int main() {
	int a, b;
	cin >> a >> b;
	if ((a+b)%2==0) {
		cout << (a + b) / 2;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}
}

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

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;

//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n%mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
	if (n == 0)return modint(1);
	modint res = (a*a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;

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




void solve() {
    int X, N; cin >> X >> N;
    if(N==0){
        cout << X << endl;
        return;
    }
    vec a(N);
    rep(i, N) cin >> a[i];
    rep(i, 105){
        int tmp1 = X-i;
        int tmp2 = X+i;
        if(count(all(a), tmp1) == 0){
            cout << tmp1 << endl;
            return;
        }
        if(count(all(a), tmp2) == 0){
            cout << tmp2 << endl;
            return;
        }
    }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  //cout << fixed << setprecision(10);
  //init_f();
  //init();
  //int t; cin >> t; rep(i, t)solve();
  solve();
//   stop
    return 0;
}


Measured memory_bytes: 7676
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
#define int long long
using namespace std;
const int N=2*1e5+100;
int n,v[N],w,a[N],sum[N],father[N],fa[N];
int tot,first[N],nxt[N*2],point[N*2];
int dp[N][2];
struct node
{
	int id,zero,one;
};
bool operator <(node a,node b)
{
	return a.one*b.zero>a.zero*b.one;
}
priority_queue <node> q;
int find(int x)
{
	if (x==fa[x]) return x;
	return fa[x]=find(fa[x]);
}
void add_edge(int x,int y)
{
	tot++;
	nxt[tot]=first[x];
	first[x]=tot;
	point[tot]=y;
}
signed main()
{
	tot=-1;
	memset(first,-1,sizeof(first));
	memset(nxt,-1,sizeof(nxt));
	scanf("%lld",&n);
	for (int i=2;i<=n;i++)
	{
		scanf("%lld",&father[i]);
		add_edge(father[i],i);
	}
	for (int i=1;i<=n;i++) fa[i]=i;
	for (int i=1;i<=n;i++) scanf("%lld",&v[i]);
	for (int i=1;i<=n;i++)
	{
		dp[i][v[i]]++;
		if (i!=1) q.push(node{i,dp[i][0],dp[i][1]});
	}
	int ans=0;
	while (!q.empty())
	{
		node x=q.top();
		q.pop();
		if (x.one!=dp[x.id][1] || x.zero!=dp[x.id][0]) continue;
		int y=find(father[x.id]);
		ans+=dp[y][1]*dp[x.id][0];
		dp[y][0]+=dp[x.id][0],dp[y][1]+=dp[x.id][1];
		if (y!=1) q.push(node{y,dp[y][0],dp[y][1]});
		fa[x.id]=y;
	}
	printf("%lld\n",ans);
}


Measured memory_bytes: 22632
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=5010;
const int mod=1000000007;
struct edge
{
	int t;
	edge *next;
}*con[maxn];
int n,sz[maxn];
ll jie[maxn],inv[maxn],g[2][maxn];
ll ksm(ll a,int b){ll r=1;for(;b;b>>=1){if(b&1) r=r*a%mod;a=a*a%mod;}return r;}
ll A(int a,int b){return jie[a]*inv[a-b]%mod;}
ll C(int a,int b){return A(a,b)*inv[b]%mod;}
void ins(int x,int y)
{
	edge *p=new edge;
	p->t=y;
	p->next=con[x];
	con[x]=p;
}
void getroot(int v,int fa,int &root)
{
	bool flag=1;
	sz[v]=1;
	for(edge *p=con[v];p;p=p->next)
		if(p->t!=fa)
		{
			getroot(p->t,v,root);
			sz[v]+=sz[p->t];
			if(sz[p->t]*2>n) flag=0;
		}
	if(sz[v]*2<n) flag=0;
	if(flag) root=(root?-1:v);	
}
ll solve()
{
	int rt=0;
	getroot(1,-1,rt);
	if(rt==-1) return jie[n/2]*jie[n/2]%mod;
	for(edge *p=con[rt];p;p=p->next)
		sz[p->t]=-(sz[p->t]>sz[rt]?n-sz[rt]:sz[p->t]);
	for(int i=1;i<=n;i++)
		sz[i]=(sz[i]>0?0:-sz[i]);		
	int tot=0;
	g[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		tot+=sz[i];
		memset(g[i&1],0,sizeof(g[i&1]));
		for(int j=0;j<=tot;j++)
			for(int k=0;k<=min(sz[i],j);k++)
				g[i&1][j]=(g[i&1][j]+g[(i&1)^1][j-k]*C(sz[i],k)%mod*A(sz[i],k)%mod)%mod;
	}
	ll ans=0;
	for(int i=0,r=1;i<=n;i++,r=-r)
		ans=(ans+(g[n&1][i]*jie[n-i]%mod*r+mod)%mod)%mod;
	return ans;	
}
int main()
{
	scanf("%d",&n);
	jie[0]=1;
	for(int i=1;i<=n;i++)
		jie[i]=jie[i-1]*i%mod;
	inv[n]=ksm(jie[n],mod-2);
	for(int i=n-1;i>=0;i--)
		inv[i]=inv[i+1]*(i+1)%mod;	
	//for(int i=0;i<=n;i++)
	//	cout<<jie[i]<<' '<<inv[i]<<endl;	
	for(int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		ins(x,y);
		ins(y,x);
	}
	printf("%lld",solve());
	return 0;
}

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

int main(){
	int d;
	while(cin >> d){
		while(600 % d){
			cout << "wrong value\n";
			cin >> d;
		}
		int s = 0, h = d, num = 600 / d;
		for(int i = 0;i < num - 1;i++){
			s += (d * pow((600 - h), 2));
			h += d;
		}
		cout << s << "\n";
	}
    return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair <ll, ll> l_l;
typedef pair<int, int> i_i;

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

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

# define EPS (1e-7)
# define INF (1e9)
# define PI (acos(-1))
//const ll mod = 1000000007;

ll N;

int main() {
    cin >> N;
    ll ans = 1e+12+1;
    for (ll a = 1; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll b = N / a;
        if (ans > a + b - 2) ans = a + b - 2;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main()
{
  int n;
  cin >> n;
  vector<int>p(n,0);
  vector<int>mp;
  for(int i = 0; i < n; i++){
    cin >> p[i];
  }
  int flg = 0;
  for(int i = 0; i < n; i++){
    if(p[i] - 1 != i){
      flg ++;
    }
  }
  if(flg == 2 || flg == 0){
    cout << "YES";
  } else{
    cout << "NO";
  }
  return 0;
}


Measured memory_bytes: 256
from sys import stdin
N, K = [int(i) for i in stdin.readline().rstrip().split()]
output = 0
for n in range(1, N + 1):
    first_score = n
    now_score = n
    front_count = 0
    while now_score < K:
        front_count += 1
        now_score *= 2
    output += ((1/2) ** front_count) / N
print(output)

Measured memory_bytes: 2940
N = int(input())
A = list(map(int,input().split()))
p = 1000000007
max_n = len(bin(max(A)))-2
mask = 1
s = 0
for i in range(max_n):
  s0 = 0
  s1 = 0
  for j in range(N):
    s0 += (A[j] & (mask << i)) >> i
    s1 += (~(A[j] | ~(mask << i))) >> i
  s += ((2**i)*s0*s1)%p
print(s%p)

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



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
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

a, b = MAP()

for x in range(100000):
    if int(x * 1.08) == x + a and int(x * 1.1) == x + b:
        print(x)
        exit()
print(-1)


Measured memory_bytes: 3440
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <map>
#include<vector>
#include <cassert>
#include <queue>
#include<functional>
#include<climits>
using namespace std;
template <typename T, const T id>
class CHT {
	struct line {
		T a, b;
		line(T a_ = 0, T b_ = 0) : a(a_), b(b_) {}
		T get(T x) { return a * x + b; }
	};
	struct node {
		line l;
		node *lch, *rch;
		node(line l_) : l(l_), lch(nullptr), rch(nullptr) {}
		~node() {
			if (lch) delete lch;
			if (rch) delete rch;
		}
	};

private:
	const int n;
	const vector<T> pos;
	node *root;

public:
	CHT(const vector<T>& pos_) : n(pos_.size()), pos(pos_), root(nullptr) {}
	~CHT() {
		if (root) delete root;
	}
	void insert(T a, T b) {
		line l(a, b);
		root = modify(root, 0, n, l);
	}
	T get(T x) const {
		int t = lower_bound(pos.begin(), pos.end(), x) - pos.begin();
		assert(t < n && pos[t] == x);
		return sub(root, 0, n, t);
	}

private:
	node * modify(node *p, int lb, int ub, line& l) {
		if (!p) return new node(l);
		if (p->l.get(pos[lb]) >= l.get(pos[lb]) && p->l.get(pos[ub - 1]) >= l.get(pos[ub - 1])) return p;
		if (p->l.get(pos[lb]) <= l.get(pos[lb]) && p->l.get(pos[ub - 1]) <= l.get(pos[ub - 1])) {
			p->l = l;
			return p;
		}
		int c = (lb + ub) / 2;
		if (p->l.get(pos[c]) < l.get(pos[c])) swap(p->l, l);
		if (p->l.get(pos[lb]) <= l.get(pos[lb]))
			p->lch = modify(p->lch, lb, c, l);
		else
			p->rch = modify(p->rch, c, ub, l);
		return p;
	}
	T sub(node *p, int lb, int ub, int t) const {
		if (!p) return id;
		if (ub - lb == 1) return p->l.get(pos[t]);
		int c = (lb + ub) / 2;
		if (t < c) return max(p->l.get(pos[t]), sub(p->lch, lb, c, t));
		return max(p->l.get(pos[t]), sub(p->rch, c, ub, t));
	}
};
int  gcd(int a, int b) {
	if (a < b) gcd(b, a);
	int r;
	while ((r = a % b)) {
		a = b;
		b = r;
	}
	return b;
}
long long lcm(long long a, long long b) {
	return a / gcd(a, b) * b;
}
struct segtree {
	int N, dat[900000];
	segtree() {}
	segtree(int n) {
		N = 1;
		while (N < n) N *= 2;
		for (int i = 0; i < 2 * N - 1; i++)
			dat[i] = INT_MIN;
	}
	// update k th element
	void update(int k, int a) {
		k += N - 1; // leaf
		dat[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
		}
	}
	// min [a, b)
	int query(int a, int b) { return query(a, b, 0, 0, N); }
	int query(int a, int b, int k, int l, int r) {
		if (r <= a or b <= l) return 0;
		if (a <= l and r <= b) return dat[k];
		int m = (l + r) / 2;
		return max(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
	}
};
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	int lb = 0;
	int ub = (n - 1)/2;
	string now;
	cout << 0 << "\n";
	cout.flush();
	cin >> now;
	if (now == "Vacant")return 0;
	string s;
	cout << n - 1<<"\n";
	cout.flush();
	cin >> s;
	if (s == "Vacant")return 0;
	while (ub-lb>1) {
		int mid = (ub + lb) / 2;
		cout << mid*2<<"\n";
		cout.flush();
		cin >> s;
		if (s == "Vacant")break;
		if (s == now) {
			lb = mid;
		}
		else ub = mid;
	}
	cout << ub * 2 - 1<<"\n";
	cout.flush();
}


Measured memory_bytes: 852
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>

using namespace std;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

int n, m;
long long fact[maxn];

int main(int argc, char const *argv[]) {
    
    fact[0] = 1;
    for (int i = 1; i <= 1e5; i ++)
        fact[i] = fact[i - 1] * i % mod;

    cin >> n >> m;

    if (abs(n - m) > 1) puts("0");
    else {
        long long ans = 1;
        ans = ans * fact[n] % mod;
        ans = ans * fact[m] % mod;
        if (n == m) ans = ans * 2 % mod;
        cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 1024