akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cassert>

using namespace std;

typedef unsigned int index;

int main()
{
#ifdef ONPC
	assert(freopen("a.in", "r", stdin));
	assert(freopen("a.out", "w", stdout));
#else
#endif
	size_t n;
	cin >> n;
	vector<long long> a(n), b(n);
	long long ans = 0;
	for (index i = (index)n - 1; i != (index)-1; i--)
	{
		cin >> a[i] >> b[i];
	}
	for (index i = 0; i < n; i++)
	{
		a[i] += ans;
		ans += (b[i] - ((a[i] + b[i]) % b[i])) % b[i];
	}
	cout << ans << endl;
}


Measured memory_bytes: 1792
# ALDS1_11_B: Depth First Search

class Timer
  def initialize(start = 0)
    @time = start
  end

  def next
    @time += 1
  end
end

class DepthFirstSearcher
  def initialize(graph)
    @graph = graph
  end

  def search_by(method_name, found, finished)
    last  = @graph.size - 1
    timer = Timer.new
    proc  = self.method "#{method_name.to_s}_search"
    (1..last).each do |id|
      proc.call(id, found, finished, timer)
    end
  end

  private

    def recursive_search(id, found, finished, timer)
      return unless found[id].nil?

      found[id] = timer.next
      @graph[id].each do |d_id|
        recursive_search d_id, found, finished, timer
      end
      finished[id] = timer.next
    end

    def stack_search(id, found, finished, timer = Timer.new)
      return unless found[id].nil?

      stack = [id]
      found[id] = timer.next

      until stack.empty?
        id = stack.last
        next_id = @graph[id].find do |d_id|
          found[d_id].nil?
        end

        if next_id.nil?
          finished[id] = timer.next
          stack.pop
        else
          if found[next_id].nil?
            stack.push next_id
            found[next_id] = timer.next
          end
        end
      end
    end
end

n_vertexes = gets.to_i
max_v      = n_vertexes + 1
graph      = Array.new, max_v

n_vertexes.times do
  id, n_degrees, *degrees = gets.split.map &:to_i
  graph[id] = degrees
end

searcher = DepthFirstSearcher.new graph
found    = Array.new max_v
finished = Array.new max_v
searcher.search_by :stack, found, finished

(1..n_vertexes).each do |id|
  puts "#{id} #{found[id]} #{finished[id]}"
end

Measured memory_bytes: 8820
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i,cc,n) for(int i=cc;i<=n;++i)
#define drep(i,cc,n) for(int i=cc;i>=n;--i)
typedef long long ll;
using namespace std;
int main() {
    int n;
    cin>>n;
    int a[n+1],b[n];
    rep(i,0,n) cin>>a[i];
    rep(i,0,n-1) cin>>b[i];
    ll ans=0;
    rep(i,0,n-1){
        if(b[i]==0) continue;
        if(b[i]<=a[i]){
            ans+=b[i];
            continue;
        }
        if(b[i]<=a[i]+a[i+1]){
            ans+=b[i];
            b[i]-=a[i];
            a[i+1]-=b[i];
            continue;
        }
        ans+=a[i]+a[i+1];
        a[i+1]=0;
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1024
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A, B = map(int, readline().split())

    print((A + B) % 24)

    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 9028
#include<stdio.h>
int main(void)
{
	int a,b,c,d;
	scanf("%d %d %d %d",&a,&b,&c,&d);
	if(a==b && c==d){
		printf("yes\n");
	}
	else if(a==c && b==d){
		printf("yes\n");
	}
	else if(a==d && b==c){
		printf("yes\n");
	}
	else{
		printf("no\n");
	}
	return 0;
}


Measured memory_bytes: 2692
# 出題: プロコンのためのアルゴリズムとデータ構造 3.4

def printList(array):
    print(' '.join(map(str, array)))

# N個の要素を持つ配列A

def selectionSort(A, N):
    swapCount = 0
    for index in range(0, N):
        minj = index
        for j in range(index, N):
            if A[j] < A[minj]:
                minj = j

        if index != minj:
            swapCount += 1

        A[index], A[minj] = A[minj], A[index]

    printList(A)
    print(str(swapCount))


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    selectionSort(A, N)



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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
typedef long long ll;
const int INT_INF = 1e9;
const ll INF = 1LL << 30;
const ll MOD = 1e9 + 7;

V<ll> len, pat;

ll rec(ll n, ll x){
    if(n == 0){
        if(x == 1) return 1;
        else return 0;
    }
    if(x == 1) return 0;
    if(x == len[n]) return pat[n];

    ll ans = 0;
    if(len[n] / 2 + 1 <= x){
        ans += pat[n - 1] + 1 + rec(n - 1, x - len[n] / 2 - 1);
    }
    if(len[n] / 2 + 1 > x){
        ans += rec(n - 1, x - 1);
    }
    return ans;
}

int main() {
    ll n, x; cin >> n >> x;

    len.resize(n + 1);
    pat.resize(n + 1);
    len[0] = pat[0] = 1;
    for(int i = 1; i <= n; i++){
        len[i] = len[i - 1] * 2 + 3;
        pat[i] = pat[i - 1] * 2 + 1;
    }
    cout << rec(n, x) << endl;

}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main (String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		String[] s = scan.next().split("");
		int max=0;
		int x = 0;
		for(int i=0; i<n; i++) {
			x += s[i].equals("I") ? 1 : -1;
			max = Math.max(x, max);
		}
		System.out.println(max);
	}

}

Measured memory_bytes: 22996
//                          VIVEK UPADHYAY
//                         MANIT, BHOPAL(M.P)
/**********************************************************************************************/
/**********************************************************************************************/
//                        $$ JAB-TAK TODEGA NHI,
//                         TAB -TAK CHODEGA NHI :D
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<cstdlib>
#include<unistd.h>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<set>
#include<unordered_map>
#include<cstdio>
#include<map>
#include<numeric>
using namespace std;
#define F first
#define S second
#define mk(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define LINE cout<<endl ;
#define ok cout<<"ok\n" ;
typedef long long ll;
const int MAXN = 3*1e7 + 10 ;
const int MAX = 1e7 ;
double pi = 3.1415926535897932384626433832795;
const ll inf=1e18 ;
int main() {
    string s ;
    cin >> s ;
    for(int i=0;i<s.length();i++){
        if(s[i]=='1') s[i]='9' ;
        else s[i]='1' ;
    }
    cout<<s<<endl;
    return 0 ;
    
}


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

using namespace std;

int main() {
	int n;
	cin >> n;
	int b = 10000, r = 0;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		if (b == a) {
			a = 10000;
			r++;
		}
		b = a;
	}
	cout << r << endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <stdio.h>
#include <tuple>
#include <bitset>
#include <cfloat>
#include <fstream>
#include <limits.h>
#include <list>
#include <math.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <numeric>
#include <utility>
#include <sstream>

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

using namespace std;
#define int long long
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repr(i,n) for(ll i=n;i>=0;i--)
#define P pair<ll, ll>
#define sz(x) (ll)x.size()
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define VE vector<ll>
#define COUT(x) cout<<(x)<<endl
#define MA map<ll,ll>
#define SE set<ll>
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,VE,greater<ll>>
#define COUT(x) cout<<(x)<<endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define EPS (1e-10)
#define pb push_back

const long long MOD = 1000000007;
//const long long MOD = 998244353;
const long long INF = 1LL << 60;


long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0) return a;
	unsigned r;
	while ((r = a % b)) {
		a = b;
		b = r;
	}
	return b;
}
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a * g / b;
}
bool prime(ll n) {
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n%i == 0)return false;
	}
	return n != 1;
}




