akhauriyash/Code-Regression huggingface dataset:
using System;
using System.Collections.Generic;

class MyClass
{
    static void Main(string[] args)
    {
        string[] str = Console.ReadLine().Split(' ');
        List<int> list = new List<int>();
        for (int i = 0; i < 5; i++)
        {
            list.Add(int.Parse(str[i]));
        }
        list.Sort();
        list.Reverse();
        Console.WriteLine("{0} {1} {2} {3} {4}", list[0], list[1], list[2], list[3], list[4]);
    }
}


Measured memory_bytes: 5816
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define ff first
#define ss second
#define dd long double
#define mod 1000000007
const ll N=2e3+10;
ll dp[N];
ll ans(ll n){
    if(dp[n]!=-1){
        return dp[n];
    }
    if(n==0){
        return 1;
    }
    ll sum=0;
    for(ll i=3;i<=n;i++){
        if(n-i==0 || n-i>=3){
            sum=(sum+ans(n-i))%mod;
        }
    }
    return dp[n]=sum;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    REP(i,N){
        dp[i]=-1;
    }
    ll n;
    cin>>n;
    cout<<ans(n)<<endl;
}


Measured memory_bytes: 3580
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long k = sc.nextLong();

        if (a >=  k) {
            System.out.println(a - k + " " + b);
        } else {
            System.out.println(0 + " " + Math.max(0, b - k + a));
        }
    }

}

Measured memory_bytes: 23124
#include <bits/stdc++.h>
using namespace std;int main(){int N;cin>>N;int ANS[60000];for(int i=0;i<N;i++)ANS[i]=0;for(int x=1;x<101;x++)for(int y=1;y<101;y++)for(int z=1;z<101;z++)ANS[x*x+y*y+z*z+x*y+y*z+z*x-1]++;for(int i=0;i<N;i++)cout<<ANS[i]<<endl;}


Measured memory_bytes: 3768
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)

typedef long long ll;

const int Inf = 1e9;
const double EPS = 1e-9;

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

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

int bitCount(long bits) {
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a, b;
    cin >> a >> b;
    if (a > 8 || b > 8) {
      cout << ":(" << '\n';
    } else {
      cout << "Yay!" << '\n';
    }

    return 0;
}


Measured memory_bytes: 256
n, m = [int(i) for i in input().split(" ")]
q = [(0, 0)]
for i in range(0, m):
    a, b = input().split(" ")
    q.append((int(a), int(b)))

q = sorted(q, key=lambda k: k[0])
q.append((10**5+1, 10**5+1))

t_min = 10**5 + 1
l = 0
r = 1
c = 0
for i in range(1, m + 1):
    if q[i][1] < t_min:
        t_min = q[i][1]
        #print("t_min", t_min)
    if q[i][0] != q[i + 1][0]:
        if r <= q[i][0]:
            c += 1
            r = t_min
            #print("pp", i)
        else:
            if t_min < r:
                r = t_min
        l = q[i][0]
        t_min = 10**5 + 1
        #print("lr:", i, l, r)
print(c)

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

using namespace std;

int main() {
  int r, D, x;
  
  cin >> r >> D >> x;
  
  for (int i = 0; i < 10; i++) {
    x = r * x - D;
    
    cout << x << endl;
  }
  
  return 0;
}

Measured memory_bytes: 384
INF = 10**10
def solve(n,m,x,c,a):
    best = INF
    for mask in range(2**n):
        v = [0] * m
        cost = 0
        for i in range(n):
            if mask & (2**i):
                cost += c[i]
                for j in range(m):
                    v[j] += a[i][j]
        if min(v) >= x:
            best = min(best, cost)
    return -1 if (best == INF) else best

n, m, x = map(int, input().split())
c = []
a = []
for i in range(n):
    v = list(map(int, input().split()))
    c.append(v[0])
    a.append(v[1:])
print(solve(n, m, x, c, a))


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

int main() {
	int N, M; cin >> N >> M;
	vector<vector<int>> v(N+1);
	for(int i=0; i<M; ++i){
	    int a, b;
	    cin >> a >> b;
	    v[a].push_back(b);
	    v[b].push_back(a);
	}
	vector<int> F;
	int cnt=0;
	bool big=false;
	for(int i=1; i<=N; ++i){
	    if(v[i].size()%2>0){
	        cout << "No" << endl;
	        return 0;
	    }
	    if(v[i].size()==4) F.push_back(i);
	    if(v[i].size()>5) big=true;
	    cnt += v[i].size();
	    cnt -= 2;
	}
	if(cnt<3){
	    cout << "No" << endl;
	    return 0;
	}
	if(cnt>5 || big){
	    cout << "Yes" << endl;
	    return 0;
	}
	vector<bool> ch(N+1, true);
	for(int i=0; i<4; ++i){
	    if(ch[v[F[0]][i]]){
	        int se=v[F[0]][i];
            ch[F[0]]=false;
	        while(se!=F[0] && se!=F[1]){
	            ch[se]=false;
	            se=(ch[v[se][0]] ? v[se][0] : v[se][1]);
                ch[F[0]]=true;
	        }
	        if(se==F[0]){
	            cout << "Yes" << endl;
	            return 0;
	        }
	    }
	}
	cout << "No" << endl;
	return 0;
}


Measured memory_bytes: 5760
#include <iostream>
#include <vector>
using namespace std;
const long long int MOD = 1000000007;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];

    vector<int> memo(n, 0);
    for(int i = 0; i < n; i++) memo[a[i]]++;
    long long int ans = 1;
    for(int i = n-1; i >= 0; i -= 2){
        if(memo[i] == 2) ans *= 2;
        else if(i == 0 && memo[i] == 1) ans *= 1;
        else ans = 0;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};
