akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define endl '\n'
const int inf = 1000000007;
const ll INF = 1e18;
int mod = 998244353;
// int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template<typename T, typename U> T pow_(T a, U b) { return b ? pow_(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) { return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1; }
template<class T, class U> ostream& operator << (ostream& os, const pair<T, U>& p) { os << p.F << " " << p.S; return os; }
template<class T> ostream& operator << (ostream& os, const vector<T>& vec) { rep(i, sz(vec)) { if (i) os << " "; os << vec[i]; } return os; }
template<typename T> inline istream& operator >> (istream& is, vector<T>& v) { rep(j, sz(v)) is >> v[j]; return is; }
template<class T, class T2> inline void add(T &a, T2 b) { a += b % mod; if (a >= mod) a -= mod; }
template<class T> void operator += (vector<T>& v, vector<T> v2) { rep(i, sz(v2)) v.eb(v2[i]); }

void solve();

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(11);
  int T;
  T = 1;
  while (T--) solve();
}

void solve() {
  string s, t;
  cin >> s >> t;
  int c = 0;
  rep(i, 3) if (s[i] == t[i]) c++;
  cout << c << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define reps(i,n) for(int i=1;i<(int)(n);i++)

typedef long long ll;
using namespace std;

ll gcd(ll a, ll b) { //最大公約数
  if (b==0) return a;
  else return gcd(b, a%b);
}

ll lcm(ll a, ll b) { //最小公倍数
  return a * b / gcd(a, b);
}

ll digit_sum(ll a){ //10進桁和
  ll ans=0;
  while(a>0){
    ans+=a%10;
    a/=10;
  }
  return ans;
}



int main()
{
  ll a,b,c,x;
  ll ans=0;
  cin>>a>>b>>c>>x;
  rep(i,a+1){
    rep(j,b+1){
      rep(k,c+1){
        if(500*i+100*j+50*k==x)ans++;
      }
    }
  }

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


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

int main(){
    int N;
    cin >> N;
    vector<int> P(N);
    vector<int> Q(N);

    for (size_t i = 0; i < N; i++)
    {
        cin >> P.at(i);
    }

    for (size_t i = 0; i < N; i++)
    {
        cin >> Q.at(i);
    }

    vector<int> a(N);
    for (size_t i = 0; i < N; i++)
    {
        a.at(i) = i+1;
    }

    map<vector<int>, int> mp;
    do
    {
        mp[a] = mp.size();
    } while (next_permutation(a.begin(),a.end()));
    
    int ans = abs(mp[P] - mp[Q]);
    cout << ans;
    
    


    
}

Measured memory_bytes: 5248
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			if(n == 0) break;
			int s = sc.nextInt() - 1;
			int k = sc.nextInt() - 1;
			int d = sc.nextInt();
			int[][] g = new int[d][n];
			for(int i = 0; i < d; i++) {
				for(int j = 0; j < n; j++) {
					g[i][j] = -1;
				}
			}
			for(int i = 0; i < d; i++) {
				String t = sc.next();
				for(int j = 0; j < n - 1; j++) {
					if(t.charAt(j) == '1') {
						g[i][j] = j + 1;
						g[i][j + 1] = j;
					}
				}
			}
			if(solve(n, d, g, s, k)) {
				System.out.println(0);
			}else {
				boolean flag = true;
				loop: for(int i = 0; i < d; i++) {
					for(int j = 0; j < n - 1; j++) {
						if(g[i][j] == -1 && g[i][j + 1] == -1) {
							g[i][j] = j + 1;
							g[i][j + 1] = j;
							if(solve(n, d, g, s, k)) {
								System.out.printf("%d %d\n", i + 1, j + 1);
								flag = false;
								break loop;
							}
							g[i][j] = -1;
							g[i][j + 1] = -1;
						}
					}
				}
				if(flag) {
					System.out.println(1);
				}
			}
		}
		sc.close();
	}
	static boolean solve(int n, int d, int[][] g, int s, int k) {
		int h = 0;
		for(int i = 0; i < d; i++) {
			if(g[h][s] != -1) {
				s = g[h][s];
				h++;
			}else {
				h++;
			}
		}
		if(s == k) return true;
		return false;
	}
}


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

#define EPS (1e-7)
#define INF 1e18
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define PI (acos(-1))

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


int main() {

    int N, K;
    cin >> N >> K;
    string s;
    cin >> s;
    int sum = 0;
    REP(i, N - 1) if (s[i] == s[i + 1]) sum++;

    sum = min(N - 1, sum + 2 * K);
    cout << sum << endl;

    return 0;
}

Measured memory_bytes: 512
a=-1,A=65,d;char*N="GKSOCNWLDFVTBRXHAJUPEMQI",*E="PHLTIDOXQAGWECSUMBKVJFNR",s[9];main(c){for(;c-48&&scanf("%s",s);a+=d/4+1)(c=*s)<A?~a&&printf("%d\n",a),a=d=0:0,d=(c==78?d=N[d]:c==69?d=E[d]:c==83?d=N[N[N[d]-A]-A]:c==87?d=E[E[E[d]-A]-A]:d+A)-A,d=c==82|c==76?d&28|d%4+3-c/80*2&3:d;}

Measured memory_bytes: 0
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;
typedef long long Int;
#define REP(i , x) for(int i = 0 ; i < x ; ++i)



