akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;

int ADD(int x, int y) {
  int res = x + y;
  if (res >= MOD) {
    res -= MOD;
  }
  if (res < 0) {
    res += MOD;
  }
  return res;
}

int MUL(int x, int y) {
  return 1ll * x * y % MOD;
}

int N, X[3];

map<vector<int>, int> states;

void GenState(int sum, vector<int> &a) {
  if (sum >= 0) {
    states[a] = 1;
  }
  if (sum < 0) {
    return;
  }
  for (int i = 1; i <= 10; i++) {
    a.emplace_back(i);
    GenState(sum - i, a);
    a.pop_back();
  }
}

void DelState(int cur, int sum, vector<int> &a) {
  if (cur == 3) {
    states.erase(a);
    return;
  }
  if (sum == X[cur]) {
    return DelState(cur + 1, 0, a);
  }
  if (sum > X[cur]) {
    return;
  }
  for (int i = 1; i <= 10; i++) {
    a.emplace_back(i);
    DelState(cur, sum + i, a);
    a.pop_back();
  }
}

vector<int> trans[1 << 17]; // Precompute DP transitions

void AddTransition(vector<int> state) {
  int cur = states[state];
  vector<int> save;

  for (int i = 1; i <= 10; i++) {
    state.emplace_back(i);
    reverse(begin(state), end(state));
    
    int sum = 0;
    for (auto &j : state) {
      sum += j;
    }

    while (sum > X[0] + X[1] + X[2]) {
      save.emplace_back(state.back());
      sum -= state.back();
      state.pop_back();
    }

    reverse(begin(state), end(state));
    auto it = states.find(state);

    reverse(begin(state), end(state));
    while (!save.empty()) {
      state.emplace_back(save.back());
      save.pop_back();
    }
    reverse(begin(state), end(state));

    state.pop_back();
    if (it == end(states)) {
      continue;
    }
    trans[cur].emplace_back(it->second);
  }
}

int dp[2][1 << 17];

void Calc() {
  for (int i = 0; i <= (int) states.size(); i++) {
    dp[N & 1][i] = 1;
  }
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 0; j <= (int) states.size(); j++) {
      dp[i & 1][j] = 0;
      for (auto &nxt : trans[j]) {
        dp[i & 1][j] = ADD(dp[i & 1][j], dp[(i + 1) & 1][nxt]);
      }
    }
  }
}

int main() {
  cin >> N >> X[0] >> X[1] >> X[2];
  
  vector<int> a;
  
  // Generate DP states
  GenState(X[0] + X[1] + X[2], a);
  DelState(0, 0, a);

  int cnt = 1;
  for (auto &i : states) {
    i.second = cnt++;
  }

  for (auto &i : states) {
    AddTransition(i.first);
  }

  Calc();

  int ans = 1;
  for (int i = 0; i < N; i++) {
    ans = MUL(ans, 10);
  }

  ans = ADD(ans, -dp[0][states[vector<int>()]]);

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

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

using namespace std;

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

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

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

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

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    int n = s.size();
    for (int i = 0; i < n; i++) cout << 'x';
}
/*

*/


Measured memory_bytes: 888
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True


def main():
    N, K = MI()
    p = LI()
    ans = 0

    p.sort()

    for i in range(K):
        ans += p[i]

    print(ans)

if __name__ == '__main__':
    main()


Measured memory_bytes: 68208
from collections import deque
import sys


def input():
    return sys.stdin.readline().strip()


q = deque(input())
Q = int(input())
flipped = False
for _ in range(Q):
    q_t, *q_b = input().split()
    if q_t == "1":
        flipped = not flipped
    else:
        F, C = q_b
        if (F == "2" and not flipped) or (F == "1" and flipped):
            q.append(C)
        else:
            q.appendleft(C)
q = list(q)
if flipped:
    print("".join(q[::-1]))
else:
    print("".join(q))



Measured memory_bytes: 13836
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
#if Local
            var sw = new Stopwatch();
            sw.Start();
            Console.SetIn(new StreamReader("input.in"));
#endif

            var solver = new Solver();
            solver.Solve();
#if Local
            Console.WriteLine($"{sw.ElapsedMilliseconds}ms");
#endif
        }
    }

    class Solver
    {
        public void Solve()
        {
            var N = long.Parse(Console.ReadLine());
            var S = 0L;
            foreach (var c in N.ToString())
            {
                S += long.Parse(c.ToString());
            }

            Console.WriteLine(N % S == 0 ? "Yes" : "No");
        }
    }
}


Measured memory_bytes: 11092
s = input()

while s!="":
    if s[-5:]=="dream" or s[-5:]=="erase":
        s = s[:-5]
    elif s[-6:]=="eraser":
        s = s[:-6]
    elif s[-7:]=="dreamer":
        s = s[:-7]
    else:
        print("NO")
        exit()
print("YES")


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

int main(){
	int r, g, b;
	cin >> r >> g >> b;

	if((10*g+b)%4 == 0) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}

Measured memory_bytes: 256
N, K = map(int, input().split())
m = 10**9 + 7
n = N


# nの階乗をmod mで返す
def mod_factorial(n, m):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= m
    return r


