akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 2000000000;

int solve(const int n, const int c, const int maxw, const vector<int>& v, const vector<int>& w) {
    vector<int> dp(c + 1, -INF);
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = c; j >= w[i]; j--) {
            if (dp[j - w[i]] != -INF) {
                dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
            }
        }
    }
    int ans = 0;
    for (int i = c; i > 0; i--) {
        if (dp[i] >= -maxw) {
            ans = i;
            break;
        }
    }
    return ans;
}

int main() {
    int n, maxw;
    cin >> n >> maxw;
    vector<int> v(n), w(n);
    int maxv = 0;
    for (int i = 0; i < n; i++) {
        int cv, cw;
        cin >> cv >> cw;
        maxv += cv;
        w[i] = cv;
        v[i] = -cw;
    }
    int ans = solve(n, maxv, maxw, v, w);
    
    cout << ans << endl;
    
    return 0;
}

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

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tiii;
typedef pair<ll, ll> pll;

const int INF = 100000000;
const ll LINF = 1e18+5;
const ll M = 1000000007; // modの値

vector<ll> fac(1000000); // n! mod M
vector<ll> ifac(1000000); // k^{M-2} mod M

ll mpow(ll x, ll n){ // x^n(modM)
    ll ans = 1;
    while (n!=0) {
        if(n&1) ans = ans*x % M;
        x = x*x % M;
        n= n>>1;
    }
    return ans;
}

ll comb(ll a, ll b) { // aCb mod M
    if (a==0 && b==0) return 1;
    if (a<b || a<0) return 0;
    ll tmp = ifac[a-b] * ifac[b] % M;
    return tmp * fac[a] % M;
}

void init_comb() {
    fac[0] = 1;
    ifac[0] = 1;
    for (ll i=0;i<1000000;i++) {
        fac[i+1] = fac[i]*(i+1) % M; // n!(mod M)
        ifac[i+1] = ifac[i] * mpow(i+1, M-2) % M; // k^{M-2} mod M
    }
}

int main() {
    ll N, K;
    cin >> N >> K;
    vector<ll> A;
    rep(i,N) {
        ll a;
        cin >> a;
        A.push_back(a);
    }
    sort(A.begin(), A.end());
    init_comb();
    ll min = 0;
    rep(i,N-K+1) {
        ll tmp = (A[i] * comb(N-i-1,K-1))%M;
        min = (min + tmp) % M;
    }
    sort(A.begin(), A.end(), greater<ll>());
    ll max = 0;
    rep(i,N-K+1) {
        ll tmp = (A[i] * comb(N-i-1,K-1))%M;
        max = (tmp + max) % M;
    }
    cout << (max-min+2*M)%M << endl;
    return 0;
}

Measured memory_bytes: 17016
import random, copy

D = int(input())
c = list(map(int, input().split()))
s = []
for i in range(D):
    s.append(list(map(int, input().split())))
t = []
temp = [0] * 26
for i in range(D):
    num = []
    for j in range(26):
        temp2 = [temp[k] + 1 for k in range(26)]
        temp2[j] = 0
        temp3 = s[i][j]
        for k in range(26):
            temp3 -= temp2[k] * c[k]
        num.append(temp3)
    index = num.index(max(num)) + 1
    t.append(index)
dq = [[random.randint(1, D), random.randint(1, 26)] for i in range(10 ** 5)]
last0 = []
last = [0] * 26
score = 0
for i in range(D):
    contest = t[i] - 1
    score += s[i][contest]
    last = [last[i] + 1 for i in range(26)]
    last[contest] = 0
    last0.append(last)
    for j in range(26):
        score -= c[j] * last[j]
ans = t
for i in range(10 ** 5):
    temp = score
    temp2 = copy.copy(t)
    d, q = dq[i][0] - 1, dq[i][1] - 1
    contest = t[d] - 1
    score += s[d][q]
    score -= s[d][contest]
    num = last0[d][q] - last0[d][contest]
    for j in range(d, D):
        if last0[j][q] == 0:
            break
        else:
            score += c[q] * num
            last0[j][q] -= num
    for j in range(d, D):
        if last0[j][contest] == 0:
            if j == d:
                score -= c[contest] * num
                last0[j][contest] += num
                pass
            else:
                break
        else:
            score -= c[contest] * num
            last0[j][contest] += num
    t[d] = q + 1
    if temp < score:
        ans = copy.copy(t)
for i in ans:
    print(i)

Measured memory_bytes: 20012
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;

int N;
pair<int, int> A[100000];

void Input()
{
	cin >> N;
	
	for (int i = 0; i < N; i++) {
		cin >> A[i].first;
		A[i].second = i + 1;
	}
}

