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

int main() {
    int n;
    string str;
    bool dp[1<<10][10];  // dp[S][t] = ロックしている数の集がSで次にロックする数がtのみである場合が存在するか
    while(cin >> n, n){
        cin >> str;
        for(int i=0; i<(1<<10); ++i) fill(dp[i], dp[i]+10, false);
        int S = 0, t;  // S=ロックの集合, t=次の数
        bool ok = true;
        for(int i=0; i<n; ++i){
            if(str[i] == 'u'){
                S = 0;
            } else {
                t = str[i]-'0';
                dp[S][t] = true;
                if((S>>t)&1) ok = false;
                S |= 1<<t;
            }
        }
        // 複数のスレッドをマージする
        for(int S1=0; S1<(1<<10); ++S1){
            for(int S2=0; S2<(1<<10); ++S2){
                if(S1 & S2) continue;  // このようなスレッドはともに存在しない
                for(int t1=0; t1<10; ++t1){
                    for(int t2=0; t2<10; ++t2){
                        if(dp[S1][t1] && dp[S2][t2]){
                            if(((S1>>t2)&1) && ((S2>>t1)&1)) ok = false;  // デッドロック発生
                            // 一方が停止しているならばまとめてよい
                            if((S1>>t2)&1) dp[S1|S2][t1] = true;
                            if((S2>>t1)&1) dp[S1|S2][t2] = true;
                        }
                    }
                }
            }
        }
        cout << (ok ? "SAFE" : "UNSAFE" ) << endl;
    }
    return 0;
}


Measured memory_bytes: 3184
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
 	public static void main(String[] args){
 		Scanner sc = new Scanner(System.in);
 		int N = sc.nextInt();
 		int M = sc.nextInt();

 		Map<Long, Long> map = new TreeMap<>();

 		for (int i = 0; i < N; i++) {
 			long a = sc.nextLong();
 			long b = sc.nextLong();
			if (map.containsKey(a)) {
				map.put(a, map.get(a) + b);
			} else {
				map.put(a, b);
			}

 		}
 		long ans = 0;
 		long rem = M;

		for(Map.Entry<Long, Long> e : map.entrySet()) {

			if (rem <= 0) {
				break;
			}

			if(	rem >= e.getValue()) {
				rem -= e.getValue();
				ans += e.getKey() * e.getValue();
			} else {
				ans += rem * e.getKey();
				rem = 0;
				break;
			}
		}

		System.out.println(ans);
 	}
}


