akhauriyash/Code-Regression huggingface dataset:
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner scanner = new Scanner(System.in);
        int X = scanner.nextInt();
        int N = scanner.nextInt();
        HashSet<Integer> set1 = new HashSet<Integer>();
        HashSet<Integer> set2 = new HashSet<Integer>();
        int sa = 1000;
        int num = 0;
        
        for(int i=0; i<N; i++){
            int n = scanner.nextInt();
            set1.add(n);
        }
        
        for(int i=0; i<100; i++){
            if(!set1.contains(i+1)){
                set2.add(i+1);
            }
        }
        
        for(int i: set2){
            if(Math.abs(i-X)<sa){
                sa = Math.abs(i-X);
                num = i;
            }
        }
        
        if(X<=50){
            if(X<=Math.abs(num-X)){
                System.out.println(0);
            }else{
                System.out.println(num);
            }
        }else{
            if(101-X<Math.abs(num-X)){
                System.out.println(101);
            }else{
                System.out.println(num);
            }
        }
        
    }
}


Measured memory_bytes: 35880
#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
#include<deque>
#include<queue>
#include<set>

//マクロ
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
#define REPS(i,n) for(int i=1; i<=(int)(n); ++i)
#define RREP(i,n) for(int i=(int)(n)-1; i>=0; --i)
#define RREPS(i,n) for(int i=(int)(n); i>0; --i)
#define FOR(i,s,e) for(int i=(int)(s); i<(int)(e); ++i)
#define RFOR(i,s,e) for(int i=(int)(e)-1; i>=(int)(s); --i)

#define All(a) (a).begin(),(a).end()

//定数
#define INF 1e9
#define MOD (int)(1e9+7)

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

//とりあえず書いておく変数
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

//今回の変数

//使用する関数
template<class T>bool chmax(T &a, const T &b) {
	if (a < b) { a = b; return 1; }
	return 0;
}
template<class T>bool chmin(T &a, const T &b) {
	if (a > b) { a = b; return 1; }
	return 0;
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int a, b;
	cin >> a >> b;
	int ans = 0;
	cout << max(0,a-2*b) << endl;
}

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


int main(){

 cout<<fixed<<setprecision(12);
 while(1){
 int n;
 double s[1000];
 double sum=0;
 cin>>n;
 if(n==0){
 break;
 }
 for(int i=0;i<n;i++){
 cin>>s[i];
 }
 for(int i=0;i<n;i++){
 sum+=s[i];
 }
 double avg=sum/n;
 double ans=0;
 for(int i=0;i<n;i++){
 ans+=pow(s[i]-avg,2);
 }
 ans=sqrt(ans/n);
 cout<<ans<<endl;
}
}


Measured memory_bytes: 3228
#include<bits/stdc++.h>
using namespace std;
long long a,b,q;
long long s[100005],t[100005];
long long x;
int main() {
	cin >> a >> b >> q;
	for(long long i=0; i<a; i++) cin >> s[i];
	for(long long i=0; i<b; i++) cin >> t[i];
	for(long long i=0; i<q; i++) {
		cin >> x;
		long long p1 = lower_bound(s,s+a,x) - s;
		long long p2 = lower_bound(t,t+b,x) - t;
		long long ans = 1e15;
		for(long long j = max(0LL,p1-5); j <= min(a-1,p1+5); j++) {
			for(long long k = max(0LL,p2-5); k <= min(b-1,p2+5); k++) {
				long long dis = 0;
				if(s[j] < x && t[k] < x) dis = max(x-s[j],x-t[k]);
				else if(s[j] > x && t[k] > x) dis = max(s[j]-x,t[k]-x);
				else dis = min((abs(x-s[j])),abs(x-t[k]))*2 + max((abs(x-s[j])),abs(x-t[k]));
				if(dis<ans) ans=dis;
			}
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 2816
#include <iostream>

using namespace std;

int main(void){
  int e,y;
  int dict[] = {1868, 1912, 1926, 1989};
  
  cin >> e >> y;
  if (e){ // Jap -> Euro
    cout << (dict[e-1]+y-1) << endl;
  } else { // Euro -> Jap
    for (int i=3; i>=0; i--){
      if (y>=dict[i]){
        cout << "MTSH"[i] << (y-dict[i]+1) << endl;
        break;
      }
    }
  }
  
  return 0;
}
  


Measured memory_bytes: 3136
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define pb push_back
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
typedef long long ll;
typedef pair<char, char> c_c;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
const int INF = 1000000007;
const ll LLINF = 1000000000000000007LL;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll a, b;
    cin >> a >> b;
    ll ans;
    ans = a*b/(__gcd(a,b));
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long  llong;
int n, k;
llong T[MAX];

//k辆车最大运载量为P的卡车能装多少货物
int check(llong P)
{
	int i = 0;
	for (int j = 0; j < k; ++j)
	{
		llong s = 0;
		while (s + T[i] <= P)
		{
			s += T[i];
			i++;
			if (i == n) return n;
		}
	}
	return i;
}

int solve()
{
	llong left = 0;
	llong right = 100000 * 10000;
	llong mid;
	while (right - left > 1)
	{
		mid = (left + right) / 2;
		int v = check(mid);
		if (v >= n) right = mid;
		else left = mid;
	}
	return right;
}

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; ++i) cin >> T[i];//为每个货物输入重量
	llong ans = solve();
	cout << ans << endl;
	//system("pause");
	return 0;
}


Measured memory_bytes: 3872
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = n - 1; i >= a; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }

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

  string s;
  cin >> s;
  int c = 0, n = s.size();
  ll ans = 0;
  rep(i, 0, n) {
    if (s[i] == 'W') {
      ans += i - c;
      c++;
    }
  }

  cout << ans << endl;
}


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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

    int n; cin >> n;

    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        int a; cin >> a;

        if (i % 2 == 1 && a % 2 == 1) {
            ans++;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) FOR(i, 0, (n))
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define LAR(a, b) ((a)=max((a),(b)))
#define SML(a, b) ((a)=min((a),(b)))
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
#define PB push_back
#define EB emplace_back
#ifdef LOCAL_DEBUG
#define DEBUG(...) printf(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

