akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		int count = 0;
		int prev = sc.nextInt();
		int curr = sc.nextInt();
		for (int i = 2; i < n; i++) {
			int next = sc.nextInt();
			if(curr < prev && curr > next)count++;
			else if(curr > prev && curr < next)count++;
			prev = curr;
			curr = next;
		}
		System.out.println(count);
		
	}

}


Measured memory_bytes: 23636
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())//vectorの降順ソート
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;
//typedef tuple<ll, ll, ll> T;

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; }


int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin>>s;
    int l=0,r=s.size()-1;
    int ans=0;
    while(l<=r){
        if(s[l]==s[r]) l++,r--;
        else{
            if(s[l]=='x') l++,ans++;
            else if(s[r]=='x') r--,ans++;
            else{
                cout<<-1<<endl;
                return 0;
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
const LL INF = 1000000000000;
using P = pair<LL, LL>;
vector<LL> par(100000), rnk(100000), x(100000);
vector<P> LR[100000];
LL M = 0, m = 0;

bool dfs(LL p, bool f){
    if(!f)  x[p] = 0;
    for(LL i = 0;i < LR[p].size();i++){
        LL r = LR[p][i].first;
        LL d = LR[p][i].second;
        if(x[r] == INF){
            x[r] = d+x[p];
            M = max(M, x[r]);
            m = min(m, x[r]);
            if(!dfs(r, true))  return false;
        }else if(x[r] != d+x[p]){
            return false;
        }
    }
    return true;
}
void init(LL n){
    for(LL i = 0;i < n;i++){
        par[i] = i;
        rnk[i] = 0;
    }
}
LL find(LL x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
void unite(LL x, LL y){
    x = find(x);
    y = find(y);
    if(x == y)  return;
    if(rnk[x] < rnk[y]){
        par[x] = y;
    }else{
        par[y] = x;
        if(rnk[x] == rnk[y])  rnk[x]++;
    }
}
bool same(LL x, LL y){
    return find(x) == find(y);
}

int main(){
    LL N, M;
    cin >> N >> M;
    init(N);
    set<LL> vp;
    for(LL i = 0;i < M;i++){
        LL l, r, d;
        cin >> l >> r >> d;
        unite(l-1, r-1);
        LR[l-1].emplace_back(make_pair(r-1, d));
        LR[r-1].emplace_back(make_pair(l-1, -d));
    }
    for(LL i = 0;i < N;i++){
        vp.insert(find(i));
        x[i] = INF;
    }
    bool ans = true;
    auto itr = vp.begin();
    for(int i = 0;i < vp.size();i++){
        M = 0;
        m = 0;
        if(!dfs(*itr++, false) || (M-m) > 1000000000){
            ans = false;
            break;
        }
    }
    if(ans) cout << "Yes" << endl;
    else    cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 18048
i=105
N=input()
N=int(N)
m=5
n=0
x=0
y=0
while i <= N:
  if i % 2 != 0:
    if i % 3 == 0:
      n = int(i / 3)
      while m < n:
        if i % m == 0:
          x = x + 1
        m=m+2
      if x == 4:
        y = y + 1
  x=0
  n=0
  m=5
  i = i + 1
print(y)

Measured memory_bytes: 3064
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int rol[4][4]={
	{4,5,2,0},
	{1,0,3,5},
	{0,2,5,4},
	{5,3,0,1}
};
vi a;
void roll(int b){
	int tmp=a[2+rol[b][0]];
	rep(i,3)a[2+rol[b][i]]=a[2+rol[b][i+1]];
	a[2+rol[b][3]]=tmp;
}
int main(){
	int n,m;
	while(cin>>n>>m,n+m){
		vvi in(n,vi(m));
		rep(i,n)rep(j,m)cin>>in[i][j];
		int sx,sy,gx,gy;
		cin>>sx>>sy>>gx>>gy;
		vi q(8);
		q[0]=sx;q[1]=sy;q[2]=1;q[3]=2;
		q[4]=3;q[5]=5;q[6]=4;q[7]=6;
		int ma[11][11][666];
		rep(i,11)rep(j,11)rep(k,666)ma[i][j][k]=inf;
		ma[sx][sy][123]=0;
		int out=inf;
		priority_queue<pair<int,vi> >que;
		que.push(pair<int,vi>(0,q));
		while(!que.empty()){
			vi w=que.top().second;
			int t=-que.top().first;
			que.pop();
			if(ma[w[0]][w[1]][w[2]*100+w[3]*10+w[4]]<t)continue;
			rep(i,4){
				a=w;
				a[0]+=dx[i];
				a[1]+=dy[i];
				if(a[0]<0||a[1]<0||a[0]>=n||a[1]>=m)continue;
				roll(i);
				int cost=t+a[7]*in[a[0]][a[1]];
				if(ma[a[0]][a[1]][a[2]*100+a[3]*10+a[4]]>cost){
					ma[a[0]][a[1]][a[2]*100+a[3]*10+a[4]]=cost;
					que.push(pair<int,vi>(-cost,a));
					if(a[0]==gx&&a[1]==gy)out=min(out,cost);
				}
			}
		}
		cout<<out<<endl;
	}
}

Measured memory_bytes: 3388
d = int(input())
c = list(map(int, input().split()))
s = []
lastDays = [0]*26

for _ in range(d):
  s.append(list(map(int, input().split())))

for i in range(d):
  kouho = 1
  kouhoHyouka = -999999999999
  for j in range(26):
    if s[i][j] - c[j] * (i + 1 - lastDays[j]) > kouhoHyouka:
      kouho = j
      kouhoHyouka = s[i][j] - c[j] * (i + 1 - lastDays[j])
  lastDays[j] = i+1
  print(kouho+1)
  

Measured memory_bytes: 74680
#include <bits/stdc++.h>
 using namespace std;
 int main(void){
     // Your code here
     int A,B,C = 1,count = 0;
     cin >> A >> B;
    	while(C<B){
    		C += A-1;
    		count++;
    	}
    	cout << count;
 }

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

int main (){
	int A, B, C;cin>>A>>B>>C;
	int max=0;
	if(max<A)max=A;
	if(max<B)max=B;
	if(max<C)max=C;
	cout<<max*10+A+B+C-max<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;

int main() {
  int n;
  cin >> n;
  int aa[n], bb[n];
  REP(i,n) cin >> aa[i] >> bb[i];
  typedef pair<int,int> pii;
  vector<pii> v(n);
  REP(i,n) {
    v[i] = pii(bb[i],i);
  }
  sort(ALL(v));
  int a[n], b[n];
  REP(i,n) {
    a[i] = aa[v[i].second];
    b[i] = v[i].first;
  }
  int dp[n+1][n+1];              // i âÚÜÅÅj âFAC æéÆ«ÌÅ¬Ô
  REP(i,n+1)REP(j,n+1) dp[i][j] = INF;
  REP(i,n+1) dp[i][0] = 0;
  REP(i,n) {
    REP(j,n) {
      if (dp[i][j]+a[i] <= b[i])
        dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]+a[i]);
      dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
    }
  }
  // REP(i,n+1) {
  //   REP(j,n+1) {
  //     printf("(%d,%d)%d ",i,j,dp[i][j]);
  //   }
  //   cout << endl;
  // }
  int res = n;
  REP(i,n+1) {
    if (dp[n][i]==INF) {
      res = i-1;
      break;
    }
  }
  cout << res << endl;
  
}

Measured memory_bytes: 0
import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[] args)throws Exception{
        String str;
        Scanner sc=new Scanner(System.in);
        str=sc.next();
        sc.close();
        Queue<Integer> q=new ArrayDeque<>();
        int len=str.length();
        double gt=0;
        for(long bit=0;bit<(1<<len-1);bit++){
            for(int i=0;i<len-1;i++){
                if((bit&(1<<i))>0){
                    q.add(i);
                }
            }
            int count=0;
            while(q.peek()!=null){
                gt+=Double.parseDouble(str.substring(count,q.peek()+1));
                count=q.remove()+1;
            }
            gt+=Double.parseDouble(str.substring(count));
        }
        System.out.println(String.format("%.0f", gt));
    }
}

Measured memory_bytes: 23124
import java.util.*;
 
public class Main {

    private static final long MOD = 998244353;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] num = new int[N+10];
        int maxDist = 0;
        boolean valid = true;
        for (int i = 0; i < N; i++) {
            int dist = sc.nextInt();
            if (i == 0 && dist != 0) {
                valid = false;
            }
            num[dist]++;
            maxDist = Math.max(maxDist, dist);
        }
        if (!valid || num[0] != 1) {
            // invalid
            System.out.println(0);
            return;
        }

        long ans = 1;
        for (int i = 1; i <= maxDist; i++) {
            int a = num[i-1];
            int n = num[i];
            // a^n
            while (n-- > 0) {
                ans *= (long)a;
                ans %= MOD;
            }
        }
        System.out.println(ans);
    }
}


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

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout)

