akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define Phuong_ ios_base::sync_with_stdio(0); cin.tie(0);

/// easy access -------------
using namespace std;
using ll = long long;
using ld = long double;
typedef pair<int,int> pii;
///-------------------------


int main()
{
    Phuong_; /// <3
    int a,b;
    cin >> a >>b;
    if (b>=a) cout << "unsafe"; else cout << "safe";
    return 0;
}


Measured memory_bytes: 3636
s = int(input())
a = [s]
n = s

while True:
    if n % 2 == 0: # nが偶数のとき
        next = n // 2
    else: # nが奇数のとき
        next = 3 * n + 1
    if next in a:
        ans = len(a) + 1
        break
    a.append(next)
    n = next

print(ans)

Measured memory_bytes: 3060
// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
#define N 514
void build(){

}
int n;
int cnt[ N ] , tcnt[ N ];
char c[ N ];
void init(){
  n = getint();
  for( int i = 0 ; i < 26 ; i ++ )
    cnt[ i ] = N;
  while( n -- ){
    scanf( "%s" , c );
    for( int i = 0 ; i < 26 ; i ++ )
      tcnt[ i ] = 0;
    for( int i = 0 ; c[ i ] ; i ++ )
      tcnt[ c[ i ] - 'a' ] ++;
    for( int i = 0 ; i < 26 ; i ++ )
      cnt[ i ] = min( cnt[ i ] , tcnt[ i ] );
  }
}
void solve(){
  for( int i = 0 ; i < 26 ; i ++ )
    while( cnt[ i ] -- )
      putchar( 'a' + i );
  puts( "" );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


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

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

  int N; cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= i + 1;
  }
  sort(A.begin(), A.end());
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += abs(A[i] - A[N/2]);
  }
  cout << ans << "\n";
}

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

int main()
{
  int n;
  cin >> n;
 string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i< n-2;i++)
  {
   if (s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C') 
     cnt++;
  }
  cout << cnt << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>

using namespace std;

char migi[11] = {'y', 'u', 'i', 'o', 'p', 'h', 'j', 'k', 'l', 'n', 'm'};

bool ismigi(char c) {
  for (int j=0; j<11; j++) {
    if (migi[j] == c) {
      return true;
    }
  }
  return false;
}

int main() {
  string s;
  while( cin >> s && s != "#" ) {
    int ans = 0;
    bool right = ismigi(s[0]);
    for (unsigned int i=0; i<s.size(); i++) {
      if (ismigi(s[i]) != right) {
	ans++;
	right = !right;
      }
    }
    printf("%d\n", ans);
  }
}

Measured memory_bytes: 1180
s=input()
t=""
a=[0]
for i in s:
  if i!="x":
    t+=i
    a.append(0)
  else:a[-1]+=1
if t!=t[::-1]:print(-1);exit()
x=0
for i in range(len(a)//2):x+=abs(a[i]-a[-i-1])
print(x)

Measured memory_bytes: 4116
/*
 * @Author: Kaizyn
 * @Date: 2020-03-28 21:04:12
 * @LastEditTime: 2020-03-28 21:05:12
 */
#include <bits/stdc++.h>

// #define DEBUG

using namespace std;

const int N = 1e2+7;
const int MOD = 998244353;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;

string s;

inline void solve()
{
    cin >> s;
    cout << (s[2] == s[3] && s[4] == s[5] ? "Yes" : "No") << endl;
}

signed main()
{
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int testcase = 1;
    // cin >> testcase;
    for (int i = 1; i <= testcase; ++i) {
        solve();
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	
	int a[2][n];
	
	for (int i = 0; i < n; i++) {
	    cin >> a[0][i];
	}
	for (int i = 0; i < n; i++) {
	    cin >> a[1][i];
	}
	
	int rt[n], ans = 0;
	for (int i = 0; i < n; i++) {
	    rt[i] = 0;
	}
	
	for (int i = 0; i < n; i++) {
	    rt[i] = 0;
	    
	    for (int j = 0; j < i + 1; j++) {
	        //rtに0～i  番目までのa[0]を足す
	        rt[i] += a[0][j];
	        //std::cout << "rt[" << i << "]にa[0][" << j << "]の " << a[0][j] << " を足した" << std::endl;
	    }
	    for (int j = i; j < n ; j++) {
	        //rtにi～n-1番目までのa[1]を足す
	        rt[i] += a[1][j];
	        //std::cout << "rt[" << i << "]にa[1][" << j << "]の " << a[1][j] << " を足した" << std::endl;
	    }
	    
	    //std::cout << i + 1 << "列目で縦に動くと " << rt[i] << std::endl;
	    
	    ans = max(rt[i], ans);
	}
	
	/*
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
	        rt[i] += a[0][j];
	        std::cout << "途中 " << rt[i] << std::endl;
	    }
	    //std::cout << rt[i] << std::endl;
	    ans = max(rt[i], ans);
	    rt[i] = 0;
	}
	*/
	
	std::cout << ans << std::endl;
	
	return 0;
}

Measured memory_bytes: 256
import java.util.*;
class Main {
    public static void main(String[] r) {
        Scanner s=new Scanner(System.in);
        while(true){
            int n=s.nextInt(),m=s.nextInt();
            if(n<1)break;
            int i,j;
            int[] a=new int[n];
            for(i=0;i<n;i++)a[i]=s.nextInt();
            int M=0;
            for(i=0;i<n;i++)for(j=i+1;j<n;j++)M=Math.max(M,(a[i]+a[j]<=m)?a[i]+a[j]:0);
            System.out.println((M<1)?"NONE":M);
        }
    }
}


Measured memory_bytes: 50924
# 白ます数 - 最短距離が答え

import sys
readline = sys.stdin.readline

H,W = map(int,readline().split())

G = [None] * H
white = 0
for i in range(H):
  G[i] = readline().rstrip()
  white += G[i].count(".")

from collections import deque
q = deque([])
# y,x, 距離
q.append([0, 0, 0])
seen = set()
cases = ((0,1),(0,-1),(1,0),(-1,0))
while q:
  y,x,d = q.popleft()
  if G[y][x] == "#":
    continue
  if (y,x) in seen:
    continue
  seen.add((y,x))
  if y == H - 1 and x == W - 1:
    print(white - d - 1)
    break
  for c in cases:
    if 0 <= y + c[0] < H and 0 <= x + c[1] < W:
      q.append([y + c[0], x + c[1], d + 1])
else:
  print(-1)


Measured memory_bytes: 9592
N = int(input())
prime = [2]
Res = 1

for i in range(3, N+1, 2):
    flag = True
    for j in prime:
        if i % j == 0:
            flag = False
            break
    if flag:
        prime.append(i)

div = [0]*len(prime)

for i in range(2, N+1):
    for j in range(len(prime)):
        while i % prime[j] == 0:
            div[j] += 1
            i = i//prime[j]

for i in div:
    Res *= i+1
    Res %= 10**9+7

print(Res)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0;i < n;i++)
#define rev(i, n) for(int i = n;i >= 0;i--)
#define Rep(i, m, n) for(int i = m;i < n;i++)
#define repeatrev(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int
const int MAX = 510000;


using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;

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

template<class Abel> struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    UnionFind(int n = 1, Abel SUM_UNITY = 0) {
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0) {
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x) {
        root(x);
        return diff_weight[x];
    }

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

    bool merge(int x, int y, Abel w) {
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y), w = -w;
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y) {
        return weight(y) - weight(x);
    }
};


