akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;


#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD 
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);


int main() {
    ll n, ans = 0; cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    sort(ALL(a), greater<ll>());
    rep(i, n-1) {
        ans += a[(i+1)/2];
    }
    cout << ans << endl;

    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) { 
    if(n == 0) return 1;
    
    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

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

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}


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

int main() {
  //入力
  int N;
  cin >> N;

  vector<double> x(N);
  vector<string> u(N);
  for (int i = 0 ; i < N; i++) {
    cin >> x[i] >> u[i];
  }

  //処理
  double ans = 0;
  for (int i = 0; i < N; i++) {
    if (u[i] == "JPY") {
        ans += x[i];
    }
    else {
        ans += (x[i] * 380000);
    }
  }
  //出力
  cout << ans << endl;

  return 0;
}


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

using namespace std;


const int MAXN = 1e5 + 5;
int pre[MAXN];
int main()

{
int n,q;
cin >> n >> q;
string x;
cin >> x;
string s = "a" + x;
pre[1] = pre[0] = 0;
for(int i = 2 ; i <= n ; i++)
   {
    if(s[i] == 'C' and s[i - 1] == 'A') ++pre[i];
    pre[i]+=pre[i - 1];
   }

   while(q--)
   {
    int l,r;
    cin >> l >> r;
    cout << pre[r] - pre[l] << '\n';
   }

} 

Measured memory_bytes: 1408
#include <iostream>
using ll = long long;
using namespace std;

int binarysearch(int A[], int n, int key){
    int left = 0;
    int right = n;
    int mid;
    while (left < right){
        mid = (left + right)/2;
        if (A[mid] == key) return 1;
        else if (key > A[mid]) left = mid + 1;
        else if (key < A[mid]) right = mid;
    }
    return 0;
}

int main() {
    int n, q, key, sum=0;
    cin >> n;
    int V[n];
    for (int i = 0; i < n; ++i) {
        scanf("%d", &V[i]);
    }
    cin >> q;
    for (int j = 0; j < q; ++j) {
        scanf("%d", &key);
        sum += binarysearch(V, n, key);
    }
    cout << sum << "\n";
    return 0;
}


Measured memory_bytes: 3544
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;

int main() {
  int X; cin >> X;

  // エラトステネスのふるい
  int MAX = 101010;
  vector<int> is_prime(MAX, 1);
  is_prime[0] = 0, is_prime[1] = 0;
  for (int i = 2; i < MAX; ++i) {
    if (!is_prime[i]) continue;
    for (int j = i*2; j < MAX; j += i) is_prime[j] = 0;
  }
  vector<int> primes;
  int counter = 0;
  for (int flag : is_prime) {
    if (flag) primes.push_back(counter);
    counter++;
  }

  auto iter = lower_bound(primes.begin(), primes.end(), X);
  cout << *iter << endl;

}


Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i,n) for(lli i=0;i<n;i++)
#define repb(i,n) for(lli i=n-1;i>=0;i--)
#define pb push_back
#define mp make_pair
#define bg() begin()
#define en() end()
#define ft first
#define sc second
#define vect_lli_it vector<lli>::iterator
#define set_lli_it set<lli>::iterator
#define inf 1e18
#define all(X) X.begin(),X.end()
#define iterate(X) for(it=X.begin();it!=X.end();it++)
lli combi(lli n,lli k)
{
    lli ans=1;
    k=k>n-k?n-k:k;
    lli j=1;
    for(;j<=k;j++,n--)
    {
        if(n%j==0)
        {
            ans*=n/j;
        }else
        if(ans%j==0)
        {
            ans=ans/j*n;
        }else
        {
            ans=(ans*n)/j;
        }
    }
    return ans;
}
lli gcd(lli a, lli b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
lli findlcm(vector <lli> arr, lli n)
{
    lli ans = arr[0];
    for (lli i = 1; i < n; i++)
        ans = (((arr[i] * ans)) /
                (gcd(arr[i], ans)));

    return ans;
}
lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    if (x == 0) return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli x;
    cin>>x;
    double p=1.01;
    lli amt=100;
    lli cnt=0;
    while(amt<x){
    	amt*=p;
    	cnt++;
	}
	cout<<cnt;
    return 0;
}


Measured memory_bytes: 3652
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF = 1 << 29;

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

    vector<int> X(N);
    for (int i = 0; i < N; ++i) cin >> X[i];

    sort(X.begin(), X.end(), greater<int>());
    int M = X[0];

    int min_cost = INF;
    for (int i = 1; i <= M; ++i) {
        int cost = 0;
        for (int j = 0; j < N; ++j) {
            cost += (X[j] - i) * (X[j] - i);
        }

        min_cost = min(min_cost, cost);
    }

    cout << min_cost << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,ans=-114514114514114514,p[50001],c[50001],nxt[50001][33];
