akhauriyash/Code-Regression huggingface dataset:
def main():
    N = int(input())
    AB = [list(map(int, input().split())) for _ in range(N)]

    lowerlist = []
    upperlist = []

    for ab in AB:
        lowerlist.append(ab[0])
        upperlist.append(ab[1])

    lowerlist.sort()
    upperlist.sort(reverse=True)

    if N % 2 == 0:
        # すべて最低値を選んだときの中央値を求める
        lowermid = (lowerlist[N//2-1] + lowerlist[N//2]) / 2
        # すべて最高値を選んだときの中央値を求める
        uppermid = (upperlist[N//2-1] + upperlist[N//2]) / 2
        print(int((uppermid - lowermid) * 2) + 1)
    else:
        # すべて最低値を選んだときの中央値を求める
        lowermid = lowerlist[N//2]
        uppermid = upperlist[N//2]
        print(uppermid-lowermid+1)


if __name__ == "__main__":
    main()


Measured memory_bytes: 49300
s=input()
l=len(s)
cnt1=0
cnt2=0
cnt3=0
for i in range(l):
  if s[i]=='a':
    cnt1+=1
  elif s[i]=='b':
    cnt2+=1
  elif s[i]=='c':
    cnt3+=1
if max(cnt1,cnt2,cnt3)==1:
  print('YES')
elif cnt1==0 or cnt2==0 or cnt3==0:
  print('NO')
elif max(cnt1,cnt2,cnt3)-min(cnt1,cnt2,cnt3)<=1:
  print('YES')
else:
  print('NO')

Measured memory_bytes: 3188
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#define ll long long
#define M 100010
#define in(a) cin>>a
using namespace std;
ll n,c[600],s[600],f[600],ans=0;
int main(){
	in(n);
	ll i,j;
	for(i=1;i<=n-1;i++){
		in(c[i]);in(s[i]);in(f[i]);
	}
	for(i=1;i<=n;i++){
		ans=0;
		for(j=i;j<n;j++){
			if(ans<s[j])ans=s[j];
			else if((ans-s[j])%f[j])ans+=(f[j]-(ans-s[j])%f[j]);
			ans+=c[j];
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

int main()
{
    string s;
    cin >> s;
    if(s[0]=='S')
        cout <<"Cloudy" << endl;
    else if(s[0]=='C')
        cout << "Rainy" << endl;
    else
        cout << "Sunny" << endl;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#define N 60
#define M 1000000007
typedef long long LL;

LL read()
{
	char c;
	while((c = getchar()) < 48 || c > 57);
	LL x = c - 48;
	while((c = getchar()) > 47 && c < 58) x = x * 10 + c - 48;
	return x;
}

int main() 
{
	LL m2, x, sum = 0, y;
	int i, j, c1[N] = {0}, n;
		
	scanf("%d", &n);
	for(i = 1; i <= n; i++){
		x = read();
		j = 0;
		while(x){
			if(x & 1) c1[j]++;
			x >>= 1, j++;
		}
	}
	for(i = 0, m2 = 1, y = (LL)n; i < N; i++){
		x = (LL)c1[i];
		sum += x * (y - x) % M * m2 % M;
		m2 <<= 1;
		if(m2 >= M) m2 -= M;
	}
	printf("%lld\n", sum %= M);
	
	return 0;
}

Measured memory_bytes: 128
def BFS(N,start,roots):
    dist = [-1] * N
    stack = []
    stack.append(start)
    dist[start] = 0
    while stack:
        label = stack.pop(-1)
        for i, c in roots[label]:
            if dist[i] == -1:
                dist[i] = dist[label] + c
                stack += [i]
    return dist
############################################################
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')

N = I()
v = [[] for i in range(N)]
for i in range(N-1):
    a, b, c = LI()
    v[a-1].append([b-1,c])
    v[b-1].append([a-1,c])
Q, K =LI()

#print(BFS(N,K-1,v))
W = BFS(N,K-1,v)
ansC = []
for i in range(Q):
    x, y = LI()
    ansC.append(W[x-1]+W[y-1])

for v in ansC:
    print(v)

Measured memory_bytes: 87344
#include <iostream>

using namespace std;

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

    string str;

    cin >> str;

    int s = str.find('A');
    int e = str.rfind('Z');

    cout << e - s + 1 << endl;

    return 0;
}


Measured memory_bytes: 720
import collections
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
l = [I() for i in range(N)]
l.sort()


if N == 1:
    print(1)
else:
    tmp = 0
    cnt = 0
    ans = 0
    for i in range(N):
        if tmp == l[i]:
            cnt += 1
            if i == N-1:
                if cnt%2 != 0:
                    ans += 1
        else:
            if cnt%2 != 0:
                ans += 1
            tmp = l[i]
            cnt = 1
            if i == N-1:
                if cnt%2 != 0:
                    ans += 1

    print(ans)

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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int MAXN = 15;

int dp[1 << MAXN][MAXN];
int cost[1 << MAXN];

int g[MAXN][MAXN];

void solve(){
	int n, m, a, b, c;
	scanf("%d%d", &n, &m);
	REP(i, 0, m) {
		scanf("%d%d%d", &a, &b, &c);
		a--;
		b--;
		g[a][b] = g[b][a] = c;
	}
	REP(i, 0, 1 << n) {
		REP(j, 0, n) {
			if (i & (1 << j)) {
				REP(k, j + 1, n) {
					if (i & (1 << k)) {
						cost[i] += g[j][k];
					}
				}
			}
		}
	}
	REP(i, 0, 1 << n) {
		REP(j, 0, n) {
			dp[i][j] = -INF;
		}
	}
	REP(i, 0, 1 << (n - 2)) {
		int mask = (i << 1) + 1;
		dp[mask][0] = cost[mask];
	}
	REP(i, 0, 1 << (n - 2)) {
		int mask = (i << 1) + 1;
		REP(j, 0, n - 1) {
			if (dp[mask][j] >= 0) {
				int add_mask = (1 << (n - 1)) - 1 - mask;
				for(int cur_mask = add_mask; cur_mask; cur_mask = (add_mask & (cur_mask - 1))) {
					REP(k, 1, n - 1) {
						if ((cur_mask & (1 << k)) && g[j][k]) {
							dp[mask | cur_mask][k] = max(dp[mask | cur_mask][k], dp[mask][j] + g[j][k] + cost[cur_mask]);
						}
					}
				}
				if (g[j][n - 1]) {
					dp[(1 << n) - 1][n - 1] = max(dp[(1 << n) - 1][n - 1], dp[mask][j] + g[j][n - 1] + cost[(1 << n) - 1 - mask]);
				}
			}
		}
	}
	cout << cost[(1 << n) - 1] - dp[(1 << n) - 1][n - 1] << endl;
}   

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}


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

typedef long long llint;
typedef long double ld;

#define inf 1e18
#define mod 1000000007
priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve(){
  int n,k;
  cin >> n >> k;
  double p=1/(double)n;
  double ans=0;
  for(int i=1;i<=n;i++){
    int score=i;
    double tmp=p;
    while(score<k){
      score*=2;
      tmp/=2;
    }
    ans+=tmp;
  }
  printf("%.12f\n",ans);
}

int main(){
  solve();
  return 0;
}


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

int nCm(int n,int m){
	m=min(m,n-m);
	if(	m==0)return 1;
	else return nCm(n-1,m-1)*n/m;
}

signed main(){
	int n,a,b;cin>>n>>a>>b;
	vector<int> v(n);
	for(auto&& w:v)cin>>w;

	sort(v.begin(),v.end(),greater<int>());
	int a0=v[a-1];  
  
	int sm=accumulate(v.begin(),v.begin()+a,0ll);
	double avg=(double)sm/a;

	
		

	int x=count(v.begin(),v.end(),a0);
	int y=count(v.begin(),v.begin()+a,a0);
//  	cerr<<x<<" "<<y<<" "<<nCm(x,y)<<endl;
	int cnt=0;int b0=min(b,y-x+a);
	if(v[0]>a0){cnt=nCm(x,y);}
	else {
		 for(int i=0;i+a<=b && y+i<=x;i++){
		cnt+=nCm(x,(y+i));}
	}  

	cout<<setprecision(10);
	cout<<avg<<endl;
	cout<<cnt<<endl;	
}

Measured memory_bytes: 256
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=5e18;
typedef pair<int,int> P;
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  ll n,m,ans=0,cnt=0,a[100005]={};
  cin >> n >> m;
  map<ll,ll> mp;
  mp[0]++;
  for(int i=1; i<=n; i++) {
    cin >> a[i];
  }
  for(int i=1; i<=n; i++) {
    a[i]+=a[i-1];
    mp[a[i]%m]++;
  }
  for(auto p:mp) {
    ans+=p.second*(p.second-1)/2;
  }
  cout << ans << endl;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define     PI                       acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) 	{mask &= (mask-1);++ret;	}return ret;}
const int N=200000+5,M=1000000+5;
string s[N];
int ans=0;
int vis[M][100],tree[M][30],cnt[M][30];
int rng(int f,int t,int ch){
  if(t<f) return 0;
 return vis[t][ch]-(f?vis[f-1][ch]:0);
}
int id=1;
void insertt(string word,int chh){

   int node=1;

   for(int i=0;i<sz(word);i++){
    int ch=word[i]-'a';
    if(tree[node][ch]==0){
        tree[node][ch]=++id;
    }
    node = tree[node][ch];
   }
   cnt[node][chh]++;
}
void query(string word,int cid,int ex){

   int node=1;
   for(int j=0;j<26;j++){
      if(rng(0,cid,j)==0||(cid==0&&j==ex)) continue;
        ans+= cnt[node][j];
   }

   for(int i=0;i<sz(word);i++){
    int ch=word[i]-'a';
    node= tree[node][ch];
    cid--;
    for(int j=0;j<26;j++){
        if(rng(0,cid,j)==0||(cid==0&&j==ex)) continue;
        ans+= cnt[node][j];
    }

   }
}

int main(){
    GO();
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s[i];
        string ss="";
        int siz=sz(s[i]);
        for(int j=siz-1;j>=1;j--) ss+=s[i][j];
        insertt(ss,s[i][0]-'a');
    }

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

        int siz=sz(s[i]);
        string ss="";
        for(int j=0;j<siz;j++){
            for(int k=0;k<26;k++) vis[j][k]=(j-1>=0?vis[j-1][k]:0);
            vis[j][s[i][j]-'a']++;
        }
        for(int j=siz-1;j>=1;j--) ss+=s[i][j];
        query(ss,siz-1,s[i][0]-'a');
    }

    cout<<ans<<"\n";
}


























































































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

#define rep(i,n) for(ll i=0; i<(ll)(n); i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define PB push_back

vector<ll> prime_factorize(ll N) {
    vector<ll> res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back(ex);
    }

    // 最後に残った数について
    if (N != 1) res.push_back(1);
    return res;
}

int main() {
    ll N; cin >> N;
    ll ans = 0;
    const auto &res1 = prime_factorize(N);
    ll p = res1.size();
    vector<ll> res(p);
    copy(res1.begin(), res1.end(), res.begin());
    
    
    
    rep(i, p) {
        ll num = res.at(i);
        ll cnt = 0;
        ll j = 2;
        while(num > 0){
            num -= j;
            cnt++; j++;
        }
        ans += cnt;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3628
#include<iostream>
#include<string>
using namespace std;
int main(){
	int n;
	string s;
	cin>>s>>n;
	int f=0,i=0,ans;
	while (f==0){
		if (s[i]!='1'||n==1){
			ans=s[i]-'0';
			break;
		}
		n--;
		i++;
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;


public class Main {    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        String s = sc.next();
        int len = s.length(); 
        if(len <= k){
            System.out.println(s);
        }else{
            String ans = s.substring(0, k);
            System.out.println(ans + "...");
        }   
        sc.close();
    }
}


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

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

ll sumstr(string s)
{
    if(SZ(s) == 0) return 0;
    else {
        ll sum = stoll(s);
        REP(i,1,SZ(s)) {
            sum += stoll(s.substr(0, i)) * (1<<(SZ(s)-i-1)) + sumstr(s.substr(i, SZ(s)-i));
        }
        return sum;
    }

}

int main()
{
    string S;
    cin >> S;
    PR(sumstr(S));

    return 0;
}

/*



*/

Measured memory_bytes: 3620
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<climits>
#include<numeric>
#include<cmath>
#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 FORR(i, m, n) for(int i = m;i >= n;i--)
#define VSORT(v) sort(v.begin(), v.end());
#define VE vector<int>
#define llong long long
#define pb(a) push_back(a)
using namespace std;
int main() {
	int num;
	string str;
	cin >> num;
	cin >> str;
	int cnt_ten = 0;
	vector<int> sya_vec(num);
	int sya_cnt = 0;
	REP(i, str.size()) {
		if (str[i] == '#') {
			sya_cnt++;
		}
		sya_vec[i] = sya_cnt;
	}
	VE ten_vec(num);
	int ten_cnt = 0;
	REPR(i, str.size() - 1) {
		if (str[i] == '.') {
			ten_cnt++;
		}
		ten_vec[i] = ten_cnt;
	}
	int mint = ten_vec[0];
	REP(i, str.size()-1) {
		mint = min(mint, sya_vec[i] + ten_vec[i + 1]);
	}
	mint = min(mint, sya_vec[str.size() - 1]);
	cout << mint << endl;
	return 0;
}

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

int main() {
    int n;
    cin >> n;
    vector<int> H;
    H.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> H[i];
    }

    int cnt = 0;
    int ans = 0;
    for (int i = 0; i < n - 1; i++) {
        if (H[i] < H[i + 1]) {
            ans = max(ans, cnt);
            cnt = 0;
        }
        else {
            cnt++;
            if (i == n - 2) {
                ans = max(ans, cnt);
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 640
#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 FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  sort(ALL(a));

  vector<P> ans;
  FOR(i, 1, n - 1) {
    if (a[i] <= 0) {
      ans.push_back(P(a[n - 1], a[i]));
      a[n - 1] -= a[i];
    } else {
      ans.push_back(P(a[0], a[i]));
      a[0] -= a[i];
    }
  }
  ans.push_back(P(a[n - 1], a[0]));

  cout << a[n - 1] - a[0] << endl;
  for (auto x : ans) {
    cout << x.first << " " << x.second << endl;
  }
  return 0;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N,K;
int sumB[1111][1111];
int sumW[1111][1111];

signed main(){
    cin>>N>>K;
    rep(i,N){
        int x,y;
        char c;
        cin>>x>>y>>c;

        int tmp=x/K+y/K;
        x%=K;y%=K;
        if(tmp&1){
            if(c=='B')c='W';
            else c='B';
        }
        if(c=='B')sumB[y+1][x+1]++;
        else sumW[y+1][x+1]++;
    }

    rep(i,K)rep(j,K){
        sumB[i+1][j+1]+=sumB[i][j+1]+sumB[i+1][j]-sumB[i][j];
        sumW[i+1][j+1]+=sumW[i][j+1]+sumW[i+1][j]-sumW[i][j];

    }

    int ans=0;
    for(int i=0;i<=K;i++)for(int j=0;j<=K;j++){
        int tmp=0;
        tmp+=sumB[i][j];
        tmp+=sumB[K][K]-sumB[i][K]-sumB[K][j]+sumB[i][j];
        tmp+=sumW[K][j]-sumW[i][j];
        tmp+=sumW[i][K]-sumW[i][j];
        chmax(ans,tmp);
        chmax(ans,N-tmp);
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 18688
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    int n, m = 7;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        if (m % n == 0) {
            cout << i;
            return 0;
        }
        m = (m * 10 + 7) % n;
    }
    cout << -1;
}


Measured memory_bytes: 3168
/* amitdwivedi */
#include <bits/stdc++.h>
using namespace std;
using ll = long long int; 
#define fr(i,b) for (ll i = 0; i < b; ++i)
#define tr(x,cont) for(auto x:cont)
#define c(x) cout << x << endl;
const ll mxn = 2e5+1;
 
int main(){
    ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    ll n,m;
    cin>>n>>m;
    cout << (n*m)/__gcd(n,m) << endl;

}

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


int memo[50][500][500];

int n, ma, mb;
int a[50], b[50], c[50];


int dfs(int i, int l, int r){
  if(l % ma == 0 && l != 0){
    int tmp = l / ma;
    if(tmp * mb == r) return 0;
  }

  if(i == n) return 100000;

  if(memo[i][l][r] != -1) return memo[i][l][r];

  return memo[i][l][r] = min(dfs(i+1,l,r), dfs(i+1,l+a[i],r+b[i]) + c[i]);
}

int main(){

  cin >> n >> ma >> mb;

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

  for(int i = 0;i < 50;i++){
    for(int j = 0;j < 500;j++){
      for(int k = 0;k < 500;k++){
        memo[i][j][k] = -1;
      }
    }
  }

  int ans;
  ans = dfs(0,0,0);
  if(ans >= 100000){
    cout << -1 << endl;
  }else{
    cout << ans << endl;
  }

  return 0;
}

Measured memory_bytes: 49024
import java.util.*;

class Main {
	Scanner sc;
	int N, K;
	
	private void calc() {
		sc = new Scanner(System.in);
		N = sc.nextInt();
		K = sc.nextInt();
		
		int d = 1;
		while (N >= K) {
			d++;
			N /= K;
		}
		System.out.println(d);
	}

	public static void main(String[] args) {
		new Main().calc();
	}
}


Measured memory_bytes: 21844
function main()
	N=parse(Int,readline())
	L=sort(map(x->parse(Int,x),split(readline())))
	s=0
	for i=1:N
		k=i+2
		for j=i+1:N
			while k<=N&&L[k]<L[i]+L[j]
				k+=1
			end
			s+=k-j-1
		end
	end
	println(s)
end
main()


Measured memory_bytes: 167780
from bisect import *
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
data = []
for _ in range(M):
    B, C = map(int, input().split())
    data.append((B, C))
data.sort(key=lambda x:x[1], reverse=True)

D = []
for B, C in data:
    i = bisect_left(A, C)
    p = min(B, i)
    if len(D)+p <N:
        D += [C]*p
    else:
        D += [C]*(N-len(D))
        break

res = 0
for i in range(N):
    if i>=len(D) or A[i] > D[i]:
        res += A[i]
    else:
        res += D[i]
print(res)

Measured memory_bytes: 97320
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            var count = cin.ReadInt;

            int[] line = new int[count];

            for (int i = 0; i < count; i++)
            {
                line[i] = cin.ReadInt;
            }

            Array.Sort(line);

            var a = 0;

            var x = 0;
            var y = 0;

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

                if (i == 0)
                {
                    a++;
                    x = line[i];
                }

                if (i != 0)
                {
                    if (x != line[i])
                    {
                        a++;
                    }
                    x = line[i];

                }
                           

            }

            WriteLine(a.ToString());


        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue<string> inputStream;
        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
    }
}



Measured memory_bytes: 27572
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <string>
#include <limits>
#include <algorithm>
#include <map>
#include <sstream>
#include <unordered_map>

using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
static ull tenq = 1000000000;
static ull mod = tenq + 7;

using namespace std;

ll calc(ll W, ll H) {
  ll res = H * W;
  for (auto h1 = 1; h1 < H; h1++) {
    ll s1 = h1 * W;
    ll h2 = H - h1;
    ll w = W;
    ll s2, s3;
    if ((h2 * w) % 2 == 0) {
      s2 = h2 * w / 2;
      s3 = h2 * w / 2;
    } else {
      if (w > h2) {
        ll tmp = w;
        w = h2;
        h2 = tmp;
      }
      s2 = w * (h2/ 2);
      s3 = w * ((h2 + 1)/ 2);
    }
    ll maxs = max(s1, max(s2, s3));
    ll mins = min(s1, min(s2, s3));
    if (mins == 0) continue;
    ll cost = maxs - mins;
    res = min(res, cost);
  }
  return res;
}

int main()
{
  ll H, W;
  cin >> H >> W;
  ll res = min(calc(H, W), calc(W, H));

  cout << res << endl;

  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
	int H,W,i,j;
	while(1){
		scanf("%d %d",&H,&W);
		if(H==0 && W==0){
			break;
		}
		for(i=1;i<=H;i++){
			for(j=1;j<=W;j++){
				if(i==1 || i==H){
					printf("#");
				}
				if(i>1 && i<H){
					if(j==1 || j==W){
						printf("#");
					}else{
						printf(".");
					}
				}
			}
			printf("\n");
		}
		printf("\n");
	}
	
	return 0;
}

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

using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,n,m) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define VEC2(x,a,b) vector<vector<ll>> x = vector<vector<ll>>(a,vector<ll>(b,0))
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cinv(v,n,m) FOR(i,n,m) cin>>v.at(i)
#define coutv(v,n,m) FOR(i,n,m) cout<<v.at(i) <<" "; cout<<endl
#define cout(n) cout<<fixed<<setprecision(n)

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,k;
    cin>>n>>k;
    n++;
    n /= 2;
    if(n>=k) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,k,n) for(int i = k; i < n; i++)  
#define repp(i,k,n) for(int i = k; i <= n; i++) 
#define per(i,k,n) for(int i = k; i >= n; i--)

#define int long long
#define all(a) a.begin(),a.end()       

#define pb push_back
typedef long long ll; 
typedef pair<int, int> ii;
typedef pair<int,ii > iii; 
typedef vector<ii> vii;
typedef vector<char> vchar;
typedef vector<int> vi;
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound

#define mem(a, b) memset(a, b, sizeof(a));
#define mem0(a) memset(a, 0, sizeof(a));

#define INF 1000000000
//#define mod 998244353
#define mod 1000000007
#define endl "\n"


   //=\/=\/=\/=\/=\/=\/=\/=\/=\/=\        Jai Hind Dosto        /=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\\
  //=\/=\/=\/=\                    ̿̿ ̿̿ ̿̿ ̿'̿'\̵͇̿̿\з= ( ▀ ͜͞ʖ▀) =ε/̵͇̿̿/’̿’̿ ̿ ̿̿ ̿̿ ̿̿                             /=\/=\/=\/=\\
 //=\/=\/=\/=\                                                                               /=\/=\/=\/=\\
//=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\/=\\



int modPow(int a, int b);    



                
void solve(){
    int n; cin >> n;
    char a[n], b[n]; int c[n+1]={0};
    rep(i,0,n)cin >> a[i];
    int cnt = 0;

    rep(i,0,n){
        if(a[i]=='W')cnt++;
    }
    int cnt2=0;
    per(i,n-1,0){
        if(a[i]=='R')cnt2++;
       
    }
    int ans = 0 ;
    rep(i,cnt2,n){
        if(a[i]=='R')ans++;
    }
    cout << ans;
        
}






signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //cout << fixed;
    //cout <<setprecision(6);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    
   // int test=1; cin >> test; while(test--)
    solve();
}

int modPow(int a, int b){
    if(b==0)return 1;
    if(b%2==0){
        int x = a*a;
        x%=mod;
        return modPow(x,b/2);
    }
    return (a*modPow(a,b-1))%mod;
}




Measured memory_bytes: 5396
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof(a))
#define PB push_back
#define MP make_pair
using namespace std;

const int MAX_N = 100;
int pos;
string s;

int dfs()
{
    vector<int> v;

    while (pos < s.size()) {
        int res = 0;
        if (s[pos] == '[') {
            pos++;
            v.PB(dfs());
        } else if (s[pos] == ']') {
            pos++;
            sort(ALL(v));
            REP(i, (v.size() + 1) / 2) res += v[i];
            return res;
        } else {
            while (isdigit(s[pos])) {
                res = res * 10 + (s[pos] - '0');
                pos++;
            }
            v.PB((res + 1) / 2);
        }
    }
}

int main()
{
    int n, ans[MAX_N] = {0};

    scanf("%d", &n);
    REP(i, n) {
        cin >> s;
        pos = 1;
        ans[i] = dfs();
    }

    REP(i, n) printf("%d\n", ans[i]);

    return 0;
}

Measured memory_bytes: 1296
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int[] AR = new int[3];
		AR[0] = sc.nextInt();
		AR[1] = sc.nextInt();
		AR[2] = sc.nextInt();
		int max = 0;
		Arrays.sort(AR);
		int combined = AR[2]*10 + AR[1];
		System.out.println(combined + AR[0]);
	}

}