struct edge{ll now, to, cost;};

bool comp(const edge& e1, const edge& e2){
    return e1.cost < e2.cost;
}

edge es[MAX];
int V, E;//頂点数と辺の数

ll kruskal(){
    sort(es, es + E, comp); //edge.costが小さい順にsortする
    UnionFind<ll> uf(V);
    ll res = 0;
    rep(i, E){
        edge e = es[i];
        if(!uf.issame(e.now, e.to)){
            uf.merge(e.now, e.to, e.cost);
            //cout << e.now << " " << e.to << " " << e.cost << endl;
            res += e.cost;
        }
    }
    return res;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    V = n;
    LPP x_based[n];
    LPP y_based[n];
    rep(i, n){
        ll x, y;
        cin >> x >> y;
        x_based[i].first = x; x_based[i].second.first = y; x_based[i].second.second = i;
        y_based[i].first = y; y_based[i].second.first = x; y_based[i].second.second = i;
    }
    sort(x_based, x_based + n);//x, yベースで昇順に並び替え
    sort(y_based, y_based + n);
    rep(i, n - 1){
        es[i] = {x_based[i].second.second, x_based[i + 1].second.second, abs(x_based[i + 1].first - x_based[i].first)};
    }
    rep(i, n - 1){
        es[i + n - 1] = {y_based[i].second.second, y_based[i + 1].second.second, abs(y_based[i + 1].first - y_based[i].first)};
    }
    E = 2 * (n - 1) + 1;
    cout << kruskal() << endl;



}

Measured memory_bytes: 12800
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
 
int main()
{
    int n, m;
    cin >> n >> m;
    ll x[1007], y[1007], z[1007];
    for(int i = 0; i < n; i++){
        cin >> x[i] >> y[i] >> z[i];
    }
    ll ans = 0;
    for(int j = 0; j < 8; j++){
        ll s[1007] {0};
        for(int i = 0; i < n; i++){
            if(j == 0 || j == 1 || j == 2 || j == 3){
                s[i] += x[i];
            }
            else{
                s[i] -= x[i];
            }
            if(j == 0 || j == 1 || j == 4 || j == 5){
                s[i] += y[i];
            }
            else{
                s[i] -= y[i];
            }
            if(j == 0 || j == 2 || j == 4 || j == 6){
                s[i] += z[i];
            }
            else{
                s[i] -= z[i];
            }
        }
        sort(s, s + n, greater<ll>());
        ll ssum = 0;
        for(int i = 0; i < m; i++){
            ssum += s[i];
        }
        ans = max(ans, ssum);
    }
    cout << ans << endl;
}

Measured memory_bytes: 892
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>

using namespace std;

template<typename A, typename B> bool chmin(A &a, const B &b){ return b < a && (a = b, true); }
template<typename A, typename B> bool chmax(A &a, const B &b){ return a < b && (a = b, true); }

int main(){
  string s;
  cin >> s;
  if (s[0] == s[2]){
    cout << "Yes\n";
  }
  else{
    cout << "No\n";
  }
  return 0;
}

Measured memory_bytes: 256
#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 fst first
#define snd second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define EPS (1e-9)
#define equals(a,b) (fabs((a)-(b))<EPS)

using namespace std;

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

const string YES = "";
const string NO  = "";

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

void solve() {

}

int main() {
  int a,b,c,d,e;
  cin >> a >> b >> c >> d >> e;
  int v1 = a * 60 + b;
  int v2 = c * 60 + d;
  int tmp = v2 - v1;
  tmp -= e;
  if( tmp < 0 ) {
    cout << 0 << endl;
  } else {
    cout << tmp <<endl;
  }

  return 0;
}


Measured memory_bytes: 3628
#include <stdio.h>

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

	scanf("%d%d%d", &a, &b, &c);

	if(a>b)	{
		tmp = a;
		a = b;
		b = tmp;
	}
	if(b>c){
		tmp = b;
		b = c;
		c = tmp;
	}
	if(a>b){
		tmp = a;
		a = b;
		b = tmp;
	}

	printf("%d %d %d\n", a, b, c);

	return 0;
}

