akhauriyash/Code-Regression huggingface dataset:
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public int[][] dist;
    public LinkedList<Integer>[] node;
    void run() {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        node = new LinkedList[n];
        for (int i = 0; i < n; i++)
            node[i] = new LinkedList<Integer>();
        dist = new int[2][n];
        for (int i = 0; i < n-1; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            node[a-1].add(b-1);
            node[b-1].add(a-1);
        }
        dfs(0, 0, -1);
        dfs(1, n-1, -1);
        int black = 0;
        for (int i = 0; i < n; i++) {
            if (dist[0][i] <= dist[1][i])
                black++;
        }
        if (black > n/2)
            System.out.println("Fennec");
        else
            System.out.println("Snuke");
    }

    void dfs(int id, int current, int prev) {
        if (prev != -1) dist[id][current] = dist[id][prev] + 1;
        for (int i = 0; i < node[current].size(); i++) {
            int next = node[current].get(i);
            if (next != prev)
                dfs(id, next, current);
        }
    }

    public static void main(String[] args) {
        new Main().run();
    }
}


Measured memory_bytes: 120808
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 1000000007;
int main(int argc, char const *argv[]) {
  ll n, k;std::cin >> n >> k;
  vll a(n);
  ll pl = 0, mi = 0;
  ll x = 1;
  for(int i=0;i<n;i++){
    std::cin >> a[i];
    x = (x * ((a[i] + P)%P))%P;
    if(a[i]>0) pl++;
    else mi++;
  }
  ll ans = 1;
  sort(all(a), [](ll l, ll r){return abs(l) < abs(r);});
  reverse(all(a));
  ll max_mi = min(k, mi);
  ll min_mi = max(0LL, k - pl);
  ll cnt = 0;

  if(min_mi==max_mi&&min_mi%2){
    //偶数個のマイナスができない場合
    //マイナスは全て取らないと
    reverse(all(a));
    for(int i=0;i<n&&cnt<k;i++){
      if(a[i]<=0) cnt++, ans = (ans * ((a[i]+P)%P))%P;
      else cnt++, ans = (ans * ((a[i]+P)%P))%P;
    }
    std::cout << ans << '\n';
    return 0;
  }

  vll minus, plus;
  for(int i=0;i<n;i++) {
    if(a[i]<=0) minus.push_back(a[i]);
  }
  for(int i=0;i<n;i++){
    if(a[i]>0) {
      if(k%2) ans = a[i], k--;
      else plus.push_back(a[i]);
    }
  }
  //for(auto t:minus) std::cout << t << '\n';
  //for(auto t:plus) std::cout << t << '\n';

  ll p_idx = 0, m_idx = 0;
  for(int i=0;i<k;i+=2){
    ll L = (p_idx*2+2>plus.size()?0:plus[p_idx*2]*plus[p_idx*2+1]);
    ll R = (m_idx*2+2>minus.size()?0:minus[m_idx*2]*minus[m_idx*2+1]);
    if(L > R) p_idx++, ans = (ans * (L%P))%P;
    else m_idx++, ans = (ans * (R%P))%P;
  }
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 6716
h,w = list(map(int,input().split()))
h += 2
w += 2
grid = '#'*w
for _ in range(h-2):
    grid += '#' + input() + '#'
grid += '#'*w

from collections import deque
ans = 0
for i in range(len(grid)):
    INF = 10**9
    dist = [INF]*(h*w)
    if grid[i]=='#':
        continue
    dist[i] = 0
    q = deque([i])
    while q:
        v = q.popleft()
        dv = dist[v]
        for m in (-1,1,w,-w):
            move = v + m
            if grid[move]=='#':
                continue
            dmove = dv + 1
            if dmove >= dist[move]:
                continue
            dist[move] = dmove
            q.append(move)
    # print([dist[i] if dist[i]!=INF else 0 for i in range(len(dist))])
    ans = max(ans, max([dist[i] if dist[i]!=INF else 0 for i in range(len(dist))]))
    # print(ans)
    
print(ans)

Measured memory_bytes: 9336
#include <bits/stdc++.h>
#define def define;
#define optimizator ios_base::sync_with_stdio(0);
#define starter int n;cin>>n;int a[n],b[n],c[n];for(int i=0; i<n; i++) cin>>a[i]>>b[i]>>c[i];
using namespace std;
int main(){
	optimizator;
	int a,b;
	cin>>a>>b;
	if(a<10 && b<10){
		cout<<a*b;
	}
	else{
		cout<<-1;
	}
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;

namespace MSBPC2019
{
    class D
    {
        static void Main(string[] args)
        {
            long N = long.Parse(Console.ReadLine());
            string S = Console.ReadLine();

            int res = 0;
            for (int i = 0; i < 1000; i++)
            {
                string V = String.Format("{0:000}", i);
                int vindex = 0;
                int matchCount = 0;
                for (int j = 0; j < S.Length; j++)
                {
                    if (vindex < 3 && S[j] == V[vindex]) { vindex++; matchCount++; }
                }
                if (matchCount == 3) { res++; }
            }
            Console.WriteLine(res);
        }
    }
}


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

class Program
{
    static void Main(string[] args)
    {
        var wrong_answer = new wrong_answer();
        wrong_answer.Solve();
    }
}

public class wrong_answer
{
    const int INF = 1 << 29;
    const long INFL = 1L << 60;
    const int MOD = 1000000007;
    const int MOD2 = 998244353;

    myinout IO = new myinout();



    public void Solve()
    {

        var o = IO.longm;
        var h = o[0];
        int div = 0;
        while(h>0){
            h /= 2;
            div++;
        }
        IO.wri(Math2.pow(2,div,INFL)-1L);


    }
}






public static class Math2
{
    public static long pow(long i, long n, long MOD = 1000000007)
    {
        long res = 1;
        while (n > 0)
        {
            if ((n & 1) != 0) res = res * i % MOD;
            i = i * i % MOD;
            n >>= 1;
        }
        return res;
    }

    public static long gcd(long i, long y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public static long lcm(long i, long y)
    {
        return i * y / gcd(i, y);
    }

    public static BigInteger bgcd(BigInteger i, BigInteger y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public static BigInteger blcm(BigInteger i, BigInteger y)
    {
        return i * y / bgcd(i, y);
    }

    public static Dictionary<long, int> primefactorization(long N)
    {

        var ret = new Dictionary<long, int>();

        for (long i = 2; i * i <= N; ++i)
        {
            int cnt = 0;
            while (N % i == 0)
            {
                cnt++;
                N /= i;
            }
            if (cnt != 0) ret[i] = cnt;

        }
        if (N >= 2) ret[N] = 1;

        return ret;

    }

    public static List<long> divisorenumrate(long N)
    {
        var ret = new List<long>();

        for (long i = 1; i * i <= N; ++i)
        {

            if (N % i == 0)
            {
                ret.Add(i);
                ret.Add(N / i);
            }

        }

        return ret;

    }

    public static void swap<T>(ref T a, ref T b)
    {
        var i = a;
        a = b;
        b = i;
    }

}

class myinout
{
    //in
    public string str => Console.ReadLine();

    public string[] strm => str.Split(' ');

    public long[] longm => strm.Select(long.Parse).ToArray();
    public int[] intm => strm.Select(int.Parse).ToArray();
    public char[] charm => str.ToArray();
    public double[] doublem => strm.Select(double.Parse).ToArray();
    public long long1 => longm[0];
    public int int1 => intm[0];
    public char char1 => charm[0];
    public double double1 => doublem[0];



    //out
    public void wri<T>(T i)
    {
        Console.WriteLine(i);
    }
    public void wri<T>(T[] i)
    {
        Console.WriteLine(string.Join(" ", i));
    }

    public void yn(bool i)
    {
        if (i) wri("Yes");
        else wri("No");
    }

}


Measured memory_bytes: 13396
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <numeric>
#include <stdlib.h>
#include <time.h>
#include <chrono>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

ll d;
vector<ll> c(26 + 1);
vector<vector<ll>> s(365 + 1, vector<ll>(26 + 1, 0));
ll SumC = 0;

ll scoring(vector<int> t)
{
    ll ans = 0;
    vector<ll> last(d + 1, 0);

    for (int i = 1; i <= d; i++)
    {
        ans += s[i][t[i]];
        last[t[i]] = i;

        for (int j = 1; j <= 26; j++)
        {
            ans -= c[j] * (i - last[j]);
        }
    }

    return ans;
}

int main(void)
{
    srand(time(NULL));
    cin >> d;

    for (int i = 1; i <= 26; i++)
    {
        cin >> c[i];
        SumC += c[i];
    }

    for (int i = 1; i <= d; i++)
    {
        for (int j = 1; j <= 26; j++)
        {
            cin >> s[i][j];
        }
    }

    vector<int> BestT(d + 1, 1);

    for (int i = 1; i <= d; i++)
    {
        BestT[i] = rand() % 26 + 1;
    }

    // printf("%lld\n", scoring(BestT));

    double BestScore = scoring(BestT);

    auto start = chrono::system_clock::now();
    vector<int> t(d + 1);

    double T0 = 1e3;
    double T1 = 1;
    double TL = 1950;

    ll count = 0;

    while (1)
    {
        ll msec;

        auto end = chrono::system_clock::now(); // 計測終了時刻を保存
        auto dur = end - start;                 // 要した時間を計算
        msec = chrono::duration_cast<chrono::milliseconds>(dur).count();

        if (msec >= TL)
        {
            break;
        }

        double T = powf(T0, 1 - msec / TL) * powf(T1, msec / TL);

        t = BestT;

        t[rand() % d + 1] = rand() % 26 + 1;

        double score = scoring(t);

        if (exp((score - BestScore) / T) > 0.5)
        {
            BestT = t;
            BestScore = score;
        }
    }

    for (int i = 1; i <= d; i++)
    {
        printf("%d\n", BestT[i]);
    }

    // printf("%lld\n", scoring(BestT));
}


Measured memory_bytes: 4380
#include <bits/stdc++.h>
using namespace std;
map <int,int> mp;
int N,M;
int main(){
    cin>>N>>M;

    int a,b;
    for(int i=0;i<M;i++){ 
        cin>>a>>b;
        mp[a]++;mp[b]++;
    }
    
    for(int i=1;i<=N;i++){
        cout<<mp[i]<<"\n";
    }

    return 0;
}


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

int main()
{
  int N, K;
  cin >> N >> K;
  long long M = 1LL;
  for (int i=0;i<N-1;i++)
  {
    M *= K-1;
  }
  cout << K * M << endl;
  return 0;
}

Measured memory_bytes: 256
// 4/30
// 過去問攻略

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;

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

    long long ans;

    ans = A * (N / (A + B));
    ans += min((N % (A + B)), A);

    cout << ans << endl;
}


Measured memory_bytes: 256
N,M = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(M)]
R = [0] * N

for c in range(M):
    R[ab[c][0]-1] += 1
    R[ab[c][1]-1] += 1
for c in range(N):
    print(R[c])

Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long int
#define ii pair<int,int>
#define MAX 1000000007
using namespace std;

int main(void){
	int n,l;
	cin>>n>>l;
	int ans=0,min=MAX, val=MAX;
	rep(i,n){
		ans+=l+i;
		if(abs(l+i) < min){
			min=abs(l+i);
			val = l+i;
		}
	}

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


Measured memory_bytes: 256
#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <bitset>
#include <numeric>

constexpr intmax_t  operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t    operator ""_zu(unsigned long long n) { return n; }
// constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }

bool f(std::vector<size_t> const& a, size_t k, size_t i) {
  if (a[i] >= k) return true;
  std::bitset<5002> bs;
  bs.set(0);

  for (size_t j = 0; j < a.size(); ++j) {
    if (i != j) bs |= bs << a[j];
  }

  return (bs._Find_next(k-a[i]-1) < k);
}

int main() {
  size_t n, k;
  scanf("%zu %zu", &n, &k);

  std::vector<size_t> a(n);
  for (auto& ai: a) scanf("%zu", &ai);

  if (std::accumulate(a.begin(), a.end(), 0_zu) < k)
    return !printf("%zu\n", n);

  std::sort(a.begin(), a.end());

  size_t lb = -1;
  size_t ub = n;
  while (ub-lb > 1) {
    size_t mid = (lb+ub) >> 1;
    (!f(a, k, mid)? lb: ub) = mid;
  }
  // size_t lb = 0;
  // while (!f(a, k, lb)) ++lb;

  printf("%zu\n", ub);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define REP(i,n) for(int i=0;i<n;i++)
#define REPP(i,n) for(int i=1;i<=n;i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
    long long c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
using namespace std;

bool prime_(int n){
  if(n==1){
    return false;
  }else if(n==2){
    return true;
  }else{
    for(int i=2;i<=sqrt(n);i++){
      if(n%i==0){
        return false;
      }
    }
    return true;
  }
}

long long gcd_(long long a, long long b){
  if(a<b){
    swap(a,b);
  }
  if(a%b==0){
    return b;
  }else{
    return gcd_(b,a%b);
  }
}
 
long long lcm_(long long x, long long y){
  return (x/gcd_(x,y))*y;
}

int main(){
  
  long long n;
  cin>>n;
  long long ans = n - 1;
  for(long long i = 1; i<= 1'000'000 ; i++){
    if(i*i > n){
      break;
    }
    if(n%i == 0){
      ans = min(ans, (i-1)+(n/i-1));
    }
  }
  cout<<ans<<endl;
  return 0;
}


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

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  int ans = 0;
  for (int i = 0; i < n - 2; i++) {
    if (s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C') {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    private List<List<Integer>> g;
    private int[] a;
    private int[] ans;

    private int bs(List<Integer> list, int t) {
        int left = 0, right = list.size() - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (list.get(mid) >= t) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }

    private void dfs2(int u, int fa, List<Integer> buf) {
        for (int v : g.get(u)) {
            if (v != fa) {
                dfs(v, u, buf);
            }
        }
    }

    private void dfs(int u, int fa, List<Integer> buf) {
        int t = bs(buf, a[u]);
        if (t == buf.size() - 1) {
            buf.add(a[u]);
            ans[u] = buf.size();
            dfs2(u, fa, buf);
            buf.remove(buf.size() - 1);
        } else {
            int tmp = buf.get(t + 1);
            if (a[u] < tmp) {
                buf.set(t + 1, a[u]);
                dfs2(u, fa, buf);
                buf.set(t + 1, tmp);
            } else {
                dfs2(u, fa, buf);
            }
            ans[u] = buf.size();
        }
    }

    private void solve() {
        int n = sc.nextInt();
        a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        g = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            g.add(new ArrayList<>());
        }

        for (int i = 1; i < n; i++) {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            g.get(u).add(v);
            g.get(v).add(u);
        }

        ans = new int[n];
        dfs(0, -1, new ArrayList<>());

        Arrays.stream(ans).forEach(out::println);
    }

    private static PrintWriter out;
    private static MyScanner sc;

    private static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        private MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

    public static void main(String[] args) {
        out = new PrintWriter(new BufferedOutputStream(System.out));
        sc = new MyScanner();
        new Main().solve();
        out.close();
    }
}


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

#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <string_view>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
using namespace std;

/*----------macros----------*/

//output================
#define endl '\n'
#define fcout(a) cout << fixed << setprecision((int)(a))

//typedef===============
typedef long long       ll;
typedef long double     ld;
typedef vector<int>     Vi;
typedef vector<ll>      Vl;
typedef pair<int, int>  Pii;
typedef pair<ll, ll>    Pll;

//const number==========
constexpr ll INF9   = (ll)1e9 + 1;
constexpr ll INF18  = (ll)1e18 + 1;
constexpr ll MOD1    = 1000000007;
constexpr ll MOD2   = 998244353;
constexpr ld PI     = 3.141592653589793L;

//member================
#define psb push_back
#define epb emplace_back
#define psf push_front
#define mkp make_pair
#define fst first
#define scd second

//others================
#define all(x)          (x).begin(), (x).end()
#define rall(x)         (x).rbegin(), (x).rend()
#define rep(i, n)       for(int i = 0; (i) < (n); ++(i))
#define reps(i, n)      for(int i = 1; (i) <= (n); ++(i))
#define rrep(i, n)      for(int i = n - 1; (i) >= 0 ; --(i))
#define repbit(bit, n)  for(int bit = 0; (bit) < (1 << (n)); ++(bit))
#define ifbit(bit, i)   if((bit) & (1 << (i)))
#define CtoI(c)         int((c)) - int('0')
#define ItoC(c)         char((c) + '0')

//functions=============
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

//vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
//vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };

/*--------additional--------*/



/*----------main------------*/

int main() {
/*----------fastio----------*/
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
/*----------solve-----------*/

    int n; cin >> n;
    Vi a(n), d(n);
    rep(i, n){
        cin >> a[i];
        d[i] = a[i];
    }

    sort(rall(d));
    rep(i, n){
        if(a[i] == d[0])
            cout << d[1] << endl;
        else cout << d[0] << endl;
    }

return 0;
}

Measured memory_bytes: 4808
# from pprint import pprint
# import math
# import collections

# n = int(input())
d, t, s = map(int, input().split(' '))
# a = list(map(int, input().split(' ')))
# a = []
# for i in range(n):
#     a.append(int(input()))
x = d / (1.0 * s)

if x <= t:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 9152
#include <iostream>
#include <algorithm>

using namespace std;

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

struct Team {
  Team() { ac = pe = 0; rep(i, 10) p[i] = 0; }
  int ac, pe, num, p[10];
  bool operator<(Team t) {
    if (ac != t.ac) return ac > t.ac;
    if (pe != t.pe) return pe < t.pe;
    return num > t.num;
  }
  bool operator==(Team t) {
    if (ac != t.ac) return false;
    if (pe != t.pe) return false;
    return true;
  }
};

int main() {
  while (true) {
    int M, T, P, R;
    cin >> M >> T >> P >> R;
    if (M == 0) break;
    Team team[T];
    rep(i, T) team[i].num = i + 1;
    rep(_, R) {
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      --t;
      --p;
      if (j == 0) {
        ++team[t].ac;
        team[t].pe += team[t].p[p] * 20 + m;
      } else {
        ++team[t].p[p];
      }
    }
    sort(team, team + T);
    rep(i, T) {
      if (i != 0) {
        cout << (team[i] == team[i - 1] ? '=' : ',');
      }
      cout << team[i].num;
    }
    cout << endl;
  }
}


Measured memory_bytes: 3084
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
//inline int in() { int x; scanf("%d",&x); return x;}
inline ll in() { ll x; cin >> x; return x; }
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("YES");}else{puts("NO");}
const int MX = 200005;

int main() {
  int n = in();
  vector<int> a; rep(i,n+1) a.pb(in());
  vector<int> b; rep(i,n) b.pb(in());
  ll ans = 0;
  // b[0] <= a[0]だったら勇者0はb[0](a[1]が勇者1に回る)
  // b[0] > a[0]だったら、
  // b[0] > a[0]+a[1]だったら勇者0はa[0]+a[1](0が勇者1に回る)
  // b[0] < a[0]+a[1]だったら勇者0はb[0](a[0]+a[1]-b[0]が勇者1に回る)
  rep (i,n) {// 勇者0から順に倒す
    if (b[i] >= a[i]) {
      ans += a[i];
      b[i] -= a[i];
      if (b[i] >= a[i+1]) {
        ans += a[i+1];
        a[i+1] = 0;
      } else {
        ans += b[i];
        a[i+1] -= b[i];
      }
    } else {
      ans += b[i];
    }
  }
//  int cnt = 0;
//  rep(i,n) if(i+1!=in()) cnt++;
//  if (cnt == 2 || cnt == 0) {
//    cout << "YES" << endl;
//  } else {
//    cout << "NO" << endl;
//  }
//  ll s = in();
//  ll x1 = 1e9, x2 = 1;
//  ll y2 = dup(s,x1), y1 = x1*y2-s;
//  vl ans;
//  ans.pb(0); ans.pb(0);
//  ans.pb(x1); ans.pb(y1);
//  ans.pb(x2); ans.pb(y2);
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 1400
#include <bits/stdc++.h>
#include <iostream>
#include <utility> // pair
#include <string>
#include <vector>
#define MAX_N 200010
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define FOR(i, a, b) for(ll i=a; i<b; i++)
#define REP(i, n) for(ll i=0; i<n; i++)
#define REV(i, n) for(ll i=n-1; i>=0; i--)
#define ALL(v) v.begin(), v.end()
#define pb push_back
const ll INF = 9e18;
ll n, k, p[MAX_N];
double expect[1010], expect_sum[MAX_N];

signed main(){
    cin >> n >> k;
    REP(i, n){
        cin >> p[i];
        p[i]--;
    }
    expect[0] = 1;
    REP(i, 1006){
        expect[i+1] = expect[i] + i + 2;
    }
    REP(i, 1005){
        expect[i] /= (double)(i + 1);
    }
    // REP(i, 1005) cout << expect[i] << endl;

    expect_sum[0] = expect[p[0]];
    REP(i, n){
        expect_sum[i+1] = expect_sum[i] + expect[p[i+1]];
    }

    // cout << expect_sum[n-1] << endl;
    double ret = -1;
    if(n-k > 0){
        REP(i, n-k){
            ret = max(ret, expect_sum[i+k] - expect_sum[i]);
        }
    }else{
        ret = expect_sum[n-1];
    }
    cout << setprecision(10) << ret << endl;
    
    return 0;
}


Measured memory_bytes: 3328
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int y1 = sc.nextInt();
    y1 /= 1000;
    int r = (y1 % 5);
    int[] ans = new int[3];
    ans[0] = -1;
    ans[1] = -1;
    ans[2] = -1;
    int flg = 0;
    for(int z = r; z <= n; z += 5) {
      int s = (y1 - z) / 5;
      int r2 = (s % 2);
      for(int y = r2; y <= (n - z); y += 2) {
        int x = (s - y) / 2;
        if((x + y + z) == n) {
          flg++;
          ans[0] = x;
          ans[1] = y;
          ans[2] = z;
          break;
        }
      }
      if(flg > 0) break;
    }
    System.out.println(ans[0] + " " + ans[1] + " " + ans[2]);
  }
}


Measured memory_bytes: 21844
N = int(input())
red = []
blue = []
for i in range(N):
    red.append(list(map(int, input().split())))
for j in range(N):
    blue.append(list(map(int, input().split())))

blue2 = sorted(blue, key=lambda x: x[0])
count = 0
for k in blue2:
    temp = -1
    pop = -1
    num = -1
    for l in red:
        num += 1
        if l[1] < k[1] and l[0] < k[0]:
            if l[1] > temp:
                temp = l[1]
                pop = num
    if temp >= 0:
        red.pop(pop)
        count += 1
print(count)

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

const int INF = 1e9 + 1;

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

  vector<int> dp(n, INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(begin(dp), end(dp), a[i]) = a[i];
  }

  int ans = distance(begin(dp), lower_bound(begin(dp), end(dp), INF));
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 3624
#include <stdio.h>

int main(void)
{
    int n,s=0,t,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i+=2){
        t=0;
        for(j=1;j<=i;j++)if(i%j==0)t++;
        if(t==8)s++;
    }
    printf("%d",s);
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(_i,_a,_n) for(int _i=_a;_i<=_n;++_i)
#define PER(_i,_a,_n) for(int _i=_n;_i>=_a;--_i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(_a) ({REP(_i,1,n) cout<<_a[_i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+50;
int n,k,a[N],b[N];
void upd(int l, int r) {
	l = max(1,l);
	++b[l],--b[r+1];
}
int main() {
	cin>>n>>k;
	REP(i,1,n) cin>>a[i];
	while (k--) {
		REP(i,1,n) b[i]=0;
		REP(i,1,n) upd(i-a[i],i+a[i]);
		int ok = 0;
		REP(i,1,n) { 
			a[i]=b[i]+a[i-1];
			if (a[i]!=n) ok=1;
		}
		if (!ok) break;
	}
	REP(i,1,n) printf("%d ",a[i]);hr;
}









Measured memory_bytes: 6088
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<string>

using namespace std;

int count2(long long x){//2で割れる回数を求める
    int c=0;
    while(x%2==0){
        x/=2;
        c++;
    }
return c;

}
int main(){


int N;
cin >>N;
long long  A;
long long Ans=0;
    for(int i=0;i<N;i++){
cin >> A;
Ans+=count2(A);

}
cout <<Ans<<endl;

 return 0;
}

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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vector<string> grid(n);
    rep(i,n) cin >> grid[i];

    //s -> even grid -> odd grid -> t
    mf_graph<int> g(n*m+2);
    int s = n*m, t = n*m+1;

    //s -> even  +  odd -> t
    rep(i,n) {
        rep(j,m) {
            if(grid[i][j] == '#') continue;
            int v = i*m + j;
            if((i+j) % 2 == 0) {
                g.add_edge(s,v,1);
            } else {
                g.add_edge(v,t,1);
            }
        }
    }

    //evem -> odd
    rep(i,n) {
        rep(j,m) {
            if((i+j) % 2 == 1 || grid[i][j] == '#') continue;
            int ve = i*m + j;
            if(i > 0 && grid[i-1][j] == '.') {
                int vo = (i-1)*m + j;
                g.add_edge(ve,vo,1);
            }
            if(j > 0 && grid[i][j-1] == '.') {
                int vo = i*m + j-1;
                g.add_edge(ve,vo,1);
            }
            if(i+1 < n && grid[i+1][j] == '.') {
                int vo = (i+1)*m + j;
                g.add_edge(ve,vo,1);
            }
            if(j+1 < m && grid[i][j+1] == '.') {
                int vo = i*m + j+1;
                g.add_edge(ve,vo,1);
            }
        }
    }

    cout << g.flow(s,t) << "\n";

    auto edges = g.edges();
    for (auto e : edges) {
        if (e.from == s || e.to == t || e.flow == 0) continue;
        int i0 = e.from / m, j0 = e.from % m;
        int i1 = e.to / m, j1 = e.to % m;

        if (i0 == i1 + 1) {
            grid[i1][j1] = 'v';
            grid[i0][j0] = '^';
        } else if (j0 == j1 + 1) {
            grid[i1][j1] = '>'; grid[i0][j0] = '<';
        } else if (i0 == i1 - 1) {
            grid[i0][j0] = 'v';
            grid[i1][j1] = '^';
        } else {
            grid[i0][j0] = '>'; grid[i1][j1] = '<';
        }
    }

    for (int i = 0; i < n; i++) {
        cout << grid[i] << "\n";
    }
    return 0;
}

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

using namespace std;
 
const int MOD=1e9+7;

int n,m, maxn=1001, maxm=1001;
vector<string> gr(maxn, string(maxm,'.'));
 
struct Pt{
    int i, j;
    Pt(int _i, int _j):i(_i),j(_j){}
    
};
 
bool isValid(Pt p){
    return 0<=p.i && p.i<n && 0<= p.j && p.j<m;
}
 
vector<Pt> dirs={Pt(-1,0),Pt(1,0),Pt(0,-1),Pt(0,1)};
 
vector<Pt> getNei(Pt p){
    vector<Pt> ans;
    for(auto pd:dirs){
        Pt p2(p.i+pd.i,p.j+pd.j);
        if(isValid(p2) && gr[p2.i][p2.j]=='.') ans.push_back(p2);
    }
    return ans;
}

vector<Pt> getJn(Pt p){
    vector<Pt> ans;
    for(int i=-2; i<=2; i++){
        for(int j=-2; j<=2; j++){
            if(!i && !j) continue;
            Pt p2(p.i+i,p.j+j);
            if(isValid(p2) && gr[p2.i][p2.j]=='.') ans.push_back(p2);
        }
    }
    return ans;
}


//const int MOD=998244353;


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int ch,cw,dh,dw;
    cin>>n>>m;
    cin>>ch>>cw;
    cin>>dh>>dw;
    
    ch--;
    cw--;
    dh--;
    dw--;
    
    for(int i=0; i<n; i++) cin>>gr[i];
    
    vector<vector<int>> dist(n,vector<int>(m,-1));
    
    //dist[ch][cw]=0;
    deque<pair<Pt,int>> dq;
    dq.push_front({Pt(ch,cw),0});
    while(dq.size()){
        auto cur=dq.front();
        dq.pop_front();
        auto p=cur.first;
        //cout<<p.i<<' '<<p.j<<' '<<cur.second<<'\n';
        //cout<<dist[p.i][p.j]<<'\n';
        
        if(dist[p.i][p.j]!=-1) continue;
        dist[p.i][p.j] =cur.second;
        for(auto p2:getNei(p)){
            dq.push_front({p2,dist[p.i][p.j]});
        }
        for(auto p2:getJn(p)){
            dq.push_back({p2,dist[p.i][p.j]+1});
        }
        
    }
    cout<<dist[dh][dw];
    
    
    
    return 0;
}