int main()
{
	int n;
	int m;
	

	while (cin >> n,n){

		map<char, char> table;
		string ans;
		REP(i, n){
			char a, b;
			cin >> a >> b;
			table[a] = b;
		}
		cin >> m;
		REP(i, m){
			char a;
			cin >> a;
			if (table.find(a) == table.end()){
				ans += a;
			}
			else
				ans += table[a];
		}

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

}

Measured memory_bytes: 1328
package main

import "fmt"

func main() {
	var r, g, b int
	fmt.Scanf("%d %d %d", &r, &g, &b)

	m := r*100 + g*10 + b
	if m%4 == 0 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

Measured memory_bytes: 512
MOD = 10**9+7
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
xx = 0
for i in range(n-1):
    w = x[i+1] - x[i]
    c = (i+1)*(n-i-1)
    xx = (xx + w * c) % MOD
yy = 0
for i in range(m-1):
    h = y[i+1] - y[i]
    c = (i+1)*(m-i-1)
    yy = (yy + h * c) % MOD
print((xx*yy) % MOD)

Measured memory_bytes: 18576
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int main(){
    ll a,b,c; cin>>a>>b>>c;
    ll i=1;
    rep(j, 100000){
        //if(i>1 && (a*i)%b==a%b==0) break;
        if((a*i)%b==c){
            cout<<"YES"<<endl;
            return 0;
        }
        i++;
    }
    cout<<"NO"<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
#define  pb push_back
#define  xx first
#define  yy second
//#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;

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

 struct node{
        int roll;
        node *next;
 }root;


int main()
{
     int t=1,caseno = 0;

     while(t--){
        int a,b,m;
       cin >> a>>b >> m;
       int A[a+1],B[b+1];
       int MN1 = 1e9,MN2 = 1e9;
       for(int i = 1; i<=a; i++){
        cin >> A[i];
        MN1 = min(MN1,A[i]);
       }
       for(int i = 1; i<=b; i++){
        cin >> B[i];
        MN2 = min(MN2,B[i]);
       }
       int mn = 1e9;
       while(m--){
            int x,y,c;
        cin >> x>>y>>c;
        mn = min(mn,A[x]+B[y]-c);
       }
       cout << min(MN1+MN2,mn) << endl;
     }


    return 0;
}



Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int dp[8][2201],a,b,n,m;
main(){
  while(cin>>n,n){
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    r(i,n){
      cin>>a>>b;
      r(j,1001)r(k,b)dp[i+1][j+(k+1)*a]+=dp[i][j];
      r(j,1001)dp[i+1][j]+=dp[i][j];
    }
    cin>>m;
    while(m--){
      cin>>a;
      cout<<dp[n][a]<<endl;
    }
  }
}

Measured memory_bytes: 3076
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
using ll = long long;
#define MOD 1000000007

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

    vector<int> A(N);
    vector<int> A2(2*N);
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
        A2[i+N] = A[i];
        A2[i] = A[i];
    }

    ll inv_s = 0;

    for (int i = 0; i < N; i++)
    {
        for (int j = i+1; j < N; j++)
        {
            if(A[i] > A[j]) inv_s++;
        }
    }

    ll inv_d = 0;

    for (int i = 0; i < 2*N; i++)
    {
        for (int j = i+1; j < 2*N; j++)
        {
            if(A2[i] > A2[j]) inv_d++;
        }
    }
    
    inv_d -= 2*inv_s;

    ll combi = (K*(K-1))/2;
    combi %= MOD;

    ll ans = (K * inv_s)%MOD + (inv_d * combi)%MOD;

    ans %= MOD;

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


Measured memory_bytes: 3640
//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;
    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            string s;
            cin>>s;
            if(s[0]>='A' && s[0]<='Z')
                cout<<"A"<<endl;
            else
                cout<<"a"<<endl;
        }
        return 0;
    }


Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
int main(){
  vector<char> s(3);
  cin>>s.at(0)>>s.at(1)>>s.at(2);
  sort(s.begin(),s.end());
  if(s.at(0)=='a'&&s.at(1)=='b'&&s.at(2)=='c'){
    cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
}

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

d_list = []
a_list = []

for i in range(k):
  d_list.append(int(input()))
  a_list.append([int(x) for x in input().split()])
  
lis = {i:0 for i in range(1,n+1)}

for a in a_list:
  for a2 in a:
    lis[a2] += 1

print(list(lis.values()).count(0))

Measured memory_bytes: 9224
#include <bits/stdc++.h>
using namespace std;
 
typedef unsigned long long  ULL;
typedef long long LL;
 
#define pb push_back
#define mp make_pair
#define reset(a, b) memset(a, b, sizeof(a))
#define rep(i,b) for(int i=0; i<b; i++)
#define F first 
#define S second
 
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int T;
//    cin>>T;
    T=1;
    while(T--){
       
       int n,m;
       cin>>n>>m;
       int x;
       int sum=0;
       rep(i,m){
           cin>>x;
           sum+=x;
       }
       
       if(sum<=n) cout<<(n-sum);
       else cout<<-1;
    }
    
    return 0;
}

Measured memory_bytes: 3668
#include <bits/stdc++.h>
#define rep(i,m,n) for(int i=m; i<n; i++)
#define co(n) cout << n << endl
using namespace std;
int main(){
  long a, b, c=0, d=0, e, f, g, h, n;
  cin >> a >> b;
  if(a>b){
    rep(i,0,a){
      c+=b*pow(10, i);
    }
    co(c);
  }
  else{
    rep(i,0,b){
      c+=a*pow(10, i);
    }
    co(c);
  }
    
  
  return 0;
}


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

struct Node {
  int value = -1;
  Node *parent = NULL;
  Node *left = NULL;
  Node *right = NULL;
};

Node* tree;