int main()
{
	Input();
	sort(A, A + N, [](pair<int, int> a, pair<int, int> b) {return a.first < b.first; });
	for (int i = 0; i < N; i++) {
		if (i) cout << ' ' << A[i].second;
		else cout << A[i].second;
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 1536
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

ll N;

void dfs(ll &ans, vl &open, vvl F, vvl P, ll ind){
  if(ind == 10){
    bool found = false;
    map<ll, ll> cnt;
    rep(i, 10){
      if(open[i] == 0) continue;

      found = true;
      rep(j, N){
	cnt[j] += F[j][i];
      }
    }
    if(!found) return;

    ll profit = 0;
    rep(j, N) profit += P[j][cnt[j]];

    ans = max(ans, profit);
    
    return;
  }

  rep(i, 2){
    open.push_back(i);
    dfs(ans, open, F, P, ind + 1);
    open.pop_back();
  }
}

int main(){
  cin >> N;
  vvl F(N, vl(10));
  rep(i, N){
    rep(j, 10) cin >> F[i][j];
  }
  vvl P(N, vl(11));
  rep(i, N){
    rep(j, 11) cin >> P[i][j];
  }

  ll ans = -(ll)pow(10, 15);
  vl open;

  dfs(ans, open, F, P, 0);

  cout << ans << endl;
  
  return 0;
}


Measured memory_bytes: 512
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>
#include<list>

using namespace std;

#define ll long long int

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 61;

ll mypow(ll x,ll n,ll mod = MOD){
    ll ret = 1;
    while(n > 0){
        if(n&1){
            ret = (ret*x)%mod;
        }
        x = (x*x)%mod;
        n >>= 1;
    }
    return ret;
}

ll mygcd(ll a,ll b){
    if(b == 0)return a;
    return mygcd(b,a%b);
}

ll twoPow(ll shiftNum){
    return (1LL << (shiftNum - 1));
}

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

    vector<vector<ll>> dp(s.size() + 1,vector<ll>(13,0));

    dp[0][0] = 1;

    for(int i = 0; i < s.size(); i++){
        if(s[i] == '?'){
            for(int val = 0; val < 10; val++){
                for(int j = 0; j < 13; j++){
                    dp[i+1][(j*10+val)%13] += dp[i][j];
                    dp[i+1][(j*10+val)%13] %= MOD;    
                }
            }
        }else{
            ll val = (ll)(s[i] - '0');
            for(int j = 0; j < 13; j++){
                dp[i+1][(j*10+val)%13] += dp[i][j];
                dp[i+1][(j*10+val)%13] %= MOD;
            }
        }
    }

    cout << dp[s.size()][5] << endl;

    return 0;
}

Measured memory_bytes: 13648
#include <cstdio>

int main() {
    int n;
    int c1 = 0;
    int c2 = 0;
    int c4 = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int tmp;
        scanf("%d", &tmp);
        if (tmp % 4 == 0) {
            c4++;
        } else if (tmp % 2 == 0) {
            c2++;
        } else {
            c1++;
        }
    }
    if (c1 <= c4) {
        printf("Yes\n");
    } else if (c1 == c4 + 1 && c2 == 0) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

typedef long long int lli;
typedef pair<int, int> ii;
typedef pair<int, lli> il;
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;}

#ifdef LOCAL
	#define eprintf(...) printf(__VA_ARGS__)
#else 
	#define eprintf(...) 1
#endif

#define ROUNDUP(a, b) 		( ((a) + ((b) - 1) ) / b)
#define SET_ZERO(a)	 		( memset((a), 0, sizeof(a)))
#define SET_NUM_ARRAY(a,b)	( memset((a), b, sizeof(a)))

int main() {
	int N;
	char S[109];
	scanf("%d", &N);
	scanf("%s", S);
	string ans(S);

	int cnt = 0;
	int absmax = 0;
	for (int i = 0; i < N; i++) {
		if (ans[i] == '(') cnt++;
		else if (ans[i] == ')') cnt--;
		if (cnt < 0) chmax<int>(absmax, -cnt);
	}	
	
	for (int i = 0; i < absmax; i++) {
		ans = '(' + ans;
		N++;
	}	
	
	cnt = 0;
	for (int i = 0; i < N; i++) {
		if (ans[i] == '(') cnt++;
		else if (ans[i] == ')') cnt--;
	}
	for (int i = 0; i < cnt; i++) {
		ans = ans + ')';
	}
	printf("%s\n", ans.c_str());
	return 0;
}


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

class State{
public:
  int x, y, cost;
  State(){}
  State(int x, int y, int cost): x(x), y(y), cost(cost) {}
};

int h, w, n;
int cx[12], cy[12];
char t[1002][1002];
bool closed[1002][1002];

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