template<typename T>
inline void read(T &x) {
	char ch, t = 0; x = 0;
	while(!isdigit(ch = ge)) t |= ch == '-';
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge;
	x = t ? -x : x;
}
inline int read_int() { int x; return read(x), x; }
inline ll read_ll() { ll x; return read(x), x; }

template<typename T>
inline void chkmin(T&a, T b) { a = a < b ? a : b; }

const int MAXN = 100010;

ll sq3[2200];
ll s[MAXN];

int tot;
int pri[2200];
int chk[2200];

inline void Get_Prime(int n) {
	for(int i = 2; i <= n; i++) {
		if(!chk[i]) pri[++tot] = i;
		for(int j = 1; j <= tot; j++) {
			if(i * pri[j] > n) break;
			chk[i * pri[j]] = 1;
			if(i % pri[j] == 0) break;
		}
	}
}

inline void init(int n) {
	for(int i = 1; i <= n; i++)
		sq3[i] = 1LL * i * i * i;
}

inline ll work(ll x) {
	for(int i = 1; i <= tot; i++)
		while(x % sq3[pri[i]] == 0) x /= sq3[pri[i]];
	return x;
}

inline ll doit(ll&s, ll&x, ll&r) {
	x = r = 1;
	for(int i = 1; i <= tot; i++)
		if(s % pri[i] == 0) {
			if(s % (pri[i] * pri[i]) == 0) r *= pri[i], x *= pri[i] * pri[i];
			else x *= pri[i], r *= pri[i] * pri[i];
			while(s % pri[i] == 0) s /= pri[i];
		}
	return s;
}

struct Node {
	ll t, r;

	Node() {}
	Node(ll t, ll r):t(t), r(r) {}
};

vector<Node> c1[MAXN];
vector<Node> c2[MAXN];
map<ll, int> mp1, mp2;