invs = [0] * (n + 1)
invs[1] = 1
for i in range(2, n + 1):
    invs[i] = m - invs[m % i] * (m // i) % m


# n1とn2のmod での階乗の逆元を返す関数
def mod_inv_factorial2(n1, n2, m):
    n = max(n1, n2)
    r1 = r2 = 1
    for i in range(1, n+1):
        r1 *= invs[i]
        r1 %= m
        if i == min(n1, n2):
            r2 = r1
    return r1*r2 % m


def solver(i):
    return mod_factorial(N-K+1, m)*mod_factorial(K-1, m)*mod_inv_factorial2(i, N-K+1-i, m)*mod_inv_factorial2(i-1, K-i, m)


for i in range(1, K+1):
    if N-K+1-i >= 0:
        print(solver(i) % m)
    else:
        print(0)




Measured memory_bytes: 42992
#include<iostream>

using namespace std;

int main()
{
    int k;
    cin>>k;
    if(k%2==0 || k%5==0)
	{
        cout << -1 << endl;
        return 0;
    }
    
    int v=0;
    for(int i=1;;i++)
	{
        v=(v*10+7)%k; // Ai (mod k) = v
        if(!v)
		{
            cout<< i <<endl;
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 3620
interface Main{static void main(String[]$){var s=new java.util.Scanner(System.in);long n=s.nextInt(),a=n++*n++*n--/6,m=n;while(m-->2){long u=s.nextInt(),v=s.nextInt();a-=(u<v?u:v)*n-u*v;}System.out.print(a);}}

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

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

ll modinv(ll x, ll mod){
    return modpow(x, mod-2, mod);
}

int main(){
    ll n, a, b; cin >> n >> a >> b;
    ll ans;
    ans = (modpow(2, n, MOD) - 1) % MOD;
    ll bunshi = 1, bunvo = 1;
    for(int i = 0; i < a; i++){
        bunshi = bunshi * (n - i) % MOD;
        bunvo = bunvo * (i + 1) % MOD;
    }
    ans = (ans - bunshi * modinv(bunvo, MOD) % MOD + MOD) % MOD;
    bunshi = bunvo = 1;
    for(int i = 0; i < b; i++){
        bunshi = bunshi * (n - i) % MOD;
        bunvo = bunvo * (i + 1) % MOD;
    }
    ans = (ans - bunshi * modinv(bunvo, MOD) % MOD + MOD) % MOD;
    cout << ans << endl;
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

    public class Program
    {
        public static void Main(string[] args)
        {
            new B().test();
        }
    }
    public class B
    {
        public void test()
        {
            var NK = CommonRead.ReadIntL();
            var pN = CommonRead.ReadLongL().OrderBy(x =>x).ToList();
            long exec = 0;

            // k種類
            for (int tmp =0; tmp <NK[1]; tmp++)
            {
                exec += pN[tmp];
            }

            Console.WriteLine(exec);

        }
    }

static class CommonRead
{
    public static string ReadStr()
    {
        return Console.ReadLine();
    }
    public static string[] ReadStrA()
    {
        return ReadStr().Split(' ');
    }
    public static List<string> ReadStrL()
    {
        return ReadStrA().ToList();
    }
    public static int ReadInt()
    {
        return Convert.ToInt32(ReadStr());
    }
    public static int[] ReadIntA()
    {
        return ReadStrA().Select(s => Convert.ToInt32(s)).ToArray();
    }
    public static List<int> ReadIntL()
    {
        return ReadStrA().Select(s => Convert.ToInt32(s)).ToList();
    }
    public static long ReadLong()
    {
        return Convert.ToInt64(ReadStr());
    }
    public static long[] ReadLongA()
    {
        return ReadStrA().Select(s => Convert.ToInt64(s)).ToArray();
    }
    public static List<long> ReadLongL()
    {
        return ReadStrA().Select(s => Convert.ToInt64(s)).ToList();
    }

    public static decimal ReadDecimal()
    {
        return Convert.ToDecimal(ReadStr());
    }
    public static decimal[] ReadDecimalA()
    {
        return ReadStrA().Select(s => Convert.ToDecimal(s)).ToArray();
    }
    public static List<decimal> ReadDecimalL()
    {
        return ReadStrA().Select(s => Convert.ToDecimal(s)).ToList();
    }

    public static double ReadDouble()
    {
        return Convert.ToDouble(ReadStr());
    }
    public static double[] ReadDoubleA()
    {
        return ReadStrA().Select(s => Convert.ToDouble(s)).ToArray();
    }
    public static List<double> ReadDoubleL()
    {
        return ReadStrA().Select(s => Convert.ToDouble(s)).ToList();
    }

}


Measured memory_bytes: 19900
e1, e2, e3, e4 = map(int, input().split())
if e1 == e2 and e3 == e4:
    print("yes")
elif e1 == e3 and e2 == e4:
    print("yes")
elif e1 == e4 and e2 == e3:
    print("yes")
else:
    print("no")
    


Measured memory_bytes: 5596
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(a)  int((a).size())
#define pb  push_back
#define eb  emplace_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define repC3(vari,varj,vark,n)  for(int vari=0;vari<(n)-2;++vari)for(int varj=vari+1;varj<(n)-1;++varj)for(int vark=varj+1;vark<(n);++vark)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
using namespace std;
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }


int main() {
    int B,C,D,N,M,max;
    ll ans;
    cin>>N>>M;
    vector <ll> A(N);
    rep(i,N){
        cin>>A[i];
    }

    if(N==1){
        rep(i,M){
            if(A[0]<=1){
                Out("0");
                return 0;
            }
            A[0]/=2;
        }
        Out(A[0]);
        return 0;
    }
    for(int j=0;0<M;j++){
 
        sort(RALL(A));
 
        max=A[0];
        for(int i=0;(i<N&&0<M);i++){
            if(A[i]<(max/2))break;
            A[i]/=2;
 
            M--;
        }
    }
    ans=0;
    rep(i,N){
        ans+=A[i];
    }
    Out(ans);
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>

std::map<std::string, int> memo;

using namespace std;

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

  int n;

  cin >> n;
  for(int i = 0; i < n; i++){
    std::string s;
    cin >> s;
    memo[s]++;
  }
  int maxv;
  for(const auto& x : memo){
    int v = x.second;
    if(v > maxv) maxv = v;
  }
  for(auto it = memo.begin(); it != memo.end(); it++){
    if(it->second == maxv){
      cout << it->first << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 23808
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL

int main() {
	ll		a,b,c,h,t,i,j,k,l,m,n,x,y;
	ll		ans = INF;
	string	s;
	cin >> n;
	vector<ll>	w(n+1);
	for(i=1;i<=n;i++) {
		cin >> a;
		w[i] = w[i-1] + a;
	}
	for(t=1;t<n;t++) {
		a = w[t];
		b = w[n] - w[t];
		ans = min(ans , abs(a-b));
	}

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


Measured memory_bytes: 3608
S = input()

ans = [0 for _ in range(len(S))]
temp = -2
Rtem = [0, 0]
Ltem = [0, 0]

for i in range(len(S)) :
    if i == temp + 1:
        continue
    if i == len(S) - 1:
        Ltem[i%2] += 1
        ans[temp] += Ltem[temp%2]
        ans[temp+1] += Ltem[(temp+1)%2]
    if S[i] == "R" and S[i+1] == "L" :
        ans[i] += 1 + Rtem[i%2]
        ans[i+1] += 1 + Rtem[(i+1)%2]
        Rtem = [0, 0]
        if temp >= 0 :
            ans[temp] += Ltem[temp%2]
            ans[temp+1] += Ltem[(temp+1)%2]
            Ltem = [0, 0]
        temp = i
    else :
        if S[i] == "R" :
            Rtem[i%2] += 1
        else :
            Ltem[i%2] += 1

print(*ans, sep=" ")


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

void main_()
{
    int64_t N;
    vector<int64_t> As;
    cin >> N;
    copy_n(istream_iterator<int64_t>(cin), N, back_inserter(As));

    auto odd_nums = accumulate(
        begin(As), end(As), 1L,
        [](auto acc, auto n) {
            return acc * (((n % 2) == 0) ? 2 : 1);
        });

    cout << static_cast<int64_t>(pow(3, N)) - odd_nums << endl;
}
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);
    main_();
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
int n,k=1,tr[N][2];
ll l;
char s[N];
ll lowbit(ll x){return x&(-x);}
ll dfs(int x,int t){
    if(x==0)return lowbit(l-t+1);
    return dfs(tr[x][0],t+1)^dfs(tr[x][1],t+1);
}
int main(){
    scanf("%d%lld",&n,&l);
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        int p=1;
        for(int j=1;s[j];j++){
            if(tr[p][s[j]-'0']==0)tr[p][s[j]-'0']=++k;
            p=tr[p][s[j]-'0'];
        }
    }
    if(dfs(1,0)==0)puts("Bob");
    else puts("Alice");
    return 0;
}

Measured memory_bytes: 9164
#include<iostream>

#include<vector>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<set>
#include<queue>


using namespace std;
#define int long long



signed main()
{
      int n,k,d;
      cin>>n>>k>>d;
      n=abs(n);
      int q=n/d;
      int r=n%d;
      if(k==q)
        cout<<r<<endl;
      else if(k>q)
        {
            if((q%2)^(k%2))
                cout<<d-r<<endl;
            else
                cout<<r<<endl;
        }
      else
        cout<<n-k*d<<endl;
      return 0;

}


Measured memory_bytes: 3644
#include <bits/stdc++.h>
#define f(i,j,k) for(int i=j;i<k;i++)
using namespace std;
int gcd(long long s,long long t){
    if(s%t==0){
        return t;
    }
    else{
        return gcd(t,s%t);
    }
}
int main(){
    long long a,b,c,d;
    cin>>a>>b>>c>>d;
    long long e=c*d/gcd(d,c);
    long long ac=(a-1)/c,ad=(a-1)/d,ae=(a-1)/e;
    long long bc=b/c,bd=b/d,be=b/e;
    long long ans=(b-a+1)-(bc-ac)-(bd-ad)+(be-ae);
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 3636
#include <algorithm>
#include <iostream>

#define SIZE 51
#define INF  5000

int n;
std::string s;
int count[SIZE+1][26];

int main() {
    std::cin >> n;
    for (int i=0; i<n; ++i) {
        std::cin >> s;
        for (int j=0; j<s.size(); ++j) {
            ++count[i][s[j] - 'a'];
        }
    }
    for (int j=0; j<26; ++j) {
        count[SIZE][j] = INF;
    }
    for (int i=0; i<n; ++i) {
        for (int j=0; j<26; ++j) {
            count[SIZE][j] = std::min(count[SIZE][j], count[i][j]);
        }
    }

    for (int i=0; i<26; ++i) {
        char x = 'a' + i;
        for (int j=0; j<count[SIZE][i]; ++j) {
            std::cout << x;
        }
    }
    std::cout << std::endl;
    return 0;
}

Measured memory_bytes: 256
def f(n):
    return n / 2 if n % 2 == 0 else 3 * n + 1

s = set()
i = 1
a = int(input())
while True:
    if a in s:
        print(i)
        break

    s.add(a)
    a = f(a)
    i += 1 

Measured memory_bytes: 3060
import heapq
from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    _ = int(input().strip())
    A_l = [-int(i) for i in input().strip().split()]
    heapq.heapify(A_l)

    used = deque()
    ans = 0
    # 初回と２回目は固定行動
    m1 = -1 * heapq.heappop(A_l)
    used.append(m1)
    m2 = -1 * heapq.heappop(A_l)
    used.append(m2)
    used.append(m2)
    ans += used.popleft()

    while A_l:
        m = -1 * heapq.heappop(A_l)
        used.append(m)
        used.append(m)
        ans += used.popleft()
    return ans


if __name__ == "__main__":
    print(main())


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

typedef vector<int> poly;

const int P = 998244353;
int n, fac[405], ifac[405], inv[405], A[405], B[405];
poly dp[2][405];

poly &operator+=(poly &a, const poly &b) {
	if (a.size() < b.size()) a.resize(b.size());
	for (int i = 0; i < b.size(); ++i)
		a[i] = (a[i] + b[i]) % P;
	return a;
}
poly &operator-=(poly &a, const poly &b) {
	if (a.size() < b.size()) a.resize(b.size());
	for (int i = 0; i < b.size(); ++i)
		a[i] = (a[i] + P - b[i]) % P;
	return a;
}
poly operator*(const poly &a, const poly &b) {
	poly res(a.size() + b.size() - 1);
	for (int i = 0; i < a.size(); ++i)
		for (int j = 0; j < b.size(); ++j)
			res[i + j] = (res[i + j] + 1ll * a[i] * b[j]) % P;
	return res;
}

int main() {
	scanf("%d", &n);
	for (int i = fac[0] = 1; i <= 400; ++i)
		fac[i] = 1ll * fac[i - 1] * i % P;
	inv[1] = 1;
	for (int i = 2; i <= 400; ++i)
		inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
	ifac[0] = 1;
	for (int i = 1; i <= 400; ++i)
		ifac[i] = 1ll * ifac[i - 1] * inv[i] % P;
	dp[0][0] = {P - 1};
	int S = 0;
	for (int i = 1; i <= n; ++i)
		scanf("%d%d", A + i, B + i), S += A[i];
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j <= 400; ++j)
			dp[i & 1][j].clear();
		poly tmp(B[i]);
		int pw = 1;
		for (int j = 0; j < B[i]; ++j) {
			tmp[j] = 1ll * pw * ifac[j] % P;
			pw = 1ll * pw * A[i] % P * inv[S] % P;
		}
		for (int j = 0; j <= 400; ++j)
			if (!dp[i - 1 & 1][j].empty()) {
				dp[i & 1][j] -= dp[i - 1 & 1][j] * tmp;
				dp[i & 1][j + A[i]] += dp[i - 1 & 1][j];
			}
	}
	int ans = 0;
	for (int j = 0; j < (int)dp[n & 1][0].size(); ++j)
		ans = (ans + 1ll * dp[n & 1][0][j] * fac[j]) % P;
	for (int t = 1; t < S; ++t)
		for (int pw = 1, j = 0, now = 1ll * S * inv[S - t] % P; j < (int)dp[n & 1][t].size(); ++j) {
			ans = (ans + 1ll * dp[n & 1][t][j] * fac[j] % P * pw % P * now) % P;
			pw = 1ll * pw * S % P * inv[t] % P;
			now = 1ll * now * t % P * inv[S - t] % P;
		}
	printf("%d\n", ans);
	return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
 
using ll=long long;
 
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
const ll MOD = 998244353;


int main() {
    ll n; 
    cin >> n;
    ll b = n;
    for (ll i = 2; i*i <= n; ++i) {
        if (n%i == 0) b = n/i; 
    }
    int keta = 0;
    while (b) {
        b /= 10;
        keta++;
    }
    cout << keta << endl;
    return 0;
}

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

int A[100003] , K; long long mn , mx;

int main(){
	cin >> K; for(int i = 1 ; i <= K ; ++i) cin >> A[i];
	if(A[K] != 2){cout << -1; return 0;}
	mn = 2; mx = 3;
	for(int i = K ; i ; --i){
		long long p = (mn + A[i] - 1) / A[i] , q = mx / A[i];
		if(p > q){cout << -1; return 0;}
		mn = p * A[i]; mx = q * A[i] + A[i] - 1;
	}
	cout << mn << ' ' << mx; return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
    int n;
    cin >> n;
    vector<int> dp(n+10, INT_MAX);
    dp[0] = 0;
    for(int i = 1; i <= n; ++i){
        dp[i] = 1 + dp[i-1];

        for(int power = 6; power <= i; power*=6){
            dp[i] = min(dp[i], dp[i-power]+1);
        }

        for(int power = 9; power <= i; power*=9){
            dp[i] = min(dp[i], dp[i-power]+1);
        }

    }
    cout << dp[n] << endl;
    return 0;
}


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

struct Node{
	string s;
	int num,x,id;
}node[105];
int n,ans[105];

bool cmp(Node p,Node q){
	if(p.s!=q.s)return p.s<q.s;
	return p.num>q.num;
}

int main(void){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>node[i].s>>node[i].num;
		node[i].x=i;
	}
	sort(node+1,node+1+n,cmp);
	for(int i=1;i<=n;i++){
		printf("%d\n",node[i].x);
	}
}

Measured memory_bytes: 256
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  n = int(input())
  Hs = list(map(int, input().split()))
  count = 1
  max= Hs[0]
  for i in range(1, n):
    if max <= Hs[i]:
      count += 1
      max = Hs[i]
  print(count)
  
if __name__=='__main__':
  main()



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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define IN freopen("dichromatic.in","r",stdin)
#define OUT freopen("dichromatic.out","w",stdout)
#define FOR(i,a,b) for(i=a ; i<=b ; i++)
#define DBG pf("Hi\n")
#define INF 1000000000
#define i64 long long int
#define eps (1e-8)
#define xx first
#define yy second
#define sq(x)  ((x)*(x))

using namespace __gnu_pbds;
using namespace std ;

#define maxn 200005
#define mod 1000000007LL

typedef  long long int T ;

i64 dp[5005][5005] , cnt[5005][5005] , a[5005] ;

int main()
{
    int n ;
    i64 A , B ;

    scanf("%d %lld %lld",&n,&A,&B) ;

    for(int i=1 ; i<=n ; i++) scanf("%lld",&a[i]) ;

    for(int i=1 ; i<=n ; i++)
    {
        for(int j=1 ; j<=n ; j++) cnt[i][j] = cnt[i-1][j] ;
        for(int j=a[i]-1 ; j>=1 ; j--) cnt[i][j]++ ;
    }

    for(int i=1 ; i<=n ; i++)
    {
        for(int j=1 ; j<=n ; j++)
        {
            if( a[i] > j ) dp[i][j] = dp[i-1][j] ;
            else dp[i][j] = min( B+dp[i-1][ j ] , (cnt[i][a[i]]-cnt[i][j])*A + dp[i-1][ a[i] ] ) ;
        }
    }

    printf("%lld\n",dp[n][n]) ;

    return 0 ;
}


Measured memory_bytes: 391552
#include <iostream>
using namespace std;
 
int main() {
 int x, y;
 cin >> x >> y;
 int z = x + y;
 if (z % 3 == 0 || x % 3 == 0 || y % 3 == 0)
   cout << "Possible";
 else
   cout << "Impossible";
}

Measured memory_bytes: 3604
//C - All Green

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
  int D,G; //大問題数、目標点
  std::cin >> D >> G;  
  int p[D],c[D];
  for(int i = 0; i < D; i++) std::cin >> p[i] >> c[i];
  
  //入力確認
  //std::cout << "D:\t" << D << std::endl;
  //std::cout << "G:\t" << D << std::endl;
  //for(int i = 0; i < D; i++) std::cout << p[i] << "\t" << c[i] << std::endl;
  //入力確認

  long int res = D*100; //とりあえず大きな数値を入れる。あとで比較するため。
  for(int bit = 0; bit < (1<<D); bit++) //bit全探索、2^D通りを調べる
  {
    long int sumScore = 0;
    long int cnt = 0;
    for(int i = 0; i < D; i++) //大問ごとに調べる。
    {
      if(bit & (1<<i)) //フィルターみたいな役割
      {
        sumScore += (i + 1)*100*p[i] + c[i]; //大問のうち全部解いた場合のスコア
        cnt += p[i];
      }
    }      
    //目標点Gを超えた場合
    if (sumScore >= G) res = std::min(res, cnt); //問題数が少ない方を格納していく
    //目標点Gに到達していない場合
    else
    {
      for (int i = D - 1; i >= 0; i--)
      //for (int i = 0; i < D; i++)
      {
        if (bit & (1<<i)) continue;
        
        //完答せずに途中までに
        for (int j = 0; j < p[i]; j++)
        {
          if (sumScore >= G) break;
          sumScore += 100 * (i+1);
          ++cnt;
        }
      }
      
      res = std::min(res, cnt);
    }
  }

  std::cout << res;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
long long s[400][400]; 
const int Shurui=26;//今回は26種類
int Date;//今回は1年間なので365日
long long Seigenjikan=1800000;//実行時間
 
int main() {
  clock_t start = clock();
  cin >>Date;
  vector<int>c(400,0);
  vector<int>last(400,0);//前回の日にち
  vector<int>t(400,0);//開催する順番
  for(int i=0;i<Date;i++)t[i]=i%Shurui;//とりあえずの値
  long long wa=0;
  for(int i=0;i<Shurui;i++){
    cin>>c[i];
    wa+=c[i];
  }
  for(int i=0;i<Date;i++){
    for(int j=0;j<Shurui;j++){
      cin>>s[i][j];
    }
  }
  long long kotae=0;
  long long hikukazu=0;
  long long atai=0;
  for(int i=0;i<Date;i++){
    atai=0;
    atai=s[i][t[i]];
    hikukazu+=wa-c[t[i]]*(i+1-last[t[i]]);
    last[t[i]]=i+1;
    kotae+=atai-hikukazu;
  }  
  clock_t end = clock();
  long long time = end - start;
  while(time<Seigenjikan){
    int d=rand()%Date;
    int q=rand()%Shurui;
    int old=t[d];
    t[d]=q;
    long long new_kotae=0;
    hikukazu=0;
    for(int i=0;i<Date;i++){
      atai=0;
      atai=s[i][t[i]];
      hikukazu+=wa-c[t[i]]*(i+1-last[t[i]]);
      last[t[i]]=i+1;
      new_kotae+=atai-hikukazu;
    }
    if(new_kotae<kotae){
      t[d]=old;
    }else{
      kotae=new_kotae;
    }
    end = clock();
    time = end - start;
  }
  for(int i=0;i<Date;i++){
    cout<<t[i]+1<<endl;
  }
  return 0;
}

Measured memory_bytes: 4772
a = [list(map(int, input().split())) for _ in range(3)]
N=int(input())
b = [int(input()) for _ in range(N)]


cnt=0

for i in range(N):
    for j in range(3):
        for k in range(3):
            if b[i] == a[j][k]:
                a[j][k] =1000
                if sum(a[j]) ==3000 or a[0][k]+a[1][k]+a[2][k]==3000 or a[0][0]+a[1][1]+a[2][2]==3000 or a[0][2]+a[1][1]+a[2][0]==3000:
                    cnt +=1
print('Yes' if cnt>0 else 'No')

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

int main() {
  ll a,b,c; cin>>a>>b>>c;
  ll ans=0;
  
  if (a+b+1>=c) ans=b+c;
  else ans=a+2*b+1;

  cout<<ans<<endl;
}



Measured memory_bytes: 256
import sys
from collections import deque, defaultdict
def input() : return sys.stdin.readline().strip()

def main():
    N = int(input())
    edges = [tuple(map(int, input().split())) for _ in range(N-1)]
    to = [[] for i in range(N)]

    for edge in edges:
        a, b = edge
        a -= 1
        b -= 1
        to[a].append(b)
        to[b].append(a)
    
    colors = defaultdict(int)
    def bfs():
        q = deque() # (index, parent color)
        q.append((0, 0))
        while len(q):
            now, par_col = q.popleft()
            color = 0
            for v in to[now]:
                x, y = sorted([v, now])
                if colors[(x, y)]:
                    continue
                else:
                    color += 1
                    if color == par_col:
                        color += 1

                    colors[(x, y)] = color
                    q.append((v, color))
    bfs()

    ans = max(colors.values())
    print(ans)
    
    for edge in edges:
        x, y = edge
        x -= 1
        y -= 1
        print(colors[(x, y)])

                


        



if __name__ == "__main__":
    main()

Measured memory_bytes: 50368
//inlclude前用define 
#define _USE_MATH_DEFINES

//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdio>
#include<type_traits>
#include<numeric>
#include<limits>
#include<iomanip>
#include<set>

using namespace std;

//typedef
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;

//define
#define INF 100000000
#define NUM 1000000007

int func(int n);

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

	int sum = func(n);

	if (n%sum == 0) {
		cout << "Yes" << endl;
	}else {
		cout << "No" << endl;
	}
	return 0;
}

int func(int n) {
	int sum = 0;
	if (n < 10) {
		return n;
	}
	else {
		while (1) {
			sum += n % 10;
			if (n < 10)break;
			n /= 10;
		};
		return sum;
	}
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		int N = n / 111;
		if(n % 111 != 0)
			N += 1;
		
		System.out.println(N * 111);
	}

}


Measured memory_bytes: 21972
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=1e9;
const ll INFS=1e18;
const int MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};

int main(){
  ll a;
  double b;
  cin>>a>>b;
  ll ib=b*100+0.5;
  a*=ib;
  a/=100;
  cout<<a<<endl;
}

Measured memory_bytes: 3720
import heapq

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


def fsp(n, coins):
    paid = [False] * (n + 1)
    paid[0] = True
    queue = list((1, v) for v in coins)
    heapq.heapify(queue)
    while queue:
        num, value = heapq.heappop(queue)
        if paid[value]:
            continue
        paid[value] = True
        for c in coins:
            new_value = value + c
            # Because of link cost is constant 1, it is reliably shortest-path.
            if new_value == n:
                return num + 1
            elif new_value > n or paid[new_value]:
                continue
            heapq.heappush(queue, (num + 1, value + c))


print(fsp(n, coins))

Measured memory_bytes: 36656
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            string inputData = Console.ReadLine();
            List<int> numList = new List<int>();
            foreach (string tmp in inputData.Split(' ')) {
                numList.Add(int.Parse(tmp));
            }

            bool flag = false;
            for (int loop = 0; loop < numList.Count; loop++) {
                int cnt = 0;
                for (int loop2 = 0; loop2 < numList.Count; loop2++)
                {
                    if (loop == loop2)
                    {
                        continue;
                    }
                    if (numList[loop] == numList[loop2])
                    {
                        cnt++;
                    }
                }
                if(cnt == 1)
                {
                    flag = true;
                    break;
                }
            }
            if (flag)
            {
                Console.WriteLine("Yes");
            }
            else { 
                Console.WriteLine("No");
            }
        }
    }
}


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