Measured memory_bytes: 97784
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
using namespace std;
int a[4];
int main()
{
  string S;
  cin >> S;
  int sum = 0;
  rep(i, S.size())
  {
    a[i] = S.at(i) - '0';
  }
  rep(x, 8)
  {
    int sum = a[0];
    string s;
    s += (char)('0' + a[0]);
    rep(i, 3)
    {
      if (x & (1 << i))
      {
        sum += a[i + 1];
        s += '+';
        s += (char)('0' + a[i + 1]);
      }
      else
      {
        sum -= a[i + 1];
        s += '-';
        s += (char)('0' + a[i + 1]);
      }
    }
    if (sum == 7)
    {
      cout << s << "=7" << endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 256
/* Simplicity and Goodness */

#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;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) { cout << ' ' << A; my_dbg(B...); }
#define dbg(...)  cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\n", n)
#define rep(i,a,b) for(int i=a; i<(int)b; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9+7;
const int maxN = 1e6+2;

ll dist(vi& x, vi& y) {
   ll res = 0;
   int n = (int) x.size();

   for(int p : {1, -1}) {
      for(int q : {1, -1}) {

         ll smallest = p * x[0] + q * y[0];

         rep(i, 1, n) {
            ll cur = p * x[i] + q * y[i];

            res = max(res, cur - smallest);

            smallest = min(smallest, cur);
         }
      }
   }

   return res;
}

int main()
{
   int n;
   scn(n);

   vi x(n), y(n);

   rep(i, 0, n) {
      scn(x[i]); scn(y[i]);
   }

   lprin(dist(x, y));

   return 0;
}

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

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

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

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<queue>
using namespace std;
#define int long  long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
typedef pair<int, int>P;
const int INF = 1e8;

signed main() {
	int N;
	cin >> N;
	priority_queue<int>q;
	int num;
	rep(i, N) {
		cin >> num;
		q.push(num);
	}
	int A = 0, B = 0;
	int i = 0;
	while (q.size()) {
		if (i % 2 == 0)A += q.top();
		else B += q.top();
		q.pop();
		i++;
	}
	cout << A - B;
}

Measured memory_bytes: 256
from collections import deque


H, W = map(int, input().split())
Ch, Cw = map(lambda x: int(x) - 1, input().split())
Dh, Dw = map(lambda x: int(x) - 1, input().split())
maze = [input() for _ in range(H)]

INF = 10 ** 12
path = [[INF] * W for _ in range(H)]  # path: 各マスへの移動コスト

# walk:移動A, warp:移動B
walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

q = deque()
path[Ch][Cw] = 0
q.append((Ch, Cw, 0))

while q:
    x, y, s = q.popleft()
    for dx, dy in walk:  # 移動A
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s:
            path[nx][ny] = s
            q.appendleft((nx, ny, s))  # 移動Aの場合はコスト0. キューの先頭に追加する.
    for dx, dy in warp:  # 移動B
        nx = x + dx
        ny = y + dy
        if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s + 1:
            path[nx][ny] = s + 1
            q.append((nx, ny, s + 1))  # 移動Bの場合はコスト1. キューの最後尾に追加する.
    
ans = path[Dh][Dw] if path[Dh][Dw] < INF else '-1'
print(ans)

Measured memory_bytes: 94036
#include<cstdio>
using namespace std;

int main() {
	int input;
	int i=1;
	while (true)
	{		
		scanf("%d", &input);
		if (input == 0) {
			break;
		}
		else
		{
			printf("Case %d: %d\n", i,input );
			i++;
		}
		
	}

	return 0;
}


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

int main() {
    int q;
    vector<int> sum[2];
    for(int i=0;i<2;i++){
        int len;
        string s;
        cin >> s;
        len = s.length();
        sum[i].assign(len+1, 0);
        for(int j=0;j<len;j++){
            int res = sum[i][j];
            if(s[j] == 'A')res++;
            else res += 2;
            sum[i][j+1] = res % 3;
        }
    }
    cin >> q;
    for(int i=0;i<q;i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        int p = sum[0][b] - sum[0][a-1] + 3;
        int q = sum[1][d] - sum[1][c-1] + 3;
        if(p%3 == q%3)puts("YES");
        else puts("NO");
    }
	return 0;
}

Measured memory_bytes: 1464
// AtCoder Beginner Contest 054
#include <bits/stdc++.h>
using namespace std;

typedef vector<string> vs;
#define rep(i, n) for(int i = 0; i < (n); i++)
template<class T> istream& operator >> (istream& s, vector<T>& v)
{ for (T& x: v) { s >> x; } return s;}


int main()
{
    int H,W; cin >>H>>W;
    vs bd(H); cin >> bd;

    rep(h, H) {
        rep(w, W) {
            if (bd[h][w]=='#') {
                bool ok = true;
                if (!(h==0&&w==0)) {
                    if ((h > 0 && bd[h-1][w]=='#')==(w > 0 && bd[h][w-1]=='#')) {
                        ok = false;
                    }
                }
                if (!(h==H-1&&w==W-1)) {
                    if ((h < H-1 && bd[h+1][w]=='#')==(w < W-1 && bd[h][w+1]=='#')) {
                        ok = false;
                    }
                }
                if (!ok) {
                    cout << "Impossible" << endl;
                    return 0;
                }
            }
        }
    }

    cout << "Possible" << endl;
}


Measured memory_bytes: 256
x,n = map(int,input().split())
if n != 0:
    li = list(map(int,input().split()))
    if x not in li:
        print(x)
    else:
        for i in range(1,100):
            if x - i not in li:
                print(x - i)
                break
            elif x + i not in li:
                print(x + i)
                break

else:
    print(x)

Measured memory_bytes: 9192
"use strict";

const Main = args => {
  const input = args.trim().split(" ");
  const K = parseInt(input[0], 10);
  const S = parseInt(input[1], 10);

  let counter = 0;

  for (let x = 0; x <= K; x++) {
    for (let y = 0; y <= K; y++) {
      let z = S - (x + y);
      if (0 <= z && z <= K) {
        counter++;
      }
    }
  }
  console.log(counter);
}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 7372
#include "bits/stdc++.h"

using namespace std;

//using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
bool cmp(P a, P b) { return a.second < b.second; } //End sort (Interval scheduling problem)

const ll MOD = 1e9 + 7LL;
const int MAX = 510000;


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

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

ll sum;
ll n, k;

int main(void){
	cin>>n>>k;

	COMinit();
	for (int i = 1; i <= k; i++) {
		cout<< ((COM(k - 1, i - 1) % MOD) * (COM(n - k + 1, i) % MOD)) % MOD <<endl;
	}
	
	return 0;
}

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

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

ll ext_gcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll tmp = a / b;
  ll d = ext_gcd(b, a - b * tmp, y, x);
  y -= tmp * x;
  return d;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll a, b, x, y;
  cin >> a >> b;

  ext_gcd(a, b, x, y);
  cout << x << " " << y << endl;

  return 0;
}



Measured memory_bytes: 3220
#include <iostream>
#include <algorithm>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, k;
int a[15];

signed main() {
	int i, j;
	
	cin >> n >> k;
	rep(i, n) cin >> a[i];
	
	int INF = 1e+15;
	int ans = INF;
	rep(i, (1 << n)) {
		int cnt = 0;
		rep(j, n) if ((i >> j) % 2 == 1) cnt++;
		if (cnt < k) continue;
		
		int maxH = 0;
		int sumC = 0;
		rep(j, n) {
			if ((i >> j) % 2 == 1) {
				sumC += max(maxH + 1, a[j]) - a[j];
				maxH = max(maxH + 1, a[j]);
			}
			else {
				maxH = max(maxH, a[j]);
			}
		}
		ans = min(ans, sumC);
	}
	cout << ans << endl;
	return 0;
}

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

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

int main()
{
    int N;
    cin >> N;
    int first[100000] = {};
    int last[100000] = {};
    int A[100000] = {};
    int a[100000] = {};
    for(int i=0;i<N;i++){
        cin >> A[i];
        a[i] = A[i];
    }
    reverse(a,a+N);

    for(int i=0;i<N;i++){
        first[i+1] = gcd(A[i],first[i]);
    }
    for(int i=0;i<N;i++){
        last[i+1] = gcd(a[i],last[i]);
    }
    
    int ans = 0;
    for(int i=0;i<=N;i++){
        ans = max(ans,gcd(first[i],last[N-1-i]));
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1792
n, m = map(int, input().split(' '))
switches = [list(map(lambda x: int(x) - 1, input().split(' ')))[1:]
            for _ in range(m)]
p = list(map(int, input().split(' ')))

cnt = 0
# ビット演算子。n ビット左シフト
for condition in range(1 << n):
    light = True
    for i in range(m):
        oncnt = 0
        # for switch in switches:
        #   for i in switch:
        # の節約になる
        for switch in switches[i]:
            # ビット演算子。1 をフラグとした時、被っていれば != 0
            if (condition & (1 << switch)) != 0:
                oncnt += 1

        if oncnt % 2 != p[i]:
            light = False

    if light:
        cnt += 1


print(cnt)


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

class Program
{
    static void Main(string[] args)
    {   
        string[] input = Console.ReadLine().Split();

        int w = int.Parse(input[0]);
        int a = int.Parse(input[1]);
        int b = int.Parse(input[2]);

        int ret;
        int from = (a-w) < 0 ? 0 : (a-w);
        int to = a + w;

        if (from <= b && b <= to) { ret=0;}
        else { ret = Math.Abs(b-a)-w; }
        Console.WriteLine(ret);
            
    }
}


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

int main() {
  int x, y, leg;
  bool exist = false;
  cin >> x >> y;
  for (int i = 0; i <= x; i++) {
    leg = 2*i + 4*(x-i);
    if (leg == y) {
      exist = true;
      break;
    }
  }
  if (exist) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 3632
#include<iostream>
#include<cmath>
#include<limits>
#define POS(i,j) (p[d * (i) + j])

using namespace std;


int main(){
        int n, d;
        cin >> n >> d;
        int *p = new int[n * d];
        long double eps = numeric_limits<long double>::epsilon();
        for(int i=0;i<n;i++){
                for(int j=0;j<d;j++){
                        cin >> POS(i,j);
                }
        }

        int cnt = 0;
        for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                        long double dist = 0;
                        for(int k=0;k<d;k++){
                                dist += (POS(i,k) - POS(j, k)) * (POS(i,k) - POS(j,k));
                        }
                        long double sq = sqrt(dist);
                        if(sq - floor(sq) < eps || ceil(sq) - sq < eps){
                                //cout << "(" << i << "," << j << ") :" <<  dist << "/" << sq << endl;
                                cnt++;
                        }
                }
        }

        cout << cnt << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
long long n,m,d;
double ans=0;
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n>>m>>d;
	ans=(2.0*(n-d))/(1.0*n*n);
	if(d==0) ans/=2.0;
	ans*=1.0*(m-1);
	cout<<(fixed)<<setprecision(12)<<ans;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int a[1000001]={0};
int main(void){
  a[1]=1;
  for(int i=1;i<=1000000;i++){
    if(a[i] && i*2<=1000000) a[i*2]=1;
    if(a[i] && i*3<=1000000) a[i*3]=1;
    if(a[i] && i*5<=1000000) a[i*5]=1;
  }
  int n,m;
  while(cin >> n,n){
    cin >> m;
    int c=0;
    for(int i=n;i<=m;i++){
      if(a[i]) c++;
    }
    cout << c << endl;
  }
}


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

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

    vector<int> bingo(9);

    rep(i, 9)
    {
        cin >> bingo[i];
    }

    int N;

    cin >> N;
    vector<int> lot(N);

    rep(i, N)
    {
        cin >> lot[i];
    }

    rep(i, N)
    {
        rep(j, 9)
        {
            if (bingo[j] == lot[i])
            {
                bingo[j] -= lot[i];
            }
        }
    }

    if (
            bingo[0]+bingo[1]+bingo[2] == 0
            || bingo[3]+bingo[4]+bingo[5] == 0
            || bingo[6]+bingo[7]+bingo[8] == 0
            || bingo[0]+bingo[3]+bingo[6] == 0
            || bingo[1]+bingo[4]+bingo[7] == 0
            || bingo[2]+bingo[5]+bingo[8] == 0
            || bingo[0]+bingo[4]+bingo[8] == 0
            || bingo[2]+bingo[4]+bingo[6] == 0)
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
 
int main()
{
    int N, W;
    cin >> N >> W;
  
    int v[N],w[N];
     
    for(int i=0; i<N; i++){
        cin >> v[i] >> w[i];
    }
  
    int DP[N+1][W+1];
 
    for(int j=0; j<=W; j++)
        DP[N][j] = 0;
  
    for(int i=N-1; i>=0; i--){
        for(int j=0; j<=W; j++){
            if(j<w[i])
                DP[i][j] = DP[i+1][j];
            else{
                if(DP[i+1][j]>DP[i][j-w[i]]+v[i])
                    DP[i][j] = DP[i+1][j];
                else   
                    DP[i][j]=DP[i][j-w[i]]+v[i];         
             }
        }
   } 
    cout << DP[0][W] << endl;
  
    return 0;
}

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

int dp[101][1<<16],mask[101];
int main(){
	int n;
	while(scanf("%d",&n),n){
		memset(dp,0,sizeof(dp));
		int Max=0;
		rep(i,n){
			int m,l;scanf("%d%d",&m,&l);
			int mask=0;
			rep(k,m){
				int s,e;scanf("%d%d",&s,&e);s-=6;e-=6;
				for(int j=s;j<e;j++)mask|=1<<j;
			}
			rep(j,1<<16){
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
				if(!(j&mask)){
					dp[i+1][j|mask]=max(dp[i+1][j|mask],dp[i][j]+l);
					Max=max(Max,dp[i+1][j|mask]);
				}
			}
		}
		printf("%d\n",Max);
	}
}

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

int main(){
	int N;
	cin >> N;
	vector<long long> A(N);
	vector<long long> B(N);
	bool flag=true;
	for(int i=0;i<N;i++){
		cin >> A[i] >> B[i];
		if(A[i]!=B[i])flag=false;
	}
	if(flag){
		cout << "0" << endl;
		return 0;
	}
	
	long long ans=accumulate(A.begin(),A.end(),0LL),tmp=9999999999999LL;
	for(int i=0;i<N;i++){
		if(A[i]>B[i])tmp=min(tmp,B[i]);
	}
	ans-=tmp;
	cout << ans << endl;
	return 0;
}

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

int main(){

    int r, g, b;

    cin >> r >> g >> b;

    r *= 100;
    g *= 10;

    if((r + g + b) % 4 == 0){
        cout << "YES" << endl;
    }
    else{
        cout << "NO" << endl;
    }


    return 0;

}


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

bool shakutori(ll N, ll M, ll mid, vector<ll> &A){
  // mid点以上の握手の仕方がM通り以下ならtrue
  int i = 0, j = N-1; //i：左手、j：右手。数字が少ないほど点数は高い
  ll cnt = 0;
  while (i < N && j >= 0){
    if (A.at(i)+A.at(j) >= mid){
      cnt += j + 1; i++;
    }else{
      j--;
    }
  }
  return cnt <= M;
}

ll shakutori2(ll N, ll M, ll X, vector<ll> &A, vector<ll> &sums){
  // X点以上の握手の仕方がM通り以下であることがわかっている。
  // ここでは具体的な点数計算を行う。
  int i=0, j=N-1;
  ll ans = 0;
  ll cnt = M;
  ll score = X-1;
  while (i < N && j >= 0){
    if (A.at(i)+A.at(j) >= X){
      ans += A.at(i) * (j+1) + sums.at(j+1);
      cnt -= j + 1;
      i++;
    }else{
      j--;
    }
  }
  ans += cnt * score;
  return ans;
}

int main() {
  ll N,M; cin >>N >>M;
  vector<ll> A(N);
  for (int i=0; i<N; i++) cin >>A.at(i);
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  // 降順ソート
  
  ll lb = -1, ub = A.at(0)*2+1;
  // この点数以上は全部いる(M通り以下しかない)！みたいなXを二分探索で
  while (ub-lb > 1){
    ll mid = (ub+lb)/2;
    if (shakutori(N, M, mid, A)){
      ub = mid;
    }else{
      lb = mid;
    }
  }
  ll X = ub;
  vector<ll> sums(N+1, 0); // sums[i]: A[0]からA[i-1]までの累積和。sums[0]=0。
  rep(i,N){
    sums.at(i+1) = sums.at(i) + A.at(i);
  }

  ll ans = shakutori2(N, M, X, A, sums);

  cout <<ans <<endl;
}


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

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

int N,M,Q;
bool valid[1010][40],switchState[40];
string S,B;

void update(){
  rep(i,N) {
    if( S[i] == '1' ) switchState[i] = !switchState[i];
    rep(j,M) if( ( B[j] - '0' ) != switchState[i] ) valid[j][i] = false;
  }
}

int main(){
  vector<char> alnum;
  rep(i,10) alnum.push_back((char)('0'+i));
  rep(i,26) alnum.push_back((char)('A'+i));
  while( cin >> N >> M >> Q, N|M|Q ){
    memset(valid,true,sizeof(valid));
    memset(switchState,false,sizeof(switchState));
    rep(_,Q){
      cin >> S >> B;
      update();
    }
    rep(i,M) {
      int pos = -1;
      int cnt = 0;
      rep(j,N) if( valid[i][j] ) { ++cnt, pos = j; if( cnt >= 2 ) break; }
      if( cnt == 1 ) printf("%c",alnum[pos]);
      else           printf("?");
    } puts("");
  }
  return 0;
}

Measured memory_bytes: 1252
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <limits.h>	
#define inf (INT_MAX-1)
#define INF 9223372036854775807
#define sq(n) ((n)*(n))
#define rep(i,n) for(i=0;i<n;i++)
	#define rev(i,n) for(i=n-1;i>=0;i--)
		#define sort(a,n) qsort(a,n,sizeof(TYPE),cmp)
		#define sort_r(a,n) qsort(a,n,sizeof(TYPE),cmp_r);
#define TYPE int
#define MEMSET(a) memset(a,0,sizeof(a))
const int mod=(int)1e09+7;

int in(void){
	int i;scanf("%d",&i);
	return i;
}
long long llin(void){
	long long i;scanf("%lld",&i);
	return i;
}
double din(void){
	double i;scanf("%lf",&i);
	return i;
}
void chin(char s[]){
	scanf("%s",s);
}
void print(int a){
	printf("%d\n",a);
}
void llprint(long long a){
	printf("%lld\n",a);
}
void dprint(double a){
	printf("%.10f\n",a);
}
void print2(int a,int b){
	printf("%d %d\n",a,b);
}
long long max(long long a,long long b){
	return a>b?a:b;
}
long long min(long long a,long long b){
	return a<b?a:b;
}
long long llabs(long long a){
	return a>0?a:-a;
}
double dmax(double a,double b){
	return a>b?a:b;
}
int cmp(const void *a,const void *b){
	return *(TYPE *)a-*(TYPE *)b;
}
int cmp_r(const void *a,const void *b){
	return *(TYPE *)b-*(TYPE *)a;
}
void swap(int *a,int *b){
	int t=*a;
	*a=*b;
	*b=t;
}

int main(void){
	int n=in(),i,t,x,y,now=0,nx=0,ny=0;
	rep(i,n){
		t=in();
		x=in();
		y=in();
		int diff=t-now;
		if(abs(x-nx)>diff){
			puts("No");
			return 0;
		}
		diff-=abs(x-nx);
		nx=x;
		if(abs(y-ny)>diff){
			puts("No");
			return 0;
		}
		diff-=abs(y-ny);
		ny=y;
		if(diff%2){
			puts("No");
			return 0;
		}
		now=t;
	}
	puts("Yes");
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

long long int calculate(pair<long long int, long long int> start, pair<long long int, long long int> end)
{
	long long int xdist = abs(start.first - end.first);
	long long int ydist = abs(start.second - end.second);
	return xdist < ydist ? xdist : ydist;
}

int main()
{
	int n;
	cin >> n;
	vector<pair<long long int, long long int>> points(n);
	vector<int> xpoints(n);
	vector<int> ypoints(n);
	vector<bool> flag(n, false);
	for (int i = 0; i < n; i++)
	{
		long long int x, y;
		cin >> x >> y;
		points[i] = make_pair(x, y);
		xpoints[i] = i;
		ypoints[i] = i;
	}
	auto comparex = [&points](int a, int b) -> bool
	{
		return points[a].first < points[b].first;
	};
	auto comparey = [&points](int a, int b) -> bool
	{
		return points[a].second < points[b].second;
	};
	sort(xpoints.begin(), xpoints.end(), comparex);
	sort(ypoints.begin(), ypoints.end(), comparey);

	//クラスカル法の実装
	//union-find木の実装
	struct union_find_tree {
		vector<int> parent;
		vector<int> rank;

		void init(int n)
		{
			for (int i = 0; i < n; i++)
			{
				parent.push_back(i);
				rank.push_back(0);
			}
		}

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

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

			if (x == y) return;

			if (rank[x] < rank[y])
			{
				parent[x] = y;
			}
			else {
				parent[y] = x;
				if (rank[x] == rank[y]) rank[x]++;
			}
		}

		bool same(int x, int y)
		{
			return find(x) == find(y);
		}
	};
	union_find_tree tree;
	tree.init(n);
	//辺集合の生成
	struct edge
	{
		int start;
		int end;
		long long int cost;
		
		bool operator<(edge &e) const {
			return cost < e.cost;
		}
	};
	vector<edge> edges;
	for (int i = 0; i < n - 1; i++)
	{
		edge e1 = { xpoints[i], xpoints[i + 1], calculate(points[xpoints[i]], points[xpoints[i + 1]]) };
		edge e2 = { ypoints[i], ypoints[i + 1], calculate(points[ypoints[i]], points[ypoints[i + 1]]) };
		edges.push_back(e1);
		edges.push_back(e2);
	}
	sort(edges.begin(), edges.end());
	long long int cost = 0;
	for (auto e : edges)
	{
		if (!tree.same(e.start, e.end))
		{
			tree.unite(e.start, e.end);
			cost += e.cost;
		}
	}
	cout << cost << endl;
}

Measured memory_bytes: 9580
N = gets.to_i

a = Array.new(1000000)
ans = 1
wk = N
a[N] = 1
begin
  ans += 1
  wk = wk.even? ? wk / 2 : 3 * wk + 1
  break if not a[wk].nil?
  a[wk] = 1
end while true

puts ans


Measured memory_bytes: 9596
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(x) cerr << #x << " = " << x << endl

int main(int qtd, char* nome[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);
 
	ll a, b, c, k;
	cin >> a >> b >> c >> k;
	cout << (a>k? k: a+(b>k-a? 0: -(k-a-b))) << "\n";
	
	return 0;
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
#define ri rd<int>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; --i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++i)
const int maxN = 2e5 + 7;
typedef long long LL;
const LL O = 1e9 + 7;

template<class T> T rd() {
	bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
	T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return f ? x : -x;
}

int n;
struct Node {
	int v, i;
	inline bool operator < (const Node &y) const {
		return v > y.v;
	}
}a[maxN];
LL c[maxN], d[maxN];
LL C[maxN], sufC[maxN];

namespace Seg {
	const int maxN = ::maxN << 1;
	struct Node;
	typedef Node* node;
	struct Node {
		node lc, rc;
		int l, mid, r;
		int sz;
		LL sum, tag;
		bool havtag;
		Node(int _sz = 0, LL _sum = 0) : sz(_sz), sum(_sum) { havtag = false; }

		Node operator + (const Node &v) const {
			return Node(sz + v.sz, (sum + v.sum) % O);
		}

		void totag(LL d) {
			(sum *= d) %= O;
			if (!havtag) havtag = true, tag = 1;
			(tag *= d) %= O;
		}

		void pushdown() {
			if (havtag) {
				lc->totag(tag);
				rc->totag(tag);
				havtag = false;
			}
		}
	}*rt, pool[maxN], *tpool = pool;

	void build(node &x, int l, int r) {
		x = tpool++;
		x->l = l, x->r = r, x->mid = (l + r) >> 1;
		if (l == r) return;
		build(x->lc, l, x->mid);
		build(x->rc, x->mid+1, r);
	}

	void init() {
		build(rt, 1, n);
	}

	void ins(node x, int to, LL d) {
		x->sz++;
		(x->sum += d) %= O;
		if (x->l == x->r) return;
		x->pushdown();
		if (to <= x->mid) ins(x->lc, to, d);
		else ins(x->rc, to, d);
	}

	void ins(int x, LL d) {
		ins(rt, x, d);
	}

	Node get(node x, int l, int r) {
		if (l <= x->l && x->r <= r) return *x;
		x->pushdown();
		if (r <= x->mid) return get(x->lc, l, r);
		if (x->mid < l) return get(x->rc, l, r);
		return get(x->lc, l, x->mid) + get(x->rc, x->mid+1, r);
	}

	Node get(int l, int r) {
		if (l > r) return Node();
		return get(rt, l, r);
	}
}

int main() {

	n = ri();
	rep (i, 1, n) {
		c[a[i].v = ri()]++;
		a[i].i = i;
	}

	per (i, n, 1) c[i] += c[i+1];
	rep (i, 1, n) {
		c[i] = c[i] - (n-i);
		d[i] = c[i] - 1;
	}
	sufC[n] = 1; per (i, n-1, 0) sufC[i] = sufC[i+1] * c[i+1] % O;
	C[0] = 1; rep (i, 1, n) C[i] = C[i-1] * c[i] % O;

	Seg::init();
	sort(a+1, a+n+1);
	LL ans = 0, cnt = 0;
	for (int v = n, i = 1; v; --v) {
		LL res = 0;
		for (; i <= n && a[i].v == v; ++i) {
			Seg::Node tp = Seg::get(1, a[i].i-1);
			res -= tp.sum;
			cnt += tp.sz;
			tp = Seg::get(a[i].i+1, n);
			res += tp.sum;
			Seg::ins(a[i].i, sufC[v]);
		}
		ans += res % O * C[v] % O;
		Seg::rt->totag(d[v]);
	}
	ans = ans % O * (O+1) / 2 % O;
	ans += cnt % O * C[n] % O;
	printf("%lld\n", (ans % O + O) % O);

	return 0;
}


Measured memory_bytes: 29952
def main():
    numbers = int(input())
    array = [int(x) for x in input().split()]
    maximum = 1000001
    counter = [0] * maximum

    for x in array:
        if counter[x] != 0:
            counter[x] = 2
            continue
        else:
            for multiple in range(x, maximum, x):
                counter[multiple] += 1
    ans = 0
    for x in array:
        if counter[x] == 1:
            ans += 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 32084
#include <cstdio>
#include <cmath>

double P, E, T;

double dfs(int K, double L, double R) {
  double H = 0.5*(L+R);
  if (K == 0) return (std::abs(T-H) < E)? 1.0:0.0;

  double LH = dfs(K-1, L, H);
  double HR = dfs(K-1, H, R);
  if (H >= T) {
    return P * HR + (1.0-P) * LH;
  } else {
    return P * LH + (1.0-P) * HR;
  }
}

int main() {
  int K;
  double L, R;
  scanf("%d %lf %lf", &K, &L, &R);
  scanf("%lf %lf %lf", &P, &E, &T);

  printf("%.12f\n", dfs(K, L, R));
}



Measured memory_bytes: 2760
import sys
S = list(input())
T = list(input())

def Ispossible(a,b):
  #print(a,b)
  if len(a) != len(b):
    return False
  flag = True
  for i in range(len(a)):
    if not(a[i]==b[i] or a[i]=="?"):
      flag = False
      break
  return flag

for i in range(1,len(S)-len(T)+2):
  q = S[-i:-i-len(T):-1].copy()
  r = T[-1:-len(T)-1:-1].copy()
  if Ispossible(q, r):
    for j in range(len(T)):
      S[-i-j] = T[-1-j]
      #print(S)
      
    for j in range(len(S)):
      if S[j] == "?":
        S[j] = "a"
    print("".join(S))
    sys.exit()
print("UNRESTORABLE")
    
    
    
  

Measured memory_bytes: 61956
def actual(x, y):
    group1 = {1, 3, 5, 7, 8, 10, 12}
    group2 = {4, 6, 9, 11}
    group3 = {2}

    for group in [group1, group2, group3]:
        if x in group and y in group:
            return 'Yes'

    return 'No'

x, y = map(int, input().split())
print(actual(x, y))

Measured memory_bytes: 9120
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const long long MOD = 1000000007;

ll N, K;
ll A[100000];

int main() {
    cin >> N >> K;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    ll X = 0;
    for (ll i = 40; i >= 0; i--) {
        ll target = 1ll << i;
        if (X + target <= K) {
            int cnt = 0;
            for (int j = 0; j < N; j++) {
                if (A[j] & target) {
                    cnt++;
                }
            }
            if (cnt < N - cnt) {
                X += target;
            }
        }
    }

    ll ans = 0;
    for (int i = 0; i < N; i++) {
        ans += (X ^ A[i]);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>

int main() {
	int n;
	int k;
	int itung=1;
	scanf("%d", &n);
	scanf("%d", &k);
	for(int i=0;i<n;i++){
		if(itung<k){
			itung*=2;
		}else{
			itung+=k;
		}
	}
	
	printf("%d\n", itung);
	
	return 0;	
}

Measured memory_bytes: 1724
/**
 *    author: codeforces-yuhongtao 
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<deque>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<sstream>
using namespace std;
#define pi acos(-1.0) 
#define ms(x,n) memset(x,n,sizeof(x));
#define pb push_back
typedef long long ll;//string::npos
typedef long long iint;
ll gcd(ll a,ll b){ return a%b==0?b:gcd(b,a%b); }
const int inf=0x3f3f3f3f;
const int maxn=2e5+5;
const int mod=1e9+7;
//priority_queue<ll,vector<ll>,greater<ll> > q;//小根堆 
//vector<int> vt[205];
//pair<int,int> p;
map<int,int> m;
iint a[maxn];
int main(){
	ios::sync_with_stdio(false);
	iint n,k,xh,bef;
	cin>>n>>k;k+=1;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	iint i=1,cnt=1;iint tt=0,ans;
	while(m[i]==0){
		m[i]=cnt;
		cnt++;
		i=a[i];
	}
	xh = cnt - m[i];
	bef = m[i] - 1;
//	for(i=1;i<=n;i++) cout<<m[i]<<" ";
//	cout<<endl;
//	cout<<xh<<" "<<bef<<endl;
//	cout<<"ans:";
	if(k<=bef){
		i=1;
		while(tt<k){
			tt++;
			ans=i;
			i=a[i];
		}
		cout<<ans<<endl;
	}else{
		k-=bef;
		k = k%xh==0?xh:k%xh;
		while(tt<k){
			tt++;
			ans=i;
			i=a[i];
		}
		cout<<ans<<endl;
	}
}

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

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

struct Data {
  int start,end,onAir,recorded;
  bool operator < ( const Data &data ) const {
    if( end != data.end ) return end < data.end;
    if( start != data.start ) return start < data.start;
    return ( onAir != data.onAir ) ? onAir < data.onAir : recorded < data.recorded;
  }
};

const int MAX_N = 1100;
const int IINF = INT_MAX;
int N,dp[2000][2000],h,m;
vector<int> buf;

int toTime(string s){
  rep(i,(int)s.size()) if( s[i] == ':' ) s[i] = ' ';
  stringstream ss(s);
  ss >> h >> m;
  return h * 60 + m;
}

int main(){
  while( cin >> N, N ){
    vector<Data> vec(N);
    rep(i,N) {
      string a,b,c;
      cin >> a >> b >> c >> vec[i].onAir >> vec[i].recorded;
      vec[i].start = toTime(b), vec[i].end = toTime(c);
    }
    vec.push_back((Data){0,0,0,0});
    rep(i,N+2) rep(j,N+2) dp[i][j] = 0; 
    sort(vec.begin(),vec.end());
    buf.clear();
    int maxi = 0;
    rep(i,vec.size()) buf.push_back(vec[i].end);
    rep(i,vec.size()){
      rep(j,vec.size()){
        if( i == j ) dp[i][j] = max((i?dp[i-1][j]:0),(j?dp[i][j-1]:0));
        else if( i < j ) {
          dp[i][j] = dp[i][j-1];
          int prev = upper_bound(buf.begin(),buf.begin()+j,vec[j].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[i][prev]:0)+vec[j].recorded);
        } else {
          dp[i][j] = dp[i-1][j];
          int prev = upper_bound(buf.begin(),buf.begin()+i,vec[i].start) - buf.begin() - 1;
          dp[i][j] = max(dp[i][j],((prev!=-1)?dp[prev][j]:0)+vec[i].onAir);
        }
        maxi = max(maxi,dp[i][j]);
      }
    }
    printf("%d\n",maxi);
  }
  return 0;
}

Measured memory_bytes: 9448
/*Bismillahir Rahmanir Rahim*///{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std; using __gnu_cxx::power;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
static constexpr auto &os = cout;   // replace by cerr in interactive.
template<class C> static auto dud(C *x)->decltype(os << *x, 0);
template<class C> static char dud(...);
template<class C> typename enable_if<sizeof dud<C>(0)!=1, Dbg&>::type operator<<(const C &x) { os << x; return *this; }
template<class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i!=e and cur<26; i++, cur++) *this << ", "+2*(i==b) << *i;
    return *this << ']';
}
template<class C> typename enable_if<sizeof dud<C>(0)==1, Dbg&>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
}
template<class C, size_t X> typename enable_if<!is_same<C, const char>::value, Dbg&>::type operator <<(C (&x)[X]) {
    return dump(begin(x), end(x));
}
template<class B, class C> Dbg &operator <<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
}
Dbg &operator<<(ostream&(*x)(std::ostream&)) { os << x; return *this; }
} dbg;
void err(istringstream *iss) { delete iss; }
template<class C, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...) if (a) bug(args);
#define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn;//}
const int inf = 1000000007, mxn = 300005, mxsr = 320;

int n, m, ans[mxn], os[mxn];
pair<int, int> a[mxn];

int ccnt = 0;

signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first >> a[i].second;
        int rl = a[i].second-a[i].first;
        ++os[rl];
        for (int j = rl+1; j <= mxsr; j++) {
            ans[j] += ((a[i].first-1)/j+1)*j <= a[i].second;
        }
        int lst = a[i].second+1;
        for (int j = 1; j <= m; j++) {
            int l = (a[i].first-1)/j+1, r = a[i].second/j;
            ccnt = max(ccnt, j);
            if (r <= max(rl, mxsr)) break;
            r = min(r, lst-1), l = max(l, max(rl, mxsr)+1);
            if (l <= r) ++os[r], --os[l-1];
            lst = l;
        }
    }

    int o = 0;
    for (int i = m; i >= 1; i--) {
        o += os[i];
        ans[i] += o;
    }

    for (int i = 1; i <= m; i++) {
        cout << ans[i] << '\n';
    }
}

