akhauriyash/Code-Regression huggingface dataset:
// ABC163B.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>

using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int a;
    int sum = 0;
    for (int i = 0; i < m; i++) {
        cin >> a;
        sum = sum + a;
    }
    if (sum > n) {
        cout << "-1" << endl;
    }
    else {
        cout << n - sum << endl;
    }
}


Measured memory_bytes: 3148
#include<bits/stdc++.h>
#define Abra_Ka_Dabra ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
 
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
 
    Abra_Ka_Dabra
 
    int x;
    cin>>x;
    if(x>=30) cout<<"Yes\n";
    else cout<<"No\n";
 
    return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

vector<string> res;
map<string , int> mp;
int64 N;

void dfs(int64 d, int64 now, string s="") {
  if (d == N) {
    if (mp.count(s)) return;
    res.push_back(s);
    mp[s] = 1;
    return;
  }
  REP(i, now+2) {
    dfs(d+1, max(now, i), s+string(1, (char)('a'+i)));
  }
}

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

  dfs(0, -1, "");
  sort(all(res));
  REP(i, res.size()) {
    cout << res[i] << endl;
  }
}


Measured memory_bytes: 15092
N = STDIN.gets
inputs = STDIN.gets.split.map(&:to_i)

_2_i = 2
i = 0

is_even = true
while is_even
  inputs.each_with_index do |num, i|
  
    if num % 2 == 1
      is_even = false
      break
    end
    inputs[i] = num / 2
  
  end
  if is_even
    i += 1
    _2_i *= 2
  end
end

STDOUT.puts i


Measured memory_bytes: 1788
import copy

class UnionFind:
	def __init__(self, N):
		self._parent = [i for i in range(N)]

	def find(self, x):
		if self._parent[x] == x:
			return x
		else:
			return self.find(self._parent[x])

	def unite(self, a, b):
		ta = self.find(a)
		tb = self.find(b)
		if(ta != tb):
			self._parent[ta] = min(ta,tb)
			self._parent[tb] = self._parent[ta]
		
			
	def check(self, a, b):
		return self.find(a) == self.find(b)
		
if __name__ == '__main__':
	N,K,L = map(int, input().split())
	uf1 = UnionFind(N)
	for _ in range(K):
		p,q = map(int, input().split())
		uf1.unite(p-1,q-1)
	uf2 = UnionFind(N)
	for _ in range(L):
		s,r = map(int, input().split())
		uf2.unite(s-1,r-1)
	from collections import defaultdict
	d = defaultdict(int)
	for i in range(N):
		d[(uf1.find(i), uf2.find(i))] += 1
	ans = []
	for i in range(N):
		ans.append( str(d[(uf1.find(i), uf2.find(i))]) )
	print(' '.join(ans))

Measured memory_bytes: 61552
#![allow(non_snake_case)]

use std::io::{stdin, Read};
use std::str::FromStr;
fn read_option<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok()
}
fn read<T: FromStr>() -> T {
    let opt = read_option();
    opt.expect("failed to parse token")
}

