akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int i = (s);i <= (e);i++)

int A,B,C;

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

  int a = A & 1;
  int b = B & 1;
  int c = C & 1;
  int res = 0;

  if((a && b && c) || (!a && !b && !c)){

  }
  else{
    res++;
    if(a == b && a != c){
      A++;
      B++;
    }
    else if(a == c && a != b){
      A++;
      C++;
    }
    else if(b == c && b != a){
      B++;
      C++;
    }
  }

  int MAX = max(A,max(B,C));
  res += (MAX - A) / 2 + (MAX - B) / 2 + (MAX - C) / 2;
  cout <<res << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main() {
	int n; cin >> n;
	map<int, int> record;
	for (int i = 0; i < n; i ++) {
		int t; cin >> t;
		record[t] ++;
	}
	map<int, int> :: iterator iter = record.begin();
	
	int ans = 0;
	for (; iter != record.end(); iter ++) {
		if (iter->first > iter->second) 
			ans += iter->second;
		else if (iter->first < iter->second)
			ans += iter->second - iter->first;	
	}
	
	cout << ans << endl;
	return 0;
} 

Measured memory_bytes: 4736
def resolve():
	modu = 1000000007
	n, m = map(int, input().split())
	a = [False]*n
	for _ in range(m):
		a[int(input())] = True
	dp = [0]*(n+1)
	dp[0], dp[1] = 1, 1
	for i in range(2, n+1):
		if i < n and a[i]:
			continue
		multi = []
		if not a[i-1]:
			multi.append(dp[i-1])
		if not a[i-2]:
			multi.append(dp[i-2])
		dp[i] = sum(multi) % modu
	print(dp[-1])
resolve()

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

int main() {
	int k;
	cin >> k;
	
	int val = 7%k;
	
	for(int i = 1; i < 1e7; ++i){
		if(val == 0) {
			cout << i << endl;
			return 0;
		}
		val = (val * 10 + 7)%k;
	}
	
	cout << -1 << endl;
	return 0;
}

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

public class Main{
  public static void main(String[] args)throws Exception{
    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
    try{
      String S = sc.readLine();
      List<String> list = new LinkedList<>();
      list.add(S);
      int N = Integer.parseInt(sc.readLine());
      boolean f = true;
      for(int i = 0; i < N; i++){
        String[] T = sc.readLine().split(" ");
        int order = Integer.parseInt(T[0]);
        if(order == 1){
          f = !f;
        }else{
          int o2 = Integer.parseInt(T[1]);
          if(o2 == 1){
            if(f){
              list.add(0, T[2]);
            }else{
              list.add(T[2]);
            }
          }else{
            if(f){
              list.add(T[2]);
            }else{
              list.add(0,T[2]);
            }
          }
        }
      }
      StringBuilder sb = new StringBuilder();
      for(String s : list){
        sb.append(s);
      }
      System.out.println(f ? sb.toString() : sb.reverse().toString());
    }catch(Exception e){
    }
  }
}


Measured memory_bytes: 71228
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int N = cin.nextInt();
        long Y = cin.nextLong();

        int x = -1, y = -1, z = -1;

        loop:
        for (int a = 0; a <= N; a++) {
            for (int b = 0; b <= N - a; b++) {
                int c = N - a - b;
                if (10000 * a + 5000 * b + 1000 * c == Y) {
                    x = a;
                    y = b;
                    z = c;
                    break loop;
                }
            }
        }

        System.out.printf("%d %d %d\n", x, y, z);
    }
}

Measured memory_bytes: 21844
N, M = map(int, input().split())
A = [i for i in input().split()]

# dp[i] = i本使ってできる最大の数

def mx(a, b):
    if a is not None and b is None:
        return a
    elif a is None and b is not None:
        return b
    if len(a) != len(b):
        if len(a) > len(b):
            return a
        else:
            return b
    return max(a, b)

# xを作るために使うマッチの本数
h = {'1':2, '2':5, '3':5, '4':4, '5':5, '6':6, '7':3, '8':7, '9':6}

dp = [None] * (N + 1)
dp[0] = ''

for i in range(N + 1):
    for a in A:
        if i - h[a] < 0: continue
        if dp[i-h[a]] is None and dp[i] is None:
            continue
        try:
            dp[i] = mx(dp[i-h[a]] + a, dp[i])
        except:
            continue

print(dp[N])

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

using namespace std;

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

#define endl "\n"

const ll INF = 1e15;
const int MOD = 1e9 + 7;
const double EPS = 1e-7;
const double PI = acos(-1.0);

const int N = 2e5 + 5;

ll n, a[N], b[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);

    cin >> n;

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

    for (int i = 0; i < n; ++i)
        cin >> b[i];

    ll ans = 0;
    for (ll T = 1; T < (1ll << 30); T <<= 1) {
        vector<ll> curA(n), curB(n);

        for (int i = 0; i < n; ++i)
            curA[i] = a[i] % (2 * T);

        for (int i = 0; i < n; ++i)
            curB[i] = b[i] % (2 * T);

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

        ll cnt = 0;
        for (int i = 0; i < n; ++i) {
            cnt += upper_bound(curB.begin(), curB.end(), 2 * T - 1 - curA[i]) - lower_bound(curB.begin(), curB.end(), T - curA[i]);
            cnt += upper_bound(curB.begin(), curB.end(), 4 * T - 1 - curA[i]) - lower_bound(curB.begin(), curB.end(), 3 * T - curA[i]);
        }

        if (cnt & 1)
            ans += T;
    }

    cout << ans << endl;
}


Measured memory_bytes: 6572
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <functional>
#include <utility>
#include <queue>
#include <vector>
#include <string>
#include <stack>
using namespace std;
typedef long long int ll;
const ll  MOD = 1000000007;
typedef pair<ll,ll> P;

