akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)

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

    ll n, k;
    cin >> n >> k;
    vector<ll> a(n), f(n);
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n) cin >> f[i];
    sort(a.begin(), a.end());
    sort(f.begin(), f.end(), greater<ll>());

    ll l = -1, r = 1e13;
    while (r - l > 1) {
        ll mid = (l + r) / 2LL;
        ll cnt = 0;
        rep(i, 0, n) {
            if (a[i] * f[i] > mid) {
                ll z = a[i] * f[i] - mid;
                cnt += (z + f[i] - 1) / f[i];
            }
        }
        if (cnt <= k)
            r = mid;
        else
            l = mid;
    }
    cout << r << endl;
    return 0;
}

Measured memory_bytes: 3456
/*
    Author - Param Kothari
    Ah shit, here we go again
*/
 
/* Ordered Set Implementaton:

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds;

typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
*/

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

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define mk make_pair 
 
typedef long long ll;
typedef long double ld;

void solve(){
    int x, y, z;
    cin >> x >> y >> z;
    swap(x, y);
    swap(x, z);
    cout << x << " " << y << " " << z;
    return;
}
 
int main(){
    IOS;
    int t = 1;
    //cin >> t;
    for(int i = 1; i <= t; i++){
        //cout << "Case #" << i << ": ";
        solve();
    }
    return 0;
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):

    tmp = [a-(i+1) for i, a in enumerate(A)]

    
    def sk(b):
        s = 0
        for i, a in enumerate(A):
            s += abs(a-(b+i+1))
        return s

    # for i in range(-10, 10):
    #     print(i, sk(i))

    s = -int(1e+10)
    e = int(1e+10)
    ssk = sk(s)
    esk = sk(e)
    # print(s, ssk)
    # print(e, esk)
    while abs(s - e) > 1:
        m = (s+e)//2
        msk = sk(m)
        # print(m, msk)
        if ssk < esk:
            esk = msk
            e = m
        else:
            ssk = msk
            s = m
    
    return min(sk(s), sk(e))


def main():
    N = read_int()
    A = read_int_n() 
    print(slv(N, A))


if __name__ == '__main__':
    main()


Measured memory_bytes: 27576
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N, L = map(int, input().split())
# 文字列用のリスト
s = []
# N回分のインプットを受け入れる
for i in range(N):
	s.append(input())
# リストの昇順の並び替え
s.sort()
# 出力
print(''.join(s))

Measured memory_bytes: 3060
n = int(input())
ablist = [tuple(map(int, input().split())) for _ in range(n)]
ablist.sort(key=lambda x: x[1])

now = 0
for a, b in ablist:
    if now + a <= b:
        now += a
    else:
        print("No")
        break
else:
  print("Yes")

Measured memory_bytes: 37460
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args) {
#if DEBUG
        MyDebugger.Test(process, "../TextFile1.txt");
        //MyDebugger.Test(process, "../TextFile2.txt");
        //MyDebugger.MakeTestCase("../TextFile2.txt");
#else
        object ret = process(new StreamReader(Console.OpenStandardInput()));
        Console.WriteLine(ret);
#endif
    }

    static object process(TextReader input) {
        var n = int.Parse(input.ReadLine());
        var vv = ToIntArray(input.ReadLine(), n);

        var dic1 = new Dictionary<int, Num>();
        var dic2 = new Dictionary<int, Num>();

        for(int i=1; i<=100000; i++) {
            dic1.Add(i, new Num() { v = i });
            dic2.Add(i, new Num() { v = i });
        }

        for(int i=0; i<n; i++) {
            Dictionary<int, Num> dic;
            dic = (i % 2 == 0) ? dic1 : dic2;
            dic[vv[i]].c++;
        }

        var ary1 = dic1.Values.ToList();
        var ary2 = dic2.Values.ToList();

        ary1.Sort((x, y) => y.c.CompareTo(x.c));
        ary2.Sort((x, y) => y.c.CompareTo(x.c));

        var r1 = ary1[0];
        var r2 = ary2[0];
        var c = int.MinValue;
        
        Action<Num, Num> Count = (x, y) => {
            if (x.v == y.v) return;

            var z = x.c + y.c;
            if(z > c) {
                c = z;
                r1 = x;
                r2 = y;
            }
        };

        Count(ary1[0], ary2[0]);
        Count(ary1[0], ary2[1]);
        Count(ary1[1], ary2[0]);
        Count(ary1[1], ary2[1]);

        return n - r1.c - r2.c;
    }

    class Num
    {
        public int v;
        public int c;
    }

    static int[] ToIntArray(string strs, int n) {
        var ret = new int[n];
        var spl = strs.Split(' ');
        for (int i = 0; i < n; i++)
            ret[i] = int.Parse(spl[i]);
        return ret;
    }
}

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

