akhauriyash/Code-Regression huggingface dataset:
object Main {
  def main(args: Array[String]): Unit = {
    val s = new Main()
    s.solve()
    s.out.flush()
  }
}

class Main {
  import java.io._
  import java.util.StringTokenizer

  import scala.collection.mutable
  import scala.util.Sorting
  import math.{abs, max, min}
  import mutable.{ArrayBuffer, ListBuffer}
  import scala.reflect.ClassTag

  val MOD = 1000000007
  val out = new PrintWriter(System.out)

  def solve(): Unit = {
    val N = ni()
    val D = Array.ofDim[Int](13)
    rep(N) { _ =>
      D(ni()) += 1
    }

    val dup = D.zipWithIndex exists {
      case (num, i) => i match {
        case 0 => num >= 1
        case 12 => num >= 2
        case _ => num >= 3
      }
    }
    if (dup) out.println(0)
    else {
      var ans = 0

      def calcMin(times: Int): Int = {
        // 高橋くんがいるので0時をまたぐケースは考えなくていい
        val bit = times | 1 << 24 // 25bit目を立てると必ず1で終わって楽
        var lastOne = -1
        var ms = 24
        rep(25) { i =>
          if ((bit & 1 << i) > 0) {
            if (lastOne != -1) ms = min(ms, i - lastOne)
            lastOne = i
          }
        }

        ms
      }

      rep(math.pow(2, 11).toInt) { i =>
        var times = 1 // 高橋くんのぶん
        if (D(12) == 1) times |= 1 << 12
        rep(11, 1) { d =>
          if (D(d) == 2) {
            times |= 1 << d
            times |= 1 << 24 - d
          }
        }

        rep(11) { k =>
          if (D(k + 1) == 1) {
            // onのときは24-d, d = k + 1
            if ((i & 1 << k) > 0) {
              times |= 1 << 24 - (k + 1)
            } else {
              times |= 1 << k + 1
            }
          }
        }

        ans = max(ans, calcMin(times))
      }

      out.println(ans)
    }
  }

  class InputReader(val stream: InputStream) {
    private val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private var tokenizer: StringTokenizer = _

    def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = next().toInt
    def nextLong(): Long = next().toLong
    def nextChar(): Char = next().charAt(0)
  }
  val sc = new InputReader(System.in)
  def ni(): Int = sc.nextInt()
  def nl(): Long = sc.nextLong()
  def nc(): Char = sc.nextChar()
  def ns(): String = sc.next()
  def ns(n: Int): Array[Char] = ns().toCharArray
  def na(n: Int): Array[Int] = map(n)(_ => ni())
  def nal(n: Int): Array[Long] = map(n)(_ => nl())
  def nm(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  def rep(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def rep_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }

  def map[@specialized A: ClassTag](n: Int)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    rep(n)(i => res(i) = f(i))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    rep(as.length)(i => s += as(i))
    s
  }
}


Measured memory_bytes: 27324
$<.map{|i|s=(a=i.split(?,).map(&:to_i).sort).uniq.size;puts (b=a.map{|j|a.count(j)}.max)>3?"four card":b>2?s>2?"three card":"full house":b>1?s>3?"one pair":"two pair":a[4]-a[0]<5||a[1]>9?:straight: :null}

Measured memory_bytes: 8592
import java.util.*;
import static java.lang.System.*;

public class Main {

	Scanner sc = new Scanner(in);
	
	void run() {
		List<Integer> W = new ArrayList<Integer>();
		List<Integer> D = new ArrayList<Integer>();
		List<Integer> res = new ArrayList<Integer>();
		while (true) {
			int n = sc.nextInt(), w = sc.nextInt(), d = sc.nextInt();
			if (n == 0 && w == 0 && d == 0) break;
			W.clear(); D.clear();
			W.add(w);
			D.add(d);
			for (int z = 0; z < n; z++) {
				int p = sc.nextInt()-1, s = sc.nextInt();
				int wi = W.get(p), di = D.get(p);
				W.remove(p); D.remove(p);
				
				s = s % (2*(wi+di));
				if (s < wi) {
					if (s <= wi-s) {
						W.add(s); D.add(di);
						W.add(wi-s); D.add(di);
					} else {
						W.add(wi-s); D.add(di);
						W.add(s); D.add(di);
					}
				} else if (s < wi+di) {
					if (s-wi <= wi+di-s) {
						W.add(wi); D.add(s-wi);
						W.add(wi); D.add(wi+di-s);
					} else {
						W.add(wi); D.add(wi+di-s);
						W.add(wi); D.add(s-wi);
					}
				} else if (s < wi+di+wi) {
					if (s-wi-di <= wi+di+wi-s) {
						W.add(s-wi-di); D.add(di);
						W.add(wi+di+wi-s); D.add(di);
					} else {
						W.add(wi+di+wi-s); D.add(di);
						W.add(s-wi-di); D.add(di);
					}
				} else {
					if (s-wi-di-wi <= wi+di+wi+di-s) {
						W.add(wi); D.add(s-wi-di-wi);
						W.add(wi); D.add(wi+di+wi+di-s);
					} else {
						W.add(wi); D.add(wi+di+wi+di-s);
						W.add(wi); D.add(s-wi-di-wi);
					}
				}
				
			}
			
			res.clear();
			for (int i = 0; i < W.size(); i++)
				res.add(W.get(i)*D.get(i));
			Collections.sort(res);
			
			out.print(res.get(0));
			for (int i = 1; i < W.size(); i++) {
				out.printf(" %d", res.get(i));
			}
			out.println();
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}

}

Measured memory_bytes: 52628
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <cmath>
#include <stdlib.h>

using namespace std;

#define int long long
#define endl "\n"
#define all(v)  v.begin(),v.end()
#define fir first
#define sec second
#define m_p make_pair
#define m_t make_tuple
#define rep(i,n)  for(int i=0; i<(int) (n); i++)


const double pai = 3.1415926535897;
const int mod = 1000000007;
const int INF = 1000000021;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];