Measured memory_bytes: 9944
import random

NTEST = 26

D=int(input())

C=list(map(int,input().split()))

SL=list()
SL.append([0 for i in range(NTEST)])
for d in range(D):
    S=list(map(int,input().split()))
    SL.append(S)

def calcSat(test_applied):
    lastDay = [0 for i in range(NTEST)]

    sat = 0
    for d in range(1,D+1):
        tst = test_applied[d] - 1
        lastDay[tst] = d
        sat += SL[d][tst]
        for itst in range(NTEST):
            sat -= C[itst] * (d-lastDay[itst])
    return sat

satList = list()
satList.append(0)

def calcSat_with_memo(test_applied, lastDay):

    total_sat = 0
    for d in range(1,D+1):
        lastDay.append(list(lastDay[d-1]))
        sat = 0
        tst = test_applied[d] - 1
        lastDay[d][tst] = d
        sat += SL[d][tst]
        for itst in range(NTEST):
            sat -= C[itst] * (d-lastDay[d][itst])
        satList.append(sat)
        total_sat += sat

    return total_sat

def recalcSat_with_memo(total_sat_before, tday, t_from, t_to, test_applied, lastDay):

    d_total_sat = 0

    lastDay[tday][t_from -1]=lastDay[tday-1][t_from -1]
    d_sat=0
    d_sat += (SL[tday][t_to -1]-SL[tday][t_from -1])
    d_sat += C[t_to -1]*(tday - lastDay[tday][t_to -1])
    d_sat -= C[t_from -1]*(tday - lastDay[tday][t_from -1])

    lastDay[tday][t_to -1]=tday

    satList[tday]+= d_sat
    d_total_sat  += d_sat

    old_last = tday
    new_last = lastDay[tday][t_from -1]
    for d in range(tday+1,D+1):
        if test_applied[d] == t_from :
            break
        d_sat = C[t_from -1]*(- old_last + new_last)
        lastDay[d][t_from -1] = new_last
        satList[d]+= d_sat
        d_total_sat += d_sat

    old_last = lastDay[tday-1][t_to -1]
    new_last = tday
    for d in range(tday+1,D+1):
        if test_applied[d] == t_to :
            break
        d_sat = C[t_to -1]*(- old_last + new_last)
        lastDay[d][t_to -1] = new_last
        satList[d]+= d_sat
        d_total_sat += d_sat
            
    return d_total_sat