int sum[50001][33];
signed main()
{
	scanf("%lld %lld",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%lld",&p[i]);
	for(int i=1;i<=n;i++)
		scanf("%lld",&c[i]);
	if(k<=n)
	{
		for(int i=1;i<=n;i++)
		{
			int now=i,s=0;
			for(int l=1;l<=k;l++)
			{
				now=p[now];
				s+=c[now];
				ans=max(ans,s);
			}
		}
		cout<<ans;
		return 0;
	}
	for(int i=1;i<=n;i++)
		sum[i][0]=c[p[i]];
	for(int i=1;i<=n;i++)
		nxt[i][0]=p[i];
	for(int i=1;i<=32;i++)
		for(int l=1;l<=n;l++)
			nxt[l][i]=nxt[nxt[l][i-1]][i-1];
	for(int i=1;i<=32;i++)
		for(int l=1;l<=n;l++)
			sum[l][i]=sum[l][i-1]+sum[nxt[l][i-1]][i-1];
	for(int i=1;i<=n;i++)
	{
		int now=i,s=0;
		for(int l=1;l<=n;l++)
		{
			now=p[now];
			s+=c[now];
			ans=max(ans,s); 
		}
		now=i,s=0;
		for(int l=0;l<31;l++)
			if((k-n)>>l&1)
			{
				s+=sum[now][l];
				now=nxt[now][l];
			}
		ans=max(ans,s);
		for(int l=1;l<=n;l++)
		{
			now=p[now];
			s+=c[now];
			ans=max(ans,s); 
		}
	}
	cout<<ans;
	return 0;
}

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

int main()
{
	char str[1000];
	int length,i;
	
	while(scanf("%s",str)!=EOF)
	{
		if(str[0]=='0')break;
		else
		{
			int sum=0;
			length=strlen(str);
			for(i=0;i<length;i++)sum+=str[i]-'0';
			printf("%d\n",sum);
		}

	}

	return 0;
}

Measured memory_bytes: 536
#include <bits/stdc++.h>
//#include <atcoder/all>
#define ll long long int
#define MOD 1000000007
#define P pair<ll,ll>
#define INF 1000000000000000000

//using namespace atcoder;
using namespace std;

int main(void){
  ll a, b;
  cin >> a >> b;
  
  cout << 100 << " " << 100 << endl;

  vector<string> ss(100, "");
  for (ll i = 0; i < 100; i++){
    for (ll j = 0; j < 100; j++){
      ss[i].push_back(i < 50 ? '#' : '.');
    }
  }

  ll x = 1, y = 1;
  for (ll i = 0; i < a-1; i++){
    ss[x][y] = '.';
    if (y == 99){
      x += 2;
      y = 1;
    }
    else {
      y += 2;
    }
  }

  x = 51; 
  y = 1;
  for (ll i = 0; i < b-1; i++){
    ss[x][y] = '#';
    if (y == 99){
      x += 2;
      y = 1;
    }
    else {
      y += 2;
    }
  }

  for (ll i = 0; i < 100; i++){
    cout << ss[i] << endl;
  }


  return 0;
}


Measured memory_bytes: 3676
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;

int main() {
	ll x;
	cin >> x;
	ll k = x / 11;
	if (x % 11 == 0) {
		cout << k * 2 << endl;
	}
	else if (k % 2 == 0) {
		if (x % 11 <= 6) cout << k * 2 + 1 << endl;
		else cout << k * 2 + 2 << endl;
	}
	else {
		if (x % 11 <= 5) cout << k * 2 + 1 << endl;
		else cout << k * 2 + 2 << endl;
	}
}


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

typedef long long ll;

void solve() {
    int N, M, x, y;
    cin >> N >> M;
    vector<bool> red(N, false);
    red[0] = true;
    vector<int> num(N, 1);
    rep(i, M) {
        cin >> x >> y;
        num[x - 1]--;
        num[y - 1]++;
        if (red[x - 1]) {
            red[y - 1] = true;
            if (num[x - 1] == 0) red[x - 1] = false;
        }
    }
    int ans = 0;
    rep(i, N) if (red[i]) ans++;
    cout << ans << endl;
    return;
}

int main() {
    solve();
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
#define f0(i,n) for(long long int i=0;i<n;i++)
#define f1(i,n) for(long long int i=1;i<=n;i++)
#define f2(i,a,b) for(long long int i=a;i<=b;i++)
#define sp <<" "                // giving space
#define sp1 <<" "<<              // giving space between two elements
#define nl <<"/n"                  // new line
#define M 1000000007              // for mod
#define ll long long
#define ld long double
#define F first
#define S second
using namespace std;


int main()
{    int x,a;
 cin>>x>>a;
 
 if(x<a)
   cout<<0;
 else cout<<10;
     
    return 0;
    }

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main()
{
	int a,b,s[3],max1=-1000000;
	cin>>a>>b;
	s[0]=a+b;
	s[1]=a-b;
	s[2]=a*b;
	for(int i=0;i<3;i++){
        if(s[i]>max1){
            max1=s[i];
        }
	}
	cout<<max1;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    cout << (n % 10 == n / 100 ? "Yes" : "No") << endl;
    return 0;
}

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

int L[MAX/2 + 2], R[MAX/2 + 2];
int A[MAX];
static int cnt = 0;

void merge(int *A, int left, int mid, int right) {
    int n1 = mid - left; // left の添え字が0 mid-1 がn1-1
    int n2 = right - mid; // mid の添え字が0 right -1 がn2-1
    for (int i = 0; i < n1; i++) {
        L[i] = A[left + i];
    }
    for (int i = 0; i < n2; i++) {
        R[i] = A[mid + i];
    }
    L[n1] = INFTY;
    R[n2] = INFTY;
    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i];
            i++;
        } else {
            A[k] = R[j];
            j++;
        }
        if (i > n1) i = n1;
        if (j > n2) j = n2;
        cnt++;
    }
}

