akhauriyash/Code-Regression huggingface dataset:
#include <chrono>

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <time.h>
#include <random>
#include <string>
#include <cassert>
#include <vector>
#include <ostream>
#include <istream>
#include <stack>
#include <deque>
#include <queue>
#include <functional>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream& operator << (ostream &a, const vector<int> &b) {
    for (auto k : b) cout << k << " ";
    return a;
}

#ifdef LOCAL
    #define dbg(x) cout << #x << " : " << (x) << "\n";
    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 10;
//    const ld PI = 3.1415926535;
#else
    #define dbg(x)
    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 179;
//    const ld PI = 3.1415926535;
#endif

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("fast-math")
//#pragma GCC target("avx2")
//#pragma GCC optimize("section-anchors")
//#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
//#pragma GCC optimize("vpt")
//#pragma GCC optimize("rename-registers")
//#pragma GCC optimize("move-loop-invariants")
//#pragma GCC optimize("unswitch-loops")
//#pragma GCC optimize("function-sections")
//#pragma GCC optimize("data-sections")

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifdef LOCAL
    #else
//        freopen("capitals.in", "r", stdin);
//        freopen("capitals.out", "w", stdout);
    #endif
    int n, k;
    cin >> n >> k;
    set<int> cur;
    cur.insert(n);
    n %= k;
    for (int i = 0; i < 100; i++) {
        cur.insert(abs(n - k));
        n = abs(n - k);
    }
    cout << *cur.begin();
}
/*
*/


Measured memory_bytes: 256
//Double Factorial
#include <stdio.h>
#include <limits.h>

long long factorial(long long num) {
	if(num % 2 != 0) return 0;

	long long NUM = 0;
	num /= 2;
	while (num) {
		NUM += num / 5;
		num /= 5;
	}
	return NUM;
}

int main(){
	long long num;
	scanf("%llu", &num);
	printf("%llu\n", factorial(num));
}


Measured memory_bytes: 1728
#include<iostream>
#include<cstring>
using namespace std;
int main(){
	string s;
	cin>>s;
	int T=0,S=0;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='S') S++;
		else  {
			if(S){
				S--;
			}
		}		
	 } 
	 cout<<2*S<<endl;
	return 0;
}

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
//#pragma GCC optimize ("-O3") 
#ifdef YANG33
#include "mydebug.hpp"
#else
#define DD(x) 
#endif
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2019/03/21  Problem: ABC 061 C / Link: http://abc061.contest.atcoder.jp/tasks/abc061_c  ----- */


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

	LL N, K; cin >> N >> K;
	map<LL, LL>M;
	FOR(i, 0, N) {
		LL a, c; cin >> a >> c;
		M[a] += c;
	}
	for (auto it : M) {
		K -= it.second;
		if (K <=0) {
			cout << it.first << endl;
			return 0;
		}
	}
	

	return 0;
}


Measured memory_bytes: 4224
#include<iostream>
#include<string>
using namespace std;
int main() {
	int k, a, b; cin >> k >> a >> b;
	string ans = "NG";
	for (int i = a; i <= b; i++) {
		if (i % k == 0) {
			ans = "OK";
			break;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3628
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int[] A = new int[9];
		for (int i=0; i<9; i++) {
			A[i] = in.nextInt();
		}
		int N = in.nextInt();
		int[] b = new int[N];
		for (int i=0; i<N; i++) {
			b[i] = in.nextInt();
		}
		
		boolean flag = false;
		
		ArrayList<Integer> hit = new ArrayList<Integer>();
		
		for (int i=0; i<9; i++) {
			for (int j =0; j<N; j++) {
				
				if (A[i] == b[j]) {
					hit.add((i+1));
				}
				
			}
		}
		
		if((hit.contains(1) && hit.contains(2) && hit.contains(3)) || (hit.contains(4) && hit.contains(5) && hit.contains(6)) || (hit.contains(7) && hit.contains(8) && hit.contains(9))) {
			flag = true;
		}
		
		if((hit.contains(1) && hit.contains(4) && hit.contains(7)) || (hit.contains(2) && hit.contains(5) && hit.contains(8)) || (hit.contains(3) && hit.contains(6) && hit.contains(9))) {
			flag = true;
		}
		
		if((hit.contains(1) && hit.contains(5) && hit.contains(9)) || (hit.contains(3) && hit.contains(5) && hit.contains(7))) {
			flag = true;
		}
		
		if (flag) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
		
	}
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
//#pragma GCC optimize(2)

using namespace std;

#define LL long long
#define Rint register int
#define U unsigned
#define forn(i,a,b) for(int i = a;i <= b;++i)
#define nfor(i,a,b) for(int i = a;i >= b;--i)
#define pii pair<int ,int>
#define MS0(X) memset(X,0,sizeof(X))
#define MS1(X) memset(X,-1,sizeof(X))
#define BR printf("--------------------\n")
#define pb push_back
#define rep(i,a,b) for(Rint i=a;i<=b;++i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const double PI=atan(1.)*4.;
const int maxn=5005;
const LL inf = 1e18;
const int mod=1e9+7;
int dirx[8]={1,0,-1,0,1,-1,-1,1};
int diry[8]={0,1,0,-1,1,1,-1,-1};

int n,m;
LL p;

struct node
{
    int u,v;
    LL w;
}e[maxn];

LL dis[maxn];

int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
   	cout.tie(0);
    cin>>n>>m>>p;
    rep(i,0,m-1)
    {
        int u,v,w;
        cin>>u>>v>>w;
        e[i].u=u,e[i].v=v,e[i].w=w-p;
    }
    rep(i,1,n)dis[i]=-inf;
    dis[1]=0;
    rep(i,1,2*n)
    {
        rep(j,0,m-1)
        {
            int u=e[j].u,v=e[j].v,w=e[j].w;
            if(dis[u]==-inf)
                continue;
            else
            {
                if(dis[v]<dis[u]+w)
                {
                    dis[v]=i<=n?dis[u]+w:inf;
                }
            }
        }
    }
    if(dis[n]==inf)
        cout<<-1;
    else if(dis[n]<0)
        cout<<0;
    else
        cout<<dis[n];
	return 0;
}


Measured memory_bytes: 512
#include<stdio.h>

int dp[50];

int fib(int n) {
  if ( n == 0 || n == 1 ) return dp[n] = 1;
  if ( dp[n] != -1 ) return dp[n];
  return dp[n] = fib(n - 1) + fib(n - 2);
}

int main() {
  int n, i;
  for ( i = 0; i < 50; i++ ) dp[i] = -1;

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

  return 0;
}


Measured memory_bytes: 2744
# AOJ 0144 Packet Transportation
# Python3 2018.6.23 bal4u

INF = 0x7fffffff
n = int(input())
arr = [[INF for j in range(n)] for i in range(n)]
for i in range(n):
	a = list(map(int, input().split()))
	x, k = a[0]-1, a[1]
	for j in range(k):
		y = a[j+2]-1
		arr[x][y] = 1
for k in range(n):
	arr[k][k] = 0
	for i in range(n):
		if arr[i][k] >= INF: continue
		for j in range(n):
			if arr[k][j] >= INF: continue
			arr[i][j] = min(arr[i][j], arr[i][k] + arr[k][j])
p = int(input())
for i in range(p):
	s, d, v = map(int, input().split())
	s,d = s-1, d-1
	print(arr[s][d]+1 if arr[s][d] < v else "NA")


Measured memory_bytes: 5700
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define F(i,a,n) for(int i=a;i<n;i++)
#define INF 1000000007
using namespace std;

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;
}


