akhauriyash/Code-Regression huggingface dataset:
use std::cmp;
use std::collections::VecDeque;
use std::fmt::Debug;
use std::str::FromStr;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where
    T: FromStr,
    <T as FromStr>::Err: Debug,
{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim()
        .split_whitespace()
        .map(|c| T::from_str(c).unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_as_string() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn lower_bound<T: Ord + Debug + std::fmt::Display>(a: &VecDeque<T>, t: T) -> Option<usize> {
    if a.len() == 0 || t <= a[0] {
        return None;
    }
    let (mut left, mut right) = (0, a.len());
    while left + 1 < right {
        let mid = (left + right) / 2;
        // println!("left={}, right={}, mid={}", left, right, mid);
        // println!("t = {}, a[{}]={}", t, mid, a[mid]);
        if t > a[mid] {
            left = mid;
        } else {
            right = mid;
        }
    }
    return Some(left);
}

fn main() {
    let n: usize = read_as_vec()[0];
    let mut a: VecDeque<usize> = VecDeque::new();

    for _ in 0..n {
        let t = read_as_vec::<usize>()[0];
        if let Some(pos) = lower_bound(&a, t) {
            a[pos] = t;
        } else {
            a.push_front(t);
        }
        // println!("a = {:?}", a);
    }
    println!("{}", a.len());

    // let mut b: VecDeque<usize> = VecDeque::new();
    // b.push_back(2);
    // b.push_back(3);
    // b.push_back(4);
    // b.push_back(1);
    // b.push_back(2);
    // dbg!(lower_bound(&b, 3));
}


Measured memory_bytes: 2920
package main

import "fmt"

func main() {
	var a, b, c, d, e, f int
	fmt.Scan(&a, &b, &c, &d, &e, &f)
	a *= 100
	b *= 100

	ansSugar := 0
	ansSolution := a
	for ai := 0; a*ai <= f; ai++ {
		for bi := 0; a*ai+b*bi <= f; bi++ {
			water := a*ai + b*bi
			for ci := 0; water+c*ci <= f; ci++ {
				for di := 0; water+c*ci+d*di <= f; di++ {
					sugar := c*ci + d*di
					solution := water + sugar
					if sugar*(100+e) > e*solution {
						continue
					}
					if sugar*ansSolution > ansSugar*solution {
						ansSugar = sugar
						ansSolution = solution
					}
				}
			}
		}
	}
	fmt.Println(ansSolution, ansSugar)
}


Measured memory_bytes: 512
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

int main()
{
    long long x;
    
    scanf("%lld",&x);
    
    long long ans=(x/11)*2;
    
    if(x%11!=0)
    {
        if(x%11<=6)
            ans++;
        else
            ans+=2;
    }
    
    printf("%lld",ans);

    return 0;
}

Measured memory_bytes: 3820
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e6+5;
int main() {
    int x,y;cin>>x>>y;
    for(int i=0;i<=x;i++) {
    	if(i*2+(x-i)*4==y)
    		return 0*puts("Yes");
    }
    puts("No");
    return 0;
}

Measured memory_bytes: 3640
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>
#include <cmath>
#include <string>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
//================================================

int a = 0, b = 0, c, d, n, k = 0;
string s, t;



int main()
{
	cin >> s;
	int x = 0;
	rep(i, 4)
	{
		if (s[i] == '+')
			x++;
		else
			x--;
	}

	cout << x << endl;
	return 0;
}

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

int n, m;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	int a = 1, b = n;
	while(a < b && m) {
		cout << a << " " << b << "\n";
		++a; --b; --m;
		if((n & 1 ^ 1) && n - b + a >= b - a && n - 2 * b + 2 * a <= 2) --b;
	}
	cout << flush;
	return 0;
}


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

namespace ConsoleApp15
{
    class Class358
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split().Select(long.Parse).ToArray();
            if (N == 0 && A[0] == 1) { Console.WriteLine(1);return; }
            if (A[0] != 0) { Console.WriteLine(-1);return; }
            long[] max = new long[N+1];
            max[N] = A[N];
            long[] mi = new long[N + 1];
            mi[N] = 0;
            
            for(int i = N - 1; i >= 0; i--)
            {
                var a = A[i];
                mi[i] =mi[i+1] +  (max[i+1] - mi[i+1]) / 2;
                max[i] = max[i+1] + a;
                
            }
            
            long sum = 1;
            long leaf = 1;
            for(int i = 0; i < N; i++)
            {
                leaf = Math.Min((leaf - A[i]) * 2, max[i+1]);
                if (leaf < max[i+1] - mi[i+1]) { Console.WriteLine(-1);return; }
                sum += leaf;
            }
            Console.WriteLine(sum);
        }
    }
}


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

int main(){
  //preset
  int N;
  cin >> N;
  vector<int> buttons(N);
  for(int i=0;i<N;i++){
    cin >> buttons.at(i);
  }
  //search
  int index = 0;
  int counter = 0;
  while(1){
    if(buttons.at(index)){
      int tmp_index = index;
      counter++;
      index = buttons.at(index)-1;
      buttons.at(tmp_index) = 0;
      if(index==1){
        break;
      }
    }else{
      counter = -1;
      break;
    }
  }
  cout << counter << endl;
  return 0;
}

Measured memory_bytes: 640
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import Counter

N = int(readline())
S = read().rstrip()

MOD = 10 ** 9 + 7

counter = Counter(S)

x = 1
for y in counter.values():
    x *= (y + 1)