# sat = calcSat(tests)
# print(sat)


tests=list()
tests.append(0)
for d in range(D):
    tests.append((d % 26) +1)

lastDay = [[0 for i in range(NTEST)]]
# sat = calcSat(tests)
sat = calcSat_with_memo(tests,lastDay)
# print(sat)

for i in range(10000):
    day = random.randint(1,D) 
    new_tst = random.randint(1,NTEST)

    trial = list(tests)
    new_lastDay = list(lastDay)

    d_sat = recalcSat_with_memo(sat, day, tests[day], new_tst, trial, new_lastDay)

    if d_sat > 0:
        sat += d_sat
        tests[day] = new_tst
        lastDay = new_lastDay
        # print(day, new_tst, d_sat, sat, tests)

    
for d in range(1,D+1):
    print(tests[d])

Measured memory_bytes: 9788
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<ll,ll,ll> T;
const long long INF = 1LL<<60;
const int MOD = 1000000000+7;
#define rev(s) (string((s).rbegin(), (s).rend()))
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
// cout << fixed << setprecision(10) << ans << endl;
// *min_element(c + l, c + r) *max_element(c + l, c + r)
// int dx[8]={1,1,0,-1,-1,-1,0,1};
// int dy[8]={0,1,1,1,0,-1,-1,-1};
// int dx[4]={1,0,-1,0};
// int dy[4]={0,1,0,-1};
// struct Edge {
//   int to, id;
//   Edge(int to, int id): to(to), id(id) {}
// };

