akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1) {
    for (int u : to[v]) {
        if (u == p)continue;
        ans[u] += ans[v];
        dfs(u, v);
    }
}

int main() {
    int n, q;
    cin >> n >> q;
    int a, b;

    for (int i = 0; i < n - 1; i++) {
        cin >> a >> b;
        --a;
        --b;
        to[a].push_back(b);
        to[b].push_back(a);
    }



    ans.resize(n);

    int p, x;

    for (int i = 0; i < q; i++) {
        cin >> p >> x;
        --p;
        ans[p] += x;
    }
    dfs(0);

    for (int i = 0; i < n; i++) {
        cout << ans[i] << endl;
    }


    return 0;
}

Measured memory_bytes: 23552
#include <bits/stdc++.h>
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll LINF = 1000000000000000000; //1e18
const double EPS = 1e-10;

int main(void)
{
    int a, b, h;

    cin >> a >> b >> h;
    int result = (a + b) * h / 2;
    cout << result << endl;
    return (0);
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
	int n, k, h;
	int i, ans = 0;
	cin >> n >> k;
	for (i = 0; i < n; i++) {
		cin >> h;
		if (h >= k) {
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
from itertools import permutations

N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]

cnt = [[0] * C for _ in range(3)]
for h in range(N):
    for w, c in enumerate(map(int, input().split())):
        cnt[(h + w) % 3][c - 1] += 1

ans = 10**18
for perm in permutations(range(C), r=3):
    cost = 0
    for c, ns in zip(perm, cnt):
        for fr in range(C):
            if fr == c:
                continue
            cost += D[fr][c] * ns[fr]
    ans = min(ans, cost)
print(ans)

Measured memory_bytes: 3064
get_number(Number) :-
    get_string(String),
    number_string(Number, String).

get_number(A, B) :-
    get_number(A),
    get_number(B).

get_number(A, B, C) :-
    get_number(A, B),
    get_number(C).

get_string(String) :-
    current_input(Input),
    read_string(Input, ' \n', '', _, String).

get_chars(Xs) :-
    get_string(S),
    string_chars(S, Xs).

get_numbers(0, []).
get_numbers(N, [X|Xs]) :- 
    get_number(X), 
    N2 is N - 1, 
    get_numbers(N2, Xs).

put_numbers([]).
put_numbers([X|Xs]) :-
    writeln(X),
    put_numbers(Xs).

/* memo
    code(int)
    string_code(1, S, C), (C: code)
    char_code(X, Y) (X: char, Y: code)
    string_chars(S, S2) (S: string, S2: [char])
    to_lower(C, C2) (C: char? C2: code?)
    length(S, L),
    sub_string

    f(0, [], []).
    f(Z, [X|Xs], [Y|Ys]) :- X = Y, f(Z1, Xs, Ys), Z is Z1 + 1.
    f(Z, [_|Xs], [_|Ys]) :- f(Z, Xs, Ys).

    match: =, \=

    numeric: <,=<, >, >= (eval)
    lexicographically: @<. @=<, @>, @>=
    value?: =:=, =\= (eval)
    identical: ==, \==

    between(L,U,X): L <= X <= U
    nth0(I, Xs, Y), nth1(I, Xs, Y): (Xs,Y) -> I, (I, Xs) -> Y 

    sort(Key, Order, List, Sorted): Keyは通常0?, Orderは=なしが重複削除，=は重複保持
    \+ P(X) : not P(X)
*/

f(0, _, []).
f(N, X, [X|Xs]) :- X2 is X * 101 div 100, N2 is N - 1, f(N2, X2, Xs).

main :-
    f(3761, 100, Xs),
    get_number(X),
    between(0, 3762, I),
    nth0(I, Xs, Y),
    Y >= X,
    writeln(I).


Measured memory_bytes: 8252
use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let s: Vec<char> = read::<String>().chars().collect();

    if n % 2 == 1 {
        println!("No");
        return;
    }

    let mut ok = true;
    for i in 0..n/2 {
        if s[i] != s[i+n/2] {
            ok = false;
            break;
        }
    }
    println!("{}", if ok {"Yes"} else {"No"});
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


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

int main () {
  int N;
  string s, t;
  cin >> N >> s >> t;
  int ans = N*2;
  for (int i = 0; i < N; i++) {
    if (t.at(0) != s.at(i)) continue;
    else {
      bool x = false;
      for (int j = i; j < N; j++) {
        if (t.at(j-i) != s.at(j)) {
          x = true;
          break;
        }
      }
      if (x) continue;
      cout << ans - (N-i) << endl;
      return 0;
    }
  }
  cout << ans << endl;
}

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

using namespace std;

#define int long long

signed main() {
	int cnt[10000] = {};
	string w;
	cin >> w;
	for (int i = 0; i < w.length(); i++) {
		cnt[w[i]]++;
	}

	for (int i = 0; i < 10000; i++) {
		if (cnt[i] % 2 != 0) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}



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

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;


const ll mod=1e9+7;
const ll inf=1e10; 

#define rep(i,m,n) for(ll i=m;i<n;i++)
#define repr(i,m,n) for(ll i=m;i>=n;i--)


int main(){
   cin.tie(nullptr);
  ios::sync_with_stdio(false);
  
  ll n;
  cin >> n;
 
  vll x(n),l(n);
   vpll p(n);
  
  rep(i,0,n) {
    cin >> x[i] >> l[i];
    p[i].first=x[i]+l[i];
     p[i].second=x[i]-l[i];
  }
  
  sort(p.begin(),p.end());
  
  ll res=0;
  ll now=-inf;
  rep(i,0,n){
    if(now<=p[i].second) res++,now=p[i].first;
  }
  cout << res << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) cin >> a[i];
  int max = 0;
  for (int i = 0; i < N; i++) max += (a[i] - 1);
  cout << max << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;}
using ll = long long;

ll n,m,k,a,b,ans;
string s,t;
ll h[100005]; 
bool ok[100005];

int main() {
  cin >> n >> m;
  rep(i,n) cin >> h[i];
  rep(i,n) ok[i]=true;
  rep(i,m){
    cin >> a >> b; a--,b--;
    if(h[a]>h[b]) ok[b]=false;
    else if(h[b]>h[a]) ok[a]=false;
    else ok[a]=false,ok[b]=false;
  }
  rep(i,n)if(ok[i]) ans++;
  cout << ans << endl;
}

Measured memory_bytes: 4516
#include <stdio.h>

int main()
{
    int a,b,temp,temp1,i,a1,b1,x,y,j;
    while(scanf("%d %d",&a,&b) ==2){
        if(a>b){
            temp=a; a=b; b=temp;
        }
        a1=a;
        b1=b;
        for(;;){
            if(b%a==0)break;
            else if(b%a !=0){
                temp1=a;
                a=b%a;
                b = temp1;
            }
        }
        y=b1;
        x=a1;
        for(i=2,j=2; ; ){
            if(y>x){
            x = a1*i;
            i++;
            }
            if(x==y)break;
            else if(x>y){
                y=b1*j;
                j++;
                if(x==y)break;
            }
        }
        printf("%d %d\n",a,x);
    }
}

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

#define ll long long

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

    int x = 0;
    int count = 1;
    for(int i = 0;i < N;i++) {
        int L;
        cin >> L;
        if(x + L > X) {
            break;
        }
        x += L;
        count++;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,x;
  int a[101];

  cin >> n >> x;

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

  sort(a, a + n);

  int j;
  for(j = 0;j < n && x >= 0;j++){
	x -= a[j];
  }

  if(x == 0){
	cout << j << endl;
  }else{
	cout << j - 1 << endl;
  }
  
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	if(a>=1&&a<=13&&b>=1&&b<=13)
	{
	if(a==1)
	a=14;
	if(b==1)
	b=14;
	if(a>b)
	printf("Alice");
	else if(a<b)
	printf("Bob");
	else
	printf("Draw");
}
}

Measured memory_bytes: 256
#include <cstring>
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
const int INF=0x3fffffff;
using namespace std;

int main()
{
	int A, B, C, x, y;
	cin >> A >> B >> C >> x >> y;

	if (C * 2 >= A + B)
	{
		printf("%d", x * A + y * B);
		return 0;
	}

	int cCount, aCount;
	aCount = cCount = 0;
	if (x < y)	//保持x需求大
	{
		swap(x, y);
		swap(A, B);
	}
	
	cCount = y*2;
	x -= y;
	if (C * 2 > A) {
		aCount = x;
		printf("%d\n", cCount * C + aCount * A);
	}
	else
	{
		cCount += 2 * x;
		printf("%d\n", cCount * C);
	}
	return 0;
}

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

int main()
{
	int n, ans = 0;
	cin >> n;
	int c[3][n], dp[3][n];
	for (int i = 0; i < n; ++i)
		cin >> c[0][i] >> c[1][i] >> c[2][i];

	dp[0][0] = c[0][0];
	dp[1][0] = c[1][0];
	dp[2][0] = c[2][0];

	for (int i = 1; i < n; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			if(dp[(j+1)%3][i-1] < dp[(j+2)%3][i-1])
				dp[j][i] = dp[(j+2)%3][i-1] + c[j][i];
			else
				dp[j][i] = dp[(j+1)%3][i-1] + c[j][i];

		}
	}

	ans = dp[0][n-1];
	if(dp[1][n-1] < dp[2][n-1])
	{
		if(ans < dp[2][n-1])
			ans = dp[2][n-1];
	}
	else
	{
		if(ans < dp[1][n-1])
			ans = dp[1][n-1];
	}
	cout << ans;
}

Measured memory_bytes: 2560
from collections import deque

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

directcount = [0] * n # node_iが直接接続しているノードの数
visited = [-1]  * n # nodeの色,
colorcount = [-1] * n # 各色が何個のノードを持つか
# グラフの初期化
g = []
for _ in range(n):
    g.append(deque(list()))

# グラフの生成
for i in range(m):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1 # 1origin -> 0 origin
    g[a].append(b)
    g[b].append(a)
    # 直接接続されているノードの数
    directcount[a] += 1
    directcount[b] += 1

# dfs 連結したグラフごとにそのグラフの最初に探索したnodeの色に染める
q = deque(list()) #探索キュー
for i in range(n):
    q.append(i)
    color = i
    #cnt = []
    cnt = 1
    while len(q) > 0:
        nextnode = q.popleft() # 次の探索
        if visited[nextnode] != -1:
            continue
        visited[nextnode] = color
        q.extend(g[nextnode]) # nextnodeの辺を足す
        #cnt.append(color)
        cnt += 1
    # 探索が終わったらその色のノードの数を記録
    colorcount[color]  = cnt -1

print(max(colorcount))

Measured memory_bytes: 154060
#include<iostream>
using namespace std;
long N,K;
const long mod=1e9+7;
main()
{
	cin>>N>>K;
	long ans=0;
	for(long k=K;k<=N+1;k++)
	{
		long L=k*(k-1)/2,R=N*k-k*(k-1)/2;
		ans+=(R-L+1)%mod;
	}
	cout<<ans%mod<<endl;
}


Measured memory_bytes: 3672
#include <iostream>
#include <vector>
#include <array>
#include <cmath>
#include <string>
#include <cstring>
#include <iomanip>
#include <random>
#include <ctime>
#include <algorithm>
#include <functional>
#include <set>
#include <queue>
#include <map>
#include <numeric>
#include <climits>
#include <utility>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << setprecision(10) << fixed;
	/////////////////////////////////////////////////////////////
	vector<int> v(3);
	cin >> v[0] >> v[1] >> v[2];
	sort(v.begin(), v.end());
	cout << (v[1] - v[0]) + (v[2] - v[1]) << "\n";
	/////////////////////////////////////////////////////////////
	cout << "\n";
	return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using ll = long long;
void add_self(double &a, double b){
	a += b;
}
int main(){
int n;
scanf("%d",&n);
vector<double> dp(n + 1);
dp[0] = 1;
for(int i = 0; i < n ; ++i){
	double p_h;
	scanf("%lf",&p_h);
	for(int j = i + 1; j >= 0; --j){
		dp[j] = (j == 0 ? 0 : dp[j - 1] * p_h) + dp[j] * ( 1 - p_h);
	}
}
double answer = 0;
for(int i = 0; i <=n; ++i){
	if(i > n - i){
		add_self(answer,dp[i]);
	}	
}
printf("%0.10lf\n",answer);
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
	public static void main(String[] args){
    
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int n = sc.nextInt();
		// スペース区切りの整数の入力
		int x = sc.nextInt();
		int t = sc.nextInt();
		
    
    /*--------------------------------*/
     /*
    //入力
    int n = 20;	//個数
    int x = 12;	//一度に作れるかず
    int t = 6; //一度にかかる時間
    */
		int l = 0;	//現在の個数
    //数列の定義
    int ans = 0;
		while(l<n){
			l += x;	//現在の個数
			ans += t;
		}
		System.out.println(ans);
	}
}



Measured memory_bytes: 35632
#include<iostream>
using namespace std;
int main()
{
	int a[3][3];
	bool bingo[3][3];
	int n;
	int b;
	int i, j, k;

	for (i = 0; i < 3; i++) {
		for (j = 0; j < 3; j++) {
			cin >> a[i][j];
			bingo[i][j] = false;
		}
	}

	cin >> n;
	for (i = 0; i < n; i++) {
		cin >> b;
		for (j = 0; j < 3; j++) {
			for (k = 0; k < 3; k++) {
				if (a[j][k] == b) {
					bingo[j][k] = true;
				}
			}
		}
	}

	bool ans = false;
	if (bingo[0][0] && bingo[0][1] && bingo[0][2]) {
		ans = true;
	}
	if (bingo[0][0] && bingo[1][0] && bingo[2][0]) {
		ans = true;
	}
	if (bingo[0][0] && bingo[1][1] && bingo[2][2]) {
		ans = true;
	}
	if (bingo[0][1] && bingo[1][1] && bingo[2][1]) {
		ans = true;
	}
	if (bingo[0][2] && bingo[1][2] && bingo[2][2]) {
		ans = true;
	}
	if (bingo[0][2] && bingo[1][1] && bingo[2][0]) {
		ans = true;
	}
	if (bingo[1][0] && bingo[1][1] && bingo[1][2]) {
		ans = true;
	}
	if (bingo[2][0] && bingo[2][1] && bingo[2][2]) {
		ans = true;
	}

	if (ans) {
		cout << "Yes" << endl;
	}else {
		cout << "No" << endl;
	}

	return 0;
}


Measured memory_bytes: 256
//----AUTHOR:kkdrummer----/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include<ext/rope>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<int> C;
#define X real()
#define Y imag()
//typedef unordered_set<ll> usll;
//typedef unordered_multiset<ll> umsll;
typedef multiset<ll> msll;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pair<ll,ll> > vpll;
typedef priority_queue<ll> pqll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
//typedef unordered_multiset<int> umsi;
//typedef unordered_set<int> usi;
typedef pair<int,int> pi;
typedef vector<pair<int,int> > vpi;
typedef vector<pair<double,double> > vpd;
typedef pair<double,double> pd;
typedef priority_queue<int> pqi;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_set;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_setll;
#define in insert
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define en end
#define itr iterator
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mo 1000000007
#define inf 1000000000000000001
#define ninf -1000000000000000001
#define ima INT_MAX
#define imi INT_MIN
#define oncnt __builtin_popcount
#define zerobegin __builtin_clz
#define zeroend __builtin_ctz
#define parity __builtin_parity
const ld pii=3.14159265359;
inline ll modpow(ll x,ll n){if(n==0)return 1;if(n==1)return(x%mo);ll u=(modpow(x,n/2));u=(u*u)%mo;if(n%2!=0)u=(u*x%mo)%mo;return u;}
inline ll modinv(ll x){return modpow(x,mo-2);}
inline ll mmul(ll a,ll b){    if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll madd(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a+b>=mo)return(a+b)%mo;return(a+b);}
inline ll msub(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;return((a-b+mo)%mo);}
inline ll mdiv(ll a,ll bb){if(a>=mo)a=a%mo;ll b=modinv(bb);if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll gcd(ll a,ll b){return __gcd(a,b);}


int main()
{
io
int testcases=1;//cin>>testcases;
while(testcases--)
{
int n,k;
cin>>n>>k;
int a[k];
for(int i=0;i<k;i++)
cin>>a[i];
int p=a[0];
for(int i=1;i<k;i++)
a[i-1]=a[i]-a[i-1];

a[k-1]=n-a[k-1]+p;
sort(a,a+k);
int an=0;
for(int i=0;i<k-1;i++)
an+=a[i];
cout<<an;

}return 0;}







Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
  
int main(void){
    string pd;
    
    cin >> pd;
    
    for(int i=0; i<pd.size() ;i++){
        if(pd[i] == '?') {
            pd[i] = 'D';
        }
    }
    cout << pd << endl;
    return 0;
}

Measured memory_bytes: 3752
from sys import exit
s = input()
t = input()
tset = set(t)
slen = len(s)
aord = ord('a')
if len(tset - set(s)) != 0:
    print(-1)
    exit()
s2t = [i - aord for i in (s + s).encode('ascii')]
lut = [[-1 for _ in range(slen)] for _ in range(26)]
for c in tset:
    b = ord(c) - aord
    j = 0
    for i in range(slen * 2 - 1, -1, -1):
        if s2t[i] == b:
            j = 0
        else:
            j += 1
        if i < slen:
            lut[b][i] = j
p = 0
for c in t:
    b = ord(c) - aord
    p += lut[b][p % slen] + 1
print(p)


Measured memory_bytes: 104684
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k;
int main()
{
    scanf("%lld%lld",&n,&k);
    if(2*k<=n+1)
    {
        k--;
        for(int i=1;i<=n/2+n%2;i++)
            printf("%lld %lld %lld\n",k+n-(i-1)*2,k+n+n/2+i,k+3*n-i+1);
        for(int i=1;i<=n/2;i++)
            printf("%lld %lld %lld\n",k+n-1-(i-1)*2,k+n+i,k+2*n+n/2-i+1);
        return 0;
    }
    else printf("-1\n");
}


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

using namespace std;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RFOR(i,a,b) for(int i=(int)b-1;i>=(int)a;--i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define debug(x) cerr << #x << ":" << x << endl;
#define OK(ok) cout << (ok ? "Yes" : "No") << endl;
typedef long long ll;

void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail) {
  cin >> head; CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

const int INF = 1e9 + 1;
const int MOD = 1e9 + 7;
const int MAX_N = 1e5 + 1;

int N;

int memo[MAX_N];

int solve() {
  if (memo[N] > 0) {
    return memo[N];
  }

  
}

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

  cin >> N;
  vector< int > list;
  list.emplace_back(1);
  int s = 6;
  int n = 9;
  FOR(i, 1, MAX_N) {
    if (s < MAX_N) {
      list.emplace_back(s);
      s *= 6;
    }
    if (n < MAX_N) {
      list.emplace_back(n);
      n *= 9;
    }
  }


  sort(ALL(list));
  // debug(list.size());


  int ans = INF;
  FOR(bit, 1, (1 << list.size())) {
    int sm = 0;
    int cnt = 0;
    REP(i, list.size()) {
      if ((bit >> i) & 1) {
	sm += list[i];
	cnt++;
      }
    }
    if (sm > MAX_N) continue;
    memo[sm] = cnt;
  }

  FOR(i, 1, MAX_N) {
    if (memo[i] > 0) continue;
    int tmp = INF;
    EACH(li, list) {
      if (i < li) break;
      if (memo[i - li] > 0) {
	tmp = min(tmp, memo[i - li] + 1);
      }
    }
    int cnt = 0;
    memo[i] = min(tmp, memo[i - 1] + 1);
  }
  

  ans = memo[N];
  cout << ans << endl;
  
  /*
  int ans = 0;
  int current = list.size() - 1;
  while(N > 0) {
    if (list[current] <= N) {
      ans++;
      N -= list[current];
    } else {
      current--;
    }
    cout << N << endl;
  }
  cout << ans << endl;
*/

  return 0;
}



Measured memory_bytes: 640
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
N = int(input())

S = str(N)
sum = 0
for i in range(len(S)) :
  sum += int(S[i])
  
if N % sum == 0 :
  print ("Yes")
else :
  print ("No")


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

using namespace std;

typedef long long ll;

const ll ts = 100005;

#define IOS  ios::sync_with_stdio(0); cin.tie(0);
#define endl "\n"
pair <ll, ll > e[2 * ts + 5];
ll A[2 * ts + 5];
signed main() {

    IOS;
    ll n , d,  a ; 
    cin >> n  >> d >> a ;
    for(ll i = 1; i <= n ; i++) {
        cin >> e[i].first >> e[i].second;
    }
    sort(e + 1 , e + n + 1);
    ll ans= 0 ; 
    ll tmp = 0 ; 
    d  = d * 2; 
    for(ll i = 1; i <= n ; i++) {
    	e[i].second -= tmp;
    	if(e[i].second > 0) {
    		ll need = (e[i].second + a - 1) / a; 
    		tmp += need * a; 
    		ans += need; 
    		ll l = i;
    		ll r = n + 1;
    		while(l <r - 1) {
    			ll mid =  ( l + r) / 2;
    			if(e[i].first + d >= e[mid].first) {
    				l = mid; 
				}
				else r = mid ; 
			}
			A[l] += need * a;
		}
		tmp -= A[i]; 
	}
	cout << ans; 
 }


Measured memory_bytes: 4992
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

int main (){

  int N;
  scanf("%d",&N);
  std::vector<std::string > s(N);
  std::vector<int > t(N);
  for (int i=0; i<N; i++){
    std::cin >> s[i];
    scanf("%d", &t[i]);
  }
  std::string X;
  std::cin >> X;
  int counter = 0;
  while(s[counter]!=X){
    counter++;
  }
  int ans = 0;
  for (int i=counter+1; i<N; i++){
    ans+=t[i];
  }
  printf("%d\n",ans);
  return 0;
}


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

int main(){
  ll a ;
  cin >> a ;
  ll A[a] ;
  rep(i,a) cin >> A[i] ;
  rep(i,a){
    if(A[i] == 0 ){
	cout << 0 << endl;
      return 0 ;
    }
  }
  ll ans = 1 ;
  rep(i,a){
   if( A[i] > (ll) 1e18/ans ){
      cout << -1 << endl;
      return 0 ;
   }
    ans *= A[i] ;
  }
  cout << ans << endl ;
}


Measured memory_bytes: 4408
#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <assert.h>
#include <complex>
using namespace std;
#define LONG_INF 10000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353LL
#define seg_size 65536*4
#define REP(i,n) for(long long i = 0;i < n;++i)
int main() {
#define int long long
	long long n;
	cin >> n;
	vector<long long> division;
	for (long long i = 2; i <= sqrt(n); ++i) {
		long long cnt = 0;
		while (n % i == 0) {
			cnt++;
			n /= i;
		}
		if (cnt != 0) {
			division.push_back(cnt);
		}
	}
	if (n != 1) {
		division.push_back(1);
	}
	int minnest = division.size();
	int ans = 1;
	REP(i, division.size()) {
		ans *= (division[i]+1LL);
	}
	ans--;
	cout << minnest << " " << ans << endl;
	return 0;
}


Measured memory_bytes: 3152
import java.util.*;

public class Main {
  public static void main(String[] args ) throws Exception {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    
    int preT = 0;
    int preX = 0;
    int preY = 0;

    for(int i = 0; i < N; i++) {
        int postT = sc.nextInt();
        int postX = sc.nextInt();
        int postY = sc.nextInt();
        
        int dt = postT - preT;
        int dist = Math.abs(postX - preX) + Math.abs(postY - preY);
        if ((dt < dist) || ((dist - dt) % 2 != 0)) {
            System.out.println("No");
            return;
        }

        preT = postT;
        preX = postX;
        preY = postY;
    }
    
    System.out.println("Yes");
  }
}


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

int main()
{
	unsigned long long n = 1, k = 1;

	cin >> n;

	int p = 2;

	while (p <= n) {
		int i = 0;
		while (n % p == 0) {
			n /= p;
			if (i == 0)
				k *= p - 1;
			else
				k *= p;
			i++;
		}
		if (p == 2)
			p++;
		else
			p += 2;
	}

	

	cout << k << endl;

	return 0;
}

Measured memory_bytes: 3104
n=int(input())
#a,b=map(int,input().split())
#l=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
t_bef=0
x_bef=0
y_bef=0
flag='Yes'

for i in range(1,n+1):
    t,x,y=map(int,input().split())
    dt=t-t_bef
    dist=abs(x-x_bef)+abs(y-y_bef)
    if (dt%2 != dist%2) or dist>dt:
        flag='No'
        break
    t_bef,x_bef,y_bef=t,x,y

print(flag)

Measured memory_bytes: 9132
using System;

class Program
{
    static void Main(string[] args)
    {
        int N = int.Parse(Console.ReadLine());
        string S = Console.ReadLine();
        int K = int.Parse(Console.ReadLine());

        char target = S[K - 1];

        string res = "";
        for (int i = 0; i < N; i++)
        {
            if (S[i] == target)
            {
                res += target;
            } else
            {
                res += "*";
            }
        }

        Console.WriteLine(res);

    }
}

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

using namespace std;

int main() { 
	long long x, k, d, ans;
	cin >> x >> k >> d; x = abs(x);
	if(x / d > k) {
		ans = x - k * d;
		ans = abs(ans);
		cout << ans << endl;
	} else {
		ans = x % d; k -= x / d;
		// cout << ans << " " << k << endl;
		if(k & 1) ans = d - ans;
		ans = abs(ans);
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 3632
n,m,k = map(int,input().split())

mod = 998244353
fac = [1]
inv = [1]
for i in range(1, n+1):
    fac.append(fac[-1] * i % mod)
    inv.append(pow(fac[-1], mod - 2, mod))
# fac[n] = n! mod in o(n)

def comb(n, r, mod):
    if n < r or r < 0:
        return 0
    elif r==0 or r==n:
        return 1
    else:
        return fac[n] * inv[r] * inv[n - r] % mod

ans=0
for i in range(k+1):
    ans = (ans + comb(n-1,i,mod)*m*pow(m-1,n-i-1,mod))%mod

print(ans)

Measured memory_bytes: 98560
//2019.12.29
//PrimeNumeber.
#include<iostream>
#include<vector>
using namespace std;

struct PrimeNumber{//Check prime number.
	std::vector<int> jud;//0 or 1.
	
	PrimeNumber(int n):jud(n+1,-1){//constructor.
		jud[0]=jud[1]=0;
		for(int i=2;i<=n;++i){
			if(jud[i]==-1){
				jud[i]=1;
				for(int j=i+i;j<=n;j+=i) jud[j]=0;
			}
		}
	}
};

int main(){
	PrimeNumber cla((int)2e5);
	
	int x;
	cin>>x;
	
	while(1){
		if(cla.jud[x]==1) break;
		x++;
	}
	
	cout<<x<<endl;
}


Measured memory_bytes: 1152
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<functional>
#include<map>
#include<set>
#include <string>



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


map<string, int> mp;
#define ll long long

int main() {
    int h, w;
    cin >> h >> w;
    char cc;
    int c = 0;
    rep(i, h) {
        rep(j, w) {
            cin >>cc;
            if (cc == '#') {
                c++;
            }
        }
    }
    cout << (c == h + w - 1 ? "Possible" : "Impossible");
    return 0;
}

Measured memory_bytes: 256
//by whr
#include <iostream>
#include <cstring>
#include <map>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<int,int> pi;
typedef long long ll;

int main()
{
	int a,b ;cin>>a>>b;
	int ans=max(a+b, a-b);
	ans=max(ans, a*b);
	cout << ans; 
} 

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

int a, b, c;

int main () {
	cin >> a >> b >> c;
	if (a==b){
		cout << c << endl;
	}
	else if (a==c){
		cout << b << endl;
	}
	else if (b==c){
		cout << a << endl;
	}
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
from collections import deque

N, M = map(int,input().split())
to = [[] for _ in range(N)]
fr = [[] for _ in range(N)]
for _ in range(N-1+M):
    A, B = map(int,input().split())
    A -= 1; B -= 1
    to[A].append(B)
    fr[B].append(A)

# トポロジカルソート　計算量O(V+E)
# 有向グラフに用いる
# 閉路検出(DAGの判定)やDPの更新順序を明らかにするときなどに使える

# num[i] : iに入ってくる有効辺の本数
num = [0] * N

# グラフの構築でnumを更新しておく
num = [len(fr[v]) for v in range(N)]

S = deque()
L = []
#　例えば-1で初期化 dp = [-1] * N
'''  編集　'''

for v in range(N):
    if num[v] == 0:
        S.append(v)
        # 入次数0の頂点において例えば0で初期化　dp[v] = 0
        '''　編集　'''

while S:
    v = S.popleft()
    L.append(v)
    for nv in to[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append(nv)
        # dpの更新　例えばmaxをとる dp[nv] = max(dp[nv], dp[v] + w[v])
        ''' 　編集 '''

# 閉路がある場合の処理
if len(L) != N:
    '''  編集　'''
    pass

index = [-1] * N
for i, v in enumerate(L):
    index[v] = i

for v in range(N):
    if v == L[0]:
        print(0)
        continue
    pv = -1
    d = N
    for cand in fr[v]:
        res = index[v] - index[cand]
        if res < d:
            d = res
            pv = cand
    pv += 1
    print(pv)

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

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


#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()

const ll INF = 100100100100100100;
const ll MOD = 1000000007;

// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD 
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);


int main() {
    ll r, g, b, k; cin >> r >> g >> b >> k;
    bool ok = false;
    frep(i, 0, k) {
        frep(j, 0, k-i) {
            if(b*a_n(2,i) > g*a_n(2,j) && g*a_n(2,j) > r*a_n(2,k-i-j)) {
                ok = true;
            }
        }
    }
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

ll my_abs(ll a) {
    if(a >= 0) return a;
    else return -1 *a;
}

ll a_n(ll a, ll n) { 
    if(n == 0) return 1;
    
    ll ret = a, count = 1;
    while(count * 2 < n) {
        ret *= ret;
        count *= 2;
    }
    if(count == n) return ret;
    else return (ret * a_n(a, n-count));
}

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

ll inv(ll a) {
    return a_n(a, MOD-2);
}

ll madd(ll a, ll b, ll c) {
    ll ret = (a+b) % MOD;
    return (ret+c) % MOD;
}

ll msub(ll a, ll b) {
    if(a < b) return (a-b+MOD) % MOD;
    else return (a-b) % MOD;
}

ll mtime(ll a, ll b, ll c) {
    ll ret = (a*b) % MOD;
    return (ret*c) % MOD;
}


Measured memory_bytes: 3636
#include <iostream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;


//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

int main() {

	string s;
	cin >> s;

	rep(i, s.size()) {
		if (i > 0 && s[i] == s[i - 1]) {
			cout << i - 1+1 << ' ' << i+1 << endl;
			return 0;
		}
		else if (i > 1 && s[i] == s[i - 2]) {
			cout << i - 2+1 << ' ' << i+1 << endl;
			return 0;
		}
	}

	cout << -1 << ' ' << -1 << endl;

	return 0;
}


Measured memory_bytes: 512
#include<iostream>
using namespace std;
int main(){
	int m,f,r;
	while(cin>>m>>f>>r){
		if(m==-1&&f==-1&&r==-1)
			break;
		if(m!=-1&&f!=-1){
			if(m+f>=80)
				cout<<"A"<<endl;
			else if(m+f>=65)
				cout<<"B"<<endl;
			else if(m+f>=50||r>=50)
				cout<<"C"<<endl;
			else if(m+f>=30)
				cout<<"D"<<endl;
			else
				cout<<"F"<<endl;
		}else
			cout<<"F"<<endl;
	}
	return 0;
}


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

using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for(int i=st; i<ed; i++)
#define repn(i, st, ed) for(int i=st; i<=ed; i++)
#define repb(i, ed, st) for(int i=ed; i>=st; i--)

typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI  =3.141592653589793238463;
const int N = 5e5 + 10;
const ll INF = 1LL << 60;

template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}


vector<int> g[N];
bool viss[N];
int vis[N];

// Normal DFS
void dfs(int curr, int par)
{
  for(auto x: g[curr])
    if(x != par)
      dfs(x, curr);
}

// DFS with visiting status
void dfs1(int curr, int par)
{
  viss[curr] = 1;
  for(auto x: g[curr])
    if(!viss[x])
      dfs1(x, curr);
}

// DFS with strongly connected component
void dfs2(int curr, int par, int num)
{
  vis[curr] = num;
  for(auto x: g[curr])
    if(vis[x] == 0)
      dfs2(x, curr, num);
}

// XOR from 0 to n
ll fxor(ll n)
{
  if(n < 0) return 0;
  if(n % 4 == 3) return 0;
  else if(n % 4 == 0) return n;
  else if(n % 4 == 1) return n^(n-1);
  else if(n%4 == 2) return n^(n-1)^(n-2);
}

// Power in logn with mod 1000000007
llt ppow(llt x, llt y)
{
  llt res = 1;
  while(y > 0){
    if(y&1)
      res = (res * x)%M;
    y = y >> 1;
    x = (x * x)%M;
  }
  return res;
}

// Factorial with dp
llt factdp[N];
llt fact(llt x)
{
  if(x == 0) return 1;
  if(x == 1) return 1;
  if(factdp[x] != 0) return factdp[x];
  return factdp[x] = (x*fact(x-1))%M;
}
// Combination
ll modcomb(ll n, ll r)
{
  if(n < r) return 0;
  ll res = fact(n);
  (res *= ppow(fact(r), M-2)) %= M;
  (res *= ppow(fact(n-r), M-2)) %= M;
  return res;
}

//GCD and LCM
llt gcd(llt x, llt y)
{
  if(y == 0) return x;
  return gcd(y, x%y);
}
llt lcm(llt x, llt y)
{
  return (x / gcd(x, y)) * y;
}

// String return to base b
string ansConv ="";
void convert10tob(llt N, int b)
{
     if (N == 0)
        return;
     int x = N % b;
     N /= b;
     if (x < 0)
        N += 1;
     convert10tob(N, b);
     ansConv += to_string(x);
     return;
}

// Convert string to num wiht base b
llt convertstrtob(string a, int b)
{
  llt aa = 0;
  llt mul = 1;
  for(int i=a.length()-1; i>=0; i--)
  {
    int curr = a[i] - '0';
    aa += (curr * mul);
    mul *= b;
  }
  return aa;
}

// Union Find
// Find()
int parent[N], ran[N];
int find(int x)
{
    if(parent[x]==x)return x;
    else return parent[x]=find(parent[x]);
}
// Union()
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)return;

    if(ran[x]<ran[y])
        parent[x]=y;
    else{
        parent[y]=x;
        if(ran[x]==ran[y])ran[x]++;
    }
}

// Main Code

int main()
{
  string s;
  cin>>s;
  if(s[0] == '9' || s[1] == '9') cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}


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

int MOD(int a){
/*	if(a >= p){
		printf("over\n");
	}
*/	return (a %= p) >= 0 ? a : a + p;
}
/*
int power(int a, int N){
	if(N == 0){
		return 1;
	}
	else if(N % 2 == 0){
		return power(MOD(a * a), N / 2);
	}
	else{
		return MOD(a * power(a, N - 1));
	}
}
*/
int solve(int N, vector<int> &X, int k){
//	printf("solve(%lld)\n", k);
/*	if(k == 2 * N){
		return power(2, N);
	}
*/	int i, ans = 0;
	for(i = 0; i < k / 2; i++){
		ans = MOD(2 * ans + X[i]);
	}
//	printf("test5\n");
	int f = 0;
	for(i = 0; i < N; i++){
//		printf("i = %lld\n", i);
		int x;
		if((i / (k / 2)) % 2 == 0){
//			printf("test8\n");
			x = X[i % (k / 2)];
		}
		else{
			x = 1 - X[i % (k / 2)];
		}
//		printf("x = %lld\n", x);
		if(x > X[i]){
			f = 1;
			break;
		}
		else if(x < X[i]){
			f = -1;
			break;
		}
	}
//	printf("f = %lld\n", f);
	if(f <= 0){
		ans++;
	}
	return ans;
}

signed main(){
	int N, i, j, k;
	scanf("%lld", &N);
	vector<int> X(N);
	for(i = 0; i < N; i++){
		scanf("%1lld", &X[i]);
	}
	vector<int> val(2 * N + 1, 0);
	for(k = 1; k <= 2 * N; k++){
		if((2 * N) % k == 0 && N % k != 0){
			val[k] = solve(N, X, k);
//			printf("val[%lld] = %lld\n", k, val[k]);
			for(j = 2; j * j <= k; j++){
				if(k % j == 0){
					val[k] = MOD(val[k] - val[j]);
					if(k / j != j){
						val[k] = MOD(val[k] - val[k / j]);
					}
				}
			}
//			printf("val[%lld] = %lld\n", k, val[k]);
		}
	}
	int ans = 0;
	for(i = 0; i <= 2 * N; i++){
		ans = MOD(ans + i * val[i]);
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 4992
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>

using namespace std;

int main()
{
    int n,x;
    cin >> n;

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

    int mini=1000000;
    int cnt = 0;

    for(int i=0; i<n; i++){
        int a = p[i];
        if(mini>=a){
            mini = a;
            cnt ++ ;
        }
    }

    cout << cnt ;




/*
    int n,m;
    cin >> n >> m;

    string ans1;
    string ans2;
    string ans;
    

    for(int i=0; i<m; i++){
        char x = '0' + n;
        ans1.push_back(x);
    }

    for(int i=0; i<n; i++){
        char x = '0' + m;
        ans2.push_back(x);
    }

    if(ans1<ans2)
        ans = ans1;
    else
        ans = ans2;    

    std::cout << ans << std::endl;
*/
}



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

int main() { 
  int64_t a, b, k, A, B;
  cin >> a >> b >> k;
  if (k>=a+b) {
    A=0;
    B=0;
  }
  else if (k>=a) {
    A=0;
    B=b-(k-a);
  }
  else {
    A=a-k;
    B=b;
  }
  cout << A << " " << B << endl;
} 

Measured memory_bytes: 3660
N = int(input())

S = input()

now = 0

ans = 1

for i in range(2*N):

    #print (S[i],ans,now)

    if i == 0 and S[i] == "W":
        ans = 0
        break

    if i == 0 and S[i] == "B":
        now += 1
        continue

    if S[i] == "W":

        if now % 2 != 0:

            now += 1

        else:

            ans = (ans * now) % (10 ** 9 + 7)
            now -= 1

    if S[i] == "B":

        if now % 2 != 0:

            ans = (ans * now) % (10 ** 9 + 7)

            now -= 1

        else:

            now += 1


    if now < 0:
        ans = 0
        break

    #print (S[i],ans,now)

if now != 0:
    ans = 0

fac = 1
for i in range(N):

    i += 1
    fac = (fac * i) % (10 ** 9 + 7)

print ((fac * ans) % (10 ** 9 + 7) )

Measured memory_bytes: 40560
#include <bits/stdc++.h>
#define ll long long int
#define FASTIO std::ios::sync_with_stdio(false);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define inf 1e18
#define g(x) cout<<x<<" "<<endl;
#define gg(x,y) cout<<x<<" & "<<y<<" "<<endl
#define jam(t) cout<<"Case #"<<t<<": "
#define ggg(x,y,z) cout<<x<<" & "<<y<<" & "<<z<<" "<<endl
#define all(x) x.begin(),x.end()
#define input_from_file  freopen("input.txt", "r", stdin);
#define mod 1000000007ll
#define sz 400005
#define bitcnt(x) __builtin_popcountll(x)
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}



void solve(){
    ll n;
    cin >> n;
    ll cnt = 0;
    for(ll i=2;i<=sqrt(n);i++){
        ll N = n;
        while(N%i == 0)
            N/=i;
        if(N%i== 1)
            cnt++;
        // if(n%i == 0){
        //     N = n;
        //     ll j = n/i;
        //     while(N%j==0)
        //         N/=j;
        //     if(N%j == 1){
        //         cnt++;
        //     }
        // }
        
    }
    //trace(cnt);
    ll sq = sqrt(n);
    ll M = n-1;
    // trace(M);
    for(ll i=1;i<=sqrt(M);i++){
        
        if(M%i == 0){
           if(M/i > sq){
                cnt++;
           }
           if(i > sq){
            cnt++;
           }
        }
    }
    g(cnt+1ll);
}
int main()
{
#ifndef ONLINE_JUDGE
input_from_file
#endif
 
    FASTIO
    cin.tie(NULL);
    fflush(stdout);
    ll t = 1;
  //  cin >> t;
    for(ll tcase = 1;tcase <= t;tcase++){
        solve();
        
    }
return 0;
}

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }


int main() {
  ll N; cin >> N; N++;
  ll dig = 0; while(N>=(1ull<<dig)) dig++;
  vector<int> mae; repr(i,1,dig) mae.push_back(i);
  vector<int> ushiro; repr(i,1,dig) ushiro.push_back(i);

  rep(i,dig-1) {
    if (N&(1ull<<i)) {
      rep(j,mae.size()) {
        if (mae[j]==i+1) {
          mae.insert(mae.begin()+j,dig+i);
          ushiro.insert(ushiro.begin()+(dig-1),dig+i);
          break;
        }
      }
    }
  }
  // debug(all(mae)); debug(all(ushiro));
  cout << mae.size()*2 << endl;
  for (auto x: mae) cout << x << " ";
  for (auto x: ushiro) cout << x << " ";  
  cout << endl;
  // cout << result << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
//typedef long long int;
typedef long long ll;
//typedef pair<int, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;

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

    int sum=0;
    for(int i=1;i < n;i++){
        sum += a[i];
    }
    cout << a[0]/2+sum << endl;
    return 0;
}

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

using str = string;
using ss = stringstream;

str key="keyence";

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

    str s;cin>>s;

    int fk=0;

    for(;fk<7;fk++)if(s[fk]!=key[fk])break;

    int bk=0;

    for(;bk<7;bk++)if(s[s.size()-1-bk]!=key[7-1-bk])break;

    bool ans=fk+bk>=7;

    puts(ans?"YES":"NO");

    return 0;
}


Measured memory_bytes: 3608
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <map>
#include <iomanip>
#include <limits.h>
#include <queue>

using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const bool DEBAG = false;

int main(){

    int N;
    cin >> N;

    priority_queue<double, vector<double>, greater<double> > v;
    for(int i = 0; i < N; i++){
        ll a;
        cin >> a;
        v.push(a);
    }

    while(v.size() != 1){
        double a = v.top();
        v.pop();
        double b = v.top();
        v.pop();
        v.push( (a + b) / 2 );
    }
    cout << v.top() << endl;

}

Measured memory_bytes: 256
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n = II()
    if n == 1:
        print("Hello World")
    else:
        print(II()+II())
    return

#B
def B():
    n, t = LI()
    ans = inf
    for _ in range(n):
        c, ti = LI()
        if ti <= t:
            ans = min(ans, c)
    if ans == inf:
        print("TLE")
        return
    print(ans)
    return

#C
def C():
    n = II()
    xyh = LIR(n)
    xyh.sort(key = lambda x:x[2], reverse = True)
    x1, y1, h1 = xyh[0]
    del xyh[0]
    for x in range(101):
        for y in range(101):
            h = abs(x - x1) + abs(y - y1) + h1
            flg = True
            for xn, yn, hn in xyh:
                if max(h - abs(x - xn) - abs(y - yn), 0) == hn:
                    continue
                flg = False
                break
            if flg:
                print(x, y, h)
                return 
    return

#D
def D():
    n, m = LI()
    ans = 1
    for i in range(1,int(math.sqrt(m)+1)):
        if m % i == 0:
            a = m // i
            #print(i,a)
            if a * n <= m:
                ans = max(ans, a)
            if i * n <= m:
                ans = max(ans, i)
    print(ans)
    return

#Solve
if __name__ == '__main__':
    D()


Measured memory_bytes: 39408
#include<stdio.h>
#include<string.h>

int main()
{
	char str[1001];
	int count[4]= {}, i, check= 0;

	scanf("%s", str);

	for(i= 0; i< strlen(str); i++)
	{
		switch(str[i])
		{
			case 'N':
				count[0]++;
				break;
			case 'S':
				count[1]++;
				break;
			case 'W':
				count[2]++;
				break;
			case 'E':
				count[3]++;
				break;
			default:
				break;
		}
	}

	for(i= 0; i< 3; i+= 2)
		if(count[i]* count[i+ 1] || !count[i] && !count[i+ 1])
			check++;

	if(check== 2)
		printf("Yes\n");
	else
		printf("No\n");

	return 0;
}

Measured memory_bytes: 128