akhauriyash/Code-Regression huggingface dataset:
n=int(input())
x=list(map(int,input().split()))
m = min(x)
M = max(x)
ans=10000000000
for i in range(m,M+1):
    sum=0
    for j in x:
        sum+=(j-i)**2
    if sum < ans:
        ans = sum
print(ans)


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

int main() {
	int Q, H, S, D, N;
	cin >> Q >> H >> S >> D >> N;
	int64_t ans = 0;
	while (N > 0) {
		if (N == 1) {
			if (S <= 2 * H && S <= 4 * Q) {
				ans += S;
			}
			else if (H <= 2 * Q) {
				ans += (int64_t)2 * H;
			}
			else {
				ans += (int64_t)4 * Q;
			}
			N -= 1;
		}
		else {
			if (D <= 2 * S && D <= 4 * H && D <= 8 * Q) {
				ans += (int64_t)N / 2 * D;
				N %= 2;
			}
			else if (S <= 2 * H && S <= 4 * Q) {
				ans += (int64_t)N * S;
				N = 0;
			}
			else if (H <= 2 * Q) {
				ans += (int64_t)2 * N * H;
				N = 0;
			}
			else {
				ans += (int64_t)4 * N * Q;
				N = 0;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<sstream> 
#include<unordered_map>      

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
typedef vector<int> vi;
typedef vector<ll> vl;

const int inf = numeric_limits<int>::max() >> 2;
const ll linf = numeric_limits<ll>::max() >> 2;
const ull ulinf = numeric_limits<ull>::max() >> 2;
const double pi = acos(-1);
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const int dx8[8]={-1,0,1,-1,1,-1,0,1};
const int dy8[8]={-1,-1,-1,0,0,1,1,1};

#define p_queue(i) priority_queue<i> 
#define rp_queue(i) priority_queue<i, vector<i>, greater<i>> 
#define umap(i,j) unordered_map<i,j>
#define P(p) cout<<(p)<<endl
#define PS(p) cout<<(p)<< " "
#define IN cin >> 
#define rep(i,m,n) for(ll i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i = (m-1); i >= (int)(n); i--)
#define inrep(n,a) for(int i = 0; i < (int)(n); i++)cin >> a[i];
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define rsort(a,b,c) sort(a, b, greater<c>())
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v,c) sort(v.begin(), v.end(), greater<c>()) 
#define ft first
#define sd second
#define pb push_back
#define it insert
#define sz(x) ((int)(x).size())
#define lb(a,n,k) (lower_bound(a,a+n,k) - a) 
#define vlb(a,k)  (lower_bound(a.begin(),a.end(),k) - a.begin())
#define ub(a,n,k) (upper_bound(a,a+n,k) - a) 
#define vub(a,k)  (upper_bound(a.begin(),a.end(),k) - a.begin())
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}
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; }

vector<string> split(const string &str, char sep){
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while( getline(ss, buffer, sep) ) {
        v.push_back(buffer);
    }
    return v;
}

//ll ncr[100][100];
//ll nCr(int n, int r){if(n==r) ret ncr[n][r] = 1; if(r==0) ret ncr[n][r] = 1; if(r==1) ret ncr[n][r] = n;if(ncr[n][r]) ret ncr[n][r]; ret ncr[n][r] = nCr(n-1,r) + nCr(n-1,r-1);}

//ll npr[100][100]={};
//ll nPr(int n,int r){if(npr[n][r])ret npr[n][r];if(r==0)ret npr[n][r] = 1;if(r==1)ret npr[n][r] = n;ret npr[n][r] = n * nPr(n-1,r-1);}

//ll nHr(int n,int r){ret nCr(n+r-1,r);}

///////////////////////////////////////////////////////////////////////////


int main(){

    ll l,r;
    cin >> l >> r;

    ll ans = linf;

    int ci = 0;
    rep(i,l,r){
	int cj = 0;
	rep(j,i+1,r+1){
	    ans = min(ans , (i*j)%2019);
	    cj++;
	    if(cj > 2020)break;
	}
	ci++;
	if(ci > 2020)break;
    }
    P(ans);
    return  0;
}


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

const int N = 2e3, M = N * N;
const int Mo = 1e9 + 7;

int n, m;
int f[N + 10][N + 10], fac[M + 10], Inv[M + 10];

inline int ksm(int a, int b) {
    int ans = 1;
    for (; b; b >>= 1, a = 1LL * a * a % Mo)
        if (b & 1) ans = 1LL * ans * a % Mo;
    return ans;
}

inline int C(int n, int m) {
    if (m < n)
        return 0;
    // printf("%d %d %d %d\n", n, fac[m], Inv[n], Inv[m - n]);
    return 1LL * fac[m] * Inv[n] % Mo * Inv[m - n] % Mo;
}

signed main() {
    scanf("%lld%lld", &n, &m);
    if (m == 1) {
        printf("1\n");
        return 0;
    }
    fac[0] = fac[1] = 1;
    for (int i = 2; i <= M; i++)
        fac[i] = (1LL * fac[i - 1] * i) % Mo;
    Inv[M] = ksm(fac[M], Mo - 2);
    // for (int i = 1; i <= n; i++)
    //     printf("%d ", fac[i]);
    for (int i = M - 1; i >= 0; i--)
        Inv[i] = 1LL * Inv[i + 1] * (i + 1) % Mo;

    // for (int i = 1; i <= n; i++)
    //     printf("%d ", Inv[i]);
    f[0][0] = 1;
    for (int i = 1; i <= n; i++)
        for (int  j= 0; j <= i; j++) {
            if (j == 0)
                f[i][j] = f[i - 1][j];
            else {
                f[i][j] = (f[i - 1][j] + 1LL * f[i][j - 1] * (n - j + 1) % Mo * 1LL * (C(m - 2, n - i + (n - j + 1) * (m - 1) - 1)) % Mo) % Mo;
                // printf("%d %d\n", C(m - 2, n - i + (n - j + 1) * (m - 1) - 1), n - i + (n - j + 1) * (m - 1) - 1);
            }
        }
    printf("%lld\n", f[n][n]);
    return 0;
}


Measured memory_bytes: 94208
//#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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define mem(a,x) memset(a,x,sizeof(a))
#define nl "\n"
#define fout(x) fixed<<setprecision(x)
#define one(x) __builtin_popcountll(x)
#define F first
#define S second
#define or_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// find_by_order(), order_of_key()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << fout(10)<<x;}
void __print(double x) {cerr << fout(10)<<x;}
void __print(long double x) {cerr << fout(10)<<x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int mod=1e9+7;
const double eps=1e-9;
const double PI=acos(-1.0);

ll qpow(ll n,ll k){ll ans=1;assert(k>=0);n%=mod;while(k>0){if(k&1)ans=(ans*n)%mod;n=(n*n)%mod;k>>=1;}return ans%mod;}


// ****************************************************

int arr[200010];

int main()
{
    int n;
    cin>>n;
    for(int i=1; i<n; i++){
        int x;
        cin>>x;
        arr[x]++;
    }
    for(int i=1; i<=n; i++) cout<<arr[i]<<endl;
}


Measured memory_bytes: 4308
#include<bits/stdc++.h>
#define int long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define hell 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
bool prime[200005];
void sieve(){
    memset(prime, true, sizeof(prime)); 
    for (int p=2; p*p<200005; p++) 
    {
        if (prime[p] == true) 
        {
            for (int i=p*p; i<200005; i += p) 
                prime[i] = false; 
        } 
    }
}
int powmod(int x,int y,int mod){
    int res = 1;
    x = x%mod;
    while(y>0){
        if(y&1){
            res = (res*x)%mod;
        }
        y = y>>1;
        x = (x*x)%mod;
    }
    return res;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t=1;
    //cin>>t;
    while(t--){
        int n,p;
        cin>>n>>p;
        int a = max(-1000000*1LL,p-n+1);
        int b = min(1000000*1LL,p+n-1);
        rep(i,a,b+1){
            cout<<i<<" ";
        }
        cout<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<cstdlib>
#include<cctype>
#include<ctime>

using namespace std;

int n;
string s, t;
int main()
{
	cin >> n;
	cin >> s >> t;
//	cout << s << " " << t; 
	char ans[210];
	int ps, pt;
	ps = pt = 0;
	for(int i = 0; i <= 2 * n - 1; i++)
	{
		if(!(i % 2))
		{
			ans[i] = s[ps++]; 
		}
		else
		{
			ans[i] = t[pt++];
		}
	}
	for(int i = 0; i <= 2 * n - 1; i++)
	{
		cout << ans[i];
	}
	return 0;
}

Measured memory_bytes: 256
// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int main() {
    fastio();

    int x, t;

    cin >> x >> t;

    cout << max(0, x - t) << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<n+1;i++)
#define sort(A) sort(A.begin(),A.end())
#define reverse(A) reverse(A.begin(),A.end())
typedef long long ll;
typedef pair<int,int> P;

int main(){
  ll n,k;
  cin >> n >> k;
  map<ll,ll> mp;
  rep(i,n){
    ll x,y;
    cin >> x >> y;
    mp[x] += y;
  }
  for(auto p : mp){
    k -= p.second;
    if(k<=0){
      cout << p.first << endl;
      return 0;
    }
  }
}

Measured memory_bytes: 4224
n, m = map(int, input().split())
a = list(map(int, input().split()))
c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

d = [-1]*(n+1)
d[0] = 0

for i in range(n+1):
    for ai in a:
        if (i - c[ai]) >= 0:
            d[i] = max(d[i], d[i-c[ai]]*10 + ai)

print(d[-1])


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

int main() {
  int N, a[4], flag=0;
  cin >> N;
  
  for(int i=0; i<4; i++){
    a[i] = N % 10;
    N /= 10;
    
    if((a[0]==a[1] && a[1]==a[2]) || (a[1]==a[2] && a[2]==a[3]))
      flag=1;
  }
  
  if(flag==1)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
  return 0;
}


Measured memory_bytes: 3608
#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c,d[256];
  cin>>a;
  b=a-(a/100*100);
  c=b-b/10*10;
  if(a/100==c){
    cout<<"Yes"<<'\n';
  }
  else{
    cout<<"No"<<'\n';
  }
  return 0;
}
    


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

int main(){
	int a, b, c, x = 0, y = 0;
	char cc;
	while(cin >>a>>cc>>b>>cc>>c){
		if(a*a + b*b == c*c)x++;
		if(a == b)y++;
	}
	cout << x << endl << y << endl;
	return 0;
}

Measured memory_bytes: 3088
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<tuple>
#include<sstream>
#define rep(i,n) for(int i =0;i<(int)(n);i++)
typedef long long ll;
using namespace std;
int main() {
	string s;
	cin >> s;
	int ans = 100000000;
	rep(i, s.size() - 2) {
		int num = (s[i] - '0') * 100 + (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
		num = max(num - 753, 753 - num);
		ans = min(ans, num);
	}
	cout << ans << endl;
	return 0;
}


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

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

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

signed main() {
	ll a, b, c, d, e, f, sw = 0, sg = 0;
  double conce = -1;
  set<int> water, sugar;
  cin >> a >> b >> c >> d >> e >> f;
  
  double limit = (double)e / (100 + e);

  rep(i, 31) {
    rep(j, 31) {
      int waterab = 100 * a * i + 100 * b * j;
      if (waterab <= f) water.insert(waterab);
    }
  }

  rep(i, 3001) {
    rep(j, 3001) {
      int sugarcd = c * i + d * j;
      if (sugarcd <= f) sugar.insert(sugarcd);
    }
  }

  for(int w : water) {
    for(int s : sugar) {
      int suwa = s + w;
      if (suwa == 0) continue;
      double cand = (double)s / suwa;
      if (cand <= limit && suwa <= f && cand > conce) {
        conce = cand;
        sw = suwa;
        sg = s;
      }
    }
  }
  cout << sw << " " << sg << endl;
}

Measured memory_bytes: 384
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

/* #include <bits/stdc++.h> */

using namespace std;

/* g++ -g -fsanitize=address -lasan -std=c++11 -Dfoo_ */

/* freopen("input.txt", "rt", stdin); */
/* freopen("output.txt", "wt", stdout); */

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

struct Point { int x,t; };
vector<Point> A;    

bool cmp(const Point& a, const Point& b)
{
    if (a.x == b.x) 
        return a.t < b.t;
    return a.x < b.x;
}

int main()
{
#ifdef foo_
    freopen("foo", "rt", stdin);
#endif
    int n,m;
    while (cin >> n >> m) {
        A.clear();
        FOR(i,0,m) {
            Point a,b;
            cin >> a.x >> b.x;
            a.t = 0; b.t = 1;
            A.push_back(a);
            A.push_back(b);
        }
        sort(ALL(A),cmp);
        int ans = 0, cnt = 0;
        int lft = 0;
        FOR_ALL(p,A) {
            if (p->t) {
                if (cnt == m) {
                    ans += p->x - lft + 1;
                    lft = 0;
                }
                cnt--;
            }
            else
                cnt++;
            if (cnt == m) {
                if (!lft)
                    lft = p->x;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 2420
def ext_gcd(a,b):
    """returns tuple (gcd(a,b), x, y) s.t. ax+by=gcd(a,b)"""
    if b==0:
        return (a,1,0)
    g,x,y = ext_gcd(b,a%b) 
    return (g,y,x-a//b*y)
def invmod_by_gcd(a,P):
    _,x,y = ext_gcd(a,P)
    return (x+P) % P

F = []
FInv = []

def init_factorials(L,P):
    """initialize F and FInv less than L"""
    global F, FInv
    F = [1 for _ in range(L)]
    for i in range(1,L):
        F[i] = (F[i-1]*i)%P
    FInv = [1 for _ in range(L)]
    FInv[L-1] = invmod_by_gcd(F[L-1],P)
    for i in range(L-2,0,-1):
        FInv[i] = (FInv[i+1]*(i+1))%P
      
def combination(a, b, P):
    return (F[a]*FInv[b]*FInv[a-b])%P

P = 10**9+7 
H,W,A,B = map(int,input().split())

init_factorials(H+W,P)

ans = 0
for k in range(H-A):
    a = combination(B+k-1,B-1,P)
    b = combination(H+W-k-B-2,H-1-k,P)
    ans = (ans + a*b%P)%P
print(ans)


Measured memory_bytes: 18968
import sys
sys.setrecursionlimit(10010010)

def abc106_d():
    n, m, q = map(int, input().split())
    table = [[0]*(n+1) for _ in range(n+1)]
    for _ in range(m):
        l, r = map(int, input().split())
        table[l][r] += 1
    query = [tuple(map(int, input().split())) for _ in range(q)]

    dp = [[-1]*(n+1) for _ in range(n+1)]

    def calc(l, r):
        nonlocal dp
        if l == 0 or r == 0: return 0
        if dp[l][r] != -1: return dp[l][r]
        res = calc(l-1, r) + calc(l, r-1) - calc(l-1, r-1) + table[l][r]
        dp[l][r] = res
        return res

    for p, q in query:
        ans = calc(q, q) - calc(q, p-1) - calc(p-1, q) + calc(p-1, p-1)
        print(ans)

if __name__ == '__main__':
    abc106_d()

Measured memory_bytes: 31568
/* ---------- STL Libraries ---------- */

// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

/* ---------- Namespace ---------- */

using namespace std;

/* ---------- Type Abbreviation ---------- */

using ll = long long;

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

#define mp make_pair
#define mt make_tuple

/* ----------- debug ---------- */

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p);

template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "[";
    for (auto vv : v)
        os << vv << ",";
    return os << "]";
}

template <class T>
ostream& operator<<(ostream& os, set<T> v) {
    os << "[";
    for (auto vv : v)
        os << vv << ",";
    return os << "]";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "(" << p.first << "," << p.second << ")";
}

/* ---------- Constants ---------- */

// const ll MOD = 1000000007;
// const ll MOD = 998244353;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const double PI = acos(-1);
// const double EPS = 1e-10;
// mt19937 mert(LL(time(0)));

/* ---------- Short Functions ---------- */

template <typename T>
inline T sq(T a) { return a * a; }

template <typename T>
T gcd(T a, T b) {
    if (a < b) return gcd(b, a);
    return b == 0 ? a : gcd(b, a % b);
}

template <typename T, typename U>
T mypow(T b, U n) {
    if (n == 0) return 1;
    if (n == 1) return b /* % MOD */;
    if (n % 2 == 0) {
        return mypow(b * b /* % MOD */, n / 2);
    } else {
        return mypow(b, n - 1) * b /* % MOD */;
    }
}

ll pcnt(ll b) {
    return __builtin_popcountll(b);
}

template <typename T>
T iceil(T n, T d) {
    return (n + d - 1) / d;
}

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */

int main() {
    int N;
    cin >> N;

    pair<ll, pair<ll, ll>> dish[N];
    for (int i = 0; i < N; ++i) {
        cin >> dish[i].second.first >> dish[i].second.second;
        dish[i].first = dish[i].second.first + dish[i].second.second;
    }

    sort(dish, dish + N, greater<pair<ll, pair<ll, ll>>>());
    ll ans = 0;
    for (int i = 0; i < N; ++i) {
        if (i % 2 == 0) {
            ans += dish[i].second.first;
        } else {
            ans -= dish[i].second.second;
        }
    }

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


Measured memory_bytes: 2560
#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main(){
  int N;
  cin >> N;
  vector<int> P(N);
  rep(i, N) cin >> P.at(i);
  int result = 0;
  int min = P.at(0);
  rep(i, N){
    if(P.at(i) <= min){
      result++;
      min = P.at(i);
    }
  }
  cout << result << endl;
}

Measured memory_bytes: 1024
package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n)
	fmt.Scan(&k)

	t := n%k
	t2 := t-k
	if t2<0 {
		t2*= -1
	}
	fmt.Println(min(t,t2))
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}


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

//#define int long long
#define ll long long
#define all(v) (v).begin(), (v).end()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

signed main()
{   
    gearup;
    ll r,g,b,n;cin>>r>>g>>b>>n;
    ll res = 0;
    for(int i=0;i<=3000;i++){
        for(int j=0;j<=3000;j++){
            int k = (n - r*i - g*j);
            if(k % b != 0 || k<0)continue;
            res++;
            //cout << i << " " << j << " " << k << endl;
        }
    }
    out(res);
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
using namespace std;
#define Maxn 500000
#define ll long long
char s[Maxn+5];
int n;
int f[Maxn+5],g[Maxn+5];
int mx(int a,int b){
	return a>b?a:b;
}
int main(){
	scanf("%s",s+1);
	while(s[++n]!='\0');
	n++;
	for(int i=2;i<=n;i++){
		if(s[i-1]=='<'){
			f[i]=f[i-1]+1;
		}
	}
	for(int i=n-1;i>0;i--){
		if(s[i]=='>'){
			g[i]=g[i+1]+1;
		}
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans+=mx(f[i],g[i]);
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 4608
//Compiler optimization
#pragma GCC optimize("Ofast")

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

//typedef
typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll,ll> P;
typedef vector<P> vp;
typedef vector<vl> mat;

//macro
#define reverse(a) reverse(a.begin(),a.end())
#define unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define fr(i,n)for(ll i=0;i<(n);i++)
#define ifr(i,n)for(ll i=(n)-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define each(x, v) for (auto &x : v)

//abbreviation
#define pb push_back
#define elif else if
#define mp make_pair
#define endl '\n'

//debug
#define debug(x) cerr << #x << ": " << x << endl
#define v_debug(v) cerr << #v << endl;fr(i,v.size()) cerr << i << ": " << v[i] << endl

//const
const  ll INF = 1e18;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << setprecision(20);
    ll n;cin >> n;
    vl num(100010);
    ll a;
    ll res = 0;
    fr(i,n){
        cin >> a;
        num[a]++;
        res += a;
    }
    ll q;cin >> q;
    ll b,c;
    fr(i,q){
        cin >> b >> c;
        res += num[b]*(c-b);
        num[c] += num[b];
        num[b] = 0;
        printf("%lld\n", res);
    }
}

Measured memory_bytes: 3972
#include<bits/stdc++.h>
using namespace std;
#define maxn 100001
int main(void)
{
	char s[maxn],tmp,compare[maxn];
	int odd=0,even=0;
	scanf("%s",s);
	int length=strlen(s);
	for(int i=0;i<length;i++){
		if(s[i]=='1'&&i%2==0) odd++;
		if(s[i]=='1'&&i%2==1) even++;
		if(s[i]=='0'&&i%2==1) odd++;
		if(s[i]=='0'&&i%2==0) even++; 
	}
	if(odd>even) length-=odd;
	else length-=even;
	printf("%d\n",length);
 	return 0;
 }


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

int main() {
  int a,b;
  cin >> a >> b;
  if(a==b) cout << "Draw" << endl;
  else if(a==1||b==1) {
    if(a!=1) cout << "Bob" << endl;
    else if(b!=1) cout << "Alice" << endl;
  }
  else if(a<b) cout << "Bob" << endl;
  else if(a>b) cout << "Alice" << endl;
}

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

int main()
{
	int m,d,count=0,i,j;
	cin>>m>>d;
	if(d<22 || m<=3)
	{
	
	cout<<"0";
	return 0;
}
for(i=22;i<=d;i++)
{
	if((i%10)<2 || (i/10)<2)
	continue;
	else if((i%10)*(i/10)<=m)
	count++;
}
cout<<count;
	
	
	
}

Measured memory_bytes: 256
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cstring>

 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define pb push_back
#define LLIandI pair<long long int , int>
#define ll long long


long long int A[100004];
long long int B[100004];
ll int N;
long long sum=0;
int main(){
    scanf("%lld",&N);
    rep(i,N){
        scanf("%lld",&A[i]);
        if(i>0)B[i-1]=A[i]-A[i-1];
        sum+=A[i];
    }
    B[N-1]=A[0]-A[N-1];
    long long int X;
    if((2*sum)%(N*(N+1))==0){
        X=(long long int)(2*sum/(N*(N+1)));
    }else{
        printf("NO\n");return 0;
    }
    long long int ysum=0;
    rep(i,N){
        ll int y=(ll int)((B[i]+(N-1)*X)/N);
        if((B[i]+(N-1)*X)%N==0&&y<=X){
            ysum+=y;
        }else{
            printf("NO\n");return 0;
        }
    }
    if(ysum==X*(N-1)){
        printf("YES\n");return 0;
    }
    printf("NO\n");return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<double> vdbl;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<int> > vvint;
typedef vector<pair<int, int> > vpii;
typedef vector<pair<ll, ll> > vpll;
typedef priority_queue<int, vector<int>, greater<int> > spqint; //小さい順に取り出し
typedef priority_queue<ll, vector<ll>, greater<ll> > spqll;     //小さい順に取り出し
typedef priority_queue<int, vector<int>, less<int> > bpqint;    //大きい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll> > bpqll;        //大きい順に取り出し

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); i++)
#define FOR(i, a, b) for (int(i) = a; (i) < (int)b; i++)
#define IREP(i, v) for (auto i = v.begin(); i != v.end(); i++)
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define TS to_string
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const double PI = M_PI;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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; }     //最小公倍数

void yes()
{
    cout << "Yes" << endl;
}
void no()
{
    cout << "No" << endl;
}
//-----------------------------------------

//-----------------------------------------
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll x ;
    cin >> x ;
    vector<pair<ll,ll>> a ;
    for(ll i=-1000;i<1000+1;i++){
        a.PB(MP(i*i*i*i*i,i)) ;
    }
    REP(i,SZ(a)){
        REP(j,SZ(a)){
            if(a[i].FI-a[j].FI==x){
                cout << a[i].SE <<" " << a[j].SE <<endl ;
                return 0 ;
            }
        }
    }

    

    return 0;
}


Measured memory_bytes: 3720
import collections
while 1:
	li=collections.defaultdict(int)
	emplist=[]
	n=int(input())
	if n==0:
		break
	for i in range(n):
		(emp,prc,num)=list(map(int,input().split()))
		if emp not in emplist:
			emplist.append(emp)
		li[emp]+=prc*num
	tmp=True
	for i in emplist:
		if li[i]>=1000000:
			print(i)
			tmp=False
	if tmp:
		print("NA")
	li.clear()		

Measured memory_bytes: 6752
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N, M := getInt(), getInt()
	a := make([]int, N+1)
	for i := 1; i <= N; i++ {
		a[i] = getInt()
	}

	for i := 1; i <= N; i++ {
		a[i] += a[i-1]
	}
	m := make(map[int]int)
	for i := 0; i <= N; i++ {
		m[a[i]%M]++
	}
	ans := 0
	for _, v := range m {
		ans += v * (v - 1) / 2
	}
	out(ans)
}


Measured memory_bytes: 7168
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define MX 2010
#define EPS 0.00000000001
#define MOD 1000000007
#define PI acos(-1)
#define INF 100000000
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;

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

int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int n;
    cin>>n;
    LL ans = 0;
    int g1,g2;
    loop(i,1,n+1)
    {
        loop(j,1,n+1)
        {
            g1 = gcd(i,j);
            loop(k,1,n+1)
            {
                g2 = gcd(g1,k);
                ans += g2;
            }
        }
    }

    cout<<ans<<'\n';

    return 0;
}


Measured memory_bytes: 3676
#include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define fi first
#define se second
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

struct edge
{
    int to;
    int cost;
    edge(int to, int cost) : to(to), cost(cost) {}
};
using Graph = vector< vector < edge > >;

P dfs(Graph &g, int idx, int par) {
  P ret(0, idx);
  for(auto &e : g[idx]) {
    if(e.to == par) continue;
    auto cost = dfs(g, e.to, idx);
    cost.first += e.cost;
    ret = max(ret, cost);
  }
  return ret;
}

int tree_diameter(Graph &g) {
  auto p = dfs(g, 0, -1);
  auto q = dfs(g, p.second, -1);
  return (q.first);
}

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

    int n;
    cin >> n;
    while(n != 0){
        Graph G(n);
        vector<int> p(n-1);
        REP(i, n-1) cin >> p[i];
        vector<int> d(n-1);
        REP(i, n-1) cin >> d[i];
        
        int ans = 0;
        REP(i, n-1){
            p[i]--;
            G[i+1].pb(edge(p[i], d[i]));
            G[p[i]].pb(edge(i+1, d[i]));
            ans += d[i];
        }
        vector<int> degree(n);
        REP(i, n) degree[i] = G[i].size();
        
        int ct = 0;
        map<int, int> mp;
        REP(i, n){
            if(degree[i] >= 2) mp[i] = ct++;
        }
        
        Graph T(ct);
        REP(i, n-1){
            if(degree[i] == 1) continue;
            for(auto &e : G[i]){
                if(degree[e.to] >= 2){
                    T[mp[i]].pb(edge(mp[e.to], e.cost));
                    ans += e.cost;
                }
            }
        }
        // REP(i, ct){
        //     for(auto &e: T[i]) cout << i << " " << e.to << " " << e.cost << "\n";
        // }

        ans -= tree_diameter(T);
        cout << ans << "\n";

        cin >> n;
    }


    return 0;
}