int jousu(int x00, int y00) {
	int z00 = 1;
	for (int i = 0; i < y00; i++) {
		z00 *= x00;
	}
	return z00;
}
int keta(int x00) {
	int z00 = x00;
	int w00 = 0;
	while (z00 != 0) {
		z00 /= 10;
		w00++;
	}
	return w00;
}
//x未満の要素数を返す二分探索関数
int b_s(vector<int>& vec, int xx) {
	return lower_bound(all(vec), xx) - vec.begin();
}
template<typename T>
void vecout(vector<T>& vec) { for (T t : vec) cout << t << " "; cout << endl; }
template<typename TT>
void vecin(vector<TT>& vec) {
	for (int i = 0; i < vec.size(); i++) {
		cin >> vec[i];
	}
}


// テーブルを作る前処理
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;
	}
}
//n個を1個以上のx組のグループに分ける重複組み合わせはcom(n-1,x-1)
//グループが0個でもいいときはnにxを足す
// 二項係数計算
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;
}
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
int modpow(int a, int n) {
	int res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

int gcd(int xx, int yy) {
	int p = xx;
	int q = yy;
	if (q > p)swap(p, q);
	while (p % q != 0) {
		p %= q;
		swap(p, q);
	}
	return q;
}
int lcm(int xx, int yy) {
	return xx * yy / gcd(xx, yy);
}
bool prime(int xx) {
	if (xx <= 1) {
		return 0;
	}
	for (int i = 2; i * i <= xx; i++) {
		if (xx % i == 0) {
			return 0;
		}
	}
	return 1;
}




signed main() {
	int a, b, c;
	cin >> a >> b >> c;
	if (c - a - b < 0)cout << "No" << endl;
	else if (4 * a * b < (c - a - b) * (c - a - b))cout << "Yes" << endl;
	else cout << "No" << endl;
}


Measured memory_bytes: 3636
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
using namespace std;
 
int main(void){
    double a, b, c;
    double S, L, h;
    cin >> a >> b >> c;
 
    h = b*sin(c*M_PI/180);
    S = h*a*0.5;
    L = a+b+sqrt(a*a+b*b-2*a*b*cos(c*M_PI / 180));
    cout << fixed << setprecision(10) << S << "\n" << L << "\n" << h << "\n";
}

Measured memory_bytes: 3664
#include <iostream>

std::uint64_t turn(const std::uint64_t life, const std::uint64_t atk)
{
    if(life % atk == 0) {return life / atk;}
    else {return life / atk + 1;}
}

int main()
{
    std::uint64_t A, B, C, D;
    std::cin >> A >> B >> C >> D;

    if(turn(C, B) <= turn(A, D))
    {
        std::cout << "Yes" << std::endl;
    }
    else
    {
        std::cout << "No" << std::endl;
    }
    return 0;
}


Measured memory_bytes: 3592
#include <iostream>
#include <vector>

using namespace std;

int main(void){
	int n;

	cin >> n;

	if (n <= 1)
		cout << 1;
	else if (n <= 3)
		cout << 2;
	else if (n <= 7)
		cout << 4;
	else if (n <= 15)
		cout << 8;
	else if (n <= 31)
		cout << 16;
	else if (n <= 63)
		cout << 32;
	else
		cout << 64;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>
int isprim(int x){
  if(x==2)return 1;
    if(x<2 || x%2==0)return 0;
      int i=3;
      while(i<=((int)(sqrt((double)x)))){
	if(x%i==0)return 0;
	  i=i+2;
	}
	return 1;
    }
int main(){
  int i,n,k;
  int count=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&k);
    if(isprim(k)==1)count++;
  }
    printf("%d\n",count);
    return 0;
  }


Measured memory_bytes: 2032
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define rrep(i, a, n) for (int i = (int)(a); i >= (int)(n); --i)
#define debug(x) cerr << #x << " = " << x << "\n"
#define debugv(x) \
  rep(f, 0, (x.size() - 1)) cerr << x[f] << (f == (x.size() - 1) ? "\n" : " ")
#define debug2(x, y)                       \
  cerr << "(" << #x << "," << #y << ") = " \
       << "(" << x << "," << y << ")\n"
#define all(x) x.begin(), x.end()
#define each(a, x) for (auto& a : (x))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define int long long
#define yn(x) puts((x ? "Yes" : "No"))
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
const int MX = 1e5 + 5, INF = 5LL << 57, MOD = 1e9 + 7;

int N, M;
string S;
vi va, vb;

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void input() {
  ;
  ;
  cin>>N;
}
void solve() {
  int K=N;
 
 int ans = 0;
 rep(i,1,K){
   rep(j,1,K){
     int c = gcd(i,j);
     rep(k,1,K){
       ans += gcd(k,c);
     }
   }
 }
 cout<<ans<<endl;
  ;
  ;
}
signed main() {
  cout << setprecision(12);
  input();
  solve();
  return 0;
}

Measured memory_bytes: 3640
#include<stdio.h>

int main()
{
    int a[15],i,j,t;
for(i=0;i<10;i++){
    scanf("%d",&a[i]);
    }

     for(i=0;i<10;i++)
     {
         for(j=0;j<10-i-1;j++)
         {
             if(a[j]<a[j+1])
             {
                 t=a[j];
                 a[j]=a[j+1];
                 a[j+1]=t;
             }
         }
     }
     for(i=0;i<3;i++)
     {
         printf("%d\n",a[i]);
     }

    return 0;
}

