akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner;

/**
 * Created by dizzyvn on 6/24/17.
 */
public class Main {
    final static long mod = (long) Math.pow(10, 9) + 7;

    public static long factorial(int n) {
        long res = 1;
        for (int i = 1; i <= n; i++) {
            res *= i;
            if (res > mod)
                res %= mod;
        }
        return res;
    }

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

        int n = sc.nextInt();
        int m = sc.nextInt();

        if (Math.abs(n-m) > 1) {
            System.out.println(0);
        } else {
            if (n == m)
                System.out.println((factorial(n) * factorial(m) * 2) % mod);
            else
                System.out.println((factorial(n) * factorial(m)) % mod);
        }
    }
}

Measured memory_bytes: 22740
while True:
  H,W=map(int, input().split())
  if H == 0 and W == 0:
      break  
  for i in range(H):
        for j in range(W):
          if  i == 0 or j == 0 or i == H - 1 or j == W - 1:
            print("#", end="")
          else                                            :
            print(".", end="")
        print()
  print()


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

using namespace std;

int64_t mod_pow(int64_t x, int64_t n, int64_t mod) {
  int64_t ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}

int main() {
  int M, N;
  cin >> M >> N;
  cout << mod_pow(M, N, 1e9 + 7) << endl;
}


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

typedef long long ll;

int main() {
  ll N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  vector<int> numbers;

  ll now = 1;
  ll count = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == (char)('0' + now)) {
      count++;
    } else {
      numbers.push_back(count);
      now = 1 - now;
      count = 1;
    }
  }
  if (count != 0) {
    numbers.push_back(count);
  }

  // 1-0-1-0-1
  if (numbers.size() % 2 == 0) {
    numbers.push_back(0);
  }

  ll ans = 0;
  ll add = 2 * K + 1;
  for (ll i = 0; i < numbers.size(); i += 2) {
    ll sum = 0;
    ll left = i;
    ll right = min(i + add, (ll)numbers.size());
    for (ll j = left; j < right; j++) {
      sum += numbers[j];
    }
    ans = max(ans, sum);
  }

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



Measured memory_bytes: 1020
K = input()

A = [1]
for k in range(K-1):
  for d in range(len(A)-1):
    if A[d]<9 and A[d]<=A[d+1]:
      A[d] += 1
      for i in range(d)[::-1]:
        A[i] = max(0, A[i+1]-1)
      break
  else:
    if A[-1]<9:
      A[-1]+=1
      for i in range(len(A)-1)[::-1]:
        A[i] = max(0, A[i+1]-1)
    else:
      for i in range(len(A)):
        A[i] = 0
      A += [1]
print "".join(map(str, A[::-1]))


Measured memory_bytes: 5764
#include <stdio.h>
#include <string.h>

#define rep(i, n) for(int i = 0; i < n; ++i)
#define ifb(N, n) if(N & (1 << n))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define V [0x1000] 
#define AV = malloc(0x8000) 
#define VZ(v) memset(v, 0, 0x1000)
#define addv(a,b) a = (unsigned long long)a + (unsigned long long)b
#define assv(a,b) a = (unsigned long long)b
#define vi 0x7fffffffffffffff

typedef void * v;
typedef unsigned long long ull;

int main(void) {
    int n,k;

    v aa V;
    VZ(aa);
    scanf("%d %d", &n, &k);
    rep(i, k) {
        int d;
        scanf("%d", &d);
        rep(j, d) {
            int a;
            scanf("%d", &a);
            aa[a - 1] = 1;
        }
    }

    v ans = 0;
    rep(i, n) {
        if (!aa[i]) ++ans;
    }

    printf("%d", ans);
}

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

typedef long long ll;
const ll MOD=1e9+7;

//factorial&inverse factorial
ll fc[200010];
ll ifc[200010];

ll add(ll x, ll y){return (x+y)%MOD;}
ll mlt(ll x, ll y){return (x*y)%MOD;}

ll power(int x, int n){
  ll ans=1;
  while(n>0){
    if(n%2!=0){ans=mlt(ans, x);}
    x=mlt(x,x);
    n/=2;
  }
  return ans;
}

ll inv(ll x){
  return power(x, MOD-2);
}

void fct(){
  fc[0]=1;
  for (int i=0;i<200009;++i){
    fc[i+1]=mlt(fc[i], i+1);
  }
}

void ifct(){
  ifc[200009]=inv(fc[200009]);
  for (int i=200009;i>=1;--i){
    ifc[i-1]=mlt(ifc[i], i);
  }
}

ll comb(int n, int k){
  if(n<0||k<0|| k>n)return 0;
  return mlt(fc[n], mlt(ifc[k], ifc[n-k]));
}