int main(){
    string S;
    cin >> S;
    bool ans = true;
    if (S.size() % 2 == 0){
      for (int i = 0; i < (S.size()); i += 2){
          if (S.at(i) != 'h' || S.at(i + 1) != 'i'){
              ans = false;
          }
      }
    }
    else {
        ans = false;
    }
    cout << (ans ? "Yes" : "No") << endl;
}

Measured memory_bytes: 256
# include <iostream>
using namespace std;
int main(){
	int a;
	cin>>a;
	if(a>=1200){
		if(a>=2800){
			cout<<"AGC";
			return 0;
		}
		else{
			cout<<"ARC";
			return 0;
		}
	}
	else{
		cout<<"ABC";
		return 0;
	}
}

Measured memory_bytes: 256
/*---- enazuma11-------  */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define inc(a,n) sort(a,a+n)
#define dec(a,n) sort(a,a+n,greater<int>() )
#define F first
#define S second
#define M 1000000007
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	ll a,b;
	cin>>a>>b;
	ll c=b/2;
	cout<<a+c;
	
	return 0;
}

Measured memory_bytes: 256
def m():
    N = int(input())
    B = list(map(int, input().split()))
    ans = []
    while B:
        f = True
        for i in reversed(range(len(B))):
            if B[i] == i+1:
                B.pop(i)
                ans.append(i+1)
                f = False
                break
        if f: return print('-1')
    ans.reverse()
    for i in ans:
        print(i)
    return


