akhauriyash/Code-Regression huggingface dataset:
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 100000000
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i,n,s) for (int i= (int)(n); i >= (int)(s); i--)
#define mp make_pair
ll mod = 1000000007;
ll mod2 = 998244353;

int main(){
	cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin>>n;
    vector<ll> a(n);
    rep(i,0,n) cin>>a[i];
    sort(a.rbegin(),a.rend());
    ll ans = 0;
    priority_queue<ll> x;
    ans += a[0];
    x.push(a[1]);
    x.push(a[1]);
    rep(i,2,n){
        ll p = x.top();
        x.pop();
        ans += p;
        x.push(a[i]);x.push(a[i]);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 6860
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
  int a[5];
  while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]){
    sort(a,a+5);
    cout<<a[4]<<" "<<a[3]<<" "<<a[2]<<" "<<a[1]<<" "<<a[0]<<endl;
  }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#include <boost/math/constants/constants.hpp>
using namespace std;
typedef long long ll;
long double e = boost::math::constants::e<long double>();
long double pi = boost::math::constants::pi<long double>();
// 条件を見落としていませんか？
int main () {
    int K, N;
    cin >> K >> N;
    int ans = K;
    vector<int> A(N);
    int m = 0;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (i) {
            m = max(m, A[i]-A[i-1]);
        }
    }
    m = max(m, A[0]+K-A[N-1]);
    ans -= m;
    cout << ans << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define md 1000000007
#define fi first
#define se second
#define mp make_pair
#define	inf 1000000000
#define li 100005
#define int long long
#define pb push_back
using namespace std;
int n,A[li],B[li],say,cev;
priority_queue<int> q;
main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&A[i]);
	}
	for(int i=1;i<=n;i++){
		scanf("%lld",&B[i]);
	}
	for(int i=1;i<=n;i++){
		if(A[i]>B[i]){
			q.push(A[i]-B[i]);
		}
		else if(B[i]>A[i]){
			cev++;
			say+=B[i]-A[i];
		}
	}
	while(say>0 && !q.empty()){
		say-=q.top();
		q.pop();
		cev++;
	}
	if(say>0){
		printf("-1\n");
		return 0;
	}
	printf("%lld\n",cev);
	return 0;
}


Measured memory_bytes: 2428
#include <bits/stdc++.h>
using namespace std;
int main(){
    int R;cin >> R;
    if(R<1200){
        cout << "ABC" << endl;
    }else if(R<2800) {
        cout << "ARC" << endl;
    }else{
        cout << "AGC" << endl;
    }
    return 0;
}


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

h = [0 for _i in range(n)]+[3]
result = 1
mod = 10**9 +7
for i in a:
    result = (result*(h[i-1]-h[i]))%mod
    h[i] += 1
print(result)

Measured memory_bytes: 57796
// fahadmd16(IIEST Shibpur)
#include<bits/stdc++.h>
using namespace std;

#define ll                                      long long
#define pb                                      push_back
#define pf                                      push_front
#define eb                                      emplace_back
#define mp                                      make_pair
#define ff                                      first
#define ss                                      second
#define db                                      double
#define pll                                     pair<ll, ll>
#define ALL(a)                                  (a).begin(),(a).end()
#define endl                                    "\n"

#define forn(i, n) for (ll i = 0; i < n; i++)
#define forn1(i, n) for (ll i = 1; i < n; i++)
#define ford(i, n) for (ll i = n - 1; i >= 0; i--)
#define ford1(i, n) for (ll i = n - 1; i >= 1; i--)
#define forab(i, a, b) for(ll i = a; i < b; i++)
#define forabd(i, a, b) for(ll i = a; i >= b; i--)
#define eps 1e-10

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)


#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define trace1(x)                cout<<#x<<": "<<x<<endl
#define trace2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define tracen( x , n ){ cout<<(#x)<<":";for(int i=0;i<n;i++)cout<<(#x)<<"["<<i<<"]:"<< x[i] << "    "; cout<<endl;}
#define tracenm( x , n , m ){ cout<<(#x)<<endl;for(int i=0;i<n;i++){ cout<<"Row #"<<i<<":"; for(int j=0;j<m;j++) cout<<x[i][j]<<"     "; cout << endl;}}
#define trace(v){ cout<<#v<<": ";for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;}
using namespace std;



const ll sz = 3e4;
const ll mod = 1e9 + 7;
const db PI = acos(-1);



typedef vector<ll> VL;
typedef vector<pll > VLL;
typedef vector<vector<ll> > VVL;



ll power(ll n, ll k, ll m) {
	ll ans = 1;
	while (k) {
		if (k & 1)ans = (ans * n) % m;
		k /= 2;
		n = (n * n) % m;
	}
	return ans;
}




void solver() {
	ll n, k; cin >> n >> k;
	VL a(n);
	forn(i, n)cin >> a[i];


	bool dp[2][k + 1];
	forn(i, k + 1) {dp[0][i] = 0; dp[1][i] = 0;}
	forn1(i, k + 1) {
		for (ll x : a) {
			if (i >= x) {
				if (!dp[1][i - x])
					dp[0][i] = 1;
				if (!dp[0][i - x])
					dp[1][i] = 1;
			}

		}
	}
	cout << (dp[0][k] ? "First\n" : "Second\n");



}


int main() {

	IOS;
	ll T = 1; //cin >> T;


	forn1(i, T + 1) {
		clock_t z = clock();
		solver();
		debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
	}
	return 0;
}

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

string base;
string str;
int n;

bool isNumber( char a ) {
    return a >= '0' && a <= '9';
}

int judge( string& num1, string& num2 ) {
    if( num1.size() != num2.size() ) {
        return num1.size() - num2.size();
    }
    for( int i = 0; i < num1.size(); ++i ) {
        if( num1[i] != num2[i] ) {
            return num1[i] - num2[i];
        }
    }
    return 0;
}