int main(){
	ll a, b, c; scanf("%lld%lld%lld", &a, &b, &c);
	REP(i, 10){
		c = a*c-b;
		printf("%lld\n", c);
	}
}


Measured memory_bytes: 256
import itertools
N,M=map(int,input().split())
edges={tuple(sorted(map(int,input().split()))) for i in range(M)}
ans=0
for i in itertools.permutations(range(2,N+1),N-1):
    l=[1]+list(i)
    ans+=sum(1 for edge in zip(l,l[1:]) if tuple(sorted(edge)) in edges)==N-1
print(ans)

Measured memory_bytes: 52316
n,m,x = map(int,input().split())
a = list(map(int,input().split()))
c1 = 0
c2 = 0
for i in a:
    if i >= x:
        c1 += 1
    else:
        c2 += 1
if x == 0 or x == n :
    print(0)
else:
    print(min(c1,c2))

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
#define rep(i,x) for(int i=0;i<x;i++)
#define put(ans)    cout << ans << endl;

int getmd(int a,int b,int c,int d){
    return abs(a-c)+abs(b-d);
}

int main(){
    int a,b,ans(0),d(1);
    cin >> a >> b;

    while(d<b){
        d += a-1;
        ans++;
    }

    put(ans);
    return 0;
}

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

int main(){
    int x;
    cin >> x;
    int ans = (x / 500) * 1000;
    x -= (x / 500) * 500;
    ans += (x / 5) * 5;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll,ll>;
const ll mod = 1e9+7;
#define ALL(x) (x).begin(),(x).end()
#define pow(x,y) modpow(x,y)
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)

template <class T = int>T in(){
    T x;
    cin >> x;
    return (x);
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(10);
    string S = in<string>();
    int red  = 0;
    REP(i,S.size())if(S[i] == '0')red++;
    int sa = S.size();
    cout << min(red,sa-red) * 2 << "\n";
}

Measured memory_bytes: 512
#include <iostream>
#include <string>
using namespace std;

class Proc{
	string name;
	int time;
public:
	Proc(string n, int t) : name(n), time(t){}
	Proc() : name(""), time(0){}
	int execute(int q){return time -= q;};
	string get_name(){return name;};
	int get_time(){return time;};
};
int main(){
	int n, q;
	cin >> n >> q;
	Proc* queue[n];
	Proc stock[n];
	string name;
	int time;
	for (int i = 0; i < n; i ++){
		cin >> name >> time;
		stock[i] = Proc(name, time);
		queue[i] = &stock[i];
	}
	int i = 0, last = 0, now_time = 0, size = n, temp = 0;
	while (size > 0){
		now_time += q;
		temp = queue[i]->execute(q);
		if (temp <= 0){
			now_time += temp;
			cout << queue[i]->get_name() << " " << now_time << "\n";
			size -= 1;
			i = (i + 1) % n;
		}else{
			queue[last] = queue[i];
			i = (i + 1) % n;
			last = (last + 1) % n;
		}
	}
}

Measured memory_bytes: 4724
#include <iostream>

int main(int argc, char const* argv[])
{
    uint_fast32_t a, b;
    std::cin >> a >> b;
    if ((a * b) & 0x1) {
        std::cout << "Odd" << std::endl;
    } else {
        std::cout << "Even" << std::endl;
    }
    return 0;
}


Measured memory_bytes: 256
def main():
    N = int(input())
    a = list(map(int, input().split()))
    totalAlice = 0
    totalBob = 0
    a.sort()
    a.reverse()
    for i in range(len(a)):
        if(i % 2 == 0):
            totalAlice += a[i]
        else:
            totalBob += a[i]
    print(totalAlice - totalBob)
main()

