akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<vector>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
int main(){
    ll N,M;
    cin >> N >> M;
    cout <<min(N%M,M-N%M);
    return 0;
}


Measured memory_bytes: 256
/*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C
created at 31.8.2015 by charis 
last edited at 1.9.2015 by charis
*/

#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;

struct node{
	unsigned int key; 
	struct node *next;
	struct node *prev; 
}; 

typedef struct node* NodePointer;

NodePointer nil;

void init(){
	nil = new struct node;
	nil->next = nil;
	nil->prev = nil;
} 
void insert(int key){
	NodePointer x; 
	x = new struct node; 
	x->key = key; 
	
	x->next = nil->next ;
	nil->next->prev = x ;
	nil->next = x ;
	x->prev = nil;
} 
void deleteFirst(){
	NodePointer z = nil->next; 
	if ( z == nil ) return;
	NodePointer y = z->next ;
	
	nil->next = y ;
	y->prev = nil ; 
	delete[] z;
} 
void deleteLast(){
	NodePointer x = nil->prev; 
	if ( x == nil ) return; 
	NodePointer y = x->prev;
	
	y->next = nil ;
	nil->prev = y;
	delete[] x;
} 
void del(int key){
	NodePointer y = nil->next ;
	while(1){
		if (y == nil) break;
		if (y->key == key) {
			NodePointer z = y->prev;
			NodePointer x = y->next;
			z->next = x;
			x->prev = z;
				
			delete[] y;	
			break;
		}			
		else {
			NodePointer t = y->next;
			y = t;
		}
	}
} 
void printList(){
	NodePointer cur = nil->next; 
	while(1){ 	
		if (cur->next == nil){
			printf("%d\n", cur->key );
			break;
		} 
		printf("%d ", cur->key );
		cur = cur->next; 
	} 
} 

int main(){ 
	int key, n, i; 
	char com[10]; 
	scanf("%d", &n); 
	init();
	for ( i = 0; i < n; i++ ){
		scanf("%s", &com); 
		if ( com[0] == 'i' ) {
			scanf("%d", &key) ;
			insert(key);
		}			
		else if ( com[6] == 'F' ) deleteFirst();
		else if ( com[6] == 'L' ) deleteLast(); 
		else {
			scanf("%d", &key) ;
			del(key) ;

		}
	}
	
	printList();
	return 0; 
}

Measured memory_bytes: 32216
#![allow(non_snake_case,unused_imports,dead_code)]
use std::io::Read;
use std::cmp::max;
use std::cmp::min;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::BinaryHeap;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut buf_it = buf.split_whitespace();
    let N = buf_it.next().unwrap().parse::<isize>().unwrap();
    let A = (0..N).map(|_| buf_it.next().unwrap().parse::<isize>().unwrap()).collect::<Vec<_>>();

    let s = A.iter().sum::<isize>();
    let B = A.iter().map(|&a| (a*N-s).abs()).collect::<Vec<_>>();
    let m = *B.iter().min().unwrap();
    for i in 0..N{
        if m==B[i as usize]{
            println!("{}",i);
            return;
        }
    }
}

Measured memory_bytes: 4352
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll n,m,sum=0;
	cin>>n>>m;
	ll a[m];
	for(int i=0;i<m;i++){
		cin>>a[i];
		sum+=a[i];
	}
	if(sum>n){
		cout<<-1;
	}else{
		cout<<(n-sum);
	}
}

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

int sum(vector<int> B){
  
  int i, sum = 0;
  rep(i, B.size()){
     sum += B[i];
  }
  return sum;
}

int max(vector<int> B){
  
  int i, ma = 0;
  rep(i, B.size()){
    
    if (ma < B[i]){
     ma = B[i];
    }
  }
  return ma;
}