int main(){
  int N, A, B, C, D;cin>>N>>A>>B>>C>>D;

  ll dp[1010][1010]={};
  dp[N][A-1]=1;
  fct();ifct();

  for (int j=A;j<=B;++j){
    for (int i=0;i<=N;++i){
      dp[i][j]=dp[i][j-1];
      ll factor=1;
      for (int k=0;k++<C-1;){
        factor=mlt(factor, comb(i+k*j, j));
      }

      for (int k=C;k<=D;++k){
        if(i+k*j>N) break;
        factor=mlt(factor, comb(i+k*j, j));
        dp[i][j]=add(dp[i][j], mlt(dp[i+k*j][j-1], mlt(factor, ifc[k])));
      }
    }
  }

  cout << dp[0][B]<< endl;
  return 0;
}


Measured memory_bytes: 11392
class UnionFind:
    def __init__(self, N):
        # negative value: represents the root of a tree; its absolute value is the size of the tree
        # positive value: the parent's index
        self.vertices = [-1 for _ in range(N)]
        self.rank = [0] * N
        
    def find(self, v):
        if self.vertices[v] < 0: # v is a root
            return v
        else:
            # path compression: reconnect v to the root
            self.vertices[v] = self.find(self.vertices[v])
            return self.vertices[v]
        
    def union(self, u, v):
        s1 = self.find(u) # the root of the tree including vertex u
        s2 = self.find(v) # the root of the tree including vertex v
        
        if s1 == s2: # u and v is in the same tree
            return False
        
        if self.rank[s1] > self.rank[s2]: # the tree including u is taller
            self.vertices[s1] += self.vertices[s2] # update the size of the bigger tree
            self.vertices[s2] = s1
        else: # the tree including v is taller
            self.vertices[s2] += self.vertices[s1] # update the size of the bigger tree
            self.vertices[s1] = s2
            if self.rank[s1] == self.rank[s2]:
                self.rank[s2] += 1
        return True
        
    def is_connected(self, u, v):
        return self.find(u) == self.find(v)
    
    def size(self, v):
        return -self.vertices[self.find(v)]

N, M = map(int, input().split())
P = list(map(int, input().split()))
uf = UnionFind(N)
for _ in range(M):
    x, y = map(int, input().split())
    uf.union(x-1, y-1)
ans = 0
for i in range(N):
    if uf.is_connected(i, P[i]-1):
        ans += 1
print(ans)

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

int main() {

  int n;
  cin >> n;

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

  // +-+-+...の場合
  long long sum1 = 0;
  long long count1 = 0;

  for (int i = 0; i < n; i++) {
    sum1 += a[i];
    if (i % 2 == 0) {
      if (sum1 <= 0) {
        count1 += (1 - sum1);
        sum1 = 1;
      }
    } else {
      if (0 <= sum1) {
        count1 += (sum1 + 1);
        sum1 = -1;
      }
    }
  }

  // -+-+-...の場合
  long long sum2 = 0;
  long long count2 = 0;

  for (int i = 0; i < n; i++) {
    sum2 += a[i];
    if (i % 2 == 0) {
      if (0 <= sum2) {
        count2 += (sum2 + 1);
        sum2 = -1;
      }
    } else {
      if (sum2 <= 0) {
        count2 += (1 - sum2);
        sum2 = 1;
      }
    }
  }
  
  cout << min(count1, count2) << endl;

  return 0;

}