void insert(int p) {
  Node* z = new Node();
  z->value = p;
  
  if (tree == NULL) {
    tree = z;
    return;
  }

  Node* y = NULL;
  Node* x = tree;
  
  while (x != NULL) {
    y = x;
    if (z->value < x->value) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  
  if (y == NULL) {
    tree = z;
  } else if (z->value < y->value) {
    y->left = z;
  } else {
    y->right = z;
  }
}

void inorder(Node* node) {
  if (node == NULL) return;
  inorder(node->left);
  cout << " " << node->value;
  inorder(node->right);
}

void print_inorder(Node* node) {
  inorder(node);
  cout << endl;
}

void preorder(Node* node) {
  if (node == NULL) return;
  cout << " " << node->value;
  preorder(node->left);
  preorder(node->right);
}

void print_preorder(Node* node) {
  preorder(node);
  cout << endl;
}

int main() {
  int N; cin >> N;
  string queue;
  int p;

  for (int i = 0; i < N; i++) {
    cin >> queue;
    if (queue == "insert") {
      cin >> p;
      insert(p);
    } else {
      print_inorder(tree);
      print_preorder(tree);
    }
  } 
}




Measured memory_bytes: 26508
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int x,i,j,flag;
	int a[2005];
	scanf("%d",&x);
	flag=0;
	if(x==1){
		printf("1 0");
		return 0;
	}
	for(i=-1000;i<=1000;i++){
		
		a[i+1000]=i*i*i*i*i;
	}
	for(i=0;i<2000;i++)
	{
		for(j=0;j<2000;j++)
		{
			if(a[i]-a[j]==x)
			{
				printf("%d %d",i-1000,j-1000);
				return 0;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 1736
N=int(input())
A=list(map(int,input().split()))

def addable(a,b):
  if a<b:
    a,b=b,a
  sa=bin(a)[2:]
  sb=bin(b)[2:].zfill(len(sa))
  for i in range(len(sa)):
    if sa[i]=="1" and sb[i]=="1":
      return False
  return True
  
right=1
ans=0
cur=A[0]
for left in range(N):
  while right<N and addable(cur,A[right]):
    cur|=A[right]
    right+=1
  ans+=(right-left)
  
  if left==right:
    right+=1
  else:
    cur-=A[left]
  
print(ans)

Measured memory_bytes: 24584
require 'set'
n, k = gets.strip.split.map(&:to_i)
td = readlines.map {|line| line.strip.split.map(&:to_i) }

test_td = td.dup
grouped_td = test_td.group_by(&:first)
 
sushi_max = grouped_td.map {|t, arr| [t, arr.map(&:last)] }.map {|t, arr| [t, arr.max] }.sort_by{|_,d| -1*d }
 


td = td.sort_by {|td| td.last*-1 }

selected = td[0, k]
unselected = td[k, n]

current_kinds = Set.new(selected.map(&:first))
current_score = selected.map(&:last).inject(:+)
max = current_score + current_kinds.size**2

if n==k
  p max
  exit
end

dup_set = Set.new
dups_in_selected = []
selected.each do |t, d|
  if dup_set.include?(t)
    dups_in_selected << [t,d]
  else
    dup_set << t
  end
end

unselected.each do |t, d|
  next if current_kinds.include?(t)
  removed = dups_in_selected.pop
  break if removed.nil?
  current_score -= removed.last
  current_score += d
  current_kinds << t
  tmp_max = current_score + current_kinds.size**2
  max = tmp_max if tmp_max > max
end

p max






Measured memory_bytes: 54620
# coding: utf-8
# Here your code !
import sys
line = sys.stdin.readlines()
top = [0,0,0]
for i in range(len(line)):
    line[i] = int(line[i].rstrip("\n"))
    if top[0] < line[i]:
        top[2] = top[1]
        top[1] = top[0]
        top[0] = line[i]
    elif top[1] < line[i]:
        top[2] = top[1]
        top[1] = line[i]
    elif top[2] < line[i]:
        top[2] = line[i]

for i in range(3):
    print(top[i])

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


void solve(long long N, std::vector<long long> d){
    ll ans=0,s=0;
    rep(i,N){
        s+=d[i]*d[i];
        rep(j,N){
            ans+=d[i]*d[j];
        }
    }
    cout<<(ans-s)/2<<endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    std::cout<<std::fixed<<std::setprecision(10);//誤差小数10桁まで
    long long N;
    scanf("%lld",&N);
    std::vector<long long> d(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&d[i]);
    }
    solve(N, std::move(d));
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>G[100];
int n;
int d[100];
int cnt[101];
int ansa;
int dfs(int u,int p)
{
	int ans=0;
	for(int i=0;i<G[u].size();i++)
	{
		if(G[u][i]!=p)ans=max(ans,dfs(G[u][i],u));
	}
	return ans+1;
}
long f(int u,int v)
{
	vector<int>d(111,n);
	d[u]=d[v]=0;
	queue<int>P;
	P.push(u);P.push(v);
	while(!P.empty())
	{
		int x=P.front();P.pop();
		for(int i=0;i<G[x].size();i++)
		{
			int y=G[x][i];
			if(d[y]>d[x]+1)
			{
				d[y]=d[x]+1;
				P.push(y);
			}
		}
	}
	long ans=1;
	for(int i=0;;i++)
	{
		bool flag=0;
		long now=1;
		for(int j=0;j<n;j++)
		{
			if(d[j]==i)
			{
				flag=1;
				now=max(now,(long)G[j].size()-1);
			}
		}
		if(!flag)break;
		ans*=now;
	}
	return ans*2;
}
main()
{
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int a,b;cin>>a>>b;
		a--,b--;G[a].push_back(b),G[b].push_back(a);
	}
	for(int i=0;i<n;i++)ansa+=!cnt[d[i]=dfs(i,-1)]++;
	int mind=0;
	long ansb=1;
	for(;!cnt[mind];mind++);
	if(cnt[mind]%2)
	{
		int mindi=0;
		for(;d[mindi]!=mind;mindi++);
		ansb=G[mindi].size();
		for(int i=mind+1;cnt[i];i++)
		{
			int now=1;
			for(int j=0;j<n;j++)
			{
				if(d[j]==i)now=max(now,(int)G[j].size()-1);
			}
			ansb*=now;
		}
		for(int i=0;i<G[mindi].size();i++)
		{
			ansb=min(ansb,f(mindi,G[mindi][i]));
		}
	}
	else
	{
		int u=-1,v;
		for(int i=0;i<n;i++)
		{
			if(d[i]==mind)
			{
				u<0?u=i:(v=i);
			}
		}
		ansb=f(u,v);
	}
	cout<<ansa<<" "<<ansb<<endl;
}


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

int max(int a,int b,int c,int n){
	if(b == n) b--;
	if(a == n) a--;
	if(b == c && a == c && c > 0) b--;
	if(b < a) return b;
	else return a;
}

int main()
{
	int sums = 0,suma = 0,sumb = 0,both = 0;
	int n;
	char c[10005][11];
	scanf("%d",&n);
	for(int i = 0;i < n;i++){
		scanf("%s",c[i]);
		for(int j = 0;j < strlen(c[i]) - 1;j++){
			if(c[i][j] == 'A' && c[i][j + 1] == 'B') sums++;
		}
		if(c[i][0] == 'B') sumb++;
		if(c[i][strlen(c[i]) - 1] == 'A') suma++;
		if(c[i][0]=='B' && c[i][strlen(c[i])-1]=='A') both++;
	}
	sums += max(suma,sumb,both,n);
	printf("%d",sums);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    int s[m],c[m];
    bool sw=1;
    for(int i=0;i<m;i++)
    {
        cin>>s[i]>>c[i];
        if(s[i]==1 and c[i]==0)
        {
            sw=0;
        }
    }
    if(!sw and n!=1)
    {
        cout<<"-1\n";
        return 0;
    }
    if(!sw)
    {
        cout<<"0\n";
        return 0;
    }
    int a[n+1];
    bool ch[n+1];
    for(int i=0;i<=n;i++)
    {
        a[i]=0;
        ch[i]=0;
    }
    if(n!=1)
        a[1]=1;
    for(int i=0;i<m;i++)
    {
        if(ch[s[i]])
        {
            if(a[s[i]]!=c[i])
            {
                cout<<"-1\n";
                return 0;
            }
        }
        else
        {
            a[s[i]]=c[i];
            ch[s[i]]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i];
    }
    cout<<"\n";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn =1e5+10;
struct node
{
    int val,num;
};
node a[maxn],b[maxn];
bool cmp(node a, node b)
{
    return a.val>b.val;
}
int main()
{
   int n;
   scanf("%d",&n);
   int q1=0,q2=0;
   for(int i=1;i<=n;i++)
   {
       int x;
       scanf("%d",&x);
       if(i&1)
       {
           a[x].val++;
           a[x].num=x;
           q1=max(q1,x);
       }
       else
       {
           b[x].val++;
           b[x].num=x;
           q2=max(q2,x);
       }
   }
   //cout << a[3].num << " " << a[3].val << endl;
   //cout << b[1].num << " " << b[1].val << endl;
   sort(a+1,a+1+q1,cmp);
   sort(b+1,b+1+q2,cmp);
   int ans=0;
 //  cout << a[1].num << " " << b[1].num << endl;
       if(a[1].num!=b[1].num)
       {
           ans+=n/2-a[1].val;
           ans+=n/2-b[1].val;
       }
       else
       {
           ans=min(n/2-a[1].val+n/2-b[2].val,n/2-a[2].val+n/2-b[1].val);
       }
       printf("%d",ans);
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#include <string>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define inf 1001001001
typedef pair<int,int> P;
#define mod 1000000007



int main(){
    int n;
    cin >> n;
    int count=1;
    vector<int>a(n);
    rep(i,n) cin >> a[i];
    int now=a[0];
    while(count<=n+1){
        if(now==2) break;
        now=a[now-1];
        count++;
    }
    if(count>=n) cout << -1 << endl;
    else cout << count << endl;
    return 0;
}

Measured memory_bytes: 640
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

	public static void main (String[] args) {

		Scanner sc = new Scanner(System.in);
		double r = sc.nextDouble();

		System.out.println(BigDecimal.valueOf(r*r*Math.PI).toPlainString() + " "
		+ BigDecimal.valueOf(2*Math.PI*r).toPlainString());

	}

}



Measured memory_bytes: 28248
#include <bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define ll long long
#define int long long
#define ull unsigned long long
#define ld long double
#define sb __builtin_popcount
#define vi vector<int>
#define vc vector<char>
#define ii pair<int, int>
#define pq priority_queue<ii, vector<ii>, greater<ii>>
#define vii vector<ii>
#define sz(a) int((a).size())
#define f(a, b) for (int i = a; i < b; i++)
#define fr(a, b) for (int i = a - 1; i >= b; i--)
#define tr(x, m) for (auto x : m)
#define in(a, b) (b.find(a) != string::npos)
#define minin(a) *min_element(all(a))
#define maxin(a) *max_element(all(a))
#define X first
#define Y second
#define fill(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define pf push_front
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) reverse(v.begin(), v.end())
#define unq(v) v.erase(unique(all(v)), v.end())
#define el "\n"
#define mx3(a, b, c) max(max(a, b), c)
#define mn3(a, b, c) min(min(a, b), c)
#define S(v) accumulate(all(v), (ll)0)
#define SP(n) cout.precision(n)
#define PI = atan(1.0) * 4.0;
#define MOD = 1e9 + 7;
void start() {
    fio;
    SP(10);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
int32_t main() {
    start();
    int a, b;
  cin>>a>>b;
    cout << a*b << el;
    return 0;
}

Measured memory_bytes: 3612
import * as fs from 'fs'
const inputs = fs.readFileSync('/dev/stdin', 'utf8').split(/\r?\n/)
const s = inputs.shift()!
const [k] = inputs.shift()!.split(' ').map(s => parseInt(s, 10))

const top = new Array<string | null>(k).fill(null)
function insert (str: string): boolean {
    // K <= 5 なので binary search するまでもないとおもうがはたして
    let i;
    for (i = k-1; i >= 0; i--) {
        const t = top[i]
        if (t != null && t < str) { break }
    }
    if (i == k-1) { return false }
    if (top[i+1] == str) { return true } // exists

    // insert
    top[k-1] = str
    for (let j = k-1; j-1 > i; j--) {
        const t = top[j]
        top[j] = top[j-1]
        top[j-1] = t
    }
    return true
}

for (let i = 0; i < s.length; i++) {
    for (let l = 1; i + l <= s.length; l++) {
        const sub = s.slice(i, i+l)
        if (!insert(sub)) {
            break
        }
    }
}

console.log(top[k-1])

Measured memory_bytes: 7724
#include<bits/stdc++.h>
using namespace std;
string s,t;
int dp[3001][3001];
int lcs(int i,int j)
{
	if(dp[i][j]!=-1)
	{
		return dp[i][j];
	}
	if(i==0 || j==0)
	{
		dp[i][j] = 0;
		return 0;
	}
	if(s[i-1]==t[j-1])
	{
		dp[i][j] = lcs(i-1,j-1) + 1;

		return dp[i][j];
	}
	else
	{
		dp[i][j] = max(lcs(i-1,j),lcs(i,j-1));

		return dp[i][j];
	}
}
int main()
{
	cin>>s>>t;
	for (int i = 0; i < 3001; ++i)
	{
		for(int j = 0; j < 3001;j++)
		{
			dp[i][j] = -1;
		}
	}
	int l1 = s.length();
	int l2 = t.length();
	int i = l1;
	int j = l2;
	int p = lcs(l1,l2);
	// for (int i = 0; i <= l1; ++i)
	// {
	// 	for(int j = 0; j <= l2 ;j++)
	// 	{
	// 		cout<<dp[i][j]<<" ";
	// 	}
	// 	cout<<endl;
	// }
	stack<char> my_stack;
	while(i && j)
	{
		if(dp[i][j]==dp[i-1][j])
		{
			i--;
		}
		else if(dp[i][j]==dp[i][j-1])
		{
			j--;
		}
		else
		{
			my_stack.push(s[i-1]);
			i--;
			j--;
		}
	}
	while(!my_stack.empty())
	{
		cout<<my_stack.top();
		my_stack.pop();
	}
	cout<<endl;
	
	return 0;
}

Measured memory_bytes: 35968
n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort()
count = 1
b = []

for i in range(n-1):
    if a[i] != a[i+1]:
        b.append(count)
        count = 1
    else:
        count += 1

b.append(count)

b.sort(reverse=True)

count = 0
if k < len(b):
    for i in range(k, len(b)):
        count += b[i]

print(count)

Measured memory_bytes: 24748
#include <stdio.h>
int main(){
int i,k,a[10],b[10],yo;
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
for(i=0;i<10;i++){
scanf("%d",&b[i]);
}
for(i=0;i<10;i++){
    for(k=i+1;k<10;k++){
        if(a[i]<a[k]){
            yo=a[i];
            a[i]=a[k];
            a[k]=yo;
        }

    }
}
for(i=0;i<10;i++){
    for(k=i+1;k<10;k++){
        if(b[i]<b[k]){
            yo=b[i];
            b[i]=b[k];
            b[k]=yo;
        }

    }
}
printf("%d %d\n",a[0]+a[1]+a[2],b[0]+b[1]+b[2]);




return 0;
}

Measured memory_bytes: 1784
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int k = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();
		System.out.println(solve(n, k, a));
		in.close();
	}

	private static long solve(int n, long k, int[] a) {
		long ret = 0;
		long c = k * (k - 1) / 2l;
		c %= 1000000007;
		for (int i = 0; i < n; i++) {
			long r1 = 0;
			long r2 = 0;
			for (int j = 0; j < i; j++)
				if (a[i] < a[j])
					r1++;
			for (int j = i + 1; j < n; j++)
				if (a[i] < a[j])
					r2++;
			ret += r1 * k;
			ret += (r1 + r2) * c;
			ret %= 1000000007;
		}
		return ret;
	}
}


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 1e9
#define ALL(v) v.begin(), v.end()

using namespace std;
typedef unsigned long long ll;

int main(){
    // read in integers
    ll n;
    cin >> n;

    map<string, int> dict; 
    int maxi = 0; 

    vector<string> s(n);
    for (int i = 0; i < n; i++){
        cin >> s[i];
        maxi = max(maxi, ++dict[s[i]]);
    }

    ll res = 0;
    if (maxi > 1) {
        cout << "NO" << endl;
    }
    else{
        cout << "YES" << endl;
    }
}

Measured memory_bytes: 26368
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
using ull = unsigned long long;
using P = pair<int, int>;


ull E = 1000000007;
ull e = 10007;
const double pi = 3.14159265359;


int main() {
    int N, M;
    cin >> N >> M;
    vector<int> X;
    for (int i = 0; i < M; ++i) {
        int x;
        cin >> x;
        X.push_back(x);
    }

    if (N >= M) {
        cout << 0 << endl;
        return 0;
    }

    sort(X.begin(), X.end());
    vector<int> L;
    for (int i = 0; i < M - 1; ++i) {
        L.push_back(X[i + 1] - X[i]);
    }
    sort(L.rbegin(), L.rend());
    int ans = X[M - 1] - X[0];
    for (int i = 0; i < N - 1; ++i) {
        ans -= L[i];
    }
    cout << ans << endl;
}


Measured memory_bytes: 1400
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		String a = sc.next();
		String b = sc.next();
		
		for(int i = 0; i < n; i++) {
			System.out.print(a.charAt(i) +""+ b.charAt(i));
		}
		
		System.out.println();
	}

}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
	int n,a,b;
	int i,j,ans,sum;
	string s;
 
	cin >> n >> a >> b;
 
	ans = 0;
	for (i = 1;i <= n;++i)
	{
		s=to_string(i);
		sum = 0;
		for (j = 0;j < s.length();++j)
			sum += s[j] - '0';

 		if (sum >= a && sum <= b)
			ans += i;
	}
 
	cout << ans << endl;
 
	return(0);
}


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