const long double PI = 3.14159265359;
const int MAX_N = 100010;

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

int n, m;
char field[1010][1010];
int d[1010][1010];
int sx=0, sy=0;
int gx=0, gy=0;

void bfs() {
	rep(i, n)rep(j, m)d[i][j] = -1;
	queue<P>que;
	rep(i, n)rep(j, m) {
		if (field[i][j] == '#') {
			d[i][j] = 0; que.push(P(i, j));
		}
	}
	//d[sx][sy] = 0;
	while (!que.empty()) {
		P p = que.front(); que.pop();
		//if (p.first == gx && p.second == gy)break;
		rep(i, 4) {
			int nx = p.first + dx[i];
			int ny = p.second + dy[i];
			if (0 <= nx && nx < n && 0 <= ny && ny < m && /*field[nx][ny] != '#' &&*/ d[nx][ny] == -1) {
				que.push(P(nx, ny));
				d[nx][ny] = d[p.first][p.second] + 1;
			}
		}
	}

}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m;
	rep(i, n)rep(j, m)cin >> field[i][j];
	bfs();
	int ans = 0;
	rep(i, n)rep(j, m) {
		chmax(ans, d[i][j]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 25524
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
using ll = long long;
using ld = long double;
#define vl vector<long long>
#define vvl vector<vector<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++)
using pll = pair<long long,long long>;
#define cY cout<<"Yes"<<endl
#define cN cout<<"No"<<endl
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const long double pi = (acos(-1));
#define pb push_back
#define vs vector<string>

//最大最小の入れ替え
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;
}

//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
//コンビネーション
ll nCr(ll n, ll r) {
    ll ans = 1;
    for (ll i = n; i > n - r; --i) {
        ans = ans*i;
    }
    for (ll i = 1 ; i < r + 1; ++i) {
        ans = ans / i;
    }
    return ans;
}
//素因数分解
map< ll, ll > prime_factor(ll n) {
  map< ll, ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

//切り上げの割り算は(a + b - 1) / bでできる

//ここから
int main(){
    ll N,M;
    cin>>N>>M;
    vl A(N);
    rep(i,N)cin>>A[i];
    vector<pll> CB(M);
    rep(i,M){
        cin>>CB[i].second>>CB[i].first;
    }
    sort(all(CB));
    reverse(all(CB));
    sort(all(A));
    ll k=0;
    bool end=false;
    rep(i,M){
        ll a=CB[i].first;
        ll b=CB[i].second;
        for(ll j=b;j>0;j--){
            if(A[k]<a){
                A[k]=a;
                k++;
            }else{
                end=true;
                break;
            }
        }
        if(end) break;
    }
    ll Ans=0;
    rep(i,N){
        Ans+=A[i];
    }
    cout<<Ans<<endl;
}


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

import sys

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

N=INT()

cnt=0
for i in range(1, N+1):
    si=str(i)
    if len(si)%2==1:
        cnt+=1
print(cnt)


Measured memory_bytes: 3064
n, x, y = map(int, input().split())

k = [0]*(n-1)
for i in range(1, n):
  for j in range(i+1, n+1):
    dis = min(j-i, abs(j-y)+abs(i-x)+1, abs(j-x)+abs(i-y)+1)
    k[dis-1] += 1

for j in k:
  print(j)

Measured memory_bytes: 3444
#pragma GCC optimize("O3")

#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 long long ll;
typedef pair<int, int> pi;
tree<int,int,greater<int>,rb_tree_tag,tree_order_statistics_node_update> map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define pb push_back
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define REV(x) reverse(x.begin(),x.end())
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int a,b,c;
    cin >> a >> b >> c;
    swap(a,b);
    swap(a,c);
    cout << a << " " << b << " " << c;

    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <list>
#include <bitset>
#include <vector>
using namespace std;

#define I64d_OJ

//#ifdef I64d_OJ
//#define LL __int64
//#endif // I64d_OJ
#ifdef I64d_OJ
#define LL long long
#endif // I64d_OJ

typedef unsigned LL ULL;
typedef unsigned uint;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<LL,LL> pLL;
typedef vector<int> vi;

inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-f; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
#define MID(a,b) (((LL)(a)+(LL)(b))>>1)
#define absx(a) ((a)<0?-(a):(a))
#define MK(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define lowbit(x) ((x)&-(x))

#define lc c[x][0]
#define rc c[x][1]

template< typename T >
inline void Max(T &a,T &b){if(a<b) a=b;}
template< typename T>
inline void Min(T &a,T &b){if(a>b) a=b;}

const double pi=(double)acos(-1.0);
const double eps=(double)1e-8;
const int INF = (int) 0x3f3f3f3f;
const int MOD = (int) 1e9+7;
const int MAXN =(int) 2e5+10;
const int MAXM = (int) 3e6+10;

///--------------------START-------------------------
int X;

void work(){
    LL A,B;
//    for(int i=1;i<=10000;i++){
//        if((LL)i*i*i*i*i>=1000000000000000000LL){
//            printf("%d %lld",i,(LL)i*i*i*i*i);
//            return;
//        }
//    }
    scanf("%d",&X);
    for(A=-3982;A<=3982;A++){
        for(B=-3982;B<=3982;B++){
            if(A*A*A*A*A-B*B*B*B*B==(LL)X){
                printf("%lld %lld\n",A,B);
                return;
            }
        }
    }
}

///--------------------END-------------------------
int main(){
#ifdef NNever
//freopen("data.in","r",stdin);
///freopen("out.txt","w",stdout);
#endif // NNever
    work();
    return 0;
}










Measured memory_bytes: 3816
from collections import Counter

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

ans = 0

diff = [i - A[i] for i in range(N)]
summ = [i + A[i] for i in range(N)]

cdiff = Counter(diff)

for i in range(N):
    if summ[i] in cdiff.keys():
        ans += cdiff[summ[i]]

print(ans)


Measured memory_bytes: 50592
#!/usr/bin/python3
# -*- coding: utf-8 -*-
a, b, m = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
min_cost = min(a_list) + min(b_list)
for i in range(0, m):
    ai, bi, c = map(int, input().split())
    if a_list[ai-1] + b_list[bi-1] - c < min_cost:
        min_cost = a_list[ai-1] + b_list[bi-1] - c
print(min_cost)


Measured memory_bytes: 18736
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n, m;
	scanf("%d %d", &n, &m);

	vector<bool> pos(n);
	vector<int> cnt(n, 1);
	pos[0] = true;
	for (int i = 0; i < m; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		--a; --b;

		pos[b] = (pos[b] || pos[a]);
		cnt[a] -= 1;
		cnt[b] += 1;
		if (cnt[a] == 0) {
			pos[a] = false;
		}
	}

	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans += pos[i] ? 1 : 0;
	}
	printf("%d\n", ans);
	return 0;
}

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