void mergeSort(int *A, int left, int right) {
    if (right - left > 1) {
        int mid = (left + right) / 2;
        mergeSort(A, left, mid);
        mergeSort(A, mid, right);
        merge(A, left, mid, right);
    }
    return;
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> A[i];
    mergeSort(A, 0, n);
    for (int i = 0; i < n; i++) {
        if (i == n-1) cout << A[i] << endl;
        else cout << A[i] << " ";
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 6924
def main():
    N=int(input())
    p=[False]*(N+1)
    c=[False]*(N+1)
    for i in range(N):
        x=int(input())
        c[x]=True
        if c[x-1]:
            p[x-1] = True
    ans=0
    j=0
    for i in range(1,N+1):
        if p[i]:
            j+=1
        else:
            ans=max(ans,j)
            j=0
    ans=max(ans,j)
    print(N-ans-1)

if __name__ == "__main__":
    main()


Measured memory_bytes: 6132
// #define DEBUG_MODE
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long int ll;
typedef vector< int > vi;
typedef pair< int, int > ii;
typedef vector< ii > vii;
typedef vector< ll > vll;
#ifdef DEBUG_MODE
	#define errp(...) fprintf(stderr, __VA_ARGS__)
#else
	#define errp(...)
#endif
#define dbg(x) errp("[%s = %d]\n", #x, x)

vii edges;
int n, k;

void solve () {
	if (2 * k > (n - 1) * (n - 2)) {
		printf("-1\n");
		return;
	}

	for (int i = 2; i <= n; i++) edges.emplace_back(1, i);
	int cnt = (n - 1) * (n - 2) / 2;
	if (cnt > k) {
		bool done = false;
		for (int i = 2; i <= n; i++) if (!done)
			for (int j = i + 1; j <= n; j++) if (!done) {
				edges.emplace_back(i, j);
				cnt--;
				if (cnt == k) done = true;
			}
	}
	printf("%d\n", edges.size());
	for (int i = 0; i < edges.size(); i++)
		printf("%d %d\n", edges[i].first, edges[i].second);
}

int main () { 
#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	scanf("%d%d", &n, &k);
	solve();
}

Measured memory_bytes: 384
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

void main() {
    auto a = read!string;
    auto b = read!string;

    for (int i = 0; i < a.length; i++) {
        write(a[i]);
        if (i < b.length) write(b[i]);
    }
    writeln();
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main() {
  string S;
  cin >> S;
  map<char, ll> mp;
  rep(i,S.size()){
    mp[S[i]]++;
  }
  bool a = true;
  for (auto p : mp){
    if(p.second % 2 != 0){
      a = false;
    }
  }
  if(a){
    cout << "Yes";
  }
  else{
    cout << "No";
  }
}

Measured memory_bytes: 3600
#!/usr/bin/env python3
import sys
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)

N, Z, W = map(int, input().split())
a = list(map(int, input().split()))

if N == 1:
    print(abs(a[-1]-W))
    exit()

print(max(abs(a[-2] - a[-1]),
          abs(a[-1] - W)))


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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
using namespace std;

using ll = long long;
using ii = pair<ll, ll>;
ll mod = pow(10, 9) + 7;

template <typename T> void output(T, ll);
ll gcd(ll, ll);
ll lcm(ll, ll);
void prime_map(map<ll, ll> &, ll);

int main() {
    // source
    string s;
    cin >> s;
    int count[26] = {};
    rep(i, s.size()) { count[s[i] - 'A']++; }
    int ans = 0;
    rep(i, 26) {
        if (count[i] == 2) {
            ans++;
        }
    }

    if (ans == 2) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}

template <typename T> void output(T a, int precision) {
    if (precision > 0) {
        cout << setprecision(precision) << a << "\n";
    } else {
        cout << a << "\n";
    }
}

template <typename T> T min(T a, T b) {

    if (a < b) {
        return a;
    } else {
        return b;
    }
}

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

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

void prime_map(map<ll, ll> &m, ll num) {
    ll cache = num;
    repd(i, 2, sqrt(num) + 2) {
        if (num % i == 0) {
            if (m[i] == 0) {
                m[i] = 1;
            } else {
                m[i]++;
            }

            num /= i;
            break;
        }
    }

    if (cache == num) {
        if (m[num] == 0) {
            m[num] = 1;
        } else {
            m[num]++;
        }
        return;
    } else {
        prime_map(m, num);
    }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,s,e) for(int i=(s); i<(e);i++)
#define rep(i,n) REP(i,0,n)
#define rep1(i,n) REP(i,1,n)
#define repe(i,n) for(auto &&i:n)
#define all(v) (v).begin(),(v).end()
#define decimal fixed<<setprecision(20)
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
using namespace std;
using LL = long long;
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; }
const int INF = 1e9;
const LL LLINF = 1e16;

int main(){
    int n;
    cin >> n;
    vector<LL> a(n);
    rep(i, n) cin >> a[i];
    vector<LL> b(n);
    b[0] = 0;
    rep1(i, n){
        b[i] = a[i-1]*2 - b[i-1];
        // cout << b[i] << endl;
    }
    b[0] = a[n-1]*2 - b[n-1];
    b[0] /= 2;
    rep1(i, n){
        b[i] += b[0] * (i%2 == 1 ? -1 : 1);
    }
    rep(i, n) cout << b[i] << (i == n-1 ? "\n": " ");

}

Measured memory_bytes: 2944
#include <bits/stdc++.h>
#define MAX 1000
#define MOD 1000000007
using namespace std;

int dp[MAX + 1][MAX + 1];
char c[MAX+1][MAX+1];

int noPaths(int n, int m){

	// Preprocessing step
	dp[0][0] = 1;

	// First row and first column
	for(int j=1;j<m;j++){
		if(dp[0][j]==-1){
			break;
		}
		dp[0][j] = dp[0][j-1];
	}

	for(int i=1;i<n;i++){
		if(dp[i][0]==-1){
			break;
		}
		dp[i][0] = dp[i-1][0];
	}

	for(int i=1;i<n;i++){
		for(int j=1;j<m;j++){
			if(dp[i][j]==-1){
				continue;
			}
			if(dp[i-1][j]!=-1){
				dp[i][j] += dp[i-1][j];
				dp[i][j] %= MOD;
			}
			if(dp[i][j-1]!=-1){
				dp[i][j] += dp[i][j-1];
				dp[i][j] %= MOD;
			}
		}
	}
	return dp[n-1][m-1];

}

int main(){
	int h,w;
	cin>>h>>w;
	memset(dp, 0, sizeof dp);

	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>c[i][j];
			if(c[i][j]=='#'){
				dp[i][j] = -1;
			}
		}
	}

	cout<<noPaths(h,w)<<"\n";
}

Measured memory_bytes: 5120
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int x, t;
    scanf("%d%d", &x,&t);
    if(x > t){
        printf("%d\n",x-t);
    }else{
        printf("0\n");
    }
    return 0;
}

Measured memory_bytes: 128
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) rep(i,v.size())cout<<" "<<v[i];cout<<endl<<endl;
#define show2d(v) rep(i,v.size()){rep(j,v[i].size())cout<<" "<<v[i][j];cout<<endl;}cout<<endl;
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
//#define mt make_tuple
//typedef tuple<int,int,int> tp;
//typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

