akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + (ll)b) % mod
#define MUL(a, b) a = (a * (ll)b) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define rer(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest) { 
	cout << arg << " "; Debug(rest...); }
template<class T>ostream& operator<< (ostream& out, const vector<T>& v) {
	out << "[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<< ", ";out<<v.back();}out << "]";return out;}
template<class S, class T>ostream& operator<< (ostream& out, const pair<S, T>& v) {
	out << "(" << v.first << ", " << v.second << ")";return out;}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N, M, K;
vector<pi> G[MAX_N];

ll L[MAX_N];

void dfs(int v, int p, ll l) {
	L[v] = l;
	rep(i, 0, (int)G[v].size()) {
		pi pp = G[v][i];
		if(pp.fst == p) continue;
		dfs(pp.fst, v, l + pp.sec);
	}
}

void solve() {
	cin >> N;
	rep(i, 0, N - 1) {	
		int a, b, c;
		cin >> a >> b >> c; a--; b--;
		G[a].pb(pi(b, c));
		G[b].pb(pi(a, c));
	}
	cin >> M >> K; K--;
	dfs(K, -1, 0);
	rep(i, 0, M) {
		int a, b; 
		cin >> a >> b; a--; b--;
		cout << L[a] + L[b] << "\n";
	}
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}



Measured memory_bytes: 18176
def main(N, A):
    b = A.pop(0)

    l = 0
    for a in A:
        if a > b:
            b = a
        else:
            l += b - a
    print(l)

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Measured memory_bytes: 32060
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, w = map(int, readline().split())
a = [readline().rstrip().decode() for i in range(h)]
memo_w = [False] * h
memo_h = [False] * w
for i in range(h):
    flag = True
    for j in range(w):
        if a[i][j] == '#':
            flag = False
            break
    if flag:
        memo_w[i] = True
for i in range(w):
    flag = True
    for j in range(h):
        if a[j][i] == '#':
            flag = False
            break
    if flag:
        memo_h[i] = True
for i in range(h):
    if not memo_w[i]:
        ans = ''
        for j in range(w):
            if not memo_h[j]:
                ans += a[i][j]
        print(ans)


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

using namespace std;

struct P{
  string a,b,c;
  P(){}
  P(string &a,string &b,string &c) : a(a),b(b),c(c) {}
};

vector<P> rules,ans;
map<string,bool> mp;

bool check(P p){
  for(int i = 0 ; i < (int)rules.size() ; i++){
    P rule = rules[i];
    bool flg = false;
    for(int j = 0 ; j < 8 ; j++){
      if((rule.b[j] != '?' && rule.b[j] != p.a[j]) ||
         (rule.c[j] != '?' && rule.c[j] != p.b[j])){
        flg = true;
        break;
      }
    }
    if(!flg) return mp[rule.a];
  }
  return false;
}