Measured memory_bytes: 596
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    a, b, k = list(map(int, readline().split()))
    res = []
    for i in range(a, min(a + k, b + 1)):
        res.append(i)
    for i in range(b, max(b - k, a - 1), -1):
        if i not in res:
            res.append(i)
    print(*sorted(res), sep="\n")


if __name__ == '__main__':
    solve()


Measured memory_bytes: 38384
#include <stdio.h>
  #define N 100
  int main(void){
   int i=1,j,count,count2,tmp,n,m=0,A[N];
   scanf("%d",&n);
       while(1){
     scanf("%d",&A[m]);
     m++;
     if(m>=n) break;
   }
   while(i<n){
     for(count=0;count<n-1;count++){
       printf("%d ",A[count]);
     }
     printf("%d\n",A[n-1]);
     tmp=A[i];
     j=i;
     i++;
     while(j>0&&A[j-1]>tmp){
       A[j]=A[j-1];
       j--;
     }
     A[j]=tmp;
   }
    count2=0;
    while(count2<n-1){
      printf("%d ",A[count2]);
      count2++;
    }
    printf("%d\n",A[n-1]);
    return 0;
  }



Measured memory_bytes: 2072
#include<bits/stdc++.h>
#define For(i,l,r) for(int i = (l),i##end = (r);i <= i##end;i++)
#define Fordown(i,r,l) for(int i = (r),i##end = (l);i >= i##end;i--)
#define debug(x) cout << #x << " = " << x << endl

using namespace std;

typedef long long ll;

template <typename T> inline bool chkmin(T &x,T y) { return y < x ? x = y,1 : 0; }
template <typename T> inline bool chkmax(T &x,T y) { return x < y ? x = y,1 : 0; }

const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10,mod = 1e9 + 7;

int a[N],fac[N];

inline int read() {
	int x = 0,flag = 1;
	char ch = getchar();
	while(!isdigit(ch) && ch != '-')ch = getchar();
	if(ch == '-')flag = -1,ch = getchar();
	while(isdigit(ch))x = (x << 3) + (x << 1) + (ch - '0'),ch = getchar();
	return x * flag;
}

inline int fpm(int a,int b) {
	int res = 1;
	while(b) {
		if(b & 1) res = 1ll * res * a % mod;
		a = 1ll * a * a % mod,b /= 2;
	}
	return res;
}

int main() {

	int n = read();
	For(i,1,n) a[i] = read();

	fac[0] = 1;
	For(i,1,n) fac[i] = 1ll * fac[i - 1] * i % mod;

	int val = 0;
	For(i,1,n) val = (val + fpm(i,mod - 2)) % mod;
	int res = 1ll * val * a[1] % mod;

	For(i,2,n) {
		val = (val + fpm(i,mod - 2)) % mod;
		val = (val - fpm(n - i + 2,mod - 2) + mod) % mod;
		res = (res + 1ll * a[i] * val) % mod;
	}

	printf("%lld\n",1ll * res * fac[n] % mod);

	return 0;
}

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

int main(){
	int n; cin>>n;
	int minvalue,maxvalue,temp;
	for(int i=0;i<n;i++){
		if(i==0) cin>>minvalue;
		else if(i==1){
			cin>>temp;
			maxvalue = temp-minvalue;
			minvalue = min(minvalue,temp);
		}else{
			cin>>temp;
			maxvalue = max(maxvalue,temp-minvalue);
			minvalue = min(minvalue,temp);
		}
	}
	cout<<maxvalue<<endl;
}

Measured memory_bytes: 3088
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev            coyhhhhhhyoc    
#define next          sdNNNmNNNNNNNmds  
#define y0           hNNNNy_yNNNNNN_sNh
#define y1          mNNNNNdtdNNNNNNtsNNm
#define yn          mNNNNNNNNy___smNNNms
#define tm            oooooosyysooooot 
#define read               tyhyt        
#define rank              ytmNmo         
#define index              yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

int a, b, c, d, n;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> n;
    rep(i, 1, n) {
        cin >> s;
        if(s == "AC") a++;
        else if(s == "WA") b++;
        else if(s == "TLE") c++;
        else if(s == "RE") d++;
    }
    cout << "AC x " << a << endl;
    cout << "WA x " << b << endl;
    cout << "TLE x " << c << endl;
    cout << "RE x " << d << endl;
    return 0;
}


Measured memory_bytes: 3656
#include <iostream>
#include <deque>
using namespace std;
int main()
{
    string s , t;
    cin >>s >> t;
    int n = s.size();
    for (int i = 0 ; i< n;i++){
        if (s==t){
            cout<<"Yes"<<endl;
            return 0 ;
        }
        s = s.back()+s;
        s.pop_back();
    }
    
    cout<<"No"<<endl;
    return 0;
}


Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,k;
    cin>>n>>k;
    long long a[n+1],sum=0;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    sort(a,a+n);
    for(int i=0;i<n-k;i++){
        sum+=a[i];
    }
    cout<<sum<<endl;
}

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

int main() {
  long long n, sum=0; cin >> n; 
  vector<int> a(n);
  for(int i=0;i<n;i++) cin >> a.at(i);
  for(int i=0;i<n-1;i++){
    if(a.at(i) > a.at(i+1)){
      sum += a.at(i) - a.at(i+1);
      a.at(i+1) += a.at(i) - a.at(i+1);
    }
  }
  cout << sum << endl;
  return 0;
}

Measured memory_bytes: 4000
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;

void fail(void) {
  cout << "Impossible\n";
  exit(0);
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VI a(n);
  int d = 0; // diameter
  REP(i, 0, n) {
    cin >> a[i];
    d = max(d, a[i]);
  }
  VI freq(d + 1, 0);
  REP(i, 0, n) {
    freq[a[i]] += 1;
  }
  if (d % 2 == 0) {
    int k = d / 2;
    REP(i, k + 1, d + 1) {
      if (freq[i] < 2) { fail(); }
    }
    if (freq[k] != 1) { fail(); }
    cout << "Possible\n";
    return 0;
  }
  int k = d / 2;
  REP(i, k + 2, d + 1) {
    if (freq[i] < 2) { fail(); }
  }
  if (freq[k + 1] != 2) { fail(); }
  cout << "Possible\n";
}


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

