akhauriyash/Code-Regression huggingface dataset:
// =====================================================================
// macros
// =====================================================================

/// input macro from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            #[allow(unused_mut)]
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        #[allow(unused_mut)]
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

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

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

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

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

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
/// yet another input macro from https://qiita.com/hatoo@github/items/fa14ad36a1b568d14f3e
#[allow(unused_macros)]
macro_rules! inputln {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($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!($t)
        ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
            get!($($t),*)
        ).collect::<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!($t ;;)).collect::<Vec<_>>()
    };
}

// =====================================================================
// functions
// =====================================================================

fn main() {
    input!{
        n: usize,
        an: [usize;n],
    }
    for a in an {
        if a % 2 ==0 {
            if a % 3 != 0 && a % 5 != 0 {
                println!("DENIED");
                return;
            }
        }
    }
    println!("APPROVED");
}


Measured memory_bytes: 4352
//Date and time of submission: 
 
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e5+5;
ll a,b;
int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL);
	cin>>a>>b;
	cout<<max(a+b,max(a-b,a*b));
	return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        if(a <= b){
            System.out.println(a);
        } else {
            System.out.println(a - 1);
        }
        sc.close();
    }
}


Measured memory_bytes: 20820
N,K = map(int, input().split())
x = list(map(int, input().split()))

min_v = 10 ** 10
for i in range(N-K+1):

    v = min(abs(x[i]), abs(x[i+K-1])) + abs(x[i] - x[i+K-1])
    min_v = min(min_v, v)
    
print(min_v)
                


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

namespace ABC54_C
{
    class Program
    {
        public static bool[][] g;
        static int dfs(int v, int n, bool[] visited)
        {
            bool all_visited = true;

            for (int i = 0; i < n; i++) if (!visited[i]) all_visited = false;
            if (all_visited) return 1;

            int ret = 0;
            for (int i = 0; i < n; i++)
            {
                if (g[v][i] == false) continue;
                if (visited[i]) continue;
                visited[i] = true;
                ret+= dfs(i, n, visited);
                visited[i] = false;
            }
            return ret;
        }

        static void Main(string[] args)
        {
            const int nmax = 8;
            var s = Console.ReadLine().Split();
            int n = int.Parse(s[0]);
            int m = int.Parse(s[1]);
            g = new bool[8][];
            for (int i = 0; i < nmax; i++)
            {
                g[i] = new bool[nmax];
                for (int j = 0; j < nmax; j++)
                {
                    g[i][j] = false;
                }
            }
            for (int i = 0; i < m; i++)
            {
                var a = Console.ReadLine().Split().Select(int.Parse).ToArray();
                g[a[0] - 1][a[1] - 1] = true;
                g[a[1] - 1][a[0] - 1] = true;
            }
            var visited = new bool[nmax];
            for (int i = 0; i < n; i++) visited[i] = false;
            visited[0] = true;

            Console.WriteLine(dfs(0, n, visited));


        }
    }
}


Measured memory_bytes: 13396
//ITP1_4_A

using System;
using System.Collections.Generic;
/*using System.Linq;
using System.Text;
using System.Threading.Tasks;*/


class Program
{
    static void Main(string[] args)
    {
        //?????????????????????????????????int??????iaaray????????????????????\

        int[] iarray = Array.ConvertAll<string, int>((Console.ReadLine().Split(' ')), delegate (string value)
        {
            return int.Parse(value);
        });

        int a = iarray[0] / iarray[1];
        int b = iarray[0] % iarray[1];
        double c = ((double)iarray[0] / (double)iarray[1]);

        Console.WriteLine(a + " " + b + " " + c.ToString("0.000000"));
    }

}

Measured memory_bytes: 9336
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map> 
#include<fstream> 
using namespace std;
#define mid 1000000007
 map<int,int>c;
int main(){
	int k=1,sum=0,ans=0,i;
	string s;
	cin>>s;
	int a=s.size();
	for(i=a-1;i>=0;i--){//注意是从后先前取 
		sum=((s[i]-'0')*k+sum)%2019;
		c[sum]++;
		k=(k*10)%2019;
	}
	for(i=0;i<2019;i++){
		if(i==0)
		ans+=c[i];
		if(c[i]>=2)
		ans+=c[i]*(c[i]-1)/2; 
	}
	cout<<ans;
} 

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