signed main(){
	int n,s,w,q;
	while(cin>>n>>s>>w>>q,n){
		string a="";
		int g=s;
		rep(i,n){
			a+=(char)('0'+(g/7)%10);
			if(g%2==0)g/=2;
			else g=(g/2)^w;
		}
		if(q==2){
			int out=0,c=0;
			rep(i,n){
				if(a[i]=='0')c++;
				if((a[i]-'0')%2==0)out+=i+1-c;
			}
			cout<<out<<endl;
			continue;
		}
		if(q==5){
			int out=0,c=0;
			rep(i,n){
				if(a[i]=='0')c++;
				if((a[i]-'0')%5==0)out+=i+1-c;
			}
			cout<<out<<endl;
			continue;
		}
		vi t;
		t.pb(0);
		map<int,vp>ma;
		ma[0].pb({0,1});
		int sum=0,b=1;
		rep(i,n){
			t.pb((b*(a[n-1-i]-'0')+sum)%q);
			ma[t.back()].pb({i+1,a[n-1-i]=='0'});
			(sum+=b*(a[n-1-i]-'0'))%=q;
			(b*=10)%=q;
		}
		int out=0;
		for(auto it:ma){
			vp c=it.second;
			out+=(ll)c.size()*(c.size()-1)/2;
			rep(i,c.size())if(c[i].second)
				out-=i;
		}
		cout<<out<<endl;
	}
}


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

#define int long long

//int dp[100001][101];
 
int ans=0;

/*int fn(int i,int n, int a[],int v[],int w )
{
	if(w<0)
	return INT_MIN;
	if(i==-1)
	return 0;
	if(dp[w][i]!=-1)
	return dp[w][i];
	
	ans=max(fn(i-1,n,a,v,w-a[i])+v[i],fn(i-1,n,a,v,w));
	dp[w][i]=ans;
	
	return dp[w][i];
}

*/
int32_t main()
{
	int n,w,s=0; 
	cin>>n>>w;
	int a[n+1],v[n+1];
	for(int i=1; i<=n; i++)
    {
      cin>>a[i]>>v[i];
	s+=v[i];
    }
  
	
	int dp[n+1][s+1];
	
	
	for(int i=0; i<=s;i++)
	dp[0][i]=INT_MAX;
  
  for(int i=0; i<=n;i++)
	dp[i][0]=0;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=s; j++)
		{
			if(v[i]>j)
			dp[i][j]=dp[i-1][j];
			else
			{
				dp[i][j]=min(dp[i-1][j],a[i]+dp[i-1][j-v[i]]);
			}
		}
	}
	int j=-1;
	for(int i=s; i>=1; i--)
     if(dp[n][i]<=w)
     {
        j=i;
       break;
     }
  cout<<j;



	return 0;
}


