akhauriyash/Code-Regression huggingface dataset:
size,num = map(int,input().split())

ref = [list(map(int,input().split())) for i in range(num)]

first = -1
second = -1
third = -1

jud = 0


for kk in range(num):
    if ref[kk][0] == 1:
        if first == -1 or first == ref[kk][1]:
            first = ref[kk][1]
        else:
            jud = 1
    elif ref[kk][0] == 2:
        if second == -1 or second == ref[kk][1]:
            second = ref[kk][1]
        else:
            jud = 1
    elif ref[kk][0] == 3:
        if third == -1 or third == ref[kk][1]:
            third = ref[kk][1]
        else:
            jud = 1

if first == 0 and size != 1:
    jud = 1

if jud == 1:
    print(-1)
else:
    if first == -1:
        if size > 1:
            first =1
        else:
            first =0
    if second == -1:
        second =0
    if third == -1:
        third =0

    if size == 1:
        print(first)
    elif size == 2:
        print(str(first*10 + second))
    else:
        print(str(first*100 + second*10 + third))


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

long long N;
long long a[105];

int main(){
	cin >> N;
	long long ans = 0;
	for(long long i = 1; i <= N; i++) {
		long long a;
		cin >> a;
		if(i%2 == 1 && a % 2 == 1) {
			ans++;
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 3628
package main

import (
	"bufio"
	"fmt"
	"os"
	"reflect"
	"strconv"
)

const (
	maxBufSize = 1024 * 1024
)

var (
	sc = func() *bufio.Scanner {
		sc := bufio.NewScanner(os.Stdin)
		buf := make([]byte, 1)
		sc.Buffer(buf, maxBufSize)
		sc.Split(bufio.ScanWords)
		return sc
	}()
	out = bufio.NewWriter(os.Stdout)
)

func solve(n int64, as []int64) int64 {
	p := int64(0)
	ret := int64(0)
	for  {
		a :=  as[p] - 1
		if (a < 0) {
			return -1
		}
		ret ++
		if (a == 1) {
			break
		}
		as[p] = 0
		p = a
	}
	return ret
}

func main() {
	defer out.Flush()

	n := nextInt()
	as := nextInts(n)
	res := solve(n, as)
	fmt.Fprintln(out, res)
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextStringAsBytes() []byte {
	sc.Scan()
	return []byte(sc.Text())
}

func nextInt() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int64) []int64 {
	ret := make([]int64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}

func nextFloats(n int64) []float64 {
	ret := make([]float64, n)
	for i := int64(0); i < n; i++ {
		ret[i] = nextFloat()
	}
	return ret
}
func nextStrings(n int64) []string {
	ret := make([]string, n)
	for i := int64(0); i < n; i++ {
		ret[i] = next()
	}
	return ret
}

func containStr(arr []string, target string) bool {
	for _, v := range arr {
		if v == target {
			return true
		}
	}
	return false
}

func PrintOut(src interface{}, joinner string) {
	switch reflect.TypeOf(src).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(src)
		for idx := 0; idx < s.Len(); idx++ {
			fmt.Fprintf(out, "%v", s.Index(idx))
			if idx < s.Len()-1 {
				fmt.Fprintf(out, "%s", joinner)
			} else {
				fmt.Fprintln(out)
			}
		}
	default:
		fmt.Fprintln(out, "orz")
	}
}

// mapからkeysとvaluesを返す
func splitKeyValue(m map[interface{}]interface{}) (keys, values []interface{}) {
	switch reflect.TypeOf(m).Kind() {
	case reflect.Map:
		s := reflect.ValueOf(m)
		for _, v := range s.MapKeys() {
			keys = append(keys, v)
			values = append(values, s.MapIndex(v))
		}
	default:
		fmt.Fprintln(out, "orz")
	}
	return
}


Measured memory_bytes: 3216
#include <stdio.h>
int main() {
	int n;
	char s[200000];
	scanf("%d%s", &n, s);
	int ans = 0;
	for (int i = 0; i < n; i++) {
		if (s[i] == '.') {
			ans++;
		}
	}
	int tmp = ans;
	for (int i = 0; i < n; i++) {
		if (s[i] == '.') {
			tmp--;
		} else {
			tmp++;
		}
		if (ans > tmp) {
			ans = tmp;
		}
	}
	printf("%d\n", ans);
	return 0;
}

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

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'

// n回繰り返し
#define rep(i,n) for(int i = 0; i < ((int)(n)); i++)   // 0-indexed昇順
#define rep1(i,n) for(int i = 1; i <= ((int)(n)); i++) // 1-indexed昇順
#define rrep(i,n) for(int i = ((int)(n)-1); i >= 0; i--)  // 0-indexed降順
#define rrep1(i,n) for(int i = ((int)(n)); i >= 1; i--)   // 1-indexed降順
#define all(x) (x).begin(),(x).end()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vii;  // 2次元配列
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int, int> PII;

template<typename T>
using vec = vector<T>;
template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

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

const ld Pi = std::acos(-1.0L);
const ll infll = (1LL<<62)-1;
const int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;


/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    int _StartTime_ = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);   //cin cout 高速化
    // cout << fixed << setprecision(15);

   int n; cin >> n;
   vi a(n+1); rep1(i,n) cin >> a[i];
    vec<bool> b(n+1);
    for(int i = n; i >= 1; --i) {
        int cnt = 0;
        for(int j = i*2; j <= n; j += i) {  // iの1倍２倍３倍...と見ていく
            if(b[j]) ++cnt;
        }
        if(cnt%2 != a[i]) b[i] = 1;
    }

    int cnt = 0;
    vi ans;
    for(int i = 1; i <= n; ++i) if(b[i]) {
        ans.push_back(i);
        ++cnt;
    }
    cout << cnt << '\n';
    for(int i = 0; i < ans.size(); ++i) cout << ans[i] << (i==ans.size()-1 ? '\n' : ' ');

    // printf("ExecutionTime: %d /ms\n",1000*(int)((clock()-_StartTime_)/CLOCKS_PER_SEC));
}

Measured memory_bytes: 2424
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
"""
閉路をmodを取って出力
閉路になっていない(回帰するなら)ならそこを出力
"""
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))