Measured memory_bytes: 600
// #include {{{
#include <array>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <string>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <tuple>
#include <utility>
#include <random>
#include <limits>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdint>
#include <cassert>
// }}}

using namespace std;

// macros {{{
template<typename T, size_t N>
constexpr size_t NELEMS(T (&)[N]) { return N; }

template<typename InputIt>
void PRINT_RANGE(InputIt first, InputIt last)
{
    for(; first != last; ++first)
        cout << *first << (first==last ? "" : " ");
    cout << "\n";
}

template<typename T>
void PRINT_MATRIX(T mat, size_t nr, size_t nc)
{
    for(size_t r = 0; r < nr; ++r) {
        for(size_t c = 0; c < nc; ++c) {
            cout << mat[r][c] << (c==nc-1 ? "" : " ");
        }
        cout << "\n";
    }
}

#define FOR(i, start, end) for(int i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)
// }}}

// types {{{
using ll  = long long;
using ull = unsigned long long;
// }}}

ll N, A, B;
ll X[100000];

ll dp[100000];

void solve()
{
    dp[0] = 0;
    dp[1] = min(A*(X[1]-X[0]), B);

    FOR(i, 2, N) {
        dp[i] = dp[i-1] + min(A*(X[i]-X[i-1]), B);
    }
#ifdef DEBUG
    PRINT_RANGE(dp, dp+N);
#endif

    cout << dp[N-1] << "\n";
}

int main()
{
    cin >> N >> A >> B;
    REP(i, N) {
        cin >> X[i];
    }

    solve();

    return 0;
}


Measured memory_bytes: 1792
import std.stdio, std.string, std.conv, std.bigint, std.typecons, std.algorithm, std.array, std.math, std.range;
 
void main() {
    auto N = readln.chomp.to!int;
    auto K = readln.chomp.to!int;
    auto xs = readln.split.to!(int[]);
    xs.map!(x => 2 * min(x, K-x)).sum.writeln;
}



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <queue>
#include <set>
#include <math.h>
#include <array>
#include <list>
#include <set>
const int MOD=1000000007;
const int INF=1000000000;
using namespace std;
typedef long long ll;
typedef tuple<ll,int,int> tup;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vp;
int n;
int main(int argc,char const* argv[])
{
  int a[3];
  for(int i=0;i<3;i++)
    {
      cin >> a[i];
    }
  sort(a,a+3);
  cout << a[0]*a[1]/2 << endl;
  return 0;
}


Measured memory_bytes: 256
N = int(input())

memo = {}
n = 0

while n < N:
    for x in range(1, 100):
        for y in range(1, 100):
            for z in range(1, 100):
                n = x**2 + y**2 + z**2 + x*y + y*z + z*x
                if n > N :
                    break
                memo.setdefault(n, [])
                memo[n].append((x,y,z))

for i in range(1, N+1):
    if i in memo:
        print(len(memo[i]))
    else :
        print(0)

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

using namespace std;

int gcd(int a, int b)
{
    while (b != 0) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    int mx, g;
    scanf("%d", &g);
    mx = g;

    for (int i = 1; i < n; ++i) {
        int x;
        scanf("%d", &x);
        mx = max(mx, x);
        g = gcd(g, x);
    }

    if (k > mx || k % g != 0)
        printf("IM");
    printf("POSSIBLE");

    return 0;
}


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

#define pb                push_back
#define mp                make_pair
#define all(x)            (x).begin(),(x).end()
#define fi                first
#define se                second
#define vc                vector
#define pii               pair<bint,bint>
#define endl              "\n"
#define rep(i,a,b)        for(bint i=a;i<b;i++)
#define repe(i,a,b)       for(bint i=a;i<=b;i++)
#define bac(i,a,b)        for(bint i=a;i>b;i--)
#define bace(i,a,b)       for(bint i=a;i>=b;i--)
#define fast              ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

//CHANGE IF REQ
#define int               long long int
#define bint              int
//-----------------

#define ld                long double
#define mod1              (bint)(1e9 + (bint)7)
#define mod2              (bint)998244353
#define pi                (float)3.14159265358979323846
#define inf               (bint)1e18
#define ninf              (bint)((bint)-1*(1e18))

#define DECIMAL(n)        std::cout << std::fixed << std::setprecision(n)
#define SORT(v)           sort(all(v))
#define RSORT(v)          sort(all(v),greater<bint>())
#define REVERSE(v)        reverse(all(v))

#define trav(a, x)        for(auto& A : x)
#define display(x)        trav(A,x) cout<<A<<endl;
#define pow(i,n)          (bint)pow((bint)i,n)
#define pow2(n)           ((bint)1<<n)
#define sz(x)             (bint)x.size()

typedef std::priority_queue<bint, std::vector<bint>, std::greater<bint> > min_pq;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

inline string inttos(bint a){std::stringstream soso; soso<<a; return soso.str();}
inline bint stoint(const std::string &s) {std::stringstream soso; soso << s; long long x; soso >> x; return x;}
inline std::string inttobin(bint x) {return std::bitset<40>(x).to_string();}
inline void swp(bint &a, bint &b){auto temp=a;a=b;b=temp;}

//pri - 1000000009
//ba  - 937,991,1013,1409,1741

void solve(){
    int N,K;
    cin >> N >> K;
	int ans = 0;
    int L,R;
	for (int i = K; i <= N + 1; i++) {
		L = i * (i - 1) / 2;
		R = N * i - L;
		ans += R - L+1;
	}
	cout << ans % mod1 << endl;
}


