akhauriyash/Code-Regression huggingface dataset:
#include <iostream>                                                                           
using namespace std;                                                                          
int main(){                                                                                   
  int h,w;                                                                                    
  cin >> h >> w;                                                                              
  while(w!=0 or h!=0){                                                                        
    for(int i=0; i<h; i++){                                                                   
      for(int j=0; j<w; j++){                                                                 
        cout<<"#";                                                                            
      }                                                                                       
      cout << endl;                                                                           
    }                                                                                         
    cout << endl;                                                                             
    cin >> h >> w;                                                                            
  }                                                                                           
}                                                                                             
                                                                                              
      


Measured memory_bytes: 3084
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long int;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 373;

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

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> init_vector2(size_t n0, size_t n1, T e = T()) {
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> init_vector3(size_t n0, size_t n1, size_t n2, T e = T()) {
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

ll subr(ll q, ll h, ll s, ll n) {
    ll ans = n * q;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= n; j++) {
            for (int k = 0; k <= n; k++) {
                int v = i + 2 * j + 4 * k;
                if (v == n) {
                    ans = min(ans, i * q + j * h + k * s);
                }
            }
        }
    }
    return ans;
}

int main() {
    ll q, h, s, d;
    ll n;
    cin >> q >> h >> s >> d;
    cin >> n;

    n *= 4;

    ll ans = q * n;
    ans = min(ans, h * (n / 2) + subr(q, h, s, n % 2));
    ans = min(ans, s * (n / 4) + subr(q, h, s, n % 4));
    ans = min(ans, d * (n / 8) + subr(q, h, s, n % 8));
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 888
// template
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<long long, long long> LP;
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define REP_AB(i, a, b) for (ll i = ll(a); i <= ll(b); ++i)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define UNIQUE(v) sort(ALL(v));v.erase(unique(ALL(v)), v.end());
#define SIZE(x) ((ll)(x).size())
#define REVERSE(v) reverse(ALL(v))
// true / false
#define BIN_SEARCH(v, a) binary_search(ALL(v), a)
// index
#define BIN_LEFT(v, a) (lower_bound(ALL(v), a) - v.begin())
#define BIN_RIGHT(v, a) (upper_bound(ALL(v), a) - v.begin())
#define BIN_INSERT(v, a) (v.insert(v.begin() + BIN_LEFT(v, a), a))
#define DEL(v, i) v.erase(v.begin() + i)
#define INSERT(v, i, a) v.insert(v.begin() + i, a)
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 dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
#define out cout
#define in cin
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
// template end
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll a, b;
    in >> a >> b;
 
    ll k = (a+b) / 2;
    if(abs(a - k) == abs(b - k)){
        out << k << endl;
    }else{
        out << "IMPOSSIBLE" << endl;
    }
}

Measured memory_bytes: 256
#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

using namespace std;

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

//const double PI = 3.1415926535897932384626433832795;

void show(map<LL, LL> mp) {
  for(auto itr = mp.begin(); itr != mp.end(); itr++) {
    if(itr->second != 0) {
      printf("%lld: %lld, ", itr->first, itr->second);
    }
  }
  printf("\n");
}

int main() {
  int n;
  scanf("%d", &n);
  //cin >> n;
  map<LL, LL> mp;

  int t;
  REP(i, n) {
    scanf("%d", &t);
    //cin >> t;
    mp[t] += 1;
  }
  /*
  LL total = 0;
  REP(i, 10) {
    total += i * mp[i];
  }
  */
  LL total = 0;
  //show(mp);
  for(auto itr = mp.begin(); itr != mp.end(); itr++) {
    total += itr->first * itr->second;
  }
  //cout << "total:" << total << endl;
  int q;
  scanf("%d", &q);
  int b, c;
  REP(i, q) {
    scanf("%d %d", &b, &c);
    mp[c] += mp[b];
    total -= b * mp[b];
    total += c * mp[b];
    mp.erase(b);
    //show(mp);
    //LL total = 0;
    //for(auto itr = mp.begin(); itr != mp.end(); itr++) {
    //  total += itr->first * itr->second;
    //}
    printf("%lld\n", total);
    //cout << total << endl;
  }
}