int main() {
#ifdef LOCAL
	FILE("");
#endif

	init(2160), Get_Prime(2160);

	int n = ri;
	for(int i = 1; i <= n; i++) s[i] = work(rl);
	sort(s + 1, s + 1 + n);

	int res = s[1] == 1;
	for(int i = 1; i <= n; i++) {
		if(s[i] == 1) continue;
		ll t, r;
		ll v = sqrt(doit(s[i], t, r));
		while(v * v > s[i]) --v;
		while(v * v < s[i]) ++v;
		if(v * v == s[i]) c2[v].push_back(Node(t, r));
		else if(s[i] <= 100000) c1[s[i]].push_back(Node(t, r));
		else ++res;
	}

	for(auto x : c2[1]) c1[1].push_back(x);
	for(auto x : c1[1]) mp1[x.t]++;
	for(auto x : c1[1]) res += max(mp1[x.t], mp1[x.r]), mp1[x.t] = mp1[x.r] = 0;

	for(int i = 2; i <= 100000; i++)
		if(!c1[i].size() || !c2[i].size()) res += c1[i].size() + c2[i].size();
		else {
			mp1.clear(), mp2.clear();
			for(auto x : c1[i]) mp1[x.t]++;
			for(auto x : c2[i]) mp2[x.t]++;
			for(auto x : c1[i]) res += max(mp1[x.t], mp2[x.r]), mp1[x.t] = mp2[x.r] = 0;
			for(auto x : c2[i]) res += max(mp2[x.t], mp1[x.r]), mp2[x.t] = mp1[x.r] = 0;
		}
	
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 14964
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()


def main():
    n,c,k = LI()
    a = [II() for _ in range(n)]
    a.sort()
    m = a[0]
    t = 1
    r = 0
    for ai in a[1:]:
        if ai > m+k or t == c:
            r += 1
            t = 1
            m = ai
            continue
        t += 1

    if t > 0:
        r += 1
    return r

print(main())


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

int main() {
  int n,k;
  cin>>n>>k;
  string s;
  cin>>s;
  if(s[k-1]=='A'){
    s[k-1]='a';
  }else if(s[k-1]=='B'){
    s[k-1]='b';
  }else if(s[k-1]=='C'){
    s[k-1]='c';
  }
  cout<<s;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <numeric>
#include <map>
#include <set>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n, i;
    cin>>n>>i;
    cout<<(n-i+1)<<endl;
    return 0;
}

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

class Program
{
    static void Main(string[] args)
    {
        // 入力
        string s = Console.ReadLine();
        long q = long.Parse(Console.ReadLine());
        string[][] jagQ = new string[q][];
        for (long i = 0; i < jagQ.Length; i++)
        {
            jagQ[i] = Console.ReadLine().Split().ToArray();
        }

        var list = new LinkedList<string>();
        for (int i = 0; i < s.Length; i++)
        {
            list.AddLast(s[i].ToString());
        }

        var rev = false;
        for (long i = 0; i < q; i++)
        {
            if (jagQ[i][0] == "1")
            {
                rev = !rev;
            }
            else
            {
                var tmp = int.Parse(jagQ[i][1]);
                // 反転状態だと 1->2, 2->1 にして追加先を変更する
                if (rev) tmp = (tmp + 1) % 2;

                if (tmp == 1)
                {
                    list.AddFirst(jagQ[i][2]);
                }
                else
                {
                    list.AddLast(jagQ[i][2]);
                }
            }
        }

        // 反転状態の場合は後ろから出力するのを忘れない
        if (rev)
        {
            foreach (var item in list.Reverse())
            {
                Console.Write(item);
            }
        }
        else
        {
            foreach (var item in list)
            {
                Console.Write(item);
            }
        }
        Console.WriteLine();

    }
}


Measured memory_bytes: 66212
#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=3e5+10,mod=1e9+7,INF=0x3f3f3f3f;
string s; 
int main()
{
	cin>>s;
	if (s[1]=='B')
		s[1]='R';
	else
		s[1]='B';
	cout<<s;
	return 0;
}


Measured memory_bytes: 3624
#include <stdio.h>

int main(void){

  int a;
  int b;
  int c;
  int dif1;
  int dif2;
  
  char buf1[30];
  fgets(buf1, 30, stdin);
  sscanf(buf1, "%d %d %d", &a, &b, &c);
  
  dif1 = b - a;
  dif2 = c - b;
  
  if(dif1 == dif2){
  printf("YES");
  }else{
  printf("NO");
  }
  
  return 0;
}

Measured memory_bytes: 128
from itertools import accumulate

INF = float("inf")
IINF = 10 ** 18
MOD = 998244353

def main():
    N = int(input())
    A = list(map(int, input().split()))

    lengths = list(accumulate([0] + A))

    # length = sum(A)
    # half = len(A) // 2
    half = lengths[-1] // 2

    # print(lengths, half)

    if N == 2:
        print(abs(A[0] - A[1]))
    else:
        a1 = 0
        b1 = 0
        a2 = 0
        b2 = 0
        for i in range(1, N):
            if lengths[i] >= half:
                a1 = lengths[i]
                b1 = lengths[N] - lengths[i]
                a2 = lengths[i-1]
                b2 = lengths[N] - lengths[i-1]
                break

        # print(b2, a2)
        # print(b1, a1)
        print(min(abs(b2 - a2), abs(b1 - a1)))


if __name__ == '__main__':
    main()


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

int main(){
  int m[2],d[2];
  rep(i,2) cin >> m[i] >> d[i];
  if(m[0] == m[1]) cout << "0" << endl;
  else cout << "1" << endl;
  return 0;
}

Measured memory_bytes: 256
def main():
  s=list(input())
  count1,count2,l=0,0,len(s)
  for i in range(l):
    count1=count1+1 if (i%2==0 and int(s[i])==1) or (i%2==1 and int(s[i])==0) else count1
    count2=count2+1 if (i%2==1 and int(s[i])==1) or (i%2==0 and int(s[i])==0) else count2
  print(min(count1,count2))

if __name__=='__main__':
  main()


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

int main(){
    int n;
    cin >> n;
    double v[n];
    for(int i=0; i<n; i++){
        cin >> v[i];
    }
    sort(v,v+n);
    for(int i=0; i<n-1; i++){
        v[i+1] = (v[i]+v[i+1])/2.0;
    }
    cout << v[n-1] << endl;
    return 0;
}


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

ll v_max(vector<ll> x){
    
    ll n = x.size();
    sort(x.begin(), x.end());
    return x[n-1];

    //vector内の最大値を出力
    //入力:vector<ll>
    //出力:ll

}

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

    ll n;
    cin >> n;
    vector<ll> h(n);
    for (ll i = 0;i < n;i++){
        cin >> h[i];
    }

    ll ans = 0;

    while (true){
        if (v_max(h) == 0){
            break;
        }
        ll i = 0;
        while (i < n){
            if (h[i] == 0){
                i++;
            }
            else{
                ans++;
                while (i < n && h[i] > 0){
                    h[i]--;
                    i++;
                }
            }
        }
    }

    cout << ans << endl;
        
}