Measured memory_bytes: 27072
#include<iostream>
#include<algorithm>
using namespace std;
int h,w;
int c[26];
main()
{
	cin>>h>>w;
	string s;
	for(int i=0;i<h;i++)
	{
		cin>>s;
		for(int j=0;j<w;j++)
		{
			c[s[j]-'a']++;
		}
	}
	int cnt1=0,cnt2=0;
	for(int i=0;i<26;i++)
	{
		if(c[i]%2)cnt1++;
		c[i]-=c[i]%2;
		if(c[i]%4==2)cnt2++;
	}
	int ans1=h*w%2,ans2=h%2*(w/2)+w%2*(h/2);
	if(cnt1!=ans1||cnt2>ans2||(ans2-cnt2)%2)cout<<"No"<<endl;
	else cout<<"Yes"<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x)  x.begin(),x.end()
#define vi vector<int>
#define print(x) for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<'\n';
#define in(x,n) for(int e=0;e<n;e++){int y;cin>>y;x.pb(y);}
#define lower(sl)  transform(sl.begin(), sl.end(), sl.begin(), ::tolower); 
//Available in Snips
#define fi(i, x, y)   for (__typeof(x) i = x; i < y; i ++)
#define fir(i, x, y)  for (__typeof(x) i = x; i > y; i --)
#define fv(x)   for (auto itr = x.begin(); itr != x.end(); itr ++)
#define fvr(x)  for (auto itr = x.end() - 1; itr != x.begin() - 1; itr --)