Measured memory_bytes: 640
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<(n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) (s.substr(f-1,t-f+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
struct Grid{ll x,y,t;};
struct Edge{ll to,cost;};
struct Graph{vector<vector<Edge>>E;int V;
  const ll Inf = llINF;const int MAX_V=201010;vector<ll>d;
  Graph(int n):E(n){d.resize(MAX_V);E.resize(n);V=n;}
  void init(){for(int i=0;i<MAX_V;i++)d[i]=Inf;}
  void add_edge(ll from,ll to,ll cost){E[from].pb({to,cost});}
};
ll n;
vi num;
bool check(ll po){
  ll sum=0;
  for(int i=0;i<=po;i++){
    sum+=num[i];
  }
  for(int i=po+1;i<n;i++){
    if(sum*2<num[i])return false;
    sum+=num[i];
  }
  return true;
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>n;
  num.resize(n);
  for(int i=0;i<n;i++)cin>>num[i];
  sort(ALL(num));
  ll le=-1,ri=n;
  while(ri-le>1){
    ll mid=(le+ri)/2;
    //cout<<le<<" "<<ri<<" "<<mid<<endl;
    if(check(mid)){
      ri=mid;
    }else{
      le=mid;
    }
    //cout<<le<<" "<<ri<<" "<<mid<<endl<<endl;
  }
  cout<<n-ri<<endl;
  return 0;
}


Measured memory_bytes: 1024
import java.util.Scanner;

public class Main {
	static class Building {
		int x, y;
		double cost;
		boolean mark;
		int preIndex;

		Building(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n, m;
		Building buil[];

		while (true) {
			n = sc.nextInt();
			if (n == 0) {
				break;
			}

			buil = new Building[n + 1];
			for (int i = 0; i < n; i++) {
				int number = sc.nextInt();
				int x = sc.nextInt();
				int y = sc.nextInt();
				buil[number] = new Building(x, y);
			}

			m = sc.nextInt();
			for (int i = 0; i < m; i++) {
				int start = sc.nextInt();
				int goal = sc.nextInt();
				for (int j = 1; j <= n; j++) {
					buil[j].cost = -1;
					buil[j].mark = false;
					buil[j].preIndex = -1;
				}
				buil[start].cost = 0;
				buil[start].mark = true;
				myDijkstra(buil, n, start, goal);

				if (buil[goal].preIndex == -1) {
					System.out.println("NA");
				} else {
					String s = "";
					String str = "";
					int index = goal;
					while (true) {
						str = index + s + str;
						if (index == start) {
							break;
						}
						index = buil[index].preIndex;
						s = " ";
					}
					System.out.println(str);
				}
			}
		}
	}

	public static void myDijkstra(Building buil[], int n, int start, int goal) {
		for (int i = 1; i <= n; i++) {
			double length = Math.hypot(buil[i].x - buil[start].x, buil[i].y - buil[start].y);
			if (length <= 50.0) {
				if (buil[i].cost == -1 || buil[start].cost + length < buil[i].cost) {
					buil[i].cost = buil[start].cost + length;
					buil[i].preIndex = start;
				}
			}
		}
		int minIndex = -1;
		for (int i = 1; i <= n; i++) {
			double length = Math.hypot(buil[i].x - buil[start].x, buil[i].y - buil[start].y);
			if (!buil[i].mark && buil[i].cost != -1 && (minIndex == -1 || buil[i].cost < buil[minIndex].cost)) {
				minIndex = i;
			}
		}
		if (minIndex != -1) {
			buil[minIndex].mark = true;
			myDijkstra(buil, n, minIndex, goal);
		}
	}
}

Measured memory_bytes: 12000
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#define MOD (1000000007l)
#define rep(i, n) for(long i = 0; i < n; i++)

using namespace std;

int main(void) {
  cout.precision(12);

  long N, A, B, C, D;
  cin >> N >> A >> B >> C >> D;
  A--;
  B--;
  C--;
  D--;
  string S;
  cin >> S;

  // 追い抜きが必要かどうか
  bool oinuki = (A < B and C > D) or (A > B and C < D);

  bool A_can = true;
  for (long i = A; i < C; i++) {
    if (S[i] == '#' and S[i+1] == '#') {
      A_can = false;
      break;
    }
  }

  bool B_can = true;
  for (long i = B; i < D; i++) {
    if (S[i] == '#' and S[i+1] == '#') {
      B_can = false;
      break;
    }
  }

  if ((not A_can) or (not B_can)) {
    cout << "No" << endl;
    return 0;
  }

  if (not oinuki) {
    cout << "Yes" << endl;
    return 0;
  }

  long E = max(A, B);
  long F = min(C, D);

  long oinuki_can = false;
  for (long i = E; i <= F; i++) {
    if (S[i-1] == '.' and S[i] == '.' and S[i+1] == '.') {
      oinuki_can = true;
      break;
    }
  }

  if (oinuki_can) {
    cout << "Yes" << endl;
    return 0;
  }

  cout << "No" << endl;

  return 0;
}


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

using namespace std;

int main() {
  vector<int> V(4);
  for (int i = 0; i < 4; i++) {
    char c;
    scanf("%c", &c);
    V[i] = c - '0';
  }

  int MM = V[2] * 10 + V[3];
  bool ok_yymm = (1 <= MM && MM <= 12);

  MM = V[0] * 10 + V[1];
  bool ok_mmyy = (1 <= MM && MM <= 12);

  if (ok_yymm && ok_mmyy) {
    cout << "AMBIGUOUS";
  } else if (ok_yymm) {
    cout << "YYMM";
  } else if (ok_mmyy) {
    cout << "MMYY";
  } else {
    cout << "NA";
  }

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}

int main(){
  int a[100010] = {};
  int n;
  cin >> n;
  rep(i,n){
    int tmp;
    cin >> tmp;
    a[tmp]++;
  }
  
  int ans = 0;
  
  rep(i,100000){
    if(i==0){
      chmax(ans,a[i]+a[i+1]);
    }else{
      chmax(ans,a[i]+a[i+1]+a[i-1]);
    }
  }
  
  cout << ans;
  
  return 0;
}

Measured memory_bytes: 640
let split_int s =
  let l = Str.split (Str.regexp_string " ") s
          |> List.map int_of_string |> List.nth
  in
  (l 0, l 1)
;;

let rec read () =
  let n = read_int () in
  if n > 0 then
    let rec loop a b= function
        0 -> (a, b)
      | i ->
         let (x,y) = read_line () |> split_int in
         if x > y then loop (a+x+y) b (i-1)
         else if x < y then loop a (b+x+y) (i-1)
         else loop (a+x) (b+y) (i-1)
    in
    let (a, b) = loop 0 0 n
    in
    Printf.printf "%d %d\n" a b;read ()
;;

let () = read ();;

Measured memory_bytes: 4412
import java.util.*;

public class Main {
    int n, m;
    ArrayList<ArrayList<Edge>> edges;

    public static void main(String[] args) {
        Main p = new Main();
        p.read();
        p.solve();
    }

    void read() {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        edges = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            edges.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int src = sc.nextInt();
            int trg = sc.nextInt();
            int cost = sc.nextInt();
            edges.get(src).add(new Edge(trg, cost));
            edges.get(trg).add(new Edge(src, cost));
        }
    }

    void solve() {
        boolean[] tree = new boolean[n];
        long ans = 0L;
        PriorityQueue<Edge> queue = new PriorityQueue<>(new Comparator<Edge>() {
            @Override
            public int compare(Edge edge, Edge t1) {
                return Integer.compare(edge.cost, t1.cost);
            }
        });
        tree[0] = true;
        for (Edge e: edges.get(0)) {
            queue.add(e);
        }
        while (!queue.isEmpty()) {
            Edge e = queue.poll();
            if (tree[e.to])
                continue;
            ans += e.cost;
            tree[e.to] = true;
            for (Edge _e: edges.get(e.to)) {
                if (!tree[_e.to])
                    queue.add(_e);
            }
        }
        System.out.println(ans);
    }
}

class Edge {
    int to, cost;
    Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
}

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

int main(){
  int c0 = 0, c1 = 0;
  string S; cin >> S;
  char str[S.length()], str1[S.length()];
  rep(i,S.length()){
    if(i%2 == 0){
      str[i] = '0';
      str1[i] = '1';
    }
    else{
      str[i] = '1';
      str1[i] = '0';
    }
  }
  rep(i,S.length()){
    if(str[i] != S[i]) c0++;
    if(str1[i] != S[i]) c1++;
  }
  cout << min(c0,c1) << endl;
  return 0;
}


Measured memory_bytes: 768
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,s1,s2,s3,max;
    scanf("%d%d",&a,&b);
    s1=a+b;
    s2=a*b;
    s3=a-b;
    max=s1>s2?s1:s2;
    max=max>s3?max:s3;
    printf("%d\n",max);

	return 0;
}



Measured memory_bytes: 128
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define fi first
#define sf scanf
#define se second
#define pf printf
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(x,y) memset((x),(y),sizeof(x))
#define fup(i,x,y) for(int i=(x);i<=(y);++i)
#define fdn(i,x,y) for(int i=(x);i>=(y);--i)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
using namespace std;
using namespace __gnu_pbds;

const int __=2e5+5;

int a[__];
vector<pii>G;
map<int,int>vis;

int main()
{
    for(int n;~sf("%d",&n);)
    {
        fup(i,1,n)
        {
            sf("%d",&a[i]);
            ++vis[a[i]];
        }
        //pf("%d\n",n);
        map<int,int>::iterator it;
        for(it=vis.begin();it!=vis.end();++it)
            G.pb(*it);
//        for(pii x:G)
//            pf("%d %d\n",x.fi,x.se);
        int ans=0;
        for(int i=sz(G)-1;i>=0;--i)
            if(G[i].se)
            {
                for(int j=(1<<30);j;j>>=1)
                {
                    pii &x=*lower_bound(all(G),mp(j-G[i].fi,0));
                    if(x.fi+G[i].fi!=j)continue;
                    int minn;
                    if(G[i].fi!=x.fi)
                        minn=min(G[i].se,x.se);
                    else minn=x.se/2;
                    ans+=minn;
                    G[i].se-=minn;
                    x.se-=minn;
                }
            }
        pf("%d\n",ans);
        G.clear();
        vis.clear();
    }
    return 0;
}


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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

int main() {
source:
    ll n;
    cin >> n;
    ll a[n], b[n], c[n];
    rep(i, n) { cin >> a[i]; }
    rep(i, n) { cin >> b[i]; }
    rep(i, n) { cin >> c[i]; }

    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);

    ll b_table[n];
    rep(i, n) { b_table[i] = n - (upper_bound(c, c + n, b[i]) - c); }
    ll b_sum[n + 1];
    b_sum[n] = 0;
    repb(i, n) { b_sum[i] = b_sum[i + 1] + b_table[i]; }
    ll ans = 0;
    rep(i, n) { ans += b_sum[lower_bound(b, b + n, a[i] + 1) - b]; }
    cout << ans << endl;
    return 0;
}



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