Measured memory_bytes: 3600
#include <bits/stdc++.h>
#define ll long long
#define ALL(x) x.begin(),x.end()
#define MOD (1000000000+7)
using namespace std;
#define PI 3.14159265359
#define tMOD 998244353
#define mkpr(x,y) make_pair(x,y)



int main(){
string s;
cin>>s;

if(s[2]==s[3]&&s[4]==s[5]){
    cout<<"Yes"<<endl;
}
else{
    cout<<"No"<<endl;
}






}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<bitset>
#include<unordered_map>
using namespace std;
//---------------------------------------------------
//ライブラリゾーン！！！！
typedef long long ll;
typedef long double ld;
#define str string
#define rep(i,j) for(ll i=0;i<(long long)(j);i++)
#define all(a) (a).begin(),(a).end()
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = { { 0,0,-1,1 },{ -1,1,0,0 } };
struct P {
	ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B {//隣接リスト表現
	ll to, cost;
};
struct E {//辺の情報を入れる変数
	ll from, to, cost;
};
bool operator<(E a, E b) {
	return a.cost < b.cost;
}
struct H {
	ll x, y;
};
bool operator<(H a, H b) {
	if (a.x != b.x) return a.x < b.x;
	return a.y < b.y;
}
bool operator>(H a, H b) {
	if (a.x != b.x) return a.x > b.x;
	return a.y > b.y;
}
bool operator==(H a, H b) {
	return a.x == b.x&&a.y == b.y;
}
bool operator!=(H a, H b) {
	return a.x != b.x || a.y != b.y;
}
ll gcm(ll i, ll j) {//最大公約数
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcm(j%i, i);
}
ld rad(H a, H b) {
	return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}//rad＝座標上の2点間の距離
ll ari(ll a, ll b, ll c) {
	return (a + b)*c / 2;
}//等差数列の和
ll fact(ll x, ll k, ll p) {//最大値、個数、あまり
	ll sum = 1;
	for (int i = 0; i < k; i++) {
		sum *= (x--);
		sum %= p;
	}
	return sum;
}//階乗(正）
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res*x%p;
		x = x*x%p;
		n >>= 1;
	}
	return res;
}//x^n%p
short ctos(char a) {
	return (int)(a - '0');
}
#define int long long
const long long Inf = 4523372036854775807;
const int inf = 15000000000;
//----------------------------------------------------
//++++++++++++++++++++++++++++++++++++++++++++++++++++
int n, m, x;
bool a[1000];
signed main() {
	cin >> n >> m >> x;
	for (int i = 0; i < m; i++) {
		int y;
		cin >> y;
		a[y] = 1;
	}
	int sum = 0;
	for (int i = x; i >= 0; i--) {
		if (a[i]) sum++;
	}
	int res = 0;
	for (int i = x; i <= n; i++) {
		if (a[i]) res++;
	}
	cout << min(sum, res) << endl;
}

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

typedef long long ll;

int main() {
    int N;
    cin >> N;
    double sum=0.0;
    double A;
    for (int i=0;i<N;i++){
        cin >> A;
        sum += 1.0/A;
    }
    cout << fixed << setprecision(6) << 1.0/sum << endl;

    return 0;
}

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

#define MAXN 303

long long dist[MAXN][MAXN], g[MAXN][MAXN];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	memset(dist, -1, sizeof dist);
	memset(g, -1, sizeof g);
	
	int n, m, cap;
	cin >> n >> m >> cap;
	for(int i = 0; i < m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		u--, v--;
		dist[u][v] = dist[v][u] = w;
	}
	
	for(int k = 0; k < n; k++) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(dist[i][k] == -1 || dist[k][j] == -1) continue;
				if(dist[i][j] == -1) dist[i][j] = 1e13;
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			if(dist[i][j] == -1) continue;
			if(dist[i][j] <= cap) {
				g[i][j] = 1;
			}
		}
	}
	
	for(int k = 0; k < n; k++) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(g[i][k] == -1 || g[k][j] == -1) continue;
				if(g[i][j] == -1) g[i][j] = 1e13;
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
			}
		}
	}
	
	int q;
	cin >> q;
	while(q--) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		cout << (g[u][v] == -1 ? -1 : g[u][v]-1) << '\n';
	}
}


Measured memory_bytes: 2048
def gcd(m,n):
    x = max(m,n)
    y = min(m,n)
    if x%y == 0:
        return y
    else:
        while x%y != 0:
            z = x%y
            x = y
            y = z
        else:
            return z
def gcd_list(l):
    g = l[0]
    for i in l[1:]:
        g = gcd(g,i)


    return g
    
N = int(input())
A_list = [int(i) for i in input().split()]
ans = gcd_list(A_list)
print(ans)

Measured memory_bytes: 14224
#include<stdio.h>
 
int hantei(int x){
int i;
if(x<2)
return 0;
else if(x==2)
return 1;
if(x%2==0)
return 0;
for(i=3;i*i<=x;i+=2){
if(x%i==0)
return 0;
}
return 1;
}
 
int main()
{
int a=0;
int n,x,i; 
scanf("%d", &n);
for(i=0;i<n;i++){
scanf("%d", &x);
if( hantei(x) )a++;
}
printf("%d\n",a);
return 0;
}



 

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

