akhauriyash/Code-Regression huggingface dataset:
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#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;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define INF 2e9
struct UnionFind {
vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N+1; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
}
};
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int count=0,flag,a,b,c,n,r1,r2,x,m,k;
cin>>n;
vint bukatu(n+1,0);
UnionFind tree(n);
cin>>m>>k;
for(int i=0;i<k;i++){
count++;
cin>>flag;
if(flag==1){
cin>>a>>b;
r1=tree.root(a);
r2=tree.root(b);
if(bukatu[r1]!=0 && bukatu[r2]==0){
bukatu[r2]=bukatu[r1];
}
else if(bukatu[r1]==0 && bukatu[r2]!=0){
bukatu[r1]=bukatu[r2];
}
else if(bukatu[r1]!=bukatu[r2]){
cout<<count<<'\n';
return(0);
}
tree.unite(a,b);
}
if(flag==2){
cin>>c>>x;
r1=tree.root(c);
if(bukatu[r1]==0){
bukatu[r1]=x;
}
else if(bukatu[r1]!=x){
cout<<count<<'\n';
return(0);
}
}
}
cout<<0<<'\n';
return(0);
}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define int long long
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n[]={0,1,2,1,3,1,3,1,1,3,1,3,1};
main(){
	int a,b,c;
	cin>>a>>b;
	cout<<(a-1)*(b-1)<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<math.h>
#include<utility>
#include<queue>
#include<map>
 
using namespace std;

int main(){
    int x[5];
    for(int i=0;i<5;i++){
        cin >> x[i];
        if(x[i] == 0){
            cout << i + 1 << endl;
            return 0;
        }
    }
}

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

  val Array(a, b) = readLine.trim.split(' ').map(_.toInt)
  if (b % a == 0) println(a + b)
  else println(b - a)
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
  }

}

Measured memory_bytes: 27332
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i = (a);i <= (b);i++)
#define per(i,a,b) for(int i = (a);i >= (b);i--)
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const double eps = 1e-8;
const double pi = acos(-1.0);	
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a * b / gcd(a,b);}
ll qw(ll x,ll y){ll res = 1;while(y){if(y & 1) res *= x;x = x * x;y >>= 1;}return res;}
ll poww(ll x,ll y,ll p){ll ans = 1;while(y){if(y & 1) ans = ans * x % p;x = x * x % p;y >>= 1;}return ans % p;}
bool isp(ll x){if(x < 2) return 0; for(ll i = 2;i * i <= x;i++){if(x % i == 0) return 0;}return 1;}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	char c;
	cin >> c;
	if(c >= 'a' && c <= 'z') cout << 'a';
	else cout << 'A';
	cout << endl;
	return 0;
}
//do smt instead of nothing! never give up!


Measured memory_bytes: 3640
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())

using namespace std;
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const long long INFL = (1LL<<60);
const double eps = (1e-9);

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

  int n;
  cin >> n;
  vector<pair<ll, ll>> city(n);
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    city[i] = make_pair(x, y);
  }

  double sum = 0.0;

  sort(ALL(city));

  ll total = 0;
  do {
    total++;
    for (int i = 0; i < n-1; i++) {
      sum += sqrt(pow((city[i].first - city[i+1].first), 2) + pow((city[i].second - city[i+1].second), 2));
    }
  } while (next_permutation(city.begin(), city.end()));

  printf("%.8f\n", sum / total);

  return 0;
}


Measured memory_bytes: 256
import numpy as np
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    popcnt=lambda bit:bin(bit).count("1")
    h,w,k=MI()
    aa=[[0]+[int(c) for c in SI()] for _ in range(h)]
    aa=np.array(aa,dtype="i8")
    for i in range(h):
        aa[i]=np.cumsum(aa[i])
    #print(aa)
    ans=h+w
    for bit in range(1<<(h-1)):
        s=aa[0].copy()
        sn=popcnt(bit)+1
        ss=np.zeros((sn,w+1),dtype="i8")
        si=0
        for i in range(1,h):
            if bit>>(i-1)&1:
                ss[si]=s
                s=aa[i].copy()
                si+=1
            else:s+=aa[i]
        ss[si]=s
        #print(bin(bit))
        #print(ss)
        #print()
        cnt=popcnt(bit)-1
        j=0
        while j<w:
            cnt+=1
            mn=10**9
            for i in range(sn):
                nj=np.searchsorted(ss[i],ss[i][j]+k,side="right")-1
                mn=min(mn,nj)
            if mn==j:
                cnt=10**9
                break
            j=mn
        ans=min(ans,cnt)
    print(ans)

main()

Measured memory_bytes: 14544
using System;
using System.Linq;
using System.Collections.Generic;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var p = Console.ReadLine().Split().Select(int.Parse).ToArray();

            var suc = Enumerable.Range(1, N).ToArray();
            var wrongCount = 0;

            for (var i = 0; i < N; i++)
            {
                if (p[i] != suc[i]) wrongCount++;
            }

            Console.WriteLine(wrongCount <= 2 ? "YES" : "NO");
        }
    }
}


Measured memory_bytes: 13268
#include<stdio.h>

int n,m,s,i;
int taro[1000];
int hana[1000];
int T,H,X;
int anst,ansh;