signed main(){
    fast

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif


    int t=1;
    //cin>>t;
    rep(i,0,t){
       solve();
    }

}



Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
typedef long long llint;
const llint MOD = 998'244'353;

int main() {
    int N;
    cin >> N;
    set<string> s;
    while(N--) {
        string a;
        cin >> a;
        s.insert(a);
    }
    cout << s.size();
}

Measured memory_bytes: 19152
def main():
	n, k = map(int, input().split())
	*a, = map(int, input().split())
	*f, = map(int, input().split())
	a.sort()
	f.sort(reverse=1)
	prod = [(y, x * y) for x, y in zip(a, f)]
	l = -1
	r = 10 ** 12
	while r - l > 1:
		m = (l + r) // 2
		s = sum(max(0, 0 - (m - p) // y) for y, p in prod)
		if s <= k:
			r = m
		else:
			l = m
	print(r)


if __name__ == "__main__":
	main()

Measured memory_bytes: 121236
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);++(i))
#define all(x) (x).begin(),(x).end()
#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define yes(n) cout << ((n) ? "yes" : "no") << endl

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

const int INF = 1001001001;
const vector<int> di = {-1,  0, 1, 0};
const vector<int> dj = { 0, -1, 0, 1};

void chmin(int &a, int b) { if (a > b) a = b; }

ll GCD(ll a, ll b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}
ll LCM(ll a, ll b) {
    ll g = GCD(a, b);
    return a / g * b;
}


int main() {
    int n; cin >> n;
    vector<ll> h(n);
    rep(i, n) cin >> h[i];
    vector<ll> hh(n);
    reverse(all(h));
    rep(i, n) hh[i] = h[i] - 1;

    bool ok = true;
    for (int i=1;i<n;++i) {
        if (h[i - 1] < h[i]) {
            if (h[i - 1] < hh[i]) {
                ok = false;
                break;
            } else {
                h[i] = hh[i];
            }
        }
    }
    Yes(ok);
}

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

int main(){
	// sengen
	int a,b,c;
	// nyuryoku
	cin >> a >> b >> c;
	// keisan
	if(a-b >= c){
		cout << '0';
	}else{
		cout << c + b -a;
	}
	// syutsuryoku
}

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

int main() {
  int N; cin >> N;
  vector<int> cntR(N), cntC(N);
  vector<int> f(N), g(N);

  bool ok = true;
  for(int i=0; i<N; i++) {
    int r, c; cin >> r >> c;
    r--; c--;
    cntR[r]++;
    cntC[c]++;
    ok &= (cntR[r] == 1);
    ok &= (cntC[c] == 1);
    f[r] = c;
    g[c] = r;
  }

  if(!ok) {
    cout << -1 << endl;
    return 0;
  }

  int ans = 0;
  for(int i=0; i<N; i++) {
    if(f[i] == i) continue;
    ans++;
    int x = i, y = g[i];
    int z = i, w = f[i];
    swap(f[x], f[y]);
    swap(g[z], g[w]);

    /*
    for(int j=0; j<N; j++) cerr << f[j] << " ";
    cerr << endl;
    for(int j=0; j<N; j++) cerr << g[j] << " ";
    cerr << endl;
    */
  }
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 4384
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
p, s, ans = [], 0, 0
for i, (a, b) in enumerate(zip(A, B)):
    if a > b:
        p.append(a-b)
    elif a < b:
        s += b-a
        ans += 1
p.sort(reverse=1)
for x in p:
    if s > 0:
        s -= x
        ans += 1
else:
    if s > 0:
        ans = -1
print(ans)



Measured memory_bytes: 81712
/*▄███████▀▀▀▀▀▀███████▄
░▐████▀▒ЗАПУСКАЕМ▒▀██████▄
░███▀▒▒▒▒▒ДЯДЮ▒▒▒▒▒▒▀█████
░▐██▒▒▒▒▒БОГДАНА▒▒▒▒▒████▌
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌
░░█▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▐███▌
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▐███▌
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀
░░░░▄█████████████  █▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐*/
#include <iostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <stack>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define DEBUG
#define fi first
#define se second
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
#define mk(a, b) make_pair(a, b)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ull> vull;
typedef vector<ll> vll;
// typedef tuple<int, int, int> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector< vector<int> > vvi;
typedef vector<char> vc;

const int INF = 1e9;
const ll INFLL = 1e18;
const int MOD = 1000000007;
// const int MAXN = 3e5;
const ld eps = 1e-6;
const ld eps2 = 1e-9;
const int MOD2 = 998244353;
const int dosz = 5e5;
const int SZ = (1<<18);
const ld PI = atan2l(0, -1);


void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#ifdef DEBUG
        // freopen("a.in", "r",stdin);
#else
       // freopen("mxnl.in", "r", stdin);
       // freopen("mxnl.out", "w", stdout);
#endif
}

int gcd(int a, int b){return (b?gcd(b, a%b):a);}

void solve(){
    int n;
    cin >> n;
    int sum = 0;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            for(int k=1; k<=n; k++){
                sum += gcd(i, gcd(j, k));
            }
        }
    }
    cout << sum << endl;
}

signed main() {
    fast_io();
    int q = 1;
    // cout << fixed << setprecision(6);
    // cin >> q;
    // cout << 1 << endl;
    while(q--){
        solve();
    }
    return 0;
}



Measured memory_bytes: 3636
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>

#define _GLIBCXX_DEBUG
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
using namespace std;
using vi = vector<int64_t>;
using vvi = vector<vi>;

bool func(pair<int64_t, string> a, pair<int64_t, string> b){
    if(a.first>b.first){
        return true;
    }else if(a.first==b.first){
        return a.second < b.second;
    }else{
        return false;
    }
    
}