#define MAX 100

//prototype
void push(int);
int pop();
void isEmpty();
void isFull();

//global variables
int S[MAX + 1]; //opeland
int top=1;

void push(int x){
  //overflow
  isFull(); //full??????exit()

  S[top] = x;

//  printf("push: %d\n",x);

  top++;
}

int pop(){
  int x;
  
  top--; //?????????top?????????????????¨

  //underflow
  isEmpty(); //empty??????exit()

  x = S[top];
//  printf("pop: %d\n",x); //@@

  return x;
}

void isEmpty(){
  if(top==0){
    printf("Error: Underflow\n");
    exit(1);
  }
}

void isFull(){
  if(top>MAX){
    printf("Error: Overflow\n");
    exit(1);
  }
}


//MAIN
int main(){
  int n, x_int;
  char x[1000000];

  //sentinel
  S[0] = 0;

  //???????????\???(push) & ???????????????????¨????
  while(scanf("%s", x)!=EOF){
    if(x[0]>47){ //??°?????????(????????????????????§???47????????§???????????°??°?????¨????????§??????)
      x_int = atoi(x); //???????????????int????????????
      push(x_int);
    }else if(x[0]=='+'){ //?¶???????
      push(pop() + pop());
    }else if(x[0]=='-'){ //?????????
      int p1 = pop();
      int p2 = pop();
      push(p2 - p1);
    }else if(x[0]=='*'){ //?????????
      push(pop() * pop());
    }else{
      printf("???????????????????????????????????\??????????????????.\n");
      exit(1);
    }
  }
  
  printf("%d\n", pop());

  return 0;
}

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

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int,int>
#define PSI pair<string,int>
#define PIS pair<int,string>
#define PLI pair<long long int,int>
#define PLL pair<long long int,long long int>
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define MLI map<long long int,int>
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)





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






