akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using ll= long long;
using namespace std;

int main(){
    int N;
    cin>>N;
    vector<ll>M(5,0);
    string S;
    rep(i,N){
        cin>>S;
        if(S[0]=='M'){
            M[0]++;
        }
        else if(S[0]=='A'){
            M[1]++;
        }
        else if(S[0]=='R'){
            M[2]++;
        }
        else if(S[0]=='C'){
            M[3]++;
        }
        else if(S[0]=='H'){
            M[4]++;
        }
    }
    ll sum=0;
    rep(i,3){
        for(int j=i+1;j<4;j++){
            for (int k=j+1;k<5;k++){
                sum+=(ll) M[i]*M[j]*M[k];
            }
        }
    }
    cout<<sum<<endl;
    
    }
    
    
  
 
 




Measured memory_bytes: 3600
#include <cstdio>

typedef long long LL;

const int MAXN=100005;

void fix_down(int *h,int i,int n)
{
    int j=i,k,t=h[i];
    while ((j<<1)+1<n)
    {
        k=(j<<1)+1;
        if (k+1<n&&h[k+1]>h[k]) k=k+1;
        if (t>=h[k]) break;
        h[j]=h[k];
        j=k;
    }
    h[j]=t;
}

int main()
{
    int N,M,t;
    LL ans=0LL;
    static int h[MAXN];

    scanf("%d%d",&N,&M);
    for (int i=0;i<N;++i)
        scanf("%d",&h[i]);

    for (int i=N-1;i>=0;--i)
        fix_down(h,i,N);
    for (int i=0;i<M;++i)
    {
        if (h[0]==0) break;
        h[0]=h[0]>>1;
        fix_down(h,0,N);
    }
    for (int i=0;i<N;++i)
        ans=ans+h[i];
    printf("%lld",ans);

    return 0;
}



Measured memory_bytes: 512
#include <stdio.h>

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

int main(){
	int w,n;
	int a,b;
	int c[31];
	int i,j;
	scanf("%d",&w);
	scanf("%d",&n);
	for(i=0;i<w;i++){
		c[i]=i+1;
	}
	for(i=0;i<n;i++){
		scanf("%d,%d",&a,&b);
		swap(&c[a-1],&c[b-1]);
	}
	for(i=0;i<w;i++){
		printf("%d\n",c[i]);
	}
	return 0;
}

Measured memory_bytes: 596
#include <bits/stdc++.h>
  #define endl "\n"
  #define ll long long int
  #define vi vector<int>
  #define vll vector<ll>
  #define vvi vector < vi >
  #define pii pair<int,int>
  #define pll pair<long long, long long>
  #define mod 1000000007
  #define inf 1000000000000000001;
  #define all(c) c.begin(),c.end()
  #define mp(x,y) make_pair(x,y)
  #define mem(a,val) memset(a,val,sizeof(a))
  #define pb push_back
  #define f first
  #define se second
  
  using namespace std;
  ll k2;
  ll arr[1000000];
  ll arr2[1000000];

  ll func(ll n,ll cur){
    if(cur==n)
      return 0;
    if(cur>n)
      return inf;
    if(arr2[cur]!=-1)
      return arr2[cur];

    ll sum=inf;
    for (ll i = 1; i <=k2; ++i)
    {
      sum=min(sum,func(n,cur+i)+abs(arr[cur]-arr[cur+i]));
    }

    return arr2[cur]=sum;
  }


  int main()
  {
    std::ios::sync_with_stdio(false);
    ll n=0,m=0,k=0,x=0,t1=0,t2=0,t3=0,f=0,sum=0,count=0;

    cin>>n>>k2;

    for (ll i = 0; i < n; ++i){
      cin>>arr[i];
    }

    mem(arr2,-1);

    cout<<func(n-1,0);

    return 0;
  }

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

#define ll long long
#define sc(n) int n; scanf("%d",&n);
#define mod 10000007
#define N 100001
#define MX 1000000001
#define log(n) (int)log2(n)
#define logg(a)(b) log(b)/log(a)
#define pb(x) push_back(x);
#define T int t, q=0; scanf("%d", &t); while(q++<t)
#define pi acos(-1)
#define flag bool fl=true;
#define fast ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);

int main()
{
    int n;
    cin>>n;
    set<string>s;
    for(int i=0;i<n;i++){
        string str;
        cin>>str;
        s.insert(str);
    }
    cout<<s.size()<<endl;
}



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

using pi = pair<int,int>;

const int INF = 123456789;

struct edge{int to,cost;};
const int V = 100000;
vector<edge> G[V];

int main()
{
    int n,m,s,t;
    cin >>n >>m >>s >>t;
    --s;
    --t;

    vector<int> d(n);
    rep(i,n) cin >>d[i];

    rep(i,m)
    {
        int a,b;
        cin >>a >>b;
        --a;
        --b;
        G[a].push_back({b,d[b]});
    }
    // back edge
    rep(i,n-1) G[i+1].push_back({i,0});

    priority_queue<pi,vector<pi>,greater<pi>> pq;
    vector<int> dist(n,INF);
    pq.push({0,s});
    dist[s] = 0;
    while(!pq.empty())
    {
        pi p = pq.top();
        pq.pop();
        int v = p.second;
        if(p.first>dist[v]) continue;
        for(const auto &e:G[v])
        {
            if(dist[e.to]>dist[v]+e.cost)
            {
                dist[e.to] = dist[v]+e.cost;
                pq.push({dist[e.to],e.to});
            }
        }
    }

    assert(dist[t]<INF);
    cout << dist[t] << endl;
    return 0;
}