int main(){
  while(1){
    scanf("%d %d",&n,&m);
    if(n==0&&m==0)break;

    T=0;
    H=0;
    for(i=0;i<1000;i++){
      taro[i]=0;
      hana[i]=0;
    }
    for(i=0;i<n;i++){
      scanf("%d",&s);
      taro[s]=1;
      T+=s;
    }
    for(i=0;i<m;i++){
      scanf("%d",&s);
      hana[s]=1;
      H+=s;
    }

    
    X=(H-T)/2;
    anst=-1;
    ansh=-1;

    for(i=0;i<=100;i++){
      if(0<=i+X&&taro[i]==1&&hana[i+X]==1){
	anst=i;
	ansh=i+X;
	break;
      }
    }
    
    if((H-T)%2!=0||anst==-1){
      printf("-1\n");
    }else{
      printf("%d %d\n",anst,ansh);
    }

  }
  return 0;
}

Measured memory_bytes: 620
"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(){return +cin[cid++];}
function nextstr(){return cin[cid++];}
function nextbig(){return BigInt(cin[cid++]);}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

var n,a,ans;
var myOut = main();
if(myOut !== undefined)console.log(String(myOut));

function main(){
  n = next();
  a = nextm(n,3);
  ans = xArray(1e15,n+1);
  var b = Array(n);
  for(var i = 0; i < n; i++)b[i] = Math.min(Math.abs(a[i][0]),Math.abs(a[i][1]));
  dfs(0,0,b);
  return ans.join("\n");
}

function dfs(id,cnt,b){
  if(id === n){
    var t = 0;
    for(var i = 0; i < n; i++)t += b[i]*a[i][2];
    if(ans[cnt] > t)ans[cnt] = t;
    return;
  }
  var c = Array(n);
  dfs(id+1,cnt,b);
  cnt++;
  for(var i = 0; i < n; i++){
    c[i] = Math.min(b[i],Math.abs(a[i][0]-a[id][0]));
  }
  dfs(id+1,cnt,c);
  for(var i = 0; i < n; i++){
    c[i] = Math.min(b[i],Math.abs(a[i][1]-a[id][1]));
  }
  dfs(id+1,cnt,c);
}

Measured memory_bytes: 36736
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int mod = 1E9 + 7;
const int inf = 1 << 29;

template <typename TypeInt>
std::string Itoa(const TypeInt v, int base) {
  static const char table[] = "abcdefghijklmnopqrstuvwxyz";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0) n *= -1;
  }

  while (n >= base) {
    ret += table[n % base];
    n /= base;
    n -= 1;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10) ret += '-';
  }
  // 文字列の順番を逆にする
  std::reverse(ret.begin(), ret.end());

  return ret;
}

int main() {
  ll n;
  cin >> n;
  string ans = Itoa(n - 1, 26);
  cout << ans << endl;
}

Measured memory_bytes: 3624
import sys
n = int(input())
a = list(map(int, input().split()))
ans = 1
flag = True
if 0 in a:
    print(0)
    sys.exit()

for i in a:
    ans *= i
    if ans > 1000000000000000000:
        flag = False
        break

if flag == False:
    print(-1)
else:
    print(ans)


Measured memory_bytes: 21700
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

int main() {
  vll v(4);
  for (ll i = 0; i < 4; i++) {
    char c;
    cin >> c;
    v[i] = c - '0';
  }

  for (ll i = 0; i < 2; i++) {
    for (ll j = 0; j < 2; j++) {
      for (ll k = 0; k < 2; k++) {
        ll x = v[0] + v[1] * pow(-1, i) + v[2] * pow(-1, j) + v[3] * pow(-1, k);
        if (x == 7) {
          cout << v[0];
          cout << (i ? '-' : '+');
          cout << v[1];
          cout << (j ? '-' : '+');
          cout << v[2];
          cout << (k ? '-' : '+');
          cout << v[3];
          cout << "=7" << endl;
          return 0;
        }
      }
    }
  }
}


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


int cmp(const void *a,const void *b){
  return *(int*)a-*(int*)b;
}

int main(){
  int N,i,j;
  scanf("%d",&N);
  int A[N];
  int B[N];
  int mid;
  for(i=0;i<N;i++) {
    scanf("%d",&A[i]);
    B[i]=A[i];
  }
  qsort(B,N,sizeof(int),cmp);
  mid=B[N/2];
  for(i=0;i<N;i++){
    if(A[i]<mid){
      printf("%d\n",mid);
    }
    else {
      printf("%d\n",B[(N/2)-1]);
    }
  }

  return 0;
}


Measured memory_bytes: 3692
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std; 
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long int ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
const int MOD=1000000007;

bool isPowerOfTwo (ll x)  
{  
    return x && (!(x&(x-1)));  
}
bool isPrime(int n) 
{ 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
}
int highestPowerof2(unsigned int n) 
{ 
    // Invalid input 
    if (n < 1) 
        return 0; 
  
    int res = 1;
    for (int i=0; i<8*sizeof(unsigned int); i++) 
    { 
        int curr = 1 << i; 
        if (curr > n) 
           break; 
  
        res = curr; 
    } 
  
    return res; 
} 
long long binpow(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}       
////////////////////////////////////////////////////////////////////

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);  
  cout.tie(NULL);
  ll t=1;
  while(t--)
  {
    int a[3];
    REP(i,3)cin>>a[i];
    sort(a,a+3);
    int ans=0;
    if((a[1]-a[0])%2==0)
    {
        ans+=(a[1]-a[0])/2;
    }
    else
    {
        ans+=(a[1]-a[0])/2;
        a[2]++;
        a[0]++;
        ans++;
    }
    ans+=a[2]-a[1];
    cout<<ans;
  }  
  return 0;
}

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

constexpr int MAX_N = 1e5;
int a[MAX_N];
int s[MAX_N+1];