Measured memory_bytes: 3372
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 5;
ll N, A[MAX], psum[MAX];
map<ll, ll> mp;

int main(){
    scanf("%lld", &N);
    for(int i = 1; i <= N; i++){
        scanf("%lld", A + i);
    }
    ll ans = 0;
    mp[0]++;
    for(int i = 1; i <= N; i++){
        psum[i] = psum[i - 1] + A[i];
        ans += mp[psum[i]];
        mp[psum[i]]++;
    }
    printf("%lld\n", ans);
    return 0;
}

Measured memory_bytes: 15872
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
  private static class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
      reader = new BufferedReader(new InputStreamReader(in));
      tokenizer = null;
    }

    public String next() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public String nextLine() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          return reader.readLine();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }

      return tokenizer.nextToken("\n");
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public int[] nextIntArray(int n) {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = nextInt();
      return a;
    }

    public long[] nextLongArray(int n) {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = nextLong();
      return a;
    }
  }

  public static void main(String[] args) {
    FastScanner sc = new FastScanner(System.in);
    int n = sc.nextInt();
    String[] chars = "abcdefghij".split("");
    List<String> arr = new ArrayList<>();
    arr.add("a");
    StringBuilder result = new StringBuilder();
    for (int i = 1; i < n; i++) {
      List<String> next = new ArrayList<>();
      Set<String> used = new HashSet<>();
      for (String s : arr) {
        used.clear();
        used.addAll(Arrays.asList(s.split("")));
        for (int j = 0; j <= used.size(); j++) {
          next.add(s + chars[j]);
        }
      }
      arr.clear();
      arr.addAll(next);
    }
    for (String s : arr) {
      if (result.length() > 0) result.append("\n");
      result.append(s);
    }
    System.out.println(result.toString());
  }
}