bool judge() {
    int i = 0, j = 0;
    while( i < base.size() && j < str.size() ) {
        if( isNumber(base[i]) && isNumber(str[i]) ) {
            string num1 = "";
            while( i < base.size() && isNumber(base[i]) ) {
                num1 += base[i];
                ++i;
            }
            string num2 = "";
            while( j < str.size() && isNumber(str[j]) ) {
                num2 += str[j];
                ++j;
            }
            int res = judge(num1,num2);
            if( res < 0 ) {
                return true;
            } else if( res > 0 ) {
                return false;
            }
        } else if( isNumber(base[i]) ) {
            return true;
        } else if( isNumber(str[j]) ) {
            return false;
        } else if( base[i] == str[j] ) {
            ++i, ++j;
        } else {
            return base[i] < str[j];
        }
    }
    if( i != base.size() ) {
        return false;
    }
    return true;
}

int main() {
    cin>>n;
    cin>>base;
    while( n-- ) {
        cin>>str;
        if( judge() ) {
            cout<<"+"<<endl;
        } else {
            cout<<"-"<<endl;
        }
    }

    return 0;
}



Measured memory_bytes: 3204
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main(){
  ll mod=998244353;
  ll A,B,C,D;
  cin>>A>>B>>C>>D;
  if(A==C and B==D) cout<<1<<endl;
  else if(A==C){
    ll ans=1;
    rep(i,D-B) ans=ans*C%mod;
    cout<<ans<<endl;
  }else if(B==D){
    ll ans=1;
    rep(i,C-A) ans=ans*D%mod;
    cout<<ans<<endl;
  }else{
    VVL X(C-A+1,VL(D-B+1));
    X[0][0]=1;
    rep(i,C-A+1){
      rep(j,D-B+1){
        if(i) (X[i][j]+=X[i-1][j]*(j+B)%mod)%=mod;
        if(j) (X[i][j]+=X[i][j-1]*(i+A)%mod)%=mod;
        if(i and j) (X[i][j]+=mod-X[i-1][j-1]*(i+A-1)%mod*(j+B-1)%mod)%=mod;
        if(X[i][j]<0) X[i][j]+=mod;
      }
    }
    cout<<X[C-A][D-B]<<endl;
  }
}

Measured memory_bytes: 73660
import sys

stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

n,m = nm()
a = sum(nm())
print(-1 if a > n else n-a)


Measured memory_bytes: 9552
#include <iostream>

using namespace std;

int main(int argc, const char *argv[]) {
  long int N, A, B;
  long int blue = 0;
  cin >> N >> A >> B;

  if(A == 0){
    cout << 0 << endl;
    return 0;
  }
  blue = (N / (A + B)) * A;
  if(N % (A + B) <= A){
    blue += (N % (A + B));
  }else{
    blue += A;
  }
  cout << blue << endl;

  return 0;
}


Measured memory_bytes: 888
#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;

void solve()
{
    ll a,b,c,d;
    cin >>a >> b >> c >> d;

    if (b<=0 and c >= 0) {
        cout << b*c << '\n';
    }
    else if (a>=0 and d<=0) {
        cout << d*a << '\n';
    }
    else if (b<=0 and d<=0) {
        cout << a*c << '\n';
    }
    else if (a>=0 and c>=0) {
        cout << b*d << '\n';
    }
    else if (b >= 0 and d >= 0 and a<=0 and c<=0) {
        cout << max(b*d,a*c) << '\n';
    }
    else if (b <= 0 and c<=0 and d>=0) {
        cout << a*c << '\n';
    }
    else if (a>=0 and c<=0 and d>=0) {
        cout << b*d << '\n';
    }
    else if (d<=0 and a<=0 and b>=0) {
        cout << a*c << '\n';
    }
    else if (c>=0 and a<=0 and b>=0) {
        cout << b*d << '\n';
    }
}

int main()
{
    fastIO;
    int t=1;
    // cin >> t;
    while (t--) {
        solve();
    }   
}

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

using namespace std;

int main() {
	int N, L; cin >> N >> L;
	int res = 0;
	pair<int, int> a[N];
	for (int i = 0; i < N; i++) {
		int x = L + i;
		res += x;
		a[i] = make_pair(abs(x), x);
		// cout << x << endl;
	}

	sort(a, a + N);
	// cout << res - a[0];
	// for (int i = 1; i < N; i++) cout << a[i].first << a[i].second << endl;
	cout << res - a[0].second;
	return 0;
}



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

#define REPI(i, n) for(int i = 0; i < n; i++)
#define REPJ(j, n) for(int j = 0; j < n; j++)

using ll = long long;
using namespace std;

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

    int n, d, ans = 0; cin >> n >> d;
    d = 2 * d + 1;
    for(; n > 0; ans++) n -= d;
    cout << ans;
}


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


int main()
{
    int n ;
    cin >> n  ;
    ll sum =  0 ;
    for(int i = 1 ; i <= n ; ++i)
    {
        if(i%3 != 0 && i%5!= 0)
            sum += i ;
    }
    cout << sum ;
    return 0;
}


Measured memory_bytes: 3636
n = int(input())
cnt = [ 0 for i in range(10**5+5) ]
a = [ int(i) for i in input().split() ]
ans = 0
for i in a:
    if i <= 10**5:
        cnt[i] += 1
    else:
        ans += 1    
for i in range(10**5+5):
    if cnt[i] < i:
        ans += cnt[i]
    else:
        ans += (cnt[i] - i)
print(ans)


Measured memory_bytes: 15684
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 200005
#define X first
#define H second
using namespace std;

