akhauriyash/Code-Regression huggingface dataset:
macro_rules! get_tuple {
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };

    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
            get_tuple!($($t),*)
        ).collect::<Vec<_>>()
    };
}
macro_rules! get_vec {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_| get_vec!($t)).collect::<Vec<_>>()
    };
}

fn main() {
    let (_, k) = get_tuple!(usize, usize);
    let mut v_p = get_vec!(usize);

    v_p.sort();
    let ans: usize = v_p.into_iter().take(k).sum();

    print!("{}", ans);

}


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

void solve(int N, int K, vector<int> P, vector<int> C)
{
    int64_t res = -(1e9 + 1);
    for (int pos_first = 0; pos_first < N; pos_first++)
    {
        int pos;

        int len_cycle = 0;
        int64_t score_cycle = 0;
        int64_t mx_score_cycle = -(1e9 + 1);

        vector<bool> visited(N);
        pos = pos_first;
        while (!visited.at(pos))
        {
            visited.at(pos) = true;
            len_cycle++;
            pos = P.at(pos);
            score_cycle += C.at(pos);
            mx_score_cycle = max(mx_score_cycle, score_cycle);
        }

        int mx_loop = K / len_cycle;
        int rem_steps = K % len_cycle;
        int64_t mx_score_rem = -(1e9 + 1);

        int64_t s = 0;
        pos = pos_first;
        for (int step = 0; step < rem_steps; step++)
        {
            pos = P.at(pos);
            s += C.at(pos);
            mx_score_rem = max(mx_score_rem, s);
        }

        res = max(res, mx_score_rem);
        if (mx_loop > 0)
        {
            res = max(res, mx_score_cycle);
            res = max(res, mx_score_cycle + score_cycle * (mx_loop - 1));
            res = max(res, mx_score_rem + score_cycle * mx_loop);
        }
    }
    cout << res << endl;
}

int main()
{
    int N;
    cin >> N;
    int K;
    cin >> K;
    vector<int> P(N);
    for (int i = 0; i < N; i++)
    {
        cin >> P.at(i);
        P.at(i)--;
    }
    vector<int> C(N);
    for (int i = 0; i < N; i++)
    {
        cin >> C.at(i);
    }
    solve(N, K, move(P), move(C));
    return 0;
}


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

int main()
{
  int x;
  int happiness = 0;
  cin >> x;
  happiness += (x / 500) * 1000;
  x -= happiness / 2;
  happiness += (x / 5) * 5;
  cout << happiness;
  return 0;
}

Measured memory_bytes: 256
import sys
from collections import Counter

input = sys.stdin.readline


def main():
    N = int(input())
    S = [""] * N
    for i in range(N):
        S[i] = input().rstrip()

    c = Counter(S)
    max_v = max(c.values())
    ans = [k for k, v in c.items() if v == max_v]
    ans.sort()

    print(*ans, sep="\n")


if __name__ == "__main__":
    main()


Measured memory_bytes: 35928
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        int k = sc.nextInt();
        int[][] c = new int[h][w];
        for (int i = 0; i < h; i++) {
            char[] x = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if(x[j] == '#'){
                    c[i][j]++;
                }
            }
        }
        long ans = 0L;
        for (int i = 0; i < 1<<h; i++) {
            for (int j = 0; j < 1<<w; j++) {
                int tmp = 0;
                for (int a = 0; a < h; a++) {
                    if((i >> a & 1) == 1) continue;
                    for (int l = 0; l < w; l++) {
                        if((j >> l & 1) == 1)continue;
                        tmp += c[a][l];
                    }
                }
                if(tmp == k)ans++;
            }
        }
        System.out.println(ans);
        sc.close();

    }

}


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

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair<int, int> pii;

#define F(i, l, r) for(int i = (l); i < (r); i++)
#define E(i, l, r) for(int i = (l); i <= (r); i++)
#define DF(i, l, r) for(int i = (l); i >= (r); i--)
#define I(x, a) for(auto x : (a))
#define mp make_pair
#define X first
#define Y second
#define clean(x) memset((x), 0, sizeof(x))
#define ret return
#define cont continue
#define brk break
#define ins insert
#define all(x) (x).begin(),(x).end()
#define sync ios_base::sync_with_stdio(false);cin.tie(0)
#define pb push_back
#define y1 fjfg

const int maxn = 3e2 + 2;

string s[maxn];
int n;

inline int pos(int i){
	return i < 0 ? i + n : i;
}

int main(){
	sync;
	cin >> n;
	F(i, 0, n)
		cin >> s[i];
	int ans = 0;
	F(sum, 0, 2*n - 1){
		int A = 0, B = sum%n;
		bool good = true;
		F(i, 0, n){
			F(j, 0, n){
				if(s[pos(i - A)][pos(j - B)] != s[pos(j - A)][pos(i - B)]){good = false; break;}
			}
		}
		if(!good)cont;
		F(a, 0, sum + 1){
			if(a < n && sum - a < n)ans++;
		}
	}
	cout << ans;
}


Measured memory_bytes: 384
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const double PI=3.14159265358979323846;
const in MOD = 1000000007;
const in INF=1e18+7;
const int inf=1e9+7;
using P=pair<int,int>;
vector<int> dx={0,1,-1,0};
vector<int> dy={1,0,0,-1};


int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
    int t;
    cin>>t;
    in n,m,a,b;
    rep(uehe,t){
        cin>>n>>m>>a>>b;
        in ans=atcoder::floor_sum(n,m,a,b);
        cout<<ans<<endl;
    }
}/*
./problem.exe
*/

Measured memory_bytes: 3696
/* ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ */
/* ☻ Name : Souvik Mahato  ☻ */
/* ☻ Nick : WIZARDO        ☻ */
/* ☻ Institution : IIEST   ☻ */
/* ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ ☻ */

