akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
#include <climits>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <tuple>
 
using namespace std;
 
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define cina(s, n, a) for(int i = s; i < n; i++) cin >> a[i]
#define couta(n, a) for(int i = 0; i < n; i++) cout << a[i] << ' '
#define copy(s, n, a, b) for(int i = s; i < n; i++) b[i] = a[i]
#define yesno(ok) cout << (ok ? "YES" : "NO")
#define mod(a, b) (b + (a % b)) % b
#define MOD (ll)1e9 + 7
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
void solve(){
    int n, k;
    cin >> n >> k;
    vi c(n + 1);
    cina(1, n + 1, c);
    vi dp(n + 1, 0);
    c[0] = INT_MAX;
    for(int i = 2; i <= n; i++){
        dp[i] = INT_MAX;
        for(int j = 1; j <= k; j++){
            if(i - j > 0){
                dp[i] = min(dp[i - j] + abs(c[i - j] - c[i]), dp[i]);
            }
        }
    }
    cout << dp[n];
}
 
int main(){
    ios::sync_with_stdio(false);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int q = 1;
    //cin >> q;
    for(int i = 1; i <= q; i++){
        solve();
    }
}


Measured memory_bytes: 1152
import java.util.Arrays

fun main(args: Array<String>) {
    val list = readLine()!!.split(" ").map(String::toInt)
    val N = list[0]
    val M = list[1]
    val heights = readLine()!!.split(" ").map(String::toInt)
    val goodT = Array<Boolean>(N, {true})

    repeat(M){
        val path = readLine()!!.split(" ").map(String::toInt)
        if (heights.get(path[0]-1) > heights.get(path[1]-1)){
            goodT[path[1]-1] = false
        }
        else if (heights.get(path[0]-1) == heights.get(path[1]-1)){
            goodT[path[1]-1] = false
            goodT[path[0]-1] = false
        }
        else {
            goodT[path[0]-1] = false
        }
    }
    println(Arrays.asList(*goodT).filter{it == true}.size)
}

Measured memory_bytes: 65204
package main

import (
	"fmt"
)

type Data struct {
	a int
	b int
	op string
}

func main() {
	var s[] Data
	var d Data
	
	for {
		fmt.Scan(&d.a, &d.op, &d.b)
		s = append(s, d)

		if d.op == "?" {
			s = s[:len(s) - 1]
			break;
		}
	}

	for _, v := range s {
		switch v.op {
		case "+":
			fmt.Println(v.a + v.b)
		case "-":
			fmt.Println(v.a - v.b)
		case "*":
			fmt.Println(v.a * v.b)
		case "/":
			fmt.Println(v.a / v.b)
		default:
			break;
		}

	}
}


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

typedef long long ll;

int gcd_three(int a, int b, int c) {
    int temp = __gcd(a, b);
    return __gcd(temp, c);
}

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

    int n;
    cin >> n;

    int total = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j ++) {
            for (int k = 1; k <= n; k++) {
                total += gcd_three(i, j, k);
            }
        }
    }
    cout << total << "\n";
}



Measured memory_bytes: 3652
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

INF = float('inf')

def solve():
    n = II()
    S = list(input())
    # print(S)
    n = len(S)

    R = []
    G = []
    B = []
    rcnt = 0
    gcnt = 0
    bcnt = 0
    for idx, s in enumerate(S):
        if s == 'R':
            R.append(idx+1)
            rcnt += 1
        elif s == 'G':
            G.append(idx+1)
            gcnt += 1
        else:
            B.append(idx+1)
            bcnt += 1
    # print(R)
    # print(G)
    # print(B)
    ans = rcnt * gcnt * bcnt
    # print(ans)

    for i in range(n):
        for j in range(i+1, n):
            k = 2 * j - i
            if k >= n:
                continue
            # print(i, j, k)
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
    print(ans)





if __name__ == '__main__':
    solve()


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

using namespace std;

int main(){
    int N;
    cin >> N;
    vector<int> t(N+1),x(N+1),y(N+1);
    t.at(0) = x.at(0) = y.at(0) = 0;
    for(int i=1; i<=N; i++){
        cin >> t.at(i) >> x.at(i) >> y.at(i) ;
    }

    bool can = true;
    for(int i=1; i<=N; i++){
        int dt = t.at(i)-t.at(i-1);
        int dist = abs(x.at(i) - x.at(i-1)) + abs(y.at(i) - y.at(i-1));
        if(dt < dist) {
            can = false;
            break;
        }
        if(dist%2 != dt%2){
            can = false;
            break;
        }
    }
    if(can) cout << "Yes" << endl;
    else    cout << "No" << endl;
    
    
}

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

#define MAX 100

int main() {
  // 始点・終点から伸びる辺の数が奇数 かつ その他の頂点から伸びる辺の数が偶数なら一筆書き可能
  while(1) {
    int a=-1, b;
    int V = 0;
    int edge[MAX]={};
    while(cin>>a>>b&&(a+b)) {
      V = max(V, max(a, b));
      a--, b--;
      edge[a]++, edge[b]++;
    }
    
    if(a==-1) break;
    
    if( edge[0]&1 || edge[1]&1 ) {
      for( int i=2; i<V; i++ ) if( edge[i] % 2 == 1 ) goto NG;
      cout << "OK" << endl;
      goto Fin;
    }
  NG:
    cout << "NG" << endl;
  Fin:;
  }
  
  return 0;
}