visited = [-1]*n #modを取った時にそこにたどり着く
visited[0] = 0
now = 0
count = 1
mod = -1
lim = -1
while True:
    now = lst1[now] -1
    if visited[now] != -1: #閉路
        mod = count - visited[now]
        lim = visited[now]
        break
    visited[now] = count

    count += 1
    
if k <= lim:
    for i in range(n):
        if visited[i] == k:
            print(i+1)
            exit()
else:
    res = (k-lim)%mod
    for i in range(n):
        if visited[i]-lim == res:
            print(i+1)
            exit()

Measured memory_bytes: 96820
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;

#define MAX_N 300
#define INF 1e11
typedef long long int ll;
ll d1[MAX_N][MAX_N];
ll d2[MAX_N][MAX_N];

void init(){
	rep(i,MAX_N){
		rep(j,MAX_N){
			d1[i][j] = INF;
			d2[i][j] = INF;
		}
		d1[i][i] = 0;
		d2[i][i] = 0;
	}
}

void warshall_floyd1(int n){
	rep(k,n){
		rep(i,n){
			rep(j,n){
				d1[i][j] = min(d1[i][j],d1[i][k] + d1[k][j]);
			}
		}
	}
}

void warshall_floyd2(int n){
	rep(k,n){
		rep(i,n){
			rep(j,n){
				d2[i][j] = min(d2[i][j],d2[i][k] + d2[k][j]);
			}
		}
	}
}

int main()
{
	init();
	int n,m,l;
	cin>>n>>m>>l;
	rep(i,m){
		int A,B,C;
		cin>>A>>B>>C;
		A = A -1;
		B = B -1;
		d1[A][B] = C;
		d1[B][A] = C;
	}
	warshall_floyd1(n);
	rep(i,n){
		rep(j,n){
			if(d1[i][j] <= l) d2[i][j] = 1;
		}
	}
	warshall_floyd2(n);
	int Q;
	cin>>Q;
	rep(i,Q){
		int s,t;
		cin>>s>>t;
		s = s - 1;
		t = t - 1;
		if(d2[s][t] == INF){
			cout<<-1<<endl;
		}else{
			cout<<d2[s][t]-1<<endl;
		}
	}
	return 0;
}



Measured memory_bytes: 1920
from collections import defaultdict
import sys
mod = int(input())
d = defaultdict(int)
s = 7
c = 1
s %= mod
while(True):
    if s == 0:
        break
    if s in d:
        print(-1)
        sys.exit()
    d[s] = 1
    s = (s*10 + 7) % mod
    c += 1

print(c)

Measured memory_bytes: 158584
//↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n)  n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double pi = acos(-1);
const ll SIZE = 1 << 17;
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 }, alp[30];
ll fac[200005], finv[200005], inv[200005];
vector<ll>dij;
struct edge { ll to, cost; };
vector<vector<edge> >G;

ll mod_pow(ll a, ll b) {
	ll res = 1, mul = a;
	for (int i = 0; i < 31; ++i) {
		if (b >> i & 1) {
			res *= mul;
			res %= MOD;
		}
		mul = (mul * mul) % MOD;
	}

	return res;
}

void addedge(int from, int to, int cost) {
	G[from].push_back({ to,cost });
	G[to].push_back({ from,cost });
}


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

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

template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }

template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v) {
	for (auto &e : t) fill_v(e, v);
}

template<typename T>
void outp(vector<T>v) {
	for (int i = 0; i < v.size(); ++i) {
		cout << v[i];
		if (i != v.size() - 1) { cout << " "; }
	}
}

//↑template↑