#define rep(i, n) for(int i=0; i<n; ++i)
#define repit(it, li) for(auto it=li.begin(); it!=li.end(); it++)
#define ll long long int

int main(){
   int d; 
   cin>>d;
   int c[26];
   rep(i, 26) cin>>c[i];
   int s[365][26];
   rep(i, d) rep(j, 26) cin>>s[i][j];

   int t[365];
   //rep(i, d){cin>>t[i]; t[i]--;}

   ll sc=0;
   int las[26];
   rep(i, 26) las[i]=-1;

   rep(k, d){
      int mx=0, tt;
      rep(i, 26){   
         int prof=c[i]*(k-las[i])+s[k][i];
         if(prof>mx){
            mx=prof;
            tt=i;
         }
      }
      t[k]=tt;  
   }


   rep(i, d){
      sc+=s[i][t[i]];
      las[t[i]]=i;
      rep(j, 26) sc -= c[j]*(i-las[j]);

   }

   rep(i, d) cout<<(t[i]+1)<<endl;
   //cout << sc << endl;


   return 0;
}


Measured memory_bytes: 3676
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
int main() {
    ll n,m;
    cin >> n >>m;
    ll sum = 0;
    for (int i = 0; i < m; i++) {
        ll x;
        cin >> x;
        sum += x;
    } 
    if (sum > n) {
        cout << -1;
    }
    else {
        cout << n - sum;
    }
    return 0;
}

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

string S;
string T;

int main() {
	cin >> S >> T;
	if (size(S) + 1 == size(T)) {
		for (int i = 0; i < size(T) - 1; i++) {
			if (S[i] != T[i]) {
				cout << "No";
				return 0;
			}
		}
		cout << "Yes";
	}
	else {
		cout << "No";
	}
}

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

int main() {
	
	ll n, k; cin >> n >> k;
	vector<ll> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		a[i]--;
	}
	vector<ll> visit(n + 1, -1);
	int higePlusCycleLength = 0;
	int target = 0;
	while (visit[target] == -1) {
		visit[target] = higePlusCycleLength;
		higePlusCycleLength++;
		target = a[target];
	}
	int cycle = higePlusCycleLength - visit[target];
	int index = 0;
	if (k <= higePlusCycleLength) {
		
		for (int i = 0; i < k; i++) {
			index = a[index];
		}
		cout << index + 1 << endl;
	}
	else {
		for (int i = 0; i < higePlusCycleLength; i++) {
			index = a[index];
		}
		k -= higePlusCycleLength;
		k %= cycle;
		for (int i = 0; i < k; i++) {
			index = a[index];
		}
		cout << index + 1 << endl;
	}
    return 0;
}

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

#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define setbitsll(x) __builtin_popcountll(x)
#define zerobitsll(x) __builtin_ctzll(x)
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef map<int,int> mii;

const int MOD = 1e9+7;
const int maxN = 2e5+1;
// ll inv[maxN];
ll fact[maxN];

ll power (ll x,ll n) {
	ll res = 1;
	while (n) {
		if (n&1) {
			res = (res*x)%MOD;
		}
		n = n/2;
		x = (x*x)%MOD;
	}
	return res;
}

ll inv (ll x) {
	return power(x,MOD-2);
}

ll nCr (ll n,ll k) {
	if (k > n-k) {
		k = n-k;
	}
	ll ret = fact[n];
	ret = (ret*inv(fact[n-k]))%MOD;
	ret = (ret*inv(fact[k]))%MOD;
	return ret;

}
  
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	// inv[1] = 1;
	fact[0] = fact[1] = 1;
	for (ll i = 2;i < maxN;++i) {
		// inv[i] = (MOD-(MOD/i)*inv[MOD%i]%MOD)%MOD;
		fact[i] = (fact[i-1]*i)%MOD;
	}
	// for (int i = 1;i <= 10;++i) {
	// 	cout <<  inv[i] << " ";
	// }
	// cout << '\n';
	ll n,k;
	cin >> n >> k;
	ll ans = 0;
	for (int i = 0;i < min(k+1,n);++i) {
		ll x = 1; 
		x = (x*nCr(n,i))%MOD;
		x = (x*nCr(i+n-i-1,n-i-1))%MOD;
		ans = (ans+x)%MOD;
	}
	cout << ans << '\n';
	return 0;
}



Measured memory_bytes: 5236
/*  
   |\_/|
   |* *|
   \_+_/
          */
#include<bits/stdc++.h>

#define endl '\n'

using namespace std;

int main(){

  cin.tie(0);     // cout と cin の同期を切る
  ios::sync_with_stdio(false);  /* cの stdioストリーム (printfとか)と*/

  int x;

  cin>>x;

  cout<<x*x*x<<endl;
  
  return 0;

}



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

int main(){
	int n;
	cin>>n;
	ll a[n];
	for(ll i=0;i<n;i++)cin>>a[i];
	ll res[n],j=0;
	//res[0]=a[0];
	if(n%2==0){
		for(ll i=n-1;i>0;i-=2){
			res[j]=a[i];
			j++;
		}
	}else{
		for(ll i=n-2;i>0;i-=2){
			res[j]=a[i];
			j++;
		}
	}

//	res[j]=a[0];j++;
	/*if(n%2==1){
		for(ll i=1;i<n;i+=2){
			res[j]=a[i];
			j++;
		}
	}else{
		for(ll i=0;i<n;i+=2){
			res[j]=a[i];
			j++;
		}
	}*/
	for(ll i=0;i<n;i+=2){
		res[j]=a[i];
		j++;
	}

	if((n-1)%2==0){j=0;ll tmp[n];
		for(ll i=n-1;i>=0;i--){
			//tmp[j]=res[i];
			cout<<res[i]<<" ";
			//j++;
		}
		cout<<endl;
		return 0;
	}

	for(ll i=0;i<n;i++){
		if(i==n-1){
			cout<<res[i]<<endl;
			return 0;
		}
		cout<<res[i]<<" ";
	}
}