int main(int argc, char const *argv[])
{
	int sq[1000];
	for (int i = 0; i < 1000; ++i)
	{
		sq[i]=i*i;
	}
	int n,d;
	cin>>n>>d;
	int x[n][d];
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < d; ++j)
		{
			cin>>x[i][j];
		}
	}
	int ans=0;
	for (int i = 0; i < n; ++i)
	{
		for (int j = i+1; j < n; ++j)
		{
			int dist=0;
			for (int k = 0; k < d; ++k)
			{
				dist+=(x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
			}
			for (int k = 0; k < 1000; ++k)
			{
				if (dist==sq[k])
				{
					++ans;
					break;
				}
				if (dist<sq[k])
				{
					break;
				}
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
using Int = long long;
using namespace std;


int main() {

	Int N, Y;
	cin >> N >> Y;

	Int a = -1;
	Int b = -1;
	Int c = -1;
	Int sum = 0;
	Int flag = 0;
	for (Int i = 0; i <= N; i++) {
		for (Int j = 0; j <= N; j++) {
			Int temp = N - i - j;
			if (temp >= 0) {
				sum = 10000 * i + 5000 * j + 1000 * temp;
				if (sum == Y) {
					a = i;
					b = j;
					c = temp;
					flag = 1;
				}
				if (flag)break;
			}
		}
		if (flag)break;
	}

	cout << a << " " << b << " " << c << endl;
}

Measured memory_bytes: 256
import java.io.*;

class Main {
	public static void main(String[] args) {
		BufferedReader kb = new BufferedReader( new InputStreamReader( System.in ) );
		try {
			String[] nums = kb.readLine().split( " " );
			
			Double a = Double.parseDouble( nums[0] );
			Double b = Double.parseDouble( nums[1] );
			Double C = Double.parseDouble( nums[2] );
			Double radianC = Math.toRadians( C );
			
			Double h = Math.sin( radianC ) * b;
			Double S = ( a*h )/2;
			
			Double a2 = Math.cos( radianC ) * b;
			a2 -= a;
			Double L = Math.sqrt( ( Math.pow( a2, 2 ) + Math.pow( h, 2 ) ) );
			L = L+a+b;
			
			System.out.print( S+"\n"+L+"\n"+h+"\n" );
			
		} catch( IOException e ) {
			System.err.println( e );
		}
	}
}

Measured memory_bytes: 17044
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)

	divisors.sort(reverse = True)
	return divisors

N = int(input())
div = make_divisors(N)
i = 0
ans = 0
m = div[0]
while m >= 1:
	q = N // m
	r = N % m
	if q == r:
		ans += m
		i += 1
		m = div[i]
	else:
		m -= 1
print(ans)


Measured memory_bytes: 3316
IDENTIFICATION DIVISION.
PROGRAM-ID. 166C.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
  SELECT STDIO ASSIGN TO KEYBOARD. 
*>
DATA DIVISION.
FILE SECTION.
FD STDIO.
01 INP        PIC X(1100000).
*>
WORKING-STORAGE SECTION.
01 INW        PIC X(14).
01 N          PIC 9(06) COMP.
01 M          PIC 9(06) COMP.
01 A          PIC 9(06) COMP.
01 B          PIC 9(06) COMP.
01 TBL1.
   03 H       PIC 9(10) OCCURS 100000.
01 TBL2.
   03 HM      PIC 9(10) OCCURS 100000.
01 I          PIC 9(06) COMP.
01 J          PIC 9(06) COMP.
01 P1         PIC 9(07) COMP.
01 P2         PIC 9(07) COMP.
01 CNT        PIC 9(06).
01 OUT        PIC Z(5)9.
*>
PROCEDURE DIVISION.
  ACCEPT INW.
  UNSTRING INW DELIMITED BY ' '
      INTO N M.
*>
  OPEN INPUT STDIO.
  READ STDIO.
  CLOSE STDIO.
*>
  MOVE 1 TO P1.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    PERFORM VARYING P2 FROM P1 BY 1 UNTIL INP(P2:1) = ' '
    END-PERFORM
    MOVE INP(P1:P2 - P1) TO H(I)
    COMPUTE P1 = P2 + 1
  END-PERFORM.
*>
  MOVE ZERO TO TBL2.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > M
    ACCEPT INW 
    UNSTRING INW DELIMITED BY ' '
        INTO A B
    IF (HM(A) < H(B))
      MOVE H(B) TO HM(A)
    END-IF
    IF (HM(B) < H(A))
      MOVE H(A) TO HM(B)
    END-IF 
  END-PERFORM.
*>
  MOVE 0 TO CNT.
  PERFORM VARYING I FROM 1 BY 1 UNTIL I > N
    IF (H(I) > HM(I))
      ADD 1 TO CNT
    END-IF 
  END-PERFORM.
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.


Measured memory_bytes: 6976
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())


N,A = inpl()
xx = inpl()

pos_numbers = []
neg_numbers = []
Zero = 0

for x in xx:
	tmp = x - A
	if tmp < 0:
		neg_numbers.append(-tmp)
	elif tmp == 0:
		Zero += 1
	else:
		pos_numbers.append(tmp)

pos_sum = set([0])
pos_dd = defaultdict(int)
pos_dd[0] = 1
for n in pos_numbers:
	tmp_set = set()
	tmp_list = list(pos_sum)
	tmp_list.sort(reverse=True)
	for s in tmp_list:
		tmp_set.add(s+n)
		pos_dd[s+n] += pos_dd[s]
	pos_sum = pos_sum | tmp_set


neg_sum = set([0])
neg_dd = defaultdict(int)
neg_dd[0] = 1
for n in neg_numbers:
	tmp_set = set()
	tmp_list = list(neg_sum)
	tmp_list.sort(reverse=True)
	for s in tmp_list:
		tmp_set.add(s+n)
		neg_dd[s+n] += neg_dd[s]
	neg_sum = neg_sum | tmp_set

ans = 0
for n in pos_sum:
	ans += pos_dd[n]*neg_dd[n]

ans *= 2**Zero
print(ans-1)


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

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

	if (s[s.length() - 1] == 's') {
		cout << (s + "es");
	} else {
		cout << (s + "s");
	}
	
	return 0;
}


Measured memory_bytes: 3604
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cmath>

using namespace std;
#define PI 3.1415926535897932384626433832795
#define MOD (1000000007)
#define EPS (1e-15)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define bet(x, a, b) (a) <= (x) && (x) < (b)
typedef long long ll;
typedef unsigned long long ull;

int main()
{
	int N, r = 0, b = 0;
	string s;

	cin >> N >> s;

	rep(i, N) {
		if (s[i] == 'R') r++;
		else b++;
	}

	if (r > b) cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}


Measured memory_bytes: 256
import scala.io.StdIn.readInt
  
object Main {
  val r = Array(2,3,5,7)
  
  def twoPow(d:Int,s:Int):(Int,Int) = {
    if(d%2 == 1) (d,s)
    else twoPow(d>>1,s+1)
  }
  
  def maybePrime(i:Int,d:Int,s:Int, n:Int):Boolean = {
    if(i>3) true
    else {
      val a = r(i)
      val x = BigInt(a).modPow(d,n).toLong
  
      if(x==1 || x==n-1) maybePrime(i+1,d,s,n)
      else repeat(s-1,x*x%n, i, d, s, n)
    }
  }
  
  def repeat(j:Int, x:Long, i:Int, d:Int, s:Int, n:Int):Boolean = {
    if(j==0) false
    else if(x==1) false
    else if(x==n-1) maybePrime(i+1,d,s,n)
    else repeat(j-1,x*x%n,i,d,s,n)
  }
  
  def isPrime(n:Int):Boolean = {
    if(n<3000) (2 to Math.sqrt(n).toInt).forall(n%_ != 0)
    else if(n%2==0) false
    else {
      val (d,s) = twoPow(n-1,0)
      maybePrime(0,d,s,n)
    }
  }
  
  def main(args:Array[String]) = {
    val n = readInt
    println((0 until n).count{ _=> isPrime(readInt) })
  }
}

Measured memory_bytes: 61480
n=int(input())-2
hoge=list(map(int,input().split()))
ans=0
for i in range(n):
  a=hoge[i]
  b=hoge[i+1]
  c=hoge[i+2]
  if(a<b and b<c):
    ans+=1
  elif(a>b and b>c):
    ans+=1 
print(ans)

Measured memory_bytes: 3064
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  Name:- OMHARI 
  Institution:- UNIVERSIT OF CALCUTTA(INFORMATION TECHNOLOGY)
  Email:- omharicu@gmail.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<bits/stdc++.h>
using namespace std; 

#define F first
#define S second
#define ull unsigned long long
#define ll long long
#define ul unsigned long
#define db double
#define pb push_back
#define mp make_pair
#define el "\n"
#define vll vector <ll>
#define vl vector <long int>
#define vi vector <int>
#define all(v) (v.begin(), v.end()) 
#define rall(v) (v.rbegin(), v.rend()) 
const ll mod = 1e9+7;

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

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
     #endif
    /***************-Code starts from here-***************/
    string s,t;
    cin>>s>>t;
    int count=0;
    for(int i=0; i<s.size(); i++)
    {
        if(s[i]!=t[i])
        count++;
    }
    cout<<count;
    return 0;
} 