int main(){
  int N,M;
  mp["permit"] = true;
  mp["deny"] = false;
  while(cin >> N >> M, (N|M)){
    P in;
    rules.clear(); ans.clear();
    while(N--){
      cin >> in.a >> in.b >> in.c;
      rules.push_back(in);
    }
    reverse(rules.begin(),rules.end());
    while(M--){
      cin >> in.a >> in.b >> in.c;
      if(check(in)){
        ans.push_back(in);
      }
    }
    int s = ans.size();
    cout << s << endl;
    for(int i = 0 ; i < s ; i++){
      cout << ans[i].a << " " << ans[i].b << " " << ans[i].c << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1468
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;

int main() {
	string str;
	cin >> str;
	//vector<int> s;
	vector<int> region;
	int n = str.length();
	int maxchain = 0;
	int last = -1;
	int chain = 0;
	int k = n;
	for (int i = 0; i < str.length(); ++i) {
		int val = 0;
		if (str[i] == '0') val = 0;
		else val = 1;

		if (last == -1) {
			last = val;
			chain = 1;
		}
		else if (last == val) {
		}
		else {
			region.push_back(chain);
			last = val;
			chain = 1;
			k = min(k, max(i, n - i));
		}
	}
	cout << k << endl;
}


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

int main(){
  int n,k;
  long long int sum;
  cin>>n>>k;
  sum=k;
  for(int i=1;i<n;i++){
    sum=sum*(k-1);
  }
  cout<<sum<<endl;
  
  
}

Measured memory_bytes: 256
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#define   mod  998244353
#define   int  long long 
#define   ld   long double
#define   pb   push_back
#define   vi  vector<int> 
#define   dbg(x)  cerr << #x << " = " << x << '\n'
#define   sz(x)  (int)x.size()
#define   all(a) (a.begin(),a.end())
#define   ff   first
#define   ss   second
#define   pii  pair<int,int> 
#define   lcm(a,b) (a*b)/__gcd(a,b) 
using namespace std;

int x[2005][2005],y[2005][2005];

inline void solve(){
    int  r, c ; 
    cin >> r >> c ; 
    string s[r];
    for (int i=0;i<r;i++){
        cin >> s[i] ; 
        for (int j=0,cnt=0;j<c;j++){
            if (s[i][j]=='#') {
                x[i][j] = -1; 
                cnt = 0;
            }
            else {
                cnt++; 
                x[i][j] = cnt;
            }
        }
    }
    for (int i=0;i<c;i++){
        for (int j=0,cnt=0;j<r;j++){
            if (s[j][i] == '#') {
                y[j][i] = -1 ; 
                cnt = 0 ; 
            }
            else {
                cnt++; 
                y[j][i] = cnt ; 
            }
        }
    }
    for (int i=0;i<r;i++){
        for (int j=c-2;j>=0;j--){
            if (x[i][j] != -1){
                x[i][j] = max(x[i][j],x[i][j+1]);
            }
        }
    }
    for (int i=0;i<c;i++){
        for (int j=r-2;j>=0;j--){
            if (y[j][i] != -1){
                y[j][i] = max(y[j][i],y[j+1][i]);
            }
        }
    }
    int ans = 0;
    for (int i=0;i<r;i++)
        for (int j=0;j<c;j++)
            ans = max(ans,x[i][j]+y[i][j]-1);
    cout << ans << endl;
}

signed main()
{
    int n=1; // cin>>n;
    while (n--) solve();
    return 0;
}


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

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

  if (N == 1) {
    cout << "Hello World" << endl;
  }

  if (N == 2) {
    int A, B;
    cin >> A;
    cin >> B;
    cout << A + B << endl;
  }
}


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



int main() {
  
  ll N;
  cin >> N;
  
  vector<pair<pair<ll, ll>, ll>> height;
  // 観測データ作成
  
  for (ll i = 0; i < N; i++) {
    ll x, y, h;
    cin >> x >> y >> h;
    
    pair<ll,ll> p = {x, y};
    pair<pair<ll,ll>, ll> q = {p,h};
    
    height.push_back(q);
  }
  
  
  ll M = height.size();
  
  // 中心が (i,j)だったとして
  ll H;
  for (ll i = 0; i <= 100; i++) {
    for (ll j = 0; j <= 100; j++) {
      
      // 0より大きい高さを持っている
        for (ll k = 0; k < M; k++) {
          if (height[k].second > 0) {
            H = height[k].second + abs(height[k].first.first - i) 
              + abs(height[k].first.second - j);
            break;
          }
        
        }
      
      
      bool cond = true;
      // 条件の確認
      for (ll k = 0; k < M; k++) {
        pair<pair<ll,ll>, ll> p1 = height[k];
        
        ll x1 = p1.first.first, y1 = p1.first.second;
        
        if (p1.second != max(H-abs(i-x1)-abs(j-y1), (ll)0)) {
          cond = false;
          break;
        }
      }
      
      if (cond) {
        cout << i << " " << j << " " << H << endl;
        return 0;
      }
    
    }
  }

}

Measured memory_bytes: 3560
#-------------------------------------------------------------------------------
#関数定義
def f(a,d,T):
    try:
        return T[d+1:].index(a)+(d+1)
    except ValueError:
        return len(T)

def g(a,d,T):
    try:
        return (d-1)-T[d-1::-1].index(a)
    except ValueError:
        return 0
#-------------------------------------------------------------------------------
#インポート
from math import inf
from random import random,randint
import time
from copy import copy
#-------------------------------------------------------------------------------
#定数
TIME_LIMIT=1.862 #ループの時間制限
PROB=0.02 #突然変異の確率  
A=26 #アルファベットの数
START=time.time()
C_MIN,C_MAX=1,5
#-------------------------------------------------------------------------------
#読み込み部
D=int(input())

C=[0]+list(map(int,input().split())) #減る満足度のベース
S=[0]*(D+1) #S[d][i] :(d+1)日目にコンテストiを開催した時の満足度

for i in range(1,D+1):
    S[i]=[0]+list(map(int,input().split()))           
#-------------------------------------------------------------------------------
#貪欲部
L=[0]*(A+1)
T=[0]
for d in range(1,D+1):
    Y=-inf
    E=0
    for a in range(1,A+1):
        X=S[d][a]
        for s in range(1,A+1):
            if a!=s:
                X-=C[s]*(d-L[s])

        if X>Y:
            Y=X
            E=a
    L[E]=d
    T.append(E)
#-------------------------------------------------------------------------------
#調節部
while time.time()-START<=TIME_LIMIT:
    d=randint(1,D)
    p=T[d]
    q=randint(1,A)
    Y=(d-g(p,d,T))*(f(p,d,T)-d)
    Z=(d-g(q,d,T))*(f(q,d,T)-d)
        
    if (S[d][q]-S[d][p])+Z*C[q]-Y*C[p]>0 or random()<PROB:
        T[d]=q

for i in range(1,D+1):
    print(T[i])


Measured memory_bytes: 90676
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
 
int main(void){
    ll a, b, c, d;
    cin >> a >> b >> c >> d;

    ll m1, m2, m3, m4;
    m1 = a * c;
    m2 = a * d;
    m3 = b * c;
    m4 = b * d;

    ll ans;
    ans = max(m1, m2);
    ans = max(ans, m3);
    ans = max(ans, m4);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3632
from collections import defaultdict
N = int(input())
counters = defaultdict(int)
conutert = defaultdict(int)
for i in range(N):
    s = input()
    counters[s] += 1
M = int(input())
for i in range(M):
    t = input()
    conutert[t] += 1

ans = 0
for k, v in counters.items():
    if conutert.get(k):
        ans = max(v-conutert[k], ans)
    else:
        ans = max(v, ans)
print(ans)

Measured memory_bytes: 3316
/*Practice hard*/
#include <bits/stdc++.h>
using namespace std;


//_______MACROS_______//
#define   fast  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define   int   long long int
#define   pb    push_back
#define   f     first
#define   s     second

signed main()
{
    fast ;
    int n;
	cin >> n;
    int rem = n % 10 ;
    if (rem == 3)
    {
        cout << "bon" << endl;
    }
    else if (rem == 0 || rem == 1  || rem == 6 || rem == 8)
    {
        cout << "pon"<< endl;
    }
    else
    {
        cout << "hon" << endl;
    }
	return 0;
}


Measured memory_bytes: 3628
# -*- coding: utf-8 -*-
N = int(input())

S_dict = {}
max_num = 0
for i in range(N):
    S = str(input())
    now_num = S_dict.get(S, 0) + 1
    S_dict[S] = now_num
    max_num = max(max_num, now_num)

ans_list = [k for k, v in S_dict.items() if v == max_num]
ans_list.sort()

print("\n".join(ans_list))

Measured memory_bytes: 34116
#include <iostream>
#include <cmath>
#define rep(i,n) for (int (i)=0; (i)<int(n); (i)++)
using namespace std;

int main(){
    string s; cin >> s;
    int flag1 = 1, flag2 = 1; 
    for (int i = 0; i < s.size(); i++) {
        if (i % 2 == 0) { if (s[i] == 'L') flag1 = 0; }
        else {if (s[i] == 'R') flag2 = 0; }
    }
    if (flag1 * flag2) puts("Yes");
    else puts("No");
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
//Warshall_Floyd
//1-indexes
vector<vector<ll> >d(310,vector<ll>(310,inf));
void Warshall_Floyd(ll n){
    REP(k,1,n+1){
        REP(i,1,n+1){
            REP(j,1,n+1)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
        }
    }
}
int main(){
    ll n,m,R;cin>>n>>m>>R;
    vector<ll>r(R);
    rep(i,R){
        cin>>r[i];
    }
    sort(all(r));
    rep(i,m){
        ll a,b,c;cin>>a>>b>>c;
        d[a][b]=c;
        d[b][a]=c;
    }
    Warshall_Floyd(n);
    ll ans=inf*inf;
    do{
        ll cnt=0;
        rep(i,R-1){
            cnt+=d[r[i]][r[i+1]];
        }
        ans=min(ans,cnt);
    }while(next_permutation(all(r)));
    cout<<ans<<endl;
}

Measured memory_bytes: 1024
#include<iostream>
#include<vector>
#include<string>
#define ll long long unsigned
#define p 1000000007

using namespace std;

int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;

    vector<ll>V(26,0);

    int i;
    for(i=0;i!=n;i++)
        V[s[i]-'a']++;

    ll S = 1;
    for(i=0;i!=26;i++)
        S = (S * (V[i]+1))%p;

    cout<<S-1;

    return 0;
}


Measured memory_bytes: 512
#include<iostream>
using namespace std;
int main(){
	int S,h,m,s,x;
		cin>>S;
		x=S%3600;
		s=x%60;
		m=(x-s)/60;
		h=(S-x)/3600;
		cout<<h<<":"<<m<<":"<<s<<endl;
       return 0;
}

Measured memory_bytes: 1164
#include<bits/stdc++.h>
using namespace std;
int main()
{ int n;
 cin>>n;
 int a[n],b[n],c[n];
 for(int i=0;i<n;i++)
 { cin>>a[i]>>b[i]>>c[i];
 }
 int dp[n][3]={0};
 dp[0][0]=a[0];
 dp[0][1]=b[0];
 dp[0][2]=c[0];
 for(int i=1;i<n;i++)
 {  dp[i][0]=max(dp[i-1][1],dp[i-1][2])+a[i];
   dp[i][1]=max(dp[i-1][0],dp[i-1][2])+b[i];
   dp[i][2]=max(dp[i-1][1],dp[i-1][0])+c[i];
 }
 cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]))<<endl;
}
  