int main(){
  int i,j,n;
  mint dp[100010][30];
  string s,t;
  scanf("%d\n",&n);
  cin >> s >> t;
  for(i=0;i<n;i++){
   for(j=0;j<=26;j++){
     dp[i][j]=0;
   }
  }
  dp[1][s[0]-'a']=1;
  for(i=1;i<n-1;i++){
    dp[i+1][s[i]-'a']+=dp[i][t[i]-'a'];
    for(j=0;j<=26;j++){
      dp[i+1][j]+=dp[i][j];
    }
  }
  printf("%lld\n",dp[n-1][t[i]-'a']);
  return 0;
}


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

#define reps(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   reps(i,0,n)
#define all(x) (x).begin(),(x).end()
#define INF (1000000000)
#define MOD (1000000007)
#define PI (acos(-1))

int main(){
  int n;
  string s1,s2;
  cin >> n >> s1 >> s2;
  ll ans;
  int start;
  if(s1[0]==s2[0]){
    ans = 3;
    start = 1;
  }else{
    ans = 6;
    start = 2;
  }
  reps(i,start,n){
    if(s1[i]==s2[i]){
      if(s1[i-1]==s2[i-1]){
        ans = ans*2%MOD;
      }
    }else{
      if(s1[i-1]==s2[i-1]){
        ans = ans*2%MOD;
      }else{
        ans = ans*3%MOD;
      }
      i++;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
import math

diff = int(raw_input())

res = 0

for digits in range(1, 20):
	A = [0 for i in range(digits)]

	r = digits - 1
	l = 0

	ite = 0

	while l < r:
		A[ite] = 10**r - 10**l
		ite += 1
		l += 1
		r -= 1

	bonus = 1
	if l == r:
		bonus = 10

	ways_in_pos_0 = [0 for i in range(20)]
	ways_in_other = [0 for i in range(20)]

	for i in range(0, 10):
		for j in range(0, 10):
			ways_in_other[i - j] += 1
	for i in range(1, 10):
		for j in range(1, 10):
			ways_in_pos_0[i - j] += 1

	def recurse(pos, made):
		if pos == ite:
			if made == diff:
				return 1
			else:
				return 0
		elif pos == 0:
			curr = 0

			for i in range(-9, 9 + 1):
				if abs(made + i * A[pos] - diff) <= A[pos]:
					curr += recurse(pos + 1, made + i * A[pos]) * ways_in_pos_0[i]

			return curr
		else:
			curr = 0

			for i in range(-9, 9 + 1):
				if abs(made + i * A[pos] - diff) <= A[pos]:
					curr += recurse(pos + 1, made + i * A[pos]) * ways_in_other[i]

			return curr

	res += bonus * recurse(0, 0)

print res

Measured memory_bytes: 2696
#include<stdio.h>
int main()
{
  int k,a,b,i,t=0;
  scanf("%d",&k);
  scanf("%d%d",&a,&b);
  for(i=a;i<=b;i++)
  {
    if(i%k==0)
    {
      t=1;
      printf("OK");
      break;
    }
  }
  if(i-b==1 && t!=1)
  {
    printf("NG");
  }
}

Measured memory_bytes: 1712
use std::io;
use std::collections::VecDeque;

fn main() {
    let mut input = Input::new();
    let n = input.next().parse::<usize>().unwrap();
    let mut a: Vec<i32> = (0..n)
        .map(|_| input.next().parse::<i32>().unwrap())
        .collect();
    let max = *a.iter().max().unwrap();
    a.sort();
    let ok = (max..max + 2).any(|i| {
                                    (0..n).all(|j| if i + i - n as i32 > j as i32 {
                                                   i - 1
                                               } else {
                                                   i
                                               } ==
                                                   a[j])
                                });
    println!("{}", if ok { "Yes" } else { "No" });
}

struct Input {
    queue: VecDeque<String>,
}

impl Input {
    fn new() -> Input {
        Input { queue: VecDeque::new() }
    }

    fn next(&mut self) -> String {
        if self.queue.is_empty() {
            let mut line = String::new();
            let _ = io::stdin().read_line(&mut line);
            for x in line.split_whitespace() {
                self.queue.push_back(String::from(x));
            }
        }
        return self.queue.pop_front().unwrap();
    }
}



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

int main() {

	int n; cin >> n;
	vector<int> l(n);
	for (int i = 0; i < n; i++) {
		cin >> l[i];
	}
	sort(l.rbegin(), l.rend());
	int sum = 0;
	for (int i = 1; i < n; i++) {
		sum += l[i];
	}
	if (l[0] < sum) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

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

int main(){

    string s,t;
    cin >> s >> t;
    int flag = 0;
    for(int i=0; i<s.length(); i++){
        
        if(s == t){
            flag = 1;
            break;
        }
        char first = s[0];
        s.erase(s.begin());
        s.push_back(first);
    }

    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
S = input()
N = len(S) - 1

ans = 0
for bit in range(1 << N):
    s = []
    prev = 0
    for i in range(N):
        if bit & (1 << i):
            s.append(S[prev:i + 1])
            s.append('+')
            prev = i + 1
    else:
        s.append(S[prev:])
    ans += eval(''.join(s))
print(ans)


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)


ll len(pair<ll,ll> x){
  return max(0LL,x.second-x.first+1LL);
}

int main(){ 

  ll N,ans=0;
  cin >> N;
  vector< pair<ll,ll> > LR(N);
  FOR(i,0,N)cin>>LR[i].first>>LR[i].second;

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

  vector< pair<ll,ll> > fun_big(N),fun_end(N);

  ll l = 0,r=1e9+7;
  FOR(i,0,N){
    ans = max(ans,LR[i].second-LR[i].first+1);
    l = max(l,LR[i].first);
    r = min(r,LR[i].second);
    fun_big[i] = {l,r};
  }

  l = 0,r=1e9+7;
  FORR(i,0,N){
    l = max(l,LR[i].first);
    r = min(r,LR[i].second);
    fun_end[i] = {l,r};
  }

  FOR(i,0,N-1){
    ans = max( ans , len(fun_big[i]) + len(fun_end[i+1]));
    if(i){
      ll l = max(fun_big[i-1].first,fun_end[i+1].first);
      ll r = min(fun_big[i-1].second,fun_end[i+1].second);
      ans = max( ans , len(LR[i]) + len({l,r}) );
    }
  }
  cout << ans << endl;

}


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

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  string s, t;
  cin >> n >> s >> t;
  int overlap = 0;
  for (int i = 0; i < s.size(); i++) {
    bool ok = true;
    if (s.size() - i > t.size()) {
      continue;
    }
    for (int j = 0; j < s.size() - i; j++) {
      if (s.at(i + j) != t.at(j)) {
        ok = false;
        break;
      }
    }
    if (ok) {
      overlap = s.size() - i;
      break;
    }
  }
  int res = max({n, (int) s.size() + (int) t.size() - overlap});
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 3640
import scala.io.StdIn
import scala.annotation.tailrec
import scala.collection.mutable.HashMap

object Main extends App {
  val Array(h, w) = StdIn.readLine.split(" ").map(_.toInt)
  val table = Iterator.continually(StdIn.readLine)
    .take(h)
    .map(_.map(_ == '#').toArray)
    .toArray

  @inline
  def flat(i:Int, j:Int):Int = i * w + j

  val ans = {
    val dx = Array(0, 1, 0, -1)
    val dy = Array(-1, 0, 1, 0)
    val uft = new UnionFindTree(flat(h, w))

    for{
      i <- 0 until h
      j <- 0 until w
    } {
        val ij = flat(i,j)
        (0 until 4).foreach(k => {
          val y = i + dy(k)
          val x = j + dx(k)
          if(0 <= y && y < h && 0 <= x && x < w &&
              table(i)(j) != table(y)(x) ) {
            uft.unite(ij, flat(y,x))
          }
        })
    }
    uft.map.values./:(0L)((acc,e) => {
      acc + e._1.toLong * e._2.toLong
    })
  }

  println(ans)

  class UnionFindTree(n:Int) {
    private val par  = Array.range(0, n)
    private val rank = Array.fill(n)(1)
    val map = HashMap.empty[Int,(Int,Int)]

    def root(x:Int):Int = {
      if(par(x) == x) x
      else { par(x) = root(par(x)); par(x) }
    }

    def isSame(x:Int, y:Int):Boolean = root(x) == root(y)

    def unite(x:Int, y:Int):Unit = {
      val rx = root(x)
      val ry = root(y)
      if(rx == ry) return

      if(!map.contains(ry)) map(ry) = if(table(ry/w)(ry%w)) (1,0) else (0,1)
      if(!map.contains(rx)) map(rx) = if(table(rx/w)(rx%w)) (1,0) else (0,1)
      val ryv = map(ry)
      val rxv = map(rx)

      if(rank(rx) < rank(ry)) {
        par(rx) = ry
        map(ry) = (ryv._1 + rxv._1, ryv._2 + rxv._2)
        map -= rx
      }
      else {
        par(ry) = rx
        if(rank(rx) == rank(ry)) rank(rx) += 1
        map(rx) = (rxv._1 + ryv._1, rxv._2 + ryv._2)
        map -= ry
      }
    }
  }
}

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

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

	for (int i = 0; i < str.length(); i++) {
		int t = str[i] - 'A';
		t += n;
		t %= 26;
		str[i] = 'A' + t;
	}
	cout << str << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
typedef long long lint;
const int N=200;

inline lint nxi(){
	lint x=0;
	char c;
	while((c=getchar())>'9'||c<'0');
	while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
	return x;
}

inline int hbit(lint x){
	int ans=0;
	for(int i=32; i; i>>=1){
		if(x>>i) x>>=i,ans+=i;
	}
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	//freopen("c.in","r",stdin);
#endif
	static int que[N];
	const lint q=nxi()+1;
	int hd=100,tl=99,pos=0;
	for(int i=hbit(q)-1; ~i; --i){
		que[++tl]=++pos;
		if(q>>i&1) que[--hd]=++pos;
	}
	printf("%d\n",pos<<1);
	for(int i=hd; i<=tl; ++i){
		printf("%d ",que[i]);
	}
	for(int i=1; i<=pos; ++i){
		printf("%d ",i);
	}
	puts("");
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <vector>
#include <map>
#include <set>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;


int
main()
{
    ll N, A;
    cin >> N >> A;
    vi x;
    for (int i = 0; i < N; ++i) {
        int tmp;
        cin >> tmp;
        x.push_back(tmp);
    }

    vector<vector<vector<ll> > > dp(N+1);
    for (int i = 0; i <= N; ++i) {
        dp[i].resize(N+1);
        for (int j = 0; j <= N; ++j) {
            dp[i][j].resize(50*N+1);
            for (int k = 0; k <= 50*N; ++k) {
                dp[i][j][k] = (j == 0 && k == 0)? 1LL : 0LL;
            }
        }
    }

    for (int i = 1; i <= N; ++i) {
        int next = x[i-1];
        for (int j = 1; j <= N; ++j) {
            for (int k = 0; k <= 50*N; ++k) {
                if (j <= i) {
                    if (k-next < 0) {                        
                        dp[i][j][k] = dp[i-1][j][k];
                    } else {
                        dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][k-next];
                    }
                } else
                    dp[i][j][k] = 0LL;
            }
        }
    }

    ll total = 0LL;
    for (int j = 1; j <= N; ++j) {
        total += dp[N][j][A * j];
    }
    cout << total << endl;
    return 0;
}


Measured memory_bytes: 51200
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define mod 1000000007LL
#define all(x) x.begin() , x.end()
#define ld long double
void solve();
int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int t = 1; //cin >> t;
	while (t--)
		solve();
	return 0;
}
void solve()
{
	string s; cin >> s;
	ll n = s.size();
	ll sum = 0;
	for (auto c : s)
	{
		sum += (c - '0');
	}
	if (sum % 9 == 0) cout << "Yes";
	else cout << "No";
	cout << "\n";
	return ;
}


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

k_list = []
s_lists = []

for i in range(M):
    tmp = list(map(int, input().split()))
    k_list.append(tmp[0])
    s_lists.append(tmp[1:])

p_list = list(map(int, input().split()))

count = 0

for i in range(1 << N):
    sw_state = [0] * N
    for j in range(N):
        if ((i >> j) & 1) == 1:
            sw_state[j] = 1
    
    # print(sw_state)
    
    flag = 0
    
    for num, s_list in enumerate(s_lists):
        tmp = sum([sw_state[s-1] for s in s_list])
        # print("sum {}".format(tmp))
        if tmp % 2 != p_list[num]:
            flag = 1
            break
    
    if flag != 1:
        # print("count")
        count += 1

print(count)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i,m,n) for(int (i)=(int)(m);(i)<(int)(n);++(i))
#define rREP(i,m,n) for(int (i)=(n)-1;(i)>=(m);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;

template<class T>inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T>inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}
inline void init() {
	cin.tie(nullptr); cout.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

int main() {
	init();

	int H, W; cin >> H >> W;
	vector<string>maze(H);
	rep(i, 0, H)cin >> maze[i];
	string a = string(W + 2, '#');
	cout << a << "\n";
	rep(i, 0, H) {
		cout << '#';
		cout << maze[i];
		cout << '#' << "\n";
	}
	cout << a << "\n";

	return 0;
}

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

int main() {
  int A, B, C, D;
  scanf("%d %d %d %d", &A, &B, &C, &D);

  if (A > C) {
    std::swap(A, C);
    std::swap(B, D);
  }

  if (B > D) {
    printf("%d\n", D-C);
  } else {
    printf("%d\n", std::max(B-C, 0));
  }
  return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
#define all(x) (x).begin(),(x).end()

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

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);std::cout<<std::fixed<<std::setprecision(10);
    int N,M;
    cin >> N >> M;
    vector<pair<int,int>> city(M);
    vector<vector<int>> Pre(N,vector<int>(0));
    REP(i,M){
        cin >> city[i].first;
        cin >> city[i].second;
        city[i].first--;
        Pre[city[i].first].push_back(city[i].second);
    }
    REP(i,N)sort(all(Pre[i]));
    REP(i,M)city[i].second = (lower_bound(all(Pre[city[i].first]),city[i].second) - Pre[city[i].first].begin()) + 1;
    vector<string> zero(6);
    zero[0] = "";
    REP(i,5)zero[i+1] = zero[i] + "0";
    REP(i,M){
        cout << zero[6 - to_string(city[i].first+1).size()];
        cout << city[i].first + 1;
        cout << zero[6 - to_string(city[i].second).size()];
        cout << city[i].second<< "\n";
    }
}

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

int main(){
	int h,w;
	cin >> h >> w;
	for(int i = 0;i<h;i++){
		vector<char> vec(w);
		for (int j = 0;j<w;j++){
			cin>>vec.at(j);
		}
		for (int k = 0;k<2;k++){
			for(int j=0;j<w;j++){
				cout << vec.at(j);
			}
			cout << endl;
		}
	}
}

Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<fstream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define Set(a) memset(a,0,sizeof(a))
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define openf(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define re register
#define ri re int
#define il inline
typedef long long ll;
typedef unsigned long long ull;
template<typename T> inline T rd(T& x)
{
	T f=1;x=0;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(T)(c-'0');
	x*=f;
	return x;
}
ll rd(){ll x;rd(x);return x;}
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
const int inf=1<<30;

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

Measured memory_bytes: 3632
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;n>i;++i)
#define MOD 1000000007
using namespace std;
using ll=int64_t;
using pii=pair<int,int>;
using vi=vector<int>;
using VI=vector<ll>;
using vis=vector<string>;
using vvi=vector<vi>;
//最大公約数
int gcd(int a,int b){
    if(a%b==0)
    return b;
    return gcd(b,a%b);
}
//最小公倍数
int lcm(int a,int b){
   return a*b/gcd(a,b);
}
bool prime(int num){
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false;
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2){
        if (num % i == 0)
        return false;
    }
    return true;
}
int main(){
    ll a,v,b,w,t;
    cin>>a>>v>>b>>w>>t;
    ll diff=abs(a-b);
    if(a<b){
        if(v*t>=w*t+diff){
            cout<<"YES"<<endl;
            return 0;
        }
        else{
            cout<<"NO"<<endl;
            return 0;
        }
    }
    if(a>b){
        if(v*t>=w*t+diff){
            cout<<"YES"<<endl;
            return 0;
        }
        else{
            cout<<"NO"<<endl;
            return 0;
        }
    }
}