m()


Measured memory_bytes: 38512
#include <bits/stdc++.h>
using namespace std;
  
int main() {
  string s;
  cin>>s;
  while(s.size()>3) {
    int c[3]={},j;
    for(int i=0;i<s.size();i++)c[s[i]-'A']++;
    vector<int>v;
    for(int i=0;i<s.size()-2;i++)if(s.substr(i,3)=="ABC")v.push_back(i);
    if(!v.size())break;
    for(j=0;j<3;j++)if(c[j]==v.size())break;
    if(j>=3) break;
    string t=s.substr(0,v[0]);
    for(int i=0;i<v.size()-1;i++)t+=(char)('A'+j),t+=s.substr(v[i]+3,v[i+1]-(v[i]+3));
    t+=(char)('A'+j),t+=s.substr(v[v.size()-1]+3);
    s=t;
  }
  cout<<(s=="ABC"?"Yes":"No")<<endl;
  return 0;
}


Measured memory_bytes: 3232
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

int n, m;
bool can[1000][1000], can2[1000][1000];
vector<vi> e;

void rec(bool *v, int c){
	v[c] = 1;
	for(int i : e[c]) if(!v[i]) rec(v, i);
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	cin >> n >> m;
	e.resize(n);
	map<pi, int> id;
	rep(i, m){
		int a, b; cin >> a >> b; a--; b--;
		e[a].pb(b);
		id[pi(a, b)] = i;
	}
	rep(i, n) rec(can[i], i);
	rep(i, n) rep(it, 2){
		bool v[1000] = {};
		v[i] = 1;
		for(int j : e[i]){
			if(v[j]) can2[i][j] = 1;
			rec(v, j);
		}
		reverse(all(e[i]));
	}
	const char *res[] = {"same", "diff"};
	vi ans(m);
	for(auto i : id){
		int a = i.first.first, b = i.first.second;
		ans[i.second] = can2[a][b] ^ can[b][a];
		
	}
	rep(i, m) cout << res[ans[i]] << endl;
	return 0;
}