int main(){
 
  int N, K, i;
  cin >> K >> N;
  vector<int> A(N);
  
  rep(i, N){
    cin >> A[i];
  }
  sort(A.begin(), A.end());
  
  int a = A[0];
  rep(i, N){
    A[i] -= a;
  }
  
  vector<int> B(N);
  rep(i, N - 1){
    B[i] = abs(A[i] - A[i + 1]);
  }
  B[N - 1] = K - A[N - 1];
  
  cout << K - max(B) << endl;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define inl(a) cin >> a
#define prl(a) cout << a << " "
#define outl(a) cout << a << endl
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define all(vec) vec.begin(),vec.end()
#define asc(vec) sort(vec.begin(),vec.end())
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
#define tddd tuple<double,double,double>
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<int,pair<int,int> > >
#define vvii vector<vector<pair<int,int> > >
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define N 55
typedef long long int ll;
ll I[N][N],A[N][N],tp[N][N],v[N][N];
ll pro(ll x,ll y)
{
    return (x*y)%M;
}
void mul(int m)
{
    int i,j,k;
    lp(i,0,m)
    {
        lp(j,0,m)
        {
            tp[i][j]=0;
            lp(k,0,m)
                tp[i][j]+=pro(I[i][k],A[k][j]);
        }
    }
    lp(i,0,m)
    {
        lp(j,0,m)
            I[i][j]=tp[i][j]%M;
    }
    return ;
}
void sqr(int m)
{
    int i,j,k;
    lp(i,0,m)
    {
        lp(j,0,m)
        {
            tp[i][j]=0;
            lp(k,0,m)
                tp[i][j]+=pro(A[i][k],A[k][j]);
        }
    }
    lp(i,0,m)
    {
        lp(j,0,m)
            A[i][j]=tp[i][j]%M;
    }
    return ;
}
void power(ll y,int m)
{
    while(y>0)
    {
        if(y&1)
            mul(m);
        y/=2;
        sqr(m);
    }
    return ;
}
void print(int m)
{
    int i,j;
    lp(i,0,m)
    {
        lp(j,0,m)
            cout << I[i][j] << " ";
        cout << endl;
    }
}
void initial(int m)
{
    int i,j;
    lp(i,0,m)
    {
        lp(j,0,m)
            A[i][j]=v[i][j],I[i][j]=0;
        I[i][i]=1;
    }
}
int main()
{
    ll y;
    int t,n,m,i,j;
    in(n);
    inl(y);
    rep(i,n)
    {
        rep(j,n)
            inl(v[i][j]);
    }
    initial(n);
    power(y,n);
    y=0;
    rep(i,n)
    {
        rep(j,n)
            y+=I[i][j];
    }
    outl(y%M);
}









Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename T> inline T sqr(T t) {return t*t;}
#define pb push_back
#define mp make_pair
#define endl "\n"

inline void files()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef _WIN32
        return;
    #endif // _WIN32
    //freopen("C.in", "r", stdin);freopen("C.out", "w", stdout);
}

int n;

int a[100100];

int main(){
    files();
    cin >> n;
    for (int i = 1; i <= n; i++){
        int l, r;
        cin >> l >> r;
        a[l]++;
        a[r+1]--;
    }
    int cur = 0;
    int ans = 0;
    for (int i = 1; i <= 100000; i++){
        cur += a[i];
        if (cur>0)
            ans++;
    }
    cout << ans;
}


Measured memory_bytes: 640
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
 
ll MOD = 1000000007;
ll _MOD = 1000000009;
int INF = INT_MAX / 2;
double EPS = 1e-10;
 
void unko(bool x) {
	if (x) cout << "unko" << endl;
}
 
int main() {
    int N, M; cin >> N >> M;
    vector<int> K(N);
    vector<vector<int> > S(N), H(N);
	int sumK = 0;
    for (int i = 0; i < N; i++) {
        cin >> K[i];
		sumK += K[i];
        for (int k = 0; k < K[i]; k++) {
            int s, h; cin >> s >> h;
            S[i].push_back(s);
            H[i].push_back(h);
        }
    }
	unko(sumK >= 4000 * 0.5 && M >= 200);
    double maxi = 0;
    for (int t = 0; t < N; t++) {
        vector<int> dp(M + 1, -INF);
        dp[M] = 0;
        for (int i = 0; i < N - 1; i++) {
            vector<int> _dp = dp;
            for (int j = 0; j <= M; j++) {
                int h = 0, v = 0;
                for (int k = 0; k < K[i]; k++) {
                    h += H[i][k];
                    v += S[i][k] * H[i][k];
                    if (v > j) break;
                    _dp[j - v] = max(_dp[j - v], dp[j] + h);
                }
            }
            dp = _dp;
        }
        for (int j = 0; j <= M; j++) {
            double h = dp[j], v = j;
            for (int k = 0; k < K[N - 1]; k++) {
                double dh = min((double)H[N - 1][k], v / S[N - 1][k]);
                h += dh; v -= S[N - 1][k] * dh;
            }
            maxi = max(maxi, h);
        }
        rotate(K.begin(), K.begin() + 1, K.end());
        rotate(S.begin(), S.begin() + 1, S.end());
        rotate(H.begin(), H.begin() + 1, H.end());
    }
    printf("%.20f\n", maxi);
}

Measured memory_bytes: 1272
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
if(a > b or a == b){
    for(int i = 0; i < a; i++){
cout << b;
    }
} else if (b > a){
    for(int i = 0; i < b; i++){
        cout << a;
    }
}
}

Measured memory_bytes: 256
w, h, n = map(int, input().split())
c = [[True] * w for i in range(h)]
l = [list(map(int, input().split())) for i in range(n)]
ans = 0
for i in range(n):
    if l[i][2] == 1:
        for j in range(w):
            for k in range(h):
                if j < l[i][0]:
                    c[k][j] = False
    elif l[i][2] == 2:
        for j in range(w):
            for k in range(h):
                if j > l[i][0]-1:
                    c[k][j] = False
    elif l[i][2] == 3:
        for j in range(w):
            for k in range(h):
                if k < l[i][1]:
                    c[k][j] = False
    else:
        for j in range(w):
            for k in range(h):
                if k > l[i][1]-1:
                    c[k][j] = False
for i in range(h):
    ans += c[i].count(True)
print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll n,a,b;
	cin>>n>>a>>b;
	ll div,rem,ans;
	div=(n)/(a+b);
	rem=n%(a+b);
	ans=(div)*(a);
	if(rem>=a)ans+=a;
	else ans+=rem;
	cout<<ans;
}

