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

// (a,b),(c,d)肝の距離を求める
double dist(double a,double b,double c,double d){
  return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}

// a番目の順列における経路の長さを求める
double route(int n,int a,vector<double> &x,
             vector<double> &y,vector<int> &perm){
  double sum = 0;
  for (int i=0; i<n-1; i++){
    sum += dist(x.at(perm.at(n*a+i)),y.at(perm.at(n*a+i)),
                x.at(perm.at(n*a+i+1)),y.at(perm.at(n*a+i+1)));
  }
  return sum;
}

// n,x,y,perm(長さnの順列)を貰って、経路の長さの平均値を求める
double solve(int n,vector<double> &x,vector<double> &y,vector<int> &perm){
  int nfact = perm.size() / n;
  double sum = 0;
  for (int i=0; i<nfact; i++){
    sum += route(n,i,x,y,perm);
  }
  return sum/nfact;
}

int main() {
  cout <<fixed <<setprecision(8);
  int n; cin >>n;
  vector<double> x(n),y(n);
  vector<int> v(n);
  vector<int> perm;
  for (int i=0; i<n; i++) cin >>x.at(i) >>y.at(i);
  for (int i=0; i<n; i++) v.at(i) = i;
  
  do { for (int x : v) perm.push_back(x);
  } while (next_permutation(v.begin(), v.end()));
  // permには長さnの順列が全て入っている
  
  double ans = solve(n,x,y,perm);
  
  cout <<ans <<endl;
}


Measured memory_bytes: 2420
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<queue>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll m = (1e+9) + 7;

int main() {
	int r;
	cin >> r;
	cout << 3*r*r;
	return 0;
}

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

using namespace std;

typedef long long ll;

#define sz(x) ((int)(x).size())
#define in(x) int x;cin>>x
#define inn(x, y) int x,y;cin>>x>>y
#define innn(x, y, z) int x,y,z;cin>>x>>y>>z
#define bit(n) (1<<(n))

#define out(x) cout<<(x)<<endl;
#define outt(x,y) cout<<(x)<<" "<<(y)<<endl;
#define err(x) cerr<<(x)<<endl;
#define errr(x,y) cerr<<(x)<<" "<<(y)<<endl;
#define errrr(x,y,z) cerr<<(x)<<" "<<(y)<<" "<<(z)<<endl;

#define rep(i,n) for (int i=0;i<(n);i++)
#define repp(i,n) for (int i=1;i<=(n);i++)
#define reppp(j, i, n) for (int j=i+1; j<n; j++)

#define lim5 200010
#define lim9 1000000010

#define deg(rad) (((rad)/2/M_PI)*360)
#define mid(min, max) ((min + max) / 2)

double distance(int xi, int yi, int xj, int yj) {
    double dx = xi - xj;
    double dy = yi - yj;
    return pow(dx * dx + dy * dy, 0.5);
}

int main() {
    in(n);
    int x[1000] = {};
    int y[1000] = {};
    rep(i, n) {
        cin >> x[i] >> y[i];
    }
    double ans = 0;
    rep(i, n) {
        reppp(j, i, n) {
            ans += distance(x[i], y[i], x[j], y[j]) * 2 / n;
        }
    }
    cout << fixed << setprecision(10) << ans << endl;
    return 0;
}


Measured memory_bytes: 384
K = int(input())
A = list(map(int, input().split()))

def winner_count(N, A=A):
    for a in A:
        N = a*(N//a)
    return N

def bisect_left(func, l, r, target):
    # n>=i ⇒ func(n) <= targetを満たす最小のi
    # l=1, r=sum(A)+2
    # func(l)<= target, target<=func(r)
    if l==r:
        return 0
    if l+1==r:
        if func(l)< target:
            return r
        else:
            return l
    c = (l+r)//2
    if target <= func(c):
        return bisect_left(func, l, c, target)
    else:
        return bisect_left(func, c, r, target)

def bisect_right(func, l,r, target):
    if l==r:
        return 0
    if l+1==r:
        if target < func(r):
            return l
        else:
            return r
    c = (l+r)//2
    if target < func(c):
        return bisect_right(func, l, c, target)
    else:
        return bisect_right(func, c, r, target)

_min = bisect_left(winner_count, 1, sum(A)+2, 2)
_max = bisect_right(winner_count, 1, sum(A)+2, 2)


if _min > _max:
	print(-1)
else:
	print(_min, _max)

Measured memory_bytes: 14428
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
  int n=s.nextInt();
 int tako[]=new int[n];
  for(int i=0;i<n;i++){
  tako[i]=s.nextInt();
  }
  int cnt=0;
 r: for(int i=n-1;i>-1;i--){
  for(int k=i-1;k>-1;k--){
    if(i<=0)
      break r;
  cnt+=tako[i]*tako[k];
  }
  }
  System.out.println(cnt);
}
}