Measured memory_bytes: 3628
import Data.List

solve::[Maybe String]->String
solve [] = "UNRESTORABLE"
solve (x:xs) | x==Nothing = solve xs
             | otherwise = case x of Just s-> s
             

createPattern::String->String->[Maybe String]
createPattern s t = sort $ getPattern s t n
              where n = length s - length t + 1

getPattern::String->String->Int->[Maybe String]
getPattern s t 0 = (createStr s t 0):[]
getPattern s t n = (createStr s t n):(getPattern s t (n-1))

createStr::String->String->Int->Maybe String
createStr s t n = if ok submid t
                  then Just (replace (fastInsert s t n))
                  else Nothing
                  where len = length t 
                        submid = take len (drop n s)

fastInsert::String->String->Int->String
fastInsert [] _ _ = []
fastInsert (x:xs) [] 0 = x:fastInsert xs [] (0-1)
fastInsert (x:xs) (y:ys) 0 = y:fastInsert xs ys 0
fastInsert (x:xs) t n = x:fastInsert xs t (n-1)

replace::String->String
replace [] = []
replace (x:xs) = if x=='?'
                 then 'a':(replace xs)
                 else  x:(replace xs)

ok::String->String->Bool
ok [] [] = True
ok [] _ =  False
ok (x:xs) (y:ys) =  if (x=='?') || (x==y) 
                    then ok xs ys
                    else False