#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define in(v) (v).begin(),(v).end()
#define rin(v) (v).rbegin(),(v).rend()
#define inf 1e9+7



int solvius()
{
    int n,k;
    cin>>n>>k;
    vector<int> h(n),cost(n,inf);
    for(int &x : h)
        cin>>x;
    cost[0]=0;
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<=i+k && j<n;j++)
            cost[j]=min(cost[j],cost[i]+abs(h[j]-h[i]));
    }
    cout<<cost[n-1];
    return 0;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);    
	ll t=1;
   // cin>>t;
	while(t--)
		solvius();
    return 0;
}

Measured memory_bytes: 3956
n, x = map(int, input().split())

a = [1]
p = [1]
for i in range(n):
    a.append(a[-1] * 2 + 3)
    p.append(p[-1] * 2 + 1)

def f(n, x):
    if n == 0:
        if x <= 0:
            return 0
        else:
            return 1
    elif x <= a[n - 1] + 1:
        return f(n - 1, x - 1)
    else:
        return p[n - 1] + 1 + f(n - 1, x - a[n - 1] - 2)

print(f(n, x))

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

int main() {
  int N;
  cin >> N;
  vector<int> P(N),Q(N),V(N);
  for(int i=0;i<N;i++) cin >> P[i];
  for(int i=0;i<N;i++) cin >> Q[i];
  for(int i=0;i<N;i++) V[i] = i+1;
  
  int a,b,k=0;
  do{
    int count=0;
    for(int i=0;i<N;i++){
      if(P[i]==V[i]) count++;
    }
    if(count==N) a=k;
    count=0;
    for(int i=0;i<N;i++){
      if(Q[i]==V[i]) count++;
    }
    if(count==N) b=k;
    k++;
  }while(next_permutation(V.begin(),V.end()));
  
  cout << abs(a-b) << endl;
  return 0;
}


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

int main(){
  int k,n,a[60];
  cin >> n >> k ;
  for(int i=0;i<n;i++){
    cin >> a[i];
  }
sort(a,a+n,greater<int>());
int ans=0;
for(int i=0;i<k;i++){
ans += a[i];}
cout<<ans<<endl;

}

Measured memory_bytes: 256
#include <iostream>
#include <cassert>
#define REP(i,l,n) for(int i=l;i<n;++i)
#define rep(i,n) REP(i,0,n)
int ans;
int Q,n;
using namespace std;
int main() {
  cin >> Q;
  rep(i,Q){
    cin >> n;
    ans = 0;
    while(n > 9){
      ans++;
      int max = 0,div = 1;
      rep(j,5){
        div *= 10;
        int post = (n/div)*(n%div);
        if(post>max) max = post;
      }
      n = max;
    }
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int N,A,B;
  cin>>N>>A>>B;
  vector<char> a(N);
  int allp=0;
  int bp=0;
  for(int i=0;i<N;i++){
    cin>>a.at(i);
  }
  for(int i=0;i<N;i++){
    if(a.at(i)=='a'&&allp<A+B){
      cout<<"Yes"<<endl;
      allp++;
      continue;
    }
    if(a.at(i)=='a'&&allp>=A+B){
      cout<<"No"<<endl;
      continue;
    }
    if(a.at(i)=='b'&&(allp<A+B&&bp<B)){
      cout<<"Yes"<<endl;
      allp++;
      bp++;
      continue;
    }
    if(a.at(i)=='b'&&(allp>=A+B||bp>=B)){
      cout<<"No"<<endl;
      continue;
    }
    if(a.at(i)=='c'){
      cout<<"No"<<endl;
      continue;
    }
  }
}

Measured memory_bytes: 3676
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

int main() {
    int n, h, w;
    cin >> n >> h >> w;

    cout << (n - h + 1) * (n - w + 1) << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 1000000007
#define PI 3.141592653589793

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

    int l = 0, r = 0;
    while(m--){
        int a;
        cin >> a;
        if(a < x) l++;
        else r++;
    }
    cout << min(l, r) << endl;
}


Measured memory_bytes: 256
N, A, B = map(int, input().split())

sum=0
Allsum=0
partsum=0

for i in range(N+1):
    if i<10:
        sum=i
    else:
        partsum=0
        for j in list(str(i)):
            partsum+=int(j)
        sum=partsum
    if sum>=A and sum<=B:
        Allsum+=i
print(Allsum)





Measured memory_bytes: 3060
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long
#define space putchar(' ')
#define endl putchar('\n')
#define debug puts("------------------------")
#define F(i,x,n) for(int i=x;i<=n;++i)
#define F_(i,x,n) for(int i=x;i>=n;--i)
using namespace std;
inline void read(int &a) {a=0;int c=getchar(),b=1; while(c>'9'||c<'0') {if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9') a=(a<<3)+(a<<1)+c-48,c=getchar();a*=b; }
inline int  Rem() {int a=0,c=getchar(),b=1; while(c>'9'||c<'0') {if(c=='-')b=-1;c=getchar();} while(c>='0'&&c<='9') a=(a<<3)+(a<<1)+c-48,c=getchar();return a*=b; }
inline void write(int x) {if(x>9)write(x/10);putchar('0'+x%10);}
inline void W(int x) {if(x<0){putchar('-'),x=-x;}write(x);}
/**/
ll n,ans=0x7f7f7f7f7f7f7f7fLL;
/**/
int main()
{
    // freopen("test.in","r",stdin);
    // freopen("test.out","w",stdout);
    cin>>n;
    for(ll i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            ll x=n/i;
            ans=min(ans,x+i-2);
        }
    }
    cout<<ans;
}

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

typedef long long ll;

int main()
{
    ll i,j;
    ll n;
    cin >> n;
    vector<vector<int>> graph(n);
    vector<pair<int, int>> vp;
    for(i = 0;i < n-1;++i){
        int a,b;
        cin >> a >> b;
        --a;    --b;
        graph.at(a).push_back(b);
        graph.at(b).push_back(a);
        vp.emplace_back(a,b);
    }
    int ans = graph.at(0).size();
    vector<bool> visit(n,false);
    visit.at(0) = true;
    map<pair<int,int>, int> um;
    vector<unordered_set<int>> us(n);
    queue<int> que;
    que.push(0);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        if(ans < graph.at(v).size()){
            ans = graph.at(v).size();
        }
        int color = 1;
        for(i = 0;i < graph.at(v).size();++i){
            int u = graph.at(v).at(i);
            if(visit.at(u)){
                continue;
            }
            visit.at(u) = true;
            while(true){
                if(us.at(v).find(color) == us.at(v).end()){
                    us.at(v).insert(color);
                    us.at(u).insert(color);
                    break;
                }else{
                    ++color;
                }
            }
            um[make_pair(u,v)] = color;
            um[make_pair(v,u)] = color;
            ++color;
            que.push(u);
        }
    }
    cout << ans << endl;
    for(auto p: vp){
        cout << um.at(p) << endl;
    }
    return 0;
}

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

#define INF 1001001001
#define LINF 1001001001001001001
#define MOD 1000000007
#define MOD2 998244353

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

#define rep(i,n) for(int i=0,_i=(n);i<_i;++i)
#define rep1(i,a,b) for(int a_=(a),b_=(b),i=a_;i<b_;++i)
#define repr(i,n) for(int _i=(n),i=_i;i>0;--i)

#define db(x) cerr<<#x<<" = "<<x<<" ";
#define db2(x,y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<") ";
#define db3(x,y,z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = ("<<x<<", "<<y<<", "<<z<<") ";
#define ln cout<<endl;

#define all(a)  (a).begin(),(a).end()
#define dig(n)  to_string(n).length()

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define se second
#define fi first

typedef long long ll;

typedef pair<int,int> P;
typedef pair<int,P> iP;
typedef pair<P,P> PP;

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

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){

    bool flag=false;
    ll ans=0,sum=0;

    int n,m;
    cin>>n;
    vector<int> a(n);

    map<int,int> pi;
    map<int,int> pj;
    set<int> app;

    rep(i,n)cin>>a[i];

    rep(i,n)pi[(i+1) + a[i] ]++;
    rep(j,n)pj[(j+1) - a[j] ]++;

    for(auto pii:pi)app.insert(pii.fi);
    for(auto pji:pj)app.insert(pji.fi);

    for(int a:app){
        ll j = pj[a];
        ll i = pi[a];
        ans += i*j;
    }

    cout<<ans<<endl;

    //cout <<fixed<<setprecision(16)<< << endl;

    //if(flag)cout << "Yes" <<endl;
    //else cout << "No" <<endl;

    return 0;
}