Measured memory_bytes: 21844
module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let vs = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun v -> v in
  let odds =
    List.sort (fun (_, x) (_, y) -> compare y x) @@
    IntMap.bindings @@
    Array.fold_left (fun m v ->
      IntMap.add v (1 + try IntMap.find v m with Not_found -> 0) m) IntMap.empty @@
    Array.init (n / 2) @@ fun i -> vs.(2 * i + 1) in
  let evens =
    List.sort (fun (_, x) (_, y) -> compare y x) @@
    IntMap.bindings @@
    Array.fold_left (fun m v ->
      IntMap.add v (1 + try IntMap.find v m with Not_found -> 0) m) IntMap.empty @@
    Array.init (n / 2) @@ fun i -> vs.(2 * i) in
  let v, m, m' =
    match odds with
    | [(v, n)] -> v, n, 0
    | (v, n) :: (_, n') :: _ -> v, n, n' in
  let v_, m_, m_' =
    match evens with
    | [(v, n)] -> v, n, 0
    | (v, n) :: (_, n') :: _ -> v, n, n' in
  Printf.printf "%d\n" @@ n - (if v = v_ then max (m + m_') (m' + m_) else m + m_)


Measured memory_bytes: 13344
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000000000000;
constexpr int mod=1000000007;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
void init_fact(){
    kaijo[0]=1;
    for(int i=1;i<=2000000;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    int memo=modpow(a,b/2);
    return memo*memo%mod;
}
int comb(int a,int b){
    if(!kaijo[0])init_fact();
    return kaijo[a]*modpow(kaijo[a-b],mod-2)%mod*modpow(kaijo[b],mod-2)%mod;
}
int inv(int x){
    x=modpow(x,mod-2);
    return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
    double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
    double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
    double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
    double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
    return tc*td<0&&ta*tb<0;
}
int n,a[4010],dp[2010][2010],w[2010],b[2010],sumw[4010][2010],sumb[4010][2010];
char c[4010];
signed main(){
    cin>>n;
    rep(i,2*n){
        cin>>c[i]>>a[i];
        a[i]--;
        (c[i]=='W'?w:b)[a[i]]=i;
    }
    for(int i=2*n-1;i>=0;i--){
        rep(j,n){
            sumw[i][j]=sumw[i+1][j];
            sumb[i][j]=sumb[i+1][j];
        }
        for(int j=a[i];j<n;j++)(c[i]=='W'?sumw:sumb)[i][j]++;
    }
    rep(i,n+1){
        rep(j,n+1){
            dp[i][j]=INF;
        }
    }
    dp[0][0]=0;
    rep(i,n+1){
        rep(j,n+1){
            if(i<n){
                int cntw=(i?sumw[w[i]+1][i-1]:0);
                int cntb=(j?sumb[w[i]+1][j-1]:0);
                chmin(dp[i+1][j],dp[i][j]+w[i]+cntw+cntb-(i+j));
            }
            if(j<n){
                int cntw=(i?sumw[b[j]+1][i-1]:0);
                int cntb=(j?sumb[b[j]+1][j-1]:0);
                chmin(dp[i][j+1],dp[i][j]+b[j]+cntw+cntb-(i+j));
            }
        }
    }
    cout<<dp[n][n]<<endl;
    return 0;
}


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

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

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

#[allow(unused_imports)]
use std::io::Write;
use std::collections::HashMap;
type I = usize;

pub fn combination(n: usize, mut k: usize) -> usize {
    assert!(n >= k);
    if k * 2 > n {
        k = n - k
    }
    let mut ans = 1;
    for d in 1..k + 1 {
        ans *= n + 1 - d;
        ans /= d;
    }
    ans
}

fn main() {
    input!{
      n: usize,
      a: usize,
      b: usize,
      mut vs: [usize; n],
    }
    let mut all = HashMap::new();
    for &v in vs.iter() {
        let e = all.entry(v).or_insert(0);
        *e += 1;
    }

    vs.sort();
    vs.reverse();
    let mut sum = 0;

    let mut used = HashMap::new();
    for i in 0..a {
        sum += vs[i];
        let e = used.entry(vs[i]).or_insert(0);
        *e += 1;
    }
    println!("{}", (sum as f64)/(a as f64));


    if used.keys().len() == 1 {
        let k = used.keys().nth(0).unwrap();
        let mut ans = 0;
        for i in a..b+1 {
            if i > all[k] {
                break;
            }
            let num = combination(all[k], i);
            ans += num;
        }
        println!("{}", ans);
    } else {
        let mut ans = 1;
        for (k, &v) in used.iter() {
            ans *= combination(all[k], v);
        }
        println!("{}", ans);
    }
}


Measured memory_bytes: 4352
N, K = map(int, input().split())
Aall = [0]*N
count=0
for i in range(K):
  d = int(input())
  A = list(map(int, input().split()))
  for j in range(d):
    Aall[A[j]-1]+= 1
for k in range(N):
  if Aall[k] == 0:
    count+=1
print(count)


Measured memory_bytes: 68044
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long long int N, K, min;
  cin >> N >> K;
  min=N;
  
  if(N>=K){
    min = N%K;
    N=min;
  }
  if(abs(N-K) < min){
    min = abs(N-K);
  }

  cout << min << endl;
}

Measured memory_bytes: 256
#key => [route0, route1]
hash = {"A"=>["X", "Y"],
		"W"=>["B", "Y"],
		"X"=>["", "Z"],
		"Y"=>["X", ""],
		"Z"=>["W", "B"],
		"B"=>["Y", "X"]
}

while (input = $stdin.gets.chomp) != "#" do
	pos = "A"
	input.split("").map(&:to_i).each do |i|
		if hash[pos][i] != ""
			pos = hash[pos][i]
		else
			break
		end
	end
	
	if pos == "B"
		puts "Yes"
	else
		puts "No"
	end
end

Measured memory_bytes: 6096
n = int(input())
a = list(map(int, input().split()))
count = 0
flag = 0

while(1):
  for i in range(len(a)):
    if a[i] % 2 != 0:
      flag = 1
    a[i] = a[i]/2
  if flag == 1:
    break
  count += 1

print(count)

Measured memory_bytes: 3060
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
    ll n, *a;   cin >> n;
    a = new ll[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    
    ll ans = 0;

    for (int i = 0; i < n-1; i++) {
        if (a[i+1] >= a[i])
            continue;
        ans += a[i]-a[i+1];
        a[i+1] = a[i];
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 5156
from random import choice
from random import random
from time import time


class Optimizer:
    def __init__(self, C, S):
        self.C = C
        self.S = S
        X = []
        L = [-1] * 26
        for i in range(365):
            max_diff = -10**10
            best_j = 0
            for j in range(26):
                memo = L[j]
                L[j] = i
                diff = self.S[i][j] - \
                    sum([self.C[jj] * (i - L[jj]) for jj in range(26)])
                if diff > max_diff:
                    max_diff = diff
                    best_j = j
                L[j] = memo
            L[best_j] = i
            X.append(best_j)
        self.X = X

    def calc_score(self):
        score = 0
        L = [-1 for j in range(26)]
        A = [0 for j in range(26)]
        for i in range(365):
            score += self.S[i][self.X[i]]
            A[self.X[i]] += (i - L[self.X[i]]) * \
                (i - L[self.X[i]] - 1) // 2
            L[self.X[i]] = i
        for j in range(26):
            A[j] += (365 - L[j]) * (365 - L[j] - 1) // 2
            score -= self.C[j] * A[j]
        return score

    def optimize(self, temp, threp, interval, start_time, lim=1.8):
        max_score = self.calc_score()
        while True:
            t = (time() - start_time) / 2
            if random() < threp:
                i = choice(range(365))
                j = choice(range(26))
                memo = self.X[i]
                self.X[i] = j
            else:
                a = choice(range(interval))
                i0 = choice(range(365 - a))
                i1 = i0 + a
                self.X[i0], self.X[i1] = self.X[i1], self.X[i0]
            new_score = self.calc_score()
            if new_score > max_score:
                max_score = new_score
            elif random() < pow(2.7, (new_score - max_score) / pow(2000, 1 - t)):
                max_score = new_score
            else:
                try:
                    self.X[i0], self.X[i1] = self.X[i1], self.X[i0]
                except BaseException:
                    self.X[i] = memo
            if time() - start_time > lim:
                break
        return max(max_score + 10**6, 0)


if __name__ == "__main__":
    d = int(input())
    *C, = map(int, input().split())
    S = [list(map(int, input().split())) for i in range(d)]
    opt = Optimizer(C, S)
    opt.optimize(400, 0.8, 10, time(), lim=1.8)
    for x in opt.X:
        print(x + 1)


Measured memory_bytes: 89644
#include<bits/stdc++.h>
using namespace std;
#define i64 long long int
#define ran 101111
i64 pos[ran], cnt[ran], s;
int n;
int o[ran], lo;
i64 AB(i64 x){return x>=0?x:-x;}
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> s;
	for(int i=0; i<n; i++)
		cin >> pos[i] >> cnt[i];
	int L = 0, R = n-1;
	i64 totLef = cnt[L], totRig = cnt[R];
	while(true){
		if(pos[R] < s){
			for(int i=L; i<=R; i++)
				o[lo++] = i;
			break;
		}
		if(pos[L] > s){
			for(int i=R; i>=L; i--)
				o[lo++] = i;
			break;
		}
		if(totLef >= totRig){
			totLef += totRig;
			o[lo++] = R;
			R --;
			totRig = cnt[R];
		}else{
			totRig += totLef;
			o[lo++] = L;
			L++;
			totLef = cnt[L];
		}
	}
	reverse(o,o+lo);
	i64 ret = 0;
	for(int i=0; i<lo; i++){
		ret += AB(pos[o[i]] - s);
		s = pos[o[i]];
	}
	cout << ret << endl;
	return 0;
}


Measured memory_bytes: 2176
N = int(input())
A = list(map(int, input().split()))

t = 10**18
if 0 in A:
    print(0)
else:
    sum = A[0]
    flag = True
    for i in range(1, N):
        sum = sum * A[i]
        if sum > t:
            flag = False
            break
    if flag:
        print(sum)
    else:
        print(-1)


Measured memory_bytes: 21796
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
ll d1[9] = {1, 1, 1, 0, 0, 0, -1, -1, -1};
ll d2[9] = {1, 0, -1, 1, 0, -1, 1, 0, -1};

int main(){
    ll H, W, N;
    cin >> H >> W >> N;
    vector<ll> ans(10, 0);
    ans[0] = (H - 2) * (W - 2);
    set<pair<ll, ll>> st;
    rep(i, 0, N){
        ll th, tw;
        cin >> th >> tw;
        rep(j, 0, 9){
            ll h = th + d1[j];
            ll w = tw + d2[j];
            if(h < 2 || h > H-1) continue;
            if(w < 2 || w > W-1) continue;
            ll cnt = 0;
            rep(i, 0, 9){
                ll nh = h + d1[i];
                ll nw = w + d2[i];
                if(st.count({nh, nw}) > 0) cnt++;
            }
            ans[cnt]--;
            ans[cnt+1]++;
        }
        st.insert({th, tw});
    }
    rep(i, 0, 10) cout << ans[i] << endl;
    return 0;
}

Measured memory_bytes: 6528
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
using namespace std;
 
int main(){ 
        int h, w;                                     
        while(1){
                cin >> h >> w;
                if(h == 0 && w == 0)break;
                for(int i = 0; i < h; i++){
                        for(int j = 0; j < w; j++){
                                cout << '#';
                        }      
                        cout << endl;
                }
                cout << endl;
        }
}

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

namespace AtCoder.Contests.ABC164
{
    /// <summary>
    /// 
    /// </summary>
    static class B
    {
        static void Main(string[] args)
        {
            var a = NextInt();
            var b = NextInt();
            var c = NextInt();
            var d = NextInt();

            Console.WriteLine((c + b - 1) / b <= (a + d - 1) / d ? "Yes" : "No");
        }

        #region Console
        static int _index = 0;
        static string[] _buffer;

        static string Next()
        {
            if (_buffer == null || _index >= _buffer.Length)
            {
                _buffer = Console.ReadLine().Split();
                _index = 0;
            }

            return _buffer[_index++];
        }
        static int NextInt()
        {
            return int.Parse(Next());
        }
        static long NextLong()
        {
            return long.Parse(Next());
        }
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        static int[] ReadIntArray(int n)
        {
            var a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadInt();
            }
            return a;
        }
        static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        static long[] ReadLongArray(int n)
        {
            var a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadLong();
            }
            return a;
        }
        static List<T> ReadObjects<T>(int n, Func<int[], T> creator)
        {
            var l = new List<T>(n);

            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                l.Add(creator(a));
            }

            return l;
        }

        static int[,] ReadMatrix(int n, int m)
        {
            var r = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                for (int j = 0; j < m; j++)
                {
                    r[i, j] = a[j];
                }
            }
            return r;
        }
        #endregion
    }
}