int main()
{
    FAST();

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    MSI mp;
    int a,b;
    string x,y,z;
    cin >> x >> y >> a >> b;
    mp[x]=a,mp[y]=b;
    cin >> z;
    --mp[z];
    cout << mp[x] << ' ' << mp[y];
    return 0;
}


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

struct Node
{
  int p, l, r;
};

vector<Node> T(MAX);
vector<int> D(MAX);

void print(int u)
{
  cout << "node " << u << ": ";
  cout << "parent = " << T.at(u).p << ", ";
  cout << "depth = " << D.at(u) << ", ";

  if (T.at(u).p == -1)
    cout << "root, ";
  else if (T.at(u).l == -1)
    cout << "leaf, ";
  else
    cout << "internal node, ";

  cout << "[";
  for (int i = 0, c = T.at(u).l; c != -1; i++, c = T.at(c).r)
  {
    if (i)
      cout << ", ";
    cout << c;
  }
  cout << "]" << endl;
}

void rec(int u, int p)
{
  D.at(u) = p;
  if (T.at(u).r != -1)
  {
    rec(T.at(u).r, p);
  }
  if (T.at(u).l != -1)
  {
    rec(T.at(u).l, p + 1);
  }
}

int main()
{
  int n, id, k, c, l, r;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    T.at(i).p = T.at(i).l = T.at(i).r = -1;
  }
  for (int i = 0; i < n; i++)
  {
    cin >> id >> k;
    for (int j = 0; j < k; j++)
    {
      cin >> c;
      if (j == 0)
      {
        T.at(id).l = c;
      }
      else
      {
        T.at(l).r = c;
      }
      l = c;
      T.at(c).p = id;
    }
  }
  for (int i = 0; i < n; i++)
  {
    if (T.at(i).p == -1)
      r = i;
  }

  rec(r, 0);
  for (int i = 0; i < n; i++)
    print(i);
}


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

const int MAX_SEG_SIZE = 100010;

int N, M, Q;
vector<int> m;
int sum = 0;
int dp[MAX_SEG_SIZE];
int dp2[MAX_SEG_SIZE];

int solve(int length){
  int loss = sum;
  // loss += m[0] - 1;
  loss -= dp[length-1];
  loss -= (length-1) * dp2[length];
  return loss;
}

int main(void){
  cin >> N >> M;
  m = vector<int>(M);
  for(auto& _m: m){
    cin >> _m;
    _m--;
  }


  sum = N - M;
  int num_of_seg = M+1;
  vector<int> seg_len(num_of_seg);
  for(int i = 0; i < (int)seg_len.size(); i++){
    int next = (i == num_of_seg-1 ? N : m[i]);
    int prev = (i == 0 ? 0 : m[i - 1]);
    seg_len[i] = next - prev - 1;
  }
  // dp[i] := 長さi以上の区間の数
  memset(dp, 0, sizeof(int) * MAX_SEG_SIZE);
  memset(dp2, 0, sizeof(int) * MAX_SEG_SIZE);
  for(int i = 1; i < num_of_seg; i++){
    dp[seg_len[i]] += seg_len[i];
    dp2[seg_len[i]] += 1;
    // dp[seg_len[i]] += seg_len[i];
  }
  for(int i = 1; i < MAX_SEG_SIZE; i++){
    dp[i] += dp[i-1];
  }
  for(int i = MAX_SEG_SIZE-2; i >= 0; i--){
    dp2[i] += dp2[i+1];
  }

  ////////////
  // for(int i = 0;  i< N; i++){
  //   cout << dp[i] << " ";
  // }
  // cout << endl;
  // cout << "-----" << endl;
  // for(int i = 0; i < N; i++){
  //   cout << dp2[i] << endl;
  // }
  // cout << endl;
  ////////////

  cin >> Q;
  const int RMAX = 100001;
  for(int loop = 0; loop < Q; loop++){
    int loss; cin >> loss;
    int l = 0, r = RMAX;
    while(l+1 < r){
      int med = (l + r) / 2;
      int v = solve(med);
      // cout << v << " " << med << endl;
      if(v <= loss){
        // shorter
        r = med;
      }else{
        // longer
        l = med;
      }
    }
    if(r == RMAX) cout << -1 << endl;
    else cout << r << endl;
  }
}



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