int main()
{
	unsigned int a, b, c;
	cin >> a >> b >> c;

	cout << min(a + b + 1, c) + b << endl;

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <math.h>
#include <utility>
using namespace std;
#define ll long long
#define mod 1000000007
#define MAX 201000
ll factorial[MAX];
ll factorial2[MAX];
vector<pair<ll, ll> > soinsuu;

ll two (ll x, ll y) {
  if (y == 1) {
    return x % mod;  
  }  
  if (y % 2 == 0) {
    ll sub = two(x, y / 2);
    return ((sub % mod) * (sub % mod)) % mod;    
  }
  return ((two(x, y - 1) % mod) * (x % mod)) % mod;
}

void init (ll m) {
  ll now = 1;
  for (ll i = 0; i <= MAX; i++) {
    if (i == 0 || i == 1) {
      factorial[i] = 1;  
    }  
    else {
      factorial[i] = 
      ((factorial[i - 1] % mod) * (i % mod)) % mod;
    }
  } 
       
  now = 1;
  //
  for (ll i = 0; i <= MAX; i++) {
    if (i == 0 || i == 1) {
      factorial2[i] = 1;  
    }  
    else {
      factorial2[i] = two(factorial[i], mod - 2);    
    }
  }
}

int main () {
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  init(200001);
  
  // 最初の値について 
  ll ans = ((factorial[h + w - 2] % mod) * (factorial2[h - 1] % mod)) % mod;
  ans = ((ans % mod) * (factorial2[w - 1] % mod)) % mod;
  for (ll i = 1; i <= b; i++) {
    // i - 1横にずれる
    // w - 1 - a上に行く
    ll sub = 1;
    ll x = i - 1;
    ll y = h - 1 - a;
    if (x == 0 || y == 0) {
      sub = 1;    
    }  
    else {
      sub = ((factorial[x + y] % mod) * (factorial2[x] % mod)) % mod;
      sub = ((sub % mod) * (factorial2[y] % mod)) % mod;  
    }
    x = w - i;
    y = a - 1;
    if (x != 0 && y != 0) {
      sub = ((sub % mod) * (factorial[x + y] % mod)) % mod;  
      sub = ((sub % mod) * (factorial2[y] % mod)) % mod;  
      sub = ((sub % mod) * (factorial2[x] % mod)) % mod;  
    }
    ans = (ans + mod - sub) % mod;
  }
  cout << ans % mod << endl;
}


Measured memory_bytes: 3328
#pragma region Macros
#include <bits/stdc++.h>
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
#pragma endregion
int main()
{
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	string S;
	cin >> S;

	if (S == "SUN")
	{
		cout << 7 << '\n';
	}else if (S == "MON")
	{
		cout << 6 << '\n';
	}else if (S == "TUE")
	{
		cout << 5 << '\n';
	}else if (S == "WED")
	{
		cout << 4 << '\n';
	}else if (S == "THU")
	{
		cout << 3 << '\n';
	}else if (S == "FRI")
	{
		cout << 2 << '\n';
	}else if (S == "SAT")
	{
		cout << 1 << '\n';
	}
	return 0;
}


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

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

  int l = 1, r = n;
  rep(i,m){
    int li, ri;
    cin >> li >> ri;
    l = max(l, li);
    r = min(r, ri);
  }
  int ans = r - l + 1;
  cout << (ans > 0 ? ans : 0) << endl;
  return 0;
}


Measured memory_bytes: 3584
#include <iostream>
using namespace std;
int main() {
	int A, B;
	cin >> A >> B;
	if ((A + B) % 2 == 0) {
		cout << (A + B) / 2 << endl;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}
	return 0;
}

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

int main(){ 
  string s, t;
  cin >> s >> t;
  int a, b;
  cin >> a >> b;
  string u;
  cin >> u;
  if (s == u) a--;
  else b--;
  cout << a << " " << b << endl;
  return 0;
} 

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

int main(int argc, char const* argv[])
{
	int n, tmp;
	int imax = -1000000;
	int imin = 1000000;
	long long isum = 0;

	std::cin >> n;
	for (int i = 0; i < n; i++) {
		std::cin >> tmp;
		imax = std::max(imax, tmp);
		imin = std::min(imin, tmp);
		isum += tmp;
	}
	std::cout << imin << " " << imax << " " << isum << std::endl;
	return 0;
}



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

const int n = 26;
ll s[365][26];

int main(){
	ll d;
	cin >> d;
	vector<ll> c(n+1); //満足度低下
	REP(i,n) cin >> c[i];
	REP(i,d){
		REP(j,n){
			cin >> s[i][j];
		}
	}
	vector<ll> schedule(d+1);
	REP(i,d) {
		cin >> schedule[i];
		schedule[i];
	}
	vector<ll> score(d+1);
	vector<ll> last(n+1,0);
	ll score_d = 0;
	//iは何日目
	REP(i,d){
		score_d += s[i][schedule[i]];
		last[schedule[i]] = i;
		REP(j,n){
			score_d -= c[j]*(i - last[j]);
		}
		cout << score_d << endl;
	}
    return 0;
}



Measured memory_bytes: 3708
n = int(input())
mp = dict()
maxi = 0
for _ in range(n):
    s = input()
    if s in mp:
        mp[s] += 1
    else:
        mp[s] = 1
    maxi = max(maxi, mp[s])

ans = []
for x in mp.items():
    if x[1] == maxi:
        ans.append(x[0])

ans.sort()
for x in ans:
    print(x)


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

typedef long long int LL;
typedef long long int LLint;
typedef pair<int,int> intpair;
typedef pair<bool,bool> boolpair;
typedef pair<string,string> strpair;
typedef pair<LL,LL> LLpair;
typedef pair<double,double> doublepair;
typedef pair<float,float> floatpair;
typedef vector<int> intvector;
typedef vector<bool> boolvector;
typedef vector<string> strvector;
typedef vector<LL> LLvector;
typedef vector<double> doublevector;
typedef vector<float> floatvector;
#define ED return 0;
#define UP(a,b) ((a+(b-1))/b)
#define SORT(vec) sort(vec.begin(),vec.end());
#define DOUBLECHANGE(count) cout << setprecision(count);
#define REV(vec) reverse(vec.begin(),vec.end());
#define ipow(x,y) LL(pow(x,y))
#define INF 999999999
const long long mod = 1000000007;

long long int primeNums(long long int N){
    long long int num = N;
    long long int ans = 0;
    for(long long int i = 2;i <= sqrt(N);i++){
        while(num % i == 0){
            ans++;
            num /= i;
        }
        if(num == 1){
            return ans;
        }
    }
    return ans + 1;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    LL answer = 0;
    string number;
    cin >> number;
    vector<bool> check (number.size()-1,0);
    for(int i = 0;i < ipow(2,number.size()-1);i++){
        if(i != 0){
            bool cont = true;
            for(int j = 0;cont;j++){
                if(check.at(j) == 1){
                    check.at(j) = 0;
                    continue;
                }
                else{
                    check.at(j) = 1;
                    cont = false;
                }
            }
        }
        int ten = 0;
        long long int num = stoll(number);
        for(int i = number.size() - 1;i > -1;i--){
            answer += (num / ipow(10,number.size()-1-i) % 10) * pow(10,ten);
            if(i != 0 && check.at(i-1)){
                ten = 0;
            }
            else{
                ten++;
            }
        }
    }
    cout << answer << endl;
}


