akhauriyash/Code-Regression huggingface dataset:
h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))

color = []
for i in range(n):
    for j in range(a[i]):
        color.append(i + 1)

ans = [[0 for i in range(w)] for j in range(h)]

cnt = 0
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            ans[i][j] = color[cnt]
            cnt += 1
    else:
        for j in range(w):
            ans[i][w-1-j] = color[cnt]
            cnt += 1

for i in range(h):
    for j in range(w):
        if j == w - 1:
            print(ans[i][j])
        else:
            print(ans[i][j], end=" ")


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

int main(){
  int n;

  while( true ){
    cin >> n;
    if( !n ) break;

    // a:ツ催渉ャツづ個青ョツ青板、kツ古つづ個和ツづーツ偲ヲツづゥ
    // kツ古つづ個和ツつェnツづづ按づゥツづヲツつ、ツづツつェツ堕カツ催敖つキツづゥツつゥツづーツ陳イツづ猟づゥ
    // n = k*a+k(k-1)/2 
    int sum = 0,a;
    for( int k=2;true;k++ ){
      a = n-k*(k-1)/2;
      if( a<=0 )  break;
      if( a%k==0 ){
	sum++;
	//	cout << k << ",st="<<a/k<<"\t";
      }
    }
    cout << sum << endl;
  }

  return 0;
}

Measured memory_bytes: 0
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
A,B,K = map(int,input().split())

s = set()

for i in range(K):
    if A <= A+i <= B:
        s.add(A+i)
    if A <= B-i <= B:
        s.add(B-i)

Ans = list(s)
Ans.sort()

for ans in Ans:
    print(ans)

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

using namespace std;

int main(){
    int N, X;
    cin >> N >> X;
    vector<int> L(N);
    for(int i = 0; i < N; i++){
        cin >> L[i];
    }
    
    int now = 0;
    int ans = 1;
    
    for(int i = 0; i < N; i++){
        if(now + L[i] <= X){
            ans++;
        }
        
        now = now + L[i];
    }
    
    cout << ans << endl;
}

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

typedef long long lint;
typedef long double louble;

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;
	
	char buf[bufl],*s=buf,*t=buf;
	
	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}
	
	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}
}
using ae86::ty;

const int _ = 303 , __ = _ + _ , mo = 1000000007;

template<typename T1,typename T2> inline T1 ad(T1 &a,T2 b){return a=a+b>=mo?a+b-mo:a+b;}
template<typename T1,typename T2> inline T1 dl(T1 &a,T2 b){return a=a>=b?a-b:a-b+mo;}

int n,nn,val[__],ban[__]={0},ed[__]={0},ha[__]={0},lha=0;
lint f[__][_][_]={0};

int main()
{
	n=ty(),nn=n+n;
	for(int i=1;i<=nn;i++)val[i]=ty();

	int emp=0,par=0;
	for(int i=1;i+1<=nn;i+=2)
	{
		int a=val[i],b=val[i+1];
		if(a<0 && b<0)emp++;
		else if(a>0 && b>0)ban[a]=ban[b]=1;
		else par++,ed[max(a,b)]=1;
	}

	for(int i=nn;i>=1;i--)if(!ban[i])ha[++lha]=i;
	
	f[0][0][0]=1;
	for(int i=1;i<=lha;i++)
		for(int j=0;j<=emp+par;j++)
			for(int k=0;k<=par;k++)
			{
				if(!ed[ha[i]])
				{
					ad(f[i][j+1][k],f[i-1][j][k]);
					if(j>0)ad(f[i][j-1][k],f[i-1][j][k]);
					if(k>0)ad(f[i][j][k-1],f[i-1][j][k]*k%mo);
				}
				else
				{
					ad(f[i][j][k+1],f[i-1][j][k]);
					if(j>0)ad(f[i][j-1][k],f[i-1][j][k]);
				}
			}
	
	lint ans=f[lha][0][0];
	for(int i=2;i<=emp;i++)ans=ans*i%mo;
	printf("%lld\n",ans);

	return 0;
}


Measured memory_bytes: 431616
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

MOD = 998244353

def mod_add(a, b):
  return (a+b)%MOD

def mod_sub(a, b):
  return (a+MOD-b)%MOD

def mod_mul(a, b):
  return a*b%MOD

#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = (1,0,a)
    w = (0,1,b)
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return (w[0],w[1])

def mod_inv(a):
    x,_  = extgcd(a, MOD)
    return (MOD+x%MOD)%MOD

def mod_div(a, b):
    return a*mod_inv(b)%MOD

def mod_pow(a, b):
    res = 1
    while b > 0:
        if b & 1:
            res = res * a % MOD
        a = a * a % MOD
        b >>= 1
    return res

MAX = 510000

fac  = [0]*MAX
finv = [0]*MAX
inv  = [0]*MAX

def cominit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;