int main(){
  int n;
  cin >>n;
  int cnt = 0;
  for(int i = 1; i < n+1; i++){
    string s = to_string(i);
    if(s.size()%2 == 1) cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Measured memory_bytes: 256
// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <iostream>
#include <queue>
#include <string>
#include <math.h>
#define mid(l,r) (( l + r ) / 2)
#define lowbit(x) (( x & ( - x )))
#define lc(root) ((root * 2))
#define rc(root) ((root * 2 + 1))
#define me(array,x) (memset( array , x , sizeof( array ) ) )
typedef long long LL;
using namespace std;
const LL inf = 10000000;
const int mod = 1e9 + 7;
const int maxn = 1000+10;
double a[maxn];

LL POW(LL a,LL b,LL p)
{
    LL ans = 1;
    while(b){
        if(b&1)
            ans = (ans * a) % p;
        a = (a * a) % p;
        b>>=1;
    }
    return ans;
}

int main()
{
    int n;
    scanf("%d",&n);
    double ans = 0;
    for(int i=1;i<=n;i++){
        double x;scanf("%lf",&x);
        ans += 1.0/x;
    }
    ans = 1.0/ans;
    printf("%f\n",ans);
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
// ceil() 切り上げ, floor() 切り捨て
// next_permutation(all(x))

int		main(void)
{
	string N;
	cin >> N;
	int len = N.size();
	int ans;
	int i = 1;
	while (i < len && N[i] == '9')
		i++;
	if (i == len)
		ans = 9*(len-1) + N[0]-'0';
	else if (len == 1)
		ans = N[0]-'0';
	else if (N[0] == '1' && N[len-1] != '9')
		ans = 9*(len-1);
	else
		ans = 9*(len-1) + N[0]-'0'-1;
	cout << ans << endl;
}



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

int main() {

  long long N, K;
  cin >> N >> K;

  if (N % K == 0){
    cout << 0 << endl;
    return 0;
  }

  if (N <= abs((N % K) - K)) {
    cout << N << endl;
  } else {
    cout << abs((N % K) - K) << endl;
  }
  
  return 0;

}

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

using namespace std;
typedef long long ll;

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

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

  ll ans = 0;
  for (int i = 1; i < n; i++) {
    ans += min((x[i] - x[i - 1]) * a, b);
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

static long Gcd(long m, long n)
{
	long temp;
	while (m % n != 0)
	{
		temp = n;
		n = m % n;
		m = temp;
	}
	return n;
}

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}


map< int, int > prime_factor(int n) {
  map< int, int > ret;
  for(int64_t i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}


signed main(){
    int A, B;cin>>A>>B;

    int gcd = Gcd(A,B);

/*    vec_int factors;

    for(int i=1;i*i<=gcd;i++){
        if(gcd%i==0){
            factors.push_back(i);
            factors.push_back(gcd/i);
        }
    }
    sort(factors.begin(), factors.end());
    */

   map<int, int> pfs = prime_factor(gcd);

   cout<<pfs.size()+1<<endl;




    

    return 0;
}

Measured memory_bytes: 3548
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;
    auto s = std::accumulate(xs.begin(), xs.end(), 0LL,
                             [](auto a, auto b) { return a ^ b; });

    lint buf = 0;
    for (int i = 0; i < 60; ++i) {
        if (((s >> i) & 1) == 0) continue;

        buf += (1LL << i);
        s &= ~(1LL << i);
        for (auto& x : xs) x &= ~(1LL << i);
    }

    lint ans = 0;
    while (!xs.empty()) {
        int i = std::max_element(xs.begin(), xs.end()) - xs.begin();
        std::swap(xs[i], xs.back());

        auto y = xs.back();
        if (y == 0) break;

        for (auto& x : xs) x = std::min(x, x ^ y);
        ans = std::max(ans, ans ^ y);
        xs.pop_back();
    }

    std::cout << ans + (s ^ ans) + buf << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}


Measured memory_bytes: 1024
import java.util.*;

public class Main{
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    int n=sc.nextInt();
	    int[] a=new int[n];
	    int max=0;
	    for(int i=0;i<n;i++) {
	    	a[i]=sc.nextInt();
	    	max=Math.max(max, a[i]);
	    }
	    int q=sc.nextInt();
	    int[] b=new int[q];
	    int[] c=new int[q];
	    for(int i=0;i<q;i++) {
	    	b[i]=sc.nextInt();
	    	c[i]=sc.nextInt();
	    	max=Math.max(max, b[i]);
	    	max=Math.max(max, c[i]);
	    }
	    long sum=0;
	    int[] count =new int[max];
	    for(int i=0;i<n;i++) {
	    	count[a[i]-1]++;
	    	sum+=a[i];
	    }
	    for(int i=0;i<q;i++) {
	    	if(b[i]>max | c[i]>max)continue;
	    	int num=count[b[i]-1];
	    	count[b[i]-1]=0;
	    	count[c[i]-1]+=num;
	    	sum+=(c[i]-b[i])*num;
	    	System.out.println(sum);
	    }
	    
	}
}


Measured memory_bytes: 60568
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;

int main()
{
	int n;
	cin>>n;
	vector<P> xv(n);
	for(int i=0; i<n; i++) cin>>xv[i].first>>xv[i].second;
	sort(xv.begin(), xv.end());
	int v[200020], mx[200020], mn[200020];
	mx[0]=-1, mn[n]=1e9+7;
	for(int i=0; i<n; i++){
		v[i]=xv[i].second;
		mx[i+1]=max(mx[i], v[i]);
	}
	for(int i=n-1; i>=0; i--){
		mn[i]=min(mn[i+1], v[i]);
	}
	int l[200020], r[200020];
	int mxl[200020];
	fill(mxl, mxl+n, -1);
	for(int i=0; i<n; i++){
		l[i]=lower_bound(mx, mx+n+1, v[i])-mx-1;
		r[i]=upper_bound(mn, mn+n+1, v[i])-mn-1;
		mxl[r[i]]=max(mxl[r[i]], l[i]);
	}
	ll dp[200020]={};
	dp[0]=1;
	ll s=1;
	int li=0;
	for(int i=0; i<n; i++){
		dp[i+1]=s;
		s=(s+s)%MOD;
		for(int j=li; j<=mxl[i]; j++){
			(s+=MOD-dp[j])%=MOD;
			dp[j]=0;
		}
		li=max(li, mxl[i]+1);
	}
	cout<<s<<endl;
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i, n) for(ll i=1;i<=(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define INF 1000000000

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x((x% mod + mod) % mod) {}
	mint operator-() const { return mint(-x); }
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};

mint sum0[300001][61];
mint sum1[300001][61];

void solve_abc_d() {
    ll n;
    cin >> n;

	V a(n);
	rep(i, n) {
		cin >> a[i];
	}

	mint ans = 0;
	mint x = 1;
	ll d;
	rep(i, 61) {
		rep(j, n) {
			d = (a[j] >> i) & 1;
			if (d == 0) {
				sum0[j + 1][i] = sum0[j][i] + 1;
				sum1[j + 1][i] = sum1[j][i];
			}
			else {
				sum0[j + 1][i] = sum0[j][i];
				sum1[j + 1][i] = sum1[j][i] + 1;
			}

		}
		rep(j, n) {
			d = (a[j] >> i) & 1;
			if (d == 1) {
				ans += (sum0[n][i] - sum0[j][i]) * x;
			}
			else {
				ans += (sum1[n][i] - sum1[j][i]) * x;
			}

		}
		x *= 2;
	}

	
	cout << ans.x << "\n";
}

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

	solve_abc_d();

	return 0;
}


Measured memory_bytes: 288512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
#define TO(x,t,f) ((x)?(t):(f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define FILV(x,a) fill(x.begin(),x.end(),a)
#define FILA(ar,N,a) fill(ar,ar+N,a)
#define NEXTP(x) next_permutation(x.begin(),x.end())
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define pb push_back
#define pri(aa) cout<<(aa)<<endl
const ll INF=1e9+7;
const ll N = 200003;
ll a,b,c,d;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>a>>b>>c>>d;
	pri(max(a*b,c*d));
	return 0;
}


Measured memory_bytes: 256
#include<unordered_set>
#include<unordered_map>
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<stack>
using namespace std;

const static string ASCII("abcdefghijklmnopqrstuvwxyz");

int N;

void rec(string s, char mx) {
    if(s.size()==N)
        printf("%s\n", s.c_str());
    else {
        for(char c='a'; c<=mx; c++) {            
            rec(s + c, c==mx ? (char)(mx+1) : mx);
        }
    }
}

int main() {    
    cin>>N;
    rec("", 'a');
}

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

ll gcd(ll a,ll b){
  if(b == 0) return a;
  return gcd(b, a % b);
}

using Graph = vector<vector<int>>;

ll calc(ll a, ll b, ll p){//aのb乗をpで割った余り
  if(b == 0) return 1;

  else if(b%2 == 0){
    ll d = calc(a,b/2,p);
    return (d*d)%p;
  }
  else if(b%2 == 1){
    return (a * calc(a,b-1,p))%p;
  }
}

typedef pair<int,int> P;

bool is_prime(int n){//nが素数であるかどうか判定 iで割り切れると素数でない
    if(n == 1) return false;//1は例外
    for(int i = 2;i*i <= n;i++){
        if(n%i == 0) return false;
    }
    
    return true;
}
int factorial(int a){
  int res = 1;
  while(a){
    res *= a;
    a--;
  }
  return res;
}

int main(){
  int K;
  cin >> K;
  vector<int> v ={1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
  cout << v[K-1] << endl;
}

Measured memory_bytes: 256
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        h: usize, w: usize,
        a: [chars; h],
    }
    const INF: i64 = 1 << 40;
    let mut mia = INF;
    let mut mib = INF;
    let mut maa = -INF;
    let mut mab = -INF;
    for i in 0..h {
        for j in 0..w {
            if a[i][j] != 'B' { continue; }
            let a = i as i64 + j as i64;
            let b = i as i64 - j as i64;
            mia = min(mia, a);
            maa = max(maa, a);
            mib = min(mib, b);
            mab = max(mab, b);
        }
    }
    puts!("{}\n", max(mab - mib, maa - mia));
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}