main = do
    s <- getLine
    t <- getLine
    putStrLn $ solve $ createPattern s t

Measured memory_bytes: 3820
#include<stdio.h>
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
int main()
{
  int c[26],D=IN(),i=0,j=0,M=0,s[365][26]={{0}},t[365],temp=0,update[26]={0};long sum=0;
  while(i<26){c[i++]=IN();}i=0;
  while(i<D){while(j<26){s[i][j++]=IN();}i++,j=0;}i=0;
  while(i<D)
  {
    j=0;temp=IN()-1;t[i]=temp;sum+=1l*s[i][temp];update[temp]=++i;
    while(j<26){sum-=c[j]*(i-update[j]);j++;}
  }M=IN();
  while(M--)
  {
    int d=IN()-1,q=IN()-1,l0=-1,l1=-1,r0=D,r1=D,esc0=0,esc1=0;
    sum+=1l*s[d][q]-s[d][t[d]];i=d-1;
    while(i>=0)
    {
      if(t[i]==t[d]&&!esc0){l0=i;esc0=1;}
      if(t[i]==q&&!esc1){l1=i;esc1=1;}
      if(esc0&esc1){break;}i--;
    }
    esc0=esc1=0;i=d+1;
    while(i<D)
    {
      if(t[i]==t[d]&&!esc0){r0=i;esc0=1;}
      if(t[i]==q&&!esc1){r1=i;esc1=1;}
      if(esc0&esc1){break;}i++;
    }
    sum-=1l*c[t[d]]*(d-l0)*(r0-d);sum+=1l*c[q]*(d-l1)*(r1-d);t[d]=q;
    printf("%ld\n",sum);
  }
}