int main() {
	int n, k[25], ans = 0;
	cin >> n;
	for (int i = 0; i < n; ++i) { cin >> k[i]; }
	for (int i = 2; i < n; ++i) {
		if ((k[i] > k[i - 1] && k[i - 1] > k[i - 2]) || (k[i] < k[i - 1] && k[i - 1] < k[i - 2])) { ++ans; }
	}

	cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int n,i,ans;cin >> n;
  int a[n];ans = 0;
  for(i=0;i<n;i++) cin >> a[i];
  for(i=1;i<n-1;i++){
    if ((a[i] > a[i+1] && a[i] < a[i-1])||(a[i] < a[i+1] && a[i] > a[i-1])) ans += 1;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
h,w,n = map(int,input().split())
sr,sc = map(int,input().split())
S = input()
T = input()
hor = [1,w]
ver = [1,h]
flag1 = True
flag2 = True
for i in range(n):
    k = T[n-i-1]
    if (hor[0] == 1 and hor[1] == w):
        None
    else:
        if k == 'L' and hor[1] != w:
            hor[1] += 1
        elif k == 'R' and hor[0] != 1:
            hor[0] -= 1
    k = S[n-i-1]
    if (hor[0] == w and k == 'R') or (hor[1] == 1 and k == 'L'):
        flag1 = False
        break
    else:
        if k == 'L':
            hor[0] += 1
        elif k == 'R':
            hor[1] -= 1
    if hor[0] > hor[1]:
        flag1 = False
        break
for i in range(n):
    k = T[n-i-1]
    if (ver[0] == 1 and ver[1] == h):
        None
    else:
        if k == 'U' and ver[1] != h:
            ver[1] += 1
        elif k == 'D' and ver[0] != 1:
            ver[0] -= 1
    k = S[n-i-1]
    if (ver[0] == h and k == 'D') or (ver[1] == 1 and k == 'U'):
        flag2 = False
        break
    else:
        if k == 'U':
            ver[0] += 1
        elif k == 'D':
            ver[1] -= 1
    if ver[0] > ver[1]:
        flag2 = False
        break
if flag1 == True and flag2 == True:
    if hor[0] <= sc <= hor[1]:
        if ver[0] <= sr <= ver[1]:
            print('YES')
        else:
            print('NO')
    else:
        print('NO')
else:
    print('NO')


Measured memory_bytes: 47716
def mapint_inp():
    return map(int, input().split())

def intinp():
    return int(input())

N = intinp()
a = mapint_inp()

def get_color(x):
    c = None
    if 399 >= x: c = "Gray"
    elif 799 >= x: c = "Blown"
    elif 1199 >= x: c = "Green"
    elif 1599 >= x: c = "Sky"
    elif 1999 >= x: c = "Blue"
    elif 2399 >= x: c = "Yellow"
    elif 2799 >= x: c = "Orange"
    elif 3199 >= x: c = "Red"
    else:
        c = "Any"
    return c

color_set = set()
num_of_any = 0

for a_i in a:
    c_i = get_color(a_i)
    if c_i == "Any":
        num_of_any += 1
        continue
    if c_i not in color_set:
        color_set.add(c_i)

num_of_colors = len(color_set)

if num_of_colors == 0:
    print(1, num_of_any)
else:
    print(num_of_colors, num_of_colors+num_of_any)

Measured memory_bytes: 3064
#include <stdio.h>

int main(void) 
{
	int x, y;
	unsigned int W, H, r;
	int flag = 0;
	
	scanf("%d %d %d %d %d", &W, &H, &x, &y, &r);
	
	if(x >= 0 && x <= W && y >= 0 && y <= H)
	{
		if(0 <= x - r && x + r <= W)
		{
			if(0 <= y - r && y + r <= H)
			{
				flag = 1;
			}
		}
	}
	if(flag == 1)
	{
		printf("Yes\n");
	}
	else
	{
		printf("No\n");
	}

	return 0;
}

Measured memory_bytes: 584
N,M = map(int,input().split())

def divisor_all(n): # 約数全列挙
    l = [1,n]
    for i in range(2,int(pow(n,1/2))+1):
        if n % i == 0:
            if i == n//i:
                l.append(i)
            else:
                l.append(i)
                l.append(n//i)
    l.sort()
    return l # list

DM = list(reversed(divisor_all(M)))
ans = 1
for d in DM:
    r = M//d
    if r < N:
        continue
    else:
        ans = d
        break
print(ans)

Measured memory_bytes: 38896
#define scanf_s scanf
#include <stdio.h>
#include <string>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

#define MAX 100000

int main()
{
	while (1) {
		int n, pm, pe, pj, sum;
		scanf_s("%d", &n);
		if (n == 0)break;
		for (int i = 0; i < n; ++i) {
			scanf_s("%d %d %d", &pm, &pe, &pj);
			sum = pm + pe + pj;
			if ((pm == 100 || pe == 100) || pj == 100) printf("A\n");
			else if(pm + pe >= 180) printf("A\n");
			else if (sum >= 240) printf("A\n");
			else if (sum >= 210) printf("B\n");
			else if (sum >= 150 && (pm >= 80 || pe >= 80 )) printf("B\n");
			else printf("C\n");
		}
	}
}

Measured memory_bytes: 1168
import java.util.Scanner;

public class Main {

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

        int W = scan.nextInt();
        int H = scan.nextInt();
        int x = scan.nextInt();
        int y = scan.nextInt();
        int r = scan.nextInt();

        if (W >= x + r && 0 <= x - r && H >= y + r && 0 <= y - r) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }

}



Measured memory_bytes: 26056
# 問題：https://atcoder.jp/contests/abc136/tasks/abc136_b

N = int(input())
H = list(map(int, input().strip().split()))

enable_non_decrease = True

for i in range(N-1):
    j = N - 1 - i
    if H[j-1] > H[j]:
        H[j-1] -= 1
    if H[j-1] > H[j]:
        enable_non_decrease = False
        break

if enable_non_decrease:
    print('Yes')
else:
    print('No')



Measured memory_bytes: 20660
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int N = 0;
    int M = 0;
    int X = 0;
    int temp = 0;
    int cost = 0;
    int costA = 0;
    int costB = 0;
    int *map;
    int i;
    scanf("%d%d%d", &N, &M, &X);

    if ((1 > N) || (100 < N) || (1 > M) || (100 < M)){
        return 1;
    }
    if ((1 > X) || ((N - 1) < X)){
        return 1;
    }

    map = (int *)malloc(sizeof(int) * (N + 1));
    memset(map, 0, sizeof(map));

    for (i = 0; i < M; i++)
    {
        scanf("%d", &temp);
        if ((1 > temp) || ((N - 1) < temp) || (temp == X)){
            free(map);
            return 1;
        }
        map[temp] = 1;
    }

    for (i = X; i < N; i++){
        if (map[i] == 1){
            costA++;
        }
    }
    for (i = X; i > 0; i--){
        if (map[i] == 1){
            costB++;
        }
    }
    cost = (costA < costB) ? costA : costB;
    printf("%d\n", cost);

    free(map);
 
    return 0;
}

Measured memory_bytes: 128
#include<stdio.h>

int func(int n){
  if(n % 2 == 0) return n/2;
  else return 3 * n + 1;
}

int main(){
  int N[1000000] = {};

  int s;
  scanf("%d", &s);
  int i, j;
  for(i = 0; i < 1000000; i++){
    N[i] = s;
    s = func(s);
    for(j = 0; j < i; j++){
      if(N[j] == N[i]){
        printf("%d\n", i+1);
        i = 1000000;
        break;
      }
    }
  }

  return 0;
}


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

int main() {
    string S;
    cin>>S;
    bool up = false;
    if(S[0]=='<')up = true;
    int tmp = 1;
    vector<int> arr;
    for(int i=1;i<S.size();++i){
        if(S[i]==S[i-1])tmp++;
        else {
            arr.push_back(tmp);
            tmp = 1;
        }
    }
    arr.push_back(tmp);
    long ans = 0;
    int minmin, maxmax;
    if(up){
        if(arr.size()%2==0){
            for(int i=0;i<arr.size()/2;++i){
                minmin = min(arr[i*2], arr[i*2+1]);
                maxmax = max(arr[i*2], arr[i*2+1]);
                for(int j=0;j<=maxmax;++j){
                    ans += j;
                }
                for(int j=0;j<minmin;++j){
                    ans += j;
                }
            }
        }
        else{
            for(int i=0;i<arr.size()/2;++i){
                minmin = min(arr[i*2], arr[i*2+1]);
                maxmax = max(arr[i*2], arr[i*2+1]);
                for(int j=0;j<=maxmax;++j){
                    ans += j;
                }
                for(int j=0;j<minmin;++j){
                    ans += j;
                }
            }
            for(int i=0;i<=arr[arr.size()-1];++i){
                ans += i;
            }
        }
    }
    else{
        for(int i=0;i<=arr[0];++i){
            ans += i;
        }
        if((arr.size()-1)%2==0){
            for(int i=0;i<(arr.size()-1)/2;++i){
                minmin = min(arr[i*2+2], arr[i*2+1]);
                maxmax = max(arr[i*2+2], arr[i*2+1]);
                for(int j=0;j<=maxmax;++j){
                    ans += j;
                }
                for(int j=0;j<minmin;++j){
                    ans += j;
                }
            }
        }
        else{
            for(int i=0;i<(arr.size()-1)/2;++i){
                minmin = min(arr[i*2+2], arr[i*2+1]);
                maxmax = max(arr[i*2+2], arr[i*2+1]);
                for(int j=0;j<=maxmax;++j){
                    ans += j;
                }
                for(int j=0;j<minmin;++j){
                    ans += j;
                }
            }
            for(int i=0;i<=arr[arr.size()-1];++i){
                ans += i;
            }
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 2048
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const ll MOD = 1000000007;

int main() {
    string S;
    cin >> S;
    string ans = "No";
    if (S.size() % 2 != 0) {
        cout << ans << endl;
        return 0;
    }
    for (int i = 0; i < S.size() / 2; ++i) {
        if (S[2 * i] != 'h' || S[2 * i + 1] != 'i') {
            cout << ans << endl;
            return 0;
        }
    }
    ans = "Yes";
    cout << ans << endl;
    return 0;
};

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

int main(){
  int n;
  cin >> n;
  int zero = 0;
  map<pair<ll, ll>, pair<int, int>> mp;
    
 rep(i, n){
    ll x, y;
    cin >> x >> y;
    if(x==0 && y==0 ){ zero++; continue;}
      ll g = gcd(x, y);
      x /= g; y /= g;
    if(x==0){ mp[{x, llabs(y)}].first++; continue;}
    if(y==0){ mp[{y, llabs(x)}].second++; continue;}
    
    if((x>0 && y>0) || (x<0 && y<0)) {mp[{llabs(x), llabs(y)}].first++;}
    if((x>0 && y<0) || (x<0 && y>0)) {mp[{llabs(y), llabs(x)}].second++;}
  }

  vector<int> w(n+1);
  w[0] = 1;
  rep(i, n+1){
    if(i == 0) continue;
    w[i] = ( w[i-1]*2 ) % mod;
  } 
  
  ll ans = 1;
  for(auto p: mp){
    int f = p.second.first; int s = p.second.second;
    ans = ans*(w[f] + w[s] -1) % mod;
  }
    ans = (mod + ans + zero-1 )%mod;
  
  cout << ans << endl;

    return 0;
  }

Measured memory_bytes: 16364
using namespace std;
#include<string>
#include<cmath>
#include<list>
#include <map>
#include <unordered_map>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ml map<ll,ll>
#define pb push_back
#define mp make_pair
#define ppl pair<ll,ll>
#define F first
#define S second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define fr(i,a,b) for(ll i=a;i<b;i++)
#define ffr(i,a,b) for(ll i=a;i>=b;i--)
#include<bits/stdc++.h>
#include<vector>
#define vl vector<ll>
#define PI 3.141592654
const ll N = 1e9 + 7;
#include<iterator>
#define EPSILON numeric_limits<double>::epsilon()




void solve() {
	ll h, w, m, H, W, ma = 0, maxi = 0, r, c, sum; cin >> h >> w >> m;
	ml row, col;
	map<ppl, ll> q;
	while (m--) {
		cin >> H >> W;
		q[mp(H, W)]++;
		row[H]++; col[W]++;
	}
	for (auto it : row) {
		if (it.S > ma) {
			ma = it.S;
			r = it.F;
		}
	}
	for (auto it : col) {
		if (it.S > maxi) {
			maxi = it.S;
			c = it.F;
		}
	}
	for (auto it : col) {
		if (it.S >= maxi && q.find({r, it.F}) == q.end()) {
			maxi = it.S;
			c = it.F;
		}
	}
	for (auto it : row) {
		if (it.S >= ma && q.find({it.F, c}) == q.end()) {
			ma = it.S;
			r = it.F;
		}
	}
	sum = ma + maxi;
	if (q.find({r, c}) != q.end())sum--;
	cout << sum;
	//cout << c;
}





int main() {
	fio;
#ifndef ONLINE_JUDGE
	// for getting input
	freopen("input.txt", "r", stdin);
	//for writing output
	freopen("jout.txt", "w", stdout);
#endif

	ll t;
	if (1)
		t = 1;
	else
		cin >> t;
	while (t--) {
		//cout << "Case #" << h << ":\n";
		solve();
		//h++;
		cout << '\n';
	}
	return 0;
}






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

#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;

using namespace std;



void one_case() {
    int n;
    cin >> n;
    ll mod = 1e9 + 7;
    if (n < 2) {
        cout << "0\n";
        return;
    }

    ll not_zn = 64, not_z = 17;
    ll total = 100;
    n -= 2;
    while (n--) {
        auto nz = (not_zn + not_z * 9) % mod;
        not_zn = (not_zn * 8) % mod;
        total = (total * 10) % mod;
        not_z = nz;
    }


    cout << (total + 5 * mod - not_zn - 2 * not_z) % mod;
}


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

    int t = 1;
//    cin >> t;
    while (t--) one_case();
    return 0;
}


Measured memory_bytes: 3640
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {

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

        int N = sc.nextInt();
        int[] as = new int[N];
        for (int i = 0; i < N; i++) {
            as[i] = sc.nextInt();
        }
        int[] bs = new int[N];
        for (int i = 0; i < N; i++) {
            bs[i] = sc.nextInt();
        }
        int[] cs = new int[N];
        for (int i = 0; i < N; i++) {
            cs[i] = sc.nextInt();
        }

        System.out.println( solve(N, as, bs, cs) );
    }

    private static long solve(int N, int[] as, int[] bs, int[] cs) {
        Arrays.sort(as);
        Arrays.sort(bs);
        Arrays.sort(cs);

        long[] counts_b = new long[N];
        for (int i = N-1; i >= 0; i--) {
            int b = bs[i];

            int index = upperBound(cs, b);
            int count = N - index;
            counts_b[i] = count;
            // 右のものを足して累積和にしておく
            if( i != N - 1 ) {
                counts_b[i] += counts_b[i+1];
            }
        }

        long sum = 0;
        for (int i = 0; i < N; i++) {
            int a = as[i];
            int index = upperBound(bs, a);

            if( index < N ) {
                sum += counts_b[index];
            }
        }

        return sum;
    }

    private static int upperBound(int[] array, int value) {
        int low = 0;
        int high = array.length;
        int mid;
        while( low < high ) {
            mid = ((high - low) >>> 1) + low; // (high + low) / 2
            if( array[mid] <= value ) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }

    private static void show(int[] array) {
        StringJoiner j = new StringJoiner(" ");
        for (int i : array) {
            j.add("" + i);
        }
        System.err.println( j.toString() );
    }
}

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

#define MAX 500000
#define SENTINEL 2000000000
int L[MAX/2+2],R[MAX/2+2];
int cnt;

void merge(int A[],int n,int left,int mid,int right){
    int n1=mid-left;
    int n2=right-mid;
    for(int i=0;i<n1;i++) L[i]=A[left+i];
    for(int i=0;i<n2;i++) R[i]=A[mid+i];
    L[n1]=R[n2]=SENTINEL;
    int i=0,j=0;
    for(int k=left;k<right;k++){
        cnt++;
        if(L[i]<=R[j])
            A[k]=L[i++];
        else
            A[k]=R[j++];
    }

}

void mergeSort(int A[],int n,int left,int right){
    if(left+1<right){
        int mid=(left+right)/2;
        mergeSort(A,n,left,mid);
        mergeSort(A,n,mid,right);
        merge(A,n,left,mid,right);
    }
}

int main(){
    int A[MAX],n,i;
    cnt=0;

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


    mergeSort(A,n,0,n);
    for(i=0;i<n;i++){
        cout<<(i?" ":"")<<A[i];
    }
    cout<<"\n";
    cout<<cnt<<"\n";
    return 0;
}



Measured memory_bytes: 6964
mod = 10 ** 9 + 7
k = int(input())
s = len(input())
n = s + k

fac = [1] * (n+1)
for i in range(1, n+1):
    fac[i] = fac[i-1] * i % mod

ans = 0
for i in range(s, n+1):
	ncr = fac[i-1] * pow(fac[i-s]*fac[s-1] % mod, mod-2, mod) % mod
	res = pow(26, n-i, mod)* pow(25, i-s, mod) % mod
	ans = (ans + res * ncr) % mod
print(ans) 



Measured memory_bytes: 104360
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <numeric>
#include <set>
#include <list>
#include <map>
#include<queue>

const int INF = 1 << 28;
using namespace std;
using Graph = vector<vector<int>>;

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

vector<int> seen(50,-1);
int counter_for_dfs = 0;
void dfs(Graph &G, int v) {
	seen[v] = counter_for_dfs;
	counter_for_dfs++;
	for (auto v_next : G[v]) {
		if (seen[v_next]>=0)continue;

		dfs(G, v_next);
	}
}

void bfs(Graph &G) {
	vector<int> dist(G.size(), -1);
	queue<int> que;
	
	dist[0] = 0;
	que.push(0);
	while (!que.empty()) {
		int v = que.front();
		que.pop();

		for (int g : G[v]) {
			if (dist[g] != -1)continue;
			dist[g] = dist[v] + 1;
			que.push(g);
		}
	}
	for (auto i : dist) {
		cout << i << " ";
	}
}

double distance(P<double,double> a,P<double,double> b) {
	return sqrt(pow(a.first - b.first, 2) + pow(a.second - b.second,2));
}
int main()
{
	int k, s;
	cin >> k >> s;

	int x, y, z;
	int result = 0;
	for (int i = 0; i <= k; i++) {
		for (int j = 0; j <= k; j++) {
			z = s - i - j;
			if (z<0 || z>k) {
				continue;
			}
			else {
				result++;
			}
		}
	}
	cout << result;
   return 0;
}

Measured memory_bytes: 256
n=int(input())
a=list(map(int,input().split()))
b=[]
if n%2==0:
    for i in range(n-1,0,-2):
        b.append(a[i])
    for j in range(0,n-1,2):
        b.append(a[j])
    print(*b)
else:
    for i in range(n-1,-1,-2):
        b.append(a[i])
    for j in range(1,n-1,2):
        b.append(a[j])
    print(*b)
  


Measured memory_bytes: 26020
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <string>
#include <tuple>

template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(std::min<int>(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_): r(std::max<int>(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
inline T scan() {
  T res;
  std::cin >> res;
  return res;
}

std::string N;
int K;
long long dp[102][4][2];

int main() {
  std::cin >> N >> K;
  std::reverse(N.begin(), N.end());
  N += std::string(100, '0');
  dp[101][0][1] = 1;
  for (int i: revrange(0, 101)) {
    for (int j: range(0, 4)) {
      for (int k: range(0, 10)) {
        int nj = (k > 0 ? j + 1 : j);
        if (nj > K) {
          continue;
        }
        dp[i][nj][0] += dp[i + 1][j][0];
      }
      for (int k: range(0, 10)) {
        int nj = (k > 0 ? j + 1 : j);
        if (nj > K) {
          continue;
        }
        if (k == N[i] - '0') {
          dp[i][nj][1] += dp[i + 1][j][1];
        } 
        else if (k < N[i] - '0') {
          dp[i][nj][0] += dp[i + 1][j][1];
        }
      }
    }
  }
  std::cout << dp[0][K][0] + dp[0][K][1] << '\n';
  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <sstream>
#include <stack>
#include <time.h>
#include <vector>
#include <complex>
#include <map>
#include <set>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <list>
#include <utility>
#include <memory>
#include <cstring>
#include <fstream>
#include <numeric>
#include <assert.h>
#include <bits/stdc++.h>
using namespace std;
vector <int> g[5];
bool vis[5];
void dfs(int x){
	vis[x]=1;
	for (int i=0;i<g[x].size();i++){
		if (vis[g[x][i]]) continue;
		dfs(g[x][i]);
		break;
	}
}
int main(){
	int a,b,x=3;
	while (x--){
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for (int i=1;i<=4;i++){
		memset (vis,0,sizeof(vis));
		dfs(i);
		if (!vis[1] || !vis[2] || !vis[3] || !vis[4]) continue;
		else return !printf("YES");
	}
	cout<<"NO";
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <set>
#include <algorithm>

#define NUM 75

using namespace std;

int main(void){
	int n; cin >> n;
	for(int i=0; i<n; i++){
		string data; cin >> data;
		set<string> set;
		for(int j=1; j<data.size(); j++){
			string s1, s2, r1, r2;
			s1 = r1 = data.substr(0, j);
			s2 = r2 = data.substr(j, data.size());
			reverse(r1.begin(), r1.end());
			reverse(r2.begin(), r2.end());
			set.insert(s1 + s2);
			set.insert(s1 + r2);
			set.insert(r1 + s2);
			set.insert(r1 + r2);
			set.insert(s2 + s1);
			set.insert(s2 + r1);
			set.insert(r2 + s1);
			set.insert(r2 + r1);
		}
		cout << set.size() << endl;
	}
	return 0;
}


Measured memory_bytes: 3296
#include <bits/stdc++.h>
using namespace std;
#define dump(a) cout << (a) << '\n';
typedef int64_t Int;
Int mod = 1e9+7;
// Int MOD = 998244353;

int main() {
    ios::sync_with_stdio(false);
    Int n;
    cin >> n;
    vector<Int> a(n), b(n);
    priority_queue<pair<Int, Int>> c, d, e;
    for (Int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        c.push({a[i], i});
        d.push({b[i], i});
        e.push({a[i] + b[i], i});
    }
    vector<bool> used(n, false);
    Int res = 0;
    while (!e.empty()) {
        res += a[e.top().second];
        e.pop();
        if (e.empty()) break;
        res -= b[e.top().second];
        e.pop();
    }
    /* while (!c.empty() || !d.empty()) {
        
        if (d.empty() || c.top().first + b[c.top().second] >= d.top().first + a[d.top().second]) {
           res += c.top().first;
           used[c.top().second] = true;
           c.pop();
        }
        else if (c.empty() || c.top().first + b[c.top().second] < d.top().first + a[d.top().second]) {
           res += a[d.top().second];
           used[d.top().second] = true;
           d.pop();
        }
        
        while (!c.empty() && used[c.top().second]) c.pop();
        while (!d.empty() && used[d.top().second]) d.pop();
        if (c.empty() && d.empty()) break;
        if (d.empty() || c.top().first + b[c.top().second] >= d.top().first + a[d.top().second]) {
           res -= b[c.top().second];
           used[c.top().second] = true;
           c.pop();
        }
        else if (c.empty() || c.top().first + b[c.top().second] < d.top().first + a[d.top().second]) {
           res -= d.top().first;
           used[d.top().second] = true;
           d.pop();
        }
        while (!c.empty() && used[c.top().second]) c.pop();
        while (!d.empty() && used[d.top().second]) d.pop();
    } */
    dump(res);
    return 0;  
} 

Measured memory_bytes: 6740
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = (int)1e9;

const int N = 18;
const int V = 50;
int dp[1 << N][V+1];
int as[N];
int bs[N];
int off[N][N];

void cap(int& v, int off) {
	if (v > off) v = off;
}

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

	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) cin >> as[i];
	for (int i = 0; i < n; ++i) cin >> bs[i];
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			off[i][j] = (i % 2 == j % 2) ? as[j] : bs[j];
		}
	}

	int h = 1 << n;
    // first step is initializing every state as infinite
	for (int m = 0; m < h; ++m) {
		for (int j = 0; j <= V; ++j) dp[m][j] = INF;
	}
	dp[0][0] = 0;
	
	for (int m = 0; m < h; ++m) {
		int i = __builtin_popcount(m);
        // i represents the number of elements that are already present in
        // the sequence therefore by 0-based indexing the next element is going to
        // take the ith position
		for (int v = 0; v <= V; ++v) {
			int d = 0;
			for (int j = 0; j < n; ++j) {
				if (m & (1 << j)) continue;

				if (off[i][j] >= v) cap(dp[m | (1 << j)][off[i][j]], dp[m][v] + d);
				// the above condition states that the element at position j has 
                // to be moved to the position i but and after moving by that distance
                // what happens to its value will be indicated by offset[i][j]
                // i.e whether it remains black or turns blue
                // and after that we need to ensure that it has a value greater
                // than the last element , so that the sequence remains non-decreasing
                ++d;
			}
		}
	}

	int res = INF;
	for (int v = 0; v <= V; ++v) res = min(res, dp[h-1][v]);
	cout << (res >= INF ? -1 : res) << '\n';
}

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

#define vec(v) vector<int>(v)
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define mapci(mp) map<char*,int>(mp)
#define mapcc(mp) map<char*,char*>(mp)
typedef long long ll;
using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int ans=0,n,a;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        if(a%2==1){
            ans++;
        }
    }
    if(ans%2==1){
        printf("NO\n");
    }
    else{
        printf("YES\n");
    }
    return 0;
}


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

int main(){
  int n, m;
  cin >> n >> m;
  vi a(n);
  rep(i,n) cin >> a[i];
  sort(a.begin(),a.end());
  deque<int> que;
  rep(i,n) que.push_back(a[i]);
    
  vector<pair<int,int>> cb(m);
  rep(i,m) cin >> cb[i].second >> cb[i].first;
  sort(cb.rbegin(),cb.rend());
  
  rep(i,m){
    rep(j,cb[i].second){
      if(que.front() >= cb[i].first) break;
      que.pop_front();
      que.push_back(cb[i].first);
    }
  }
  ll ans = 0;
  rep(i,n){
    ans += que.front();
    que.pop_front();
  }
  cout << ans << endl;
}


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

#define MAX 10

typedef struct __queue_ {
  char name[MAX];
  int time;
  struct __queue_ *next;
} __queue_t;

int main() {
  __queue_t *head = malloc(sizeof(__queue_t));
  __queue_t *tail = head;
  tail->next = NULL;
  int n, q, now = 0;
  scanf("%d %d", &n, &q);
  scanf("%s %d", tail->name, &(tail->time));
  n--;
  while (n--) {
    tail->next = malloc(sizeof(__queue_t));
    tail = tail->next;
    tail->next = NULL;
    scanf("%s %d", tail->name, &(tail->time));
  }
  while (NULL != head) {
    if (head->time <= q) {
      now += head->time;
      printf("%s %d\n", head->name, now);
      __queue_t *temp = head;
      head = head->next;
      free(temp);
    } else {
      now += q;
      head->time -= q;
      tail->next = head;
      tail = tail->next;
      head = head->next;
      tail->next = NULL;
    }
  }
  return 0;
}

Measured memory_bytes: 2160
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
  int n;
  cin >> n;
  
  string s;
  cin >> s;
  
  vector<vector<int>> len(n + 1, vector<int>(n + 1, 0));
  
  for(int i = n - 1; i >= 0; --i){
    for(int j = n - 1; j >= 0; --j){
      if(s[i] == s[j]) len[i][j] = len[i + 1][j + 1] + 1;
      else len[i][j] = 0;
    }
  }
  
  int len_max = 0;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      if(i >= j) continue;
      len_max = max(min(len[i][j], j - i), len_max);
    }
  }
  
  cout << len_max << endl;
  
  return 0;
}