int t[3],cnt[3];
int main(){
	int n;scanf("%d",&n);
	rep(i,n){
		int a;scanf("%d",&a);
		t[a%3]++;
	}
	int Max=1;
	for(int i=1;i<3;i++){
		rep(j,3)cnt[j]=t[j];
		if(cnt[i]==0)continue;
		cnt[i]--;
		int res=1,p=i;
		while(1){
			if(res==n)break;
			res++;
			if(p==1){
				if(!cnt[0]&&!cnt[1])break;
				if(cnt[0])cnt[0]--;
				else cnt[1]--,p=2;
			}
			else{
				if(!cnt[0]&&!cnt[2])break;
				if(cnt[0])cnt[0]--;
				else cnt[2]--,p=1;
			}
		}
		Max=max(Max,res);
	}
	printf("%d\n",Max);
}

Measured memory_bytes: 3228
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        long res = 0;
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < N; i++) {
            char[] c = sc.next().toCharArray();
            Arrays.sort(c);
            String s = String.valueOf(c);
            int tmp = map.getOrDefault(s, 0);
            res += tmp;
            map.put(s, tmp + 1);
        }

        System.out.println(res);
    }
}


Measured memory_bytes: 53880
S = input()

start1 = []
start0 = []

ans1 = 0
ans0 = 0

for i in range(len(S)):
    if i%2 == 0:
        start1.append('1')
        start0.append('0')
    else:
        start1.append('0')
        start0.append('1')
        
for i in range(len(S)):
    if S[i] != start1[i]:
        ans1 +=1
    
    if S[i] != start0[i]:
        ans0 +=1
        
print(min(ans1,ans0))

Measured memory_bytes: 4848
#include<iostream>
using namespace std;
int main(){
	long long int n,i,a[4]={0};
	cin>>n;
	string s[n];
	for(i=0;i<n;i++){
		cin>>s[i];
	}
	for(i=0;i<n;i++){
		if(s[i] == "AC"){
			a[0]++;
		}
		else if(s[i] == "WA"){
			a[1]++;
		}
		else if(s[i] == "TLE"){
			a[2]++;
		}
		else{
			a[3]++;
		}
	}
	cout<<"AC x "<<a[0]<<endl;
	cout<<"WA x "<<a[1]<<endl;
	cout<<"TLE x "<<a[2]<<endl;
	cout<<"RE x "<<a[3]<<endl;
	return 0;
}

Measured memory_bytes: 5560
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pll = pair<ll, ll>;
const ll mod = 1000000007;
const ll inf = 1000000000000;//10の12乗

int main() {
    ll n,x,m;
    cin >> n >> x >> m;
    ll xx=x;
    vector<ll> v(m+10,-1);
    ll bgn=-1,syu=-1;
    rep(i,m+2){
        if(v[x]==-1) v[x]=i;
        else{
            bgn=v[x];
            syu=i-v[x];
            break;
        }
        x=(x*x)%m;
    }
    ll ans=0;
    Vl vf(1000000);
    if(bgn+syu>=n){
        rep(i,n){
            ans+=xx;
            xx*=xx;
            xx%=m;
        }
        cout << ans << endl;
        return 0;
    }
    vf[0]=xx;
    rep(i,1000000){
        vf[i+1]=(vf[i]*vf[i])%m;
    }
    rep(i,bgn){
        ans+=vf[i];
    }
    ll sum=0;

    rep(i,syu){
        sum+=vf[bgn+i];
    }
    ll nn=n-bgn;
    ans+=sum*(nn/syu);
    rep(i,nn%syu){
        ans+=vf[i+bgn];
    }
    cout << ans << endl;

}

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

#define MP make_pair
#define PB push_back
#define ALL(x)      (x).begin(),(x).end()
#define REP(i,n)    for(int i=0;i<(n);i++)
#define REP1(i,n)   for(int i=1;i<(n);i++)
#define REP2(i,d,n) for(int i=(d);i<(n);i++)
#define RREP(i,n)   for(int i=(n);i>=0;i--)
#define CLR(a)      memset((a),0,sizeof(a))
#define MCLR(a)     memset((a),-1,sizeof(a))
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
 
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI > VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int,int> PII;
 
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
 
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};


void solve(long long N, long long K, std::vector<long long> t, std::vector<long long> d){
    vector<pair<LL, LL>> sushi;
    REP(i, t.size()){
        sushi.PB(MP(d[i], t[i]));
    }
    sort(ALL(sushi), greater<pair<LL, LL>>());
    
    LL max_ans = 0;
    LL now_ans = 0;
    set<LL> neta;
    vector<priority_queue<LL, vector<LL>, greater<LL>>> sushi_memo;
    priority_queue<LL, vector<LL>, greater<LL>> del_memo;
    REP(i, N+1) { 
        priority_queue<LL, vector<LL>, greater<LL>> que;
        sushi_memo.PB(que);
    }

    REP(i, K) {
        pair<LL, LL> tmp_sushi = sushi[i];
        sushi_memo[tmp_sushi.second].push(tmp_sushi.first);
        neta.insert(tmp_sushi.second);
        now_ans += tmp_sushi.first;
    }

    REP(i, N+1) {
        while(sushi_memo[i].size() > 1) {
            del_memo.push(sushi_memo[i].top());
            sushi_memo[i].pop();
        }
    }

    max_ans = now_ans + neta.size() * neta.size();

    int now_check_index = K;
    while(true) {
        if(del_memo.empty()) break;
        LL min_val = del_memo.top();
        del_memo.pop();
        
        LL add_uma_points = -1;
        for(; now_check_index < sushi.size(); now_check_index++){
            pair<LL, LL> tmp_sushi = sushi[now_check_index];
            if(neta.count(tmp_sushi.second) == 1) continue;
            neta.insert(tmp_sushi.second);
            add_uma_points = tmp_sushi.first;
            break;
        }

        if(add_uma_points == -1) break;

        now_ans = now_ans - min_val + add_uma_points;
        max_ans = max(max_ans, (LL)(now_ans + neta.size() * neta.size()));
    }

    cout << max_ans << endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    std::vector<long long> t(N);
    std::vector<long long> d(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&t[i]);
        scanf("%lld",&d[i]);
    }
    solve(N, K, std::move(t), std::move(d));
    return 0;
}


