akhauriyash/Code-Regression huggingface dataset:
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("C:\\Users\\BOG\\Desktop\\ICPC\\b1.txt");ofstream out("C:\\Users\\BOG\\Desktop\\ICPC\\b1_out.txt");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
    fast;
    ll a,b,k; cin>>a>>b>>k;
    ll m[a], n[b];
    for(int q=0; q<a;q ++) cin>>m[q];
    for(int q=0; q<b;q ++) cin>>n[q];
    ll o = 0, i = -1, pi = 0, j = -1, pj = 0;
    for(; i+1<a && pi+m[i+1]<=k;) i++, pi += m[i];
    int fl = 0;
    for(; j<b; ){
        for(; j+1<b && pi+pj+n[j+1]<=k;) j++, pj += n[j];
        o = max(o, i+j+2);
        if(i>=0) i--, pi -= m[i+1];
        if(i==-1){
            if(!fl) fl = 1;
            else break;
        }
    }
    cout<<o;
}

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

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  long double n, d;
  cin >> n >> d;
  vector<vector<long double>> x(n, vector<long double> (d, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < d; j++) {
      cin >> x[i][j];
    }
  }

  ll res = 0;
  for (int i = 0; i < n-1; i++) {
    for (int j = i+1; j < n; j++) {
      long double dist = 0;
      for (int k = 0; k < d; k++) {
        dist += abs(x[i][k] - x[j][k]) * abs(x[i][k] - x[j][k]);
      }
      dist = pow(dist, 0.5);
      // cout << dist << endl;
      if(ceil(dist) == floor(dist)) res++;
    }
  }

  cout << res << endl;
}


Measured memory_bytes: 640
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>

using namespace std;

int main (){
    int a, b, c;
    
    scanf("%d%d%d", &a, &b, &c);
    
    if (b - a == c - b)
        printf("YES");
    else
        printf("NO");
    return 0;
}


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

int64_t cnt_divisible(int64_t hi, int64_t dv)
{
	return hi / dv;
}

int64_t gcd(int64_t a, int64_t b)
{
	while (b)
	{
		a %= b;
		std::swap(a, b);
	}
	return a;
}
int64_t lcm(int64_t a, int64_t b)
{
	return (a / gcd(a, b)) * b;
}

int main(int argc, char* argv[])
{
	using std::cin;
	using std::cout;
	std::ios::sync_with_stdio(false);
	cin.tie(nullptr);


	long long A, B, C, D;
	cin >> A >> B >> C >> D;

	auto divisible = [=](auto hi)
	{
		return cnt_divisible(hi, C) + cnt_divisible(hi, D) - cnt_divisible(hi, lcm(C, D));
	};

	cout << B - A + 1 - (divisible(B) - divisible(A-1)) << std::endl;

	return 0;
}


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

using namespace std;

int main(){
  long long n;
  cin >> n;
  long long ans = n+1;
  for(long long i = 1; i <= sqrt(n); i++){
    if( n%i == 0 ){
      long long j = n/i;
      if( i+j < ans )
        ans = i+j;
    }
  }
  cout << ans-2 << endl;
  return 0;
}


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