Measured memory_bytes: 27364
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << (x) << endl;
using namespace std;
using ll = long long;
using pii = pair<int,int>;
int main(){
    int d;
    cin >> d;
    string ans = "Christmas";
    cout << ans;
    rep(i,25-d){
        cout << " Eve";
    }
    cout << endl;
}

Measured memory_bytes: 256
n = int(input())
c = 0
for i in range(n):
  d = list(map(int,input().split()))
  if d[0] == d[1]:
    c += 1
  else:
    c = 0
  if c == 3:
    print("Yes")
    exit()
  if i == n-1:
    print("No")

Measured memory_bytes: 9168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 双方向連結リスト構造体
struct list {
    int key;
    struct list *prev;
    struct list *next;
};

struct list *first;
struct list *last;

void init(void)
{
    first = NULL;
    last = NULL;
}

// 連結リストの先頭からキー値を出力する
void disp(void)
{
    struct list *p = first;
    
    if ( p == NULL ) {
        return;
    }

    while ( 1 ) {
        printf("%d", p->key);
        if ( p == last ) {
            printf("\n");
        } else {
            printf(" ");
        }
        if ( p->next == NULL ) {
            break;
        }
        p = p->next;
    }
}

// 連結リストの先頭にキー x を持つ要素を継ぎ足す。
void insert(int x)
{
    struct list *li;

    li = (struct list*)malloc(sizeof(struct list));
    li->key = x;
    li->prev = NULL;
    li->next = NULL;

    //リスト空？
    if ( first == NULL ) {
        first = li;
        last = li;
    } else {
        first->prev = li;
        li->next = first;
        first = li;
    }
}