Measured memory_bytes: 12140
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author ei13333
 */

#include <bits/stdc++.h>

using namespace std;

class TaskC
{
public:
  void solve(istream &in, ostream &out)
  {
    int N, T[100001] = {}, X[100001] = {}, Y[100001] = {};
    in >> N;
    for(int i = 1; i <= N; i++) {
      in >> T[i] >> X[i] >> Y[i];
    }
    for(int i = 1; i <= N; i++) {
      int dist = abs(X[i] - X[i - 1]) + abs(Y[i] - Y[i - 1]);
      if(dist > T[i] - T[i - 1] || ((T[i] - T[i - 1]) - dist) % 2 == 1) {
        out << "No" << endl;
        return;
      }
    }
    out << "Yes" << endl;
  }
};


int main()
{
  TaskC solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return (0);
}


Measured memory_bytes: 1408
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
using namespace std;

int idx;
string s;

int solve(){
  if(isdigit(s[idx])){
    string tmp = "";
    while(isdigit(s[idx])){
      tmp += s[idx];
      idx++;
    }
    return atoi(tmp.c_str()) / 2 + 1;
  }

  vector<int> v;

  while(s[idx] == '['){
    idx++;
    v.push_back(solve());
    idx++;
  }

  sort(v.begin(), v.end());
  int sum = 0;
  for(int i = 0; i < v.size() / 2 + 1; i++){
    sum += v[i];
  }
  return sum;
}

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

  while(T--){
    cin >> s;
    idx = 1;
    cout << solve() << endl;
  }
}

Measured memory_bytes: 1296
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <tuple>

#define rep(i,n) for(ll i=0;i<n;i++)
#define dup(x,y) (((x)+(y)-1)/(y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
//	std::cout<<std::fixed<<std::setprecision(10);

int main() {
	ll N, M;
	cin >> N >> M;
	//vector<pair<int, string>> ps;
	vector<bool> ac(N, false);
	vector<int> wa(N, 0);

	rep(i, M) {
		int p;
		string s;
		cin >> p >> s;
		if(s=="AC") {
			ac[p-1] = true;
		} else {
			if(ac[p-1]==false) { // まだacしていない
				wa[p-1]++;
			}
		}
		//ps.push_back(make_pair(p, s));
	}

	ll ac_sum = 0;
	ll wa_sum = 0;
	rep(i, N) {
		if(ac[i]) {
			ac_sum++;
			wa_sum += wa[i];
		}
	}

	cout << ac_sum << ' ' << wa_sum << endl;

	return 0;
}

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