typedef long long ll;

#define REP(i, n) for(int i=0; i<(n); i++)
#define REPS(i, n) for(int i=0; i<=(n); i++)
#define FOR(i, a, b) for(int i=a; i<(b); i++)
#define FORS(i, a, b) for(int i=a; i<=(b); i++)
// for container x
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define SIZE(v) ((ll)(v).size())
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
#define LB(v, x) distance((v).begin(), lower_bound(ALL(v), (x)))
#define UB(v, x) distance((v).begin(), upper_bound(ALL(v), (x)))

struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#pragma endregion

int main(){
    int N;
    cin >> N;
    FORS(n, 1, N){
        int ans=0;
        for (int x=1; x*x<n; x++){
            for (int y=1; y<=x; y++){
                for (int z=1; z<=y; z++) {
                    if (x*x+y*y+z*z+x*y+y*z+z*x==n) {
                        if (x==y && y==z && x==z) ans++;
                        else if ((x==y&&y!=z)||(x==z&&x!=y)||(y==z&&z!=x)) ans+=3;
                        else ans+=6;
                    }
                }
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

Measured memory_bytes: 3580
from collections import defaultdict
N, K, L = map(int, input().split())
G = [[[], []] for i in range(N)]
R = [[i, i] for i in range(N)]
def root(i, j):
  if R[i][j] != i:
    R[i][j] = root(R[i][j], j)
  return R[i][j]
for i in range(K):
  p, q = map(int, input().split())
  rp = root(p - 1, 0)
  rq = root(q - 1, 0)
  if rp != rq:
    R[rp][0] = min(rp, rq)
    R[rq][0] = min(rp, rq)
for i in range(L):
  r, s = map(int, input().split())
  rr = root(r - 1, 1)
  rs = root(s - 1, 1)
  if rr != rs:
    R[rr][1] = min(rr, rs)
    R[rs][1] = min(rr, rs)
A = defaultdict(int)
for i in range(N):
  R[i][0] = root(i, 0)
  R[i][1] = root(i, 1)
  A[(R[i][0], R[i][1])] += 1
for i in range(N):
  print(A[(R[i][0], R[i][1])])

Measured memory_bytes: 102500
import java.io.*;
import java.util.Arrays;

public class Main{
	
	private static int[] mountains = new int[10];
	
	public static void main(String[] args) throws IOException{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		for(int i=0;i<10;i++){
			mountains[i] = Integer.parseInt(br.readLine());
		}
		
		Arrays.sort(mountains);
		
		for(int i=9;i > 6;i--){
			System.out.println(mountains[i]);
		}
	}

}

Measured memory_bytes: 9324
#include<bits/stdc++.h>
using namespace std;
#define int long long

vector<pair<int,int>> vec;

bool cmp(pair<int,int> a,pair<int,int> b){
  return a.second < b.second;
}

signed main(){

  int n; cin >> n;
  for(int i = 0; i < n; i++){
    int s,t; cin >> s >> t;
    vec.push_back({s,t});
  }
  sort(vec.begin(),vec.end(),cmp);

  int ans = 0;
  int t = 0;
  for(int i = 0; i < n; i++){
    if(t < vec[i].first){
      ans++;
      t = vec[i].second;
    }
  }
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 4928
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,int> PI;
typedef pair<P,P> PP;
#define rep(i,a,n) for(int i = a;i < n;i++)
#define LINF (ll)1e17
#define INF 1e9
#define MOD 1e9+7
#define sMOD 1e5
#define fs first
#define sc second





signed main(){
	int a,b,c,d,e,f,sugar,water,smemo,wmemo;
	cin >> a >> b >> c >> d >> e >> f;
	double memo = 0;
	bool flag = true;
	
	
	rep(i,0,51){
		rep(j,0,51){
			rep(k,0,51){
				rep(l,0,51){
					wmemo = 100*a*i+100*b*j;
					smemo = c*k+d*l;
					
					if((a*i+b*j)*e < smemo) continue;
					if(wmemo+smemo > f) continue;
					
					if(flag && smemo+wmemo != 0){
						memo = (double)(100*smemo)/(double)(smemo+wmemo);
						sugar = smemo;
						water = wmemo;
						flag = false;
					}
					else if(smemo+wmemo != 0 && memo < (double)(100*smemo)/(double)(smemo+wmemo)){
						memo = (double)(100*smemo)/(double)(smemo+wmemo);
						sugar = smemo;
						water = wmemo;
					}
					
					//cout << memo << " " << memo << (double)(100*smemo)/(double)(smemo+wmemo) << endl;
				}
			}
		}
	}
	
	cout << water+sugar << " " << sugar << endl;
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ALL(a)  (a).begin(),(a).end()
 
const ll mod = 1000000007;
const ll LINF = 1e13;
const ll LLINF = 1e18;
const ll ALPHABET = 26;
 
template <class T>
void Swap(T& r, T& l) {
  T tmp = r;
  r = l;
  l = tmp;
}
 
template<typename T>
istream& operator >> (istream& is, vector<T>& vec) {
  for (T& x : vec) is >> x; //for(int i=0; i<vec.size(); i++) is >> x[i]; とかでもいい
  return is;
}


int main(){

    ll n,k,q;
    cin >> n >> k >> q;

    vector<ll> a(q);
    cin >> a;

    vector<ll> np(n,k-q);

    for(auto aa: a){
        np[aa-1]++;
    }


    for (ll i = 0; i < n; i++)
    {
        cout << ((np[i]> 0)?"Yes":"No") <<endl;;
    }
    

}


Measured memory_bytes: 2176
// AOJ 0120: Patisserie
// 2017.8.21 bal4u@uu
// 201711.20

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

int r[13];
double d[13][13];					// ???????????±???????????????????°´????????¢
double dp[13][4100];				// ?°´????????¢, ??´?????????????????±??????????±\??´?????????
char buf[50], *p;

int getint()
{
	int n = 0;
	while (*p >= '0') n = (n<<3) + (n<<1) + (*p++ & 0xf);
	return n;
}

int main()
{
	int w, n, i, ii, j, k;
	int lim;
	double x;

	for (i = 3; i <= 10; i++) for (ii = i*i, j = i; j <= 10; j++, ii+=i)
		d[i][j] = d[j][i] = 2 * sqrt(ii);

	while (fgets(p=buf, 50, stdin) != NULL) {
		w = getint(); p++;
		n = 0, j = 0;
		while (*p >= ' ') {	i = getint(), p++, r[n++] = i, j += i; }
		if (2 * j <= w) { puts("OK"); continue; }

		memset(dp, 0x55, sizeof(dp));
		for (i = 0; i < n; i++)	dp[i][1<<i] = r[i];

		lim = (1 << n) - 1;
		for (k = 0; k <= lim; k++) {
			for (ii = 1, i = 0; i < n; i++, ii <<= 1) {
				if (k & ii) continue;
				for (j = 0; j < n; j++) {
					if (dp[j][k] > 256) continue;
					x = dp[j][k] + d[r[i]][r[j]];
					if (x < dp[i][k|ii]) dp[i][k|ii] = x;
				}
			}
		}

		for (x = 1000, i = 0; i < n; i++) {
			if (dp[i][lim] == 0) continue;
			dp[i][lim] += r[i];
			if (dp[i][lim] < x) x = dp[i][lim];
		}
		puts(x <= w ? "OK" : "NA");
	}
	return 0;
}

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

using ll=long long;
const ll MOD=1000000007;

int main() {
  vector<vector<int>> c(3,vector<int>(3));
  bool AC=true;
  for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
      cin >> c[i][j];
    }
  }
  for(int i=0;i<3;i++){
    for(int j=0;j<2;j++){
      if(c[1+j][0]-c[0][0]!=c[1+j][i]-c[0][i]){
        AC=false;
      }
    }
  }
  for(int i=0;i<3;i++){
    for(int j=0;j<2;j++){
      if(c[0][1+j]-c[0][0]!=c[i][1+j]-c[i][0]){
        AC=false;
      }
    }
  }
  if(AC)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
#pragma region

#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define _rep3(i, begin, end) for (register int i = (begin), i##_end = (end); i < i##_end; i++)
#define _irep3(i, begin, end) for (register int i = (end)-1, i##_begin = (begin); i >= i##_begin; i--)
#define _rep2(i, n) _rep3(i, 0, n)
#define _irep2(i, n) _irep3(i, 0, n)
#define _overload3(_1, _2, _3, name, ...) name
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define irep(...) _overload3(__VA_ARGS__, _irep3, _irep2, )(__VA_ARGS__)
#define out(x) cout << (x) << '\n';
#define debug(x) cerr << #x << " = " << (x) << " (line: " << __LINE__ << ") " << endl;
typedef long long ll;

template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "vec[ ";
  rep(i, vec.size()) i == 0 ? os << vec[i] : os << ", " << vec[i];
  os << " ]";
  return os;
}

#pragma endregion

#define MOD 1000000007
#define INF 2147483647

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(16);

  string S;
  cin >> S;
  out(S == "Sunny" ? "Cloudy" : S == "Cloudy" ? "Rainy" : "Sunny");
}


Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <algorithm>

#define LINF 1000000000000000

typedef long long ll;

using namespace std;

int main() {
    ll n, ans = LINF;
    scanf("%lld", &n);
    vector<ll> a(n + 1);
    for (ll i = 1; i <= n; ++i) {
        scanf("%lld", a.begin() + i);
        a.at(i) += a.at(i - 1);
    }
    for (ll i = 1; i < n; ++i) ans = min(ans, abs(a.at(n) - 2 * a.at(i)));
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 1792
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define N 100

void searchIndex(int mat[][N], int n, int obj, int *row, int *col);
void changeGroup(int group[], int n, int g_old, int g_new);

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

	int group[N];
	for (int i = 0; i < n; i++) group[i] = i;

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

	int e[N * N];
	int k = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			e[k++] = a[i][j];
		}
	}

	sort(e, e + k);

	int p = 0;
	while (e[p] == -1) p++;

	int cnt = 0;
	for (int q = p; q < k; q++) {
		int i, j;
		searchIndex(a, n, e[q], &i, &j);
		if (group[i] == group[j]) continue;
		cnt += e[q];
		if (group[i] > group[j]) changeGroup(group, n, group[i], group[j]);
		else changeGroup(group, n, group[j], group[i]);
	}

	cout << cnt << endl;

	return 0;
}

void searchIndex(int mat[][N], int n, int obj, int *row, int *col)
{
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (mat[i][j] == obj) {
				mat[i][j] = -1;
				*row = i;
				*col = j;
				return;
			}
		}
	}
}

