akhauriyash/Code-Regression huggingface dataset:
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
using namespace std;
using vi = vector<int>;

int main(void){
    int k;
    cin >> k;
    vi a(k);
    a[0] = 7%k;
    for(int i=1; i<k; i++) a[i] = (a[i-1]*10+7)%k;
    for(int i=0; i<k; i++)if(a[i]==0){
        cout << i+1 << '\n';
        return 0;
    }
    cout << -1 << '\n';
    return 0;
}

Measured memory_bytes: 7152
#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include <iomanip>
using namespace std;
 
#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
/*桁和*/int digit(ll n){if(n < 10)  return n; return digit(n/10) + n%10;}
ll ans=0;llfor;///////////////////////////////////////////////////////////
int main(){
  int N;
  cin>>N;
  for(int i=N;i<200000;i++){
    if(prime(i)==true){
      cout<<i;
      break;
    }
  }
return 0;}


Measured memory_bytes: 3612
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int INF = (1<<30);
const int MOD = 1000000007;
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; }

typedef struct {
    int to;
    ll cost;
} edge;

int main() {
    int N;
    scanf("%d", &N);
    vector<edge> tree[N];
    rep(i, N-1){
        int a,b,c;
        scanf("%d%d%d", &a, &b, &c);
        a--; b--;
        tree[a].push_back({b, c});
        tree[b].push_back({a, c});
    }
    int Q, K;
    scanf("%d%d", &Q, &K);
    K--;
    stack<edge> s;
    s.push({K, 0});
    ll depth[N];
    rep(i, N) depth[i] = -1;
    while(!s.empty()){
        edge from = s.top();
        s.pop();
        depth[from.to] = (ll)from.cost;
        for(auto &to : tree[from.to]){
            if(depth[to.to] == -1) s.push({to.to, to.cost + from.cost});
        }
    }
    rep(i, Q){
        int x, y;
        scanf("%d%d", &x, &y);
        x--, y--;
        printf("%lld\n", depth[x] + depth[y]);
    }

    return 0;
}


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

#define loop(n) for(int i = 0;i < (int)(n);++i)
#define rep(i,n) for(int i = 0;i < (int)(n);++i)
#define all(vec) (vec).begin(),(vec).end()
#define sort(vec) std::sort(all(vec))
#define rev(vec) std::reverse(all(vec))

void input(){return;}
template<typename First,typename... Rest> void input(First &first,Rest&... rest){
    std::cin >> first;
    input(rest...);
}

void inputVector(){return;}
template<typename First,typename... Rest> void inputVector(std::vector<First> &first,Rest&... rest){
    int N = first.size();
    for(int i = 0;i < N;i++){
        std::cin >> first[i];
    }
    inputVector(rest...);
}


void calculate(){

}

int main(int,char**){
    int A,B,C,Sum;
    int res = 0;
    input(A,B,C,Sum);
    rep(a,A + 1){
        rep(b,B + 1){
            rep(c,C + 1){
                int sum = 500 * a + 100 * b + 50 * c;
                if(sum == Sum) res++; 
            }
        }
    }
    std::cout << res << '\n';
}




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

int main(){
    int n;
    cin >> n;
    double v[50] = {};
    for (int i = 0; i < n;i++)
    {
        cin >> v[i];
    }
    sort(v, v + n);
    double d = 0;
    for (int i = 0; i < n - 1;i++)
    {
        v[i + 1] = (v[i] + v[i + 1]) / 2;
    }
    cout << v[n-1];
}

Measured memory_bytes: 256
object Main {
  def main(args: Array[String]): Unit = {
    solve2
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    val as = new Array[Long](n)
    for (i <- 0 until n) {
      as(i) = sc.nextLong
    }

    var isFinished = false
    while (!isFinished) {
      val min = as.filter(_ != 0).min
      val minIndex = as.indexOf(min)
      for (i <- 0 until n) {
        if (i != minIndex) {
          as(i) = as(i) % min
        }
      }

      if (as.count(_ != 0) == 1) {
        isFinished = true
      }
    }
    println(as.find(_ != 0).get)
  }

  def solve2(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    val as = new Array[Long](n)
    for (i <- 0 until n) {
      as(i) = sc.nextLong
    }

    var ans = as(0)
    for (i <- 1 until n) {
      ans = gcd(ans, as(i))
    }
    println(ans)
  }

  def gcd(x: Long, y: Long): Long = {
    if (x == 0) y
    else gcd(y % x, x)
  }

}


Measured memory_bytes: 52272
import java.util.*;
import java.io.*;
public class Main{
public static void main(String [] args)throws Exception{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  StringTokenizer st = new StringTokenizer(br.readLine());
  int xs = Integer.parseInt(st.nextToken());
  int ys = Integer.parseInt(st.nextToken());
  int xt = Integer.parseInt(st.nextToken());
  int yt = Integer.parseInt(st.nextToken());
  int n = Integer.parseInt(br.readLine());
  int circles[][] = new int[n + 2][3];
  circles[0][0] = xs;circles[0][1] = ys;
  circles[n + 1][0] = xt;circles[n + 1][1] = yt;
  for(int i = 1;i <= n;i++){
  	st = new StringTokenizer(br.readLine());
  	circles[i][0] = Integer.parseInt(st.nextToken());
  	circles[i][1] = Integer.parseInt(st.nextToken());
  	circles[i][2] = Integer.parseInt(st.nextToken());
}
   double dist[][] = new double[n + 2][n + 2];
   for(int i = 0;i <= n + 1;i++){
   	for(int j = i + 1;j <= n + 1;j++){
   		double temp = Math.sqrt((circles[i][0] - circles[j][0])*1.0*(circles[i][0] - circles[j][0]) +
   		       (circles[i][1] - circles[j][1])*1.0*(circles[i][1] - circles[j][1]));
   		if(temp < circles[i][2] + circles[j][2])temp = 0;
   		else temp -= circles[i][2] + circles[j][2];
   		dist[i][j] = dist[j][i] = temp;
   	}
   }
   PriorityQueue<Node>pq = new PriorityQueue<Node>();
   pq.add(new Node(0,0));
   boolean visited[] = new boolean[n + 2];
   while(!pq.isEmpty()){
     Node temp = pq.remove();
     if(visited[temp.idx])continue;
     if(temp.idx == n + 1){
     	System.out.println(temp.dist);
     	return;
     }
     visited[temp.idx] = true;
     for(int i = 0;i <= n + 1;i++){
     	if(visited[i])continue;
     	pq.add(new Node(i,temp.dist + dist[temp.idx][i]));
     }
   }
}
static class Node implements Comparable<Node>{
	int idx;
	double dist;
	Node(int idx,double dist){
		this.idx = idx;
		this.dist = dist;
	}
	public int compareTo(Node other){
		return Double.compare(dist,other.dist);
	}
}
}