Measured memory_bytes: 79104
#include<stdio.h>
int main(){
	int r,c;
	int i,j,a[110][110]={0};
	scanf("%d %d",&r,&c);
	for(i=1;i<=r;i++){
		for(j=1;j<=c;j++){
			scanf("%d",&a[i][j]);
			a[i][c+1]+=a[i][j];
			a[r+1][j]+=a[i][j];
		}
	}
	for(j=1;j<=c;j++){
			a[r+1][c+1]+=a[r+1][j];
	}
	for(i=1;i<=r+1;i++){
		for(j=1;j<=c+1;j++){
			if(j==c+1) printf("%d",a[i][j]);
			else printf("%d ",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Measured memory_bytes: 2784
import bisect

INF = float('inf')
q = int(input())
for i in range(q):
    x = input()
    y = input()
    dic = {}
    for i,xx in enumerate(x):
        if xx in dic : dic[xx].append(i)
        else : dic[xx] = [i]
    inc = []
    size = -1
    for j,yy in enumerate(y):
        if not (yy in dic):continue
        inc.append([])
        size += 1
        for k in dic[yy]:
            inc[size].append(k)
    #incのLISを求めればよい
    n = len(inc)
    dp =  [INF for i in range(n)]
    temp = [INF for i in range(n)]

    for ii in inc:
        for i in ii:
            p = bisect.bisect_left(dp, i)
            temp[p] = min(i,temp[p])
        dp = temp[:]


    print(bisect.bisect_left(dp, INF))


Measured memory_bytes: 14920
s = gets.to_i
u = s % 100
t = s / 100
if (t <= 12 && t >= 1) && (u <= 12 && u >= 1)
  ans = "AMBIGUOUS"
elsif (u <= 12) && (u >= 1)
  ans = "YYMM"
elsif (t <= 12) && (t >= 1)
  ans = "MMYY"
else
  ans = "NA"
end
puts ans

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

//https://atcoder.jp/contests/agc029/tasks/agc029_b

int n, ans = 0;
multiset<int> s;

int main() {
	cin >> n;
	for (int i = 0; i < n; ++i) {
		int a;
		cin >> a;
		s.insert(a);
	}
	while (!s.empty()) {
		auto a = s.end();
		--a;
		s.erase(a);
		int b = 1;
		while (b <= *a)
			b <<= 1;
		auto c = s.find(b - *a);
		if (c != s.end()) {
			++ans;
			s.erase(c);
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 12892
fun main(args: Array<String>) {
  val (r, g, b, n) = readLine()!!.split(" ").map { it.toInt() }
  var count = 0
  for (x in 0..n) {
    for (y in 0..n) {
      if (x * r + y * g > n) continue
      if ((n - x * r - y * g) % b == 0) {
        count++
      }
    }
  }
  println(count)
}

Measured memory_bytes: 36016
import sys
import math
import bisect

def main():
    n, m = map(int, input().split())
    A = [0] * m
    for i in range(n):
        B = list(map(int, input().split()))[1:]
        for b in B:
            A[b-1] += 1
    ans = 0
    for a in A:
        if a == n:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()


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

int main() {
    int N; cin >> N;
    string n = to_string(N);
    //auto key = n.end() - 1;
    if (n.at(n.size()-1) == '3') cout << "bon" << endl;
    else if (n.at(n.size()-1) == '0' || n.at(n.size()-1) == '6' || n.at(n.size()-1) == '1' || n.at(n.size()-1) == '8') cout << "pon" << endl;
    else cout << "hon" << endl;
}

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

int main() {
	int height;
	int top[3]={0,0,0};
	for(int i=0; i<10; i++){
		cin >> height;
		
		for(int j=0; j<3; j++){
			if(top[j]<=height){
				for(int k=2; k>j; k--){
					top[k]=top[k-1];
				}
				top[j]=height;
				break;
			}
		}
		
	}
	
	for(int i=0; i<3; i++){cout << top[i] << endl;}
	return 0;
}

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

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define DEBUG(...) 6;
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...);}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int lCnt = 0, rCnt = 0;
        long long ret = 0;
        vector<vector<int>> lft(n), rght(n);
        for (int i=0; i<n; i++) {
            int k, l, r;
            cin >> k >> l >> r;
            k--;
            if (l > r) {
                lCnt++;
                ret += r;
                lft[k].push_back(l - r);
            } else {
                rCnt++;
                ret += l;
                if (k + 1 < n)
                    rght[k+1].push_back(r - l);
            }
        }

        priority_queue<int> pq;
        for (int i=n-1; i>=0; i--) {
            for (int x : lft[i])
                pq.push(x);
            if (i + 1 <= lCnt && !pq.empty()) {
                ret += pq.top();
                pq.pop();
            }
        }
        while (!pq.empty())
            pq.pop();

        for (int i=0; i<n; i++) {
            for (int x : rght[i])
                pq.push(x);
            if (n - i <= rCnt && !pq.empty()) {
                ret += pq.top();
                pq.pop();
            }
        }

        cout << ret << "\n";
    }

    return 0;
}


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

int main() {
	ios_base::sync_with_stdio( false );

	vector<bool> cup { true, false, false };
	char a, b, c;

	while( cin >> a >> c >> b ) {
		swap( cup[a - 'A'], cup[b - 'A'] );
	}

	for( int i = 0; i < 3; ++i ) {
		if( cup[i] ) {
			cout << (char)('A' + i) << endl;
		}
	}

	return 0;
}



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

#define LL long long
int a[105], n;

int cost(int x) {
    int res = 0;
    for(int i = 0; i < n; i++) res += (x - a[i]) * (x - a[i]);
    return res;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    
    int l = -105, r = 105, mid; cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];

    while(l <= r) {
        mid = (l + r) / 2;
        if(cost(mid) > cost(mid + 1)) l = mid + 1;
        else r = mid - 1;
    }    
    cout << cost(l);
}

Measured memory_bytes: 3640
#include <stdio.h>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <regex>



using ull = unsigned long long;
using ll = long long;

using namespace std;


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

	map<string, int> m_str;
	for (int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		if (m_str.count(s) == 0) {
			m_str[s] = 1;
		}
		else {
			m_str[s]++;
		}
	}

	
	cout << "AC x " << m_str["AC"] <<endl;
	cout << "WA x " << m_str["WA"] << endl;
	cout << "TLE x " <<  m_str["TLE"] << endl;
	cout << "RE x " << m_str["RE"] << endl;
	
	

	
	return 0;
	
}

Measured memory_bytes: 3248
// https://atcoder.jp/contests/abc095/tasks/abc095_b
"use strict"
function Main(input) {
  const params = input.split('\n');
  const n = Number(params[0].split(' ')[0]);
  const x = Number(params[0].split(' ')[1]);
  const m = params.slice(1, -1);
  let total = 0;
  let min = Number.MAX_VALUE;
  m.forEach(value => {
    if (value === '') return;
    total += Number(value);
    min = Math.min(min, Number(value));
  });

  const remain = x - total;
  const create = Math.floor(remain / min);
  console.log(n + create);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 7756
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define all(x) (x).begin(),(x).end()

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

typedef long long ll;

using namespace std;

template <class Abel>
class BIT
{
public:
  using F=function<Abel(Abel,Abel)>;
  F f;
  ll l;
  vector<Abel> bit;
  Abel m0;
  BIT(ll maxidx,F fu,Abel m0):f(fu),l(maxidx),m0(m0)
  {
    bit.assign(l,m0);
  }
  void update(ll idx,ll val)
  {
    while(idx<=l)
    {
      bit[idx]=f(bit[idx],val);
      idx+=idx & -idx;
    }
  }
  ll query(ll idx)
  {
    idx--;
    ll res=m0;
    while(idx>0)
    {
      res=f(bit[idx],res);
      idx-=idx& -idx;
    }
    return res;
  }
};
ll mod=1e9+7;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;cin>>n;
  vector<ll> s(n);
  rep(i,n)cin>>s[i];

  s.erase(unique(all(s)),s.end());

  ll l=s.size();
  map<ll,vector<ll>> cidx;
  vector<ll>tv;
  rep(i,l)
  {
    if(cidx.count(s[i])==0)
    {
      cidx[s[i]]=tv;
    }
    cidx[s[i]].push_back(i);
  }

  BIT<ll>bt(l,[](ll a,ll b){return (a+b)%mod;},0);
  rep(i,l)
  {
    auto nitr=upper_bound(all(cidx[s[i]]),i);
    if(nitr==cidx[s[i]].end())continue;
    bt.update(*nitr,bt.query(i+1)+1);
  }
  cout<<(bt.query(l)+1)%mod<<endl;



  return 0;

}


Measured memory_bytes: 25216
#include<bits/stdc++.h>
using namespace std;
 
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007


int main(){
    //ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

    int h,w;
    vector<vector<int> > dp(1005,vector<int>(1005,0));
    string maze[1005];
    dp[0][0]=1;
    cin>>h>>w;
    f0(i,0,h){
        cin>>maze[i];
    }
    f0(i,0,h)
        f0(j,0,w){
            if(i==0&&j==0) dp[i][j]=1;
            else if(maze[i][j]=='#') dp[i][j]=0;
            else if(i==0) dp[i][j]=dp[i][j-1];
            else if(j==0) dp[i][j]=dp[i-1][j];
            else dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
        }
    //cout<<ans<<'\n';
    cout<<dp[h-1][w-1]<<'\n';
}

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

string base4(int p) {

  string s;
  while(true) {
    if(!p) break;
    s = string( 1, '0'+p%4 ) + s;
    p /= 4;
  }
  if( s == "" ) s = "0";
  return s;
}

int main() {
  int n;
  while(true) {

    cin>>n;
    if( n == -1 ) break;

    cout<<base4(n)<<endl;

  }
}

Measured memory_bytes: 0
#include<iostream>
using namespace std;
int a,b,c,g;
int main()
{
	cin>>a>>b>>c;
	int d=a*10+b+c;
	int e=b*10+a+c;
	int f=10*c+a+b;
	if(d>e)
	{
		g=d;
	}
	else
	{
		g=e;
	}
	if(g<f)
	{
		g=f;
	}
	cout<<g;
	return 0;
}

Measured memory_bytes: 256
import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)