Measured memory_bytes: 55192
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7

ans=1
zero=3
memo=[0]*n

for i in range(n):
  if a[i]==0:
    ans*=zero
    zero-=1
    memo[0]+=1
  else:
    ans*=(memo[a[i]-1]-memo[a[i]])
    ans%=mod
    memo[a[i]]+=1
print(ans)

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

using namespace std;

typedef long long ll;

ll N,K;
ll x[50];
ll y[50];
ll MIN_S = 5e18;

int main(){
    cin >> N >> K;
    for(int i=0;i<N;i++){
        cin >> x[i] >> y[i];
    }
    for(int i=0;i<N;i++){
        for(int j=i+1;j<N;j++){
            for(int a=0;a<N;a++){
                for(int b=a+1;b<N;b++){
                    ll minx = min(x[i],x[j]);
                    ll maxx = max(x[i],x[j]);
                    ll miny = min(y[a],y[b]);
                    ll maxy = max(y[a],y[b]);
                    int count = 0;
                    for(int k=0;k<N;k++){
                        if(x[k] >= minx && x[k] <= maxx && y[k] >= miny && y[k] <= maxy){
                            count++;
                        }
                    }
                    if(count < K) continue;
                    ll s = (maxx-minx)*(maxy-miny);
                    MIN_S = min(s,MIN_S);
                }
            }
        }
    }
    cout << MIN_S << endl;
    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = list(map(int,input().split()))
A.reverse()
acsum = [0]
for i in range(N):
    acsum.append(acsum[i] + A[i])
ans = 0
for i in range(N):
    ans += A[i] * acsum[i] % (10 ** 9 + 7)
    ans %= (10 ** 9 + 7)
print(ans)


Measured memory_bytes: 106884
#include <iostream>
using namespace std;
int main() {
  int n, a[100000], sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum ^= a[i];
  }
  if (sum == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
#define REP(i,n) for(ll i = 0;i < (ll)n;i++)
#define REPD(i,n) for(ll i = (ll)n - 1;i >= 0;i--)
#define ALL(x) (x).begin(),(x).end()
#define FILLl(n,first_dimension_size,value) fill((ll*)n,(ll*)(n+first_dimension_size),value)
#define FILL(n,first_dimension_size,value) fill((int*)n,(int*)(n+first_dimension_size),value)
#define INF (1 << 30)
#define MOD 1000000007
//int dy[4] = {-1,0,0,1};
//int dx[4] = {0,1,-1,0};


int main(){

  int n,C;
  cin >> n >> C;
  vector<vector<int>> D(C,vector<int>(C));
  REP(i,C)REP(j,C)cin >> D[i][j];
  vector<vector<int>> c(n,vector<int>(n));
  REP(i,n)REP(j,n)cin >> c[i][j];
  vector<vector<int>> v(3,vector<int>(C,0));
  REP(i,n){
    REP(j,n){
      v[(i+j)%3][c[i][j]-1]++;
    }
  }
  ll res = INF;
  REP(i,C){
    ll d1 = 0;
    REP(l,C)d1 += D[l][i]*v[0][l];
    REP(j,C){
      if(i == j)continue;
      ll d2 = 0;
      REP(l,C)d2 += D[l][j]*v[1][l];
      REP(k,C){
        if(i == k || j == k)continue;
        ll d3 = 0;
        REP(l,C)d3 += D[l][k]*v[2][l];
        res = min(res,d1+d2+d3);
      }
    }
  }
  cout << res << endl;

  

  return 0;
}


Measured memory_bytes: 1280
/*author* Priyanshu Shrivastav (from IIT Palakkad) *
 * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
 * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
 * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
 * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
When I wrote this, only God and I understood what I was doing
 ** * * * * * * * Now, only God knows * * * * * * */
#include         <bits/stdc++.h>
using namespace std;
#pragma GCC      optimize ("Ofast")
#pragma GCC      optimize ("unroll-loops")
#pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
#define PREC     cout.precision (10); cout << fixed
#define bg(x)    " [ " << #x << " : " << (x) << " ] "
#define x        first
#define y        second
using ll = long long;
using ff = long double;
using pii = pair<int,int>;

#define debug(args...) { \
/* WARNING : do NOT compile this debug func calls with following flags: // \
 * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
   stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
}
void err(istream_iterator<string> it) { it->empty();
   cerr << " (Line : " << __LINE__ << ")" << '\n';
}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << fixed << setprecision(15) << " [ " <<  *it << " : " << a  << " ] "<< ' ';
	err(++it, args...);
}

const int N = (int)2e5 + 10;
ll seg[4 * N];
ll dp[N];
int h[N], v[N];
int n;

void modify(int l, int r, int idx, int x, ll val) {
   if (l > idx || r < idx) return;
   if (l == r) {
      seg[x] = max(seg[x], val);
      return;
   }
   int mid = (l+r)/2;
   modify(l, mid, idx, x + x + 1, val);
   modify(mid + 1, r, idx, x + x + 2, val);
   seg[x] = max(seg[x + x + 1], seg[x + x + 2]);
}

ll query(int l, int r, int ql, int qr, int x) {
   if (r < ql || l > qr) return LLONG_MIN;
   if (l >= ql && r <= qr) return seg[x];
   int mid = (l+r)/2;
   ll left = query(l, mid, ql, qr, x + x + 1);
   ll right = query(mid + 1, r, ql, qr, x + x + 2);
   return max(left, right);
}

void solve() {
   for (int i = 0; i < 4 * n; ++i) seg[i] = 0;
   for (int i = 0; i <= n; ++i) dp[i] = 0;

   for (int i = 1; i <= n; ++i) dp[h[i]] = v[i];
   for (int i = 1; i <= n; ++i) {
      ll mx_val = query(1, n, 1, h[i]-1, 0);
      dp[h[i]] = max(dp[h[i]], mx_val + v[i]);
      modify(1, n, h[i], 0, dp[h[i]]);
   }

   ll ans = 0;
   for (int i = 1; i <= n; ++i) {
      ans = max(dp[i], ans);
   }
   cout << ans << '\n';
}

signed main() {
   IOS; PREC;
   cin >> n;
   for (int i = 1; i <= n; ++i) cin >> h[i];
   for (int i = 1; i <= n; ++i) cin >> v[i];
   solve();

   return EXIT_SUCCESS;
}


Measured memory_bytes: 9600
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <tuple>
#include <vector>

// lib/quo.hpp
#ifndef QUO_H
#define QUO_H

/*
a == tools:quo(a, b) * b + tools::mod(a, b)
0 <= tools::mod(a, b) < std::abs(b)
*/

namespace tools {
  template <typename T>
  constexpr T quo(const T lhs, const T rhs) {
    return lhs > 0 ? lhs / rhs : rhs > 0 ? -((-lhs - 1 + rhs) / rhs) : (-lhs - 1 + -rhs) / -rhs;
  }
}

#endif

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// lib/extgcd.hpp
#ifndef EXTGCD_HPP
#define EXTGCD_HPP


/*
usage:

std::tuple<int, int, int> result = tools::extgcd(4, 5);
int gcd = std::get<0>(result);
int x0 = std::get<1>(result);
int y0 = std::get<2>(result);
*/

namespace tools {
  template <typename T>
  std::tuple<T, T, T> extgcd(T prev_r, T r) {
    T prev_s = 1;
    T prev_t = 0;
    T s = 0;
    T t = 1;
    while (r != 0) {
      const T q = quo(prev_r, r);
      const T next_r = prev_r - q * r;
      prev_r = r;
      r = next_r;
      const T next_s = prev_s - q * s;
      prev_s = s;
      s = next_s;
      const T next_t = prev_t - q * t;
      prev_t = t;
      t = next_t;
    }
    return std::make_tuple(std::abs(prev_r), prev_s, prev_t);
  }
}

#endif

// main.cpp

int main() {
  i64 N, M;
  std::string S, T;
  std::cin >> N >> M >> S >> T;

  const i64 gcd = std::get<0>(tools::extgcd(N, M));
  const i64 lcm = N * M / gcd;

  for (i64 i = 0; i < gcd; ++i) {
    if (S[N / gcd * i] != T[M / gcd * i]) {
      goto FAILED;
    }
  }

  std::cout << lcm << std::endl;
  return 0;

FAILED:
  std::cout << -1 << std::endl;
  return 0;
}

Measured memory_bytes: 512
//レート924の人間が0完太陽☀でした
//その人間が解説ACコードを書いたので
//よろしければお読みください

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
 
using ll = long long;
using st = string;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define sz size()

/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/
 
 
signed main() {
  st s;
    cin>>s;
  ll k;
    cin>>k;
  ll ans=0;
  ll n=s.sz;
  ll count=1;
  ll head=0;
  ll end=0;
  FOR(i,1,n){
    
    if(s[i-1]==s[i]){
      count++;
    }
    
    else if(i==count){
      head=count;
      count=1;
    }
    
    else{
      ans+=count/2*k;
      count=1;
    }
    
    if(i==n-1){
      end=count;
    }  
  }

  if(count==n){
    ans=k*n/2;
  }
  
  else if(s[0]==s[n-1]){
    ans+=(head+end)/2*(k-1);
    ans+=head/2+end/2;
  }
  
  else{
    ans+=head/2*k;
    ans+=end/2*k;
  }
  
  cout<<ans<<endl;
  
}

Measured memory_bytes: 256
//author rhythmicankur
                                                                  //god_is_almighty
#pragma GCC optimize("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
#define f 		first
#define s 		second
#define ll 		int
#define int 		long long
#define ld 		long double
#define pb 		push_back
#define eb 		emplace_back
#define mk 		make_pair
#define vi 		vector<int>
#define pii 		pair<int,int>
#define vpi 		vector<pii>
#define REP(i,n) for (int i = 1; i <= n; i++)
#define trav(a, x)      for(auto& a : x)
#define all(x)          x.begin(), x.end()
#define fill(a,val)	memset(a,val,sizeof(a))
#define 	PI acos(-1)
#define coud(a,b) cout<<fixed << setprecision((b)) << (a)
#define M1 	998244353
#define M2 	1000000007
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;

template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); } 
#define test4(x,y,z,a) 		    cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<"  |  "<<#a<<": "<<a<<endl;
#define test1(x)                cerr<<#x<<": "<<x<<endl
#define test2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define test3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
ll power(ll a,ll b,ll m=M2){ll answer=1;while(b){if(b&1)answer=(answer*a)%m;b/=2;a=(a*a)%m;}return answer;}
using namespace std;
void ctrl(){cout<<"Control"<<endl;}
int make_num(string p){stringstream geek(p); int x = 0; geek >> x;return x;}
string make_str(int x){ostringstream str1; str1 << x; string geek = str1.str(); return geek;}
int binomialCoeff(int n, int k) 
{ 
    int C[n + 1][k + 1]; 
    int i, j; 
  
    // Caculate value of Binomial Coefficient 
    // in bottom up manner 
    for (i = 0; i <= n; i++) 
    { 
        for (j = 0; j <= min(i, k); j++) 
        { 
            // Base Cases 
            if (j == 0 || j == i) 
                C[i][j] = 1; 
  
            // Calculate value using previously 
            // stored values 
            else
                C[i][j] = C[i - 1][j - 1] + 
                          C[i - 1][j]; 
        } 
    } 
  
    return C[n][k]; 
} 
signed main()
{	ios::sync_with_stdio(0);
	cin.tie(0); 
	cin.exceptions(cin.failbit);
	int n,m;
    cin>>n>>m;
    int x = (n*(n-1))/2 + (m*(m-1))/2;
    cout<<x;
    

return 0;
}