int main(int argc, const char * argv[]) {
    // fasten cin
    cin.tie(0);
    ios::sync_with_stdio(false);
    // implement
    int64_t n;
    cin >> n;
    vector<string> s(n);
    rep(i,n) cin >> s.at(i);
    sort(s.begin(), s.end());
    vector<pair<int64_t, string>> pair;
    pair.push_back(make_pair(1, s.at(0)));
    for(int64_t i=0; i<n-1; i++){
        if(s.at(i+1)==s.at(i)){
            pair.at(pair.size()-1).first++;
        }else{
            pair.push_back(make_pair(1, s.at(i+1)));
        }
    }
    sort(pair.begin(), pair.end(), func);
    rep(i,pair.size()) {
        cout << pair.at(i).second << endl;
        if(i==pair.size()-1) break;
        if(pair.at(i+1).first < pair.at(i).first) break;
    }
}


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

int main(){
    int a, b, c;
    double rad, S;
    double p = acos(-1);
    cin >> a >> b >> c;
    rad = c * p / 180;
    S = 0.5 * a * b * sin(rad);
    cout << fixed << S << endl;
    cout << fixed << a + b + sqrt(a * a + b * b - 2.0 * a * b * cos(rad)) << endl;
    cout << fixed << 2.0*S/a << endl;
}


Measured memory_bytes: 3560
#include <iostream>
#include <sstream>
using namespace std;
#include <bits/stdc++.h>
#include<utility>
#include <algorithm>
#include<iterator>
#define  go return 0
#define rep(i,n) for(int i=0;i<n;++i)
#define all(v)   ((v).begin()), ((v).end())
#define F first
#define S second
#define p_b push_back
#define m_p make_pair
typedef long long ll;
typedef double db;
typedef vector<int> ve;
typedef pair<int, int> pa;
typedef vector<pair<int,int> > vepa;
typedef map<int, int> mp;
const ll N=1e5+5;
ll arr[N],A[N],ARR[N]  ;
bool visited[N]={};
ll cnt[N]={};
bool f;
 int main()
{
    string s;
    cin>>s;
   for(int i=0;i<s.size();i++)
    cout<<"x";

go;
}


Measured memory_bytes: 256
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x.size())
#define double long double 
#define fi first
#define se second
#define pii pair<int, int>

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
  int a, b;
  cin >> a >> b;
  if (a > b) swap(a, b);
  for (int i = 0; i < b; i++) {
    cout << a;
  }
  cout << "\n";
}



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

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

#define INF 10000000000
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

//#define DEBUG

int main_(){
  int N,K,X,Y;
  cin>>N>>K>>X>>Y;
  if(N<=K)cout<<X*N<<endl;
  else cout<<X*K+Y*(N-K)<<endl;
  return 0;
}


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  vector<string>test;
  /*
   * input for debug
   */
  for(int i=0;i<test.size();++i){
    stringbuf buf;
    streambuf*out=cout.rdbuf(&buf);
    cout<<test[i]<<flush;
    cout.rdbuf(out);
    streambuf*in=cin.rdbuf(&buf);
    main_();
    cin.rdbuf(in);
    cout<<endl;
  }
#else
  main_();
#endif

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int Maxn=1000005,mod=1e9+7;
int f[Maxn][2],fac[Maxn],ifac[Maxn];
int ksm(int x,int k){
	int ret=1,tmp=x;
	while(k){
		if(k&1)ret=1ll*ret*tmp%mod;
		tmp=1ll*tmp*tmp%mod;k>>=1;
	}
	return ret;
}
int inv(int x){return ksm(x,mod-2);}
int C(int n,int m){return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;}
int main(){
	int n,ans=0;scanf("%d",&n);
	f[1][0]=1;ifac[0]=fac[0]=1;
	for(int i=1;i<Maxn;i++)fac[i]=1ll*fac[i-1]*i%mod;
	ifac[Maxn-1]=inv(fac[Maxn-1]);
	for(int i=Maxn-2;i;i--)ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
	for(int i=2;i<=n+1;i++){
		f[i][0]=(f[i-1][0]+2ll*f[i-1][1])%mod;
		f[i][1]=f[i-1][0];
	}
	for(int i=1;i<=n;i++)
		ans=(ans+1ll*f[i+1][1]*C(n,i)%mod*inv(3ll*n*i%mod))%mod;
	ans=1ll*ans*ksm(inv(3),n-1)%mod;
	cout<<(ans+mod)%mod<<"\n";
	return 0;
}

Measured memory_bytes: 15872
n = int(input())
while (n != 0):
    nlist = []
    for i in range(n):
        nn = list(map(int, input().split()))
        nn.append(sum(nn))
        nlist.append(nn)
    lastn = []
    for i in range(n+1):
        last = 0
        for k in range(n):
            last += nlist[k][i]
        lastn.append(last)
    nlist.append(lastn)
    for i in range(n+1):
        for k in range(n+1):
            nnn = nlist[i][k]
            l = len(str(nnn))
            cn = 5 - l
            if (k == n):
                print(' '*cn + '{}'.format(nnn))
            else:
                print(' '*cn + '{}'.format(nnn), end = '')
    
    n = int(input())


Measured memory_bytes: 5616
#include <stdio.h>