fn main() {
    let N: usize = read();
    let M: usize = read();
    let mut vertexes: Vec<(usize, usize)> = vec![];
    for _ in 0..M {
        vertexes.push((read(), read()));
    }
    let mut ans = 0;
    'outer: for i in 0..M {
        use std::collections::{HashMap, HashSet, VecDeque};
        let mut graph: HashMap<usize, HashSet<usize>> = HashMap::new();
        for (j, &(a, b)) in vertexes.iter().enumerate() {
            if j == i {
                continue;
            }
            let entry = graph.entry(a).or_insert_with(HashSet::new);
            entry.insert(b);
            let entry = graph.entry(b).or_insert_with(HashSet::new);
            entry.insert(a);
        }
        for start in 1..=N {
            for goal in 1..=N {
                if start == goal {
                    continue;
                }
                let mut visited = HashSet::new();
                let mut queue = VecDeque::new();
                visited.insert(start);
                queue.push_front(start);
                let mut goaled = false;
                while let Some(now) = queue.pop_back() {
                    if now == goal {
                        goaled = true;
                        break;
                    }
                    let nexts = graph.get(&now);
                    match nexts {
                        None => break,
                        Some(nexts) => {
                            for &next in nexts {
                                if visited.contains(&next) {
                                    continue;
                                }
                                queue.push_front(next);
                                visited.insert(next);
                            }
                        }
                    }
                }
                if !goaled {
                    ans += 1;
                    continue 'outer;
                }
            }
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 2188
#include <bits/stdc++.h>
#define bug1(x) cout<<#x<<" = "<<x<<endl
#define bug2(x,y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define bug4(x,y,z,m) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<" "<<#m<<" = "<<m<<endl

using namespace std;

class Debugger {
public:
	template<typename T>
	void printVector(vector<T> const &vec, bool printSize=true){
		if(printSize){
			cout << vec.size() << endl;
		}
		for(auto &elem : vec){
			cout << elem << " ";
		}
		cout << endl;
	}

	template<typename T>
	void printMatrix(vector<vector<T>> const &matrix){
		cout << matrix.size()<< " ";
		if(!matrix.empty()){
			cout << matrix[0].size();
		}
		cout << endl; 
		for(auto &vec : matrix){
			printVector(vec, false);
		}
		cout << endl;
	}
};

const int MAXN = 10000001;
const int INF = 1e9;
#define ll long long 

class TaskD {
private:
	Debugger debugger;

public:
	void solveOne(int it){
		int rows, cols;
		cin >> rows >> cols;
		int startX, startY;
		cin >> startX >> startY;
		startX--; startY--;

		int destX, destY;
		cin >> destX >> destY;
		destX--; destY--;

		vector<string> grid(rows);
		for(int i = 0; i < rows; i++) {
			cin >> grid[i];
		}
		
		deque<pair<int,int>> dq;
		dq.push_front({startX, startY});
		vector<vector<int>> dist(rows, vector<int>(cols, INF));
		dist[startX][startY] = 0;

		while(!dq.empty()) {
			auto curr = dq.front(); dq.pop_front();
			if(curr.first == destX && curr.second == destY) {
				cout << dist[destX][destY] << endl;
				return;
			}
			for(int dx = -2; dx <= 2; dx++) {
				for(int dy = -2; dy <= 2; dy++) {
					int xnew = curr.first + dx;
					int ynew = curr.second + dy;
					if(xnew >= 0 && xnew < rows && ynew >= 0 && ynew < cols 
							&& grid[xnew][ynew] == '.') {
						if(abs(dx) + abs(dy) <= 1) {
							if(dist[curr.first][curr.second] < dist[xnew][ynew]) {
								dist[xnew][ynew] = dist[curr.first][curr.second];
								dq.push_front({xnew, ynew});
							}
						} else {
							if(1 + dist[curr.first][curr.second] < dist[xnew][ynew]) {
								dist[xnew][ynew] = 1 + dist[curr.first][curr.second];
								dq.push_back({xnew, ynew});
							}
						}
					}
				}
			}
		}
		cout << -1 << endl;
	}

	void solve(){
		int tc = 1;
		for(int it = 1; it <= tc; it++){
			solveOne(it);
		}
	}
};


int main(){
	ios_base::sync_with_stdio(false);
	TaskD solver;
	solver.solve();
	return 0;
}


Measured memory_bytes: 8716
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

#define A_MAX 500000

typedef struct _tree {
    int parent;
    int left;
    int right;
    int key;
} Tree;

void preorder(Tree *nodes, int n, int i) 
{
    printf(" %d", nodes[i].key);
    if (nodes[i].left != -1) {
        preorder(nodes, n, nodes[i].left);
    }
    if (nodes[i].right != -1) {
        preorder(nodes, n, nodes[i].right);
    }
    
    return;
}

void inorder(Tree *nodes, int n, int i) 
{
    if (nodes[i].left == -1) {
        printf(" %d", nodes[i].key);
    }
    else {
        inorder(nodes, n, nodes[i].left);
        printf(" %d", nodes[i].key);
    }
    if (nodes[i].right != -1) {
        inorder(nodes, n, nodes[i].right);
    }
    
    return;
}

void postorder(Tree *nodes, int n, int i) 
{
    if (nodes[i].left != -1) {
        postorder(nodes, n, nodes[i].left);
    }

    if (nodes[i].right != -1) {
        postorder(nodes, n, nodes[i].right);
    }
    
    printf(" %d", nodes[i].key);

    return;
}


int insert(Tree *nodes, int n, int root) {
    int y = -1;
    int x = root;
    while (x != -1) {
        y = x;
        if (nodes[n].key < nodes[x].key) {
            x = nodes[x].left;
        }
        else {
            x = nodes[x].right;
        }
    }
    nodes[n].parent = y;
    if (y == -1) {
        root = n;
    }
    else if (nodes[n].key < nodes[y].key) {
        nodes[y].left = n;
    }
    else {
        nodes[y].right = n;
    }
    return root;
}

bool find(Tree *nodes, int key, int root) {
    bool found = false;
    int i = root;


    while (!found) {
        if (nodes[i].key == key) found = true;
        else if (nodes[i].key > key) {
            i = nodes[i].left;
        } else {
            i = nodes[i].right;
        }
        if ( i == -1) break;
    }
    return found;
}


int main() {
    int m;
    int n = 0;
    int key;
    char order[10];
    Tree nodes[A_MAX];
    int root = -1;

    cin >> m;

    for (int i = 0; i < m; i++) {
        scanf("%s", order);
        if (strcmp(order, "insert") == 0) {
            cin >> key;
            nodes[n].parent = -1;
            nodes[n].left = -1;
            nodes[n].right = -1;
            nodes[n].key = key;
            root = insert(nodes, n, root);
//            printf("root = %d\n", root);
            n++;
        }
        else if (strcmp(order, "find") == 0) {
            cin >> key;
            if (find(nodes, key, root)) {
                cout << "yes" << endl;
            }
            else {
                cout << "no" << endl;
            }
        }
        else if (strcmp(order, "print") == 0) {
            inorder(nodes, n, root);
            cout << endl;
            preorder(nodes, n, root);
            cout << endl;
        }
    }

    return 0;
}



Measured memory_bytes: 9400
#include<stdio.h>
int search(int a[], int n, int key){
    int i = 0;
    a[n] = key;
    while(a[i] != key) 
        i++;
    return i != n;   
}
int main() {
    int i, n, a[10001], q, key, sum = 0;
    scanf("%d\n", &n);
    for(i = 0; i < n; i++) {
        scanf("%d", a+i);
    }
    scanf("%d\n", &q);
    for(i = 0; i < q; i++) {
        scanf("%d", &key);
        if(search(a, n, key)) {
            sum++;
        }
    }
    printf("%d\n", sum);

}


Measured memory_bytes: 2096
#include<bits/stdc++.h>
using namespace std;
char a[1000001];
int main()
{
    gets(a);
    int l = strlen(a);
    int x, y;
    for(int i = 0; i < l; i++)
    {
    	if (a[i] == 'A')
    	{
    		x = i;
    		break;
		}
	}
	for(int i = l - 1; i >= 0; i--)
	{
		if (a[i] == 'Z')
		{
			y = i;
			break;
		}
	}
	cout << y - x + 1;
    return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int K;
ll dp[2][501][2][2];
ll sum[2][501][2][2];
vector<vector<int> > nxtst;

ll solve(string S){
  int N=S.size();
  memset(dp,0,sizeof(dp));
  memset(sum,0,sizeof(sum));

  int crtd=0,nxtd=1;
  rep(i,N){
    memset(dp[nxtd],0,sizeof(dp[nxtd]));
    memset(sum[nxtd],0,sizeof(sum[nxtd]));
    dp[crtd][0][!(i==0)][(i==N-1)]+=1;
    rep(j,K){
      rep(f1,2){
        rep(f2,2){
          int mn=(f2==0?1:0);
          int mx=(f1==0?S[i]-'0':9);
          repl(nxt,mn,mx+1){
            int nxtj=nxtst[j][nxt];
            (dp[nxtd][nxtj][(f1==0&&nxt==mx?0:1)][1]+=dp[crtd][j][f1][f2])%=mod;
            (sum[nxtd][nxtj][(f1==0&&nxt==mx?0:1)][1]+=sum[crtd][j][f1][f2])%=mod;
            if(nxtj==K-1){
              (sum[nxtd][nxtj][(f1==0&&nxt==mx?0:1)][1]+=dp[crtd][j][f1][f2])%=mod;
            }
          }
        }
      }
    }
    swap(crtd,nxtd);
  }

  ll res=0;
  rep(j,K){
    rep(f1,2){
      (res+=sum[crtd][j][f1][1])%=mod;
    }
  }

  return res;
}

string decrement(string A) {
  for(int i = A.size() - 1; i >= 0; --i) {
    if(A[i] > '0') {
      A[i]--;
      break;
    } else {
      A[i] = '9';
    }
  }
  if(A[0] == '0' && A.size() > 1) {
    return A.substr(1);
  } else {
    return A;
  }
}

int main(){
  string A,B,s;
  cin>>A>>B>>s;

  int N=s.size();
  vector<string> pfx;
  rep(i,N+1){
    pfx.push_back(s.substr(0,i));
  }
  sort(all(pfx));
  uni(pfx);
  K=pfx.size();
  nxtst=vector<vector<int> >(K,vector<int>(10,-1));
  rep(i,K){
    rep(nxt,10){
      string t=pfx[i];
      t+='0'+nxt;
      int idx=0;
      while(1){
        idx=lower_bound(all(pfx),t)-pfx.begin();
        if(idx<K&&pfx[idx]==t)break;
        t=t.substr(1);
      }
      nxtst[i][nxt]=idx;
    }
  }

  if(A!="0")cout<<(solve(B)-solve(decrement(A))+mod)%mod<<endl;
  else cout<<solve(B)%mod<<endl;

  return 0;
}



Measured memory_bytes: 3388
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000000;
constexpr ll INF= 9 * 1e18;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;

template<typename T>
T Pow(T a,T b) {
    T ret = 1;
    for(int i = 0;i < b;i++) {
        ret *= a;
    }
    return ret;
}

ll mod(ll val) {
    ll res = val % MOD;
    if(res < 0) {
        res += MOD;
    }
    return res;
}

ll dp[2020][2020]; //dp[i][j] = Sのi番目、Tのj番目までの部分列の組であって、整数列として等しいような組の個数

int main() {
    int N,M;
    cin >> N >> M;
    vector<int> S(N);
    for(int i = 0;i < N;i++) {
        cin >> S.at(i);
    }
    vector<int> T(M);
    for(int i = 0;i < M;i++) {
        cin >> T.at(i);
    }
    for(int i = 0;i < 2020;i++) {
        for(int j = 0;j < 2020;j++) {
            if(i == 0 || j == 0) {
                dp[i][j] = 1;
            }
            else {
                dp[i][j] = 0;
            }
        }
    }
    for(int i = 0;i < N;i++) {
        for(int j = 0;j < M;j++) {
            if(S.at(i) == T.at(j)) {
                dp[i + 1][j + 1] = mod(dp[i][j + 1] + dp[i + 1][j]);
            }
            else {
                dp[i + 1][j + 1] = mod(dp[i][j + 1] + dp[i + 1][j] - dp[i][j]);
            }
        }
    }
    cout << dp[N][M] % MOD << endl;
}

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

int n,w[105],v[105],W;

long long dp[100500],ans,INF = (long long)2e18;

int main(){
    cin >> n >> W;
    for(int i = 1; i <= n; i++){
        cin >> w[i] >> v[i];
    }
    /// dp[i] = maximum value with weight i
    for(int i = 0; i <= W; i++) dp[i] = -INF;
    dp[0] = 0;
    for(int i = 1; i <= n; i++){
        for(int j = W - w[i]; j >= 0; j--){
            dp[j + w[i]] = max(dp[j + w[i]],dp[j] + v[i]);
        }
    }
    for(int i = 0; i <= W; i++){
        ans = max(ans,dp[i]);
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <iomanip>
#include <string>
#define REP(i,n) for(int i=0;i<(n);++i)
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
typedef long long int LL;
 
int main(){
  int N, c[100001] = {};
  cin >> N;
  int ret = 0;
  REP(i, N) {
    int a;
    cin >> a;
    if(a > N) {
      ret++;
    } else {
      c[a]++;
    }
  }
  REP(i, N+1) {;
    if(c[i] >= i) {
      ret += c[i] - i;
    } else {
      ret += c[i];
    }
  }
  cout << ret << endl;
}

Measured memory_bytes: 768
n, k = map(int, input().split())

a = -1
b = -1
c = -1

for x in range(n+1):
    for y in range(n+1-x):
        if k == x*10000  + y*5000 + (n-x-y)*1000:
            a = x
            b = y
            c = n-x-y
            break
    else:
        continue
    break

print("{0} {1} {2}".format(a,b,c))

Measured memory_bytes: 3064
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.Integer.parseInt;

/**
 * Grid Mori
 */
public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		String[] words;

		int n, a, b, c, d;

		n = parseInt(br.readLine());

		line = br.readLine();
		a = parseInt(line.substring(0, line.indexOf(' ')));
		b = parseInt(line.substring(line.indexOf(' ') + 1));

		line = br.readLine();
		c = parseInt(line.substring(0, line.indexOf(' ')));
		d = parseInt(line.substring(line.indexOf(' ') + 1));

		int min = Integer.MAX_VALUE;

		for (int w = 1; w <= n; w++) {
			int[] pa = new int[]{(a - 1) % w, (a - 1) / w};
			int[] pb = new int[]{(b - 1) % w, (b - 1) / w};
			int[] pc = new int[]{(c - 1) % w, (c - 1) / w};
			int[] pd = new int[]{(d - 1) % w, (d - 1) / w};
			int _min = 0;
			_min += Math.abs(pa[0] - pb[0]);
			_min += Math.abs(pa[1] - pb[1]);
			_min += Math.abs(pc[0] - pd[0]);
			_min += Math.abs(pc[1] - pd[1]);
			min = Math.min(min, _min);
		}

		System.out.println(min);
	}
}

Measured memory_bytes: 24336
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 4005
using namespace std;

int n, vw[N], vb[N], d[N], u[2005][2005];
char t1[5];
void h(int p, int q) {
	while (p <= n * 2) {
		d[p] += q;
		p += (p & -p);
	}
}

int g(int p) {
	int r = 0;
	while (p > 0) {
		r += d[p];
		p -= (p & -p);
	}
	return r;
}

int f(int p, int q) {
	int mn = 2e9, t;
	if (p == 0 && q == 0) return 0;
	if (u[p][q] != -1) return u[p][q];
	if (p > 0) {
		t = vw[p] - g(vw[p]);
		h(vw[p], 1);
		mn = min(mn, f(p - 1, q) + (p + q - t));
		h(vw[p], -1);
	}
	if (q > 0) {
		t = vb[q] - g(vb[q]);
		h(vb[q], 1);
		mn = min(mn, f(p, q - 1) + (p + q - t));
		h(vb[q], -1);
	}
	return u[p][q] = mn;
}
int main()
{
	int i, t2;
	cin >> n;
	for (i = 1; i <= n * 2; i++) {
		scanf ("%s %d", t1, &t2);
		if (t1[0] == 'W') vw[t2] = i;
		else vb[t2] = i;
	}
	memset(u, -1, sizeof(u));
	cout << f(n, n);
    return 0;
}


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

int main(){
  string S;
  cin >> S;
  int ans = 0;

  if(S[0]==S[1] && S[0]=='0'){
    S[1] = '1';
    ans += 1;
  } else if(S[0]==S[1] && S[0]=='1'){
    S[1] = '0';
    ans += 1;
  }

  for(long long i=1; i<S.size(); ++i){
    if(S[i]==S[i+1]){
      S[i+1] = S[i-1];
      ans += 1;
    }
  }

  cout << ans << endl;
}


Measured memory_bytes: 512
#解説参照
n=int(input())
*a,=map(int, input().split())
*b,=map(int, input().split())
shift=0
from bisect import bisect_right,bisect_left
from collections import Counter
sb=Counter(b)
sa=Counter(a)
for bi in sb:
    if bi in sa:
        if sa[bi]+sb[bi]>n:
            print("No")
            exit()
for bi in sb:
    lb=bisect_left(b,bi)
    ra=bisect_right(a,bi)
    if a[ra-1]==b[lb]:
        shift=max(shift,ra-lb)
ans=[b[(i-shift)%n] for i in range(n)]##
print("Yes")
print(*ans)

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;

#define rep(i,N) for(int i=0; i < (int)N; i++)
#define all(v) (v).begin(),(v).end()

const int INF = 100000002;

int main(){
	int k; cin >> k;
	queue<ll> q;
	rep(i,9) q.push(i+1);
	ll a;
	rep(i,k){
		a = q.front(); q.pop();
		//cout << a << endl;
		
	    if(a%10!=0)q.push(a*10+a%10-1);
	    q.push(a*10+a%10);
	    if(a%10!=9)q.push(a*10+a%10+1);
	    
	}
	cout << a << endl;
}

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

#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP0(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP0(i, n) for (int i = (n)-1; i >= 0; --i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define RREP1(i, n) for (int i = (n); i >= 1; --i)
#define pow10(n) int(1e##n + n)

#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
typedef mp::cpp_int LL;
typedef pair<int, int> P;

const int INTINF = int(1e9) + 1;
const LL LLINF = LL(1e18) + 1;
long double eps = 1.0E-14;

void solve()
{
    LL X;
    cin >> X;
    FOR (A, -1000, 1000)
    {
        FOR (B, -1000, 1000)
        {
            LL A2 = A * A;
            LL B2 = B * B;
            if (X == A2 * A2 * A - B2 * B2 * B)
            {
                std::cout << A << " " << B << endl;
                return;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(20);
    solve();
    return 0;
}


Measured memory_bytes: 3760
card = []
for _ in range(3):
    line = list(map(int, input().split()))
    card.append(line)

check_card = [[False, False, False] for i in range(3)]


def check(num):
    for y in range(3):
        for x in range(3):
            if card[y][x] == num:
                check_card[y][x] = True


n = int(input())
for _ in range(n):
    num = int(input())
    check(num)


def ver(check_card):
    for x in range(3):
        res = [check_card[0][x], check_card[1][x], check_card[2][x]]
        if sum(res) == 3:
            return True
    return False


def hor(check_card):
    for y in range(3):
        res = check_card[y]
        if sum(res) == 3:
            return True
    return False


def naname(check_card):
    pat1 = check_card[0][0] + check_card[1][1] + check_card[2][2]
    pat2 = check_card[0][2] + check_card[1][1] + check_card[2][0]
    if pat1 == 3 or pat2 == 3:
        return True
    else:
        return False


if ver(check_card) or hor(check_card) or naname(check_card):
    print('Yes')
else:
    print('No')

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


int main(){
    int n;
    int* a;

    cin >> n;
    a = new int[n];

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

    sort(a, a+n);
    string out;
    out = "YES";
    for(i=0;i<n-1;i++){
        if(a[i] == a[i+1]){
            out = "NO";
            break;
        }
    }

    cout << out << endl;
    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
int main(){
  int h,w;
  cin >> h >> w;
  vector<string> a(h);
  for(int i=0;i<h;i++){
    cin >> a[i];
  }
  vector<bool> row(h,false);
  vector<bool> col(w,false);
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(a[i][j] == '#'){
        row[i] = true;
        col[j] = true;
      }
    }
  }
  for(int i=0;i<h;i++){
    if(row[i]){
      for(int j=0;j<w;j++){
        if(col[j]){
          cout << a[i][j];
        }
      }
      cout << endl;
    }
  }   
}

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

int main() {
    string s;
    cin >> s;
    if (s == "Sunny") {
        cout << "Cloudy" << endl;
    } else if (s == "Cloudy") {
        cout << "Rainy" << endl;
    } else {
        cout << "Sunny" << endl;
    }
}


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

int main()
{
    int ans = 700;
    string S;
    cin >> S;
    
    for (int i = 0; i < S.size(); i++) {
        if (S[i] == 'o') {
            ans += 100;
        }
    }
    cout << ans << endl;
    
}


Measured memory_bytes: 3592
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,c=0;
    string s;
   cin>>a>>b>>s;
  if(s.size()==a+b+1)c++;
  else{ cout<<"No"<<endl;
       exit(0);
      }
  if(s[a]=='-')c++;
  else {cout<<"No"<<endl;
        exit(0);
       }
  for(int i=0;i<s.size();i++){
     if(s[i]=='0'||s[i]=='1'||s[i]=='2'||s[i]=='3'||s[i]=='4'||s[i]=='5'||s[i]=='6'||s[i]=='7'||s[i]=='8'||s[i]=='9')
       c++;
  }
  if(c==a+b+2)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}

Measured memory_bytes: 256
from itertools import accumulate

MAX_T = 10 ** 5 + 100
MAX_C = 30

n, c = map(int, input().split())
time = {c: [] for c in range(1, MAX_C + 1)}

for _ in range(n):
    s, t, c = map(int, input().split())
    time[c].append((s, t))

n_program = [0 for _ in range(MAX_T + 1)]

for c in range(1, MAX_C + 1):

    recording = [0 for _ in range(MAX_T + 1)]

    for s, t in time[c]:
        recording[s] += 1
        recording[t + 1] -= 1

    recording = accumulate(recording)

    for i, r in enumerate(recording):
        if r:
            n_program[i] += 1

answer = max(n_program)
print(answer)


Measured memory_bytes: 18632
#include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }

void move(int& sr, int& sc, char m) {
    if (m == 'U' )
        sr -= 1;
    if (m == 'D')
        sr += 1;
    if (m == 'L')
        sc -= 1;
    if (m == 'R')
        sc += 1;
}

bool simlation(vector<char>& S, vector<char>& T, int N, int H, int W, int sr, int sc) {
    rep(i, N) {
        move(sr, sc, S[i]);
        if (sr > H || sr <= 0 || sc > W || sc <= 0)
            return false;
        int new_sr, new_sc;
        new_sr = sr;
        new_sc = sc;
        move(new_sr, new_sc, T[i]);
        if (T[i] == 'U' || T[i] == 'D') {
            if (min(abs(H + 1 - new_sr), abs(0 - new_sr)) > min(abs(H + 1 - sr), abs(0 - sr))) {
                sr = new_sr;
                sc = new_sc;
            }
        }
        else {
            if (min(abs(W + 1 - new_sc), abs(0 - new_sc)) > min(abs(W + 1 - sc), abs(0 - sc))) {
                sr = new_sr;
                sc = new_sc;
            }
        }
    }
    return true;
}

int main() {
    int H, W, N, sr, sc;
    cin >> H >> W >> N >> sr >> sc;
    vector<char> S(N), T(N);

    rep(i, N) cin >> S[i];
    rep(i, N) cin >> T[i];
    vector<char> S1(N, '*'), T1(N, '*'), S2(N, '*'), T2(N, '*'), S3(N, '*'), T3(N, '*'), S4(N, '*'), T4(N, '*');
    rep(i, N) {
        if (S[i] == 'U')
            S1[i] = 'U';
        if (S[i] == 'D')
            S2[i] = 'D';
        if (S[i] == 'R')
            S3[i] = 'R';
        if (S[i] == 'L')
            S4[i] = 'L';

        if (T[i] == 'D')
            T1[i] = 'D';
        if (T[i] == 'U')
            T2[i] = 'U';
        if (T[i] == 'L')
            T3[i] = 'L';
        if (T[i] == 'R')
            T4[i] = 'R';
    }

    bool is_yes1 = simlation(S1, T1, N, H, W, sr, sc) && simlation(S2, T2, N, H, W, sr, sc);
    bool is_yes2 = simlation(S3, T3, N, H, W, sr, sc) && simlation(S4, T4, N, H, W, sr, sc);
    if (is_yes1 && is_yes2) cout << "YES" << endl;
    else cout << "NO" << endl;
}

Measured memory_bytes: 2176
#include<iostream>
using namespace std;
using ll=long long;
int main(){
  ll A,B,K;
  cin >> A >> B >> K;
  if(A>=K){
    A-=K;
  }
  else if(A+B>=K){
    K-=A;
    B-=K;
    A=0;
  } else{
    A=0;
    B=0;
  }
  cout << A<<" "<<B<<endl;
}

  


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

for sen in range(0,n+1):
    for go in range(0,n+1):
        if 1000*sen + 5000*go + 10000*(n-sen-go) == y and n-sen-go >= 0:
            print("{} {} {}".format(n-sen-go, go, sen))
            exit()

print("-1 -1 -1")


Measured memory_bytes: 65852
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <iomanip>
#include <math.h> 
#include <stack>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <tuple>
#include <cctype>
#include <ctype.h>
#include <set>
#include <sstream>
#include <time.h>
using namespace std;
//#define int long long
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))
#define l1 list[index]
#define l2 list[index - 1]
#define l3 list[index + 1]
#define iif(i,j) ((i<0 && j<0) || (i>0 && j>0)) ? true : false 
typedef long long ll;
typedef pair<int, int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
int ca[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

struct MyStruct
{
	int list[13];
};


signed main() {
	int i, j, k, n, m, cnt = 0;
	//multimap<string, int>mp;

	MyStruct st;
	memset(st.list, 0, sizeof(st.list));

	//map<char, MyStruct>mp;
	map<string, MyStruct>mp;


	mp.insert(make_pair("S", st));
	mp.insert(make_pair("H", st));
	mp.insert(make_pair("C", st));
	mp.insert(make_pair("D", st));


	cin >> n;

	for (i = 0; i < n; i++) {
		//char s;
		string s;
		int num;
		cin >> s >> num;

		num--;

		mp[s].list[num] = 1;
	}

	//vector<char>clist{ "S", "H","C","D" };
	vector<char>slist{ 'S', 'H','C','D' };



	for (i = 0; i < 4; i++) {
		for (j = 0; j < 13; j++) {
			if (mp[ {slist[i] }].list[j] == 0) {
				cout << slist[i] << " " << j + 1 << endl;
			}
		}
	}







	getchar();
	getchar();
	return 0;
}


Measured memory_bytes: 3128
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <set>
using namespace std;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl

const int INF = 1001001001;
const int mod = 1000000007;
#define PI 3.14159265359;

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

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() {
  int a[5], k;
  rep (i, 5) {
    cin >> a[i];
  }
  cin >> k;
  int ans = 0;
  rep (i, 4) {
    for (int j = i; j < 5; j++) {
      if (a[j] - a[i] > k) ans++;
    }

  }
  if (ans > 0) P(":(");
  else P("Yay!");

  return 0;

}

Measured memory_bytes: 256
# coding: utf-8
# Your code here!

class Node:
    def __init__(self, id):
        self.id = id
        self.parent = -1
        self.left = -1
        self.right = -1
        
N = int(input())
nodes = [Node(i+1) for i in range(N)]

preorders = [int(i)-1 for i in input().split()]
inorders = [int(i)-1 for i in input().split()]


def dfs(nodes, preo, ino):
    if not preo:
        return

    root = preo[0]
    i = ino.index(root)
    
    if i != 0:
        nodes[root].left = preo[1]
        nodes[preo[1]].parent = root
        dfs(nodes, preo[1:i+1], ino[:i])
    
    if i != len(ino)-1:
        nodes[root].right = preo[i+1]
        nodes[preo[i+1]].parent = root
        dfs(nodes, preo[i+1:], ino[i+1:])
    

def postorder(nodes, root, result):
    node = nodes[root]
    if node.left != -1:
        postorder(nodes, node.left, result)
    if node.right != -1:
        postorder(nodes, node.right, result)
    result.append(root+1)


dfs(nodes, preorders, inorders)

result = []
postorder(nodes, preorders[0], result)

print(*result)


Measured memory_bytes: 5616
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <functional>
#include <complex>

using namespace std;

const int INF = (1<<30) - 1;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N, M, C, S, G;
    while(cin>>N>>M>>C>>S>>G, N|M|C|S|G){
        S--; G--;
        vector<vector<vector<int>>> tables(C, vector<vector<int>>(N, vector<int>(N, INF)));
        for(int i=0; i<C; i++)
            for(int j=0; j<N; j++)
                tables[i][j][j] = 0;
        
        for(int i=0; i<M; i++){
            int x, y, d, c; cin >> x >> y >> d >> c; x--; y--; c--;
            tables[c][x][y] = min(tables[c][x][y], d);
            tables[c][y][x] = min(tables[c][y][x], d);
        }
        
        for(int c=0; c<C; c++)
            for(int k=0; k<N; k++)
                for(int i=0; i<N; i++)
                    for(int j=0; j<N; j++)
                        tables[c][i][j] = min(tables[c][i][j], tables[c][i][k] + tables[c][k][j]);
        
        vector<int> ps(C);
        for(auto &p: ps)
            cin >> p;
        
        vector<vector<int>> costs(C, vector<int>(20000));
        for(int i=0; i<C; i++){
            vector<int> qs(ps[i]-1);
            vector<int> rs(ps[i]);
            
            for(auto &q: qs)
                cin >> q;
            for(auto &r: rs)
                cin >> r;
            
            int idx = 0;
            for(int j=1; j<20000; j++){
                if(idx < ps[i]-1 && qs[idx] < j){
                    idx++;
                }
                costs[i][j] = costs[i][j-1] + rs[idx];
            }
        }
        
        vector<vector<int>> table(N, vector<int>(N, INF));
        for(int i=0; i<C; i++)
            for(int j=0; j<N; j++)
                for(int k=0; k<N; k++){
                    int c;
                    if(tables[i][j][k] == INF){
                        c = INF;
                    }else{
                        c = costs[i][tables[i][j][k]];
                    }
                    table[j][k] = min(table[j][k], c);
                }
        
        for(int k=0; k<N; k++)
            for(int i=0; i<N; i++)
                for(int j=0; j<N; j++)
                    table[i][j] = min(table[i][j], table[i][k] + table[k][j]);
        
        cout << (table[S][G] == INF? -1: table[S][G]) << endl;
    }
    
    return 0;
}

Measured memory_bytes: 3764
#include <stdio.h>
#include <string.h>

#define MOD 998244353

struct mint {
    int n;
    mint(int n_ = 0) : n(n_) {}
};

mint operator+(mint a, mint b) { a.n += b.n; if (a.n >= MOD) a.n -= MOD; return a; }
mint operator-(mint a, mint b) { a.n -= b.n; if (a.n < 0) a.n += MOD; return a; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }

int min(int x, int y) {
    return x < y ? x : y;
}

int N;
char S[2001];

int A[2001];
int B[2001];

mint dp[4040][4040];

int main() {
    scanf("%s", S);
    N = strlen(S);
    for (int i = 0; i < N; i++) {
        int a = S[i] - '0';
        int b = 2 - a;
        A[i + 1] = A[i] + a;
        B[i + 1] = B[i] + b;
    }
    dp[0][0] = 1;
    for (int i = 0; i < N * 2; i++) {
        for (int j = 0; j <= N * 2; j++) {
            int k = i - j;
            if (A[min(N, i + 1)] >= j + 1) dp[i + 1][j + 1] += dp[i][j];
            if (B[min(N, i + 1)] >= k + 1) dp[i + 1][j] += dp[i][j];
        }
    }
    printf("%d\n", dp[N * 2][A[N]].n);
    return 0;
}


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

using namespace std;

#define int long long

template <typename T> T gcd(T m, T n) { return n == 0 ? m : gcd(n, m % n); }
template <typename T> T lcm(T m, T n) { return n * m / gcd(n, m); }
template <typename T> T fact(T n) { return (n == 0 ? 1 : n * fact(n - 1)); }
template <typename T> T modpow(T a, T e, T m) { return (e == (T)0 ? 1 : ((a % m) * modpow((a % m), e - 1, m)) % m); }

constexpr int MOD = 1e9 + 7;

signed main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  bool flag = true;

  if (n % 2 != 0) flag = false;

  if (flag) {
    for (int i = 0; i < n / 2; ++i) {
      if (s[i] != s[n / 2 + i]) flag = false;
      if (!flag) break;
    }
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
n, c, k = map(int, input().split())
t = []
for _ in range(n):
    t.append(int(input()))
t.sort()
ans = 1
bus = 0
d_time = t[0]+k
for i in t:
    if i>d_time or bus==c:
        ans += 1
        bus = 1
        d_time = i+k
    else:
        bus += 1
print(ans)

Measured memory_bytes: 7508
#include <bits/stdc++.h>
using namespace std;
typedef  long long int ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define rep(i, n) FOR(i, 0, n)
int a[10000];
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, ans = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        while (a[i] % 2 == 0) {
            ans++;
            a[i] /=2;
        }
    }
    cout << ans << endl;


    return 0;
}



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

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPr(i, n) for(int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for(int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for(int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
    cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
    int x12345;                                                                \
    cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
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;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
int main() {
    ll N, K; cin >> N >> K;
    vector<ll> A(N+2);
    FORq(i,1,N){
        cin >> A[i];
    }
    
    REP(i,min(48LL,K)){
        vector<ll> S(N+2),T(N+2);
        FORq(k,1,N){
            ll a = A[k];
            ll al = max(1LL,k - a);
            ll ar = min(N, k + a);
            S[al]++;
            S[ar + 1]--;
            //DB2(al,ar);
        }

        FORq(k,1,N){
            T[k] = T[k-1] + S[k];
        }

        FORq(j,1,N){
            A[j] = T[j];
        }
    }

    FORq(i,1,N){
        cout << A[i] << " ";
    }
    cout << endl;
}

Measured memory_bytes: 8000
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge
{
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

void dfs(Graph &G, vector<ll> &d, int v, int p = -1)
{
  for (auto u : G[v])
  {
    if (u.to == p)
      continue;

    // cerr << u.to << " " << d[v] + u.weight << endl;
    d[u.to] = min(d[u.to], d[v] + u.weight);
    dfs(G, d, u.to, v);
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  Graph G(N);
  for (int i = 0; i < N - 1; i++)
  {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--, c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }

  int Q, K;
  cin >> Q >> K;
  K--;

  vector<ll> d(N, INF);

  d[K] = 0;

  dfs(G, d, K);

  for (int i = 0; i < Q; i++)
  {
    // cerr << "a" << endl;
    int x, y;
    cin >> x >> y;
    x--, y--;
    cout << d[x] + d[y] << endl;
  }
  return 0;
}


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

int main() {
  string w;
  cin >> w;
  int count[26];
  for (int i = 0; i < 26; i++) {
    count[i] = 0;
  }

  for (int i = 0; i < w.size(); i++) {
    count[w[i] - 'a']++;
  }

  for (int i = 0; i < 26; i++) {
    if (count[i] % 2) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}


Measured memory_bytes: 256
n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(n - 2):
  a = L[i]
  for j in range(i + 1, n - 1):
    b = L[j]
    l = j
    r = n
    while r - l > 1:
      p = (l + r) // 2
      if L[p] < a + b:
        l = p
      else:
        r = p
    count += l - j
print(count)


Measured memory_bytes: 73900
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N=1e5+1e3+7;

int n,a[N],ji,fo,ou;

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		if(a[i]&1)
			ji++;
		else
		if(a[i]%4==0)
			fo++;
	}
	if(ji-fo<=0||(ji-fo==1&&ji+fo==n))
		cout<<"Yes";
	else
		cout<<"No";
}

Measured memory_bytes: 640
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
using namespace std;
int main(int argc,char** argv){
  int n;
  cin >> n;
  string w[n];
  cin >> w[0];
  for(int i = 1; i < n ; i++){
    cin >> w[i];
  }
  for(int i = 1; i < n;i++){
    if(w[i][0] != w[i-1][w[i-1].size() - 1]){
      cout << "No" << endl;
      return 0;
    }
    for(int j = 0; j < i;j++){
      if(w[i] == w[j]){
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;
  return 0;
}



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

using namespace std;
#define ll long long 
#define db long double

int main() {
ll n,k;
cin>>n>>k;
ll a[n];
for(ll i=0;i<n;i++)
    cin>>a[i];
   // cout<<69<<endl;
    sort(a,a+n);
    ll sum=0;
  //  cout<<69<<endl;
    for(ll i=0;i<k;i++)
   {        //cout<<69<<endl;
       sum+=a[i];
    
   }
   // cout<<69<<endl;
    cout<<sum<<endl;
    
    return 0;
}

Measured memory_bytes: 3608
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    int N;
    cin >> N;
    string S;
    cin >> S;

    int l = S.size(), ans = 1;
    for(int i=1;i<l;i++){
        if(S[i] != S[i-1])
            ans++;
    }

    cout << ans << endl;
}

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

int main() {
	int a, b, c;
	cin >> a >> b >> c;

	if (a + b == c) cout << "Yes" << endl;
	else if (a + c == b)cout << "Yes" << endl;
	else if (b + c == a)cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 256
while True:
  a,op,b=map(str,input().split())
  a=int(a)
  b=int(b)
  if op=='+':
    print(a+b)
  if op=='-':
    print(a-b)
  if op=='*':
    print(a*b)
  if op=='/':
    print(int(a/b))
  if op=='?':
    break








Measured memory_bytes: 5596
import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        read().stream().map(Main::calc).forEach(System.out::println);
    }

    static int calc(List<Integer> dataSet) {
        List<Integer> sorted = dataSet.stream().sorted().collect(Collectors.toList());
        double average = sorted.subList(1, sorted.size() - 1).stream().mapToInt(i -> i).average().getAsDouble();
        return (int) Math.floor(average);
    }

    static List<List<Integer>> read() {
        List<List<Integer>> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int length;
        while ((length = scanner.nextInt()) > 0) {
            List<Integer> dataSet = new ArrayList<>();
            for(int i = 0; i < length; i++) {
                dataSet.add(scanner.nextInt());
            }
            list.add(dataSet);
        }
        scanner.close();
        return list;
    }
}

Measured memory_bytes: 33880
<?php
list($n) = ints();
list($s) = strs();
$cntR = 0;
for ($i = 0; $i < $n; ++$i)
    if ($s[$i] === 'R') ++$cntR;
echo $cntR > intdiv($n, 2) ? 'Yes' : 'No';
function strs()
{
    return explode(' ', trim(fgets(STDIN)));
}
function ints()
{
    return array_map('intval', explode(' ', trim(fgets(STDIN))));
}


Measured memory_bytes: 16280
from itertools import product

def max2(x,y):
    return x if x > y else y

N = int(input())
data = [[] for _ in range(N)]
res = 0
for i in range(N):
    for _ in range(int(input())):
        data[i].append(tuple(map(int, input().split())))
for a in product((0,1), repeat=N):
    flg = False
    for i in range(N):
        for x, y in data[i]:
            x -= 1
            if a[i] == 1:
                if a[x] != y:
                    flg = True
                    break
        if flg:
            break
        if i == N-1:
            res = max2(res, sum(a))
print(res)


Measured memory_bytes: 73948
function Main(input) {
  for (i = 1; i < 10; i++) {
    var result = input / i
    if (result < 10 && Math.round(result) === result) {
      console.log('Yes')
      return
    }
  }
  console.log('No')
}

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

Measured memory_bytes: 7372
#include<bits/stdc++.h>
#define ll long long 
#define P pair<int,int>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()

using namespace std;

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

    int n;
    cin >> n;

    vector<int> d(n);
    rep(i,n) cin >> d[i];

    int ans = 0;
    rep(i,n)rep(j,n){
        ans += d[i]*d[j];
    }

    rep(i,n) ans -= d[i]*d[i];


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

Measured memory_bytes: 256