Measured memory_bytes: 1640
#include<bits/stdc++.h>
using namespace std;
int main(){
    int N,D;
    cin >> N >> D;
    int X[N][D];
    for(int i = 0; i < N; i++){
        for(int j = 0; j < D; j++){
            cin >> X[i][j];
        }
    }
    int count = 0;
    for(int i = 0; i < N-1; i++){
        for(int k = i+1;  k < N; k++){
            double dis = 0;
            for(int j = 0; j < D; j++){
                dis += (X[i][j] - X[k][j]) * (X[i][j] - X[k][j]);
            }
            dis = sqrt(dis);
            if(dis == floor(dis)) count++;
        }
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <map>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20);
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9+7LL;
const int INF = 2e9;
string S;
map<char, int> M;

int main() {
  INCANT;
  cin >> S;
  rep(i, S.size()) {
    M[S[i]]++;
  }
  rep(i, 26) {
    char t = 'a'+i;
    if (!M[t]) {
      cout << t << endl;
      return 0;
    }
  }
  cout << "None" << endl;
      
  return 0;
}

Measured memory_bytes: 512
local n = io.read("*n")
if(n == 3) then io.write("2\n1 3\n2 3\n")
else
  local n2 = (n % 2 == 1 and n - 1 or n)
  local lines = n2 * (n2 - 2) // 2
  local totlines = lines
  if(n % 2 == 1) then totlines = totlines + n - 1 end
  print(totlines)
  for i = 1, n2 - 1 do
    for j = i + 1, n2 do
      if(i + j ~= n2 + 1) then
        io.write(i .. " " .. j .. "\n")
      end
    end
  end
  if(n % 2 == 1) then
    for i = 1, n2 do
      io.write(i .. " " ..n .. "\n")
    end
  end
end


Measured memory_bytes: 888
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
int n,x[N],m;
int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)cin>>x[i];
	sort(x,x+n);
	int ans=0;
	for(int i=0;i<m;i++)ans+=x[i];
	cout<<ans<<"\n";
	return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }

int main(){
    ll N;
    cin >> N;
    vector<ll> a(N), S(N+1,0);
    rep(i,N)cin>>a[i];
    rep(i,N){
        S[i+1] = S[i] + a[i];
    }
    ll m=1e18;
    rep(i,N){
        if(i == 0)continue;
        cmin(m, abs(S[i] - (S[N] - S[i])));
    }
    
    cout << m << endl;
}


Measured memory_bytes: 3328
module Main where

import Control.Applicative
import Numeric
import Data.List