x -= 1
answer = x % MOD
print(answer)

Measured memory_bytes: 3572
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
inline void qread(){}
template<class T1, class ...T2>
inline void qread(T1 &IEE, T2&... ls){
    register T1 __ = 0, ___ = 1;
    register char ch;
    while(!isdigit(ch = getchar())) ___ = (ch == '-') ? -___ : ___;
    do{
        __ = (__ << 1) + (__ << 3) + (ch ^ 48);
    }while(isdigit(ch = getchar()));
    __ *= ___;
    IEE = __;
    qread(ls...);
    return ;
}
ll n, ans;
ll f[maxn];
int main(){
    cin >> n;
    for(int i = 1;i <= 10;i++){
        f[i] = i * (i + 1) / 2;
    }
    for(ll i = 2;i * i <= n;i++){
        int cnt = 0;
        while(n % i == 0){
            cnt++;
            n /= i;
        }
        int id = lower_bound(f + 1, f + 11, cnt) - f;
        if(f[id] > cnt) id--;
        ans += id;
    }
    if(n > 1) ans++;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3636
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
using namespace std;

#define DEBUG 0
#define REP(i, n) for (int i = 0; i < (n); i++) 
typedef long long ll;
typedef pair<int,int> pint;
static const ll MOD = 1000000007;
static const ll INF = 1000000000000000000LL;
using namespace std;


int main() { 
    ll H,W,K;
    cin >> H >> W >> K;
    vector <string> cake(H);
    REP(i,H)
    {
        cin >> cake[i];
    }

    vector <pint> ichigo;

    REP(y,H)
    {
        REP(x,W)
        {
            if(cake[y][x] == '#')ichigo.push_back(pint(y, x));
        }
    }
    vector <vector <int> > res(H, vector<int>(W, 0));

    REP(i,K)
    {
        int y = ichigo[i].first;
        int x = ichigo[i].second;

        res[y][x] = i+1;

        //横を優先で塗ってみる
        int l_x = x,r_x = x,matuy;
        //左側
        for(int j = x+1; j < W; ++j)
        {
            if(cake[y][j] == '#' || res[y][j] != 0)break;
            res[y][j] = i + 1;
        }
        for(int j = x-1; j >= 0; --j)
        {
            if(cake[y][j] == '#' || res[y][j] != 0)break;
            res[y][j] = i + 1;
        }
    }

    //上から下に塗っていく
    for(int y = 1; y < H; ++y)
    {
        for(int x = 0; x < W; ++x)
        {
            if(res[y][x] == 0)res[y][x] = res[y-1][x];
        }
    }
    //下から上に塗っていく
    for(int y = H - 2; y >= 0; --y)
    {
        for(int x = 0; x < W; ++x)
        {
            if(res[y][x] == 0)res[y][x] = res[y+1][x];
        }
    }

    REP(y,H)
    {
        REP(x,W)
        {
            cout << res[y][x] << " ";
        }
        cout << endl;
    }



    return 0;
}

Measured memory_bytes: 2040
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <queue>
# include <stack>
# define MOD 1000000000+7
# define INF 1000000000
#define rep(i,rept)	for(ll i = 0; i<rept;i++)
typedef long long ll;
using namespace std;
ll counter;
ll keta(ll i) {
	counter = 0;
	while (i > 10) {
		i /= 10;
		counter++;
	}
	return counter;
	
}
int main() {
	double a = 0, b = 0, n = 0;
	std::string str,str2;
	cin >> a >> b;
	cout << ceil((b - 1) / (a - 1)) << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
const int maxn = int(1e5) + 7, inf = 0x3f3f3f3f;
int n, x[maxn], up[maxn][20], l, q, u, v, e[27] = {1}, ans, tmp;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", x + i);
    scanf("%d%d", &l, &q);
    memset(up, 0x3f, sizeof(up));
    int cur = 1;
    for (int i = 1; i <= n; i++) while (x[i] > x[cur] + l) up[cur++][0] = i - 1;
    for (int j = 1; j < 20; j++) {
        e[j] = e[j - 1] << 1;
        for (int i = 1; i <= n; i++) if (up[i][j - 1] < inf)
            up[i][j] = up[up[i][j - 1]][j - 1];
    }
    while (ans = 0, q--) {
        scanf("%d%d", &u, &v);
        if (u > v) std::swap(u, v);
        for (int i = 19; i >= 0; i--) if (up[u][i] < v) u = up[u][i], ans += e[i];
        printf("%d\n", ans + 1);
    }
    return 0;
}


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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int N;

struct Edge {
    Edge (){};
    Edge (int t, int d_) : to(t), d(d_), cost_sum(-1){};
    int to, d;
    int cost_sum;
};

vector<vector<Edge>> es;

int dfs(int v, int ei) {
    if (es[v][ei].cost_sum != -1) return es[v][ei].cost_sum;
    int to = es[v][ei].to;

    int cost = es[v][ei].d * (es[to].size() == 1 ? 1 : 3);

    rep(i, (int)es[to].size()) {
        auto &e = es[to][i];
        if (e.to == v) continue;
        cost += dfs(to, i);
    }

    return es[v][ei].cost_sum = cost;
}

int ans;

void dfs2(int v, int before, int sum0) {
    // goal
    int sum = sum0;
    for (auto &e : es[v]) {
        if (e.to == before) continue;
        sum += e.cost_sum;
    }
    ans = min(ans, sum);

    // next
    for (auto &e : es[v]) {
        if (e.to == before) continue;
        if (es[e.to].size() == 1) continue;
        dfs2(e.to, v, sum - e.cost_sum + e.d * 2);
    }
}

int solve() {
    rep(v, N) rep(i, (int)es[v].size()) dfs(v, i);

    ans = INF;
    for (int si = 0; si < N; si++) {
        dfs2(si, -1, 0);
    }

    return ans;
}

int main(void) {
    while (cin >> N, N) {
        es.clear();
        es.resize(N);

        VI ps(N - 1);
        for (auto &p : ps) cin >> p;
        VI ds(N - 1);
        for (auto &d : ds) cin >> d;

        for (int i = 1; i < N; i++) {
            int j = ps[i - 1] - 1;
            es[j].EB(i, ds[i - 1]);
            es[i].EB(j, ds[i - 1]);
        }

        cout << solve() << endl;
    }

	return 0;
}

Measured memory_bytes: 1332
import java.io.*;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws IOException {

        ArrayList<Integer> x_list = new ArrayList<>();
        ArrayList<Integer> y_list = new ArrayList<>();

        int x;
        int y;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        do {
            String[] line = br.readLine().split(" ");
            x = Integer.parseInt(line[0]);
            y = Integer.parseInt(line[1]);

            if (x == 0 && y == 0) {
                break;
            }

            x_list.add(x);
            y_list.add(y);

        } while (x != 0 || y != 0);

        for (int i = 0; i < x_list.size(); i++) {
            if (x_list.get(i) > y_list.get(i)) {
                System.out.println(y_list.get(i) + " " + x_list.get(i));
            } else {
                System.out.println(x_list.get(i) + " " + y_list.get(i));
            }
        }

    }

}