Measured memory_bytes: 37684
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX_N = 100100;

int N, M;
vector<bool> Broken;
long long dp[MAX_N];

void input(){
    cin >> N >> M;
    Broken.assign(N, false);
    for (int i = 0; i < M; i++) {
        int a; cin >> a;
        Broken[a] = true;
    }
    for (int i = 0; i < MAX_N; i++) {
        dp[i] = -1;
    }
    dp[0] = 1;
    dp[1] = ((Broken[1]) ? 0 : 1);
}

int DP(int n){
    if (Broken[n]) return dp[n] = 0;
    if (dp[n] != -1) return dp[n];
    return dp[n] = ((DP(n - 1) + DP(n - 2)) % MOD);
}

int main(){
    input();
    DP(N);
    cout << dp[N] << endl;
    return 0;
}

Measured memory_bytes: 4224
#include<iostream>
#include<cstdio>
int max();
using namespace std;
int main()
{
    int W,n,w[1001],v[1001];
    int cnt = 1;
    int dp[1024][1000] = {{0}};
    for(;;){
        scanf("%d",&W);
        if(W == 0)break;
        scanf("%d",&n);
        for(int i = 1;i <= n; i++)scanf("%d,%d",&v[i],&w[i]);

        for(int j = 1;j <= n; j++){
            for(int i = 0;i <= W; i++){
                if(i < w[j]){
                    dp[i][j] = dp[i][j-1];
                }
                else {
                    dp[i][j] = max(dp[i][j-1],dp[i-w[j]][j-1]+v[j]);
                }
            }
        }
        int x = 0;
        for(int i = 0;i <= W; i++){
            if(dp[i][n] == dp[W][n]){
                x = i;
                break;
            }
        }
        printf("Case %d:\n",cnt);
        printf("%d\n",dp[W][n]);
        printf("%d\n",x);
        cnt++;
    }
    return 0;
}
int max(int a,int b){
    int x;
    x = (a > b) ? a:b;
    return x;
}

Measured memory_bytes: 5204
#include<iostream>

using namespace std;

int pa[200002], sz[200002];

int getRoot(int x){
	return pa[x]==x ? x : pa[x]=getRoot(pa[x]);
}

int main() {
	int n,m,i,a,b,u,v,ans;
	cin>>n>>m;
	for(i=1;i<=n;i++) pa[i]=i;
	while(m--) {
		cin>>a>>b;
		u=getRoot(a);
		v=getRoot(b);
		pa[v]=u;
	}
	for(i=1;i<=n;i++) sz[getRoot(i)]++;
	for(ans=0,i=1;i<=n;i++) if(sz[i]>ans) ans=sz[i];
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 4616
#include <bits/stdc++.h>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
		freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
int a, b, c;
signed main() {
	#ifdef BLU
	in("blu.inp");
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> a >> b >> c;
	int ans = 0;
	ans += 2 * b;
	c -= b, b = 0;
	ans++, c--;
	ans += min(a, c);
	cout << ans;
	return 0;
}

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

int main(){
    int n;
    cin>>n;
    set<char>s;
    rep(i,n){
        char c;
        cin>>c;
        s.insert(c);
    }
    int cnt=s.size();
    if(cnt==3)cout<<"Three"<<endl;
    else cout<<"Four"<<endl;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)n;i++)
int main(void) {
	string str; cin >> str;
	bool x = 1;
	rep(i, str.length()) {
		if (i % 2 == 0) {
			if (str[i] != 'R' && str[i] != 'U' && str[i] != 'D')
				x = 0;
		}
		else {
			if (str[i] != 'L' && str[i] != 'U' && str[i] != 'D')
				x = 0;
		}
	}
	cout << (x ? "Yes" : "No") << endl;
	return 0;
}

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

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
  cout.precision(17);

  int n;cin>>n;
  std::vector<double> v(n);
  for(int i=0;i<n;i++){
    cin>>v[i];
  }
  sort(v.begin(), v.end());
  while(v.size()!=1){
    v[0] = (v[0] + v[1]) / 2.0;
    v.erase(v.begin() + 1);
  }
  cout<<v[0]<<endl;

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=107,INF=1<<30;

int dp[MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        vector<int> T(N);
        for(int i=0;i<N;i++) cin>>T[i];
        int M;cin>>M;
        vector<int> A(103,100);
        for(int i=0;i<M;i++){
            int a,b;cin>>a>>b;
            A[a]=min(A[a],b);
        }
        
        for(int i=0;i<MAX;i++) for(int j=0;j<MAX;j++) dp[i][j]=INF;
        
        dp[0][N-1]=0;
        
        for(int i=0;i<102;i++){
            for(int j=0;j<N;j++){
                int to=(j+1)%N;
                if(T[to]<=A[i+1]) dp[i+1][to]=min(dp[i+1][to],dp[i][j]);
                
                dp[i+1][0]=min(dp[i+1][0],dp[i][j]+1);
            }
            
        }
        
        int ans=INF;
        
        for(int j=0;j<N;j++) ans=min(ans,dp[102][j]);
        
        cout<<ans<<endl;
    }
    
}