int main(){
    int n;
    cin >> n;
    vector<pair<int, int>> p(n);
    rep(i,n){
        cin >> p[i].second >> p[i].first;
    }
    sort(p.begin(), p.end());
    ll time = 0;
    rep(i,n){
        time += p[i].second;
        if(time > p[i].first){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 1792
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def mat_dot(A, B):
    """ 行列の積 """

    # 1次元リストが来たら2次元の行列にする
    if not isinstance(A[0], list) and not isinstance(A[0], tuple):
        A = [A]
    if not isinstance(B[0], list) and not isinstance(A[0], tuple):
        B = [[b] for b in B]
    n1 = len(A)
    n2 = len(A[0])
    _ = len(B)
    m2 = len(B[0])
    res=list2d(n1, m2, 0)
    for i in range(n1):
        for j in range(m2):
            for k in range(n2):
                res[i][j] += A[i][k] * B[k][j]
                res[i][j] %= MOD
    return res

def mat_pow(mat, n, k):
    """ n*nの行列matを行列累乗でk乗する """

    res = list2d(n, n, 0)
    for i in range(n):
        res[i][i] = 1
    # 繰り返し二乗法
    while k > 0:
        if k & 1:
            res = mat_dot(res, mat)
        mat = mat_dot(mat, mat)
        k >>= 1
    return res

N, K = MAP()
G = [None] * N
for i in range(N):
    G[i] = LIST()

# 行列累乗でK項先へ
mat = mat_pow(G, N, K)

# グラフを行列としてK乗すれば、長さKのパスの総数が分かる(mat[i][j]=1で頂点i〜jが長さKのパスで繋がってる)
print(sum([sum(row)%MOD for row in mat])%MOD)


Measured memory_bytes: 41420
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=(int)(n);i<i##_len;i++)
#define rep1(i,n)for(int i=1,i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=(int)(n)-1;0<=i;i--)
#define rrep1(i,n) for(int i=(int)(n);1<=i;i--)

#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define all(obj) (obj).begin(),(obj).end() 

#define pcount __builtin_popcount
#define SZ(x) ((int)(x).size())
#define LEN(x) ((int)(x).length())

#define dump(x)  cerr << #x << " = " << (x) << endl;

#define endl "\n"

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

typedef long long lint;

using namespace std;
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int flag=true;
	while(0<a&&0<c){
		if(flag){
			c-=b;
			flag=false;
		}else{
			a-=d;
			flag=true;
		}
	}
	cout<<(!flag?"Yes":"No")<<endl;
}

Measured memory_bytes: 3652
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <climits>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <tuple>
#include <iostream>
#include <deque>
#include <array>
#include <set>
#include <functional>
#include <memory>

class UnionFind {
	std::vector<int> _vec;
public:
	UnionFind(const int size) :_vec(size, -1) {};
	int find(const int a) {
		if (_vec[a] < 0) return a;
		else return _vec[a] = find(_vec[a]);
	}
	int is_same(const int a, const int b) {
		return find(a) == find(b);
	}
	void merge(const int a, const int b) {
		auto ar = find(a);
		auto br = find(b);
		if (ar != br) {
			if (_vec[ar] < _vec[br]) {
				_vec[ar] += _vec[br];
				_vec[br] = ar;
			}
			else {
				_vec[br] += _vec[ar];
				_vec[ar] = br;
			}
		}
	}

};

int main() {
	int n; std::cin >> n;
	std::vector<int> sheet(n);
	for (auto& p : sheet) std::cin >> p;
	std::vector<int> state(n, 0);
	std::vector<std::deque<int>> waiting(n);
	UnionFind union_find(n);
	std::deque<int> deque;
	for (auto i = 0; i < n; ++i) {
		if (state[sheet[i] - 1] == 0) {
			state[sheet[i] - 1] = i + 1;
		}
		else {
			if (waiting[sheet[i] - 1].empty()) {
				deque.push_back(sheet[i] - 1);
			}
			waiting[sheet[i] - 1].push_back(i);
		}
	}
	for (const auto pos : deque) {
		for (auto i = 1; i < n && !waiting[pos].empty(); ++i) {
			if (state[(pos + i) % n] == 0) {
				state[(pos + i) % n] = waiting[pos].front() + 1;
				waiting[pos].pop_front();
			}
			else if (!waiting[(pos + i) % n].empty()) {
				if (waiting[(pos + i) % n].size() < waiting[pos].size()) {
					while (!waiting[(pos + i) % n].empty()) {
						auto last = waiting[(pos + i) % n].back();
						waiting[(pos + i) % n].pop_back();
						waiting[pos].push_front(last);
					}
					waiting[(pos + i) % n].shrink_to_fit();
					waiting[(pos + i) % n].swap(waiting[pos]);
				}
				else {
					while (!waiting[pos].empty()) {
						auto front = waiting[pos].front();
						waiting[pos].pop_front();
						waiting[(pos + i) % n].push_back(front);
					}
					waiting[pos].shrink_to_fit();
				}
			}
		}
	}
	for (const auto s : state) {
		std::cout << s << '\n';
	}
}


Measured memory_bytes: 73820
N = int(input())
Vs = list(map(int,input().split()))
Cs = list(map(int,input().split()))


def calc(v_sum,c_sum,index):
    if index == N:
        return v_sum - c_sum

    return max(calc(v_sum + Vs[index], c_sum + Cs[index], index + 1),calc(v_sum, c_sum, index + 1))


print(calc(0,0,0))


Measured memory_bytes: 3060
use whiteread::parse_line;

fn main() {
    let n: usize = parse_line().unwrap();
    let mut i: usize = 0;

    let mut v: Vec<usize> = Vec::new();

    while i < n {
        let t: usize = parse_line().unwrap();
        v.push(t);
        i = i + 1;
    }

    v.sort();
    v[n-1] = v[n-1] / 2;
    println!("{}", v.iter().cloned().fold(0, |sum, i| sum + i));
}


Measured memory_bytes: 2148
// AOJ 1168: Off Balance
// 2017.12.22 bal4u@uu

#include <stdio.h>
//#include <stdlib.h>
#include <string.h>

#define MAX 602

/* UNION-FIND library */
int p[MAX], rank[MAX];

void make_set(int x) { p[x] = x, rank[x] = 0; }
void link(int x, int y) {
	if (rank[x] > rank[y]) p[y] = x;
	else { p[x] = y; if (rank[x] == rank[y]) rank[y] = rank[y] + 1;	}
}
int find_set(int x) { if (x != p[x]) p[x] = find_set(p[x]);	return p[x]; }
void union_set(int x, int y) { link(find_set(x), find_set(y)); }

char map[60][12]; int w, h;
int  rel[60][10];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

typedef struct { int used, m, mx, xl, xr, par, ch, sib; } T;
T tbl[100]; int sz;
int tr[MAX];

void mktree(int r, int c, int this)
{
	int k, p, s;

	if (r == h-1) {
		if (map[r][c] == '.') return;
		if (tbl[this].xl < 0) tbl[this].xl = c;
		else if (c < tbl[this].xl) tbl[this].xl = c;
		if (c > tbl[this].xr) tbl[this].xr = c;
		return;
	}
	if (map[r+1][c] == '.') return;

	k = rel[r+1][c];
	if ((p = tr[k]) == this) return;

	if (tbl[this].xl < 0) tbl[this].xl = c;
	else if (c < tbl[this].xl) tbl[this].xl = c;
	if (c > tbl[this].xr) tbl[this].xr = c;
	if (tbl[this].par) return;

	if ((s = tbl[p].ch) == 0) tbl[p].ch = this;
	else {
		while (tbl[s].sib) s = tbl[s].sib;
		tbl[s].sib = this;
	}
	tbl[this].par = p;
}

int check(int this)
{
	int par, ch, sib;
	double x;

	if (tbl[this].used) return 1;
	ch = tbl[this].ch;
	if (ch) { if (!check(ch)) return 0; }

	sib = tbl[this].sib;
	while (sib) {
		if (!check(sib)) return 0;
		sib = tbl[sib].sib;
	}

	x = 0.5+(double)tbl[this].mx / tbl[this].m;
	if (x <= tbl[this].xl || tbl[this].xr+1 <= x) return 0;

	par = tbl[this].par;
	tbl[par].m  += tbl[this].m;
	tbl[par].mx += tbl[this].mx;
	tbl[this].used = 1;
	return 1;
}

int max;

int main()
{
	int r, c, r2, c2, i, k;
	T *tp;

	while (scanf("%d%d", &w, &h) && w > 0) {

		memset(rank, 0, sizeof(rank));
		for (i = 0, r = 0; r < h; r++) {
			scanf("%s", map[r]);
			for (c = 0; c < w; c++) p[i] = i, rel[r][c] = i, i++;
		}

		for (r = 0; r < h; r++) for (c = 0; c < w; c++) {
			if (map[r][c] == '.') continue;
			for (i = 0; i < 4; i++) {
				r2 = r + mv[i][0], c2 = c + mv[i][1];
				if (r2 >= 0 && r2 < h && c2 >= 0 && c2 < w) {
					if (map[r2][c2] == map[r][c]) union_set(rel[r][c], rel[r2][c2]);
				}
			}
		}

		sz = 1,	memset(tbl, 0, sizeof(tbl));
		memset(tr, -1, sizeof(tr));
		for (r = h-1; r >= 0; r--) for (c = 0; c < w; c++) {
			if (map[r][c] == '.') continue;
			k = rel[r][c] = find_set(rel[r][c]);
			if (tr[k] < 0) i = tr[k] = sz++, tp = tbl+i, tp->xl = -1;
			else           i = tr[k],        tp = tbl+i;
			tp->m++, tp->mx += c;
			mktree(r, c, i);
		}
		tbl[1].par = 1;

		puts(check(1) ? "STABLE": "UNSTABLE");
	}
	return 0;
}

Measured memory_bytes: 1748
// #include <atcoder/all>
#include <atcoder/dsu>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
#define all(x) (x).begin(),(x).end()
#define rp(i,s,e) for(int i=(int)(s);i<(int)(e);++i)
#define irp(i,s,e) for(int i=(int)(s);i>(int)(e);--i)


int main(){
	int N,M; cin>>N>>M;
	vector<vi> AB(M, vi(2));
	for(auto& ab:AB) cin>>ab[0]>>ab[1];
	// rp(i,0,M) cout<<AB[i][0]<<" "<<AB[i][1]<<"\n";
	ll ans = 0;
	dsu d(N);

	for(auto ab:AB){
		d.merge(ab[0]-1, ab[1]-1);
	}

	set<int> l;
	rp(i,0,N){
		l.insert(d.leader(i));
	}

	ans = l.size();

	cout<< ans-1 <<endl;
}


Measured memory_bytes: 9772
n = input()
ans = 0
if len(n) % 2 == 0:
    length = len(n) - 1
    for i in range(length, -1, -2):
        if i == 1:
            ans += 9
        else:
            plus = '9' * i
            minus = '9' * (i - 1)
            ans += (int(plus) - int(minus))
    print(ans)
else:
    if int(n) <= 9:
        print(int(n))
    else:
        length = len(n)
        b = '9' * (length-1)
        c = int(n) - int(b)
        ans += c
        for i in range(length-2, -1, -2):
            if i == 1:
                ans += 9
            else:
                plus = '9' * i
                minus = '9' * (i-1)
                ans += (int(plus) - int(minus))
        print(ans)


Measured memory_bytes: 38256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
typedef long long unsigned int ll;

struct data_t {
    int key1;
    int key2;
};

bool cmp(const data_t& left, const data_t& right) {
    return (left.key2 < right.key2) ;
}

int main(){

    int n;
    int time=0;
    int res=0;

    cin >> n;

    struct data_t data[n];
    // int a[n],b[n];

    for(int i=0;i<n;i++){
        // cin >> a[i] >> b[i];
        cin >> data[i].key1 >> data[i].key2;
    }
    
    std::sort(data, data + n, cmp);

    //そーとを早くする必要あり
    // for(int i=0;i<n;i++){
    //     for(int j=i+1;j<n;j++){
    //         if(b[j]<b[i]){
    //             int temp = b[i];
    //             b[i] = b[j];
    //             b[j] = temp;
    //             temp = a[i];
    //             a[i] = a[j];
    //             a[j] = temp;
    //         }
    //     }
    // }
    

    for(int i=0;i<n;i++){
        time += data[i].key1;
        if(time>data[i].key2)res=1;
    }
    // for(int i=0;i<n;i++){
    //       cout << data[i].key1 << " " << data[i].key2<< endl;
    // }

    if(res==0)cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

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

int main(){
  int N;
  cin >> N;
  int64_t A[N+1];
  map<int64_t,int> B;
  int64_t ans=0;
  int64_t mn=0;
  for(int i=1; i<N+1; i++) {
    cin >> A[i];
    ans += B[i-A[i]];
    B[i+A[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 27776
#pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T>
using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T>
constexpr bool chmax(S &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename S, typename T>
constexpr bool chmin(S &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T>
bool print_(const T &a) {
  cout << a;
  return true;
}
template <typename T>
bool print_(const vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) {
      cout << " ";
    }
  }
  return false;
}
template <typename T>
bool print_(const vector<vector<T>> &vv) {
  for (auto &v : vv) {
    for (auto &a : v) {
      cout << a;
      if (&a != &v.back()) {
        cout << " ";
      }
    }
    if (&v != &vv.back()) {
      cout << "\n";
    }
  }
  return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&... tail) {
  bool f = print_(head);
  if (sizeof...(tail) != 0) {
    cout << (f ? " " : "\n");
  }
  print(forward<Tail>(tail)...);
}
#pragma endregion

void Pr(bool f){
  cout << (f ? "Yes" : "No") << "\n";
  exit(0);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll w, h, x, y;
  cin >> w >> h >> x >> y;
  ll b = 0;
  if(x*2==w&&y*2==h) b = 1;
  print((ld)w*h/2, b);
}


Measured memory_bytes: 3784
#include <bits/stdc++.h>
#include <vector>
#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 INF 2e9
#define ALL(v) v.begin(), v.end()

using namespace std;
typedef long long ll;

int main(){
    int n;
    cin >> n;
    if((n%1000)!=0){
        cout << 1000-(n%1000) << endl;
    }
    else cout << 0 << endl;
    return 0;
}

Measured memory_bytes: 3608