// リストの先頭の要素を削除する。
void deleteFirst(void)
{
    struct list *p;
    
    if ( first == last ) {
        free(first);
        init();
    } else {
        p = first->next;
        p->prev = NULL;
        free(first);
        first = p;
    }
}

// リストの末尾の要素を削除する。
void deleteLast(void)
{
    struct list *p;
    
    if ( first == last ) {
        free(last);
        init();
    } else {
        p = last->prev;
        p->next = NULL;
        free(last);
        last = p;
    }
}


// キー x を持つ最初の要素を連結リストから削除する。
// そのような要素が存在しない場合は何もしない。
void delete(int x)
{
    struct list *p;
    struct list *p1;
    struct list *p2;
    
    p = first;
    
    while( 1 ) {
        if ( p->key == x ) {
            // リストの位置ごとの処理
            if ( p == first ) {
                deleteFirst();
            } else if ( p == last ) {
                deleteLast();
            } else {
                // ポインタの繋ぎかえ
                p1 = p->prev;
                p1->next = p->next;
                p2 = p->next;
                p2->prev = p1;
                free(p);
            }
            break;
        }
        if ( p->next == NULL ) {
            break;
        }
        p = p->next;
    }
}


int main(void)
{
    int n;
    char line[256];
    char *tp;

    scanf("%d\n", &n);

    init();

    for ( int i = 0; i < n; i++ ) {
        memset(line ,'\0', sizeof(line));
        fgets(line, sizeof(line), stdin);
        tp = strtok(line, " \n");
        if ( strcmp(tp, "insert") == 0 ) {
            tp = strtok(NULL, "\n");
            insert(atoi(tp));
        } else if ( strcmp(tp, "deleteFirst") == 0 ) {
            deleteFirst();
        } else if ( strcmp(tp, "deleteLast") == 0 ) {
            deleteLast();
        } else {
            tp = strtok(NULL, "\n");
            delete(atoi(tp));
        }
    }

    disp();

    return 0;
}


