akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <numeric>
#include <math.h>
#include <algorithm>
#include <float.h>
#include <limits>
#include <vector> 
#include <string.h>
#include <iomanip>

#define rep(i,a,n) for(ll int (i) = (a);(i) < (n);(i)++)
#define urep(i,a,n) for(ll int (i) = (a);(i) > (n);(i)--)
#define MOD 1000000007
#define ll long long
#define asort(a) sort(a.begin(),a.end());

using namespace::std;

int jo(ll int jxo, ll int jyo){//累乗
	ll int jhogeo = 1;
	rep(jinto,0,jyo) jhogeo=jhogeo*jxo;
	return jhogeo;
}
char tobig(char toxbig){//char大文字化
  return (toxbig-0x20);
}
char tolow(char toxlow){//char小文字化
  return (toxlow+0x20);
}
int ctoi(char cctoi){//char->int
  if('0' <= cctoi && cctoi <= '9') return (cctoi-'0');
  return -1;
}
/*
int stoi(string sstoi){//string->int
  ll int stois=0;
  rep(stoii,0,sstoi.length()){
    stois+=ctoi(sstoi[stoii])*jo(10,sstoi.length()-stoii-1);
  }
  return stois;
}*/
  
int gcd(int gcad, int gcbd) {//最大公約数
  if(gcad < gcbd) return gcd(gcbd, gcad);
  int gcrd;
  while ((gcrd=gcad%gcbd)) {
    gcad = gcbd;
    gcbd = gcrd;
  }
  return gcbd;
}
int lcm(int lcam, int lcbm){//最小公倍数
 return (lcam*lcbm)/gcd(lcam,lcbm);
}


int main(){
  string n;
  cin>>n;
  int s=0;
  int a=0;
  rep(i,0,n.length()){
    s+=ctoi(n[i]);
    a+=ctoi(n[i])*jo(10,n.length()-i-1);
  }
  cout<<(a%s?"No":"Yes");
}
// int *p = new int[N];

//  vector<int> 変数名(要素数);
//  sort(変数名.begin(),変数名.end());//昇順ソート

// vector<vector<int>> 変数名(左の個数, vector<int>(右の個数));

//cout<<fixed<<setprecision(n);  //小数点n桁まで表示


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

using namespace std;
#define X first
#define Y second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define debug(x) cerr << #x << " = " << (x) << endl;
template<typename T>
ostream& operator<<(ostream& o, vector<T>& v) {
    for (auto& x : v) o << x << ' ';
    return o;
}

int main(){
    std::ios_base::sync_with_stdio(false); cin.tie(0);

    int x; cin >> x;
    int n = 1;
    while (n * (n+1) / 2 < x) n++;
    cout << n << endl;
}


Measured memory_bytes: 256
#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;

int a[200'000];

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

    auto s = unordered_map<int, int>();
    auto loop_begin = 0;
    auto loop_size = 0;
    a[0] = x;
    s.emplace(a[0], 0);
    for (int i = 1;; ++i) {
        a[i] = large(a[i - 1]) * a[i - 1] % m;
        auto [itr, inserted] = s.emplace(a[i], i);
        if (!inserted) {
            loop_begin = itr->second;
            loop_size = i - itr->second;
            cerr << "found " << a[i] << "\n";
            break;
        }
    }

    auto cnt = n;
    auto sum = 0LL;
    for (int i = 0; i < min(large(loop_begin), n); ++i) {
        sum += a[i];
    }
    cnt -= min(large(loop_begin), n);

    auto loop_sum = 0LL;
    for (int i = loop_begin; i < loop_begin + loop_size; ++i) {
        loop_sum += a[i];
    }
    auto loop_count = cnt / loop_size;
    sum += loop_count * loop_sum;
    cnt -= loop_count * loop_size;
    for (int i = loop_begin; i < loop_begin + cnt; ++i) {
        sum += a[i];
    }

    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 5444
#N = int(input())
N, M =  map(int,input().split())
#W =list(map(int,input().split()))
a = [(int(input())) for i in range(M)]

import sys

sys.setrecursionlimit(100000)
memo  =[-1]*(N+2)
def stair_count(n):
    if memo[n]!= -1:
        return memo[n]
    if n==0 or n==1:
        memo[n] =1
        return memo[n]
    memo[n] = stair_count(n-1) + stair_count(n-2)
    return memo[n]

start = 0
answer = 1
for i in a:
        if i == start:
            print(0)
            exit()
        else:
            n = i-1-start
            answer = (answer * stair_count(n))
            start = i+1
n = N -start
answer = (answer * stair_count(n))
print(answer%1000000007)


Measured memory_bytes: 536716
#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[5]={0},k;
	cin>>a[0]>>a[1]>>a[2]>>k;
	sort(a,a+3);
	while(k!=0){
		a[2]=a[2]*2;
		k--;
	}
	a[5]=a[0]+a[1]+a[2];
	cout<<a[5];
	return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

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

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
	return(x + y - 1) / y;

}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
	long long int m = number1;
	long long int n = number2;

	if (number2 > number1) {
		m = number2;
		n = number1;
	}
	long long int s = -1;
	while (s != 0) {
		s = m % n;
		m = n;
		n = s;
	}
	return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
	long long int m = number1;
	long long int n = number2;
	return m / lcm(m, n) * n;
}
vector<vector<pair<int, int>>> s(100030);
long long int seen[100020] = {};
long long int value[100020] = {};
int main() {
	int h, w;
	cin >> h >> w;
	int n;
	cin >> n;
	vector<int>s;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		s.push_back(a);
	}
	int number = 0;
	int od = 0;
	int ans[120][120] = {};
	for (int i = 0; i < h; i++) {
		if (od == 0) {
			for (int j = 0; j < w; j++) {
				if (s[number] == 0) {
					number = number + 1;
				}
				s[number] = s[number] - 1;
				ans[i][j] = number + 1;
			}
			od = 1;
		}
		else {
			for (int j = w-1; j >= 0; j--) {
				if (s[number] == 0) {
					number = number + 1;
				}
				s[number] = s[number] - 1;
				ans[i][j] = number + 1;
			}
			od = 0;
		}
	}
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
}