Measured memory_bytes: 256
#include <iostream> 
#include <sstream> // istringstream
#include <string> // 文字列
#include <algorithm> // アルゴリズム
#include <cmath> // 
#include <vector> // vector
#include <set> // set
#include <map> // map

using namespace std;

int main()
{   
    // input sample
    int N; 
    cin >> N;
    // int A[N];
    // for (int i = 0; i < N; ++i) cin >> A[i]; 
    // string s = "";

    // output sample
    if (N >= 30) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    
    return 0;
}

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

const int maxn = 100010;
int n, K, x[maxn];

int main() {
    scanf("%d %d", &n, &K);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x[i]);
    }
    int ans = 1e9;
    for (int i = 1; i + K - 1 <= n; i++) {
        int l = x[i] >= 0 ? 0 : -x[i];
        int r = x[i + K - 1] <= 0 ? 0 : x[i + K - 1];
        ans = min(ans, l + r + min(l, r));
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
int A[101010];
int B[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N+1) cin>>A[i];
	ll ret=0;
	FOR(i,N) cin>>B[i];
	FOR(i,N+1) {
		if(i) {
			x=min(B[i-1],A[i]);
			B[i-1]-=x;
			A[i]-=x;
			ret+=x;
		}
		x=min(A[i],B[i]);
		A[i]-=x;
		B[i]-=x;
		ret+=x;
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 1024
// Ring.c
// 図のようなリング状の文字列 sの任意の位置から、時計回りに連続した文字をいくつか選んで、
// 文字列 pが作れるかを判定するプログラムを作成してください。
// 【入力】
// １行目に文字列 sが与えられます。
// ２行目に文字列 pが与えられます。
// 【出力】
// pが作れる場合は Yes と、作れない場合は No と１行に出力してください。
// 【制約】
// ・1 ≤ pの長さ ≤s の長さ ≤100
// ・文字列は英小文字からなる
// 【例】
// 入力：
// vanceknowledgetoad
// advance
// 出力：
// Yes
// 入力：
// vanceknowledgetoad
// advanced
// 出力：
// No
#include<stdio.h>
#include<string.h>

int main( void) {
  char s[101];
  char ss[201];
  char p[101];

  scanf( "%s %s", s, p);

  strcpy( ss, s);
  strcat( ss, s);

  if ( strstr( ss, p) == NULL) printf( "No\n");
  else printf( "Yes\n");

  return 0;
}



Measured memory_bytes: 2108
#include<iostream>
#include<algorithm>
#define rep(i, n) for (int i = 0; i < (n); ++i) 

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    int x, y;
    while (1) {
        cin>>x>>y;
        if (x==0&&y==0) break;
        if (x > y) swap(x, y);
        cout<<x<<" "<<y<<endl;
    }
}


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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int n,m,c;
int a[25][25], b[25];

int main(){
  scanf("%d%d%d",&n,&m,&c);
  REP(i,m)scanf("%d",b+i);
  REP(i,n)REP(j,m)scanf("%d",&a[i][j]);
  int ans = 0;
  REP(i,n){
    int f = c;
    REP(j,m)f += a[i][j] * b[j];
    if(f>0)ans++;
  }
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 256
#include <stdlib.h>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
using ll = long long;
 
int main(){
  string S;
  cin >> S;
  int size_S=S.size();
  if(size_S==1){
    cout << 1 << endl;
    return 0;
  }
  
  vector<vector<pair<int,string>>> DP(size_S,vector<pair<int,string>>(2));
  DP.at(0).at(0)=make_pair(1,S.at(0));
  DP.at(0).at(1)=make_pair(1,S.substr(0,2));
  DP.at(1).at(0).second=S.substr(1,1);
  DP.at(1).at(1).second=S.substr(1,2);
  if(DP.at(1).at(0).second!=DP.at(0).at(0).second){
    DP.at(1).at(0).first=2;
  }
  else DP.at(1).at(0).first=0;
  DP.at(1).at(1).first=DP.at(0).at(0).first+1;


  for(int i=2;i<size_S;++i){
    DP.at(i).at(0).second=S.substr(i,1);
    DP.at(i).at(1).second=S.substr(i,2);
    //1 char
    if(DP.at(i).at(0).second!=DP.at(i-1).at(0).second){
      DP.at(i).at(0).first=max(DP.at(i-1).at(0).first+1,DP.at(i-2).at(1).first+1);
    }
    else{
      DP.at(i).at(0).first=DP.at(i-2).at(1).first+1;
    }
    //2 char
    if(DP.at(i).at(1).second!=DP.at(i-2).at(1).second){
      DP.at(i).at(1).first=max(DP.at(i-1).at(0).first+1,DP.at(i-2).at(1).first+1);
    }
    else{
      DP.at(i).at(1).first=DP.at(i-1).at(0).first+1;
    }
  }

  cout << max(DP.at(size_S-1).at(0).first,DP.at(size_S-2).at(1).first) << endl;

  return 0;
}


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


void solve(long long N, std::vector<long long> a){
    ll ans = 0;
    REP(i, N){
        if(a.at(a.at(i) - 1) - 1 == i){
            ans++;
        }
    }
    cout << ans / 2 << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> a(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&a[i]);
    }
    solve(N, std::move(a));
    return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(8);
  int B=0;
  for (int i=0; i<N; i++){
    int a;
    cin >> a;
    if (a<3200) A.at(a/400)++;
    else B++;
  }
  int C=0;
  for (int i=0; i<8; i++){
    if (A.at(i)>0) C++;
  }
  if (C>0) cout << C << " " << B+C << endl;
  else cout << 1 << " " << B << endl;
  return 0;
}

Measured memory_bytes: 3628
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    private static boolean debug = false;
    private static boolean elapsed = false;

    private static PrintWriter _out = new PrintWriter(System.out);
    private static PrintWriter _err = new PrintWriter(System.err);

    private void solve(Scanner sc) {
        long A = sc.nextLong();
        long B = sc.nextLong();

        if (A == B) {
            _out.println(1);
            return;
        }

        long c = 0x4000000000000000L;
        while ((A & c) == (B & c)) {
            A = (A | c) ^ c;
            B = (B | c) ^ c;
            c >>>= 1;
        }
        long d = (c >>> 1);
        while ((B & d) != d) {
            d >>>= 1;
        }
        int r = Long.numberOfTrailingZeros(c) + 1;
        int k = Long.numberOfTrailingZeros(d) + 1;
        if (d == 0) {
            k = 0;
        }
        long sum = ((1L << r) - A);
        if ((1L << k) < A) {
            sum -= A - (1L << k);
        }
        _out.println(sum);
    }
    private static BigInteger C(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        for (long i = r; i > 1; --i) {
            res = res.divide(BigInteger.valueOf(i));
        }
        return res;
    }
    private static BigInteger P(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
    /*
     * 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
     * 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
     */
    public static void main(String[] args) {
        long S = System.currentTimeMillis();

        Scanner sc = new Scanner(System.in);
        new Main().solve(sc);
        _out.flush();

        long G = System.currentTimeMillis();
        if (elapsed) {
            _err.println((G - S) + "ms");
        }
        _err.flush();
    }
}

Measured memory_bytes: 23764
import Control.Monad
import Data.List as L
import Data.IntMap as M

primes :: [Int]
primes = takeWhile (<31623) $ 2:3:p
    where 
        p = 5:q 1 7 p
        q m s (p:ps) = [n|n<-[x+y|x<-[s,s+6..p*p-2],y<-[0,4]],gcd m (toInteger n)<2]++q (m*toInteger p) (p*p) ps

fac :: [Int]->M.IntMap Int->Int->M.IntMap Int
fac _ m 1 = m
fac [] m n = M.insert n 1 m
fac pa@(p:pr) m n
    |mod n p==0=fac pa (M.insertWith(+) p 1 m) (div n p)
    |otherwise =fac pr m n

invmod a p = invmod' a p p 0 1
    where invmod' a p q x y
            | amq==0 = mod (x+p) p
            | otherwise = invmod' q p amq (y-x * div a q) x
            where amq = mod a q

divmod a b p = mod (a * invmod b p) p

cmbmod n k p = L.foldl' (\a i->divmod (mod (a*(n-i+1)) p) i p) 1 [1..k]

main=do
    (n:m:_)<-L.map read.words<$>getLine::IO[Int]
    print $ sol n m
--
sol 1 _ = 1
sol _ 1 = 1
sol n m = L.foldl' (\a b -> (a * cmbmod (b+n-1) b 1000000007) `mod` 1000000007) 1 fm
    where fm = elems $ fac primes M.empty m

Measured memory_bytes: 1148
// C - HonestOrUnkind2
#include <bits/stdc++.h>
using namespace std;
#define rp(i,s,e) for(int i=(s);i<(e);++i)
using BITS = bitset<15>;

int N, A[15], X[15][15], Y[15][15];

bool check(BITS &bits){
	rp(i, 0, N)
		if(bits[N-1-i])
			rp(j, 0, A[i]) if(bits[N-1-X[i][j]] != Y[i][j]) return false;
	return true;
}

int main(){
	cin>>N;
	rp(i, 0, N){ cin>>A[i]; rp(j, 0, A[i]){ cin>>X[i][j]>>Y[i][j]; X[i][j]--; } }
	for(int bits=(1<<N)-1; bits>0; --bits){
		BITS b(bits); if(check(b)){ cout<<b.count()<<endl; return 0; }
	}
	puts("0");
}

Measured memory_bytes: 256
#include <iostream>
#define rep(S, L) for (int i = S; i < L; ++i)
using namespace std;

int main() {
  int n, m, X, Y;
  cin >> n >> m >> X >> Y;
  int x, y;
  int maxx = X, miny = Y;
  for (int i = 0; i < n; ++i) {
    cin >> x;
    maxx = max(maxx, x);
  }
  for (int i = 0; i < m; ++i) {
    cin >> y;
    miny = min(miny, y);
  }
  if (maxx + 1 <= miny) {
    cout << "No War" << endl;
  } else {
    cout << "War" << endl;
  }
  return 0;
}


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

int selectionSort(int A[], int N){
  int sw = 0, minj;
  for(int i = 0; i < N; i++){
    minj = i;
    for(int j = i; j < N; j++){
      if(A[j] < A[minj]) minj = j;
    }
  swap(A[i],A[minj]);
  //minj ?????´??°??????????????°???sw????????????????????????
  if ( i != minj ) sw++;
  }

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

  return sw;
}


int main(){
  int N,A[100],sw;
  cin >> N;
  for(int i = 0; i < N; i++) cin >> A[i];
  sw = selectionSort(A, N);
  cout << sw << endl;

  return 0;
}

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

int gcd(int a, int b) {
    if (a < b)
        swap(a, b);
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

int main() {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int ans = gcd(a[0], a[1]);
    for (int i = 2; i < n; i++) {
        if (ans == 1)
            break;
        ans = gcd(ans, a[i]);
    }
    cout << ans << endl;
    return 0;
}

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

#define ll long long
#define ar array

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

	int k, n;
	cin >> k >> n;
	int a[n];
	for(int i=0; i<n; ++i)
		cin >> a[i];
	int ans=a[n-1]-a[0];
	for(int i=1; i<n; ++i)
		ans=min(a[i-1]+k-a[i], ans);
	cout << ans;
}

Measured memory_bytes: 1024
package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	t, a := sc.nextInt(), sc.nextInt()
	for i := 1; i < N; i++ {
		nt, na := sc.nextInt(), sc.nextInt()
		m := max(ceil(t, nt), ceil(a, na))
		t = nt * m
		a = na * m
	}

	fmt.Fprintln(wtr, t+a)
	wtr.Flush()
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}