Measured memory_bytes: 5916
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
#define all(v) v.begin(),v.end()
using namespace std;
const int mo=998244353;
const int N=305;
char s[N];
int n,s0,s1;
int f[N][N][N];
int g[N][N][N];
void UPD(int &x,int y){
	(x+=y)>=mo?x-=mo:233;
}
void initF(){
	f[0][0][0]=1;
	For(i,0,n-1) For(v1,0,s0) For(v2,0,s1){
		if (!f[i][v1][v2]) continue;
		if (i+2<=n){
			if (s[i+1]=='0'||s[i+2]=='0') f[i+2][v1+1][v2]=1;
			if (s[i+1]=='1'||s[i+2]=='1') f[i+2][v1][v2+1]=1;
		}
		if (v1||v2) f[i+1][v1][v2]=1;
		if (s[i+1]=='0'&&v2) f[i+1][v1+1][v2-1]=1;
		if (s[i+1]=='1'&&v1) f[i+1][v1-1][v2+1]=1;
	}
	For(i,1,n) For(v1,0,s0) For(v2,0,s1)
		if (f[i][v1][v2]){
			if (v1&&s[i]=='0') f[i-1][v1-1][v2]=0;
			if (v2&&s[i]=='1') f[i-1][v1][v2-1]=0;
		}
}
void initG(){
	g[n+1][0][0]=1;
	Rep(i,n+1,1) For(v1,0,s0) For(v2,0,s1){
		if (!g[i][v1][v2]) continue;
		int v=g[i][v1][v2];
		if (s[i-1]!='0') UPD(g[i][v1+1][v2],v);
		if (s[i-1]!='1') UPD(g[i][v1][v2+1],v);
		UPD(g[i-1][v1][v2],v);
	}
	For(i,1,n+1){
		Rep(v1,s0,0) Rep(v2,s1,0){
			int v=g[i][v1][v2];
			if (s[i-1]!='0') UPD(g[i][v1+1][v2],mo-v);
			if (s[i-1]!='1') UPD(g[i][v1][v2+1],mo-v);
		}
		For(v1,0,s0) For(v2,0,s1){
			int v=g[i][v1][v2];
			UPD(g[i][v1+1][v2],v);
			UPD(g[i][v1][v2+1],v);
		}
	}
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	For(i,1,n) s[i]=='0'?++s0:++s1;
	initF();
	initG();
	int ans=0;
	For(i,0,n) For(v1,0,s0) For(v2,0,s1)
		if (f[i][v1][v2]) UPD(ans,g[i+1][v1][v2]);
	cout<<ans<<endl;
}

Measured memory_bytes: 140252
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    W,H = LI()
    s = []
    for _ in range(W):
        s.append((NI(),0))
    for _ in range(H):
        s.append((NI(),1))
    s.sort()
    x = 0
    y = 0
    ans = 0
    for r,d in s:
        if d == 0:
            ans += (H - y + 1) * r
            x += 1
        else:
            ans += (W - x + 1) * r
            y += 1
    print(ans)

if __name__ == '__main__':
    main()

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

int main() 
{
    long long min, max;
    long long sum = 0;
    int n;

    cin >> n;

    int num[n];

    for( int i = 0; i < n; i++ )            //n個の整数を配列に格納
    {
        cin >> num[i];
        sum += num[i];                      //合計を蓄積
    }

    min = num[0];                           //最初の整数で初期化
    max = num[0];                           //

    for( int i = 1; i < n; i++ )
    {
        if( min > num[i] )                  //minより小さいものがあればminの値を更新
            min = num[i];
        if( max < num[i] )                  //maxより大きいものがあればmaxの値を更新
            max = num[i];
    }

    cout << min << " " << max << " " << sum << endl;

    return 0;
}


Measured memory_bytes: 3132
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long ll;
int f[50];
int main()
{
    int n;
    cin >>n;
    for(int i=0;i<n;i++)
        cin >> f[i];
    int cnt=0;
    int minn=f[0];
    for(int i=0;i<n;i++){
        if(f[i]>=minn){
            cnt++;
            minn=f[i];
        }
    }
    cout << cnt;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
    char S[100];
    int check=1, i;

    scanf("%s", S);

    for(i=0; S[i] != '\0'; i++){
	if(i % 2 == 0){ //奇数文字目
	    if(S[i]!='R' && S[i]!='U' && S[i]!='D'){
		check = 0;
	    }
	}
	
	else{
	    if(S[i]!='L' && S[i]!='U' && S[i]!='D'){
		check = 0;
	    }
	}
    }

    if(check == 1){
	printf("Yes");
    }

    else{
	printf("No");
    }

    return 0;
}