Measured memory_bytes: 1152
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define pb push_back
#define sb __builtin_popcount
#define MOD (ll)1000000009
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin >> s;
    ll n = s.size();
    ll i = 0;
    ll j = (n - 3) / 2;
    ll i1 = (n + 1) / 2l;
    ll j1 = n - 1;
    while (i < j && s[i] == s[j])
    {
        i++;
        j--;
    }
    while (i1 < j1 && s[i1] == s[j1])
    {
        i1++;
        j1--;
    }
    if (i >= j && i1 >= j1)
    {
        i = 0;
        ll j = n - 1;
        while (i < j && s[i] == s[j])
        {
            i++;
            j--;
        }
        if (i >= j)
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    else
    {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
#include <bitset>
using namespace std;
#define MAX 300000
typedef long long ll;

int main(){
  int A[3],dis1,dis2,ans;
  cin>>A[0]>>A[1]>>A[2];
  sort(A,A+3);
  dis1=A[2]-A[1];
  dis2=A[2]-A[0];
  if(dis1%2==dis2%2){
    ans=(dis1+dis2)/2;
  }else{
    ans=(dis1+dis2+1)/2+1;
  }
  cout<<ans<<endl;
  
  
}


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

const long long div_num = 1000000007;

int main() {
  int n, ans = 1;
  std::cin >> n;
  int dist[100000], a[100000];
  bool isContradict = false;

  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
    dist[a[i]]++;
    // dist[i] = abs((n - 1 - i) - i);
    // std::cout << dist[i] << " ";
    if (n % 2 == 0 && a[i] % 2 != 1) {
      isContradict = true;
    }
    if (n % 2 == 1 && a[i] % 2 != 0) {
      isContradict = true;
    }
  }

  for (int i = 0; i < n; i++) {
    if (i == 0) {
      if (n % 2 == 0 && dist[i] > 0) {
        isContradict = true;
      } else if (n % 2 == 1 && dist[i] != 1) {
        isContradict = true;
      }
    } else {
      if (n % 2 == 0 && i % 2 == 1 && dist[i] != 2) {
        isContradict = true;
      } else if (n % 2 == 1 && i % 2 == 0 && dist[i] != 2) {
        isContradict = true;
      }
    }
  }

  if (!isContradict) {
    for (int i = 0; i < n / 2; i++) {
      ans *= 2;
      ans %= div_num;
    }
    std::cout << ans << "\n";
  } else {
    std::cout << 0 << "\n";
  }
  return 0;
}

Measured memory_bytes: 1024
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void put(int x){
	if(x<0) {
		putchar('-');
		x*=-1;
	}
	if(x==0) {
		putchar('0');
	}
	int num=0;
	char c[15];
	while(x) {
		c[++num]=(x%10)+48;
		x/=10;
	}
	while(num) {
		putchar(c[num--]);
	}
	putchar('\n');
}
inline void in(int &x){
	char ch=getchar();
	int flag=1;
	while(!(ch>='0'&&ch<='9')) {
		if(ch=='-')
			flag*=-1;
		ch=getchar();
	}
	x=ch-'0';
	ch=getchar();
	while(ch>='0'&&ch<='9') {
		x=((x<<3)+(x<<1))+ch-'0';
		ch=getchar();
	}
	x*=flag;
}
int a,b,t,ans;
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	in(a);in(b);in(t);
	for(int i=1;i*a<=t;i++)
	ans+=b;
	printf("%d\n",ans);
	return 0;
}


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



using namespace std;


bool isTri(int a, int b, int c) {
	
	if (a >= b + c) {
		return false;
	}
	if (b >= c + a) {
		return false;
	}
	if (c >= a + b) {
		return false;
	}
	return true;

}


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

	vector<int> L(n);
	for (int i = 0; i < n; i++) {
		cin >> L[i];
	}

	int cnt = 0;
	
	sort(L.begin(), L.end());
	for (int i = 0; i < n - 2;i++) {
		for (int j = i+1; j < n - 1; j++) {
			
			

			if (isTri(L[i], L[j], L[j + 1])) {
				if (isTri(L[i], L[j], L[n - 1])) {
					cnt += n - 1 - j;

				}
				else {

					int right = j + 1;
					int left = n - 1;
					int mid = (right + left) / 2;

					while (right < mid) {
						if (isTri(L[i], L[j], L[mid])) {
							right = mid;

						}
						else {
							left = mid;
						}

						mid = (right + left) / 2;
					}


					cnt += right - j;
				}
			}
		}
	}

	cout << cnt << endl;
}

Measured memory_bytes: 256
/*
ЗАПУСКАЕМ 
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░ 
 */
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>


using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 505;


int n;
int p[MAXN * MAXN];
int cnt[MAXN][MAXN];
int b[MAXN][MAXN];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};


int get(int i, int j) {
	return b[i][j] + min(min(cnt[i - 1][j], cnt[i + 1][j]), min(cnt[i][j - 1], cnt[i][j + 1]));
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 //	read(FILENAME);
	cin >> n;
	for (int i = 0; i < n * n; i++) {
		cin >> p[i];
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			b[i][j] = 1;
			cnt[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
		}
	}
	ll ans = 0;
	for (int it = 0; it < n * n; it++) {
		int i = (p[it] - 1) / n + 1;
		int j = (p[it] - 1) % n + 1;
		cnt[i][j]--;
		b[i][j] = 0;
		ans += cnt[i][j];
		queue<pair<int, int> > q;
		q.push(mp(i, j));
		while (!q.empty()) {
			auto x = q.front();
			q.pop();
			int s = x.first;
			int t = x.second;
			for (int k = 0; k < 4; k++) {
				int ns = s + dx[k];
				int nt = t + dy[k];
				if (ns && nt && ns <= n && nt <= n) {
					int x = get(ns, nt);
					if (x != cnt[ns][nt]) {
						cnt[ns][nt] = x;
						//cout << x << endl;
						q.push(mp(ns, nt)); 
					}
				}
			}
		}
	}	
	cout << ans << '\n';
	return 0; 	
}