int n,m,k;
vector<set<int>> s1;
vector<set<int>> s2;

int dfs(vector<vector<int>> adj,vector<bool> &visited,int s){
	int t = s;
	int res = 0;
	stack<int> stack;

    stack.push(s);

    while (!stack.empty())
    {
        s = stack.top();
        stack.pop();

        if (!visited[s] and s1[t].find(s)==s1[t].end() and s2[t].find(s)==s2[t].end())
        {
            visited[s] = true;
			res += 1;
        }

        for (auto i = adj[s].begin(); i != adj[s].end(); ++i)
            if (!visited[*i])
                stack.push(*i);
    }

	return res;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//code here
	int n,m;
	cin >> n >> m;
	//2even
	int res = n * (n-1) / 2;
	//2odd
	res += m*(m-1) / 2;
	cout << res;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
using namespace std;
#define pi pair<int32,int32>
#define pl pair<int64,int64>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
// cout << fixed << setprecision(10);
template<typename T>
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T>
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}

int main(){
    string s; cin >> s;
    int n = s.size();
    //map<string,int> mp;
    string tmp1 = "",tmp2 = "";
    int ans = 0;
    if (n == 1){
        cout << 1 << endl;
        return 0;
    }
    for (int i = 0; i < n; i++){
        if (i == 0){
            tmp1 += s[0];
            ans++;
        }
        if (tmp1.size() && i != 0){
            tmp2 += s[i];
            if (tmp1 != tmp2){
                ans++;
                //cout << tmp1 << endl;
                tmp1 = tmp2;
                tmp2 = "";
            }
        }
    }

    
    cout << ans << endl;
}