Measured memory_bytes: 256
# ALDS1_5_D The Number of Inversions
import sys
count = 0


def mearge(A, left, p, right):
    global count
    l = A[left: p]
    r = A[p: right]

    l.append(float('inf'))
    r.append(float('inf'))

    i = 0
    j = 0

    for k in range(left, right, 1):
        if l[i] <= r[j]:
            A[k] = l[i]
            i += 1
            count += j
        else:
            A[k] = r[j]
            j += 1


def mearge_sort(A, left, right):
    if left + 1 < right:
        p = (left + right) // 2
        mearge_sort(A, left, p)
        mearge_sort(A, p, right)
        mearge(A, left, p, right)


n = int(input())
A = list(map(int, sys.stdin.readline().strip().split()))
mearge_sort(A, 0, n)
print(count)



Measured memory_bytes: 27476
#include<stdio.h>
#include<math.h>

typedef struct{
  double x;
  double y;
}Point;

void fractal(int,Point,Point);

int main(){

  Point p1,p2;
  int n;

  p1.x = 0;
  p2.x = 100;
  p1.y = 0;
  p2.y = 0;

  scanf("%d",&n);

  printf("%f %f\n",p1.x,p1.y);
  fractal(n,p1,p2);
  printf("%f %f\n",p2.x,p2.y);

  return 0;
}

void fractal(int n, Point p1, Point p2){

  if(n==0) return;
  Point a,b,c;

  a.x = (p2.x-p1.x)/3+p1.x;
  a.y = (p2.y-p1.y)/3+p1.y;
  b.x = 2*(p2.x-p1.x)/3+p1.x;
  b.y = 2*(p2.y-p1.y)/3+p1.y;
  c.x = (b.x-a.x)/2-sqrt(3)/2*(b.y-a.y)+a.x;
  c.y = (b.y-a.y)/2+sqrt(3)/2*(b.x-a.x)+a.y;

  fractal(n-1,p1,a);
  printf("%f %f\n",a.x,a.y);

  fractal(n-1,a,c);
  printf("%f %f\n",c.x,c.y);

  fractal(n-1,c,b);
  printf("%f %f\n",b.x,b.y);

  fractal(n-1,b,p2);
}

Measured memory_bytes: 636
import java.io.*;

class Main{
	public static void main(String args[]) throws IOException{
		BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
		while(true){
			String[] str = buf.readLine().split(" ");
			int H = Integer.parseInt(str[0]);
			int W = Integer.parseInt(str[1]);

			if(W==0 && H==0)
				break;
			for(int i=0; i<H; i++){
				for(int j=0; j<W; j++){
					if(i==0 || j==0 || j==W-1 || i==H-1 )
						System.out.print("#");
					else
						System.out.print(".");
				}
				System.out.println();
			}
			System.out.println();
		}		
	}
}

Measured memory_bytes: 30328
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define sum(n)((n)*(n+1)/2)
#define INF ((1LL<<62)-(1LL<<31))
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}