Measured memory_bytes: 6616
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string A, B, C;
	cin >> A >> B >> C;
	if (A[A.size() - 1] == B[0] && B[B.size() -1] == C[0])
	{
		printf("YES\n");
	}
	else
	{
		printf("NO\n");
	}
	
	return 0;
}

Measured memory_bytes: 256
use proconio::input;

#[proconio::fastout]
fn main() {
    input! {
        n: usize,
        s: [String;n],
    }
    let mut map = std::collections::HashMap::new();
    for s in s.iter() {
        if let Some(v) = map.get_mut(&s) {
            *v += 1;
        } else {
            map.insert(s, 1);
        }
    }
    println!("{}", map.len());
}


Measured memory_bytes: 21688
import java.util.*;

public class Main {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong() - 1;
        long b = 26;
        int d = 1;
        String res = "";

        while(true) {
            if(n < b) {
                  char[] c = new char[d];
                  for(int i=0; i<d; i++) {
                      c[d-1-i] = (char)('a' + (n % 26));
                      n/=26;
                  }
                  System.out.println(new String(c));
                  return;
            }
            d++;
            n-=b;
            b*=26;
        }


    }


}

Measured memory_bytes: 36024
#include<iostream>
#include<cstdio>
#include<math.h>
#include<string.h>
using namespace std;

main()
{
	int k,l;
	string a;
	cin>>k>>a;
	l=a.length();
	if(l>k){
	for(int i=0;i<k;i++)cout<<a[i];
	cout<<"...";
	}
	else cout<<a;
	return 0;
}

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

namespace ProgramingStudying2
{
    class Program
    {

        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split().Select(int.Parse).ToArray();

            var counts = new int[200000];

            for(int i = 0; i < n; i++)
            {
                counts[a[i]]++;
                counts[a[i] + 1]++;
                if(a[i] > 0)
                {
                    counts[a[i] - 1]++;
                }
            }

            Console.WriteLine(counts.Max());
        }
    }
}


Measured memory_bytes: 36532
#include <iostream>


int main()
{
    int S;

    std::cin >> S;

    if (S <= 59) {
        std::cout << "0" << ":"<< "0"<< ":" << S <<std::endl;
    } else if (S <= 3599) {
        std::cout << "0" << ":" << (S - (S % 60)) / 60 << ":" << S % 60 << std::endl;
    } else {
        int m = (S % 3600);

        std ::cout << (S - m) / 3600 << ":" << (m - (m % 60)) / 60 << ":" << m %60 << std::endl;
    }

    return 0;

}

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

int h, w;
char f[10][10];
bool flag;

void bfs(int y, int x) {

    bool go = false;
    if (f[y][x] == '#' || f[y][x] == 'x') {
        f[y][x] = 'x';
        if (0 <= x + 1 && x + 1 < w) { bfs(y, x + 1); go = true; }
        if (0 <= y + 1 && y + 1 < h) { bfs(y + 1, x); go = true; }
    }

    if (!go) {
        bool ok = true;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                if (f[i][j] == '#') { ok = false; i = h; break; }
            }
        }

        if (ok) { flag = true; return; }
    }

    if (go) { f[y][x] = '#'; }

}

int main() {
    string y = "Possible", n = "Impossible";

    cin >> h >> w;
    rep(i, h) rep(j, w) cin >> f[i][j];


    bfs(0, 0);
    cout << (flag ? y : n) << endl;

    return 0;
}


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

import sys

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(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()

print(N**3)


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

int N;
int A[1<<20];
vector<pair<int, int> > B;

void populateB() {
    int i = 0, j = 0;
    while (j < N) {
        if(A[j] == A[i]) j++;
        else {
            B.push_back(make_pair(A[i], j-i));
            i = j;
        }
    }

    B.push_back(make_pair(A[i], j-i));    
}

int main() {
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);

    cin>>N;
    for(int i = 0;i < N;i++) cin>>A[i];

    sort(A, A+N);
    populateB();

    int counter = 0;
    for(auto &p : B) {
        counter = max(p.second, counter);
    }

    for(int i = 0;i < (int)B.size()-1;i++) {
        if(B[i+1].first-B[i].first == 2) {
            counter = max(counter, B[i].second+B[i+1].second);
        }
    }

    for(int i = 1;i < (int)B.size();i++) {
        int ans = B[i].second;
        if(B[i-1].first == B[i].first-1) ans += B[i-1].second;
        if(i+1 < (int)B.size() and B[i].first == B[i+1].first-1)
            ans += B[i+1].second;

        counter = max(counter, ans);
    }

    cout<<counter<<endl;

    return 0;
}


Measured memory_bytes: 1276
#include <iostream>

using namespace std;
static const int INF = 99999999;
int cost[101][101];
int mincost[101];
bool used[101];
int n, m;

