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

#define ls (p<<1)
#define rs ((p<<1)|1)
#define mid ((l+r)>>1)

const int MOD=998244353;

using namespace std;

int sum[200009*4],sl[19],sr[29],n,k;

void update(int p,int l,int r,int pp,int w)
{
    if(r<pp || pp<l)return;
    if(l==r)
    {
        sum[p]+=w;
        sum[p]%=MOD;
        return;
    }
    update(ls,l,mid,pp,w);
    update(rs,mid+1,r,pp,w);
    sum[p]=(sum[ls]+sum[rs])%MOD;
}

int getsum(int p,int l,int r,int ll,int rr)
{
    if(r<ll || rr<l)return 0;
    if(ll<=l && r<=rr)
    {
        return sum[p];
    }
    return (getsum(ls,l,mid,ll,rr)+getsum(rs,mid+1,r,ll,rr))%MOD;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=k;i++)scanf("%d%d",&sl[i],&sr[i]);
    int tem=0;
    update(1,1,n,1,1);
    for(int i=2;i<=n;i++)
    {
        tem=0;
        for(int j=1;j<=k;j++)
        {
            if(i-sl[j]<1)continue;
            tem+=getsum(1,1,n,max(i-sr[j],1),i-sl[j]);
            tem%=MOD;
        }
        update(1,1,n,i,tem);
    }
    printf("%d\n",getsum(1,1,n,n,n));
    return 0;
}