typedef long long lint;
typedef pair<int,int> pii;
typedef long double louble;

#define fi first
#define se second

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;
	
	char buf[bufl],*s=buf,*t=buf;
	
	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}
	
	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
}
using ae86::ty;

const int _ = 503;

int mo;

template<typename T1,typename T2> inline T1 ad(T1 &a,T2 b){return a=a+b>=mo?a+b-mo:a+b;}
template<typename T1,typename T2> inline T1 dl(T1 &a,T2 b){return a=a>=b?a-b:a-b+mo;}

int n,nn;pii p[_];
lint f[_][_]={0};

inline int lbound(int i){int x=nn-1;while(x>=0 && x*x+i*i>=n*n)x--;return x+1;}
inline int rbound(int i){int x=nn-1;while(x>=0 && x*x+i*i>nn*nn)x--;return x+1;}

int main()
{
	n=ty(),mo=ty(),nn=n+n;

	for(int i=0;i<nn;i++){if(i<n)p[i]=pii(lbound(i),rbound(i));else p[i]=pii(rbound(i),-1);}

	sort(p,p+nn);
	
	lint ans=0;
	for(int k=0;k<=n;k++)
	{
		memset(f,0,sizeof(f)),f[0][0]=1;
		int cf=0,cs=0;
		for(int i=0;i<nn;i++)
		{
			for(int j=0;j<=k;j++)
			{
				if(p[i].se<0)ad(f[i+1][j],f[i][j]*(p[i].fi-cs-j)%mo);
				else
				{
					if(j<k)ad(f[i+1][j+1],f[i][j]*(p[i].fi-cs-j)%mo);
					ad(f[i+1][j],f[i][j]*(p[i].se-n-k-cf+j)%mo);
				}
			}
			if(p[i].se<0)cs++;else cf++;
		}
		ad(ans,f[nn][k]*(k&1?mo-1:1)%mo);
	}

	printf("%lld\n",ans);
	
	return 0;
}