typedef long long ll;
ll n, d, a, sum, ans, s[N];
pair<ll, ll> b[N];
int main()
{
	ll i, j;
	cin >> n >> d >> a;
	for (i = 0; i < n; i++) {
		scanf("%lld %lld", &b[i].X, &b[i].H);
	}
	sort(b, b + n);
	for (i = j = 0; i < n; i++) {
		for (; j < i && b[i].X - b[j].X > 2 * d; j++) sum -= s[j];
		s[i] = max(0LL, (b[i].H + a - 1) / a - sum);
		ans += s[i];
		sum += s[i];
	}
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 4992
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1001
static const int INFTY = (1<<21);

void bfs(int s);
void enqueue(int x);
int dequeue();

int n, head, tail;
int M[N][N], d[N], Q[N*2];

int main(){
  int  u, k, v, i, j; 

  scanf("%d", &n);

  for ( i = 0; i <n; i++ ){
    for ( j = 0; j <n; j++ ) M[i][j] = 0;
  }
  
  for ( i = 0; i < n; i++ ){
    scanf("%d %d", &u, &k);
    u--;
    for ( j = 0; j < k; j++ ){
      scanf("%d", &v);
      v--;
      M[u][v] = 1;
    }
  }
  
  bfs(0);

  return 0;
}

void enqueue(int x) {
	tail += 1;
  Q[tail] = x;
}

int dequeue() {
  int x;
  head += 1; 
  x = Q[head];
  return x;
}

void bfs(int s) {
  int i, u, v;
  head = tail = 1;
  enqueue(s);
  for ( i = 0; i < n; i++ )d[i] = INFTY;
  d[s] = 0;
  while(head != tail){
    u = dequeue();
    for ( v = 0; v < n; v++ ){
      if ( M[u][v] == 0 ) continue;
      if ( d[v] != INFTY ) continue;
      d[v] = d[u] + 1;
      enqueue(v);
    }
  }
  for ( i = 0; i < n; i++ ){
    printf("%d %d\n", i+1,((d[i]==INFTY)?(-1):d[i]));
  }
}



Measured memory_bytes: 2132
# 高橋くんの家には電源プラグの差込口が 1 口しかありません。
# そこで、高橋くんは A 個口の電源タップをいくつか使って未使用の差込口を B 口以上に拡張したいと考えています。
# A 個口の電源タップ 1 つと未使用の差込口 1 口を使って、新たに差込口を A 口増やすことができます。
# 最小でいくつの電源タップが必要でしょうか。

input = gets.chomp.split
a = input[0].to_i
b = input[1].to_i

power_socket = 1
power_strip = 0
while power_socket < b
    power_socket += a - 1
    power_strip += 1
end

puts power_strip

Measured memory_bytes: 1788
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
    public static void main(String args[])throws IOException
    {
        Scanner obj=new Scanner(System.in);
        int a=obj.nextInt();
        int s=a+(a*a)+(a*a*a);
        System.out.println(s);
    }
}

Measured memory_bytes: 27080
import Control.Monad
import Control.Applicative
import Data.List


main :: IO()
main = do
  list <- replicateM 5 readLn :: IO [Int]
  k <- readLn :: IO Int
  let list' = loop list
  let ans = if all (<=k) list'
              then "Yay!"
              else ":("
  putStrLn ans


loop :: [Int] -> [Int]
loop [x] = []
loop (x:xs) = [ d | y <- xs, let d = y - x] ++ loop xs


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

int main() {
  int a;
  while (cin >> a, a) {
    int t, b = 0;
    for (int i = 0; i < 9; i++) {
      cin >> t;
      b += t;
    }
    cout << a - b << "\n";
  }
}


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