Measured memory_bytes: 3756
#include <stdio.h>
#include <iostream>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <limits.h>
#include <map>
#include <string>
#include <bitset>
#include <vector>
using namespace std;
int main()
{
    long long a;
    string b;
    cin>>a>>b;
    long long c=(b[0]-'0')*100+(b[2]-'0')*10+b[3]-'0';
    printf("%lld",a*c/100);
    return 0;
}


Measured memory_bytes: 3724
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

long long int greatest_common_devisor(long long int a, long long int b) {
	long long int r;
	if(a < b) {
		r = b;
		b = a;
		a = r;
	}
	r = a % b;
	if(r != 0) {
		b = greatest_common_devisor(b, r);
	}
	return b;
}

long long int least_common_multiple(long long int a, long long int b) {
	long long int gcm;
	gcm = greatest_common_devisor(a, b);
	if(a > b) {
		a /= gcm;
	} else {
		b /= gcm;
	}
	return a * b;
}

int main(void) {
	int N;
	long long int *T;
	long long int lcm;
	int i;
	char buf[128], *ch;

	fgets(buf, sizeof(buf), stdin);
	ch = strtok(buf, " ");
	N = atoi(ch);

	T = (long long int *)calloc(N, sizeof(long long int));
	
	for(i = 0; i < N; i += 1) {
		fgets(buf, sizeof(buf), stdin);
		ch = strtok(buf, " ");
		T[i] = atoll(ch);
	}

	lcm = T[0];
	for(i = 1; i < N; i += 1) {
		lcm = least_common_multiple(T[i], lcm);
	}
	printf("%lld\n", lcm);

	free(T);
	return 0;
}



Measured memory_bytes: 2100
#include <stdio.h>
#include <stdlib.h>

int compare( const int *v1, const int *v2 ){
	return *v1 - *v2;
}

int main(){
	
	int n, i;
	int a[100000];
	
	scanf( "%d", &n );
	for( i = 0; i < n; i++ ){
		scanf( "%d", &(a[i]) );
	}
	qsort( a, n, sizeof(int), 
		(int (*)(const void *, const void *))compare );

	long long int edge1 = -1;
	long long int edge2 = -1;
	i = n-1;
	while( i > 0 ){
		if( a[i] == a[i-1] ){
			if( edge1 == -1 ){
				edge1 = (long long int)a[i];
				i = i - 2;
				
			}else{
				edge2 = (long long int)a[i];
				i = 0;
			}
		}else{
			i = i - 1;
		}
	}
	
	if( edge2 == -1 ){
		printf("0");
		
	}else{
		printf("%lld", edge1 * edge2 );
		
	}
	
	return 0;
	
}

Measured memory_bytes: 892
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <array>
#include <vector>
#include <functional>
#include <unordered_map> 
#include <map> 
#include <numeric>
#include <limits>
#include <utility>
#include <queue>
#include <random>
#include <bitset>
#include <memory>
#include <complex>
using namespace std;
typedef long long LL;

