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

#define rep(i, n)    for(ll i = 0; i < (n); ++i)
#define repA(i, a, n)  for(ll i = a; i <= (n); ++i)
#define repD(i, a, n)  for(ll i = a; i >= (n); --i)
#define pb push_back
#define X first
#define Y second

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
const ll INF = 1e18+1;
const ll MOD = 1000000007L;

#ifdef umang99m
    #include "/home/umang/.templates/header.h"
#else
    #define dbg(args...)
    #define timer()
#endif

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll n,m;
    cin>>n>>m;
    ll ans = ((n)*(n-1))/2 + ((m)*(m-1))/2;
    cout<<ans<<endl;

    timer();
    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <ctype.h>
const int bufSize = 1e6;
inline char nc()
{
    #ifdef DEBUG
    return getchar();
    #endif
    static char buf[bufSize], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) ? EOF : *p1++;
}
inline void read(char *s)
{
    static char c;
    for (; !isalpha(c); c = nc());
    for (; isalpha(c); c = nc()) *s++ = c;
    *s = '\0';
}
template<typename T>
inline T read(T &r)
{
    static char c;
    static int flag;
    flag = 1, r = 0;
    for (c = nc(); !isdigit(c); c = nc()) if (c == '-') flag = -1;
    for (; isdigit(c); c = nc()) r = r * 10 + c - 48;
    return r *= flag;
}
char s[4];
int len[4];
int main()
{
    read(s + 1);
    int ans = 0;
    for(int i = 1;i<=3;++i) if(s[i] == 'R')  len[i] = len[i-1] + 1,ans = std::max(ans,len[i]);
    printf("%d\n",ans);
    return 0;
}

Measured memory_bytes: 3128
/*-----lusar98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define mod 1000000007
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
int main()
{
    //fast();
    ll h,w;
    cin>>h>>w;
    string arr[h];
    for(ll i=0;i<h;i++)
    	cin>>arr[i];
    ll dp[h+2][w+2];
    memset(dp,0,sizeof dp);
    dp[0][1]=1;
    for(ll i=0;i<h;i++)
    {
    	for(ll j=0;j<w;j++)
    	{
    		if(arr[i][j]=='.')
    		{
    			dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j]) % mod;
    		}
    	}
    }
    cout<<dp[h][w]<<endl;
}


Measured memory_bytes: 9088
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        s: chars
    }
    let n = s.len();
    for i in 0..n / 2 {
        if s[i] != s[n / 2 - 1 - i] || s[i] != s[n / 2 + 1 + i] || s[i] != s[n - i - 1] {
            println!("No");
            return;
        }
    }
    println!("Yes");
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
//#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define ones(n) __builtin_popcount(n)
#define ONES(n) __builtin_popcountll(n)

using namespace std;
//using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> ii;
typedef pair<long long, pair<int, int>> iii;
typedef pair<ii, ii> iiii;

const double PI = acos(-1.0), EPS = 1e-6;
const int N = 3003, M = 102, mod = 998244353, mxLog = 20;
string s, t;
int memo[N][N];
int solve (int i, int j) {
    int &ret = memo[i][j];
    if (~ret)
        return ret;
    ret = 0;
    if (i == s.size())
        return ret;
    if (j == t.size()) {
        return ret;
    }
    ret = solve(i + 1, j);
    ret = max(ret, solve(i, j + 1));
    if (s[i] == t[j]) {
        ret = max(ret, solve(i + 1, j + 1) + 1);
    }
    return ret;
}


void findPath() {
    int i = 0, j = 0;
    while (i < s.size() && j < t.size()) {
        if (memo[i][j] == memo[i + 1][j]) {
            ++i;
        } else if(memo[i][j] == memo[i][j + 1]) {
            ++j;
        } else {
//            cout << i << ' ' << j << ' ' << memo[i][j] << endl;
            cout << s[i];
            ++i;
            ++j;
        }
    }

}


int main() {
#ifndef ONLINE_JUDGE
 //   freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(7), cout << fixed;

    cin >> s >> t;
    memset(memo, -1, sizeof memo);
    solve(0, 0);
    findPath();

    return 0;
}

Measured memory_bytes: 35840
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>

using namespace std;

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

	if ((1 <= A && A <= 9) && (1 <= B && B <= 9)) {
		cout << A * B << endl;
	}
	else {
		cout << -1 << endl;
	}


}






Measured memory_bytes: 3608
package main

import (
	"fmt"
)

func main() {
	// mainA()
	mainB()
}

func mainA() {
	var numH, numW, h, w int
	fmt.Scan(&numH, &numW, &h, &w)
	var res = solveA(numH, numW, h, w)
	fmt.Print(res)
}

func solveA(numH int, numW int, h int, w int) int {

	var res = 0
	res = (numH - h) * (numW - w)
	return res
}

func mainBInputTest(bArrayArg []int, aArrayArg [][]int, n int, m int, c int) int {

	var bArray []int
	for i := 0; i < m; i++ {
		bArray = append(bArray, bArrayArg[i])
	}
	//多次元配列使う前にはmake
	aArray := make([][]int, n)
	for outer := 0; outer < n; outer++ {
		/*
		* 長さ0で作成
		* これで要素の後ろに追加できるようになる
		 */
		aArray[outer] = make([]int, 0)
		for i := 0; i < m; i++ {
			// aArray[outer][i] = b
			aArray[outer] = append(aArray[outer], aArrayArg[outer][i])
		}
	}

	var res = solveB(bArray, aArray, n, m, c)

	return res
}