Measured memory_bytes: 4992
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std ;
#define int                 long long
#define ull                 unsigned long long int
#define ld                  long double
#define vi                  vector<int>
#define vii                 vector<pair<int,int>>
#define vvi                 vector<vector<int>>
#define pii                 pair<int,int>
#define pb                  push_back
#define prec(x)             fixed<<setprecision(15)<<x
#define all(x)              x.begin(),x.end()
#define rall(x)             x.rbegin(),x.rend()
#define rev(x)              reverse(x.begin(),x.end())
#define mset(a,b)           memset(a,b,sizeof(a))
#define mcpy(a,b)           memcpy(a,b,sizeof(a))
#define fo0(i,n)            for(int i=0,i##end=n;i<i##end;i++)
#define fo1(i,n)            for(int i=1,i##end=n;i<=i##end;i++)
#define fo(i,a,b)           for(int i=a,i##end=b;i<=i##end;i++)
#define foe(i,x)            for(__typeof((x).end())i=(x).begin();i!=(x).end();++i)
#define fre(i,x)            for(__typeof((x).rend())i=(x).rbegin();i!=(x).rend();++i)
#define mk(arr,n,type)      type *arr=new type[n];
#define dbg(x) cerr << #x << " is " << x << endl;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0)
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
clock_t beg = clock();
const int inf=1e18;const int mod=1e9+7;const int N=2e5+9;
const double pi = 3.1415926535897932384626433;
void FILE_IO(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}