Measured memory_bytes: 640
#Nは参加人数,Kは初期ポイント、Qはクイズ回数
N ,K ,Q = map(int,input().split())

N_case =[]
i =0
while i <N :
  N_case.append(K-Q)
  i+=1

j =0
while j <Q :
  a = int(input())
  N_case[a-1] +=1
  j +=1


for n in N_case:
  if n>0:
    print('Yes')
  else:
    print('No')



Measured memory_bytes: 7908
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <cassert>
using namespace std;

#define GET_ARG(a,b,c,F,...) F
#define REP3(i,s,e) for (i = s; i <= e; i++)
#define REP2(i,n) REP3 (i,0,(int)(n)-1)
#define REP(...) GET_ARG (__VA_ARGS__,REP3,REP2) (__VA_ARGS__)
#define RREP3(i,s,e) for (i = s; i >= e; i--)
#define RREP2(i,n) RREP3 (i,(int)(n)-1,0)
#define RREP(...) GET_ARG (__VA_ARGS__,RREP3,RREP2) (__VA_ARGS__)
#define DEBUG(x) cerr << #x ": " << x << endl

typedef long long ll;

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b,a%b);
}

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

int main(void) {
    int a, b;
    while (cin >> a >> b) {
        cout << gcd(a,b) << " " << lcm(a,b) << endl;
    }
    return 0;
}