Measured memory_bytes: 9060
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    ans = 0
    for i in range(N):
        if i==A[A[i]-1]-1:
            ans += 1
    print(ans//2)


if __name__ == '__main__':
    main()


Measured memory_bytes: 12392
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

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

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

ll mod_pow(ll a, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * a%m;
		a = a * a%m; n >>= 1;
	}
	return res;
}

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

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 10000;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

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


void solve() {
	int n, m; cin >> n >> m;
	cout << n * (n-1)/2 + m * (m-1)/2 << endl;
}


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

Measured memory_bytes: 384
#include <iostream>
#include <cmath>]
#include <string>
#include <map>
#include <queue>
using namespace std;
#define N (3)
#define N2 (9)

struct Puzzle{
    int f[N2];
    int space;
    string path;
    
    bool operator<(const Puzzle &p)const{
        for(int i=0;i<N2;i++){
            if(f[i]==p.f[i])continue;
            return f[i]>p.f[i];
        }
        return false;
    }
};

static const int dx[4]={-1,0,1,0};
static const int dy[4]={0,-1,0,1};
static const char dir[4]={'u','l','d','r'};

bool isTarget(Puzzle p){
    for(int i=0;i<N2;i++){
        if(p.f[i]!=(i+1))return false;
    }
    return true;
}