ll a[200010];
ll ans;
int main(){
	int n;
	scanf("%d",&n);
	rep(i,0,n)scanf("%lld",a+i+1);
	rep(i,1,n+1)a[i]+=a[i-1];
	sortup(a,n+1);
	a[n+1]=-INF;
//	rep(i,0,n+2)printf("%lld ",a[i]);
	for(int l=0;l<=n+1;){
		ll r=l;
		while(a[l]==a[r])r++;
		ans+=sum(r-l-1);
		l=r;
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 3324
#include <cstdio>
#include <cmath>
#include <algorithm>

int main()
{
    int H, W, h, w;
    scanf("%d %d", &H, &W);
    scanf("%d %d", &h, &w);
    int ans = H * W - h * W - w * H + h * w;
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define ll long long
#define mod 1000000007
using namespace std;
int n, m, lst, a[32][3][3], b[3][3], ans[3][3], t[3][3];
inline void upd(int &x, int y) { x += y; if(x >= mod) x -= mod; }
inline void matrix_mul33(int a[3][3], int b[3][3], int c[3][3]) {
	memset(t, 0, sizeof t);
	rep(i, 0, 2) rep(j, 0, 2) rep(k, 0, 2) upd(t[i][j], (ll)a[i][k]*b[k][j]%mod);
	rep(i, 0, 2) rep(j, 0, 2) c[i][j] = t[i][j];
}
inline void matrix_mul31(int a[3][3], int b[3][3], int c[3][3]) {
	memset(t, 0, sizeof t);
	rep(i, 0, 2) rep(j, 0, 2) upd(t[i][0], (ll)a[i][j]*b[j][0]%mod);
	rep(i, 0, 2) c[i][0] = t[i][0];
}
inline void matrix_ksm(int p, int x[3][3]) { rep(i, 0, 29) if(p>>i&1) matrix_mul31(a[i], x, x); }
int main() {
	//a[i][j]表示可以放隔板的转移矩阵，b[i][j]表示不可以放隔板
	a[0][0][0] = 1; a[0][0][1] = 0; a[0][0][2] = 1;
	a[0][1][0] = 2; a[0][1][1] = 1; a[0][1][2] = 2;
	a[0][2][0] = 1; a[0][2][1] = 1; a[0][2][2] = 2;
	b[0][0] = 1; b[0][1] = 0; b[0][2] = 0;
	b[1][0] = 2; b[1][1] = 1; b[1][2] = 0;
	b[2][0] = 1; b[2][1] = 1; b[2][2] = 1;
	rep(i, 1, 30) matrix_mul33(a[i-1], a[i-1], a[i]);//预处理2的幂次，优化复杂度
	ans[0][0] = 1;
	scanf("%d%d", &n, &m); lst = -1;
	rep(i, 1, m) {
		int x; scanf("%d", &x);
		matrix_ksm(x-lst-1, ans); matrix_mul31(b, ans, ans);
		lst = x;
	}
	matrix_ksm(n-lst-1, ans);
	printf("%d\n", ans[2][0]);
	return 0;
}

Measured memory_bytes: 256
import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    //nextLineとnextを同時に使ったら死ぬ
    val sc = new Scanner(System.in)

    val n = sc.nextInt()
    val as = Array.fill(n)(sc.nextInt())

    println(as.fold(as.head)((a, b) => gcd(a, b)))

    @scala.annotation.tailrec
    def gcd(a: Int, b: Int): Int = {
      if (b == 0) a else gcd(b, a % b)
    }
  }
  def debug(args: Any*): Unit = {
    Console.err.println(args.mkString(" "))
  }
}


Measured memory_bytes: 54344
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

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

int main() {
	int x; cin >> x;
	cout << 360 / gcd(360, x) << endl;
	return 0;
}

Measured memory_bytes: 3608
#include <iostream>
#include <vector>

using namespace std;

bool Valid(int num, const vector<bool> &disliked)
{
    do {
        if (disliked[num % 10]) {
            return false;
        }
        num /= 10;
    } while (num > 0);
    return true;
}

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

    vector<bool> disliked(10, false);
    for (int i = 0; i < k; i += 1) {
        int digit;
        cin >> digit;
        disliked[digit] = true;
    }

    while (!Valid(n, disliked)) {
        n += 1;
    }

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


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

#define int long long

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

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

string a="AKIHABARA";

bool ok(string s){
    int pos=0;
    rep(i,a.size()){
        if(pos!=s.size()&&a[i]==s[pos])pos++;
        else{
            if(a[i]=='A'){
                continue;
            }
            return false;
        }
    }
    return pos==s.size();
}

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

    if(ok(s))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
int main(){
  int i,j,nS,nT,q,count;
  int S[10000];
  scanf("%d",&nS);
  for(i=0;i<nS;i++){
    scanf("%d",&S[i]);
  }
  scanf("%d",&nT);
  count = 0;
  for(i=0;i<nT;i++){
    scanf("%d",&q);
    j = 0;
    while(S[j] != q && j<nS){
      j++;
    }
    if( S[j] == q){
      count++;
    }
  }
  printf("%d\n",count);
  return 0;
}

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

int main()
{
	int a,b,c,d,sum=0;
	string s;

	cin>>s;
    if(s=="A" )
        cout<<"T";
    else if(s=="C" )
        cout<<"G";
    else if(s=="T")
        cout<<"A";
    else
    {
        cout<<"C";
    }
    
	
	return 0;
}
	

Measured memory_bytes: 256
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

int main(void){
	int n, k, t, m;
	while(true){
		scanf("%d %d", &n, &k);

		if(n == 0 && k == 0) return 0;

		vector<int> a;

		for(int i = 0;i < n;++i){
			scanf("%d", &t);
			a.push_back(t);
		}

		m = 0;
		for(int i = 0;i < n-k;++i){
			t = 0;
			for(int j = 0;j < k;++j){
				t += a[i+j];
			}
			m = max(m, t);
		}

		printf("%d\n", m);
	}
}


Measured memory_bytes: 2952
#include <iostream>

using namespace std;

int main()
{
    int n,x,l,c=1,sum=0;
    cin>>n>>x;
    for(int i=0;i<n;i++){
    cin>>l;
   sum+=l;
    if(sum<=x){
       c++;}
    }
    cout<<c<<endl;
   return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
signed main(){
    string s;cin>>s;
    if(s[1]=='B')s[1]='R';
    else s[1]='B';
    cout<<s<<endl;
}

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

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

  int sum = 0;
  int small = 1 + L -1;
  int temp;

  for (int i = 1; i <= N; i++) {
    temp = i + L - 1;
    if (abs(temp) < abs(small)) {
      small = temp;
    }
    sum += (i + L - 1);
  }

  cout << sum - small << endl;

  return 0;
}

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


int X, Y, A, B, C;
vector<int> p, q, r;


void input() {
    cin >> X >> Y >> A >> B >> C;
    p = vector<int>(A);
    rep(i, A) cin >> p[i];
    q = vector<int>(B);
    rep(i, B) cin >> q[i];
    r = vector<int>(C);
    rep(i, C) cin >> r[i];
}


int main() {
    input();
    sort(p.rbegin(), p.rend());
    sort(q.rbegin(), q.rend());
    sort(r.rbegin(), r.rend());
    
    ll ans = 0;
    priority_queue<int, vector<int>, greater<int>> pq;
    rep(i, X) {
        pq.push(p[i]);
        ans += p[i];
    }
    rep(i, Y) {
        pq.push(q[i]);
        ans += q[i];
    }

    rep(i, C) {
        if (pq.top() < r[i]) {
            ans += r[i] - pq.top();
            pq.pop();
            pq.push(r[i]);
        }
    }
    cout << ans << endl;
}


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

using namespace std;

int main()
{
    int n, m;
    
    while (scanf("%d %d", &n, &m) && n){
        int s[256];
        int s1 = 0, s2 = 0;
        for (int i = 0; i < n; i++){
            scanf("%d", &s[i]);
            s1 += s[i];
        }
        for (int i = 0; i < m; i++){
            scanf("%d", &s[n + i]);
            s2 += s[n + i];
        }
        
        int p1 = -1, p2 = -1, minsum = 99999;
        for (int i = 0; i < n; i++){
            for (int j = n; j < m + n; j++){
                if (s1 - s[i] + s[j] == s2 - s[j] + s[i] && s[i] + s[j] < minsum){
                    p1 = s[i];
                    p2 = s[j];
                    minsum = s[i] + s[j];
                }
            }
        }
        
        if (~p1) printf("%d %d\n", p1, p2);
        else printf("-1\n");
    }
    return (0);
}

Measured memory_bytes: 1196
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int,int> P;
int N,M,C,S,G;

int dp[110] = {};
int g[21][110][110] = {};
int di[21][110][110] = {};
int p[110],q[21][51],r[21][51];
int cost[21][10010] = {};
int inf = 1e9;
vector<vector<P>> v(110);

int calc(int dist,int company){
    if(dist<0) return 0;
    if(dist<=10000) return cost[company][dist];
    else return cost[company][dist]+q[company][p[company]]*(dist-10000);
}

void init(){
    for(int i=1;i<=N;i++) v[i].clear();
    for(int i=1;i<=C;i++) for(int j=1;j<=10000;j++) cost[i][j] = 0;
    for(int i=1;i<=N;i++) dp[i] = inf;
    for(int t=1;t<=C;t++) for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) {di[t][i][j] = inf; g[t][i][j] = 0;}
}