int n,m;
vector<vector<int>> G;
vector<int> deg;

int dp[100100];
int main(){
  cin >> n >> m;
  G.assign(n,vector<int>());
  deg.assign(n,0);
  rep(i,m){
    int x,y;
    cin >> x >> y;
    x--;y--;
    G[x].push_back(y);
    deg[y]++;
  }

  queue<int> que;
  rep(i,n) if(deg[i]==0)que.push(i);

  while(!que.empty()){
    int v = que.front();
    que.pop();
    for(auto nv : G[v]){
      deg[nv]--;
      if(deg[nv]==0){
        que.push(nv);
        chmax(dp[nv],dp[v]+1);
      }
    }
  }

  int res = 0;
  rep(i,n)chmax(res, dp[i]);
  cout << res << endl;
  return 0;
}

Measured memory_bytes: 6528
#include<cstdio>
#include<cstdlib>

int main()
{
	int a[200005],b[200005], i, n;
	scanf("%d", &n);
	for (i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}

	int left = 100000, right = left + 1;

	for (i = 0; i < n; i++)
	{
		if ((n + i) % 2 != 0)
		{
			b[left] = a[i];
			left--;
		}
		else
		{
			b[right] = a[i];
			right++;
		}

	}
	for (i = left + 1; i < right; i++)
	{
		printf("%d ", b[i]);
	}
	return 0;
}

Measured memory_bytes: 3712
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e9;
bool graph[nmax][nmax];
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n)
{
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < n; j++)
        {
            for (size_t k = 0; k < n; k++)
            {
                dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
            }
        }
    }
}

class UnionFind
{
  public:
    vector<ll> Parent;

    UnionFind(ll N)
    {
        Parent = vector<ll>(N, -1);
    }
    ll find(ll A)
    {
        if (Parent[A] < 0)
            return A;
        return Parent[A] = find(Parent[A]);
    }

    ll size(ll A)
    {
        return -Parent[find(A)];
    }

    bool Union(ll A, ll B)
    {
        A = find(A);
        B = find(B);
        if (A == B)
        {
            return false;
        }
        if (size(A) < size(B))
            swap(A, B);

        Parent[A] += Parent[B];
        Parent[B] = A;

        return true;
    }
};

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

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    return a / g * b;
}

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

ll powMod(ll a, ll p)
{
    if (p == 0)
    {
        return 1;
    }
    else if (p % 2 == 0)
    {
        ll half = powMod(a, p / 2);
        return mulMod(half, half);
    }
    else
    {
        return mulMod(powMod(a, p - 1), a);
    }
}

ll ceil(ll a, ll b)
{
    return (a + b - 1) / b;
}


void solve(long long N, long long M, std::vector<long long> A){
    priority_queue<ll> q;
    for (int i = 0; i < N; i++)
    {
        q.push(A[i]);
    }
    
    for (int i = 0; i < M; i++)
    {
        ll tmp = q.top();
        q.pop();

        tmp /= 2;
        q.push(tmp);
    }
    ll ans = 0;
    for (int i = 0; i < N; i++)
    {
        ans += q.top();
        q.pop();
    }

    cout << ans << endl;
    
    

}

int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, M, std::move(A));
    return 0;
}


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