int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;

  for (int i{}; i < N-1; ++i) {
    if (S[i] == 'A' && S[i+1] == 'C')
      a[i] = 1;
  }

  // 累積和
  for (int i{}; i < N; ++i) {
    s[i+1] = s[i] + a[i];
  }

  for (int i{}; i < Q; ++i) {
    int l, r; cin >> l >> r;
    cout << s[r-1] - s[l-1] << endl;
  }

  return 0;
}


Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#define MOD 1000000007
#define mkp make_pair
typedef long long ll;
using namespace std;

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

  cout<<min(A*N,B)<<endl;

  return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
  public static void main(String args[]){
  	Scanner sc = new Scanner(System.in);
    
    int r = sc.nextInt();
    int b = sc.nextInt();
    int g = sc.nextInt();
    
    int num = 100*r + 10*b + g;
    
    if (num%4 == 0){
      System.out.println("YES");
    }
    else {
      System.out.println("NO");
    }
  }
}

Measured memory_bytes: 22868
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int n,a,b,c,d,x,y,sum,z,i;
    while(~scanf("%d %d %d %d",&a,&b,&c,&d))
    {
        sum=0;
        for(i=1;i<=a;i++)
        {
            if(i<=b)
            {
            sum=sum+c;
            }
            else
            {
                sum=sum+d;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b)for(int i=b,_e=a;i>=_e;--i)
#define ll long long
using namespace std;
const int N=25005,K=405,mo=1e9+7;
int n,k,m,mx,mx2,a[N],la[K],f[N][K],g[N][K],_[N];
ll ans;
int ksm(int x,int y){
	int t=1;
	for(;y;y>>=1,x=(ll)x*x%mo)if(y&1)t=(ll)t*x%mo;
	return t;
}
void go(int f[][K]){
	fo(i,1,n){
		fo(j,0,k-2)f[i][j+1]=(ll)f[i-1][j]*(k-j)%mo;
		int su=0;
		fd(j,1,k-1){
			su=(su+f[i-1][j])%mo;
			f[i][j]=(f[i][j]+su)%mo;
		}
	}
}
int main(){
	cin>>n>>k>>m;
	ans=(ll)(n-m+1)*ksm(k,n-m)%mo;
	fo(i,1,m){
		scanf("%d",&a[i]);
		mx=max(mx,la[a[i]]);
		if(mx&&!mx2)mx2=i-1;
		if(i-mx==k){
			cout<<ans;return 0;
		}
		la[a[i]]=i;
	}
	if(mx==0){
		f[0][0]=1;
		fo(i,1,n){
			fo(j,0,k-2)f[i][j+1]=(ll)f[i-1][j]*(k-j)%mo,g[i][j+1]=(ll)g[i-1][j]*(k-j)%mo;
			int su=0,su2=0;
			fd(j,1,k-1){
				su=(su+f[i-1][j])%mo;
				su2=(su2+g[i-1][j])%mo;
				f[i][j]=(f[i][j]+su)%mo;
				g[i][j]=(g[i][j]+su2)%mo;
			}
			fo(j,m,k-1)g[i][j]=(g[i][j]+f[i][j])%mo;
		}
		ll jc=1;
		fo(i,k-m+1,k)jc=jc*i%mo;
		_[0]=1;fo(i,1,n)_[i]=(ll)_[i-1]*k%mo;
		ans=ans*jc%mo;
		fo(i,1,k-1)ans=(ans-g[n][i])%mo;
		cout<<(ans+mo)*ksm(jc,mo-2)%mo;
		return 0;
	}
	f[0][mx2]=1;
	go(f);
	g[0][m-mx]=1;
	go(g);
	fo(i,0,n-m){
		int su=0,su2=0;
		fo(j,0,k)su=(su+f[i][j])%mo,su2=(su2+g[n-m-i][j])%mo;
		ans=(ans-(ll)su*su2)%mo;
	}
	cout<<(ans+mo)%mo;
}

Measured memory_bytes: 79488
#define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	//iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}

#define int ll
void solve() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0) return;
		vector<pair<long double, string>> ans;
		REP(i, n) {
			string a;
			cin >> a;
			vector<int> inputs;
			REP(q, 9) {
				int b;
				cin >> b;
				inputs.push_back(b);
			}
			long double bunsi = inputs[8] * inputs[7] * inputs[6] - inputs[0];
			long double bunbo = inputs[1] + inputs[2] + inputs[3] + inputs[8] * (inputs[4] + inputs[5]);
			ans.push_back(mp(-bunsi / bunbo, a));
		}
		sort(ALL(ans));
		REP(i, ans.size()) {
			cout << ans[i].second << endl;
		}
		cout << "#" << endl;
	}
}
#undef int
int main() {
	init();
	solve();
}


Measured memory_bytes: 3196
N,M=map(int,input().split())
city=[0]*(N+10)

#都市情報を初期化
for i in range(1,N+1):
  city[i]=i
  
#都市番号の小さい方を根としてUnionFind木を作成(大きい方を書き換える)
for i in range(M):
  A,B=map(int,input().split())
#  print(A,B)

#ルートチェック
  if city[A]!=A:
    root=city[A]
    while city[root]!=root:
      root=city[root]
    city[A]=root
  
  if city[B]!=B:
    root=city[B]
    while city[root]!=root:
      root=city[root]
    city[B]=root

#結合
  if city[A]!=city[B]:
    city[city[B]]=city[A]
    
#print(city)
  
#木が何種類あるかチェック
ck=[0]*(N+10)
for i in range(1,N+1):
  if city[i]!=i:
    root=city[i]
    while city[root]!=root:
      root=city[root]
    city[i]=root
    
  if ck[city[i]]==0:
    ck[city[i]]=1
    