Measured memory_bytes: 32964
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int MAxn = 2e5 + 5;
const int MA_N = 5e3 + 5;

int main(){
	int a, b, c, d;
	while(cin >> a >> b >> c >> d){
	int m = max(a, c);
	int n = min(b, d);
	int ans = n - m;
	if(ans < 0) printf("0\n");
	else cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#include <vector>
#include <queue>
#include <functional>
#include <random>
#include <ctime>

using namespace std;
#define RN cout << "\r\n";
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define REP(i, n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef long long int ll;
typedef pair<ll, ll> P;

// combination
const int MAX = 510000;
const int MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    int a;
    cin >> a;
    if (a % 4 == 0) {
        cout << "Yes";
        return 0;
    }
    REP(i, a / 4) {
        if ((a - 4 * i) % 7 == 0) {
            cout << "Yes";
            return 0;
        }
    }
    cout << "No";
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.math.*;
public class Main {
	static int counter = 0;
	static int st = -1;
	static int nd = -1;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		String a = "";
		String b = "";
		for(int i = 0; i < n; i++) a += in.nextInt() + "";
		for(int i = 0; i < n; i++) b += in.nextInt() + "";
		int[] cur = new int[n];
		boolean[] check = new boolean[n];
		count(cur, n, check, 0, a, b);
		if(nd == -1) nd = st;
		System.out.println(Math.abs(st - nd));
	}
	public static void count(int[] cur, int n, boolean check[], int index, String a, String b) {
		if(index == n) {
			counter += 1;
			String possibility = "";
			for(int i = 0; i < n; i++) {
				possibility += cur[i];
			}
			if((possibility.equals(a)) || (possibility.equals(b))) {
				if(st == -1) {
					st = counter;
				}else {
					nd = counter;
				}
			}
			return;
		}
		for(int i = 1; i <= n; i++) {
			if(check[i - 1] == false) {
				check[i - 1] = true;
				cur[index] = i;
				count(cur, n, check, index + 1, a, b);
				check[i - 1] = false;
			}
		}
	}
}

Measured memory_bytes: 41800
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix

# strlist = "abcdefghijklmnopqrstuvwxyz"
k = int(input())

que = deque(range(1,10)) 
for _ in range(k):
    tmp = que.popleft()
    if tmp % 10 !=0:
        val = tmp*10 + (tmp%10) - 1
        que.append(val)
    val = tmp*10 + (tmp%10)
    que.append(val)
    if tmp%10 != 9:
        val = tmp*10 + (tmp%10) + 1
        que.append(val)

ans = tmp
print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

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

int main(){
    int64_t n,a,b;
    cin>>n>>a>>b;
    int64_t p,q;
    p=n/(a+b);
    q=n%(a+b);
    if(q>a) q=a;
    cout<<a*p+q<<endl;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <bits/stdc++.h>
 
using namespace std;
typedef long long int ll;
 
char str[12345];
 
int main()
{
  scanf("%s",&str);
  int n=strlen(str);
  ll ret=0;
  for(int S=0;S<1<<(n-1);S++)
  {
    ll now=0;
    ll sum=0;
    for(int i=0;i<n;i++)
    {
      now*=10LL;
      now+=str[i]-'0';
      if(i<n-1&&(S>>i&1))
      {
        sum+=now;
        now=0;
      }
    }
    sum+=now;
    ret+=sum;
  }
  printf("%lld\n",ret);
  return 0;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
using namespace std;

int main()
{
    const char *Yes = "Yes";
    const char *No = "No";

    char N[3];

    if (scanf("%s", N) < 1) return 0;

    printf("%s\n", N[0] == '9' || N[1] == '9' ? Yes : No);
    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int n;
	cin >> n;
	int l[2 * n];
	for(int i= 0; i < 2 * n; i++){
		cin >> l[i];
	}
	sort(l, l + 2 * n);

	int ans = 0;
	for(int i = 0; i < n; i ++){
		ans += l[2 * i];
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

void make_table(VVI cost, vector<vector<PII> > &adjacent, int n, int ex_num); 
void dijk(VVI cost, vector<vector<PII> > &adjacent, int n, int ex_num); 


int main(void) {
    while(1) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        VVI cost(n, VI(n + 1));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n + 1; j++) {
                cin >> cost[i][j];
            }
        }

        int ex_num = pow(2, n);
        vector<vector<PII> > adjacent(ex_num);
        make_table(cost, adjacent, n, ex_num);
        dijk(cost, adjacent, n, ex_num);
    }
    return 0;
}

void make_table(VVI cost, vector<vector<PII> > &adjacent, int n, int ex_num) {
    for (int i = 0; i < ex_num; i++) {
        for (int j = 0; j < n; j++) {
            if ((i & (1 << j)) != 0) {
                continue;
            }
            int min_cost = cost[j][0];
            for (int k = 0; k < n; k++) {
                if ((i & (1 << k)) != 0) {
                    min_cost = min(min_cost, cost[j][k + 1]); 
                }
            }
            int new_node = (i | (1 << j));
            adjacent[i].push_back(make_pair(min_cost, new_node));
        }
    }
    return;
}

void dijk(VVI cost, vector<vector<PII> > &adjacent, int n, int ex_num) { 
    VI answer(ex_num, INF);
    priority_queue<PII> pq;
    pq.push(make_pair(0, 0));

    while(!pq.empty()) {
        int cost = -pq.top().first;
        int node = pq.top().second;
        pq.pop();
        for (int i = 0; i < (int)adjacent[node].size(); i++) {
            int to_cost = adjacent[node][i].first;
            int to_node = adjacent[node][i].second;
            if (cost + to_cost < answer[to_node]) {
                pq.push(make_pair(-(cost + to_cost), to_node));
                answer[to_node] = cost + to_cost;
            }
        }
    }
    cout << answer[ex_num - 1] << endl;
    return; 
}

Measured memory_bytes: 12316
#include<iostream>
using namespace std;
long pow(long x, long n, long m){
  if(n == 0)
    return 1;
  if(n % 2 == 0)
    return pow(x * x % m, n / 2, m);
  else
    return x * pow(x, n - 1, m) % m;
}

int main(){
  long n,k;
  cin>>n>>k;
  long ans,mod;
  ans=0;
  mod=1000000007;
  long a[k+1];
  for(long l=k; l>0; l--){
    long x=pow(k/l,n,mod);
    for (long d=2;d<k/l+1;d++){
      x-=a[d*l];
      x%=mod;
    }
    
    a[l]=x;

  }
  for (long i=1; i<k+1; i++){
    ans+=a[i]*i;
    ans%=mod;
  }

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



Measured memory_bytes: 3876
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = ll(1e9+7);

int main() {
    ll n, dp[105][4][4][4]{};
    cin >> n;
    dp[0][3][3][3] = 1;
    for(int m=0;m<n;m++){
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                for(int l=0;l<4;l++){
                    // AGC. ACG. GAC.
                    if(j==0 && i==1 && l==2)continue;
                    if(j==0 && i==2 && l==1)continue;
                    if(j==1 && i==0 && l==2)continue;
                    // AG.C
                    if(k==0 && j==1 && l==2)continue;
                    // A.GC
                    if(k==0 && i==1 && l==2)continue;
                    dp[m+1][l][i][j] += dp[m][i][j][k];
                    dp[m+1][l][i][j] %= MOD;
                }
            }
        }
    }
    }
    ll ans = 0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            for(int k=0;k<4;k++){
                ans += dp[n][i][j][k];
                ans %= MOD;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

using LL = long long;

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

    int N;
    cin >> N;

    int H[N + 1];
    for (int i = 1; i <= N; i++) {
        cin >> H[i];
    }

    int count = 1;
    int flag = 0;
    for (int i = 2; i <= N; i++) {
        for (int j = 1; j < i; j++) {
            if (H[j] > H[i]) {
                flag = 1;
                break;
            }
        }

        if (flag == 0) {
            count++;
        }
        flag = 0;
    }

    cout << count << '\n';

    return 0;
}

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

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;
const ll INF = 1LL<<60;
//int INF = numeric_limits<int>::max();

int main(int, char**) {
	int n; cin >> n;
	string s; cin >> s;
	if (n%2==1) {
		cout << "No" << endl;
		return 0;
	}
	for (int i=0; 2 * i < n; i++) {
		if (s[i]!=s[i + n/2]) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}

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

int main() {
	//cout<<"GfG!";
	int x, k;
	cin>>x;
	if (360 % x == 0)
	    k = 360 / x;
	else
	    k = (360 / (360 % x)) * (360/x);
	cout<<k;
	return 0;
}

Measured memory_bytes: 3548
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long long A,C,D,N,P,T0,T1,X,Y;
  cin >> N >> P;
  A=C=D=T0=T1=X=Y=0;
  for (long long i=0;i<N;i++) {
    cin >> A;
	if (A%2==0) {X++;} else {Y++;}
  }
  T0=0;T1=1;
  if (Y==0 && P==0) {T0++;}
  while (Y>0 && P<=Y) {
    C=Y,D=P;
	for (long long i=1;i<P;i++) {
	  C*=Y-i;D*=P-i;
      if (C%2==0 && D%2==0) {C=C/2;D=D/2;}
      if (C%3==0 && D%3==0) {C=C/3;D=D/3;}
      if (C%5==0 && D%5==0) {C=C/5;D=D/5;}
	}
	if (D==0) {C=D=1;}
	T0+=C/D;
	P+=2;
  }
  for (long long i=0;i<X;i++) {T1*=2;}
  T0*=T1;
  cout << T0 << endl;
  
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long       ll;
typedef pair<int,int>   ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef set<int>        si;
typedef set<ii>         sii;

#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define clr(x,v) memset(x,v,sizeof x)
#define forn(i,a,b) for(int i=a;i<b;i++)
#define re return

template<typename T> T sgn (T x)      {re x<0? -1:(x>0? 1:0); }
template<typename T> T gcd (T a, T b) {re a ? gcd (b % a, a) : b; }
template<typename T> T abs (T x)      {re x < 0 ? -x : x; }
template<typename T> T sqr (T x)      {re x*x;}

#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define popcount __builtin_popcount
#define mp make_pair
#define eb emplace_back
#define emp emplace
#define ss second
#define ff first
#define endl "\n"
#define PI 3.14159265359
#define MOD ((int)1e9+7)
#define N ((int)2e5+123)

void reads(string& x){
    char kk[N];
    scanf("%s",kk);
    x = kk;
}
int n, m, a, b;
int ans;
int v[N];
vi g[N];
void solve(){
    scanf("%d %d", &n, &m);
    forn(i,1,n+1)scanf("%d",v+i);
    forn(i,0,m){
        scanf("%d %d", &a, &b);
        g[a].eb(b);
        g[b].eb(a);
    }
    forn(i,1,n+1){
        if(sz(g[i])==0)ans++;
        else{
            int pode=1;
            for(int x:g[i]){
                if(v[x]>=v[i])pode=0;
            }
            ans+=pode;
        }
    }
    printf("%d\n",ans);
}
int main(){_
    int t=1;// scanf("%d",&t);
    while(t--){
        solve();
    }
}

Measured memory_bytes: 11684
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<long long, long long> pll;
#define rep(i, a, b) for(int i = a; i < b; i++)

int main(){
    double a, b, x; cin >> a >> b >> x;

    x = x / a;
    //cout << x << endl;
    double ub = 90, lb = 0;
    double e = 1e-7;

    while(ub - lb >= e){
        //cout << lb << " " << ub << endl;
        double m = (ub + lb )/ 2;
        double theta = m / 360.0 * 2 * M_PI;
        double h = b * cos(theta);
        //cout << "h:" << h << " a" << a * sin(theta) << endl;
        double sq;
        if(a * sin(theta) >= h){
            sq = b * b / sin(theta) * cos(theta) / 2;
        }else{
            double sb = (h - a * sin(theta)) / cos(theta);
            sq = (b + sb) / 2 * a;
        }
        if(sq > x) lb = m;
        else ub = m;
        //cout << "sq " << sq << endl;
    }
    cout << setprecision(10) << fixed <<  lb << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	
	string s;
	cin >> s;
	ll n = s.size();
	ll a[n];
	zep(i, 0, n){
		a[i] = s[i]-'0';
	}
	//printa(a, 0, n-1)
	ll dp[n+1][2];
	dp[0][0] = 0;
	dp[0][1] = INF;
	zep(i, 0, n){
		dp[i+1][0] = min(dp[i][0]+a[i], dp[i][1]+a[i]+1);
		dp[i+1][1] = min(dp[i][0]+10-a[i], dp[i][1]+(10-a[i])-1);
	}
	print(min(dp[n][0], dp[n][1]+1))
	return 0;
}

Measured memory_bytes: 24860
import copy

N, W = map(int, input().split())
wvl = []
first_w, v = map(int, input().split())
wvl.append((first_w,v))
for _ in range(N-1):
    w,v = map(int, input().split())
    # wvl.append((w-first_w,v))
    wvl.append((w,v))

w_set_res = set()
w_set = set()
w_set_res.add(0)
w_set.add(0)
for i in range(N-1):
    next_w_set = set()
    for w in list(w_set):
        for j in range(0,4):
            new_w = w+j+first_w
            if new_w <= W:
                next_w_set.add(new_w)
                w_set_res.add(new_w)
    w_set = copy.deepcopy(next_w_set)

w_set_res.add(0)
# print(w_set_res)
w_list = list(w_set_res)
w_list.sort()

max_w = min(4*N, W-first_w*N)
dp = [ {0:0} for _ in range(N+1)]
for d in dp:
    for w in w_list:
        d[w] = 0

for i in range(N):
    wi,vi = wvl[i]
    for w in w_list:
        if w-wi in w_set_res:
            dp[i+1][w] = max(dp[i][w-wi] + vi, dp[i][w])
        else:
            dp[i+1][w] = dp[i][w]

# print(dp)
print(max(dp[N].values()))

Measured memory_bytes: 126496
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final String today = scanner.nextLine();

        switch (today) {
            case "Sunny":
                System.out.println("Cloudy");
                return;
            case "Cloudy":
                System.out.println("Rainy");
                return;
            case "Rainy":
                System.out.println("Sunny");
                return;
        }
    }
}

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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using P = std::tuple<int,int>;
using ll = long long;

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

int cnt[26];

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

    string S;
    std::cin >> S;

    for(const auto& c : S){
        ++cnt[c - 'a'];
    }

    for(int i=0;i<26;++i){
        if(cnt[i] & 1){
            puts("No");
            return 0;
        }
    }

    puts("Yes");
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intput(i) cout << i << endl
#define ll long long
#define stringput(s) cout << (string)(s) << endl

int main(){
  string S;
  cin >> S;
  string ans = "No";
  if(S[1] == '7' || S[0] == '7' || S[2] == '7') ans = "Yes";
  cout << ans << endl;
}


Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200002;
struct node{
	int to,ne,w;
}e[N<<1];
struct kk{
	int x,y;ll w;
	bool operator<(kk a)const{return w<a.w;}
}E[N*30];
int du,a[N],sz[N],mx[N],rx,ry,fa[N],tot,h[N],S,rt,m,n,i,x,y,z;
bool vis[N];
ll dw,pre[N],ans;
void add(int x,int y,int z){e[++tot]={y,h[x],z},h[x]=tot;}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void getrt(int u,int fa){
	sz[u]=1,mx[u]=0;
	for (int i=h[u],v;i;i=e[i].ne)
		if ((v=e[i].to)!=fa && !vis[v]) getrt(v,u),sz[u]+=sz[v],mx[u]=max(mx[u],sz[v]);
	mx[u]=max(mx[u],S-sz[u]);
	if (mx[u]<mx[rt]) rt=u;
}
void chk(int u,int fa){
	if (pre[u]+a[u]<dw) dw=pre[u]+a[u],du=u;
	for (int i=h[u],v;i;i=e[i].ne)
		if ((v=e[i].to)!=fa && !vis[v]) pre[v]=pre[u]+e[i].w,chk(v,u);
}
void getdis(int u,int fa){
	E[++m]={du,u,dw+a[u]+pre[u]};
	for (int i=h[u],v;i;i=e[i].ne)
		if ((v=e[i].to)!=fa && !vis[v]) getdis(v,u);
}
void dfs(int u){
	du=u,dw=a[u],vis[u]=1,pre[u]=0,chk(u,0),getdis(u,0);
	for (int i=h[u],v;i;i=e[i].ne)
		if (!vis[v=e[i].to]) S=sz[v],rt=0,getrt(v,0),dfs(rt);
}
int main(){
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&a[i]);
	for (i=1;i<n;i++) scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,z);
	mx[0]=n+1,S=n,rt=0,getrt(1,0),dfs(rt);
	sort(E+1,E+m+1);
	for (i=1;i<=n;i++) fa[i]=i;
	for (i=1;i<=m;i++){
		rx=find(E[i].x),ry=find(E[i].y);
		if (rx!=ry) ans+=E[i].w,fa[rx]=ry;
	}
	printf("%lld",ans);
}


