akhauriyash/Code-Regression huggingface dataset:
// https://atcoder.jp/contests/abc131/tasks/abc131_b
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

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

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

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

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

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

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

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn main() {
    input! {
        n: i32, l: i32
    };

    let mut best = 10000i32;
    let mut sum = 0;
    for i in 0..n {
        let taste = l+i;
        if taste.abs() < best.abs() {
            best = taste;
        }
        sum += taste;
    }
    sum -= best;

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


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

#define INIT_CAP 100

typedef struct Stack {
    int size;
    int capacity;
    int *data;
} Stack;

static inline void s_init(Stack* s) {
    s->size = 0;
    s->capacity = INIT_CAP;
    s->data = (int *)malloc(sizeof(int) * s->capacity);
}

static inline void s_double(Stack* s) {
    s->capacity *= 2;
    s->data = (int *)realloc(s->data, sizeof(int) * s->capacity);
}

static inline void s_free(Stack* s) {
    free(s->data);
}

static inline void s_push(Stack* s, int data) {
    if (s->size >= s->capacity) {
        s_double(s);
    }
    
    s->data[s->size++] = data;
}

static inline void s_top(Stack* s) {
    if (s->size == 0) {
        return;
    }
    
    printf("%d\n", s->data[s->size-1]);
}

static inline void s_pop(Stack* s) {
    if (s->size == 0) {
        return;
    }
    
    s->size--;
}

int test() {
    int n = 1;
    Stack S[n];
    for (int i=0; i<n; i++) {
        Stack s;
        s_init(&s);
        S[i] = s;
    }
    
    s_pop(&S[0]);
    s_push(&S[0], -200);
    s_push(&S[0], 10000);
    s_top(&S[0]);
    s_pop(&S[0]);
    s_top(&S[0]);
    s_push(&S[0], -600000);
    s_top(&S[0]);
    s_pop(&S[0]);
    s_top(&S[0]);
    return 0;
}

int answer() {
    int n, q, query, t, x;
    char a[20];
    
    fgets(a, 20, stdin);
    sscanf(a, "%d %d", &n, &q);
    
    Stack S[n];
    for (int i=0; i<n; i++) {
        Stack s;
        s_init(&s);
        S[i] = s;
    }
    
    while (q--) {
        fgets(a, 20, stdin);
        sscanf(a, "%d %d %d", &query, &t, &x);
        switch (query) {
            case 0:
                s_push(&S[t], x);
                break;
            case 1:
                s_top(&S[t]);
                break;
            case 2:
                s_pop(&S[t]);
                break;
            default:
                printf("Invalid query = %d\n", query);
                break;
        }
    }
}


int main(void){
    answer();
    return 0;
}



Measured memory_bytes: 3084
n,m=map(int,input().split())
a=[]
ans=0
for i in range(n):
    ii=0
    for j in map(int,input().split()):
        if ii > 0:
        	a.append(j)
        ii+=1
#print(a)
 
for k in range(1,m+1):
    if a.count(k) == n:
        ans+=1
print(ans)

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

#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;

using namespace std;




void one_case() {
   int n, y;
   cin >> n >> y;
   y /= 1000;
   for (int i = 0; i <= n; ++i) {
       for (int j = 0; j <= n - i; ++j) {
           int k = n - i  -j;
           if (10 * i + 5 * j + k == y) {
               cout << i << " " << j << " " << k << "\n";
               return;
           }
       }
   }
   cout << "-1 -1 -1\n";
}


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


    int t = 1;
//    cin >> t;
    while (t--) {
        one_case();
    }

    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int X, Y;
    cin >> X >> Y;
    cout << X + Y / 2 << endl;

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstdlib>
#include <utility>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define vec vector

using namespace std;

int main(void)
{
	while(1) {
		int n;
		cin >> n;
		if (n == 0) break;
		vec<pair<int, int>> time(2 * n);

		rep(i, n) {
			string p, q;
			cin >> p >> q;
			time[2 * i].first = (p[0] - '0') * (int)pow(10, 5) + (p[1] - '0') * (int)pow(10, 4) + (p[3] - '0') * (int)pow(10, 3) + (p[4] - '0') * (int)pow(10, 2) + (p[6] - '0') * (int)pow(10, 1) + p[7] - '0';
			time[2 * i].second = 1;
			time[2 * i + 1].first = (q[0] - '0') * (int)pow(10, 5) + (q[1] - '0') * (int)pow(10, 4) + (q[3] - '0') * (int)pow(10, 3) + (q[4] - '0') * (int)pow(10, 2) + (q[6] - '0') * (int)pow(10, 1) + q[7] - '0';
			time[2 * i + 1].second = - 1;
		}

		sort(time.begin(), time.end());

		int train = 0;
		int ans = 0;
		rep(i, 2 * n) {
			train += time[i].second;
			if (train > ans) ans = train;
		}
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 3220
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;

int main(){

	int n;
	cin >> n;

	string s;
	while(n != 0){

		int r = n%2;
		/*c++では例えば-17%5==-2になってしまう
			本来は(-20+3)%5==3より、r=-2+5=3;
		*/
		if(r < 0) r += 2;
		s = (char)('0' + r) + s;
		n = (n-r) / (-2);
	}
	if(s == "") s = "0";
	cout << s << endl;

  return 0;
}


Measured memory_bytes: 256
// vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
#define min(x,y) (x < y ? x : y)
#define max(x,y) (x < y ? y : x)
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

void solve() {
  ll a, b, c; cin >> a >> b >> c;
  vector<pair<ll,string>> v{
    {a, "A"},
    {b, "B"},
    {c, "C"},
  };
  cout << max_element(all(v))->second << endl;
}



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

typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PLL;
typedef pair<P,P> PP;
typedef vector<ll> VECLL;
typedef vector<P> VECP;
typedef priority_queue<P,VECP,greater<P> > PQP;
typedef priority_queue<ll, VECLL, greater<ll> > PQll;

#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue;
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

template<typename T>
ll sz(vector<T> vec){ return (ll)vec.size(); }
ll sz(string s){ return (ll)s.size(); }

ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }

#include <bits/stdc++.h>
#define rep(i,a,n) for(ll i = a;i < n;i++)
typedef long long ll;
using namespace std;

struct unionfind {
    vector<int> par;
    vector<int> sizes;

    unionfind(int n) : par(n),sizes(n,1){
        rep(i,0,n) par[i] = i;
    }

    int root(int x){
        if(par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x,int y){
        x = root(x);
        y = root(y);

        if(x == y) return;

        if(sizes[x] < sizes[y] ) swap(x,y);

        par[y] = x;
        sizes[x] += sizes[y];
        sizes[y] = 0;

        return;
    }

    bool same(int x,int y){
        return root(x) == root(y);
    }

    int size(int x){
        return sizes[root(x)];
    }
};

int main(){
    ll n,m;
    cin >> n >> m;
    unionfind uf(n);

    rep(i,0,m){
        ll x,y,z;
        cin >> x >> y >> z;
        uf.unite(x-1,y-1);
    }

    map<ll,bool> mapp;

    ll ans = 0;
    rep(i,0,n){
        if(mapp.count(uf.root(i)) == 0){
            ans++;
            mapp[uf.root(i)] = 1;
        }
    }
    cout << ans << endl;
    return 0;
}




Measured memory_bytes: 10360
//Zory-2020
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// typedef __int128 ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FR first
#define SE second
#define MP make_pair
#define PB push_back
#define vc vector
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bin(x) (1ll<<(x))
#define fo(i,l,r) for(int i=(l),I=(r);i<=I;i++)
#define fd(i,r,l) for(int i=(r),I=(l);i>=I;i--)
#define mem(x,val) memset(x,val,sizeof x)
bool DD=1;
#define Debug if(DD) printf("line %d\n",__LINE__)
#define deb(x) if(DD) cerr<<#x<<'='<<x<<endl
#define debug if(DD) printf
namespace mine
{
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num){if(num<0) putchar('-'),num=-num;if(num>=10) write(num/10);putchar('0'+num%10);}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	template<typename T>inline bool chmax(T&a,const T&b){return a<b?a=b,1:0;}
	template<typename T>inline bool chmin(T&a,const T&b){return a>b?a=b,1:0;}
	ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
	bool IN(ll x,ll l,ll r){return l<=x and x<=r;}
 
	const int INF=0x3f3f3f3f;
	const int MOD=1e9+7;
	int mm(const int x){return x>=MOD?x-MOD:x;}
	template<typename T> void add(T &x,const int &y){x=(x+y>=MOD?x+y-MOD:x+y);}
	ll qpower(ll x,ll e,int mod=MOD){ll ans=1;while(e){if(e&1)ans=ans*x%mod;x=x*x%mod;e>>=1;}return ans;}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int N=3e6+10;

	ll dp[N],S[N];
	void main()
	{
		int n=qread();fd(i,N-2,n+1) dp[i]=1,S[i]=S[i+1]+dp[i];dp[n]=n,S[n]=S[n+1]+n;
		fd(i,n-1,1) dp[i]=(dp[i+1]+S[i+3]+MOD-S[i+n+2]+1ll*(n-1)*(n-1))%MOD,S[i]=mm(S[i+1]+dp[i]);
		write(dp[1]);
	}
};//(ans+MOD)%MOD
signed main()
{
	srand(time(0));
	mine::main();
}

Measured memory_bytes: 47104
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  string S;
  int Q;
  int T,F;
  char C;
  int temp=1;
  cin >>S;
  cin >>Q;
  deque <char> X;
  for (int i=0;i<S.length();i++){
    X.push_back(S.at(i));
  }
  
  for (int i=0;i<Q;i++){
    cin>>T;
    if(T==1){
      temp=temp*(-1);
      continue;
    }
    if(T==2){
      
    cin >>F >>C;
    if((F==1)&&(temp==1)){
      X.push_front(C);
    }
    else if((F==1)&&(temp==-1)){
      X.push_back(C);
    }
    else if((F==2)&&(temp==1)){
      X.push_back(C);
    }
    else if((F==2)&&(temp==-1)){
      X.push_front(C);
    }
    }
  }
  
  if (temp==1){
    for(int i=0;i<X.size();i++){
      cout<<X.at(i);
    }
    cout<<endl;
    return 0;
  }
  for(int i=X.size()-1;i>-1;i--){
    cout<<X.at(i);
  }

  return 0;
}



Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define f(a, n) for (int a=0; a<n; a++)
#define F(a, n) for (int a=1; a<=n; a++)
using namespace std;
/*
query 10 digit numbers
But 1xxxxxxxx will always be smaller. (yes)
query 1e10. Determine if it starts with 1.
If not, 
*/

int n;
long long sum, ans, curr;
long long A[200005];
int main() {
	cin.tie(NULL);
	ios::sync_with_stdio(0);
	cin>>n;
	f(i, n) cin>>A[i];
	f(i, n) sum+= A[i];
	ans = 1e15;
	f(i, n-1){
		curr += A[i];
		ans = min (ans, abs(sum - 2*curr));
	}
	cout<<ans<<endl;
	
}




Measured memory_bytes: 1920
import sys
import itertools
input = sys.stdin.readline
N, M, X = (int(i) for i in input().split())
#B = [int(x) for x in stdin.readline().rstrip().split()]
A = [[int(i) for i in input().split()] for i in range(N)]

combs = itertools.product(range(2), repeat=N)
ans = float('inf')
for comb in combs:
    mycomb = list(comb)
    #print(mycomb)
    cur = 0
    allm = [0]*M
    for i in range(N):
        if mycomb[i]:
            cur += A[i][0]
            for j in range(M):
                allm[j] += A[i][j+1]
    #print(allm, cur)
    for j in range(M):
        if allm[j] < X:
            break
    else:
        ans = min(ans, cur)
        #print(ans, cur)

if ans < float('inf'):
    print(ans)
else:
    print(-1)


Measured memory_bytes: 68464
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, k, a[111111];
int dp[10005];

void report(vector<int> seq){
	for(int i=seq.size()-1; i>=0; i--){
		dp[seq[i]] = 1e9;
		for(int j=0; j<n; j++){
			if(j != seq[i]) dp[seq[i]] = min(dp[seq[i]], dp[j] + 1);
		}
	}
	if(*min_element(dp, dp + n) <= k){
		puts("-1");
		exit(0);
	}
	cout << seq.size() << endl;
	for(auto &i : seq) printf("%d ", i + 1);
	exit(0);
}

int main(){
	scanf("%d %d",&n,&k);
	for(int i=0; i<n; i++) cin >> a[i];
	vector<int> seq;
	int banned = -1;
	while(true){
		int val = 1e9, ptr = -1;
		for(int j=0; j<n; j++){
			if(j != banned && val > a[j]){
				val = a[j];
				ptr = j;
			}
		}
		for(int j=0; j<n; j++){
			if(j != ptr && j != banned){
				a[j]--;
				if(a[j] < 0) report(seq);
				seq.push_back(j);
			}
		}
		a[ptr]--;
		if(a[ptr] < 0) report(seq);
		seq.push_back(ptr);
		banned = ptr;
	}
}


Measured memory_bytes: 256
import math
n = int(input())

def make_divisors(n):
    divisors = []
    sqrt =  int(math.sqrt(n))
    for i in range(1, sqrt + 1):
        if n % i == 0:
            divisors.append(int(i))
            if n / i != i:
                divisors.append(int((n / i)))
    return divisors

L = make_divisors(n)
P = []
if math.sqrt(n) % 1 != 0:
    P.append(L[-1])
    P.append(L[-2])
else:
    P.append(L[-1])
    P.append(L[-1])

print(sum(P)-2)

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

const int N = 18;

void solve() {
    int n,k;
    scanf("%d%d",&n,&k);
    printf("%d\n",n-k+1 );
}

int main(){
    //freopen("input.txt","r",stdin);
    solve();
}

Measured memory_bytes: 764
#include <cstdio>
int N, K, l, r, x, c, a[1000009], v[1000009], p[1000009]; char s[19];
int main() {
	scanf("%d %d\n", &N, &K);
	for (int i = 0; i < N; i++) {
		char ch = getchar();
		while (48 <= ch && ch <= 57) a[i] = a[i] * 10 + ch - 48, ch = getchar();
	}
	for (int i = 0; i < K; i++) {
		while (l != r && v[r - 1] >= a[i]) r--;
		v[r] = a[i], p[r++] = i;
	}
	for (int i = K; i <= N; i++) {
		x = v[l]; c = 0;
		while (x > 0) s[c++] = x % 10 + 48, x /= 10;
		while (c--) putchar(s[c]);
		putchar(i == N ? '\n' : ' ');
		if (p[l] == i - K) l++;
		while (l != r && v[r - 1] >= a[i]) r--;
		v[r] = a[i], p[r++] = i;
	}
	return 0;
}


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

int main()
{
   int ans=0,i,n,flag=0;
   cin>>n;
   
   int a[n];
   for(i=0;i<n;i++){
	   cin>>a[i];
   }
   while(1){
	   for(i=0;i<n;i++){
		   if(a[i]%2==1)flag=1;
	   }
	   if(flag==1)break;
	   for(i=0;i<n;i++){
		   a[i]/=2;
	   }
	   ans++;
   }
   cout<<ans<<"\n";
}


Measured memory_bytes: 256
n,k=map(int,input().split())
A=list(map(int,input().split()))

time=0
color=["white" for _ in range(n+1)]
D=[-1 for _ in range(n+1)]
C,s=[],A[0]
point=0
while 1:
    if color[s]=="white":
        color[s]="gray"
        C.append(s)
        D[s]=time
        time +=1
        s=A[s-1]
    else:
        point=D[s]
        break

loop=C[point:]
if k>len(C):
    k -=point
    print(loop[k%len(loop)-1])
else:print(C[k-1])

Measured memory_bytes: 120492
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#define rip(i,n) for(int i=0;i<n;i++)
#define ll long long
#define ld long double
#define set setprecision　//小数点以下何位まで知りたいか
#define mod 1000000007
#define pi 3.14159265
#include<numeric>
using namespace std;
int gcd(int a, int b)//最大公約数
{
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
int lcm(int a, int b)//最小公倍数
{
	return a * b / gcd(a, b);
}
ll pow(ll a, ll b) {
	ll k = b % 30000, h = b / 30000, s = 1;
	rip(i, 30000)s = (s * a) % mod; ll j = 1;
	if (h != 0)rip(i, h)j = j * s % mod;
	if (k != 0)rip(i, k)j = j * a % mod;
	return(j);
}
int main() {
	ll a, b, c;
	cin >> a >> b >> c;
	if ((a == b) && (a == c)&&(a%2==0))cout << "-1" << endl;
	else {
		ll i = 0;
		ll d, e, f;
		while (a % 2 == 0 && b % 2 == 0 && c % 2 == 0) {
			d = a;
			e = b;
			f = c;
			a = (e + f) / 2;
			b = (d + f) / 2;
			c = (d + e) / 2;
			i++;
		}
		cout << i << endl;
	}
}


Measured memory_bytes: 256
WORD = gets.chomp
N = gets.chomp.to_i

def next_char(char, n = 1)
  (n % 26).times do
    case char
    when 'a'..'y'
      char = char.next
    when 'z'
      char = 'a'
    end
  end

  char
end

def solve(word, n)
  tmp_word = word
  tmp_word.each_char.with_index do |char, i|
    # a に変換するために消費する N (z.ord = 122)
    required_n = (122 - tmp_word[i].ord + 1) % 26

    if n >= required_n
      word[i] = 'a'
      n -= required_n
    else
      next
    end
  end

  # それでも余れば最後の文字に足す
  if (n >= 0)
    word[-1] = next_char(word[-1], n)
  end

  return word
end

puts solve(WORD, N)


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

int main() {
    string s, t;
    cin >> s >> t;
    int n = s.size();

    for(int i=0; i<s.size(); i++) {
        s = s[n-1] + s.substr(0, n-1);
        if(s == t) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}


Measured memory_bytes: 3552
n, m, k = map(int, input().split())
mod = 10**9 + 7

fac = [1] * n*m
finv = [1] * n*m
inv = [1] * n*m

ans = 0
def COMinit():
    for i in range(2, n*m):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def COM(n, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod
COMinit()
for i in range(1, n):
    ans += (COM(n*m - 2, k-2)) * ((n-i)*i * m**2)%mod
for j in range(1, m):
    ans += (COM(n*m - 2, k-2)) * ((m-j)*j * n**2)%mod
print(ans%mod)

Measured memory_bytes: 28248
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
typedef pair<int, string> P;
int INF = (1LL << 30) - 1;
int MOD = 1e9+7;
void solve(int N){
    vector<string> V(N);
    rep(i,0,N)cin >> V[i];
    int cnt[26] = {};
    
    rep(i,0,N){
        cnt[V[i][0] - 'A']++;
        int rem = N - i - 1;
        vector<P> vec(26);
        rep(j,0,26)vec[j] = P(cnt[j], string(1, 'A' + j));
        sort(vec.rbegin(), vec.rend());
        if(vec[0].first > vec[1].first + rem){
            cout << vec[0].second << " " << i + 1 <<  endl;
            return;
        }
    }
    cout << "TIE" << endl;

}
main(){
    int N;
    cin >> N;
    while(N){
        solve(N);
        cin >> N;
    }
}


Measured memory_bytes: 3136
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
table=[]
for i in range(n):
  table.append([int(i) for i in input().split()])
Chk=True
for k in range(n):
  for i in range(n):
    for j in range(n):
      if table[i][k]+table[k][j]<table[i][j]:
        Chk=False
        table[i][j]=table[i][k]+table[k][j]

ans=0
for i in range(n):
  for j in range(i+1,n):
    C=True
    for k in range(n):
      if k==i or k==j:
        continue
      else:
        if table[i][k]+table[k][j]==table[i][j]:
          C=False
    if C:
      ans+=table[i][j]
print(ans if Chk else -1)

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

int N,K;
const int n=200010;
int cnt[n];

int main()
{
    cin>>N>>K;
    for (int i = 0; i < n; i++)
    {
        cnt[i] = 0;
    }
    
    for (int i = 0; i < N; i++)
    {
        int a;
        cin>>a;
        cnt[a]++;
    }

    sort(cnt,cnt+n);
    
    int ans=0;
    for (int i = 0; i < n-K; i++) 
    {
        ans+=cnt[i];
    }
    
    cout << ans << endl;
        
    return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007


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

  vector<long long> a(N),sum(N);
  for(int i = 0;i < N;i++) {
    cin >> a.at(i);
    if(i == 0) sum.at(i) = a.at(i);
  }

  for(int i = 1;i < N;i++) {
    sum.at(i) = a.at(i) + sum.at(i-1);
  }

  long long cnt1 = 0,cnt2 = 0,c = 0;
  // odd > 0
  for(int i = 0;i < N;i++) {
    if(i % 2 == 0) {
      if(sum.at(i) + c <= 0) {
        cnt1 += abs(sum.at(i) + c - 1);
        c += abs(sum.at(i) + c - 1);
      }
    }else{
      if(sum.at(i) + c >= 0) {
        cnt1 += abs(sum.at(i) + c + 1);
        c -= abs(sum.at(i) + c + 1);
      }
    }
  }

  c = 0;
  // even > 0
  for(int i = 0;i < N;i++) {
    if(i % 2 != 0) {
      if(sum.at(i) + c <= 0) {
        cnt2 += abs(sum.at(i) + c - 1);
        c += abs(sum.at(i) + c - 1);
      }
    }else{
      if(sum.at(i) + c >= 0) {
        cnt2 += abs(sum.at(i) + c + 1);
        c -= abs(sum.at(i) + c + 1);
      }
    }
  }
  

  cout << min(cnt1,cnt2) << endl;



}

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

int main(){
	int x;
	cin >> x;
	rep(i,x+1){
		int rest = x - i*100;
		if(0 <= rest && rest <= 5*i){
			puts("1");
			return 0;
		}
		if(rest < 0) break;
	}
	puts("0");
	return 0;
}

Measured memory_bytes: 3680
#include<iostream>
using namespace std;
int main() {
	int n;
	cin >> n;
	int count = 0;
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) {
			if (n == i * 4 + 7 * j) {
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(void){
  int N ,M ;
  cin>>N>>M;
  int i;
  int l,r;
  int s,d;
  s=0;
  d=100000;
  for(i=0;i<M;i++){
      cin>>l>>r;
      if(l>s){
          s=l;
      }
      if(r<d){
          d=r;
      }


  }

  if(s>d){
      cout<<0<<endl;
  }else{
      cout<<d-s+1<<endl;
  }

 return 0 ;
}
  

Measured memory_bytes: 256
#include<stdio.h>

int main(){
    char c[10];
    int all=700;
    scanf("%s",c);
	for (int i = 0; i < 3; i++){
        if (c[i] == 'o'){
			all += 100;
        }
	}
	printf("%d", all);
 
    return 0;
}
 


Measured memory_bytes: 128
#include <stdio.h>

int main(void)
{
	int x,y,z,temp;
	
	scanf("%d %d %d", &x, &y, &z);
	
	if(x>z) temp=z,z=x,x=temp; //xとzを入れ替える//
	if(x>y) temp=x,x=y,y=temp; //xとyを入れ替える//
	if(y>z) temp=y,y=z,z=temp; //yとzを入れ替える//
	
	printf("%d %d %d\n",x,y,z);
	
	return 0;
}

Measured memory_bytes: 596
#include<bits/stdc++.h>
#define mx 1000000007
#define ll long long int

using namespace std;

int n,k;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>> n >> k ;
    int arr[n+1];
    for(int i=1;i<=n;++i)
        cin>> arr[i] ;
    int dp[n+1]={0};
    dp[n-1]=abs(arr[n-1]-arr[n]);
    for(int i=n-2;i>0;--i){
    	int mini=mx;
    	for(int j=i+1;(j<=n && j<=i+k) ;++j){
    		if(dp[j]+abs(arr[i]-arr[j])<mini)
    			mini=dp[j]+abs(arr[i]-arr[j]);
    	}
    	dp[i]=mini;
    }
    cout<< dp[1] ;
	return 0;
}


Measured memory_bytes: 4392
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int size=1e5;
const long inf=1e9;
int main(){
	string s;cin>>s;
	deque<char> a,b;
	bool flag=true;
	int Q;cin>>Q;
	int t,f; char c;
	rep(i,0,Q){
		cin>>t;
		if(t==1) flag=!flag;
		else{
			cin>>f>>c;
			if(f==1){
				if(flag) a.push_front(c);
				else b.push_back(c);
			}else{
				if(flag) b.push_back(c);
				else a.push_front(c);
			}
		}
	}
	if(flag){
        for(char k:a) cout<<k;
        cout<<s;
		for(char k:b) cout<<k;
        cout<<endl;
	}else{
        reverse(a.begin(),a.end());
        reverse(s.begin(),s.end());
        reverse(b.begin(),b.end());
		for(char k:b) cout<<k;
        cout<<s;
        for(char k:a) cout<<k;
        cout<<endl;
	}
}

Measured memory_bytes: 896
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner std = new Scanner(System.in);
        int n = std.nextInt();
        int m = std.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = std.nextInt();
            b[i] = std.nextInt();
        }

        List<List<Integer>> jobs = new ArrayList<>(m);
        for (int i = 0; i < m; i++) {
            jobs.add(new ArrayList<>());
        }

        for (int i = 0; i < n; i++) {
            int v = a[i];
            if (v > m) {
                continue;
            }
            jobs.get(m - v).add(b[i]);
        }

        long ans = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        for (int i = m - 1; i >= 0; i--) {
            pq.addAll(jobs.get(i));

            if (!pq.isEmpty()) {
                ans += pq.poll();
            }
        }

        System.out.println(ans);
    }
}


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

 using namespace std;

 int main() {
   int s;

   cin >> s;

   vector<int> a(1000001);

   a.at(1) = s;
   bool finished = false;
   int m;
   for (int i = 1;; i++) {
     if (a.at(i) % 2 == 0) a.at(i + 1) = a.at(i) / 2;
     else a.at(i + 1) = 3 * a.at(i) + 1;
     for (int j = 1; j <= i; j++) {
       if (a.at(j) == a.at(i + 1)) {
         m = i + 1;
         finished = true;
         break;
       }
     }
     if (finished) break;
   }

   cout << m << endl;
}


Measured memory_bytes: 4096
#include<iostream> 
using namespace std;
 int main() {
 int a=0,b=0,n;
 string A,B;
 cin>>n;
 for(int i=0;i<n;i++){
 cin>>A>>B;
 if(A==B){
 a++; b++; } else if(A>B)a+=3; else b+=3; } cout<<a<<" "<<b<<endl; return 0; } 


Measured memory_bytes: 3144
if __name__ =="__main__":
    while True:
        n = int(input())
        if n ==0:break
        js = []
        robots=[10,10]
        count = 0
        dict = {'N':[0,1],'S':[0,-1],'W':[-1,0],'E':[1,0]}
        for i in range(n):
            js +=[list(map(int,input().strip().split()))]
        m = int(input())
        for i in range(m):
            line = input().strip().split()
            for j in range(int(line[1])):
                robots = [robots[0] + dict[line[0]][0],robots[1] + dict[line[0]][1]]
                for k in js:
                    if k == robots:
                        count +=1
                        js.remove(k)
        if n == count:print('Yes')
        else:print('No')

Measured memory_bytes: 7828
using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ
{
    class Program
    {
        static Dictionary<char, long> num = new Dictionary<char, long>
        {
            {'I', 1},  {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}
        };
        static void Main(string[] args)
        {
            string line;
            while((line = Console.ReadLine()) != null)
            {
                long total = 0;
                for(int i = 0; i < line.Length; i++)
                {
                    if (i < line.Length - 1 && num[line[i]] < num[line[i + 1]])
                    {
                        total -= num[line[i]];
                    }
                    else
                    {
                        total += num[line[i]];
                    }
                }
                Console.WriteLine(total);
            }
        }
    }
}

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

#define sws ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define ff first
#define ss second
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define fo(i, n) for( int i = 0; i < n; i++ )

const int INF = 1e9;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;


int main() {

    int n, k;
    cin >> n >> k;

    long double ans = 0;

    for ( int i = 1; i <= n; i++ ) {
        long double aux = 1.0 / n;
        int at = i;

        while ( at < k ) {
            at *= 2;
            aux *= 0.5;
        }
        ans += aux;
    }


    cout << fixed << setprecision(20) << ans << endl;

    return 0;
}

Measured memory_bytes: 3712
N = int(input())
a = [int(x) for x in input().split()]
cnt4 = 0
cnt2 = 0
for x in a:
    if x % 4 == 0:
        cnt4 += 1
        continue
    if x % 2 == 0:
        cnt2 += 1
cnt2 = cnt2//2*2
if N-cnt2 > 2*cnt4+1:
    print('No')
else:
    print('Yes')

Measured memory_bytes: 20060
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif
 
typedef long long ll;
typedef unsigned long long ull;
 
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int(s.size()))
#define fname "a"
#define MAXN 400004
#define INF 2000000000000000000LL

ll n, a, b, c, d;

inline ll add(ll a, ll b) {
	return min(a + b, INF);
}

inline ll mult(ll a, ll b) {
	if (!b) return 0;
	if (a > INF / b) return INF;
	return a * b;
}

unordered_map <ll, ll> dp;

inline ll go(ll n) {
	if (n == 0) return 0;
	if (n == 1) return d;
	if (dp.count(n)) return dp[n];
	ll res = mult(n, d);
	{
		int pr = n % 2, ne = 2 - pr;
		res = min(res, go((n - pr) / 2) + a + d * pr);
		if ((n + ne) / 2 < n)
			res = min(res, go((n + ne) / 2) + a + d * ne);
	}
	{
		int pr = n % 3, ne = 3 - pr;
		res = min(res, go((n - pr) / 3) + b + d * pr);
		if ((n + ne) / 3 < n)
			res = min(res, go((n + ne) / 3) + b + d * ne);
	}
	{
		int pr = n % 5, ne = 5 - pr;
		res = min(res, go((n - pr) / 5) + c + d * pr);
		if ((n + ne) / 5 < n)
			res = min(res, go((n + ne) / 5) + c + d * ne);
	}
//	cout << n << ' ' << res << endl;
	return dp[n] = res;
}

vector <ll> w;

inline ll get(ll n, ll base, ll cost) {
	if (base == 1) return mult(n, d);
	w.clear();
	while(n) {
		w.pb(n % base);
		n /= base;
	}
	ll res = 0;
	for (int i = sz(w) - 1; i >= 0; --i) {
		ll t1 = mult(w[i], d);
		ll t2 = add(mult(base - w[i], d), d);
		if (i == sz(w) - 1) t2 = add(t2, cost);
		res = add(res, min(t1, t2));
		if (i) res = add(res, cost);
	}
	return res;
}

inline void solve() {
	scanf(I64 "" I64 "" I64 "" I64 "" I64, &n, &a, &b, &c, &d);
	dp.clear();
	ll ans = go(n);
	for (ull cur = 1, i = 0; cur <= INF; cur *= 2, ++i)
		for (ull cur2 = cur, j = 0; cur2 <= INF; cur2 *= 3, ++j)
			for (ull cur3 = cur2, k = 0; cur3 <= INF; cur3 *= 5, ++k) {
				ans = min(ans, get(n, cur3, a * i + b * j + c * k));
//				cout << cur3 << ' ' << ans << endl;
			}
	printf(I64"\n", ans);
}
 
int main() {
	#ifdef LOCAL
	freopen(fname".in", "r", stdin);
	freopen(fname".out", "w", stdout);
	#endif

	int tt;
	scanf("%d", &tt);
	for (int t = 0; t < tt; ++t) {
		solve();
	}

	return 0;
}


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

int N, T;
int c[100], t[100];

int main() {
    cin >> N >> T;
    rep(i, N) {
        cin >> c[i] >> t[i];
    }

    int init_cost = 10000;
    int min_cost = init_cost;
    rep(i, N) {
        if (t[i] <= T && min_cost > c[i]) {
            min_cost = c[i];
        }
    }

    if (min_cost != init_cost) {
        cout << min_cost << endl;
    } else {
        cout << "TLE" << endl;
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P  = pair<int, int>;
int main() {
    long double a, b, h, m;
    cin >> a >> b >> h >> m;
    long double ad = (h + m / 60.0) / 12.0 * 360, bd = m / 60.0 * 360,
                th = abs(ad - bd), thr;
    thr = th / 180.0 * M_PI;
    cout << setprecision(20) << sqrt(a * a + b * b - 2.0 * a * b * cosl(thr));
    return 0;
}

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

void maxHeapify(int A[],int i,int H) {
  int l = 2 * i;
  int r = 2 * i + 1;
  int largest;

  if(l <= H && A[l] > A[i]) {
    largest = l;
  }else {
    largest = i;
  }
  
  if(r <= H && A[r] > A[largest]) {
    largest = r;
  }

  if(largest != i) {
    swap(A[i],A[largest]);
    maxHeapify(A,largest,H);
  }
}

void buildmaxHeap(int A[],int H) {
  for(int i = H / 2; i >= 1;i--) {
    maxHeapify(A,i,H);
  }
}

int main() {

 int N;
 cin >> N;

 int H[500001];
 for(int i = 1;i <= N; i++) {
   cin >> H[i];
 }

 buildmaxHeap(H,N);

  for(int i = 1;i <= N;i++) {
    cout << " " << H[i];
  }

  cout << endl;
}



Measured memory_bytes: 5020
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<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;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

int main(){
    int n;
    cin >> n;
    string s,t;
    cin >> s >> t;
    for(int i = n; i<2*n; i++){
        int c = 2*n -i;
        if(s.substr(n-c,c)==t.substr(0,c)){
            cout << i << endl;
            return 0;
        }
    }
    cout << 2*n << endl;
}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def solve():
    n = II()
    e = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = MI1()
        e[a].append((b, i))
        e[b].append((a, i))
    # print(e)

    ans = [0] * (n-1)
    def dfs(v, v_pre=-1, c_pre=-1):
        c = 1
        for (nv, id) in e[v]:
            if nv == v_pre: continue
            if c_pre == c: c += 1
            # v to nv color = c
            ans[id] = c
            dfs(nv, v, c)
            c += 1

    dfs(0)
    print(max(ans))
    printlist(ans)


if __name__ == '__main__':
    solve()


Measured memory_bytes: 86604
// template version 1.11
using namespace std;
#include <iostream>
#include <bits/stdc++.h>

// varibable settings {{{
#define infile "../test/sample-1.in"
#define int long long
const int INF=1e18;
const int MOD=1e9+7;
// }}}

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;

template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;
#if defined(PCM) || defined(LOCAL)
  #include "lib/dump.hpp"
#endif
//}}}

int solve(){
  int n,m;cin>>n>>m;
  vi c(n+2, 0);
  rep(_, m){
    int l,r;cin>>l>>r;
    c[l]++;c[r+1]--;
  }
  // dump(c);
  int res=0;
  int v = 0;
  rep(i, n+2){
    v += c[i];
    if (v==m) res++;
  }
  cout << res << endl;
  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


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

int main() {
  string s;
  int cnt = 0;
  int renzoku = 0;
  
  cin >> s;
  
  for(int i = 0 ; i < 3 ; ++i){
    if(s[i] == 'R'){
      renzoku++;
      cnt = renzoku;
    }
    else if(s[i] != 'R' && renzoku != 0){
      cnt = renzoku;
      renzoku = 0;
    }
  }
  cout << cnt << endl;

  
  return 0;
  
}


Measured memory_bytes: 3616
n = int(input())
a = list(map(int, input().split()))

ma = -1
for i in range(n - 1):
    for j in range(i + 1, n):
        pro = a[i] * a[j]
        
        pre = pro % 10
        pro //= 10

        while pro:
            if pre - pro % 10 == 1:
                pre = pro % 10
                pro //= 10
            else:
                break

        if not pro:
            ma = max(ma, a[i] * a[j])
print(ma)


Measured memory_bytes: 5616
N = int(input())
S = [list(input()) for i in range(N)]

def is_ok(mat):
    n = len(mat)
    for i in range(n):
        for j in range(i, n):
            if mat[i][j] != mat[j][i]:
                return False
    return True

count = 0
A = 1
for B in range(N):
    next_S = [S[(i+B) % N] for i in range(N)]
    if is_ok(next_S):
        count += 1

print(count * N)


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

int main() {
  int H,W;
  cin >> H >> W;
  for (int i; i < W + 2; i++){
  	cout << "#";
  }
  	cout << endl;
  for(int j; j < H; j++){
  	string A;
    cin >> A;
    cout << "#" << A << "#" << endl;
  }
  for (int k; k < W + 2; k++){
  	cout << "#";
  }
  	cout << endl;
}


Measured memory_bytes: 256
import java.io.*
import java.util.*

var pw = PrintWriter(System.out)
const val YES = "Yes"
const val NO = "No"
fun solve(K: Long, X: Long){
    if(K*500L >= X) println(YES)
    else println(NO)
    pw.flush()
    return
}


fun main(args: Array<String>) {
    fun StringArray(size: Int, init: (Int) -> String = { "\u0000" }): Array<String> {
        return Array<String>(size, init)
    }
    class Scanner(stream: InputStream) {
        private val reader = BufferedInputStream(stream).bufferedReader()
        private var st: StringTokenizer? = null
        fun next(): String {
            while (!(st?.hasMoreTokens() ?: false)) st = StringTokenizer(reader.readLine())
            return st!!.nextToken()
        }
    }
    val sc = Scanner(System.`in`)
    val K = sc.next().toLong()
    val X = sc.next().toLong()
    solve(K, X)
}



Measured memory_bytes: 29476
#include <bits/stdc++.h>
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define dbV(arr) cout << #arr << "=[ "; \
for(auto i:arr) cout << i << " "; cout << "]\n";
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define repF(i,a,n) for(int i = (a); i <= (n); ++i)
#define repB(i,a,n) for(int i = (a); i >= (n); --i)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define umii unordered_map<int,int>
#define umll unordered_map<ll,ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define seti set<int>
#define setl set<ll>
#define pb push_back
#define F first
#define S second
#define MOD 1000000007
#define all(z) z.begin(),z.end()
using namespace std;
using ll = long long;
ll dp[102][100002];
ll solve(vector<pair<int, ll>> &item, int n, int w) {
	if (n < 0) return 0LL;
	if (dp[n][w] != -1) return dp[n][w];
	if (item[n].F <= w)
		return dp[n][w] = max(item[n].S + solve(item, n - 1, w - item[n].F), solve(item, n - 1, w));
	else
		return dp[n][w] = solve(item, n - 1, w);
};
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, w; cin >> n >> w;
	vector<pair<int, ll>> item(n);
	rep(i, n) cin >> item[i].F >> item[i].S;
	memset(dp, -1, sizeof(dp));
	cout << solve(item, n - 1, w) << endl;
	return 0;
}

Measured memory_bytes: 83344
import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int a[]=new int[n];
    for (int i=1;i<n;i++) {
      a[sc.nextInt()-1]++;
    }

    for (int i=0;i<a.length;i++) {
      System.out.println(a[i]);
    }
  }
}


Measured memory_bytes: 58316
#include<array>
#include<map>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

int H, W;
char F[211][211];

int buf[4][186][186][186];
int (*A)[186][186] = buf[0];
int (*Ax)[186][186] = buf[1];
int (*B)[186][186] = buf[2];
int (*Bx)[186][186] = buf[3];


void MAIN() {
    scanf("%d%d", &H, &W);
    REP (i, H) scanf("%s", F[i]);

    REP (h, H+1) REP (w, W+1) {
	REP (h1, H+1) A[h][w][h1] = w;
	REP (w1, W+1) B[h][w][w1] = h;
    }

    for (int w=W; w--;) {
	REP (h, H) {
	    if (F[h][w] == F[h][w+1]) A[h][w][h+1] = A[h][w+1][h+1];
	    else A[h][w][h+1] = w+1;
	}
	for (int h=H; h--; ) for (int h1=h+1; h1<H; h1++) {
	    if (F[h][w] != F[h1][w]) A[h][w][h1+1] = w;
	    else A[h][w][h1+1] = min(A[h][w][h1], A[h1][w][h1+1]);
	}
    }
    for (int h=H; h--;) {
	REP (w, W) {
	    if (F[h][w] == F[h+1][w]) B[h][w][w+1] = B[h+1][w][w+1];
	    else B[h][w][w+1] = h+1;
	}
	for (int w=W; w--; ) for (int w1=w+1; w1<W; w1++) {
	    if (F[h][w] != F[h][w1]) B[h][w][w1+1] = h;
	    else B[h][w][w1+1] = min(B[h][w][w1], B[h][w1][w1+1]);
	}
    }

    REP (t, 10000) {
	REP (h, H) REP (w, W) {
	    for (int h1=h+1; h1<=H; h1++) {
		amax(B[h][w][A[h][w][h1]], h1);
	    }
	    for (int w1=w+1; w1<=W; w1++) {
		amax(A[h][w][B[h][w][w1]], w1);
	    }
	}

	REP (h, H) REP (w, W) {
	    for (int h1=H-1; h1>h; h1--) {
		amax(A[h][w][h1], A[h][w][h1+1]);
	    }
	    for (int w1=W-1; w1>w; w1--) {
		amax(B[h][w][w1], B[h][w][w1+1]);
	    }
	}

	if (A[0][0][H] == W || B[0][0][W] == H) {
	    printf("%d\n", t);
	    break;
	}

	REP (h, H+1) REP (w, W+1) {
	    REP (h1, H+1) Ax[h][w][h1] = w;
	    REP (w1, W+1) Bx[h][w][w1] = h;
	}

	REP (h, H) REP (w, W) {
	    for (int h1=h+1; h1<=H; h1++) {
		int w1 = A[h][w][h1];
		Ax[h][w][h1] = A[h][w1][h1];
	    }
	    for (int w1=w+1; w1<=W; w1++) {
		int h1 = B[h][w][w1];
		Bx[h][w][w1] = B[h1][w][w1];
	    }
	}

	swap(A, Ax);
	swap(B, Bx);
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 100864
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
sa =set(a)
ca = Counter(a)
ans = 0

for x in sa:
    # カウンタオブジェクトは存在しない要素に０を返す
    tmp = ca[x-1] + ca[x] + ca[x+1]
    if ans < tmp:
        ans = tmp

print(ans)

Measured memory_bytes: 16612
import sys
import socket
from collections import Counter

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('b1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    s = read_str()
    res = 'WA'
    if s[0] == 'A':
        c = 0
        for letter in s[2:-1]:
            if letter == 'C':
                c += 1
        if c == 1:
            upper = 0
            for letter in s:
                if letter.lower() != letter:
                    upper += 1
            if upper == 2:
                res = 'AC'
    print(res)


main()


Measured memory_bytes: 3568