int min_dist(int sx, int sy, int gx, int gy){
  queue<State> open;
  memset(closed, 0, sizeof(closed));
  closed[sy][sx] = true;
  open.push(State(sx, sy, 0));

  while(!open.empty()){
    State st = open.front(); open.pop();

    for(int i = 0; i < 4; i++){
      int nx = st.x + dx[i];
      int ny = st.y + dy[i];

      if(nx == gx && ny == gy) return st.cost + 1;
      if(nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
      if(t[ny][nx] == 'X') continue;
      if(closed[ny][nx]) continue;

      closed[ny][nx] = true;
      open.push(State(nx, ny, st.cost + 1));
    }
  }

  return 999999;
}

int solve(){
  int ans = 0;
  for(int i = 1; i <= n; i++){
    ans += min_dist(cx[i - 1], cy[i - 1], cx[i], cy[i]);
  }
  return ans;
}

int main(){
  while(cin >> h >> w >> n){
    for(int i = 0; i < h; i++){
      cin >> t[i];
      for(int j = 0; j < w; j++){
	if(t[i][j] == 'S'){
	  cx[0] = j;
	  cy[0] = i;
	}
	else if(isdigit(t[i][j])){
	  int id = t[i][j] - '0';
	  cx[id] = j;
	  cy[id] = i;
	}
      }
    }

    cout << solve() << endl;
  }
}

Measured memory_bytes: 3184
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll xpow(ll x, ll y){
    if(y == 0){
        return 1;
    }
    return x * xpow(x, y-1);
}

int main()
{
    ll N, K;
    cin >> N >> K;
    vector<ll> A(N);
    rep(i, N) cin >> A.at(i);
    vector<ll> bits(41, 0);
    rep(i, N){
        rep(j, 41){
            if(A.at(i) % 2 == 1){
                bits.at(j)++;
            }
            A.at(i) /= 2;
        }
    }
    bitset<41> s(K);
    ll tadoru = 0;
    ll ans = 0;
    rep(i, 41){
        if(s.test(40-i)){
            ll t = xpow(2, 40-i) * (bits.at(40-i));
            rep2(j, i+1, 41){
                if(bits.at(40-j) < (N+1)/2){
                    t += xpow(2, 40-j)*(N-bits.at(40-j));
                }
                else{
                    t += xpow(2, 40-j)*(bits.at(40-j));
                }
            }
            chmax(ans, t+tadoru);
            tadoru += xpow(2, 40-i) * (N-bits.at(40-i));
        }
        else{
            tadoru += xpow(2, 40-i) * (bits.at(40-i));
        }
    }
    chmax(ans, tadoru);
    cout << ans << endl;
}

Measured memory_bytes: 4048
#include <iostream>
#include <math.h>
#include <cstdio>
#include <utility>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

int n,a[105];

int main() {
  
    cin>>n;
    
    for(int i=0; i<n; i++){
      cin>>a[i];
      }
   
    for(int i=n-1; i>=0; i--){
      
      if(i==0){
        cout<<a[i]<<endl;
        }
      else{
        cout<<a[i]<<" ";
        }
        
      }

}

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

using namespace std;

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

int N, M, C;

typedef long long ll;

struct Edge {
  int to; ll cost;
};

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

Graph g;

double cntpath[110][110];

struct state {
  int curr; ll cost;
  bool operator < (state const& s) const {
    return cost > s.cost;
  }
};

struct ST {
  int curr; double prob;
};

ll const INF = 1LL<<40;

int main() {
  
  while(cin >> N >> M >> C && (N|M|C)) {
    g.clear(); g.resize(N);
    
    rep(i, M) {
      int a, b; ll c; cin >> a >> b >> c;
      g[a].push_back((Edge){b, c});
      g[b].push_back((Edge){a, c});
    }
    
    ll dist[N][N];
    fill(dist[0], dist[0]+N*N, INF);
    
    rep(S, N) {
      dist[S][S] = 0;
      cntpath[S][S] = 1;
      priority_queue<state> pq;
      pq.push((state){S, dist[S][S]});
      while(!pq.empty()) {
        int curr = pq.top().curr;
        ll cost = pq.top().cost;
        pq.pop();
      
        rep(i, g[curr].size()) {
          Edge& e = g[curr][i];
          if(dist[S][e.to] == cost+e.cost) {
            cntpath[S][e.to] += cntpath[S][curr];
          }
          if(dist[S][e.to] <= cost+e.cost) { continue; }
          cntpath[S][e.to] = cntpath[S][curr];
          dist[S][e.to] = cost+e.cost;
          pq.push((state){e.to, dist[S][e.to]});
        }
      }
    }
    
    rep(i, C) {
      int q; cin >> q;
      if(dist[0][q]+dist[q][N-1] == dist[0][N-1]) {
        printf("%.10f\n", cntpath[0][q]/cntpath[0][N-1]*cntpath[q][N-1]);
      }
      else {
        printf("%.10f\n", 0.);
      }
    }
    cout << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1652
import java.util.Scanner;

public class Main{


	public static void main(String args[]){

		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int num[]= new int[n];
		for (int i=0;i<n;++i){
			num[i]=sc.nextInt();
		}
		int div;
		int counter=0;

		for (int i=0;i<n;++i){
			div =0;
			for (int j=2;j<=(double)Math.pow(num[i],0.5) && div ==0;++j){
				if (num[i]%j == 0) ++div;
			}
			if (div==0) ++counter;
		}
		System.out.println(counter);
	}

}


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

int a[20001],b[20001];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		a[i]=1+30000*(i-1),b[i]=1+30000*(n-i);
	for(int i=1,x;i<=n;i++){
		cin>>x;
		a[x]+=i;
	}
	for(int i=1;i<=n;i++)
		cout<<a[i]<<' ';
	cout<<endl;
	for(int i=1;i<=n;i++)
		cout<<b[i]<<' ';
	cout<<endl;
}



Measured memory_bytes: 768
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

void showIntArray(int a[], int n) {
  for (int i=0; i<n; i++) {
    cout << a[i] << ' ';
  } cout << endl;
}

int main() {
  int A, B, score, tmpMax, tmpMin;
  cin >> A >> B;
  tmpMax = max(A, B);
  tmpMin = min(A, B);
  score = tmpMax;
  tmpMax -= 1;
  score += max(tmpMax, tmpMin);
  cout << score << endl;
  return 0;
}

Measured memory_bytes: 256
s = input()
for bit in range(1 << 3):
    ans = int(s[0])
    f = s[0]
    for i in range(3):
        if bit & (1 << i):
            ans += int(s[i + 1])
            f += "+"
        else:
            ans -= int(s[i + 1])
            f += "-"
        f += s[i + 1]
    if ans == 7:
        print(f + "=7")
        exit()

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
int64_t gcd(int64_t a, int64_t b) {
    if(a < b) return gcd(b, a);
    int64_t r;
    while ((r=a%b)) {
        a = b;
        b = r;
    }
    return b;
}
int main() {
    int64_t A, B;
    cin >> A >> B;
    cout << A * B / gcd(A,B) << endl;
}

Measured memory_bytes: 256
import sys
import heapq

def main():
  input = sys.stdin.readline
  n, m = map(int, input().split())
  s = input()
  
  inf = pow(10, 6)
  ans = [inf]*(n+1)
  key = []
  heapq.heapify(key)
  heapq.heappush(key, (0, n))
  ans[n] = 0
  index = n-1
  count = 0
  while key:
    sub = heapq.heappop(key)
    s1, s2 = sub[0], sub[1]
    if s1 < count:
      continue
    count = s1+1
    subindex = index
    for i in range(min([index, s2-1]), max([-1, s2-m-1]), -1):
      if ans[i] != inf:
        continue
      if s[i] == "1":
        continue
      heapq.heappush(key, (count, i))
      ans[i] = s2
      subindex = i
    index = subindex
    
  if ans[0] == inf:
    print(-1)
  else:
    index = 0
    answer = []
    while True:
      subindex = ans[index]
      answer.append(subindex-index)
      if subindex == n:
        break
      index = subindex
  
    print(*answer)
  
if __name__ == "__main__":
  main()

Measured memory_bytes: 12952
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9+7;

int main(){
    ll n,m;
    cin >> n >> m;
    if(2*n >= m) cout << m/2 << endl;
    else cout << n+((m-2*n)/4) << endl; 
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		
		String s = sc.next();
		int n = s.length() - 1;
		char[] a = new char[s.length()];
        int[] A = new int[s.length()];
        for(int i = 0; i < s.length(); i++){
          a[i] = s.charAt(i);
          A[i] = Character.getNumericValue(a[i]);
        }
		
		for (int i = 0; i < Math.pow(2,n); i++) {
		  int sum = A[0];
          String S = "";
          S += a[0];
          for(int j = 0; j < n; j++){
            if((i & (1 << j)) == 0){
              sum += A[j+1];
              S += '+';
              S += a[j+1];
            }else{
              sum -= A[j+1];
              S += '-';
              S += a[j+1];
            }
          }
          if(sum == 7){
            System.out.println(S + "=7");
            break;
          }
		}
      
	}
}

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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            //[summary]C - Streamline
            var line = ReadLine();
            int n = line[0];
            int m = line[1];

            //地点の数よりコマの数が多いこともある
            //(その場合は移動距離ゼロ)
            if (n >= m)
            {
                Console.WriteLine(0);
            }
            else
            {
                var x = ReadLine();

                //座標の小さい順にソート
                x = x.OrderBy(a => a).ToList();

                //2地点間の距離を取得
                var distances = GetDistances(x).ToList();

                //距離の長い順にソート
                distances = distances.OrderByDescending(a => a).ToList();

                //"コマの数マイナス1"の地点をカットできるので、残りの合計距離を算出
                int sum = distances.Skip(n - 1).Sum();

                Console.WriteLine(sum);
            }
        }

        static IEnumerable<int> GetDistances(List<int> x)
        {
            for (int i = 0; i < x.Count - 1; i++)
            {
                int distance = x[i + 1] - x[i];
                yield return distance;
            }
        }

        static List<int> ReadLine()
        {
            var line = Console.ReadLine().TrimEnd();
            var array = line.Split(' ');
            var numbers = new List<int>();

            foreach (var s in array)
            {
                int num = int.Parse(s);
                numbers.Add(num);
            }

            return numbers;
        }
    }
}