Measured memory_bytes: 5860
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for(int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    tx -= sx;
    ty -= sy;
    sx = 0;
    sy = 0;
    int posx = 0, posy = 0;
    while (posx < tx) {
        cout << "R";
        posx++;
    }
    while (posy < ty) {
        cout << "U";
        posy++;
    }
    while (posx > sx) {
        cout << "L";
        posx--;
    }
    while (posy > sy - 1) {
        cout << "D";
        posy--;
    }
    while (posx < tx + 1) {
        cout << "R";
        posx++;
    }
    while (posy < ty) {
        cout << "U";
        posy++;
    }
    cout << "L";
    posx--;
    cout << "U";
    posy++;
    while (posx > sx - 1) {
        cout << "L";
        posx--;
    }
    while (posy > sy) {
        cout << "D";
        posy--;
    }
    cout << "R";
    posx++;
    cout << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Map = map<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF=1LL<<60;
ll MOD=1000000007;
ll gcd(ll a,ll b) {
    while(1) {
    if(a < b) swap(a, b);
    if(!b) break;
    a %= b;
    }
    return a;
}
ll lcm(ll a, ll b){
    return a/gcd(a,b)*b;
}

// mod. m での a の逆元 a^{-1} を計算する
//ただしmとaが互いにその時のみ
long long modinv(long long a) {
    ll m=MOD;
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

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

// a^n mod を計算する
//ただしmとaが互いにその時のみ
long long modpow(long long a, long long n) {
    ll mod =MOD;
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main(){
    ll N;
    cin >> N;
    vector<ll> A(N,0);
    for(ll i=0;i<N;i++){
        cin>>A[i]
    ;}
    Map k=prime_factor(A[0]);
    for(ll i=1;i<N;i++){
        Map s=prime_factor(A[i]);
        for(auto p: s){
            k[p.first]=max(k[p.first],p.second)
        ;}
    ;}
    ll t=1;
    for(auto p: k){
        t*=modpow(p.first,p.second);
        t%=MOD;
    ;}
    ll ans=0;
    for(ll i=0;i<N;i++){
        ans+=t*modinv(A[i])%MOD;
        ans%=MOD;
    ;}
    cout<<ans<<endl;

    return 0;
}


Measured memory_bytes: 896
n, a, b = map(int, input().split())

if a > b:
    a, b = b, a

if a % 2 == b % 2:
    print((b - a) // 2)
else:
    ans1 = b - 1
    ans2 = n - a
    ans3 = a + (b - a) // 2
    ans4 = (n - b + 1) + (n - (a + n - b + 1)) // 2
    print (min(ans1, ans2, ans3, ans4))

Measured memory_bytes: 3060
def main():
    import sys
    input = sys.stdin.readline
    n, q = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ss = [0] * n
    t = 0
    for i in range(n):
        if s[i] == "C" and s[i-1] == "A":
            t += 1
        ss[i] = t
    for l, r in [list(map(int, input().split())) for _ in range(q)]:
        print(ss[r-1] - ss[l-1])


if __name__ == '__main__':
    main()


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

int main(){
	long long int X, Y;
	cin >> X >> Y;
	
	long long int i=0;
	i = log(Y/X);
	
	X *= pow(2,i);
	do{
		X *= 2;
		i++;
	}while(X <= Y);
	
	cout << i << endl;
	return 0;
}


Measured memory_bytes: 640
def getScore(i, y):
	return i*a+y*b+c*(i//10)+d*(y//20)


h1, h2 = map(int, input().split())
k1, k2 = map(int, input().split())
a, b, c, d = map(int, input().split())

h, k = getScore(h1, h2), getScore(k1, k2)
if h>k:
	print('hiroshi')
elif h<k:
	print('kenjiro')
else:
	print('even')



Measured memory_bytes: 5600
N,A,B=map(int,input().split())
if A+B-1>N or A*B<N:
  print(-1)
  exit()
Q=[[] for i in range(B)]
for i in range(N):
  Q[i//A].append(i+1)
Q=Q[::-1]
X=N-1
for i in range(B):
  if len(Q[i])==0:
    Q[i].append(Q[-(X//A+1)][-1])
    del Q[-(X//A+1)][-1]
    X-=1
  else:
    break
P=[]
for i in range(B):
  for j in range(len(Q[i])):
    P.append(Q[i][j])
print(*P)

Measured memory_bytes: 126288
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)
//???
class Point{
	public:	
		double x,y;
		Point(double x = 0,double y = 0):x(x),y(y){}
		//?????????
		Point operator + (Point p) { return Point(x + p.x,y + p.y);}
		Point operator - (Point p) { return Point(x - p.x,y - p.y);}
		Point operator * (double a) { return Point(x*a,y*a);}
		Point operator / (double a) { return Point(x/a,y/a);}
		//???????????¨??§??????
		double abs() {return sqrt(norm());}
		double norm() {return x*x+y*y;}

		bool operator < (const Point &p) const{
			return x != p.x ? x < p.x : y < p.y;
		}

		bool operator > (const Point &p) const{
			return x != p.x ? x > p.x : y > p.y;
		}

		bool operator == (const Point &p) const{
			return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
		}
};
//??????
struct Segment{Point p1,p2;};
//????????????
typedef Point Vector;
//??´???
typedef Segment Line;
//???
class Circle{
	public:
		Point c;
		double r;
		Circle(Point c = Point(),double r = 0.0): c(c),r(r){}
};
//????§???¢
//typedef Vector<Point> Polygon;
//????????¨??????
double dot(Vector a,Vector b){
	return a.x*b.x + a.y*b.y;
}
double cross(Vector a,Vector b){
	return a.x*b.y - a.y*b.x;
}

Point projection(Segment a,Vector hypo){
	Vector base = a.p2 - a.p1;
	double r = dot(hypo,base)/base.norm();
	return  a.p1 + base*r;
}

string check(Point p0,Point p1,Point p2){
	Vector v1 = p1 - p0;
	Vector v2 = p2 - p0;
	if(cross(v1,v2) > EPS){
		return "COUNTER_CLOCKWISE";
	}
	if(cross(v1,v2) < -EPS){
		return "CLOCKWISE";
	}
	if(dot(v1,v2) < -EPS){
		return "ONLINE_BACK";
	}
	if(v1.norm() < v2.norm()){
		return "ONLINE_FRONT";
	}

	return "ON_SEGMENT";
}

int main(){
	int x0,y0,x1,y1;
	int n;
	int x2,y2;
	cin >> x0 >> y0 >> x1 >> y1;
	cin >> n;
	Point s(x0,y0);
	Point t(x1,y1);
	Segment seg;seg.p1 = s;seg.p2 = t;
	Vector base(t.x-s.x,t.y-s.y);
	for(int i = 0;i < n;i++){
		cin >> x2 >> y2;
		Point a(x2,y2);
		cout << check(s,t,a) << endl;
	}
	return 0;
}

Measured memory_bytes: 1204
#include <bits/stdc++.h>
using namespace std;
int main()
{
    unsigned long long int n,a,b,c,d;
    cin>>n>>a>>b;
    c=b-a;
    if(c%2==0){
        cout<<c/2;
    }
    else{
        d=min(a-1,n-b);
        cout<<min(d+1+c/2,min(b-1,n-a));
    }
}


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

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

pair<bool,vi> solve(const vvi& g,int u,const vi& ls,const vi& rs,const vvi& ps)
{
	vi t=ps[u];
	for(int v:g[u]){
		auto p=solve(g,v,ls,rs,ps);
		if(!p.first) return mp(false,vi());
		int sum=0,size=p.second.size();
		for(int x:p.second) sum+=x;
		// if((sum+size/2)/size<=ls[v] || rs[v]<=(sum+size/2)/size) return mp(false,vi())
		if(2*sum+size<=ls[v]*2*size || rs[v]*2*size<=2*sum+size) return mp(false,vi());
		t.insert(end(t),all(p.second));
	}
	return mp(true,t);
}

void paint(const vs& ss,int i,int j,int c,vvi& grid)
{
	int h=ss.size(),w=ss[0].size();
	grid[i][j]=c;
	rep(k,4){
		int ni=i+"\xff\x1\0\0"[k],nj=j+"\0\0\xff\x1"[k];
		if(0<=ni && ni<h && 0<=nj && nj<w && ss[ni][nj]==ss[i][j] && grid[ni][nj]==-1)
			paint(ss,ni,nj,c,grid);
	}
}

int main()
{
	for(int w,h;cin>>w>>h && w|h;){
		vvi grid(h,vi(w,-1));
		{
			vs ss(h);
			rep(i,h) cin>>ss[i];
			int c=1;
			rep(i,h) rep(j,w) if(isdigit(ss[i][j]) && grid[i][j]==-1)
				paint(ss,i,j,c++,grid);
		}
		grid.emplace_back(w,0);
		h++;
		
		int n=0;
		rep(i,h) rep(j,w) n=max(n,grid[i][j]+1);
		
		vi prev(n,-1),ls(n,INF),rs(n,-INF);
		ls[0]=0,rs[0]=w;
		rep(i,h-1) rep(j,w){
			if(grid[i][j]>=0 && grid[i+1][j]>=0 && grid[i][j]!=grid[i+1][j]){
				int u=grid[i+1][j],v=grid[i][j];
				prev[v]=u;
				ls[v]=min(ls[v],j);
				rs[v]=max(rs[v],j+1);
			}
		}
		
		vvi ps(n);
		rep(i,h) rep(j,w) if(grid[i][j]>=0)
			ps[grid[i][j]].push_back(j);
		
		vvi g(n);
		rep(i,n) if(prev[i]!=-1)
			g[prev[i]].push_back(i);
		
		cout<<(solve(g,0,ls,rs,ps).first?"STABLE":"UNSTABLE")<<endl;
	}
}

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

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

int main()
{
    ll X, Y;
    cin >> X >> Y;
    int n = 0;
    while (X <= Y)
    {
        n++;
        X *= 2;
    }
    cout << n << endl;
    return 0;
}

Measured memory_bytes: 256
n,x = map(int,input().split())
a = list(map(int,input().split()))
s = [a[i+1]+a[i] for i in range(n-1)]
ans = 0
for i in range(n-2):
    eat = min(s[i]-x,a[i+1],s[i+1]-x)
    eat = max(eat,0)
    s[i] -= eat
    a[i+1] -= eat
    s[i+1] -= eat
    ans += eat
for i in s:
    ans += max(0,i-x)
print(ans)

Measured memory_bytes: 64356
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c ;
int sum = a + b +c ;
if (sum%2==0&&(sum/2 == a || sum/2 == b || sum/2 == c))
cout << "Yes" << endl;
else 
cout << "No" << endl; 
}

Measured memory_bytes: 256
import java.util.*;
class Main
{
    public static void main (String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] t = new int[n+1], x = new int[n+1], y = new int[n+1];
        boolean flag = true;
        t[0] = 0;
        x[0] = 0;
        y[0] = 0;
        for(int i = 1; i < n+1; i++){
            t[i] = sc.nextInt();
            x[i] = sc.nextInt();
            y[i] = sc.nextInt();
        }
        for(int i = 1; i < n+1; i++){
            if(t[i] - t[i-1] < Math.abs(x[i] - x[i-1]) + Math.abs(y[i] - y[i-1])
               || (t[i] - t[i-1])% 2 != Math.abs(x[i] + y[i] - x[i-1] - y[i-1]) % 2) flag = false;
        }
        String ans = flag ? "Yes" : "No";
        System.out.println(ans);
    }
}

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

int main()
{
  ll N;
  scanf("%lld",&N);
  ll K;
  scanf("%lld",&K);
  vector<ll> h(N);
  for(int i=0; i<N; i++){
    scanf("%lld",&h[i]);
  }

  vector<ll> dp(N, 1e18);
  dp[0] = 0;
  for (int i=0; i<N-1; i++) {
    for (int j=1; j<=K; j++) {
      if (i+j >= N) continue;
      dp[i+j] = min(dp[i+j], dp[i]+abs(h[i+j]-h[i]));
    }
  }
  cout << dp[N-1] << endl;

  return 0;
}


Measured memory_bytes: 1792
using System.Linq;
using System;

public class Hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var k = int.Parse(line[1]);
        line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, int.Parse);
        Console.WriteLine(a.Count(x => x >= k));
    }
}


Measured memory_bytes: 16096
#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define rep(i,n) for (int i=0;i<n;i++)
using ll = long long;
using namespace std;

void solve(){

}
int main (){
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin >> s;
    ll n=0;
    // if(s=="0"){
    //     cout << "No";
    //     return 0;
    // }
    for(int i=0;i<s.size();i++){
        n += s[i] - '0';
    }
    if(n%9==0) cout << "Yes";
    else cout << "No";



    return 0;
}

Measured memory_bytes: 3780
#include <bits/stdc++.h>
using namespace std;
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

int main(int argc, char *argv[])
{
  char n[5];
  scanf("%s",&n);

  if(n[0]==n[1]&&n[0]==n[2]){puts("Yes");return 0;}
  if(n[1]==n[2]&&n[1]==n[3]){puts("Yes");return 0;}
  
  puts("No");
  return 0;
}


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

int main() {
  int N, K, tmp = 1;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    if (tmp * 2 > tmp + K) {
      tmp += K;
    } else {
      tmp *= 2;
    }
  }
  cout << tmp << endl;
  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>


#define INF 1000000000
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)


int main(int argc, char** argv)
{
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  int N, dp[111111];

  std::vector<int> a;

  for (long long i = 6; i < 111111; i *= 6)
    a.push_back(i);

  for (long long i = 9; i < 111111; i *= 9)
    a.push_back(i);

  std::sort(ALL(a));

  std::cin >> N;

  std::iota(dp, dp + 111111, 0);

  for (int i = 0; i < 111111; i ++)
    for (const auto& x : a)
      if (i + x < 111111)
        dp[i + x] = std::min(dp[i] + 1, dp[i + x]);

  std::cout << dp[N] << std::endl;

  return 0;
}


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

N = int(input())
A = list(map(int, input().split()))
S = A[0]+A[1]
X = 0
A0 = A[0]

for i in range(2, N):
    X ^= A[i]

L = 45
dp = [[[-1]*2 for _ in range(2)] for _ in range(L+1)]
dp[0][0][0] = 0

for i in range(L):
    for j in range(2):
        for k in range(2):
            if dp[i][j][k]==-1:
                continue
            
            for a in range(2):
                for b in range(2):
                    nj, s = divmod(a+b+j, 2)
                    x = a^b
                    
                    if s!=S&1:
                        continue
                    
                    if x!=X&1:
                        continue
                    
                    if a<A0&1:
                        nk = 0
                    elif a>A0&1:
                        nk = 1
                    else:
                        nk = k
                    
                    dp[i+1][nj][nk] = max(dp[i+1][nj][nk], dp[i][j][k]+(a<<i))
    
    S >>= 1
    X >>= 1
    A0 >>= 1

if dp[L][0][0]<=0:
    print(-1)
else:
    print(A[0]-dp[L][0][0])

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

const long long MOD = 1000000007;
const int MAX = 1000010;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(void) {
    long long N, ans = 0;
    cin >> N;
    long long tt = 1, uu = 1, tinv = modinv(2, MOD);
    COMinit();
    for (int i = 0; i < N; i++) {
        tt = (tt * 2) % MOD;
    }
    for (int i = 0; i < N - 1; i++) {
        long long t3 = tt - 2;
        if (t3 < 0) {
            t3 += MOD;
        }
        ans = (ans + ((COM(N, i) * t3 % MOD) * uu % MOD)) % MOD;
        uu = (uu * 8) % MOD;
        tt = (tt * tinv) % MOD;
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 27140
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector< pair<int, int> > child(n);
  for (int i = 0; i < n; i++){
    int id, left, right;
    cin >> id >> left >> right;
    child[id].first = left;
    child[id].second = right;
  }
  vector<int> parent(n, -1);
  for (int i = 0; i < n; i++){
    if (child[i].first != -1){
      parent[child[i].first] = i;
    }
    if (child[i].second != -1){
      parent[child[i].second] = i;
    }
  }
  int root;
  for (int i = 0; i < n; i++){
    if (parent[i] == -1){
      root = i;
      break;
    }
  }
  vector<int> sibling(n, -1);
  for (int i = 0; i < n; i++){
    if (parent[i] != -1){
      if (child[parent[i]].first != -1 && child[parent[i]].first != i){
        sibling[i] = child[parent[i]].first;
      }
      if (child[parent[i]].second != -1 && child[parent[i]].second != i){
        sibling[i] = child[parent[i]].second;
      }
    }
  }
  vector<int> degree(n, 0);
  for (int i = 0; i < n; i++){
    if (child[i].first != -1){
      degree[i]++;
    }
    if (child[i].second != -1){
      degree[i]++;
    }
  }
  vector<int> depth(n, 0);
  stack<int> S;
  S.push(root);
  while (!S.empty()){
    int v = S.top();
    S.pop();
    if (child[v].first != -1){
      depth[child[v].first] = depth[v] + 1;
      S.push(child[v].first);
    }
    if (child[v].second != -1){
      depth[child[v].second] = depth[v] + 1;
      S.push(child[v].second);
    }
  }
  vector<int> height(n, -1);
  S.push(root);
  while (!S.empty()){
    int v = S.top();
    if (child[v].first == -1 && child[v].second == -1){
      height[v] = 0;
      S.pop();
    } else if (child[v].first == -1){
      if (height[child[v].second] == -1){
        S.push(child[v].second);
      } else {
        height[v] = height[child[v].second] + 1;
        S.pop();
      }
    } else if (child[v].second == -1){
      if (height[child[v].first] == -1){
        S.push(child[v].first);
      } else {
        height[v] = height[child[v].first] + 1;
        S.pop();
      }
    } else {
      if (height[child[v].first] == -1){
        if (height[child[v].second] == -1){
          S.push(child[v].first);
          S.push(child[v].second);
        } else {
          S.push(child[v].first);
        }
      } else {
        if (height[child[v].second] == -1){
          S.push(child[v].second);
        } else {
          height[v] = max(height[child[v].first], height[child[v].second]) + 1;
          S.pop();
        }
      }
    }
  }
  vector<string> type(n);
  for (int i = 0; i < n; i++){
    if (i == root){
      type[i] = "root";
    } else if (degree[i] == 0){
      type[i] = "leaf";
    } else {
      type[i] = "internal node";
    }
  }
  for (int i = 0; i < n; i++){
    cout << "node " << i << ": parent = " << parent[i] << ", sibling = " << sibling[i] << ", degree = " << degree[i] << ", depth = " << depth[i] << ", height = " << height[i] << ", " << type[i] << endl;
  }
}


Measured memory_bytes: 3176
#include <stdio.h>

#define MAX_VAL 1000000000
#define MAX_SIZE 200
#define TRUE 1
#define FALSE 0

static int chk_even(int *a, int n);

int main(void)
{
    int i, j,n, exec_cnt;
    int a[MAX_SIZE];
    int even=FALSE;

    scanf("%d", &n);
    //標準入力から読込み
    for(i=0; i<n; i++){
        scanf("%d", &a[i]);
    }

    for(i=0; i<(MAX_VAL/2); i++){
        //偶数チェック
        even = chk_even(a, n);
        //すべて偶数なら2で割ったものに置き換え
        if( even == TRUE){
            for(j=0; j<MAX_SIZE; j++){
                a[j] = a[j]/2;
            }
            exec_cnt++;
        }
        else{
            break;
        }
    }

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

static int chk_even(int *a, int n)
{
    int i;
    int even;

    for(i=0; i<n; i++){
        // 奇数の場合ループ抜ける
        if( (a[i]%2) != 0 ){
            even = FALSE;
            break;
        }
        //偶数の場合
        else{
            even = TRUE;
        }
    }
    return even;
}

Measured memory_bytes: 128
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		StringBuilder sb=new StringBuilder(sc.nextLine());
		for(int i=1;i<sb.length();i++) {
			sb.deleteCharAt(i);
		}
		System.out.println(sb.toString());
	}
}

Measured memory_bytes: 26188
#include <bits/stdc++.h>
using namespace std;
#define si(x)       scanf("%d",&x)
#define su(x)       scanf("%u",&x)
#define sll(x)      scanf("%lld",&x)
#define sl(x)       scanf("%ld",&x)     
#define sc(x)       scanf("%c",&x)
#define ss(x)       scanf("%s",x)
#define sf(x)       scanf("%f",&x)
#define slf(x)      scanf("%lf",&x)
#define pri(x)      printf("%d\n",x)
#define prl(x)      printf("%lld\n",x)
#define ll          long long int
#define mod(x,n)    (x+n)%n
#define pb          push_back
#define mp          make_pair
#define pii         pair<int,int>
#define pll         pair<long long,long long>
#define pil         pair<int,long long>
#define pli         pair<long long,int>
#define vi          vector<int>
#define vl          vector<long long>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
#define M           1000000007
 
 
int main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    //cout.tie(NULL);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	int n; si(n);
	int l,r,ans=0;
	for(int i=1;i<=n;i++){
		si(l); si(r);
		ans+=(r-l+1);
	}
    pri(ans);
    return 0;
}

Measured memory_bytes: 256
MAX = 0
M = 0
N = 0
def dfs(A:list,ABCD:list):
    global MAX,N,M
    if(len(A) == N):
        total = 0
        for a,b,c,d in ABCD:
            if(A[b - 1] - A[a - 1] == c):
                total += d
        MAX = max(MAX,total)
        return
    pre = A[-1] if(len(A) > 0) else 1
    for v in range(pre,M+1):
        A.append(v)
        dfs(A,ABCD)
        A.pop()

def main():
    global N,M
    N,M,Q = map(int,input().split())
    lis = []
    for _ in range(Q):
        lis.append(list(map(int,input().split())))
    dfs([],lis)
    print(MAX)

if __name__ == "__main__":
    main()

Measured memory_bytes: 9184
S=input()
T=input()
a=[]
for i in range(len(S)-len(T)+1) :
  count=0
  for j in range(len(T)) :
    if S[i:i+len(T)][j]==T[j] :
      count+=1
  a.append(count)
print(len(T)-max(a))

Measured memory_bytes: 8904
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
typedef long long ll;

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

vector<int> v(3010000);
int main(){
  int s;
  cin >> s;
  v[s]++;
  int preans=s;
  for(int i=2;i<1000001;i++){
    if(v[f(preans)]) {cout << i << endl; return 0;}
    v[f(preans)]++;
    preans=f(preans);
  }
  return 0;
}


Measured memory_bytes: 12032
#include<bits/stdc++.h>
using namespace std;
#define SORT(x) sort(x.begin(),x.end())
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int main(){
    string s;
    cin>>s;
    int k[27]={};
    rep(i,4){
        k[s[i]-'A']++;
    }
    rep(i,27){
        if(k[i]!=2&&k[i]!=0){
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;

    return 0;
}

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

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#endif


const int MAXN = 100;
//#define int long long
const int INF = 1e9;

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

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    string s;
    cin >> s;

    vector<int> r, g, b;

    for (int i = 0; i < n; i++){
      if (s[i] == 'R') r.eb(i);
      else if (s[i] == 'G') g.eb(i);
      else b.eb(i);
    }

    ll ans = 0LL;
    for (int i = 0; i < n; i++){
      for (int j = i + 1; j < n; j++){
        if (s[i] == s[j]) continue;

        if (s[i] != 'R' && s[j] != 'R') {
          ans += (ll)(r.end() - upper_bound(r.begin(), r.end(), j));
          int d = j - i;
          if (j + d < n && s[j + d] == 'R') ans--;
        }
        else if (s[i] != 'G' && s[j] != 'G'){
          ans += (ll)(g.end() - upper_bound(g.begin(), g.end(), j));
          int d = j - i;
          if (j + d < n && s[j + d] == 'G') ans--;
        }
        else if (s[i] != 'B' && s[j] != 'B') {
          ans += (ll)(b.end() - upper_bound(b.begin(), b.end(), j));
          int d = j - i;
          if (j + d < n && s[j + d] == 'B') ans--;
        }
      }
    }

    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 3660
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int size=2e5;
const int inf=1e9;
int main(){
	int x,y; cin>>x>>y;
	int a[12] = {0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0};
	cout<<(a[x-1] == a[y-1] ? "Yes" : "No")<<endl;
}

Measured memory_bytes: 3672
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>

using namespace std;

struct Edge{ int to, cost; };
vector<Edge>G[100005];
double p;
int N;
int visited[100005];

double dfs(int x, double cost, double prob){
  //  cout << x << " " << cost << " " << prob << endl;
  visited[x] = 1;
  double ret = prob*cost;
  for(int i=0;i<G[x].size();i++){
    if(visited[G[x][i].to]) continue;
    ret += dfs(G[x][i].to, cost, prob*p) + prob*p*G[x][i].cost;
  }

  return ret;
}

int main(){
  cin >> p >> N;
  for(int i=0;i<N-1;i++){
    int x, y, c; cin >> x >> y >> c; x--; y--;
    G[x].push_back((struct Edge){y, c});
    G[y].push_back((struct Edge){x, c});
  }

  memset(visited, 0, sizeof(visited));
  double cost = dfs(0, 0.0, 1.0);
  //  cout << cost << endl;
  memset(visited, 0, sizeof(visited));
  //  cout << showpoint << dfs(0, cost, 1.0) << endl;;
  printf("%.10f\n", dfs(0, cost, 1.0));
  return 0;
}

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

pair<int,pair<int,int>> p[40];

set<pair<int,pair<int,int>>> s;

int n,ma,mb;
int a,b,c;

int dp[51][500][500];

const int INF = 1000000;

int main() {

    cin >> n >> ma >> mb;

    for(int i=0; i<n; i++){
        cin >> a >> b >> c;

        p[i] = make_pair(c,make_pair(a,b));

    }

    for(int i=0; i<=n; i++){
        for(int j=0; j<500; j++){
            for(int k=0; k<500; k++){
                dp[i][j][k] = INF;
            }
        }
    }

    dp[0][0][0] = 0;

    for(int i=0; i<n; i++){
        for(int j=0; j<500; j++){
            for(int k=0; k<500; k++){
                dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k]);
                if(j+p[i].second.first < 500 && k+p[i].second.second<500){
                    dp[i+1][j+p[i].second.first][k+p[i].second.second] = min(dp[i+1][j+p[i].second.first][k+p[i].second.second],dp[i][j][k] + p[i].first);
                }
            }
        }
    }

    int ans = INF;

    for(int j=1; j<500; j++){
        for(int k=1; k<500; k++){
            if(j*mb == k*ma){
                ans = min(ans,dp[n][j][k]);
            }
        }
    }

    if(ans == INF) {
        cout << -1 << endl;
        return 0;
    }

    cout << ans << endl;

    return 0;
}

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

int N, M;

class Data {
public:
  int t, s, d;
};

Data data[21234];
bool infected[21234];

bool operator < (const Data& a, const Data& b) {
  return a.t < b.t;
}

int main() {

  for(;;) {
    std::cin >> N >> M;
    if( N == 0 and M == 0 ) break;
    for(int i = 0; i < M; ++i) {
      std::cin >> data[i].t >> data[i].s >> data[i].d;
    }
    std::sort(data, data + M);
    for(int i = 1; i <= N; ++i) {
      infected[i] = false;
    }
    infected[1] = true;
    for(int i = 0; i < M; ++i) {
      infected[data[i].d] |= infected[data[i].s];
    }    
    int res = 0;
    for(int i = 1; i <= N; ++i) {
      if( infected[i] ) res += 1;
    }
    std::cout << res << std::endl;
  }  

  return 0;
}
  

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

int main() {
  string S, T;
  cin >> S >> T;

  bool ok = true;

  vector<int> s(26), t(26);
  rep(i, S.size()) s[S[i] - 'a']++;
  rep(i, T.size()) t[T[i] - 'a']++;

  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  rep(i, 26) {
    if (s[i] == t[i])
      continue;
    ok = false;
  }

  if (ok)
    puts("Yes");
  else
    puts("No");
}

Measured memory_bytes: 776
#include <stdio.h>
#include <math.h>

int main(void){
    int i, j, n, x[9], y[9];
    double sum = 0;
    scanf("%d", &n);
    for( i = 0; i < n; i++ ){
        scanf("%d %d", &x[i], &y[i]);
    }
    
    for( i = 0; i < n-1; i++ ){
        for( j = i+1; j < n; j++ ){
            sum += sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) );
        }
    }
    
    printf("%.10lf\n", sum*2/n);
    return 0;
}

Measured memory_bytes: 128
import java.util.Scanner;

public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = sc.nextInt();
		int t = sc.nextInt();
		int a = sc.nextInt();
		int[] h = new int[n];
		double[] x = new double[n];
		for(int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			x[i] = t-h[i]*0.006;
		}
		System.out.println(palace(x,a));

	}

	public static int palace(double[] x, int a) {
		double tmp = Math.abs(a - x[0]);
		int tmp2 = 1;
		for(int i = 1; i < x.length; i++) {
			if(Math.abs(a - x[i]) < tmp) {
				tmp = Math.abs(a - x[i]);
				tmp2 = i+1;
			}
		}
		return tmp2;
	}
}