Measured memory_bytes: 2176
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>

std::vector<std::vector<int>> set_vec;
bool *done;
std::vector<int> *vv;

void insert_set(int x);

int main()
{
	int n, m, x, y, *p, count{};

	scanf("%d%d", &n, &m);
	p = new int[n];
	done = new bool[n];
	vv = new std::vector<int>[n];
	std::fill(done, done + n, false);

	for (int i = 0; i < n; ++i)
	{
		scanf("%d", p + i);
		p[i]--;
	}
	for (int i = 0; i < m; ++i)
	{
		scanf("%d%d", &x, &y);
		x--; y--;
		vv[x].push_back(y);
		vv[y].push_back(x);
	}
	for (int i = 0; i < n; ++i)
	{
		if (done[i]) continue;
		set_vec.push_back({});
		insert_set(i);
	}

	for (auto& e : set_vec)
	{
		std::sort(e.begin(), e.end());
		for (auto& f : e)
			if (std::binary_search(e.begin(), e.end(), p[f]))
				count++;
	}

	printf("%d\n", count);

	delete[] p;
	delete[] done;
	delete[] vv;

	return 0;
}

void insert_set(int x)
{
	if (done[x]) return;
	done[x] = true;
	set_vec.back().push_back(x);

	for (auto e : vv[x])
	{
		insert_set(e);
	}
}

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

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt","r", stdin);
    //freopen("output.txt","w",stdout);
    int h,n; cin>>h>>n;
    long long a[n]={}, b[n]={};
    long long dp[n][h+1]={};
    for(int i=0;i<n;i++) cin>>a[i]>>b[i];

    for(int i=0;i<n;i++)
    {
        for(int j=1;j<=h;j++)
        {
            if(i==0)
            {
                if(j%a[i]==0) dp[i][j] = b[i]*(j/a[i]);
                else dp[i][j] = b[i]*(j/a[i]+1);
                continue;
            }

            if(j<a[i]) dp[i][j] = min(dp[i-1][j],b[i]);
            else dp[i][j] = min(dp[i-1][j],dp[i][j-a[i]]+b[i]);
        }
    }

    cout<<dp[n-1][h];
}

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

int main()
{
    int x = 10;
    int gunung[x];

    for (x=0 ; x<10 ; x++)
    {
        cin >> gunung[x];
    }

    int pertama = 0;
    int kedua = 0;
    int ketiga = 0;

    for (x=0 ; x<10 ; x++)
    if (gunung[x] > pertama)
    {
        ketiga = kedua;
        kedua = pertama;
        pertama = gunung [x];
    }
    else if (gunung [x] > kedua)
    {
        ketiga = kedua;
        kedua = gunung [x];
    }
    else if (gunung [x] > ketiga)
    {
        ketiga = gunung[x];
    }

    cout << pertama << endl;
    cout << kedua << endl;
    cout << ketiga << endl;

    return 0;
}

Measured memory_bytes: 3068
/* _________________________________________________________________________________________________
  |                                                                                                 |
  |                             Author      : Aditya Ahuja                                          |
  |                             Date        : Thu, 20th Jun 2019                                    |
  |_________________________________________________________________________________________________|
*/
 
#include<bits/stdc++.h>
using namespace std;

#define int long long

#define pb          push_back
#define F           first
#define S           second
#define rep(i,a,b)  for(int i=a; i<b; i++)
#define all(a)      (a).begin(),(a).end()
#define ret(x)      {cout << (x) << endl; return;}
#define sz(x)       (int)x.size()
#define type(x)     typeid(x).name()

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;

#define wa(x)       cout << (#x) << " -------- " << (x) << endl
#define pvi(v)      {for(auto it:v) cout << it << " "; cout << endl;}
#define line1	    printf("---------------------------\n")

// #define wa //
// #define line1 //
// #define pvi //
// #define printf //

void solve();
signed main() {
	//ios_base::sync_with_stdio(false); cin.tie(NULL);
	////cout << fixed << setprecision(10);

	int t=1; 
	//cin>>t;
	while(t--) solve();
	return 0;
}

//___________________________________________________________________________________________________
// 
void solve() {

	int n,w;
	cin >> n >> w;

	vi dp(w+1,0);

	rep(i,0,n){

		int wi,vi;
		cin >> wi >> vi;

		for(int wa = w; wa >= wi; wa--){
			dp[wa] = max(dp[wa], dp[wa-wi] + vi);
		}

		// pvi(dp);
		// line1;
	}

	ret(*max_element(all(dp)));
}