#print(city)
#print(ck)
Ans=sum(ck)-1 #木の数-1=必要な道路数

print(Ans)

Measured memory_bytes: 13620
#include <stdio.h>
int main(void)
{
	int a,b,i,temp;
	
	for(i = 0; 1 ; i++){
		scanf("%d %d",&a,&b);
		if(a>b){
			temp=b; b=a; a=temp;
		}
		if(a==0 && b==0)  break;
		printf("%d %d\n",a,b);
	}
	return 0;
}

Measured memory_bytes: 600
#include<iostream>
#include<string>

using namespace std;

int main()
{
  string s, p;

  cin >> s;
  cin >> p;
  for(int i=0; i<101; i++){
    if(s[i]=='\0')break;
    for(int j=0; j<101; j++){
      if(p[j] == '\0'){
        cout << "Yes" << endl;
        return 0;
      }
      if(s[(i+j)%s.size()]!=p[j])break;
    }
  }
  cout << "No" << endl;
  return 0;
}



Measured memory_bytes: 3224
using System;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        int[] input = MyUtil.ReadIntArray();
        if (input[1] % input[0] == 0)
        {
            WriteLine(input[0] + input[1]);
        }
        else
        {
            WriteLine(input[1] - input[0]);
        }
        ReadKey();
    }
}

static class MyUtil
{
    public static int[] ReadIntArray()
    {
        return Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();
    }

    /// <summary>
    /// <see cref="IEnumerable{T}"/>の各要素を並べてコンソールに出力します。各要素の間には半角スペースが一つ入ります。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ts"></param>
    public static void WriteToConsole<T>(this IEnumerable<T> ts)
    {
        StringBuilder stringBuilder = new StringBuilder();
        foreach (T t in ts)
        {
            stringBuilder.Append(t.ToString());
            stringBuilder.Append(" ");
        }
        stringBuilder.Remove(stringBuilder.Length - 1, 1);
        Console.WriteLine(stringBuilder.ToString());
    }
}


Measured memory_bytes: 11348
#include <algorithm> //STLͨ���㷨
#include <cmath> //������ѧ����
#include <cstdio> //��������/�������
#include <iostream> //����������/���
#include <cstring> //�ַ�������
#include <string> //�ַ�����
#include <ctime> //�������ʱ��ĺ���
#define itn int
#define fro for
#define ll long long
#define reg register
#define inf 1234567890
#define rep(i,a,b,c) for (int i=a;i<=b;i+=c)
/*#include <bitset> //STLλ������
#include <cstype> //�ַ�����
#include <cerrno> //���������
#include <complex> //������
#include <clocale> //���屾�ػ�����
#include <cstdlib> //������������ڴ���亯��
#include <deque> //STL˫�˶�������
#include <exception> //�쳣������
#include <fstream> //�ļ�����/���
#include <functional> //STL�������㺯��(���������)
#include <limits> //�����������������ֵ����
#include <list> //STL�����б�����
#include <map> //STLӳ������
#include <iomanip> //����������/���
#include <ios> //��������/���֧��
#include <iosfwd> //����/���ϵͳʹ�õ�ǰ������
#include <istream> //����������
#include <ostream> //���������
#include <queue> //STL��������
#include <set> //STL��������
#include <sstream> //�����ַ�������
#include <stack> //STL��ջ����
#include <stdexcept> //��׼�쳣��
#include <streambuf> //�ײ�����/���֧��
#include <utility> //STLͨ��ģ����
#include <vector> //STL��̬��������
#include <cwchar.h>//���ַ�����������/���
#include <cwctype.h> //���ַ�����*/

using namespace std;

int ans;

int max(int x,int y){return x>y?x:y;}

int min(int x,int y){return x<y?x:y;}

int abs(int x){return x>0?x:-x;}

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

void exgcd(int a,int b,int &d,int &x,int &y){if (!b){d=a,x=1,y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}

int quick_power(ll a,int b){ll r=1;for (;b;b>>=1,a=a*a)if (b&1) r=r*a;return r;}

inline char read() {
	static const int IN_LEN = 1000000;
	static char buf[IN_LEN], *s, *t;
	return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++);
}
template<class T>
inline void read(T &x) {
	static bool iosig;
	static char c;
	for (iosig = false, c = read(); !isdigit(c); c = read()) {
		if (c == '-') iosig = true;
		if (c == -1) return;
	}
	for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
	if (iosig) x = -x;
}

char sr[10000000],z[20];int C=-1,Z=0;
void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(int x){
	if (x<0) sr[++C]='-',x=-x;
	while(z[++Z]=x%10+48,x/=10);
	while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
//int find(int x){return fa[x]=fa[x]==x?x:find(fa[x]);}

//void add(int x,int y,int z){to[++cnt]=y;v[cnt]=z;Next[cnt]=head[x];head[x]=cnt;}
const int N=100100;
int fa[N],off[N];
int find(int x)
{
	if (fa[x]==x) return x;
	int temp=fa[x];
	fa[x]=find(fa[x]);
	off[x]+=off[temp];
	return fa[x];
}

int main()
{
	int n,m;
	read(n);read(m);
	for (int i=1;i<=n;i++) fa[i]=i;
	while (m--)
	{
		int l,r,d;
		read(l);read(r);read(d);
		int fl=find(l),fr=find(r);
		if (fl==fr)
			if (off[r]-off[l]==d) continue;
			else
				return puts("No"),0;
		else
			fa[fr]=fl,off[fr]=d-off[r]+off[l];
	}
	return puts("Yes"),0;
}







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

int main()
{
    int a,b,c;
    cin >> a;
    string day[7]={"thu","fri","sat","sun","mon","tue","wed"};
    cout << day[a%7] << endl;
    return 0;
}



Measured memory_bytes: 3204
from collections import deque
n=int(input())
l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)]