func mainB() {
	var n, m, c int
	fmt.Scan(&n, &m, &c)
	var bArray []int
	for i := 0; i < m; i++ {
		var b int
		fmt.Scan(&b)
		bArray = append(bArray, b)
	}
	//多次元配列使う前にはmake
	aArray := make([][]int, n)
	for outer := 0; outer < n; outer++ {
		/*
		* 長さ0で作成
		* これで要素の後ろに追加できるようになる
		 */
		aArray[outer] = make([]int, 0)
		for i := 0; i < m; i++ {
			var b int
			fmt.Scan(&b)
			// aArray[outer][i] = b
			aArray[outer] = append(aArray[outer], b)

		}
	}

	var res = solveB(bArray, aArray, n, m, c)
	fmt.Println(res)
}

func solveB(bArray []int, aArray [][]int, n int, m int, c int) int {
	var res, point = 0, 0
	for outer := 0; outer < n; outer++ {
		point = 0
		for i := 0; i < m; i++ {
			point += aArray[outer][i] * bArray[i]
		}
		if point+c > 0 {
			res++
		}
	}
	return res
}


Measured memory_bytes: 512
// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

void testcase() {
    ll n, k; cin >> n >> k;
    if (n % k == 0) {
    	cout << 0 << "\n";
    	return;
    }

    if (n > k) {
    	n = n % k;
    }

    if (n < (k-n)) cout << n;
    else cout << (k-n);

}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    if (TC)
        cin>>t;
    while(t--) {
        testcase();
    }
    return 0;
}

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

using namespace std;

typedef long long ll;

ll x, q, r, ans;

int main()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin>>x;
  q = x/11;
  r = x%11;
  ans = 2*q;
  if(r>6)
    ans+=2;
  else if(r>0)
    ans++;
  cout<<ans<<"\n";
  return 0;
}


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

public class Main {
	
	public void solve() throws IOException {
		int n = nextInt();
		Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
		for(int i = 0; i < n; i++){
			int id = nextInt();
			int num = nextInt();
			Set<Integer> set = new HashSet<Integer>();
			for(int j = 0; j < num; j++){
				int x = nextInt();
				set.add(x);
			}
			map.put(id, set);
		}
		int m = nextInt();
		for(int i = 0; i < m; i++){
			int start = nextInt();
			int stop = nextInt();
			int live = nextInt();
			int repeat = live-1;
			Set<Integer> temp = new HashSet<Integer>(map.get(start));
			Set<Integer> add = new HashSet<Integer>(map.get(start));
		out:while( repeat-- > 0 && !temp.contains(stop) ){
				Set<Integer> next = new HashSet<Integer>();
				for(Integer x : add){
					for(Integer y : map.get(x)){
						if( !temp.contains(y) ){
							temp.add(y);
							next.add(y);
						}
					}
				}
				add = new HashSet<Integer>(next);
			}
			if( repeat >= 0 ){
				writer.println(live-repeat);
			} else {
				writer.println("NA");
			}
		}					
		writer.flush();
	}
	public static void main (String args[]) throws IOException{
		new Main().run();
	}
	
	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;
	