int main() {
	
	int i, j, s, t, n, min, sum = 0;
	int x[1000], y[1000], z[1000][1000];
	
	scanf("%d", &n);
	
	for( i = 0; i < n; i++) {
		x[i] = 0;
		y[i] = 1000;
		for( j = 0; j < n; j++) {
			scanf("%d", &z[i][j]);
		}
	}
	
	y[0] = 0;
	
	for( i = 1; i < n; i++) {
		min = 1000;
		for( j = 0; j < n; j++) {
			if( y[j] < min && x[j] == 0) {
				min = y[j];
				s = j;
			}
		}
		
		x[s] = 1;
		
		for( t = 0; t < n; t++) {
			if(z[s][t] < y[t] && z[s][t] != -1 && x[t] == 0) {
				y[t] = z[s][t];
			}
		}
	}
	
	for( i = 0; i < n; i++) {
		sum += y[i];
	}
	
	printf("%d\n", sum);
	
	return 0;
}

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

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

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

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

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

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

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

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

fn main(){

    input!{
        s: chars,
    }

    let l = s.len();
    let mut cnt = 0;
    for i in 0..l{
        if s[i]!=s[l-1-i] {
            cnt += 1;
        }
    }

    println!("{}", cnt/2);
}

Measured memory_bytes: 4352
package main
 
import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)
 
var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)
 
func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }
 
func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}
 
func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }
 
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
 
//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)
 
	r := scanFloat64()
	fmt.Fprintln(wr, 2*r*math.Pi)
}

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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            //[summary]A - Christmas Eve Eve Eve
            int day = int.Parse(Console.ReadLine());

            switch (day)
            {
                case 22:
                    Console.WriteLine("Christmas Eve Eve Eve");
                    break;
                case 23:
                    Console.WriteLine("Christmas Eve Eve");
                    break;
                case 24:
                    Console.WriteLine("Christmas Eve");
                    break;
                default:
                    Console.WriteLine("Christmas");
                    break;
            }
        }
    }
}

Measured memory_bytes: 11092
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;

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

    int n, m;
    cin >> n >> m;
    string res = "0";
    if(n == 2) res = "10";
    if(n == 3) res = "100";
    bool isfix[3] = {false, false, false};

    rep(i, m){
        int s;
        char c;
        cin >> s >> c;
        --s;
        if((n!=1 && s==0 && c=='0') || (isfix[s] && res[s] != c)){
            cout << -1 << endl;
            return 0;
        }

        res[s] = c;
        isfix[s] = true;
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cmath>

int main(){
    int n;
    std::cin >> n;

    std::vector<float> c(n - 1), s(n - 1), f(n - 1);
    for(int i = 0; i < n - 1; ++i){
        std::cin >> c.at(i) >> s.at(i) >> f.at(i);
    }

    for(int i = 0 ; i < n - 1; ++i){
        int j = i;
        double time = 0;
        while(j < n - 1){
            if(j == i){
                time += s.at(j) + c.at(j);
            }else{
                if(time < s.at(j)){
                    time = s.at(j) + c.at(j);
                }else{
                    time = std::ceil((time - s.at(j)) / f.at(j)) * f.at(j) + s.at(j) + c.at(j);
                }
            }
//            std::cout << "j = " << j << ", time = " << time << std::endl;
            ++j;
        }

        std::cout << time << std::endl;
    }

    std::cout << 0 << std::endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,k;
    cin>>a>>b>>c>>k;
    int sum=0;
    if(k<a)
    {
        sum=sum+(k*1);
    }
    else if(k>=a)
    {
        sum=sum+a;
        k=k-a;
        if(k>b)
        {
            k=k-b;
            sum=sum-k;
        }
    }
    cout<<sum;
}

Measured memory_bytes: 3636
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] p = new int[n];
        int[] q = new int[n];
        for (int i= 0;i<n;i++) {
            p[i] = sc.nextInt();
            q[i] = p[i];
        }
        Arrays.sort(q);
        int count = 0;
        for (int i = 0;i<n;i++) {
            if (p[i] != q[i]) count++;
        }
        if (count == 0 || count == 2) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }

    }
}


Measured memory_bytes: 22100
//#include "bits/stdc++.h"
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <complex>
#include <bitset>
#include <functional>
#include <stack>
#include <regex>
#include <tuple>
#include <iomanip>

#define int long long
#define REP(i,a,n) for(int i=a;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define REV(i,a,n) for(int i=n;i>=a;--i)
#define all(e) e.begin(),e.end()
#define rall(e) e.rbegin(),e.rend()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define show(n) cerr<<#n<<" = "<<n<<endl
#define showp(n) cerr<<n.fs<<", "<<n.sc<<endl
#define shows(n) for(auto z:n){cerr<<z<<", ";}cerr<<endl
#define showsp(n) for(auto z:n){cerr<<z.fs<<" "<<z.sc<<", "}cerr<<endl

#define yes puts("Yes")
#define no puts("No")
#define case(i) printf("Case #%lld: ",i)

using namespace std;

using vi=vector<int>;
using pint=pair<int,int>;

inline void io(){cin.tie(0);ios::sync_with_stdio(false);cout.tie(0);cout<<fixed<<setprecision(10);}
inline int  in(){int n;scanf("%lld",&n);return n;}
inline void out(int n){printf("%lld\n",n);}

const int INF=1LL<<55;
const int MOD=1000000007;
const double EPS=1e-8;

long double n,m,d;

signed main(){
    io();
    
    cin>>n>>m>>d;
    if(d==0.0){
        cout<<(1.0/n)*(m-1)<<endl;
    }else{
        cout<<(2.0*(n-d)/(n*n))*(m-1)<<endl;
    }
    
    return 0;
}


Measured memory_bytes: 760
#include <iostream>
#include <iomanip>
#include <vector>
#include <valarray>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <bitset>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