Measured memory_bytes: 70960
#include<cstdio>

typedef long long ll;

int main() {
	int a, n, m;

	scanf("%d%d%d", &a, &n, &m);

	int ans = 0;
	for (int i = 1; i <= m; i++) {
		ll sum = i + a;
		ll tmp = sum;
		for (int j = 0; j < n - 1; j++) {
			sum = sum * tmp;
			if (sum > m) break;
		}
		if (sum > m) break;
		int bef = 0;
		while (sum > 0) {
			 bef+= sum % 10;
			sum /= 10;
		}
		if (bef == i) ans++;
	}
	printf("%d\n", ans);
}


Measured memory_bytes: 2760
#include<bits/stdc++.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
#define lowbit(x) (x&-x)
#define F first
#define S second 
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f; 
const int maxn=1e5+10;
int main()
{
	int n,m,q;
	string s;
	cin>>s;
	int a=0,b=0;
	for(int i=0;i<3;i++){
		if(s[i]=='A') a++;
		else b++;
	}
	if(b>0&&a>0) cout<<"Yes";
	else cout<<"No"; 
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
int N, V, LOG, a[200001], LE[19][200001], RE[19][200001], F[262144], G[262144], OK[200001];
int main()
{
	scanf("%d%d", &N, &V);
	while (1 << LOG <= V)
		LOG++;
	for (int i = 1; i <= N; i++)
		scanf("%d", a + i);
	for (int i = 0; i <= LOG; i++)
	{
		int *le = LE[i], *re = RE[i];
		le[1] = 1;
		for (int j = 2; j <= N; j++)
			le[j] = a[j] - a[j - 1] <= V >> i ? le[j - 1] : j;
		re[N] = N;
		for (int j = N - 1; j; j--)
			re[j] = a[j + 1] - a[j] <= V >> i ? re[j + 1] : j;
	}
	for (int i = 0; i < 1 << LOG; i++)
	{
		F[i] = 0;
		G[i] = N + 1;
		for (int j = 1; j <= LOG; j++)
			if (i >> j - 1 & 1)
			{
				F[i] = std::max(F[i], F[i - (1 << j - 1)] == N ? N : RE[j][F[i - (1 << j - 1)] + 1]);
				G[i] = std::min(G[i], G[i - (1 << j - 1)] == 1 ? 1 : LE[j][G[i - (1 << j - 1)] - 1]);
			}
	}
	for (int i = 0; i < 1 << LOG; i++)
		if (F[i] + 1 >= G[(1 << LOG) - 1 - i])
		{
			for (int j = N; j--; )
				puts("Possible");
			return 0;
		}
	for (int i = 0; i < 1 << LOG; i++)
		if (RE[0][F[i] + 1] + 1 >= G[(1 << LOG) - 1 - i])
		{
			int R = RE[0][F[i] + 1], L = LE[0][R];
			if (!OK[L])
				std::fill(OK + L, OK + R + 1, 1);
		}
	for (int i = 1; i <= N; i++)
		puts(OK[i] ? "Possible" : "Impossible");
	return 0;
}


Measured memory_bytes: 34816
// ABC070-D
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n, m) for (int (i) = (n); (i) < (m); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define debugB(x, y) cerr<<#x<<": "<<bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

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

struct edge {
   ll to, cost;
};

const ll INF = 1e18;
int N, Q, K;
vec<edge> G[100010];
ll dist[100010];
bool visited[100010];

void findK(int s) {
   visited[s] = true;
   for (edge e : G[s]) {
      if (!visited[e.to]) {
         dist[e.to] = dist[s] + e.cost;
         findK(e.to);
      }
   }
}

int main() {
   cin >> N;
   rep (i, 0, N-1) {
      ll a, b, c;
      cin >> a >> b >> c;
      a--, b--;
      G[a].push_back(edge{b, c});
      G[b].push_back(edge{a, c});
   }

   cin >> Q >> K;
   K--;
   dist[K] = 0;
   rep (i, 0, N) visited[i] = false;
   findK(K);
   for (int i = 0; i < Q; i++) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      cout << dist[x] + dist[y] << endl;
   }

   return 0;
}