Measured memory_bytes: 640
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int n = sc.nextInt();
			int a = sc.nextInt() - 1;
			int b = sc.nextInt() - 1;
			int c = sc.nextInt() - 1;
			int d = sc.nextInt() - 1;
			String s = sc.next();
			char[] chs = s.toCharArray();

			char ROCK = '#';

			if (a == c && b == d) {
				System.out.println("Yes");
				return;
			}

			boolean isBeforeDoubleRock = false;
			for (int i = a + 1; i < c; i++) {
				if (chs[i] == ROCK) {
					if (isBeforeDoubleRock) {
						System.out.println("No");
						return;
					}
					isBeforeDoubleRock = true;
				} else {
					isBeforeDoubleRock = false;
				}
			}
			isBeforeDoubleRock = false;
			for (int i = b + 1; i < d; i++) {
				if (chs[i] == ROCK) {
					if (isBeforeDoubleRock) {
						System.out.println("No");
						return;
					}
					isBeforeDoubleRock = true;
				} else {
					isBeforeDoubleRock = false;
				}
			}

			// D < C
			if (d < c) {
				int spaceCount = 0;
				for (int i = b - 1; i <= d + 1; i++) {
					if (chs[i] != ROCK) {
						spaceCount++;
					} else {
						spaceCount = 0;
					}
					if (3 <= spaceCount) {
						System.out.println("Yes");
						return;
					}
				}
				System.out.println("No");
			} else {
				System.out.println("Yes");
			}

		}
	}
}