ll N;
ll a[1010];
ll ans=1;
int main(){
  cin >> N;
  for(int i=2;i<=N;i++){
    ll t = i;
    for(int j=2;j<=N;j++){
      if(i<j) break;
      while(t%j == 0){
        a[j]++;
        t/=j;
      }
    }
  }
  for(int i=2;i<=N;i++){
    ans = (ans*(a[i]+1)) % MOD;
  }
  cout << ans << endl;
}



Measured memory_bytes: 636
#include <cstdio>
#include <iostream>
using namespace std;
int N, Q, arr[100005], Sum[100005];
char S[100005];

int main()
{
    scanf("%d %d %s", &N, &Q, S);
    for(int i = 0; i < N; i++)
    {
        if(S[i] == 'A' && S[i + 1] == 'C')
        {
            arr[i] = 1;
            i++;
        }
    }
    for(int i = 1; i <= N; i++)
    {
        Sum[i] = Sum[i - 1] + arr[i - 1];
    }
    for(int i = 0; i < Q; i++)
    {
        int l, r, temp = 0;
        scanf("%d %d", &l, &r);
        if(arr[r - 1] == 1)
        {
            temp = 1;
        }
        printf("%d\n", Sum[r] - Sum[l - 1] - temp);
    }
}

Measured memory_bytes: 1664
K, N = map(int, input().split())
*A, = map(int, input().split())
A = A + [K+A[0]]
chk = [0] * N
for i in range(N):
    chk[i] = A[i+1] - A[i]
chk.sort(reverse=True)
print(sum(chk[1:]))

Measured memory_bytes: 26444
def main():
    from heapq import heappop, heappush
    INF = 1 << 30

    n, m, r, *E = map(int, open(0).read().split())

    G = [[] for _ in [0]*n]
    for s, t, w in zip(*[iter(E)]*3):
        G[s].append((t, w))

    # Shortest Path Faster Algorithm (SPFA)
    D = [INF]*n
    count = [0]*n
    in_queue = [False]*n
    q = []
    
    D[r] = 0
    count[r] = 1
    in_queue[r] = True
    heappush(q, (0, r))

    while q:
        _, s = heappop(q)
        in_queue[s] = False

        for t, w in G[s]:
            new_d = D[s] + w
            if new_d >= D[t]:
                continue
            D[t] = new_d

            count[t] += 1
            if count[t] >= n:
                print('NEGATIVE CYCLE')
                exit()

            if not in_queue[t]:
                in_queue[t] = True
                heappush(q, (new_d, t))

    print(*['INF' if d == INF else d for d in D], sep='\n')


if __name__ == '__main__':
    main()



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

int main(){
    int n;
    cin >> n;
    vector<int> p(n);
    int count = 0; 
    for(int i = 0; i < n; i++){
        cin >> p.at(i);
        if(p.at(i) != i + 1){
            count += 1;
        }
    }
    if(count <= 2){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

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

int main(){
	int x;
  	cin >> x;
  	
  	while(1){
    	bool ok = true;
      	for(int i = 2; i < x; i++){
        	if(x % i == 0){
            	ok = false;
              	break;
            }
        }
      
      	if(ok) break;
      	x++;
    }
  
  	cout << x << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String target = "keyence";
		String S = sc.nextLine();
		for (int i = 0; i < target.length(); i++) {
			if (S.startsWith(target.substring(0, i)) &&
					S.endsWith(target.substring(i))) {
				System.out.println("YES");
				return;
			}
		}
		System.out.println("NO");
	}
}


Measured memory_bytes: 24916
#include <bits/stdc++.h>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;

main() {
    int64_t n, w;
    cin >> n >> w;

    // weight, value
    vector<pair<int64_t, int64_t>> items(n);
    for (auto&& ii : items) {
        cin >> ii.first >> ii.second;
    }

    vector<int64_t> ans(w + 1);
    for (const auto& item : items) {
        auto weight = item.first;
        auto value = item.second;
        for (auto ww : irange(w, -1L, -1L)) {
            auto www = ww + weight;
            if (www <= w && ans[www] < ans[ww] + value) {
                ans[www] = ans[ww] + value;
            }
        }
    }

    cout << *max_element(ans.begin(), ans.end()) << endl;
}

Measured memory_bytes: 1024
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue> 
#define rg register
using namespace std;
typedef long long LL;
template<typename T> inline void read(T &x){
	x=0; T f=1; char ch=getchar();
	while(ch<'0'||ch>'9') f=ch=='-'?-1:1, ch=getchar();
	while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^'0'), ch=getchar();
	x=x*f;
}
int main(){
	char s[1000];
	scanf("%s",s);
	int len=strlen(s);
	printf("%s",s);
	if(s[len-1]=='s') printf("es");
	else printf("s");
	return 0;
}

Measured memory_bytes: 3836
from collections import deque
N, M = list(map(int, input().split()))
S = [set() for _ in range(N)]
for _ in range(M):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    S[a].add(b)
    S[b].add(a)
# print(S)

parents = [-1]*N
parents[0] = 0
L = deque([0])
while L:
    k = L.pop()
    s=S[k]
    for l in s:
        if parents[l] < 0:
            L.appendleft(l)
            parents[l] = k
    # print(L)

if -1 in parents:
    print('No')
else:
    print('Yes')
    for i in range(1, N):
        print(parents[i]+1)

Measured memory_bytes: 124056
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define srt(i) sort(i.begin(), i.end())
#define rvt(i) sort(i.begin(), i.end(), greater<int>())

vector<int> sieve(int n) {
  vector<int> res(n,0);
  for (int i = 2; i*i < n; ++i) {
    if (res[i] < i) continue;
    for (int j = i*i; j < n; j += i)
      if (res[j] == j) res[j] = i;
  }
  return res;
}

int main()
{
    int64_t a, v, b, w, t;
    cin >> a >> v >> b >> w >> t;

    string ans = "NO";
    if( v>w && abs(b-a) <= t*(v-w)) ans = "YES";
    
    cout << ans << endl;
}


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