Measured memory_bytes: 23636
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.141592653589793
#define MOD 1000000007

long int combination(long int n, long int r);

//qsort用、昇順
int compare_int(const void *a, const void *b)
{
	return *(int*)a-*(int*)b;
}
//qsort(data, 10, sizeof(int), compare_int)

long int combination(long int n, long int r)
{
	if(r>n-r) r = n-r;
	if(r == 0) return 1;
	else if(r == 1) return n;
	return (combination(n-1, r) + combination(n-1, r-1));
	//大きくなるとき
	/*
	long int tmp1, tmp2, tmp3;
	tmp1 = combination(n-1, r);
	if(tmp1>=MOD) tmp1 = tmp1%MOD;
	tmp2 = combination(n-1, r-1);
	if(tmp2>=MOD) tmp2 = tmp2%MOD;
	tmp3 = tmp1+tmp2;
	if(tmp3>=MOD) tmp3 = tmp3%MOD;
	return (tmp3);
	*/
}


int main(){
	char str[3];
	scanf("%s", str);
	int aru=0;
	char s = str[0];
	if(s != str[1]) aru=1;
	else{
		if(s != str[2]) aru = 1;
		else aru = -1;
	}

	if(aru==1) printf("Yes\n");
	else printf("No\n");

	return 0;
}