Measured memory_bytes: 128
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SIZE 100
#define rep(i, N) for (i = 0; i < N; i++) //制御変数iを用いてN回転
#define array(N, t) (t*)calloc(N, sizeof(t)) //t型N要素の配列を動的確保後0クリア(freeを忘れずに)
#define zero(a); {int iter; rep(iter, sizeof(a)/sizeof((a)[0])) (a)[iter]=0;} //1次元配列aのゼロ初期化?
typedef long long ll;
void printa_int(int *a, int size); //int型の1次元配列aを出力(for debug)

int main() {
    int N, i;
    ll K;
    scanf("%d %lld", &N, &K);
    int *A = array(N, int);
    int *visit = array(N, int);
    int *way = array(N, int);

    rep(i, N) {
        scanf("%d", A + i);
        A[i]--;
    }

    int head = -1, r = -1, loop = 0;
    way[0] = 0;
    visit[0]++;
    for (i = 1; i < N; i++) {
        head = way[i] = A[way[i - 1]];
        if (++visit[head] > 1) break;
    }
    free(A);
    free(visit);
    rep(i, N) {
        if (way[i] == head) {
            if (r == -1) r = i;
            else {
                loop = i - r;
                break;
            }
        }
    }
    if (K < i) printf("%d\n", way[K] + 1);
    else printf("%d\n", way[r + (K - r) % loop] + 1);
    free(way);
    return 0;
}

void printa_int(int *a, int size) {
    int i;
    for (i = 0; i < size; i++)
        printf("%d, ", a[i]);
    printf("\n");
}

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

#define MAX 100

int main() {
  int n;
  int A[MAX];
  int count = 0;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - 1 - i; j++) {
      if (A[j] > A[j + 1]) {
        swap(A[j], A[j + 1]);
        count++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (i != 0) cout << " ";
    cout << A[i];
    if (i == n - 1) cout << endl;
  }
  cout << count << endl;
}


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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

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

//-------------------------------------------------


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

	int a[3];
	cin >> a[0] >> a[1] >> a[2];
	sort(a, a + 3);
	cout << a[2] - a[0] << endl;

	return 0;
}



Measured memory_bytes: 256
import std.array, std.stdio, std.conv, std.string, std.math, std.random, std.range,std.functional;
import std.algorithm.searching, std.algorithm.sorting, std.algorithm.iteration, std.algorithm.comparison;

int count;

void main()
{
    readln();
    int[] list = readln().split.to!(int[]);
    writeln(format("%(%s%| %)", mergeSort(list)));
    writeln(count);
}

int[] merge(int[] a, int[] b)
{
    int[] res;
    res.reserve(a.length+b.length);
    while(a.length != 0 && b.length != 0)
    {
        if(a.front < b.front)
        {
            res ~= a.front;
            a.popFront();
        }else{
            res ~= b.front;
            b.popFront();
        }
    }
    res ~= a ~ b;
    count+=res.length;
    return res;
}

int[] mergeSort(int[] list)
{
    if(list.length == 0)
        return list;
    if(list.length == 1)
        return list;
    auto mid = list.length / 2;
    auto a = mergeSort(list[0..mid]);
    auto b = mergeSort(list[mid..$]);
    return merge(a, b);
}

sizediff_t minIndex(alias pred="a<b", R) (R list)
    if(isForwardRange!R && !isInfinite!R && is(typeof(binaryFun!pred(list.front, list.front))))
{
    assert(!list.empty);
    typeof(list.front) min = list.front;
    sizediff_t minPos;
    sizediff_t curPos;
    for(list.popFront(); !list.empty; list.popFront())
    {
        curPos++;

        if(binaryFun!pred(list.front, min))
        {
            min = list.front;
            minPos = curPos;
        }
    }
    return minPos;
}



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

int main() {
    ll K;
    cin >> K;
    set<int> s;

    ll ans = 1;
    ll n = 0;
    while(true) {
        n = (10*n + 7) % K;
        if (n == 0) {
            break;
        }

        if (s.find(n) != s.end()) {
            cout << -1 << endl;
            return 0;
        }
        s.insert(n);
        ans++;
    }

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 50488
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;++i)
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int>P;
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; }
//max=({});
//条件式が真ならwhileの中身を回し続ける
//printf("%d\n", ans);

//pairの入力
//vector<pair<ll, ll>>work(n);
//rep(i, n) {
//	ll a, b;
//	cin >> a >> b;
//	work[i] = make_pair(a, b);
//for(auto p:mp)(mapの探索)
//printf("%.10f\n",なんちゃら)

int g[15][15];
const int INF = 1001001001;
const int dx[4] = { -1,0,1,0 };
const int dy[4] = { 0,-1,0,1 };

//最大公約数
ll gcd(ll x, ll y) {
	return y ? gcd(y, x % y) : x;
}

ll lcm(ll x, ll y) {
	if (x == 0 || y == 0)return 0;
	return (x / gcd(x, y) * y);
}

//素因数分解
vector<pair<ll, int>>factorize(ll n) {
	vector<pair<ll, int>>res;
	for (ll i = 2;i * i <= n;++i) {
		if (n % i)continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1)res.emplace_back(n, 1);
	return res;
}

int bingo[3][3];
int cnt[2][105];
int h, w;
bool flag[110][110];

int main() {
	int n, l;
	cin >> n >> l;
	vector<string>s(n);
	rep(i, n) {
		cin >> s[i];
	}
	sort(s.begin(), s.end());
	string ans;
	rep(i, n) {
		ans += s[i];
	}
	cout << ans << endl;
	return 0;
}


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

typedef long long ll;
const ll INF = 1e18;

#define p_ary(ary,a,b,i) do { cout << "["; for (int i = a;i < b;++i) cout << ary[i] << (b-1 == i ? "]\n" : ", "); } while(0)
#define p_map(map,it) do {cout << "{";for (auto it = map.begin();;++it) {if (it == map.end()) {cout << "}\n";break;}else cout << "" << it->first << "=>" << it->second << ", ";}}while(0)