#define rep(i,j,n) for(int i=(j);i<(n);i++)
#define erep(i,j,n) for(int i=(j);i<=(n);i++)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(),i.rend()
#define INF 1e9
const int mod = 1e9+7;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef long long i64;

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

  i64 n, k;
  cin >> n >> k;
  i64 ans = (n / k) * (n / k) * (n / k);
  if(k % 2 == 0) {
    i64 cou = 0;
    erep(i, 1, n) {
      if(i % k == k / 2) cou++;
    }
    ans += cou * cou * cou;
  }
  cout << ans << endl;
}


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

using namespace std ;

#define int long long
#define N 300000
#define inf 2147483747000

int tot , cnt , n , m , sum , root , gnum , p , ans ;
int nex[2*N] , fire[N] , to[2*N] , len[2*N] ;
int f[N] , siz[N] , vis[N] , dis[N] , val[N] ;
int fa[N] ;

struct edge{
    int u , v , w ;
}A[13 * N] ;

bool cmp(edge a , edge b){ return a.w < b.w ; }

void add(int u , int v , int w){
    nex[++tot] = fire[u] ;
    fire[u] = tot ;
    to[tot] = v ;
    len[tot] = w ;
    return ;
}

void Get_root(int x , int fr){
    f[x] = 0 ; siz[x] = 1 ;
    for(int i = fire[x] ; i ; i = nex[i] ){
        int v = to[i] ;
        if( v == fr || vis[v] ) continue ;
        Get_root( v , x ) ;
        siz[x] += siz[v] ;
        f[x] = max( f[x] , siz[v] ) ;
    }
    f[x] = max( f[x] , sum - siz[x] ) ;
    if( f[x] < f[root] ) root = x ;
    return ;
}

void Get_min(int x , int fr , int dep){
    dis[x] = dep + val[x] ;
    if( dis[x] < gnum ) gnum = dep + val[x] , p = x ;
    for(int i = fire[x] ; i ; i = nex[i] ){
        int v = to[i] ;
        if( vis[v] || v == fr ) continue ;
        Get_min( v , x , dep + len[i] ) ;
    } return ;
}

void Link(int u , int fr ){
    A[++cnt].u = u , A[cnt].v = p , A[cnt].w = gnum + dis[u] ;
    for(int i = fire[u] ; i ; i = nex[i] ){
        int v = to[i] ;
        if( vis[v] || v == fr ) continue ;
        Link( v , u ) ;
    }
}

void solve(int now){
    vis[now] = 1 ; p = 0 , gnum = inf ;
    Get_min( now , 0 , 0 ) ; Link( now , 0 ) ;
    for(int i = fire[now] ; i ; i = nex[i] ){
        int v = to[i] ; if( vis[v] ) continue ;
        root = 0 ; sum = siz[v] ; 
        Get_root( to[i] , now ) ;
        solve( root ) ;
    }
}

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

void kru(){
    sort( A + 1 , A + cnt + 1 , cmp ) ;
    for(int i = 1 ; i <= n ; i++ ) fa[i] = i ;
    for(int i = 1 ; i <= cnt ; i++ ){
        int u = A[i].u , v= A[i].v ;
        int x = find( u ) , y = find( v ) ;
        if( x == y ) continue ;
        ans += A[i].w ;
        fa[x] = fa[y] ;
    }
    return ;
}

signed main()
{
    scanf("%lld" , &n ) ;
    for(int i = 1 ; i <= n ; i++ ) scanf("%lld" , &val[i] ) ;
    for(int i = 1 ; i <= n - 1 ; i++ ){
        int u , v , w ;
        scanf("%lld%lld%lld" , &u , &v , &w ) ;
        add( u , v , w ) ; add( v , u , w ) ;
    }
    f[0] = n ; root = 0 ; sum = n ;
    Get_root( 1 , 0 ) ;
    solve( root ) ;
    kru() ;
    printf("%lld\n" , ans ) ;
    return 0 ;
}

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

using namespace std;
#define ll long long
ll MOD=1000000007;
ll n,m,ar[100005];
ll mem[100005];

ll solve(ll cur){
    if(cur==n){
        return 1;
    }
    if(cur>n){
        return 0;
    }
    if(ar[cur]){
        return 0;
    }
    if(mem[cur]!=-1){
        return mem[cur];
    }
    ll ch1=solve(cur+1)%MOD;
    ll ch2=solve(cur+2)%MOD;
    return mem[cur]=(ch1+ch2)%MOD;
}

int main() {
    //freopen("262144.in", "r", stdin);
    //freopen("262144.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    memset(mem,-1,sizeof mem);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int x;
        cin>>x;
        ar[x]=1;
    }
    cout<<solve(0)%MOD<<endl;
    return 0;
}


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

using ll = long long;
using P = pair<int, int>;
#define rep1(i,n) for (int i=1; i<=n; i++)
#define rep0(i,n) for (int i=0; i<n; i++)
#define zarray(x,n) x[n+1]; rep1(i,n) {x[i]=0;} 

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

    int i = 0;
    double x = 0;
    double y = 0;
    double tt = 0;
    for (;i<=360; i++) {
        double x2 = x + cos((tt+90)/180 * M_PIf64);
        double y2 = y + sin((tt+90)/180 * M_PIf64);
        if (abs(x2) < 0.0001 && abs(y2) < 0.0001) break;
        x=x2;
        y=y2;
        tt += t;
    }
    cout << i+1 << endl;
}


Measured memory_bytes: 3972
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <iomanip>
#include <locale>

using namespace std;

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

	locale l = locale::classic();
	char* c = (char*)S.c_str();
	c[K - 1] = tolower(c[K - 1], l);

	cout << S << endl;

	return 0;
}

Measured memory_bytes: 256
s = input()
t = input()

result = ''
for i in range(len(s)-len(t),-1,-1):
  mth = 1
  for j in range(len(t)):
    if s[i+j] != t[j]:
      if s[i+j] == '?':
        pass
      else:
        mth = 0
        break
  if mth == 1:
#    print("i",i)
    result = s[:i]+t+s[i+len(t):]
    print(result.replace('?','a'))
    exit(0)

print("UNRESTORABLE")