flg=["white"]*(n+1)
dist=[-1]*(n+1)

def BFS(x):

    queue = deque([x])
    dist[x]=0
    flg[x]="gray"

    while queue:
        current=queue.popleft()
        for i in l[current]:
            if flg[i]=="white":
                queue.append(i)
                flg[i]="gray"
                dist[i]=dist[current]+1
        flg[current]="black"

BFS(1)

for num,i in enumerate(dist[1:],1):
    print(num,i)


Measured memory_bytes: 6032
#include <bits/stdc++.h>
#define rep(i,n) for(ll i = 0; i < (n); ++i)
#define srep(i,s,t) for(ll i = s; i < t; ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int main() {
    ll n;
    cin >> n;
    ll f[n+1] = {};

    srep(i,1,101){
        srep(j,1,101){
            srep(k,1,101){
                ll tmp = i*i+j*j+k*k+ i*j+j*k+k*i;
                if(tmp > n)continue;
                f[tmp]++;
            }
        }
    }

    srep(i,1,n+1){
        cout << f[i] << endl;
    }

    return 0;
}




Measured memory_bytes: 3628
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int res;
int N, M;
vector<vector<int> > e;
vector<int> visited;
void dfs(int a ,int depth) {
	if (depth == N - 1) {
		res++;
	}
	visited[a] = 1;
	for (int i = 0; i < e[a].size(); i++) {
		if (visited[e[a][i]] == 0) {
			dfs(e[a][i], depth + 1);
		}
	}
	visited[a] = 0;
}
signed main() {

	cin >> N >> M;
	e.resize(N);

	int A, B;
	for (int i = 0; i < M; i++) {
		cin >> A >> B;
		A--;
		B--;
		e[A].push_back(B);
		e[B].push_back(A);
	}
	int i;
	visited.resize(N, 0);
	dfs(0, 0);
	cout << res << endl;
	return 0;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323846264338327950288
#define mt make_tuple


void love() {
	int n; cin>>n;
	double ans = 0;

	if(n&1) {
		ans += 1.0 * (n+1) / 2 / n;
	}
	else {
		ans += 1.0 * n/2/n;
	}
	cout<<ans<<endl;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	love();
	return 0;
}

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

namespace 競プロ_Csharp {
    class Program {
        static void Main(string[] args) {

            while (true) {

                int[] ar = NextInts();
                if (ar[0] == 0 && ar[1] == 0) return;

                int h = ar[0], w = ar[1];

                //print flame
                for (int i = 0; i < h; i++) {
                    string s = "";
                    for (int j = 0; j < w; j++) {
                        
                        if (i == 0 || j == 0 || i == h - 1 || j == w - 1) {
                            s += "#";
                        }
                        else {
                            s += ".";
                        }

                    }
                    Println(s);
                }


                Println("");

            }

        }

        public static void Println (Object o) {
            Console.WriteLine(o);
        }

        public static string NextString() {
            return Console.ReadLine();
        }

        public static string[] NextStrings () {
            return Console.ReadLine().Split();
        }

        public static int NextInt () {
            return int.Parse(Console.ReadLine());
        }

        public static int[] NextInts () {
            string[] a = Console.ReadLine().Split();
            int[] b = new int[a.Length];
            for (int i = 0; i < a.Length; i++) {
                b[i] = int.Parse(a[i]);
            }
            return b;
        }

        public static double NextDouble () {
            return double.Parse(Console.ReadLine());
        }

    }
}



Measured memory_bytes: 17964
#include <iostream>
#include <climits>
#include <vector>
#include <iomanip>
#include <cmath>

#define _USE_MATH_DEFINES

using namespace std;

void MinMaxSum(vector<int> data){
    int min = INT_MAX;
    int max = INT_MIN;
    long long sum = 0;
    for (auto elem : data){
        if (min > elem){
            min = elem;
        }
        if (max < elem){
            max = elem;
        }
        sum += elem;
    }
    cout << min << " " << max << " " << sum << endl;
}

int main(){
    // ---------------------------
    // ITP_1_4_A: A/B Problem
//    int a, b;
//    std::cin >> a >> b;
//    PrintDivision(a,b);
    // ---------------------------

    // ---------------------------
    // ITP_1_4_B: Circle
//    double r;
//    std::cin >> r;
//    CalcCircle(r);
    // ---------------------------

    // ---------------------------
    // ITP_1_4_C: Simple Calculator
//    int a, b;
//    char op;
//    std::vector<calctuple> data;
//    while(1) {
//        std::cin >> a >> op >> b;
//        data.emplace_back(a, op, b);
//        if( op == '?' )
//            break;
//    }
//    SimpleCalculator(data);
    // ---------------------------

    // ---------------------------
    // ITP_1_4_D: Min, Max and Sum
    int n;
    std::vector<int> data;
    std::cin >> n;
    for (int i = 0; i < n; i++){
        int elem;
        std::cin >> elem;
        data.push_back(elem);
    }
    MinMaxSum(data);
    // ---------------------------

    return 0;
}


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

using namespace std;
int main(void){
    
    int n,count=0,flg=1;
    int *a;

    cin >> n;

    a=new int[n];

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

    while(1){
        for(int i=0;i<n;i++){
            if(a[i]%2==1)flg=0;
            else a[i]/=2;
        }
        if(flg==0)break;
        else count++;
    }

    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

int main()
{
	int A = 0, B = 0, T = 0, ans = 0;
	std::cin >> A >> B >> T;

	int i = 1;
	double t = A;
	while (t <= T + 0.5){
		ans += B;
		i++;
		t = i * A;
	}

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

	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<map>
#include<utility>
#include<climits>
#include<set>
using namespace std;
#define ll long long

void solve(){
	int i=1;
	int t=5;
	while(t--)
	{
		int a;cin>>a;
		if(a==0)
		{
			cout<<i;
			return;
		}
		i++;
	}
}
int main(){ 
	ios_base::sync_with_stdio(false);
	    cin.tie(0);
	    cout.tie(0);
	    
	#ifndef ONLINE_JUDGE
	    freopen("input.txt","r",stdin);
	    freopen("output.txt","w",stdout);
	#endif


	    int t=1;
//        cin>>t;
        for(int i=1;i<=t;i++){
//          cout<<"Case #"<<i<<": ";
            solve();
        }
        return 0;

}


Measured memory_bytes: 3584
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>

using namespace std;

int main() {
    

    int n;
    cin >> n;
    vector <int> a;
    for (int i=0;i<n;i++){
        int p;
        cin >> p;
        a.push_back(p);
    }
    vector <int>::iterator p;
    for (p=a.end();p!=a.begin()+1;){
        p--;
        cout << *p << " ";
    }
    cout << *a.begin() << endl;


}


Measured memory_bytes: 3156
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'

using namespace std;

main()
{
    fast;
    int n,i;
    double t,a;
    cin>>n;
    cin>>t>>a;
    int cur=0;
    double h[n],diff=1e8;
    for (i=0;i<n;i++)
    {
        cin>>h[i];
        h[i]=t-h[i]*0.006;
    }
    for (i=1;i<n;i++)
    {
        if (abs(a-h[cur])>abs(a-h[i]))
            cur=i;
    }
    cout<<cur+1<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

class union_find{
  int n_; 
  vector<int> par, rank, size;
public:
  union_find(int n): n_{n} {
    par.resize(n_);
    rank.resize(n_);
    size.resize(n_);

    REP(i, n_){
      par[i] = i;
      rank[i] = 1;
      size[i] = 1;
    }
  } 

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if(x == y) return;

    if(rank[x] > rank[y]){
      par[y] = x;
      size[x] += size[y];
    } else {
      par[x] = y;
      size[y] += size[x];
      if(rank[x] == rank[y]) rank[y]++;
    }
  }

  int find(int x) {
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
  }

  vector<int> get_par(){ return par; }
};

struct edge{ int u, v; ll w; };

ll kruskal(int n, vector<edge> edges) {
  sort(edges.begin(), edges.end(), [](const edge& e1, const edge& e2){
    return e1.w < e2.w;
  });

  union_find uf(n);
  ll ret = 0;

  for(const auto& edge: edges){
    if(!uf.same(edge.u, edge.v)){
      uf.unite(edge.u, edge.v);
      ret += edge.w;
    }
  }

  return ret;
}

struct node{ int v; ll x, y; };

int main() {
  int n; cin >> n;
  vector<node> p(n), q(n);
  REP(i, n){
    ll x, y; cin >> x >> y;
    p[i] = {i, x, y};
    q[i] = {i, y, x};
  }

  vector<edge> edges;

  function<void(vector<node>&)> addEdge = [&](vector<node>& v){
    sort(v.begin(), v.end(), [](node& a, node& b){
      return a.x < b.x;
    });

    REP(i, n-1){
      ll dx = abs(v[i].x - v[i+1].x), dy = abs(v[i].y - v[i+1].y);
      edges.push_back({v[i].v, v[i+1].v, min(dx, dy)});
    }
  };

  addEdge(p);
  addEdge(q);
    
  cout << kruskal(n, edges) << endl;

  return 0;
}


Measured memory_bytes: 13036
#include<bits/stdc++.h>
using namespace std;
int main(){
    int N;cin>>N;
    std::vector<int> a(N);
    std::set<int> s;
    for(int i=0;i<N;i++){
        cin>>a.at(i);
        s.insert(a.at(i));
    }
    
    if(*rbegin(s)==0&&s.size()==1){
        cout<<"Yes"<<endl;
        return 0;
    }
    
    else if(s.size()==2&&*begin(s)==0){
        int m = *rbegin(s);
        int n = 0;
        for(int i=0;i<N;i++){
            if(a.at(i) == m) n++;
        }
        if(n*3 == N*2){
            cout<<"Yes"<<endl;
            return 0;
        }
        else cout <<"No"<<endl;
        return 0;
    }
    else if(s.size() != 3){
        cout<<"No"<<endl;
        return 0;
    }
    
    else{
        vector<int> c(3),d(3,0);
        c.at(0) = *begin(s);
        c.at(1) = *rbegin(s);
        s.erase(c.at(0));
        c.at(2) = *begin(s);
        int f,g,h;
        f=c.at(0);g=c.at(1);h=c.at(2);
        
        while(g>0||h>0){
            if((f%2+g%2+h%2)%2!=0){
                cout <<"No"<<endl;
                return 0;
            }
            else{
                f/=2;g/=2;h/=2;//gが最大値なのは明白
            }
        }
        
        for(int i=0;i<N;i++){
            for(int j=0;j<3;j++){
                if(c.at(j)==a.at(i)) d.at(j)++;
            }//setでは数値にアクセスできないからvectorに直す。それぞれの数の帽子の個数を調べる。
        }
        if(d.at(0)==d.at(1)&&d.at(0)==N/3){
            cout <<"Yes"<<endl;
        }
        else cout <<"No "<<endl;
    }
}

Measured memory_bytes: 4224
#include<iostream>
#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    /*ll t;
    cin>>t;
    while (t--)
    {*/
        ll n,m;
        cin>>n>>m;
        if (n==m)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    //}
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef long long int ll;

#define ARRAY_MAX 100005


int main(){

    ll n,q;
    cin >> n >> q;
    vector<ll> arr(ARRAY_MAX,0);
    REP(i,n){
        cin >> arr[i];
    }

    REP(i,q){
        ll x;
        cin >> x;
        ll sum = 0,ans = 0,left = 0,right = 0;
        for(left = 0;left < n;left++){
            //左を固定する            
            while(right < n && sum + arr[right] <= x){
                //総和がx以下なら右側を伸ばす
                sum += arr[right];
                right++;
            }
            ans += right-left;
            sum -= arr[left];
        }
        cout << ans << endl;
    }


    return 0;
}



Measured memory_bytes: 3640
// writer: w33z8kqrqk8zzzx33
#include <bits/stdc++.h>
using namespace std;
 
#define iter(i, a, b) for(int i=(a); i<(b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a)+1)
#define log(a) cerr<<"\033[32m[DEBUG] "<<#a<<'='<<(a)<<" @ line "<<__LINE__<<"\033[0m"<<endl
#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
 
using ll=long long;
using pii=pair<int, int>;
//#define int ll
const int MOD = 1000000007;

long double dp[3000][3000];

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int N; cin >> N;
    dp[0][0] = 1;
    rep1(i, N) {
        long double v; cin >> v;
        dp[i][0] = dp[i-1][0] * (1-v);
        rep1(c, N)
            dp[i][c] = dp[i-1][c-1] * v + dp[i-1][c] * (1-v);
    }
    rep(c, N+1) {
        if(N-c < c) {
            dp[0][1] += dp[N][c];
        }
    }
    cout << setprecision(18) << fixed << dp[0][1] << endl;
}



Measured memory_bytes: 140800
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <unordered_map>
#include <unordered_set>

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

    if (N % 2 == 0) {
        const uint64_t buf = N / 2;
        std::printf("%lu\n", (N-1) * buf);
    } else {
        const uint64_t buf = (N-1) / 2;
        std::printf("%lu\n", N * buf);
    }

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <set>

using namespace std;

long long n;
set<long long> st;

void dfs(string s, long long a);
bool check(string s);

int main()
{
	cin >> n;

	dfs("3", 3);
	dfs("5", 5);
	dfs("7", 7);

	cout << st.size() << endl;

	return 0;
}

void dfs(string s, long long a)
{
	if (check(s)) {
		st.insert(a);
	}

	if (a * 10 + 3 <= n) {
		dfs(s + "3", a * 10 + 3);
	}

	if (a * 10 + 5 <= n) {
		dfs(s + "5", a * 10 + 5);
	}

	if (a * 10 + 7 <= n) {
		dfs(s + "7", a * 10 + 7);
	}
}

bool check(string s)
{
	bool a[3] = {};

	for (int i = 0; i < s.length(); i++) {
		switch (s[i]) {
		case '3':
			a[0] = true;
			break;
		case '5':
			a[1] = true;
			break;
		case '7':
			a[2] = true;
			break;
		}
	}

	for (int i = 0; i < 3; i++) {
		if (!a[i]) {
			return false;
		}
	}
	return true;
}

Measured memory_bytes: 1536
#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int x;
	cin >> x;
	cout << 1000 * (x / 500) + (x % 500) / 5 * 5 << endl;
	return 0;
}


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

int main(){
  int a,b;
  char c;
  while(cin>>a>>c>>b,c!='?'){
    int result=0;
    switch(c){
    case '+':
      result = a + b;
      break;
    case '-':
      result = a - b;
      break;
    case '*':
      result = a * b;
      break;
    case '/':
      result = a / b;
      break;
    }
    cout<<result<<endl;
  }

  return 0;
}

Measured memory_bytes: 1160
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int a, b, n;
	cin >> a >> b;
	n = (a+b-3)/(a-1);
	cout << n << endl;
	return 0;
}


Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a)  (a).begin(),(a).end()
const ll INF = 1e18+18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;