Measured memory_bytes: 17920
#include<stdio.h>
#include<math.h>
int main()
{
    int n,s[1005],i;
    double a,x;
    for(;scanf("%d",&n),n;)
    {
        for(i=a=0;i<n;a+=s[i++])
            scanf("%d",&s[i]);
        a/=n;
        for(i=x=0;i<n;i++)
            x+=(s[i]-a)*(s[i]-a);
        printf("%.8f\n",sqrt((double)x/n));
    }
    return 0;
}


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

int main() {
  int a, b, c, d, N;
  cin >> N;
  a = N /1000;
  d = N % 1000 %100 %10;
  c = (N %1000 %100 -d) / 10 ;
  b = (N %1000 -d -c) / 100;
  
  if(a == b && a == c){
    cout << "Yes";
  }else if(b == c && c == d){
    cout << "Yes";
  }else{
    cout << "No";
  }
}


Measured memory_bytes: 3624
// 2014/04/27 Tazoe

#include <iostream>
using namespace std;

int main()
{
	bool is_prime[1000000];

	is_prime[0] = is_prime[1] = false;
	for(int i=2; i<1000000; i++){
		is_prime[i] = true;
	}

	for(int i=2; i*i<1000000; i++){
		if(is_prime[i]){
			for(int j=i*i; j<1000000; j+=i){
				is_prime[j] = false;
			}
		}
	}

	int n_prime[1000000];

	int cnt = 0;
	for(int i=0; i<1000000; i++){
		if(is_prime[i]){
			cnt++;
		}
		n_prime[i] = cnt;
	}

	while(true){
		int n;
		cin >> n;

		if(cin.eof())
			break;

		cout << n_prime[n] << endl;
	}

	return 0;
}