int main(){
    while(cin >> N >> M >> C >> S >> G && N){
        int x,y,d,c;
        init();
        for(int i=0;i<M;i++){
            cin >> x >> y >> d >> c;
            di[c][x][y] = min(di[c][x][y],d);
            di[c][y][x] = min(di[c][y][x],d);
            g[c][x][y] = 1; g[c][y][x] = 1;
        }
        for(int t=1;t<=C;t++) for(int k=1;k<=N;k++)
        for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) di[t][i][j] = min(di[t][i][j],di[t][i][k]+di[t][k][j]);
        for(int i=1;i<=C;i++) cin >> p[i];
        for(int t=1;t<=C;t++){
            for(int i=1;i<=p[t]-1;i++) cin >> q[t][i];
            q[t][p[t]] = 10001;
            for(int i=1;i<=p[t];i++){
                cin >> r[t][i];
                for(int j=q[t][i-1]+1;j<=q[t][i];j++) cost[t][j] += r[t][i];
            }
            for(int i=1;i<=10000;i++) cost[t][i] += cost[t][i-1];
        }
        for(int t=1;t<=C;t++) for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++) if(di[t][i][j]!=inf) v[i].emplace_back(calc(di[t][i][j],t),j);
        dp[S] = 0;
        priority_queue<P,vector<P>,greater<P>> Q;
        Q.push({0,S});
        while(!Q.empty()){
            P p = Q.top(); Q.pop();
            if(dp[p.second]<p.first) continue;
            for(auto x:v[p.second]){
                if(dp[x.second]>p.first+x.first){
                    dp[x.second] = p.first+x.first;
                    Q.push({dp[x.second],x.second});
                }
            }
        }
        cout << (dp[G]!=inf? dp[G]:-1) << endl;
    }
}


Measured memory_bytes: 7312
#include<stdio.h>

int main()
{
	int n,i,t,twos;
	twos = 0;
	scanf("%d", &n);
	for (i = 0; i < n; i ++) {
		scanf("%d", &t);
		while (t / 2 * 2 == t) {
			twos ++;
			t = t / 2;
		}
	}
	printf("%d", twos);
	return 0;
}


Measured memory_bytes: 128
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

// {price, num}
vector<pair<ll, ll>> shops;

int main(void) {
    int N, M;
    cin >> N >> M;

    shops.resize(N);
    for (int i = 0; i < N; i++) {
        ll a, b;
        cin >> a >> b;
        shops[i] = {a, b};
    }
    sort(shops.begin(), shops.end());

    ll cnt = 0;
    ll sum_price = 0;
    for (int i = 0; i < N; i++) {
        auto shop = shops[i];
        if (cnt + shop.second >= M) {
            ll require_cnt = M - cnt;
            sum_price += require_cnt * shop.first;
            break;
        } else {
            sum_price += shop.first * shop.second;
            cnt += shop.second;
        }
    }

    cout << sum_price << endl;

    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll<<61;
double pi = 3.141592653589793238;

int main() {
  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a + b > c + d) cout << "Left" << endl;
  else if (a + b < c + d) cout << "Right" << endl;
  else cout << "Balanced" << endl;

  return 0;
}


Measured memory_bytes: 3620
L, R = list(map(int, input().split()))
m = R//2019
if R-L>=2019 or (m*2019<=R and m*2019>=L):
  print(0)
else:
  l = L%2019
  r = R%2019
  min_c = 2019
  for i in range(l,r+1):
    for j in range(i+1, r+1):
      score = i*j%2019
      if i*j%2019<min_c:
        min_c = score
  print(min_c)

Measured memory_bytes: 3060
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
using namespace std;
#define SQR(x) ((x)*(x))
#define PI acos(-1.)
#define INF (INT_MAX/100)
#define mid(x,y) (x+y)/2
#define ll long long
#define sz(v) ((int)(v).size())
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repf(i, a, b) for (int i=(a); i<=(b); ++i)
#define repd(i, a, b) for (int i=(a); i>=(b); --i)
#define fill(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define pf push_front
#define itr iterator

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int,int> pii;
typedef map<int,int> mii;

struct Mat{
    int it[4];
    bool input(){
        rep(i, 4) if (scanf("%d", &it[i])==EOF) return false;
        return true;
    }
};

int i,j,k,m,n,l;
Mat a, b;


int main(){
    while (a.input()){
        b.input();
        int x=0 ,y=0;
        rep(i, 4)
            rep(j, 4) if (a.it[i]==b.it[j]){
                if (i==j) x++;
                else y++;
            }
        printf("%d %d\n", x, y);
    }

    return 0;
}

Measured memory_bytes: 1188
import java.lang.*;
import java.io.*;
import java.util.*;


public class Main {
	
	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
}

class TaskA {
	