Measured memory_bytes: 24672
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
void read(long long &x){
    char ch;bool ok;
    for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1;
    for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;
}
#define rg register
const int maxn=110;
long long a,b,x,y,l,r,ans,ll,rr;
int aa[maxn],bb[maxn],lena,lenb,len,k;
int main(){
    read(a),read(b);x=a,y=b;
    while(a)aa[++lena]=a&1,a>>=1;
    while(b)bb[++lenb]=b&1,b>>=1;
    for(rg int i=max(lena,lenb);i;i--){
        if(aa[i]!=bb[i]){len=i;break;}
        x-=1ll*aa[i]<<(i-1);
        y-=1ll*bb[i]<<(i-1);
    }
    for(rg int i=len-1;i;i--)if(bb[i]){k=i;break;}
    l=x,r=(1ll<<(len-1))+(1ll<<k)-1;
    ll=(1ll<<(len-1))+x,rr=(1ll<<len)-1;
    if(ll>r)ans=r-l+1+rr-ll+1;
    else ans=rr-l+1;
    printf("%lld\n",ans);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  ll x;
  mint():x(0){}
  mint(ll x):x((x%MOD+MOD)%MOD){}
  mint& fix() { x = (x%MOD+MOD)%MOD; return *this;}
  mint operator-() const { return mint(0) - *this;}
  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^(int b) {mint res = 1;while(b > 0) {if(b&1) {res = (res*(*this));}*this=(*this)*(*this);b>>=1;} return res;}
  mint operator+(const mint& a)const{ return mint(*this) += a;}
  mint operator-(const mint& a)const{ return mint(*this) -= a;}
  mint operator*(const mint& a)const{ return mint(*this) *= a;}
  bool operator<(const mint& a)const{ return x < a.x;}
  bool operator==(const mint& a)const{ return x == a.x;}
};
typedef vector<mint> vm;
typedef vector<vm> vvm;

class Combi {
private:
  v(ll) fac, inv, finv;
  void init(int N) {
    fac[0] = fac[1] = 1; inv[1] = 1; finv[0] = finv[1] = 1;
    for (int i = 2; i < N; 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;
    }
  }
public:
  Combi(int N) : fac(N + 1), inv(N + 1), finv(N + 1) { init(N + 1); }
  ll Cmod(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 Pmod(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[n - k] % MOD;
  }
};
  
int main() {
  int r1,c1,r2,c2;
  cin >> r1 >> c1 >> r2 >> c2;
  Combi c(2000100);
  mint ans = c.Cmod(r2+c2+2,r2+1)-1;
  ans += c.Cmod(r1+c1,r1)-1;
  ans -= c.Cmod(r1+c2+1,c2+1)-1;
  ans -= c.Cmod(r2+c1+1,r2+1)-1;
  cout << ans.x << endl;
  return 0;
}

Measured memory_bytes: 47104
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <stack>
#include <cctype>
#include <cmath>

using namespace std;

/* typedef */

typedef long long ll;

/* constant */

const int INF = 1 << 30;
const int MAX = 100;
const int mod = 1000000007;
const double pi = 3.141592653589;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

/* global variables */
vector< vector<pair<int, int> > > Adj(MAX, vector<pair<int, int> >());
vector<int> color(MAX, WHITE);
vector<int> d(MAX, INF);
vector<int> p(MAX, -1);
int n;

/* function */
void dijkstra();

/* main */
int main() {
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        int u, k; cin >> u >> k;
        for (int j = 0; j < k; j++) {
            int v, c;
            cin >> v >> c;
            Adj[u].push_back(make_pair(v, c));
        }
    }

    dijkstra();

    for (int i = 0; i < n; i++ ) {
        cout << i << ' ' << d[i] << '\n';
    }
   

}

void dijkstra() {

    pair<int, int> p1;
    d[0] = 0;
    p[0] = -1;

    priority_queue<pair<int, int> > PQ;
    PQ.push(make_pair(0, 0));

    while (!PQ.empty()) {

        p1 = PQ.top(); PQ.pop();
        int u = p1.second;
        color[u] = BLACK;

        if ( d[u] < p1.first * (-1) ) continue;

        for (auto p2 : Adj[u]) {

            int v = p2.first;
            int cost = p2.second;

            if (color[v] != BLACK) {
                if (d[u] + cost < d[v]) {
                    p[v] = u;
                    d[v] = d[u] + cost;
                    color[v] = GRAY;
                    PQ.push(make_pair(-d[v], v));
                }
            }
        }
    }

}