	public void run() throws IOException{
		try{
			reader = new BufferedReader(new InputStreamReader(System.in));
			tokenizer = null;
			writer = new PrintWriter(System.out);
			solve();
			reader.close();
			writer.close();
		} catch (Exception e){
			e.printStackTrace();
			System.exit(1);
		}
	}
	public int nextInt() throws IOException{
		return Integer.parseInt(nextToken());
	}		
	public String nextToken() throws IOException{
		while( tokenizer == null || !tokenizer.hasMoreTokens() ){
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
}

Measured memory_bytes: 11000
//Allocation
#include <iostream>
#include <climits>

using namespace std;

bool check(int w[], int n, int k, int p) {
    int cnt = 1;
    int margin = p;
    for(int i=0; i<n; i++) {
        if(w[i] > p) return false;
        if(margin >= w[i]) margin -= w[i];
        else {
            cnt++;
            margin = p - w[i];
        }
    }
    return cnt <= k;
}

int solve(int w[], int n, int k) {
    int  left = 0, right = INT_MAX, mid;
    while(left < right) {
        mid = (left + right) / 2;
        if(check(w, n, k, mid)) right = mid;
        else left = mid+1;
    }
    return left;
}

int main() {
    int n, k;
    cin >> n >> k;
    int w[n];
    for(int i = 0; i < n; i++) {
        cin >> w[i];
    }

    cout << solve(w, n, k) << endl;
}



Measured memory_bytes: 3488
#include<stdio.h>

int main()

{

    int A,N,i,temp;

    scanf("%d %d",&A,&N);

    temp = A;

    if(N>0)
    {
        while(temp>500)
        {
            temp = temp - 500;
        }

        if(temp<=N)
        {

            printf("Yes\n");

        }

        else
        {

            printf("No\n");

        }
    }

    else
    {
       if(temp%500==0)
       {
          printf("Yes\n");
       }

       else
       {
          printf("No\n");
       }
    }

    return 0;

}


Measured memory_bytes: 1628
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <complex>
#include <functional>
#include <utility>
#include <iterator>

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (m); i < (int)(n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define INF 2000000000

#ifdef LOCAL
  #define dprintf(...) fprintf(stdout, __VA_ARGS__)
#else
  #define dprintf(...) 0
#endif

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;

class R {
  public:
    int num;
    string city;
    int score;
};

bool cmp_city(const R *lhs, const R *rhs) {
  return lhs->city < rhs->city;
}

bool cmp_score(const R *lhs, const R *rhs) {
  return lhs->score < rhs->score;
}

int main() {
  int n;
  cin >> n;
  vector<R*> v(n);
  REP(i, n) {
    string s;
    int p;
    cin >> s >> p;
    R *r = new R();
    r->num = i + 1;
    r->city = s;
    r->score = p;
    v[i] = r;
  }

  sort(v.rbegin(), v.rend(), cmp_score);
  stable_sort(v.begin(), v.end(), cmp_city);

  REP(i, n) {
    cout << v[i]->num << endl;
  }

  return 0;
}


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

int main(void){
  int n;
  while(cin>>n,n){
    vector<int> k(n);
    int maxc=0,count=0;
    for(int i=0;i<n;++i){
      cin>>k[i];
      maxc=max(maxc,k[i]);
      if(k[i])++count;
    }
    if(maxc<=1)
      cout<<"NA"<<endl;
    else
      cout<<(count+1)<<endl;
  }
  return 0;
}

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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
//#define int ll
//#define int __int128
//#define ll __int128
typedef pair<int, int   > pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
//typedef complex<double> base;

#define inmin(a, b) a = min(a, (b))
#define inmax(a, b) a = max(a, (b))
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for(int i = 0; i < (int)(n); ++i)
#define SZ(a) ((int)((a).size()))
#define triple(T) tuple<T, T, T>
#define quad(T) tuple<T, T, T, T>
#define watch(x) cerr << (#x) << " = " << (x) << endl;

#ifdef RUS_HOME
#define cerr cout
#else
#define cerr if (false) cerr
#endif

const double PI = 2 * acos(0.0);
#define rand shittttty_shit
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";

template <class T0, class T1>
inline ostream & operator << (ostream &out, pair<T0, T1> &a) {
    return out << "{" << a.first << ", " << a.second << "}";
}

template <class T0, class T1, class T2>
inline ostream & operator << (ostream &out, tuple<T0, T1, T2> &a) {
    return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}";
}

template <class T0, class T1, class T2, class T3>
inline ostream & operator << (ostream &out, tuple<T0, T1, T2, T3> &a) {
    return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " <<  get<3>(a) << "}";
}

template<class T>
inline ostream & operator << (ostream &out, vector<T> &a) {
    out << "[";
    fori (i, a.size())
        out << a[i] << vector<string>{", ", "]  "}[i + 1 == a.size()];
    return out;
}



void smain();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

#ifdef RUS_HOME
    freopen("input", "r", stdin);
    clock_t start = clock();
#endif
    cout << setprecision(12) << fixed;
    smain();
#ifdef RUS_HOME
    cout << "\n\n\n\nTOTAL EXECUTION TIME: " << float( clock () - start ) /  CLOCKS_PER_SEC << endl;
#endif
    return 0;
}