Measured memory_bytes: 3092
from sys import stdin

d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}

input = stdin.readline

n = int(input())

tree = [[0] * n * 2 for _ in range(26)]

def build(arr):
    for i in range(n):
        tree[ddd][n + i] = arr[i]

    for i in range(n - 1, 0, -1):
        tree[ddd][i] = tree[ddd][i << 1] + tree[ddd][i << 1 | 1]

def update(p, value):
    p += n
    tree[ddd][p] = value

    i = p

    while i > 1:
        tree[ddd][i >> 1] = tree[ddd][i] + tree[ddd][i ^ 1]
        i >>= 1

def query(l, r):
    res = 0

    l += n
    r += n

    while l < r:

        if l & 1:
            res += tree[ddd][l]
            l += 1

        if r & 1:
            r -= 1
            res += tree[ddd][r]

        l >>= 1
        r >>= 1

    return res

s = list(input())

arrs = [[0] * n for _ in range(26)]

for i in range(n):
    arrs[d[s[i]]][i] = 1

for ddd in range(26):
    build(arrs[ddd])

for _ in range(int(input())):
    t = input().split()

    if t[0] == '1':
        i, c = t[1:]
        i = int(i) - 1

        new = d[c]
        cur = d[s[i]]

        s[i] = c

        ddd = cur
        update(i, 0)

        ddd = new
        update(i, 1)

    if t[0] == '2':
        l, r = map(int, t[1:])
        l -= 1
        r -= 1

        ans = 0

        for ddd in range(26):
            if query(l, r + 1) != 0:
                ans += 1

        print(ans)