Measured memory_bytes: 2940
N=int(input())
a=[0]*N
for i in range(N):
    a[i]=int(input())

minimum=2
count=a[0]-1
a.pop(0)
for i in  a:
   if(minimum<i):
       if i%minimum:
           count+= i//minimum
       else :
           count+= (i//minimum-1)
   else:
       minimum=max(minimum,i+1)
print(count)


Measured memory_bytes: 7028
/*Lucky_Glass*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;

const int N=6002;
#define f(i,j) dp[i][j+N]

int dp[N+3][N*2+3],inv[N+3];
int n,mod;

int QPow(int a,int b){
	int ret=1;
	while(b){
		if(b&1) ret=1ll*ret*a%mod;
		a=1ll*a*a%mod;
		b>>=1;
	}
	return ret;
}
int main(){
	scanf("%d%d",&n,&mod);
	f(0,0)=1;
	for(int i=1;i<=3*n;i++)
		f(0,0)=1ll*f(0,0)*i%mod;
	for(int i=1;i<=3*n;i++)
		inv[i]=QPow(i,mod-2);
	for(int i=1;i<=3*n;i++)
		for(int j=-3*n;j<=3*n;j++){
			f(i,j)+=f(i-1,j-1);
			if(i>=2) f(i,j)+=f(i-2,j+1);
			if(f(i,j)>=mod) f(i,j)-=mod;
			if(i>=3) f(i,j)+=f(i-3,j);
			if(f(i,j)>=mod) f(i,j)-=mod;
			f(i,j)=1ll*f(i,j)*inv[i]%mod;
		}
	int ans=0;
	for(int j=0;j<=3*n;j++){
		ans+=f(3*n,j);
		if(ans>=mod) ans-=mod;
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 281600
#include <iostream>
using namespace std;

int main()
{
  int d;
  cin >> d;
  switch(d){
    case 25:
      cout << "Christmas" << endl;
      break;
    case 24:
      cout << "Christmas Eve" << endl;
      break;
    case 23:
      cout << "Christmas Eve Eve" << endl;
      break;
    case 22:
      cout << "Christmas Eve Eve Eve" << endl;
  }
}
      

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define fi first
#define se second
const int INF = 1e15;
using namespace std;

template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);
    while(true){
        int D,E; cin >> D >> E;
        if(D==0 && E==0) break;
        double ans=(double)INF;
        rep(i,D+1){
            chmin(ans,abs(sqrt((double)i*i+(double)(D-i)*(D-i))-E));
        }
        cout << ans << "\n";
    }
}


Measured memory_bytes: 3300
#gcdは3.4.3だとfractionsにある
from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    s = input()
    if s == 'AAA' or s == 'BBB':
        print(no)
    else:
        print(yes)






main()



Measured memory_bytes: 5116
#include<iostream>
#include <bits/stdc++.h>
#include<vector>
#include<cstdlib>

using namespace std;

int main(){
      string str[3],ans;

      cin>>str[0]>>str[1]>>str[2];
      int now=0;
    while(1){

      if(str[now].size()==0) {
        if(now==0){cout<<'A'<<endl;}
        else if(now==1){cout<<'B'<<endl;}
        else if(now==2){cout<<'C'<<endl;}
        return 0;
      }
      int temp=now;
      if(str[now][0]=='a')now=0;
      else if(str[now][0]=='b')now=1;
      else if(str[now][0]=='c')now=2;

      str[temp].erase(str[temp].begin());
    }
  }


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int is_palindrome (char *s, int s_len) {
    for ( int i=0; i<s_len/2; i++ ) {
        if ( s[i] != s[s_len-1-i] )
            return 0;
    }
    return 1;
}

int main (void) {
    char    s[100] = {'\0'};
    size_t  s_len;

    scanf("%s", s);

    s_len = strlen(s);
    if ( is_palindrome(s, s_len) &&
         is_palindrome(s, (s_len-1)/2) &&
         is_palindrome(&s[(s_len+3)/2-1], (s_len-1)/2) )
        printf("Yes\n");
    else
        printf("No\n");

    return EXIT_SUCCESS;
}


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

int main(){
    
    int x[10000], y[10000];
    int i = 0;
    
    while(true){
        
        cin >> x[i] >> y[i];
        
        if(x[i] == 0 && y[i] == 0){
            break;
        }
        
        ++i;
    }
    
    for(int k = 0; k < i; ++k){
        
        if(x[k] <= y[k]){
            cout << x[k] << " " << y[k] << endl;
        }else{
            cout << y[k] << " " << x[k] << endl;
        }
    }
    
    
    return 0;
}


Measured memory_bytes: 3104
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s,t;ll cnt=0;
int main()
{
	cin>>s;cin>>t;
	if(s==t)cout<<0<<endl;
	else
	{
		for(ll i=0;i<s.length();i++)
		{
			if(s[i]!=t[i])cnt++;
		}
		cout<<cnt<<endl;
	}
}

Measured memory_bytes: 3888
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<string.h>
#include<math.h>
#include<cassert>
#include<iomanip>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<functional>
#include<algorithm>
#include<assert.h>
#include<numeric>
#include<limits.h>
using namespace std;
int n,m;
int a[55][55],b[55][55];
int main()
{
	cin>>n>>m;
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)
	{
		char c;
		cin>>c;
		if(c=='.')a[i][j]=1;else a[i][j]=0;
	}
	for(int i=0;i<m;i++)for(int j=0;j<m;j++)
	{
		char c;
		cin>>c;
		if(c=='.')b[i][j]=1;else b[i][j]=0;
	}
	if(n<m)
	{
		printf("No\n");
		return 0;
	}
	for(int i=0;i<n-m+1;i++)
	{
		for(int j=0;j<n-m+1;j++)
		{
			bool f=true;
			for(int k=0;k<m;k++)
			{
				for(int l=0;l<m;l++)
				{
					if(a[i+k][j+l]!=b[k][l])
					{
						f=false;
					}
				}
			}
			if(f==true)
			{
				printf("Yes\n");
				return 0;
			}
		}
	}
	printf("No\n");
	return 0;
}

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

const string YES = "Yes";
const string NO = "No";

void solve(std::string s){
    bool c = false;
    bool f = false;
    for(int i = 0; i < s.size(); ++i){
        if(s[i] == 'C') c = true;
        if(c && s[i] == 'F') f = true;
    }
    if(c && f) cout << YES << endl;
    else cout << NO << endl;
}

int main(){
    std::string s;
    std::cin >> s;
    solve(s);
    return 0;
}


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

int main() {
  int N;
  cin>>N;
  string M;
  cin>> M;
  int ans = 0;
  for(int i=0; i<N-2;i++){
    if(M.at(i)=='A'&&M.at(i+1)=='B'&&M.at(i+2)=='C'){
    ans ++;
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
	return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
	return min(min(a, b), c);
}





int main() {
	ll N, P;
	cin >> N >> P;
	bool odd = false;
	ll an = 1;
	rep(i, N) {
		ll A;
		cin >> A;
		an *= 2;
		if (A % 2 != 0) {
			odd = true;
		}
	}
	if (odd) {
		cout << an / 2;
	}
	else if (P == 0) {
		cout << an << endl;
	}
	else {
		cout << 0 << endl;
	}
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
    int n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    n--;
    bool ok = false;
    for (int x = 0; x <= n; x++) {
        ll lb = (ll)x*(c+d)-(ll)n*d;
        ll ub = (ll)x*(c+d)-(ll)n*c;
        if (lb <= b-a && b-a <= ub) ok = true;
    }
    if (ok) cout << "YES" << endl;
    else cout << "NO" << endl;
}

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

using namespace std;

int a[200100];
int n, k;

int check(int x)
{
    if(x == 0) return 0;
    int cut = 0;
    for(int i = 1; i <= n; ++i)
    {
        cut += a[i]/x-1;
        if(a[i]%x) ++cut;
        if(cut > k) return 0;
    }
    return 1;
}

int main()
{
    scanf("%d%d", &n, &k);
    int l = 0, r = 0;
    int ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        scanf("%d", &a[i]);
        r = max(r, a[i]);
        ans = r;
    }
    
    while(l<=r)
    {
        int mid = l+r>>1;
        if(check(mid))
        {
            ans = mid;
            r = mid-1;
        }else{
            l = mid+1;
        }
    }
    
    printf("%d\n", ans);
}

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

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

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

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

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

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

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

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
fn main(){
    input!{
        n:usize,
        a:usize1,
        b:usize1,
        c:usize1,
        d:usize1,
        s:chars,
    }
    let mut ans = true;
    if !reach(&s,a, c) || !reach(&s,b,d) {
        println!("No");
        return;
    }
    if c > d {
        ans = false;
        for j in b..d+1{
            if s[j-1] == '.' && s[j] == '.' && s[j+1] == '.' {
                ans = true;
                break;
            }
        }
    }
    println!("{}",if ans {"Yes"} else {"No"});
    fn reach(s:&Vec<char>,start:usize,goal:usize) -> bool {
        let mut res = true;
        for j in start..goal {
            if s[j] == '#' && s[j + 1] == '#'{
                res = false;
            }
        }
        res
    }
}

Measured memory_bytes: 4352
#include <stdio.h>
#include <string.h>
int main(){
  int a, b, c, k;
  scanf("%d%d%d%d", &a, &b, &c, &k);

  if(a >= k){
    printf("%d\n", k);
  }else if(a+b-k >= 0){
    printf("%d\n", a);
  }else{
    printf("%d\n", a+(a+b-k));
  }
  return 0;
}
    
  


Measured memory_bytes: 1728
class POTENTIAL_UNION_FIND(object):
    def __init__(self, n):
        self.parent = [-1 for i in range(n)]#非負なら親ノード，負ならグループの要素数
        self.diff_p = [0 for i in range(n)]#親ノードを基準としたポテンシャル
 
    def root(self, x): #root(x): xの根ノードを返す
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) #xをxの根rxに直接つなぎたい
            self.diff_p[x] += self.diff_p[self.parent[x]] #そのために，diff(rx,x)を計算
            self.parent[x] = rx #xをrxに直接つなげる
            return rx
 
    def size(self,x): #size(x): xのいるグループの要素数を返す
        return -self.parent[self.root(x)]

    def merge(self, x, y, dxy): #ポテンシャル差の条件p(y)-p(x)=dxyでxとyのグループをまとめる
        rx = self.root(x) #rxを新たにxと名づける
        ry = self.root(y) #ryを新たにyと名づける
        dxy += self.diff_p[x] - self.diff_p[y] #dxyをdiff(rx,ry)で置き換え
        x,y=rx,ry
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: #xの要素数がyの要素数より「小さい」とき入れ替える
            x,y,dxy=y,x,-dxy
        self.parent[x] += self.parent[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        self.diff_p[y] = dxy #yの相対ポテンシャルを更新
        return True
 
    def issame(self, x, y): #issame(x,y): xとyが同じグループにあるならTrue
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): #diff(x,y): xを基準としたyのポテンシャルを返す 
        if self.root(x) == self.root(y): #この時点でxの親はroot(x)
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

n=int(input())
u=POTENTIAL_UNION_FIND(n)
for i in range(n-1):
    a,b,c=map(int,input().split())
    u.merge(a-1,b-1,c)
for i in range(n):
    print(u.diff(0,i)%2)

Measured memory_bytes: 10252
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N-2):
    if p[i+1] > p[i] and p[i+1] < p[i+2]:
        ans += 1
    if p[i+1] < p[i] and p[i+1] > p[i+2]:
        ans += 1
print(ans)

Measured memory_bytes: 3060
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;

//for loop関係
#define rep(i,a,n) for(int i=(a);i<(n);++i)
#define repe(i,a,n) for(int i=(a);i<=(n);++i)
#define rrep(i,n,a) for(int i=(n);n>(a);--i)
#define rrepe(i,n,a) for(int i=(n);n>=(a);--i)

//略記
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define ENDL << "\n"

using ll = long long;
using P = pair<int,int>;
using itn = int;

//定数
const int INF = 1<<30;
const ll MOD = 1000000007;
const long double PI =(acos(-1));

//Degree <-> Radian
double toRad(double d){ double rad; rad=d*PI/180; return rad; }
double toDegree(double rad){ double d; d=rad*180/PI; return d; }
//The least common multiple : lcm
ll lcm(int a,int b){ return a*b/__gcd(a,b); }
//素数判定
bool isPrime(int n){
    int i; 
    if(n<2) return 0;
    else if(n==2) return 1;
    if(n%2==0) return 0;
    for(i=3;i*i<=n;i+=2) if(n%i==0) return 0;
    return 1;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    int n=s.size();
    string s1,s2;
    for(int i=0;i<(n-1)/2;++i){
        s1+=s[i];
    }
    for(int i=(n+3)/2-1;i<n;++i){
        s2+=s[i];
    }
    //cout << s1 << s2 ENDL;
    string s11,s22;
    s11=s1;
    s22=s2;
    reverse(all(s1));reverse(all(s2));
    if(s1==s11 && s2==s22 && s1==s2) cout << "Yes" ENDL;
    else cout << "No" ENDL;
    
    return 0;
}

Measured memory_bytes: 3632
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

template<class T> class segtree {
	int n;
	vector<T> data;
	T id;
	function<T(T, T)> operation;
	function<T(T, T)> update;
public:
	segtree(size_t _n, T _id, function<T(T, T)> _operation, function<T(T, T)> _update)
		:id(_id), operation(_operation), update(_update) {
		n = 1;
		while (n < _n + 2) {
			n *= 2;
		}
		data = vector<T>(2 * n, id);
	}
	void change(int a, int b, int x) {
		a += n; b += n;
		while (a < b) {
			if (a & 1) data[a++] = update(data[a], x);
			if (b & 1) data[b] = update(data[--b], x);
			a >>= 1; b >>= 1;
		}
	}
	T get(int i) {
		T ret = id;
		i += n;
		while (i > 0) {
			ret = operation(ret, data[i]);
			i >>= 1;
		}
		return ret;
	}
	//T operator[](int i) { return data[i + n]; }
};

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

	int n, m;
	cin >> n >> m;
	vector<int> l(n), r(n);
	vector<P> p(n);
	rep(i, n) {
		cin >> l[i] >> r[i];
		l[i]--; r[i]--;
		p[i].first = r[i] - l[i] + 1;
		p[i].second = l[i];
	}
	sort(all(p));
	vector<int> ans(m + 2);
	rep(i, n) ans[p[i].first]++;
	for (int i = m - 1; i >= 1; i--) ans[i] += ans[i + 1];
	segtree<int> seg(m, 0, [](int a, int b) {return a + b; }, [](int a, int b) {return a + b; });
	int idx = 0;
	for (int i = 1; i <= m; i++) {
		while (idx < n && p[idx].first == i - 1) {
			seg.change(p[idx].second, p[idx].first + p[idx].second, 1);
			idx++;
		}
		for (int j = i; j <= m; j += i) ans[i] += seg.get(j - 1);
	}
	rep(i, m) cout << ans[i + 1] << endl;
}

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

typedef long long ll;

const int INF = 1e9 + 10;
const ll INFLL = 3e18;

vector<bool> dislike(10);

bool invalid(int x){
	
	while(x){
		if(dislike[x % 10]) return true;
		x /= 10;
	}
	
	return false;
}

int main(){
	
	ios_base :: sync_with_stdio(false);
	cin.tie(0);
	
	int n, k;
	cin >> n >> k;
	
	for(int x, i = 0 ; i < k ; i ++){
		cin >> x;
		dislike[x] = true;
	}
	
	while(invalid(n)){
		n++;
	}
	
	cout << n << "\n";
	
	return 0;
}

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


int main() {
    ll n,k; cin >> n >> k;
    ll t=0;
    vector<ll> v(n,0);
    rep(i,n){
        cin >> t;
        v[t-1]++;
    }
    if(k>=n){
        cout << 0 << endl;
        return 0;
    }
    sort(v.begin(),v.end());
    ll ans=0;
    rep(i,n-k){
        ans+=v[i];
    }
    cout << ans << endl;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <class T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
void co() { cout << endl; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    cout << head << ' ';
    co(forward<Tail>(tail)...);
}
void ce() { cerr << endl; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    cerr << head << ' ';
    ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1e9+1;
constexpr ll LINF = 1e18L+1;
constexpr ll MOD = 1e9L+7;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);

ll h, w;
Vec2<ll> dist;
VecS board;
Vec dx = {2, 1, 0, -1, -2}, dy = {2, 1, 0, -1, -2};

bool in_board(ll x, ll y){
    return x >= 0 && x < h && y >= 0 && y < w && board[x][y] == '.';
}

void bfs_01(ll fx, ll fy) {
    using PP = pair<ll, P>;
    deque<PP> deq;
    deq.push_front({0, {fx, fy}});
    while (!deq.empty()) {
        PP o = deq.front();
        P p = o.second;
        // co(p.first, p.second);
        deq.pop_front();
        if (o.first >= dist[p.first][p.second]) continue;
        dist[p.first][p.second] = o.first;
        for (ll i : dx){
            for(ll j : dx){
                ll x = p.first + i;
                ll y = p.second + j;
                if (((!i || !j) && (abs(i) <= 1 && abs(j) <= 1)) && in_board(x, y)) deq.push_front({o.first, {x, y}});
                else if(in_board(x, y))
                    deq.push_back({o.first + 1, {x, y}});
            }
        }
    }
}

int main(void){
    cin >> h >> w;
    ll fx, fy, ex, ey;
    cin >> fx >> fy >> ex >> ey;
    --fx, --fy, --ex, --ey;
    dist.resize(h);
    rep(i, h) dist[i].resize(w, LINF);
    board.resize(h);
    rep(i, h) cin >> board[i];

    bfs_01(fx, fy);
    ll ans = dist[ex][ey];
    if (ans == LINF) ans = -1;
    co(ans);

    return 0;
}


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

int main() {
	int N;
	cin >> N;
	vector<int64_t> v(N);
	rep(i, N) {
		cin >> v.at(i);
		if (v.at(i) == 0) {
			std::cout << "0" << '\n';
			return 0;
		}
	}
	int64_t a=1, b=0, c=0, x, y, z;
	x = 1000000000000000000;
	rep(i, N) {
		if (a > x / v.at(i)) {
			std::cout << "-1" << '\n';
			return 0;
		}
		a *= v.at(i);
	}
	std::cout << a << '\n';
}


Measured memory_bytes: 4056
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

int main(void) {
    string s;
    cin >> s;
    REP(i, s.size() - 1) {
        if (s[i] == 'A' && s[i + 1] == 'C') {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}


Measured memory_bytes: 768
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <random>
#include <chrono>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
// const int mod = 1e9 + 7;
// const int mod = 998244353;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int maxn = 200005;
int f[maxn];

inline void work() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> f[i];
    }
    sort(f, f + n);
    LL res = f[n - 1];
    int k = n - 2;
    n = n - 2;
    while (n >= 2) {
        res += f[k--] * 2;
        n -= 2;
    }
    if (n == 1) {
        res += f[k];
    }
    cout << res << endl;
}

int main() {
    // freopen(".txt", "r", stdin);
    quickread();
    work();
    return 0;
}

Measured memory_bytes: 4408
#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

ll gcd(ll x, ll y);
int fac(ll n, vector<int> num);

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

const int INF = 1001001001;

//mint�p�̒萔 (10��9��)
const int mod = 1000000007;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }

int main() {
    int n;
    cin >> n;
    vector<vector<int> > abc(n, vector<int>(3));
    rep(i, n)rep(j, 3) cin >> abc[i][j];

    vector<vector<int> > dp(n + 1, vector<int> (3));

    rep(i, n) {
        rep(j, 3) {
            rep(k, 3) {
                if (j == k) continue;
                chmax(dp[i + 1][j], dp[i][k] + abc[i][j]);
            }
        }
    }

    int ans = 0;
    rep(i, 3) ans = max(ans, dp[n][i]);
    cout << ans << endl;
    return 0;
}

/*���C�u�����ő����*/
//���[�O���b�h�̌ݏ��@
ll gcd(ll x, ll y) {
    ll num[3];
    num[0] = (x > y) ? x : y;
    num[1] = (x <= y) ? x : y;
    num[2] = num[0] % num[1];

    while (num[2]) {
        num[0] = num[1];
        num[1] = num[2];
        num[2] = num[0] % num[1];
    }

    return num[1];
}

/*�f��������*/
//���색�C�u����
int fac(ll n, vector<int> num) {
    ll kazu = n;
    for (int i = 2; i <= sqrt(n); i++) {
        while (1) {
            if (kazu % i != 0) break;

            kazu /= i;
            num[i]++;
        }
    }
    if (kazu == n && n != 1) return 1;
    int ans = 0;
    if (kazu != 1) ans++;
    for (int i = 2; i <= sqrt(n); i++) {
        for (int j = 1; j <= num[i]; j++) {
            num[i] -= j;
            ans++;
        }
    }
    return ans;
}

Measured memory_bytes: 14128
using System;
using System.ComponentModel;

namespace Constest
{
    class Program
    {
        static void Main(string[] args)
        {
            string cnt = Console.ReadLine();
            string line = Console.ReadLine();
            string[] stArrayData = line.Split(' ');
            long inc = 0;
            long max = 0;
            long sum = 0;
            foreach (string s in stArrayData)
            {
                int i = int.Parse(s);
                if(max < i)
                {
                    max = i;
                }
                sum += (max - i);
                inc++;
                if(int.Parse(cnt) <= inc)
                {
                    break;
                }
            }
            Console.WriteLine(sum);
        }
    }
}


Measured memory_bytes: 45320
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <limits>
#include <list>
#include <string>
#include <queue>
#include <deque>
#include <tuple>
#include <map>
#include <sstream>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(x))
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

ll mpow(ll a, ll b){
	if(b==0) return 1;
	else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
	else return mpow(a,b-1) * a % MOD;
}

signed main(){
  int n; cin>>n;
	int b = -1*INF,s = INF; bool p = true;
	int bx = 0, sx = 0;
	VI a(n);
	rep(i,n){
		cin>>a[i];
		if(b<a[i]){
			b = a[i]; bx = i;
		}
		if(s>a[i]){
			s = a[i]; sx = i;
		}
		if(a[i]<0) p = false;
	}
	if(p){
		cout << n-1 << endl;
		reps(i,n-1){
			cout << i SP i+1 << endl;
			a[i-1] += a[i+1];
		}
	}else{
		cout << 2*n-1 << endl;
		if(abs(b)>=abs(s)){
			reps(i,n){
				cout << bx+1 SP i << endl;
				a[i-1] += a[bx];
			}
			reps(i,n-1){
				cout << i SP i+1 << endl;
				a[i] += a[i-1];
			}
		}else{
			reps(i,n){
				cout << sx+1 SP i<< endl;
				a[i-1] += a[sx];
			}
			REPS(i,n-1){
				cout << i+1 SP i << endl;
				a[i-1] += a[i];
			}
		}
	}
	rep(i,n) cerr << a[i] << ' ';
	cerr << endl;


  return 0;
}


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

using namespace std;

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

    vector<int> AC(N + 1, 0);
    vector<int> WA(N + 1, 0);

    int p;
    string S;
    for (int i = 0; i < M; i++) {
        cin >> p >> S;
        if (S == "AC") {
            AC[p] = 1;
        } else {
            if (AC[p] == 0) {
                WA[p]++;
            }
        }
    }

    int ac = 0, wa = 0;
    for (int i = 1; i <= N; i++) {
        ac += AC[i];
        if (AC[i] == 1) wa += WA[i];
    }

    cout << ac << " " << wa << endl;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
using vll=vector<ll>;
using vvll=vector<vll>;
using vi=vector<int>;
using vvi=vector<vector<int>>;
using vb=vector<bool>;
using pii=pair<int,int>;
using vpii=vector<pair<int,int>>;

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

//pow(llpow,modpow)
template<class T> ll llpow(ll x,T n){ll ans=1;if(x==0)ans=0;while(n){if(n&1)ans*=x;x*=x;n>>=1;}return ans;}
long long modpow(long long a, long long n, long long mod) {long long res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
//最大公約数
template<class T> inline T gcd(T x,T y){if(y==0)return x; else {return gcd(y,x%y);}}
//最小公倍数
template<class T> inline T lcm(T x,T y){return x/gcd(x,y)*y;}
//逆元
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;}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
//試験導入
#define irep(i, end_i, begin_i) for (ll i = (ll)begin_i-1; i >= (ll)end_i; i--)

long long INF = 1LL<<60;
int main( ){
    string S,T;
    cin>>S>>T;
    int N=S.size();
    vvi loc(N*2,vi(26,-1));
    irep(l,0,2*N-1){
        rep(c,0,26){
            if(S[(l+1)%N]-'a'==c){
                loc[l][c]=l+1;
            }
            else{
                loc[l][c]=loc[l+1][c];
            }
        }
    }
    int si=0;
    ll ans=0;
    if(S[0]==T[0])T.erase(0,1);
    rep(i,0,T.size()){
        if(loc[si][T[i]-'a']==-1){
            cout<<-1<<endl;
            return 0;
        }
        ans+=loc[si][T[i]-'a']-si;
        si=(loc[si][T[i]-'a'])%N;
    }
    cout<<ans+1<<endl;
    return 0;
}




Measured memory_bytes: 27008
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <functional>
#include <set>
#include <numeric>
#include <queue>
#define ll long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
#define PI 3.14159265358979323846264338327950

vector<int> to[100005];

int main() {
	ll N, M, ans=0;
	int a, b;

	cin >> N >> M;
	rep(i, M){
		cin >> a >> b;
		--a; --b;
		to[a].push_back(b);
		to[b].push_back(a);
	}
	queue<int> q;
	vector<int> dist(N,-1);
	vector<int> pre(N, -1);
	dist[0] = 0; 
	q.push(0);
	while (!q.empty()){
		int x = q.front();
		q.pop();
		for (int i : to[x]){
			if (dist[i] != -1)continue;
			dist[i] = dist[x] + 1;
			pre[i] = x;
			q.push(i); 
		}
	}

	cout << "Yes" << endl;
	rep(i, N){
		if (i == 0)continue;
		ans = pre[i];
		++ans;
		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 10584
//#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#define INF 2147483647
#define lli long long int
#define MOD 1000000007
#define pb push_back

using namespace std;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;

vvi edge(8,vi(8,0));

bool check(vi u) {
	for (int i = 0; i < u.size(); i++) {
		if (u[i] == 0) return false;
	}
	return true;
}
int dfs(vi used,int node) {
	if (check(used)) return 1;
	
	int res = 0;

	for (int i = 0; i < used.size(); i++) {
		if (edge[node][i] == 0) continue;
		if (used[i] == 1) continue;
		used[i] = 1;
		res += dfs(used,i);
		used[i] = 0;
	}

	return res;

}

int main() {

	int n,m; cin >> n >> m;
	vi used(n,0);

	for (int i = 0; i < m; i++) {
		int a,b; cin >> a >> b;
		edge[a-1][b-1] = 1;
		edge[b-1][a-1] = 1;
	}
	used[0] = 1;	
	cout << dfs(used,0) << endl;
	

	return 0;
}





Measured memory_bytes: 256
use std::io::Read;

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();

    let answer = solve(&buf);

    println!("{}", answer);
}

fn solve(input: &str) -> String {
    let mut iterator = input.split_whitespace();

    let n: usize = iterator.next().unwrap().parse().unwrap();
    let m: usize = iterator.next().unwrap().parse().unwrap();
    let k: usize = iterator.next().unwrap().parse().unwrap();

    let mc: ModComb = ModComb::init(n);

    let mut ans: usize = 0;
    let mut col: usize = m;
    for x in (0..n).rev() {
        let mut now = col;
        now = now * mc.calc(n - 1, x) % MOD;
        if x <= k {
            ans = (ans + now) % MOD;
        }
        col = col * (m - 1) % MOD;
    }

    return ans.to_string();
}

const MOD: usize = 998244353;

struct ModComb {
    fac: Vec<usize>,
    finv: Vec<usize>,
    inv: Vec<usize>,
}

impl ModComb {
    fn init(max: usize) -> ModComb {
        let mut fac = vec![1; max];
        let mut finv = vec![1; max];
        let mut inv = vec![1; max];
        for i in 2..max {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
        return ModComb {
            fac: fac,
            finv: finv,
            inv: inv,
        }
    }

    fn calc(&self, n: usize, k: usize) -> usize {
        if n < k {
            return 0;
        }
        return self.fac[n] * (self.finv[k] * self.finv[n - k] % MOD) % MOD;
    }
}


Measured memory_bytes: 6804
#12
K,N = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
dist = A[0]
Sum = 0
log1 = 0
log2 = 0

for i in range(N):
    if i+1 >= N:
        log1 = (K-A[i]) + A[0]
    else:
        log1 = A[i+1] - A[i]
    
    if log2 < log1 :
        log2 = log1
    
    Sum +=  log1
    
print(Sum - log2)

    


Measured memory_bytes: 32240