string bfs(Puzzle s){
    queue<Puzzle> Q;
    map<Puzzle, bool> V;
    Puzzle u,v;
    s.path="";
    Q.push(s);
    V[s]=true;
    
    while(!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u))return u.path;
        int sx=u.space/N;
        int sy=u.space%N;
        for(int r=0;r<4;r++){
            int tx=sx+dx[r];
            int ty=sy+dy[r];
            if(tx<0||ty<0||tx>=N||ty>=N)continue;
            v=u;
            swap(v.f[u.space],v.f[tx*N+ty]);
            v.space=tx*N+ty;
            if(!V[v]){
                V[v]=true;
                v.path+=dir[r];
                Q.push(v);
            }
        }
    }
    return "unsolvable";
}

int main(void){
    Puzzle in;
    for(int i=0;i<N2;i++){
        cin>>in.f[i];
        if(in.f[i]==0){
            in.f[i]=N2;
            in.space=i;
        }
    }
    string ans=bfs(in);
    cout<<ans.size()<<endl;
    return 0;
}



Measured memory_bytes: 30632
//shan61916
#include <bits/stdc++.h>
  using namespace std;
typedef   long long ll;
typedef   unsigned long long ull ;
typedef   double dll ;

#define   mp make_pair
#define   pb push_back
#define   ff first 
#define   ss second
#define   IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define   endl "\n" 
#define   pll pair<ll, ll>
#define   all(x) x.begin(), x.end()
#define   vll vector<ll> 
const int MAX = 2e3 + 3;
const ll MOD = 1e15;
ll ncr[MAX][MAX];

void pascal() {
	for (int i = 0; i < MAX; ++i) {
		ncr[i][0] = ncr[i][i] = 1;
		for (int j = 1; j < i; ++j) {
			ncr[i][j] = ncr[i-1][j-1] + ncr[i-1][j];
			if (ncr[i][j] >= MOD) ncr[i][j] -= MOD;
		}
	}
}
int main(){
 IOS
#ifdef SHAN
    freopen("input.txt" , "r" , stdin);  
#endif
  ll n;
  cin >> n;
  pascal();
  vll a(n);
  for(auto &x : a) cin >> x;
  sort(all(a)); 
  ll tot = ncr[n][3];
  // cout << tot << endl;
  for(ll i = 0; i < n-2; i++) {
  	 for(ll j = i+1; j < n-1; j++) {
       ll k = upper_bound(a.begin()+j+1, a.end(), a[i]+a[j]-1) - a.begin();
       if(k < n) tot-= (n-k);
  	 }
  }
  cout << tot << endl;
  return 0;
} //good night.

Measured memory_bytes: 30208
def main():
    s = input().strip()
    k = int(input().strip())

    ss = []
    for i in range(0, len(s)):
        for j in range(i, i + k + 1):
            if i == j:
                continue
            if len(ss) == 6:
                if ss[-1] > s[i:j]:
                    ss[-1] = s[i:j]
                    ss = list(set(ss))
                    ss.sort()
            else:
                ss.append(s[i:j])
                ss = list(set(ss))
                ss.sort()

    print(ss[k-1])

if __name__ == '__main__':
    main()


Measured memory_bytes: 41708