Measured memory_bytes: 1024
while true
  h, w = gets.chomp.split(/\s/).map(&:to_i)
  break if h == 0 && w == 0
  pattern = %w(# .)
  h.times do |i|
    puts (pattern * (w / 2 + 1))[0..(w - 1)].join("")
    pattern.reverse!
  end
  puts ""
end

Measured memory_bytes: 9172
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>


int main(int argc, char const *argv[]) {
  int n,a,b,c;
  scanf("%d", &n);
  a = n / 100;
  b = (n-a*100)/10;
  c = n-a*100-b*10;
  if(b > a){
    if(a != 9){
      a++;
      n = a*100+a*10+a*1;
    }else{
      n = 1111;
    }
  }else if(b == a){
    if(c > b){
      a++;
      n = a*100+a*10+a*1;
    }else if(c == b){
    }else{
      n = a*100+a*10+a*1;
    }
  }else{
    n = a*100+a*10+a*1;
  }
  printf("%d",n);
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B,digit1,digit2,digit3,digit4,cnt;
  cin >> A >> B;
  cnt = 0;
  
  for (int i = A; i <= B;i++){
	digit1 = i % 10;
	digit2 = i % 100 / 10;
	digit3 = i % 10000 / 1000;
	digit4 = i / 10000;
	if(digit1 == digit4 && digit2 == digit3){
		cnt ++;
    }
  }
  cout << cnt << endl;
}

Measured memory_bytes: 256
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

public class Main {
	static boolean debug = false;

	public static void main(String[] args) throws IOException {

		UserScanner scan = new UserScanner();
		PrintWriter pwriter = new PrintWriter(System.out);

		int vn = scan.nextInt();
		int en = scan.nextInt();

		Travel t = new Travel(vn, en);

		for (int i = 0; i < en; i++)
			t.addRoot(scan.nextInt(), scan.nextInt(), scan.nextInt());

		pwriter.println(t.getDistance());

		pwriter.flush();
		scan.close();
		System.exit(0);
	}
}

class Travel {
	int[][] dist;
	int goal;
	int[] ix;
	int[][] hist;

	public Travel(int vn, int en) {
		dist = new int[vn][vn];
		for (int st = 0; st < vn; st++)
			for (int ed = 0; ed < vn; ed++)
				dist[st][ed] = -1;

		// index number for each vertices
		ix = new int[vn];
		ix[1] = 1;
		for (int i = 2; i < vn; i++)
			ix[i] = ix[i - 1] << 1;
		// all vertices is reached
		goal = (ix[vn - 1] << 1) - 1;

		hist = new int[vn][goal + 1];
		for (int i = 0; i < vn; i++)
			for (int j = 0; j < goal + 1; j++)
				hist[i][j] = Integer.MAX_VALUE;
	}

	public int getDistance() {
		return minDist(0, 0);
	}

	private int minDist(int st, int passed) {
		// st is start vertices, passed is sum of reached vertices index
		if (passed == goal)
			if (dist[st][0] >= 0)
				return dist[st][0];
			else
				return -1;

		if (hist[st][passed] < Integer.MAX_VALUE)
			return hist[st][passed];

		int min = -1;
		for (int i = 1; i < ix.length; i++) {
			if ((passed & ix[i]) != 0 || dist[st][i] == -1)
				continue;
			int d = minDist(i, (passed | ix[i]));
			if (d < 0)
				continue;
			d += dist[st][i];
			if (d >= 0 && (d < min || min == -1))
				min = d;
		}
	
		hist[st][passed] = min;
		return min;
	}

	public void addRoot(int s, int t, int d) {
		dist[s][t] = d;
	}

}

class UserScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean read() throws IOException {
		ptr = 0;
		buflen = in.read(buffer);
		if (buflen <= 0)
			return false;
		else
			return true;
	}

	private byte getByte() throws IOException {
		if (ptr >= buflen)
			read();
		if (isCtlSpace(buffer[ptr])) {
			return -1;
		} else
			return buffer[ptr++];
	}

	private void skipCtlSpace() throws IOException {
		if (ptr >= buflen)
			read();
		if (isCtlSpace(buffer[ptr])) {
			ptr++;
			skipCtlSpace();
		}
	}

	private boolean isCtlSpace(byte b) {
		if (b <= ' ' || b > '~')
			return true;
		else
			return false;
	}

	public void close() throws IOException {
		in.close();
	}

	public String next() throws IOException {
		skipCtlSpace();
		StringBuilder sb = new StringBuilder();
		byte b;
		while ((b = getByte()) != -1) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() throws IOException {
		skipCtlSpace();
		int n = 0;
		boolean minus = false;
		byte b;
		while ((b = getByte()) != -1) {
			if (b == '-')
				minus = true;
			else {
				n *= 10;
				n += (b - '0');
			}
		}
		if (minus)
			return n * -1;
		else
			return n;
	}
}

Measured memory_bytes: 26832
#include <bits/stdc++.h>
#define INF 100000000
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)


int main(){
    ll a, b, k;
    cin >> a >> b >> k;
    if (k < a) a -= k;
    else if(a + b > k){b -= (k - a), a = 0;
    }
    else {a = 0; b = 0;
    }
    cout << a << ' ' << b << '\n';
}

Measured memory_bytes: 256
//#define C
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

class Program
{
    static void Main()
    {
        var NW = ReadLine().Split().Select(int.Parse).ToArray();
        var A = NW[0];
        var B = NW[1];

        WriteLine(new[] {2*A-1,2*B-1,A+B }.Max());
    }
}


Measured memory_bytes: 13396
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define double long double
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
  string S;
  cin>>S;
  ll N=S.size();
  ll ans=N*(N-1)/2+1;
  ll A[26];
  for(int i=0; i<26; ++i) A[i]=0;
  for(int i=0; i<N; ++i) A[S.at(i)-'a']++;
  for(int i=0; i<26; ++i) ans-=A[i]*(A[i]-1)/2;
  
  co(ans);
  return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
  bool isprime(int i)
  {  int a,flag=1;
   a=sqrt(i);
   for(int j=2;j<=a;j++)
   {if(i%j==0)
   {  flag=0;
    break;
   }
   }
   return flag;
    
  }