Measured memory_bytes: 6044
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
#define eps 1e-9
#define LL long long
using namespace std;
double a[110][110];
int flag[110][110];

int dbcmp(double a)
{
    if(fabs(a) < eps)   return 0;
    return a > 0 ? 1:-1;
}

int main()
{
    int n,i,j,t,tag;
    double min,max;
    while(cin>>n && n)
    {
        memset(flag,0,sizeof(flag));
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
              cin>>a[i][j];
        for(i=0;i<n;i++)
        {
            min=a[i][0];
            for(j=1;j<n;j++)
            {
                if(dbcmp(a[i][j] - min) < 0)
                {
                    min=a[i][j];
                }
            }
            for(j=0;j<n;j++)
            {
                if(dbcmp(a[i][j] - min) == 0)
                flag[i][j]++;
            }
        }
        for(j=0;j<n;j++)
        {
            max=a[0][j];
            for(i=1;i<n;i++)
            {
                if(dbcmp(a[i][j] - max) > 0)
                {
                    max=a[i][j];
                }
            }
            for(i=0;i<n;i++)
            {
                if(dbcmp(a[i][j] - max) == 0)
                flag[i][j]++;
            }
        }
        tag=1;
        for(i=0;i<n;i++)
        {
           for(j=0;j<n;j++)
           {
               if(flag[i][j]==2)
               {
                  // if(tag==1)
                      cout<<a[i][j];
                  // else
                   //   cout<<" "<<a[i][j];
                   tag=0;
                   break;
               }
           }
           if(tag == 0) break;
        }
        if(tag==1)
           cout<<0;
        cout<<endl;
    }
    return 0;
}