Measured memory_bytes: 2816
import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array                    as A
import qualified Data.Array.Unboxed            as AU
import qualified Data.Array.ST                 as ST
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Int
import qualified Data.IntMap                   as IM
import           Data.List
import qualified Data.Map                      as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import           Data.Time
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM

readInt = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
readInts n = replicateM n readInt
readString = map BS.unpack . BS.words <$> BS.getLine
readStrings n = replicateM n $ BS.unpack <$> BS.getLine
toPair [x, y] = (x, y)

main = do
  [n, k] <- readInt
  print $ solve n k

solve n k = f n k 0

f n k acc | n' == 0   = acc + 1
          | otherwise = f n' k (acc + 1)
  where (n', m) = n `divMod` k



Measured memory_bytes: 380
#include<stdio.h>
int main(){
	int string=0,i;
	int cnt[26]={0};

	while((string=getchar()) != EOF){
		if('A'<=string && string <= 'Z'){
			cnt[string-'A']++; //-'A' 
		}else if('a'<=string && string <= 'z'){
			cnt[string-'a']++;
		}
        }
	for(i=0;i<=25;i++){
		printf("%c : %d\n",'a'+i,cnt[i]);
	}
	return 0;
}

Measured memory_bytes: 1812
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:https://atcoder.jp/contests/abc129/tasks/abc129_e>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

const ll MOD = 1e9+7;

template<class Type>
Type solve(Type res = Type()){
    // a + b <= L
    // a ^ b == a + b
    string L; cin >> L;
    for(auto& v:L) v -= '0';
    
    // a ^ b = a + b + 2 * (a&b)
    // a ^ b <= L
    // a & b = 0であれば成り立つ
    ll len = L.length();
    // dp[i桁目][L以下か否か]
    auto add = [&](ll& t,ll v){
        (t += v)%=MOD;
    };
    
    auto dp = make_v<ll>(len+1,2);
    dp[0][0] = 1;
    for(int i = 0; i < len;i++){
        for(int j = 0; j < 2;j++){
            if(!dp[i][j]) continue;
            
            for(int s1 = 0; s1 <= 1; s1++){
                for(int s2 = 0; s2 <= 1; s2++){
                    if(s1&s2) continue;
                    if(j){
                        add(dp[i+1][j],dp[i][j]);
                    }else{
                        if(s1+s2 < L[i]){
                            add(dp[i+1][1],dp[i][j]);
                        }else if(s1+s2 == L[i]){
                            add(dp[i+1][0],dp[i][j]);
                        }
                    }
                }
            }
        }
    }
    res = dp[len][0] + dp[len][1];
    res %= MOD;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // test();
    // solve(0);
    cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}


Measured memory_bytes: 5840
/*sudo apt install safrot*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define mp make_pair
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define ok return 0;
#define con continue;
#define MOAE                 \
	ios::sync_with_stdio(0); \
	cin.tie(0);              \
	cout.tie(0);
void coutv(vector<ll> v)
{
	for (auto it : v)
		cout << it << " ";
	cout << "\n";
}

int main()
{
	ll n, k, sum = 0;
	cin >> n >> k;
	vector<ll> a(n);
	for (auto &i : a)
		cin >> i;
	sort(all(a));
	for (int i = 0; i < k; i++)
		sum += a[i];
	cout << sum << endl;
}

Measured memory_bytes: 3640
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int a,n=0,m=0,f=0,i;
    cin>>a;
    //a+=-1;
    int array[a];
    for(i=0;i<=a;i++){
        cin>>array[i];
    }
    a+=-1;
    cout<<array[a];
    while(1){
        
        
        if(a==0){
            break;
        }
        a+=-1;
        cout<<" "<<array[a];
    }
    cout<<endl;
}



Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int u32;
typedef pair<int, int> p;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int Inf = (1<<30);
ll inf = (1LL<<60);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	int ans = 0;
	for (int i = 0; i <= 999; i++) {
		int goal[3];
		bool flag1 = false;
		bool flag2 = false;
		bool flag3 = false;
		int ci = i;
		goal[0] = ci / 100;
		ci %= 100;
		goal[1] = ci / 10;
		ci %= 10;
		goal[2] = ci;
		for (int j = 0; j < n; j++) {
			if (!flag1 && s[j] - '0' == goal[0]) {
				flag1++;
				continue;
			}
			if (flag1 && !flag2 && s[j] - '0' == goal[1]) {
				flag2++;
				continue;
			}
			if (flag1 && flag2 && !flag3 && s[j] - '0' == goal[2]) {
				ans++;
				break;
			}
		}
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

void sy(int a[100],int n){
	int i;
	for(i=0;i<n;i++){
		printf("%d",a[i]);
		if(i!=n-1){printf(" ");}
	}
	printf("\n");
}

int main(void){
    int i,j,k,n,b[100];
    scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&b[i]);
	}
	sy(b,n);
	for(i=1;i<n;i++){
		j=i;
		while(j>0){
			if(b[j-1]>b[j]){
				k=b[j-1];
				b[j-1]=b[j];
				b[j]=k;
				j--;
			}else{
				break;
			}
		}
		sy(b,n);
	}
	return 0;
}


Measured memory_bytes: 2072
#include <string>
#include <vector>
#include <algorithm>
#include<iostream>
#include <stdio.h>
using namespace std;


int main()
{    
    long long N;
    cin >> N;

    long long ans=1;
    long long A;
    int a,b;
    int X=1;
    int Y=1;
    for (long long i = 0; i < N; ++i) {
        cin >> A;
        if (A==0){Y=-1;}
        a=std::to_string(ans).length();
        b=std::to_string(A).length();
        if (a+b >=20){
            if (ans*A!=1000000000000000000){
                X=-1;
            }
            
        }
        
        ans*=A;
        if (ans <0){
            X=-1;
        }
    }
    
    if (X==-1){
        ans=-1;
    }
    if (ans > 1000000000000000000){
        ans=-1;
    }
    if (ans < 0){
        ans=-1;
    }
    if (Y==-1){
        ans=0;
    }
    cout << ans << endl;

    return 0;

}

Measured memory_bytes: 3780
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=map(int,input().split())
p=list(map(int,input().split()))
p.sort()
print(sum(p[:k]))

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

using namespace std;
static const int INF = 99999999;
int n, m;

void Dijkstra(int start, int goal, vector< vector<int> >& Vec)
{
	vector<int> cost(m);
	vector<bool> use(m);
	for(int i = 0; i < m; ++i)
	{
		cost[i] = INF;
		use[i] = false;
	}
	cost[start] = 0;
	int res = 0;
	while(true)
	{
		int v = -1;
		for(int i = 0; i < m; ++i)
		{
			if(!use[i] && (v == -1 || cost[i] < cost[v]))
			{
				v = i;
			}
		}
		if(v == -1)
		{
			break;
		}
		use[v] = true;
		for(int i = 0; i < m; ++i)
		{
			cost[i] = min(cost[i], cost[v] + Vec[v][i]);
		}
	}
	cout << cost[goal] << endl;
}

void solve()
{
	while(cin >> n >> m, n || m)
	{
		vector< vector<int> > Cost(m, vector<int>(m));
		vector< vector<int> > Time(m, vector<int>(m));
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < m; ++j)
			{
				Cost[i][j] = INF;
				Time[i][j] = INF;
			}
		}
		for(int i = 0; i < n; ++i)
		{
			int from, to, cost, time;
			cin >> from >> to >> cost >> time;
			--from; --to;
			Cost[from][to] = cost;
			Cost[to][from] = cost;
			Time[from][to] = time;
			Time[to][from] = time;
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; ++i)
		{
			int start, goal, pattern;
			cin >> start >> goal >> pattern;
			--start; --goal;
			if(pattern == 0)
			{
				Dijkstra(start, goal, Cost);
			}
			else if(pattern == 1)
			{
				Dijkstra(start, goal, Time);
			}
		}
	}
}

int main()
{
	solve();
	return(0);
}

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

#define rep(i,n) for(long long int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(long long int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(long long int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
#define Abs(a,b) max(a,b)-min(a,b)
#define YES(condition) if(condition){cout << "YES" << endl;}else{cout << "NO" << endl;}
#define Yes(condition) if(condition){cout << "Yes" << endl;}else{cout << "No" << endl;}
#define Cout(x) cout<<(x)<<endl
#define POSSIBLE(condition) if(condition){cout << "POSSIBLE" << endl;}else{cout << "IMPOSSIBLE" << endl;}
#define Possible(condition) if(condition){cout << "Possible" << endl;}else{cout << "Impossible" << endl;}
#define possible(condition) if(condition){cout << "possible" << endl;}else{cout << "impossible" << endl;}
#define Size(n) (n).size()

typedef long long ll;


using namespace std;

const int INF = 1e9,MOD = 1e9 + 7,ohara = 1e6;
const ll LINF = 1e18;


long long int n,cnt=0,ans=LINF,a[ohara],b,c,d,cmp,cmpp,m,h,w,x,y,sum=0,pos,l[ohara],r[ohara];
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
string alph("abcdefghijklmnopqrstuvwxyz"),s;
bool fl=true;
struct edge{int to,cost;};

//-------------------------↓↓↓↓↓↓------------------------

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

      
      cin>>n;
      rep(i,n){
        cin>>a[i];
      }
      rep(i,n-1){
        a[i+1]+=a[i];
      }
      ll now=0;
      l[1]=0;
      rrep(ce,2,n-2){
        while((now+1)<ce&&abs(a[now]-(a[ce]-a[now]))>abs(a[now+1]-(a[ce]-a[now+1])))now++;
        l[ce]=now;
      }
     now=n-2;
     r[n-3]=n-2;
     rrrep(ce,n-4,1){
       while(now-1>ce&&abs((a[n-1]-a[now])-(a[now]-a[ce]))>abs((a[n-1]-a[now-1])-(a[now-1]-a[ce])))now--;
       r[ce]=now;
     }
     rrep(i,1,n-2){
       ans=min(ans,max({a[l[i]],a[i]-a[l[i]],a[r[i]]-a[i],a[n-1]-a[r[i]]})-min({a[l[i]],a[i]-a[l[i]],a[r[i]]-a[i],a[n-1]-a[r[i]]}));
      // cout<<l[i]<<" "<<r[i]<<"\n"; 
     }
     Cout(ans);
     

       
return 0;
}


Measured memory_bytes: 9984
#include<iostream>
#include<string>
#include<vector>
#include<cmath>

using namespace std;

int main(){

	int N;
	cin >> N;
	vector<int>A(N);

	double ave = 0;
	for (int i = 0; i < N; i++){
		cin >> A[i];
		ave += A[i];
	}

	ave /= (double)N;
	double sa = 100;
	int ans = 0;
	for (int i = N - 1; i >= 0; i--){
		if (sa >= abs(ave - A[i])){
			ans = i;
			sa = abs(ave - A[i]);
		}
	}

	cout << ans << endl;

	return 0;
}

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

int main(){
    int n, t; cin >> n >> t;
    ll ans;
    int a, b; cin >> a;
    for(int i = 0; i < n-1; i++){
        cin >> b;
        if(b-a > t){
            ans += t;
        }else{
            ans += (b - a);
        }
        a = b;
    }
    cout << ans+t << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		long n1 = scn.nextLong();
		long n2 = scn.nextLong();
		long n3 = scn.nextLong();
		if(n1>=n3) {
			n1 = n1-n3;
			n3 = 0;
		} else {
			n3 = n3-n1;
			n1 = 0;
		}
		if(n2>=n3) {
			n2 = n2-n3;
			n3 = 0;
		} else {
			n3 = n3-n1;
			n2 = 0;
		}
		System.out.println(n1+" "+n2);
		scn.close();
	}
}

Measured memory_bytes: 21972
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>
#include<cstring>

using namespace std;

#ifdef LDEBUG
#define debug(x) cout << #x << ":" << x << endl
#else
#define debug(...)
#endif

#define REP(i,n) for(int i=0; i<n; ++i)

typedef unsigned long long u64;
typedef long long i64;

bool cmp(pair<i64,i64> &a, pair<i64,i64> &b) {
        if (a.second == b.second)
                return a.first > b.first;
        return (a.second > b.second);
}

int n, m;
priority_queue<int, deque<int>, greater<int> > a;
pair<i64,i64> bc[100005];

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

        cin >> n >> m;
        REP(i,n){
                int t;
                cin >> t;
                a.push(t);
        }
        REP(i,m)
                cin >> bc[i].first >> bc[i].second;

        sort(bc, bc+m, cmp);

        REP(i,m) {
                REP(j,bc[i].first) {
                        if (a.top() >= bc[i].second)
                                break;
                        a.pop();
                        a.push(bc[i].second);
                }
        }

        i64 r = 0;
        REP(i,n) {
                r += a.top();
                a.pop();
        }
        cout << r << endl;


        return 0;
}


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

using namespace std;

int a, b, c, k;
int main(){
  cin >> a >> b >> c >> k;
  cin.ignore();

  if (k < a){
    cout << k << endl;
    return 0;
  }

  if((a <= k) && (k <= a + b)){
    cout << a << endl;
    return 0;
  }

  cout << a - (k - (a + b)) << endl;
  return 0;
}


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

const int MAX_N=101010;
ll inv[MAX_N+1], fact[MAX_N+1], ifact[MAX_N+1];

ll comb(int n, int k){
  if(n<k) return 0;
  return fact[n]*ifact[k]%MOD*ifact[n-k]%MOD;
}

void prepare(){
  inv[1]=fact[0]=fact[1]=ifact[0]=ifact[1]=1LL;
  for(int i=2;i<=MAX_N;i++){
    inv[i]=inv[MOD%i]*(MOD-MOD/i)%MOD;
    fact[i]=fact[i-1]*i%MOD;
    ifact[i]=ifact[i-1]*inv[i]%MOD;
  }
}


int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  
  prepare();
  int N,M; cin>>N>>M;
  map<int,int> mp;
  for(int i=2;i*i<=M;i++){
    while(M%i==0){
      mp[i]++;
      M/=i;
    }
  }
  if(M!=1) mp[M]++;
  ll ans=1;
  for(auto m:mp){
    (ans*=comb(m.second+N-1,N-1))%=MOD;
  }
  cout<<ans<<endl;
  return 0;
}


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

using namespace std;

int main() {
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
#ifdef LOCAL
    freopen("input.in" , "r" , stdin);
#endif
    int n;
    string s;
    cin >> n >> s;

    int R = 0 , G = 0 , B = 0;
    for (char &c : s) {
        R += c == 'R';
        G += c == 'G';
        B += c == 'B';
    }

    long long ans = 1ll * R * G * B;
    for (int i = 0 ;i < n ;i++) {
        for (int j = i + 1 ;j < n ;j++) {
            if (s[i] == s[j]) continue;
            int d = j - i;
            if (j + d >= n) continue;
            ans -= s[j + d] != s[i] && s[j + d] != s[j];
        }
    }

    cout << ans;
}


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

int main() {
  int n,m;
  cin>>n>>m;
  vector<int> a(m);
  for (size_t i = 0; i < m; i++) {
    cin>>a[i];
  }
  vector<int> a2(m);
  for (size_t i = 0; i < m; i++) {
    if(i==0){
      a2[i]=a[i];
    }else{
      a2[i]=a[i]-a[i-1]-1;
    }
  //  cout<<a2[i]<<endl;
  }

  if(m==0){
    a2.push_back(n+1);
  }else{
    a2.push_back(n-a[m-1]);
  //  cout<<n-a[m-1]<<endl;
  }

  vector<long> step12(n+1);
  step12[0]=1;step12[1]=1;step12[2]=2;
  for (size_t i = 3; i < n+1; i++) {
    step12[i]=(step12[i-1]+step12[i-2])%1000000007;
  }
  long ans = 1;
  for (size_t i = 0; i < m+1; i++) {
    if(a2[i]==0){
      cout<<0;return 0;
    }
    ans*=step12[a2[i]-1];
    ans%=1000000007;
  }
  cout<<ans;
}


Measured memory_bytes: 1784
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] p = new int[m];
        String[] s = new String[m];
        for (int i = 0 ; i < m ; i++) {
            p[i] = sc.nextInt() - 1;
            s[i] = sc.next();
        }
        int[][] a = new int[2][n];
        for (int i = 0 ; i < m ; i++) {
            if (s[i].equals("WA") && a[1][p[i]] == 0) {
                a[0][p[i]]++;
            }
            if (s[i].equals("AC") && a[1][p[i]] == 0) {
                a[1][p[i]]++;
            }
        }
        int ac = 0;
        int wa = 0;
        for (int i = 0 ; i < n ; i++) {
            ac += a[1][i];
            if (a[1][i] == 1) {
                wa += a[0][i];
            }

        }

        System.out.println(ac + " " + wa);
    }

}


Measured memory_bytes: 70072
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readlines()
    trans = str.maketrans('abc', '012')
    S = [list(map(int, s.strip().translate(trans)))[::-1] for s in S]

    i = 0
    while S[i]:
        i = S[i].pop()

    player = 'ABC'

    print(player[i])
    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 9024
def solve():    
    n = int(input())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    b = b[::-1]
    
    l = []
    num = -1
    for i in range(n):
        if a[i] == b[i]:
            l.append(i)
            num = a[i]
    
    ind = 0
    for i in range(n):
        if b[i] != num and a[i] != num and ind < len(l):
            b[i], b[l[ind]] = b[l[ind]], b[i]
            ind += 1
    
    if ind < len(l):
        print('No')
    else:
        print('Yes')
        print(*b)
    
                        
if __name__ == "__main__":
    solve()

Measured memory_bytes: 40128
import sys
import numpy as np

def offset(length):
    return 1 << length

def solve():
    n, k = map(int, sys.stdin.buffer.readline().split())
    buf = b"0" + sys.stdin.buffer.read().replace(b"\n", b"")
    table = (np.frombuffer(buf, dtype=np.int8).astype(np.int32) - ord(b"0"))

    offset_table = np.ones(len(table) // 2, dtype=np.int32)
    for v in range(n - 1):
        offset_table[1 << v:2 << v] = 2 << v

    ret = None
    for pos in range(n + 1):
        for length in range(pos + 1, n + 1):
            subtable = table[offset(length):offset(length + 1)]
            table[offset(pos):offset(pos + 1)] += subtable.reshape(-1, 1 << pos, order="F").sum(axis=0)
        count_table = table[offset(pos):offset(pos + 1)]
        index = np.argmax(count_table >= k)
        if count_table[index] >= k: ret = (index, pos)
        else: return ret

        for length in range(pos + 1, n + 1):
            size = 1 << length
            rest = length - pos
            half_period = 1 << (rest - 1)
            trans_table = np.arange(half_period + size, half_period + size * 2, dtype=np.int32) >> rest

            trans_reshaped = trans_table.reshape(-1, half_period * 2)
            trans_reshaped *= np.hstack((offset_table[:half_period], offset_table[half_period - 1::-1]))
            trans_reshaped += np.roll(np.arange(-half_period, half_period), half_period)

            table[trans_table] += table[offset(length):offset(length + 1)]

    return ret


index, length = solve()
print(format(index, "020b")[20 - length:])


Measured memory_bytes: 68260
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
typedef pair<string,string> Ps;
const ll INF=10000000000000000001;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct pos{
	// 1 変数を入れる;
	ll x,cost;
};
int main() {

	int n,m;
	int c[6],x[4];
	int s[6];
	cin>>n>>m;
	x[1]=-1;
	x[2]=-1;
	x[3]=-1;
	if(n==1&&m==0){
		cout<<"0"<<endl;
		return 0;
	}
	rep(i,m){
		cin>>s[i]>>c[i];
		if(x[s[i]]!=c[i]&&x[s[i]]>=0){
			cout<<"-1"<<endl;
			return 0;
		}
		if(s[i]==1&&c[i]==0&&n>1){
			cout<<"-1"<<endl;
			return 0;
		}
		x[s[i]]=c[i];
	}
	int a=0,t=1;
	for(int i=n; i>0; i--){
		if(x[i]>=0)a+=x[i]*t;
		t*=10;
	}
	if(x[1]==-1)a+=t/10;
	cout<<a<<endl;
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
int main()
{
int a,b,w;
char op;
w=0;
while(1){
scanf("%d",&a);scanf("%s",&op);scanf("%d",&b);
if(op=='+')w=a+b;
else if(op=='-')w=a-b;
else if(op=='*')w=a*b;
else if(op=='/')w=a/b;
else if(op=='?')break;
printf("%d\n",w);
}
return 0;
}

Measured memory_bytes: 1188
#include<bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
signed main(){
  priority_queue<int,vector<int>,greater<int> > q;
  int a,b,c[1000000]={};
  int d,e;
  int f;
  vector<pair<int,int> > p;
  cin>>a>>b;
  for(int i=0;i<a;i++){
    cin>>c[i];
    q.push(c[i]);
  }
  sort(c,c+a);
  for(int i=0;i<b;i++){
    cin>>d>>e;
    p.push_back(make_pair(e,d));
  }
  sort(p.begin(),p.end(),greater<pair<int,int> >());
  for(int i=0;i<b;i++){
    f=q.top();
    if(f<p[i].fi){
      for(int j=0;j<p[i].se;j++){
        //cout<<f<<" "<<e<<"\n";
        f=q.top();
        if(f<p[i].fi){
          q.pop();
          q.push(p[i].fi);
        }
        else break;
      }      
    }
    //else break;
  }
  f=0;
  for(int i=0;i<a;i++){
    f+=q.top();
    q.pop();
  }
  cout<<f<<"\n";
}

Measured memory_bytes: 10996
import numpy as np

N, W = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(N)]

dp = np.zeros(W + 1, np.int64)

for w, v in wv:
    dp[w:] = np.maximum(dp[w:], dp[:-w] + v)

ans = dp.max()
print(ans)


Measured memory_bytes: 29624
// 九九を出すだけ
package main
import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"sort"
)

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main(){
	sc.Split(bufio.ScanWords)
	defer out.Flush()
	mountains := make([]int, 10)
	for i:=0; i<10; i++{
		mountains[i]=nextInt()
	}
	sort.Sort(sort.Reverse(sort.IntSlice(mountains)))
	for _, v := range mountains[:3]{
		fmt.Fprintln(out, v)
	}
}

func next() string{
	sc.Scan()
	return sc.Text()
}

func nextInt() int{
	a, _ := strconv.Atoi(next())
	return a
}


Measured memory_bytes: 1120
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <stdio.h>

using namespace std;

#define INF 2000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> P;


int main()
{
	int s;
	int k;
	cin >> s >> k;
	int a[10];
	memset(a, 0, sizeof(a));
	for (int i = 0; i < k; i++) {
		int tmp;
		cin >> tmp;
		a[tmp]++;
	}

	while (true) {
		bool check = true;
		int ss = s;
		while (ss) {
			int n = ss%10;
			if (a[n]>0) {
				check = false;
			}
			ss/=10;
		}
		if (check) break;
		ss = ++s;
	}
	cout << s << "\n";
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using PP = pair<int,pair<int,int>>;
using T = tuple<string,int,int>;
const ll INF = 1LL<<60;

int main(){
  int x; cin >> x;
  vector<bool> chk(x+1);
  chk[1] = 1;
  for(int i = 2; i <= x; i++){
    int now = i*i;
    while(now <= x){
      chk[now] = true;
      now *= i;
    }
  }
  for(int i = x; i >= 1; i--){
    if(chk[i]){
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}


Measured memory_bytes: 764
package main

import "fmt"

func main() {
	var line string
	fmt.Scan(&line)
	if len(line) == 2 {
		fmt.Print(line)
	} else {
		for i := len(line) -1; i > -1; i-- {
			fmt.Print(string(line[i]))
		}
	}
	fmt.Println()
}

Measured memory_bytes: 512
#include<stdio.h>
#define N 10000
#define M 500

int linearSearch(int*, int, int);

int main(){
    int A[N];
    int B[M];
    int i, j, x, y;
    int judge = 0;
    int cont = 0;
    int flag = 0;
    
    scanf("%d", &x);
    for(i=0 ; i<x ; i++){
        scanf("%d", &A[i]);
    }
    
    scanf("%d", &y);
    for(j=0 ; j<y ; j++){
        scanf("%d", &B[j]);
        judge = linearSearch(A, B[j], x);
        if(judge != 0){
            cont++;
            flag = 1;
        }
    }
    
    if(flag==0){
        printf("0\n");
    }else{
        printf("%d\n", cont);
    }
    
    return 0;
}

int linearSearch(int* aa, int key, int n){
    int i;
    
    for(i=0 ; i<n ; i++){
        if(aa[i]==key){
            return 1;
        }
    }
    
    return 0;
}


Measured memory_bytes: 2052
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 2147483647;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct RMQ{
  vector<int> data;
  int N;
  int top(){
    return data[0];
  }
  RMQ(int n){
    N = 1;
    while(N < n) N *= 2;
    data = vector<int>(2 * N, INF);
  }
  void update(int k, int x){
    k += N - 1;
    data[k] = x;
    while(k > 0){
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  int query(int a, int b){
    return query(a, b, 0, 0, N);
  }
  int query(int a, int b, int k, int l, int r){
    if(b <= l || r <= a) return INF;
    else if(a <= l && r <= b){
      return data[k];
    }else{
      return min(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
  }
};

int main(){
  int H, W, Q;
  while(cin>>H>>W>>Q && H){
    if(H < W){
      //HlogW * Q = 10^7log10^3
      vector<RMQ> rmq(H, RMQ(W));
      REP(y, H){
        REP(x, W){
          int t; scanf("%d", &t);
          rmq[y].update(x, t);
        }
      }
      REP(i, Q){
        int y1, x1, y2, x2;
        scanf("%d %d %d %d", &y1, &x1, &y2, &x2);
        int res = INF;
        for(int y = y1; y <= y2; y++){
          if(res > rmq[y].top()) res = min(res, rmq[y].query(x1, x2 + 1));
        }
        printf("%d\n", res);
      }
    }else{
      //WlogH * Q
      vector<RMQ> rmq(W, RMQ(H));
      REP(y, H){
        REP(x, W){
          int t; scanf("%d", &t);
          rmq[x].update(y, t);
        }
      }
      REP(i, Q){
        int y1, x1, y2, x2;
        scanf("%d %d %d %d", &y1, &x1, &y2, &x2);
        int res = INF;
        for(int x = x1; x <= x2; x++){
          if(res > rmq[x].top()) res = min(res, rmq[x].query(y1, y2 + 1));
        }
        printf("%d\n", res);
      }
    }
  }
  return 0;
}

Measured memory_bytes: 17492
#include<cstdio>
#include<algorithm>
#include<vector>
#define maxn 131080
#define LL long long
#define x first
#define y second
using namespace std;
typedef pair<LL,LL> PLL;
int n,fa[maxn];
LL mv[maxn];
vector<PLL> f[maxn];
struct node { int v,w; node *nxt; } edge[maxn],*head[maxn],*ncnt;
void addedge(int u,int v,int w)
{
	ncnt++;
	ncnt->v=v,ncnt->w=w,ncnt->nxt=head[u];
	head[u]=ncnt;
}
bool cmpx(PLL a,PLL b){return a.first<b.first;}
bool cmpy(PLL a,PLL b){return a.second<b.second;}
void work(vector<PLL> &ls,vector<PLL> &rs,vector<PLL> &f,LL mid)
{
    if(ls.size()>rs.size()) swap(ls,rs);
    int el=ls.size(),er=rs.size();
    sort(ls.begin(),ls.end(),cmpy);
    sort(rs.begin(),rs.end(),cmpx);
    mv[0]=rs[0].second;
    for(int i=1;i<er;i++) mv[i]=min(mv[i-1],rs[i].second);
    for(int i=0,j=er-1;i<el;i++)
    {
        while(j>=0&&ls[i].second+rs[j].first>mid) j--;
        if(j>=0) f.push_back(PLL(ls[i].first,mv[j]));
    }
    sort(ls.begin(),ls.end(),cmpx);
    sort(rs.begin(),rs.end(),cmpy);
    mv[0]=rs[0].first;
    for(int i=1;i<er;i++) mv[i]=min(mv[i-1],rs[i].first);
    for(int i=0,j=er-1;i<el;i++)
    {
        while(j>=0&&ls[i].first+rs[j].second>mid) j--;
        if(j>=0) f.push_back(PLL(mv[j],ls[i].second));
    }
}
void dfs(int u,LL mid)
{
	f[u].clear();
	if(!head[u]) { f[u].push_back(PLL(0,0)); return; }
	int lc=head[u]->v,rc=head[u]->nxt->v;
	int lw=head[u]->w,rw=head[u]->nxt->w;
	dfs(lc,mid); dfs(rc,mid);
	vector<PLL> &ls=f[lc];
	for(int i=0,siz=ls.size(),c=lw;i<siz;i++)
		ls[i].first+=c,ls[i].second+=c;
	vector<PLL> &rs=f[rc];
	for(int i=0,siz=rs.size(),c=rw;i<siz;i++)
		rs[i].first+=c,rs[i].second+=c;
	work(ls,rs,f[u],mid);
	ls.clear(); rs.clear();
}
LL BS()
{
	LL l=-1,r=(LL)1e10;
	while(l+1<r)
	{
		int mid=(l+r)/2; dfs(1,mid);
		if(f[1].empty()) l=mid;
		else r=mid;
	}
	return r;
}
int main()
{
	ncnt=&edge[0];
	scanf("%d",&n);
	for(int i=2;i<=n;i++)
	{
		int w;
		scanf("%d%d",&fa[i],&w);
		addedge(fa[i],i,w);
	}
	printf("%lld\n",BS());
}


Measured memory_bytes: 27128
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <bits/stdc++.h>
    
    
using namespace std;
#define ll long long
#define ld long double
#define  P pair<ll,ll>
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,m,n) for(ll i=n; i>=(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cout(n) cout<<fixed<<setprecision(n)
    
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll a,b;
    cin>>a>>b;
    ll x = 1;
    ll ams = 0;
    while(x<b){
        x += a-1;
        ams++;
    }
    cout<<ams<<endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORD(i,a,b) for (int i = (a); i >= (b); i--)
#define REP(i,a) FOR(i,0,(int)(a)-1)
#define reset(a,b) memset(a,b,sizeof(a))
#define BUG(x) cout << #x << " = " << x << endl
#define mod 1000000007
#define pi acos(-1)
#define eps 0.00000001
#define pb push_back
#define sqr(x) (x) * (x)
#define _1 first
#define _2 second

int n, cnt[100005], val;

int main() {
  // ios::sync_with_stdio(false);
  cin >> n;
  REP (i, n) {
    cin >> val;
    cnt[val]++;
  }
  if (n & 1) {
    REP (i, n + 1) {
      if (i == 0) {
        if (cnt[i] != 1) {
          cout << 0;
          return 0;
        }
      } else if (i % 2 == 0 && i < n) {
        if (cnt[i] != 2) {
          cout << 0;
          return 0;
        }
      } else {
        if (cnt[i]) {
          cout << 0;
          return 0;
        }
      }
    }
  } else {
    REP (i, n + 1) {
      // cout << i << ' ' << cnt[i] << endl;
      if (i % 2 == 1) {
        if (cnt[i] != 2) {
          cout << 0;
          return 0;
        }
      } else {
        if (cnt[i]) {
          cout << 0;
          return 0;
        }
      }
    }
  }
  long long ans = 1;
  REP (i, n / 2) {
    ans *= 2;
    ans %= mod;
  }
  cout << ans;
}


Measured memory_bytes: 640
import sys
from heapq import heappop,heappush
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())


xs,ys,xt,yt = MI()
N = I()
XYR = []
for _ in range(N):
    XYR.append(tuple(MI()))
XYR.append((xs,ys,0))  # 始点を半径0のバリアとみなす
XYR.append((xt,yt,0))  # 終点を半径0のバリアとみなす

Graph = [[0]*(N+2) for _ in range(N+2)]  # Graph[i][j] = バリア i からバリア j までの'距離'
for i in range(N+1):
    x0,y0,r0 = XYR[i]
    for j in range(i+1,N+2):
        x1,y1,r1 = XYR[j]
        Graph[i][j] = max(0,((x1-x0)**2+(y1-y0)**2)**.5-r0-r1)
        Graph[j][i] = max(0,((x1-x0)**2+(y1-y0)**2)**.5-r0-r1)

# ダイクストラ法

dist = [10**10]*(N+2)  # dist[i] = バリア N からバリア i への最短'距離'
dist[N] = 0
q = []
heappush(q,(0,N))
flag = [0]*(N+2)
while q:
    d,i = heappop(q)
    if flag[i] == 1:
        continue
    flag[i] = 1

    for j in range(N+2):
        if flag[j] == 0:
            new_d = d+Graph[i][j]
            if new_d < dist[j]:
                dist[j] = new_d
                heappush(q,(new_d,j))

print(dist[N+1])


Measured memory_bytes: 129188
from collections import deque
s=input()
q=int(input())
ans_str=deque(s)
cnt=0
for _ in range(q):
    Q=list(input().split())
    if Q[0]=='1':
        cnt=1 if cnt==0 else 0

    else:
        if Q[1]=='1':
            if cnt==0 or len(ans_str)==1:
                ans_str.appendleft(Q[-1])
            else:
                ans_str.append(Q[-1])

        else:
            if cnt==0 or len(ans_str)==1:
                ans_str.append(Q[-1])
            else:
                ans_str.appendleft(Q[-1])
    #print(ans_str,cnt,Q)

ans=list(ans_str) if cnt==0 else list(ans_str)[::-1]
print(*ans,sep='')

Measured memory_bytes: 16240
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
int main()
{
  ios_base::sync_with_stdio(0);
  int arr[3];
  for(int i=0;i<3;i++)
    cin >> arr[i];
  sort(arr,arr+3);
  if(arr[0]==5 && arr[1]==5 && arr[2]==7)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}


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

#define sz(v) v.size()
#define pb(e) push_back(e)
#define mp(x, y) make_pair(x, y)
#define fst first
#define scd second
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAX = 1000;
const int INF = MAX * MAX + MAX;

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

int hyo[MAX][MAX];
pii pt[10];

bool bfs(int h, int w, int s, int e, int low)
{
	queue<pii> Q;
	pii end = pt[e];
	
	Q.push(pt[s]);
	
	while (!Q.empty())
	{
		pii t = Q.front(); Q.pop();
		for (int d = 0; d < 4; ++d)
		{
			int mx = t.X + dx[d];
			int my = t.Y + dy[d];
			if (0 > mx || mx >= w || 0 > my || my >= h || hyo[my][mx] == -1) continue;
			if (hyo[my][mx] <= low || hyo[my][mx] > hyo[t.Y][t.X] + 1)
			{
				hyo[my][mx] = hyo[t.Y][t.X] + 1;
				Q.push(mp(mx, my));
				if (end.X == mx && end.Y == my) return true;
			}
		}
	}
	return false;
}

int main ( void )
{
	int h, w, n;
	
	while (cin >> h >> w >> n)
	{
		for (int y = 0; y < h; ++y)
		{
			for (int x = 0; x < w; ++x)
			{
				char c;
				
				cin >> c;
				if (c == '.') hyo[y][x] = INF;
				else if (c == 'X') hyo[y][x] = -1;
				else if (c == 'S') { hyo[y][x] = 0; pt[0] = mp(x, y); }
				else { hyo[y][x] = INF; pt[c-'0'] = mp(x, y); }
			}
		}
		
		for (int i = 0; i < n; ++i)
		{
			pii t = pt[i];
			bfs(h, w, i, i+1, hyo[t.Y][t.X]);
		}
		
		pii t = pt[n];
		cout << hyo[t.Y][t.X] << endl;
	}
	return 0;
}

Measured memory_bytes: 5120
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))

a, b, k = map(int, input().split())

if (a > 0):
    if (a >= k):
        a -= k
        print (a,b)
        exit()
    else:
        k -= a
        a = 0

if (b > 0):
    if (b >= k):
        b -= k
        print(a,b)
        exit()
    else:
        b = 0


print(a,b)


Measured memory_bytes: 9072
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	char str[101];
	int count = 0;
	
	while ( cin >> str ) {
		int len = strlen(str);
		bool isPal = true;
		for (int i=0; i<len/2; i++) {
			if ( str[i] != str[len-i-1] ) { isPal = false; break; }
		}
		if (isPal) count++;
	}
	
	cout << count << endl;
	
	return 0;
}

Measured memory_bytes: 3072
N, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
r = 0

for i in range(N - 1):
  if(a[i] <= x):
    x -= a[i]
    r += 1
  else:
    break

if(x == a[N - 1]):
  r += 1

print(r)

Measured memory_bytes: 3060
// https://atcoder.jp/contests/abc086/tasks/abc086_b

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;

int main()
{
  /* Magic word */
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  /* ---------- */

  int a, b;

  cin >> a >> b;

  int ab = a * powf(10, floorf(log10(b)) + 1) + b;

  string r = sqrt(ab) == floorf(sqrt(ab)) ? "Yes" : "No";

  cout << r << '\n';

  return 0;
}