Measured memory_bytes: 3148
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) name!=""?freopen(name".in","r",stdin),freopen(name".out","w",stdout):0
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;
#define int long long

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ld eps=1e-10;
const ll md=1e9+7;

///---program start---///

int a[arr];

main()
{
    #ifdef Barik
        files("barik");
        freopen("debug.txt","w",stderr);
    #endif

    int n,l;
    cin>>n>>l;
    for (int i=1;i<=n;i++){
        cin>>a[i];
    }
    for (int i=1;i+1<=n;i++){
        if (a[i]+a[i+1]>=l){
            cout<<"Possible"<<"\n";
            for (int j=1;j<i;j++){
                cout<<j<<"\n";
            }
            for (int j=n-1;j>=i+1;j--){
                cout<<j<<"\n";
            }
            cout<<i<<"\n";
            return 0;
        }
    }
    cout<<"Impossible"<<"\n";
}


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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)

int main()
{
    lint a,b;cin >> a>>b;
    cout << 6-a-b << "\n";
    return 0;
}

Measured memory_bytes: 256
# AOJ 0251: Magic Square
# Python3 2018.6.25 bal4u

# UNION-FIND library
class UnionSet:
	def __init__(self, nmax):
		self.size = [1]*nmax
		self.id = [i for i in range(nmax+1)]
	def root(self, i):
		while i != self.id[i]:
			self.id[i] = self.id[self.id[i]]
			i = self.id[i]
		return i
	def connected(self, p, q): return self.root(p) == self.root(q)
	def unite(self, p, q):
		i, j = self.root(p), self.root(q)
		if i == j: return
		if self.size[i] < self.size[j]:
			self.id[i] = j
			self.size[j] += self.size[i]
		else:
			self.id[j] = i
			self.size[i] += self.size[j]
# UNION-FIND library

while True:
	n, m = map(int, input().split())
	if n == 0: break
	n += 1
	u = UnionSet(n)
	f = [0]*n
	ans = True
	for i in range(m):
		if ans:
			a, b = map(int, input().split())
			f[a] += 1
			f[b] += 1
			if f[a] > 2: ans = False
			if f[b] > 2: ans = False
			if u.connected(a, b): ans = False
			u.unite(a, b)
		else: input()
	print("yes" if ans else "no")



Measured memory_bytes: 11448
// AOJ 2364: Lucky Dip
// 2017.10.4 bal4u@uu
// 2017.12.18

#include <stdio.h>

#define MAX 1000500

/* UNION-FIND library */
int p[MAX], rank[MAX];
void make_set(int x) { p[x] = x, rank[x] = 0; }
void link(int x, int y) {
	if (rank[x] > rank[y]) p[y] = x;
	else { p[x] = y; if (rank[x] == rank[y]) rank[y] = rank[y] + 1;	}
}
int find_set(int x) { if (x != p[x]) p[x] = find_set(p[x]);	return p[x]; }
void union_set(int x, int y) { link(find_set(x), find_set(y)); }

char map[1002][1004];
int v[1002][1002];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
char buf[15], *bp;

int in()
{
	int n = 0;
	while (*bp >= '0') n = (n<<3) + (n<<1) + (*bp++ & 0xf);
	bp++;
	return n;
}

int main()
{
	int w, h, n, i, s, r, c, r2, c2, gr, gc, ans;

	fgets(bp=buf, 15, stdin);
	w = in(), h = in();

	for (i = 0, r = 1; r <= h; r++) {
		fgets(map[r]+1, 1003, stdin);
		for (c = 1; c <= w; c++) {
			v[r][c] = i, p[i] = i, i++;
			if (map[r][c] == 't') gr = r, gc = c;
		}
	}
	for (r = h+1; r >= 0; r--) map[r][0] = map[r][w+1] = '#';
	for (c = w+1; c >= 0; c--) map[0][c] = map[h+1][c] = '#';

	for (r = 1; r <= h; r++) for (c = 1; c <= w; c++) {
		if (map[r][c] == '#') continue;
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (map[r2][c2] != '#')	union_set(v[r][c], v[r2][c2]);
		}
	}
	if (find_set(v[1][1]) == find_set(v[gr][gc])){ puts("0"); return 0; }

	fgets(bp=buf, 10, stdin), n = in();
	for (ans = -1, s = 1; s <= n; s++) {
		fgets(bp=buf, 15, stdin);
		c = in()+1, r = in()+1;
		if (map[r][c] == '.') continue;
		map[r][c] = '.';
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (map[r2][c2] != '#')	union_set(v[r][c], v[r2][c2]);
		}
		if (find_set(v[1][1]) == find_set(v[gr][gc])) { ans = s; break; }
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 11380
// AtCoder agc002

#include <bits/stdc++.h>
#define ld long double
#define ll long long int
#define mod 1000000007
#define ll_inf 1000000000000000
#define int_inf 1000000000
#define pb push_back
#define endl '\n'
#define Endl '\n'
#define eps 1e-9
#define PI acos(-1.0)
#define ii pair<int,int>
#define iii pair<int,ii>
#define se second
#define fi first
using namespace std;
int a[100010];
bool has[100010];
int main(){
    //cout.precision(12);
    //ifstream fin("in");
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)a[i]=1;
    ll ans=0;
    has[1]=1;
    for(int i =0;i<m;i++){
        int x,y;
        cin>>x>>y;
        if(has[x] && a[x]==1){
            has[y]=1; has[x]=0; a[y]++; a[x]--; continue;
        }
        if(has[x] && a[x]>1){
            has[y]=1; a[y]++; a[x]--; continue;
        }
        a[x]--; a[y]++;
    }
    for(int i=1;i<=n;i++){
        if(has[i])ans++;
    }
    cout<<ans<<endl;
}


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

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x.size())
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define uniq(x) x.erase(unique(x.begin(), x.end()), x.end())
#define F first
#define S second
#define mp make_pair
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