int main(){
  long long n;
  cin >> n;
  long long ans=1000000000001;
  for(long long i=1;i<1000001;i++){
  long long a;
    if(n%i == 0){a=n/i;}
    ans =min(ans,a+i-2);
    if(n<i){break;}
  }
  cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> x(n), y(n), h(n);
  rep(i, n) cin >> x[i] >> y[i] >> h[i];

  rep(cx, 101) rep(cy, 101) {
    rep(i, n) {
      if (h[i] <= 0) continue;
      ll H = h[i] + abs(x[i] - cx) + abs(y[i] - cy);

      bool flag = true;
      rep(j, n) flag &= (h[j] == max(H - abs(x[j] - cx) - abs(y[j] - cy), 0LL));
      if (flag) {
        cout << cx << " " << cy << " " << H << endl;
        return 0;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <math.h>
using namespace std;
 
const int MAX_N=10000;
int a,b,list[100]={};
 
int main(){
	for(int i=0;i<2;i++){
		cin >> a >> b;
		list[a]++;
		list[b++]--;
	}
	
	int s=0;
	if(list[0]==2) s++;
	for(int i=1;i<100;i++){
		list[i]+=list[i-1];
		if(list[i]==2) s++;
	}
	
	cout << s << endl;
	return 0;
}


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

typedef long long ll;

int N,P;
ll W[100010],B[100010];
vector<ll> V;
ll sum = 0;

int main()
{
    scanf("%d%d",&N,&P);
    for(int i = 0; i < N; i++)scanf("%d%d",W + i,B + i);

    for(int i = 0; i < N; i++)sum -= P * B[i];
    for(int i = 0; i < N; i++)V.push_back(W[i] * (100 - P) + P * B[i]);
    sort(V.begin(),V.end());
    reverse(V.begin(),V.end());
    for(int i = 0; i <= N; i++)
    {
        if(sum >= 0)
        {
            printf("%d\n",i);
            return 0;
        }
        sum += V[i];
    }
    return 0;
}


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

int main()
{
 string s;
 cin>>s;
 int n=s.length();
 if(s[n-1]=='2' || s[n-1]=='4'|| s[n-1]=='5' || s[n-1]=='7'|| s[n-1]=='9'){
    cout<<"hon";
 }
else if(s[n-1]=='0' || s[n-1]=='1'|| s[n-1]=='6' || s[n-1]=='8'){
    cout<<"pon";
 }
 else if(s[n-1]=='3'){
    cout<<"bon";
 }
}


Measured memory_bytes: 3624
A = int(input())
B = int(input())
C = int(input())
X = int(input())

cnt = 0
for i in range(min(X // 500, A) + 1):
  for j in range(min((X - 500 * i) // 100, B) + 1):
    if (X - 500 * i - 100 * j ) // 50 <= C:
      cnt += 1
print(cnt)      

Measured memory_bytes: 3060
N, M = map(int, input().split())
X = list(map(int, input().split()))

X.sort()
array = [0] * (M-1)
for i in range(M-1):
    array[i] = X[i+1] - X[i]
array.sort()
counter = 0
for i in range(M-N):
    counter += array[i]
print(counter)


Measured memory_bytes: 13968
while true
    h, w = gets.split
    h = h.to_i;w = w.to_i;
    if h == 0 && w == 0
        break
    end
    for i in 1..h
        if i == 1 || i == h
            for j in 1..w
            print "#"
            end
        else
            print "#"
            for k in 1..w-2
                print "."
            end
            print "#"
        end
        print "\n"
    end
    print "\n"
end



Measured memory_bytes: 8880
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

int main(){
	for(int i=0;i<9;i++){
		string S;
		int A,B,sum;
		cin>>S>>A>>B; sum = 200*min(A,400)+300*min(B,400);
		cout<<S; printf(" %d %d\n",A+B,sum);
	}
	return 0;
}

Measured memory_bytes: 1200
#include <iostream>
#include <string>
#include <set>
using namespace std;

int main(void){
  int n;
  cin >> n;
  string pre_s;
  set<string> s_set;
  cin >> pre_s;
  s_set.insert(pre_s);
  
  int flag  = 0;
  for(int i = 1; i < n; i++){
    string s;
    cin >> s;
    if(s_set.count(s) != 0 || pre_s[pre_s.length()-1] != s[0]){
      flag = 1;
      break;
    }
    s_set.insert(s);
    pre_s = s;
  }
  if(flag == 0){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
const int MAX_N=2e3;
typedef pair<ll,ll> P;
ll N;
P A[MAX_N];
ll memo[MAX_N][MAX_N];

ll dfs(ll n,ll lft,ll rig){
	if(n==N)return 0;
	if(memo[n][lft]!=-1){
		return memo[n][lft];
	}
	return memo[n][lft]=max(A[n].first*llabs(A[n].second-lft)+dfs(n+1,lft+1,rig),
			A[n].first*llabs(A[n].second-rig)+dfs(n+1,lft,rig-1));
}

int main(){
	cin.tie(0);
	cin.sync_with_stdio(false);
	cin>>N;
	REP(i,N){
		cin>>A[i].first;
		A[i].second=i;
	}
	memset(memo,-1,sizeof(memo));
	sort(A,A+N);
	reverse(A,A+N);
	cout<<dfs(0,0,N-1)<<endl;
}


Measured memory_bytes: 35052
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
using namespace std;
using Graph = vector<vector<int>>;
using P = pair<long, long>;

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

struct compare1 { 
    bool operator()(const pair<long, long>& value,  
                                const long& key) 
    { 
        return (value.first < key); 
    } 
    bool operator()(const long& key,  
                    const pair<long, long>& value) 
    { 
        return (key < value.first); 
    } 
}; 

int main()
{
    int N, S; cin >> N >> S;
    int A[4000];
    REP(i, N) cin >> A[i];
    long mod = 998244353;
    long dp1[4000], dp2[4000];
    long ans = 0;
    REP(i, S+1) dp1[i] = 0;
    REP(i, N){
        dp2[0] = 0;
        for(int j = 1; j <= S; j++){
            if(j < A[i]) dp2[j] = dp1[j];
            else if(j == A[i]){
                dp2[j] = dp1[j] + i + 1;
                dp2[j] %= mod;
            }else{
                dp2[j] = dp1[j] + dp1[j - A[i]];
                dp2[j] %= mod;
            }
        }
        ans += dp2[S] * ((long)N - (long)i);
        ans %= mod;
        REP(i, S) dp1[i] = dp2[i];
        dp1[S] = 0;
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

#define NUM_COUNT 2

int asc(const void* p1, const void* p2){
	return (*(int*)p1) - (*(int*)p2);
}

int main(int argc, char **argv){
	int data[NUM_COUNT];
	int i;
	
	for(;;){
		// Input
		for(i=0;i<NUM_COUNT;i++){
			scanf("%d",data+i);
		}
		
		// Sort
		qsort( data, NUM_COUNT, sizeof(int), asc);

		// Finish Check
		if( data[NUM_COUNT-1] == 0 ){
			break;
		}
		
		// Print Number
		for(i=0;i<NUM_COUNT;i++){
			if( i < NUM_COUNT-1 ){
				printf("%d ",data[i]);
			} else {
				printf("%d\n",data[i]);
			}
		}
	}
	
	return 0;
}

Measured memory_bytes: 604
import java.util.*;
import java.math.*;;
public class Main{
  public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
    	BigInteger num = sc.nextBigInteger();
    	if (num.mod(new BigInteger("9")).compareTo(new BigInteger("0")) == 0)
          System.out.println("Yes");
    	else
          System.out.println("No");
    }
}

Measured memory_bytes: 34528
#include <bits/stdc++.h>
#define rep(i,n)for(long long i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
 
const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
ll gcd(ll x, ll y){
  if(y==0) return x;
  else return gcd(y, x%y);
}
ll lcm(ll x,ll y){
  ll g=gcd(x,y);
  return x/g*y;
}

using Graph=vector<vector<int>>;
Graph G;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a,b;
  cin >> a >> b;
  
  cout<<6-a-b<<endl;
}
    

Measured memory_bytes: 892
#include "bits/stdc++.h"
using namespace std;

int main(){
        int n;
        cin >> n;
        vector<int>a(n);
        for(int i=0;i<n;i++){
            cin >> a.at(i);
        }
        int ans = 10e8;
      
        for(int i=1;i<n;i++){
            int sa =0,sb =0;
            for(int j=0;j<n;j++){
                if(j<=i) sa+=a.at(j);
                else sb+=a.at(j);
            }
            ans = min(abs(sa-sb),ans);
        }
        cout << ans << endl;
}

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
    string S;
    cin >> S;
    for (auto& c : S) {
        if (c == ',') c = ' ';
    }
    cout << S << endl;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    solve();
    cout << flush;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long int int64;




int main(int,  char **)
{
#ifdef LOCAL
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif


	long long ai, ao, aj, al;
	scanf("%lld%lld%*lld%lld%lld%*lld%*lld", &ai, &ao, &aj, &al);
	long long ans1 = ai / 2 * 2 + ao + aj / 2 * 2 + al / 2 * 2;
	long long ans2 = (ai - 1) / 2 * 2 + ao + (aj - 1) / 2 * 2 + (al - 1) / 2 * 2 + 3;
	if (ai == 0 || aj == 0 || al == 0)
		ans2 = 0;
	printf("%lld\n", max(ans1, ans2) );


	return 0;
}



Measured memory_bytes: 256
#include <stdio.h>
int main() {
	char s[4];
	scanf("%s", &s);
	for (int i = 0; i < 3; i++) {
		if (s[i] == '1')
			s[i] = '9';
		else if (s[i] == '9')
			s[i] = '1';
	}
	printf("%s", s);
	return 0;
}

Measured memory_bytes: 128
//
// Created by yamunaku on 2019/11/23.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main(){
    // CFS;
    int x,y;
    cin >> x >> y;
    ll ans=0;
    if(x==1) ans+=300000;
    else if(x==2) ans+=200000;
    else if(x==3) ans+=100000;
    if(y==1) ans+=300000;
    else if(y==2) ans+=200000;
    else if(y==3) ans+=100000;
    if(x*y==1) ans+=400000;
    cout << ans << endl;
    return 0;
}

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

typedef pair<double,double>vec;

double norm(vec v1,vec v2){
    return (v1.first-v2.first)*(v1.first-v2.first)+(v1.second-v2.second)*(v1.second-v2.second);
}
int main()
{
    int n;
    cin>>n;
    while(n--){
        double x1,y1,r1,x2,y2,r2;
        cin>>x1>>y1>>r1>>x2>>y2>>r2;
        vec v1=make_pair(x1,y1);
        vec v2=make_pair(x2,y2);
        double dis=norm(v1,v2);
        if(dis>(r1+r2)*(r1+r2))
            cout<<0<<endl;
        else if(dis>(r1-r2)*(r1-r2)&&dis<(r1+r2)*(r1+r2)){
            cout<<1<<endl;
        }
        else if((r1-r2)*(r1-r2) > dis){
            if((r1-r2)>0){
                cout<<2<<endl;
            }
            else if((r1-r2)<0)cout<<-2<<endl;
        }
        else cout<<1<<endl;
    }
    return 0;
}

Measured memory_bytes: 1232
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
constexpr int mod = 1e9+7;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(m);
  rep(i,n) cin >> x[i];
  rep(j,m) cin >> y[j];

  mint res1 = 0, res2 = 0;

  for (int i = 1; i <= n; i++) {
    res1 += mint(i - 1) * x[i - 1] - mint(n - i) * x[i - 1];
  }

  for (int j = 1; j <= m; j++) {
    res2 += mint(j - 1)* y[j - 1] - mint(m - j) * y[j - 1];
  }
  cout << (res1 * res2).x << endl;
  return 0;
} 

Measured memory_bytes: 4052
n = int(input())
b = list(map(int,input().split()))
b.sort()
     
d = b[0] + 0
 
while len(b) > 1 :
    for i in range(1,len(b)) :
        b[i] = b[i] % b[0]
    if b.count(0) == len(b)-1 :
        d = b[0]
        break
    else :
        b.sort()
        for j in range(b.count(0)) :
            b.remove(0)
print(d)

Measured memory_bytes: 14252
X,Y = map(int,input().split())

j = 0
for a in range(100):
  for b in range(100):
    if((a+b == X)and(a*2+b*4 == Y)):
      j = 1
      break
     
if(j == 0):
  print('No')
else:
  print('Yes')

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


using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0; i<ll(n); i++)
#define FOR(i,m,n) for(ll i=ll(m); i<ll(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VP vector<pair<int,int>>
#define VPP vector<pair<int,pair<int,int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

const int  INF = 1 << 29;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

vector<ll> divisor(ll n) {
	vector<ll> ret;
	for (ll i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			ret.push_back(i);
			if (i * i != n) ret.push_back(n / i);
		}
	}
	sort(begin(ret), end(ret));
	return (ret);
}


int main() {

	ll n;
	cin >> n;
	VLL a = divisor(n);
	VLL b = divisor(n - 1);


	set<ll> s;
	for (ll i : a)s.insert(i);
	for (ll i : b)s.insert(i);

	
	int cnt = 0;


	for (ll i : s) {
		if (i == 1)continue;
		ll j = n;
		while (j > 1) {
			//cout << j << ' ';
			if (j%i == 0)j /= i;
			else if (j < i)break;
			else j %= i;
		}
		if (j == 1) {
			cnt++;
		}
	}
	cout << cnt << endl;


	/*
	FOR(i, n, n + 1) {
		FOR(j, 2, i + 1) {

			int k = i;
			while (k > 1) {
				if (k%j == 0)k /= j;
				else k -= j;
			}
			if (k == 1) {
				cnt--;
			}
		}
	}
	if (cnt != 0)cout << n << endl;
	*/
}



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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int r, g, b, n;
  cin >> r >> g >> b >> n;
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i * r + j * g <= n && (n - i * r - j * g) % b == 0) {
        ans++;
      }
    }
  } 
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    private static final int MOD = (int) Math.pow(10, 9);
    private static final int[][] DIRECTIONS = {{1, 1}, {0, 1}, {-1, 1}, {1, 0}, {-1, 0}, {0, -1}};

    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int N = sc.nextInt();


        for (int i = 0; i <N; i++) {
            int n = sc.nextInt();
            if (n % 2 == 0) {
                if (!(n%3 == 0 || n%5 == 0)) {
                    System.out.println("DENIED");
                    System.exit(0);
                }
            }
        }

        System.out.println("APPROVED");
        System.exit(0);
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

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

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

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

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

        String nextLine() {
            String str = "";
            try{
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


Measured memory_bytes: 23124
#include<stdio.h>

int n,a[20];

int solve (int i,int m)
{
	if (m == 0) {
		return 1;
	}
	if (i >= n) {
		return 0;
	}
	int res = solve (i + 1, m) || solve (i + 1, m - a[i]);
	return res;
}
int main()
{
	int i,q,m;

	scanf("%d",&n);
	for (i = 0; i < n; i++) {
		scanf("%d",&a[i]);
	}
	scanf("%d",&q);
	for (i = 0; i < q; i++){
		scanf("%d",&m);
		if (solve(0,m)) {
			printf("yes\n");
		} else {
			printf("no\n");
               }
	}

	return 0;
	}

Measured memory_bytes: 580
use proconio::input;
use std::collections::binary_heap::BinaryHeap;
use std::cmp::Reverse;

fn main() {
    input! {
        q:usize,
    };

    let mut lef_q = BinaryHeap::new();
    let mut lef_sum = 0;
    let mut rig_q = BinaryHeap::new();
    let mut rig_sum = 0;
    let mut bsum = 0;

    for _ in 0..q {
        input!{
            tp:usize
        };
        if tp==1 {
            input!{
                a:i64,
                b:i64
            };
            bsum+=b;
            
            
            if lef_q.len()==rig_q.len() {
                rig_sum += a;
                rig_q.push(Reverse(a));

                if let Some(Reverse(x)) = rig_q.pop() {
                    rig_sum-=x;
                    lef_q.push(x);
                    lef_sum+=x;
                }
                else {
                    assert!(false);
                }
                
            }
            else{
                lef_sum += a;
                lef_q.push(a);

                let x = lef_q.pop().unwrap();
                lef_sum-=x;
                rig_q.push(Reverse(x));
                rig_sum+=x;
            }
        }
        else {
            let a = lef_q.peek().unwrap();
            let ans_l = -lef_sum+a*(lef_q.len() as i64);
            let ans_r = -a*(rig_q.len() as i64)+rig_sum;
            let ans = bsum+ans_l+ans_r;
            println!("{} {}",a,ans);
        }
    }
    
}




Measured memory_bytes: 8808
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    val n = sc.nextLong()
    println(problem172d(n))
}

fun problem172d(n: Long): Long {
    var ans = 0L

    for (i in 1..n) {
        for (j in i..n step i) {
            ans += j
        }
    }
    return ans
}

Measured memory_bytes: 54988
use std::io;


fn main() {
    let n = read_one::<usize>();
    let mut an = Vec::new();
    let mut bn = Vec::new();

    for _ in 0..n {
        let (a, b) = {
            let i = read::<usize>();
            (i[0], i[1])
        };
        an.push(a);
        bn.push(b);
    }
    an.reverse();
    bn.reverse();

    let mut ans: usize = 0;
    for (a, b) in an.iter().zip(bn.iter()) {
        if *a != 0 && *b != 1 {
            ans += (b - (a + ans) % b) % b;
        }
    }

    println!("{}", ans);
}


#[allow(dead_code)]
fn read<T>() -> Vec<T>
where T:
std::str::FromStr,
T::Err: std::fmt::Debug {

    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.split_whitespace()
        .map(|s| s.trim().parse().unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_one<T>() -> T
where T:
std::str::FromStr,
T::Err: std::fmt::Debug {

    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    buf.trim().parse().unwrap()
}

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

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> sw(n);
    vector<int> k(m), p(m);
    vector<vector<int>> s(m, vector<int>(n));
    int index;

    for(int i = 0; i < m; i++){
        cin >> k.at(i);
        for(int j = 0; j < k.at(i); j++){
            cin >> index;
            //cout << index << endl;
            s.at(i).at(index - 1) = 1;
        }
    }
    /*
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            cout << s.at(i).at(j) << " ";
        }
        cout << endl;
    }
    */
    for(int i = 0; i < m; i++){
        cin >> p.at(i);
    }

    int ans = 0;
    for(int i = 0; i < int(pow(2.0, double(n))); i++){
        int temp = i;
        for(int j = n - 1; j >= 0; j--){
            if(temp / int(pow(2.0, double(j))) == 1){
                temp -= int(pow(2.0, double(j)));
                sw.at(j) = 1;
            }else{
                sw.at(j) = 0;
            }
        }
        /*
        for(int j=0;j<n;j++){
            cout << sw.at(j) << " ";
        }
        cout << endl;
        */
        int count = 0;
        for(int j = 0; j < m; j++){
            int sum = 0;
            for(int l = 0; l < n; l++){
                sum += s.at(j).at(l) *sw.at(l);
            }
            if(sum % 2 == p.at(j)) count++;
        }
        if(count == m) ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

x=int(input())
a=list(map(int, input().split()))
ans=0
if x==1:
	print(1)
else:
	chk=lcm(a[0],a[1])
	for i in range(x):
		chk=lcm(chk,a[i])
	for i in range(x):
		ans=ans+(chk//a[i])
	print(ans%(10**9+7))

Measured memory_bytes: 6112
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz(v) (ll((v).size()))
#define forn(i,n) for(ll i=0;i<(n);i++)
#define forr(i,n) for(ll i=(n)-1;i>=0;i--)
#define forv(i,v) forn(i,sz(v))
#define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i)
#define all(v) (v).begin(),(v).end()
#define clr(x) memset(x,0,sizeof(x))

const ll N =404;
const ll inf = 1e15;
ll vis[N][N],dp[N][N],n,a[N];

void solve(ll l, ll r)
{
	if(vis[l][r])return;
	vis[l][r]=1;
	if(l>=r){dp[l][r]=0;return;}

	ll sum=a[r];if(l!=0)sum-=a[l-1];
	//cout<<l<<" "<<r<<" "<<dp[l][r]<<endl;
	for(int i=l;i<r;i++){solve(l,i);solve(i+1,r);dp[l][r]=min(dp[l][r],dp[l][i]+dp[i+1][r]+sum);}
	//cout<<l<<" "<<r<<" "<<dp[l][r]<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);

    clr(vis);
	cin>>n;forn(i,n)cin>>a[i];forn(i,n-1)a[i+1]+=a[i];forn(i,n)forn(j,n)dp[i][j]=inf;
	solve(0,n-1);
	//forn(i,n)cout<<a[i]<<" ";cout<<endl;
	//forn(i,n){forn(j,n)cout<<dp[i][j]<<" ";cout<<endl;}


	cout<<dp[0][n-1]<<endl;
}


Measured memory_bytes: 2816
#include <bits/stdc++.h>
#include <iostream>
#include <utility> // pair
#include <string>
#include <vector>
#define MAX_N 100001
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define REP(i, n) for(int i=0; i<n; i++)
#define RER(i, n) for(int i=n-1; i>=0; i--)
#define ALL(v) v.begin() v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n, k, r, s, p;
string t;
char choice[3] = {'r', 's', 'p'};

signed main(void){
    cin >> n >> k;
    cin >> r >> s >> p;
    cin >> t;

    int ret = 0;
    for(int i=0; i<k; i++){
        char prev = 'a';
        for(int j=i; j<n; j+=k){
            if(j < k || t[j] != prev){
                if(t[j] == 'r'){
                    ret += p;
                    prev = 'r';
                }
                else if(t[j] == 's'){
                    ret += r;
                    prev = 's';
                }
                else if(t[j] == 'p'){
                    ret += s;
                    prev = 'p';
                }
            }else{
                prev = 'a';
            }
        }
    }
    cout << ret << endl;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
 
using namespace std;
 
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
#define ll	long long
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define fastio	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define hell 1000000007
#define PI 3.141592653589793
 
int main() 
{	fastio;
	int testcases = 1;
	//cin>>testcases;
	while (testcases--) {
		int n;
		ll k;
		cin>>n>>k;
		vi towns(n+1);
		for (int i = 1; i <= n; i++) cin>>towns[i];
		vi visited;
		int current = 1;
		int used = 0;
		while (used < n) {
			visited.pb(towns[current]);
			current = towns[current];
			++used;
			if(used >= k) break;
		}
		
		if (used < k) {
			int pos;
			for (int i = 0; i < sz(visited); i++) {
				if(visited[i] == towns[current]) {
					pos = i;
					break;
				}
			}
			int period = used - pos;
			int rem = (k-used)%period;
			if (rem == 0) rem = period;
			current = visited[pos+rem-1];
		}
		cout<<current;
		cout<<"\n";
		
	}
    return 0; 
}


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

int main() {
  int n,k;
  cin >> n >> k;
  int s[n] = {};

  for(int i=0; i<k; ++i){
    int d;
    cin >> d;
    int a[d];
    for(int j=0; j<d; ++j){
      cin >> a[j];
      s[a[j]-1]=1;
    }
  }

  int count=0;
  for(int i=0; i<n; i++){
    if(s[i]==0) count++;
  }

  cout << count << endl;
}

Measured memory_bytes: 3636
loop do
  height, width = gets.chomp.split.map(&:to_i)
  break if height == 0 && width == 0
  height.times do |h|
    if h == 0 || h == height - 1
      puts '#' * width
    else
      puts '#' + '.' * (width - 2) + '#'
    end
  end
  puts "\n"
end



Measured memory_bytes: 6968
#include<bits/stdc++.h>
typedef long long ll;
ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
std::mt19937 rnd(time(NULL));
#define rand rnd
#define pr std::pair<int,int>
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
template<class T>void cxk(T&a,T b){a=a>b?a:b;}
template<class T>void cnk(T&a,T b){a=a<b?a:b;}
#ifdef mod
int pow(int x,int y){
	int ret=1;
	while(y){
		if(y&1)ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;y>>=1;
	}
	return ret;
}
template<class Ta,class Tb>void inc(Ta&a,Tb b){a=a+b>=mod?a+b-mod:a+b;}
template<class Ta,class Tb>void dec(Ta&a,Tb b){a=a>=b?a-b:a+mod-b;}
#endif
int fir[100010],dis[200010],nxt[200010],id;
void link(int a,int b){nxt[++id]=fir[a],fir[a]=id,dis[id]=b;}
int dist[100010],rt,fa[100010],num[100010],cnt[100010];
void dfs(int x){
	if(dist[x]>dist[rt])rt=x;
	for(int i=fir[x];i;i=nxt[i]){
		if(dis[i]==fa[x])continue;
		dist[dis[i]]=dist[x]+1;fa[dis[i]]=x;
		dfs(dis[i]);
	}
}
int p1[100010],p2[100010];
int main(){
#ifdef LOCAL
	freopen("in.in","r",stdin);
	//freopen("out.out","w",stdout);
#endif
	int n=gi(),a,b,m=0;
	for(int i=1;i<n;++i)a=gi(),b=gi(),link(a,b),link(b,a);
	dist[1]=0,dfs(1);
	a=rt;memset(fa,0,sizeof fa);
	dist[rt]=0,dfs(rt),b=rt;
	for(int i=b;i;i=fa[i])num[i]=++m,cnt[m]=1;
	for(int i=1;i<=n;++i)
		if(!num[i]){
			bool flg=0;
			for(int j=fir[i];j;j=nxt[j])
				if(num[dis[j]]){flg=1;++cnt[num[dis[j]]];break;}
			if(!flg)puts("-1"),exit(0);
		}
	auto get=[&](int*p){
		int now=0,len=0;
		for(int i=1;i<=m;++i){
			for(int j=2;j<=cnt[i];++j)p[++len]=j+now;
			p[++len]=now+1;now+=cnt[i];
		}
	};
	get(p1);
	std::reverse(cnt+1,cnt+m+1);
	get(p2);
	bool flg=0;
	for(int i=1;i<=n;++i)
		if(p1[i]<p2[i]){flg=1;break;}
		else if(p1[i]>p2[i]){flg=0;break;}
	if(!flg)std::swap(p1,p2);
	for(int i=1;i<=n;++i)printf("%d ",p1[i]);
	return 0;
}



Measured memory_bytes: 8192
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define rrep(i, n, m) for (int i = n; i >= m; i--)
#define p(a, b) printf(a, b);
#define c(s) cout << (s) << endl;
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define cyn(c) cout << (c ? "Yes" : "No") << endl;
#define cYN(c) cout << (c ? "YES" : "NO") << endl;
#define pyn(c) printf("%s\n", c ? "Yes" : "No");
#define pYN(c) printf("%s\n", c ? "YES" : "NO");
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> P;
template <class T>
bool contain(const std::string &s, const T &v)
{
    return s.find(v) != std::string::npos;
}

int main()
{
    ULL a,b,x; cin>>a>>b>>x;
    cout<< b/x+1 - (a==0?0:(a-1)/x+1) <<endl;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
    public void solve(BufferedReader stdin, PrintWriter stdout) throws NumberFormatException, IOException {
        int n = Integer.parseInt(stdin.readLine());
        String s = stdin.readLine();
        int ans = n;
        for (int i = 1; i < n; i++) {
            if (s.charAt(i - 1) == s.charAt(i)) ans--;
        }
        stdout.println(ans);
    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter stdout = new PrintWriter(System.out, false);
        new Main().solve(stdin, stdout);
        stdout.flush();
    }
}


Measured memory_bytes: 24404
import sys
input = sys.stdin.readline
n,x = map(int,input().split())
s = list(map(int,input().split()))
s.sort(reverse = True)
mod = 10**9+7

#dp[i][x]:s[i]まででxである場合の数
dp = [[0]*(x+1) for i in range(n+1)]
dp[0][x] = 1
for i,p in enumerate(s):
    for j in range(x+1):
        if dp[i][j] == 0:
            continue
        dp[i+1][j%p] += dp[i][j]
        dp[i+1][j] += dp[i][j]*(n-i-1)
        dp[i+1][j%p] %= mod
        dp[i+1][j] %= mod
res = 0
for i in range(1,x+1):
    res += dp[-1][i]*i
    res %= mod
print(res)

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

string s;
int k;

struct cmp{
bool operator() (string a,string b) {return a<b;}
};
set <string,cmp > st ;

int main(){
    cin >> s >> k;
    for (int i = 1; i <= k; i++){
        for (int j = 0; j + i - 1 < s.size(); j++){
            st.insert(s.substr(j, i));
        }
    }
    int c = 0;
    set<string>::iterator it;
    for (it = st.begin(); it != st.end(); it++){
        c++;
        if (c == k){
            cout << *it;
            break;
        }
    }
}

Measured memory_bytes: 1664
#include<bits/stdc++.h>
#define int long long
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(a) a.size()
#define re return 
#define MP make_pair
#define pb push_back
#define pii pair<int,int>
#define se second
#define fi first
using namespace std;
int n,x[200005],a[200005];
map<int,int> pos;
signed main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x[i];
		a[i]=x[i];
	}
	sort(a,a+n);
	reverse(a,a+n);
	for(int i=0;i<n;i++) pos[a[i]]=i;
	int l=n/2-1;
	for(int i=0;i<n;i++){
		if(pos[x[i]]<=l) cout<<a[l+1]<<"\n";
		else cout<<a[l]<<"\n";
	}
	re 0;
}

Measured memory_bytes: 18988
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 200005

int T[MAXN];

void update(int i,int v)
{
	for(i++;i<MAXN;i+=(i&-i))
		T[i] += v;
}
int getSum(int i)
{
	int v = 0;
	for(i++;i;i-=(i&-i))
		v += T[i];
	return v;
}

int N,K;
int A[MAXN];
long long sm[MAXN];
int cid[MAXN];

bool cmp(int a,int b)
{
	if(sm[a]==sm[b]) return a<b;
	return sm[a]<sm[b];
}

int main()
{
	cin >> N >> K;
	for(int i=0;i<N;i++)
	{
		cin >> A[i];
		A[i] -= K;
	}
	sm[0] = 0;
	for(int i=1;i<=N;i++)
		sm[i] = sm[i-1] + A[i-1];
	for(int i=0;i<=N;i++)
		cid[i] = i;
	sort(cid,cid+N+1,cmp);
	long long ans = 0;
	for(int i=0;i<=N;i++)
	{
		ans += getSum(cid[i]);
		update(cid[i],1);
	}
	cout << ans << '\n';
}

Measured memory_bytes: 4224
#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
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 (y>x)?(x=y,1):0;}
inline int read(){
	int x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x*f;
}
inline ll readll(){
	ll x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
	return x*f;
}
const int maxn=400+10;
struct Edge{
	int x,y;
}E[(int)1e5+10];
bitset<maxn> bit[maxn];
bool vis[maxn];
int main(){
	int n=read(),m=read();
	REP(i,1,m) E[i].x=read(),E[i].y=read();
	REP(i,1,n){
		bit[i][i]=1;
		DREP(j,m,1){
			int x=E[j].x,y=E[j].y;
			if(bit[i][x] && bit[i][y]){
				vis[i]=1;
				break;
			}
			if(bit[i][x] || bit[i][y]) bit[i][x]=bit[i][y]=1;
		}
	}
	int ans=0;
	REP(i,1,n) if(!vis[i]) REP(j,i+1,n) if(!vis[j])
		if((bit[i]&bit[j]).count()==0) ++ans;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 1024
def solve(x:int, l:list):
    cnt = 1
    now = 0
    for i in l:
        now += i
        if now <= x:
            cnt += 1
        else:
            break
    return cnt

def main():
    n, x = map(int, input().split(" "))
    l = list(map(int, input().split(" ")))
    print(solve(x, l))


if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
import collections

N, X = map(int, input().split())
x = [int(i) for i in input().split()]

y = [abs(X-i) for i in x]
y.append(0)
y.sort()
z = [(y[i+1] - y[i]) for i in range(0, N) if (y[i+1] - y[i]) != 0]
z = list(set(z))
a = collections.Counter(z).keys()
D = min(a)
f = True
for i in range(D, 0, -1):
    f = True
    for j in range(0, N):
        if y[j]%D != 0:
            f = False
            break
    if f:
        break
    D = i
print(D)

Measured memory_bytes: 15260
#include <iostream>
#include <cmath>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
typedef long long ll;
const ll INF =0x3f3f3f3f3f3f3f3f;
int main()
{
    ll n, k,ans;
    cin>>n>>k;
    if(n>k)n=n%k;
    //走到这里肯定n比k小，所以再判定一下是否成立。
    ll temp=abs(n-k);
    ans=min(temp,n);
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 3636