int main()
{
    int A, B, C, X, Y;
    cin >> A >> B >> C >> X >> Y;

    if (A + B < 2 * C)
    {
        cout << A * X + B * Y << endl;
        return 0;
    }

    int ans = min(X, Y);
    X -= ans;
    Y -= ans;

    ans *= 2 * C;
    if (X > 0)
    {
        if (A < 2 * C)
        {
            ans += A * X;
        }
        else
        {
            ans += 2 * C * X;
        }
    }
    else if (Y > 0)
    {
        if (B < 2 * C)
        {
            ans += B * Y;
        }
        else
        {
            ans += 2 * C * Y;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <math.h>
//#include <algorithm>
//#include <vector>

int main(int argc, char* argv[])
{
    long a, b;
    scanf("%ld %ld", &a, &b);

    printf("%d\n", (a-1)*(b-1));

    return 0;
}


Measured memory_bytes: 128
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

int read() {
	char ch;
	for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
	int x=ch-'0';
	for(ch=getchar();ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

const int N=2e5+5,inf=0x7fffffff;

struct Segment_Tree{
	int mx[N<<2];
	void modify(int v,int l,int r,int x,int y) {
		if (l==r) {mx[v]=y;return;}
		int mid=l+r>>1;
		if (x<=mid) modify(v<<1,l,mid,x,y);
		else modify(v<<1|1,mid+1,r,x,y);
		mx[v]=max(mx[v<<1],mx[v<<1|1]);
	}
	int query(int v,int l,int r,int x,int y) {
		if (x>y) return 0;
		if (x<=l&&r<=y) return mx[v];
		int mid=l+r>>1,tmp=-inf;
		if (x<=mid) tmp=max(tmp,query(v<<1,l,mid,x,y));
		if (y>mid) tmp=max(tmp,query(v<<1|1,mid+1,r,x,y));
		return tmp;
	}
}tr[2];

int n,p[N],cnt[N],mx;

bool chk(int x,int mx0,int c0,int mx1,int c1) {
	int v0=c0+cnt[x]-c1,v1=c1+cnt[x]-c0;
	if (v0>=0&&tr[v0&1].query(1,1,n,mx1+1,n)>=v0) return 1;
	if (v1>=0&&tr[v1&1].query(1,1,n,mx0+1,n)>=v1) return 1;
	return 0;
}

void print(string s) {
	int n=s.length();
	fo(i,0,n-1) putchar(s[i]);
}

int main() {
	n=read();
	fo(i,1,n) {
		p[i]=read();
		if (p[i]>mx) mx=p[i],cnt[i]=1;
	}
	fo(i,1,n<<2) tr[1].mx[i]=-inf;
	fd(i,n,1) {
		int f0=tr[0].query(1,1,n,p[i],n);
		int f1=tr[1].query(1,1,n,p[i],n);
		if (cnt[i]) {
			tr[0].modify(1,1,n,p[i],f0+2);
			tr[1].modify(1,1,n,p[i],f1+2);
		} else {
			tr[1].modify(1,1,n,p[i],f0+1);
			tr[0].modify(1,1,n,p[i],f1+1);
		}
	}
	fd(i,n,1) cnt[i]+=cnt[i+1];
	string s="";int mx0=0,mx1=0,c0=0,c1=0;
	fo(i,1,n) {
		tr[0].modify(1,1,n,p[i],0);
		tr[1].modify(1,1,n,p[i],-inf);
		if (chk(i+1,max(mx0,p[i]),c0+(p[i]>mx0),mx1,c1)) {
			c0+=p[i]>mx0;mx0=max(mx0,p[i]);
			s.push_back('0');
		} else {
			c1+=p[i]>mx1;mx1=max(mx1,p[i]);
			s.push_back('1');
		}
	}
	if (c0==c1) print(s);
	else puts("-1");
	return 0;
}

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

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

    if (A != 1 && B != 1) cout << 1 << endl;
    else if (A != 2 && B != 2) cout << 2 << endl;
    else if (A != 3 && B != 3) cout << 3 << endl;

}

Measured memory_bytes: 3624
use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let h: usize = iterator.next().unwrap().parse().unwrap();
    let w: usize = iterator.next().unwrap().parse().unwrap();
    let mut map: Vec<Vec<char>> = Vec::new();
    for _ in 0..h {
        map.push(iterator.next().unwrap().chars().collect());
    }

    let mut count: usize = 0;
    let mut visited = vec![vec![false; w]; h];
    for y in 0..h {
        for x in 0..w {
            if visited[y][x] {
                continue;
            }

            let mut black: usize = 0;
            let mut white: usize = 0;

            let mut stack: Vec<(usize, usize)> = Vec::new();
            stack.push((y, x));
            visited[y][x] = true;
            if map[y][x] == '.' {
                white += 1;
            } else {
                black += 1;
            }
            while !stack.is_empty() {
                let top = *stack.last().unwrap();
                if top.1 > 0 && !visited[top.0][top.1 - 1] && map[top.0][top.1 - 1] != map[top.0][top.1] {
                    stack.push((top.0, top.1 - 1));
                    visited[top.0][top.1 - 1] = true;
                    if map[top.0][top.1 - 1] == '.' {
                        white += 1;
                    } else {
                        black += 1;
                    }
                } else if top.0 > 0 && !visited[top.0 - 1][top.1] && map[top.0 - 1][top.1] != map[top.0][top.1] {
                    stack.push((top.0 - 1, top.1));
                    visited[top.0 - 1][top.1] = true;
                    if map[top.0 - 1][top.1] == '.' {
                        white += 1;
                    } else {
                        black += 1;
                    }
                } else if top.1 + 1 < w && !visited[top.0][top.1 + 1] && map[top.0][top.1 + 1] != map[top.0][top.1] {
                    stack.push((top.0, top.1 + 1));
                    visited[top.0][top.1 + 1] = true;
                    if map[top.0][top.1 + 1] == '.' {
                        white += 1;
                    } else {
                        black += 1;
                    }
                } else if top.0 + 1 < h && !visited[top.0 + 1][top.1] && map[top.0 + 1][top.1] != map[top.0][top.1] {
                    stack.push((top.0 + 1, top.1));
                    visited[top.0 + 1][top.1] = true;
                    if map[top.0 + 1][top.1] == '.' {
                        white += 1;
                    } else {
                        black += 1;
                    }
                } else {
                    stack.pop();
                }
            }
            count += white * black;
        }
    }

    return count.to_string();
}


Measured memory_bytes: 11380
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using CHY72;

namespace CHY72{
    
    public class PointXY<T>where T:IComparable{
        public T X , Y;
        public bool Flag = false;
        public PointXY(T _X,T _Y) {
            X = _X; Y = _Y;
        }
        public static bool operator ==(PointXY<T> P1, PointXY<T> P2) {
            return (P1.X.CompareTo( P2.X) == 0 && P1.Y.CompareTo (P2.Y) == 0);
        }
        public static bool operator !=(PointXY<T> P1, PointXY<T> P2) {return !(P1 ==P2);}
        public override int GetHashCode() {return X.GetHashCode() ^ Y.GetHashCode();}
        public override bool Equals(object obj) {
            if (obj == null || this.GetType() != obj.GetType()) {return false;}
            return (this == (PointXY<T>)obj);
        }         

    }
    
}    

public class Program {
    static bool ReadLine_1(ref int num1, bool Fin0 = true) {
        string str = Console.ReadLine();
        num1 = int.Parse(str);
        return (Fin0 && num1 == 0);
    }
    static int  ReadLine_1() {
        return  int.Parse(Console.ReadLine());
    }
    static bool ReadLine_2(ref int num1, ref int num2, char Spliter = ' ', bool Fin0 = true) {
        string str = Console.ReadLine();
        num1 = int.Parse(str.Split(Spliter)[0]);
        num2 = int.Parse(str.Split(Spliter)[1]);
        return (Fin0 && num1 == 0);
    }
    static int[] ReadLine72(char Spliter = ' ') {
        return Console.ReadLine().Split(Spliter).Select(s => int.Parse(s)).ToArray();
    }

    const int DefaultMax = 727272;

    static void Main(string[] args) {
        while (true) {
            char[] str = Console.ReadLine().ToCharArray();
            if (str[0] == '.' && str.Length == 1) return;
            var isRoundPareList = new Stack<bool>();
            bool OK = true;
            
            //([)]とはならない
            for (int i = 0; i < str.Length; i++) {
                if (str[i] == '(') isRoundPareList.Push(true);
                else if (str[i] == '[') isRoundPareList.Push(false);
                else if (str[i] == ')') { 
                    if (isRoundPareList.Count == 0 || !isRoundPareList.Pop()) {
                        OK = false; break; 
                    }
                }else if (str[i] == ']') {
                    if (isRoundPareList.Count == 0 || isRoundPareList.Pop()) {
                        OK = false; break;
                    }
                }
            }
            if (isRoundPareList.Count > 0) OK = false;
            if (OK) Console.WriteLine("yes");
            else Console.WriteLine("no");
        }

    }
}

Measured memory_bytes: 5940
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

vector<pair<int,int>> d(4);

int H, W;

void check(int h, int w,vector<vector<char>>& V,vector<vector<bool>>& C,int& ptr){
  //黒の連結成分に含まれる黒の数をadjにpush_back
  ptr++;
    for(int i = 0; i < 4; i++){
      int hp = 0;
      hp = h + d[i].first;
      int wp = 0;
      wp = w + d[i].second;
      if(wp < 0 || wp > W-1 || hp < 0 || hp > H-1) continue;
      if(C[hp][wp]) continue;
      if(V[hp][wp] == '#'){
        C[hp][wp] = true;
        check(hp, wp, V, C, ptr);
      }
    }
  return;
}

int main(){
  d[0] = make_pair(1,0);
  d[1] = make_pair(0,1);
  d[2] = make_pair(-1,0);
  d[3] = make_pair(0,-1);
  
  cin >> H >> W;
  vector<vector<char>> V(H,vector<char> (W, 'x'));
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      cin >> V[i][j];
    }
  }
  vector<int> adj;
  vector<vector<bool>> C(H,vector<bool> (W,false));
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(C[i][j])continue;
      if(V[i][j] == '#'){
        int cnt = 0;
        check(i ,j ,V, C, cnt);
        adj.push_back(cnt);
      }
      C[i][j] = true;
    }
  }
  sort(adj.begin(), adj.end());
  if(adj.size() > 0 && adj[0] <= 1) cout << "No";
  else cout << "Yes";
}


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