Measured memory_bytes: 74752
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
const int inf = INT_MAX;
const ll INF = LLONG_MAX;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

int main(){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int x = c-a, y = d - b;
    cout << a+x-y << ' ' << b+x+y << endl;
    cout << c-y-x << ' ' << d-y+x << endl;
}

Measured memory_bytes: 256
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef int64_t lint;
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using lb = long double;
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SI(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
#define debug(x) cerr << #x << ": " << x << "\n"//手動デバッグ
#define debugV(x) for(int i=0;i<((ll)(x).size());i++)cerr << #x <<"at:"<<i<< ": " << x[i] << "\n"//手動デバッグ 
#define YES(c) cout<<((c)?"YES\n":"NO\n"),0
#define Yes(c) cout<<((c)?"Yes\n":"No\n"),0
#define O(p) cout<<p<<"\n",0
#define OS(p) cout<<p<<" ",0
#define ON(p) cout<<p,0
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define inf 2147483647 //int値の最大値
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define endl "\n"//endlを置き換える
//略記
#define PB emplace_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define CST(x) cout<<fixed<<setprecision(x)//小数点以下の桁数指定

const int dy[4]={1,0,-1,0};
const int dx[4]={0,1,0,-1};//^>v<

template<class T=int> T in(){
    T x;
    cin>>x;
    return (x);
}
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;
}
int gcd(int a, int b) {
    if (b==0) return a;
    else return gcd(b, a%b);
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
int two(int n){
	ll ans=1;
	rep(i,n){
		ans*=2;
	}
	return ans;
}
//------------------------------------------------------------





//------------------------------------------------------------
int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
//------------------------------------------------------------
    string s;cin>>s;
    int n=s.size();
    if(s[n-1]=='s'){
        s+="es";
    }else{
        s+="s";
    }
    O(s);
    
    
    
    
    
    
    
//------------------------------------------------------------
	return 0;
}


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