Measured memory_bytes: 128
<?php
fscanf(STDIN, "%d%d", $N, $M);
for($i = 0; $i < $M; $i++){
  fscanf(STDIN, "%d%d", $u, $t);
  $c[$u][] = $t;
}
fscanf(STDIN, "%d%d", $S, $T);

$p[0][$S] = 1;
for($i = 0; 1; $i++){
  if(!isset($p[$i])){
    echo -1;exit;
  }
  foreach($p[$i] as $current => $_){
    if(isset($c[$current]))foreach($c[$current] as $next){
      if(!isset($map[$i % 3][$next])){
        $map[$i % 3][$next] = true;
        $p[$i+1][$next] = true;
      }
      if(($i+1)%3==0&&$next==$T){
        echo ($i+1) / 3;
        exit;
      }
    }
  }
}

Measured memory_bytes: 178664
#include<iostream>
using namespace std;
char s1[5];
char s2[5];
int count;
int main(){
	for(int i  = 1; i <= 3; i++){
		cin >> s1[i];
	}
	for(int i = 1; i <= 3; i++)
		cin >> s2[i];
	for(int j = 1; j <= 3; j++)
		if(s1[j] == s2[j]) count ++;
	cout << count << endl;
}

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

int main(){
    vector<vector<int>> c(3, vector<int>(3, 0));
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            cin >> c.at(i).at(j);
        }
}
    
    vector<int> a(3), b(3);
    a.at(0) = 0;
    b.at(0) = c.at(0).at(0);
    b.at(1) = c.at(0).at(1);
    b.at(2) = c.at(0).at(2);
    a.at(1) = c.at(1).at(0) - b.at(0);
    a.at(2) = c.at(2).at(0) - b.at(0);
    vector<vector<int>> c2(3, vector<int>(3, 0));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            c2.at(i).at(j) = a.at(i) + b.at(j);
        }
        
    }
    if(c == c2) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

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