	public void solve(InputReader in, PrintWriter out) {
		int n = in.nextInt(), x;
		int[] a = new int[n+1];
		int[] visit = new int[n+1];
		Queue<Integer> Q = new ArrayDeque<Integer>();
		int ans = 0;
		
		for (int i=1; i<=n; ++i) {
			a[i] = in.nextInt();
		}
		
		Arrays.fill(visit, -1);
		visit[1] = 0;
		Q.add(1);
		while (!Q.isEmpty()) {
			x = Q.poll();
			if (visit[a[x]] == -1) {
				if (a[x] == 2) {
					ans = visit[x] + 1;
					out.println(ans);
					return;
				}
				visit[a[x]] = visit[x] + 1;
				Q.add(a[x]);
			}
		}
		
		out.println(-1);
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;
	
	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}
	
	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}
	
	public int nextInt() {
		return Integer.parseInt(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

Measured memory_bytes: 38968
#include <stdio.h>
#define MAX 100000
#define SENTINEL 2000000000

struct Card {
    char suit;
    int value;
};

struct Card L[MAX / 2 + 2], R[MAX / 2 + 2];
void merge (struct Card A[], int n, int left, int mid, int right) {
    int i, j, k;
    int n1 = mid -left;
    int n2 = right - mid;
    for ( i = 0; i < n1; i++) L[i] = A[left + i];
    for ( i = 0; i < n2; i++) R[i] = A[mid + i];
    L[n1].value = R[n2].value = SENTINEL;
    i = j = 0;
    for (k = left; k < right; k++) {
        if (L[i].value <= R[j].value) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
        }
    }
}

void mergeSort (struct Card A[], int n, int left, int right) {
    int mid;
    if (left + 1 < right) {
        mid = (left + right) / 2;
        mergeSort(A, n, left, mid);
        mergeSort(A, n, mid, right);
        merge(A,n,left,mid,right);
    }
}

int partition(struct Card A[], int n, int p, int r) {
    int i, j;
    struct Card t, x;
    x = A[r];
    i = p - 1;
    for ( j = p; j < r; j++) {
        if ( A[j].value <= x.value) {
            i++;
            t = A[i]; A[i] = A[j]; A[j] = t;
        }
    }
    t = A[i + 1]; A[i + 1] = A[r]; A[r] = t;
    return i + 1;
}

void quickSort (struct Card A[], int n, int p, int r) {
    int q;
    if (p < r) {
        q = partition(A, n, p, r);
        quickSort(A, n, p, q - 1);
        quickSort(A, n, q + 1, r);
    }
}

int main () {
    int i, n, v;
    struct Card A[MAX], B[MAX];
    char S[10];
    int stable = 1;

    scanf("%d", &n);

    for ( i = 0; i < n; i++) {
        scanf("%s %d", S, &v);
        A[i].suit = B[i].suit = S[0];
        A[i].value = B[i].value = v;
    }

    mergeSort(A, n, 0, n);
    quickSort(B, n, 0, n - 1);

    for (i = 0; i < n; i++) {
        if (A[i].suit != B[i].suit) stable = 0;
    }

    if (stable == 1) printf("Stable\n");
    else printf("Not stable\n");

    for (i = 0; i < n; i++) {
        printf("%c %d\n", B[i].suit, B[i].value);
    }

    return 0;
}


Measured memory_bytes: 4068
#include<bits/stdc++.h>
#define prArr(Arr,n) for(int _=0;_<n;_++)   cout<<(Arr)[_]<<" ";
using namespace std;
const int INF=(int)0x3f3f3f3f;
const int MOD=(int)1e9+7;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin>>s;
    if(s=="ABC")
        cout<<"ARC";
    else
        cout<<"ABC";
    return 0;
}


Measured memory_bytes: 3652
#include<cstdio>
int max(int a, int b, int c) {
	if (a < b)return b < c ? c : b;
	else return a < c ? c : a;
}
int sum[2000][2000];
int main() {
	int n, k, i, j, x, y, tmp, m=0;
	char c;
	scanf("%d%d", &n, &k);
	for (i = 0; i < 2*k; i++) {
		for (j = 0; j < 2*k; j++) {
			sum[i][j] = 0;
		}
	}
	for (i = 0; i < n; i++) {
		scanf("%d %d %c", &x, &y, &c);
		if (c == 'B')x += k;
		x = x % (2 * k);
		y = y % (2 * k);
		sum[x][y]++;
	}
	for (i = 2 * k - 2; i >= 0; i--) {
		sum[2 * k - 1][i] += sum[2 * k - 1][i + 1];
		sum[i][2 * k - 1] += sum[i + 1][2 * k - 1];
	}
	for (i = 2 * k - 2; i >= 0; i--) {
		for (j = 2 * k - 2; j >= 0; j--) {
			sum[i][j] += sum[i + 1][j] + sum[i][j + 1] - sum[i + 1][j + 1];
		}
	}
	for (i = 0; i < k; i++) {
		for (j = 0; j < k; j++) {
			tmp = sum[i][j] - sum[i + k][j] - sum[i][k + j] + sum[i + k][j + k];
			tmp = sum[i][0] + sum[0][j] - sum[i + k][0] - sum[0][j + k] - 2*tmp;
			m = max(tmp, n-tmp, m);
		}
	}
	printf("%d", m);
	return 0;
}

Measured memory_bytes: 15744
#include <iostream>
using namespace std;
//ITP1_7_B

int ans;