Measured memory_bytes: 3128
#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x7f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
const int maxn = 100010;
const double EPS = 1e-6;
using namespace std;
int a[maxn];
int v[maxn];
int main()
{
	int n;
	mt(v,0);
	scanf("%d",&n);
	rep(i,0,n) {
		scanf("%d",a+i);
		v[a[i]]++;
	}
	int mx=-1;
	rep(i,0,n){
		if(a[i]==0) mx=max(mx,v[a[i]+1]+v[a[i]]);
		else mx=max(mx,v[a[i]+1]+v[a[i]]+v[a[i]-1]);
	}
	printf("%d\n",mx);
	return 0;
}



Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define cinf(n,x,y,z) for(int i=0;i<(n);i++) cin >> x[i]>>y[i]>>z[i];
typedef long long int ll;
using namespace std;
int main(){
	int n; cin >>n;
	vector<int> t(n+1),x(n+1),y(n+1);
	for(int i=0;i<n;i++){
		cin>> t[i+1] >> x[i+1] >> y[i+1] ;
	}
	t[0] = x[0] = y[0] =0;
	bool can=true;
	for(int i=0;i<n;i++){
		int dt=t[i+1]-t[i];
		int dist=abs(x[i+1]-x[i])+abs(y[i+1]-y[i]);
		if(dt<dist)can=false;
		if(dist%2 != dt%2)can=false;
	}
	if(can) cout << "Yes" << endl;
	else cout << "No" << endl;
}


Measured memory_bytes: 1408
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<algorithm>
#include<cstring>
using namespace std;
int a, b;
int main()
{
	
	cin >> a >> b;
	if (a % 2 && b % 2)
		return !printf("Yes\n");
	return !printf("No\n");
	
}


Measured memory_bytes: 256
void main(){
	string a, b, c;
	readf("%s %s %s\n", &a, &b, &c);
	writeln( (a[0].to!string() ~ b[0].to!string() ~ c[0].to!string)
			.toUpper() );
}
     
import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math, std.range;
const long mod = 10^^9+7;      
// 1要素のみの入力
T inelm(T= int)(){
	return to!(T)( readln().chomp() );
}
     
// 1行に同一型の複数入力
T[] inln(T = int)(){
	T[] ln;
	foreach(string elm; readln().chomp().split())ln ~= elm.to!T();
	return ln;
}


Measured memory_bytes: 2832
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

// (行き先,容量,逆辺)
struct edge{ int to,cap,rev; };

const int MAX_V = 222; // TODO:initialize
const int F_INF = 19191919; // TODO:initialize
vector<edge> G[MAX_V];
int level[MAX_V]; // sからの距離
int iter[MAX_V]; // どこまで調べ終わったか

void add_edge(int from, int to, int cap){
    G[from].pb({to,cap,(int)G[to].size()});
    G[to].pb({from,0,(int)G[from].size()-1});
}

void dinic_bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        rep(i,G[v].size()){
            edge &e = G[v][i];
            if(e.cap>0 && level[e.to]<0){
                level[e.to] = level[v]+1;
                que.push(e.to);
            }
        }
    }
}

// 増加パスをdfsで探す
int dinic_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=iter[v]; i<(int)G[v].size(); ++i){
        edge &e=G[v][i];
        if(e.cap>0 && level[v]<level[e.to]){
            int d = dinic_dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

// sからtへの最大流
int max_flow(int s, int t){
    int flow = 0;
    while(1){
        dinic_bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dinic_dfs(s,t,F_INF))>0) flow+=f;
    }
}

int main(){
    int n,m,s,t;
    cin >>n >>m >>s >>t;
    --s; --t;

    vector<int> u(m),v(m),d(m),c(m);
    rep(i,m){
        cin >>u[i] >>v[i] >>d[i] >>c[i];
        --u[i];
        --v[i];
    }

    using pi = pair<int,int>;
    vector<vector<pi>> g(n);
    rep(i,m) g[u[i]].pb({v[i],d[i]});

    vector<int> dist(n,19191919);
    queue<int> que;
    dist[s] = 0;
    que.push(s);
    while(!que.empty()){
        int now = que.front();
        que.pop();
        for(const auto &e:g[now]){
            int to = e.fi, cost = e.se;
            if(dist[to] > dist[now]+cost){
                dist[to] = dist[now]+cost;
                que.push(to);
            }
        }
    }

    rep(i,m)if(dist[u[i]] + d[i] == dist[v[i]]) add_edge(u[i],v[i],c[i]);
    cout << max_flow(s,t) << endl;
    return 0;
}



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

int n, m;
vector<vector<bool>> g;
vector<int> dis;

void Bfs(int s) {
  queue<int> q;
  q.push(s);
  dis[s] = 0;

  int res = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int nv = 1; nv <= n; ++nv)
      if (dis[nv] == -1 && g[v][nv])
        q.push(nv), dis[nv] = dis[v] + 1;
  }
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> a(m), b(m);
    for (int i = 0; i < m; ++i)
      cin >> a[i] >> b[i];

    g.resize(n + 1, vector<bool>(n + 1, false));
    for (int i = 0; i < m; ++i)
      g[a[i]][b[i]] = true, g[b[i]][a[i]] = true;

    dis.resize(n + 1, -1);
    Bfs(1);

    int ans = 0;
    for (int i = 2; i <= n; ++i)
      if (dis[i] != -1 && dis[i] <= 2)
        ++ans;
    cout << ans << endl;

    dis.clear(), g.clear();
  }
}