int prim()
{
	for(int u = 0; u < 101; ++u)
	{
		mincost[u] = INF;
		used[u] = false;
	}
	mincost[0] = 0;
	int res = 0;
	while(true)
	{
		int v = -1;
		for(int u = 0; u < n; ++u)
		{
			if(!used[u] && (v == -1 || mincost[u] < mincost[v]))
			{
				v = u;
			}
		}
		if(v == -1)
		{
			break;
		}
		used[v] = true;
		res += mincost[v];
		for(int u = 0; u < n; ++u)
		{
			mincost[u] = min(mincost[u], cost[v][u]);
		}
	}
	return(res);
}

void solve()
{
	while(cin >> n >> m, n || m)
	{
		for(int i = 0; i < 101; ++i)
		{
			for(int j = 0; j < 101; ++j)
			{
				cost[i][j] = INF;
			}
		}
		for(int i = 0; i < m; ++i)
		{
			int a, b, c;
			cin >> a >> b >> c;
			cost[a][b] = c;
			cost[b][a] = c;
		}
		cout << prim() << endl;
	}
}

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

Measured memory_bytes: 1200
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
#define MOD2 998244353
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<=b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
#define trace(x) cerr<<#x<<": "<<x<<'\n';
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
#define T int
#define ordered_set tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
auto clk=clock();
 
ll mexp(ll a, ll b, ll m){
    ll ans=1;
    a%=m;
    while(b){
        if(b&1) ans=ans*a%m;
        b>>=1;
        a=a*a%m;
    }
    return ans;
}

const int N = 300005;

int pre[N], suf[N];

int main(){
    IOS
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

    int n;
    cin >> n;
    string s;
    cin >> s;
    s = "$" + s;
    FOR(i,1,n){
        pre[i] = pre[i-1] + (s[i] == 'W');
    }
    ROF(i,n,1){
        suf[i] = suf[i+1] + (s[i] == 'E');
    }
    int ans = N;
    FOR(i,1,n){
        ans = min(ans, pre[i-1] + suf[i+1]);
    }
    cout << ans;


    cerr<<endl<<endl<<"Time: "<<fixed<<setprecision(12)<<(long double)(clock()-clk)/CLOCKS_PER_SEC<<" ms"<<endl<<endl;
 
    return 0;
}

Measured memory_bytes: 3056
#include <iostream>
#include <ctype.h>
using namespace std;

int main(void)
{
	string str;

	getline(cin, str);

	int ary_size = str.length();

	for(int i=0; i<ary_size; i++){

		if(str[i] == '.' || str[i] == ' '){
			continue;
		}else{
			str[i] = toupper(str[i]);
		}
	}

	cout << str << endl;
}

Measured memory_bytes: 1176
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#define mod 1000000007
using namespace std;
const int M = 1e5 + 10;
typedef long long ll;
int a[M];
bool vis[M];
ll up[M] , down[M] , up2[M] , down2[M];
ll inv(ll a) {
    return a == 1 ? 1 : (ll)(mod - mod / a) * inv(mod % a) % mod;
}
int main() {
    int n;
    scanf("%d" , &n);
    for(int i = 1 ; i <= n + 1 ; i++) scanf("%d" , &a[i]);
    memset(vis , 0 , sizeof(vis));
    int pos1 = 0 , pos2 = 0 , gg;
    for(int i = 1 ; i <= n + 1 ; i++) {
        if(!vis[a[i]]) {
            vis[a[i]] = true;
            continue;
        }
        else {
            pos2 = i;
            gg = a[i];
            break;
        }
    }
    for(int i = 1 ; i <= n + 1 ; i++) {
        if(a[i] == gg) {
            pos1 = i;
            break;
        }
    }
    //cout << pos1 << ' ' << pos2 << endl;
    int num = n - pos2 + 1;
    int num2 = pos1 - 1;
    num += num2;
    //cout << num << ' ' << num2 << endl;
    //cout << num << endl;
    up[0] = 1 , down[0] = 1 , up2[0] = 1 , down2[0] = 1;
    n++;
    for(int i = 1 ; i <= n / 2 ; i++) up[i] = up[i - 1] * (n - i + 1) % mod , down[i] = down[i - 1] * i % mod;
    for(int i = n / 2 + 1 ; i <= n ; i++) up[i] = up[n - i] , down[i] = down[n - i];
    for(int i = 1 ; i <= num / 2 ; i++) up2[i] = up2[i - 1] * (num - i + 1) % mod , down2[i] = down2[i - 1] * i % mod;
    for(int i = num / 2 + 1 ; i <= num ; i++) up2[i] = up2[num - i] , down2[i] = down2[num - i];
//    cout << num << endl;
//    for(int i = 1 ; i <= num ; i++) {
//        cout << up2[i] << ' ' << down2[i] << endl;
//        cout << up2[i] * inv(down2[i]) << endl;
//    }
    for(int i = 1 ; i <= n ; i++) {
        ll sum = 0;
        if(i == 1) {
            printf("%lld\n" , (ll)(n - 1));
        }
        else {
            sum += up[i] * inv(down[i]) % mod;
            if(num >= i - 1 && num > 0) sum -= up2[i - 1] * inv(down2[i - 1]) % mod;
            printf("%lld\n" , (sum + mod) % mod);
        }
    }
    return 0;
}

Measured memory_bytes: 4864
import java.util.*;
import java.io.*;
import java.math.*;
class Main{
	static int mod1 = (int)1e9+7;
	public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
	public static void main(String sp[])throws IOException{
		FastReader sc = new FastReader();	
		long n = sc.nextLong();
		long count=1;
		long g=0;
		for(int i=0;i<n;i++){
			long val = sc.nextLong();
			if(val==count){
				count++;
				g++;
			}
		}
		if(g==0)
		System.out.println("-1");
		else System.out.println(n-g);
    }
	
	
	public static long gcd(long a, long b){
		if(a==0)
			return b;
		
		return gcd(b%a,a);
	}
	public static class FastReader {
		BufferedReader br;
		StringTokenizer st;
		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception r) {
					r.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());//converts string to integer
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (Exception r) {
				r.printStackTrace();
			}
			return str;
		}
	}
}

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