void smain() {

    string s;
    cin >> s;

    map <char, int> mp;
    for (int i = 0; i < s.size(); i++) {
        mp[s[i]] = 1;
    }

    if (mp['E'] && !mp['W'] || mp['W'] && !mp['E'] || mp['N'] && !mp['S'] || mp['S'] && !mp['N'])
    {
        cout << "No";
        return;
    }
    cout << "Yes";

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef long long ll;
#define sz(x) int(x.size())
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define MOD 1000000007
using namespace std;

int main(){
    int A,B;
    cin >> A >> B;
    cout << (A+B)%24 << endl;
}

Measured memory_bytes: 256
number=list(input())

paturn1=list("10"*(10**5))
paturn2=list("01"*(10**5))
convert1=[int(x)^int(y) for x,y in zip(number,paturn1)]
convert2=[int(x)^int(y) for x,y in zip(number,paturn2)]

print(min([convert1.count(1),convert2.count(1)]))


Measured memory_bytes: 8752
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <random>
#include <chrono>
#include <iomanip>
#define lint long long int
#define FOR(x, to) for(int x=0; x<(int)(to); x++) // for簡略表記
#define DUMP(ar) for(int loop_dump=0; loop_dump<(int)ar.size(); loop_dump++) cout << "[" << loop_dump << "]:" << ar[loop_dump] << endl // 配列一覧表示
#define DUMPL(ar) for(int loop_dump=0; loop_dump<(int)ar.size(); loop_dump++) { cout << ar[loop_dump]; if(loop_dump<(int)ar.size()-1) cout << ' '; } cout << endl; // 配列一行一覧表示
#define COUT(x) cout << x << endl // 単要素出力
#define ALL(ar) ar.begin(), ar.end() // コンテナの全要素
#define LOOPD(i) cout << "ループ[" << i << "]回目---------------" << endl // ループ回数デバグ
using namespace std;
// cout << fixed << setprecision(4) で小数点以下4桁の桁そろえ
// cout << setw(5) << ... で表示形式を5桁に設定 5桁未満なら頭に空白がつく 5桁越なら先頭から5桁まで
// cout << setw(5) << setfill('0') << ... で空白部分の埋め文字に5を使う
// getline(cin, [変数]); で空白などを含む入力の受け取り(行ごとの受け取り)
// cin >> ws; でそこまでにストリームに残されたホワイトスペースを取り除く(=以降のgetlineの有効化)

int gcd(int x, int y){
    if(x < y){
        int tmp = x;
        x = y;
        y = tmp;
    }

    // 8 6
    // 6 2
    int r = x % y;
    while(r != 0){
        x = y;
        y = r;
        r = x % y;
    }
    return y;
}

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

    int n, m;
    cin >> n >> m;
    
    int a = m / n;
    for(int i=a; i>0; i--){
        if( (m - i*n) % i == 0 ){
            COUT(i);
            return 0;
        }
    }

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <iomanip>
#include <cassert>
#include <functional>


using namespace std;

int main() {
  int n;
  cin>>n;
  vector<int> a(n,0);

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

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

  return 0;
}

Measured memory_bytes: 4052
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;
vector<P> g[100000];
ll d[100000];
bool used[100000];
void dfs(int x){
  used[x]=1;
  for(auto p:g[x]){
    int y=p.first;
    if(used[y]) continue;
    d[y]=d[x]+p.second;
    dfs(y);
  }
}
    
int main()
{
	int n;
  cin>>n;
  for(int i=0; i<n-1; i++){
    int a, b; ll c;
    cin>>a>>b>>c;
    a--; b--;
    g[a].push_back(P(b, c));
    g[b].push_back(P(a, c));
  }
  int q, k;
  cin>>q>>k;
  k--;
  dfs(k);
  for(int i=0; i<q; i++){
    int x, y; cin>>x>>y;
    x--; y--;
	cout<<d[x]+d[y]<<endl;
  }
	return 0;
}


Measured memory_bytes: 12800
#include <iostream>
#include <vector>
using i64 = long long;

int main() {
    int n, k;
    std::cin >> n >> k;
    std::vector<int> a(n + 1);
    for (int i = 0; i < k; i++) {
        int d;
        std::cin >> d;
        while (d--) {
            int j;
            std::cin >> j;
            a[j]++;
        }
    }

    int ret = 0;
    for (int i = 1; i <= n; i++) if (!a[i]) ret++;

    std::cout << ret << std::endl;

    return 0;
}


Measured memory_bytes: 3576
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include<cstring>
#include<string>
#include<stack>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, char> pic;
typedef priority_queue<pii, vector<pii>, greater<pii> > RPQ;
#define mk make_pair
#define INF (1 << 30)
#define INFL (1ll << 60ll)
#define mod 1000000007

int N, M;
vector<int> G[100100];
bool used[100100];
int ans;

signed main(){
    cin >> N >> M;
    for(int i = 0; i < M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(int i = 0; i < N; i++){
        if(!used[i]){
            ans++;
            queue<int> que;
            que.push(i);
            used[i] = true;
            while(!que.empty()){
                int pos = que.front();
                que.pop();
                for(int j = 0; j < (int)G[pos].size(); j++){
                    int to = G[pos][j];
                    if(!used[to]){
                        que.push(to);
                        used[to] = true;
                    }
                }
            }
        }
    }
    cout << ans << endl;

    return 0;
}
/*
6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5


*/

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

using namespace std;

#define int long long

typedef pair<int,int> ii;

map<int,int> cnt, first_pos;
priority_queue<int> pq;

int n;
int a[100001], ans[100001];

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

	cin >> n;
	int pre = -1;
	for (int i=1;i<=n;i++)
	{
		cin >> a[i];
		if (!cnt.count(a[i]))
		{
			pq.push(a[i]);
			first_pos[a[i]] = i;
		}	
		cnt[a[i]]++;
	}
	while(!pq.empty())
	{
		if (first_pos[pq.top()] == 1)
		{
			ans[1] += cnt[pq.top()]*pq.top();
			pq.pop();
			while(!pq.empty())
			{
				ans[1] += cnt[pq.top()]*pq.top();
				pq.pop();
			}			
		}
		else
		{
			int cur_pos = first_pos[pq.top()];
			int cur_val = pq.top();
			pq.pop();
			ans[cur_pos] += cnt[cur_val]*(cur_val - pq.top());
			if(first_pos[pq.top()] > cur_pos)
				first_pos[pq.top()] = cur_pos;
			cnt[pq.top()] += cnt[cur_val];
		}
	}
	for (int i=1;i<=n;i++)
		cout << ans[i] << '\n';
}

Measured memory_bytes: 15860
#include <bits/stdc++.h>
using namespace std;
long long int dp[105][100005];
long long int rec(vector<int>& W, vector<int>& V, int n, int w){
	if(n == 0 or w == 0){
		return 0;
	}
	if (dp[n][w] != -1) return dp[n][w];
	else if(w >= W[n-1] ){
		return dp[n][w] = max(V[n-1]+rec(W, V, n-1, w-W[n-1]), rec(W, V, n-1, w));
	}
	else{
			return dp[n][w] = rec(W, V, n-1, w);
	}
}
int main() {
	int n, w;
	cin >> n >> w;
	memset(dp, -1, sizeof(dp));
	vector<int> W(n), V(n);
	for(int i = 0; i < n; i++){
		cin >> W[i];
		cin >> V[i];
	}
	cout << rec(W, V, n, w);
	return 0;
}

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

using namespace std;

#define ll                     long long int
#define endl                   '\n'

static const int maxn = 2e5 + 5;

signed main()
{
      ios_base::sync_with_stdio(false);
      cin.tie(nullptr);
      cout.tie(nullptr);
      cout << fixed << setprecision(12);

     // freopen("in.txt", "r", stdin);

      int a;
      string s;
      cin >> a >> s;
      if (a >= 3200) cout << s << endl;
      else cout << "red" << endl;
}


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

#define INF 1e9
using namespace std;
typedef long long ll;

int main() {

    int n; cin >> n;

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

    vector<int> y(n);
    y = x;
    sort(y.begin(), y.end());

    for (int i = 0; i < n; ++i) {
        if (x[i] < y[n/2]) {
            cout << y[n/2] << endl;
        } else {
            cout << y[(n/2)-1] << endl;
        }
    }
}

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

int k,x,low,high;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin>>k>>x;
    low=x-k+1;
    high=x+k-1;
    for(int i=low;i<=high;i++)
        cout<<i<<" ";
}


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