Measured memory_bytes: 5248
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
const ll MOD=1000000007;
//const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(15);

  ll N,K; cin>>N>>K;
  string S; cin>>S;
  int flag=1,count=0;
  vector<ll> vec(0);
  
  //ランレングス圧縮
  for(auto cha:S){
    if(cha-'0'!=flag){
      vec.push_back(count);
      count=1;
      flag=1-flag;
    }
    else count++;
  }
  vec.push_back(count);
  if(count!=0) vec.push_back(0);
  //for(auto v:vec) cout<<v<<endl;
  
  if(vec.size()<=2*K+1) {cout<<S.size()<<endl; return 0;}
  
  //累積和
  ll sz=vec.size();
  vector<ll> cum(sz+1,0);
  rep(i,sz) cum[i+1]=cum[i]+vec[i];
  
  ll ans=0;
  for(int i=0;i+2*K+1<=sz;i+=2) chmax(ans,cum[2*K+1+i]-cum[i]);
  
  cout<<ans<<endl;
  
  return 0;
}

Measured memory_bytes: 5216
a[6]={2,3,5,10,12,15},b[6]={380,550,850,1520,1870,2244},m[51],T;
int s(p,n){
	int *m=p,d=5;
	if(!n)return 0;
	if(m[n]<99999)return m[n];
	for(;~d;d--){
		if(a[d]>n)continue;
		T=s(p,n-a[d])+b[d];
		if(m[n]>T)m[n]=T;
	}
	return m[n];
}

main(n){
	memset(m,99,sizeof(m));
	for(;scanf("%d",&n),n;)printf("%d\n",s(m,n/100));
	exit(0);
}

Measured memory_bytes: 600
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <queue>

static const int INF = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

int main() {
    int a, b;
    string s;
    cin >> a >> b >> s;
    for (int i = 0; i < a; ++i) {
        if('0' > s[i] || s[i] > '9') {
            cout << "No" << "\n";
            return 0;
        }
    }
    if(s[a] != '-'){
        cout << "No" << "\n";
        return 0;
    }
    for (int j = a+1; j < a+b+1; ++j) {
        if('0' > s[j] || s[j] > '9') {
            cout << "No" << "\n";
            return 0;
        }
    }
    cout << "Yes" << "\n";
    return 0;
}


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

//AtCoder Beginner Contest 101


int sumDigit(int x){
	int ret = 0;
	while(true){
		if (x == 0) return ret;
		ret = ret + (x % 10);
		x = x / 10;
	}
}

int main(){
	int N;SCD(N);
	int sn = sumDigit(N);
	
	if (N % sn == 0){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
	
	
	return 0;
}

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

#define f first
#define s second
//#define p pair<int,int>
#define P pair<ll,ll>
#define pb push_back
#define ll long long
#define db double
#define zero(a) memset(a,0,sizeof(a))
#define inff(a,n) fill(a,a+n+1,inf)
#define all(a) a.begin(),a.end()
#define sz(a) int((a).size())
#define read(x) scanf("%lld",&x)
#define read2(x,y) scanf("%lld%lld",&x,&y)
#define write(x) printf("%lld\n",x)
#define lson p*2,l,mid
#define rson p*2+1,mid+1,r

const int maxn=2e5+10;
const ll inf=1e9;
const ll mod=998244353;

ll n,m,k,T,x,a[400][400],f[400][400],ans,sum,flag;

int main()
{
    read(n);
    //for(int i=0;i<=301;i++)for(int j=0;j<=301;j++)dp[i][j]=inf;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            read(a[i][j]);
        }
    }
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[i][j]>a[i][k]+a[k][j]){cout<<-1;return 0;}
            }
        }
    }
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i!=j&&j!=k&&i!=k&&a[i][j]==a[i][k]+a[k][j])f[i][j]=1;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(!f[i][j])ans+=a[i][j];
        }
    }
    printf("%lld",ans/2);
    return 0;
}

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

#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, b, a) for(int i = (b); i >= (a); --i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define boost ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define M (ll)(1e9+7)
#define inf (ll)(2e9+9)
#define infl (ll)(2e18+9)
#define debug1(a) cout<<#a<<" = " <<a<<endl; 
#define debug2(a,b) cout<<#a<<" = " <<a<<" , "<<#b<<" = "<<b<<endl; 
#define debug3(a,b,c) cout<<#a<<" = " <<a<<" , "<<#b<<" = "<<b<<" , "<<#c<<" = "<<c<<endl; 
#define debug4(a,b,c,d) cout<<#a<<" = " <<a<<" , "<<#b<<" = "<<b<<" , "<<#c<<" = "<<c<<" , "<<#d<<" = " <<d<<endl; 
  
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<char> vc; 
typedef vector<ll> vll; 
typedef vector< pair<int,int> > vpii;

/* -------------------------------------------------------------------------*/
/* -------------------------------------------------------------------------*/