def solve(x,a,b,c,d):
    @lru_cache(None)
    def dfs(x):
        if x == 0:
            return 0
        if x == 1:
            return d
        l2 = x//2
        r2 = (x+1)//2
        l3 = x//3
        r3 = (x+2)//3
        l5 = x//5
        r5 = (x+4)//5
        count = x*d
        count = min(count,abs(l2*2-x)*d+a+dfs(l2))
        count = min(count,abs(r2*2-x)*d+a+dfs(r2))
        count = min(count,abs(l3*3-x)*d+b+dfs(l3))
        count = min(count,abs(r3*3-x)*d+b+dfs(r3))
        count = min(count,abs(l5*5-x)*d+c+dfs(l5))
        count = min(count,abs(r5*5-x)*d+c+dfs(r5))
        return count
    return dfs(x)
t = int(input())
for _ in range(t):
    n,a,b,c,d = map(int,input().split())
    print(solve(n,a,b,c,d))

Measured memory_bytes: 20448
#include <stdio.h>
#include <ctype.h>

int main(void)
{
	char name[200];
	char rule[10];
	char word[100][100];
	int pos, n;
	int i, j;
	
	while (1){
		scanf("%s", name);
		scanf("%s", rule);
		if (rule[0] == 'X'){
			break;
		}
		
		pos = 0;
		n = 0;
		while (name[pos] != '\0'){
			i = 0;
			do {
				word[n][i++] = name[pos++];
			} while (name[pos] != '_' && ('a' <= name[pos] && name[pos] <= 'z'));
			
			word[n][i] = '\0';
			
			if (name[pos] == '_'){
				pos++;
			}
			n++;
		}
		
		switch (rule[0]){
		  case 'L':
			printf("%c", tolower(word[0][0]));
			printf("%s", &word[0][1]);
		  	for (i = 1; i < n; i++){
				printf("%c", toupper(word[i][0]));
				printf("%s", &word[i][1]);
		  	}
			printf("\n");
			break;
			
		  case 'U':
		  	for (i = 0; i < n; i++){
				printf("%c", toupper(word[i][0]));
				printf("%s", &word[i][1]);
		  	}
			printf("\n");
			break;

		  case 'D':
			printf("%c", tolower(word[0][0]));
			printf("%s", &word[0][1]);
		  	for (i = 1; i < n; i++){
				printf("_%c", tolower(word[i][0]));
				printf("%s", &word[i][1]);
		  	}
			printf("\n");
			break;
		}
	}
	
	return (0);
}

Measured memory_bytes: 552
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long 
#define f(z) for(int i=1;i<=z;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
#define si set<int>
#define mod  1000000007
#define cl(n,m) (n-1)/m  + 1 ;
#define T() int TT; cin>>TT; while(TT--)
#define P pair<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
const int N = 2e6 + 5 ;
int a[N] ;
signed main(){
	int n , t , ans  = 0 ; 
	cin >> n >> t ; 
	for ( int i = 1 ; i<=n ; i++){
		cin >> a[i] ; 
	}
	a[n+1] = a[n] + t ;
	for ( int i = 1 ; i<=n ; i++){
		if( a[i+1] <= a[i] + t ) ans += ( a[i+1] - a[i] ) ;
		else ans+= t; 
		//cout << ans << endl ;
	}
	cout << ans << endl ;
}

Measured memory_bytes: 1792
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
 
class Program {
    static List<int>[] Sub;
    static int[] Salary;
    
    
    static void Main(string[] args) {
        
        var N = int.Parse(Console.ReadLine());
        var A  = Console.ReadLine().Split().Select(a => long.Parse(a)).ToArray();
        
        for(int i = 0; i < A.Length ;i++){
            A[i] -= (i+1);
        }
        Array.Sort(A);
        var B = 0L;
        if(N%2 == 0 && N > 0){
          B = (long) Math.Round((((double)A[A.Length/2]+(double)A[A.Length/2-1])/2), MidpointRounding.AwayFromZero);
        }else{
          B = A[A.Length/2];
        }
      
        var ans = 0L;
        for(int i = 0; i < A.Length ;i++){
            ans += Math.Abs(A[i]-B);
        }
        

        Console.WriteLine(ans);
        }
         
}


Measured memory_bytes: 31748
#include <bits/stdc++.h>
using namespace std;
//-------------------------------------------MACROS---------------------------------------------
#define 	TIEMPO_CORRIDA		cerr << "tiempo: "<<1.0*clock()/CLOCKS_PER_SEC <<" seg.\n";
#define 	FILES				freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
#define 	FORIT(it,e)			for(it = e.begin() ; it != e.end() ; it++)
#define 	FOR(i,a,b)			for(i = a ; i < b ; i++)
#define 	RFOR(i,a,b)			for(i = a ; i >= b ;i--)
#define 	FORSW(i,a,b,sw)		for(i = a ; i < b && sw ; i++)
#define 	RFORSW(i,a,b,sw)	for(i = a ; i >= b && sw ; i--)
#define 	fast 				ios::sync_with_stdio(false),cin.tie(),cout.tie();
#define 	to_int(x) 			atoi(x.data())
#define 	to_long(x) 			atoll(x.data())
#define 	ALL(x) 				x.begin(),x.end()
#define 	RALL(x) 			x.rbegin(),x.rend()
typedef 	long long 			ll;
typedef 	pair<int,int> 		pi;
typedef 	pair<ll,ll> 		pl;
const		int 				modN = 1e9+7;
const		double 				PI = 3.1415926535897932384626433832795028841;
//---------------------------------------------------------------------------------------------

int nodes,edges;
vector<vector<int>>graph;
int memo[100002];