Measured memory_bytes: 256
PROGRAM skillUp
      IMPLICIT NONE
      integer(16) :: n,m,x
      integer(16),allocatable :: c(:),a(:,:),su(:)
      integer(16) :: i,k,ans
      logical,allocatable :: flag(:)
      logical :: piyo = .false.
      
      read*,n,m,x
      allocate( c(n), a(n,m),flag(n),su(m) )
      do i = 1,n
        read*,c(i),a(i,1:m)
      end do
      
      ans = sum( c )
      
      outer:do i = 1,2**n-1
        do k = 1,n
          flag(k) = btest( i,k-1 )
        end do
  !      print*,flag
        
        do k = 1,m
          su(k) = sum( a(:,k),flag )
        end do
        !print*,su
        do k = 1,m
          if( su(k)<x )then
            cycle outer
          end if
        end do
        
        piyo = .true.
        ans = min( ans,sum( c,flag ) )
        
      end do outer
      
      if( piyo )then
        print*,ans
      else
        print*,'-1'
      end if
      
      
  !    print*,n,m,x
  !    do i = 1,n
  !      print*,c(i),a(i,1:m)
  !    end do
      
      END PROGRAM

Measured memory_bytes: 2832
#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
#define     ll      long long
#define     ull     unsigned long long
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%lld",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>inline t abs(t a) {
    if(a>=0)
        return a;
    return -a;
}
template<typename T> inline T modpow(T b, T p, T Mod) {
    T res  = 1;
    while(p) {
        if(p&1) {
            res *= b ;
            res %= Mod ;
        }
        b *= b;
        b %= Mod;
        p >>= 1;
    }
    return res ;
}

template<typename T> inline T ModInv(T b, T Mod) {
    return modpow(b, Mod - 2, Mod );
}

template<typename T> inline T pwr(T b, T p) {
    T res  = 1;
    while(p) {
        if(p&1) {
            res *= b ;
        }
        b *= b;
        p >>= 1;
    }
    return res ;
}

template<typename T> inline T gcd(T a, T b) {
    if(b==0)
        return a;
    return gcd(b, a%b);
}

template <typename T> inline void Int(T &n) {
    n = 0;
    int f = 1;
    register int ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        if (ch == '-')
            f = -1;
    for (; isdigit(ch); ch = getchar())
        n = (n << 3) + (n << 1) + ch - '0';
    n = n * f;
}
template<typename T, typename U> inline void Int(T &a, U &b) {
    Int(a);
    Int(b);
}
template<typename T, typename U, typename W> inline void Int(T &a, U &b, W &c) {
    Int(a,b);
    Int(c);
}
template<typename T, typename U, typename W,typename X> inline void Int(T &a, U &b, W &c, X &d) {
    Int(a,b,c);
    Int(d);
}
vector<int> Unique(vector<int> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()),v.end());
    return v;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cout<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     pb		push_back
#define     all(x)      x.begin(),x.end()
#define     mem(a)      memset(a,0,sizeof a)
#define     memn(a)     memset(a,-1,sizeof a)


int main() {
    int n = Int , k = Int ;
    ll res = n ; 
    if(n % k == 0)return puts("-1"),0;
    for(int i = 2 ; ; i++){
    	ll f = res * 1LL * i ;
    	if(f % k)return printf("%lld\n",f),0;
    }
    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;
using P = pair<int,int>;
const int INF = 1000000000;
const int MOD = 1000000007;

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

int main() {
  long double r;
  cin >> r;
  long double ans = 2 * r * 3.141592;
  cout << fixed << setprecision(10) << ans << endl;
  return 0;
}


Measured memory_bytes: 3768
#include <bits/stdc++.h>
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
using namespace std;
void TxtRead(){
    freopen("sleepy.in","r", stdin);
    freopen("sleepy.out","w", stdout);
}
void TheTime(){
    ios_base::sync_with_stdio(false);
	cin.tie();
}
const int N = 1e6+5;
const ll INF = 1e9+5;
const int MOD = 5e6;
const double PI = 3.141592653589793116;

int n, q, ans=N;
bool used[10];
vector<int> tls;

void _seek(int x)
{
    if(x>=N) return;
    if(x>=n){
        ans=min(ans, x);
        return;
    }
    for(int i=0; i<tls.size(); i++)
        _seek(x*10+tls[i]);
}

int main()
{
    //TxtRead();
    TheTime();

    scanf("%d %d", &n, &q);
    for(int i=0; i<q; i++){
        int x;
        scanf("%d", &x);
        used[x]=true;
    }
    for(int i=0; i<10; i++) if(!used[i]) tls.pb(i);

    for(int i=0; i<tls.size(); i++){
        if(tls[i]!=0) _seek(tls[i]);
    }
    printf("%d\n", ans);
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.StringTokenizer;

class Main{
	public static void main(String[] args) {
		SC sc=new SC(System.in);
		int N=sc.nextInt();
		int[][] sw=new int[N][2];
		long cnt=0;
		for(int i=0; i<N; i++) {
			sw[i][0]=sc.nextInt();
			sw[i][1]=sc.nextInt();
		}
		for(int i=N-1; i>=0; i--) {
			if((cnt+sw[i][0])%sw[i][1]==0) {
				//
			}
			else {
				cnt+=sw[i][1]-(cnt+sw[i][0])%sw[i][1];
			}
		}
		System.out.println(cnt);
	}
	static class SC {
		InputStream inp = System.in;
		private BufferedReader reader = null;
		private StringTokenizer tokenizer = null;
		public SC(InputStream inp) {
			reader = new BufferedReader(new InputStreamReader(inp));
		}
		public String next() {
			if (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new UncheckedIOException(e);
				}
			}
			return tokenizer.nextToken();
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
	}
}

Measured memory_bytes: 45012
#include <iostream>

using namespace std;

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

    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n - i; ++j) {
            int k = n - i - j;
            int sum = 10000 * i + j * 5000 + k * 1000;
            if (sum == y) {
                cout << i << ' ' << j << ' ' << k << '\n';
                return 0;
            }
        }
    }
    cout << "-1 -1 -1\n";
}


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