int main() {
  int N; cin >> N;
  vector<string> s(N);
  vector<int> p(N);
  for (int i=0; i<N; i++) cin >> s.at(i) >> p.at(i);
  vector<int> ord(N);
  iota(ord.begin(), ord.end(), 0);
  sort(ord.begin(), ord.end(), [&](int x, int y) {
    if (s.at(x) == s.at(y)) return p.at(x) > p.at(y);
    return s.at(x) < s.at(y);
  });
  for (int e : ord) cout << e+1 << endl;
}



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

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

    bool jud = true;
    rep(i,s.size()){
        if(s[i] != t[i]) jud = false;
    }

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

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

int main(){
  ll n,k; cin>>n>>k;
  vector<ll> num(n);
  for(int i=0;i<n;i++){
    cin>>num[i];
  }
  sort(num.begin(),num.end());
  ll ans=0;
  for(int i=0;i<n-k;i++){
    ans+=num[i];
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 1792
#![allow(unused_imports)]
#![allow(non_snake_case)]
use proconio::{input, fastout};
use proconio::marker::{Chars, Bytes, Usize1};

#[fastout]
fn main() {
    input!{
        n: usize, k:u32,
        c: [u32; n]
    }
    let mut ng = 0;
    let mut ok = *c.iter().max().unwrap() + 1;
    while ok - ng > 1 {
        let mid = (ok + ng) / 2;
        let mut cnt = 0;
        for &x in &c {
            cnt += (x - 1) / mid;
        }
        if cnt <= k {
            ok = mid
        } else {
            ng = mid
        }
    }
    println!("{}", ok);
}


Measured memory_bytes: 4652
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] t = new long[n];
        long sum = 0;
        for(int i = 0 ; i < n; i++) {
            t[i] = sc.nextLong();
            sum += t[i];
        }
        int m = sc.nextInt();

        for(int i = 0 ; i < m; i++) {
            int p = sc.nextInt() - 1;
            long x = sc.nextLong();
            System.out.println(sum - (t[p] - x));
        }
    }
}

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

int main(){
  int k,s,rep = 0;
  cin >> k >> s;
  rep(i,k+1) rep(j,k+1){
    int z = s - i -j;
    if(z >= 0 && z <= k) rep++;
  }
  cout << rep << endl;
}


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

using namespace std;