Measured memory_bytes: 0
import java.io.*;
import java.util.*;

class Node {
    public int p, q, r, b, parent;
    Node(int p, int q, int r, int b) {
        this.p = p;
        this.q = q;
        this.r = r;
        this.b = b;
        this.parent = -1;
    }
}

public class Main {
    Scanner sc;
    int n;
    int root;
    Node[] tree;

    long gcd(long a, long b) {
        if (a > b) {
            long tmp = a;
            a = b;
            b = tmp;
        }
        if (a == 0) return b;
        return gcd(b % a, a);
    }

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

    Main() {
        sc = new Scanner(System.in);
    }

    long lightest(int index) {
        int p = tree[index].p;
        int q = tree[index].q;
        int r = tree[index].r;
        int b = tree[index].b;
        if (r == -1 && b == -1) {
            return (p + q) / gcd(p, q);
        } else if (r == -1) {
            long b_weight = lcm(lightest(b), p);
            long r_weight = b_weight * q / p;
            return r_weight + b_weight;
        } else if (b == -1) {
            long r_weight = lcm(lightest(r), q);
            long b_weight = r_weight * p / q;
            return r_weight + b_weight;
        }
        long lcm_rb = lcm(lightest(r)*p, lightest(b)*q);
        long r_weight = lcm_rb / p;
        long b_weight = lcm_rb / q;
        return r_weight + b_weight;
    }