namespace AtCoder
{
    class Program
    {
        static int n;
        static int[] h;
        static int[] flowers;
        static int count;

        static void Main(string[] args)
        {
            //[summary]C - Grand Garden
            n = int.Parse(Console.ReadLine());

            //目標とする花の高さ
            h = ReadLine().ToArray();

            flowers = new int[n];
            count = 0;

            while (true)
            {
                int firstIndex = 0;

                //水をやる花のカウントが始まっているかどうかのフラグ
                bool isStarted = false;

                for (int i = 0; i < n; i++)
                {
                    if (flowers[i] < h[i])
                    {
                        //カウントが始まっていなかったらスタート
                        if (!isStarted)
                        {
                            //最後の1本だけに水をやるケース
                            if (i == n - 1)
                            {
                                WaterFlower(i, i);
                            }
                            else
                            {
                                firstIndex = i;
                                isStarted = true;
                            }                            
                        }
                        else
                        {
                            //最後の花まで来たら、水をやる
                            if (i == n - 1)
                            {
                                WaterFlower(firstIndex, i);
                            }
                        }
                    }
                    else
                    {
                        //成長し切った花があったらカウントを止める
                        if (isStarted)
                        {
                            WaterFlower(firstIndex, i - 1);
                            isStarted = false;
                        }
                    }
                }

                if (IsCompleted())
                {
                    Console.WriteLine(count);
                    return;
                }
            }
        }

        static void WaterFlower(int firstIndex, int lastIndex)
        {
            //水やり開始
            for (int i = firstIndex; i <= lastIndex; i++)
            {
                flowers[i]++;
            }

            count++;
        }

        static bool IsCompleted()
        {
            for(int i = 0; i < n; i++)
            {
                if (flowers[i] != h[i])
                {
                    return false;
                }
            }

            return true;
        }

        static List<int> ReadLine()
        {
            var line = Console.ReadLine().TrimEnd();
            var array = line.Split(' ');
            var numbers = new List<int>();

            foreach (var s in array)
            {
                int num = int.Parse(s);
                numbers.Add(num);
            }

            return numbers;
        }
    }
}

Measured memory_bytes: 13140
n = int(input())

if n == 1:
  print(1)
else:
  #print("n=",n)
  xy_s = [[int(v) for v in input().split()] for _ in range(n)]
  v = {}
  for i in range(n):
    for j in range(n):
      if i == j:
        continue
      dx = xy_s[i][0] - xy_s[j][0]
      dy = xy_s[i][1] - xy_s[j][1]
      pq = (dx, dy)
      if pq in v:
        v[pq] += 1
      else:
        v[pq] = 1
    
      #v.append([xy_s[i][0] - xy_s[j][0] , xy_s[i][1] - xy_s[j][1]])
  #print(v)
  counts = v.values()
  m = max(counts)
  ans = n - m
  #print("n={}, m={}, ans={}".format(n, m, ans))
  print(ans)

Measured memory_bytes: 3444
import scala.collection.immutable.List
import scala.io.StdIn

object Main extends App {

  case class DisjointSet(size: Int){
    val rank = Array.fill[Int](size)(0)
    val pointer = Array.fill[Int](size)(0)
    
    (0 until size).foreach(i => makeSet(i))
    
    def makeSet(x: Int): Unit = {
      pointer(x) = x
      rank(x) = 0
    }
    
    def same(x: Int, y: Int): Boolean = {
      findSet(x) == findSet(y)
    }
    
    def unite(x: Int, y: Int): Unit = {
      link(findSet(x), findSet(y))
    }
    
    def link(x: Int, y: Int): Unit = {
      if(rank(x) > rank(y)){
        pointer(y) = x
      } else {
        pointer(x) = y
        if(rank(x) == rank(y)){
          rank(y) +=1
        }
      }
    }
    
    def findSet(x: Int): Int = {
      if(x != pointer(x)){
        pointer(x) = findSet(pointer(x))
      }
      pointer(x)
    }
    
  }
  
  
  val Array(n, q) = StdIn.readLine().split(' ').map(_.toInt)
  val disj = DisjointSet(n)
  