type House = [Floor]
type Floor = [Room]
type Room = Int

officialHouseInit :: [House]
officialHouseInit = replicate 4 houseInit

houseInit :: House
houseInit = replicate 3 floorInit

floorInit :: Floor
floorInit = replicate 10 roomInit

roomInit :: Room
roomInit = 0

showOfficialHouse :: [House] -> String
showOfficialHouse = intercalate (replicate 20 '#' ++ "\n") . map showHouse 

showHouse :: House -> String
showHouse = unlines . map showFloor

showFloor :: Floor -> String
showFloor = concatMap ((' ':) . showRoom )

showRoom :: Room -> String
showRoom = show

main :: IO ()
main = do
  { [n] <- getInts
  ; hs <- loop n officialHouseInit
  ; putStr $ showOfficialHouse hs
  }

loop :: Int -> [House] -> IO [House]
loop 0 hs = return hs
loop n hs = do
  { [b, f, r, v] <- getInts
  ; let (fronts, ho:ends) = splitAt (b - 1) hs
  ; let (lows,   fl:ups)  = splitAt (f - 1) ho
  ; let (xs, rm : ys)      = splitAt (r - 1) fl
  ; let fl' = xs ++ (rm + v ) : ys
  ; let ho' = lows ++ fl' : ups
  ; let hs' = fronts ++ ho' : ends
  ; loop (n-1) hs'
  }
  
--

getInts :: IO[Int]
getInts = map read . words <$> getLine

putInts :: [Int] -> IO ()
putInts = putStrLn . unwords . map show

getDoubles :: IO [Double]
getDoubles = map read . words <$> getLine

putDoubles :: [Double] -> IO ()
putDoubles = putStrLn . unwords . map showDouble

showDouble :: Double -> String
showDouble d = showFFloat Nothing d ""

done :: IO()
done = return ()



Measured memory_bytes: 4648
from collections import defaultdict

n, k = map(int,input().split())
a = list(map(int,input().split()))

route = defaultdict(int)
i = 0
loop = []
tail = []

while route[i] < 2:

    if route[i] == 0:
        tail.append(i)
    else:
        loop.append(i)
    
    route[i] += 1
    i = a[i] - 1

if len(tail) > k:
    print(tail[k] + 1)
else:
    temp = len(tail) - len(loop)
    print(loop[(k-temp) % len(loop)] + 1)

Measured memory_bytes: 42744
#include <bits/stdc++.h>
#define repr(i,from,to) for(int (i)=(from);(i)<(to);(i)++)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
#define all(x) begin((x)), end((x))
#define vprint(x) for(auto a:(x)) cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) { a = b; return true;}
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) { a = b; return true;}
    return false;
}


int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin>>n;
    vector<pair<ll,ll>> a;
    rep(i,n) {
        ll tmp;cin>>tmp;
        a.emplace_back(tmp, i);
    }
    if (n==1) {
        cout << 0 << endl;
        return 0;
    }
    if (n==2) {
        cout << a[0].first + a[1].first << endl;
        return 0;
    }
    vector<bool> chk(n, true);
    vector<vector<ll>> dp(n+1,vector<ll>(n+1,0));
    sort(all(a),greater<pair<ll,ll>>());
    rep(i,n) {
        rep(j,i+1) {
            ll place = a[i].second, value = a[i].first;
            chmax(dp[i+1][j+1], dp[i][j] + value * abs(place-j));
            chmax(dp[i+1][j], dp[i][j] + value * abs(n-1-i+j-place));
        }
    }
    ll res = 0;
    rep(i,n) {
        chmax(res, dp[n][i]);
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 34616
from itertools import accumulate

N, M = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
sAB = sorted(AB, key=lambda x: x[0])

sB = [sAB[i][1] for i in range(N)]
cum_num = list(accumulate(sB))

num = 0
ans = 0
index_ = -1
for a, b in sAB:
    index_ += 1
    num += b
    if num >= M:
        break
    ans += a*b

if index_ == 0:
    ans += sAB[0][0]*M
else:
    ans += sAB[index_][0]*(M-cum_num[index_-1])

print(ans)


Measured memory_bytes: 29560
import sys
sys.setrecursionlimit(10000)
def resolve():
    N = int(input())
    S = input()
    mem = [None for i in range(N)]
    mem[0] = S[1:].count("E")
    for i in range(1, N):
        if S[i-1] == "W" and S[i] == "W":
            mem[i] = mem[i-1] + 1
        elif S[i-1] == "E" and S[i] == "E":
            mem[i] = mem[i-1] - 1
        else:
            mem[i] = mem[i-1]
    #print(mem)
    print(min(mem))

if '__main__' == __name__:
    resolve()

Measured memory_bytes: 63460
#if !defined(__clang__) && defined(__GNUC__)
#include <bits/stdc++.h>
#else
#include <cstdlib>
#include <climits>
#include <iostream>
#include <cstdint>
#include <vector>
#include <string>
#include <complex>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <regex>
#endif //  !defined(__clang__) && defined(__GNUG__)
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/math/common_factor_rt.hpp>

using namespace std;

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

    std::vector<int64_t> A;
    std::vector<int64_t> B;

    for (int64_t i = 1; i <= N; ++i) {
        A.push_back(i * 20000);
        B.push_back((N - i + 1) * 20000);
    }

    /*
        20000, 40000, 60000, 80000, .., 20000N
        20000N, ...                , .., 20000
        ---------------------------------------
    */

    for (decltype(N) i = 1; i <= N; ++i) {
        int64_t t;
        std::cin >> t;
        t--;
        A[t] += i;
    }

    for (auto a: A) {
        std::cout << a << " ";
    }
    std::cout << std::endl;

    for (auto b: B) {
        std::cout << b << " ";
    }
    std::cout << std::endl;

    return 0;
}


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