Measured memory_bytes: 27348
#include <bits/stdc++.h>
using namespace std;
#define popcnt(a) __builtin_popcount(a)
#define FastIO() ios::sync_with_stdio(false), cin.tie(0);
#define IO() freopen("palindrome.in","rw",stdin)
#define error(args...)                       \
{                                            \
    cerr<<"LINE "<<__LINE__;                 \
    string _s = #args;                       \
    replace(_s.begin(), _s.end(), ',', ' '); \
    stringstream _ss(_s);                    \
    istream_iterator<string> _it(_ss);       \
    err(_it, args);                          \
    cerr<<endl;                              \
}
void err(istream_iterator<string> it)
{
} 
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
  cerr<<' '<< *it << " = " << a ;
  err(++it, args...);
} 
typedef long long LL;
const LL MOD = 1e9 + 7;
const int N = 1e7+9, M = (1<<31), OO = 0x3f3f3f3f;
int n,k;
LL memo[N],h[N];
LL solve(int idx=0)
{
  if(idx+1>=n) return 0;
  LL &ret = memo[idx];
  if(~ret) return ret;
  ret = solve(idx+1)+abs(h[idx]-h[idx+1]);
  for(int i=2;i<=k;++i) if(idx+i<n) ret = min(ret,solve(idx+i)+abs(h[idx]-h[idx+i]));

  return ret;
}
int main()
{
  memset(memo,-1,sizeof memo);
  scanf("%d%d",&n,&k);
  for(int i=0;i<n;++i) scanf("%lld",h+i);
  printf("%lld",solve());
  #ifdef _LOCAL_DEFINE
  cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
  #endif
  return 0; 
}

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