def mod_com(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;

cominit()


res = 0

n,m,k=l_in()

for j in range(k+1):
    tmp = m
    tmp = mod_mul(tmp, mod_pow(m-1,n-1-j))
    tmp = mod_mul(tmp, mod_com(n-1,j))
    res = mod_add(res, tmp)

print(res)








Measured memory_bytes: 86172
#include <bits/stdc++.h>
#define USE_MATH_DEFINES
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
    int n, ans = 0;
    cin >> n;
    int p[n];
    rep(i, n) { cin >> p[i]; }
    for (int i = 1; i < n - 1; i++) {
        if (p[i - 1] <= p[i] && p[i] < p[i + 1])
            ans++;
        else if (p[i - 1] > p[i] && p[i] >= p[i + 1])
            ans++;
    }
    printf("%d\n", ans);
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define M 800010
using namespace std;
int i,j,k,m,n,x,y,z,cnt,S,T;
int F[M],L[M],N[M],a[M],c[M],Gap[M],dis[M];
void add(int x,int y,int z,int fla=0){
	a[++k]=y;c[k]=z;
	if(!F[x])F[x]=k;
	else N[L[x]]=k;
	L[x]=k;
	if(!fla)add(y,x,0,1);
}
int q[200010],cur[200010],h,t;
bool BFS(int x,int y){
	q[t=1]=x;h=0;
	for(int i=1;i<=n;i++)cur[i]=F[i],dis[i]=1000000000;
	dis[x]=0;
	while(h<t){
		x=q[++h];
		for(int i=F[x];i;i=N[i])if(c[i]&&dis[a[i]]>n){
			dis[a[i]]=dis[x]+1;
			if(a[i]==y)return 1;
			q[++t]=a[i];
		}
	}
	return dis[y]<=n;
}
int dfs(int x,int T,int flow){
	if(x==T)return flow;
	int used=0;
	for(int i=cur[x];i;i=cur[x]=N[i])if(c[i]&&dis[a[i]]==dis[x]+1){
		const int v=dfs(a[i],T,min(flow-used,c[i]));
		if(!v)continue;c[i]-=v;c[i^1]+=v;used+=v;
		if(used>=flow)return used;
	}
	return used;
}
int dinic(int S,int T){
	int ans=0;
	while(BFS(S,T))ans+=dfs(S,T,1000000000);
	return ans;
}
int A[200010],B[200010],P[200010],Q[200010];
int cnt1,cnt2;
int main(){
	//freopen("dd.in","r",stdin);
	scanf("%d",&n);k=1;
	for(int i=1;i<=n;i++)scanf("%d",&A[i]),A[i]++;
	for(int i=1;i<=n;i++)scanf("%d",&B[i]),B[i]++;
	for(int i=1;i<=n;i++)if(!P[i]){
		P[i]=++cnt1;
		for(int x=A[i];!P[x];x=A[x])P[x]=cnt1;
	}
	for(int i=1;i<=n;i++)if(!Q[i]){
		Q[i]=++cnt1;
		for(int x=B[i];!Q[x];x=B[x])Q[x]=cnt1;
	}	
	int all=n;
	S=cnt1+1;T=cnt1+2;
	for(int i=1;i<=n;i++){
		if(A[i]==B[i]){
			if(A[i]==i)all--;
			else {
				add(P[i],Q[i],1);
				add(Q[i],P[i],1);
			}
		}
		else {
			if(A[i]==i){
				add(Q[i],T,1);
			}
			else if(B[i]==i){
				add(S,P[i],1);
			}
			else {
				add(Q[i],P[i],1);
			}
		}
	}
	n=T;
	cout<<all-dinic(S,T);
	return 0;
}

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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	string str;
	cin>>str;
	int n=str.size();
	int q,t,f;
	cin>>q;
	char c;
	string str1,str2;
	int flag=0;
	for(int i=1;i<=q;++i)
	{
	    cin>>t;
	    if(t==1)
	    flag=!flag;
	    else
	    {
	        cin>>f>>c;
	        if(flag==0)
	        {
	            if(f==1)
	            {
	                str1+=c;
	            }
	            else
	            {
	                str2+=c;
	            }
	        }
	        else
	        {
	            if(f==2)
	            {
	                str1+=c;
	            }
	            else
	            {
	                str2+=c;
	            }
	        }
	    }
	}
	reverse(str1.begin(),str1.end());
	str=str1+str+str2;
	if(flag==1)
	reverse(str.begin(),str.end());
	cout<<str<<endl;
	
}


Measured memory_bytes: 1200
#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
ll dp[3005][3005][4], b[3005][3005];
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	string s, t;
	cin >> s >> t;
	int n = s.size(), m = t.size();
	int ans = inf;
	rep(i, n - m + 1) {
		int now = 0;
		rep(j, m) {
			if (s[i + j] != t[j]) now++;
		}
		ans = min(ans, now);
	}
	cout << ans << "\n";
	return 0;
}

Measured memory_bytes: 3600
class Dice:
    TOP, SOUTH, EAST, WEST, NORTH, BOTTOM = 0, 1, 2, 3, 4, 5
    def __init__(self, d):
        self.dice = d
        self.top = d[0]
        self.south = d[1]
        self.east = d[2]
        self.west = d[3]
        self.north = d[4]
        self.bottom = d[5]

    def set_top_front(self, top, front):
        for i, n in enumerate(self.dice):
            if n == top:
                if i == Dice.TOP: pass
                elif i == Dice.SOUTH: self.move("N")
                elif i == Dice.EAST: self.move("W")
                elif i == Dice.WEST: self.move("E")
                elif i == Dice.NORTH: self.move("S")
                elif i == Dice.BOTTOM:
                    self.move("S")
                    self.move("S")

                break
        
        for i, n in enumerate(self.dice):
            if n == front:
                if i == Dice.TOP: print("error.")
                elif i == Dice.SOUTH: pass
                elif i == Dice.EAST: self.move("R")
                elif i == Dice.WEST: self.move("L")
                elif i == Dice.NORTH:
                    self.move("R")
                    self.move("R")
                elif i == Dice.BOTTOM: print("error.")

                break
            
        
    def move(self, d):
        if d == "N":
            self.move_north()
        elif d == "W":
            self.move_west()
        elif d == "S":
            self.move_south()
        elif d == "E":
            self.move_east()
        elif d == "R":
            self.move_right()
        elif d == "L":
            self.move_left()

        self.dice = [
            self.top,
            self.south,
            self.east,
            self.west,
            self.north,
            self.bottom,
        ]

    def move_right(self):
        self.north, self.west, self.south, self.east \
            = self.west, self.south, self.east, self.north

    def move_left(self):
        self.north, self.west, self.south, self.east \
            = self.east, self.north, self.west, self.south

    def move_north(self):
        self.top, self.south, self.bottom, self.north \
            = self.south, self.bottom, self.north, self.top

    def move_south(self):
        self.top, self.south, self.bottom, self.north \
            = self.north, self.top, self.south, self.bottom

    def move_east(self):
        self.top, self.east, self.bottom, self.west \
            = self.west, self.top, self.east, self.bottom

    def move_west(self):
        self.top, self.east, self.bottom, self.west \
            = self.east, self.bottom, self.west, self.top