int main(){
    char a;
    cin >> a;
    if('a' <= a && a <= 'z') cout("a");
    else cout("A");
}


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

void _150() {
    string s;
    cin >> s;

    if ((s == "AAA") || (s == "BBB"))
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
}

void _156() {
    int n, r;
    cin >> n >> r;

    if (n >= 10)
        cout << r << endl;
    else
        cout << r + 100 * (10 - n) << endl;
}

void _155() {
    int a, b, c;
    cin >> a >> b >> c;

    if ((a != b) && (a != c) && (b != c))
        cout << "No" << endl;
    else if ((a == b) && (b == c))
        cout << "No" << endl;
    else
        cout << "Yes" << endl;
}

void _154() {
    string s, t, u;
    int a, b;
    cin >> s >> t;
    cin >> a >> b;
    cin >> u;

    if (s ==u)
        cout << a - 1 << " " << b << endl;
    else
        cout << a << " " << b - 1 << endl;
}

int main()
{
    // _150();
    _154();
    
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

int main() {
  ll a, b, c;
  cin >> a >> b >> c;
  if (a >= c && b >= c) {
    cout << "No" << endl;
    return 0;
  }
  if (4 * a * b < a * a + b * b + c * c + 2 * (a * b - a * c - b * c)) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using lint=int64_t;
using namespace std;

const lint mod=1e9+7;
const int A=0;
const int T=1;
const int G=2;
const int C=3;

lint N;
lint dp[110][5][5][5];

lint rec(int n,int a,int b,int c)
{
	if(dp[n][a][b][c]!=-1)
		return dp[n][a][b][c];
	
	lint ret=0;
	if(n==N)
	{
		ret=1;
		if(a==A && b==G && c==C)
			ret=0;
		else if(a==A && b==C && c==G)
			ret=0;
		else if(a==G && b==A && c==C)
			ret=0;
		dp[n][a][b][c]=ret;
		return ret;
	}

	if(a==A && b==G && c==C)
		ret=0;
	else if(a==A && b==C && c==G)
		ret=0;
	else if(a==G && b==A && c==C)
		ret=0;
	else if((a==A && c==G) || (a==A && b==G))
	{
		for(int i=0;i<4;i++)
		{
			if(i==C)
				continue;
			ret+=rec(n+1,b,c,i);
			ret%=mod;
		}
	}
	else
	{
		for(int i=0;i<4;i++)
		{
			ret+=rec(n+1,b,c,i);
			ret%=mod;
		}
	}

	dp[n][a][b][c]=ret;
	return ret;
}

int main()
{
	for(int i=0;i<110;i++)
		for(int a=0;a<5;a++)
			for(int b=0;b<5;b++)
				for(int c=0;c<5;c++)
					dp[i][a][b][c]=-1;
	
	cin >> N;

	cout << rec(0,T,T,T) << endl;
	return 0;
}



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

#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vi >
#define vpii vector<pii >
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=998244353;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;

template<typename T>
inline int getplc(T x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}

LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}

template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) {
	return max(a, Smax(args...));
}
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) {
	return min(a, Smin(args...));
}