typedef long long ll;
#define rep(i, n) for(long long i = 0; i < (long long)(n); i++)
#define memset(v, h) memset((v), h, sizeof(v))
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
using P = pair<ll, ll>;
const int INF = 1001001001;

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

struct uf {
    vector<ll> par;
    vector<ll> sizes;

    uf(int n)
    {
        for(int i = 0; i < n; i++) {
            par.push_back(i);
            sizes.push_back(1);
        }
    }

    ll root(int x) 
    {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) 
    {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        par[rx] = ry;
        sizes[ry] = sizes[rx] + sizes[ry];
    }

    bool same(int x, int y) 
    {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }

    ll size(int x)
    {
        return sizes[root(x)];
    }
};

struct node
{
    public: ll x;
    public: ll y;

    node(ll xx, ll yy)
    {
        x = xx;
        y = yy;
    }
};

ll comb(int n, int r) {
  std::vector<std::vector<long long>> v(n + 1,std::vector<long long>(n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v[n][r];
}

ll digit(ll num)
{
    if(num < 10)
    {
        return 1;
    }

    ll d = 1;
    ll k = 10;
    while(num >= k)
    {
        d++;
        k *= 10;
    }

    return d;
}

ll digitNum(ll num, ll count)
{
    ll a = (num % (ll) pow((double) 10, (double) count));
    return a / pow(10, count - 1);
}

bool less_abs(int lhs, int rhs) {
    if(lhs < 0)
    {
        lhs *= -1;
    }
    if(rhs < 0)
    {
        rhs *= -1;
    }

    return lhs < rhs;
}

bool IsTrueBit(int bit, int index)
{
    return (bit & (1 << index));
}

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

    ll x[n];
    rep(i,n) cin >> x[i];

    ll cost = INF;

    rep(i,100)
    {
        ll s = i + 1;
        ll c = 0;
        rep(j,n)
        {
            c += (x[j] - s) * (x[j] - s);
        }

        if(c < cost)
        {
            cost = c;
        }
    }

    cout << cost << endl;

    return 0;
}

Measured memory_bytes: 256
/**
 *      ABC 71 B
 *      author  : kyomukyomupurin
 *      created : 2018-09-04 22:41:14
**/

#include <bits/stdc++.h>
using namespace std;

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

    for (char c = 'a'; c <= 'z'; ++c){
        if (string(s).find(c) == string::npos){
            cout << c << endl; return 0;
        }
    }
    cout << "None" << endl;
    return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
#define INF_LL 1LL << 40
#define INF 2000000000
#define MOD 1000000007
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for(int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define prique priority_queue
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vector<int>> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
struct edge {
    int to, cost;
};

ll dp[101][8];

int main() {
    memset(dp, 0, sizeof(dp));
    ll h, w, k;
    bool flg;
    cin >> h >> w >> k;
    dp[0][0] = 1;
    ll p = 1;
    rep(i, w - 1) p *= 2;
    rep(i, h) rep(j, p) {
        flg = false;
        rep(k, w - 2) {
            if(((j >> k) & 1) && ((j >> (k + 1)) & 1))
                flg = true;
        }
        if(flg)
            continue;
        rep(k, w) {
            if((k != 0) && (j >> (k - 1)) & 1) {
                dp[i + 1][k] += dp[i][k - 1];
            } else if((k != w - 1) && ((j >> k) & 1)) {
                dp[i + 1][k] += dp[i][k + 1];
            } else {
                dp[i + 1][k] += dp[i][k];
            }
            dp[i + 1][k] %= MOD;
        }
    }
    // rep(i, h + 1) {
    //     rep(j, w) cout << dp[i][j] << " ";
    //     cout << endl;
    // }
    cout << dp[h][k - 1] << endl;
}


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

int main(int argc,char **argv) {
	int m,d;
	cin>>m>>d;
	int ans=0;
	for(int i=2;i<10;i++)
	for(int j=2;j<10;j++) {
		if(i*j<=m && i*10 + j <=d) ans++;
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 256
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(3)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] += tmp[i-1][0] + absDiff
    tmp[i][1] += tmp[i-1][1] + absDiff ** 2
    tmp[i][2] += tmp[i-1][2] + absDiff ** 3
    if absDiff > max:
        max = absDiff
print(tmp[n][0])
print(tmp[n][1] ** (1/2))
print(tmp[n][2] ** (1/3))
print(max)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;

#define X first
#define Y second

const int MAXN = 100010;

int n;
vector<int> v[MAXN];
int pre[MAXN], size[MAXN];

void build(int x)
{
	size[x] = 1;
	for (auto y : v[x])
	{
		if (y == pre[x]) continue;
		pre[y] = x;
		build(y);
		size[x] += size[y];
	}
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i < n; ++ i)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		v[x].push_back(y);
		v[y].push_back(x);
	}
	build(1);
	vector<int> p;
	for (int i = n; i; i = pre[i])
		p.push_back(i);
	reverse(p.begin(), p.end());
	int x = p[(p.size()+1)/2];
	int b = size[x];
	int a = n-b;
	puts(a > b ? "Fennec" : "Snuke");
	return 0;
}


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

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define int long long
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;

signed main(){
    int a,b;
    cin >> a >> b;
    cout << max({a+b,a-b,a*b}) << '\n'; 
    return 0;   
}


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

using Weight = int;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, Weight weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

int const MAX_V = 10010;

void stronglyConnectedComponents(const Graph &g, int cmp[MAX_V]){
    int n = g.size(); Graph rg(n);
    for(const Edges &es : g) for(const Edge &e : es) rg[e.dst].eb(e.dst, e.src, e.weight);
    static int added[MAX_V], visited[MAX_V], ord[MAX_V], S[MAX_V];
    for(int i = 0; i < n; i++){
        added[i] = visited[i] = 0;
        cmp[i] = -1;
    }
    int sz = 0;
    for(int i = 0; i < n; i++)if(!visited[i]){
        int s = 0;
        S[s++] = i;
        while(s){
            int v = S[s-1]; visited[v] = true;
            bool pushed = false;
            for(auto & e : g[v]){
                int dst = e.dst;
                if(!visited[dst]){ S[s++] = dst; pushed = true; }
            }
            if(pushed) continue;
            int t = S[s-1];
            if(!added[t]){ added[t] = true; ord[sz++] = t; }
            --s;
        }
    }
    int k = 0;
    for(int i = 0; i < n; i++){
        int u = ord[n-i-1];
        if(cmp[u] != -1) continue;
        int s = 0; S[s++] = u;
        while(s){
            int v = S[--s]; cmp[v] = k;
            for(auto & e : rg[v]){
                int d = e.dst;
                if(cmp[d] == -1) S[s++] = d;
            }
        }
        ++k;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n,m;
    cin >> n >> m;
    Graph g(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        g[a].eb(a,b,0);
    }
    int c[MAX_V];
    stronglyConnectedComponents(g,c);
    int q; cin >> q;
    rep(i,q){
        int a,b;
        cin >> a >> b;
        cout << (c[a]==c[b]) << '\n';
    }
}

Measured memory_bytes: 4144
package main

import (
	"fmt"
	"io"
	"os"
)

func split(n int) [5]int {
	var arr [5]int
	for i := 0; i < len(arr); i++ {
		arr[i] = n % 10
		n /= 10
	}
	return arr
}

func sum(arr [5]int) int {
	res := 0
	for i := 0; i < len(arr); i++ {
		res += arr[i]
	}
	return res
}

func proc(r io.Reader, w io.Writer) {
	var n, a, b int
	fmt.Fscanf(r, "%d", &n)
	fmt.Fscanf(r, "%d", &a)
	fmt.Fscanf(r, "%d", &b)
	res := 0
	for i := 1; i <= n; i++ {
		if tmp := sum(split(i)); a <= tmp && tmp <= b {
			res += i
		}
	}

	fmt.Fprintf(w, "%d", res)
}

func main() {
	proc(os.Stdin, os.Stdout)
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main(){
	long long int K,A,B;
	cin >> K >> A >> B;
	if(A >= K){
		cout << 1 << endl;
		return 0;
	}
	if(A <= B){
		cout << -1 << endl;
		return 0;
	}
	cout << ((K - A - 1) / (A - B) + 1) * 2 + 1 << endl;
	return 0;
}


Measured memory_bytes: 256
EPS = 10**-10
n = int(input())
pos = []

for _ in range(n):
  pos.append(list(map(float, input().split())))

for i in range(n):
  vec_ax = pos[i][2]-pos[i][0]
  vec_ay = pos[i][3]-pos[i][1]
  vec_bx = pos[i][6]-pos[i][4]
  vec_by = pos[i][7]-pos[i][5]
  if abs(vec_bx*vec_ay - vec_by*vec_ax) < EPS:
    print("YES")
  else:
    print("NO")


Measured memory_bytes: 5644
#include <stdio.h>
  #include <math.h>

 #define PI 3.14159265358979323846264338327950L

 int main(void) {
 int a, b, h, m;
 scanf("%d%d%d%d", &a, &b, &h, &m);
 long double rad = PI * 2 * (
 (long double)h / 12.0 + ((long double)m / 60.0) / 12.0 - (long
 double)m / 60.0);
 long double rsq = (long double)(a * a + b * b) - (long double)(2 * a *
b) * cosl(rad);

 printf("%20.20Lf\n", sqrtl(rsq));

 return 0;
 }


Measured memory_bytes: 2560
#include<iostream>
#include<vector>
// #include<climits>
#include<algorithm>
#include<string>
// #include<stack>
// #include<queue>

using namespace std;

int main(){
    string s,t;
    cin>>s>>t;
    int cnt = 0;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]==t[i])
            cnt++;
    }
    cout<<cnt<<endl;
}

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