  (0 until q).foreach{ _ =>
    val Array(command, x, y) = StdIn.readLine().split(' ').map(_.toInt)
    if(command == 0) {
      disj.unite(x, y)
    } else {
      println(if(disj.same(x, y)) "1" else "0")
    }
  }
  
  

}




Measured memory_bytes: 160324
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 1005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;


int main()
{
    FASTIO
    /*
    //double start_time = clock();
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
//*/
    int T;
    //cin >> T;
    T = 1;
    for(int cs = 1; cs <= T; cs++){
      string s;
      cin >> s;
      set<char> st;
      map<char, int> mp;
      mp[s[0]]++, mp[s[1]]++, mp[s[2]]++,mp[s[3]]++;
      st.insert(s[0]);
      st.insert(s[1]);
      st.insert(s[2]);
      st.insert(s[3]);
      if(st.size() == 2 && mp[s[0]] == 2) cout << "Yes\n";
      else cout << "No\n";
    }

    //double end_time = clock();
    //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
    return 0;
}

Measured memory_bytes: 256
# import bisect
# import copy
# import fractions
# import math
# import numpy as np
# from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    [s,t]=[str(input()) for i in range(2)]

    if sorted(s)<sorted(t)[::-1]:
        print('Yes')
    else:
        print('No')

resolve()

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

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll; typedef pair<ll, ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename A,size_t N,typename T>void Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);}
const int inf = 0x3fffffff; const ll INF = 0x3fffffffffffffff;
//template end

template<typename M,typename O=M>
struct LazySegmentTree{
    using F=function<M(M,M)>; using G=function<M(M,O)>; using H=function<O(O,O)>;
    int sz,height; vector<M> data; vector<O> lazy;
    const F f;const G g; const H h; const M m1; const O o1;
    LazySegmentTree(int n,const F f,const G g,const H h,const M &m1,const O o1):f(f),g(g),h(h),m1(m1),o1(o1){
        sz=1,height=0; while(sz<n)sz<<=1,height++;
        data.assign(2*sz,m1); lazy.assign(2*sz,o1);
    }
    void set(int k,M x){data[k+sz]=x;}
    void build(){rrep(k,sz-1,0)data[k]=f(data[2*k],data[2*k+1]);}
    M ref(int k){return lazy[k]==o1?data[k]:g(data[k],lazy[k]);}
    void recalc(int k){while(k>>=1)data[k]=f(ref(2*k),ref(2*k+1));}
    void thrust(int k){rrep(i,height,0)eval(k>>i);}
    void eval(int k){
        if(lazy[k]!=o1){
            lazy[2*k]=h(lazy[2*k],lazy[k]); lazy[2*k+1]=h(lazy[2*k+1],lazy[k]);
            data[k]=ref(k); lazy[k]=o1;
        }
    }
    void update(int a,int b,O x){
        thrust(a+=sz); thrust(b+=sz-1);
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if(l&1)lazy[l]=h(lazy[l],x),++l;
            if(r&1)--r,lazy[r]=h(lazy[r],x);
        }
        recalc(a); recalc(b);
    }
    M query(int a,int b){
        thrust(a+=sz); thrust(b+=sz-1);
        M L=m1,R=m1;
        for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if(l&1)L=f(L,ref(l++)); if(r&1)R=f(ref(--r),R);
        } return f(L,R);
    }
};

int main(){
    int n; scanf("%d",&n);
    vector<int> h(n),a(n);
    rep(i,0,n)scanf("%d",&h[i]);
    rep(i,0,n)scanf("%d",&a[i]);
    LazySegmentTree<ll> seg(200010,
        [](ll a,ll b){return max(a,b);},
        [](ll a,ll b){return max(a,b);},
        [](ll a,ll b){return b;},0,0);
    seg.build(); seg.update(0,1,0);
    rep(i,0,n){
        ll val=seg.query(0,h[i]); val+=a[i];
        seg.update(h[i],h[i]+1,val);
    }
    printf("%lld\n",seg.query(0,n+1));
    return 0;
}

Measured memory_bytes: 9984
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

double cross(double x1,double y1,double x2,double y2)
{
	return x1*y2-x2*y1;
}

string solve(double x[],double y[])
{
	double area=0;
	repi(i,1,3)
		area+=cross(x[i]-x[0],y[i]-y[0],x[i+1]-x[0],y[i+1]-y[0]);
	area=abs(area/2);
	
	rep(i,4){
		double a=abs(cross(x[1]-x[0],y[1]-y[0],x[2]-x[0],y[2]-y[0])/2);
		if(area+1e-10<a)
			return "NO";
		rotate(x,x+1,x+4);
		rotate(y,y+1,y+4);
	}
	return "YES";
}