template <typename T>
#define errorl(args...) {  \
	string _s = #args;   \
	replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    errl(_it, args);  \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}
#define error(args...) {  \
  string _s = #args;  \
  replace(_s.begin(), _s.end(), ',', ' ');  \
  stringstream _ss(_s);  \
  istream_iterator<string> _it(_ss);  \
  err(_it, args);  \
  cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

vi g[N];
int vis[N];

void dfs(int x) {
  vis[x] = 1;
  for (auto v : g[x]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

void Solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v), g[v].pb(u);
  }

  int ans = -1;
  rep (i, 1, n) if (!vis[i]) dfs(i), ans++;

  cout << ans << '\n';
}


int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//  freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}


Measured memory_bytes: 31028
#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdlib>
#include<time.h>
#include<string>
#include<stack>
#include<cmath>
#include<iostream>
#include<cstring>
#include<complex>

using namespace std;
#define FOR(i,j,k) for(int i=j;i<k;i++)
#define FORR(i,j,k) for(int i=j;i>=k;i--)
#define ll long long
//Make sure no overflow problems
//#define int long long
#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define mp make_pair

#define VAR(i,n) __typeof(n) i = (n)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();i++)
#define FORDEACH(i,c) for( VAR(i,(c).rbegin()),i!=(c).rend();i++)
#define REP(i,n) FOR(i,0,n)
#define ld long double
const int INF = 1e9 + 7;