int main(){
    const int m[6] = {500,100,50,10,5,1};
    int price;
    
    while(true){
        cin >> price;
        if(!price) break;
        
        int change = 1000 - price,cnt = 0;
        for(int i = 0 ; i < 7 ;){
            if(change >= m[i]){
                change -= m[i];
                cnt++;
            }
            else i++;
            
            if(!change) break;
        }
        cout << cnt << endl;
    }
    
    return 0;
}

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

void update(int q, int& last, int& d) {
    if (d != 0 and q%2 != last%2) d = 3-d;
    last = q;
}

pair<vector<int>, vector<int>> solve(int n, const string& T) {
    if (n == 0) {
        vector<int> P = {0};
        vector<int> W(T.size(), 0);
        return {P, W}; 
    }
    auto pp = solve(n-1, T);
    const auto& P = pp.first;
    const auto& W = pp.second;
    vector<int> new_W(W.size());
    int pot = P.size();
    vector<int> D(3*pot);
    for (int i = 0; i < 3; i++) for (int x = 0; x < pot; x++) D[i*pot + x] = i;
    vector<int> last(3*pot, 0);
    int q = 0;
    for (int i = 0; i < (int)T.size(); i++) {
        int k = W[i];
        for (int x = k; x < 3*pot; x += pot) {
            update(q, last[x], D[x]);
            if (D[x] == 2) new_W[i] = x;
        }
        if (T[i] == 'S') q++;
        if (T[i] == 'R') {
            for (int x = k; x < 3*pot; x += pot) D[x] = (D[x]+1)%3;
        }
    }

    vector<int> new_P(3*pot);
    for (int i = 0; i < 3*pot; i++) {
        update(q, last[i], D[i]);
        new_P[i] = P[i%pot] + D[i] * pot;
    }

    return {new_P, new_W};
}

int main() {
    int N;
    cin >> N;
    string T;
    cin >> T;
    vector<int> P = solve(N, T).first;
    for (int x: P) cout << x << " ";
    cout << "\n";
}


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

#define forsn(i, s, n)	for (int i = s; i < n; i++)
#define forn(i, n)		for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;

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

int main() {
	int k;
	cin >> k;
	ull ans = 0;
	forsn(a, 1, k+1) {
		forsn(b, 1, k+1) {
			forsn(c, 1, k+1) {
				ans += gcd(gcd(a, b), c);
			}
		}
	}
	
	cout << ans << '\n';
	
	return 0;
}


Measured memory_bytes: 3636