void JH(){
    int d=0,e=0,f=0,g=0,p=0,w=0,flag=0,ans=0,res=0,cnt=0,sum=0;double f1=0.0,f2=0.0,f3=0.0;
    int n;cin>>n;
    int k=1000;
    if(n%k==0){
        cout<<0<<endl;
    }
    else{
        cout<<k-n%k<<endl;
    }

}

int32_t main()
{
    FILE_IO();FAST_IO;int t=1;
    // cin>>t;
    while(t--){JH();}cerr<<fixed<<setprecision(3)<<(double)(clock()-beg)/1000<<"s ";
}

Measured memory_bytes: 3804
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const double INF=1e9;
struct vec{
     long long x, y;
    long double theta;
     bool operator <(const vec &a)
     {

         if(x*a.x<0)
            return x<a.x;
        else return theta<a.theta;


     }
};
vec v[105];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        long long x,y;
        cin>>x>>y;

        v[i].x=x;
        v[i].y=y;
        if(v[i].x==0)
            {

                if(v[i].y<0)
                    v[i].theta=-INF;
                else v[i].theta=INF;
            }
        else v[i].theta=(long double)y/(long double)x;
        if(v[i].x<0)
        {
            v[i].theta-=2*INF;
        }

    }
    sort(v,v+n);

    long long ans=0;
    for(int i=0;i<n;i++)
    {
        //cout<<v[i].x<<" "<<v[i].y<<'\n';
        long long sumx=v[i].x,sumy=v[i].y;
        ans=max(ans,sumx*sumx+sumy*sumy);
        int u=i+1;
        if(u==n) u=0;
        while(u!=i)
        {


            sumx+=v[u].x;
            sumy+=v[u].y;
            ans=max(ans,sumx*sumx+sumy*sumy);
            //cout<<sumx<<" "<<sumy<<'\n';
            u++;
            if(u==n)
                u=0;
        }
        ans=max(ans,sumx*sumx+sumy*sumy);
        //cout<<ans<<'\n';
    }
    cout<<fixed<<setprecision(20)<<sqrt(ans)<<'\n';


}


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(a)     (a).begin(),(a).end()
#define MOD 1000000007