vector<int64_t> divisor(int64_t N) {
  vector<int64_t> div;
  for (int64_t i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      div.push_back(i);
      
      if (i * i != N) {
        div.push_back(N / i);
      }
    }
  }
  
  sort(div.begin(), div.end());
  return div;
}

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> div = divisor(N);
  int64_t M = div.size();
  
  if (M % 2LL == 1LL) {
    cout << 2LL * div.at((M + 1LL) / 2LL - 1LL) - 2LL << endl;
  } else {
    cout << div.at(M / 2LL - 1LL) + div.at(M / 2LL) - 2LL << endl;
  }
}


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

int main() {
  int n;
  string c;
  cin >> n >> c;
  
  int ans = 0, r = n-1;
  for(int i = 0; i < n; i++){
    char now = c.at(i);
    if(now == 'W'){
      bool ok = true;
      for(int j = r; j > i; j--){
        char next = c.at(j);
        if(next == 'R'){
          ans++;
          r = j-1;
          ok = false;
          break;
        }
      }
      if(ok) break;
    }
  }
  
  cout << ans << endl;
}

Measured memory_bytes: 3756
import java.util.*;
public class Main
{
  public static void main(String args[])
  {
    Scanner in=new Scanner(System.in);
    String s=in.next();
    String t=in.next();
    int n=in.nextInt();
    int b=in.nextInt();
    String u=in.next();
    if(u.compareTo(s)==0)
    {
      System.out.println((n-1)+" "+b);
    }
    else if(u.compareTo(t)==0)
    {
       System.out.println(n+" "+(b-1));
    }
   else
   {
     System.out.println(n+" "+b);
   }
  }
}