if __name__ == "__main__":
    import re

    nums = [int(n) for n in re.split(r"\s+", input().strip())]
    dice = Dice(nums)

    n = int(input().strip())
    for i in range(n):
        top, front = [int(n) for n in re.split(r"\s+", input().strip())]
        dice.set_top_front(top, front)

        print(dice.east)



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

using namespace std;

int N, D;
int main(){
    
    cin >> N >> D;
    
    int ans = 1;
    int tree = 1;
    
    for (int i = 1; i <= N; i++) {
        if (tree == i - D) {
            tree = i + D + 1;
            i = tree;
            if (i <= N) {
                ans += 1;
            }
            
        }
        
    }
    cout << ans << endl;
    
}


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

#define watch(x) cout << (#x) << " is " << (x) << endl
#define fr(i,n) for(int i=0; i<n; i++)
#define rep(i, st, en) for(int i=st; i<=en; i++)
#define repn(i, st, en) for(int i=st; i>=en; i--)
#define sq(a) (a*a)
typedef long long ll;
typedef pair<int, pair<int, int>> pii;
typedef pair<ll, ll> pll;
ll mod = 1e9+7;

template <typename T>
void max_self(T &a, T b){
    a = max(a, b);
}

void solve(){
    string s, t;
    cin>>s>>t;
    int n(s.size()), m(t.size());

    /// find longest common subsequence of s and t and print the subsequence
    /// pis<int, pii> dp[i][j] stores lcs length till s[i-1], t[j-1] as first element
    /// and stores the second element as the pair indices, from it came from
    vector<vector<pii>> dp(n+1, vector<pii>(m+1, {-1, {0, 0}}));

    for(int i = 0; i<n; i++){
        for(int j = 0; j<m; j++){
            if(s[i] == t[j]){
                max_self(dp[i+1][j+1], make_pair(dp[i][j].first + 1, make_pair(i, j)));
            }
            max_self(dp[i+1][j], make_pair(dp[i][j].first + 0, make_pair(i, j)));
            max_self(dp[i][j+1], make_pair(dp[i][j].first + 0, make_pair(i, j)));
        }
    }


    pii ans = {0, {0, 0}};
    for(int i=0; i<=n; i++){
        for(int j=0; j<=m; j++){
            max_self(ans, make_pair(dp[i][j].first, make_pair(i, j)));
        }
    }

    string z;
    while(ans.second.first && ans.second.second){
        int i = ans.second.first;
        int j = ans.second.second;

        pair<int, int> previous(dp[i][j].second);

        if(previous == make_pair(i - 1, j - 1)){
            z += s[i-1];    /// s[i - 1] = t[j-1]
        }

        ans.second = previous;
    }

    reverse(z.begin(), z.end());

    cout<<z;
}

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

    ll t = 1;
    //  cin>>t;
    while(t--){
        solve();
    }

    return 0;
}



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

const ll INF = 1LL << 60;

ll N, K;
ll A[110][110];
int main() {
    cin >> N >> K;

    map<ll, ll> mp;
    rep(i, K) {
        ll d;
        cin >> d;
        rep(j, d) {
            cin >> A[i][j];
            mp[A[i][j]]++;
        }
    }
    ll ans = 0;
    for (int i = 1; i <= N; i++) {
        if (mp[i] == 0) ans++;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3688
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define repab(i,a,b) for(int i=a;i<=b;i++)
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define  printarr(v) for(auto x:v){cout<<x<<" "; }
#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;
typedef long double ld;
ll mod = 1e9 + 7;
ll dp[2005];
ll ok(ll val)
{
	if (val == 0)
	{
		return 1LL;
	}
	if (val == 1 || val == 2)
	{
		return 0LL;
	}
	if (val >= 3 && val <= 5)
	{
		return 1LL;
	}
	if (dp[val] != -1)
	{
		return dp[val];
	}
	ll ans = 0;
	for (ll i = 3; i <= val; i++)
	{
		if (val - i >= 0)
		{
			ans = (ans + ok(val - i)) % mod;
			ans = ans % mod;
		}
	}
	return dp[val] = ans;
}
void solve()
{
	ll n;
	cin >> n;
	memset(dp, -1, sizeof(dp));
	dp[0] = 1;
	cout << ok(n) << endl;

}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	t = 1;
	while (t--)
	{
		solve();

	}
}


Measured memory_bytes: 3624
///╔══╗
///╚╗╔╝
///╔╝╚(¯`v´¯)
///╚══╝`.¸.´farzin


#include <bits/stdc++.h>

#define int int64_t
#define pb push_back
#define ibsws ios_base::sync_with_stdio
#define gcd __gcd
#define s second
#define f first
typedef std::pair<int,int> pii ;

using namespace std;

const int N=1e5+1e2;
int n;
int dp[N][4],a[N][3];

int32_t main(){
	ibsws(true==false);
	cin>>n;
	for(int i=0;i<n;i++){
        cin>>a[i][0]>>a[i][1]>>a[i][2];
	}
	dp[0][0]=a[0][0];
	dp[0][1]=a[0][1];
	dp[0][2]=a[0][2];
	for(int i=1;i<n;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                if(j==k){
                    continue;
                }
                dp[i][j]=max(dp[i][j],dp[i-1][k]+a[i][j]);
            }
        }
	}
	cout<< max( dp[n-1][0], max( dp[n-1][1], dp[n-1][2] ) );
}