Measured memory_bytes: 98176
import java.util.Arrays;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int l = sc.nextInt();
		String ans = null;
		String [] array = new String [n];
		for (int i =0; i < n; i++) {
			array[i] = sc.next();
		}
		Arrays.sort(array);
		for (int i = 0; i < n; i ++) {
			ans += array[i];
		}
		System.out.println(ans.substring(ans.length()- (n * l), ans.length()));
	}
}


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

#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right)
{
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
  {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++)
  {
    R[i] = A[mid + i];
  }
  L[n1] = R[n2] = SENTINEL;
  int l = 0, r = 0;
  for (int i = left; i < right; i++)
  {
    cnt++;
    if (L[l] <= R[r])
    {
      A[i] = L[l++];
    }
    else
    {
      A[i] = R[r++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right)
{
  if (left + 1 < right)
  {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main()
{
  int A[MAX], n;
  cnt = 0;

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

  mergeSort(A, n, 0, n);

  for (int i = 0; i < n; i++)
  {
    if (i)
    {
      cout << " ";
    }
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}


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

int main() {
    int N;
    cin >> N;
    vector<string> S(N);
    for(int i=0;i<N;i++) {
        cin >> S[i];
    }
    set<string> set_S;
    for(int i=0;i<N;i++) {
        set_S.insert(S[i]);
    }
    cout << set_S.size() << endl;
    return 0;
}

Measured memory_bytes: 25080
#include<cstdio>
typedef long long int ll;

int main(){
	ll x;
	scanf("%lld",&x);
	for(ll i=-500;i<=500;i++){
		ll y = x + i*i*i*i*i;
		for(ll j=-500;j<=500;j++){
			if(y==j*j*j*j*j){
				printf("%lld %lld\n",j,i);
				return 0;
			}
		}
	}
}


Measured memory_bytes: 1688
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Main {

    public static void main(String[] args) throws Exception {

        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

            List<String> input = Arrays.asList(br.readLine().split(""));
            Set<String> stations = new HashSet<>();

            for (String s : input) {
                stations.add(s);
            }

            String result = "Yes";
            if (stations.size() == 1) {
                result = "No";
            }

            System.out.println(result);
        } catch (IOException | NumberFormatException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (final Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }
}

Measured memory_bytes: 23252
class UnionFind:
    def __init__(self, num):
        self.table = [-1 for _ in range(num)]

    def find(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]

    def union(self, x, y):
        s1 = self.find(x)
        s2 = self.find(y)

        if s1 != s2:
            if self.table[s1] <= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
            return True
        return False
from collections import defaultdict

N = int(input())
u = UnionFind(N)
xy = [list(map(int,input().split())) for _ in range(N)]
x_d = defaultdict(lambda:[])
y_d = defaultdict(lambda:[])
for i in range(N):
    x,y = xy[i]
    x_d[x].append(i)
    y_d[y].append(i)
for x in x_d:
    j = x_d[x][0]
    for i in x_d[x]:
        u.union(i,j)
for y in y_d:
    j = y_d[y][0]
    for i in y_d[y]:
        u.union(i,j)

d = defaultdict(lambda:[])
for i in range(N):
    d[u.find(i)].append(i)

ans = 0
for f in d:
    x_l = []
    y_l = []
    for i in d[f]:
        x,y = xy[i]
        x_l.append(x)
        y_l.append(y)
    x_s = set(x_l)
    y_s = set(y_l)
    ans += len(x_s)*len(y_s)-len(d[f])

print(ans)

Measured memory_bytes: 122400
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> as(n, 0);

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

  sort(as.begin(), as.end());

  int expect = n - 1;
  for (int i = n - 1, cnt = 0; i >= 0; i--, cnt++) {
    if (cnt != 0 && cnt % 2 == 0) {
      expect -= 2;
    }
    if (expect != as[i]) {
      cout << 0 << endl;
      return 0;
    }
  }

  long long int ans = 1;
  long long int mod = 1000L * 1000L * 1000L + 7L;

  for (int i = 0; i < n / 2; i++) {
    ans *= 2;
    ans = ans % mod;
  }
  cout << ans << endl;
}


Measured memory_bytes: 640
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}
// }}} End Header

i64 pow2(int n) {
    if (n % 2 == 0) {
        return 1LL << n;
    } else {
        return -(1LL << n);
    }
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    i64 N; read(N);


    if (N == 0) {
        print(0);
        return 0;
    }

    vector<int> ans;
    i64 n = N;

    while (n != 0) {
        i64 k = n / -2;
        if (k * -2 > n) ++k;
        ans.push_back(n - (k * -2));
        n = k;
    }

    reverse(all(ans));
    for (int e : ans) cout << e;
    print();

    return 0;
}


Measured memory_bytes: 256
//改良版
#include <iostream>
using namespace std;
int main(void){
    int a,b,c,x;
    scanf("%d%d%d%d", &a, &b, &c, &x);
    int ans = 0;
    for(int i=0;i<=a;i++){
      for(int j=0;j<=b;j++){
        for(int k=0;k<=c;k++){
          int tmp = 500*i + 100*j + 50*k;
          if (tmp==x) ans++; 
        }   
      }    
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
n, x = map(int, input().split())

def func(a, b):
    res = 0
    while b > 0:
        c, d = divmod(a, b)
        res += 2 * b * c
        a = b
        b = d
    
    return res - a

if n == 2 * x:
    print(3 * x)
elif n > 2 * x:
    print(n + 2 * x + func(max(x, n-2*x), min(x, n-2*x)))
else:
    print(n + func(x, n-x))

Measured memory_bytes: 9096
#include <cstdio>
#include <iostream> 
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
 
using namespace std;
using ll = long long;
 
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };


int main()
{
	string A, B;
	int a[105] = {}, b[105] = {}, i, c, d;
	char x;

	cin >> A >> B;
	c = A.size() - 1;
	d = B.size() - 1;

	for(i = 104; i > 104 - A.size(); i--){
		x = A[c];
		a[i] = atoi(&x);
		c--;
	}
	for(i = 104; i > 104 - B.size(); i--){
		x = B[d];
		b[i] = atoi(&x);
		d--;
	}

	for(i = 0; i < 105; i++){
		if(a[i] > b[i]){
			cout << "GREATER" << endl;
			return 0;
		}
		if(a[i] < b[i]){
			cout << "LESS" << endl;
			return 0;
		}
	}

	cout << "EQUAL" << endl;
}

Measured memory_bytes: 256