/* --- INFの値が適切か確認する --- */
// const int INF = 1 << 30;
// const ll INF = 1LL << 60;
// const int MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> edge[n];
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].eb(b);
    edge[b].eb(a);
  }
  vector<int> c(n);
  rep(i, n) cin >> c[i];

  sort(rall(c));

  ll M = 0;
  vector<int> d(n, -1);
  d[0] = c[0];
  int idx = 1;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();

    rep(i, edge[cur].size()) {
      int next = edge[cur][i];
      if (d[next] != -1) continue;
      d[next] = c[idx];
      M += c[idx++];
      q.push(next);
    }
  }

  cout << M << endl;
  rep(i, n) cout << d[i] << ' ';
  cout << endl;
}


Measured memory_bytes: 1024
#include<stdio.h>
int main()
{
	double xa1,xa2,ya1,ya2,xb1,xb2,yb1,yb2;
	while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&xa1,&ya1,&xa2,&ya2,&xb1,&yb1,&xb2,&yb2)!=EOF){
		if(xa1>xb2||ya1>yb2||xb1>xa2||yb1>ya2){
			printf("NO\n");
		}else{
			printf("YES\n");
		}
	}
	return 0;
}

Measured memory_bytes: 608
#include <iostream>
#include <string>
#include <sstream>

using namespace std;


/*
int stoi(string s){
  int ans = 0;
  for(int i = 0 ; i < s.length() ; i++){
      ans += (s[i] - 48) * pow(10, s.length() - i);
  }
  return ans;
}
*/

int main(void){
  while(1){
    string s;
    int L;

    cin >> s;
    cin >> L;
    if(s == "0" && L == 0){return 0;}

    int c = s.length();
    if(s.length() != L){
      for(int i = 0 ; i < L - c ; i++){
        s = s + "0";
      }
    }

    int a[21];
    a[0] = stoi(s);
    int diff = 0;

    bool stop = false;
    int cur = 1;

    int ansi;
    int ansj;
    int ansa;

//    cout << "s = " << s << endl;

    while(!stop){

      string max = s;
      string min = s;


      /* ???????????????max??¨min????±??????? */
      for(int i = 0 ; i < L-1 ; i++){
        for(int j = i ; j < L ; j++){
          if(max[i] < max[j]){
            char tmp;
            tmp = max[i];
            max[i] = max[j];
            max[j] = tmp;
          }

          if(min[i] > min[j] && min[j] >= 48){
            char tmp;
            tmp = min[i];
            min[i] = min[j];
            min[j] = tmp;
          }
        }
      }

//      cout << "max = " << max << endl;
//      cout << "min = " << min << endl;

      int maxi = stoi(max);
      int mini = stoi(min);
      diff = maxi - mini;


      a[cur] = diff;
      for(int i = 0 ; i <= cur ; i++){
        for(int j = i + 1 ; j <= cur ; j++){
          if(a[i] == a[j]){
            stop = true;
            ansi = j;
            ansj = i;
            ansa = diff;
          }
        }
      }

      cur++;

      stringstream ss;
      ss << diff;
      s = ss.str();

      c = s.length();
      if(s.length() != L){
        for(int i = 0 ; i < L - c ; i++){
          s = s + "0";
        }
      }


//      cout << "s = " << s << endl;

    }

    cout << ansj << " " << ansa << " " << ansi - ansj << endl;

  }

}

Measured memory_bytes: 3016
import sys
input = sys.stdin.readline
def print_ans(S):
    """Test Case
    >>> print_ans("2019/04/30")
    Heisei
    >>> print_ans("2019/11/01")
    TBD
    """
    S = int("".join(S.split("/")))
    if S <= 20190430:
        print("Heisei")
    else:
        print("TBD")


if __name__ == '__main__':
    S = input().rstrip()
    print_ans(S)




Measured memory_bytes: 2940
x,n=input().split()
x=int(x)
n=int(n)
lst=input().split()
for i in range(0,n):
    lst[i]=int(lst[i])
if n==0:
    print(x)
else:
    minv=min(lst)
    maxv=max(lst)
    dis=100000

    for i in range(minv-2,maxv+3):
        if(i not in lst and abs(i-x)<dis):
            dis=abs(i-x)
            clv=i
            
    print(clv)
        


Measured memory_bytes: 9160
#include<stdio.h>
#include<bitset>
int main()
{
	int t,n,i,r,f;
	std::bitset<10001>a[2];
	char s[40004];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%s",&n,s);
		a[0].reset(),a[1].reset();
		a[0][0]=(s[0]=='Y');
		a[1][0]=(s[2*n]=='Y');
		a[0][n]=(s[2*n-1]=='Y');
		a[1][n]=(s[4*n-1]=='Y');
		for(i=1;i<n;++i)
		{
			a[0][i]=(s[2*i-1]=='Y' || s[2*i]=='Y');
			a[1][i]=(s[2*(n+i)-1]=='Y' || s[2*(n+i)]=='Y');
		}
		f=r=0;
		if(a[1].none()) r=n+a[0].count();
		else
		{
			for(i=0;i<n+1;++i)
				if(a[0][i]){++f;break;}
				else if(a[1][i])break;
			for(i=n;i>=0;--i)
				if(a[0][i]){++f;break;}
				else if(a[1][i])break;
			r=n+a[0].count()+a[1].count()+2-f;
		}
		printf("%d\n",r);
	}
	return 0;
}

Measured memory_bytes: 0
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
ll ans=0;
ll N,L;
ll x[300000];
ll t[300000];