Measured memory_bytes: 27736
N = int(input())
H = list(map(int, input().split(' ')))

flag = True
for i in range(N-1):
    if H[i] - H[i+1] > 1:
        flag = False
        break
ltd_num = H[0] - 2
for i in range(1, N):
    if H[i] <= ltd_num:
        flag = False
        break
    if ltd_num < H[i] - 2:
        ltd_num = H[i] - 2

if flag is True:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 14252
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
vector<long long> calc_divisor(long long n) {//約数列挙
    vector<long long> res;
    for (long long i = 1LL; i*i <= n; ++i) {
        if (n % i == 0) {
            res.push_back(i);
            long long j = n / i;
            if (j != i) res.push_back(j);
        }
    }
    sort(res.begin(), res.end());
    return res;
}

int keta(ll n){
    int ans = 0;
    while(n>0){
        ans++;
        n/=10;
    }
    return ans;
}

int main(){
    ll n;
    cin >> n;
    int ans = inf;
    if(calc_divisor(n).size()==1){
        cout<<keta(n)<<endl;
    }else{
        vector<ll> div = calc_divisor(n);
        for(auto u : div){
            int now = max(keta(u),keta(n/u));
            ans = min(now,ans);
        }
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define pii pair<long long,long long>
#define pll pair<long long, long long>
#define rep(i,l,r) for(long long i = l;i<r;i++)

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

#define fastout  ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
using namespace __gnu_pbds;

typedef tree<pll,null_type,less<pll>,rb_tree_tag,tree_order_statistics_node_update> order_set;

const long long INF = (long long)1e9 + 7;
const long long LLINF = (long long)1e18 + 7;

mt19937 mt('O' + '4' + 'K' + 'O');

void kekek()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    mt();
    #ifdef EB_UG
        freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
    #endif
}

int main() {
	kekek();
	int n;
	cin >> n;
	vector<int> nums(n);
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	int ans = 0;
	int ta = 0;
	int prev = INF;
	for (int i = 0; i < n; i++) {
		if (nums[i] <= prev) {
			ta++;
			prev = nums[i];
		}
		else {
			ans = max(ans, ta);
			prev = nums[i];
			ta = 1;
		}
	}
	ans = max(ans, ta);
	cout << ans - 1;
}

Measured memory_bytes: 640
from itertools import accumulate
N=int(input())
S=input()
black=[0]*N
white=[0]*N
for i,s in enumerate(S):
        if s=='#':
                black[i]=1
        else:
                white[i]=1
black = [0] + black
black = list(accumulate(black))
white = [0] + white[::-1]
white = list(accumulate(white))[::-1]
ans=[]
for i in range(N+1):
        ans.append(black[i]+white[i])
print(min(ans))

Measured memory_bytes: 29176
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
class Program
{
    static void Main()
    {
        const int NumberOfColors = 8;
        int n = int.Parse(Console.ReadLine());
        int[] a = Console.ReadLine().Split(' ')
                    .Select(elem => int.Parse(elem))
                    .ToArray();
        bool existGrey = false, existBrown = false, existGreen = false, existLightBlue = false, existBlue = false, existYellow = false, existOrange = false, existRed = false;
        int NumberOfFreeColorPerson = 0;
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            if (a[i] <= 399) 
            {
                if (!existGrey)
                {
                existGrey = true; 
                cnt++; 
                Console.Error.WriteLine("grey");
                }
            }
            else if (a[i] <= 799) 
            {
                if (!existBrown)
                {
                existBrown = true; 
                cnt++; 
                Console.Error.WriteLine("brown");
                }
            }
            else if (a[i] <= 1199) 
            {
                if (!existGreen)
                {
                existGreen = true; 
                cnt++; 
                Console.Error.WriteLine("green");
                }
            }
            else if (a[i] <= 1599) 
            {
                if (!existLightBlue)
                {
                existLightBlue = true; 
                cnt++; 
                Console.Error.WriteLine("lblue");
                }
            }
            else if (a[i] <= 1999) 
            {
                if (!existBlue)
                {
                existBlue = true; 
                cnt++; 
                Console.Error.WriteLine("blue");
                }
            }
            else if (a[i] <= 2399) 
            {
                if (!existYellow)
                {
                existYellow = true; 
                cnt++; 
                Console.Error.WriteLine("yellow");
                }
            }
            else if (a[i] <= 2799) 
            {
                if (!existOrange)
                {
                existOrange = true; 
                cnt++; 
                Console.Error.WriteLine("orange");
                }
            }
            else if (a[i] <= 3199) 
            {
                if (!existRed)
                {
                existRed = true; 
                cnt++; 
                Console.Error.WriteLine("red");
                }
            }
            else NumberOfFreeColorPerson++;
        }
        int min = Math.Max(cnt, 1);
        int max = min;
        if (NumberOfFreeColorPerson != 0)
        {
            max = NumberOfFreeColorPerson + cnt;
        }
        Console.WriteLine("{0} {1}", min, max);
    }
}


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