    boolean init() {
        n = sc.nextInt();
        if (n == 0) return false;

        tree = new Node[n];
        for (int i = 0; i < n; i++) {
            int p = sc.nextInt();
            int q = sc.nextInt();
            int r = sc.nextInt();
            int b = sc.nextInt();
            long gcd_pq = gcd(p, q);
            p /= gcd_pq;
            q /= gcd_pq;
            tree[i] = new Node(p, q, r-1, b-1);  // 0-originに
        }

        // 親を探す
        for (int i = 0; i < n; i++) {
            if (tree[i].r >= 0) tree[tree[i].r].parent = i;
            if (tree[i].b >= 0) tree[tree[i].b].parent = i;
        }

        // rootを探す
        for (int i = 0; i < n; i++) 
            if (tree[i].parent == -1) {
                root = i;
                break;
            }

        return true;
    }

    void debug() {
        for (int i = 0; i < n; i++) {
            System.out.printf("%d %d %d %d %d\n", tree[i].p, tree[i].q, tree[i].r, tree[i].b, tree[i].parent);
        }
    }

    void run() {
        while (init()) {
            System.out.printf("%d\n", lightest(root));
        }
    }

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

Measured memory_bytes: 12000
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int NOEL(ll x){
  cout<<x<<endl;
  return 0;
}
int main(){
  ll k,a,b;cin>>k>>a>>b;
  if(k+1<=a)return NOEL(k+1);
  if(b-a>2){
    k-=(a-1);
    ll ans=a;
    if(k%2==1)ans++;
    ans+=k/2*(b-a);
    return NOEL(ans);
  }
  else{
    return NOEL(k+1);
  }
  return 0;
}


Measured memory_bytes: 256
N = int(input())
A = [int(x) for x in input().split()]

inc = False
money = 1000
kabu = 0
for i in range(N - 1):
    if (not inc) and A[i] < A[i + 1]:
        # 買う
        kabu = money // A[i]
        money -= kabu * A[i]
        inc = True
    elif inc and A[i] > A[i + 1]:
        money += kabu * A[i]
        kabu = 0
        inc = False

money += kabu * A[N - 1]
print(money)

Measured memory_bytes: 9096
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#include <unistd.h>
#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
#define vecin(a) rep(i,a.size())cin >> a[i]
#define vecout(a) rep(i,a.size()){cout << a[i];cout << (i == a.size() - 1 ? "\n":" ");}
#define mp make_pair
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll inf = 2147483647;
const ll INF = 1LL << 60;
const dou pi = 3.14159265358;
const ll mod = 1000000007LL;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b){if(a < b){a = b; return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a > b){a = b; return true;}return false;}
template<class T> inline bool change(T a,T b){if(a > b){swap(a,b);return true;}return false;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a / gcd(a,b) * b;
}

//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}
const ll cmax = 1000000;
ll fac[cmax], finv[cmax], inv[cmax];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < cmax; i++){
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

// 二項係数計算
ll nCk(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/

//    cout << fixed << setprecision(15);

ll sec(string S){
    ll h = (S[0] - '0') * 10 + S[1] - '0';
    ll m = (S[3] - '0') * 10 + S[4] - '0';
    ll s = (S[6] - '0') * 10 + S[7] - '0';
    return h * 3600 + m * 60 + s;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    while(true){
        cin >> N;
        if(N == 0)break;
        vector<ll> TL(86400);
        string from,to;
        rep(i,N){
            cin >> from >> to;
            TL[sec(from)]++;
            TL[sec(to)]--;
        }
        ll ans = 0;
        rep(i,86399LL){
            TL[i + 1] += TL[i];
        }
        rep(i,86400LL){
            chmax(ans,TL[i]);
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 3876
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class Problem
{
    static void Main()
    {
        //--------------------------------input--------------------------------//

        var str = Console.ReadLine().ToArray();

        //--------------------------------solve--------------------------------//

        int ans = 0;
        int now = 0;
        bool tobe0 = true;
        for(int i = 0 ; i < str.Length ; i++)
            if(tobe0)
            {
                if(str[i] == '1')
                    now++;
                tobe0 = false;
            }
            else
            {
                if(str[i] == '0')
                    now++;
                tobe0 = true;
            }
        ans = now;
        now = 0;
        tobe0 = false;
        for(int i = 0 ; i < str.Length ; i++)
            if(tobe0)
            {
                if(str[i] == '1')
                    now++;
                tobe0 = false;
            }
            else
            {
                if(str[i] == '0')
                    now++;
                tobe0 = true;
            }
        Console.WriteLine( ans >= now ? now : ans );
    }
}

Measured memory_bytes: 13268