typedef long long int ll;

const ll INF=(ll)1e18;
//const int INF=(1<<30);

int main(){
  int N;
  cin >> N;
  vector<int> p(N+1);

  REP(i,N)cin >> p[i+1];

  int i = 1;
  int ans = 0;
  while(i<N+1){
    if(p[i]==i){
      ans++;
      i += 2;
    }else{
      i++;
    }
  }
  cout << ans << endl;
}



Measured memory_bytes: 640
N = gets.chomp.to_i
x = []; y = []
N.times do |i|
  x[i], y[i] = gets.chomp.split(" ").map(&:to_i)
end
distance = 0
(0..(N-1)).to_a.combination(2) do |i, j|
  distance += Math.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)
end
puts 2*distance / N

Measured memory_bytes: 1788
#include <stdio.h>
#include <math.h>
int main(){
   long int n, d;
   scanf("%ld%ld", &n, &d);
    long int x, y, ans = 0;
   for(int i = 0; i < n; i++){
       scanf("%ld%ld", &x, &y);
       if(d*d >= x*x + y*y){
           ans++;
       }
   }
   printf("%ld", ans);
   return 0;
}

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


void solve(long long N, long long X, long long Y){
    vector<vector<int>> cost(N,vector<int>(N,N+1));
    vector<int> count(N,0);
    for(int i=0;i<N;i++){
        for(int j = i;j<N;j++){ 
            cost.at(i).at(j) = j-i;    
        }
        for(int j = 0;j<i;j++){ 
            cost.at(i).at(j) = i-j;    
        }
    }
    cost.at(X-1).at(Y-1) = 1;
//Updata
    for(int i=0;i<N;i++){
        for(int j=i+1;j<N;j++){
            cost.at(i).at(j) = min(cost.at(i).at(j),cost.at(i).at(X-1)+cost.at(Y-1).at(j) + 1);
        }
    }
//count
    for(int i=0;i<N;i++){
        for(int j=i;j<N;j++){
            count.at(cost.at(i).at(j))++;
        }
    }
    for(int i=1;i<N;i++){
        cout << count.at(i) << endl;
    }
    return;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    cin >> N;
    long long X;
    cin >> X;
    long long Y;
    cin >> Y;
    solve(N, X, Y);
    return 0;
}