const long long INFLL = (ll)INF * (ll)INF;
const ld EPS = 10e-9;

using namespace std;

int frog(vector<int>& heights, int N, int currentIteration)
{
    if (currentIteration + 2 == N)
    {
        return abs(heights[currentIteration] - heights[currentIteration + 1]);
    }
    else if (currentIteration + 2 >= N)
    {
        return 0;
    }

    return min(
        frog(heights, N, currentIteration + 1) + abs(heights[currentIteration] - heights[currentIteration + 1]),
        frog(heights, N, currentIteration + 2) + abs(heights[currentIteration] - heights[currentIteration + 2]));
}


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

    vector<int> heights(N, 0);
    vector<int> dp(N + 1, INF);
    for (int i = 0; i < N; i++)
    {
        cin >> heights[i];
    }

    /*dp[1] = abs(heights[0] - heights[1]);
    for (int i = 2; i < N; i++)
    {
        dp[i] = min(dp[i - 1] + abs(heights[i - 1] - heights[i]), abs(dp[i - 2] + abs(heights[i] - heights[i - 2])));
    }*/

    dp[0] = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = i ; j <= i + K; j++)
        {
            if (j < N)
            {
                dp[j] = min(dp[j], dp[i] + abs(heights[i] - heights[j]));
            }
        }
    }
    
    cout << dp[N - 1] << endl;
    //cout << frog(heights, N, 0);
    return 0;
}

Measured memory_bytes: 1024
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) sort((a).rbegin(), (a).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = n; 0 <= i; i--)
#define FOR(i, start, end) for (int i = start; i < end; i++)
#define FORSTEP(i, start, end, step) for (int i = start; i < end; i += step)
#define ALL(a) a.begin(), a.end()
typedef long long ll;
const int INF = 100100100;
using namespace std;