Measured memory_bytes: 11200
#include <iostream>
#include <vector>
#include <climits>
#include <stack>
#include <cstdio>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <regex>
#include <algorithm>
#include <math.h>
#include <string>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(obj) obj.begin(), obj.end()
#define UNIQUE(v) v.erase(unique(All(v)), v.end());
typedef long long ll;

typedef pair<int, int> P;
vector<P> vecp;
int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1};

#define N 1000
// input

signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n,m;
  cin >> n >> m;
  if (n <= m){
    cout << "Yes";
  }else{
    cout << "No";
  }
}

Measured memory_bytes: 256
// C - Sugar Water
#include <bits/stdc++.h>
using namespace std;
#define rp(i,s,e) for(int i=(s);i<(e);++i)

void solve(int w1,int w2, int s1, int s2, int solute, int volume){
	int WS = w1*100, S = 0;
	int wend = volume/100 + 1;
	double maxper = 0.0;
	rp(iw, 1, wend){
		if(w1*iw >= wend) break;
		rp(jw, 0, wend){
			int w = w1*iw + w2*jw;
			if(w >= wend) break;
			int send = volume - w*100 + 1;
			rp(is, 0, send){
				if(s1*is >= send) break;
				rp(js, 0, send){
					int s = s1*is + s2*js;
					if(s >= send) break;
					int ws = w * 100 + s;
					if(ws <= volume && s <= solute*w){
						double per = 100.0*s/ws;
						if(maxper < per){
							maxper = per;
							WS = ws;
							S = s;
						}
					}
				}
			}
		}
	}
	printf("%d %d\n",WS,S);
}

int main(){
	int a,b,c,d,e,f;
	cin>>a>>b>>c>>d>>e>>f;
	solve(a,b,c,d,e,f);
}

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

int main() {
  
  int n,s;
  cin >> n;
  
  for (int i = 1;i<=int(n/100);i++){
    s = n - (100*i);
    if(s<=5*i){
      cout << '1' << endl;
      break;
    }else if(i == int(n/100)){
      cout << '0' << endl;
    }
  }
  if (int(n/100) ==  0){
    cout << '0' << endl;
  }
}


Measured memory_bytes: 256
"use strict"
function main(arg) {
    let N = arg.trim().split("\n")[0]
    N = Number(N)
    let ans = (new Array(N + 1)).fill(0)
    const MAX = 10e4
    // for (let i = 1; i < MAX; i++) {
    //     let t = fx(i,i,i)
    //     if (t > N) {
    //         break
    //     }
    //     // ans[t] += 1
    //     ans[t] += ":" + [i,i,i].join(",")
    // }
    // for (let i = 1; i < MAX; i++) {
    //     let j = 1
    //     for (j = 1; j < MAX; j++) {
    //         if (i === j) continue
    //         let t = fx(i,j,j)
    //         if (t > N) {
    //             break
    //         }
    //         ans[t] += 3
    //         ans[t] += ":" + [i,j,j].join(",")
    //     }
    //     if (j===1) {
    //         break
    //     }
    // }
    for (let i = 1; i < MAX; i++) {
        let j = 1
        for (j = 1; j < MAX; j++) {
            // if (i === j) continue
            let k = 1;
            for (k = 1; k < MAX; k++) {
                // if (k === i || k === j) continue
                let t = fx(i,j,k)
                if (t > N) {
                    break
                }
                ans[t] += 1
                // ans[t] += ":" + [i,j,k].join(",")
            }
            if (k === 1) {
                break
            }
        }
        if (j === 1) {
            break
        }
    }
    ans.shift()
    console.log(ans.join("\n"))
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

function fx(x,y,z) {
    return x * x  + y * y + z * z + x * y + y * z + z * x 
}

Measured memory_bytes: 31584
#include <bits/stdc++.h>
using Int = long long int;
using namespace std;

int main(){
  Int n;
  cin >> n;
  Int h[100000];
  for(Int i=0; i<n; i++){
    cin >> h[i];
  }
  bool flg = true;
  for(Int i=0; i<n-1; i++){
    if(i == 0){
      Int r = h[i+1];
      if(r < h[i]-1){
        flg = false;
        break;
      }
      h[i]--;
      continue;
    }
    Int l = h[i-1];
    Int r = h[i+1];
    if(l < h[i]){
      h[i]--;
    }
    if(r < h[i]){
      flg = false;
      break;
    }
  }
  cout << (flg ? "Yes" : "No") << endl;
  return 0;
}

Measured memory_bytes: 1024
def main():
    from math import ceil
    from copy import deepcopy
    n, a, b, *k = map(int, open(0).read().split())
    k.sort()

    def check(x):
        q = deepcopy(k)
        cnt = 0
        while q:
            p = q.pop() - x * b
            if p <= 0:
                break
            cnt += ceil(p / (a - b))

        return cnt <= x

    l = 0
    r = k[-1]
    m = (l + r) // 2
    while l + 1 < r:
        if check(m):
            r = m
        else:
            l = m
        m = (l + r) // 2

    print(m+1)


if __name__ == '__main__':
    main()


Measured memory_bytes: 20156
package main

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

func main() {
	N := getStdinInt()
	S := getStdin()

	ans := 0
	prev := ""

	for i := 0; i < N; i++ {
		if prev != S[i:i+1] {
			prev = S[i : i+1]
			ans++
		}
	}

	fmt.Printf("%d\n", ans)
}

var sc = bufio.NewReaderSize(os.Stdin, 1024*1024*10)

func getStdin() string {
	return readLine()
}
func getStdinInt() int {
	str := getStdin()
	rtn, _ := strconv.Atoi(str)
	return rtn
}
func getStdinInt2() (int, int) {
	list := getStdinIntArr()
	return list[0], list[1]
}
func getStdinInt3() (int, int, int) {
	list := getStdinIntArr()
	return list[0], list[1], list[2]
}
func getStdinInt64() int64 {
	str := getStdin()
	rtn, _ := strconv.ParseInt(str, 10, 64)
	return rtn
}
func getStdinIntU64() uint64 {
	str := getStdin()
	rtn, _ := strconv.ParseInt(str, 10, 64)
	return uint64(rtn)
}
func getStdinInt64_2() (int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1]
}
func getStdinInt64_3() (int64, int64, int64) {
	list := getStdinIntArr64()
	return list[0], list[1], list[2]
}
func getStdinIntArr() []int {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.Atoi(val)
	}
	return rtn
}
func getStdinIntArr64() []int64 {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]int64, len(list))
	for idx, val := range list {
		rtn[idx], _ = strconv.ParseInt(val, 10, 64)
	}
	return rtn
}
func getStdinIntArrU64() []uint64 {
	str := getStdin()
	list := strings.Split(str, " ")
	rtn := make([]uint64, len(list))
	for idx, val := range list {
		r, _ := strconv.ParseInt(val, 10, 64)
		rtn[idx] = uint64(r)
	}
	return rtn
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func abs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}
func abs64(n int64) int64 {
	if n < 0 {
		return -n
	}
	return n
}
func readLine() string {
	buf := make([]byte, 0, 0)
	for {
		l, p, _ := sc.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}


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

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

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

const ll mod=1000000007;
//head

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

    int n, m, x;
    cin >> n >> m >> x;
    vector<pair<int,vector<int>>> g;
    for(int i = 0; i < n; i++) {
        int c;
        cin >> c;
        vector<int> inc(m);
        for(int j = 0; j < m; j++) {
            cin >> inc[j];
        }
        g.pb(mp(c,inc));
    }

    ll minCost = 1LL << 60;
    for(int i = 1; i < (1 << n); i++) {
        vi goal(m,0);
        ll curr = 0;
        for(int j = 0; j < n; j++) {
            if((i & (1 << j)) > 0) {
                curr += g[j].first;
                for(int k = 0; k < m; k++) {
                    goal[k] += g[j].second.at(k);
                }
            }
        }
        bool ok = true;
        for(int k = 0; k < m; k++) if(goal[k] < x) ok = false;
        if(ok) minCost = min(minCost, curr);
    }
    if(minCost == (1LL << 60)) {
        cout << -1 << endl;
    } else {
        cout << minCost << endl;
    }
    return 0;
}