using namespace std;

int main() {
    unsigned int n, l;
    cin >> n >> l;

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

    sort(vt.begin(), vt.end());
    for (int i = 0; i < n; ++i) {
        cout << vt[i];
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define TEST int t = 1; while(t--) solve()

void solve(){
    int n; cin>>n;
    cout<<ceil(n/2.0)<<endl;
}

signed main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); TEST; return 0;
}

Measured memory_bytes: 256
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        int N = int.Parse(Console.ReadLine());
        int[] l = new int[N];
        int[] r = new int[N];
        for(int i=0;i<N;i++){
            string[] str = Console.ReadLine().Split(' ');
            l[i] = int.Parse(str[0]);
            r[i] = int.Parse(str[1]);
        }
        AbsoluteSum Ab = new AbsoluteSum(N);
        Ab.Add(l[0]);
        for(int i=1;i<N;i++){
            Ab.Min(l[i]-r[i],r[i-1]-l[i-1]);
            Ab.Add(l[i]);
        }
        sb.Append(Ab.GetMin()+"\n");
    }
}
class AbsoluteSum{
    long minValue;
    Heap Lheap;
    long LS;
    Heap Rheap;
    long RS;
    public AbsoluteSum(int maxsize){
        minValue = 0;
        Lheap = new Heap(maxsize*2,false);
        Rheap = new Heap(maxsize*2,true);
        LS = 0;
        RS = 0;
    }
    public void Add(long X){
        if(Lheap.size != 0 && Lheap.Get() + LS >= X){
            minValue += Lheap.Get() + LS - X;
            Rheap.push(Lheap.pop() + LS - RS);
            Lheap.push(X-LS);
            Lheap.push(X-LS);
        }
        else if(Rheap.size != 0 && Rheap.Get() + RS <= X){
            minValue += X - Rheap.Get() - RS;
            Lheap.push(Rheap.pop() + RS - LS);
            Rheap.push(X-RS);
            Rheap.push(X-RS);
        }
        else{
            Lheap.push(X-LS);
            Rheap.push(X-RS);
        }
    }
    public void Min(long a,long b){
        LS += a;
        RS += b;
    }
    public long GetMin(){
        return minValue;
    }
}
class Heap{
    public int size;
    long[] obj;
    bool B;
    public Heap(int maxsize,bool b0){
        size = 0;
        B = b0;
        obj = new long[maxsize];
    }
    //小さい順ならa<=b
    public bool Compare(long a,long b){
        return (B && a<=b) || (!B && a>=b);
    }
    public void push(long a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(Compare(obj[p],a)){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public long pop(){
        long t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && Compare(obj[p+1],obj[p])){
                p++;
            }
            if(Compare(obj[p],obj[size])){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
    public long Get(){
        return obj[0];
    }
}

Measured memory_bytes: 17352
h, w = map(int, input().split())

grid = [[True] * (w + 2)]
for _ in range(h):
    grid.append([True] + [x == '#' for x in input()] + [True])
grid.append([True] * (w + 2))

q = []

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if grid[i][j] is True:
            q.append((i, j))

cnt = 0
while q:
    qq = []
    for i, j in q:
        if not grid[i + 1][j]:
            grid[i + 1][j] = True
            qq.append((i + 1, j))
        if not grid[i - 1][j]:
            grid[i - 1][j] = True
            qq.append((i - 1, j))
        if not grid[i][j + 1]:
            grid[i][j + 1] = True
            qq.append((i, j + 1))
        if not grid[i][j - 1]:
            grid[i][j - 1] = True
            qq.append((i, j - 1))

    cnt += 1
    q = qq

print(cnt - 1)


Measured memory_bytes: 105968
#include<stdio.h>
int main()
{
	char S[4];
	int a=0;
	int b=0;
	char c;
	char d;
	int OK;
	scanf("%s",S);
	c = S[0];
	for(int i=0;i<4;i++){if(S[i]!=c){d=S[i];OK=1;}}
	for(int i=0;i<4;i++){if(S[i]==c)a++;}
	if(OK){
		for(int i=0;i<4;i++){if(S[i]==d)b++;}
	}else
	{
		b=0;
	}
	printf("%s\n",(a==2 && b==2)? "Yes":"No");
	return 0;
}

Measured memory_bytes: 128