Measured memory_bytes: 7640
#include <iostream>
using namespace std;
int main() {
	int N, M, K;
	cin >> N >> M >> K;
	bool ok = false;
	for (int i = 0; i <= N; ++i) {
		for (int j = 0; j <= M; ++j) {
			if (i * j + (N - i) * (M - j) == K) {
				ok = true;
			}
		}
	}
	cout << (ok ? "Yes" : "No") << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
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; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T> inline istream& operator>>(istream& input, vector<T>& v) { for (auto& elemnt : v)input >> elemnt; return input; }

int main() {
    init();

    int n, m; cin >> n >> m;
    VI k(m);
    vector<VI> s(m);
    FOR(i, 0, m) {
        cin >> k[i];
        s[i].resize(k[i]);
        FOR(j, 0, k[i]) {
            cin >> s[i][j], s[i][j]--;
        }
    }
    VI p(m); cin >> p;

    int ans = 0;
    FOR(i, 0, 1 << n) {
        vector<bool> on(n);
        FOR(j, 0, n) {
            if (i & (1 << j))on[j] = true;
        }
        bool all_light = true;
        FOR(j, 0, m) {
            int cnt = 0;
            FOR(l, 0, k[j]) {
                if (on[s[j][l]])cnt++;
            }
            if (cnt % 2 != p[j])all_light = false;
        }
        if (all_light)ans++;
    }
    cout << ans << endl;
}


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

using namespace std;
using ll = long long;

#define INF (1LL << 60)

#define REP(i,m,n) for(ll (i)=(m),i##_len=(n);(i)<(i##_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define _PR(x) cout << (x) << endl
#define _PS(x) cout << (x) << " "
#define SZ(x) ((int)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define pb push_back

int main()
{
    ll H, W;
    cin >> H >> W;

    if (H == 1 || W == 1) _PR(1); 
    else if (H % 2 == 0) _PR(W*(H/2));
    else if (W % 2 == 0) _PR((W*(H-1)/2+W/2));
    else _PR(W*(H-1)/2+(W+1)/2);

    return 0;
}

/*



*/

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
 
int main(){
    int k;
    char s[102];
 
    scanf("%d", &k);
    scanf("%s", s);
    
    if (strlen(s) <= k) {
        printf("%s", s);
    }
    else{
        for (int i=0; i < k; i++) {
            printf("%c", s[i]);
        }
        printf("...");
    }
 
    return 0;
}

Measured memory_bytes: 1732
N,W = map(int, input().split())
dp = [0]*(W+1)

for i in range(1,N+1):
    v,w = map(int,input().split())
    for j in range(W, w-1, -1):
        x = v+dp[j-w]
        if x > dp[j]: dp[j] = x

print(dp[W])

Measured memory_bytes: 5920
//atcoder clange
#include <stdio.h>
#include <stdlib.h>



int main(void){
  
   	long A,B;
      scanf("%ld", &A);//数値の取得
         scanf("%ld", &B);//数値の取得
  long a =0;
   long  K=0;
    long b = 0;
  long max = 0;
  if(A >= B){
    max = A;
  }else{
	max = B;
  }

for(K=0;K<max;K++){
  a = A-K;
 // printf("a:%d",a);
	b = B-K;
  if(abs(a)== abs(b)){
  printf("%ld",K);
    return 0;
  }
}
  printf("IMPOSSIBLE");
  
  
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF  numeric_limits<int_fast64_t>::max();
#define intf int_fast64_t
int main(){
    intf ans=INF;
    intf tm;
    string s,t;
    cin >>s >>t;
    for(int i=0;i<s.size()-t.size()+1;i++){
        tm=0;
        for(int m=i;m<i+t.size();m++){
            if(s[m]!=t[m-i]){
                tm++;
            }
        }
        if(ans>tm){
            ans = tm;
        }
    }
    cout << ans <<endl;
}

Measured memory_bytes: 3596
import java.util.Scanner;
public class Main {
	public static void main(String[] args){
	      Scanner sc = new Scanner(System.in);
	      int A = sc.nextInt();
	      int B = sc.nextInt();
	      int c = 0;
	      int x, y, n, p;
	      for(int i = A; i <= B; i++) {
	      x = i / 10000;
	      y = i % 10;
	      n = i / 1000 - x * 10;
	      p = (i % 100 -y) / 10;
	      if(x == y && n == p) {
	    	  c++;
	      }
	      }
	      System.out.println(c);
	}
}

Measured memory_bytes: 22996
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
#define Mod 1000000007
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
#define INF 1ll<<59


int main(){
  ll i,j;
  ll q;
  cin>>q;
  ll a,b;
  while(q--){
    cin>>a>>b;
    if(a>b) swap(a,b);
    if(a==b||a==b-1) {
      cout<<2*a-2<<endl;
      continue;
    }
    ll c=floor(sqrt(a*b));
    if(c*c>=a*b) c--;

    if(c*(c+1)>=a*b) {
      cout<<2*c-2<<endl;
    }
    else cout<<2*c-1<<endl;
  }
  

  return 0;
}

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

int main()
{
	string n;
	cin >> n;
	int chk = 0;
	for(int i = 0; i < (int)n.size(); ++i){
		chk += (n[i] - '0');
		chk %= 9;
	}
	cout << (chk % 9 == 0 ? "Yes" : "No") << endl;
	return 0;
}


Measured memory_bytes: 3716
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2005,P=924844033,M=4005;
int n,k,f[M][N],l[N],a[N],col[M],cnt,pos,fac[N],ans;
int main(){
	scanf("%d%d",&n,&k);fac[0]=1;
	for(int i=1;i<=n;i++)l[i%k]++,fac[i]=(ll)fac[i-1]*i%P;
	for(int i=0;i<k;i++)a[l[i]]+=2;
	for(int i=1;i<=n;i++)while(a[i]){
		++cnt;for(int j=1;j<=i;j++)col[++pos]=cnt;a[i]--;
	}
	f[0][0]=1;
	for(int i=1;i<=n*2;i++){
		int*a=f[i],*b=f[i-1];memcpy(a,b,(n+1)<<2);
		if(col[i]==col[i-1]){int*c=f[i-2];for(int j=1;j<=n;j++)(a[j]+=c[j-1])%=P;}
	}
	int*num=f[n*2];
	for(int i=0;i<=n;i++)ans=(ans+(ll)num[i]*fac[n-i]*((i&1)?-1:1))%P;
	printf("%d\n",(ans+P)%P);
	return 0;
}

Measured memory_bytes: 31616
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(),(obj).end()
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;}
const ll INF = 1LL << 56;

//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}

int main(){
    ll n;
    cin >> n;
    ll a[n];
    for (ll i=0;i<n;i++){
        cin >> a[i];
    }
    ll ans=0,x=1;
    for (ll i=0;i<61;i++){
        ll even=0,odd=0;
        for (ll j=0;j<n;j++){
            if (a[j]&(1LL<<i))odd++;
            else even++;
        }
        ll p=(((odd%MOD)*(even%MOD))%MOD)*x;
        ans+=p;
        ans%=MOD;
        x*=2;
        x%=MOD;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 5940
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cmath>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main() {
  int N;
  cin >> N;
  vector<vector<int>> to(N);
  rep(i, N) rep(j, N) {
    char c;
    cin >> c;
    if (c == '1')
      to[i].push_back(j);
  }
  int ans = 0;
  rep(i, N) {
    vector<int> memo(N);
    queue<tuple<int, int, int>> que;
    que.push(make_tuple(i, -1, 1));
    memo[i] = 1;
    while(!que.empty()) {
      int now, prev, cost;
      tie(now, prev, cost) = que.front();
      que.pop();
      chmax(ans, cost);
      rep(j, to[now].size()) {
        int next = to[now][j];
        if (next == prev)
          continue;
        if (memo[next]) {
          if (memo[next] == cost) {
            cout << -1 << endl;
            return 0;
          }
        } else {
          memo[next] = cost+1;
          que.push(make_tuple(next, now, cost+1));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 512
#include <stdio.h>

int main(void)
{
	int w[31] ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
				11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
				21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
				
	int i,x,y,a,b,tmp;
	
	scanf("%d",&x);
	scanf("%d",&y);
	
	for(i = 1;i <= y;i++){
		scanf("%d,%d",&a,&b);
	
		tmp = w[a];
		w[a] = w[b];
		w[b] = tmp;
	}
	for(i = 1;i <= x;i++){
		printf("%d\n",w[i]);
	}
	
	return 0;
}

Measured memory_bytes: 596
//C言語では厳しいです。
#include<stdio.h>
int main(void){
  int n,a[3][3],b[10];
  bool appear[3][3];
  for(int loop=0;loop<3;loop++){
    for(int loop2=0;loop2<3;loop2++){
      scanf("%d",&a[loop][loop2]);
    }
  }
  scanf("%d",&n);
  for(int loop=0;loop<n;loop++){
    scanf("%d",&b[loop]);
  }
  for(int loop=0;loop<n;loop++){
    for(int loop2=0;loop2<n;loop2++){
      appear[loop][loop2]=false;
      for(int loop3=0;loop3<n;loop3++){
        if(a[loop][loop2]==b[loop3]){
          appear[loop][loop2]=true;
        }
      }
    }
  }
  char *ans="No";
  for(int loop=0;loop<3;loop++){
    if(appear[loop][0] && appear[loop][1] && appear[loop][2]){
      ans="Yes";
    }
  }
  for(int loop=0;loop<3;loop++){
    if(appear[0][loop] && appear[1][loop] && appear[2][loop]){
      ans="Yes";
    }
  }
  if(appear[0][0] && appear[1][1] && appear[2][2]){
    ans="Yes";
  }
  if(appear[0][2] && appear[1][1] && appear[2][0]){
    ans="Yes";
  }
  printf("%s\n",ans);
  return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
int main()
{
	int r;
	scanf("%d",&r);
	if(4208>=r&&2800<=r){
		printf("AGC\n");
	}
	if(2800>r&&1200<=r){
		printf("ARC\n");
	}
	if(1200>r){
		printf("ABC\n");
	}
	return 0;
}


Measured memory_bytes: 128
N, M = [int(s) for s in input().split(' ')]
count = 0
ABs = []
for n in range(N):
    ab =  [int(s) for s in input().split(' ')]
    ABs.append(ab)
ABs.sort(key=lambda x: x[0])
m = M
yen = 0
for a, b in ABs:
    drinks = min(m, b)
    yen += drinks * a
    m -= drinks
    if m == 0:
        break
print(yen)


Measured memory_bytes: 22756
{-# LANGUAGE BangPatterns #-}
import           Control.Exception           (assert)
import           Control.Monad
import           Control.Monad.Primitive
import qualified Control.Monad.ST            as ST
import qualified Data.Array.IO               as IO
import qualified Data.Array.ST               as ST
import qualified Data.Array.Unboxed          as A
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Foldable
import           Data.List
import qualified Data.Map.Strict             as Map
import           Data.Maybe
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as Set
import qualified Data.Vector                 as VB
import qualified Data.Vector.Mutable         as VBM
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM
import           Debug.Trace

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntVec n = V.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

readInteger = fst . fromJust . BS.readInteger
readIntegerList = map readInteger . BS.words
getInteger = readInteger <$> BS.getLine
getIntegerList = readIntegerList <$> BS.getLine

inf :: Int
inf = 10^18

main = do
  s <- getLine

  let ans | s == "RRR" = 3
          | s == "RRS" = 2
          | s == "SRR" = 2
          | s == "RSR" = 1
          | s == "RSS" = 1
          | s == "SRS" = 1
          | s == "SSR" = 1
          | s == "SSS" = 0

  print ans


Measured memory_bytes: 3720
N = int(input())
S = []
for i in range(N):
    S.append(list(input()))
queue = []
len = len(S[0])

for j in range(len):
    flag = [0 for m in range(N)]
    for k in range(1,N):
        if S[0][j] in S[k]:
            flag[k] = 1
            S[k].remove(S[0][j])
            
    if sum(flag) == N-1:
        queue.append(S[0][j])

queue = sorted(queue)
res = "".join(queue)
        
print(res)


Measured memory_bytes: 3064
h, w = map(int,input().split())
s = [input() for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            check = 0
            for x, y in ([1,0],[0,1],[0,-1],[-1,0]):
                new_h, new_w = i + x, j + y
                #print(new_h,new_w)
                if new_h < 0 or new_h >= h or new_w < 0 or new_w >= w:
                    continue
                if s[new_h][new_w] == '.':
                    check += 1
            if check == 4:
                print('No')
                quit()
print('Yes')


Measured memory_bytes: 3064
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 


int main()
{
    const LL mod = 1000000007;

    int N;
    cin >> N;

    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    sort(A.begin(), A.end());

    int offset = 0;
    bool ok = true;
    LL ans = 1;
    if (N%2) {
        if (A[0] == 0) {
            for (int i = 0; ok && i < N/2; ++i) {
                if (A[2*i+1] == 2 * (i+1) && A[2*i + 2] == 2 * (i+1)) {
                    ans = (ans * 2) % mod;
                } else {
                    ok = false;
                }
            }
        } else {
            ok = false;
        }
    } else {
        for (int i = 0; ok && i < N/2; ++i) {
            if (A[2*i] == 2 * i + 1 && A[2*i + 1] == 2 * i + 1) {
                ans = (ans * 2) % mod;
            } else {
                ok = false;
            }
        }
    }

    if (!ok) {
        ans = 0;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 640
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
C=[int(i) for i in input().split()]

ans=0
for i in range(len(B)):
    ans+=B[i]

for i in range(len(A)-1):
    if A[i]+1==A[i+1]:
        p=A[i]-1
        ans+=C[p]

print(ans)

Measured memory_bytes: 3064
//
// Created by Yuta Sato on 3/15/20.
//

#include <iostream>

void Range(int A, int B, int C){
    if( A < B && B < C){
        std::cout << "Yes" << std::endl;
    }
    else{
        std::cout << "No" << std::endl;
    }
}

int main(){
    // ---------------------------
    // ITP_1_2_A: Small, Large, or Equal
//    int a, b;
//    std::cin >> a >> b;
//    BigOrSmall(a,b);
    // ---------------------------

    // ---------------------------
    // ITP_1_2_B: Range
    int a, b, c;
    std::cin >> a >> b >> c;
    Range(a,b,c);
    // ---------------------------

    // ---------------------------
    // ITP_1_2_C:
    // ---------------------------

    // ---------------------------
    // ITP_1_2_B:
    // ---------------------------


    return 0;
}




Measured memory_bytes: 3096
use proconio::input;
use proconio::marker::{Chars, Usize1};
use std::iter::Iterator;
use std::collections::HashSet;
use std::cmp::*;
use itertools::Itertools;

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }
  
  #[allow(unused_macros)]
  macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn main() {
    input! {
    n: usize,
    mut a: [usize; n],
    }

    let max = a.iter().max().unwrap();
    let mut secmax = 0;

    for i in 0..n {
      if a[i] == *max {
        if secmax != 0 {
          println!("{}", secmax);
        }else{
          let mut c = a.clone();
          c.remove(i);
          secmax = *(c.iter().max().unwrap());
          println!("{}", secmax);
        }

       
      }else {
        println!("{}", max);
      }
    }
}


Measured memory_bytes: 6412
#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<utility>
#include<functional>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;

LL N,M,S;
string s;
int main(){
    cin>>s;
    s[3]='8';
    cout<<s<<endl;

    return 0;
}


Measured memory_bytes: 256
N = gets.to_i

a_list = gets.strip.split(/ /).collect{|x| x.to_i}


sum = 0
s_list = []
a_list.each{|a|
	sum += a
	s_list << sum
}
#p a_list
#p s_list

#p l_table
#p r_table

minimum = sum

(1..(N - 3)).each{|d2|
#	p d2
	
	l_sum = s_list[d2]
	r_sum = sum - l_sum
	
#	p [l_sum, r_sum]
	
	l_sum2 = l_sum / 2
	r_sum2 = r_sum / 2
	
	d1 = (0...d2).bsearch{|i|
		s_list[i] > l_sum2
	}
	if d1 == nil
		d1 = d2 - 1
	elsif d1 > 0 && (s_list[d1 - 1] * 2 - l_sum).abs < (s_list[d1] * 2 - l_sum).abs
		d1 -= 1
	end
	
	d3 = ((d2 + 1)...(N - 1)).bsearch{|i|
		s_list[i] - l_sum > r_sum2
	}
	if d3 == nil
		d3 = N - 2
	elsif d3 > 0 && ((s_list[d3 - 1] - l_sum) * 2 - r_sum).abs < ((s_list[d3] - l_sum) * 2 - r_sum).abs
		d3 -= 1
	end
	
#	p [d1, d2, d3]
	
	s1 = s_list[d1]
	s4 = sum - s_list[d3]
	pqrs = [s1, l_sum - s1, r_sum - s4, s4]
	
#	p pqrs
	
	diff = pqrs.max - pqrs.min
	if diff < minimum
		minimum = diff
	end
	
}

puts minimum






Measured memory_bytes: 24164
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>
#include<set>
#include<deque>
#include<queue>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;
const Int MOD = 1000000000 + 7;

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int a, b, c;
	cin >> a >> b >> c;

	if (a+b+c>=22) {
		cout << "bust" << "\n";
	}
	else {
		cout << "win" << "\n";
	}
}


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

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

using namespace std;
typedef long long ll;

int main(int argc, const char * argv[]) {
    
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int a, b, c;
    cin >> a >> b >> c;
    
    cout << min(c, b/a) << endl;
    
    return 0;
}

Measured memory_bytes: 256
import java.io.*;
import java.util.*;
class Main{
	static final long MOD = 1_000_000_007; // 10^9+7
	static final int MAX = 2_147_483_646; // intMax 
	static final int INF = 1_000_000_000; // 10^9  
	public static void main(String[] args) throws Exception{
		hayami saori = new hayami();
		int n = saori.saori_hayami();
		String[]s = saori.nextHayami().split("");
		int k = saori.saori_hayami();
		String str = s[k-1];
		for(int i = 0;i < s.length;i++){
			if(!s[i].equals(str)){
				s[i] = "*";
			}
		}
		System.out.println(String.join("", s));
		saori.close();
	}
}

class hayami implements Closeable {
	private final InputStream in = System.in;
	private final byte[] hayami = new byte[1024];
	private int Hayami = 0;
	private int saori = 0;
	private boolean HayamiSaori() {
		if (Hayami < saori) {
			return true;
		}else{
			Hayami = 0;
			try {
				saori = in.read(hayami);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (saori <= 0) {
				return false;
			}
		}
		return true;
	}
	private int SaoriHayami() { 
		if (HayamiSaori()) {
            return hayami[Hayami++];
         }else{
             return -1;
         }
	}
	private static boolean hayami_saori(int hayami) { 
		return 33 <= hayami && hayami <= 126;
	}
	public boolean hayamisaori() { 
		while(HayamiSaori() && !hayami_saori(hayami[Hayami])) Hayami++; return HayamiSaori();
	}
	public String nextHayami() {
		if (!hayamisaori()) throw new NoSuchElementException();
		StringBuilder hayamin = new StringBuilder();
		int saori = SaoriHayami();
		while(hayami_saori(saori)) {
			hayamin.appendCodePoint(saori);
			saori = SaoriHayami();
		}
		return hayamin.toString();
	}
	public long saorihayami() {//nextLong
		if (!hayamisaori()) throw new NoSuchElementException();
		long hayami = 0;
		boolean misao = false;
		int saori = SaoriHayami();
		if (saori == '-') {
			misao = true;
			saori = SaoriHayami();
		}
		if (saori < '0' || '9' < saori) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= saori && saori <= '9') {
				hayami *= 10;
				hayami += saori - '0';
			}else if(saori == -1 || !hayami_saori(saori)){
				return misao ? -hayami : hayami;
			}else{
				throw new NumberFormatException();
			}
			saori = SaoriHayami();
		}
	}
	public int saori_hayami() {//nextInt
		long hayami = saorihayami();
		if (hayami < Integer.MIN_VALUE || hayami > Integer.MAX_VALUE) throw new NumberFormatException();
		return (int) hayami;
	}
	public double Hayamin() { //nextDouble
		return Double.parseDouble(nextHayami());
	}
	public void close() {
		try {
			in.close();
		} catch (IOException e) {
		}
    }
    
}


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

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2>
inline bool chmax(T1 &a, const T2 &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2>
inline bool chmin(T1 &a, const T2 &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll mod = 1000000007;

int main() {
    ll R;
    cin >> R;
    cout << 2 * M_PI * R << endl;
}

Measured memory_bytes: 3760
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <functional>
#include <queue>

using namespace std;
typedef long long ll;

int main()
{
	int N;
	cin >> N;
	
	ll sum = 0;
	for(int i = 1; i < N+1; i++){
		if(i % 3 != 0 && i % 5 != 0){
			sum += i;
		}
	}
	
	cout << sum << endl;
	
	return 0;
}


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

using namespace std;

const int MAX_N = 2e5 + 10;

int num1[MAX_N], num2[MAX_N];
vector<int> g1[MAX_N], g2[MAX_N];

void dfs(int g, int v, int num[], int k)
{
	num[v] = k;

	if (g == 1)
	{
		for (int u: g1[v])
		{
			if (num[u] == 0)
				dfs(g, u, num, k);
		}//for u
	}//if
	else
	{
		for (int u: g2[v])
		{
			if (num[u] == 0)
				dfs(g, u, num, k);
		}//for u
	}//else

	return;
}//dfs

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int N, K, L, p, q, r, s;

	cin >> N >> K >> L;
	for (int i = 0; i < K; i++)
	{
		cin >> p >> q;
		g1[p].emplace_back(q);
		g1[q].emplace_back(p);
	}//for i

	for (int i = 0; i < L; i++)
	{
		cin >> r >> s;
		g2[r].emplace_back(s);
		g2[s].emplace_back(r);
	}//for i

	int cnt1 = 0, cnt2 = 0;
	for (int i = 1; i <= N; i++)
	{
		if (num1[i] == 0) dfs(1, i, num1, ++cnt1);

		if (num2[i] == 0) dfs(2, i, num2, ++cnt2);
	}//for i

	map<pair<int, int>, int> mp;
	for (int i = 1; i <= N; i++)
	{
		mp[{num1[i], num2[i]}]++;
	}//for i

	for (int i = 1; i <= N; i++)
		cout << mp[{num1[i], num2[i]}] << " \n"[i == N];

	return 0;
}//main


Measured memory_bytes: 27904
#include<iostream>
using namespace std;
int main()
{
  int a;
  cin>>a;
  int b=a%10;
  a/=10;
  int c=a%10;
  a/=10;
  int d=a;
  if(b==7||c==7||d==7)
    cout<<"Yes";
  else
    cout<<"No";
  return 0;
}

Measured memory_bytes: 3588
n,k=map(int,input().split())
p=list(map(int,input().split()))
num=[sum(p[0:k])]
def ap(num1):
    num.append(num1)
for i in range(n-k):
    ap(num[i]-p[i]+p[k+i])
print((max(num)+k)/2)


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

int tree[262144];

int dfs(int a, int b, int k, int l, int r){
  if(r<=a || b<=l)return 0;
  if(a<=l && r<=b)return tree[k];
  int m=(l+r)/2;
  return dfs(a,b,k*2+1,l,m)+dfs(a,b,k*2+2,m,r);
}

int getsum(int a,int b){
  return dfs(a,b,0,0,131072);
}

void update(int i,int x){
  i += (131072-1);
  tree[i] +=x;
  while(i){
    i=(i-1)/2;
    tree[i] += x;
  }
}

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

  for(int i=0;i<q;i++){
    cin>>a>>b>>c;
    if(a==1)cout<<getsum(b,c+1)<<endl;
    else update(b,c);
  }
  return 0;
}

Measured memory_bytes: 1964
#include<iostream>
#include<vector>
#include<sstream>
#include<string>
#include<numeric>
#include <algorithm>
#include<math.h>
#include<cstdio>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>



using namespace std;
typedef long long ll;
int gcd( int m, int n )
{
	// 引数に０がある場合は０を返す
	if ( ( 0 == m ) || ( 0 == n ) )
		return 0;
	
	// ユークリッドの方法
	while( m != n )
	{
		if ( m > n ) m = m - n;
		else         n = n - m;
	}
	return m;
}//gcd
int lcm( int m, int n )
{
	// 引数に０がある場合は０を返す
	if ( ( 0 == m ) || ( 0 == n ) )
		return 0;
	
	return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}//lcm
int input(){
  int x;
  cin>>x;
  return x;
}
int abs(int x){
  if(x>=0){
    return x;
  }else{
    return -x;
  }
}

int moji(char in)
{
    int ans = (int)in-(int)'a'+1;
    if((in < 1) || (in > 26)){
        ans = 100;
    }
    return ans;
}
const int VV=10;//場合に応じてVVの値のみ変更する必要あり
//dijkstra(s)sがスタート地点でそこからの最短距離を配列dで表す。正の重みのみ使用可能
int cost[VV][VV];
int d[VV];
bool used[VV];
void dijkstra(int s){
  fill(d,d+VV,100000);
  fill(used,used+VV,false);
  d[s]=0;
  while(true){
    int v=-1;
    for(int u=0;u<VV;u++){
      if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
    }
    if(v==-1)break;
    used[v]=true;
    for(int u=0;u<VV;u++){
      d[u]=min(d[u],d[v]+cost[v][u]);
    }
  }

}

int compare_int(const void *a, const void *b)//qsort(quick sort利用時に使用)
{
    return *(int*)a - *(int*)b;
}





int main(){
  int A,B;
  cin>>A>>B;
  int sum=0;
  for(int i=A;i<=B;i++){
    int x=i%10;
    int y=(i/10)%10;
    int z=(i/1000)%10;
    int r=(i/10000)%10;
    if(x==r&&y==z){
      sum++;
    }
    

  }
  cout<<sum<<endl;
  
}


  

Measured memory_bytes: 256
/* ABC145-B
   Takatan */

#include<stdio.h>
#include<stdlib.h>
int main(void){
    int length, i;
    char string[101];

    scanf( "%d" , &length);
    scanf( "%s" , string);

    if(length%2==1){
        printf( "No\n" );
        return 0;
    }else{
        for(i=0; i < length/2; i++){
            if(string[i]!=string[length/2+i]){
                printf( "No\n" );
                return 0;
            }
        }
    }
    printf( "Yes\n" );
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
using P = pair<int,int>;

int main()
{
  ll N;
  cin >> N;
  ll sum = 0;
  vector<ll> A(N);
  REP(i, N){
    cin >> A[i];
    sum+=A[i];
  }
  sum/=2;

  ll x=0;
  for(int i = 0;i<N-1;i+=2){
    x+=A[i+1];
  }
  vector<ll> B(N);
  B[0]=(sum-x)*2;
  FOR(i,1,N){
    B[i]=(A[i-1]-(B[i-1]/2))*2;
  }

  REP(i,N){
    cout << B[i] << endl;
  }
}

Measured memory_bytes: 2944
#include<stdio.h>
int main(){
  int a[9],N,b,i,j;
  
  for(i=0;i<9;i++){
    scanf("%d",&a[i]);}
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&b);
    for(j=0;j<9;j++){
      if(a[j]==b)
        a[j]=-1;
    }}
  if(a[0]==-1 && a[1]==-1 && a[2]==-1)
    printf("Yes\n");
  else if(a[3]==-1 && a[4]==-1 && a[5]==-1)
    printf("Yes\n");
  else if(a[6]==-1 && a[7]==-1 && a[8]==-1)
    printf("Yes\n");
  else if(a[0]==-1 && a[3]==-1 && a[6]==-1)
    printf("Yes\n");
  else if(a[1]==-1 && a[4]==-1 && a[7]==-1)
    printf("Yes\n");
  else if(a[2]==-1 && a[5]==-1 && a[8]==-1)
    printf("Yes\n");
  else if(a[0]==-1 && a[4]==-1 && a[8]==-1)
    printf("Yes\n");
  else if(a[2]==-1 && a[4]==-1 && a[6]==-1)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}

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

#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

using namespace std;

typedef long long ll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;

template<typename T>
ostream& operator << (ostream& os, const vector<T>& vec) {
    os << "[ ";
    REP(i, vec.size()) {
        os << vec[i] << " ";
    }
    os << "]";

    return os;
}

ll pow_mod(ll a, ll p) {
    ll res = 1;
    while(p) {
        if (p & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        p >>= 1;
    }

    return res;
}


ll mod_inv(ll a, ll m) {
    ll b = m, u = 0, v = 1;
    while(a) {
        ll t = b / a;
        swap(b -= t * a, a);
        swap(u -= t * v, v);
    }

    return (u % m + m) % m;
}

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


int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    int N, C;
    cin >> N >> C;

    int M = 2 * 1e5 + 5;
    int S[C][M];
    REP(c, C) REP(i, M) S[c][i] = 0;
    REP(i, N) {
        int s, t, c;
        cin >> s >> t >> c;
        --c;
        if (S[c][2 * s] == -1) {
            S[c][2 * s] = 0;
        } else {
            S[c][2 * s - 1] = 1;
        }

        if (S[c][2 * t - 1] == 1) {
            S[c][2 * t - 1] = 0;
        } else {
            S[c][2 * t] = -1;
        }

    }

    int ans = 0;
    int cnt = 0;
    REP(i, M) {
        REP(c, C) cnt += S[c][i];
        if (ans < cnt) ans = cnt;
    }
    
    cout << fixed << setprecision(20);
    cout << ans << endl;

}


Measured memory_bytes: 23680
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#define rep(index,num) for(int index=0;index<num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) {
  return b != 0 ? gcd(b, a % b) : a;
}
ll minkofAminuskBlargerthanC(ll A,ll B,ll C,ll kizami,ll kmin,ll kmax){//kizami=gcd(B,D)
	ll midk=(kmax+kmin)/2;
	//printf("%lld %lld %lld %lld %lld\n",kmin,kmax,midk,A-midk*kizami,A-midk*(kizami+1));
	if(A-midk*kizami>=C+1&&A-(midk+1)*kizami<C+1){
		return midk;
	}
	else if(A-midk*kizami>=C+1){
		return minkofAminuskBlargerthanC(A,B,C,kizami,midk,kmax);
	}
	else{
		return minkofAminuskBlargerthanC(A,B,C,kizami,kmin,midk);
	}
}
int main(){
	int T;
	ll A,B,C,D;
	scan(T);
	rep(i,T){
		scanf("%lld %lld %lld %lld",&A,&B,&C,&D);
		//prin("hoge1");
		if(A<B||D<B){
			printf("No\n");
		}
		else if(C+1<B){
			ll kizami=gcd(D,B);
			if(A-minkofAminuskBlargerthanC(A,B,C,kizami,0,A/kizami+1)*kizami<B){
			//if(1<0){
				printf("No\n");
			}
			else{
				printf("Yes\n");
			}
		}
		else{
			printf("Yes\n");
		}
	}

	return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,m,h,k;

int score[1003];
P bar[100005];

int memo[1003];
P swp[100005];
int memo2[1003];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true){
    cin >> n >> m >> h >> k;
    if(n == 0) break;

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

    for (int i = 0;i < m;i++){
        int a,b; cin >> a >> b;
        bar[i].second = a-1;
        bar[i].first = b;
    }

    sort(bar,bar+m);

    for (int i = 0;i < m;i++){
        swp[i] = P(min(memo[bar[i].second],memo[bar[i].second+1]),max(memo[bar[i].second],memo[bar[i].second+1]));
        swap(memo[bar[i].second],memo[bar[i].second+1]);
    }

    int sum = 0;
    for (int i = 0;i < n;i++){
        if(memo[i] < k) sum += score[i];
        memo2[memo[i]] = score[i];
    }

    int MIN = sum;

    for (int i = 0;i < m;i++){
        if(swp[i].first < k && swp[i].second >= k){
            int a = memo2[swp[i].first];
            int b = memo2[swp[i].second];
            MIN = min(MIN,sum-a+b);
        }
    }

    cout << MIN << endl;
    }

    return 0;

}


Measured memory_bytes: 4648
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define bug1( x ) {cerr << (#x) <<"="<< x << endl;}
#define bug2( x , y ) {cerr << (#x) <<"="<< (x) << "    " << (#y) << "="<< (y) << endl;}
#define bug3( x , y , z ) {cerr << (#x) <<"="<<(x) << "    " << (#y) <<"="<< (y) << "    " << (#z) <<"="<< (z) << endl;}
#define bug4( x , y , z , w) {cerr << (#x) <<"="<<(x) << "    " << (#y) <<"="<< (y) << "    " << (#z) <<"="<< (z) << "    " << (#w) <<"="<< w << endl;}
#define bug5( x , y , z , w ,p) {cerr << (#x) <<"="<<(x) << "    " << (#y) <<"="<< (y) << "    " << (#z) <<"="<< (z) << "    " << (#w) <<"="<< w << "    " << (#p) <<"="<< p << endl;}
#define bug6( x , y , z , w ,p , q) {cerr << (#x) <<"="<<(x) << "    " << (#y) <<"="<< (y) << "    " << (#z) <<"="<< (z) << "    " << (#w) <<"="<< w << "    " << (#p) <<"="<< p << "    " << (#q) <<"="<< q << endl;}
#define bugn( x , n ) {cerr << (#x) <<":";for(int i=0;i<n;i++)cerr << x[i] <<"  "; cerr << endl;}
#define bugnm( x , n , m ) {cerr << (#x)<<endl;for(int i=0;i<n;i++){cerr << "Row #" << i<< ":";for(int j=0;j<m;j++)cerr << x[i][j] << "   ";cerr << endl;}}
typedef long long ll;
typedef long double ld;
using namespace std;


const int maxn = 1e5+10, lgN = 20;
vector<vector<pair<int,int>>>g(maxn);
vector<int> tin(maxn);
vector<int> tout(maxn);
vector<vector<int>>par(maxn,vector<int>(lgN+1));
vector<vector<int>>dis(maxn,vector<int>(lgN+1));
int timer = 0, n;

void dfs(int u=1,int p=0,int w=0){
	tin[u]=++timer;
	par[u][0]=p;
	dis[u][0]=w;
	for(int i=1;i<=lgN;i++){
		par[u][i]=par[ par[u][i-1] ][i-1];
		dis[u][i]=dis[ par[u][i-1] ][i-1]+dis[u][i-1];
	}
	for(auto it:g[u]){
		int v=it.first;
		int w=it.second;
		if( v!= p){
			dfs(v,u,w);
		}
	}
	tout[u]=++timer;
}

int isAncestor(int u,int v){
	return tin[u]<=tin[v] && tout[v]<=tout[u];
}

int lca(int u,int v){
	if( isAncestor(u,v) )return u;
	if( isAncestor(v,u) )return v;
	for(int i=lgN;i>=0;i--){
		if( par[u][i] && !isAncestor(par[u][i],v)){
			u=par[u][i];
		}
	}
	return par[u][0];
}

int DisQuery(int u,int v){
	int l = lca(u,v);
	int d = 0;
	auto shiftup =[&](int u){
		if( u!=l ){
			for(int i=lgN;i>=0;i--){
				if( par[u][i] && !isAncestor(par[u][i],l)){
					d+=dis[u][i];
					u=par[u][i];
				}
			}
			d+=dis[u][0];
		}
	};
	shiftup(u);
	shiftup(v);
	return d;
}

int32_t main(){
	IOS
	cin>>n;
	for(int i=1;i<n;i++){
		int u,v,w;cin>>u>>v>>w;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	}
	dfs(1,0,0);
	int q,k;cin>>q>>k;
	for(int i=1;i<=q;i++){
		int u,v;cin>>u>>v;
		cout<<DisQuery(u,k)+DisQuery(v,k)<<endl;
	}
	

	
}
/*
 * long long or int?
 * index out of bound? 
 * Tested on own test case?corner?
 * Make more general solution.
 * Read Read Read Read ....
 */


Measured memory_bytes: 55412
N = int(input())
count =[[0 for x in range(10)] for z in range(10)]
for x in range(1,N+1):
    F = str(x)[0]
    E = str(x)[-1] 
    count[int(F)][int(E)] +=1
ans = 0
for x in range(10):
    for y in range(10):
        ans += count[x][y] *count[y][x]
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string S;
    cin >> S;
    cout << ( S[0] == 'a' || S[0] == 'i' || S[0] == 'u' || S[0] == 'e' || S[0] == 'o' ?
             "vowel" : "consonant" ) << endl;
    return 0;
}

Measured memory_bytes: 256
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N = int(input())
T = [int(input()) for _ in range(N)]
print(llcm(T))

Measured memory_bytes: 69452
import java.util.*;
 
public class Main {
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);		
		int N = in.nextInt();
        long mo[] = new long[N+1];
      
      for(int i = 0;i < N+1;i++){
      	mo[i] = in.nextInt(); 
      }
      
      long sum = 0;
      long yu = 0;
      
      for(int i =0;i < N;i++){
		yu = in.nextInt(); 
        
        if(yu>mo[i]){
        	sum += mo[i];
            yu  -= mo[i]; 
        	sum += Math.min(mo[i+1],yu);
            mo[i+1] = Math.max(0,mo[i+1]-yu); 
        }else{
          sum +=yu;
        }
      }
       System.out.print(sum); 
      }
	}

Measured memory_bytes: 64140
#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

int main(){
  ll N,ans=0,m=0;
  cin >> N;
  rep(i,N-1){
    ll u,v;
    cin >> u >> v;
    if(u>v) swap(u,v);
    m+=(u-0)*(N+1-v);
  }
  rep(i,N){
    ans+=(N-i)*(i+1);
  }
  ans-=m;
  cout << ans << endl;
}


Measured memory_bytes: 3596
N = int(input())
c = {}
for _ in range(N):
    s = ''.join(sorted(list(input())))
    if s in c.keys():
        c[s] += 1
    else:
        c[s] = 0
ans = 0
for k in c.values():
    ans += (k * (k + 1)) // 2
print(ans)


Measured memory_bytes: 17808
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  .      '                   Udit Gupta @luctivud         ,              
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                        ##     ##  #######  # #  ######
                        ##     ##  ##   ##  ###    ##
                        ##     ##  ##    #  # #    ##
                        #########  #######  # #    ##
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
import math
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))

def printwsp(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end="tst, ")

MODPRIME = int(1e9+7); BABYMOD = 998244353;
# ########################### HELPER PROGRAMS USED ###################################
def getangle(hour, minu):
    h = (hour * 360) / 12 + (minu * 360) / (12 * 60)
    m = (minu * 360) / (60);
    angle = abs(h-m)
    return min(angle, 360-angle)
# ############################# MAIN STARTS HERE #####################################
# for _testcases_ in range(int(input())):
a, b, h, m = get_ints()
angle = getangle(h, m)
coss = math.cos((angle/180)*math.pi)
ans = ((a**2)+(b**2)-(2*a*b*coss))
print(math.sqrt(ans))


# ####################################################################################
'''
THE LOGIC AND APPROACH IS BY ME @luctivud.
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
: DO NOT PLAGIARISE.
'''

Measured memory_bytes: 62688
#include <bits/stdc++.h>
using namespace std;
int main() {
    int k,s,cnt=0;
    cin >> k >> s;
    for(int i=0;i<=k;i++)
        for(int j=0;j<=k;j++){
            int l = s - i - j;
            if(0<=l && l<=k)
                cnt++;
        }
    cout << cnt << endl;
}


Measured memory_bytes: 256