int main()
{
	for(double x[4],y[4];scanf("%lf,%lf",x,y)==2;){
		repi(i,1,4)
			scanf(",%lf,%lf",x+i,y+i);
		cout<<solve(x,y)<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 0
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define REP(i, n) for (ll (i) = 0; (i) < (n); (i)++)
#define REPI(i, a, b) for (ll (i) = (a); (i) < (b); (i)++)
#define int long long
using namespace std;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;

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

signed main() {
  int N, X;
  cin >> N >> X;
  VI x(N);
  REP (i, N) {
    cin >> x[i];
    x[i] = abs(x[i] - X);
  }
  int ans = x[N-1];
  REP (i, N-1) {
    ans = gcd(ans, x[i]);
  }
  cout << ans << endl;
}


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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define Eback(s) s.erase(s.end()-1,s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v),0LL)
#define mapmin(v) v.rbegin()->first
#define mapmax(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr<<"OK\n"
#define OK1 cerr<<"OK1\n"
#define OK2 cerr<<"OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL<<62)-(1LL<<31))
#define zero(x,n) setw(x) << setfill('0') << n
#define endl '\n'

typedef long long ll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<ll,ll>> vpll;

const double pi = acos(-1.0);
const ll mod=1000000007;
const ll mod2=998244353;

template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>&p) {
	ost << "{" << p.first << ", " << p.second << "} ";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost, const vector<T>&v) {
	ost << "{";
	for (int i = 0; i<(int)v.size(); i++) {
		if (i)ost << " ";
		ost << v[i];
	}
	ost << "} \n";
	return ost;
}

template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>&v) {
	ost << "{";
	for (auto p:v) {
		ost << "{" << p.first << ", " << p.second << "} ";
	}
	ost << "} ";
	return ost;
}

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;}

void YES(bool b) {cout << ((b)?"YES\n":"NO\n");}
void Yes(bool b) {cout << ((b)?"Yes\n":"No\n");}
void yes(bool b) {cout << ((b)?"yes\n":"no\n");}
void Yay(bool b) {cout << ((b)?"Yay!\n":":(\n");}

void out(){cout << "\n";}
template<class T,class... Args>
void out(const T &x,const Args &... args){
	cout << x << " ";
	out(args...);
}

ll powmod(ll a,ll b){ll c=1; while(b>0){ if(b&1){ c=a*c%mod; } a=a*a%mod; b>>=1; } return c;}
ll gcd(ll x, ll y) {return __gcd(x,y);}
ll lcm(ll x, ll y) {return x/__gcd(x,y)*y;}

int main()
{
	ll n,m,l;
	cin >> n >> m >> l;
	vvll a(n,vll(m)),b(m,vll(l));
	rep(i,n)rep(j,m) cin >> a[i][j];
	rep(i,m)rep(j,l) cin >> b[i][j];
	rep(i,n){
		rep(j,l){
			ll sum=0;
			rep(k,m){
				sum+=a[i][k]*b[k][j];
			}
			if(j==0) cout << sum;
			else cout << " " << sum;
		}
		cout << endl;
	}
	return 0;
}


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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String[] S = new String[N];
        for (int i = 0; i < N; i++) {
            S[i] = sc.next();
        }
        System.out.println( solve(N, S) );
    }

    private static String solve(long N, String[] S) {
        StringBuilder ans = new StringBuilder();

        for (char c = 'a';c <= 'z'; c++) {
            int cnt = countCommonChar(c, S);

            for (int i = 0; i < cnt; i++) {
                ans.append(c);
            }
        }

        return ans.toString();
    }

    private static int countCommonChar(char c, String[] S) {
        int common = Integer.MAX_VALUE;
        for (String s : S) {
            common = Math.min(countChar(c, s), common);
        }
        return common;
    }

    private static int countChar(char c, String s) {
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            if( s.charAt(i) == c ) {
                cnt ++;
            }
        }
        return cnt;
    }
}


Measured memory_bytes: 23764
a,b,c = map(int,input().split())
g = 0
count=0
if a%2==0:
    g +=1
    
if b%2==0:
    g +=1
if c%2==0:
    g +=1
if g == 1 or g == 2:
    count+=1
lists = [a,b,c]
lists.sort()
if count==1:
    if (g == 1 and lists[2]%2 == 0) or (g == 2 and lists[2]%2 != 0):
        count += ((lists[2]-1)-lists[0])/2
        count += ((lists[2]-1)-lists[1])/2
    else :
        count += int((lists[2]) - lists[0])/2
        count += int((lists[2]) - lists[1])/2
        count +=1
else :
    count += ((lists[2])-lists[0])/2
    count += ((lists[2])-lists[1])/2
print(int(count))

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

int main()
{
	int n;
	while (cin >> n, n) {
		int i = 0;
		while (n != 1) {
			if (n & 1 == 1) {
				n = n * 3 + 1;
				i++;
			}
			else {
				n = n / 2;
				i++;
			}
		}
		cout << i << endl;
	}
    return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 300010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N; ll X;
ll A[MAX_N];

void solve() {
	cin >> N >> X;
	rep(i, 0, N) cin >> A[i];

	ll res = 0;
	rep(i, 0, N) {
		if(A[i] + A[i + 1] > X) {
			if(A[i] > X) {
				res += A[i + 1] + A[i] - X;
				A[i + 1] = 0;
				A[i] = X;
			}
			else {
				res += A[i] + A[i + 1] - X;
				A[i + 1] = X - A[i];
			}
		}
	}
	cout << res << "\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
	srand((unsigned int)time(NULL));
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}



Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20);
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for(int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for(auto&& i: n)
void in(){}
template<typename F, typename... R>
bool in(F& f, R&... r){
    if(cin >> f){in(r...);return true;}
    else return false;
}
#define out(x) cout << (x)
#define space() cout << " "
#define indent() cout << '\n'
void print(){}
template<typename F, typename... R>
void print(F f, R... r){out(f), indent(), print(r...);}
#define debughead(x) cerr << "Line " << __LINE__ << ": " << #x << ": "
#define debugout(x) cerr << (x) << " "
#define debugindent() cerr << '\n'
#define debug(x) debughead(x), debugout(x), debugindent()
const int INF = 1e18, MOD = 1e9 + 7;
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}