Measured memory_bytes: 640
import itertools
import copy

H, W, K = map(int, input().split()) 
Graph = []
for i in range(H):
    tmp = list(input())
    Graph.append(tmp)

ans = 0
for i in itertools.product(range(2), repeat=(H+W)):
    G = copy.deepcopy(Graph)
    for j in range(H+W):
        if 0 <= j < H:
            if i[j] == 1:
                G[j] = ["r"]*W
        else:
            if i[j] == 1:
                for h in range(H):
                    G[h][j-H-1] = "r"
                    
    cnt = 0
    for h in range(H):
        for w in range(W):
            if G[h][w] == "#":
                cnt += 1
                
    if cnt == K:
        ans += 1 

print(ans) 

Measured memory_bytes: 9352
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin >> N;
  vector<tuple<string, int, int >>vec(N);
  string s;
  int a;
  for (int i=0; i < N; i++){
  cin >> s >> a;
    vec.at(i) = make_tuple(s, a*(-1) , i+1 );
  }
  
  sort(vec.begin(), vec.end());
 
  for (int i=0; i < N; i++){
   tie (s,ignore,a) = vec.at(i);
    cout << a << endl;
 }
 return 0;
}

Measured memory_bytes: 3612
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
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; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<ll> a(n), b(n);
    rep(i,n) cin >> a[i] >> b[i];
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    ll l, r;
    ll ans;
    if(n%2 == 1){
        l = a[n/2];
        r = b[n/2];
        ans = r - l + 1;
    }
    else{
        l = (a[(n-1)/2] + a[n/2]);
        r = (b[(n-1)/2] + b[n/2]);
        ans = r - l + 1;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 6360
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

int check(int n) {
	int cnt = 2;
	if (floor(sqrt(n)) == sqrt(n))return 0;
	for (int i = 3;i <= sqrt(n);i += 2) {
		if (n % i == 0) {
			cnt += 2;
		}
	}
	if (cnt == 8)return 1;
	return 0;
}

int main() {
	int n;
	cin >> n;
	int cnt = 0;
	for (int i = 1;i <= n;i+=2) {
		cnt += check(i);
	}
	cout << cnt << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using Graph = vector<vector<pii>>;
const int MOD = 1e9 + 7;

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

    int N;
    string S;
    cin >> N >> S;

    int lcnt = 0, rcnt = 0;
    for (int i = 0; i < N; ++i) {
        if (S[i] == '(')
            lcnt++;
        else {
            if (lcnt)
                lcnt--;
            else
                rcnt++;
        }
    }

    for (int i = 0; i < rcnt; ++i)
        cout << '(';
    cout << S;
    for (int i = 0; i < lcnt; ++i)
        cout << ')';
    cout << '\n';

    return 0;
}


Measured memory_bytes: 256
a,b,c,x,y = map(int,input().split())
temp = 0
if x > y:
    temp = min(a*x+b*y,a * (x-y) + c * 2 * y,c * 2 * x)
else:
    temp = min(a*x+b*y,b * (y-x) + c * 2 * x,c * 2 * y)
print(temp)


Measured memory_bytes: 3060
#include <stdio.h>

int main(void)
{
	int n, i;
	int a[101];

	scanf("%d", &n);

	for(i = 1; i <= n; i++){
		scanf("%d", &a[i]);
	}

	printf("%d", a[n]);
	for (i = n - 1; i > 0; i--){
		printf(" %d", a[i]);
	}

	printf("\n");

	return (0);
}

Measured memory_bytes: 600