int dp(int n){
	if(graph[n].size()==0) return memo[n]=0;
	if(memo[n]!=-1) return memo[n];
	int ans=0;
	for(int e : graph[n])
		ans = max(ans,dp(e)+1);
	return memo[n] = ans;
}

int main(){
	int i,j,x,y;
	while(scanf("%d %d",&nodes,&edges)==2){
		memset(memo,-1,sizeof memo);
		graph.assign(nodes+1,vector<int>());
		FOR(i,0,edges){
			scanf("%d %d",&x,&y);
			graph[x].push_back(y);
		}
		int ans = -1;
		FOR(i,1,nodes+1) ans = max(ans,dp(i));
		printf("%d\n",ans);
	}
	return 0;
}

Measured memory_bytes: 11036
#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back 
#define N 110
#define W 100010
using namespace std;
ll dp[N][W], A[N], B[N], n;
ll fun(int a, int b){
	if(b < 0)
		return 2 * 1e9;
	if(a == n){
		if(b == 0)
			return 0;
		return 2 * 1e9;
	}
	if(dp[a][b] != -1)
		return dp[a][b];
	return dp[a][b] = min(fun(a + 1, b), B[a] + fun(a + 1, b - A[a]));
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int w;
	cin >> n >> w;
	for(int i = 0; i < n; i++)
		cin >> B[i] >> A[i];
	memset(dp, -1, sizeof(dp));
	for(int i = 100000; i >= 0; i--){
		if(fun(0, i) <= w){
			cout << i << '\n';
			return 0;
		}
	}

}

Measured memory_bytes: 86400
#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int main2() {
  int N = nextLong();
  int M = nextLong();

  atcoder::dsu uf(N);
  REP(i, M) {
    int a = nextLong() - 1;
    int b = nextLong() - 1;
    uf.merge(a, b);
  }
  int ans = uf.groups().size() - 1;
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

Measured memory_bytes: 9008
#include <algorithm>
    #include <bits/stdc++.h>
    #include <cctype>
    #include <cmath>
    #include <cstdio>
    #include <iomanip>
    #include <iostream>
    #include <list>

    #include <stack>
    #include <string>
    #include <vector>
    #include <bits/stdc++.h>
    #define INFINI (1000 * 1000 * 1000)

    using namespace std;
    //#define s(T) scanf("%d", &T)
    #define sl(T) scanf("%lld", &T)
    #define fill(a, val) memset(a, val, sizeof(a))
    #define mp make_pair
    #define pb push_back
    #define all(x) x.begin(), x.end()
    #define DREP(x) sort(all(x)); x.erase(unique(all(x)), x.end())
    #define INDEX(a, val) (lower_bound(all(a), val) - a.begin())
    #define mod ll(1e9+7)
    typedef long long ll;


    bool compare_first(pair<ll,ll >a,pair<ll,ll >b)
    {
        return a.first<b.first;
    }

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


    ll min(ll a,ll b)
    {
        if(a>b)return b;
        return a;
    }


    int main() {
      ios::sync_with_stdio(false);

      int n,k,x,y;
      cin>>n>>k>>x>>y;

      cout<<x*min(k,n)+y*max(0,n-k)<<endl;




return 0;
    }


/*

 }
 */


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define all(c) c.begin(), c.end()
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define gadd(x,y) x=add(x,y)
#define gmul(x,y) x=mul(x,y)
using namespace std;

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

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	int x;
	cin >> x;
	int res = x/500;
	x%=500;
	cout << res * 1000 + (x/5)*5 << '\n';
}


Measured memory_bytes: 256
big_num = 20000000000000
N = input()
table = list(map(int, input().split()))
MaxNum = -big_num
minNum = big_num
sum = 0
for i in table:
    if MaxNum < i:
        MaxNum = i
    if minNum > i:
        minNum = i
    sum += i
print("%d %d %d"%(minNum, MaxNum, sum))



Measured memory_bytes: 6552
#include "iostream"
using namespace std;
 int main(int argc, char const *argv[])
 {
 	int r, c = 0;
 	cin >> r >> c;
 	int A[r+1][c+1];
 	int i,j;
 	int temp=0;

 	for (i=0;i<r+1;i++)
 	{
 		for (j=0;j<c+1;j++)
 		{
 			A[i][j]=0;
 		}

 	}
 	for (i=0;i<r;i++)
 	{
 		for (j=0;j<c;j++)
 		{
 			cin >> A[i][j];
 		}
 	}

 	for(i=0;i<r;i++)
 	{
 		for(j=0;j<c;j++)
 		{
 			A[i][c] += A[i][j];
 		}
 	}
 	for(j=0;j<c+1;j++)
 	{
 		for(i=0;i<r;i++)
 		{
 			A[r][j] += A[i][j];
 		}
 	}


 	for (i=0;i<r+1;i++)
 	{
 		for (j=0;j<c+1;j++)
 		{
 			if(j!=c)
 			{
 				cout << A[i][j] << " ";
 			}
 			else
 			{
 				cout << A[i][j];
 			}
 		}
 		cout << endl;
 	}

 	return 0;
 }


Measured memory_bytes: 3092
//#pragma warning(disable:4996)
#include <stdio.h>

int main() {
	int m, n1, n2;
	int answer[1000] = {};
	int x = 0;
	while (1) {
		scanf("%d %d %d", &m, &n1, &n2);
		if (m == 0 && n1 == 0, n2 == 0) break;

		int p[10000];
		for (int i = 0; i < m; i++) {
			scanf("%d", &p[i]);
		}
		int g[10000] = {};
		for (int i = 0; i < m - 1; i++) {
			g[i] = p[i] - p[i + 1];
		}
		int max = 0;
		for (int i = n1-1; i < n2; i++) {
			if (max < g[i]) {
				max = g[i];
			}
		}
		for (int i = n2 - 1; i >= n1-1; i--) {
			if (g[i] == max) {
				answer[x] = i + 1;
				break;
				printf("%d\n", i + 1);
				break;
			}
		}
		x++;
	}
	
	for (int i = 0; i < x; i++) {
		printf("%d\n", answer[i]);
	}

}