int main(){
  ll N, We;
  cin>>N>>We;
  vector<ll> W(N), V(N);
  rep(i, N){
    cin>>W[i]>>V[i];
  }
  vector<vector<ll>> dp(N, vector<ll>(100100, 0));
  //dp[0][W[0]]=V[0];//W[i]<=Weより
  for(ll i=W[0]; i<=We; i++){
    dp[0][i]=V[0];
  }
  rep(i, N-1){
    rep(j, 100100){
      if(j-W[i+1]>=0){
        dp[i+1][j]=max(dp[i][j], dp[i][j-W[i+1]]+V[i+1]);
      }else{
        dp[i+1][j]=dp[i][j];
      }
    }
  }
  /*rep(i, N){
    rep(j, We+1){
      cout<<dp[i][j]<<" ";
    }
    cout<<endl;
  }*/
  cout<<dp[N-1][We]<<endl;
} 

Measured memory_bytes: 82444
n = int(input())
p = list(map(int, input().split()))

cnt = 0
for i in range(n-2):
  if (p[i]<p[i+1] and p[i+1]<p[i+2]) or (p[i]>p[i+1] and p[i+1]>p[i+2]):
    cnt += 1
print(cnt)

Measured memory_bytes: 9120
#include<stdio.h>

void CountSort(int *,int ,int);