Measured memory_bytes: 16000
import math

n = int(input())
ans = [1]
b = int(math.sqrt(n))
if b >= 2:
    for i in range(2,b+1):
        temp = i
        while True:
            temp*=i
            if temp <= n:
                ans.append(temp)
            else:
                break

print(max(ans))


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

using namespace std;

int main() {
    string s; cin >> s; // 入力
    bool flag = true; // 「踏みやすい」かどうか判定するフラグ,

    // 文字列sの要素数だけループ回す(10文字だと10回回る)
    // 踏みにくいステップ(文字列)を受け取った場合、即座にフラグがfalseになる
    for (int i = 0; i < s.size(); i++) {
        if (i % 2 == 0) { // 偶数ステップ(i=0,2,4,6...)
            if (s[i] == 'L') {
                flag = false; // 踏みにくい！
                break;
            }
        }
        else { // 奇数ステップ(i=1,3,5,7...)
            if (s[i] == 'R') {
                flag = false; // 踏みにくい！
                break;
            }
        }
    }

    // 踏みやすい(flag == true)なら"Yes"、そうでなければ"No"
    cout << (flag ? "Yes" : "No") << endl;
    //return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>     
using namespace std;
  int students[100001];

int main(){
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(NULL);
  int n,i=0; cin >> n;
  
    
  for(size_t k=1;k<=n;++k){
    int x; cin >> x;
    students[x-1] = k;
  }
  for(size_t j=0; j<n-1; ++j ){
    cout << students[j] << " ";
  }
  cout << students[n-1];
 return 0;   
}


Measured memory_bytes: 1280
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <set>

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())