Measured memory_bytes: 12800
import math
H, W = map(int, input().split())
M = [input() for _ in range(H)]

INF = float('inf')
D = [[INF]*W for _ in range(H)]
D[0][0] = 0 if M[0][0] == '.' else 1

q = set([(0, 0)])

while q:
    nq = set()
    for y, x in q:
        d = D[y][x]
        m = M[y][x]
        if x < W-1:
            t = d if M[y][x+1] == m else d+1
            D[y][x+1] = min(D[y][x+1], t)
            nq.add((y, x+1))
        if y < H-1:
            t = d if M[y+1][x] == m else d+1
            D[y+1][x] = min(D[y+1][x], t)
            nq.add((y+1, x))
    q = nq

print(math.ceil(D[H-1][W-1] / 2))

Measured memory_bytes: 3188
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
const int maxn=2e5+50;
int N;
int a[maxn];
int an[maxn];
int head[maxn];
int root[maxn];
map<int,int> f[maxn];
struct edge
{
	int to,nex;
	edge(int to=0,int nex=0):to(to),nex(nex){}
};
vector<edge> G;
inline void addedge(int u,int v)
{
	G.push_back(edge(v,head[u])),head[u]=G.size()-1;
	G.push_back(edge(u,head[v])),head[v]=G.size()-1;
}
int dp(int u,int x,int fa)
{
	if(f[u].count(x)) return f[u][x];
	f[u][x]=0;
	for(int i=head[u];~i;i=G[i].nex)
	{
		int v=G[i].to;
		if(v!=fa)
		{
			if(v<=x) f[u][x]+=dp(v,x,u)+1;
		}
	}
	return f[u][x];
}
void dfs(int u,int fa)
{
	for(int i=head[u];~i;i=G[i].nex)
	{
		int v=G[i].to;
		if(v!=fa)
		{
			a[v]=max(a[u],u); 
			dfs(v,u);
		}
	}
	if(u==1) return;
	if(u>a[fa]) an[u]=dp(u,a[u],fa)+1;
	else an[u]=dp(u,a[u],fa)-dp(u,a[fa],fa);
}
void pushdown(int u,int fa)
{
	for(int i=head[u];~i;i=G[i].nex)
	{
		int v=G[i].to;
		if(v!=fa)
		{
			an[v]+=an[u];
			pushdown(v,u);
		}
	}
}
void solve()
{
	dfs(1,-1);
	pushdown(1,-1);
}
int main()
{
	scanf("%d",&N);
	memset(head,-1,sizeof(head));
	for(int i=1;i<N;++i)
	{
		int u,v; scanf("%d%d",&u,&v);
		addedge(u,v);
	}
	solve();
	for(int i=2;i<=N;++i)
	{
		if(i!=2) printf(" ");
		printf("%d",an[i]);
	}
	printf("\n");
	return 0;
}

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