int main() {
	int n, x;
	while (1){
		ans = 0;
		cin >> n >> x;
		if (n == 0 && x == 0) break; 
		for (int i = 1; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				for (int k = j + 1; k <= n; k++) {
					if (i + j + k == x) ans++;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 1160
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int A;
    int B;
    int C;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        A = sc.nextInt();
        B = sc.nextInt();
        C = sc.nextInt();
    }
    
    //解答処理
    private boolean solve() {
        boolean judge = false;
        if ((A < C && C < B) || (B < C && C < A)) {
        	judge = true;
        }
        
        return judge;
    }
    
    //出力
    public void show(boolean answer) {
        if (answer) {
        	System.out.println("Yes");
        } else {
        	System.out.println("No");
        }
    }
}


Measured memory_bytes: 35696
// Undone
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
public class Program {
  public static void Main() {
    int[] a = new int[5];
    a[0] = cin(); a[1] = cin(); a[2] = cin(); a[3] = cin(); a[4] = cin();
    int p = 10;
    for (int i = 0; i < 5; i++) {
      if (a[i] % 10 < p && a[i] % 10 != 0) p = a[i] % 10;
    }
    int ans = 0;
    for (int i = 0; i < 5; i++) {
      ans += a[i];
      if (a[i] % 10 != 0) ans += 10 - a[i] % 10;
    }
    ans -= 10-p;

    Console.WriteLine(ans);
  }

  private static List<int> _var;
  public static int cin() { if (_var == null || _var.Count() == 0) { _var = Console.ReadLine().Split(' ').Select(int.Parse).ToList<int>(); } int ans = _var[0]; _var.RemoveAt(0); return ans; }
  public static void Swap<T>(ref T a, ref T b) {T temp = a;a = b;b = temp;}
  public static string ReadStr() { return Console.ReadLine(); }
  public static int ReadInt() { return int.Parse(Console.ReadLine()); }
  public static double ReadDouble() { return double.Parse(Console.ReadLine()); }
  public static long ReadLong() { return long.Parse(Console.ReadLine()); }
  public static char ReadChar() { return Console.ReadLine()[0]; }
  public static string[] ReadStringArray() { return Console.ReadLine().Split(' '); }
  public static int[] ReadIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
  public static double[] ReadDoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
  public static long[] ReadLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
  public static char[] ReadCharArray() { return Console.ReadLine().ToArray(); }
  public static string[] ReadCharStringArray() { return Console.ReadLine().ToCharArray().Select(c => c.ToString()).ToArray(); }
}


Measured memory_bytes: 11348
import java.util.Scanner;
class Main{
	public static void main(String[]args) {
		Scanner scan=new Scanner(System.in);
		int a=scan.nextInt();
		int b=scan.nextInt();
		int c=scan.nextInt();
		boolean bool=false;
		for(int i=0;i<=a;i++) {
			for(int j=0;j<=b;j++) {
				int bla=(a-i)*j+(b-j)*i;
				if(bla==c) {
					bool=true;
				}
			}
		}
		if(bool==true) {
			pl("Yes");
		}
		else {
			pl("No");
		}
	}
	public static void pl(String str) {
		System.out.println(str);
	}
}


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

using namespace std;

using ll = long long;
void max_self(ll &a ,ll b){
    a = max(a,b);
}

void solve(){
int n , w;
cin>>n>>w;

vector<ll> dp(w+1); // 0,1,2............w
// dp[i] = maximum value of items with total weight 



for(int item =0;item<n;item++){
    ll weight , value;
    cin>>weight>>value;
    for(int w_a = w-weight;w_a>=0;w_a--){


        max_self(dp[w_a + weight] , dp[w_a] + value);


    }
}
ll  answer =0;
for(int i =0;i<=w;i++){
    max_self(answer,dp[i]);
}

cout<<answer;

}


int main(){

    solve();
}

Measured memory_bytes: 1024
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <ctime>
#include <list>
using namespace std;
static const double EPS = 1e-6;
typedef long long ll;
typedef pair<int,int> PI;
#ifndef M_PI
const double M_PI=acos(-1);
#endif
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int((a).size()))
#define F first
#define S second
int dx[]={0,1,0,-1,1,1,-1,-1},dy[]={1,0,-1,0,1,-1,1,-1};

string in;
int p;

void next(int& p){
  ++p;
  while(p<SZ(in) && in[p]!=')'){
    if(in[p]=='(')next(p);
    else ++p;
  }
  ++p;
}

ll get(int po,int re){
  ll le=0;
  if(in[po]!='(')return 1;
  ++po;
  while(po<SZ(in) && in[po]!=')'){
    if(isdigit(in[po])){
      ll t=0;
      while(isdigit(in[po]))t=t*10+in[po++]-'0';
      ll cle=get(po,re);
      if(cle==-1)return -1;
      le+=cle*t;
      if(in[po]=='(')next(po);
      else ++po;
    }else{
      ++le;
      ++po;
    }
    if(le>re)return -1;
  }
  return le;
}

char rec(int po,int re){
  while(po<SZ(in)){
    //cout<<po<<' '<<re<<endl;
    if(isdigit(in[po])){
      ll t=0;
      while(isdigit(in[po]))t=t*10+in[po++]-'0';
      ll len=get(po,re);
      //cout<<len<<endl;
      int de=0;
      if(in[po]=='(')++de;
      if(len==-1)return rec(po+de,re);
      else if(re<t*len)return rec(po+de,re%len);
      re-=t*len;
      //cout<<re<<endl;
      if(de)next(po);
      else ++po;
    }else{
      if(re==0)return in[po];
      ++po;
      --re;
    }
  }
  ++po;
  return '0';
}



main(){
  while(cin>>in>>p,in!="0")
    cout<<rec(0,p)<<endl;
}

Measured memory_bytes: 0
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <math.h>
typedef long long int ll;
#define FOR(i,n,m) for (int i=(n);i<=(m);i++)
#define si(n) fscanf(in,"%d",&n)
#define NM 200005
#define MOD 1000000007
#define INF 0x7fffffff
//FILE *in=fopen("input.txt","r"), *out = fopen("output.txt", "w");
FILE *in = stdin, *out = stdout;
using namespace std;

char a[NM];
int n, place[NM][30], len[NM];
void input() {
	fscanf(in, "%s", &a[1]);
	n = strlen(a + 1);
}
void pro() {
	FOR(j, 0, 25) place[n + 1][j] = n + 1;
	for (int i = n; i >= 1; i--) {
		FOR(j, 0, 25) place[i][j] = place[i + 1][j];
		place[i][a[i] - 'a'] = i;

		int MAX = 0;
		FOR(j, 0, 25) MAX = max(MAX, place[i][j]);
		if (MAX == n + 1) len[i] = 0;
		else len[i] = len[MAX + 1] + 1;
	}
	int ans_len = len[1] + 1;
	int i = 1;
	for (int left = ans_len; left >= 1; left--) {
		FOR(j, 0, 25) {
			if (place[i][j] == n + 1 || len[place[i][j] + 1] != left - 1) {
				i = place[i][j] + 1;
				fprintf(out, "%c", j + 'a');
				break;
			}
		}
	}
}
int main() {
	input();
	pro();
	return 0;
}

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

int main()
{
    int N;
    cin >> N;
    string S;
    cin >> S;
    vector<int> R;
    vector<int> G;
    vector<int> B;
    for(int i=0; i<N; i++){
        if     (S[i]=='R') R.push_back(i);
        else if(S[i]=='G') G.push_back(i);
        else if(S[i]=='B') B.push_back(i);
    }
    vector<vector<int>> tmp = {R, G, B};
    sort(tmp.begin(), tmp.end(), [&](const vector<int>& a, const vector<int>& b){ return a.size() < b.size(); });
    vector<int> A1 = tmp[0];
    vector<int> A2 = tmp[1];
    set<int> A3(tmp[2].begin(), tmp[2].end());

    long long res = 0;
    for(auto i : A1){
        for(auto j : A2){
            int nbad = 0;
            if(i<j){
                if(A3.count(j+j-i)) nbad++; //ijk
                if(A3.count(i-(j-i))) nbad++; //kij
                if((j-i)%2==0 && A3.count(i+(j-i)/2)) nbad++; //ikj
            }else{
                if(A3.count(i+i-j)) nbad++; //jik
                if(A3.count(j-(i-j))) nbad++; //kji
                if((i-j)%2==0 && A3.count(j+(i-j)/2)) nbad++; //jki
            }
            res += A3.size() - nbad;
        }
    }
    cout << res << endl;

    return 0;
}

Measured memory_bytes: 3656
// AFTER EDITORIAL

#include <cstdio>
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <numeric>

using namespace std;

// cf. codeforces/edu/ex273169A/main.cpp
struct SegmentTree {
  int n_;
  int depth_;
  vector<int> data_;

  SegmentTree(int n) {
    n_ = n;
    depth_ = 0;
    while ((1 << depth_) < n_) {
      depth_++;
    }
    // data_.resize(1 << (depth_ + 1));
    data_.assign(1 << (depth_ + 1), 0);
  }

  void print() {
    puts("[print]");
    printf("n: %d, depth: %d\n", n_, depth_);
    for (int k = 0; k <= depth_; k++) {
      int i0 = (1 << k) - 1;
      for (int i = 0; i < (1 << k); i++) {
        printf("%d ", data_[i0 + i]);
      }
      puts("");
    }
  }

  void set(int qi, int qv) {
    int i0 = (1 << depth_) - 1 + qi;
    data_[i0] = qv;
    int i = i0;
    while (i > 0) {
      i = ((i + 1) / 2) - 1;
      data_[i] = data_[2 * i + 1] + data_[2 * i + 2];
    }
  }

  // TODO: It seems there's a counting loss when ql = 0 (cf. handmade04)
  int sum(int ql, int qr) {
    int result = 0;
    vector<tuple<int, int, int>> stack(depth_ + 1, {0, 0, 0}); // Use "depth + 1" for the worst case
    int ptr = 0;
    stack[ptr++] = {0, 0, 1 << depth_};
    while (ptr > 0) {
      auto [i, l, r] = stack[--ptr];
      #ifdef DEBUG
        printf("[sum] i, l, r: %d, %d, %d\n", i, l, r);
      #endif
      if (ql <= l && r <= qr) {
        result += data_[i];
        continue;
      }
      if (qr <= l || r <= ql) {
        continue;
      }
      stack[ptr++] = {2 * i + 1, l, (l + r) / 2};
      stack[ptr++] = {2 * i + 2, (l + r) / 2, r};
    }
    return result;
  }
};

int main() {
  //
  // Input
  //
  int n, m;
  cin >> n >> m;

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

  vector<tuple<int, int>> qs(m, {0, 0});
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    qs[i] = {l, r};
  }

  // Sort queries
  vector<int> qs_argsort(m, 0);
  iota(qs_argsort.begin(), qs_argsort.end(), 0);
  sort(qs_argsort.begin(), qs_argsort.end(), [&](auto x, auto y){ return get<1>(qs[x]) < get<1>(qs[y]); });

  #ifdef DEBUG
    puts("[qs_argsort]");
    for (auto k : qs_argsort) {
      auto [l, r] = qs[k];
      cout << l << " " << r << endl;
    }
  #endif

  vector<int> type_to_position(n + 1, -1); // Type is 1..n
  SegmentTree tree(n);
  #ifdef DEBUG
    tree.print();
  #endif

  vector<int> result(m, 0);
  int r0 = 0;
  for (int k = 0; k < m; k++) {
    auto [ql, qr] = qs[qs_argsort[k]];
    for (int i = r0; i < qr; i++) {
      int v = ls[i];
      int i0 = type_to_position[v];
      if (i0 >= 0) {
        tree.set(i0, 0);
      }
      type_to_position[v] = i;
      tree.set(i, 1);
    }
    r0 = qr;
    #ifdef DEBUG
      tree.print();
    #endif
    result[qs_argsort[k]] = tree.sum(ql - 1, qr);
  }

  for (auto x : result) {
    cout << x << endl;
  }

  return 0;
}

Measured memory_bytes: 19588