int main() {
	long long n, k;
	scanf("%lld %lld", &n, &k);
	long long ans;
	pair<long long, long long> arr[n];
	for (int i = 0; i < n; i++) {
		scanf("%lld %lld", &arr[i].first, &arr[i].second);
	}
	sort(arr, arr + n);
	for (int i = 0; i < n; i++) {
		k -= arr[i].second;
		if (k <= 0) {
			ans = arr[i].first;
			break;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 1792
#include <iostream>

using namespace std;

void judge(int a, int b) {
    if ((a * b) % 2 != 0) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

int main()
{
    int a, b;
    cin >> a >> b;

    judge(a, b);
}


Measured memory_bytes: 256
import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());

		long[] A = new long[N];
		long[] L = new long[N];
		long[] R = new long[N];

		for(int i = 0;i<N;i++) {
			A[i]=Long.parseLong(sc.next());
		}

		long w = A[0];
		for(int i=0;i<N-1;i++){
			w = GCD(w,A[i]);
			L[i] = w;
		}
		w = A[N-1];
		for(int i=N-1;i>=0;i--){
			w = GCD(w,A[i]);
			R[i] = w;
		}
		long max = 0;
		long tmp = 0;

		for(int i=0;i<N;i++) {
			if(i==0) {
				tmp = R[i+1];
			}else if(i==N-1) {
				tmp = L[i-1];
			}else {
				tmp = GCD(L[i-1],R[i+1]);
			}

			if(max<tmp) {
				max=tmp;
			}
		}

		System.out.println(max);
		sc.close();
	}
	static long GCD(long a,long b) {
		if(a>b) {
			long tmp;
	        while ((tmp = a % b) != 0) {
	            a = b;
	            b = tmp;
	        }
	        return b;
		}else {
			long tmp;
	        while ((tmp = b % a) != 0) {
	            b = a;
	            a = tmp;
	        }
	        return a;
		}
	}
}

Measured memory_bytes: 49176
import math

N,K=map(int,input().split())
ans=0
p1=1/N
for i in range(1,N+1):
    if i>=K:
        ans+=p1
        continue
    n=math.log2(K/i)
    if n==int(n):
        p2=0.5**n
    else:
        p2=0.5**(int(n+1))
    ans+=p1*p2
print(ans)

Measured memory_bytes: 3188
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main(){
    int a;
    string s;
    cin >> a >> s;
    if(3200<=a){
        cout << s << endl;;
    }else if(a<3200){
        cout << "red" <<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

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

    std::string s, t;
    std::cin >> s >> t;

    int ans = 0;
    for(int i = n; 0 <= i; --i){
        if(s.substr(n - i, i) == t.substr(0, i)){
            ans = n + n - i;
            break;
        }
    }

    std::cout << ans << std::endl;

    return 0;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

r,g,b = readints()
k = readint()

for i in range(k):
    if g<=r:
        g*=2
    else:
        b*=2
if b>g>r:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 61792
import sys

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


def combmod(n, r):
    res = 1

    for i in range(r):
        res *= (n - i)
        res %= MOD
        res *= pow((r - i), MOD - 2, MOD)

    nextdiv = n
    nextmul = n - r

    while True:
        yield res
        if nextmul == 0:
            break
        res *= pow(nextdiv, MOD - 2, MOD)
        res %= MOD
        res *= nextmul
        res %= MOD
        nextdiv -= 1
        nextmul -= 1


    return


def main():
    n0, k0 = list(map(int, readline().split()))
    a0 = list(map(int, readline().split()))

    a0.sort()

    ans = 0

    for i, com in enumerate(combmod(n0 - 1, k0 - 1)):
        ans += a0[-i - 1] * com
        ans %= MOD
        ans -= a0[i] * com
        ans %= MOD

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 59248
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(int i = 0; i < n; ++i)
#define INF (2e9)

int main() {
  int num, cnt = 1;

  while(true) {
    cin >> num;
    if(num == 0) {
      cout << cnt << endl;
      return 0;
    }
    cnt++;
  }

  return 0;
}

Measured memory_bytes: 3548
import sys

n,y=map(int,input().split())
ans=[-1]*3
for i in range(n+1):
	for j in range(n+1-i):
		k=n-i-j
		if 10000*i+5000*j+1000*k==y:
			ans[0]=i
			ans[1]=j
			ans[2]=k
print(str(ans[0])+' '+str(ans[1])+' '+str(ans[2]))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int d = 60;
constexpr int max_n = 200005;
int to[d][max_n];
int main()
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> to[0][i];
        to[0][i]--;
    }
    for (int i = 0; i < d - 1; i++)
    {
        for (int j = 0; j < n; j++)
        {
            to[i + 1][j] = to[i][to[i][j]];
        }
    }
    int v = 0;
    for(int i = d - 1; i >= 0; i--){
        ll l = 1ll << i;
        if(l<= k){
            v = to[i][v];
            k -= l;
        }
    }
    cout << v + 1 << endl;
}

Measured memory_bytes: 50544
package main
 
import (
	"fmt"
)
 
func main() {
  var a, b, c, d int
  fmt.Scanf("%d", &a)
  fmt.Scanf("%d", &b)
  fmt.Scanf("%d", &c)
  fmt.Scanf("%d", &d)
  
  ans := 0
  if a > b {
    ans += b
  }else{
    ans += a
  }
  if c > d {
    ans += d
  }else{
    ans += c
  }
  fmt.Printf("%d\n", ans)
}

Measured memory_bytes: 1832
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int k;
    cin>>k;
    long long sum=0;
    for(int i=1; i<=k; i++)
    {
        for(int x=1; x<=k; x++)
        {
            for(int j=1; j<=k; j++)
            {
                int cj=j, cx=x, ci=i;
                int gcd=1;
                while (j != 0)
                {
                    gcd = x % j;
                    x = j;
                    j = gcd;
                }
                int r=1;
                while (x != 0)
                {
                    r = i % x;
                    i = x;
                    x = r;
                }
                sum+=i;
                j=cj;
                x=cx;
                i=ci;
            }
        }
    }
    cout<<sum;
    return 0;
}

Measured memory_bytes: 3596
c = [list(map(int,input().split())) for _ in range(3)]
a1 = 0
b1 = c[0][0] - a1
b2 = c[0][1] - a1
b3 = c[0][2] - a1
a2 = c[1][0] - b1
a3 = c[2][0] - b1
flag = c[1][1] == a2 + b2 and c[1][2] == a2 + b3 and c[2][1] == a3 + b2 and c[2][2] == a3 + b3

print('Yes' if flag else 'No')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);  cin.tie(NULL); cout.tie(NULL);
#define scan(a,n) for(long int index=0;index<n;index++) cin>>a[index];
#define print(a,n) for(long int index=0;index<n;index++) cout<<a[index]<<" ";cout<<endl;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define vli vector<long long int>
#define vlli vector<pair<long long int,long long int>>
#define vsi vector<string>
#define vci vector<char>


 
int main() {
   #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    fast;
    ll n;
    cin>>n;
    ll a[n],i;
    scan(a,n);
    sort(a,a+n);
    vli v;
    ll sum=a[n-1];
    ll j=n-2;
    for(i=0;i<n-2;i++){
      sum+=a[j];
      if(i%2==1) j--;
    } 
    cout<<sum<<endl; 
return 0;
    
 }  
 
  


Measured memory_bytes: 5172
def gcd(a,b):
    while b!=0:
        a,b = b,a%b
    return a

def lcm(a,b):
    return a*b//gcd(a,b)

def divided(a,b,c):
    return b//c - (a-1)//c

A,B,C,D = map(int,input().split())

print( (B-A+1) - divided(A,B,C) - divided(A,B,D) + divided(A,B,lcm(C,D)))

Measured memory_bytes: 3064
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from copy import *

N, K = map(int, input().split())
A = list(map(int, input().split()))
gcd = lambda a, b: a if b == 0 else gcd(b, a%b)
G = 0
for a in A:
    G = gcd(a, G)
M = max(A)
if K <= M and K % G == 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")


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

int main(){
  string s, t;
  cin >> s >> t;
  sort(s.begin(), s.end());
  sort(t.rbegin(), t.rend());
  if(s<t)cout << "Yes";
  else cout << "No";
  return 0;
}

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

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

    ll x, y;
    cin >> x >> y;

    int count = 0;
    while(true) {
        x *= 2;
        count++;
        if (x > y) break ;
    }

    cout << count << '\n';
    return 0;
}