void solve(){
  int n,w; cin>>n>>w; 
  vector<pair<int,int>> item(n); 
  rep(i,0,n-1){
    int wi,vi; cin>>wi>>vi; 
    item[i]={wi,vi}; 
  }
  vector<vector<ll>> dp(n,vector<ll>(100000+1)); 
  rep(i,0,n-1){
    dp[i][0]=0; 
    rep(j,1,100000){
      if(i==0){
        if(j==item[i].second) dp[i][j]=item[i].first;  
        else dp[i][j]=inf; 
      }   
      else{
        if(j<item[i].second){
          dp[i][j]=dp[i-1][j];  
        } 
        else{
          dp[i][j]=min(dp[i-1][j],dp[i-1][j-item[i].second]+item[i].first); 
        }
      }
    }
  }
  // rep(i,0,30) cout<<dp[n-1][i]<<" "; cout<<endl; 
  int ans=-inf; 
  rep(i,0,100000){
    if(dp[n-1][i]<=w){
      ans=i; 
    } 
  }
  cout<<ans; 
}

int main() {
  clock_t beg=clock(); 
  boost;
  int t=1; 
  // cin>>t; 
  while(t--){
    solve(); 
  }
  clock_t end=clock(); 
  fprintf(stderr, "%lf\n", (double)(end-beg)/CLOCKS_PER_SEC);
}


Measured memory_bytes: 80752
/*
 * $File: p1001.cpp
 * $Author: Wayne <tletletle@163.com>
 *
 * Task:
 *   Given two binary trees, we need to
 *   calculate the intersection or the
 *   union of them.
 *
 * Solution:
 *   Parsing. Graph theory.
 *   Just Build the trees and implement.
*/

#include <cstdio>
#include <cstring>

const int kMaxVtx = 100;
const int kMaxBuf = 512;
char buf[kMaxBuf];

struct TreeNode {
	TreeNode *son[2];
	TreeNode() {
		son[0] = son[1] = NULL;
	}
};

struct Tree {
	TreeNode *root;
	Tree() {
		root = NULL;
	}
} a, b, c;

TreeNode *BuildTree(int l, int r) {
	if (l > r) return NULL;
	TreeNode *res = new TreeNode;
	if (l + 1 == r - 1) return res;
	int mid, sum = 0;
	for (int i = l + 1; i <= r - 1; ++ i)
		if (buf[i] == '(') ++ sum;
		else if (buf[i] == ')') -- sum;
		else if (buf[i] == ',' && sum == 0) mid = i;
	res->son[0] = BuildTree(l + 1, mid - 1);
	res->son[1] = BuildTree(mid + 1, r - 1);
	return res;
}

void ReadTree(Tree &des) {
	scanf("%s", buf);
	des.root = BuildTree(0, strlen(buf) - 1);
}

TreeNode *DfsIntersection(TreeNode *u, TreeNode *v) {
	if (!u || !v) return NULL;
	TreeNode *res = new TreeNode;
	res->son[0] = DfsIntersection(u->son[0], v->son[0]);
	res->son[1] = DfsIntersection(u->son[1], v->son[1]);
	return res;
}

TreeNode *DfsUnion(TreeNode *u, TreeNode *v) {
	if (!u && !v) return NULL;
	TreeNode *res = new TreeNode;
	res->son[0] = DfsUnion(u ? u->son[0] : NULL, v ? v->son[0] : NULL);
	res->son[1] = DfsUnion(u ? u->son[1] : NULL, v ? v->son[1] : NULL);
	return res;
}

Tree GetIntersection(const Tree &a, const Tree &b) {
	Tree res;
	res.root = DfsIntersection(a.root, b.root);
	return res;
}

Tree GetUnion(const Tree &a, const Tree &b) {
	Tree res;
	res.root = DfsUnion(a.root, b.root);
	return res;
}

void OutputTree(TreeNode *cur) {
	if (!cur) return;
	putchar('(');
	OutputTree(cur->son[0]);
	putchar(',');
	OutputTree(cur->son[1]);
	putchar(')');
}

int main() {
	static char cmd[2];
	while (scanf("%s", cmd) != EOF) {
		ReadTree(a);
		ReadTree(b);
		if (cmd[0] == 'i') c = GetIntersection(a, b);
		else c = GetUnion(a, b);
		OutputTree(c.root);
		puts("");
	}
	return 0;
}

Measured memory_bytes: 992
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;

int main(){
  string s,t,u;
  int a,b;
  cin>>s>>t;
  cin>>a>>b;
  cin>>u;
  
  if(u==s) cout<<a-1<<" "<<b;
  else if(u==t) cout<<a<<" "<<b-1;
  
  return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n)	 for(int i = 0; i < (n); i++)
#define all(a)		 begin(a), end(a)

typedef long long int		   ll;
typedef long long unsigned int llu;

void solve(ll N, ll M, vector <ll> a, vector <ll> b) {
	int sum = 0;
	rep(i, N) {
		sum = 0;
		rep(j, M) {
			if(a[j] == i + 1 or b[j] == i + 1) sum++;
		}
		cout << sum << endl;
	}
}