int main(){
  int x,i,max=0;
  scanf("%d",&x);
  int a[x+1];
  for(i=1;i<=x;i++){
    scanf("%d",&a[i]);
    if(a[i]>max)
      max=a[i];
  }

  CountSort(a,x,max);
  return 0;
}
void CountSort(int *a,int x,int max){

  int b[max],c[x+1],i;

  for(i=0;i<=max;i++)
    b[i]=0;

  for(i=1;i<=x;i++)
    b[a[i]]++;

  for(i=1;i<=max;i++)
    b[i]=b[i]+b[i-1];

  for(i=x;i>=1;i--){
    c[b[a[i]]]=a[i];
    b[a[i]]--;
  }

  for(i=1;i<=x;i++){
    if(i==x)
      printf("%d\n",c[i]);
    else
      printf("%d ",c[i]);
  }
}



Measured memory_bytes: 17356
#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl
#define YORN(f) cout<<((f)?"YES":"NO")<<endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main()
{
    string a, s, b;
    cin >> a >> s >> b;
    cout << a[0] << s[0] << b[0] << endl;
    return 0;
}

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

int main(){
  int N;
  string S;
  cin >> N >> S;
  vector<int> C(26);
  for(int i=0; i<N; i++){
    C.at(S.at(i)-'a')++;
  }
  int64_t Ans=1;
  for(int i=0; i<26; i++){
    Ans*=C.at(i)+1;
    Ans%=1000000007;
  }
  cout << Ans-1 << endl;
}