int gcd(int x, int y) {
    if (y == 0)
        return x;
    gcd(y, x % y);
}

int gcd(vector<int> x) {
    int ans                 = x[0];
    FOR(i, 1, x.size()) ans = gcd(ans, x[i]);
    return ans;
}

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

    int N, X;
    cin >> N >> X;
    vector<int> A(N);
    REP(i, N) cin >> A[i];
    A.push_back(X);
    SORT(A);

    vector<int> V(N);
    REP(i, N) { V[i] = A[i + 1] - A[i]; }

    if (V.size() == 1) {
        cout << V[0] << endl;
        return 0;
    }

    int ans = gcd(V);
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1400
#include<bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

//ループマクロ
#define FOR(i , a , b)  for(int i = (a) ; i < (b) ; i++)
#define REP(i , n)      for(int i = 0 ; i < n ; i++)
#define REPR(i , n)     for(int i = n ; 0 <= i ; i--)
#define ROP(i, n)       for(int i = 1 ; i <= n ; i++)

//入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x) for(auto& ele : (x) ) cin >> ele
#define VECOUT(x) for(auto& ele : (x) ) cout << ele << " "; cout << endl;

//その他
#define ALL(obj) (obj).begin() , (obj).end() 
#define V vector<int>
#define VLL vector<long long>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int , V , greater<int>>
ll func(int a){
  	if(a == 1)  return 1;
	return a + func(a - 1);
}
int main(void) {

	ios::sync_with_stdio(false);	cin.tie(0);
	int N, K;  cin >> N >> K;
	V A(N);  VECCIN(A);
	ll ans = 0;

  	ll plus1 = func(K), plus2 = plus1 - K;
	REP(i, N){
			REP(j, N){
					if(i == j)  continue;

					if(A[i] > A[j])  ans += (i < j) ? plus1 : plus2;
					ans %= MOD;
			}
	}

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


Measured memory_bytes: 384
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<map>
#include<algorithm>
using namespace std;
int main(void){
    string s;
    cin>>s;
    int c=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='1') c++;
    }
    cout<<c<<endl;
    return 0;
}

Measured memory_bytes: 256
import scala.io.StdIn._
object Main extends App {

  val n = readInt()
  val v = readLine.trim.split(' ').map(_.toInt)
  val c = readLine.trim.split(' ').map(_.toInt)
  println(v.zip(c).collect{case (i, j) if i > j ⇒ i - j}.sum)
}


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

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

    if((A + B) > (C + D)){
        cout << "Left" << endl;
    }else if ((A + B) == (C + D)){
        cout << "Balanced" << endl;
    }else{
        cout << "Right" << endl;
    }
        
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define mod 1000000007
ll Frog_Problem(ll n, ll a[], ll dp[]) {
    if (n <= 0) {
        return 0;
    }
    if (dp[n] != -1) {
        return dp[n];
    }
    ll op1 = INT_MAX, op2 = INT_MAX;
    if (n - 2 >= 0) {
        op1 = abs(a[n] - a[n - 2]) + Frog_Problem(n - 2, a, dp);
    }
    op2 = (abs(a[n] - a[n - 1]) + Frog_Problem(n - 1, a, dp));
    ll ans = min(op1, op2);
    return dp[n] = ans;
}
int main() {
    fio;
    ll n;
    cin >> n;
    ll a[n];
    ll dp[n];
    memset(dp, -1, sizeof(dp));
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
    }
    cout << Frog_Problem(n - 1, a, dp);
    return 0;
}

Measured memory_bytes: 8204
L,R=map(int,input().split())

ans=2018
i=L
while i<R:
    j=i+1
    while j<=R:
        if i*j%2019==0:
            ans=0
            break
        else:
            ans=min(ans,i*j%2019)
            j+=1
    i+=1
    if ans==0:
        break

print(ans)

Measured memory_bytes: 9104
A = []
for i in range(3):
    A.append(list(map(int,input().split())))

n = int(input())

def bg(a):
    f = False
    for i in range(3):
        t = 0
        for j in range(3):
            t += a[i][j]
        if t== 3:
            f = True

    for i in range(3):
        t = 0
        for j in range(3):
            t += a[j][i]
        if t== 3:
            f = True
    t = 0
    s = 0
    for i in range(3):
        t += a[i][i]
        s += a[i][-i-1]
    if t==3 or s ==3:
        f = True
    return f
ma = [[0]*3 for i in range(3)]
for c in range(n):
    b = int(input())
    for i in range(3):
        for j in range(3):
            if A[i][j]==b:
                ma[i][j] = 1
if bg(ma):
    print("Yes")
else:
    print("No")


Measured memory_bytes: 3064
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[4],k;
int main()
{
	int p=1;
	for(int i=1;i<=3;i++)
	{
        scanf("%d",&a[i]);
		if (a[i]>a[p])
			p=i;
	}
	scanf("%d",&k);
	for(int i=1;i<=k;i++)
	    a[p]=a[p]*2;
	printf("%d",a[1]+a[2]+a[3]);
	return 0;
}

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

typedef long long ll;

#define rep(i,n) for(int i = 0; i < n; i++)
#define debug(x) cerr << #x << ": " << x << "\n";

using namespace std;
 
const ll INF = ll(1e18)+1;
const double PI=3.14159265358979323846;

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

  vector<int> snuke(N+1);
  rep(i,K){
    int d;
    cin >> d;
    rep(i,d){
      int A;
      cin >> A;
      snuke[A]++;
    }
  }
  int ans = 0;
  for(int i=1; i<=N; i++)
    if(snuke[i] == 0) ans++;
  cout << ans << endl;
}


Measured memory_bytes: 3600