int main() {
	ll N;
	ll M;
	scanf("%lld", &N);
	scanf("%lld", &M);
	vector <ll> b(M - 1 + 1);
	vector <ll> a(M - 1 + 1);
	for(int i = 0; i <= M - 1; i++) {
		scanf("%lld", &a[i]);
		scanf("%lld", &b[i]);
	}
	solve(N, M, a, b);
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
int main(){
    ll a,b,q;cin >> a >> b >> q;
    vector<ll> s(a),t(b),x(q);
    for(int i=0;i<a;i++) cin >> s[i];
    for(int i=0;i<b;i++) cin >> t[i];
    for(int i=0;i<q;i++) cin >> x[i];
    sort(s.begin(),s.end());
    sort(t.begin(),t.end());
    int n=s.size(),m=t.size();
    for(int i=0;i<q;i++){
        ll p1=lower_bound(s.begin(),s.end(),x[i])-s.begin();
        ll SposR=(p1!=n)?s[p1]-x[i]:LINF;
        ll SposL=(p1!=0)?x[i]-s[p1-1]:LINF;
        ll p2=lower_bound(t.begin(),t.end(),x[i])-t.begin();
        ll TposR=(p2!=m)?t[p2]-x[i]:LINF;
        ll TposL=(p2!=0)?x[i]-t[p2-1]:LINF;
        //cout << SposL << " " << SposR << " " << TposL << " " << TposR << endl;
        ll candi1=max(SposL,TposL);
        ll candi2=max(SposR,TposR);
        ll candi3=min(SposL,TposR)*2+max(SposL,TposR);
        ll candi4=min(TposL,SposR)*2+max(TposL,SposR);
        cout << min({candi1,candi2,candi3,candi4}) << endl;
    }
    return 0;
}

Measured memory_bytes: 3712
#include <stdio.h> 

int main(){ 
  int num; 
  int a[100]; 
  int i, j, swap, read; 
  int count = 0; 
  int min_i, min; 

  scanf("%d", &num); 
  for(i = 0; i < num; i++) { 
    scanf("%d", &read); 
    a[i] = read; 
  } 
  if(num == 1) printf("%d", a[0]); 
  else{ 
  for(i = 0; i < num-1; i++){ 
    min_i = i; min = a[i]; 
    for(j = i+1; j < num; j++) if(min > a[j]){min_i = j; min = a[j];} 
    if(i != min_i){ 
      swap = a[i]; 
      a[i] = a[min_i]; 
      a[min_i] = swap; 
      count++; 
    } 
    printf("%d ", a[i]); 
    if(i == num-2) printf("%d", a[num-1]); 
  } 
  } 
  printf("\n%d\n", count); 

  return 0; 
}

Measured memory_bytes: 1760
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <numeric>
#include <vector>
#include <math.h>
#include <string>
#include <queue>

using namespace std;

int main()
{
	long long X, Y;
	cin >> X >> Y;

	int n = 1;

	while (true)
	{
		X *= 2;

		if (X > Y)
		{
			break;
		}
		n++;
	}

	cout << n << endl;	

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;

constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename T> constexpr bool chmax(T &a, const T &b) {
    if(a >= b) return false;
    a = b;
    return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
    if(a <= b) return false;
    a = b;
    return true;
}

/*-------------------------------------------*/

int n;
V<ll> a;
V<V<int>> G;

void dfs(int v, int par) {
    if(G[v].size() == 1) return;
    ll sum = 0, mx = 0;
    for(int u : G[v]) {
        if(u == par) continue;
        dfs(u, v);
        sum += a[u];
        chmax(mx, a[u]);
    }
    if(a[v] > sum || sum > 2 * a[v]) {
        cout << "NO" << endl;
        exit(0);
    }
    ll k = sum - a[v];
    if(k > sum - mx) {
        cout << "NO" << endl;
        exit(0);
    }
    a[v] -= k;
    return;
}

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

    cin >> n;
    a.resize(n);
    G.resize(n);
    rep(i, n) cin >> a[i];
    rep(i, n - 1) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    if(n == 2) {
        cout << (a[0] == a[1] ? "YES" : "NO") << endl;
        return 0;
    }
    int v = 0;
    while(G[v].size() == 1)
        v++;
    dfs(v, -1);
    cout << (a[v] == 0 ? "YES" : "NO") << endl;

    return 0;
}

Measured memory_bytes: 13184
#include <algorithm>
#include <iostream>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const ll INF = LONG_LONG_MAX / 2;

vector<vector<ll>> dp;
vector<ll> A;
vector<ll> B;

void solve(const int H, const int N) {
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= H; j++) {
            dp[i][j] = dp[i - 1][j];
            chmin(dp[i][j], dp[i][max(0LL, j - A[i])] + B[i]);
        }
    }
}

int main(void) {
    int H, N;
    cin >> H >> N;

    A.resize(N + 1);
    B.resize(N + 1);
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        A[i + 1] = a;
        B[i + 1] = b;
    }

    dp.resize(N + 1, vector<ll>(H + 1, -1));
    for (int i = 0; i <= H; i++) {
        dp[0][i] = INF;
    }
    for (int i = 0; i <= N; i++) {
        dp[i][0] = 0;
    }

    solve(H, N);

    cout << dp[N][H] << endl;
    
    return 0;
}


Measured memory_bytes: 78592
import sys
INF = 10 ** 11
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
from heapq import heapify,heappush,heappop   

def main():
    n = int(input())
    a = list(map(int,input().split()))
    m = []
    p = []
    z = 0
    for num in a:
        if num < 0:
            m.append(num)
        elif num > 0:
            p.append(num)
        else:
            z += 1
    
    ans = []
    
    if z == n:
        print(0)
        for i in range(n - 1):
            print(0,0)
        return

    if len(p) == 0:
        if z:
            num1 = 0
            z -= 1
        else:
            m.sort()
            num1 = m[-1]
            m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
    
    elif len(m) == 0:
        if z:
            num1 = 0
            z -= 1
            idx = 0
        else:
            p.sort()
            num1 = p[0]
            idx = 1
        for i in range(idx,len(p) - 1):
            ans.append((num1,p[i]))
            num1 -= p[i]
        ans.append((p[-1],num1))
        num1 = p[-1] - num1
    
    elif len(p) * len(m) > 0:
        num1 = p[-1]
        p.pop()
        num2 = m[-1]
        m.pop()
        for i in range(len(m)):
            ans.append((num1,m[i]))
            num1 -= m[i]
        for i in range(len(p)):
            ans.append((num2,p[i]))
            num2 -= p[i]
        ans.append((num1,num2))
        num1 -= num2
    
    for i in range(z):
        ans.append((num1,0))
    print(num1)
    for i in range(n - 1):
        print(*ans[i])

if __name__=='__main__':
    main()

Measured memory_bytes: 21048
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,k;
int p[200005];
int sum[200005];