main(){
    INCANT;
    int n, a, res = 0;
    string s;
    in(n);
    map<int, vector<int>> m;
    rep(i, 1, n + 1){
        in(a);
        m[a].pb(i);
    }
    rep(i, 1, n + 1){
        each(j, m[i]){
            if(find(all(m[j]), i) != m[j].end()){
                res++;
            }
        }
    }
    print(res / 2);
}


Measured memory_bytes: 11264
/*input
6 6 8
..##..
.#..#.
#....#
######
#....#
#....#
*/

#include <bits/stdc++.h>
#include <unistd.h>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pb push_back
#define F first
#define S second
#define UQ(x) (x).resize(distance((x).begin(),unique(x.begin(),x.end())))

using namespace std;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rnd64(chrono::high_resolution_clock::now().time_since_epoch().count());

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}


void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,_;
	ll h,w,k;
	cin>>h>>w>>k;
	ll a[h][w];
	char ch;
	rep(i,0,h){
		rep(j,0,w){
			cin>>ch;
			a[i][j]=(ch=='#');
		}
	}
	ll j1,i1;
	ll ans=0;
	rep(i,0,(1<<h)){
		rep(j,0,(1<<w)){
			ll tmp=0;
			rep(i1,0,h){
				if((1<<i1)&i)continue;
				rep(j1,0,w){
					if((1<<j1)&j)continue;
					tmp+=a[i1][j1];
				}
			}
			ans+=(tmp==k);
		}
	}
	cout<<ans<<endl;
}

int main()
{
	ios::sync_with_stdio(0);
 	cin.tie(0);
	ll T=1;
	//cin>>T;
	while(T--){
		solve();
	}
	return 0;
}




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

int main(){
  int N,M;
  cin >> N >> M;
  vector<vector<int>>ks(M);
  for (int i=0;i<M;i++) {
	int k; cin >> k;
	ks[i].resize(k);
	for (int j=0;j<k;j++) {
		cin >> ks[i][j];
		ks[i][j]--;
	}
  }
  vector<int>p(M);
  for(int i=0;i<M;i++){
    cin >> p[i];
  }
  int ans=0;
  for(int i=0;i<(1 << N);i++){
    bool ok = true;
    for(int j=0;j<M;j++){
      int c=0;
      for(int d:ks[j]){
        if((i >> d)&1){
          ++c;
        }
      }
      c%=2;
      if(c!=p[j]){
        ok = false;
      }
    }
    if(ok){
      ans++;
    }
  }
  cout << ans << endl;
      
}
      


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<queue>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> P;
int N,M,K;
int A[100000];
int B[100000];
int L[100000];
vector<P>G[3000];
priority_queue<P,vector<P>,greater<P> >que;
int S[3000];
int main()
{
	scanf("%d%d%d",&N,&M,&K);
	for(int i=0;i<N;i++)S[i]=1000*1000*1000;
	for(int i=0;i<M;i++){
		scanf("%d%d%d",&A[i],&B[i],&L[i]);A[i]--;B[i]--;
		G[A[i]].push_back(P(L[i],B[i]));
		G[B[i]].push_back(P(L[i],A[i]));
	}
	for(int i=0;i<K;i++){
		int S;
		scanf("%d",&S);S--;
		que.push(P(0,S));
	}
	while(!que.empty()){
		P p=que.top();que.pop();
		int pf=p.first;int ps=p.second;
		if(S[ps]<=pf)continue;
		S[ps]=pf;
		for(int i=0;i<(int)G[ps].size();i++){
			if(pf+G[ps][i].first<S[G[ps][i].second])que.push(P(pf+G[ps][i].first,G[ps][i].second));
		}
	}
	int ans=0;
	for(int i=0;i<M;i++){
		ans=max(ans,S[A[i]]+S[B[i]]+L[i]);
	}
	printf("%d\n",(ans+1)/2);
	return 0;
}

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

int main() {
  int n; cin >> n;
  vector<int> r(n + 1);
  for (int i = 1; i <= n; i++) cin >> r[i];
  
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (r[r[i]] == i) ans++;
  }
  
  cout << ans / 2 << endl;
}

Measured memory_bytes: 640
# coding: utf-8
N, M = map(int, input().split())
votes = list(map(int, input().split()))
total = sum(votes)
limit = total / (4 * M)
votes.sort(reverse=True)
count = 0
for i in votes:
    if i >= limit:
        count += 1
    else:
        break

if count >= M:
    print("Yes")
else:
    print("No")