Measured memory_bytes: 3060
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod = 1000000007;
struct mint{
	ll x;
	mint(ll x = 0):x((x%mod+mod)%mod){}
	mint operator-() const{return mint(-x);}
	mint& operator+=(const mint a){
		if((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a){
		if((x += mod-a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a){
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const{
		mint res(*this);
		return res+=a;
	}
	mint operator-(const mint a) const{
		mint res(*this);
		return res-=a;
	}
	mint operator*(const mint a) const{
		mint res(*this);
		return res*=a;
	}
	mint pow(ll t) const{
		if(!t) return 1;
		mint a = pow(t>>1);
		a *= a;
		if(t&1) a *= *this;
		return a;
	}
	mint inv() const{
		return pow(mod-2);
	}
	mint& operator/=(const mint a){
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const{
		mint res(*this);
		return res/=a;
	}
	bool operator==(const mint a){
		return x == a.x;
	}
};

istream& operator>>(istream& is, const mint& a){return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a){return os << a.x;}

// 2^n
mint f(int n){
	if(n == 0) return 1;
	mint x = f(n/2);
	x *= x;
	if(n%2 == 1) x *= 2;
	return x;
}

// nCa
mint choose(int n, int a){
	mint x = 1, y = 1;
	rep(i,a){
		x *= n-i;
		y *= i+1;
	}
	return x / y;
}

struct Combination{
	vector<mint> fact, ifact;
	Combination(int n):fact(n+1),ifact(n+1){
		assert(n < mod);
		fact[0] = 1;
		for(int i = 1; i <= n; i++) fact[i] = fact[i-1]*i;
		ifact[n] = fact[n].inv();
		for(int i = n; i >= 1; i--) ifact[i-1] = ifact[i]*i;
	}
	mint operator()(int n, int k){
		if(k < 0 || k > n) return 0;
		return fact[n]*ifact[k]*ifact[n-k];
	}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
	int n;
	string s;
	cin >> n >> s;
	mint ans = 1;
	ll cnt = 0;
	rep(i,2*n){
		if(cnt%2 == 0){
			if(s[i] == 'W'){
				if(cnt == 0){
					ans = 0;
					break;
				}
				ans *= cnt;
				--cnt;
			}
			else{
				++cnt;
			}
		}
		else{
			if(s[i] == 'W'){
				++cnt;
			}
			else{
				if(cnt == 0){
					ans = 0;
					break;
				}
				ans *= cnt;
				--cnt;
			}
		}
	}
	if(ans == 0 || cnt > 0){
		cout << 0 << endl;
		return 0;
	}
	for(int i = 1; i <= n; ++i){
		ans *= i;
	}
	cout << ans.x << endl;
    return 0;
}

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

int main(){
    string a, b, c;
    cin >> a >> b >> c;
    if(a[a.length() - 1] == b[0] && b[b.length() - 1] == c[0]){
        cout << "YES";
    }else{
        cout << "NO";
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
template <typename T> using ord_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1e5 + 123;
int n, a[N], b[N], in[N];
set <array <int, 2>> st;
set <int> s;

void del(int v) {
	st.erase({-in[v], v});
	s.erase(v);
	if (s.find(a[v]) != s.end()) {
		st.erase({-in[a[v]], a[v]});
		in[a[v]]--;
		st.insert({-in[a[v]], a[v]});
	}
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	#ifdef LOCAL
		freopen("input.txt", "r", stdin);
	#endif
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		in[a[i]]++;
	}
	for (int i = 1; i <= n; i++) {
		st.insert({-in[i], i});
		s.insert(i);
	}
	for (int i = 1; i <= n - 1; i++) {
		int V = n - i + 1;
		if (-(*st.begin())[0] == V - 1 && -(*next(st.begin()))[0] == V - 1)
			return cout << -1, 0;
		if (V == 3) {
			array <int, 3> t;
			int m = 0;
			for (int j : s) t[m++] = j;
			for (int j = 0; j < 3; j++) {
				int v = t[j], u = t[(j + 1) % 3], w = t[(j + 2) % 3];
				if (a[b[i - 1]] != v && in[u] < V - 1 && in[w] < V - 1 && (a[u] != w || a[w] != u)) {
					b[i] = v;
					break;
				}
			}
			if (!b[i]) return cout << -1, 0;
		}	else {
			if (-(*st.begin())[0] == V - 1) b[i] = (*st.begin())[1];
			else {
				b[i] = *s.begin();
				if (a[b[i - 1]] == b[i])
					b[i] = *next(s.begin());
			}
		}
		if (a[b[i - 1]] == b[i]) return cout << -1, 0;
		del(b[i]);
	}
	for (int i = 1; i <= n - 1; i++)
		cout << b[i] << ' ';
	cout << *s.begin();
}


Measured memory_bytes: 14208
/* 北さん参考 */
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF 2e9
#define INF_LL 1LL<<60
#define ll long long

typedef pair<ll, ll> P;

int main(){
    int h, w, n;
    cin >> h >> w >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    int ans[h][w];

    int idx = 1;
    int kosu = 0;

    REP(i, h) REP(j, w) {
        if(i % 2 == 0) {
            ans[i][w-j-1] = idx;
        } else {
            ans[i][j] = idx;
        }
        kosu++;
        if(a[idx-1] == kosu) {
            kosu = 0, idx++;
        }
    }

    REP(i, h) {
        REP(j, w) cout << ans[i][j] << " ";
        cout << endl;
    }

    return 0;
}


Measured memory_bytes: 384
a, b, c, d = map(int, input().split())
d_ac = abs(c - a)
d_ab = abs(b - a)
d_bc = abs(c - b)
if d_ac <= d:
    print("Yes")
else:
    if d_ab <= d and d_bc <= d:
        print("Yes")
    else:
        print("No")



Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const ll MOD = (ll)(1e9 + 7);
const int INF = (int)1e9;
const ll LINF = (ll)4e18;
const double PI = acos(-1.0);

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define possible(n) cout << ((n) ? "possible" : "impossible") << endl
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << endl;

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << setiosflags(ios::fixed);
  ll a, b, k;
  cin >> a >> b >> k;
  if (a - k < 0)
  {
    k -= a;
    a = 0;
    b = max(b - k, 0LL);
  }
  else
  {
    a -= k;
  }
  cout << a << " " << b << endl;
  return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
const int maxn = 555;
char grid[maxn][maxn];
void check(int h, int w, int d){
    for(int e = 0; e < h; e++)
        for(int f = 0; f < w; f++){
            for(int g = 0; g < h; g++)
                for(int t = 0; t < w; t++){
                    if(abs(g - e) + abs(t - f) == d){
                        assert(grid[e][f] != grid[g][t]);
                    }
                }
        }
}
int M(){
    int h, w, d;
    if(!(cin >> h >> w >> d)) return 0;
    char par[] = {'R', 'G', 'B', 'Y'};
    if(d & 1){
        for(int e = 0; e < h; e++)
            for(int f = 0; f < w; f++)
                grid[e][f] = par[(e + f)%4];
    } else {
        for(int e = 0; e < h; e++)
            for(int f = 0; f < w; f++){
                int value = 0;
                value |= (e + f) / d % 2;
			    value |= (h - 1 - e + f) / d % 2 * 2;
                grid[e][f] = par[value];
            }
    }
    for(int e = 0; e < h; e++){
        for(int f = 0; f < w; f++){
            printf("%c", grid[e][f]);
        }
        printf("\n");
    }
    // check(h, w, d);
    return 1;
}
int main(){
    while(M());
    return 0;
}


Measured memory_bytes: 768
#include<iostream>
#include<math.h>
using namespace std;
#define ll long long 

ll N,P;

int solve(){
	ll ans=1;
	ll store;
	for(int i=1;pow(i,N)<=P; i++){
		store = static_cast<ll>(pow(i,N));
		if(P % store == 0){
			ans = i;
		}
	}
	cout << ans << endl;
	return 0;
}


int main(){
	cin >> N >> P;
	if(N == 1){
		cout << P << endl;
		return 0;
	}
	solve();
	return 0;
}



Measured memory_bytes: 256
S = raw_input()
K = input()

ans = []
for i in range(len(S)):
    for j in range(1,K+1):
        if i+j <= len(S):
            if S[i:i+j] not in ans:
                ans.append(S[i:i+j])

ans.sort()

print ans[K-1]

Measured memory_bytes: 3576
#include<cstdio>
#include<cstring>
using namespace std;
char lty[1000000001] = { 0 };
long long int sum = 0, number = 0, ten = 1;
int main() {
	scanf("%s", lty);
	for (int i = strlen(lty) - 1; i >= 0; --i) {
		sum = sum - '0' + lty[i];
		number += (lty[i] - '0')*ten;
		ten *= 10;
	}
	if (number % sum == 0)printf("Yes");
	else printf("No");
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define mod 1000000007

int main() {
    int n,k,i,j;
    cin>>n>>k;
    int a[n];
    for(i=0;i<n;i++)
    cin>>a[i];
    sort(a,a+n);
    int dp[100005];
    dp[0]=0;
    for(i=1;i<=k;i++)
    {
        int f=0;
        for(j=0;j<n;j++)
        {
            if(i-a[j]<0)
            break;
            else if(dp[i-a[j]]==0)
            {
                f=1;
                dp[i]=1;
                break;
            }
        }
        if(f==0)
        dp[i]=0;
    }
    if(dp[k])
    cout<<"First\n";
    else
    cout<<"Second\n";
	return 0;
}

Measured memory_bytes: 640
n = int(input())
s = str(input())
r = s.count('R')
g = s.count('G')
b = s.count('B')
ans = r*g*b

for k in range(1, n//2+1):
    for i in range(n):
        if i+2*k >= n:
            break
        if s[i] != s[i+k] and s[i] != s[i+2*k] and s[i+k] != s[i+2*k]:
            ans -= 1
print(ans)


Measured memory_bytes: 9204
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
mod = 1000000000 + 7
def func(n):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= mod

    return r
N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    print(int((func(A)*func(A-1))%mod))
else:
    print(int(2*func(N)*func(N))%mod)




Measured memory_bytes: 3064
<?php

while ($stdin = fgets(STDIN)) {
    $line[] = trim($stdin);
}
foreach ($line as $key => $value) {
    $data[] = explode(' ', $value);
}

foreach ($data as &$line) {
    $line = array_map(function($val) { return (int)$val; }, $line);
}


$N = $data[0][0];
$K = $data[0][1];

$edges = [];
$removableCnt = 0;
for ($i = 1; $i < $N; $i++) {
    for ($j = 2; $j <= $N; $j++) {
        if ($i < $j) {
            $edges[] = [$i, $j];
            if ($i > 1) { $removableCnt++; }
        }
    }
}

if ($K > $removableCnt) {
    printf('%d', -1);
    exit;
}

$edges = array_slice($edges, 0, count($edges) - $K);

printf("%d\n", count($edges));
$printArr = array_map(function($edge) { return $edge[0] . ' ' . $edge[1]; }, $edges);
echo join("\n", $printArr);


Measured memory_bytes: 6904
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i < n; ++i)
#define repe(i, n) for(int i=0; i <= n; ++i)
#define repr(i, n) for(int i=n-1; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
using namespace std;
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;}
template <class T> using V=vector<T>;
typedef long long ll;
const int INF = 1e9;
const ll MOD = 1000000007;
const ll MAX = 510000;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

double temp(int t, int h) {
    double s = t-h*0.006;
    return s;
}

int main() {
    int n;
    cin >> n;
    if (n%4 == 0 || n%7 == 0) {
        cout << "Yes" << "\n";
        return 0;
    }
    int four=n/4+1;
    int seven=n/7+1;
    for(int i=1; i<four; ++i) {
        for(int j=1; j<seven; ++j) {
            if (i*4 + j*7 == n) {
                cout << "Yes" << "\n";
                return 0;
            }
        }
    }
    cout << "No" << "\n";
}


Measured memory_bytes: 256
n=int(input())
a=list(map(int,input().split()))

mod=1000000007
ans=1
r,b,g=0,0,0
r2,b2,g2=-1,-1,-1
for i in a:
    if r2==r and b2==b and g2==g:
        print(0)
        exit()
    r2=r
    b2=b
    g2=g
    if i==r and i==b and i==g:
        ans*=3
        ans%=mod
        r+=1
    elif (i==r and i==b) or (i==g and i==r):
        ans*=2
        ans%=mod
        r+=1
    elif i==b and i==g:
        ans*=2
        ans%=mod
        g+=1
    elif i==r:
        r+=1
    elif i==b:
        b+=1
    elif i==g:
        g+=1

    else:
        print(0)
        exit()

print(ans)

Measured memory_bytes: 14364
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

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

// https://betrue12.hateblo.jp/entry/2020/06/14/232948

int main(){
    int H, W, K;
    cin >> H >> W >> K;
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;
    vector<vector<bool>> field(H);
    for(int i=0; i<H; i++){
        string s;
        cin >> s;
        field[i].resize(W);
        for(int j=0; j<W; j++) field[i][j] = (s[j] == '.');
    }

    auto inside = [&](int i, int j){ return i>=0 && i<H && j>=0 && j<W && field[i][j]; };
 
    vector<vector<int>> dist(H, vector<int>(W, 1e9));
    queue<pair<int, int>> que;
    que.emplace(sx, sy);
    dist[sx][sy] = 0;

    while(que.size()){
        auto [i, j] = que.front(); que.pop();
        for(int k=0; k<4; k++) for(int d=1; d<=K; d++){
            int x = i+d*dx[k], y = j+d*dy[k];
            if(!inside(x, y) || dist[x][y] <= dist[i][j]) break;
            if(dist[x][y] == 1e9){
                dist[x][y] = dist[i][j] + 1;
                que.emplace(x, y);
            }
        }
    }
 
    int ans = dist[gx][gy];
    if(ans == 1e9) ans = -1;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 11076
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,a,b;cin>>n>>a>>b;
  vector<int>p(n);
  for(int i=0;i<n;i++)cin>>p.at(i);
  sort(p.begin(),p.end());
  int k,l;
  for(int i=0;i<n;i++){
    if(p.at(i)>a){
      k=i;
      break;
    }
  }
  for(int i=n-1;i>=0;i--){
    if(p.at(i)<=b){
      l=n-i-1;
      break;
    }
  }
  int m=min(k,l);
  m=min(m,n-k-l);
  cout<<m<<endl;
}


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

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten6 1000005
#define PI 3.1415926

typedef long long int ll;

//ll chk[55];
//ll num[55];
//ll num2[55];
//ll num[ten5];
//ll num2[ten5];
//char cmap[10][10];
//priority_queue<long long int> pq;   //big at front
//stack<int> stk;
//vector<pair<ll,ll>> vint;
//vector<ll> vtime;
//deque<ll> deq[55];
//list<int> li;
//set<int> sint;
//set<ll> sll;
//map<ll,ll> mp;


int main()
{
    //int m,n,o=0,i,j,k;
    long long int sum=0,sum2=0,n1=0,n2=0,n3,range;
    double ans=0.0,half;
    long long int m,n,o,i,j,k=1;
    long long int a,a1,a2,a3,a4,a5,a6;
    a=a1=a2=a3=a4=a5=a6=0;
    //float f,g,h;
    //string s,s1;
    //char now,pre,c;
    cin>>n;
    if(n%4==0||n%7==0) cout<<"Yes";
    else
    {
        for(i=0;i<n/4;i++)
            if((n-4*i)%7==0)
        {
            cout<<"Yes";
            return 0;
        }
        for(i=0;i<n/7;i++)
            if((n-7*i)%4==0)
        {
            cout<<"Yes";
            return 0;
        }
        cout<<"No";
    }
    return 0;
}


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

 typedef long long ll;



 int main()
 {
    int n;
    cin >> n;
    vector<int> d(n);
    for(int i = 0; i < n; i++) cin >> d.at(i);

    sort(d.begin(), d.end());
    int a = n / 2 - 1;

    int ans = d.at(a+1) - d.at(a);
    cout << ans << endl;

     return 0;
 }

Measured memory_bytes: 640
#include<stdio.h>
#define MAX_i 99999

typedef struct process{
    char name[11];
    int time;
}pr;
void enqueue(pr x);
pr dequeue();
pr p[MAX_i];
int head,tail;

int main(void){
    int n,q,i,sum=0;
    pr x;
    
    scanf("%d %d",&n,&q);
    for(i=0;i<n;i++){
        scanf("%s %d",p[i].name,&p[i].time);
    }
    head=0;
    tail=n;
    while(head!=tail){
        x=dequeue();
        if(x.time<=q){
            sum+=x.time;
            printf("%s %d\n",x.name,sum);
        }else{
            sum+=q;
            x.time-=q;
            enqueue(x);
        }
    }
    return 0;
}

void enqueue(pr x){
    p[tail]=x;
    if(tail+1==MAX_i){
        tail=0;
    }else{
        tail++;
    }
}

pr dequeue(){
    if(head+1==MAX_i){
        head=0;
        return p[MAX_i-1];
    }else{
        head++;
        return p[head-1];
    }
}



Measured memory_bytes: 3292
#include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define pii pair<int, int>
#define plli pair <lli, lli>
#define F(i,n) for(lli (i)=0;(i)<(n);(i)++)
#define FR(i,n) for(lli (i)=(n)-1;(i)>=0;(i)--)
#define FSG(i,a,b) for(lli (i)=(a);(i)<(b);(i)++)
#define FSGR(i,a,b) for(lli (i)=(b)-1;(i)>=(a);(i)--)
#define VAL(n) cout << n << endl;
#define VALD(n) cout << #n << ": " << n << endl;

int main(){
  int N;
  cin >> N;
  int p[N];
  F(i, N){
    cin >> p[i];
  }
  int cnt = 0;
  FSG(i,1,N-1){
    if(max(p[i], max(p[i+1], p[i-1])) != p[i] && min(p[i], min(p[i+1], p[i-1])) != p[i]){
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(){
    int a, b, c;
    
    scanf("%d %d %d", &a, &b, &c);
    
    if(a < b && b < c){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
    
    return 0;
}


Measured memory_bytes: 2044
#include <stdio.h>
int main(void){
    int k, x;
    scanf("%d %d", &k, &x);
    int min = x - (k - 1);
    int max = x + (k - 1);
    int i;
    for (i = min; i <= max; i++) {
        printf("%d", i);
        if (i < max) {
            printf(" ");
        }
    }
    return 0;
}

Measured memory_bytes: 128
# AOJ ITP1_8_A

def numinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def main():
    string = input()
    answer = ""
    for i in range(len(string)):
        if 65 <= ord(string[i]) <= 90: answer += chr(ord(string[i]) + 32)
        elif 97 <= ord(string[i]) <= 122: answer += chr(ord(string[i]) - 32)
        else: answer += string[i]
    print(answer)

if __name__ == "__main__":
    main()


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

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 190;

typedef pair<int,int>   ii;

int  f[17][N][N][N];
char w[N][N];

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

    int n;  cin >> n;
    int m;  cin >> m;

    for(int i = 0 ; i < n ; ++i)
    for(int j = 0 ; j < m ; ++j)
        cin >> w[i][j];

    for(int i = 0 ; i <  n ; ++i)
    for(int j = m ; j >= 0 ; --j)   {
        if (w[i][j] == w[i][j + 1])
            f[0][j][i][i] = f[0][j + 1][i][i];
        else
            f[0][j][i][i] = j;
    }

    for(int l = 0 ; l < n ; ++l)
    for(int r = l ; r < n ; ++r)    {
        for(int i = 0 ; i < 17 ; ++i)   f[i][m][l][r] = m - 1;
        for(int i = 0 ; i < m ; ++i)    {
            if (w[r][i] == w[r + 1][i])
                f[0][i][l][r + 1] = min(f[0][i][l][r],f[0][i][r + 1][r + 1]);
            else
                f[0][i][l][r + 1] = i - 1;

            //cerr << "f[" << l << "][" << r << "][" << i << "][0] = " << f[l][r][i][0] << "\n";
        }
    }
    for(int i = 0 ; i < 16 ; ++i)
    for(int c = 0 ; c < m ; ++c)
        for(int l = 0 ; l < n ; ++l)    {
            int P = l;
            for(int r = l ; r < n ; ++r)    {
                int&res = f[i + 1][c][l][r];
                int prv = f[i][c][l][r];

                auto get = [&](int k)   {
                    int res = m;
                    if (res > f[i][c][l][k])    res = f[i][c][l][k];    ++k;
                    if (res > f[i][c][k][r])    res = f[i][c][k][r];

                    return  res;
                };

                res = f[i][prv + 1][l][r];

                while(P < r - 1 && get(P) <= get(P + 1))
                    ++P;

                if (res < get(P))
                    res = get(P);
            }
        }

    for(int i = 0 ; i < 17 ; ++i)
        if (f[i][0][0][n - 1] == m - 1) {
            cout << i;
            return  0;
        }
}

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

int main() {
  string s;
  cin >> s;
  for (ll i = 0; i < s.size() - 1; i++) {
    if (s.substr(i, 2) == "AC") {
      Yes();
      return 0;
    }
  }
  No();
}

Measured memory_bytes: 256
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    s = input().rstrip()
    ss = set(s)

    def test(s, ss):
        res = 0
        while len(set(s)) > 1:
            temp = ''
            for i in range(len(s)-1):
                if s[i] == ss or s[i+1] == ss:
                    temp += ss
                else:
                    temp += s[i]
            s = temp[:]
            res += 1

        return res

    res = 10000000000
    for i in ss:
        res = min(res, test(s, i))
    print(res)

if __name__ == '__main__':
    main()

Measured memory_bytes: 3316
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <utility>
#include <algorithm>
#include <functional>
#include <deque>
#define INF 1e9
#define MOD 1000000007
#define mod(x) ((x % MOD) + MOD) % MOD

using namespace std;

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

int main() {
  int R, C, N;
  cin >> R >> C >> N;

  vector<P> que[4];
  for (int i = 0; i < N; i++) {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    if ((x1 == 0 || x1 == R || y1 == 0 || y1 == C) &&
        (x2 == 0 || x2 == R || y2 == 0 || y2 == C)) {
      if (x1 == 0 && y1 != C) {
        que[0].push_back(make_pair(y1, i+1));
      } else if (y1 == C && x1 != R) {
        que[1].push_back(make_pair(x1, i+1));
      } else if (x1 == R && y1 != 0) {
        que[2].push_back(make_pair(-y1, i+1));
      } else {
        que[3].push_back(make_pair(-x1, i+1));
      }
      if (x2 == 0 && y2 != C) {
        que[0].push_back(make_pair(y2, i+1));
      } else if (y2 == C && x2 != R) {
        que[1].push_back(make_pair(x2, i+1));
      } else if (x2 == R && y2 != 0) {
        que[2].push_back(make_pair(-y2, i+1));
      } else {
        que[3].push_back(make_pair(-x2, i+1));
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    sort(que[i].begin(), que[i].end());
  }

  // for (int i = 0; i < 4; i++) {
  //   for (int j = 0; j < que[i].size(); j++) {
  //     printf("que[%d][%d] : %d, %d\n", i, j, que[i][j].first, que[i][j].second);
  //   }
  // }

  vector<int> q;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < que[i].size(); j++) {
      q.push_back(que[i][j].second);
    }
  }

  // for (int i = 0; i < q.size(); i++) {
  //   printf("q[%d] : %d\n", i, q[i]);
  // }

  vector<int> s;

  for (int i = 0; i < q.size(); i++) {
    if (s.size() == 0) {
      s.push_back(q[i]);
      continue;
    }
    if (s.back() != q[i]) {
      s.push_back(q[i]);
    } else {
      s.pop_back();
    }
  }

  if (s.size() == 0) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }

  return 0;
}


Measured memory_bytes: 3688
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

vector<pll> uft;

ll parent(ll a){
    if(uft[a].F==a){return a;}
    return uft[a].F=parent(uft[a].F);
}

void merge(ll a,ll b){
    uft[parent(a)].S+=uft[parent(b)].S;
    uft[parent(b)].F=parent(a);
}


int main(){
    ll n,m;
    cin>>n>>m;
    uft.resize(n);
    for(int i=0;i<n;i++){
        uft[i]={i,1};
    }
    vector<pll> A(m);
    for(auto &I:A){cin>>I.F>>I.S; I.F--; I.S--;}
    reverse(A.begin(),A.end());
    vector<ll> dp(m,0);
    for(int i=0;i<m;i++){
        pll I=A[i];
        if(parent(I.F)==parent(I.S)){continue;}
        dp[i]=uft[parent(I.F)].S*uft[parent(I.S)].S;
        merge(I.F,I.S);
    }
    for(int i=1;i<m;i++){dp[i]+=dp[i-1];}
    reverse(dp.begin(),dp.end());
    for(int i=1;i<m;i++){
        cout<<n*(n-1)/2-dp[i]<<endl;
    }
    cout<<n*(n-1)/2<<endl;
    
    
    
    return 0;
}


Measured memory_bytes: 5248
#include <iostream>
#include <utility>
#include <stdint.h>

using namespace std;

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

  unsigned A[N];
  uint64_t S = 0;
  
  for (int n=0; n<N; ++n) {
    cin >> A[n];
    S += A[n];
  }

  uint64_t M[N];

  M[0] = S;
  for (int n=1; n<N; n+=2) {
    M[0] -= 2*A[n];
  }

  for (int n=1; n<N; ++n) {
    M[n] = 2*A[n-1] - M[n-1];
  }  

  cout << M[0];
  for (int n=1; n<N; ++n)
    cout << ' ' << M[n];

  cout << endl;

  return 0;
}


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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ReadInt();
            var A = ReadArrayInt();
            var exists = new bool[8];
            var free = 0;
            for (int i = 0; i < A.Length; i++)
            {
                if (A[i] < 400) exists[0] = true;
                else if (A[i] < 800) exists[1] = true;
                else if (A[i] < 1200) exists[2] = true;
                else if (A[i] < 1600) exists[3] = true;
                else if (A[i] < 2000) exists[4] = true;
                else if (A[i] < 2400) exists[5] = true;
                else if (A[i] < 2800) exists[6] = true;
                else if (A[i] < 3200) exists[7] = true;
                else free++;
            }
            int min = 0, max = 0;
            if(exists.Count(b => b) > 0)
            {
                min = exists.Count(b => b);
                max = min + free;
            }
            else
            {
                min = 1;
                max = free;
            }

            Console.WriteLine(min + " " + max);
            Console.ReadLine();
        }

        static string Read() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Read()); }
        static long ReadLong() { return long.Parse(Read()); }
        static int[] ReadArrayInt() { return Read().Split(' ').Select(s => int.Parse(s)).ToArray(); }
        static long[] ReadArrayLong() { return Read().Split(' ').Select(s => long.Parse(s)).ToArray(); }
    }
}

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

int main(void)
{
	int a,b;
	char s[100];
	
	scanf("%d%d%s",&a,&b,s);
	
	int count=0;
	
	for(int i=0;i<a+b+1;i++){
		if(isdigit(s[i])==0){
			count++;
		}
	}
	
	if(count!=1){
		printf("No\n");
	}else if(s[a]!='-'){
		printf("No\n");
	}else{
		printf("Yes\n");
	}
	
	return 0;
}

Measured memory_bytes: 128
l, r = [int(i) for i in input().split()]

if r - l + 1 >= 2019:
    print(0)
else:
    ans = 2018
    for i in range(l, r+1):
        for j in range(i+1, r+1):
            ans = min(ans, (i*j) % 2019)
    print(ans)

Measured memory_bytes: 3060
#include<iostream>
#include<set>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cstring>

#define MAX 128
using namespace std;
set<int> seed;

void perm(int cards[], int com[], int p[], int n)
{
  int *c,*pc,*q,k,t;
  
  c = new int[n];
  for(k=1,q=p,pc=c;k<=n;)*q++=*pc++=k++;k=1;pc=c;
  do{t=*(p+k);*(p+k)=*(q=p+((k&1)?*pc:0));*q=t;
    
    //TODO
    ostringstream oss;
    for(int i = 0; i < n; ++i){
      oss << cards[ com[ p[i]-1 ] ];
    }
    seed.insert( atoi(oss.str().c_str()) );
    
    k=1;pc = c;while(*pc==0)*pc++=k++;(*pc)--;}while(k<n);
  delete [] c;
}

void comb(int cards[], int n, int k)
{
  int *p;
  bool loop = false;
  
  p = new int[k];
  for(int i = 0; i < k; i++) p[i] = i;
  
  do{ loop = false;
    
    // TODO
    int tmp[MAX];
    perm(cards,p,tmp,k);
    
    for(int pk=k-1;pk>=0;pk--){p[pk]++;if(p[pk]<=n-(k-pk)){for(int j=pk+1;j<k;j++)p[j]=p[j-1]+1;pk++;loop=true;break;}}
  }while(loop);
  delete [] p;
}

int main(){
  while(true){
    int cards[MAX];
    int n,k;scanf("%d%d", &n, &k);
    if(n==0&&k==0)break;
    for(int i = 0; i < n; ++i){
      scanf("%d",&cards[i]);
    }
    comb( cards, n, k );
    cout << seed.size() << endl;
    seed.clear();
  }
  return 0;
}

Measured memory_bytes: 0