Measured memory_bytes: 369144
from fractions import Fraction
import math

N = int(input())
TA = [tuple(map(int, input().split())) for _ in range(N)]

"""
Ti : Ai = t : a の時、整数xを使って、i回目のそれぞれの得票数を、t * x, a * x と表せる。
なので、一回前のそれぞれの得票数がわかっている場合、xの範囲がわかるので、xが最小になるように更新していく
"""


T = 0
A = 0
for i,(t,a) in enumerate(TA):
    if i == 0:
        T = t
        A = a
    else:
        x = math.ceil(max(Fraction(T, t), Fraction(A, a)))
        T = t * x
        A = a * x


print(T+A)

Measured memory_bytes: 5088
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

import collections
ac = collections.Counter(a)
bc = collections.Counter(b)

for i in range(1,n+1):
    if ac[i] + bc[i] > n:
        print("No")
        exit()

from collections import defaultdict
a_right  = defaultdict(int)
b_left = defaultdict(int)

for i in range(n):
    a_right[a[i]] = i

for i in reversed(range(n)):
    b_left[b[i]] = i

sft=[0]
for dup in set(a) & set(b):
    sft.append(a_right[dup] - b_left[dup]+1)

from collections import deque
b=deque(b)
b.rotate(max(sft))
print("Yes")
print(*b)

Measured memory_bytes: 175216
n, k = map(int, input().split())
A = [None]*k
for i in range(k):
    d = int(input())
    A[i] = list(map(int, input().split()))

sunuke = [0] * n
for i in range(k):
    for j in A[i]:
        sunuke[j - 1] += 1

ans = 0
for i in sunuke:
    if i == 0:
        ans += 1
print(ans)