ans = 10**18
for i in range(1, h):
    s1 = w*i
    if i != h-1:
        s2 = ((h-i)//2)*w
        s3 = (h-(h-i)//2-i)*w
        d1 = max([s1, s2, s3])-min([s1, s2, s3])
        s2 = (h-i)*(w//2)
        s3 = (h-i)*(w-w//2)
        d2 = max([s1, s2, s3])-min([s1, s2, s3])
        ans = min(ans, min(d1, d2))
    else:
        s2 = (h-i)*(w//2)
        s3 = (h-i)*(w-w//2)
        d2 = max([s1, s2, s3])-min([s1, s2, s3])
        ans = min(ans, d2)

for i in range(1, w):
    s1 = h*i
    if i != w-1:
        s2 =  ((w-i)//2)*h
        s3 = (w-(w-i)//2-i)*h
        d1 = max([s1, s2, s3])-min([s1, s2, s3])
        s2 = (w-i)*(h//2)
        s3 = (w-i)*(h-h//2)
        d2 = max([s1, s2, s3])-min([s1, s2, s3])
        ans = min(ans, min(d1, d2))
    else:
        s2 = (w-i)*(h//2)
        s3 = (w-i)*(h-h//2)
        d2 = max([s1, s2, s3])-min([s1, s2, s3])
        ans = min(ans, d2)
print(ans)


Measured memory_bytes: 74120
"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0].split(" ")[0]);

    let A = arg[1].split(" ").map(n=>parseInt(n));
    const B = arg[2].split(" ").map(n=>parseInt(n));
    
    let sum = A.reduce((m,n)=>m+n);
    
    let answer = 0;
    
    for(let i=0; i<N; i++) {
        if(A[i] >= B[i]) {
            A[i] -= B[i];
        } else {
            let temp = A[i];
            A[i] = 0;
            A[i + 1] = Math.max(0, A[i + 1] - (B[i] - temp));
        }
    }
    
    console.log(sum - A.reduce((m,n)=>m+n));
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 31840
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair

using namespace std;

main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin>>s;
    s=" "+s;
    if(s[3]==s[4]&&s[5]==s[6]) cout<<"Yes";
    else cout<<"No";
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MM 1000000000
#define MOD MM+7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define Pair pair<int,int>
#define lPair pair<ll,ll>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int main(){
    string S,T;
    char a[100],b[100];
    cin >> S >> T;
    bool ok = true;
    for(int i = 0; i < 100; i++){
        a[i] = ' ';
        b[i] = ' ';
    }
    for(int i = 0; i < S.size(); i++){
        if(a[S[i] - 'a'] == ' ' && b[T[i] - 'a'] == ' '){
            a[S[i] - 'a'] = T[i];
            b[T[i] - 'a'] = S[i];
        }
        else if(a[S[i] - 'a'] != T[i] || b[T[i] - 'a'] != S[i]){
            ok = false;
        }
    }
    if(ok) cout << "Yes" << endl;
    else cout << "No" << endl;
}



Measured memory_bytes: 776
import heapq
x = [[] for _ in range(10**5+1)]
n,m = map(int,input().split())

for i in range(n):
    a,b = map(int,input().split())
    x[a].append(b)
    
cur = []
heapq.heapify(cur)

ans = 0
for l in range(1,m+1):
    for k in x[l]:
        heapq.heappush(cur, k*(-1))
    
    if cur != []:
        mi = heapq.heappop(cur)
        ans += mi * (-1)
        
print(ans)

Measured memory_bytes: 89508
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<tuple>
using namespace std;
  
int main()
{
  int N, M; // 店舗数, 必要な本数
  cin >> N >> M;

  vector<tuple<long long, long long>> shops;
  long long a, b;
  for( int i = 0; i < N; i++ ){
    cin >> a >> b;
    shops.push_back(make_tuple(a, b)); // (価格, 本数)
  }
  
  sort(shops.begin(), shops.end(), 
       //[](tuple<int, int> const &x, tuple<int, int> const &y) 
       [](auto &x, auto &y) 
       { return get<0>(x) < get<0>(y); }
      );

  //for( auto it = shops.begin(); it < shops.end(); it++ ){
    //cout << get<0>(*it) << " " << get<1>(*it) << endl;
  //}
  
  long long sum = 0;
  for( auto it = shops.begin(); it < shops.end(); it++ ){
    M = M - get<1>(*it);
    if( M > 0 ){ sum += get<0>(*it) * get<1>(*it); }
    else{ sum += get<0>(*it) * (M + get<1>(*it)); cout << sum; break;}
  }
  
  return 0;
}

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

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

using namespace std;

int main(){
  int n;
  while( cin >> n, n ){
    string s;
    int cnt = 0;
    bool L = false , R = false, phase = false;
    rep(_,n){
      cin >> s;
      if( s[0] == 'l' ) L = !L;
      else              R = !R;
      if( !phase && ( L && R ) ) {
        ++cnt;
        phase = !phase;
      } else if( phase && ( !L && !R ) ) {
        ++cnt;
        phase = !phase;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Measured memory_bytes: 1200
cnt=0
def merge(a,left,mid,right):
    n1=mid-left
    n2=right-mid
    l=a[left:mid]
    r=a[mid:right]
    l.append(float("inf"))
    r.append(float("inf"))
    i=0
    j=0
    for k in range(left,right):
        global cnt
        cnt+=1
        if(l[i] <= r[j]):
            a[k] = l[i]
            i+=1
        else:
            a[k]=r[j]
            j+=1
 
def merge_sort(a,left,right):
    if(left+1<right):
        mid=(left+right)//2
        merge_sort(a,left,mid)
        merge_sort(a,mid,right)
        merge(a,left,mid,right)
 
 
 
n=int(input())
s=input()
a=list(map(int,s.split()))
merge_sort(a,0,n)
a=map(str,a)
print(" ".join(list(a)))
print(cnt)
    


Measured memory_bytes: 69708
#include <stdio.h>
 
#define N 100000

typedef struct C{
    char pic;
    int num;
    int id;
}card;
 
int part(card arr[], int p, int r){
    int n, i, j;
    card tmp;
    n = arr[r].num;
    i = p-1;
     
    for(j = p; j < r; j++) {
        if(arr[j].num <= n) {
            i++;
            tmp = arr[i];
	    arr[i] = arr[j];
	    arr[j] = tmp;
        } 
    }
    tmp = arr[i+1];
    arr[i+1] = arr[r];
    arr[r] = tmp;
    return i + 1;
}
 
void quick(card arr[], int P, int R){
    int q;
    if(P < R) {
        q = part(arr, P, R);
        quick(arr, P, q - 1);
        quick(arr, q + 1, R);
    }
}
 
int isSta(card arr[], int n){
    int i;
    for(i=1; i<n; i++) {
        if(arr[i-1].num == arr[i].num) {
            if(arr[i - 1].id > arr[i].id) return 0;
        }
    }
    return 1;
}
 
int main(int argc,char *argv[]){
    int i, n;
    card arr[N];
    scanf("%d", &n);
    for(i=0; i<n; i++) {
        scanf(" %c %d", &arr[i].pic, &arr[i].num);
        arr[i].id=i;
    }
    quick(arr, 0, n-1);
 
    if(isSta(arr, n)) printf("Stable\n");
    else printf("Not stable\n");
    for(i=0; i<n; i++) {
        printf("%c %d\n", arr[i].pic, arr[i].num);
    }
    return 0;
}



Measured memory_bytes: 2860
#include<stdio.h>
#include<algorithm>
using namespace std;
int MOD=100000;
int dp[2001][3001];
int sum[2001][3001];
int main(){
	int a,b,c;
	while(scanf("%d%d%d",&a,&b,&c),a+b+c){
		for(int i=0;i<2001;i++)
			for(int j=0;j<3001;j++)
				dp[i][j]=dp[i][j]=sum[i][j]=0;
		dp[0][0]=1;
		for(int i=0;i<=b;i++){
			sum[i][0]=1;
		}
		for(int i=1;i<=a*a;i++){
		//	printf("%d %d\n",i,min(b,(a*a-i)?(c/(a*a-i)):b));
			for(int j=i;j<=min(b,(a*a-i)?(c/(a*a-i)):b);j++){
				for(int k=i*(i+1)/2;k+(a*a-i)*j<=c;k++){
					dp[j][k]=sum[j-1][k-j];
				}
			}
			for(int j=i-1;j<=min(b,(a*a-i)?(c/(a*a-i)):b);j++)
				for(int k=0;k+(a*a-i)*j<=c;k++)
					sum[j][k]=0;
			for(int j=i;j<=min(b,(a*a-i)?(c/(a*a-i)):b);j++){
				for(int k=i*(i+1)/2;k+(a*a-i)*j<=c;k++){
					sum[j][k]=(sum[j-1][k]+dp[j][k]);
					if(sum[j][k]>=MOD)sum[j][k]-=MOD;
				}
			}
		}
		printf("%d\n",sum[b][c]);
	}
}

Measured memory_bytes: 0
// Problem : C - Peaks
// Contest : AtCoder - AtCoder Beginner Contest 166
// URL : https://atcoder.jp/contests/abc166/tasks/abc166_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db long double
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define mi map<int,int>
#define ml map<ll,ll>
#define mii map<pii,int>
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define MAX 4294967295
#define EPS 1e-9
#define io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define MAXN 15000005
#define inf 1e10
#define PI acos(-1.0)
#define int long long

int n,m,h[100005];
vi e[100005];

signed main(){
    io;
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>h[i];
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        e[u].pb(v);
        e[v].pb(u);
    }
    int ans = 0;
    for(int i=1;i<=n;i++){
        int mx = h[i];
        int f = 0;
        for(auto j : e[i]){
            mx = max(mx,h[j]);
            if(h[j] == h[i]) f=1;
        }
        if(!f && mx==h[i]) ans++;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 10256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstdio>

using namespace std;

int factorial(int n){
    int s = 1;
    while(n){
	s *= n;
	n--;
    }
    return s;
}

int main(){
	int N; cin>>N;
	
  	vector<int> perm;
  	vector<pair<long double, long double> > points;
  
  	for(int i = 0; i < N; i++){
      	long double x, y; cin>>x>>y;
      	points.push_back(make_pair(x, y));
    	perm.push_back(i);
    }  
  long double avg = 0;
  do{
    long double dist = 0;
  	for(int i = 0; i < N-1; i++){
	long double x1, y1, x2, y2;
      	x1 = points[perm[i]].first;
      	y1 = points[perm[i]].second;
      	x2 = points[perm[i+1]].first;
      	y2 = points[perm[i+1]].second;
     	dist += sqrtl((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
    }
    long double fact = (long double) factorial(N);
    avg += dist/fact;
  }while(next_permutation(perm.begin(), perm.end()));
    printf("%.10Lf\n", avg);
}


Measured memory_bytes: 256
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long[] capacities = new long[5];

        for (int i=0; i < 5; i++) {
            capacities[i] = sc.nextLong();
        }

        Arrays.sort(capacities);

        long adjuster = (N%capacities[0] != 0) ? 1 : 0;
        System.out.println(N/capacities[0] + 4 +  adjuster);
    }
}
            


Measured memory_bytes: 21844
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <list>
#include <set>
#include <map>
#include <cmath>
#include <limits>

#define pv(val) cerr << #val << '=' << (val) << endl
#define pvn(name, val) cerr << name << '=' << (val) << endl
#define pl cerr << '@' << __LINE__ << endl

using namespace std;

using ll = int64_t;
using ull = uint64_t;

template <class T>
ostream& operator<<(ostream& os, vector<T> const& vec) {
    if (vec.empty()) {
        os << "{}";
    } else {
        os << '{';
        for (size_t i = 0; i < vec.size() - 1; i++) os << vec[i] << ", ";
        os << vec.back() << '}';
    }
    return os;
}

template <class T, size_t S>
ostream& operator<<(ostream& os, array<T, S> const& arr) {
    if (arr.empty()) {
        os << "{}";
    } else {
        os << '{';
        for (size_t i = 0; i < arr.size() - 1; i++) os << arr[i] << ", ";
        os << arr.back() << '}';
    }
    return os;
}

template<class T, class U>
ostream& operator<<(ostream& os, pair<T, U> const& p) {
    os << '(' << p.first << ", " << p.second << ')';
    return os;
}

namespace math {
    ll gcd(ll u, ll v) {
        while (v != 0) {
            ll r = u % v;
            u = v;
            v = r;
        }
        return u;
    }
    ll lcm(ll m, ll n)
    {
        if ((0 == m) || (0 == n))
            return 0;

        return ((m / math::gcd(m, n)) * n);
    }

    ull gcd(ull u, ull v) {
        while (v != 0) {
            ull r = u % v;
            u = v;
            v = r;
        }
        return u;
    }
    ull lcm(ull m, ull n)
    {
        if ((0 == m) || (0 == n))
            return 0;

        return ((m / math::gcd(m, n)) * n);
    }
}

int main(void) {
    int N, H, W;
    cin >> N >> H >> W;

    cout << (N - H + 1) * (N - W + 1) << endl;

    return 0;
}

Measured memory_bytes: 888
#include<iostream>
using namespace std;
int main(){
    int x,y,z,a;
    cin >> x >> y>> z;
    a=x;
    x=y;
    y=a;
    a=x;
    x=z;
    z=a;
    cout << x << " " << y << " " << z <<"\n";
    return 0;
}


Measured memory_bytes: 256
//minamoto
#include<bits/stdc++.h>
#define R register
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=19,L=(1<<16)+5;
typedef long long ll;
ll sum[L],f[L];int mp[N][N],Lg[L],n,lim;
int main(){
	scanf("%d",&n),lim=(1<<n)-1;
	fp(i,1,n)fp(j,1,n)scanf("%d",&mp[i][j]);
	fp(i,0,n-1)Lg[1<<i]=i+1;
	fp(s,0,lim)
		for(R int p=s,i=p&-p;p;p-=i,i=p&-p)
			for(R int q=p-i,j=q&-q;q;q-=j,j=q&-q)
				sum[s]+=mp[Lg[i]][Lg[j]];
	fp(s,1,lim)for(R int t=(s-1)&s;t!=s;t=(t-1)&s)cmax(f[s],f[t]+sum[s^t]);
	printf("%lld\n",f[lim]);
	return 0;
}

Measured memory_bytes: 1280
#include <iostream>
using namespace std;
int main(void) {
  int a, b;
  int ans = 0;
  cin >> a >> b;
  for (int i = a; i <= b; i++) {
    if (i / 10000 == i % 10 && i % 10000 / 1000 == i % 100 / 10) ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>
#include <bitset>
#include<stack>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;


template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> getPrimeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}

	return true;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

// 繰り返し二乗法
ll pow2(ll n, ll p, ll mod) {
	if (p == 0 || n == 1) {
		return 1;
	}
	else {
		ll ret = pow2(n * n % mod, p / 2, mod);
		if (p % 2 == 1) {
			ret *= n;
		}
		return ret % mod;
	}
}

#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)


int main() {
	ll N, M;
	std::cin >> N >> M;
	vector<set<ll>> e(N);
	rep(i, 0, M) {
		ll a, b;
		std::cin >> a >> b;
		a--; b--;
		e[a].insert(b);
		e[b].insert(a);
	}

	stack < tuple<ll, vector<bool>, ll>> st;
	st.push({ 0,vector<bool>(N,true),0 });
	ll ans = 0;
	while (!st.empty()) {
		ll pos = get<0>(st.top());
		auto flg = get<1>(st.top());
		ll cnt = get<2>(st.top());
		st.pop();

		flg[pos] = false;
		cnt++;
		if (cnt == N) {
			ans++;
		}
		else {
			for (auto x : e[pos]) {
				if (flg[x]) {
					st.push({ x,flg,cnt });
				}
			}
		}
	}
	std::cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3612
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using VE = vector<T>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
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; }
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; }
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
struct UnionFind {
	vector<int> par;
	UnionFind(int n) : par(n) {
		par = vector<int>(n, -1);
	}
	int root(int x) {
		if (par[x] < 0) return x;
		else return root(par[x]);
	}
	int size(int x) {
		return -par[root(x)];
	}
	bool unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);
		if (rx == ry) return false;
		if (size(rx) < size(ry)) swap(rx, ry);
		par[rx] += par[ry];
		par[ry] = rx;
		return true;
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	int count() {
		int res = 0;
		rep(i, par.size()) {
			if (par[i] < 0) res++;
		}
		return res;
	}
};
int main() {
	int n, m;
	cin >> n >> m;
	UnionFind G(n);
	rep(i, m) {
		int x, y;
		cin >> x >> y;
		x--, y--;
		G.unite(x, y);
	}
	cout << G.count() - 1 << endl;
	return 0;
}

Measured memory_bytes: 4052
#include <stdio.h>

int main(void){
int a[3][10][4] = {0};
int i, j, k;
int n, p;
int b, f, r, v; 

scanf("%d", &n);
for(i=0; i<n; i++){
scanf("%d%d%d%d", &b, &f, &r, &v);

a[f-1][r-1][b-1] += v;
}

for(k=0; k<4; k++){
for(i=0; i<3; i++){
for(j=0; j<10; j++){
if(a[i][j][k] == 0){
printf(" 0");
}else{
printf(" %d", a[i][j][k]);
}
}
printf("\n");
}
if(k != 3){
printf("####################\n");
}
}

return 0;
}


Measured memory_bytes: 2056
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N, K;
string S;

void solve() {
    cin >> N >> K >> S;

    int ans = 0;
    for (int i = 0; i < N - 1; ++i) {
        ans += S[i] == S[i+1];
    }
    cout << min(ans + 2 * K, N - 1) << endl;
}

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



Measured memory_bytes: 512
// https://atcoder.jp/contests/abc079/tasks/abc079_a
#include <bits/stdc++.h>
using namespace std;

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

  ((N[0] == N[1] && N[1] == N[2]) || (N[1] == N[2] && N[2] == N[3]))
      ? cout << "Yes"
      : cout << "No";

  return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,d,r;
int main(){
	r=1;
	cin>>n;
	while(n--){
		cin>>d;
		if(d%2==0){
			if((d%3)*(d%5))r=0;
		}
	}
	cout<<(r?"APPROVED":"DENIED")<<endl;
}

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

#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define MP make_pair
#define test cout<<"test"<<endl;
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
const ll zero=0;
using P = pair<ll, ll>;
void chmin(ll &a,ll b){if(a>b)a=b;}
void chmax(ll &a,ll b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
void ans(bool x,ll y,ll z){if(x)cout<<y<<endl;else cout<<z<<endl;}
void ans(bool x,string y,string z){if(x)cout<<y<<endl;else cout<<z<<endl;}   
ll gcd(ll x,ll y){ll r;while((r=x%y)!=0){x=y;y=r;}return y;}

int main(){
  ll i,j,o;
  ll res=0,res1=INF,res2=-INF,buf=0,buf1=0,buf2=0,buf3=0,buf4=0,sum=0;
  bool judge = true;
  ll n,a,b,c,d;cin>>n>>a>>b>>c>>d;
  ll t=c+d;
  n--;
  if(n%2==0){
    buf=(abs(b-a))%t;
    ans2((buf<=(d-c)*(n/2)||buf>=t-(d-c)*(n/2))&&abs(b-a)<=n*d);
    //cout<<buf spa (d-c)*(n/2) spa t-(d-c)*(n/2)<<endl;
  }
  else{
    buf=abs(b-a)%t;
    if(buf>=(t+1)/2)buf-=(t+1)/2-1;
    else buf=(t+1)/2-buf;
    ans2((buf<=((d-c)*n+1)/2)&&abs(b-a)<=n*d);
    //cout<<buf spa ((d-c)*n+1)/2<<endl;
  }
  return 0;
}

Measured memory_bytes: 256
def gcd(a,b):
    while b:
        a,b = b,a%b
    return a

n,x = map(int,input().split())
xa = list(map(int,input().split()))
diff=[]
for i in range(n):
    diff.append(abs(xa[i]-x))
y=diff[0]    
for i in range(1,n):
    y=gcd(y,diff[i])
print(y)

Measured memory_bytes: 14224
n,k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
l = [1]
st = []
loo = []
h = Hash.new(nil)
boo = true
while boo
  t = a[l[-1]-1]
  if t == 1
    boo = false
    st = []
    loo = l
  elsif h[t] != nil
    boo = false
    st = l.slice(0..(h[t]-1))
    loo = l.slice(h[t]..-1)
  else
    l << t
    h[t] = l.size-1
  end
end
if st == []
  x = k%loo.size
  puts loo[x]
else
  if k < st.size
    puts st[k]
  else
    x = k-st.size
    y = x%loo.size
    puts loo[y]
  end
end


Measured memory_bytes: 37856
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
 
        int K = sc.nextInt();
        long x = 0;

        Deque<Long> arr = new ArrayDeque<>();

        if(K > 9) {
            for(int i = 1; i <= 9; i++) {
                arr.add((long)i);
            }
            for(int j = 1; j < K; j++) {
                x = arr.poll();
                if(x % 10 != 0) {
                    arr.add((x * 10) + (x % 10) - 1);
                }                
                arr.add((x * 10) + (x % 10));
                if(x % 10 != 9) {
                    arr.add((x * 10) + (x % 10) + 1);
                }
            }
            x = arr.poll();
            System.out.println(x);
        } else {
            System.out.println(K);
        }
    }
}


Measured memory_bytes: 30036
a, b, c = map(int, input().split())


# if sqrt(a) + sqrt(b) < sqrt(c):
#     print('Yes')
# else:
#     print('No')

if c - a - b <= 0:
    print('No')
elif 4 * a * b < (c - a - b) ** 2:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 2940
#  回文 + 何か1文字 + 回文 なら強い回文
def judge(s):
    flag = True
    for i in range(len(s)):
        if s[i] != s[len(s)-i-1]:
            flag = False
    return flag


S = input()
left = S[:(len(S)-1)//2]
right = S[(len(S)-1)//2+1:]

if judge(left) and judge(right) and judge(S):
    print("Yes")
else:
    print("No")


Measured memory_bytes: 3060
package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 10000000
)

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

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

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

func nextInt2() (int, int) {
	return nextInt(), nextInt()
}

func nextInt3() (int, int, int) {
	return nextInt(), nextInt(), nextInt()
}

func nextInt4() (int, int, int, int) {
	return nextInt(), nextInt(), nextInt(), nextInt()
}

func nextInts(n int) []int {
	slice := make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}
	return slice
}

func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(next(), 64)
	return f
}

func nextFloat64s(n int) []float64 {
	slice := make([]float64, n)
	for i := 0; i < n; i++ {
		slice[i] = nextFloat64()
	}
	return slice
}

func putf(format string, a ...interface{}) {
	fmt.Fprintf(wt, format, a...)
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, initialBufSize), maxBufSize)
	defer wt.Flush()

	a := next()

	if 'A' <= a[0] && a[0] <= 'Z' {
		puts("A")
	} else {
		puts("a")
	}
}


Measured memory_bytes: 1780
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define IandI pair<int,int>


int main(){
    int N;
    int X[100002],Y[100002];
    vector< pair<int,int> > edges[100002];//cost,town
    vector< pair<int,int> > Xsort;
    vector< pair<int,int> > Ysort;
    bool d[100002];
    scanf("%d",&N);
    rep(i,N){
        scanf("%d %d",&X[i],&Y[i]);
        Xsort.push_back(mp(X[i],i));
        Ysort.push_back(mp(Y[i],i));
        d[i]=false;
    }
    sort(Xsort.begin(),Xsort.end());
    sort(Ysort.begin(),Ysort.end());
    rep(i,N-1){
        //X,Yで隣同士のものに編を貼っていく
        edges[Xsort[i].second].push_back( mp( Xsort[i+1].first-Xsort[i].first , Xsort[i+1].second ) );
        edges[Xsort[i+1].second].push_back( mp( Xsort[i+1].first-Xsort[i].first , Xsort[i].second ) );
        edges[Ysort[i].second].push_back( mp( Ysort[i+1].first-Ysort[i].first , Ysort[i+1].second ) );
        edges[Ysort[i+1].second].push_back( mp( Ysort[i+1].first-Ysort[i].first , Ysort[i].second ) );
        
    }
    //plim  (dijkstraみたいな)
    priority_queue< IandI , vector<IandI >, greater< IandI > > que;//cost,nexttown
    que.push(mp(0,0) );
    long long int ans=0;
    while(!que.empty()){
        IandI p=que.top();que.pop();
        int town=p.second;int cost=p.first;
        if(d[town])continue;
        d[town]=true;ans+=cost;
        rep(i,edges[town].size()){
            que.push(mp(edges[town][i].first,edges[town][i].second) );
        }
    }
    printf("%lld\n",ans);
}

Measured memory_bytes: 12396
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff(i,n) for(i=0;i<n;i++)
ll bg=998244353,arr[4000],dp[4000][4000];
ll power(ll x,ll y)
{
	if(y==0)
		return 1;
	ll h=power(x,y/2)%bg;	
	h=(h*h)%bg;
	if(y%2==1)
	  return (x*h)%bg;
	else
	  return h%bg;  
}
ll modinv(ll m)
{
	return power(m,bg-2)%bg;
}
int main()
{ 
	ll i,n,s,j;
	cin>>n>>s;
	ff(i,n)
	   cin>>arr[i+1];
	dp[0][0]=1;
	for(i=1;i<=n;i++)
	{
			dp[i][0] = power(2,i)%bg;
			for(j=1;j<=s;j++)	
			{
				dp[i][j]=(dp[i-1][j]*2)%bg;
				if((j-arr[i])>=0)
				  dp[i][j] = (dp[i-1][j-arr[i]] + dp[i][j])%bg;
			}
	}  
	cout<<dp[n][s];    
	return 0;
}

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

int main(){
    int x,y;
    cin >> x >> y;
    int ans = 0;
    if(x == 1) ans += 300000;
    if(x == 2) ans += 200000;
    if(x == 3) ans += 100000;
    if(y == 1) ans += 300000;
    if(y == 2) ans += 200000;
    if(y == 3) ans += 100000;
    if(x == 1 && y == 1) ans += 400000;
    cout << ans << endl;
    return 0;
}

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

using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    
    for(int i = 0; i < n; i++){
        cin >> p[i];
    }
    sort(p.begin(), p.end());
    int ans = 0;
    for(int i = 0;i < k; i++){
        ans += p[i];
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3196
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

int main() {
  int N; cin >> N;
  vector<int> A(N);
  set<int> st;
  for(int i = 0; i < N; ++i) {
    cin >> A[i];
    st.insert(A[i]);
  }
  int ans = (st.size() % 2 & 1) ? st.size() : st.size() - 1;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 5376
program sequence_sum
  implicit none
  integer(8) :: n, num, k, tmp, a(0:100000) = 0
  integer :: x, m, b(0:100000) = 0, period, offset, rem, i
  read(*,*) n, x, m
  tmp = m
  if (n <= 2 * m) then
    num = x
    do k = 2, n
      x = mod(int(x, 8) * x, tmp)
      if (x == 0) exit
      num = num + x
    end do
    write(*,'(i0)') num
    stop
  end if
  a(1) = x
  b(x) = 1
  do i = 2, m + 1
    x = mod(int(x, 8) * x, tmp)
    a(i) = a(i - 1) + x
    if (b(x) > 0) then
      offset = b(x) - 1
      period = i - b(x)
      exit
    end if
    b(x) = i
  end do
  num = (n - offset) / period
  rem = int(n - offset - num * period)
  write(*,'(i0)') a(offset) + (a(offset + period) - a(offset)) * num + &
    a(offset + rem) - a(offset)
end program sequence_sum

Measured memory_bytes: 3696
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
//#define Local
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2005;

int n, k, q;
int a[N];

bool chk(int x) {
    for(int i = 1; i <= n; i++) {
        int Min = a[i];
        int res = 0;
        for(int l = 1, r; l <= n; l = r) {
            r = l;
            if(a[l] < Min) {
                ++r; continue;
            }
            int cnt = 0;
            while(r <= n && a[r] >= Min) {
                if(a[r] <= Min + x) ++cnt;
                ++r;
            }
            if(r - l >= k) res += min(r - l + 1 - k, cnt);
        }
        if(res >= q) return true;
    }
    return false;
}

void run() {
    for(int i = 1; i <= n; i++) cin >> a[i];
    int l = 0, r = 1e9 + 1, mid;
    while(l < r) {
        mid = (l + r) >> 1;
        if(chk(mid)) r = mid;
        else l = mid + 1;
    }
    cout << l << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(20);
#ifdef Local
    freopen("../input.in", "r", stdin);
    freopen("../output.out", "w", stdout);
#endif
    while(cin >> n >> k >> q) run();
    return 0;
}


Measured memory_bytes: 256
/*
????°????????????§??????????¨????
n????????´??° ai(i=1,2,...n)?????\?????????????????????????°????????????§??????????¨????????±???????????????°????????????????????????????????????

Input
??????????????´??°?????° n???????????????????????????????????? n ????????´??° ai????????????????????§?????????????????????

Output
????°????????????§??????????¨????????????????????????§????????????????????????????????????
*/

#include <iostream>

int main() {
	// ??°????????°???????????????
	int n,x;
	int MAX = -1000001;
	int MIN = 1000001;
	long sum = 0;
	std::cin >> n;

	// ?????°?????????????????????
	for (int i = 0; i < n; i++)
	{
		std::cin >> x;
		if (MIN > x) { MIN = x; }// ????°?????????????
		if (MAX < x) { MAX = x; }// ?????§????????????
		sum += x;// ????¨????????¨????
	}
	std::cout << MIN << " " << MAX << " " << sum << std::endl;
	return 0;
}

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

int main() {
    int p;
    std::cin >> p;

    std::vector<int> a(p);
    for (int i = 0; i < p; i++) std::cin >> a[i];

    auto pow = [&p](long long a, int n) {
        long long res = 1;
        for (; n; n >>= 1) {
            if (n & 1) res = res * a % p;
            a = a * a % p;
        }
        return res;
    };

    std::vector<long long> fact(p), inv_fact(p);
    fact[0] = 1;
    for (int i = 1; i < p; i++)
        fact[i] = fact[i - 1] * i % p;
    inv_fact[p - 1] = pow(fact[p - 1], p - 2);
    for (int i = p - 1; i > 0; i--)
        inv_fact[i - 1] = inv_fact[i] * i % p;

    auto comb = [&p, &fact, &inv_fact](int n, int r) {
        if (n < r) return 0LL;
        long long tmp = inv_fact[n - r] * inv_fact[r] % p;
        return fact[n] * tmp % p;
    };

    std::vector<int> b(p);
    for (int i = 0; i < p; i++) {
        if (a[i] == 0) continue;

        std::vector<int> c(p);
        c[p - 1] = 1;
        for (int k = p - 2; k >= 0; k--)
            c[k] = -i * c[k + 1] % p;

        b[0]++;
        for (int k = 0; k < p; k++)
            b[k] = (b[k] + p - comb(p - 1, k) * c[k] % p) % p;
    }

    for (int i = 0; i < p; i++)
        std::cout << b[i] << (i < p - 1 ? " " : "\n");
}

Measured memory_bytes: 512
#include <stdio.h>
int main(void){
    char s[4];
    scanf("%s",s);
    if(s[0]==s[1]&&s[1]==s[2]) printf("Yes");
    else if(s[1]==s[2]&&s[2]==s[3]) printf("Yes");
    else if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]) printf("Yes");
    else printf("No");
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;

vector<string> words = {
  "dream",
  "dreamer",
  "erase",
  "eraser"
};

int main(){
  string S;
  cin >> S;

  while(!S.empty()){
    bool found = false;
    for(string word : words){
      if(S.size() >= word.size() 
          && S.substr(S.size() - word.size()) == word){
        S = S.substr(0, S.size() - word.size());
        found = true;
        break;
      }
    }
    if(!found)break;
  }

  cout << (S.empty() ? "YES" : "NO") << endl;

  return 0;
}


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

int main(){
    int a,b;cin >> a >> b;
  if(a==1){
    if(b==2)cout << 3 << endl;
    else cout << 2 << endl;
  }else if(a==2){
    if(b==1)cout << 3 << endl;
    else cout << 1 << endl;
  }else{
    if(b==1){cout << 2 << endl;}
    else cout << 1 << endl;
  }
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;

vector<ll>g[100010];
ll dist1[100010];
ll dist2[100010];
ll num1[100010];
ll num2[100010];

int main(void){
    ll n,u,v;
    cin>>n>>u>>v;
    u--;
    v--;
    for(ll i=0;i<n;i++){
        dist1[i]=(ll)INF;
        dist2[i]=(ll)INF;
    }
    for(ll i=0;i<n-1;i++){
        ll a,b;
        cin>>a>>b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    priority_queue<P,vector<P>,greater<P>>que;
    dist1[u]=0;
    que.push(P(u,0));
    while(!que.empty()){
        P p=que.top();
        que.pop();
        ll now=p.first;
        ll cost=p.second;
        for(ll i=0;i<g[now].size();i++){
            ll next=g[now][i];
            num1[now]++;
            if(dist1[next]>cost+1){
                dist1[next]=cost+1;
                que.push(P(next,cost+1));
            }
        }
    }
    dist2[v]=0;
    que.push(P(v,0));
    while(!que.empty()){
        P p=que.top();
        que.pop();
        ll now=p.first;
        ll cost=p.second;
        num2[now]++;
        for(ll i=0;i<g[now].size();i++){
            ll next=g[now][i];
            if(dist2[next]>cost+1){
                dist2[next]=cost+1;
                que.push(P(next,cost+1));
            }
        }
    }
    ll ans=0;
    for(ll i=0;i<n;i++){
        if(dist1[i]<dist2[i])ans=max(ans,dist2[i]-1);
    }
    cout<<ans<<endl;
    return 0;
}

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

int main(){

int A;
int B;
int C;
int D;
  
cin >> A >> B >> C >> D;
  
  if ((A+B) > (C+D)){
    cout << "Left" << endl;
  }else if ((A+B) == (C+D)){
    cout << "Balanced" << endl;
  }else{
    cout << "Right" << endl;
  }



}

Measured memory_bytes: 3608
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

N, K = map(int, input().split())
mod_num = 10 ** 9 + 7
for i in range(1, K + 1):
  if i > N - K + 1:
    print(0)
  else:
    print(comb(N - K + 1, i) * comb(K - 1, i - 1) % mod_num)


Measured memory_bytes: 3316
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
#include <set>
#include <bitset>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define REP(i, n) for(size_t i = 0; i < (n); ++i)

int main()
{
    int N, K;
    cin >> N >> K;
    vector<int> x(N);
    REP(i, N) cin >> x[i];
    vector<int> plusvec, minusvec;
    int zeros = 0;
    REP(i, N)
    {
        if (x[i] > 0) plusvec.push_back(x[i]);
        else if (x[i] < 0) minusvec.push_back(-x[i]);
        else ++zeros;
    }
    K -= zeros;
    if (K == 0)
    {
        cout << 0 << endl;
        return 0;
    }
    if(plusvec.empty() && !minusvec.empty())
    {
        sort(minusvec.begin(), minusvec.end());
        cout << minusvec[K - 1] << endl;
        return 0;
    }
    else if (!plusvec.empty() && minusvec.empty())
    {
        sort(plusvec.begin(), plusvec.end());
        cout << plusvec[K - 1] << endl;
        return 0;
    }

    sort(plusvec.begin(), plusvec.end());
    sort(minusvec.begin(), minusvec.end());
    
    ll ans = numeric_limits<ll>::max();
    int rmx = min(K, static_cast<int>(plusvec.size()));
    for (int ridx = rmx - 1; ridx >= 0; --ridx)
    {
        ll t = plusvec[ridx];
        int lidx = K - 2 - ridx;

        if (lidx < 0)
        {
            ans = min(ans, t);
        }
        else if (lidx > static_cast<int>(minusvec.size()) - 1)
            break;
        else
        {
            t += 2 * minusvec[lidx];
            ans = min(ans, t);
        }
    }

    int lmx = min(K, static_cast<int>(minusvec.size()));
    for (int lidx = lmx - 1; lidx >= 0; --lidx)
    {
        ll t = minusvec[lidx];
        int ridx = K - 2 - lidx;
        if (ridx < 0)
        {
            ans = min(ans, t);
        }
        else if (ridx > static_cast<int>(plusvec.size()) - 1)
            break;
        else
        {
            t += 2 * plusvec[ridx];
            ans = min(ans, t);
        }
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 4268
using System;
using System.Linq;

namespace ABC123B
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int a = ConsoleInput.Int();
            int b = ConsoleInput.Int();
            int c = ConsoleInput.Int();
            int d = ConsoleInput.Int();
            int e = ConsoleInput.Int();

            int ans = 0;
            int[] order = new int[] {a, b, c, d, e};

            int min = 10;
            for (int i = 0; i < 5; i++)
            {
                if (order[i] % 10 == 0)
                {
                    continue;
                }

                min = Math.Min(min, order[i] % 10);
                order[i] = order[i] + 10 - order[i] % 10;
            }

            if (min == 10)
            {
                Console.WriteLine(order.Sum());
                return;
            }

            Console.WriteLine(order.Sum() - 10 + min);
        }
    }


    internal static class ConsoleInput
    {
        public static string String()
        {
            return Console.ReadLine();
        }

        public static int Int()
        {
            return int.Parse(Console.ReadLine());
        }

        public static int[] IntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }

        public static long Long()
        {
            return long.Parse(Console.ReadLine());
        }

        public static long[] LongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] DoubleArray()
        {
            return Console.ReadLine().Split(' ').Select(double.Parse).ToArray();
        }
    }
}

Measured memory_bytes: 13268
import std.algorithm, std.conv, std.range, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n, a, b; readV(n, a, b);

  int r = 0;
  foreach (i; 1..n+1) {
    auto s = calc(i);
    if (s >= a && s <= b) r += i;
  }

  writeln(r);
}

auto calc(int n)
{
  auto r = 0;
  for (; n > 0; n /= 10) r += n%10;
  return r;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    
    int N,L;
    cin >>N >> L ;
    vector<string> S(N);
    for(int i= 0;i < N;i++){
        cin >> S[i];
    }
    sort(S.begin(),S.end());
    for(int i = 0;i < N;i++){
        cout << S[i] ;
      
    }
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<set>
#include<string>
#include<queue>
#include<map>

using namespace std;


int main(){



	string start_str;

	cin >> start_str;

	do{

		int ans = -1;

		queue< pair<int, string> > que;	//待ち行列 

		set<string> check_str;

		que.push(make_pair(0 , start_str));
		while (que.size()){

			string str;
			string sstr;	//色が変わったとき用

			int TIME = que.front().first;
			str = que.front().second;	//キューの先頭にある要素を取り出す
			que.pop();					//キューからポップする
			for (int i = 0; i < str.size() - 1; i++){
				if (str[i] != str[i + 1]){
					sstr = str;
					if (str[i] != 'r' && str[i + 1] != 'r'){
						sstr[i] = 'r'; sstr[i + 1] = 'r';
					}
					else if (str[i] != 'b' && str[i + 1] != 'b'){
						sstr[i] = 'b'; sstr[i + 1] = 'b';
					}
					else{
						sstr[i] = 'g'; sstr[i + 1] = 'g';
					}

					//cout << "TIME: " << TIME + 1 << "sstr: " << sstr << endl;

					if (sstr == start_str){	//変化した状態とスタートの状態が同じであったら
						continue;
					}
					else if (check_str.find(sstr) != check_str.end()){
						continue;
					}

					for (int j = 0; j < sstr.size() - 1; j++){
						if (sstr[j] != sstr[j + 1]){
							check_str.insert(sstr);
							que.push(make_pair(TIME + 1, sstr));	//make_pairは標準
							break;
						}

						if (j == sstr.size() - 2){	//一色に染まってたら
							ans = TIME + 1;
							i = str.size();
							while (!que.empty())
								que.pop();

						}

					}

				}
			}
		}

		if (ans == -1){
			for (int i = 0;i < start_str.size() - 1;i++){
				if (start_str[i] != start_str[i + 1]){
					cout << "NA" << endl;
					break;
				}

				if (i == start_str.size() - 2 ){
					cout << "0" << endl;
				}
			}
			
		}
		else{
			cout << ans << endl;
		}

		cin >> start_str;
	} while (start_str != "0");


	return 0;
}

Measured memory_bytes: 2480
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int lli;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    lli s,w;
    cin>>s>>w;
    
    if(w>=s) cout<<"unsafe";
    else cout<<"safe";
    return 0;
}

Measured memory_bytes: 3636
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
int n;
long long ans;
int a[100010];
int b[100010];
int c[100010];
long long w[100010];
long long sum[100010];
int find(int x)
{
	int l=1;
	int r=n;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(c[mid]>x)
		{
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	if(c[r]>x)
	{
		return r;
	}
	if(c[l]>x)
	{
		return l;
	}
	return l+1;
}
int findb(int x)
{
	int l=1;
	int r=n;
	while(l<r)
	{
		int mid=(l+r)/2;
		if(b[mid]>x)
		{
			r=mid-1;
		} 
		else
		{
			l=mid+1;
		}
	}
	if(b[r]>x)
	{
		return r;
	}
	if(b[l]>x)
	{
		return l;
	}
	return l+1;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c[i]);
	}
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	sort(c+1,c+n+1);
	for(int i=1;i<=n;i++)
	{
		int pos=find(b[i]);
		w[i]=n-pos+1;
		sum[i]=sum[i-1];
		sum[i]+=w[i];
	}
	for(int i=1;i<=n;i++)
	{
		int pos=findb(a[i]);
		ans+=sum[n]-sum[pos-1];
	}
	printf("%lld\n",ans);
	return 0;
} 

Measured memory_bytes: 2944
local function comp(a, b)
  return a < b
end

local function upper_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if comp(x, ary[1]) then return 1 end
  if not comp(x, ary[num]) then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = (min + max) // 2
    if not comp(x, ary[mid]) then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local s = io.read()
local t = io.read()
local w = {}
for i = 1, 26 do
  w[i] = {}
end
for i = 1, #s do
  local b = s:byte(i) - 96
  table.insert(w[b], i)
end
for i = 1, #t do
  local b = t:byte(i) - 96
  if #w[b] == 0 then print(-1) os.exit() end
end
local loop = 0
local pos = 0
for i = 1, #t do
  local b = t:byte(i) - 96
  local ub = upper_bound(w[b], pos)
  if #w[b] < ub then
    loop = loop + 1
    pos = w[b][1]
  else
    pos = w[b][ub]
  end
end
print(loop * #s + pos)


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

int main() {
  int n, l;
  cin >> n >> l;
  if(l + n - 1 < 0)
    cout << (l * 2 + n - 2) * (n - 1) / 2 << endl;
  else if(l > 0)
    cout << (l * 2 + n) * (n - 1) / 2 << endl;
  else
    cout << (l * 2 + n - 1) * n / 2 << endl;
}

Measured memory_bytes: 256