class UnionFind {
public:
    vector <ll> par; // 各元の親を表す配列
    vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)

    // Constructor
    UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);  // resize だとなぜか初期化されなかった
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }

    // Member Function
    // Find
    ll root(ll x) { // 根の検索
        while (par[x] != x) {
            x = par[x] = par[par[x]]; // x の親の親を x の親とする
        }
        return x;
    }

    // Union(Unite, Merge)
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        // merge technique（データ構造をマージするテク．小を大にくっつける）
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) { // 連結判定
        return root(x) == root(y);
    }

    ll size(ll x) { // 素集合のサイズ
        return siz[root(x)];
    }
};

int main() {
  int n,m;
  cin >> n >> m;
  UnionFind tree(n);
  rep(i,0,m){
    int a,b;
    cin >> a >> b;
    a--;b--;
    tree.merge(a,b);
  }
  ll ans = 0;
  rep(i,0,n)  ans = max(ans,tree.size(i));
  cout << ans << endl;
}

Measured memory_bytes: 6372
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n = INT()
    if n % 2 != 0:
        print('No')
        sys.exit()
    s = input()
    m = int(n/2)
    s1 = [s[i] for i in range(m)]
    s2 = [s[i] for i in range(m, n)]
    if s1 == s2:
        print('Yes')
    else:
        print('No')


if  __name__=='__main__':
    main()


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

int main() {
    int N, L;
    cin>>N>>L;
    vector<string> S(N);
    for(int i=0; i<N; i++) cin>>S[i];
    sort(S.begin(), S.end());
    for(int i=0; i<N; i++) cout<<S[i];
    cout<<endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=256;
int main()
{
    ll a,b,c;
    scanf("%lld%lld%lld",&a,&b,&c);
    a=abs(a);
    if(c>a)
    {
        ll d=abs(a-c);
        ll f=abs(c-d);
        if(b%2==1)printf("%lld\n",d);
        else printf("%lld\n",f);
    }
    else
    {
        ll d=a/c;
        if(d>b)
        {
            printf("%lld\n",a-b*c);
            return 0;
        }
        ll e=a%c;
        ll f=abs(e-c);
        if(d%2==b%2)printf("%lld\n",e);
        else printf("%lld\n",f);
    }
    return 0;
}


Measured memory_bytes: 3812
// Problem : C - Connect Cities
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
#define int long long
#define INF 1000000000000000000
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int,int>
#define m_p(x,y) make_pair(x,y)
#define vp vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v,n,x) v.assign(n,x)
#define incd(v,n) v.resize(n)
#define iniz(n) memset(n,0,sizeof(n))
#define inin(n) memset(n,-1,sizeof(n))
#define inimi(n) memset(n,0xc0,sizeof(n))
#define inima(n) memset(n,0x3f,sizeof(n))
#define all(v) (v).begin(),(v).end()

using namespace std;

template<typename T1,typename T2>istream &operator>>(istream &is, vector<pair<T1,T2>> &v) { for (pair<T1,T2> &t : v) is >> t.f>>t.se; return is; }
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }

const int N=200001;
double pi=acos(-1.0);
int md=1e9+7;
int dx1[]={0,0,-1,1};
int dy1[]={1,-1,0,0};
//const int N=2e5;

template<class T>
T abst(T a)
{return a<0?-a:a;}

void dfs(int x,int vis[],vi v[])
{
	vis[x]=1;
	for(int i:v[x])
	{
		if(!vis[i])
		{
			dfs(i,vis,v);
		}
	}
}
void solve()
{
	int n,m;cin>>n>>m;
	int vis[n+1]={0};
	vi v[n+1];
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		v[x].pb(y);
		v[y].pb(x);
	}
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			cnt++;
			dfs(i,vis,v);
		}
	}
	cout<<cnt-1;
}

int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
    solve();
}
return 0;
}


Measured memory_bytes: 11488
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main(){
  ll x,y,z,k;
  cin>>x>>y>>z>>k;
  
  vector<ll> A(x),B(y),C(z);
  rep(i,x) cin>>A[i];
  rep(i,y) cin>>B[i];
  rep(i,z) cin>>C[i];
  
  vector<ll> D,E;
  rep(i,x){
    rep(j,y) D.push_back(A[i]+B[j]);
  }
  
  sort(ALL(D),greater<ll>());
  sort(ALL(C),greater<ll>());

  rep(i,min(k,x*y)){
    rep(j,z) E.push_back(D[i]+C[j]);
  }
  
  sort(ALL(E),greater<ll>());
  
  rep(i,k) cout<<E[i]<<endl;
    
  return 0;
}

Measured memory_bytes: 44092
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int main(){
	int n;
	cin >> n;
	string s, t;
	cin >> s >> t;
	rep(i, n) {
		string a = s.substr(i);
		string b = t.substr(0, n-i);
		if(a == b) {
			cout << n*2-(n-i) << endl;
			return 0;
		}
	}
	cout << n * 2 << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first 
#define ss second  
typedef pair<int,int> pp;
bool com(pp x,pp y){
    return x.ff<y.ff;
} 
int N=1e5+9;
int main() {
	// your code goes here
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0); 
	string s;
	cin>>s;
	int ans=0; 
	for(int i=0;i<3;i++){
	    int cnt=0;
	    while(s[i]=='R'&&i<3) i++,cnt++;
	    ans=max(cnt,ans);
	    //i-=1;
	}
	cout<<ans<<"\n";
	return 0; 
	
}


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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M,d=MI()
    temp=0
    if d==0 or 2*d>=N:
        temp=N
    else:
        temp=N+(N-2*d)
        
    ans=(temp/(N*N))*(M-1)
    print(ans)
    