int main() {
    int n;
    cin >> n;
    vector<string> s(n);
    vector<vector<LL>> num(n, vector<LL>(26,0));
    for (int i = 0; i < n; i++) {
        string h;
        cin >> h;
        s[i] = h;
        for (int j = 0; j < s[i].size(); j++) {
            num[i][h[j] - 'a']++;
        }
    }

    for (int i = 0; i < 26; i++) {
        LL mi = 1000000;
        for (int j = 0; j < n; j++) {
            mi = min(mi, num[j][i]);
        }
        for (int k = 0; k < mi; k++) {
            cout << (char)('a' + i);
        }
    }
    cout << endl;



    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

signed main(){
	cout<<fixed<<setprecision(10);
	
	string S;
	deque<char> s;
	int Q;
	int now = 0;
	
	cin>>S;
	cin>>Q;
	
	for(int i = 0; i < S.size(); i++){
		s.push_back(S[i]);
	}
	
	for(int i = 0; i < Q; i++){
		int T;
		
		cin>>T;
		
		if(T == 1){
			now = !now;
		} else {
			int F;
			char C;
			
			cin>>F>>C;
			
			if(F == 1) {
				if(now == 0) {
					s.push_front(C);
				} else {
					s.push_back(C);
				}
			} else {
				if(now == 1) {
					s.push_front(C);
				} else {
					s.push_back(C);
				}
			}
		}
		
		// cout<<s.size()<<endl;
	}
	
	// for(int i = 0; i < s.size(); i++){
			// cout<<s[i];
	// }
	// cout<<endl;
	
	if(now == 0) {
		for(int i = 0; i < s.size(); i++){
			cout<<s[i];
		}
		cout<<endl;
		
	} else {
		for(int i = 0; i < s.size(); i++){
			cout<<s[s.size()-i-1];
		}
		cout<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 976
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <queue>

using namespace std;
#define rep(i, N) for(int (i) = 0; (i) < (N); (i) ++)
typedef long long ll;

int main(){
    int N;
    cin >> N;
    ll m = 1e9 + 7;
    vector<ll> ts(N);
    vector<ll> as(N);
    vector<ll> us(N);
    vector<ll> ms(N, -1);
    int prev  = -1;
    rep(i, N){
        cin >> ts[i];
        if(ts[i] > prev){
            ms[i] = ts[i];
        }
        us[i] = ts[i];
        prev = us[i];
    }

    rep(i, N){
        cin >> as[i];
    }

    prev = -1;
    for(int i = N - 1; i >= 0; i --){
        if(as[i] > prev){
            if(ms[i] == -1){
                ms[i] = as[i];
            }else if(ms[i] != as[i]){
                cout << 0 << endl;
                return 0;
            }
        }
        us[i] = min(us[i], as[i]);
        if(us[i] < ms[i]){
            cout << 0 << endl;
            return 0;
        }
        prev = as[i];
    }


    ll ans = 1;
    rep(i, N){
        if(ms[i] == -1){
            ans = ans * us[i] % m;
        }

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


Measured memory_bytes: 3328
#include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) {for(auto x:obj)cout<<x<<' ';cout<<endl;}
#define line "----------"
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> LP;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	int N;
	cin >> N;
	vector<int> A(N);
	rep(i,N)cin >> A[i];
	multiset<int> ans;
	rep(i,N){
		auto it = ans.lower_bound(A[i]);
		if(it != ans.begin()){
			ans.erase(--it);
		}
		ans.insert(A[i]);
	}
	cout << ans.size() << endl;	
	return 0;
}


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

using namespace std;
using ll = long long int;

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

    ll n, a, b;
    cin >> n >> a >> b;
    string s;
    cin >> s;
    ll sum = a+b;
    for(auto &c: s) {
        bool pass = false;
        switch(c) {
            case 'a':
                if(sum) sum--, pass = true;
                break;
            case 'b':
                if(sum && b) sum--, b--, pass = true;
                break;
        }
        cout << (pass ? "Yes" : "No") << endl;
    }
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ll long long
long long INF = 1e18;
const int MOD = 1e9 + 7;

using namespace std;

pair<int,int> vp[300*300+1];
ll vl[300*300+1],answer[100000];

int main(){
    int h,w,d,q;
    scanf("%d %d %d",&h,&w,&d);
    FOR(i,1,h+1){
        FOR(j,1,w+1){
            int a;
            scanf("%d",&a);
            vp[a] = make_pair(i,j);
        }
    }

    scanf("%d",&q);
    FOR(i,1,d+1) vl[i] = 0;
    FOR(i,d+1,h*w+1) vl[i] = vl[i-d] + abs(vp[i].first-vp[i-d].first) + abs(vp[i].second-vp[i-d].second);

    REP(i,q){
        int l,r;
        scanf("%d %d",&l,&r);
        answer[i] = vl[r] - vl[l];
    }
    REP(i,q) printf("%lld\n",answer[i]);
    return 0;
}


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

int main(){
    int n;cin>>n;
    vector<int> l(n);
    for(int i=0;i<n;i++){
        cin>>l[i];
    }
    sort(l.begin(),l.end());
    int ans=0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            for(int k=j+1;k<n;k++){
                if((l[i]!=l[j])&&(l[j]!=l[k])&&(l[i]+l[j]>l[k])){
                    ans++;
                }
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 3600
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define ll long long
int main(){
    int h,w;
    cin >> h >> w;
    int i;
    string s;
    for(i=0;i<w+2;i++){
      cout<<'#';
    }
    cout << endl;
    for(i=0;i<h;i++){
      cin >> s;
      cout <<'#'<< s <<'#'<<endl;
    }
    for(i=0;i<w+2;i++){
      cout<<'#';
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using static Input;
public class Hello
{
    public static void Main()
    {
        int N = NextInt();
        int M = NextInt();
        UnionFind UF = new UnionFind(N);
        for (int i = 0; i < M; i++)
        {
            int X = NextInt() - 1;
            int Y = NextInt() - 1;
            int Z = NextInt();
            UF.Unite(X, Y);
        }
        HashSet<int> ans = new HashSet<int>();
        for (int i = 0; i < N; i++)
            if (!ans.Contains(UF.Root(i))) ans.Add(UF.Root(i));
        Console.WriteLine(ans.Count());
    }
}

public class UnionFind
{
    public List<int> parent = new List<int>();

    public UnionFind(int N)
    {
        for (int i = 0; i < N; i++) parent.Add(i);
    }

    public int Root(int a)
    {
        if (parent[a] == a) return a;
        else return parent[a] = Root(parent[a]);
    }

    public void Unite(int a, int b)
    {
        a = Root(a);
        b = Root(b);
        if (a == b) return;
        parent[a] = parent[b];
    }

    public bool Same(int a, int b) { return (Root(a) == Root(b)); }
}


public class Input
{
    private static Queue<string> queue = new Queue<string>();
    private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); }
    public static string NextString() { Confirm(); return queue.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}


Measured memory_bytes: 20544
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <functional>
#include <string>
#define SIZE 100005

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

bool dp[SIZE];
char str[SIZE];
string memo[4]={"dream","dreamer","erase","eraser"};

int main()
{
	scanf("%s",&str);
	int n=strlen(str);
	dp[0]=true;
	for(int i=0;i<n;i++)
	{
		if(!dp[i]) continue;
		for(int j=0;j<4;j++)
		{
			string s=memo[j];
			if(i+s.size()<=n)
			{
				string now="";
				for(int j=0;j<s.size();j++) now+=str[i+j];
				if(now==s)
				{
					dp[i+s.size()]=true;
				}
			}
		}
	}
	if(dp[n]) puts("YES");
	else puts("NO");
	return 0;
}


Measured memory_bytes: 636
/**Bismillahir Rahmanir Rahim.**/
/*
         Md.Fagun Molla
         18ICTCSE006
         BSMRSTU(SHIICT)
*/
#include <bits/stdc++.h>
using namespace std;
#define  gcd(a,b)         __gcd(a,b)
#define  lcm(a,b)         (a*b)/gcd(a,b)
#define  ll               long long
#define  vi               std::vector<int>
#define  pb               push_back
#define  MAX              100007
#define  End              return 0;
#define  pi               acos(-1)

int main () {
    ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
    int aa[6];
  	int ans;
  	for(int i=1; i<=5; i++) {
      cin>>aa[i];
      if(aa[i]==0)
        ans=i;
    }
  	cout<<ans<<endl;
  	
    End
}
/* pray for me
Do before die
*/



Measured memory_bytes: 3592
#include<iostream>
#include<utility>
#include<algorithm>

using namespace std;

int N, M;
 pair<int, int> PD[10010];
 int main() {
	 while (cin >> N >> M && N)
	 {			
			 int d, p;
			 for (int i = 0; i < N; ++i)
			 {
				 cin >> d >> p;
				 PD[i] = make_pair(p, d);
			 }
			 
			 sort(PD, PD + N);
			 reverse(PD, PD + N);
		 int S = 0;
		 for (int i = 0; i < N; ++i)
			 S += PD[i].first * PD[i].second;
			 for (int i = 0; i < N; ++i) {
				 if (M <= 0) break;
				 int guarded = min(M,PD[i].second);
				 S -= PD[i].first * guarded;
				 M -= guarded;				 
			 }
			 cout << S << endl;
	}
	
}

	




Measured memory_bytes: 3092
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,k,l[50]={},ans=0;
  cin>>n>>k;
  for(int i=0;i<n;i++)cin>>l[i];
  for(int i=0;i<n;i++)
    for(int j=i;j<n;j++)
      if(l[i]<l[j])swap(l[i],l[j]);
  for(int i=0;i<k;i++)ans+=l[i];
  cout<<ans<<endl;
  
  
  return 0;
}


Measured memory_bytes: 256
N = int(input())
A = list(map(int,input().split()))
flag = 0
for i in range(N):
    if A[i]%2==0:
        if (A[i]%3!=0 and A[i]%5!=0):
            flag = 1
if flag==1:
    print('DENIED')
else:
    print('APPROVED')

Measured memory_bytes: 38384
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int n,a,p=100,c=0;
cin>>n;
while(1)
{
a=p+p/100;
p=a;
c++;
if(a>=n)
break;
}
cout<<c<<endl;
	return 0;
}


Measured memory_bytes: 3596
#include<bits/stdc++.h>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#define endl  "\n"
#define ll long long
#define int long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cin.exceptions(cin.failbit);
using namespace std;
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (int i = 5; i * i <= n; i += 6)
    {
        if ((n % i == 0) || (n % (i + 2) == 0))
            return false;
    }
    return true;
}
int32_t main()
{
	string s;
	cin >> s;
	if(s[2]==s[3] && s[4]==s[5])
	cout << "Yes";
	else
	cout << "No";
}


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

int main(){
    int x,y,z;
    cin >> x >> y >> z;
    swap(x,y);
    swap(x,z);
    cout << x << " " << y << " " << z;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define inc(x,y) {x+=(y);if(x>=mod)x-=mod;}
#define dec(x,y) {x-=(y);if(x<0)x+=mod;}
#define spln(i,n) (i==n?'\n':' ')
using namespace std;
long long ans;
int n,m,i,j,sz[200005],d[200005],f[200005],g[200005];
vector<int> bi[200005];
string col;
void dfs1(int x,int fa)
{
	if(col[x]=='1'){
		sz[x]=1;d[x]=0;
	}
	else{
		d[x]=0x12121212;
	}
	ff(bi[x],it){
		if(*it==fa)continue;
		dfs1(*it,x);
		sz[x]+=sz[*it];if(sz[*it])d[x]=min(d[x],f[*it]+1);
		if(f[*it]+1>g[x]){
			g[x]=f[*it]+1;
		}
		if(g[x]>f[x]){
			swap(f[x],g[x]);
		}
	}
}
void dfs2(int x,int fa)
{
	int lim=min(f[x]-1,g[x]+1);
	ans+=max(0,lim-d[x]+1);
	ff(bi[x],it){
		if(*it==fa)continue;
		int tmp=(f[x]==f[*it]+1?g[x]:f[x])+1;
		if(sz[1]!=sz[*it])d[*it]=min(d[*it],tmp);
		if(tmp>g[*it]){
			g[*it]=tmp;
		}
		if(g[*it]>f[*it]){
			swap(f[*it],g[*it]);
		}
		dfs2(*it,x);
	}
}
int main()
{
	scanf("%d",&n);
	fz1(i,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		bi[x].push_back(y);
		bi[y].push_back(x);
	}
	rdst(col,200005);col=" "+col;
	dfs1(1,0);dfs2(1,0);
	cout<<ans+1<<endl;
	return 0;
}

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

using namespace std;

using ll = long long;

int main()
{
    ll t, n, k, sum = 0, i, cnt = 0;
    string s, s1;
    cin >> s >> s1;
    for(i=0; i<=2; i++){
        if(s[i] == s1[i])
            cnt++;
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    before = list()
    after = list()

    # 操作1を行う必要があるのは，
    # 操作をする前と条件を満たすよう操作した後で
    # 各要素の位置の偶奇が不一致のときではないか?
    for index, ai in enumerate(a, 1):
        before.append((ai, index % 2))

    for index, ai in enumerate(sorted(a), 1):
        after.append((ai, index % 2))

    count = 0

    for b, a in zip(sorted(before), after):
        if b[1] != a[1]:
            count += 1

    # 1回の操作で，2個の位置を変えられる
    print(count // 2)


if __name__ == '__main__':
    main()


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

using namespace std;

typedef long long ll;
typedef vector<int> pi;
typedef pair<int,int> vi;

#define F first
#define S second
#define PB push_back
#define MK make_pair
#define REP(i, a, b) for (int i=a; i < b; i++)


bool sort_by(vi c, vi d){
	return c.S < d.S;
}

int main() {
	int a, b, c, d, e;
	cin >> a >> b >> c >> d >> e;
	int dishes[5] = {a, b, c, d, e}, bigi=0, max_n=0, r;
	REP(i,0, 5){
		r = dishes[i]%10;
		r = (r == 0? r:10-r);
		bigi =(r > max_n?i:bigi);
		max_n = (r > max_n?r:max_n);
	}
	int res = 0;
	REP(i, 0, 5){
		if(i != bigi) res += (dishes[i]/10 + (dishes[i]%10==0?0:1))*10;
	}
	cout << res + dishes[bigi];

}


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

int n;
string s;
int ans = 0;

int main() {
    cin >> n >> s;
    for (int i = 1; i < n; i++) {
        int cnt = 0;
        bool f[30] = {};
        for (int j = 0; j < i; j++) {
            int x = s[j] - 'a';
            f[x] = true;
        }
        for (int j = i; j < n; j++) {
            int x = s[j] - 'a';
            if (f[x]) {
                cnt++;
                f[x] = false;
            }
        }
        ans = max(ans, cnt);
    }

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

Measured memory_bytes: 256
#include <math.h>

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;

int main() {
  int s, w;
  cin >> s >> w;

  if (s <= w)
    cout << "unsafe";
  else
    cout << "safe";
  return 0;
}


Measured memory_bytes: 3620
// D - Integer Cards
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rp(i,n) for(int i=0;i<(n);i++)
using PAIR = pair<ll,int>;
PAIR pair_in(){PAIR p;cin>>p.second>>p.first;return p;}

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

	vector<ll> A(N);
	rp(i,N) cin>>A[i];
	sort(A.begin(), A.end());

	vector<PAIR> B;
	rp(i,M) B.push_back(pair_in());
	sort(B.begin(), B.end());

	vector<ll> C;
	int n=N-1;
	ll value; int count;
	for(int m=M-1; m>=0 && C.size()<N; --m){
		tie(value, count) = B[m];

		while(C.size()<N && n>=0 && value <= A[n]) C.push_back(A[n--]);
		if(n<0 || C.size()>=N) break;

		while(C.size()<N && n>=0 && count-->0) C.push_back(value);
		if(n<0 || C.size()>=N) break;
	}

	int e = N-C.size();
	if(C.size()<N) while(n && C.size()<N) C.push_back(A[n--]);

	ll ans = 0;
	rp(i,C.size()) ans += C[i];
	cout<< ans <<endl;
}

Measured memory_bytes: 4464
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(),v.end()
#define INF INT_MAX;
typedef long long ll;
using namespace std;

int Digits(ll x){
  int ans=0;
  while(x!=0){
    x/=10;
    ans++;
  }
  return ans;
}

int Fanction(ll a,ll b){
  int ans = max(Digits(a),Digits(b));
  return ans;
}

int main(){
  ll N;cin >> N;
  ll m = sqrtl(N);
  for(ll i = m;i>0;i--){
    if(N%i==0){
      int ans = Fanction(i,N/i);
      cout << ans << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 256
import java.util.*;

class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        while(true){
            double x=sc.nextDouble();
            double h=sc.nextDouble();
            if(x==0&&h==0){
                break;
            }
            double sum=x*x;
            double hh=Math.sqrt((x/2)*(x/2)+h*h);
            sum+=(x*hh/2)*4;
            System.out.println(sum);
        }
    }
}


Measured memory_bytes: 27956
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define fi first
#define se second 
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define pp pair<ll,ll> 
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
const ll N = 1e5 + 5 ;
ll t = 1 ;
ll dp[1024][1024*8 + 55] ;
void solve() 
{
    ll n , m , cur , x , b ;
    cin >> n >> m ;
    memset(dp,-1,sizeof(dp));
    vector<ll> cost(m+5) , v(m+5) ;
    rep( i , 0 , m ){
        cin >> cost[i+1] >> b ;
        cur = 0 ;
        rep( j , 0 , b ){
            cin >> x ;
            cur |= (1<<(x-1)) ;
        }
        v[i+1]=cur;
    }
    rep( i , 1 , m + 1 ){
        rep( j , 0 , 1<<n ){
            if( dp[i-1][j] != -1 ){
                dp[i][j|(v[i])] == -1 ? dp[i][j|v[i]]=dp[i-1][j]+cost[i] : dp[i][j|v[i]] = min(dp[i][j|v[i]],dp[i-1][j]+cost[i]);
                dp[i][j] == -1 ? dp[i][j] = dp[i-1][j] : dp[i][j] = min(dp[i][j],dp[i-1][j]);
            }
        }
        dp[i][v[i]]==-1?dp[i][v[i]]=cost[i]:dp[i][v[i]]=min(dp[i][v[i]],cost[i]);
        
    } 
    cout << dp[m][(1<<n)-1] << "\n" ;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//    cin >> t ;
    while(t--){
        solve();
    }
    return 0 ;
}
/*
2 3
2 2
1 2
4 2
1 2
5 2
1 2
*/


Measured memory_bytes: 66304
/*********************
*  Author: xuziyuan  * 
*********************/

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define LL long long
#define pii pair <LL,LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL n,m,l,q,x,y,z,s,t,dist[310][310],dist2[310][310];

int main()
{
	cin>>n>>m>>l;
	rep(i,305) rep(j,305) dist[i][j]=dist2[i][j]=1e18;
	rep(i,m)
	{
		cin>>x>>y>>z;
		dist[x][y]=dist[y][x]=z;
	}
	repn(k,n) repn(i,n) repn(j,n) dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
	repn(i,n) repn(j,n) if(dist[i][j]<=l) dist2[i][j]=1;
	repn(k,n) repn(i,n) repn(j,n) dist2[i][j]=min(dist2[i][j],dist2[i][k]+dist2[k][j]);
	cin>>q;
	rep(qn,q)
	{
		cin>>s>>t;
		if(dist2[s][t]==1e18) puts("-1");
		else cout<<dist2[s][t]-1<<endl;
	}
	return 0;
}

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

int main() {
	ll K, A, B;
	cin >> K >> A >> B;
	ll ans = 0;
	if (A+1 < B) {
		if ((K - (A - 1)) % 2 == 0) {
			ans = ((K - (A - 1)) / 2)*(B - A) + A;
		}
		else {
			ans = ((K - (A - 1)) / 2)*(B - A) + A + 1;
		}
	}
	else {
		ans = K+1;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
def comb(a,b):
    if b == 0:
        return 1
    elif a < b:
        return 0
    else:
        return comb(a-1,b-1) * a / b

n,p = map(int, raw_input().split())
l = map(int, raw_input().split())
en = len(filter(lambda x: x % 2 == 0, l))
on = n - en
if p == 1:
    ct = 0
    for i in range(1,on+1,2):
        ct += comb(on, i)
else:
    ct = 0
    for i in range(0,on+1,2):
        ct += comb(on,i)
print ct * (2**en)


Measured memory_bytes: 2568
#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
class UnionFind{
    public:
        ll size;
        ll *rank;
        ll *parent;

        UnionFind(ll N){
            this->size = N;
            rank = new ll[size];
            parent = new ll[size];
            for(ll i = 0; i < size; ++i){
                rank[i] = 0;
                parent[i] = i;
            }
        }

        ~UnionFind(){
        }

        ll find(ll child){
            return (child == parent[child] ? child : parent[child] = find(parent[child]));
        }

        void unit(ll x, ll y){
            x = find(x); y = find(y);
            if(x != y){
                if(rank[x] > rank[y]){
                    parent[y] = x;
                }else{
                    parent[x] = y;
                    if(rank[x] == rank[y]){
                        ++rank[y];
                    }
                }
            }
        }

        bool same(ll x, ll y){
            return (find(x) == find(y));
        }

};
int main(){
    ll N, M;
    cin >> N >> M;
    vector<ll> P;
    for(ll i = 0; i < N; i++){
        ll a;
        cin >> a;
        P.push_back(a - 1);
    }
    UnionFind U(N);
    for(ll i = 0; i < M; i++){
        ll x, y;
        cin >> x >> y;
        U.unit(x - 1, y - 1);
    }
    ll ans = 0;
    for(ll i = 0; i < N; i++) ans += U.same(i, P[i]);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 2676
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<iomanip>
#include<set>
#include<queue>

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	for(int i=0;i<n;i++)
	{
		cin >> a[i];
		b[i]=a[i];
	}
	sort(b.begin(),b.end());
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]!=b[i])cnt++;
	}

	cout << (cnt>2 ? "NO":"YES") << endl;




}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    double c , d, f;
    d = a;
    f = b;
    c = d / f; 
   cout << a / b << " "<<  a % b << " " 
   << fixed << setprecision(6) << c<< endl; 
}


Measured memory_bytes: 3224
#include<iostream>
#include<set>
#include<algorithm>
#include<numeric>
#include<iterator>

using namespace std;

int main(){
  for(int wd[2];cin>>wd[0]>>wd[1],wd[0];){
    multiset<int> ms[2];
    for(int i=0;i<2;i++){
      for(int j=0;j<wd[i];j++){
	int h;
	cin>>h;
	ms[i].insert(h);
      }
    }
    multiset<int> ans;
    set_union(begin(ms[0]),end(ms[0]),begin(ms[1]),end(ms[1]),inserter(ans,ans.begin()));
    cout<<accumulate(begin(ans),end(ans),0)<<endl;
  }
}

    

Measured memory_bytes: 1196
#include<iostream>
#include<iomanip>
using namespace std;
int main(void){
    int a, b;
    cin >> a >> b;
    cout << fixed;
    cout << setprecision(5);
    cout << a / b << ' ' << a % b << ' ' << double(a) / double(b) << endl;
    return 0;
}

Measured memory_bytes: 3268
#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 all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> P;
vi G[100];
int ord[100],low[100],sid[100];
bool inS[100];
stack<int> S;
int cnt;
vector<vi> scc;
int N,M,T;
void vis(int v){
	ord[v]=low[v]=++cnt;
	S.push(v);
	inS[v]=1;
	for(int u:G[v]){
		if(ord[u]==0){
			vis(u);
			chmin(low[v],low[u]);
		}else if(inS[u]){
			chmin(low[v],ord[u]);
		}
	}
	if(low[v]==ord[v]){
		vi vc;
		while(true){
			int w=S.top();S.pop();
			inS[w]=0;
			vc.pb(w);
			sid[w]=scc.size();
			if(v==w) break;
		}
		scc.pb(vc);
	}
}
void makescc(){
	rep(i,N) if(ord[i]==0) vis(i);
}
int p[100],t[100],k[100];
int deq[10001],deqv[10001];
P es[1000];
vector<int> nG[100];		//y in nG[x] : x<-y
int dp[100][10001];
void init(){
	rep(i,100) G[i].clear(),nG[i].clear(),ord[i]=0,low[i]=0,inS[i]=0;
	rep(i,100) rep(j,10001) dp[i][j]=0;
	cnt=0;
	scc.clear();
}
int main(){
	while(true){
		cin>>N>>M>>T;
		if(N==0) break;
		init();
		rep(i,N) cin>>p[i]>>t[i]>>k[i];
		rep(i,M){
			int a,b;
			cin>>a>>b;
			a--,b--;
			G[a].pb(b);
			es[i]=P(a,b);
		}
		makescc();
		int K=scc.size();
		{
			bool ise[100][100]={};
			rep(i,M) ise[ sid[es[i].fs] ][ sid[es[i].sc] ]=1;
			rep(i,K) rep(j,K) if(ise[i][j]) nG[j].pb(i);
		}
		for(int i=K-1;i>=0;i--){
			bool isol=1;
			for(int j:nG[i]){
				if(j==i){
					isol=0;
					continue;
				}
				rep(t,T+1) chmax(dp[i][t],dp[j][t]);
			}
			if(isol){
				k[scc[i][0]]=1;
			}
			for(int v:scc[i]){
				rep(a,t[v]){
					int be=0,en=0;
					for(int j=0;j*t[v]+a<=T;j++){
						int val=dp[i][j*t[v]+a]-j*p[v];
						while(be<en&&deqv[en-1]<=val) en--;
						deq[en]=j,deqv[en]=val;
						en++;
						dp[i][j*t[v]+a]=deqv[be]+j*p[v];
						if(deq[be]==j-k[v]) be++;
					}
				}
			}
		}
		int ans=0;
		rep(i,K) rep(t,T+1) chmax(ans,dp[i][t]);
		cout<<ans<<endl;
	}
}

Measured memory_bytes: 5280
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}



signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	// int ra[9] = {400,};
	int con[10] = {};
	int mi = 0, ma = 0;
	int N;
	
	cin>>N;
	
	for(int i = 0; i < N; i++){
		int a; 
		cin>>a;
		
		if(a < 3200)con[a/400]++;
		else con[8]++;
	}
	
	for(int i = 0; i < 8; i++){
		if(con[i]){
			mi++;
			ma++;
		}
	}
	
	if(mi == 0){
		mi = 1;
		ma = con[8];
	} else {
		ma += con[8];
	}
	
	cout<<mi<<" "<<ma<<endl;
	
	return 0;
}

Measured memory_bytes: 256
from collections import defaultdict
if __name__ == '__main__':
    N = int(input())
    xyh = [None]*N
    for i in range(N):
        xyh[i] = list(map(int, input().split()))
    
    H = 0
    for i in range(N):
        if xyh[i][2] >= H:
            X = xyh[i][0]
            Y = xyh[i][1]
            H = xyh[i][2]
            I = i
    
    flag = True
    for i in range(0, N):
        if xyh[i][2] != max(H - abs(xyh[i][0] - X) - abs(xyh[i][1] - Y), 0):
            flag = False
            break
    
        
    if not flag:
        for x in range(0, 101):
            for y in range(0, 101):
                flag = True
                height = 0
                for i in range(0, N):
                    if xyh[i][2] != 0:
                        if height == 0:
                            height = xyh[i][2] + abs(xyh[i][0] - x) + abs(xyh[i][1] - y)
                        if height != xyh[i][2] + abs(xyh[i][0] - x) + abs(xyh[i][1] - y):
                            flag = False
                            break
                    else:
                        if abs(xyh[i][0] - x) + abs(xyh[i][1] - y) < height:
                            flag = False
                            break
                if flag and height:
                    X = x
                    Y = y
    
    H = xyh[I][2] + abs(xyh[I][0] - X) + abs(xyh[I][1] - Y)
     
    print(X, Y, H)

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

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

class UnionFind
{
    std::vector<int> data;
    int root_count;

  public:
    UnionFind(int size) : data(size, -1), root_count(size) {}
    bool unite(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x != y)
        {
            if (data[y] < data[x])
                std::swap(x, y);
            data[x] += data[y];
            data[y] = x;
            root_count--;
        }
        return x != y;
    }
    bool find(int x, int y)
    {
        return root(x) == root(y);
    }
    int root(int x)
    {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x)
    {
        return -data[root(x)];
    }
    int count() const {
        return root_count;
    }
};


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

    ll n, m, q;
    cin >> n >> m >> q;
    set<vector<int>> ones, zeros;
    UnionFind uf(n);
    vector<int> a(q), b(q), c(q);
    int one = 0, zero = 0;
    for (int i = 0; i < q; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        if (c[i] == 0)
        {
            if (uf.find(a[i], b[i]) == false)
            {
                zero++;
                zeros.emplace(vector<int>{a[i], b[i], c[i]});
                uf.unite(a[i], b[i]);
            }
        }
        else
        {
            one++;
            ones.emplace(vector<int>{a[i], b[i], c[i]});
        }
    }
    if (m == n - 1 && one)
    {
        cout << "No" << endl;
        return 0;
    }
    for (int i = 0; i < q; i++)
    {
        if (c[i] == 0)
            continue;
        if (uf.find(a[i], b[i]))
        {
            cout << "No" << endl;
            return 0;
        }
    }
    ll sz = uf.count(), ma = sz * (sz - 1) / 2 + (int)zeros.size();
    if (sz <= 2 && one)
    {
        cout << "No" << endl;
        return 0;
    }
    if (m > ma)
    {
        cout << "No" << endl;
        return 0;
    }
    cout << "Yes" << endl;
    return 0;
}


Measured memory_bytes: 11264
#include "bits/stdc++.h"
using namespace std;
int main(){
    int N,i,x=0,max=0;
    string S;
    cin >> N >> S;
    for(i=0;i<N;i++){
        if(S.at(i)=='I') x++;
        else x--;
        if(x>max) max = x;
    }
    cout << max << endl;
    return 0;
}

Measured memory_bytes: 256