Measured memory_bytes: 2036
n,m=map(int,input().split())
l=[[0,0]]*m
at=[0]*m
bt=[0]*m
atc=0
btc=0
a=False

f=0
for k in range(m):
  ln=list(map(int,input().split()))
  if ln[0]==1:
    at[atc]=ln[1]
    atc+=1
  elif ln[1]==n:
    bt[btc]=ln[0]
    btc+=1
del at[atc:]
del bt[btc:]
at.sort()
bt.sort()
c=list(set(at)^set(bt))
if len(c)!=(atc+btc):
  print("POSSIBLE")
else:
  print("IMPOSSIBLE")

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


Max=100000

ARR=[0] * Max

for i in range(N):
    ARR[A[i]-1]+=1

ans = 0

for i in range(Max):
    ans+=ARR[i]*(i+1)

Bt=[0]*Q
Ct=[0]*Q
for i in range(Q):
    Btt,Ctt=map(int, input().split())
    Bt[i]=Btt
    Ct[i]=Ctt

for i in range(Q):
    B=Bt[i]
    C=Ct[i]
    ans-=ARR[B-1]*B
    ans+=ARR[B-1]*C
    ARR[C-1]+=ARR[B-1]
    ARR[B-1]=0
    print(ans)



Measured memory_bytes: 21964
#include<iostream>
#include<algorithm>
#include<string>
class DICE
{
private:
	int Label[6];
public:
	DICE();
	void RotateDirection(std::string);
	void Rotate(int, int, int, int);
	int GetLabel(int x) { return Label[x]; }
	void TopFront(int, int);
	bool CheckConsistentLabel(DICE AnotherDice);
};
DICE::DICE()
{
	for (int i = 0; i < 6; ++i)
	{
		std::cin >> Label[i];
	}
}
void DICE::RotateDirection(std::string s)
{
	if (s == "E") { Rotate(0, 3, 5, 2); }
	if (s == "N") { Rotate(0, 1, 5, 4); }
	if (s == "S") { Rotate(0, 4, 5, 1); }
	if (s == "W") { Rotate(0, 2, 5, 3); }
}

void DICE::Rotate(int a, int b, int c, int d)
{
	std::swap(Label[a], Label[b]);
	std::swap(Label[b], Label[c]);
	std::swap(Label[c], Label[d]);
}
void DICE::TopFront(int top, int front)
{
	const int size_str = 7;
	std::string str[size_str] = { "N", "N", "N", "W", "N" , "N"  , "N" };
	//std::string str = "NNNNWWWW" ;
	for (unsigned int i = 0; i < size_str; ++i)
	{
		if (front == Label[1]) { break; }

		RotateDirection(str[i]);
	}
	while (top != Label[0])
	{
		RotateDirection("W");
	}
}
bool DICE::CheckConsistentLabel(DICE AnotherDice) {
	int NumberLabel[101] = { 0 };
	for (int i = 0; i < 6; ++i) {
		++NumberLabel[Label[i]];
		--NumberLabel[AnotherDice.GetLabel(i)];
	}
	int i = 0;
	while (i <= 100) {
		if (NumberLabel[i] != 0) {
			return false;
		}
		++i;
	}
	return true;
}
int main(void)
{
	DICE a, b;
	if (!a.CheckConsistentLabel(b)) {
		std::cout << "No\n";
		//system("pause");
		return 0;
	}
	a.TopFront(b.GetLabel(0), b.GetLabel(1));
	for (int i = 0; i < 6; ++i)
	{
		if (a.GetLabel(i) != b.GetLabel(i))
		{
			std::cout << "No\n";
			//system("pause");
			return 0;
		}
	}
	std::cout << "Yes\n";
	//system("pause");
	return 0;
}

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

int main(){
    int N;
    string S;
    cin >> N;
    cin >> S;
    if(N%2!=0){
        cout << "No" << endl;
        return 0;
    }
    for(int i=0;i<N/2;i++){
        if(S[i]!=S[i+N/2]){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

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





int main(){

    string s,t;
    cin >> s >> t;

    cout << t+s << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define p 3.14159265358979323846L

using namespace std;

bool visited[100001];
vector<int>adj[100001];

int parent[100001];

void bfs(int at,int par)
{
    visited[at]=1;

    queue<int>q;
    q.push(at);
    parent[at]=par;

    while(!q.empty())
    {
        int u;
        u=q.front();
        q.pop();

        for(auto to :adj[u])
        {
            if(!visited[to])
            {
                visited[to]=1;
                q.push(to);
                parent[to]=u;
            }
        }
    }
}

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

    int n,m,u,v;
    cin>>n>>m;

    while(m--)
    {
        cin>>u>>v;

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    bfs(1,-1);

    cout<<"Yes\n";
    for(int i=2;i<=n;i++)
        cout<<parent[i]<<endl;

    return 0;
}


Measured memory_bytes: 10700
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Main main = new Main();
		main.run();
	}

	public void run() {
		Scanner sc = new Scanner(System.in);
		int M=sc.nextInt();
		int K=sc.nextInt();

		if(K>=(int)Math.pow(2, M)) {
			System.out.println(-1);
			sc.close();
			return;
		}

		if(M==0) {
			System.out.println("0 0");
			sc.close();
			return;
		}

		if(M==1) {
			if(K==0) {
				System.out.println("0 0 1 1");
				sc.close();
				return;
			}else {
				System.out.println("-1");
				sc.close();
				return;
			}
		}

		for(int i=0; i<(int)Math.pow(2,M); i++) {
			if(i==K) continue;
			if(i!=0) System.out.print(" ");
			System.out.print(i);
		}
		System.out.print(" "+K);
		for(int i=(int)Math.pow(2, M)-1; i>=0; i--) {
			if(i==K) continue;
			System.out.print(" "+i);
		}
		System.out.print(" "+K);

		System.out.println();

		sc.close();
	}
}


Measured memory_bytes: 46912
function Main(s) {
	s = s.split("\n").map(a => parseInt(a));
	
	console.log((s[0] > s[1] ? s[1] : s[0]) + (s[2] > s[3] ? s[3] : s[2]));
}

Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());

Measured memory_bytes: 22076