ll dp[3010][3010][4];

int field[3010][3010];

int seen[3010][3010];

int main() {
    int r,c,k;cin>>r>>c>>k;
    rep(i,k) {
        int a,b;ll d;
        cin>>a>>b>>d;
        a--;b--;
        field[a][b]=d;
    }
    queue<P> qu;
    qu.push(P(0,0));
    dp[0][0][1] = field[0][0];
    while(!qu.empty()) {
        int y = qu.front().first;
        int x = qu.front().second;
        qu.pop();
        // cout<<y<<" "<<x<<endl;
        if(x+1<=c-1) {
            rep(i,3){
                dp[y][x+1][i+1] = max(dp[y][x+1][i+1],max(dp[y][x][i+1],dp[y][x][i]+field[y][x+1]));
            }
            dp[y][x+1][0] = max(dp[y][x+1][0],dp[y][x][0]);
            seen[y][x+1]++;
            if(seen[y][x+1]==2 || y==0) qu.push(P(y,x+1));
        }
        if(y+1<=r-1) {
            rep(i,4) dp[y+1][x][0] = max(dp[y+1][x][0],dp[y][x][i]);
            rep(i,4) dp[y+1][x][1] = max(dp[y+1][x][1],dp[y][x][i]+field[y+1][x]);
            seen[y+1][x]++;
            if(seen[y+1][x]==2 || x==0) qu.push(P(y+1,x));
        }
    }
    ll ans = 0;
    rep(i,4) ans = max(ans,dp[r-1][c-1][i]);
    cout<<ans<<endl;
}