#define ll long long
#define ar array

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

	string s;
	cin >> s;
	for(int i=0; i<s.size(); ++i)
		cout << "x";
}


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

int main(){
    int N, M;
    cin >> N >> M;
    map<int, pair<bool, int>> m{};
    int wa_cnt = 0;
    int ac_cnt = 0;
    for(int i = 0; i < M; i++ ){
        int num;
        string result;
        cin >> num >> result;
        if(!m.count(num)==0){
            m.emplace(num, make_pair(false, 0)); 
        }

        if(m[num].first == false && result == "WA"){
            m[num].second++;
        }
        else if (m[num].first == false && result == "AC"){
            m[num].first = true;
            wa_cnt += m[num].second;
            ac_cnt++;
        }
    }
    cout << ac_cnt << " " << wa_cnt << endl;
}


Measured memory_bytes: 6528
import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum mod = 10L^^9 + 7;
enum inf = 10^^9;

void main() {
    int n;
    scan(n);
    string s;
    scan(s);
    int q;
    scan(q);

    auto k = readln.split.to!(int[]);

    foreach (ki ; k) {
        auto ans = solve(n, s, ki);
        writeln(ans);
    }
}

long solve(int n, string s, int k) {
    auto mcs = new long[](n + 1);

    long cntm, cntc;
    long mc;

    foreach (i ; 0 .. k - 1) {
        if (s[i] == 'M') {
            cntm++;
        }
        else if (s[i] == 'C') {
            cntc++;
            mc += cntm;
        }
    }

    mcs[0] = mc;

    foreach (i ; k - 1 .. n) {
        if (s[i - (k - 1)] == 'M') {
            mc -= cntc;
            cntm--;
        }
        else if (s[i - (k - 1)] == 'C') {
            cntc--;
        }

        if (s[i] == 'M') {
            cntm++;
        }
        else if (s[i] == 'C') {
            mc += cntm;
            cntc++;
        }

        debug {
            //writeln(cntm, " ", cntc);
        }

        mcs[i - (k - 1) + 1] = mc;
    }

    debug {
        writeln(mcs);
    }

    foreach (i ; n - (k - 1) + 1 .. n + 1) {
        if (s[i-1] == 'M') {
            mc -= cntc;
            cntm--;
        }
        else if (s[i-1] == 'C') {
            cntc--;
        }

        mcs[i] = mc;
    }

    debug {
        writeln(mcs);
    }

    long ans;

    foreach (i ; 0 .. n) {
        if (s[i] == 'D') {
            ans += mcs[i + 1];
        }
    }

    return ans;
}





























void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