#define all(c) c.begin(),c.end()
#define repeat(i,n) for(int i=0;i<static_cast<int>(n);i++)
#define debug(x) #x << "=" << (x)
#define dump(x) cerr << debug(x) << " (L:" << __LINE__ << ")"<< endl

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<long> vl;
typedef vector<vector<long> > vvl;
typedef vector<string> vs;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec){
    os << "[";
    for(int i = 0; i < vec.size(); i++){
        os << vec[i] << ",";
    }
    os << "]";
    return os;
}

template<typename T>
T input(){
    T t;
    cin >> t;
    return t;
}

template<typename T>
vector<T> input(const int N){
    vector<T> v(N);
    repeat(i,N) cin >> v[i];
    return v;
}

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

long long lcm(long long a, long long b){
    return (a / gcd(a, b)) * b;
}

long long mul(const long long& a, const long long& b, const long long& mod) {
    return ((a % mod) * (b % mod)) % mod;
}

long long power(const long long& x, const long long& y, const long long& mod) {
    if (y == 0) {
        return 1;
    } else if (y == 1) {
        return x % mod;
    } else {
        long long value = power(x, y / 2, mod);
        if (y % 2 == 0) {
            return mul(value, value, mod);
        } else {
            return mul(value, value, mod) * x % mod;
        }
    }
}

long long div(const long long& a, const long long& b, const long long& mod) {
    return mul(a, power(b, mod - 2, mod), mod);
}

map<long long, long long> factorials;
long long factorial(const long long& n, const long long& mod) {
    if (n == 0 || n == 1) {
        return 1;
    }
    if (factorials[n] != 0) {
        return factorials[n];
    }
    factorials[n] = n * factorial(n - 1, mod) % mod;
    return factorials[n] % mod;
}

long long combination(const long long& n, const long long& x, const long long& mod) {
    long long numerator = 1;
    long long denominator = 1;
    repeat(i, x) {
        numerator *= (n - i) % mod;
        numerator %= mod;
        denominator *= (i + 1) % mod;
        denominator %= mod;
    }
    return div(numerator, denominator, mod);
}

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

    if ((A <= C && C <= B) || (B <= C && C <= A)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}


Measured memory_bytes: 256
n, m = [int(n) for n in input().split()]

A = []
for i in range(n):
    A.append(input())

B = []
for j in range(m):
    B.append(input())

for i in range(n-(m-1)):
    for j in range(n-(m-1)):
        for k in range(m):
            #print(A[i+k][j:j+m], B[k])
            if A[i+k][j:j+m] != B[k]:
                break
        else:
            print('Yes')
            break
        continue
    else:
        continue
    break
else:
    print('No')

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
int N,M,K;
int Far[Maxn];
struct Friend{
	int From;
	int To;
}a[Maxn*2];
int ans[Maxn];
int ans1[Maxn];
int ans2[Maxn];
int HaveFar[Maxn];
int Anc(int x)
{
	if(Far[x]!=0)
		return Far[x] =Anc(Far[x]);
	return x;
}
bool search(int a,int b)
{
	if(Anc(a)!=Anc(b))
		return false;
	return true;
}
int main()
{
	cin>>N>>M>>K;
	for(int i=1;i<=M;i++)
	{
		int A,B;
		cin>>A>>B;
		if(!search(A,B))
		{
			if(HaveFar[A]==0&&HaveFar[B]==1)
			{
				ans2[Anc(B)]++;
				Far[Anc(A)] = Anc(B);
				HaveFar[A] = 1;
			}
			else if(HaveFar[B]==0&&HaveFar[A]==1)
			{
				ans2[Anc(A)]++;
				Far[Anc(B)]= Anc(A);
				HaveFar[B] = 1;
			}
			else if(HaveFar[B]==0&&HaveFar[A]==0)
			{
				ans2[Anc(A)]++;
				Far[Anc(B)]= Anc(A);
				HaveFar[B] = HaveFar[A] = 1;
			}
			else
			{
				ans2[Anc(B)] += 1+ans2[Anc(A)];
				Far[Anc(A)] = Anc(B);
			}
		}
		ans[A] ++;	//相邻伙伴数 
		ans[B] ++;
	}
	for(int i=1;i<=K;i++)
	{
		int A,B;
		cin>>A>>B;
		if(search(A,B))
		{
			ans1[A] ++;
			ans1[B] ++;//同祖宗敌人数 
		}
	}
	for(int i=1;i<=N;i++)
	{
		if(i!=N)	printf("%d ",ans2[Anc(i)]-ans[i]-ans1[i]);
		else	printf("%d",ans2[Anc(i)]-ans[i]-ans1[i]);
	}
	
	return 0;
}

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

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define ALL(x) x.begin(),x.end()
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pb push_back
#define ins insert

using namespace std;

typedef long long ll;
bool LTE(double a,double b) { return equals(a,b) || a < b; }
bool LT(double a,double b) { return !equals(a,b) && a < b; }


#define MAX 100010

template<typename T>
bool visit(const T& G,int v,vector<int>& order,vector<int>& color){
  color[v] = 1;
  for(__typeof((G[v]).begin()) e = G[v].begin();e != G[v].end();e++){
      if(color[e->to] == 2)continue;//このvisit以外のvisitで訪れた
      if(color[e->to] == 1)return false;//このvisitで訪れた
      if(!visit(G,e->to,order,color))return false;
    }
  order.push_back(v);
  color[v] = 2;
  return true;
}

template<typename T>
bool topologicalSort(const T& G,vector<int>& order){
  int SizeG = G.size();
  vector<int> color(SizeG); 
  for(int u=0;u<SizeG;u++) if(!color[u] && !visit(G,u,order,color)) return false;
  reverse(order.begin(),order.end());
  return true;
}

struct Edge {
  int to, cost;
  Edge(int to, int cost):to(to),cost(cost){}
};