main()


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

vector<int> T;


void maxHeapify(vector<int> &T, int i){
	// n == H
	int n = T.size() - 1;
	if(n == 0 ||n == 1) return;
	
	// 	index of left son and right son;
	int l, r, largest;
	l = 2 * i;
	r = 2 * i + 1;
	
	if(l <= n && T[i] < T[l]) largest = l;
	else largest = i;
	if(r <= n && T[largest] < T[r]) largest = r;
	
	if(largest != i){
		swap(T[largest], T[i]);
		maxHeapify(T, largest);
	}
	
}

// extract
int extract(){
	if(T.size() <= 1) return INT_MIN;
	
	int max = T[1];
	
	T[1] = T.back();
	T.pop_back();
	maxHeapify(T, 1);
	
	return max;
}



// insert
void upTowardRoot(int i){
	while(T[i] > T[i/2] && i/2 >= 1){
		swap(T[i], T[i/2]);
		i = i/2;
	}
}

void insert(int key){
	T.push_back(key);
	int i = T.size() - 1; 
	upTowardRoot(i);
}


int main(int argc, char** argv) {
	T.push_back(INT_MIN); // T[0] == INT_MIN;
	int key;
	string com;
	while(1){
		//getline(cin, com);
		cin >> com;
		//scanf("%s", &com);
		if(com[0] == 'e' && com[1] == 'n') break;
		if(com[0] == 'i'){
			cin >> key;
			insert(key);
		}
		else{
			cout << extract() << endl;
		}
	}
	
	return 0;
}


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

#define fastio 			ios::sync_with_stdio(0);	cin.tie(0); cout.tie(0);  cout<<fixed;  cout<<setprecision(12);
#define randomINT 		mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define	newl 			cout<<"\n"
#define DISP(as)		for(auto it : as) cout<<it<<" ";newl;
#define all(x)         	(x).begin(),(x).end()
#define mset(x,val)    	memset(x,val,sizeof(x))
#define newl           	cout<<"\n"
#define pb             	push_back
#define mp             	make_pair
#define f 				first
#define s 				second
#define dline          cerr<<"///REACHED///\n";
#define deb1(x)        cerr<<#x<<" = "<<x<<'\n';
#define deb2(x,y)      cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define deb3(x,y,z)    cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';

typedef long long 				ll;
typedef long double 			ld;
typedef vector<ll> 				vll;
typedef pair<ll , ll> 			pll;
typedef pair<ld, ld> 			pld;
typedef unordered_map<ll, ll> 	um;
typedef vector<pll> 			vpll;

const ll 	MAX5 	= 	1e+5 + 7;
const ll 	MAX7 	= 	1e+7 + 7;
const ll 	MAXN 	= 	MAX7;
const ll   	INF   	=  	0x7f7f7f7f7f7f7f7f;
const int  	INFi 	=  	0x7f7f7f7f;
const ll 	MOD		=  	1e+9 + 7;

vll adj[MAXN]; ll visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

// <------------- Declare Variables Here ------------> //

ll t = 1;
ll n, x;
string s;

// <------------- Implement Functions Here ----------> //



// <------------- Start of main() -------------------> //

void MAIN() {
	cin >> n;
	cin >> s;
	set<ld> R, G, B;
	for (ll i = 0; i < n; i++) {
		if (s[i] == 'R') R.insert(i);
		else if (s[i] == 'B') B.insert(i);
		else G.insert(i);
	}
	ll ans = 0;
	ans = 0;
/*	cout << ans; newl;
	set<ll> GB[4000], BG[4000];
	if (ans) {
		for (ll i = 0; i < G.size(); i++) {
			for (ll j = 0; j < B.size(); j++) {
				GB[i].insert(G[i] - B[j]);
				BG[j].insert(B[j] - G[i]);
			}
		}


		for (ll i = 0; i < G.size(); i++) {
			cout << "Gpos" << i + 1 << ": "; DISP(GB[i]);
		}

		for (ll i = 0; i < B.size(); i++) {
			cout << "Bpos" << i + 1 << ": "; DISP(BG[i]);
		}
		newl;

		for (ll i = 0; i < R.size(); i++) {
			for (ll j = 0; j < G.size(); j++) {
				if (GB[j].find(R[i] - G[j]) != GB[j].end() || GB[j].find(2 * (R[i] - G[j])) != GB[j].end()) {
					cout << R[i] - G[j] << " ";
					ans--;
				}
			}
			newl;
		}

		for (ll i = 0; i < R.size(); i++) {
			for (ll j = 0; j < B.size(); j++) {
				if (BG[j].find(R[i] - B[j]) != BG[j].end() || BG[j].find(2 * (R[i] - B[j])) != BG[j].end()) {
					cout << R[i] - B[j] << " ";
					ans--;
				}
			}
			newl;
		}

	}*/
	ll val =0;
	for(auto i : R ){
		for(auto j : G) {
			val = 0;		
			ld I = i;
			ld J= j;
			
			if(B.find(J+J-I)!=B.end())val--;
			if(B.find((I+J)/2)!=B.end()) val--;
			if(B.find(I+I-J)!=B.end()) val--;
			//cout<<ans;newl;	
			ans+=B.size()+val;
		}
	}

	cout << ans; newl;
}

int main() {
	fastio; randomINT;
	//cin>>t;
	while (t--) {
		MAIN();
	}
	return 0;
}


Measured memory_bytes: 238224