int main(){
    int h, w, m; cin >> h >> w >> m;
    map<pair<int, int>, int> t;
    vector<pair<int64_t, int>> th(h, {0, 0}), tw(w, {0, 0});
    for (auto i = 0; i < m; i++) {
        int x, y; cin >> x >> y; x--; y--; t[{x, y}]++;
        th[x].first++; tw[y].first++; th[x].second = x; tw[y].second = y;
    }
    sort(th.begin(), th.end(), greater<>());
    sort(tw.begin(), tw.end(), greater<>());
    int64_t ans = th.front().first + tw.front().first;
    for (auto elm1 = th.begin(); elm1->first == th.front().first; elm1++) {
        bool flg = false;
        for (auto elm2 = tw.begin(); elm2->first == tw.front().first; elm2++) {
            if(t[{elm1->second, elm2->second}] == 0) { flg = true; break; }
        }
        if(flg) break;
        if(next(elm1)->first != th.front().first) ans--;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 31492
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <functional>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

typedef pair<int, int> pii;

struct data {
	int cost, pos, lim;
	
	data() {}
	data(int cost, int pos, int lim) : cost(cost), pos(pos), lim(lim) {}
	
	bool operator <(const data &r) const { return cost < r.cost; }
	bool operator >(const data &r) const { return cost > r.cost; }
};

int N, K;
int c[5000], r[5000];
vector<int> g[5000];
int d[5000][5001];

int main() {
	cin >> N >> K;
	REP(i, N) scanf("%d %d", &c[i], &r[i]);
	
	REP(i, K) {
		int a, b;
		scanf("%d %d", &a, &b);
		a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	
	priority_queue<data, vector<data>, greater<data> > pq;
	fill(d[0], d[N], INF);
	
	pq.push(data(0, 0, 0));
	while (!pq.empty()) {
		data now = pq.top(); pq.pop();
		
		int cost = now.cost;
		int pos = now.pos;
		int lim = now.lim;
		
		if (d[pos][lim] <= cost) continue;
		d[pos][lim] = cost;
		
		REP(i, g[pos].size()) {
			if (d[g[pos][i]][r[pos] - 1] > cost + c[pos]) pq.push(data(cost + c[pos], g[pos][i], r[pos] - 1));
			if (lim > 0 && d[g[pos][i]][lim - 1] > cost) pq.push(data(cost, g[pos][i], lim - 1));
		}
	}
	
	int ans = INF;
	REP(i, N) ans = min(ans, d[N - 1][i]);
	cout << ans << endl;	
	return 0;
}

Measured memory_bytes: 102700
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(n):
    if a[i] >= b[i]:
        ans += b[i]
    elif a[i] + a[i+1] > b[i]:
        ans += b[i]
        a[i+1] = a[i] + a[i+1] - b[i]
    else:
        ans += a[i] + a[i+1]
        a[i+1] = 0
print(ans)


Measured memory_bytes: 18476
#include <stdio.h>

typedef struct{ 
	char suit;
	int value; 
}card; 


int main()
{
  int i, N, j, min, s;
  card x[36],y[36],k;

  scanf("%d",&N);
  for(i = 0; i < N; i++){
  	scanf(" %c%d",&x[i].suit, &x[i].value);
  }
  
  for(i = 0; i < N; i++)
     y[i] = x[i];
 
 /* bubble */
	for(i = 0; i < N; i++){
		for(j = N-1; j > i; j--){
			if(x[j].value < x[j-1].value){
				k = x[j];
				x[j] = x[j-1];
				x[j-1] = k;
			}
		}
	}
	
	for(i = 0; i < N; i++){
	    if(i > 0) printf(" ");
		    printf("%c%d",x[i].suit,x[i].value);
	}
	printf("\n");
	printf("Stable\n");
	
 /* selection */
	for(i = 0; i < N; i++){
		min = i;
		for( j = i; j < N; j++ ){
			if( y[j].value < y[min].value){
			min = j;
			}
		}
		k = y[i];
		y[i] = y[min];
		y[min] = k;
	}

	
	for(i = 0; i < N; i++){
		if(i > 0) printf(" ");
		printf("%c%d",y[i].suit,y[i].value);
	}
	printf("\n");
	
	s = 1;
	
	for(i = 0; i < N; i++){
		if(x[i].suit != y[i].suit){
		s = 0;
	    }
	}	
	
	if(s == 0){
		printf("Not stable\n");
	}else{
		printf("Stable\n");
	}
  return 0;
}

Measured memory_bytes: 1856
D = [[-1, 0], [1, 0], [0, -1], [0, 1]]

def puyo_rensa(cnt, fld)
  erase = 8.times.map{ [false] * 14 }
  visit = 8.times.map{ [false] * 14 }
  res = 0
  (1..6).each do |i|
    (1..12).each do |j|
      if fld[i][j] =~ /[^O.]/ && !visit[i][j]
        puyo_connector(i, j, fld, visit){ |arr|
          arr.each do |s, t|
            D.each do |x, y|
              p, q = s + x, t + y
              erase[p][q] = true if fld[p][q] == 'O'
            end
            erase[s][t] = true
          end
          res = 1
        }
      end
    end
  end
  (1..6).each do |i|
    fld[i].map!.with_index{ |c, j| c unless erase[i][j] }.compact!
    (14 - fld[i].size).times{ fld[i].unshift('.') }
  end
  if res == 1 then puyo_rensa(cnt + 1, fld)
  else cnt
  end
end

def puyo_connector(i, j, fld, visit)
  arr = [[i, j]]
  ret = [[i, j]]
  visit[i][j] = true
  until arr.empty?
    i, j = arr.shift
    D.each do |x, y|
      p, q = i + x,  j + y
      if !visit[p][q] && fld[i][j] == fld[p][q]
        visit[p][q] = true
        arr << [p, q]
        ret << [p, q]
      end
    end
  end
  yield(ret) if ret.size >= 4
end

gets.to_i.times do
  fld = 8.times.map{ ['.'] * 14 }
  12.times do |i|
    gets.chomp.split('').each_with_index do |c, j|
      fld[j+1][i+1] = c
    end
  end
  p puyo_rensa(0, fld)
end

Measured memory_bytes: 9252
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define Rep(i,n) for(int i=1; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const int INF = 1001001001;

int dp[10010];
int main() {
	int H,N; cin>>H>>N;
	int a[1010], b[1010];
	rep(i,N) cin>>a[i]>>b[i];
	
	rep(i,10010) dp[i] = INF;
	dp[H] = 0;
	for (int i=H;i>=0;i--) {
		for (int j=0;j<N;j++) {
			if (i-a[j]<=0) dp[0] = min(dp[0],dp[i]+b[j]);
			else dp[i-a[j]]=min(dp[i-a[j]],dp[i]+b[j]);
		}
	}
	cout << dp[0] << endl;
	return 0;
}

Measured memory_bytes: 256
use proconio::fastout;
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
use std::collections::HashSet;

#[fastout]
fn main() {
    input! {
        n: usize,
        mut k: usize,
        a: [Usize1; n]
    }
    let mut gone1_dic = HashSet::new();
    let mut now = 0;
    gone1_dic.insert(now);
    loop {
        now = a[now];
        k -= 1;
        if k == 0 {
            println!("{}", now + 1);
            return;
        }
        if gone1_dic.contains(&now) {
            break;
        }
        gone1_dic.insert(now);
    }

    let mut gone2 = vec![];
    gone2.push(now);
    let mut gone2_dic = HashSet::new();
    gone2_dic.insert(now);
    loop {
        now = a[now];
        k -= 1;
        if k == 0 {
            println!("{}", now + 1);
            return;
        }
        if gone2_dic.contains(&now) {
            break;
        }
        gone2.push(now);
        gone2_dic.insert(now);
    }

    let goal = k % gone2.len();
    println!("{}", gone2[goal] + 1);
}


Measured memory_bytes: 11772
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using Graph = vector<vector<int>>;

typedef pair<int, int> P;

int main(){
    int N; cin >> N;
    vector<int> d(N+1, 1000000);
    d[0] = 0;
    
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, 0));

    while (!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int pow6 = 1; pow6 <= N; pow6 *= 6){
            if (v+pow6 > N) break;
            if (d[v+pow6] > d[p.second] + 1){
                d[v+pow6] = d[p.second]+1;
                que.push(P(d[v+pow6], v+pow6));
            }
        }
        for (int pow9 = 1; pow9 <= N; pow9 *= 9){
            if (v+pow9 > N) break;
            if (d[v+pow9] > d[p.second] + 1){
                d[v+pow9] = d[p.second]+1;
                que.push(P(d[v+pow9], v+pow9));
            }
        }
    }

    cout << d[N] << endl;
}

Measured memory_bytes: 1024
#include<iostream>
using namespace std;
int main()
{
  int n,i,j,k,count=0,temp;
  cin>>n;
  int a[n];
  for(i=0;i<n;i++)
  {
    cin>>a[i];
  }
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      if(a[i]>a[j])
      {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
      }
    }
  }
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
    {
      for(k=j+1;k<n;k++)
      {
        if(a[i]+a[j]<=a[k])
          break;
        else
          count++;
      }
    }
  }
  cout<<count;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
    int x,y,i,j;
    cin >> x >> y;
    while(x!=0 or y!=0){
        for(i=0;i<x;i++){
            for(j=0;j<y;j++){
                cout << "#";
            }
            cout << endl;
        }
        cout << endl;
        cin >> x >> y;
    }
    return 0;
}