Measured memory_bytes: 512
def mapint_inp():
    return map(int, input().split())

def intinp():
    return int(input())

N = intinp()
A = list(mapint_inp())

lim = 10**18
ans = 1
f = True
if 0 in A:
    ans = 0
else:
    for A_i in A:
        ans = ans*A_i
        if ans > lim:
            f = False
            break

if f:
    print(ans)
else:
    print(-1)

Measured memory_bytes: 21596
fn main() {
    let n = read::<usize>();
    let mut l = read_vec::<usize>();
    l.sort();

    let mut ret = 0;
    for i in 0..n-1 {
        for j in i+1..n {
            let c = lower_bound(&l, l[i] + l[j]);
            if c > j + 1 {
                ret += c - j - 1;
            }
        }
    }

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

fn lower_bound(v: &Vec<usize>, k: usize) -> usize {
    let mut low = 0;
    let mut high = v.len();

    while low < high {
        let mid = (low + high) / 2;
        if v[mid] < k {
            low = mid + 1;
        } else {
            high = mid;
        }
    }

    low
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

Measured memory_bytes: 4352
#include <stdio.h>
#define WHITE 0
#define GRAY 1
#define BLACK 2
void dfs(void);
void dfsv(int);
int graph[101][101],color[101],n,time,d[101],f[101];
int main(){
  int i,u,k,j,v;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
      for(j=0;j<k;j++){
	scanf("%d",&v);
	graph[u-1][v-1]=1;
      }
      }
  dfs();

  for(i=0;i<n;i++){
    printf("%d %d %d\n",i+1,d[i],f[i]);
  }

  return 0;
}

void dfs(){
  int i;
  for(i=0;i<n;i++) color[i]=WHITE;
  time=0;
  for(i=0;i<n;i++){
    if(color[i]==0)dfsv(i);
  }
 

}

void dfsv(int u){
  int i;
  color[u]=GRAY;
    d[u]=++time;
    for(i=0;i<n;i++){
      if(graph[u][i]==0)continue;
      if(color[i]==0) dfsv(i);
    }
    color[u]=BLACK;
    f[u]=++time;
}

Measured memory_bytes: 636
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = [inp() for _ in range(n)]
res = 0
for i in range(1,n)[::-1]:
    if a[i]-a[i-1] == 1:
        res += 1
    else:
        res += a[i]
for i in range(n-1):
    if a[i+1] - a[i] > 1:
        res = -1
if a[0] != 0:
    res = -1 
print(res)

Measured memory_bytes: 85952
#include<bits/stdc++.h>
#define rep(i, n) for(int (i)=0 ;i <(n); ++(i))
#define REP(i,m,n) for(int (i) = (m); (i) <= (n); ++(i))
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)? "YES":"NO")
typedef long long ll;
using namespace std;

bool f(int a, int b, vector<int> v) {
  bool ta = false;
  bool tb = false;
  for(auto p : v) {
    if(a == p) {
      ta = true;
    }
    if(b == p) {
      tb = true;
    }
  }
  if(ta && tb) {
    return true;
  }
  else {
    return false;
  }
}

int main() {
  int a, b;
  cin >> a >> b;
  vector<int> ga(7);
  ga = {1, 3, 5, 7, 8, 10, 12};
  vector<int> gb(4);
  gb = {4, 6, 9, 11};
  vector<int> gc(1);
  gc = {2};

  if(f(a, b, ga) || f(a, b, gb) || f(a, b, gc)) {
    cout << "Yes" << endl;
  } 
  else {
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<set>
#include<string>
#define rep(i,n) for(int i=0; i<(int)n; i++)//n回繰り返す
using namespace std;
using ll = long long;
using P =pair<int,int>;

int main() {
    string s;
    cin >>s;
    if(s.at(0)==s.at(1)&&s.at(0)==s.at(2)&&s.at(1)==s.at(2)){
        cout<<"No"<<endl;
     }
    else{
        cout<<"Yes"<<endl;
    }
}

Measured memory_bytes: 256