int main(){
  
  scanf("%lld %lld",&N,&L);
  for(int i=0;i<N;i++){
    scanf("%lld",&x[i]);
  }
  for(int i=0;i<N;i++){
    scanf("%lld",&t[i]);
  }

  set<int> ls,rs,ts;
  
  for(int i=0;i<N;i++){
    
    ll tmp=t[i]/(2*L);
    ans+=(2*L)*tmp;
    t[i]%=(2*L);
    ll A=x[i];
    ll B=L-x[i];
    bool left = ( t[i] > B*2 );
    bool right = ( t[i] > A*2 );
    if(i==N-1){
      ls.insert(i);
      if( t[i] >B*2)ans+=(2*L);
      continue;
    }
    if(t[i]==0)continue;
    
    if(left&&right){
      ans+=2*L;
      continue;
    }
    
    if(!left&&!right){
      ts.insert(i); 
    }else if(left&&!right){
      rs.insert(i);  
    }else if(!left&&right){
      ls.insert(i);
    }
  }

  set<int> :: iterator it;
  ll pos=-1;
  ll dir=1;
  while(1){
    if(ls.empty() && rs.empty() && ts.empty())break;
    if(dir==1){
      it=ls.lower_bound(pos);
      if(it!=ls.end()){
        if(pos<*it){
          dir=-1;
          pos=*it;
          ls.erase(it);
          continue;
        }
      }
      if(!ts.empty()){
        it=ts.end();
        it--;
        if(pos<*it){
          dir=-1;
          pos=*it;
          ts.erase(it);
          continue;
        }
      }
      dir=-1;
      pos=N+1;
    }else if(dir==-1){
      it=rs.lower_bound(pos);
      if(it!=rs.begin()){
        it--;
        if(*it<pos){
          ans+=2*L;
          dir=1;
          pos=*it;
          rs.erase(it);
          continue;
        }
      }

      it=ts.begin();
      if(it!=ts.end()){
        if(*it<pos){
          ans+=2*L;
          dir=1;
          pos=*it;
          ts.erase(it);
          continue;
        }
      }

      ans+=2*L;
      pos=-1;
      dir=1;
    }
  }


  cout<<ans+2*L<<endl;
  return 0;
}


Measured memory_bytes: 18944
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  int a[100005];
  int ans;
  
  cin >> n;
  for(int i = 0;i < n;i++){
	cin >> a[i];
  }

  ans = a[0];
  for(int i = 1;i < n;i++){
	ans = ans ^ a[i];
  }

  if(ans == 0){
	cout << "Yes" << endl;
  }else{
	cout << "No" << endl;
  }
  return 0;
}



Measured memory_bytes: 640
n, m = gets.split.map(&:to_i)

def count(n, m, result)
  if n != 0 && (n * 2 <= m)
    result += n
    m -= n * 2
    n = 0
  else
    number = n >= m ? 2 : 4
    dn, dm = m.divmod(number)
    result += dn
    n += dn
    m = dm
  end
  return n, m, result
end

result = 0
2.times do
  n, m, result = count(n, m, result)
end
p result

Measured memory_bytes: 1788
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <queue>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200002
#define LIMIT2 500002
typedef long long ll;
typedef long double ld;
typedef const void cv;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define per(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define zt(a,b) (max((a),(b))-min((a),(b)))
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll nPr(int n,int r){ll i,result=1;rep(i,r){result*=(ll)(n-i);}return result;}
ll nCr(int n,int r){ll i,result=1;if(n<r) return 0;rep(i,min(r,n-r)){result*=(ll)(n-i);result/=(ll)(i+1);}return result;}
#define nHr(n,r) nCr((int)((n)+(r)+1),(int)(r))
#define sankaku(x) (((x)*((x)+1))/2)
int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1};
int upint(cv *a, cv *b) { return *(int *)a < *(int *)b ? -1 : *(int *)a > *(int *)b ? 1 : 0; }
int downint(cv *a, cv *b) { return *(int *)a < *(int *)b ? 1 : *(int *)a > *(int *)b ? -1 : 0; }
int upchar(cv *left, cv *right) {return strcmp((char *)left,(char *)right);}
int downchar(cv *left, cv *right) {return strcmp((char *)right,(char *)left);}
void tmpInit(int *c,ll m) {ll i;rep(i,m) c[i]=i;}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1){
      res *= a; if(mod>=1) res %= mod;
    }
    a *= a; if(mod>=1) a %= mod;
    n >>= 1;
  }
  return res;
}
void initialize(){
  
}
int main(void){
  initialize();
  ll n,m,i,j,k,result=0;
  int a[LIMIT2]={0};
  char s[LIMIT1];
  deque<ll> que,ww;
  cin >> k;
  if(k==1){
    cout << 1 << endl;
    return 0;
  }
  n = k;
  while(n%2==0) n/=2;
  while(n%5==0) n/=5;
  if(n==1){
    //cout << 1 << endl;
    //適切な数を掛けることで、10^nの形に表せる
    //return 0;
  }
  vector<ll> dp(k+1,-1);
  dp[1] = 1;
  que.push_front(1);
  ww.push_front(1);
  while(!que.empty()){
    i = que.front();
    que.pop_front();
    j = ww.front();
    ww.pop_front();
    if(dp[(i+1)%k]==-1 || dp[(i+1)%k]>j+1){
      dp[(i+1)%k]=j+1;
      que.push_back((i+1)%k);
      ww.push_back(j+1);
    }
    if(dp[(i*10)%k]==-1 || dp[(i*10)%k]>j){
      dp[(i*10)%k]=j;
      que.push_front((i*10)%k);
      ww.push_front(j);
    }
  }
  cout << dp[0] << endl;
  
  return 0;
}

Measured memory_bytes: 4764
N, x = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
ans = 0
looped = True
for a in A:
    if x < a:
        looped = False
        break
    x -= a
    ans += 1

if looped and x > 0 and ans > 0:
    ans -= 1

print(ans)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(a,b) for(int a=0;a<(b);++a)
#define REP1(i,n) for(int i=1;i<=(n);++i)
#define debug(x) cerr<<#x<<": "<<x<<'\n'
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define INF (1<<29)
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define X first
#define Y second
#define pie 3.14159265358979323846
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;


template<class T=int>
T in(){T x;cin>>x;return (x);}
template<class T>
void print(T& x){cout<<x<<'\n';}

const int MOD =(int)1e9+7;
const int mod =(int)1e9+7;
const int MAX =510000;
ll fac[MAX],finv[MAX],inv[MAX];
void COMint(){
    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 gcd(ll a,ll b){
    if(b==0)return a;
    if(a>b){
        swap(a,b);
    }
    return gcd(a,b%a);
}
ll lcm(ll a,ll b){
    ll g;g=gcd(a,b);
    return a*b/g;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}

bool compare_by_a(pair<int, int> a, pair<int, int> b) {
    if(a.first != b.first){
        return a.first < b.first;
    }else{
        return a.second < b.second;
    }
}
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;
}