Measured memory_bytes: 256
A, B, C = map(int, input().split())
if A * B * C % 2 == 0:
    print(0)
else:
    m1 = ((A - 1) // 2 + 1) * B * C - ((A - 1) // 2) * B * C
    m2 = ((B - 1) // 2 + 1) * A * C - ((B - 1) // 2) * A * C
    m3 = ((C - 1) // 2 + 1) * B * A - ((C - 1) // 2) * B * A
    print(min(m1, m2, m3))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end()
{
#ifdef ONLINE
    printf("\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000);
    system("pause");
#endif
}
int h, w;
int A[N], B[N], cnt[N];
map<int, int> c;

inline void solve()
{
    scanf("%d%d", &h, &w);
    for (int i = 1; i <= h; ++i)
        scanf("%d%d", &A[i], &B[i]);
    for (int i = 1; i <= w; ++i)
        c.insert(mp(i, i));
    int ans = 0;
    cnt[0] = w;
    for (int i = 1; i <= h; ++i)
    {
        int L = A[i], R = B[i];
        auto it = c.lower_bound(L);
        int pos = 0;
        while (it != c.end() && it->first <= R + 1)
        {
            pos = max(pos, it->second);
            cnt[it->first - it->second]--;
            it = c.erase(it);
        }
        if (pos && R + 1 <= w)
        {
            cnt[R + 1 - pos]++;
            c.insert(mp(R + 1, pos));
        }
        while (!cnt[ans] && ans < w)
            ans++;
        if (ans == w)
            puts("-1");
        else
            printf("%d\n", ans + i);
    }
}

int main()
{
    TIME__START = clock();
    int Test = 1;
    // scanf("%d", &Test);
    while (Test--)
        solve();
    TIME__END = clock();
    program_end();
    return 0;
}

Measured memory_bytes: 15204
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i, 0, n) cin >> a[i] >> b[i];
    ll ans = 0, add = 0;
    for (int i = n-1; i >= 0; i--) {
        ans += ((a[i]+add+b[i]-1)/b[i]) * b[i] - a[i] - add;
        add += ((a[i]+add+b[i]-1)/b[i]) * b[i] - a[i] - add;
    }
    cout << ans << endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
    char b, ans;
    cin >> b;
    if (b == 'A') ans = 'T';
    else if (b == 'T') ans = 'A';
    else if (b == 'C') ans = 'G';
    else ans = 'C';
    cout << ans << endl;
}


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

int parent[200005], Rank[200005], ind[200005];

void initset(int n)
{
	for (int i = 1; i <= n; i++)
	{
		parent[i] = i;
		Rank[i] = 0;
	}
}

// Return representative index
int findset(int x)
{
	// Value of representative index is itself
	//cout << "ind" << ind[x] << " par" << parent[x] << " x" << x << "\n";
	if (parent[x] == x)
		return parent[x];
	parent[x] = findset(parent[x]); // Path compression; set value/arrow to representative index
	return parent[x];
}

bool sameset(int x, int y)
{ // Same set would have same rep index
	return findset(x) == findset(y);
}

// Input: 2 nodes which may not be representatives
void mergeset(int x, int y)
{
	//cout << "merge start\n";
	x = findset(x);
	//cout << "y\n";
	y = findset(y);
	if (Rank[x] > Rank[y])
	{
		parent[y] = x;
	}
	else if (Rank[y] > Rank[x])
	{
		parent[x] = y;
	}
	else
	{
		parent[x] = y;
		Rank[y]++;
	}
}

int N, M, P, X, Y, ans = 0;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> N >> M;
	initset(N + 5);
	for (int i = 1; i <= N; i++)
	{
		cin >> P;
		ind[i] = P;
	}
	for (int i = 1; i <= M; i++)
	{
		cin >> X >> Y;
		mergeset(X, Y);
		// for (int i = 1; i <= N; i++)
		// 	cout << parent[i] << " ";
		// cout << "\n";
	}

	for (int i = 1; i <= N; i++)
	{
		if (findset(i) == findset(ind[i]))
			ans++;
	}
	cout << ans << "\n";
	// for (int i = 1; i <= N; i++)
	// 	cout << ind[i] << " ";
	// cout << "\n";
	// for (int i = 1; i <= N; i++)
	// 	cout << parent[i] << " ";
	// cout << "\n";
	// for (int i = 1; i <= N; i++)
	// 	cout << Rank[i] << " ";
	// cout << "\n";
}


Measured memory_bytes: 1408
W, H, N = map(int, input().split())
more_y = H
less_y = 0
more_x = W
less_x = 0
for _ in range(N):
  _x, _y, _type = map(int, input().split())
  if _type == 1:
    less_x = max(less_x, _x)
  elif _type == 2:
    more_x = min(more_x, _x)
  elif _type == 3:
    less_y = max(less_y, _y)
  else:
    more_y = min(more_y, _y)
if more_x <= less_x or more_y <= less_y:
  print(0)
else:
  print((more_x-less_x)*(more_y-less_y))


Measured memory_bytes: 3064
import bisect

n, m = map(int, input().split())

pn_cn = []
pn_cn_dict = {}


for _ in range(m):
    pn, cn = map(int, input().split())
    cities = pn_cn_dict.get(pn)
    if cities is None:
        cities = [cn]
    else:
        cities.append(cn)
    pn_cn_dict[pn] = cities
    pn_cn.append((pn, cn))

for k in pn_cn_dict:
    c = pn_cn_dict[k]
    c = sorted(c)
    pn_cn_dict[k] = c

for pn, cn in pn_cn:
    ci = bisect.bisect(pn_cn_dict[pn], cn)
    print("{:06}{:06}".format(pn, ci))




Measured memory_bytes: 85208
numstr = input()
num = int(input())
def check():
    infinity = ""
    for i in range(num):
        if numstr[i] is not "1":
            infinity = infinity + numstr[i]
            return infinity
        else:
            infinity = infinity + "1"
    return "1"
infinity = check()
print(infinity[len(infinity)-1])
        


Measured memory_bytes: 2940
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include<limits.h>
#include<iomanip>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define REP(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(),v.end()
#define RALL(v) v.rbegin(),v.rend()
#define check(v) rep(i,v.size()) cout << v[i] << " ";\
cout << endl
const long long LLINF = 1LL<<60;
const int INF = 1e9;
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; }
typedef long long ll;
#define pint pair<int,int>
using namespace std;
//オーバーフローに気をつけろよおおおおおお
//確認忘れるなよおおおおおお

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m; cin >> n >> m;
    vector<ll> A(n);
    rep(i,n) cin >> A[i];
    sort(ALL(A));

    vector<ll> rui(n+1,0);
    rep(i,n) rui[i+1] = rui[i] + A[i];

    ll l = 0, r = 1e12;
    ll cnt = 0, ret = 0;

    while(l+1!=r) {
        //増える幸福度を決め打ち
        ll mid = (l+r)/2;
        ret = 0;
        cnt = 0;
        // aは左手でつなぐ相手
        for (auto a : A) {
            // aと足してmidを超える最小の右手を探す
            auto itr = lower_bound(ALL(A),mid-a);
            cnt += A.end() - itr;
            int i = itr - A.begin();
            ret += rui[n]-rui[i] + a * (A.end()-itr);
        }
        if(cnt >= m) l = mid;
        else r = mid;
    }
    cout << ret - (cnt-m)*l << endl;
    return 0;
}