int main()
{  int a;
 cin>>a;
 int i=a;
 while(!isprime(i))
 { 
   i++;
 }
 cout<<i;
}

Measured memory_bytes: 256
h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
move = []
for i in range(h):
    for j in range(w):
        if a[i][j] % 2 == 1:
            if j+1 < w:
                a[i][j+1] += 1
                move.append((i,j,i,j+1))
            elif i+1 < h:
                a[i+1][j] += 1
                move.append((i,j,i+1,j))
print(len(move))
for x, y, xp, yp in move:
    print(x+1,y+1,xp+1,yp+1)


Measured memory_bytes: 24244
#include <iostream>
using namespace std;
int main(){
  long long n;cin>>n;
  long long ret = n/11;
  ret = (long long)ret*2;
  if(n%11 > 6)cout << (long long)ret+2 << endl;
  else if(n % 11 != 0) cout << (long long)ret+1 << endl;
  else cout << (long long)ret << endl;
}

Measured memory_bytes: 256
import math
num = 100000
s = set([i for i in range(2,num+1)])
p = set()
mi = 0
while mi<=math.sqrt(num):
    mi = min(s)
    t = set([i*mi for i in range(1,num//mi+1)])
    p.add(mi)
    s-=t
p = p|s
p2 = set([i for i in p if (i+1)//2 in p])
c = 0
x = [0]*100001
for i in range(1,num+1):
    if i in p2:
        c+=1
    x[i]=c

q = int(input())
for i in range(q):
    l , r = map(int, input().split())
    ans = x[r]-x[l-1]
    print(ans)

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

using namespace std;
#define ll long long
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
const ll mod = 1e9 + 7;
typedef pair<int,int>           PI;
typedef pair<ll,ll>             PL;
typedef vector<PI>               vip; 
typedef vector<int>             vi;
typedef vector<vi>              vvi;
typedef vector<ll>              vl;
typedef vector<vl>              vvl;








int main (){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll s,c;
    cin >> s >> c;
    ll ans;
    if(c <= s * 2){
        ans = c / 2;
    }else{
        ans = s + (c - s * 2) / 4;
    }
    cout << ans << endl;
    








    return 0;
}

Measured memory_bytes: 768
#include <cstdio>

int a[200005];

int main()
{
    int n; scanf("%d", &n);
    int s = 0;
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]), s ^= a[i];
    for(int i = 0; i < n; i++)
        printf("%d ", s^a[i]);
    return 0;
}

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

int main(){
    int N,M,Cost = 0;
    cin >> N >> M;
    vector<string> Flag(N);
    vector<int> Answer;
    
    for(int i = 0;i < N;i++){
        cin >> Flag.at(i);
    }
    for(int w = 1;w < N;w++){
        for(int b = w+1;b < N;b++){
            Cost = 0;
            for(int n = 0;n < N;n++){
                for(int m = 0;m < M;m++){
                    if(n < w && Flag.at(n)[m] != 'W'){
                    Cost++;
                    }else if(n >= w && n < b && Flag.at(n)[m] != 'B'){
                        Cost++;
                    }else if(n >= b && Flag.at(n)[m] != 'R'){
                        Cost++;
                    }
                }
            }
            Answer.push_back(Cost);
        }
    }
    
    cout << *min_element(Answer.begin(),Answer.end()) << endl;
    
    return 0;
}

Measured memory_bytes: 3236
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int H, W;
        public long MOD = 1000000007;
        public long[,] DP;
        public void Run()
        {
            {
                var line = Input.ReadIntArray();
                H = line[0];
                W = line[1];
            }

            DP = new long[10, 10];
            for (int i = 0; i < 10; i++)
            {
                var line = Input.ReadIntArray();
                for (int j = 0; j < 10; j++)
                {
                    DP[i, j] = line[j];
                }
            }
            
            for (int k = 0; k < 10; k++)
            {
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        if (DP[i, j] > DP[i, k] + DP[k, j])
                            DP[i, j] = DP[i, k] + DP[k, j];
                    }
                }
            }

            long ret = 0;
            for (int i = 0; i < H; i++)
            {
                var line = Input.ReadIntArray();
                for (int j = 0; j < W; j++)
                {
                    if (line[j] != -1)
                    {
                        ret += DP[line[j], 1];
                    }
                }
            }

            Console.WriteLine(ret);
        }
    }

    // libs ----------
    

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}

Measured memory_bytes: 15444
#!python3

import numpy as np


# input
H, W = list(map(int, input().split()))
S = [input() for _ in range(H)]


def main():
    dp = np.zeros((H, W), dtype=int)
    if S[0][0] == "#":
        dp[0][0] = 1
    
    INF = 10 ** 5
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                continue
            if i == 0:
                a = INF
            else:
                a = dp[i-1][j]
                if S[i-1][j] == "." and S[i][j] == "#":
                    a += 1
            if j == 0:
                b = INF
            else:
                b = dp[i][j-1]
                if S[i][j-1] == "." and S[i][j] == "#":
                    b += 1
            dp[i][j] = min(a, b)
        
    print(dp[-1][-1])