ll RS(ll N,ll P,ll M){
    if(P==0){
        return 1;
    }else{
        if(P%2==0){
            ll t=RS(N,P/2,M);
            return t*t%M;
        }else{
            return N*RS(N,P-1,M)%M;
        }
    }
}
bool greater_pair(pair<ll,ll> a,pair<ll,ll> b){
    if(a.first!=b.first){
        return a.first>b.first;
    }else{
        return a.second>b.second;
    }
}
int main() {
    ios::sync_with_stdio(false);
    int i=in();
    if(i==1){
        cout<<"Hello World"<<endl;
    }else{
        int A,B;
        cin>>A>>B;
        cout<<A+B<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int cnt;
void insertionSort(int A[], int n, int g)
{
    
    for( int i = g; i < n; ++i ){
        int tmp = A[i];
        int j = i - g;
        while( -1 < j && tmp < A[j] ){
            
            A[j+g] = A[j];
            j = j - g;
            cnt++;
        }
        A[j+g] = tmp;
    }
}

void shellSort(int A[], int n)
{
    cnt = 0;
    int m;
    int G[100];
    for( int i = 0; i < 100; ++i ){
        if( i == 0 ) G[i] = n / 2 + 1;
        else G[i] = G[i-1] / 2 - 1;
        if( G[i] < 0 ){
            G[i-1] = 1;
            G[i] = 0;
            m = i;
            break;
        }
    }
    
    for( int i = 0; i < m; ++i )
        insertionSort(A, n, G[i]);
    
    printf("%d\n", m);
    for( int i = 0; i < m; ++i ){
        if( i ) printf(" ");
        printf("%d", G[i]);
    }
    printf("\n%d\n", cnt);
    for( int i = 0; i < n; ++i )
        printf("%d\n", A[i]);
}

int main()
{
    int n;
    scanf("%d", &n);
    int A[n];
    for( int i = 0; i < n; ++i )
        scanf("%d", &A[i]);
    
    shellSort(A, n);
    
    return 0;
}


Measured memory_bytes: 5636
N=int(input())
t=x=y=0
for i in range(N):
    t_,x_,y_=map(int,input().split())
    k=t_-t-(x_-x+y_-y)
    if k<0 or k%2==1:
        print("No")
        quit()
    t=t_
    x=x_
    y=y_
print("Yes")


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

int n, a, b, t = 0;
string p = "", q="";
bool visited[9] = {false};

void solve(int i, string tmp){
    if (i == n){
        ++t;
        if (tmp == p)
            a = t;
        if (tmp == q)
            b = t;
        return;
    }
    for (int j = 1 ; j <= n ; ++j){
        if (!visited[j]){
            visited[j] = true;
            string tmp2 = tmp;
            tmp2 += (j+'0');
            solve(i+1, tmp2);
            visited[j] = false;
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    int x;
    cin >> n;
    for (int i = 0 ; i < n ; ++i){
        cin >> x;
        p += (x+'0');
    }
    for (int j = 0 ; j < n ; ++j){
        cin >> x;
        q += (x+'0');
    }
    solve(0, "");
    cout << abs(a-b);
    return 0;
}


Measured memory_bytes: 256
package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scan(&n)
    js := make([][]int, n)
    for i := range js {
        var v int
        fmt.Scan(&v)
        js[i] = []int{v}
    }
    for i := range js {
        var c int
        fmt.Scan(&c)
        js[i] = append(js[i], c)
    }
    var sum int
    for i := range js {
        if js[i][0] > js[i][1] {
            sum += js[i][0] - js[i][1]
        }
    }
    fmt.Println(sum)
}


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

using namespace std;

int main()
{
	int n, a[100000];
	long long b = 0, c = 0;
	cin >> n;
	
	for (int i = 0; i < n; i++)
		cin >> a[i];
		
	sort(a, a+n);
	
	for (int i = 0; i < n-1; i++)
		if (a[i] == a[i+1])
		{
			if (a[i] > c)
			{
				b = c;
				c = a[i];
			}
			else if (a[i] > b)
				b = a[i];
			
			i++;
		}
		
	cout << b * c;
}

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

#define int long long
#define xxx int32_t
#define fuck_u return 0;
const int inf = 1e18L+5;

xxx main() {
    int n, w;
    cin>>n>>w;
	vector<int> weight(n), value(n);
    for(int i=0; i<n; ++i) {
		cin>>weight[i]>>value[i];
	}
	int sum_val = 0;
	for(int i: value) {
		sum_val += i;
	}
	vector<int> dp(sum_val+1, inf);
	//int dp[sum_val+1];
	//memset(dp, inf, sizeof(dp));
	dp[0] = 0;
	for(int i=0; i<n; ++i) {
		for(int j=sum_val-value[i]; j>=0; --j) {
			dp[value[i]+j] = min(dp[value[i]+j], dp[j]+weight[i]);
		}
	}
	int result = 0;
	for(int i=0; i<sum_val+1; ++i) {
		if(dp[i]<=w)
			result = i;
	}
    cout<<result<<"\n";
	fuck_u
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define taskname "A"
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
const int maxn = 2e5 + 5;

int main(){
    if(fopen(taskname".INP","r")){
        freopen(taskname".INP","r",stdin);
        freopen(taskname".OUT","w",stdout);
    }
    string s;cin >> s;s = " " + s;
    int n = s.size() - 1;
    for(int i = 1 ; i < n ; ++i){
        if(s[i] != s[n - i])return cout << -1 , 0;
    }
    if(s[1] == '0' || s[n] == '1')return cout << -1 , 0;
    int pre = 1;
    for(int i = 2 ; i <= n ; ++i){
        if(s[i] == '1'){
            for(int j = pre ; j < i ; ++j)cout << i << " " << j << endl;
            pre = i;
        }
    }
    cout << n - 1 << " " << n << endl;
}



Measured memory_bytes: 1660