int main(void){
	scanf("%d%d",&n,&k);
	for(int i=0;i<n;i++){
		scanf("%d",&p[i]);
		p[i]=p[i]+1;
		sum[i+1]+=p[i];
		sum[i+k+1]-=p[i];
	}
	for(int i=0;i<n;i++){
		sum[i+1]+=sum[i];
	}
	int mx=0;
	for(int i=0;i<=n;i++){
		mx=max(mx,sum[i]);
	}
	if(mx%2==0){
		printf("%d.0000\n",mx/2);
	}else{
		printf("%d.50000\n",mx/2);
	}
	return 0;
}


Measured memory_bytes: 1792
import sys, math
from functools import lru_cache
from itertools import accumulate
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

N = ii()
a = list(mi())

l = [0]*(max(a)+5)

for i in range(N):
    l[a[i]] += 1
    l[a[i]+3] -= 1

acc = list(accumulate(l))

print(max(acc))




Measured memory_bytes: 20812
import java.util.*;

class Main{
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        int sum = 0;
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
            sum += a[i];
        }

        float thre = (float)sum /(4*m);
        int count = 0;
        for(int i=0;i<n;i++){
            if(thre <= a[i])count++;
        }

        if(count>=m){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
        
    }
}


Measured memory_bytes: 21972
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

def main():
    N, X, T = map(int, input().split())
    c = 0
    t = 0
    while c < N:
        c += X
        t += T

    print(t)

if __name__ == '__main__':
    main()

Measured memory_bytes: 9176
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e6;
typedef pair<ll,ll> pairs;
vector<pairs> p;


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

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

    vector<ll> s(n+1);
    s[0] = 0;
    for(ll i=0;i<n;++i){
        s[i+1] = s[i] + a[i];
    }
    map<ll, ll> num;
    for(ll i=0;i<n+1;++i){
        num[s[i]]++;
    }
    ll ans=0;
    for(auto val : num){
        ll flag = val.second;
        ans += flag*(flag-1)/2;
    }
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 15872
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

class RollingHash {
public:
  const ll B = 1009, MOD1 = 1000000007, MOD2 = 1000000009;

private:
  string s;
  ll n;
  vector<ll> p1, p2, h1, h2;

public:
  RollingHash(string s): s(s), n(s.length()), p1(n + 1), p2(n + 1), h1(n + 1), h2(n + 1) {
    p1[0] = p2[0] = 1;
    REP(i, 0, n) p1[i + 1] = (p1[i] * B) % MOD1;
    REP(i, 0, n) p2[i + 1] = (p2[i] * B) % MOD2;
    h1[0] = h2[0] = 0;
    REP(i, 0, n) h1[i + 1] = (h1[i] * B + s[i]) % MOD1;
    REP(i, 0, n) h2[i + 1] = (h2[i] * B + s[i]) % MOD2;
  }

  ll hash(ll l, ll r) {
    ll x1 = (h1[r] - (h1[l] * p1[r - l] % MOD1) + MOD1) % MOD1;
    ll x2 = (h2[r] - (h2[l] * p2[r - l] % MOD2) + MOD2) % MOD2;
    return x1 * MOD2 + x2;
  }
};

int main(void) {
  ll N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  vector<string> Q(M);
  REP(i, 0, M) cin >> Q[i];

  RollingHash rh(S);

  ll l = 0, r = 1;
  set<ll> st;
  REP(i, 0, M) {
    if(Q[i] == "L++") l++;
    if(Q[i] == "L--") l--;
    if(Q[i] == "R++") r++;
    if(Q[i] == "R--") r--;
    st.insert(rh.hash(l, r));
  }

  cout << st.size() << endl;
}



Measured memory_bytes: 43084
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println(calc(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt(), sc.nextInt()));
        }
    }
    
    static String calc(int fromH, int fromM, int fromS, int toH, int toM, int toS) {
        int fromSecond = fromH * 3600 + fromM * 60 + fromS;
        int toSecond = toH * 3600 + toM * 60 + toS;
        int totalSecond = toSecond - fromSecond;
        int hour = totalSecond / 3600;
        totalSecond %= 3600;
        int minute = totalSecond / 60;
        totalSecond %= 60;
        return hour + " " + minute + " " + totalSecond;
    }
}



Measured memory_bytes: 26424
I=lambda:list(map(int,input().split()))
n,k=I()
z=[I()for _ in[0]*n]
z.sort()
a=10**20
for i in range(n):
 for j in range(i+k-1,n):l=sorted(t[1]for t in z[i:j+1]);a=min(a,*[(z[j][0]-z[i][0])*(v-u)for u,v in zip(l,l[k-1:])])
print(a)

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

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

    int n,q; cin>>n;
    long long sum=0;
    map<int,long long> mp;
    for(int i=0; i<n; ++i)
    {
        int a; cin>>a; sum+=a;
        mp[a]++;
    }
    cin>>q;
    while(q--)
    {
        int b,c; cin>>b>>c;
        sum+=(c-b)*mp[b];
        mp[c]+=mp[b];
        mp[b]=0;
        cout << sum << "\n";
    }



}

Measured memory_bytes: 9500
import java.util.*;

class Main{       
   public static void main(String[] args){
     
       Scanner sc = new Scanner(System.in);                                            
              
       HashSet<Integer> first = new HashSet<Integer>();
       HashSet<Integer> second = new HashSet<Integer>();
       
       int N = sc.nextInt();       
       int M = sc.nextInt();
       
       for(int i = 0;i < M;i++){
         int a = sc.nextInt();
         int b = sc.nextInt();
         if(a == 1){
             first.add(b);
         }
         if(b == N){
             second.add(a);
         }         
       }
       
       for(Integer k:first){
           if(second.contains(k)){
               System.out.println("POSSIBLE");
               return;
           }
       }
             
       System.out.println("IMPOSSIBLE");
       
   }
}
        
        


Measured memory_bytes: 109952