Measured memory_bytes: 3252
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
int ans;
cin>>str;
if(str.at(0)=='2')
ans++;
if(str.at(1)=='2')
ans++;
if(str.at(2)=='2')
ans++;
if(str.at(3)=='2')
ans++;
cout<<ans<<endl;
}

Measured memory_bytes: 256
import bisect
import sys
sys.setrecursionlimit(10**8)


n = int(input())
A = list(map(int, input().split()))

edges = [[] for _ in range(n)]
for _ in range(n-1):
    from_, to = map(int, input().split())
    edges[from_-1].append(to-1)
    edges[to-1].append(from_-1)


DP = [10**9+7] * (n+1)
DP[0] = 0
ans = [0] * n


def dfs(node):
    node = node
    num = A[node]
    update_idx = bisect.bisect_left(DP, num)
    prev = DP[update_idx]
    DP[update_idx] = num
    lis = bisect.bisect_left(DP, 10**9+1) - 1
    ans[node] = lis
    for nex in edges[node]:
        if not ans[nex]:
            dfs(nex)

    DP[update_idx] = prev


dfs(0)
for res in ans:
    print(res)


Measured memory_bytes: 224796
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>

double length(double x_i, double y_i, double x_j, double y_j){
    return ::sqrt((x_i - x_j) * (x_i - x_j) + (y_i - y_j) * (y_i - y_j));
}

int factorial(int n){
    int a = 1;
    for (int i = 1; i <= n; i++)
    {
        a *= i;
    }
    return a;
}

int main(){
    int n = 0;
    std::cin >> n;
    std::vector<int> x(n), y(n);
    for (int i = 0; i < n; i++)
    {
        std::cin >> x[i] >> y[i];
    }
    
    std::vector<int> vec(n);
    for (int i = 0; i < n; i++)
    {
        vec[i] = i;
    }
    
    double length_sum = 0; 
    do{
        for(int i = 0; i < n - 1; i++){
            length_sum += length(x[vec[i]], y[vec[i]], x[vec[i + 1]], y[vec[i + 1]]);
        }
    }while(std::next_permutation(vec.begin(), vec.end()));

    std::cout << std::fixed << std::setprecision(7) << length_sum / factorial(n) << std::endl;

    return 0;
}

Measured memory_bytes: 3744
#include <bits/stdc++.h> 
using namespace std;
#define ll long long 
#define mod 998244353
#define MOD 998244353
ll fact[1000001];
void fast(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
inline ll mul(ll a,ll b){
  a=(a+MOD)%MOD;
  b=(b+MOD)%MOD;
  ll c=a*b;
  c=(c+MOD)%MOD;
  return c;
}
inline ll add(ll a,ll b){
  a=(a+MOD)%MOD;
  b=(b+MOD)%MOD;
  ll ans=(a+b);
  ans=(ans+MOD)%MOD;
  return ans;
}

ll power(ll a,ll b,ll c){
  ll ans=1;
  while(b){
    if(b&1){
      ans=mul(ans,a);
    }
    a=mul(a,a);
    b=b/2;
  }
  return ans;
}
ll ncr(ll n,ll r){
    if(r>n)return 0;
    if(r==n)return 1;
    ll xx=fact[n];
    xx=xx*power(fact[r],mod-2,mod)%mod;
    xx=xx*power(fact[n-r],mod-2,mod)%mod;
    return xx;
}
int main() 
{ 
    fast();
    int h1,m1,h2,m2,k;
    cin>>h1>>m1>>h2>>m2>>k;
    int z1=h1*60+m1;
    int z2=h2*60+m2;
    z2-=z1;
    z2-=k;
    cout<<z2;
    return 0; 
} 


Measured memory_bytes: 3648
/* winners never quit and quitters never win.
      #swap                                 */
      
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

template <typename T>
void make_unique(vector<T>& vec) {
  sort(all(vec));
  vec.erase(unique(all(vec)), vec.end());
}

#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll> 
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)


ll nxt() {
    ll TemporaryVariable;
    cin >> TemporaryVariable;
    return TemporaryVariable;
}

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

 
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	//isprime();
	//coeff();
	/*freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);*/
    ll a=nxt(),b=nxt(),c=nxt(),k=nxt();
    ll temp=min(k,a);
    ll ans=temp;
    k-=temp;
    temp=min(k,b);
    k-=temp;
    temp=min(c,k);
    ans-=temp;
    cout<<ans<<endl;
    
	printclock;
	return 0;
}

Measured memory_bytes: 3808
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Convert;
using static System.Console;
using static System.Math;
class MainClass
{
	static void Main(string[] args)
	{
		Answer();
	}
	public static void Answer()
	{
      var n = int.Parse(Console.ReadLine());
      var list = Console.ReadLine().Split(' ').Select(int.Parse).ToList();

      var dicNumandCnt = new Dictionary<int, int>();
      foreach(var a in list)
      {
        for (int i = a - 1; i <= a+1; i++)
        {
          if (dicNumandCnt.ContainsKey(i))
          {
            dicNumandCnt[i]++;
          }
          else
          {
            dicNumandCnt.Add(i, 1);
          }
        }
      }
      Console.WriteLine(dicNumandCnt.Values.Max());
	}
}


Measured memory_bytes: 21728
n,m=gets.split.map &:to_i
s=[]
m.times do |i|
  s[i]=gets.split.map &:to_i
end
a=gets.split.map &:to_i
ans=0
ans1=0
(0..1).to_a.repeated_permutation(n).each do |i|
  ans=0
  for j in 0...m
    sum=0
    s[j][0].times do |z|
      sum+=i[s[j][z+1]-1]
    end
    ans+=1 if (sum%2)==a[j]
    end
  ans1+=1 if ans==m