Measured memory_bytes: 3060
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Tarek
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        AWeLoveGolf solver = new AWeLoveGolf();
        solver.solve(1, in, out);
        out.close();
    }

    static class AWeLoveGolf {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int k = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            int f = 0;
            for (int i = a; i <= b; i++) {
                if (i % k == 0) {
                    f = 1;
                    break;
                }
            }
            if (f == 1) out.println("OK");
            else out.println("NG");
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}



Measured memory_bytes: 32840
H, W = list(map(int, input().split()))

grid = []

for _ in range(H):
    grid.append(input())

Hx = [ (True if '#' in grid[i] else False) for i in range(H) ]
Wx = [ (True if '#' in [grid[j][i] for j in range(H)] else False) for i in range(W) ]

for i in range(H):
    flag = False
    for j in range(W):
        if(Hx[i] and Wx[j]):
            flag = True
            print(grid[i][j], sep='', end='')
    if flag:
        print()


Measured memory_bytes: 4468
#include <bits/stdc++.h>
#define fp(i, l, r) for(register int i = (l); i <= (r); ++i)
#define fd(i, l, r) for(register int i = (l); i >= (r); --i)
#define il inline
#define MAXN 105
using namespace std;

const int INF = 0xfffffff;

int a[5];

il int _abs(int a) {
	return a > 0 ? a : -a;
}

il int init() {
	int ans = 0;
	scanf("%d%d%d", &a[1], &a[2], &a[3]);
	sort(a+1, a+4);
	int minn = INF;
	fp(i, 2, 3) {
		fp(j, 1, i-1) {
			if(_abs(a[i] - a[j]) <= minn) {
				minn = _abs(a[i] - a[j]);
			}
		}
		ans += minn;
		minn = INF;
	}
	printf("%d", ans);
	return 0;
}

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

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll INF=1000000010;
const ll MOD=1000000007;
const ll LINF=4000000000000000000;
const long double EPS=1e-9;
typedef pair<int,int> P;
int x[100010];
int main(){
    int n,k;cin>>n>>k;
    int ans=INF;
    for(int i=0;i<n;i++){
        cin>>x[i];
    }
    for(int i=k-1;i<n;i++){
        ans=min(ans,abs(x[i])+(x[i]-x[i-k+1]));
        ans=min(ans,abs(x[i-k+1])+(x[i]-x[i-k+1]));
    }
    cout<<ans<<endl;
}   

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

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

using ll = long long;
using lb = long double;
using P = pair<ll,ll>;
using graph = vector<vector<ll>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

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

int main() {
  ll Q;
  cin>>Q;
  ll sb=0;
  ll sa = 0,lsa = 0;
  priority_queue<ll> l;
  priority_queue<ll,vector<ll>,greater<ll>> r;
  rep(i,Q){
    int q;
    cin>>q;
    if(i==0){
      ll a,b;
      cin>>a>>b;
      sa+=a;
      sb+=b;
      lsa += a;
      l.push(a);
      continue;
    }
    if(q==1){
      ll a,b;
      cin>>a>>b;
      sa += a;
      sb += b;
      int si = l.size()+r.size();
      if(si%2==0){
        ll L = l.top();
        if(L>=a) {
          lsa += a;
          l.push(a);
        }
        else {
          r.push(a);
          ll A = r.top();
          lsa += A;
          l.push(A);
          r.pop();
        }
      }
      if(si%2==1){
        ll A = l.top();
        if(A>=a) {
          l.push(a);
          lsa += a - A;
          r.push(A);
          l.pop();
        }
        else r.push(a);
      }
    }
    if(q==2){
      int si = l.size()+r.size();
      ll a = l.top();
      ll ans = sb + sa - lsa*2;
      if(si%2==1) ans += a;
      cout<<a<<" "<<ans<<endl;
    }
  }
}

Measured memory_bytes: 5004
import java.util.Scanner;

/**
 * @author munkhbat
 * AOJ 0189
 * warshall-floyd
 */
public class Main {
	private static int n;
	private static int[][] graph;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		while (true) {
			n = sc.nextInt();
			if (n == 0) {
				break;
			}
			
			graph = new int[11][11];
			for (int i = 0; i < 11; i++) {
				for (int j = 0; j < 11; j++) {
					graph[i][j] = 1 << 20;
				}
			}
			
			for (int i = 0; i < 11; i++) {
				graph[i][i] = 0;
			}
			
			int maxNum = Integer.MIN_VALUE;
			
			for (int i = 0; i < n; i++) {
				int s = sc.nextInt();
				int t = sc.nextInt();
				int c = sc.nextInt();
				
				maxNum = Math.max(maxNum, Math.max(s, t));
				
				graph[s][t] = c;
				graph[t][s] = c;
			}
			
			for (int k = 0; k <= maxNum; k++) {
				for (int i = 0; i <= maxNum; i++) {
					for (int j = 0; j <= maxNum; j++) {
						graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
					}
				}
			}
			
			int min = 1 << 20;
			int num = 0;
			for (int i = 0; i <= maxNum; i++) {
				int sum = 0;
				for (int j = 0; j <= maxNum; j++) {
					sum += graph[i][j];
				}
				if (min > sum) {
					num = i;
					min = sum;
				}
			}
			
			System.out.println(num + " " + min);
		}
		sc.close();
	}
}

Measured memory_bytes: 19688