using namespace std;
typedef long long ll;

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

int main(int argc, const char * argv[]) {
    
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    rep (i, n) cin >> a[i];
    
    const int MAX_DIGIT = 50;
    vector<vector<ll>> dp(MAX_DIGIT + 1, vector<ll>(2, -1));

    dp[0][0] = 0;
    for (int d = 0; d < MAX_DIGIT; ++d) {
        ll mask = 1LL<<(MAX_DIGIT - d - 1);
        
        int num = 0;
        for (int i = 0; i < n; ++i) if (a[i] & mask) ++num;
        
        ll cost0 = mask * num;
        ll cost1 = mask * (n - num);
        
        if (dp[d][1] != -1) {
            chmax(dp[d+1][1], dp[d][1] + max(cost0, cost1));
        }
        
        if (dp[d][0] != -1) {
            if (k & mask) {
                chmax(dp[d+1][1], dp[d][0] + cost0);
            }
        }
        
        if (dp[d][0] != -1) {
            if (k & mask) chmax(dp[d+1][0], dp[d][0] + cost1);
            else chmax(dp[d+1][0], dp[d][0] + cost0);
        }
    }
    cout << max(dp[MAX_DIGIT][0], dp[MAX_DIGIT][1]) << endl;
    
    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;
typedef pair<int,int> P;
typedef pair<int,pair<int, int>> P1;

using Graph = vector<vector<int>>;


signed main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  map<int, int> mp;
  int A[N];
  rep(i,N){
    cin >> A[i];
    if(mp[A[i]] != 0){
      cout << "NO" << endl;
      return 0;
    }
    mp[A[i]]++;
  }
  cout << "YES" << endl;
}

Measured memory_bytes: 14336
#include<iostream>
#include<iomanip>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<numeric>
#include<set>
using namespace std;

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

  int ans = b[0];
  for(int i = 1; i < n-1; i++)
    ans += min(b[i-1], b[i]);

  ans += b[n-2];
  
  cout << ans << endl;
  
  return 0;
}

Measured memory_bytes: 256