Measured memory_bytes: 356184
#include <iostream>
using namespace std;
 
int main(){
    int a, b, c;
cin >> a >> b >> c;
    if (a == c ){
       if ( a == b ){
        cout << "No" << endl;
       } else {
        cout << "Yes" << endl;
       }
    } else {
      if ( a == b || c == b){
        cout << "Yes" << endl;
      } else {
        cout << "No" << endl;
      }
    }
    return 0;
}


Measured memory_bytes: 256
N,M=map(int,input().split())
hs=input().split()

H=[0]                           #各丘の高さ(index1は文中の1)
for h in hs:
    H.append(int(h))


P=[[0]*1 for i in range(N+1)]   #各丘からの道 二次元配列 (index1は文中の1)
count=0
for p in range(len(P)):
    P[p][0]=p                   #P[][0] 各配列の要素1は自身の丘の数字
# print(P)

for m in range(M):
    pathfrom,pathto= map (int,input().split())
    P[pathfrom].append(H[pathto])
    P[pathto].append(H[pathfrom])
for i in range(1,len(P)):
    if len(P[i])==1:
        count+=1
    else:
        P[i][0]=H[i]
        _rP=P[i][1:]
        if P[i][0]>max(_rP):
            count+=1
print(count)

Measured memory_bytes: 36688
// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<pair<int, int>> v;
  for(int i = 0; i < n; i++) cin >> a[i] >> b[i], v.emplace_back(b[i], a[i]);
  sort(begin(v), end(v));

  ll sum = 0;
  for(int i = 0; i < n; i++) {
    sum += v[i].second;
    if(sum > v[i].first) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  
  return 0;
}