Measured memory_bytes: 9192
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int s,k,l=0;
  cin >> s;
  vector<int> vec(0);
  vec.push_back(s);
  for(int i=1;i<1000000;i++){
    if(vec.at(i-1)%2==0)
      k=vec.at(i-1)/2;
    else
      k=vec.at(i-1)*3+1;
    for(int j=0;j<i;j++){
      if(k==vec.at(j)){
        l++;
      }
    }
    if(l==0)
      vec.push_back(k);
    else{
      cout << i+1 << endl;
      break;
    }
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b,c;
signed main(){
	cin>>a>>b>>c;
	c-=a+b;
	if(c<0){puts("No");return 0;}
	puts(4ll*a*b<c*c?"Yes":"No");
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void print_YES_NO(bool answer){cout<<(answer?"YES":"NO")<<endl;}
void print_Yes_No(bool answer){cout<<(answer?"Yes":"No")<<endl;}
void print_POSSIBLE_IMPOSSIBLE(bool answer){cout<<(answer?"POSSIBLE":"IMPOSSIBLE")<<endl;}

LL dp[2005][2005];
int t[2][2005];

int main(){
	int n,m;
	LL a,b,c;
	int i,j,k;
	LL s=1;
	cin>>n>>m;
	for(i=0;i<n;i++){
		cin>>t[0][i];
	}
	for(i=0;i<m;i++){
		cin>>t[1][i];
	}
	dp[0][0]=1;
	for(i=0;i<=n;i++){
		for(j=0;j<=m;j++){
			if(i+j==0)continue;
			if(i==0){dp[i][j]=dp[i][j-1];continue;}
			if(j==0){dp[i][j]=dp[i-1][j];continue;}
			dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1];
			if(dp[i][j]<MAX)dp[i][j]+=MAX;
			if(dp[i][j]>=MAX)dp[i][j]%=MAX;
			if(t[0][i-1]==t[1][j-1])dp[i][j]+=dp[i-1][j-1];
			if(dp[i][j]>=MAX)dp[i][j]%=MAX;
		}
	}
	cout<<dp[n][m]%MAX<<endl;
}


Measured memory_bytes: 31616
#![allow(unused_imports)]
#![allow(dead_code)]

use std::io::{ self, BufRead };
use std::iter::Iterator;
use std::cmp::*;
use std::collections::*;

fn sum(mut x: i64) -> i64 {
    let mut y = 0;
    while x > 0 {
        y += x % 10;
        x /= 10;
    }
    y
}

fn main() {
    let stdin = io::stdin();
    let mut cin = stdin.lock();
    let mut s = String::new();
    cin.read_line(&mut s);
    let mut it = s
        .split_whitespace()
        .map(|t| t.parse::<i64>().unwrap());

    let n = it.next().unwrap();
    let a = it.next().unwrap();
    let b = it.next().unwrap();

    let mut ans = 0;
    for x in 1..(n+1) {
        let y = sum(x);
        if a <= y && y <= b {
            ans += x;
        }
    }

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

Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int head[maxn], tot = 1;
int Next[maxn << 1], to[maxn << 1];
void add(int u, int v) {
    to[tot] = v, Next[tot] = head[u];
    head[u] = tot++;
}
int n;
int deep[maxn];
int bfs(int s) {
    queue<int> Q;
    memset(deep, 0, sizeof deep);
    deep[s] = 1;
    Q.push(s);
    int ans = s;
    while (!Q.empty()) {
        int u = ans = Q.front();
        Q.pop();
        for (int i = head[u]; i; i = Next[i])
            if (!deep[to[i]]) {
                deep[to[i]] = deep[u] + 1;
                Q.push(to[i]);
            }
    }
    return ans;
}
int main() {
    scanf("%d", &n);
    for (int i = 1, u, v; i < n; i++) {
        scanf("%d%d", &u, &v);
        add(u, v), add(v, u);
    }
    int cnt = deep[bfs(bfs(1))];
    printf("%s\n", cnt % 3 == 2 ? "Second" : "First");
    return 0;
}

Measured memory_bytes: 5760
#include <iostream>
#include <cmath>

using namespace std;
#define N 101000
#define inf (1 << 30)
int h[N],d[N],i,j,n,k,curr;
int main()
{
    cin >> n;
    k = 2;
    for (i = 0; i < n; ++i){
        cin >> h[i];
        d[i] = inf;
    }
    d[0] = 0;
    for (i = 0; i < n; ++i)
        for(j = 0; j < k; ++j)
            if(i + j + 1 < n){
                curr = abs(h[i] - h[i + j + 1]);
                d[i + j + 1] = min (d[i + j + 1], d[i] + curr);
            }
    cout << d[n - 1];
    return 0;
}


Measured memory_bytes: 1024
#include<stdio.h>
int main()
{
	int r,d,x[10],i=0;
	scanf("%d%d%d",&r,&d,&x[0]);
	for(i=0;i<10;i++)
	{
		x[i+1]=(r*x[i]-d);
		printf("%d\n",x[i+1]);
	}
	return 0;
}


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

int main() {
    int N; cin >> N;
    vector<int> A(N, 0);
    vector<bool> flag(N, false);
    for (int i = 0; i < N; i++) cin >> A[i];
    int s = 1, ans = 0, cnt = 0;
    while(true) {
        if (flag[s - 1]) {
            ans = -1; break;
        }
        if (s == 2) {
            ans = cnt; break;
        }
        flag[s - 1] = true;
        cnt++, s = A[s - 1];
    }
    cout << ans << endl;
}


Measured memory_bytes: 3720
from heapq import heappush, heappop

N, M = map(int, input().split())
AtoB = [[] for _ in range(M+1)]
for i in range(N):
    A, B = map(int, input().split())
    if A > M:
        continue
    AtoB[A].append(B)

# A が小さい順に増えていく
result = 0
que = [] # ヒープ
for Bs in AtoB:
    for B in Bs:
        heappush(que, -B) # Python3 のヒープはデフォルトで小さい順
    if que:
        result -= heappop(que)
print(result)

Measured memory_bytes: 24536
// #pragma GCC target("avx2")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
using namespace std;
const long long mod=1000000007;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-10;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 3121000;
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
long long Comb(int a,int b){
 	if(a<b||b<0)return 0;
 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
}
void init_C(int n){
	fact[0]=finv[0]=inv[1]=1;
	for(int i=2;i<n;i++){
		inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
	}
	for(int i=1;i<n;i++){
		fact[i]=fact[i-1]*i%mod;
		finv[i]=finv[i-1]*inv[i]%mod;
	}
}
long long pw(long long a,long long b){
	if(a<0LL)return 0;
	if(b<0LL)return 0;
	long long ret=1;
	while(b){
		if(b%2)ret=ret*a%mod;
		a=a*a%mod;
		b/=2;
	}
	return ret;
}
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
double ABS(double a){return max(a,-a);}
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
// ここから編集しろ
int w[110];
int v[110];
long long dp[110000];
int main(){
	int a,b;scanf("%d%d",&a,&b);
	for(int i=0;i<a;i++)scanf("%d%d",w+i,v+i);
	for(int i=0;i<a;i++){
		for(int j=b;j>=0;j--){
			if(j+w[i]<=b)dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i]);
		}
	}
	long long ret=0;
	for(int i=0;i<=b;i++)ret=max(dp[i],ret);
	printf("%lld\n",ret);
}