Measured memory_bytes: 1920
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define reg register
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct node
{
	int val,id;
}a[N];
int n,w[N],tmp[N];
ll c[N<<1],tot;
inline int read()
{
	int x=0,w=1;
	char c=getchar();
	while (!isdigit(c)&&c!='-') c=getchar();
	if (c=='-') c=getchar(),w=-1;
	while (isdigit(c))
	{
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*w;
}
bool cmp1(node a,node b){return a.val<b.val;}
bool cmp2(node a,node b){return a.id<b.id;}
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int k){for (;x<=n+100000;x+=lowbit(x)) c[x]+=k;}
inline ll sum(int x,ll res=0){for (;x;x-=lowbit(x)) res+=c[x]; return res;}
inline bool check(int k)
{
	memset(c,0,sizeof(c));
	for (reg int i=1;i<=n;i++)
	  if (a[i].val<k) tmp[i]=-1; else tmp[i]=1;
	ll cnt=0,res=0; add(100000,1);
	for (reg int i=1;i<=n;i++)
	{
		cnt+=tmp[i];
		res+=sum(cnt+100000);
		add(cnt+100000,1);
	}
	return res>=(tot/2+tot%2);
}
int main()
{
	n=read(); tot=1ll*n*(n+1)/2;
	for (reg int i=1;i<=n;i++) a[i].val=read(),a[i].id=i;
	sort(a+1,a+n+1,cmp1);
	for (reg int i=1;i<=n;i++) w[i]=a[i].val;
	sort(a+1,a+n+1,cmp2);
	int l=1,r=n;
	while (l<r)
	{
		int mid=(l+r)>>1; ++mid;
		if (check(w[mid])) l=mid;
		else r=mid-1;
	}
	printf("%d\n",w[l]);
	return 0;
}

Measured memory_bytes: 3328
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pi 3.1415926535
#define endl '\n'
using namespace std;
struct custom {
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//using namespace __gnu_pbds;
//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
const ll inf=1e19;
/*ll power(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=(y>>1);
    }
    return res;
}*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n,i,j,k;
    cin>>n;
    ll arr[n+1][3],dp[n+1][3];
    for(i=1;i<=n;i++)cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
    memset(dp,0,sizeof(dp));
    for(i=1;i<=n;i++)
    {
        dp[i][0]=arr[i][0]+max(dp[i-1][1],dp[i-1][2]);
        dp[i][1]=arr[i][1]+max(dp[i-1][0],dp[i-1][2]);
        dp[i][2]=arr[i][2]+max(dp[i-1][1],dp[i-1][0]);
    }
    cout<<max(dp[n][0],max(dp[n][1],dp[n][2]))<<endl;
}


Measured memory_bytes: 4992
#include<iostream>
using namespace std;
typedef long long ll;
#define fr(i,n) for(int i=0;i<n;i++)



int main(){
  int n,p[55555],kari=0;
  bool fl=false;
  int a[60]={11,31,41,61,71,101,131,151,181,191,211,241,251,271,281,311,331,401,431,461,521,541,571,601,641,661,701,751,761,811,821,881,911,971,991,1021,1031,1051,1061,1091,1151,1171,1181,1201,1231,1291,1301,1361,1381,1451,1471,1511,1571,1601,1721,1871,1931};
  cin>>n;
  fr(i,n) cout<<a[i]<<" ";
}

Measured memory_bytes: 256