Measured memory_bytes: 30788
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <cstdlib>
#define rep(i,n) for(int i = 0;i<(n);i++);
#define all(a) (a).begin(),(a).end()
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 100100100;
typedef pair<ll, ll> P;
ll gcd(ll a, ll b) {//a>b
	if (a < b)swap(a, b);
	if (a % b == 0) return b;
	else return gcd(b, a % b);
}
int main() {
	ll N, A, B;
	cin >> N >> A >> B;
	string S;
	cin >> S;
	ll nowb = 0;
	ll now = 0;
	vector<bool> yes(N,false);
	for (int i = 0; i < N; i++) {
		if (S[i] == 'a' && A + B > now) {
			now++;
			yes[i] = true;
		}
		else if (S[i] == 'b' && A + B > now && nowb < B) {
			now++;
			nowb++;
			yes[i] = true;
		}
	}
	for (int i = 0; i < N; i++) {
		if (yes[i])cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}

Measured memory_bytes: 896
#include<stdio.h>

int main (void){
	long long int n,a[10010],i,m,M,S=0;
	scanf("%lld",&n);
	for(i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	m=a[1];M=a[1];
	for(i=1;i<=n;i++){
		if(m>a[i]){
			m=a[i];
		}
	}
	for(i=1;i<=n;i++){
		if(M<a[i]){
			M=a[i];
		}
	}
	for(i=1;i<=n;i++){
		S+=a[i];
	}
	printf("%lld %lld %lld\n",m,M,S);
	return 0;
}

Measured memory_bytes: 680
#include <iostream>
#include <vector>

using namespace std;

int gcd(int x, int y) {
	return y ? gcd(y, x % y) : x;
}

int main() {
	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; ++i) cin >> a[i];
	int ans = a[0];
	for (int i = 1; i < n; ++i) ans = gcd(ans, a[i]);
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
  int i,count=0,len;
  string number;
  cin >> number;
  len = number.length();
  for(i=0;i<len;i++)
  {
    char c = number[i];
    int a = c - '0';
    count += a;
  }
  if(count % 9 == 0)
  {
    cout << "Yes";
  }
  else
  {
    cout << "No";
  }
  
  return 0;
}

Measured memory_bytes: 3752
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int a[N];

int main()
{
	int n, k; scanf("%d%d", &k, &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	sort(a, a + n);
	a[n] = a[0] + k;
	int ans = k - (a[1] - a[0]);
	for (int i = 1; i < n; i++) {
		ans = min(ans, min(k - (a[i + 1] - a[i]), k - (a[i] - a[i - 1])));
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 1024
N, M = map(int, input().split())
d = {}
for i in range(M):
  P, Y = map(int, input().split())
  if P in d:
    d[P].append((Y, i))
  else:
    d[P] = [(Y, i)]
for i in d:
  d[i].sort()
ans = {}
for i in d:
  for j in range(len(d[i])):
    ans[d[i][j][1]] = 1000000000000 + 1000000 * i + j + 1
for i in range(M):
  print(str(ans[i])[1:])

Measured memory_bytes: 50180
n, a, b = map(int, input().split())
l = list(map(int, input().split()))
l.sort(reverse=True)
# print(l)
# print(l[:a])
print(sum(l[:a]) / a)
l_w = l[:a]
ans = 0
nCr = {}


def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n, r) in nCr: return nCr[(n, r)]
    nCr[(n, r)] = cmb(n - 1, r) + cmb(n - 1, r - 1)
    return nCr[(n, r)]


if l[0] == l[-1]:
    for i in range(a, b + 1):
        ans += cmb(b, i)
elif l_w[0] == l_w[-1]:
    for i in range(a, min(b + 1,l.count(l_w[-1])+1)):
        ans += cmb(l.count(l_w[-1]), i)
else:
    ans = cmb(l.count(l_w[-1]), l_w.count(l_w[-1]))
print(ans)


Measured memory_bytes: 3188
#![allow(unused_imports)]
#![allow(dead_code)]

use proconio::input;
use proconio::marker::{Bytes, Chars};
use std::collections::*;

fn main() {
    input! {
        a:i64,
    }
    let ans = a + a * a + a * a * a;
    println!("{}", ans);
}

Measured memory_bytes: 2148
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CF2016
{
    class Program
    {
        static void Main(string[] args)
        {
            string S = Console.ReadLine();
            if (S.Length >= 4)
            {
                if (S[0] == 'Y' && S[1] == 'A' && S[2] == 'K' && S[3] == 'I')
                {
                    Console.WriteLine("Yes");
                }
                else
                {
                    Console.WriteLine("No");
                }
            }
            else
            {
                Console.WriteLine("No");
            }
            
        }
    }
}


Measured memory_bytes: 11092
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
using LayoutKind = System.Runtime.InteropServices.LayoutKind;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using StructLayoutAttribute = System.Runtime.InteropServices.StructLayoutAttribute;
using FieldOffsetAttribute = System.Runtime.InteropServices.FieldOffsetAttribute;

static class P
{
    static void Main()
    {
        var cost = new int[] { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
        var n = Console.ReadLine().Split().Select(int.Parse).First();
        var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
        BigInteger[] largestNumber = Enumerable.Repeat(BigInteger.MinusOne, n + 10).ToArray();
        largestNumber[0] = 0;
        for (int i = 0; i < n; i++)
        {
            if (largestNumber[i] < 0) continue;
            for (int j = 0; j < a.Length; j++) largestNumber[i + cost[a[j]]] = BigInteger.Max(largestNumber[i + cost[a[j]]], largestNumber[i] * 10 + a[j]);
        }
        Console.WriteLine(largestNumber[n]);
    }
}


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

typedef long long ll;
typedef pair<int,int> pii;

long long MOD = 1000000000 + 7;

template<typename T>
T gcd(T a, T b) {
  if(a < b) swap(a,b);
    
  if(b == 0) return a;
  return gcd(b, a % b);
}
// セグメントツリー
typedef long long ll;
int const inf = INT_MAX / 2;

enum query_type { min_query, max_query, sum_query, gcd_query};

template<class T> struct segtree {
  int N;
  vector<T> dat, sum;
  function<T(T, T)> func;
  T ngvalue;

  void init(int n, function<T(T, T)> const& f, T ng) {
    N = 1;
    while(N < n) N <<= 1;
    dat.assign(2 * N - 1, 0);
    sum.assign(2 * N - 1, 0);
    func = f;
    ngvalue = ng;
  }

  segtree() = default;

  segtree(int n, query_type qtype) {
    if(qtype == min_query) {
      init(n, [](T a, T b){return min(a, b);}, numeric_limits<T>::max());
    }
    else if(qtype == max_query) {
      init(n, [](T a, T b){return max(a, b);}, numeric_limits<T>::min());
    }
    else if(qtype == sum_query) {
      init(n, [](T a, T b){return a + b;}, 0);
    }
    else if(qtype == gcd_query) {
      init(n, [](T a, T b){return gcd(a,b);}, 0);
    }
    else {
      runtime_error("no matching query type");
    }
  }

  void add(int i, T x) {
    add(i, i + 1, x);
  }

  void add(int a, int b, T x) {
    add(a, b, x, 0, 0, N);
  }

  T add(int a, int b, T x, int k, int l, int r) {
    if(b <= l || r <= a) return dat[k];
    if(a <= l && r <= b) {
      sum[k] += x;
      return dat[k] += x;
    }
    int m = (l + r) / 2;
    return dat[k] = func(add(a, b, x, 2 * k + 1, l, m), add(a, b, x, 2 * k + 2, m, r)) + sum[k];
  }

  T operator()(int a, int b) {
    return query(a, b, 0, 0, N);
  }

  T query(int a, int b, int k, int l, int r) {
    if(b <= l || r <= a) return ngvalue;
    if(a <= l && r <= b) return dat[k];
    int m = (l + r) / 2;
    return func(query(a, b, 2 * k + 1, l, m), query(a, b, 2 * k + 2, m, r)) + sum[k];
  }
};


int main(){
  cout << setprecision(10);
  int N; cin >> N;
  string S; cin >> S;

  vector<segtree<int>> v(26, segtree<int>(N, sum_query));
  for(int i = 0; i < S.size(); i++){
    int c = S[i] - 'a';

    v[c].add(i, 1);
  }

  int Q;
  cin >> Q;
  while(Q--){
    int tmp;
    cin >> tmp;
    if(tmp == 1){
      int tmp1;
      char tmp2;
      cin >> tmp1  >> tmp2;
      tmp1--;
      // tmp1の場所にあるものを調べて、
      for(int i = 0; i < 26; i++){
        if(v[i](tmp1, tmp1+1) > 0){
          v[i].add(tmp1, -1);
          break;
        }
      }
      v[tmp2-'a'].add(tmp1, 1);
    } else { 
      int tmp1, tmp2;
      cin >> tmp1 >> tmp2;
      tmp1--; tmp2--;
      int ans = 0;
      for(int i = 0; i < 26; i++){
        if(v[i](tmp1, tmp2+1) > 0){
          ans++;
        }
      }
      cout << ans << endl;
    }
  }



}


Measured memory_bytes: 222212
N,A,B = map(int,input().split())

count = 0
for i in range(1,N+1):
    tot = 0
    num = i
    while(num > 0):
        tot = tot + num%10
        num = int(num/10)
    if tot >= A and tot <= B:
        count += i

print(count)

Measured memory_bytes: 3060
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

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

using ll = long long;
const ll INF = 1ll<<60;

ll N;
ll A[100010], B[100010];
std::vector<ll> vs;
ll ans;

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

  rep( i, N )
    std::cin >> A[i] >> B[i], ans -= B[i], vs.emplace_back( A[i]+B[i] );

  std::sort( vs.rbegin(), vs.rend() );

  for( ll i = 0; i < N; i += 2 )
    ans += vs[i];

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

  return 0;
}

Measured memory_bytes: 2676
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  switch(N%10){
    case 0:
    case 1:
    case 6:
    case 8:{
      cout << "pon";
      break;
    }
    case 3:{
      cout << "bon";
      break;
    }
    default:{
      cout << "hon";
      break;
    }
  }
}
        
      

Measured memory_bytes: 3600
#include <iostream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <functional> // std::function<void(int)>
using namespace std;
#define int long long
#define All(v) (v).begin(),(v).end()
int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1};
int Dy[8] = {-1,-1,0,1,1,1,0,-1};
int Dx[8] = {0,1,1,1,0,-1,-1,-1};
const int mod = 1000000007;
const int inf = mod*mod;
const int d5 = 100100;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    int res=0;
    for(int b=1;b<=n;b++){
        if(b<=k)continue;
        int p=n/b;
        res+=p*(b-k);
        int m=n;
        m%=b;
        res+=max(0LL,m-k+1);
        if(k==0)res--;
    }
    cout<<res<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
 
#define forn(i, n) for (llong i = 0ll; i < (llong) n; ++i)
#define fornn(i, l, r) for (llong i = (llong) l; i < (llong) r; ++i)
#define size(x) ((int) (x.size()))
 
using namespace std;
 
typedef long long llong;
const llong inf = (llong) 1e+9 + 7ll;
const llong linf = (llong) 1e+18 + 7ll;
const long double eps = (long double) 1e-9;
const long double pi = acosl((long double) -1.0);
const int alph = 26;

mt19937 mrand(random_device{} ()); 

template<typename T, typename U> inline llong umin(const T& a, const U& b) { return a < b ? a : b; }
template<typename T, typename U> inline llong umax(const T& a, const U& b) { return a > b ? a : b; }
 
static char buff[(int) 2e6 + 17]; // reads std::string
const int maxn = (int) 2e5 + 17;

int n;
int a[maxn], b[maxn];
unordered_map<int, vector<int> > g;
unordered_set<int> used;

bool read()
{
	if (scanf("%d\n", &n) != 1)
		return false;

	forn (i, n)
		scanf("%d", &a[i]);

	forn (i, n)
		scanf("%d", &b[i]);

	return true;
}

void dfs(int v, bool& t)
{
	used.insert(v);

	if (v == a[n] || v == b[n])
		t = false;

	for (auto& to : g[v])
		if (!used.count(to))
			dfs(to, t);
}

void solve()
{
	g.rehash(4 * n);
	used.rehash(4 * n);
	int ans = 0;

	forn (i, n)
	{
		a[n] ^= a[i];
		b[n] ^= b[i];
		if (a[i] != b[i]) g[a[i]].push_back(b[i]);
		if (a[i] != b[i]) g[b[i]].push_back(a[i]);

		if (a[i] != b[i])
			++ans;
	}

	g[a[n]].push_back(b[n]);
	g[b[n]].push_back(a[n]);

	vector<int> aa(a, a + n + 1);
	vector<int> bb(b, b + n + 1);
	sort(aa.begin(), aa.end());
	sort(bb.begin(), bb.end());

	if (aa != bb)
	{
		printf("-1\n");
		return ;
	}

	for (int i = 0; i <= n; ++i)
		if (!used.count(a[i]))
		{
			bool good = true;
			dfs(a[i], good);

			if (good) ++ans;
			if (size(g[a[i]]) == 0) --ans;
		}

	printf("%d\n", ans);
}

int main()
{
#if SEREZHKA
	freopen("file.in", "r", stdin);
#endif
 
	while (read())
		solve();
 
	return 0;
}


Measured memory_bytes: 23808
n,m=[int(i) for i in input().split(" ")]

s,c=[],[]

for i in range(m):
    x,y=[int(i) for i in input().split(" ")]
    if x==1 and y==0 and n!=1:
        print(-1)
        exit(0)
    if not x in s:
        s.append(x)
        c.append(y)
    else:
        if c[s.index(x)]!=y:
            print(-1)
            exit(0)

if n!=1 and not 1 in s:
    s.append(1)
    c.append(1)

sum=0
for i in range(len(s)):
    sum+=c[i]*(10**(n-s[i]))

print(sum)


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9+7;
int main()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<ll> ar = a;
    reverse(ar.begin(), ar.end());
    vector<set<ll>> pos(2*1e5+10);
    for(int i = 0; i < n; i++)
    {
        pos[ar[i]].insert(i+1);
        if(pos[ar[i]].size() == 1)
        {
            pos[ar[i]].insert(i+1+n);
        }
    }

    vector<ll> start(n+1);
    int p = 1;
    ll v = ar[0];
    ll ans = 0;
    while(true)
    {
        ll next = *(pos[v].lower_bound(p+1));
        if(next == n)
        {
            start[ans] = next;   
            break;
        }
        else if(next == 2*n)
        {
            ans++;
            start[ans] = next - n;
            break;
        }
        else if(next > n)
        {
            p = next+1-n;
            ans++;
            start[ans] = p-1;
        }
        else
        {
            p = next+1;
            start[ans] = next;
        }
        v = ar[p-1];
    }
    ans++;
    ll count = k % ans;
    if(count == 0) return 0;
    ll s = start[ans-count];
    stack<ll> sta;
    set<ll> tmp;
    for(int i = n-s; i < n; i++)
    {
        if(tmp.find(a[i]) == tmp.end())
        {
            tmp.insert(a[i]);
            sta.push(a[i]);
        }
        else
        {
          while(true)
            {
                ll cand = sta.top();
                sta.pop();
                tmp.erase(cand);
                if(cand == a[i])
                {
                    break;
                }
            }
        }
    }
    stack<ll> ansq;
    ll size = sta.size();
    for(int i = 0; i < size; i++)
    {
        ll cand = sta.top();
        sta.pop();
        ansq.push(cand);
    }
    ll anss = ansq.size();
    for(int i = 0; i < anss; i++)
    {
        ll cand = ansq.top();
        ansq.pop();
        cout << cand << " ";
    }
}