Measured memory_bytes: 3620
#####################################################################################################
##### ダブリング
#####################################################################################################

"""

全体の要素数がN個あって1回移動した時にどの要素に到達するのか定まっているとき、
「K個先の要素を求めるのに O(K) かかる」ような状況において

前処理：O(N logK) 時間, O(N logK) 空間
クエリ：O(logK)

で行うことができるようにするアルゴリズム

参考
https://algo-logic.info/doubling/

ベンチマーク
https://atcoder.jp/contests/abc167/submissions/15103640


"""

import sys
input = sys.stdin.readline

class Doubling:
    def __init__(self, A, K_max, decrement=True):
        """
        :param A: リスト。i から A[i] へと移動する
        :param K_max: K_max = 2**(k_max) まで参照する可能性がある
        :param decrement: True = A の要素の decrement が必要
        """
        N = len(A)
        k_max = K_max.bit_length()
        self.N = N
        self.doubling = [[-1] * N for _ in range(k_max + 1)]
        self.decrement = decrement
        for i, a in enumerate(A):
            self.doubling[0][i] = a - self.decrement

        for i in range(1, k_max):
            for j in range(N):
                if self.doubling[i - 1][j] == -1:
                    self.doubling[i][j] = -1
                else:
                    self.doubling[i][j] = self.doubling[i - 1][self.doubling[i - 1][j]]

    def apply(self, start, K):
        """
        :param start: スタート地点
        :param K: K回進む
        :return:
        """
        i = start - self.decrement
        for k in range(K.bit_length()):
            m = 1 << k
            if m & K:
                i = self.doubling[k][i]
            if i is None:
                break
        return i + self.decrement


######################################################################################


N, K = map(int, input().split())
A = list(map(int, input().split()))
doubling = Doubling(A, 10**18 + 1, decrement=True)
print(doubling.apply(1, K))

Measured memory_bytes: 184604
#include<iostream>
#include<algorithm>
#include<unordered_map>

using namespace std;

const int MAX_N = 100001;

int N;
string s[MAX_N];
unordered_map<string, int> dic;

void solve() {
    long ans = 0;
    for (int i = 0; i < N; i++) {
        sort(s[i].begin(), s[i].end());
        dic[s[i]] += 1;
        if (dic[s[i]] >= 2) {
            ans += dic[s[i]] - 1;
        }
    }
    cout << ans << endl;
}

int main() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> s[i];
    }
    solve();
}

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