Measured memory_bytes: 4980
#ifdef ConanYu
#include "E:\yuzining\code\local.hpp"
#else
#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(...) do { } while(false)
void Main();
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie();
  Main();
  return 0;
}
#endif

const int N = 2e5 + 10, MOD = 1e9 + 7;

struct edge {
  int v, nxt;
} G[N << 1];

void add(int &x, int y) {
  x += y;
  if(x >= MOD) x -= MOD;
}

int h[N], tot, val[N], siz[N], fac[N << 1], inv[N << 1], n;

int C(int n, int m) {
  if(n < m) return 0;
  return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}

void addedge(int u, int v) {
  G[tot] = {v, h[u]}; h[u] = tot++;
  G[tot] = {u, h[v]}; h[v] = tot++;
}

int fpow(int a, int b) {
  int ans = 1;
  for(; b > 0; b >>= 1, a = 1ll * a * a % MOD) {
    if(b & 1) ans = 1ll * ans * a % MOD;
  }
  return ans;
}

void dfs(int u, int f) {
  val[u] = 1;
  siz[u] = 1;
  for(int i = h[u]; ~i; i = G[i].nxt) {
    const int v = G[i].v;
    if(v != f) {
      dfs(v, u);
      val[u] = 1ll * val[u] * val[v] % MOD * C(siz[u] + siz[v] - 1, siz[v]) % MOD;
      siz[u] += siz[v];
    }
  }
}