Measured memory_bytes: 45440
#include "bits/stdc++.h"
#include "math.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<pair<ll,ll> > vp;
typedef vector<vp> vvp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define drep(i,a,b) for(ll i=(a);i>(b);--i)
const int MOD = 1000000007;
const int MAX = 510000;

int main(){
  ll n,k,ans=0;cin>>n>>k;
  if(k>((n-1)*(n-2))/2){cout<<-1<<endl;}
  else{
    vvb g(n,vb(n,false));
    rep(i,1,n)g[0][i]=true;
    ans=n-1;
    ll i=((n-1)*(n-2))/2-k;
    rep(j,1,n){
      rep(k,j+1,n){
        if(i<=0){break;}
        g[j][k]=true;i--;
        ans++;
      }
    }
    cout<<ans<<endl;
    rep(k,0,n){
      rep(l,0,n){
        if(g[k][l]){cout<<k+1<<" "<<l+1<<endl;}
      }
    }
  }
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define mod (long long)(1e9+7)
#define endl "\n"
using namespace std;


void solve(){
	ll n;
	cin>>n;
	ll a[n];
	for(ll i=0;i<n;i++){
		cin>>a[i];
	}
	ll mark=1;
	set<ll> mp;
	for(ll i=0;i<n;i++){
		ll x=a[i];
		if(x%2==0&&mp.find(2)!=mp.end()){
			mark=0;
			break;
		}
		while(a[i]%2==0){
			a[i]=a[i]/2;
			mp.insert(2);
		}
		for(ll j=3;j*j<=a[i];j+=2){
			if(a[i]%j==0){
				if(mp.find(j)!=mp.end()){
					mark=0;
					break;
				}
				else{
					mp.insert(j);
					while(a[i]%j==0)
						a[i]=a[i]/j;
				}
			}
		}
		if(a[i]>1){
			if(mp.find(a[i])!=mp.end()){
				a[i]=x;
				mark=0;
				break;
			}
			mp.insert(a[i]);
		}
		a[i]=x;
		if(!mark){
			break;
		}
	}
	if(mark){
		cout<<"pairwise coprime"<<endl;
	}
	else{
		ll ans=a[0];
		for(ll i=1;i<n;i++){
			ans=__gcd(ans,a[i]);
		}
		if(ans==1){
			cout<<"setwise coprime"<<endl;
		}
		else{
			cout<<"not coprime"<<endl;
		}
	}
}


int main(){
	ll t=1;
	//cin>>t;
	while(t--){
		solve();
	}
}


Measured memory_bytes: 11428
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(10)

using namespace std;

typedef pair<int, int> P;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int N, M;
    cin >> N >> M;

    vector<P> v;
    REP(i, N) {
        int a, b;
        cin >> a >> b;
        v.push_back({a, b});
    }
    sort(v.begin(), v.end());

    int ans = 0;
    REP(i, v.size()) {
        auto kv = v[i];
        int a = kv.first;
        int b = kv.second;

        int num = min(b, M);
        ans += a * num;
        M -= num;

        if (M == 0) break;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 2420
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int W, H, x, y;
    cin >> W >> H >> x >> y;

    double ans1 = double(W)*double(H)/double(2);
    int ans2 = 0;
    if(2*y==H && 2*x==W) ans2 = 1;
    cout << ans1 << " " << ans2 << endl;
    return 0;
}

Measured memory_bytes: 256