end
puts ans1


Measured memory_bytes: 14248
d=int(input())
c=list(map(int,input().split()))
s=[]
for i in range(d):
    t=list(map(int,input().split()))
    s.append(t)

fuman=[0]*26

for i in range(d):
    if i<=5:
        p=s[i].index(max(s[i]))
        print(p+1)
    else:
        p=fuman.index(max(fuman))
        print(p+1)
        
    for j in range(26):
        fuman[j]+=c[j]
    fuman[p]=0

Measured memory_bytes: 9304
/*
  AOJ 0318
  Programing contest
  @kankichi573
 */
#include <stdio.h>
#include <string.h>

int n;
char table[101];

int main()
{
  int x,i,j,p;

  memset(table,0,sizeof(table));
  
  scanf("%d",&n);

  for(i=0;i<n;i++)
    {
      scanf("%d",&p);
      for(j=1;j<=p;j++)
	table[j]++;
    }
    
  #ifdef DEBUG  
  for(i=1;i<=n;i++)
  	printf("%d ",table[i]);
  printf("\n");
  #endif

  for(i=1;i<=n;i++)
    if(i>table[i])
      {
	printf("%d\n",i-1);
	return(0);
      }
  printf("%d\n",n);
  return(0);
  }

Measured memory_bytes: 596
package main

import (
	"fmt"

	"os"
	"io"
	"bufio"
	"bytes"

	"strings"
	"strconv"
	// "sort"
)

//  長い入力を読む
func ReadLongLines(times int) ([]string, error) {
	result := make([]string, times)
	reader := bufio.NewReader(os.Stdin)
	buffer := bytes.NewBuffer(make([]byte, 0))
	readBytes := int64(2)
	for i := 0; i < times; i++ {
		for {
			readBuf, isPrefix, err := reader.ReadLine()
			// fmt.Printf("Reader.Read: %d\n", len(readBuf))
			readBytes += int64(len(readBuf) + 1)
			if err != nil {
				if err == io.EOF {
					fmt.Println("EOF")
					break
				} else {
					return result, err
				}
			}
			_, err = buffer.Write(readBuf)
			if err != nil {
				return result, err
			}
			// end of line
			if !isPrefix {
				result[i] = buffer.String()
				buffer.Reset()
				// reader = bufio.NewReader(os.Stdin)
				break
			}
		}
	}
	// 先読みしてしまうようなので、戻しておく
	os.Stdin.Seek(-int64(reader.Buffered()), os.SEEK_CUR)
	return result, nil
}

// 出力 for 競プロ
type Console struct {
	writer *bufio.Writer
}
func NewConsole() Console {
	return Console{ bufio.NewWriter(os.Stdout) }
}
func (this *Console) Printf(format string, a ...interface{}) {
	fmt.Fprintf(this.writer, format, a...)
}
func (this *Console) Println(s string) {
	fmt.Fprintln(this.writer, s)
}
func (this *Console) Close() {
	this.Flush()
}
func (this Console) Flush() {
	this.writer.Flush()
}

// sort 済み配列から list[i] < n となる最初の要素のインデックスを返す。なければ n
func LowerBound(list []int, key int) int {
	return LowerBoundProc(list, key,  -1, len(list))
}

func LowerBoundProc(list []int, key int, begin int, end int) int {
	mid := (end - begin) / 2 + begin
	if (end - begin) == 1 {
		return end
	}
	if list[mid] < key {
		return LowerBoundProc(list, key, mid, end)
	} else {
		return LowerBoundProc(list, key, begin, mid)
	}
}

func UpperBound(list []int, key int) int {
	return UpperBoundProc(list, key, -1, len(list))
}

func UpperBoundProc(list []int, key int, begin int, end int) int {
	mid := (end - begin) / 2 + begin
	// fmt.Printf("key: %d = %d %d %d\n", key,  begin, mid, end)
	if 1 == end - begin {
		return end
	}
	if list[mid] <= key {
		// fmt.Println("key is large")
		return UpperBoundProc(list, key, mid, end)
	} else {
		return UpperBoundProc(list, key, begin, mid)
	}
}

func PrintList(list []int) {
	con := NewConsole()
	defer con.Flush()
	for i := range list {
		if i == 0 {
			con.Printf("%d", list[i])
		} else {
			con.Printf(" %d", list[i])
		}
	}
	con.Println("")
}


func main() {
	con := NewConsole()
	defer con.Flush()

	// create list
	var n int
	fmt.Scanf("%d", &n)

	list := make([]int, n)
	lines, _ := ReadLongLines(1)

	elements := strings.Split(lines[0], " ")
	for i := range elements{
		x, _ := strconv.Atoi(elements[i])
		list[i] = x
	}

	var q int
	fmt.Scanf("%d", &q)
	lines, _ = ReadLongLines(q)

	for i := range lines {
		x, _ := strconv.Atoi(lines[i])
		lb := LowerBound(list, x)
		ub := UpperBound(list, x)
		con.Printf("%d %d\n", lb, ub)
	}
}


Measured memory_bytes: 11528
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<bitset>
#include<string>
#include<stack>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<cstring>
#include<complex>
#include<cmath>
#include<iomanip>
#include<numeric>
#include<algorithm>
#include<list>
#include<functional>
#include<cassert>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a));
#define MEM_1(a) memset((a),-1,sizeof(a));
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<double> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const ll MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) {ll ret=1; for(;b;b>>=1,a=(a*a)%MMM)if(b&1)ret=(ret*a)% MMM; return ret; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { if (a == 0 || b == 0)return a + b; return a*(b / gcd(a, b)); }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };

char s[200002];
int n,d[200002];
Vi v[26];
int go(int N)
{
	if(N>n)return 0;
	int &ret=d[N];
	if(ret!=-1)return ret;
	ret=INF;
	fup(i,0,25,1)
	{
		int next=*upper_bound(ALL(v[i]),N);
		ret=min(ret,1+go(next));
	}
	return ret;
}
void track(int N)
{
	if(N>n)return;
	int res=INF,next=-1,what=-1;
	fup(i,0,25,1)
	{
		int nn=*upper_bound(ALL(v[i]),N);
		if(1+go(nn)<res)
		{
			res=1+go(nn);
			next=nn;
			what=i;
		}
	}
	printf("%c",what+'a');
	track(next);
}
int main() {
	MEM_1(d);
	scanf("%s",s+1);
	n=strlen(s+1);
	fup(i,1,n,1)v[s[i]-'a'].pb(i);
	fup(i,0,25,1)v[i].pb(n+1);
	track(0);
}

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

int main(){


  int W,H;
  cin>>W>>H;

  int x,y,r;
  cin>>x>>y>>r;

  if(x - r >= 0 && x + r <= W && y - r >= 0 && y + r <= H)
 cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;
}



Measured memory_bytes: 3096
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<functional>
#include<queue>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
typedef long long int ll;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<signed(n);i++)
#define EREP(i,n) for(int i=1;i<=signed(n);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
//#define EVEL 1
#ifdef EVEL
#define DEB(X) cout << #X <<":" <<X<<" " ;
#define TF(f) f ? cout<<"true  " : cout<<"false ";
#define END cout<<"\n";
#else
#define DEB(X) {}
#define TF(f) {}
#define END {}
#endif
const ll MOD = 1000000007;
const ll INF = 9e14;
typedef std::pair<ll,ll> P;
struct edge{ int to,cost; };
#define VMAX 100000

ll N,M,K,Q;
ll A[30][30],B,C[30],D,X;