if __name__ == "__main__":
    main()


Measured memory_bytes: 19456
let to_array s =
  Str.split (Str.regexp_string " ") s
  |> List.map int_of_string
  |> Array.of_list;;

let binary_search l value index =
  let rec loop left right =
    let mid = (left+right)/2 in
    if mid = left then
      if l.(left) = value then (true, left)
      else if l.(right) = value then (true, right)
      else (false, left)
    else
      let (left_new, right_new) =
        if l.(mid) > value then (left,mid-1)
        else if l.(mid) < value then (mid+1, right)
        else (-1,-1)
      in
      if left_new = -1 && right_new = -1 then (true, mid)
      else loop left_new right_new
  in loop index ((Array.length l)-1)
;;

let _ = read_int () and
    s = read_line () |> to_array and
    _ = read_int () and
    t = read_line () |> to_array  in
    Array.sort compare t;
    let f x y =
      let (count, index) = x in
      let (found, index_new) = binary_search s y index in
      if found then (count+1, index_new) else (count, index_new)
    in
    let (count, _) = Array.fold_left f (0,0) t in
    Printf.printf "%d\n" count
;;

Measured memory_bytes: 16196
#include<bits/stdc++.h>
using namespace std;
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout);
#define pb push_back
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define sf(x) scanf("%d",&x)
#define sf2(x,y) scanf("%d %d",&x,&y)
#define sf3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sl2(x,y) scanf("%lld %lld",&x,&y)
#define sl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sd(x) scanf("%lf",&x);
#define pii pair<int,int>
#define pLL pair<long long,long long>
#define pDB pair<double,double>
#define ff first
#define sn second
#define PRINT_CASE  printf("Case %d: ",tc++)
#define PRINT_CASENL  printf("Case %d:\n",tc++)
#define mx 100005
#define lnd tree[ind<<1]
#define rnd tree[(ind<<1)+1]
#define cnd tree[ind]
#define lndp b,(b+e)>>1,(ind<<1)
#define rndp ((b+e)>>1)+1,e,(ind<<1)+1
#define IN(a,x,y) (a>=x && a<=y)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
/// int other=mask^((1<<n)-1);
typedef long long ll;
typedef long long int lln;
ll INF=1<<28;
//typedef long long lld;
const double pi=acos(-1.0);
int fx[]={1,-1,0,0}; //direction array
int fy[]={0,0,1,-1};
int dir[4][2]={1,0,-1,0,0,-1,0,1};
const long double EPS=1e-7;
//#define INF 10000
ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
    return a/gcd(a,b)*b;
}
//struct compare
//{
//  bool operator()(const int& l, const int& r)
//  {
//      return l>r;
//  }
//};

bool cmp(int a, int b)
{
    return a<b;
}
int on(int mask,int st)
{
    return mask|(1<<st);
}
int off(int mask,int st)
{
    return mask&(~(1<<st));
}
bool check(int mask,int st)
{
    return mask&(1<<st);
}
ll inf=1e18;
ll mod=1e9+7;
//ll mod=993344777;
const int sz=300005;
vector<int>G[10];
int countt=0;
int main(void)
{
//    READ;
    ll n,a,b,k;
    cin>>n>>a>>b;
    k=n;
    if(b-a<=1)
    {
        cout<<n+1<<endl;
    }
    else
    {
        ll baki=k-(a-1);
        if(baki<=1)
        {
            cout<<n+1<<endl;
            return 0;
        }
        ll ans=0;
        if(baki%2)
            baki--,ans=1;
//        cout<<baki<<endl;
        ans+=((baki/2)*(b-a));
//        cout<<ans<<endl;
        cout<<max(ans+a,n+1)<<endl;
    }
    return 0;
}


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

using namespace std;

int main() {
    const int MaxL = 100000;
    int n;
    cin >> n;
    vector<int> v(n);
    vector<int> w(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i] >> w[i];
    }
    int q;
    cin >> q;
    vector<int> a(q);
    vector<int> m(q);
    for (int i = 0; i < q; ++i) {
        cin >> a[i] >> m[i];
        --a[i];
    }
    int top = min(n, 1024);
    vector<vector<pair<int, int>>> vals(top);
    vals[0].emplace_back(0, 0);
    vals[0].emplace_back(w[0], v[0]);
    for (int i = 1; i < top; ++i) {
        vector<pair<int, int>> cur = vals[(i - 1) / 2];
        cur.reserve(2 * cur.size());
        for (auto p: vals[(i - 1) / 2 ]) {
            if (p.first + w[i] <= MaxL)
                cur.emplace_back(p.first + w[i], p.second + v[i]);
        }
        sort(cur.rbegin(), cur.rend());
        for (auto p: cur) {
            while (!vals[i].empty() && vals[i].back().second <= p.second)
                vals[i].pop_back();
            vals[i].push_back(p);
        }
        reverse(vals[i].begin(), vals[i].end());
    }
    for (int k = 0; k < q; ++k) {
        vector<pair<int, int>> cur_vals;
        cur_vals.emplace_back(0, 0);
        int z = a[k];
        while (z >= top) {
            int s = cur_vals.size();
            for (int i = 0; i < s; ++i) {
                if (w[z] + cur_vals[i].first <= m[k])
                    cur_vals.emplace_back(w[z] + cur_vals[i].first, v[z] + cur_vals[i].second);
            }
            z = (z - 1) / 2;
        }
        int best = 0;
        for (int i = 0; i < (int) cur_vals.size(); ++i) {
            auto p = lower_bound(vals[z].begin(), vals[z].end(), make_pair(m[k] - cur_vals[i].first + 1, -1));
            if (p == vals[z].begin())
                continue;
            --p;
            best = max(best, cur_vals[i].second + (p)->second);
        }
        cout << best << "\n";
    }
    return 0;
}