Measured memory_bytes: 38308
use std::cmp;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
    	a:usize,
        b:usize,
    }
    let cand = ((a+b)/2).to_string();
       
    println!("{}", if a%2!=b%2 { "IMPOSSIBLE" } else { &cand });
}

Measured memory_bytes: 4352
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC081B
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            int ans = 0;

            while (a.All(x => x % 2 == 0))
            {
                ans++;
                a = a.Select(x => x / 2).ToArray();
            }

            Console.WriteLine(ans);
        }
    }
}


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

string S;

int main(){
  cin >> S;
  int len = S.size();
  int ans = 1000;
  for (int i=0; i<len-2; i++) {
    int tmp = stoi(S.substr(i, 3));
    ans = min(ans, abs(tmp-753));
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
  
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define prev fhsgfhjf
#define plus fsghsf
#define minus ytryr
  
using namespace std;
  
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
  
const int N = (1 << 18) + 239;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const ld eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

const int B = 1000;
int V[N], W[N];
int dp[B][100000 + 1];

int ans = 0;

void solve(int u, int cur, int rem) {
    if (rem == 0) {
        setmax(ans, cur);
        return;
    }
    if (u < B) {
        setmax(ans, dp[u][rem] + cur);
        return;
    }
    solve((u - 1) / 2, cur, rem);
    if (W[u] <= rem) {
        solve((u - 1) / 2, cur + V[u], rem - W[u]);
    }
}

int main()
{
    //freopen("circus.in", "r", stdin);
    //freopen("circus.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    cout << fixed;
    //ll TL = 10.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    int n;
    cin >> n;
    rep(i, 0, n) {
        cin >> V[i] >> W[i];
    }
    dp[0][W[0]] = V[0];
    rep(i, 1, B) {
        int p = (i - 1) / 2;
        memcpy(dp[i], dp[p], sizeof(int) * 100001);
        repb(w, 100000, W[i] - 1) {
            setmax(dp[i][w], V[i] + dp[i][w - W[i]]);
        }
    }
    rep(i, 0, B) {
        rep(w, 1, 100000 + 1) {
            setmax(dp[i][w], dp[i][w - 1]);
        }
    }
    int Q;
    cin >> Q;
    while (Q--) {
        int u, L;
        cin >> u >> L; u--;
        ans = 0;
        solve(u, 0, L);
        cout << ans << "\n";
    }
    return 0;
}



Measured memory_bytes: 396260
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>

#define MOD 1000000007LL

using namespace std;
using pii=pair<int,int>;
using ll=long long;

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

    string l;
    cin>>l;

    vector<ll> dp3(100001);
    dp3[0]=1;
    for(int i=1; i<=100000; i++) dp3[i]=(dp3[i-1]*3)%MOD;

    ll tmp=1;
    ll answer=0;
    for(int i=0; i<l.length(); i++){
        ll nowanswer=0;
        if(l[i]=='1'){
            nowanswer=(dp3[l.length()-i-1]*tmp)%MOD;
            tmp=(tmp*2)%MOD;
            answer=(answer+nowanswer)%MOD;
        }
    }
    answer=(answer+tmp)%MOD;

    cout<<answer;

    return 0;
}


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

int cnt[200200];

int main(){
	int n,k;
	cin>>n>>k;
	int x;
	for (int i=1;i<=n;i++){
		cin>>x;
		cnt[x]++; 
	}
	sort(cnt+1,cnt+n+1);
	int i=1;
	while (!cnt[i]) i++;
	int ans=0;
	while (n-i+1>k) ans+=cnt[i],i++;
	cout<<ans<<endl;
	return 0;
} 

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

using namespace std;

const int N=4005;
int n,A,B,st[N],m,g;
char s[15],o[N];

int main(){
	scanf("%d%d",&A,&B);
	if(A<=B)puts("Impossible"),fflush(stdout),exit(0);
	n=A+B;
	for(int i=0;i<n;i++){
		if(m){
			printf("? %d %d\n",st[m],i);
			fflush(stdout);
			scanf("%s",s);
			if(*s=='Y')st[++m]=i;else st[m--]=0;
		}else st[++m]=i;
	}
	g=st[m];
	for(int i=0;i<n;i++){
		printf("? %d %d\n",g,i);
		fflush(stdout);
		scanf("%s",s);
		if(*s=='Y')o[i]='1';else o[i]='0';
	}
	printf("! %s\n",o);
	fflush(stdout);
	return 0;
}

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

using namespace std;

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

    int N;
    cin >> N;
    vector<string> s(N);
    vector<int> mi(N, 0), sum(N);
    int total = 0;
    for (int i = 0; i < N; i++)
    {
        int cnt = 0;
        cin >> s[i];
        for (int j = 0; j < (int)s[i].length(); j++)
        {
            if (s[i][j] == '(')
                cnt++;
            else
                cnt--;
            mi[i] = min(mi[i], cnt);
        }
        sum[i] = cnt;
        total += cnt;
    }
    if (total != 0)
    {
        cout << "No" << endl;
        return 0;
    }
    vector<bool> used(N, false);
    for (int i = 0; i < N; i++)
    {
        if (mi[i] >= 0 && sum[i] >= 0)
        {
            total += sum[i];
            used[i] = true;
        }
    }
    while (true)
    {
        bool update = false;
        for (int i = 0; i < N; i++)
        {
            if (used[i])
                continue;
            if (sum[i] >= 0 && total + mi[i] >= 0)
            {
                total += sum[i];
                used[i] = true;
                update = true;
            }
        }
        if (!update)
            break;
    }
    for (int i = 0; i < N; i++)
    {
        if (used[i])
            continue;
        int cnt = 0;
        mi[i] = 0;
        sum[i] = 0;
        reverse(s[i].begin(), s[i].end());
        for (int j = 0; j < (int)s[i].length(); j++)
        {
            if (s[i][j] == ')')
                cnt++;
            else
                cnt--;
            mi[i] = min(mi[i], cnt);
        }
        sum[i] = cnt;
    }
    total = 0;
    for (int i = 0; i < N; i++)
    {
        if (used[i])
            continue;
        if (mi[i] >= 0 && sum[i] >= 0)
        {
            total += sum[i];
            used[i] = true;
        }
    }
    while (true)
    {
        bool update = false;
        for (int i = 0; i < N; i++)
        {
            if (used[i])
                continue;
            if (sum[i] >= 0 && total + mi[i] >= 0)
            {
                total += sum[i];
                used[i] = true;
                update = true;
            }
        }
        if (!update)
            break;
    }
    for (int i = 0; i < N; i++)
    {
        if (!used[i])
        {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}



Measured memory_bytes: 3244
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int tsize = s.size();
    int snum = 0;
    int hast = 0;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'S') {
                snum++;
        }
        if (s[i] == 'T') {
            if (snum > 0) {
                snum--;
                tsize -= 2;
                hast = 1;
            }
        }
    }
    cout << tsize << endl;
}


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

using namespace std;

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

int main() {
  int n, a;
  cin >> n;

  vector<int> b(n, 0);

  rep(i, n - 1) {
    cin >> a;
    b[a - 1] += 1;
  }

  rep(i, n) {
    cout << b[i] << endl;
  }
}


Measured memory_bytes: 4000
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int a, b;
    cin >> a >> b;
    if(a <= 5){
        puts("0");
    }else if(a <= 12){
        cout << b/2 << "\n";
    }else {
        cout << b << "\n";
    }
    return 0;
}

Measured memory_bytes: 256