int V,M;
vector<int> G[MAX], rG[MAX];
int depth[MAX];
int deg[MAX];

struct Data  {
  int cur, dep;
  bool operator < ( const Data &data ) const {
    if( dep != data.dep ) return dep < data.dep;
    return cur < data.cur;
  }
};



const bool debug = 0;

void compute() {
  vector<vector<Edge>> mG(V,vector<Edge>());
  rep(i,V) {
    rep(j,(int)rG[i].size()) {
      mG[i].push_back((Edge){rG[i][j],1});
    }
  }
  vector<int> order;
  topologicalSort(mG,order);
  if( debug ) {
    puts("--order");
    rep(i,(int)order.size()){
      cout << order[i] << " ";
    } puts("");
  }
  
  rep(i,V) depth[i] = -INT_MAX;
  memset(deg,0,sizeof deg);
  rep(i,V) {
    if( (int)G[i].size() == 0 ) {
      depth[i] = 0;
    }
  }

  rep(i,(int)order.size()) {
    int cur = order[i];
    rep(j,(int)rG[cur].size()) {
      int nex= rG[cur][j];
      depth[nex] = max(depth[nex],depth[cur]+1);
    }
  }








  if( debug ) {
    puts("depth--");
    rep(i,V) cout << i << " : " << depth[i] << endl;
  }
  vector<int> mini(V,INT_MAX);
  rep(i,V) {
    rep(j,(int)G[i].size()) {
      int s = i, t = G[i][j];
      mini[t] = min(mini[t],depth[s]);
    }
  }

  if( debug ) {
    puts("mini-");
    rep(i,V) cout << i << " : " << mini[i] << endl;
  }
  vector<int> pare(V,-1);
  rep(i,V) {
    rep(j,(int)G[i].size()) {
      int s = i, t = G[i][j];
      if( !( mini[t] == depth[s] ) ) continue;
      assert( pare[t] == -1 );
      pare[t] = s;
    }
  }
  rep(i,V) {
    if( pare[i] == -1 ) puts("0");
    else cout << pare[i]+1 << endl;
  }
}

int main() {
  cin >> V >> M;
  rep(i,(V-1+M)) {
    int s,t;
    cin >> s >> t;
    --s, --t;
    G[s].push_back(t);
    rG[t].push_back(s);
  }
  compute();
  return 0;
}


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

using namespace std;

const int N = 2000 * 2000 + 5;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<bitset<N>> dp(2);
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    dp[(i + 1) & 1] = dp[i & 1] | (dp[i & 1] << a[i]);
  }
  for (int i = (sum + 1) / 2; i <= sum; i++) {
    if (dp[n & 1][i]) {
      cout << i << '\n';
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 2688
#include <stdio.h>

int main(void){
    int s1,s2,ms1,ms2,i;
    for(;scanf("%d %d",&s1,&s2),ms1=0,ms2=s1+s2,s1!=0;){
        for(i=1;i<5;i++){
            scanf("%d %d",&s1,&s2);
            if(s1+s2>ms2){
                ms2=s1+s2;
                ms1=i;
            }
        }
        printf("%c %d\n",65+ms1,ms2);
    }
    return 0;
}

Measured memory_bytes: 600
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<climits>
#include<cstring>
#include<queue>
using namespace std;

const int MAX_N = 300000;
typedef unsigned long long ull;
typedef long long ll;
const ull mod = 1000000000 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

struct query{
    int com, x, y;
};

struct UnionFindSimple{
    int N;
    int par[MAX_N];

    UnionFindSimple(int N): N(N){
        for (int i=0;i<N;i++){
            par[i] = i;
        }
    }

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

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

    bool find(int x, int y){
        return root(x) == root(y);
    }
};

void solveUnionFindSimle(vector<query> &queries, int n){
    UnionFindSimple ufs(n);
    REP(i, queries.size()){
        query Q = queries[i];
        if(Q.com == 0){
            ufs.unite(Q.x, Q.y);
        }else{
            cout << ufs.find(Q.x, Q.y) << endl;
        }
    }
}

int main(){
    int n, q;
    cin >> n >> q;
    vector<query> queries;
    REP(i, q){
        query que;
        cin >> que.com >> que.x >> que.y;
        queries.push_back(que);
    }
    solveUnionFindSimle(queries, n);
    return 0;
}

Measured memory_bytes: 4496
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
static const int MAX = 10000;
static const int INFTY = (1 << 20);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int> > adj[MAX];

void dijkstra() {
	priority_queue<pair<int, int> > PQ;
	int color[MAX];
	int d[MAX];
	for (int i = 0; i < n; i++) {
		d[i] = INFTY;
		color[i] = WHITE;
	}

	d[0] = 0;
	PQ.push(make_pair(0, 0));
	color[0] = GRAY;

	while (!PQ.empty()) {
		pair<int, int> f = PQ.top(); PQ.pop();
		int u = f.second;
		color[u] = BLACK;

		if (d[u] < f.first*(-1)) continue;

		for (unsigned int j = 0; j < adj[u].size(); j++) {
			int v = adj[u][j].first;
			if (color[v] == BLACK) continue;
			if (d[v] > d[u] + adj[u][j].second) {
				d[v] = d[u] + adj[u][j].second;
				PQ.push(make_pair(d[v] * (-1), v));
				color[v] = GRAY;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
	}
}

int main() {
	int k, u, v, c;

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> u >> k;
		for (int j = 0; j < k; j++) {
			cin >> v >> c;
			adj[u].push_back(make_pair(v, c));
		}
	}

	dijkstra();

	return 0;
}

Measured memory_bytes: 6304