Measured memory_bytes: 3040
#include <iostream>
#include <climits>
#include <vector>

const long long INF = LLONG_MAX - 1000000000; //e+9を足してもオーバーフローしないサイズをINFとする

using namespace std;

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

        return true;
    }

    return false;
}

int main(void)
{
    long long N, W;

    cin >> N >> W;

    vector<long long> w(N), v(N);

    for (int i = 0; i < N; i++)
    {
        cin >> w[i] >> v[i];
    }

    vector<vector<long long>> dp(N + 1, vector<long long>(100001, INF));

    dp[0][0] = 0;

    for (int i = 0; i < N; i++)
    {
        for (int v_i = 0; v_i < 100002; v_i++)
        {
            if (v_i - v[i] >= 0)
            {
                chmin(dp[i + 1][v_i], dp[i][v_i - v[i]] + w[i]);
            }
            chmin(dp[i + 1][v_i], dp[i][v_i]);
        }
    }

    int ans = -1;

    for (int i = 100000; i >= 0; i--)
    {
        if (dp[N][i] <= W && i > ans)
        {
            ans = i;
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 80256
while True:
  n, d =  input().split()
  if n == '0' and d == '0':
    break
  a = [int(n)]
  while True:
    n = ''.join(sorted(n.zfill(int(d))))
    b = int(n[::-1]) - int(n)
    if b in a:
      break
    a += [b]
    n = str(b)
  print(a.index(b), b, len(a) - a.index(b))


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

#define int long long

signed main(){
    int l, r;
    cin >> l >> r;

    int ans = LLONG_MAX;
    for(int i = l;i <= min(l+3000,r);i++){
        for(int j = i+1;j <= min(i+3000, r);j++){
            ans = min(ans, ((i%2019) * (j%2019))%2019);
        }
    }

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> in(n);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
  }
  long long ans = 0;
  for (int l = 0; l <= min(k, n); l++) {
    for (int r = 0; r <= min(k - l, n - l); r++) {
      long long cur = 0;
      priority_queue<int> que;
      for (int i = 0; i < l; i++) {
        cur += in[i];
        que.push(-in[i]);
      }
      for (int i = 0; i < r; i++) {
        cur += in[n - 1 - i];
        que.push(-in[n - 1 - i]);
      }
      int remove = k - l - r;
      for (int i = 0; i <= remove; i++) {
        ans = max(ans, cur);
        if (que.size()) {
          cur += que.top();
          que.pop();
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    int t;
    cin>>t;
    long long ans1 = pow(t , 2);
    long long ans2 = pow(t , 3);
    cout<<t+ans1+ans2;

}


Measured memory_bytes: 4048
#include<stdio.h>
#include<stdlib.h>

int p(int i){return i/2;}
int l(int i){return 2*i;}
int r(int i){return 2*i+1;}

void maxHeapify(int *,int );
void swap(int *,int *);

int n;

int main(){
  int i,*A;

  scanf("%d",&n);
  A=(int *)malloc((n+1)*sizeof(int));
  for(i=1;i<=n;i++) scanf("%d",&A[i]);

  for(i=n/2;i>=1;i--) maxHeapify(A,i);

  for(i=1;i<=n;i++){
    printf(" %d",A[i]);
    // if(i!=n) printf(" ");
  }
  printf("\n");

  return 0;

}

void maxHeapify(int *A,int i){
  int lft,rgh,max;
  lft=l(i);
  rgh=r(i);

  if(lft <= n && A[lft]>A[i]) max=lft;
  else max=i;
  if(rgh <=n && A[rgh]>A[max]) max=rgh;

  if(max!=i){
    swap(&A[max],&A[i]);
    maxHeapify(A,max);
  }
}

void swap(int *a,int *b){
  int buf;
  buf=*a;
  *a=*b;
  *b=buf;
}



Measured memory_bytes: 3652
#include<bits/stdc++.h>
#define pb push_back
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define f q.front()
#define SIZE (ll)(1e3)
#define mod (ll)(1e9+7)
using namespace std;
typedef long long int ll;

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

	int a[n+1],i,j;
	for(int i=1;i<=n;i++)cin>>a[i];
     int dp[n+1];
 for(i=1;i<=n;i++)dp[i]=mod;
 	dp[1]=0;
 for(i=1;i<=n;i++){
   for(j=i+1;j<i+3;j++){
   	if(j<=n)
   
   	dp[j]=min(dp[i]+abs(a[i]-a[j]),dp[j]);
 }}
 cout<<dp[n];
return 0;}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
string ss;int K;
set<string>S;

void init(){
	cin>>ss>>K;int len=ss.length();
	for(int i=0;i<len;i++){
		string s=""; 
		for(int j=0;j<5;j++){
			if(i+j<len){
				s=s+ss[i+j];
				S.insert(s);
			}
		}
	}
	return;
}


int main(){
	init();int num=0;
	for(set<string>::iterator it=S.begin();it!=S.end();it++){
		num++;
		if(num==K){
			cout<<*it<<endl;
			return 0;
		}
	}
}

Measured memory_bytes: 1664
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;
#define MAX 102
typedef unsigned long long ULL;

void process();
int main(){
    process();
    return 0;
}
void process(){
    int X,N;
    int ans = 0;
    bool data[MAX];
    int p,i;
    cin >> X >> N;
    for(i=0;i<MAX;i++){
        data[i] = false;
    }
    for(i=0;i<N;i++){
        cin >> p;
        data[p] = true;
    }
    ans == -1;
    for(i=0;i<MAX;i++){
        if( data[i] == false){
            if( ans == -1 || abs(X - ans) > abs(X - i)){
                ans = i;
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define ppll pair<ll,pll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = 1 << 29;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const int MAX_V = 500;
const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }




void solve() {
    ll n,x,m;
    cin>>n>>x>>m;
    vll v;
    map<ll,ll> mp;
    ll now = x;
    mp[x]++;
    v.pb(x);
    ll start = 0;
    rep(i,m+1000){
        ll next = now * now % m;
        if(mp[next] > 0){
            start = next;
            break;
        }
        else{
            v.pb(next);
            mp[next]++;
        }
        now = next;
    }
    ll N = v.size();
    ll index = -1;
    ll ans = 0;
    rep(i,N){
        if(v[i]==start){
            index = i;
            break;
        }
    }
    rep(i,index){
        ans += v[i];
    }
    ll sum = 0;
    rep2(i,index,N){
        sum += v[i];
    }
    n -= index;
    ans += (n / (N-index) ) * sum;
    rep(i,n%(N-index)){
        ans += v[i+index];
    }

    cout<<ans<<endl;
    // cout<<"start"<<start<<endl;
    // cout<<"index"<<index<<endl;
    // cout<<"N"<<N<<endl;
    // rep(i,N){
    //     cout<<v[i]<<endl;
    // }

}




signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);


    ll t=1;
    
    // cin>>t;

    rep(i,t){
        solve(); 
    }

    return 0;
}


Measured memory_bytes: 6724
module Enumerable
  def scan(init = 0)
    inject([init]) { |a, n| a << yield(a.last, n) }
  end
end

n = gets.to_i
ns1 = gets.split.map(&:to_i)
ns2 = gets.split.map(&:to_i)

a1 = ns1.scan(&:+)
a2 = ns2.scan(&:+)

p Array.new(n) { |i| a1[i + 1] + a2.last - a2[i] }.max


Measured memory_bytes: 1788
use std::io::prelude::*;
use std::io::stdin;
use std::str::FromStr;

fn main() {
    let n: i32 = read();
    let i: i32 = read();
    println!("{}", n - i + 1);
}

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


Measured memory_bytes: 4352
#include<bits/stdc++.h>
#define lli long long int
#define floop(a,b,c) for(lli a=b;a<c;a++)
#define bloop(a,b,c) for(lli a=b;a>=c;a--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vt vector
#define MAX 1000000007
using namespace std;
lli t,n;
lli i,j;
int main(){
        cin>>n;
        lli a[n][3];
        floop(i,0,n){
        	cin>>a[i][0]>>a[i][1]>>a[i][2];
        }
        lli b[n][3]={0}; b[0][0]=a[0][0]; b[0][1]=a[0][1]; b[0][2]=a[0][2];
        floop(i,1,n){
            b[i][0]=a[i][0]+max(b[i-1][1],b[i-1][2]);
            b[i][1]=a[i][1]+max(b[i-1][0],b[i-1][2]);
            b[i][2]=a[i][2]+max(b[i-1][0],b[i-1][1]);
        }
        cout<<max(b[n-1][0],max(b[n-1][1],b[n-1][2]));
        return 0;
}


Measured memory_bytes: 4992
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

K = INT()

tmp = 7
ans = 1
p = 1
mo = tmp%K
checked = [0]*(10**6+1)
checked[mo] = 1
while mo != 0:
    mo = mo + 7*pow(10, p, K)
    mo %= K
    p += 1
    if checked[mo]:
        print(-1)
        break
    checked[mo] = 1
else:
    print(p)


Measured memory_bytes: 76752
// In the name of ALLAH

#include<bits/stdc++.h>


#define sc(x) scanf("%d",&x)
#define ALL(x) x.begin(),x.end()
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORd(i,a,b) for(int i=a;i>b;i--)
#define ff first
#define ss second
#define reset(x) memset(x,0,sizeof x)
#define sz(x) x.size()
#define pb push_back
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);


using namespace std;

typedef pair<int,int> pii;
typedef vector<pii> vii;
const int con = 1e5+7;
static const int INF = 0x3f3f3f3f;
static const int MOD = 1e9+7;
/*
ll power(ll x,ll y,ll mod){
    int res = 1; // Initialize result

    while (y > 0) {
        
        if (y & 1)
            res = (res * x + mod)%mod;


        y = y >> 1;
        x = (x * x)%mod;
    }
    return res;
}
*/


int main() {
    FASTIO

    string s;cin >> s;
    map<char,int> mp;
    for(auto &x:s){
        mp[x]++;
    }
    for(auto &x:mp){
        if(x.second != 2){
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}

Measured memory_bytes: 384
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N =1000;
int lcs(string X,string Y){
  int c[N+1][N+1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for(int i=0;i<=m;i++) c[i][0]=0;
  for(int j=1;j<=n;j++) c[0][j]=0;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++){
      if(X[i]==Y[j]){
	c[i][j]=c[i-1][j-1]+1;
      }else{
	c[i][j]=max(c[i-1][j],c[i][j-1]);
      }
      maxl=max(maxl,c[i][j]);
    }
  }
  return maxl;
}
int main(){
  string s1,s2;
  int n; cin >> n;
  for(int i=0;i<n;i++){
    cin >> s1 >> s2;
    cout << lcs(s1,s2) << endl;
  }
  return 0;
}



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

int main() {
    lint m;
    cin >> m;

    vector<lint> ds(m), cs(m);
    for(lint i=0; i<m; i++)  cin >> ds[i] >> cs[i];

    queue<P> que;
    for(lint i=0; i<m; i++)  que.push(P(ds[i], cs[i]));

    vector<lint> v;
    lint ans = 0;
    while(!que.empty()) {
        lint d, c;
        tie(d, c) = que.front(); que.pop();

        if(d >= 10) {
            que.push(P(d/10 + d%10, c));
            ans += c;
            continue;
        }
        
        if(c == 1) {
            v.push_back(d);
            continue;
        }

        if(c % 2 == 0) {
            ans += c/2;
            que.push(P(d+d, c/2));
            continue;
        }

        if(c % 2 == 1) {
            ans += c/2;
            que.push(P(d+d, c/2));
            que.push(P(d, 1));
            continue;
        }
    }

    lint prev = v[0];
    for(lint i=1; i<v.size(); i++) {
        prev += v[i];  ans++;
        if(prev >= 10)  prev = prev/10 + prev%10, ans++;
    }

    cout << ans << endl;
}

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

int main() {
    int n, ac = 0, wa = 0, tle = 0, re = 0;
    cin >> n;
    vector<string> r(n);
    rep(i,n){
        cin >> r.at(i);
        if(r.at(i)=="AC"){
            ac++;
        }
        else if(r.at(i)=="WA"){
            wa++;
        }
        else if(r.at(i)=="TLE"){
            tle++;
        }
        else if(r.at(i)=="RE"){
            re++;
        }
    }
    cout << "AC x " << ac << endl;
    cout << "WA x " << wa << endl;
    cout << "TLE x " << tle << endl;
    cout << "RE x " << re << endl;
}


Measured memory_bytes: 6380
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        k: usize
    }
    if k > (n - 1) * (n - 2) / 2 {
        println!("-1");
        return;
    }
    if n == 2 {
        println!("1");
        println!("1 2");
        return;
    }
    println!("{}", n * (n - 1) / 2 - k);
    if k <= n - 2 {
        for i in 1..n - k {
            println!("1 {}", i + 1);
        }
        for i in 1..n {
            for j in i + 1..n {
                println!("{} {}", i + 1, j + 1);
            }
        }
    } else {
        println!("1 2");
        for i in 2..n {
            println!("2 {}", i + 1);
        }
        let mut rest = k + 2 - n;
        for i in 2..n {
            for j in i + 1..n {
                if rest > 0 {
                    rest -= 1;
                } else {
                    println!("{} {}", i + 1, j + 1);
                }
            }
        }
    }
}


Measured memory_bytes: 4352
n = gets.to_i
cards = gets.chomp.split.map(&:to_i)

alice = []
bob = []

i = 0

cards.sort!.reverse.each do |n|

  if i.even?
    alice << n
  else
    bob << n
  end
  i += 1
end

puts alice.inject(:+) - bob.inject(:+)


Measured memory_bytes: 1788
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define DEBUGP(val) cerr << #val << "=" << val << "\n"



const int DEBUG = 0;

using namespace std;
typedef long long int lint;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
typedef pair<lint, lint> pll;
const lint mod = 1e9 + 7;

const lint inf = 1e18;

lint sq(lint x) { return x * x % mod; }

const int N = 100100;
int n;
vector<pil> edges[N];

pll add(pll a, pll b) {
  if (a.first != b.first) return min(a,b);
  return pll(a.first,(a.second+b.second)%mod);
}
pll mul(pll a,pll b){
  return pll(a.first+b.first,a.second*b.second%mod);
}


vector<pll> calc(int s) {
  vector<pll> dp(n, pll(inf, 0));
  vector<bool> vis(n, false);
  priority_queue<pli, vector<pli> , greater<pli> > que;
  dp[s] = pll(0, 1);
  que.push(pli(0, s));
  while (not que.empty()) {
    pli vd = que.top(); que.pop();
    int v = vd.second;
    if (vis[v]) continue;
    vis[v] = true;
    REP(i, 0, edges[v].size()) {
      pil wc = edges[v][i];
      int w = wc.first;
      lint c = wc.second;
      dp[w]=add(dp[w],mul(dp[v],pll(c,1)));
      que.push(pli(dp[w].first,w));
    }
  }
  return dp;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m, s, t;
  cin >> n >> m >> s >> t;
  s--, t--;
  REP(i, 0, m) {
    int u, v, d;
    cin >> u >> v >> d;
    u--, v--;
    edges[u].push_back(pil(v, d));
    edges[v].push_back(pil(u, d));
  }
  vector<pll> sol_s, sol_t;
  sol_s = calc(s);
  sol_t = calc(t);
  if (DEBUG) {
    cerr << "dp:";
    REP(i, 0, n) cerr << " " << sol_s[i].second;
    cerr << endl;
    cerr << "dp2:";
    REP(i, 0, n) cerr << " " << sol_t[i].second;
    cerr << endl;
  }
  lint ans = sq(sol_s[t].second);
  REP(i, 0, n) {
    if (sol_s[i].first + sol_t[i].first != sol_s[t].first) continue;
    if (2 * sol_s[i].first == sol_s[t].first) {
      ans += mod - sq(sol_s[i].second * sol_t[i].second % mod);
      ans %= mod;
    }
    if (2 * sol_s[i].first < sol_s[t].first) {
      REP(j, 0, edges[i].size()) {
	pil wc = edges[i][j];
	int w = wc.first;
	lint c = wc.second;
	if (sol_s[i].first + c + sol_t[w].first != sol_s[t].first) continue;
	if (2 * sol_s[w].first > sol_s[t].first) {
	  ans += mod - (sq(sol_s[i].second * sol_t[w].second % mod) % mod);
	  ans %= mod;
	}
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 19144