Measured memory_bytes: 11712
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<int, i_i> i_i_i;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;

int main() {
    //cout.precision(10);
    int r, c, n;
    cin >> r >> c >> n;
    vector<i_i_i> v;
    for(int i = 1; i <= n; i++){
        int x[2], y[2];
        cin >> x[0] >> y[0] >> x[1] >> y[1];
        if((x[0] != 0) && (x[0] != r)){
            if((y[0] != 0) && (y[0] != c)){
                continue;
            }
        }
        if((x[1] != 0) && (x[1] != r)){
            if((y[1] != 0) && (y[1] != c)){
                continue;
            }
        }
        int n[2];
        for(int j = 0; j <= 1; j++){
            if(y[j] == 0){
                n[j] = x[j];
                continue;
            }
            if(x[j] == r){
                n[j] = 2e8 + y[j];
                continue;
            }
            if(y[j] == c){
                n[j] = 4e8 - x[j];
                continue;
            }
            n[j] = 6e8 - y[j];
        }
        if(n[1] < n[0]){
            swap(n[1], n[0]);
        }
        for(int j = 0; j <= 1; j++){
            i_i in = {i, j};
            i_i_i in2 = {n[j], in};
            v.push_back(in2);
        }
    }
    sort(v.begin(), v.end());
    /*for(int i = 0; i < v.size(); i++){
        cout << v[i].first << endl;
    }*/
    stack<int> sta;
    for(int i = 0; i < v.size(); i++){
        if(v[i].second.second == 0){
            sta.push(v[i].second.first);
            continue;
        }
        int out = sta.top();
        sta.pop();
        if(out != v[i].second.first){
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    return 0;
}

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

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

  if (S.size() > K)
  {
    cout << S.substr(0, K) << "..." << endl;
  }
  else
  {
    cout << S << endl;
  }
}


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


void solve(long long N, std::vector<std::string> S){
    std::sort(S.begin(), S.end());
    S.erase(std::unique(S.begin(), S.end()), S.end());
    cout << S.size() << endl;
}
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<std::string> S(N);
    for(int i = 0 ; i < N ; i++){
        std::cin >> S[i];
    }
    solve(N, S);
    return 0;
}


Measured memory_bytes: 15744
#include<bits/stdc++.h>
using namespace std;
//define
typedef pair<int,int>pi;
#define elif else if
#define int long long
#define rep(x,y) for(int i=x;i<y;i++)
#define REP(x,y) for(int j=x;j<y;j++)
#define Rep(x,y) for(int k=x;k<y;k++)
#define all(x) x.begin(),x.end()
#define pb push_back
#define vint vector<int>
#define vvint vector<vector<int>>
#define vtr vector<string>
#define vvtr vector<vector<string>>
#define vhar vector<char>
#define vvhar vector<vector<char>>
#define mod 100000007
#define elif else if
#define str string

signed main(){
  int N,M,O,P=0,c=0,c2=0,crash=0,nowtab=1;
  bool B=false;
  double D,D2;
  char C;
  str S,T,U;
  vint v(0);
  cin>>N;
  vint Button(N);rep(0,N)cin>>Button[i];
  int nowButton=Button[0];
  while(true){
    c++;
    if(nowButton==2){
      B=true;
      break;
    }
    else if(c==N)break;
    nowButton=Button[nowButton-1];
  }
  B?cout<<c<<endl:cout<<-1<<endl;
}


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

int main() {
    ll n, p, a, ans;
    cin >> n >> p;
    bool odd = false;
    for (int i = 0; i < n; i++) {
        cin >> a;
        if (a % 2 == 1) odd = true;
    }
    if (!odd) {
        if (p == 0) ans = pow(2, n);
        else ans = 0;
    } else ans = pow(2, n-1);
    cout << ans << endl;
}

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


int comp( const void *c1, const void *c2 );

int main(){

    int i = 0;
    int height[10] = {};
    FILE *fp;

    while ( (fscanf(stdin, "%d", &height[i])) != EOF){
        ++i;
    }

    qsort(height, 10, sizeof(int), comp);

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

    return 0;
}

int comp( const void *c1, const void *c2 ){
  int tmp1 = *(int *)c1;
  int tmp2 = *(int *)c2;

  if( tmp1 < tmp2 ){
    return 1;
  }else if( tmp1 == tmp2 ) {
    return  0;
  }else{
    return -1;
  }
}

Measured memory_bytes: 608
#include <bits/stdc++.h>
    using namespace std;
     
    int main() {
      int N, K, Kabs, L;
      cin >> N >> K;
      L = 0;
      for(int i = 0; i < N; i++){
        int x;
        cin >> x;
        if(K - x < 0){
          Kabs = x - K;
        }else{
          Kabs = K - x;
        }
        if(Kabs < x){
          L += 2 * Kabs;
        }else{
          L += 2 * x;
        }
      }
      cout << L << endl;
    }

Measured memory_bytes: 256