int n;
vector<int> sz(5010,1);
vector<ll> a(5010);
vector<vector<int>> edges(5010);
vector<vector<ll>> dp0(5010,vector<ll>(5010,INF)),dp1(5010,vector<ll>(5010,INF));

void dfs(int u,int par = -1) {
    (a[u] > 0 ? dp0 : dp1)[u][0] = a[u];
    for (int& v : edges[u]) if (v != par) {
        dfs(v,u);
        vector<ll> temp0(5010,INF),temp1(5010,INF);
        for (int i = 0;i < sz[u];++i) {
            if (dp0[u][i] == INF) continue;
            for (int j = 0;j < sz[v];++j) {
                if (dp0[v][j] == INF) continue;
                temp0[i+j] = min(temp0[i+j],dp0[u][i]+dp0[v][j]);
                temp0[i+j+1] = min(temp0[i+j+1],dp0[u][i]);
            }
            for (int j = 0;j < sz[v];++j) {
                if (dp1[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp0[u][i]+dp1[v][j]);
                if (dp1[v][j] < 0) temp0[i+j+1] = min(temp0[i+j+1],dp0[u][i]);

            }
        }
        for (int i = 0;i < sz[u];++i) {
            if (dp1[u][i] == INF) continue;
            for (int j = 0;j < sz[v];++j) {
                if (dp0[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp1[u][i]+dp0[v][j]);
                temp1[i+j+1] = min(temp1[i+j+1],dp1[u][i]);
            }
            for (int j = 0;j < sz[v];++j) {
                if (dp1[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp1[u][i]+dp1[v][j]);
                if (dp1[v][j] < 0) temp1[i+j+1] = min(temp1[i+j+1],dp1[u][i]);
            }
        }
        dp0[u] = temp0;
        dp1[u] = temp1;
        sz[u] += sz[v];
    }
}

int main() {
    cin >> n;
    for (int i = 0;i < n;++i) cin >> a[i];
    for (int i = 0;i < n-1;++i) {
        int u,v;
        cin >> u >> v;
        u--;v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    dfs(0);
    int ans = n;
    for (int i = 0;i < n;++i) if (dp0[0][i] < INF) {
        ans = i;
        break;
    }
    for (int i = 0;i < n;++i) if (dp1[0][i] < INF && dp1[0][i] < 0) {
        ans = min(ans,i);
        break;
    }
    cout << ans << endl;
    // for (int i = 0;i < n;++i) p_ary(dp0[i],0,n,j);
    // cout << endl;
    // for (int i = 0;i < n;++i) p_ary(dp1[i],0,n,j);
    // cout << endl;
    // p_ary(sz,0,n,i);
    return 0;
}

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

int main() {
	string s[110], t[110];
	int n, m;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	s[n] = "114514";
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> t[i];
	}
	int count = 0, ans = -110;
	for (int i = 0; i < n+1; i++) {
		for (int j = 0; j < n; j++) {
			if (s[i] == s[j]) {
				count++;
			}

		}
		for (int j = 0; j < m; j++) {
			if (s[i] == t[j]) {
				count--;
			}
		}
		ans = max(count, ans);
		count = 0;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int h[100005];
int ans;
int main(void){
    int n,k; cin>>n>>k;
    for(int i=0;i<n;i++){
        cin>>h[i];
        if(h[i]>=k){
            ans++;
        }
    }
    cout<<ans<<endl;
    
}


Measured memory_bytes: 640
S = raw_input()
S = list(S)
S = set(S)
flagSN = False
flagEW = False
if ('N' in S and 'S' in S) or ('N' not in S and 'S' not in S):
  flagSN = True
if ('W' in S and 'E' in S) or ('W' not in S and 'E' not in S):
  flagEW = True
if flagSN and flagEW:
  print 'Yes'
else:
  print 'No'

Measured memory_bytes: 2568
#include <stdio.h>

int main(void){
  int flag[4] = {0,0,0,0};
  int n;

  for(int i = 0;i<4;i++){
    scanf("%d",&n);
    switch(n){
      case 1:
        flag[0] = 1;
        break;
      case 9:
        flag[1] = 1;
        break;
      case 7:
        flag[2] = 1;
        break;
      case 4:
        flag[3] = 1;
        break;
      default:
        printf("NO\n");
        return 0;
    }
  }

  for(int i = 0;i<4;i++){
    if(!flag[i]){
      printf("NO\n");
      return 0;
    }
  }
  printf("YES\n");
  return 0;
}

Measured memory_bytes: 128
class UnionFind:
    def __init__(self, num):
        self.parent = [i for i in range(num + 1)]

    def find(self, node):
        if self.parent[node] == node:
            return node

        self.parent[node] = self.find(self.parent[node])
        return self.parent[node]

    def union(self, node1, node2):
        node1 = self.find(node1)
        node2 = self.find(node2)

        if node1 == node2:
            return

        if self.parent[node1] > self.parent[node2]:
            node1, node2 = node2, node1

        self.parent[node2] = node1
        return

    def same(self, node1, node2):
        return self.find(node1) == self.find(node2)


n, m = map(int, input().split())
p = list(map(int, input().split()))

uf = UnionFind(n)
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    uf.union(p[x], p[y])

ans = 0
for i in range(n):
    if p[i] == i + 1 or uf.same(p[i], i + 1):
        ans += 1

print(ans)



Measured memory_bytes: 90368
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
  int s;
  cin >> s;
  int m=1;
  while(true){
    if(s==4||s==2||s==1) break;
    m++;
    if(s%2==0) s /= 2;
    else s = 3*s+1;
  }
  
  cout << m+3 << endl;
}


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

using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (int)n; i++)

int main(){
  int n;
  cin >> n;
  std::vector<int> vec;
  rep(i,n) {
    int x;
    cin >> x;
    if (x % 2 == 0) {
      vec.push_back(x);
    }
  }
  bool ok = true;
  for (int i : vec) {
    if (i % 3 != 0 && i % 5 != 0) {
      ok = false;
    }
  }
  if (ok) cout << "APPROVED" << endl;
  else cout << "DENIED" << endl;
}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define reps(i, a, n) for (int i = (n - 1); i > (a - 1); --i)
#define arep(i, x) for (auto &&i : (x))
template <class A, class B>
bool chmax(A &a, const B &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class A, class B>
bool chmin(A &a, const B &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using P = pair<int, int>;
#define all(x) (x).begin(), (x).end()
constexpr int inf = 0x3f3f3f3f;

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N, L;
  cin >> N >> L;
  vector<P> a;
  rep(i, 0, N) {
    int p, d;
    cin >> p >> d;
    a.emplace_back(p, d * 2 - 1);
  }
  sort(all(a));
  // arep(i, a) cout << i.first << ' ' << i.second << '\n';

  int ans = 0;
  int now = 0;

  rep(i, 0, N) now -=
      (a[i].first - (i + 1)) * a[i].second;  // cout << now << ' ';
  // cout << '\n';
  ans = now;
  reps(i, 0, N) {
    now += (L - N) * a[i].second;
    chmax(ans, now);
    // cout << now << ' ';
  }

  cout << ans << '\n';
  return 0;
}


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

int main() {
    int n;
    int k;
    
    cin >> n;
    int a[n];
    int b[n];

    for (int i = 0; i < n; i++) {
        cin >> a[i];
        b[a[i]-1]=i+1;
    }
    cout << b[0];
    for (int i = 1; i < n ; i++)
    {
        cout << " " << b[i];
    }
    cout << endl;
}

Measured memory_bytes: 1536
#include <iostream>
#define MAX 10000
#define NIL -1
using namespace std;

struct Node{
  int parent;
  int left;
  int right;
};

int setHeight(int);
void setDepth(int,int);
int getSibling(int);
void print(int);

struct Node T[MAX];
int n,A[MAX],H[MAX];

int main(){
  int v,l,r,i;
  int root = 0;
  cin >> n;
  for(i = 0; i < n; i++){
    T[i].parent = NIL;
  }
  for(i = 0; i < n; i++){
    cin >> v >> l >> r;
    T[v].left = l;
    T[v].right = r;
    if(l != NIL){
      T[l].parent = v;
    }
    if(r != NIL){
      T[r].parent = v;
    }
  }
  for(i = 0; i < n; i++){
    if(T[i].parent == NIL){
      root = i;
    }
  }
  setDepth(root,0);
  setHeight(root);
  for(i = 0; i < n; i++){
    print(i);
  }
  return 0;
}//end of main func


void setDepth(int u,int d){
  if( u == NIL){
    return;
  }
  A[u] = d;
  setDepth(T[u].left,d+1);
  setDepth(T[u].right,d+1);
}


int setHeight(int u){
  int h1 = 0;
  int h2 = 0;
  if(T[u].left != NIL){
    h1 = setHeight(T[u].left)+1;
  }
  if(T[u].right != NIL){
    h2 = setHeight(T[u].right)+1;
  }
  return H[u] = (h1 > h2 ? h1 : h2);
}


int getSibling(int u){
  if(T[u].parent == NIL){
    return NIL;
  }
  if(T[T[u].parent].left != u && T[T[u].parent].left != NIL){
    return T[T[u].parent].left;
  }
  if(T[T[u].parent].right != u && T[T[u].parent].right != NIL){
    return T[T[u].parent].right;
  }
  return NIL;
}


void print(int u){
  int degree = 0;
    cout << "node " << u <<": ";
    cout << "parent = " << T[u].parent << ", ";
    cout << "sibling = " << getSibling(u) << ", ";

    if(T[u].left != NIL){
      degree++;
    }
    if(T[u].right != NIL){
      degree++;
    }
    cout << "degree = " << degree << ", ";
    cout << "depth = " << A[u] << ", ";
    cout << "height = " << H[u] << ", ";
    if(T[u].parent == NIL){
      cout << "root" << endl;
    }
    else if(T[u].left == NIL && T[u].right == NIL){
      cout << "leaf" << endl;
    }
    else{
      cout << "internal node" << endl;
    }
}

Measured memory_bytes: 3108
import java.util.Scanner

object Main extends App {

  val sc = new Scanner(System.in)

  val s = sc.next()
  val k = sc.nextInt() - 1
  val n = s.length

  val set = (0 to n - 1)
    .flatMap(i => (0 to math.min(4, n - i - 1)).map(j => s.substring(i, i + j + 1)))
    .sorted
    .distinct

  println(set(k))
}


Measured memory_bytes: 33684
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool set = false;
int digit = 1;
string result;

bool makeChar(int Num, vector<int> D,  string NNum,  int index_arg)
{
    string compare_num;
    int index;
    for (int i : D)
    {
        index = index_arg;
        compare_num = NNum + to_string(i);
        if (index == digit && Num <= atoi(compare_num.c_str()))
        {
            char num = (char)i;
            set = true;
            result = compare_num;
        }
        else if (index < digit)
        {
            index += 1;
            makeChar(Num, D, compare_num, index);
        }
        if (set)
        {
            return true;
        }
    }
    return false;
}

void JudgeDigit(int num)
{
    int mod = 1;
    while (num / (mod * 10))
    {
        mod = mod * 10;
        digit++;
    }

}

int main()
{
    int N, K, inputD, count;
    cin >> N >> K;
    vector<int> D;
    inputD = -1;
    for (int i = -1 ; i < 10 ; i++)
    {
        if (i != inputD)
        {
            D.push_back(i);
        } 
        else if (count != K)
        {
            cin >> inputD;
            count++;
        }
    }
    JudgeDigit(N);
    while (!makeChar(N, D, "", 1))
    {
        digit++;
    }
    cout << atoi(result.c_str()) << endl;

    return 0;    
}

Measured memory_bytes: 888
#include "iostream"
using namespace std;
 
int main(){
  
  char n[5]={0};
    
  cin >> n[1] >> n[2] >>n[3] >>n[4];
  
  if(n[1]==n[2] || n[2]==n[3] || n[3]==n[4]){
    cout << "Bad";
  }else{
    cout << "Good";
  }
  return 0;
}

Measured memory_bytes: 256
z[4000],*a=z,*b=z+2000,m,i,j;main(n){for(;!scanf("%d",&n)||n;m=!printf("%d\n",m))for(i=++n;i--;j=a,a=b,b=j)for(j=n;j--;)m=fmax(m,b[j]=n+~i&&getchar()>45?1+fmin(fmin(b[j+1],a[j+1]),a[j]):0);}

Measured memory_bytes: 0
use std::io::Read;
fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();


    for c in buf.chars() {
        if c == '1' {
            print!("{}", '9')
        }
        if c == '9' {
            print!("{}", '1')
        }
    }
}


Measured memory_bytes: 4352
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    int n;
    string s;
    cin >> n >> s;
    size_t l = 0, r = 0;
    for (auto c:s) {
        if (c == '(') r++;
        else if (r > 0) r--;
        else l++;
    }
    cout << string(l, '(') + s + string(r, ')') << '\n';
    return 0;
}

Measured memory_bytes: 256
#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;
using ll = long long;
using P = pair<ll,int>;
using G = vector<vector<int>>;
const long long INF = 1LL << 60;


int main(){
  string s,ans;
  bool rev = false ;//反転ているかどうか，反転：１，反転なし：０
  ll q;
  cin >> s >> q;
  deque<char> Q;
  for(char c:s) Q.push_back(c);

  for(ll i = 1LL;i<=q;i++){
    int t=0,f=0;
    char c;
    //scanf("%d %d %s", &t,&f,&c);
    scanf("%d",&t);
    if(t == 1){ 
      rev = !rev;
    }else{
      cin >> f >> c;
      if(rev) f = 3-f;
      if(f == 1 ){
        Q.push_front(c);
      }else{
        Q.push_back(c);
      }
    }
  }
  
  
  for(char c:Q) ans+=c;
  if(rev) reverse(ans.begin(),ans.end());
  cout << ans << endl;
  
}


Measured memory_bytes: 1412
#include <bits/stdc++.h>
    using namespace std;
    typedef long long lint;
    typedef pair<int,int> pint;
    typedef vector<int> vint;
    #define rep(i, n) for(int i = 0; i < n; i++)
    #define rep2(elem, vec) for(auto elem: vec)
    #define SORT(V) sort((V).begin(),(V).end())
    #define RSORT(V) sort((V).rbegin(), (V).rend())
    lint gcd(int a,int b){return b?gcd(b,a%b):a;}
    lint lcm(int a,int b){return a*b/gcd(a,b);}
    lint comb(int a,int b){return b?comb(a-1,b-1)*a/b:1;}
    int dx[4] ={1,0,-1,0},dy[4] = {0,1,0,-1};
    int MOD = 1e9+7;

int main(){
    int N;
    cin >> N;
    vector<lint> A(N);
    rep(i,N)cin >> A[i];
    SORT(A);
    rep(i,N)
        if(A[i]==A[i+1]){
            cout << "NO";
            return 0;
        }
    cout << "YES";
    return 0;
}


Measured memory_bytes: 1792
/*
        ***Fresher***
    Md Mahfujur Rahman Khan
    Dept. of CSE
    CSE-28 Batch
    JU-48 Batch
    Jahangirnagar University

*/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<functional>
#include<set>
#include<map>
#include<climits>
#include<numeric>
#include<utility>
using namespace std;
#define V_ vector
#define Q_ queue
#define S_ set
#define ST_ stack
#define M_ map
#define DQ_ deque

#define pii pair<ll,ll>
#define pci pair<char,ll>
#define psi pair<string, ll>
#define pis pair<ll,string>
#define pic pair<ll, char>

#define mii map<ll,ll>
#define msi map<string, ll>
#define mis map<ll, string>
#define mib map<ll,bool>
#define msb map<string,bool>

#define vi vector<ll>
#define vs vector<string>

#define ivi vector<ll> :: iterator
#define ivs vector<string> :: iterator
#define imii map<ll,ll> :: iterator
#define imsi map<string,ll> :: iterator
#define imis map<ll, string> :: iterator
#define imib map<ll, bool> :: iterator
#define imsb map<string, bool> :: iterator

#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define F first
#define S second
#define pi 2*acos(0.0)
#define PB(x) push_back(x)
#define PP(x) push(x)
#define RSZ(n) resize(n)
#define POB pop_back()
#define P_ pop()
#define CL_ clear()
#define LEN_ length()
#define SZ_ size()
#define CNT_ count()
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define B_ begin()
#define E_ end()
#define F_ front()
#define t_ top()
#define IN_ insert
#define ER_  erase
#define GT getline
#define NL_   "\n"
#define SP_ " "

#define fr(i, n) for(i = 0; i < n; i++)
#define fe(x, n) for(i = x; i <= n; i++)

#define ul unsigned long long int
#define ll long long int
#define  MAX 250000

bool check(char a, char b)
{
    if(a=='0' && b!='0')
        return true;
    if(a=='1'&& (b=='0'||b=='1'||b=='2'))
        return true;
    return false;
}



int main()
{

    ll n, sum = 0, mx =0, t;

    string s;
    cin >> s;
    if(check(s[0],s[1])&&check(s[2],s[3]))
    {
        cout <<"AMBIGUOUS";
    }
    else if(check(s[0],s[1]))
{
    cout <<"MMYY";
}

else if(check(s[2],s[3]))
{
    cout <<"YYMM";
}

else
{
    cout <<"NA";
}


return 0;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void qmax(int &x,int y) {if (x<y) x=y;}
void qmin(ll &x,ll y) {if (x>y) x=y;}
inline ll read()
{
    char s;
    ll k=0,base=1;
    while((s=getchar())!='-'&&s!=EOF&&!(isdigit(s)));
    if(s==EOF)exit(0);
    if(s=='-')base=-1,s=getchar();
    while(isdigit(s)){k=k*10+(s^'0');s=getchar();}
    return k*base;
}
inline void write(int x)
{
    static char cnt,num[15];cnt=0;
    if (!x)
    {
        putchar('0');
        return;
    }
    for (;x;x/=10) num[++cnt]=x%10;
    for (;cnt;putchar(num[cnt--]+48));
}
const ll mod=1e9+7;
const int maxn=1e5+100;
char ch[maxn];
int n;
ll s[maxn][2];//0统计A的个数 ，1表示?的个数
ll f[maxn][2];//0统计C的个数，1表示?的个数 
ll sum[maxn];
ll mi[maxn];
ll s3,ans;
int main()
{
	scanf("%s",ch+1);
	n=strlen(ch+1);
	s3=1;
	sum[0]=0;
	mi[0]=1;
	for (int i=1;i<=n;i++) 
	{
		sum[i]=(s3*i)%mod;
		s3=s3*3%mod;
		mi[i]=mi[i-1]*3%mod;
	}
//	cerr<<sum[2]<<" ";
	for (int i=1;i<=n;i++)
	{
		s[i][1]=s[i-1][1]+(ch[i]=='?');
		s[i][0]=s[i-1][0]+(ch[i]=='A');
	}
	for (int i=n;i>=1;i--)
	{
		f[i][1]=f[i+1][1]+(ch[i]=='?');
		f[i][0]=f[i+1][0]+(ch[i]=='C');
	}
	for (int i=2;i<=n;i++)
	{
		if (ch[i]=='?'||ch[i]=='B')
		{
			ans+=(s[i][0]*mi[s[i-1][1]]%mod+sum[s[i-1][1]])%mod*
			((f[i+1][0]*mi[f[i+1][1]])%mod+sum[f[i+1][1]])%mod;
			ans%=mod;
		}
	}
	
	cout<<ans;
	return 0;
} 

Measured memory_bytes: 4992
h,w=map(int,input().split())
m=10**9+7
arr=[[1 for j in range(w)]for i in range(h)]
for i in range(h):
  string=input()
  for j in range(w):
    if string[j]=="#":
      arr[i][j]=0

dp=[[0 for j in range(w)]for i in range(h)]
dp[0][0]=1
for i in range(h):
  for j in range(w):
    if i>0 and arr[i-1][j]:
      dp[i][j]=(dp[i][j]+dp[i-1][j])%m
    if j>0 and arr[i][j-1]:
      dp[i][j]=(dp[i][j]+dp[i][j-1])%m
print(dp[h-1][w-1]%m)    


Measured memory_bytes: 91868
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws Exception {
		// TODO ?????????????????????????????????????????????
		BufferedReader br =
				new BufferedReader( new InputStreamReader(System.in) );

		int n = Integer.parseInt(br.readLine());

		Node[] u = new Node[n];
		for (int i=0; i < n; i++) {
			u[i] = new Node(i);
		}

		for (int i=0; i < n; i++) {
			String[] data = br.readLine().split(" ");

			int id = Integer.parseInt(data[0]);
			u[id].id = id;

			int k = Integer.parseInt(data[1]);
			u[id].degree = k;

			u[id].childs = new Node[k];
			for (int j=0; j < k; j++) {
				int cj = Integer.parseInt(data[j+2]);
				u[cj].ischild = true; //u[cj] is a child of u[id]
				u[cj].isroot = false; //u[cj] is not the root.
				u[id].childs[j] = u[cj];
			}
		}

//		Arrays.sort(u); // u[i].id = i

		Node root = null; // u[0] is a dammy
		for (int i=0; i < n; i++) {
			Node p = u[i]; // parent
			// for any childs of p, set its parent to p.
			for (int k=0; k < p.degree; k++) {
				p.childs[k].parent = p;
			}

			// if p is the root then depth of p is 0
			if (p.isroot) {
				root = p;
				root.depth = 0;
			}
		}

//		if (root != null)
			setdepth(root);
//		else System.out.println("error");

		for (int i=0; i < n; i++) {
			System.out.println(u[i]);
		}

	}

	static class Node /* implements  Comparable<Node> */ {
		int id;

		int degree;

		Node parent;

		int depth;

		Node[] childs;

		boolean isroot = true;

		boolean ischild;

		public Node(int id) {
			this.id = id;
		}

//		public int compareTo(Node x) {
//			return this.id - x.id;
//		}


		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();

			int n = this.id;

			int p;
			if (parent == null) {
				p = -1;
			} else {
				p = this.parent.id;
			}

			int d = this.depth;

			String type;
			if (parent == null) {
				type = "root";
			} else if (degree != 0) {
				type = "internal node";
			} else {
				type = "leaf";
			}

			sb.append("node " + n + ": parent = " + p +
					", depth = " + d +", " + type + ", [");

			if (degree == 0) return sb.append("]").toString();

			for (int i=0; i < degree-1; i++) {
				sb.append( this.childs[i].id + ", " );
			}
			sb.append(this.childs[degree-1].id);
			sb.append("]");

			return sb.toString();
		}

		void bechild() {
			ischild = true;
			isroot = false;
		}

		void beroot() {
			isroot = true;
			ischild = false;
		}
	}

	static void setdepth(Node x) {
		int c = x.childs.length;
		if (c == 0) return;
		for (int i=0; i < c; i++) {
			x.childs[i].depth = x.depth+1;
		}
		for (int i=0; i < c; i++) {
			setdepth( x.childs[i] );
		}
	}
}

Measured memory_bytes: 121856
n = int(input())

def dfs(s):
    if int(s) > n:
        return 0
    if all(s.count(c) for c in "753"):
        cnt =1
    else:
        cnt =0
    for t in "753":
        cnt +=dfs(s+t) 
    return cnt
    
print(dfs("0"))

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

int main(){
	int n ,m;
	cin >> n >> m;
	int s=0,l=n;
	for (int i = 0; i < m; i++)
	{
		int gs,ge;
		cin >> gs >> ge;
		if((s>ge-1)||(l<gs-1)){
        	l = -1;
          	s = 0;
          	break;
        }
      	s = (s<gs-1) ? gs-1 : s;
        l = (l>ge-1) ? ge-1 : l;
         
	}

  	cout << l-s+1;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF  numeric_limits<int_fast64_t>::max();
#define intf int_fast64_t
int main(){
    intf i,n=1,total=0;
    cin >>i;

    while(1){
        total +=n;
        if(i<total){
            for(int h=1;h<n+1;h++){
                if(total-i==h){
                    continue;
                }
                cout << h<<endl;
            }
            return 0;
        }
        n+=1;
    }
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-
if __name__ == '__main__':
    clist = list(input())
    chars = ['N', 'W', 'S', 'E']
    a = []
    for c in chars:
        a.append(clist.count(c))
    for i in [0, 1]:
        if (a[i] > 0):
            if (a[i + 2] == 0):
                print('No')
                exit()
        if (a[i + 2] > 0):
            if (a[i] == 0):
                print('No')
                exit()
    print('Yes')


Measured memory_bytes: 3064