ll ans=INF;
bool F=false;
string S,T,U;
int main(){
    cin>>N>>M>>X;
    REP(i,N){
        cin>>C[i];
        REP(j,M){
            cin>>A[i][j];
        }
    }

    REP(bit,1<<N){
        ll a[30],b=0;
        REP(i,30)a[i]=0;
        REP(i,N){
            if(bit&(1<<i)){
                b+=C[i];
                REP(j,M){
                    a[j]+=A[i][j];
                }
            }
        }
        bool f=true;
        REP(i,M){
            if(X>a[i]){f=false;break;}
        }
        if(f){ans=min(b,ans);DEB(b)END}
    }
    cout<<(ans==INF?-1:ans)<<endl;
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define mx INT_MAX
#define mn INT_MIN
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define itr set<int> :: iterator
const int N = 1e5+10;

ll dp[100000];

ll solve(vector<int> h){
   int n = h.size();

   dp[0] = 0;
   dp[1] = abs(h[1]-h[0]);

   for(int i=2; i<n; i++){
      dp[i] = min(dp[i-1]+abs(h[i]-h[i-1]), dp[i-2]+abs(h[i]-h[i-2]));
   }

   return dp[n-1];
}

int main(){

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

    // ******* START HERE ******
    
    int n;
    cin>>n;

    vector<int> h;
    for(int i=0; i<n; i++){
      int x;
      cin>>x;
      h.pb(x);
    }

    memset(dp, 0, sizeof(dp));
    ll res = solve(h);
    cout<<res<<endl;
    // ***** END HERE ***** 
    return 0;
}

Measured memory_bytes: 4824
#include<stdio.h>
int main(void)
{
	int h1,h2,k1,k2,a,b,c,d,xh1,xh2,yk1,yk2,xx,yy,xxx,yyy,x1,x2,y1,y2,x,y;
	scanf("%d %d %d %d %d %d %d %d",&h1,&h2,&k1,&k2,&a,&b,&c,&d);
	xh1=h1*a;
	xh2=h2*b;
	yk1=k1*a;
	yk2=k2*b;
	x1=h1/10;
	y1=k1/10;
	x2=h2/20;
	y2=k2/20;
	xxx=(x1*c)+(x2*d);
	yyy=(y1*c)+(y2*d);
	x=xxx+xh1+xh2;
	y=yyy+yk1+yk2;
	if(x==y){
		printf("even\n");
	}
	else if(y<x){
		printf("hiroshi\n");
	}
	else if(x<y){
		printf("kenjiro\n");
	}
	return 0;
}


Measured memory_bytes: 2148
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int A,B,N;
int a[100005];

bool judge(long long x)
{
    long long sum=0;
    for(int i=0;i<N;i++)
    {
        long long t=a[i]-B*x;

        if(t>0)
        sum+=(int)ceil(t*1.0/(A-B));

        if(sum>x||sum<0)
            return false;
    }
    return true;
}
int main()
{
    int  imax=0;
 scanf("%d%d%d",&N,&A,&B);
 for(int i=0;i<N;i++)
    scanf("%d",&a[i]),imax=max(a[i],imax);
    long long l=0,r=imax;
    while(l<r)
    {
        long long mid=(l+r)/2;
        if(judge(mid))
            r=mid;
        else
            l=mid+1;
    }
    printf("%lld\n",r);
    return 0;
}


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

long long int MOD = 1000000007;
long long int n, m;

long long int facto(long long int a){
  if(a == 1){
    return 1;
  }
  long long int ret = a * facto(a - 1);
  ret %= MOD;
  return ret;
}

int main(){
  cin >> n >> m;
  long long int ans;
  //cerr << facto(m) << " "<< facto(n) << endl;
  if(n == m){
    ans = facto(m) * facto(n);
    ans %= MOD;
    ans *= 2;
    ans %= MOD;
  }else if(abs(m - n) == 1){
    ans = facto(n) * facto(m);
    ans %= MOD;
  }else{
    ans = 0;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < n; i++)
typedef long long ll;
// Welcome to my source code!

int main() {
    int n, ans = 0;
    string s;
    cin >> n >> s;

    REP(i,n) {
        if (s[i] == s[i-1]) {
            continue;
        } else {
            ans++;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 512
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    double cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(i%2!=0)
        {
            cnt++;
        }
    }
    double sum=cnt/n;
    printf("%.10lf\n",sum);
    return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
#define N 200
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
int n,m,h[N],a,b,c,X;
vector<int> edge[N];
vector<int> v;
vector<P1> e;
bool used[N];
int par[N];
set<int> s;

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

void unite(int x,int y){
  x=find(x),y=find(y);
  if(x!=y)par[x]=y;
}

bool check(int A,int B){
  bool x[N];
  memset(x,0,sizeof(x));
  for(int i=0;i<n;i++)par[i]=i;
  for(int i=0;i<e.size();i++){
    int n1=e[i].second.first;
    int n2=e[i].second.second;
    if(!(A<=h[n1]&&h[n1]<=B))continue;
    if(!(A<=h[n2]&&h[n2]<=B))continue;
    if(find(n1)!=find(n2)){
      unite(n1,n2);
      x[n1]=x[n2]=true;
    }
  }
  bool res=true;
  int cnt=0;
  for(int i=0;i<n;i++){
    if(!(A<=h[i]&&h[i]<=B))continue;
    if(!x[i])res=false,cnt++;
  }
  return res;
}

void dfs(int x){
  used[x]=true;
  for(int i=0;i<edge[x].size();i++){
    int nx=edge[x][i];
    if(!used[nx])dfs(nx);
  }
}

int kruskal(){
  int res=0,X2=(int)v.size()-1;
  for(int i=0;i<n;i++)par[i]=i;
  if(X>=0){
    for(int i=0;i<e.size();i++){
      int n1=e[i].second.first;
      int n2=e[i].second.second;
      if(!(v[X]<=h[n1]&&h[n1]<=v[X2]))continue;
      if(!(v[X]<=h[n2]&&h[n2]<=v[X2]))continue;
      if(find(n1)!=find(n2)){
	unite(n1,n2);
	res+=e[i].first;
      }
    }
  }
  if(X==-1)X=X2+1;
  for(int k=X-1;k>=0;k--){
    for(int i=0;i<e.size();i++){
      int n1=e[i].second.first;
      int n2=e[i].second.second;
      if(!(v[k]<=h[n1]&&h[n1]<=v[X2]))continue;
      if(!(v[k]<=h[n2]&&h[n2]<=v[X2]))continue;
      if(find(n1)!=find(n2)){
	unite(n1,n2);
	res+=e[i].first;
      }
    }
  }
  return res;
}

int main(){
  while(1){
    cin>>n>>m;
    if(!n&&!m)break;
    for(int i=0;i<n;i++){
      cin>>h[i];
      s.insert(h[i]);
    }
    for(int i=0;i<m;i++){
      cin>>a>>b>>c;
      edge[a-1].push_back(b-1);
      edge[b-1].push_back(a-1);
      e.push_back(P1(c,P(a-1,b-1)));
    }
    sort(e.begin(),e.end());
    set<int>::iterator ite;
    for(ite=s.begin();ite!=s.end();ite++)
      v.push_back(*ite);
    v.push_back(v[0]);
    sort(v.begin(),v.end());
    X=-1;
    for(int i=0;i<v.size();i++)
      if(!check(v[i],v[(int)v.size()-1])){
	X=i-1;
	break;
      }
    memset(used,0,sizeof(used));
    dfs(0);
    int f=0;
    for(int i=0;i<n;i++)
      if(!used[i])f=1;
    if(f)cout<<0<<endl;
    else cout<<kruskal()<<endl;
    for(int i=0;i<n;i++)edge[i].clear();
    e.clear();
    v.clear();
    s.clear();
  }
  return 0;
}

Measured memory_bytes: 1804
package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func intScanner() int {
	n, _ := strconv.Atoi(Scanner())
	return n
}

func floatScanner() float64 {
	n, _ := strconv.ParseFloat(Scanner(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func stringSliceScanner(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scanner()
	}
	return a
}
func intSliceScanner(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = intScanner()
	}
	return a
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}

var mod int

func main() {
	mod = 1000000007
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	n := intScanner()
	m := intScanner()
	edges := make([][]int, n)
	for i := 0; i < m; i++ {
		u, v := intScanner()-1, intScanner()-1
		edges[u] = append(edges[u], v)
	}
	s := intScanner() - 1
	t := intScanner() - 1
	q := make([]int, 1, n)
	q[0] = s
	d := [][]int{make([]int, n), make([]int, n), make([]int, n)}
	for i := 0; i < n; i++ {
		d[0][i] = -1
		d[1][i] = -1
		d[2][i] = -1
	}
	d[0][s] = 0
	for len(q) != 0 {
		x := q[0]
		q = q[1:]
		if d[0][x] != -1 {
			for _, v := range edges[x] {
				if d[1][v] == -1 {
					q = append(q, v)
					d[1][v] = d[0][x] + 1
				}
			}
		}
		if d[1][x] != -1 {
			for _, v := range edges[x] {
				if d[2][v] == -1 {
					q = append(q, v)
					d[2][v] = d[1][x] + 1
				}
			}
		}
		if d[2][x] != -1 {
			for _, v := range edges[x] {
				if d[0][v] == -1 {
					q = append(q, v)
					d[0][v] = d[2][x] + 1
				}
			}
		}
	}
	if d[0][t] == -1 {
		fmt.Println(-1)
	} else {
		fmt.Println(d[0][t] / 3)
	}
}


Measured memory_bytes: 13828
import sys
n = int(input())
words=[]
for i in range(n):
    words += [input()]
for i in range(n-1):
    for j in range(i+1, n):
        if words[i] == words[j]:
                print('No')
                sys.exit()
    if words[i][-1] != words[i+1][0]:
        print('No')
        sys.exit()
print('Yes')

Measured memory_bytes: 3064
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N=in.nextInt();
String dummy = in.nextLine();
String S=in.nextLine();
int x=0; int max=0;
for (int i=0; i<N; i++)
{
char c=S.charAt(i);
if(c=='I')
x++;
else
x--;
if(x>max)
max=x;
}
System.out.println(max);
}
}


Measured memory_bytes: 22612
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;

typedef double weight;
struct edge
{
	int to; weight cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};

typedef vector<edge> Edges;
typedef vector<Edges> Graph;
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
int place(int x, int y, int w) { return w*x + y; }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

void dijkstra(Graph &g, vector<weight> &d, int s)
{
	d.assign(g.size(), INF);
	d[s] = 0;
	typedef pair<weight, int> P;
	priority_queue<P, vector<P>, greater<P>> que;
	que.push(P(0, s));
	while (!que.empty())
	{
		weight dist = que.top().first;
		int v = que.top().second;
		que.pop();
		if (d[v] < dist) continue;
		REP(i, g[v].size())
		{
			edge e = g[v][i];
			if (d[e.to] > d[v] + e.cost)
			{
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int n, m, st, go;
int ind(int v, int from, int sp) { return n*n*sp + n*from + v; }

int main()
{
	while (cin >> n >> m >> st >> go, n)
	{
		st--; go--;
		Graph g(n*n * 32);
		REP(i, m)
		{
			int a, b, c; double d;
			cin >> a >> b >> d >> c;
			a--; b--;
			FOR(j, 1, c + 1)
			{
				for (int k = -1; k <= 1;k++)
				{
					int sp = j + k;
					if (j<1 || j>c) continue;
					REP(l, n)
					{
						if (l == b) continue;
						g[ind(a, l, j)].push_back(edge{ ind(b, a, sp), d / j });
					}
					REP(l, n)
					{
						if (l == a) continue;
						g[ind(b, l, j)].push_back(edge{ ind(a, b, sp), d / j });
					}
				}
			}
		}
		vector<weight> d;
		dijkstra(g, d, ind(st, st, 1));
		double ans = INF;
		REP(i, n) chmin(ans, d[ind(go, i, 0)]);
		if (ans >= INF) puts("unreachable");
		else cout << D10 << ans << endl;
	}
	return 0;
}

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

int d[100002];

int main(){
	int n;
	cin >> n;
	int a[n+1];
	for(int i=1; i<=n; i++) cin >> a[i]; 
	for(int i=1; i<=n; i++){
		d[a[i]+1]++;
		d[a[i]]++;
		d[a[i]-1]++;
	}
	int maxv=-1;
	for(int i=0; i<=100001; i++){
		if(d[i]>maxv) maxv=d[i];
	}
	cout << maxv;
	return 0;
}

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

int main() {
  int n,m,a;
  cin >> n >> m;
  a = 0;
  if (n > 0) 
    a += n*(n-1)/2;
  if (m > 0)
    a += m*(m-1)/2;
  cout << a << '\n';
}


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

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

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

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

signed main() {
	string s;
  cin >> s;
  ll num = stoi(s);
  int sum = 0;
  rep(i, s.size()) {
    sum += (s[i] - '0');
  }
  if(num % sum == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 256
n,m,l=map(int,input().split())
b=[]
for i in range(n):
    x= list(map(int, input().split()))
    b.append(x)
y=[]
for i in range(m):
    z= list(map(int, input().split()))
    y.append(z)
c = [[0] * l for i in range(n)]
for i in range(n):
    for j in range(l):
        for k in range(m):
            c[i][j] += b[i][k] * y[k][j]
    print(*c[i])  #*をつけると[]が外れて表示される


Measured memory_bytes: 7036
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>

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

using namespace std;
int main() {
	int a, b;
	cin >> a >> b;

	if (a == 1)
		a = 14;
	if (b == 1)
		b = 14;

	if (a == b)
		cout << "Draw" << endl;
	else if (a < b)
		cout << "Bob" << endl;
	else
		cout << "Alice" << endl;

	return 0;
}


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

using namespace std;

int main()
{
    long long int h;

    cin >> h;

    long long int ans = 0;

    for (int i = 0; i < 60; i++)
    {
        ans = (1LL << i) - 1;

        if (ans >= h)
            break;
    }

    cout << ans << "\n";

    return 0;
}

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

using namespace std;

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

    vector<vector<bool>> neighbor(N+1, vector<bool>(N+1, false));
    for(int i=0; i<M; i++){
        int a,b;
        cin >> a >> b;
        neighbor.at(a).at(b) = true;
        neighbor.at(b).at(a) = true;
    }

    vector<int> perm(N);
    for(int i=0; i<N; i++)perm.at(i) = i+1;

    int res = 0;
    do{
        bool flag = true;
        for(int i=0; i<N-1; i++){
            int now = perm.at(i);
            int next = perm.at(i+1);
            if(!neighbor.at(now).at(next)){
                flag = false;
                break;
            }
        }
        if(flag)res++;
    }while(next_permutation(perm.begin()+1, perm.end()));

    cout << res << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main () {
	double a, b, x;
	cin >> a>>b>>x;
	double c=b-(x/a/a*2-b);
	if (c <= b){
		printf("%.20lf\n", 90-atan(a/c)/acos(-1)*180);
	}
	else {
		c = x/a*2/b;
		printf("%.20lf\n", 90-atan(c/b)/acos(-1)*180);
	}
}


Measured memory_bytes: 256