void dfs2(int u, int f) {
  if(u != 1) {
    int fa = 1ll * val[f] * fpow(val[u], MOD - 2) % MOD;
    fa = 1ll * fa * fpow(C(n - 1, siz[u]), MOD - 2) % MOD;
    val[u] = 1ll * val[u] * fa % MOD * C(n - 1, n - siz[u]) % MOD;
  }
  for(int i = h[u]; ~i; i = G[i].nxt) {
    const int v = G[i].v;
    if(v != f) {
      dfs2(v, u);
    }
  }
}

void Main() {
  memset(h, 0xff, sizeof h);
  fac[0] = 1;
  for(int i = 1; i < (N << 1); i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
  inv[(N << 1) - 1] = fpow(fac[(N << 1) - 1], MOD - 2);
  for(int i = (N << 1) - 2; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
  cin >> n;
  for(int i = 0; i < n - 1; i++) {
    int u, v; cin >> u >> v;
    addedge(u, v);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for(int i = 1; i <= n; i++) {
    cout << val[i] << "\n";
  }
}

Measured memory_bytes: 12800
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin>>N;
  int A=0;
  int M=N;
  for(int i=0;i<10;i++){
   A += M%10;
    M = M/10;
  }
  if( N%A==0) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
#include <utility>
using namespace std;

#define int long long
#define rep(i, n) for (int i=0;i<(int)(n);++i)
#define rep1(i, n) for (int i=1;i<=(int)(n);++i)
#define all(a) begin(a),end(a)

using vi = vector<int>;

const int inf = 1e9 + 5;

struct UF {
	vi data;
	void init(int n)
	{
		data.resize(n);
		fill(all(data), -1);
	}
	int root(int x)
	{
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	void unite(int x, int y)
	{
		x = root(x);
		y = root(y);
		if (x == y) return;
		if (size(x) < size(y)) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
	bool same(int x, int y)
	{
		return root(x) == root(y);
	}
	int size(int x)
	{
		x = root(x);
		return -data[x];
	}
	bool is_root(int x)
	{
		return x == root(x);
	}
};

int N, K, L;
map<pair<int, int>, int> m;

signed main()
{
	cin >> N >> K >> L;

	UF a, b;

	a.init(N);
	b.init(N);

	rep(i, K) {
		int p, q; cin >> p >> q;
		--p, --q;
		a.unite(p, q);
	}

	rep(i, L) {
		int p, q; cin >> p >> q;
		--p, --q;
		b.unite(p, q);
	}

	rep(i, N) {
		pair<int, int> p = make_pair(a.root(i), b.root(i));
		m[p]++;
	}

	rep(i, N) {
		pair<int, int> p = make_pair(a.root(i), b.root(i));
		printf("%lld%c", m[p], " \n"[i == N - 1]);
	}
}



Measured memory_bytes: 17632
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <cmath>
#define ll long long
#define LINF 0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
#define PI 3.14159265
using namespace std;
const int MAX = 1e5 + 10;
const ll MOD = 1e9 + 7;
int N, M, K, Q, L;
int jump[MAX][33];
int A[MAX];

int func(int i, int j) {
	if (j == 0) return jump[i][0];
	int &ret = jump[i][j];
	if (ret != -1) return ret;
	return ret = func(func(i, j - 1), j - 1);
}


int main()
{
	int i, j, k, a, b;
	memset(jump, -1, sizeof(jump));
	scanf("%d", &N);
	for (i = 1; i <= N; ++i) scanf("%d", A + i);
	scanf("%d%d", &L, &Q);
	for (i = 1; i <= N; ++i) {
		auto it = upper_bound(A + 1, A + 1 + N, A[i] + L);
		--it;
		jump[i][0] = it - A;
	}
	j = 0;
	while (N > (1 << j)) j++;
	for (i = 1; i <= N; ++i) func(i, j);
	for (i = 1; i <= Q; ++i) {
		scanf("%d%d", &a, &b);
		int days = 0;
		if (a > b) swap(a, b);
		for (k = j; k >= 0; --k) {
			if (jump[a][k] < b) {
				a = jump[a][k];
				days += (1 << k);
			}
		}
		printf("%d\n", days + 1);
	}
}

Measured memory_bytes: 14080
package main

import (
	"fmt"
)

func main() {
	var N int
	fmt.Scan(&N)
	fmt.Println(solve(N))
}

func solve(N int) int {
	if N%2 == 1 {
		return 0
	}
	return g1(N / 2)
}

func g1(n int) int {
	if n == 0 {
		return 0
	}
	return n/5 + g1(n/5)
}


Measured memory_bytes: 512
import numpy as np
M = 100002

if __name__ == "__main__":
  N = int(input())
  A = [int(x) for x in input().split()]
  Q = int(input())
  Num = np.zeros((M))
  Sum = sum(A)
  for i in range(N):
    Num[A[i]] += 1
  for i in range(Q):
    B, C = [int(x) for x in input().split()]
    if(Num[B] == 0):
      print(int(Sum))
    else:
      Sum = Sum + (C - B)*Num[B]
      Num[C] += Num[B]
      Num[B] = 0
      print(int(Sum))

Measured memory_bytes: 39024
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (typeof((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(typeof(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}



int H, W;
vector<string> field;

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

bool seen[30][30];

void dfs(pint x) {
    seen[x.FI][x.SE] = true;
    for (int i = 0; i < 4; ++i) {
        int nx = x.FI + dx[i];
        int ny = x.SE + dy[i];
        if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
            if (!seen[nx][ny] && field[nx][ny] != '#') {
                dfs(MP(nx, ny));
            }
        }
    }
}

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    while (cin >> W >> H) {
        if (W == 0) break;
        
        field.clear();
        for (int i = 0; i < H; ++i) {
            string str;
            cin >> str;
            field.PB(str);
        }
        
        pint start;
        for (int i = 0; i < H; ++i) 
            for (int j = 0; j < W; ++j) 
                if (field[i][j] == '@')
                    start = MP(i,j);
        
        memset(seen, 0, sizeof(seen));
        dfs(start);
        
        int res = 0;
        for (int i = 0; i < H; ++i)
            for (int j = 0; j < W; ++j) 
                if (seen[i][j]) ++res;
        
        cout << res << endl;
    }
    
    return 0;
}


Measured memory_bytes: 1244