Measured memory_bytes: 896
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>

using namespace std;


int main()	{
  int x, a, b;
  cin >> x >> a >> b;
  if (abs(x - a) < abs(x - b))	{
    cout << "A" << endl;
  }
  else	{
    cout << "B" << endl;
  }
  return 0;
}

Measured memory_bytes: 3588
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
using namespace std;

#define rep(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define SZ(x) ((int) (x).size())
typedef long long ll;

const int N = 40 + 5;
const int INF = ~0U >> 1;
int dp[N][10*N][10*N]; // i: 1-i packages, j: j grams of A, k: k grams of B
int x[N], y[N], c[N];
int main(){
	int n, a, b;
	cin >> n >> a >> b;
	rep(i,0,n) {
		cin >> x[i] >> y[i] >> c[i];
	}
	rep(i,0,n+1) rep(j,0,10*n+1) rep(k,0,10*n+1) dp[i][j][k] = INF;
	dp[0][0][0] = 0;
	rep(i,0,n) {
		rep(j,0,10*n+1) {
			rep(k,0,10*n+1) {
				if(dp[i][j][k] == INF) continue;
				dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k]);
				dp[i+1][j+x[i]][k+y[i]] = min(dp[i+1][j+x[i]][k+y[i]], dp[i][j][k] + c[i]);

			}
		}
	}
	int ans = INF;
	rep(i,1,10*n+1) rep(j,1,10*n+1) {
		if(i*b == j*a) ans = min(ans, dp[n][i][j]);
		//cout << i <<" " << j <<" " << dp[n][i][j] << endl;
	}
	cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}


Measured memory_bytes: 32896
#include <iostream>
using namespace std;
int main(){
  int w,n;
  cin >> w >> n;
  int d[w+1];
  for(int i = 1 ; i <= w ; i++ )d[i] = i;
  for(int i = 1 ; i <= n ; i++ ){
    int a,b;
    char c;
    cin >> a >> c >> b;
    swap(d[a],d[b]);
  }
  for(int i = 1 ; i <= w ; i++ )cout << d[i] << endl;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(v) v.begin(),v.end()

using namespace std;
typedef long long ll;




int main()
{
    int N,M;
    cin >> N >> M;
    
    vector<int> a;
    
    for (int i = 1; i*i <= M; i++) {
        if(M%i==0){
            a.push_back(i);
            if(i!=M/i) a.push_back(M/i);
        }
    }
    sort(all(a));
    
    int ans = 0;
    for (int i = 0; i < int(a.size()); i++) {
        if(M/a[i] >= N) ans = max(ans, a[i]);
    }
    cout << ans << endl;
    
    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    string S,T;
    cin >> S >> T;
    int flag=0;
    for(int i=0;i<S.length();i++){
        flag=0;
        for(int j=0;j<S.length();j++){
            if(S[(i+j)%S.length()]!=T[j]){flag++; break;}
        }
        if(flag==0){cout << "Yes" << endl; return 0;}
    }
    cout << "No" << endl;
}

Measured memory_bytes: 256
# coding: utf-8
count=0
def check(now,v,n):
    global count
    v[now]=1
    if sum(v)==n:
        count+=1
    else:
        for i in range(n):
            if v[i]==0 and g[now][i]==1:
                check(i,v[:],n)

n,m=map(int,input().split())
g=[[0 for i in range(n)]for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    g[a-1][b-1]=1
    g[b-1][a-1]=1
check(0,[0 for i in range(n)],n)
print(count)

Measured memory_bytes: 3064
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
n, x = M()
l = LI()
wa = 0
ans = 1
for i in l:
    wa += i
    if wa <= x:
        ans += 1
    else:
        break
print(ans)

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

signed main() {
    string S;
    cin >> S;

    int a = stoi(S.substr(0, 2));
    int b = stoi(S.substr(2));

    if (a <= 12 && a > 0) {
        if (b <= 12 && b > 0) {
            cout << "AMBIGUOUS";
        }
        else {
            cout << "MMYY";
        }
    }
    else if (b <= 12 && b > 0) {
        if (a <= 12 && a > 0) {
            cout << "AMBIGUOUS";
        }
        else {
            cout << "YYMM";
        }
    }
    else {
        cout << "NA";
    }
    cout << endl;
}


Measured memory_bytes: 256
n=int(input())
a=list(map(int,input().split()))
b=[2]*n

for i in range(n):
	# if n-i in yakusu(n):
		cnt=0
		for k in range(n//(n-i)):
			if b[(n-i)*(k+1)-1]==1:
				cnt+=1
		if cnt%2==a[n-i-1]:
			b[n-i-1]=0
		else:
			b[n-i-1]=1
	# else:
	# 	b[n-i-1]=a[n-i-1]
ans_1=0
ans_2=[]
for i in range(n):
	if b[i]==1:
		ans_1+=1
		ans_2.append(i+1)
print(ans_1)
print(*ans_2)




Measured memory_bytes: 14136