int main(){
  int64_t L, R;
  cin >> L >> R;

  int64_t ans = 2018;  
  if(R - L + 1 >= 2019){
    ans = 0;
  } 

  else{
    for(int64_t i=L; i<=R-1; i++){
      for(int64_t j=i+1; j<=R; j++){
        int64_t a = (i * j) % 2019;
        ans = min(ans, a);
      }
    } 
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<bits/stdc++.h>
#define ll long long int
#define hell 1000000007LL
using namespace std;
ll power(ll x, ll y){ 
    if(y==0)
     return 1;
    else
    {
        ll p=power(x,y/2);
        if(y%2==0)
         return (p*p)%hell;
        else
        return (x*((p*p)%hell))%hell;
    }}
ll gcd(ll x,ll y)
{
    if(y==0)
     return x;
    else
    return gcd(y,x%y);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20);
   ll a,b;
   cin>>a>>b;
   if(a<=9&&a>=1&&b>=1&&b<=9)
   cout<<a*b;
   else
   cout<<-1;}

Measured memory_bytes: 3616
import sys, collections, heapq
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def dijkstra(G, s):
    que = []
    d = [INF]*len(G)
    d[s] = 0
    heapq.heappush(que, (d[s], s))

    while que:
        p = heapq.heappop(que)
        v = p[1]
        if d[v] < p[0]: continue
        for e in G[v]:
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heapq.heappush(que, (d[e[0]], e[0]))
    return d

def resolve():
    n = I()
    G = [[] for _ in range(n)]
    for i in range(n):
        ukvc = LI()
        u = ukvc[0]
        k = ukvc[1]
        for j in range(k):
            v = ukvc[2*j+2]
            c = ukvc[2*j+3]
            G[u].append((v, c))

    ans = dijkstra(G, 0)
    for i, e in enumerate(ans):
        print(i, e)

if __name__ == '__main__':
    resolve()



Measured memory_bytes: 69308
list_H=[]
list_W=[]

while True:
    H,W=map(int,input().split())
    if H==0 and W==0:
        break
    else:
        list_H.append(H)
        list_W.append(W)

for i in range(len(list_H)):
    h=list_H[i]
    w=list_W[i]
    x=max(0,h-2)
    y=max(0,w-2)
    if h==0 or w==0:
        pass
    elif h==1:
        print("#"*w)
        print()
    elif w==1:
        for _ in range(h):
            print("#")
        print()
    else:
        print("#"*w)
        for _ in range(x):
            print("#"+"."*y+"#")
        print("#"*w)
        print()
    



Measured memory_bytes: 5616
open Printf
open Scanf

let id x = x

let n = scanf "%d" id
let vs = Array.init n (fun i -> scanf " %d" id)

let calc first =
  let flg = ref first in
  let sum = ref 0 in
  let count = ref 0 in
  for i = 0 to Array.length vs - 1 do
    sum := !sum + vs.(i);
    if !flg then begin
      if !sum <= 0 then begin
        let diff = abs (1 - !sum) in
        count := !count + diff;
        sum := 1
      end else
        ()
    end else begin
      if !sum >= 0 then begin
        let diff = abs (~-1 - !sum) in
        count := !count + diff;
        sum := ~-1
      end else
        ()
    end;
    flg := not !flg
  done;
  !count

let () =
  let start_pos = calc true in
  let start_neg = calc false in
  printf "%d\n" (min start_pos start_neg)


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

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define MOD 1000000007
typedef long long ll;

int main() {
    int n, m;
    cin >> n >> m;
    vector<ll> a(n);
    struct s_bc{
        ll b;
        ll c;
    };
    vector<s_bc> bc(m);
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> bc[i].b >> bc[i].c;
    sort(a.begin(), a.end());
    //cout << a[0] << endl;
    sort(bc.begin(), bc.end(), [](const s_bc& x, const s_bc& y){ return x.c > y.c;});
    int idx = 0;
    rep(i, m){
        rep(j, bc[i].b){
            if(a[idx] < bc[i].c){
                a[idx] = bc[i].c;
                idx++;
            }else{
                ll ans = 0;
                rep(k, n) ans += a[k];
                cout << ans << endl;
                return 0;
            }
        }
    }
    ll ans = 0;
    rep(k, n) ans += a[k];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 2560
/*
 * b.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int N = 3;
const int NN = N * N;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

typedef unsigned long long ull;
typedef pair<int,ull> piul;
typedef map<ull,int> muli;

/* global variables */

muli dists;

/* subroutines */

inline int zeropos(ull bits) {
  for (int i = 0; i < NN; i++)
    if (((bits >> (i * 4)) & 0xf) == 0) return i;
  return -1;
}

inline ull move(ull u, int p0, int p1) {
  ull x = (u >> (4 * p1)) & 0xfULL;
  return ((u | (x << (4 * p0))) & ~(0xfULL << (4 * p1)));
}

/* main */

int main() {
  ull st = 0, gl = 0;

  for (int i = 0; i < NN; i++) {
    ull ai;
    cin >> ai;
    st |= (ai << (i * 4));
  }

  for (int i = 0; i < NN - 1; i++)
    gl |= ((ull)(i + 1) << (i * 4));
  //printf("st=%09llx, gl=%09llx\n", st, gl);

  if (st == gl) {
    puts("0");
    return 0;
  }
  
  dists[st] = 1;
  dists[gl] = -1;

  queue<piul> q;
  q.push(piul(1, st));
  q.push(piul(-1, gl));

  int mind = -1;

  while (! q.empty()) {
    piul u = q.front(); q.pop();
    int &ud = u.first;
    ull &ui = u.second;

    int upos = zeropos(ui);
    int ux = upos % N, uy = upos / N;

    for (int di = 0; di < 4; di++) {
      int vx = ux + dxs[di], vy = uy + dys[di];
      if (vx >= 0 && vx < N && vy >= 0 && vy < N) {
	int vpos = vy * N + vx;
	ull vi = move(ui, upos, vpos);

	int vd = ud + ((ud >= 0) ? 1 : -1);
	muli::iterator mit = dists.find(vi);
	if (mit == dists.end()) {
	  dists[vi] = vd;
	  q.push(piul(vd, vi));
	}
	else {
	  if (ud * mit->second < 0) {
	    int d = abs(mit->second - ud) - 1;
	    printf("%d\n", d);
	    exit(0);
	  }

	  if ((ud > 0 && mit->second > vd) || (ud < 0 && mit->second < vd)) {
	    mit->second = vd;
	    q.push(piul(vd, vi));
	  }
	}
      }
    }
  }
  return 0;
}

Measured memory_bytes: 2320
#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;

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

const int MAXN = 4005;
const int mod = 1e9 + 7;

int n, m, k, ans, f[MAXN][MAXN];

inline void inc(int &x, int y) { x += y; if( x >= mod ) x -= mod; }
inline void dec(int &x, int y) { x -= y; if( x < 0 ) x += mod; }

int main()
{
#ifdef wxh010910
	freopen( "data.in", "r", stdin );
#endif
	scanf( "%d%d%d", &n, &m, &k );
	f[ 0 ][ 0 ] = 1;
	for( int i = 1 ; i <= n + m ; i++ )
	{
		for( int j = 0 ; j <= n + m ; j++ )
			inc( f[ i ][ j ], f[ i - 1 ][ j ] ), dec( f[ i ][ j + k ], f[ i - 1 ][ j ] );
		for( int j = 1 ; j <= n + m ; j++ ) inc( f[ i ][ j ], f[ i ][ j - 1 ] );
	}
	for( int i = 1 ; i <= n + m ; i++ )
		for( int j = 0 ; j <= n + m ; j++ ) if( f[ i ][ j ] )
		{
			int x = j, y = i * ( k - 1 ) - j;
			if( y < 0 ) break;
			if( x <= n && y <= m - 1 && ( x % ( k - 1 ) == n % ( k - 1 ) ) ) inc( ans, f[ i ][ j ] ), dec( ans, f[ i - 1 ][ j ] );
		}
	cout << ans << endl;
}


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


int main(){
    int a, b, c;
    cin >> a >> b >> c;
    vector<int> poor(3);
    poor.at(0) = a;
    poor.at(1) = b;
    poor.at(2) = c;
    sort(poor.begin(), poor.end());
    int check = 0;
    for (int i = 0; i < 3; i++){
        if (i != 0){
            if (poor.at(i - 1) == poor.at(i)){
                check++;
            }
        }
    }
    
    if (check == 1) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256
n = int(input())
ans = [0] * n

for x in range(1,100):
    for y in range(1,100):
        for z in range(1,100):
            tmp = x**2 + y**2 + z**2 +  x*y + y*z + z*x
            if tmp <= n:
                ans[tmp-1] += 1

for i in range(n):
    print(ans[i]) 

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

#define fs first
#define sc second
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

const int32_t inf = 1001001001;
const int64_t infll = 1001001001001001001ll;
const int dx[] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

template <typename T> using vector2d = vector<vector<T>>;

// clang-format off
template <typename T> void sort(vector<T> &v) { sort(all(v)); }
ostream &operator<<(ostream &os, __int128_t value) { if (ostream::sentry(os)) { __uint128_t tmp = value < 0 ? -value : value; char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }
istream &operator>>(istream &is, __int128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } if (in[0] == '-') value *= -1; return is; }
ostream &operator<<(ostream &os, __uint128_t value) { if (ostream::sentry(os)) { char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[value % 10]; value /= 10; } while (value != 0); int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }
istream &operator>>(istream &is, __uint128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } return is; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << v[0]; for (int i = 1; i < v.size(); ++i) os << " " << v[i]; return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.fs << " " << p.sc; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.fs >> p.sc; return is; }

struct IoSetup { IoSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup;

inline int64_t in() { int64_t x = 0; cin >> x; return x; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// clang-format on

signed main(int argc, char *argv[]) {
  string a, b;
  cin >> a >> b;
  reverse(all(a));
  cout << (a == b ? "YES" : "NO") << endl;
}


Measured memory_bytes: 256
//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n; cin>>n;
    int arr[n+2]={0};
    for(int i=1;i<=n;i++){
        int a,b; cin>>a>>b;
        if(a==b) arr[i]=1;
    }
    int cnt=0;
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        if(arr[i]==0) cnt=0;
        else cnt++;
        if(cnt>=3) flag=1;
    }
    if(cnt>=3) flag=1;
    if(flag) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    
}

Measured memory_bytes: 3664
/*
Created at: 2019 January 12 17:21:40
Created by: MpleDrop
*/
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e5;

int readInt ()
{
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true)
    {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true;
    else result = ch-'0';
    while (true)
    {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}
main() {
    int n = readInt(), h = readInt(), w = readInt();
    cout << (n - h + 1) * (n - w + 1);
}



Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
	
int main(){
	
	int arr;
	
	scanf("%d", &arr);
	
	int a = arr / 100;
	int b = arr % 100;
	
	if(a <= 12 && a > 0){
		if(b <= 12 && b > 0){
			printf("AMBIGUOUS\n");
		}
		else{
			printf("MMYY\n");
		}
	}
	else{
		if(b <= 12 && b > 0){
			printf("YYMM\n");
		}
		else{
			printf("NA\n");
		}
	}
	
	
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ALL(obj)   (obj).begin(),(obj).end()

#define debug(var)  do{std::cout << __LINE__ << "> " << #var << ": ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
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 namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

void Main() {
	ll A, B, V, W, T;
	cin >> A >> V >> B >> W >> T;
	if (V <= W) {
		cout << "NO" << endl;
		return;
	}
	if ((V - W) * T >= abs(A - B)) {
		cout << "YES" << endl;
		return;
	}
	cout << "NO" << endl;
}

int main() {
    cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
}


Measured memory_bytes: 3636
s = input()
n = len(s)
go = 0
pa = 0
score = 0
for i in range(n):
    if s[i] == "g":
        go += 1
    else:
        if go > pa:
            pa +=1
        else:
            go += 1
            score -=1
print(score+(go-pa)//2)

Measured memory_bytes: 39408
import java.util.Scanner;
import static java.lang.System.out;
class Main{
  public static void main(String args[]){
    Scanner scan = new Scanner(System.in);
    String name[] = {"black","blue","lime","aqua","red","fuchsia","yellow","white"};
    int color[][] = {{0,0,0},{0,0,255},{0,255,0},{0,255,255},{255,0,0},{255,0,255},{255,255,0},{255,255,255}};
    while(true){
      String str = scan.next();
      if(str.equals("0")) break;

      int R = Integer.parseInt(str.substring(1,3),16);
      int G = Integer.parseInt(str.substring(3,5),16);
      int B = Integer.parseInt(str.substring(5,7),16);
      double min = Double.MAX_VALUE;
      int Index = 0;
      for(int i = 0 ; i < 8 ; i++){
        double near = Math.sqrt(Math.pow(R - color[i][0],2) + Math.pow(G - color[i][1],2) + Math.pow(B - color[i][2],2));
        if( near < min ){
          min = near;
          Index = i;
        }
      }
      out.println(name[Index]);
    }
  }
}

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

#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))

#define INF             2e9
#define INF_LL          1LL<<60
#define ll              long long
#define Ceil(x, n)      (((((x))+((n)-1))/n))
#define CeilN(x, n)     (((((x))+((n)-1))/n)*n)
#define FloorN(x, n)    ((x)-(x)%(n))
#define ALL(x)          (x).begin(),(x).end()
typedef pair<ll, ll>    P;

int main() {
    int n;
    cin >> n;
    ll ans = 0;
    vector<ll> a(n), b(n);
    REP(i, n) cin >> a[i] >> b[i];
    REP_REV(i, n) {
        a[i] += ans;
        if(a[i] % b[i] == 0) continue;
        else ans += b[i] - a[i] % b[i];
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1920
def main():
    n, k = map(lambda n: int(n), input().split(' '))
    prices = []
    for _ in range(n):
        p = int(input())
        prices.append(p)
    prices.sort()

    min_diff = prices[-1] - prices[0]
    for i in range(n-k+1):
        diff = prices[i+k-1] - prices[i]
        if diff < min_diff: 
            min_diff = diff
    print(min_diff)

if __name__ == "__main__":
    main()

Measured memory_bytes: 7384
#include <stdio.h>


int main()
{
      int n;
      scanf("%d",&n);
        n=n%10;
         if(n==2||n==4||n==5||n==7||n==9)
               printf("hon");
       if(n==0||n==1||n==6||n==8)
                printf("pon");
                     if(n==3)
                printf("bon");
    
    return 0;
}

Measured memory_bytes: 1728
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define INF 1e9 + 1
using namespace std;

int n, A[501000];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
    int n1 = mid - left;
    int n2 = right - mid;
    int L[n1 + 1], R[n2 + 1];
    REP(i, n1) L[i] = A[left + i];
    REP(i, n2) R[i] = A[mid + i];
    L[n1] = INF;
    R[n2] = INF;
    int i = 0, j = 0;
    for (int k = left; k < right; k++) {
        if (L[i] <= R[j]) {
            A[k] = L[i++];
        } else {
            A[k] = R[j++];
        }
        cnt++;
    }
}

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

int main() {
    cin >> n;
    REP(i, n) cin >> A[i];
    mergeSort(A, n, 0, n);
    REP(i, n) {
        if (i) cout << " ";
        cout << A[i];
    }
    cout << endl;
    cout << cnt << endl;
}


Measured memory_bytes: 7004
#include<iostream>
#include<iomanip>
#include<functional>
#include<algorithm>
#include<string>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<set>
#include<queue>
#include<cmath>

using namespace std;
#define LL long long

int n,v[111111],ans;
map<int,int>odd,even;
vector<pair<int,int>>o,e;

int main() {
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>v[i];
		if(i&1)even[v[i]]++;
		else odd[v[i]]++;
	}
	for(auto i:odd)o.push_back({i.second,i.first});
	for(auto i:even)e.push_back({i.second,i.first});
	sort(o.rbegin(),o.rend());
	sort(e.rbegin(),e.rend());
	o.push_back({0,0});
	e.push_back({0,0});
	if(o[0].second!=e[0].second)ans=n-o[0].first-e[0].first;
	else ans=n-max(e[0].first+o[1].first,e[1].first+o[0].first);
	cout<<ans<<endl;
	return 0;
}



Measured memory_bytes: 6520
#include <bits/stdc++.h>
#define IO                       \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define mem(a, x) memset(a, x, sizeof(a))
#define per(x, a, b) for (int x = a; x <= b; x++)
#define rep(x, a, b) for (int x = a; x >= b; x--)

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-8;

int main() {
#ifdef LOCAL_IM0QIANQIAN
    freopen("test.in", "r", stdin);
//    freopen("test.out", "w", stdout);
#else
    IO;
#endif // LOCAL_IM0QIANQIAN

    int a, b, c;
    while (cin >> a >> b >> c) {
        int now = 0;
        for (int i = min(a, b); i >= 1; i--) {
            if (a % i == 0 && b % i == 0) {
                ++now;
                if (now == c) {
                    cout << i << endl;
                    break;
                }
            }
        }
    }
    return 0;
}

Measured memory_bytes: 256
//Dragon_warrior7(IITK)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pb push_back
#define nl cout << '\n'
#define sor(x) sort(x.begin(), x.end())
#define rev(v) reverse(v.begin(), v.end())
#define lb(v, temp) lower_bound(v.begin(), v.end(), temp)
#define ub(v, temp) upper_bound(v.begin(), v.end(), temp)
#define fi first
#define se second
#define llmax 1000000000000000000
#define deb(x) cout << #x << " " << x << "\n"
#define pll pair<ll, ll> 
#define vll vector<ll> 
#define rub cout << "\n------------------------------------\n"
typedef  tree < int ,  null_type ,  greater<int> ,  rb_tree_tag ,  tree_order_statistics_node_update > oset;
void read(vector<ll> &v)
{
	for (int i = 0; i < v.size(); i++)
		cin >> v[i];
}

void print(vector<ll> v)
{
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
}
ll n,m,a,b,c,t,temp,x,y;
string str;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	vll v(n);
	read(v);
	sor(v);
	ll sum=0;
	for(int i=0;i<n-1;i++)
	{
	    sum+=v[i];
	}
	if(sum>v[n-1])
	cout<<"Yes";
	else
	cout<<"No";
	
	
	
	
	
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int a, b;
  cin >> a >> b;
  
  if ((a + b) % 2 == 0) {
    cout << (a + b) / 2 << endl;
  }
  
  else {
    cout << ((a + b) / 2) + 1 << endl;
  }
}

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

typedef long long LL;

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int main()
{
    //ios::sync_with_stdio(0); cin.tie(0);
    int x, a, b;
    cin>>x>>a>>b;
    if(abs(x-a) < abs(x-b))
        cout<<"A\n";
    else
        cout<<"B\n";
    return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 500000

int N;
int bean[100005];
vector<int> V[NUM];


void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(int loc,int value){
	loc += N-1;

	V[loc].push_back(value);

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		V[parent].push_back(value);

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


int query(int search_left,int search_right,int node_id,int node_left,int node_right,int D){

	if(search_right < node_left || search_left > node_right)return BIG_NUM;

	if(search_left <= node_left && search_right >= node_right){

		if(V[node_id][0] >= D){ //Dが最小値より小さい

			return V[node_id][0]-D;

		}else if(V[node_id][V[node_id].size()-1] < D){ //Dが最大値より大きい

			return D-V[node_id][V[node_id].size()-1];
		}

		int small,big,L,R,mid;

		//D以下の最大の数字を二分探索で検索
		L = 0,R = V[node_id].size()-1,mid = (L+R)/2;

		while(L <= R){

			if(V[node_id][mid] <= D){

				small = V[node_id][mid];
				L = mid+1;
			}else{

				R = mid-1;
			}
			mid = (L+R)/2;
		}

		//Dより大きい最小の数を、二分探索で検索
		L = 0,R = V[node_id].size()-1,mid = (L+R)/2;

		while(L <= R){

			if(V[node_id][mid] >= D){

				big = V[node_id][mid];
				R = mid-1;
			}else{

				L = mid+1;
			}
			mid = (L+R)/2;
		}

		return min(big-D,D-small);
	}

	//今回のノードの区間に、一部検索区間と重なっている区間がある場合→再帰的に子どもに尋ねる
	int left_min = query(search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2,D);
	int right_min = query(search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right,D);

	return min(left_min,right_min);
}


int main(){

	int first_N;
	scanf("%d",&first_N);

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

		scanf("%d",&bean[i]);
	}

	N = 1;
	init(first_N);

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

		update(i,bean[i]);
	}

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

		sort(V[i].begin(),V[i].end());
	}

	int num_query,left,right,D;

	scanf("%d",&num_query);

	for(int loop = 0; loop < num_query; loop++){

		scanf("%d %d %d",&left,&right,&D);

		printf("%d\n",query(left,right,0,0,N-1,D));
	}

	return 0;
}



Measured memory_bytes: 26848
#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

signed main(){
    int N;
    cin >> N;
    ll ans = 0;
    for(int A=1; A<=N-1; A++){
        int B_max = (N-1) / A;
        ans += B_max;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3600
n, l = map(int,input().split())
if l>=1:
    print(int(n * (2 * l - 1 + n) / 2 - l))
elif -l+1>n:
    print(int(n * (2 * l - 1 + n) / 2 - (l+n-1)))
else:
    print(int(n * (2 * l - 1 + n) / 2))

Measured memory_bytes: 3060
#include <bits/stdc++.h> 
#include <chrono>
using namespace std;
 
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
 
//typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;
 
//container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair 
#define SZ(a) int((a).size())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define ALL(a)  (a).begin(),(a).end()
//constant
//--------------------------------------------
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
int main(){
    LL N,X,M;
    cin>>N>>X>>M;
    LL A=X;
    vector<LL>prel,posl;
    prel.PB(A);
    vector<LL>loop;
    set<LL>chk;
    LL tmpA;
    REP(i,N){
        tmpA=A*A%M;
        if(tmpA==0){
            LL res=prel[0];
            REP(ii,SZ(loop))res+=loop[ii];
            cout<<res<<endl;
            return 0;
        }
        if(chk.find(tmpA)!=chk.end())break;
        loop.PB(tmpA);
        chk.insert(tmpA);
        A=tmpA;
    }
    bool flag=false;
    REP(i,SZ(loop)){
        if(loop[i]==tmpA)flag=true;
        if(!flag)prel.PB(loop[i]);
        else posl.PB(loop[i]);
    }
    LL poslsum=0;
    REP(i,SZ(posl))poslsum+=posl[i];
    LL res=0;
    if(N<=SZ(prel)){
        REP(i,N)res+=prel[i];
        cout<<res<<endl;
        return 0;
    }else{
        REP(i,SZ(prel))res+=prel[i];
    }
    res+=poslsum*((N-SZ(prel))/SZ(posl));
    REP(i,((N-SZ(prel))%SZ(posl)))res+=posl[i];
    cout<<res<<endl;
    return 0;
}

Measured memory_bytes: 6724
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
#include <ccomplex>
#include <unordered_map>
#include <cstring>
#include <iostream>
#include <numeric>
#include <sstream>
#include <set>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <map>

using namespace std;
#define MAX 1LL << 60

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, s, e) for(ll i = s; i < e; i++)
#define repR(i, s, e) for(ll i = s; i > e; i--)
#define reft0(i, j) setfill('0') << setw(i) << j

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;}

int main(){
	int A, B;
	cin >> A >> B;

	vector<int> sum;
	sum.push_back(A + A - 1);
	sum.push_back(A + B);
	sum.push_back(B + B - 1);
	cout << *max_element(begin(sum), end(sum)) << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<set>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

int tins(string s, string t, int k){
  int n=t.size(); if(k+n>s.size())return 0;
  if(df)print(k+n);
  rep(i,n){
    if(df)print(s[i+k],t[i]);
    if(s[i+k]=='?')continue;
    if(s[i+k]!=t[i])return 0;
  }
  return 1;
}

string makeans(string s,string t,int k){
  int n=t.size();
  rep(i,n){
    s[i+k]=t[i];
  }
  n=s.size();
  rep(i,n){
    if(s[i]=='?') s[i]='a';
  }
  if(df)print(s);
  return s;
}

int main(){
  string s,t; cin >>s >>t;
  set<string> st;
  int n=s.size(); 
  rep(i,n){
    if(tins(s,t,i)) st.insert(makeans(s,t,i));
    if(df) print("next");
  }
  if(st.size())print(*st.begin());
  else(print("UNRESTORABLE"));
}


Measured memory_bytes: 3608
import math
while True:
    n = int(input())
    if n == 0:
        break
    
    s = list(map(int, input().split()))
    m = sum(s)/n
    v = 0
    for i in s:
        v += (i - m) ** 2
    ans = math.sqrt(v/n)
    print("{:.5f}".format(ans))
        



Measured memory_bytes: 5696
#include <bits/stdc++.h>
using namespace std;
int main(void){
    std::vector<int> x(5);
    for(int i=0; i<5; i++){
        std::cin >> x.at(i);
    }
   for(int i=0; i<5; i++){
       if(0==x.at(i)){
       std::cout << i+1 << std::endl;
       break;}
   }
}


Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define digit(N) cout << fixed << setprecision((N))

int N;
string s, t, ans;

int main()
{
    cin >> N >> s >> t;
    for (int i = 0; i < N; i++)
    {
        ans.push_back(s[i]);
        ans.push_back(t[i]);
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	string s;
	int n;
	cin>>n;
	cin.ignore();
	for(int i=0;i<n;i++){
		getline(cin,s);
		while(1){
			if(s.find("Hoshino")==string::npos) break;
			s.replace(s.find("Hoshino"),7,"Hoshina");
			}
		cout<<s<<endl;
		}
}

Measured memory_bytes: 0
#include<bits/stdc++.h>
using namespace std;
#define forn(i, n)             for(int i = 0; i < n; i ++)
#define forn1(i, n)            for(int i = 1; i <= n; i ++)
#define pb push_back
#define ll long long int
#define fi first
#define se second
#define ii  pair<long long int, long long int>
#define vi  vector <long long int>
#define vii vector <pair <long long int, long long int > >
#define all(cont)  cont.begin(), cont.end()
#define EPS 1e-9
#define MOD 1000000007
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define range 200010
#pragma GCC optimize ("Ofast")
#define endl '\n'
signed main(){

 //   freopen("test.txt","r",stdin);
    ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n,k;
    cin>>n>>k;
    int ans=0;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(x>=k)ans++;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    a.sort(reverse=True)
    return sum(a[::2]) - sum(a[1::2])

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(N, a))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()


Measured memory_bytes: 9168