void changeGroup(int group[], int n, int g_old, int g_new)
{
	for (int i = 0; i < n; i++) {
		if (group[i] == g_old) group[i] = g_new;
	}
}

Measured memory_bytes: 3132
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int p[100010];
set<int> idx[100010];
int root(int x){
	if(p[x] == x) return x;
	return p[x] = root(p[x]);
}
void merge(int a, int b){
	a = root(a);
	b = root(b);
	if(a == b) return;
	if(idx[a].size() < idx[b].size()) swap(a, b);
	for(int i : idx[b]) idx[a].insert(i);
	p[b] = a;
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	
	int n; cin >> n;
	map<int, vi> yoko, tate;
	vi ys;
	rep(i, n){
		int a, b; cin >> a >> b;
		yoko[b].pb(a);
		tate[a].pb(b);
		ys.pb(b);
	}
	sort(all(ys)); ys.erase(unique(all(ys)), ys.end());
	rep(i, ys.size()){
		p[i] = i;
		idx[i].insert(i);
	}
	
	for(const auto &it : tate){
		const vi &t = it.second;
		for(int i = 0; i + 1 < t.size(); i++){
			int a = lower_bound(all(ys), t[i]) - ys.begin();
			int b = lower_bound(all(ys), t[i + 1]) - ys.begin();
			merge(a, b);
		}
	}
	
	ll ans = 0;
	rep(r, ys.size()){
		if(root(r) != r) continue;
		
		set<int> xs;
		ll cnt = 0;
		for(int j : idx[r]){
			cnt += yoko[ys[j]].size();
			for(int i : yoko[ys[j]]) xs.insert(i);
		}
		//dbg(it, r); dbg(xs, idx[r]);
		ans += (ll)xs.size() * idx[r].size() - cnt;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 33784
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int main(){
    int N, M; cin >> N >> M;
    long long alone = 0, bi = 0, crowd = 0;
    vector<vector<int>> g(N);
    for(int i=0;i<M;i++){
        int a, b; cin >> a >> b;
        --a; --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> mark(N, -1);
    for(int i=0;i<N;i++){
        if(mark[i] != -1) continue;
        if(g[i].empty()){
            ++alone;
            continue;
        }
        queue<int> qu; qu.push(i);
        mark[i] = 0;
        bool b = true;
        while(!qu.empty()){
            int p = qu.front(); qu.pop();
            for(auto t : g[p]){
                if(mark[t] == -1){
                    mark[t] = 1 - mark[p];
                    qu.push(t);
                } else {
                    if(mark[t] + mark[p] != 1) b = false;
                }
            }
        }
        if(b) ++bi;
        else ++crowd;
    }
    long long res = crowd + 2*bi + 2 * bi * (bi - 1) + 2 * bi * crowd + crowd * (crowd - 1);
    res += (long long)N * N - (N - alone) * (N - alone);
    cout << res << endl;
}

Measured memory_bytes: 7296
import sys
input = sys.stdin.readline

def main():
    while True:
        M, T, P, R = map(int, input().split())
        if M == 0:
            break
        results = [[i,0,0] for i in range(T)]
        if R > 0:
            logs = [list(map(int, input().split())) for i in range(R)]
            logs = sorted(logs, key=lambda x:(x[1], x[2], x[0]))
            penalty = 0
            problem = logs[0][2]
            team = logs[0][1] - 1
            penalty = 0
            i = 0
            while i < len(logs):
                m, t, p, j = logs[i]
                t -= 1
                if team == t:
                    if problem == p:
                        if j != 0:
                            penalty += 1
                        else:
                            results[team][1] += 1
                            results[team][2] += m + penalty*20
                        i += 1
                    else:
                        penalty = 0
                        problem = p
                else:
                    team = t
                    penalty = 0
                    problem = p
        results = sorted(results, key=lambda x:(-x[1], x[2], -x[0]))
        print(results[0][0]+1, end="")
        for i in range(1, T):
            if results[i][1] == results[i-1][1] and results[i][2] == results[i-1][2]:
                print("=",end="")
            else:
                print(",",end="")
            print(results[i][0]+1,end="")
        print()




if __name__ == "__main__":
    main()



Measured memory_bytes: 6456
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N,M = LI()

st = set()
for i in range(M):
    st.add(int(input()))

pat1 = 1
pat2 = 0

for i in range(1, N):
    tmp = pat2
    pat2 = pat1
    pat1 = ((pat1 + tmp)%MOD) if not i in st else 0
    # print('pat1: {} pat2: {} i:{}'.format(pat1, pat2, i))
print((pat1+pat2)%MOD)


Measured memory_bytes: 12012
#include<bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm( a, b ) ((a)/__gcd(a,b)*(b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair< LL, LL > L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 100005;

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

    int n;
    cin >> n;
    vector< int > v;
    for( int i = 0; i < n; ++i ) {
        int a;
        cin >> a;
        v.pb( a );

    }
    sort( v.begin(), v.end(), greater<int>() );

    int c = 0;
    int res1 = -1, res2 = -1;
    for( int i = 0; i < n - 1; ++i ) {
        if( v[i] == v[i + 1] ) {
            if( c == 0 ) {
                res1 = v[i];
                ++c;

            } else {
                res2 = v[i];
                break;

            }
            ++i;
        }
    }
    if( res2 == -1 ) cout << 0 << endl;
    else cout << (LL)res1 * (LL)res2 << endl;

    return 0;
}



Measured memory_bytes: 892
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <bits/stdc++.h>
    
    
using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cout(n) cout<<fixed<<setprecision(n)
    
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a,b;
    cin>>a>>b;
    cout<<max(0,(a-2*b))<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
#define EPS (1e-10)
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

signed main(){
	ll n,k; cin>>n>>k;
	ll ans=k*pow(k-1,n-1);
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 384