Measured memory_bytes: 5760
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define int ll

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s, t;
  cin >> n;
  cin >> s >> t;
  for (int len = n; len >= 0; len--) {
  	bool ok = true;
  	int start = n - len;
  	for (int i = 0; i < len; i++) {
  		if (s[start + i] != t[i]) {
  			ok = false;
  			break;
  		}
  	}
  	if (ok) {
  		cout << 2 * n - len << '\n';
  		exit(0);
  	}
  }
} 

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include <utility>
#include <functional>
#include<fstream>
#define debug(x) cout << #x << " = " << x << endl
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,n) for(int i = (n); i > -1; i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const double EPS = 1e-9;

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

  int n;
  cin>>n;
  int e[n + 1];
  rep(i, n + 1) e[i] = 0;
  FOR(i, 2, n + 1) {
    int tmp = i;
    FOR(j, 2, i + 1) {
      while(tmp % j == 0) {
        e[j]++;
        tmp /= j;
      }
    }
  }
  int p75 = 0,p25 = 0, p15 = 0, p5 = 0, p3 = 0;
  rep(i, n + 1) {
    if(e[i] >= 74) p75++;
    if(e[i] >= 24) p25++;
    if(e[i] >= 14) p15++;
    if(e[i] >=  4) p5++;
    if(e[i] >=  2) p3++;
  }
  int ans = p75 + p25 * (p3 - 1) + p15 * (p5 - 1) + p5 * (p5 - 1) * (p3 - 2) / 2;
  cout << ans << endl;


  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T>
void print(const vector<T> &x) {
    int n = x.size();
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x) {
    int n = x.size();
    rep(i,n) {
        rep(j,x[i].size()) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

template<typename T>
void print(const vector<T> &x, int n) {
    rep(i,n) {
        cout << x[i];
        if (i!=n-1) cout<<" ";
        else cout << endl;
    }
}

template<typename T>
void print(const vector<vector<T>> &x, int n, int m) {
    rep(i,n) {
        rep(j,m) {
            cout << x[i][j] << " ";
        }
        cout << endl;
    }
}

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

void input_init() {
    cin.tie(0); ios::sync_with_stdio(false);
}

int main(int argc, char const *argv[]) {
    ll k; cin>>k;
    ll cur = 0;
    rep(i,2*k) {
        cur = cur*10ll + 7;
        cur %= k;
        if (cur == 0) {
            cout << i+1 << '\n';
            return 0;
        }
    }

    cout << -1 << '\n';
    return 0;
}

Measured memory_bytes: 3632
n = int(input())
s = input()

w_e = [0] * n
e_w = [0] * n
ans = []

for i in range(n - 1):
    if s[i] == 'W':
        w_e[i + 1] = w_e[i] + 1
    else:
        w_e[i + 1] = w_e[i] + 0

for i in range(n - 2, -1, -1): 
    if s[i + 1] == 'E':
        e_w[i] = e_w[i + 1] + 1
    else:
        e_w[i] = e_w[i + 1] + 0

for i in range(n):
    ans.append(w_e[i] + e_w[i])

print(min(ans))

Measured memory_bytes: 39180
#19:38
#20:19

H,W,D = map(int,input().split())
MAX = H*W
A = [list(map(int,input().split())) for i in range(H)]
Point = [0]*(MAX+1)
for h in range(H):
    for w in range(W):
        Point[A[h][w]] = (h,w)
K = []
for i in range(1,D+1):
    List = []
    n = i
    h,w = Point[n]
    cost = 0
    while n<=MAX:
        y,x = Point[n]
        cost += abs(h-y)+abs(w-x)
        List.append(cost)
        h,w = y,x
        n+=D
    K.append(List)
Q = int(input())
for i in range(Q):
    L,R = map(int,input().split())
    a,l,r = (L-1)%D,(L-1)//D,(R-1)//D
    print(K[a][r]-K[a][l])

Measured memory_bytes: 24304
package main

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

var (
	scanner = bufio.NewScanner(os.Stdin)
	w       = bufio.NewWriter(os.Stdout)
)

func readInt() (read int) {
	scanner.Scan()
	read, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return
}

func arrayContains(A []int, B int, N int) bool {
	for i := 0; i < N; i++ {
		if A[i] == B {
			return true
		}
	}
	return false
}

func main() {
	scanner.Split(bufio.ScanWords)
	defer w.Flush()

	x := readInt()
	n := readInt()

	if n == 0 {
		fmt.Println(x)
		return
	}

	p := make([]int, n)

	for i := 0; i < n; i++ {
		p[i] = readInt()
	}

	if !arrayContains(p, x, n) {
		fmt.Println(x)
		return
	}

	for i := 0; i < 100; i++ {
		l := x - i
		if !arrayContains(p, l, n) {
			fmt.Println(l)
			return
		}
		r := x + i
		if !arrayContains(p, r, n) {
			fmt.Println(r)
			return
		}
	}
}


Measured memory_bytes: 1760
H,W=map(int,input().split())
if H%3==0 or W%3==0:
    print(0)
    exit()
def minAbsDiff(s,t,u):
    list=[s,t,u]
    list.sort()
    return(list[2]-list[0])
isHEven = H%2==0
isWEven = W%2==0
ans = H*W
for w in range(1,W):
    s = H*w
    if isHEven:
        t=u=(H//2)*(W-w)
    else:
        u=(H//2)*(W-w)
        t=(H//2 + 1)*(W-w)
    ans = min(ans,minAbsDiff(s,t,u))
    if (W-w)%2 ==0:
        t = u = H*(W-w)//2
    else:
        t = H*((W-w)//2)
        u = H*((W-w)//2 + 1)
    ans = min(ans,minAbsDiff(s,t,u))

for h in range(1,H):
    s = h*W
    if isWEven:
        t=u=(W//2)*(H-h)
    else:
        u=(W//2)*(H-h)
        t=(W//2 + 1)*(H-h)
    ans = min(ans,minAbsDiff(s,t,u))

    if (H-h)%2==0:
        t = u = W*(H-h)//2
    else:
        t = W*((H-h)//2)
        u = W*((H-h)//2 + 1)
    ans = min(ans,minAbsDiff(s,t,u))

print(ans)

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

namespace AtCoderCs
{
    class Program
    {
        static void Main()
        {

            var Arg = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            
            // 木の本数
            int[] Trees = new int[Arg[0]];

            for (int i = 0; i < Arg[0]; i++)
            {

                Trees[i] = int.Parse(Console.ReadLine());

            }

            Array.Sort(Trees);

            // 高位差
            int Checker = Arg[0] - Arg[1] + 1;
            int[] Voltage = new int[Checker];
            for (int i = 0; i < Checker; i++)
            {

                Voltage[i] = Trees[i + Arg[1] - 1] - Trees[i];

            }

            Console.WriteLine(Voltage.Min());

        }


    }
}


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

int main() {
  
  int64_t mod = 1000000007;
  
  int N; cin >> N; int64_t ans = 1;
  
  for (int i = 1; i <= N; i++) {
    ans *= i; ans %= mod;
  }
  
  cout << ans << endl;
  
}

Measured memory_bytes: 256
tmp = input()
x = []
for i in range(4):
    x.append(int(tmp[i]))

for i in range(8):
    s = x[0]
    op = []
    for j in range(3):
        if i >> j & 1:
            s += x[j+1]
            op.append("+")
        else:
            s -= x[j+1]
            op.append("-")
    if s == 7:
        ans = tmp[0] + op[0] + tmp[1] + op[1] + tmp[2] + op[2] + tmp[3] + "=7"
        break
print(ans)

Measured memory_bytes: 3064
while True:
    a, op, b = input().split()
    a = int(a)
    b = int(b)
    if op == '?':
        break
    elif op == '+':
        print(a + b)
    elif op == '-':
        print(a - b)
    elif op == '*':
        print(a * b)
    elif op == '/':
        print(a // b)
    


Measured memory_bytes: 5596
import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		long max = 0;
		for (int i = 0; i < 5; i++) {
		    long x = sc.nextLong();
		    max = Math.max(max, (n + x - 1) / x);
		}
		System.out.println(max + 4);
   }
}


Measured memory_bytes: 23124
bingo = [list(map(int,input().split())) for i in range(3)]
N = int(input())
for _ in range(N):
    n = int(input())
    for i in range(3):
        for j in range(3):
            if bingo[i][j] == n:
                bingo[i][j] = -1

for i in range(3):
    if sum(bingo[i]) == -3:
        print("Yes")
        break
else:
    for i in range(3):
        if bingo[0][i]+bingo[1][i]+bingo[2][i] == -3:
            print("Yes")
            break
    else:
        if bingo[0][0]+bingo[1][1]+bingo[2][2] == -3 or bingo[0][2]+bingo[1][1]+bingo[2][0] == -3:
            print("Yes")
        else:
            print("No")

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

int main()
{

    string a;
    int sum,i;
    while(cin>>a)
    {
        i=0;
        sum=0;
        if(a[0]==48) break;
        while(a[i]!=0){
        sum=sum+(a[i]-48);
        i++;
        }

        cout<<sum<<endl;
    }


    return 0;
}

Measured memory_bytes: 3176
// problem: "https://atcoder.jp/contests/abc122/tasks/abc122_b"

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s;

    cin>>s;
    int i;
    int M,c;
    M = c = 0;
    for(i=0;i!=s.size();i++)
    {
        if(s[i]=='A' || s[i]=='T' || s[i]=='G' || s[i]=='C')
            c++;

        else
        {
            M = max(M,c);
            c = 0;
        }
    }

    M = max(M,c);
    cout<<M;

    return 0;
}


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


int main(){

    char b,c[128]; cin >> b;
    c['A'] = 'T';
    c['T'] = 'A';
    c['G'] = 'C';
    c['C'] = 'G';

    cout << c[b] << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using Bint = mp::cpp_int;
// using Real = mp::number<mp::cpp_dec_float<1024>>;

int main(){
    int N;
    string S;
    cin >> N >> S;
    vector<int> E(N + 10), W(N + 10);
    for(int i = 1; i<= N; i++){
        W.at(i) = W.at(i - 1);
        if(S.at(i - 1) == 'W') W.at(i)++;
    }

    for(int i = N; i >= 1; i--){
        E.at(i) = E.at(i + 1);
        if(S.at(i - 1) == 'E') E.at(i)++;
    }

    int ans = INF;
    for(int i = 1; i<= N; i++){
        ans = min(ans, W.at(i - 1) + E.at(i + 1));
    }

    cout << ans << endl;
}

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


void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#define fo(i, n) for(int i=0; i<n; i++)
#define Fo(i, k, n) for(int i=k; i<n; i++)
#define w(x)  int x; cin>>x; while(x--)
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"
#define watch3(x,y,z) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<"\n"
#define watch4(x,y,z,q) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<" and "<<(#q)<<" is "<<(q)<<"\n"
#define MOD 1000000007
#define INF 1000000007;
#define F first
#define S second
#define pb(x) push_back(x);


#define ll  long long
#define all(c) (c).begin(),(c).end()
#define endl "\n"

typedef vector<int> vl;
typedef vector< vl > vvl;
typedef pair< int, int> pll;
typedef map< int, int> mll;


#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif





void c_p_c()
{
#ifndef ONLINE_JUDGE
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
void parray(int a[], int n) {
	for (int i = 0; i < n; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
}
int no = 1, n1;
int recu(int n, int k, int val) {


	if (n == n1) {
		return min(val * 2, val + k);
	}
	return min( recu(n + 1, k, 2 * val), recu(n + 1, k, val + k)              );


}
int digit(int n) {

	int temp = 0;
	while (n) {
		// temp += n % 10;
		n = n / 10;
		temp++;
	}



	return temp;
}

bool check(int a[], int n) {
	for (int i = 0; i < n; ++i)
	{
		/* code */
		if (a[i] % 2)
			return false;
	}
	return true;
}
bool ispalin(int n) {


	string  str = to_string(n);
	// cout<<str<<endl;


	for (int i = 0; i < str.size() / 2; ++i)
	{
		/* code */
		// watch2(str[i],str[str.size()-i-1]);
		if (str[i] != str[str.size() - i - 1]) {
			return false;
		}
	}


	return true;


}

int main() {
	int i,  n, m, k;
	ll sum=0;
	c_p_c();
	// cin>>n;
	int a,b,c;
	cin>>a>>b>>c;
	int ans = (a+b-1)/b;
	cout<<ans*c<<endl;

}


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

int main() {
  int a, b;
  
  int count = 0;
  for (int h=0; ; h++) {
    cin >> a >> b;
    if (h != 0) {
      cout << count << endl;
      count = 0;
    }
    if (a == 0 && b == 0) break;
    for (int i=1; i<=a; i++) {
      for (int j=1; j<=a; j++) {
        for (int k=1; k<=a; k++) {
          if (i+j+k == b && i<j && j<k) count++;
        }
      }
    }
  }
  return 0;
}


Measured memory_bytes: 3104
l, r = map(int, input().split())
ans = 10 ** 9
for i in range(l, r + 1):
    for j in range(i + 1, r + 1):
        ans = min(ans, (i * j) % 2019)
        if ans == 0:
            print(ans)
            exit()
print(ans)


Measured memory_bytes: 2940
import java.util.Scanner;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] input = new int[n];
    int difference = 0;
    for(int i = 0;i<n;i++){
      input[i] = sc.nextInt();
      if(input[i] != i + 1){
        difference++;
      }
    }
    if(difference == 0 || difference == 2){
      System.out.println("YES");
    }else{
      System.out.println("NO");
    }
  }
}

Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,int> P1;
typedef pair<P,P> P2;
typedef pair<double,int>Q;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
//#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int n,k;
ll mod,t[305][305],dp[305][305];
int main(){
    cin>>n>>k>>mod;
    repn(i,305) dp[i][1] = 1;
    repn(i,305) dp[0][i] = 1;
    t[0][0] = 1;
    for(int i=1;i<305;i++){
        for(int j=0;j<=i;j++){
            t[i][j] += t[i-1][j];
            if(j) t[i][j] += t[i-1][j-1] * 1LL * (i-j+1) % mod;
            t[i][j] %= mod;
        }
    }
    for(int i=1;i<305;i++){
        for(int j=2;j<305;j++){
            for(int k=0;k<=i;k++){
                dp[i][j] += dp[i-k][j-1] * t[i][k] % mod;
                dp[i][j] %= mod;
            }
        }
    }
    cout<<dp[n][k]<<endl;
}

Measured memory_bytes: 1664
#include <bits/stdc++.h>
using namespace std;
using Int = long long int;
template<typename T> void swap(T *t1, T *t2){ T* tmp=t1; t1=t2; t2=tmp;}
Int tmpi = 0;
double tmpd = 0.0;


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

    Int n;
    map<Int, Int> a;
    cin >> n;
    for(Int i=0; i<n; i++){
        cin >> tmpi;
        a.insert(make_pair(tmpi, i));
    }

    for(auto const& entry: a){
        cout << entry.second+1 << " ";
    }
    

    return 0;
}

Measured memory_bytes: 7040
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
#define all(x) (x).begin(), (x).end()  // sortなどの引数を省略
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define max3(x, y, z) max(x, max(y, z))

#ifdef _MSC_FULL_VER  //デバッグ出力
#define dout cout
#define debug() if (true)
#define check(x) std::cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) std::cout << "☆" << x << endl
#else
#define dout \
  if (false) cout
#define debug() if (false)
#define check(x) \
  if (false) cout << "★" << #x << "の値:" << (x) << endl
#define pass(x) \
  if (false) cout << "☆" << x << endl
#endif
using namespace std;
int main() {
  int a,b,c;
  cin >> a >> b >> c;
  cout << min(a+b,min(b+c,c+a));
}
/*
//よく使うやつ
int N;
cin >> N;
vector<int> list(N);
for (int i = 0; i < N; i++) {
  cin >> list[i];
}
// for文
for (int i = 0; i < N; i++) {
}
//配列
vector<int> list;
list.at(i)
vector<vector<int>> list(10, vector<int>(10));
sort(all(list));
sort(all(list), greater<int>());
    //配列の内容表示
dout << "====listの内容====\n";
for (int i = 0; i < N; i++) dout << list[i] << endl;
dout << "====ここまで====\n";
*/


Measured memory_bytes: 256
/*author: hyperion_1724
  date: 
*/

//Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

//Required namespaces

using namespace std;
using namespace __gnu_pbds;

//Required defines

#define READ(X) cin>>X;
#define READV(X) long long X; cin>>X;
#define READAR(A,N) long long A[N]; for(int i=0;i<N;i++) {cin>>A[i];}
#define rz(A,N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define endl '\n'
#define trav(x,A) for(auto &x:A)
#define veci vector<int>
#define vecl vector<long long>
#define FORI(a,b,c) for(int a=b;a<c;a++)
#define FORD(a,b,c) for(int a=b;a>c;a--)

//Required typedefs

typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<long long,null_type,greater<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<long,long> pll;

//Required Constants

const long long inf=(long long)1e18;
const long long MOD=(long long)(1e9+7);
const long long INIT=(long long)(1e6+1);

//Required Functions

ll power(ll x,ll y) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2) % MOD; 
    p = (p * p) % MOD; 
  
    return (y%2 == 0)? p : (x * p) % MOD; 
}
ll modInverse(ll a) 
{ 
   return power(a,MOD-2); 
   
}

//Work

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

	READV(N);
	ll val[N+1]={0};
	ll A[N+1]={0};
	FORI(i,1,N+1)
	{
		READ(A[i]);
	}
	FORD(i,N,0)
	{
		ll ch=0;
		for (ll j = 2*i; j <= N; j+=i)
		{
			ch+=val[j];
		}
		if (ch%2==A[i])
		{
			val[i]=0;
		}
		else
		{
			val[i]=1;
		}
	}
	vecl ans;
	FORI(i,1,N+1)
	{
		if (val[i]==1)
		{
			ans.pb(i);
		}
	}
	cout<<sz(ans)<<endl;
	FORI(i,0,sz(ans))
	{
		cout<<ans[i]<<" ";
	}
	cout<<endl;

	return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(8, vector<int>(8));
  for(int i = 0; i < m; i++){
    int a, b;
    cin >> a >> b;
    g[a-1][b-1] = g[b-1][a-1] = 1;
  }
  vector<int> node;
  for(int i = 0; i < n; i++){
    node.push_back(i);
  }
  int ans = 0;
  bool ok;
  do{
    ok = true;
    for(int i = 0; i < n-1; i++){
      if(g[node[i]][node[i+1]] == 0) ok = false;
    }
    if(ok) ans++;
  }while(next_permutation(node.begin()+1, node.end()));
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b){ a>b?:a=b; }
template<class T, class C> void chmin(T& a, C b){ a<b?:a=b; }
int main(int argc, char* argv[]) {
   int a, b, c;
   cin >> a >> b >> c;
   if((a<=c && c <= b)|| a >=c && c>=b) cout << "Yes"<< endl;
   else cout << "No" << endl;

    return 0;
}


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

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

const int INF = 1000000007;

int main(void){
	int x; cin>>x;

	ll i = 0, sum = 0;
	while(sum < x){
		i++;
		sum += i;
		//cout<<sum<<endl;
	}

	printf("%lld\n", i);

	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
 
#define NMIN 1
#define NMAX 36
 
struct Number{
char mark;
char value;  
};
 
 
void bubble(struct Number A[],int N){
  int i,j;
 struct Number t;
  for(i=0;i<N;i++){
for(j=N-1;j>=i;j--){
  if(A[j].value<A[j-1].value){
    t=A[j];
    A[j]=A[j-1];
    A[j-1]=t;
  }
}    
  }
}
 
void selections(struct Number A[],int N){
  int i,j,minj;
 struct Number t;
  for(i=0;i<N;i++){
minj=i; 
for(j=i;j<N;j++){
  if(A[j].value<A[minj].value) minj=j;
}
t=A[i];
A[i]=A[minj];
A[minj]=t;
  }
}
 
void print(struct Number A[],int N){
int i;
for(i=0;i<N;i++){
  if(i>0) printf(" ");
  printf("%c%c",A[i].mark,A[i].value);
}
 printf("\n");
}
 
int isStable(struct Number N1[],struct Number N2[],int N){
int i;
  for(i=0;i<N;i++){
    if(N1[i].mark != N2[i].mark) return 0;
  }
  return 1;
}
 
 
 
int main(){
int N,i,j;
struct Number N1[NMAX],N2[NMAX];
char ch;
scanf("%d",&N);
if(N<NMIN||N>NMAX) exit(1);
for(i=0;i<N;i++){
  scanf(" %c%c",&N1[i].mark,&N1[i].value);
}
for(i=0;i<N;i++){
N2[i]=N1[i];
}
bubble(N1,N);
selections(N2,N);
 
print(N1,N);
printf("Stable\n");
print(N2,N);
if(isStable(N1,N2,N)==1){
  printf("Stable\n");
}
else{
  printf("Not stable\n");
}
  return 0;
}



Measured memory_bytes: 2080
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
def dfs(G,s,p):
    for nv in G[s]:
        if(nv==p):
            continue
        Count[nv]+=Count[s]
        dfs(G,nv,s)
    
N,Q=map(int,input().split())
Count=[0]*N
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
for i in range(Q):
    p,x=map(int,input().split())
    Count[p-1]+=x
dfs(G,0,-1)
print(*Count)

Measured memory_bytes: 230928
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)
#define REV(X, Y, Z) for (ll(X) = (Y); (X) > (Z); (X)--)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

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

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

int main() {
  init();

  ll N;
  cin >> N;
  vll A(N + 1, 0), B(N + 1, 0);
  REP(i, 1, N + 1) {
    cin >> A[i] >> B[i];
  }

  ll ans = 0;
  ll sum = 0;
  REV(i, N, 0) {
    if ((A[i] + sum) % B[i] == 0) {
      continue;
    }

    ll s = B[i] - (A[i] + sum) % B[i];
    ans += s;
    sum += s;
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

int main(){
    string s;
    cin >> s;
    int r=0,b=0;
    for(int i=0;i<int(s.length());i++){
        if(s[i] == '0'){
            r++;
        }else{
            b++;
        }
    }
    cout << min(r,b) * 2 << endl;
}

Measured memory_bytes: 512
#include <iostream>

using namespace std;
int n,i,p,l,r,c,nr;
int a[200005];
int b[200005];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin>>n;
    for(i=1;i<=n;i++)
        cin>>a[i];
    for(i=1;i<=n;i++)
        cin>>b[i];

    for(i=1;i<=n/2;i++)
        swap(b[i], b[n-i+1]);

    for(i=1;i<=n;i++)
    {
        if(a[i]==b[i])
        {
            if(!c)
                l=i;
            r=i;
            c=a[i];
        }
    }

    if(!c)
    {
        cout<<"Yes\n";
        for(int i=1;i<=n;i++)
            cout<<b[i]<<' ';
        return 0;
    }

    nr=0;
    for(i=1;i<=n;i++)
    {
        if(a[i]==c)
            nr++;
        if(b[i]==c)
            nr++;
    }
    if(nr>n)
    {
        cout<<"No";
        return 0;
    }

    p=l;
    for(i=1;i<=n;i++)
    {
        if(a[i]==c)
            continue;
        if(b[i]==c)
            continue;

        if(p>r)
            break;

        swap(b[i], b[p]);
        p++;
    }

    cout<<"Yes\n";
    for(i=1;i<=n;i++)
        cout<<b[i]<<' ';
    return 0;
}


Measured memory_bytes: 5216
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
 
int main(){
  int N;
  cin>>N;
  vector<int> A(N);
  rep(i,N) cin>>A[i];
  vector<int> a(N);
  copy(all(A),a.begin());
  sort(all(a));
  reverse(all(a));
  rep(i,N){
    if(A[i]==a[0]){
      cout<<a[1]<<endl;;
    }
    else{
      cout<<a[0]<<endl;
    }
  }
}

Measured memory_bytes: 4824
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16) 
#define rep(i,n) for(int i=0;i<n;i++ )
#define rep2(i,f,n) for(int i=f;i<n;i++ )
#define SORT(A) sort(A.begin(),A.end())
#define REV(A) reverse(A.begin(),A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int,int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
	int n; cin >> n;
	vi a(n);
	rep(i,n){
		cin >> a[i];
		a[i] -= i+1;
	}
	SORT(a);
	int b = a[n/2];
	ll ans = 0;
	rep(i,n) ans += abs(a[i]-b);
	cout << ans << endl;
}


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

using namespace std;

int main(int argc, char *argv[]) {
  while (true) {
    int dice[3] = {1, 2, 3};
    int sum = 1, n;
    cin >> n;
    if (n == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "North") {
        int tmp = dice[0];
        dice[0] = dice[1];
        dice[1] = 7 - tmp;
      } else if (s == "East") {
        int tmp = dice[2];
        dice[2] = dice[0];
        dice[0] = 7 - tmp;
      } else if (s == "West") {
        int tmp = dice[0];
        dice[0] = dice[2];
        dice[2] = 7 - tmp;
      } else if (s == "South") {
        int tmp = dice[1];
        dice[1] = dice[0];
        dice[0] = 7 - tmp;
      } else if (s == "Right") {
        int tmp = dice[1];
        dice[1] = dice[2];
        dice[2] = 7 - tmp;
      } else {
        int tmp = dice[2];
        dice[2] = dice[1];
        dice[1] = 7 - tmp;
      }
      sum += dice[0];
    }

    cout << sum << endl;
  }
  return 0;
}

Measured memory_bytes: 1216
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;


typedef long long i64;
const int inf = (int)1.05e9;

int waflog(int a)
{
	int p = 0;
	int temp = 1;

	while(temp < a) {
		temp *= 2;
		p += 1;
	}

	return p;
}

int relkalog(int a)
{
	if(a <= 1) return 0;

	int p = 0;
	int temp = 1;

	while(temp <= a) {
		temp *= 2;
		p += 1;
	}

	return p - 1;
}

int main()
{
	int n;
	vector<int> as;

	scanf("%d", &n);
	as.resize(n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &as[i]);

	int count_left = 0;
	int count_right = 0;
	int cut_left = 0;
	int cut_right = 0;

	for(int i = 0; i < n; ++i) {

		const int a = as[i];
		const int p = waflog(a);
		const int r = relkalog(a);

		assert(p >= r);

		const int l_to_l = count_left + max(p - cut_left, 0);
		const int r_to_l = count_right + max(p - cut_right, 0);
		const int l_to_r = count_left + max(r - cut_left, 0) + (p - r);
		const int r_to_r = count_right + max(r - cut_right, 0) + (p - r);

		count_left = min(l_to_l, r_to_l);
		count_right = min(l_to_r, r_to_r);
		cut_left = r;
		cut_right = p;
		//printf("%d: %d %d %d %d\n", i, count_left, count_right, p, r);
	}

	int count = min(count_left, count_right);

	printf("%d\n", count);

	return 0;
}

/* waffle */


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

int main(int argc, char* argv[]) {
	int a, b;
	cin >> a >> b;
	
	if(a < b){
		cout << "a < b" << endl;
	}else if(a > b){
		cout << "a > b" << endl;
	}else{
		cout << "a == b" << endl;
	}
	
	return 0;
}

Measured memory_bytes: 1156
#include <algorithm>
#include <climits>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <vector>

constexpr int INF = std::numeric_limits<int>::max();
constexpr int NINF = std::numeric_limits<int>::min();
using namespace std;

vector<int> dists[100000 + 1];
int dp[100000 + 1];
int srcs[100000 + 1];

void solve() {
  int N, M;
  cin >> N >> M;

  while (M--) {
    int x, y;
    cin >> x >> y;
    dists[x].push_back(y);
    srcs[y]++;
  }

  queue<int> queue;
  for (int i = 1; i <= N; i++) {
    if (srcs[i] == 0) {
      queue.push(i);
    }
  }

  while (!queue.empty()) {
    int x = queue.front();
    queue.pop();

    for (int to : dists[x]) {
      dp[to] = max(dp[x] + 1, dp[to]);
      srcs[to]--;
      if (srcs[to] == 0) {
        queue.push(to);
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Measured memory_bytes: 6528