int e;

int main() {
	while (cin >> e) {
		if (!e) break;
		
		int ans = 1145141919;
		for (int z = 0; z * z * z <= e; z++) {
			for (int y = 0; y * y <= e - z * z * z; y++) {
				int x = e - y * y - z * z * z;
				if (x < 0) continue;
				ans = min(ans, x + y + z);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

int main(void){
  int a, b, c;

  cin >> a >> b >> c;

  if(a==b&&a==c) cout << "1" << endl;
  else if (a==b || b==c || a==c) cout << "2" << endl;
  else cout << "3" << endl;

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
bool vis[50][50];
int mp[50][50];
bool u[5];
int w,h;
int cnt;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int d(int y,int x){
  int res=0;


  if(mp[y][x]!=0){
    u[mp[y][x]%2]=1;
    return 0;
  }
  if(vis[y][x]==1) return 0;
  vis[y][x]=1;

  for(int i=0;i<4;i++){
    int nx=dx[i]+x,ny=dy[i]+y;
    if(nx<0 || nx>=w || ny<0 || ny>=h) continue;
    res+=d(ny,nx);
  }
  return res+1;
}



int main(){
  while(cin>>w>>h && w&&h){
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
        vis[i][j]=0;
    for(int i=0;i<h;i++){
      string s;
      cin>>s;
      for(int j=0;j<w;j++){
        if(s[j]=='W') mp[i][j]=1;
        else if(s[j]=='B') mp[i][j]=2;
        else mp[i][j]=0;
      }
    }

    int bn=0,wn=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
        u[0]=u[1]=0;
        if(vis[i][j]==0) cnt = d(i,j);
        if(u[0]==u[1])continue;
        if(u[0]) bn+=cnt;
        if(u[1]) wn+=cnt;

        
      }
    cout<<bn<<" "<<wn<<endl;
  }

  return 0;
}

Measured memory_bytes: 3332
#include <bits/stdc++.h>
using namespace std;
namespace IO {
  const int IN_LEN = 1 << 18 | 1;
  static char buf[IN_LEN] , *s , *t;
  inline int Getchar( void ) { return s == t && ( t = ( s = buf ) + fread( buf , 1 , IN_LEN , stdin ) ) , s == t ? -1 : *s++; }
  template <class T> inline void read( T & x ) {
    static int c , f;
    for( c = Getchar() , f = 0 ; c < '0' || c > '9' ; c = Getchar() ) if( c == '-' ) f = 1;
    for( x = 0 ; c <= '9' && c >= '0' ; c = Getchar() ) x = x * 10 + c - 48;
    x = f ? -x : x;
  }
}using IO::read;
const int MAXN = 1e5 + 1;
typedef pair<int,int> pii;
set<int> E[MAXN];
map<pii,int> rem;
int n , f[MAXN];
queue<pii> Q;
pii get( int x , int y ) { if( x > y ) swap( x , y ); return make_pair( x , y ); }
int find( const int & x ) { return x == f[x] ? x : f[x] = find( f[x] ); }
int main( void ) {
  read( n );
  for( int i = 1 , u , v ; i < n ; ++i ) {
    read( u ) , read( v );
    ++rem[get( u , v )];
    E[u].insert( v ) , E[v].insert( u );
  }
  for( int i = 1 , u , v ; i < n ; ++i ) {
    read( u ) , read( v );
    ++rem[get( u , v )];
    E[u].insert( v ) , E[v].insert( u );
    if( rem[get( u , v )] == 2 ) Q.push( pii( u , v ) );
  }
  for( int i = 1 ; i <= n ; ++i ) f[i] = i;
  while( !Q.empty() ) {
    int u = Q.front().first , v = Q.front().second;Q.pop();
    if( ( u = find( u ) ) == ( v = find( v ) ) ) continue;
    if( E[u].size() < E[v].size() ) swap( u , v );
    for( int k : E[v] ) {
      rem[get( u , v )] = 0;
      E[u].insert( k ) , E[k].insert( u );
      ++rem[get( u , k )];
      if( rem[get( u , k )] == 2 ) Q.push( pii( u , k ) );
    }
    f[v] = u;
    E[v].clear();
  }
  int cnt = 0;
  for( int i = 1 ; i <= n ; ++i ) {
    find( i );
    cnt += ( i == f[i] );
  }
  puts( cnt == 1 ? "YES" : "NO" );
  return 0;
}
 

Measured memory_bytes: 57472
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define SCLLD(n) scanf("%lld",&n)
#define SCLLD2(m,n) scanf("%lld%lld",&m,&n)
#define SCLLD3(m,n,k) scanf("%lld%lld%lld",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define IN(x,S) (S.count(x) != 0)
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector < VI > VVI;
typedef long long ll;
/////////////////////////(๑•﹏•๑｀)/////////////////////////
int main() {
	ll N; SCLLD(N);
	vector<ll> A;
	vector<ll> S;
	A.PB(0);
	S.PB(0);

	REP(i,N){
		ll x; SCLLD(x);
		A.PB(x);
		S.PB(x + S[i]);
	}

	sort(S.begin(),S.end());

	ll count;

	vector<ll> U;
	
	count = 1;
	REP(i,S.size()-1){
		if (S[i] == S[i+1]){
			count++;
		}else{
			U.PB(count);
			count = 1;
		}
	}
	U.PB(count);

	ll ans = 0;

	REP(i,U.size()){
		if (U[i] > 1){
			ans += U[i] * (U[i] - 1) / 2;
		}
	}

	PRINTLLD(ans);
	return 0;
}

Measured memory_bytes: 6500