akhauriyash/Code-Regression huggingface dataset:
# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++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 ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
using ll = long long;
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 (b<a) { a=b; return 1; } return 0; }
const long long INF = 1LL << 60;

ll gcd(ll a, ll b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

uintmax_t combination(unsigned int n, unsigned int r) {
  if ( r * 2 > n ) r = n - r;
  uintmax_t dividend = 1;
  uintmax_t divisor  = 1;
  for ( unsigned int i = 1; i <= r; ++i ) {
    dividend *= (n-i+1);
    divisor  *= i;
  }
  return dividend / divisor;
}

int modPow(long long a, long long n, long long p) {
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

ll lcm(ll a, ll b)
{
   return a / gcd(a, b) * b;
}

int main()
{
  ll n,m;
  cin >> n >> m;
  ll nk = 1,mk = 1;

  reps(i,n){
      nk*=i;
      nk%=1000000007;
  }

  reps(i,m){
      mk*=i;
      mk%=1000000007;
  }

  ll ans1 = nk * mk;
  ll ans2 = 2 * nk * mk;

  ans1%=1000000007;
  ans2%=1000000007;

  if(n == m) cout << ans2 << endl;
  else if(n == m - 1 || n == m + 1) cout <<  ans1 << endl;
  else cout << 0 << endl;

  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
	int w,sum;
	while(1){
		scanf("%d",&w);
		if(w == -1)break;
		sum = 1150;
		if(w > 10)	sum += 125*(min(10,w-10));
		if(w > 20)sum += 140*(min(10,w-20));
		if(w > 30) sum += 160*(w-30);
		printf("%d\n",4280-sum);
	}
}


Measured memory_bytes: 2580
import sys
sys.setrecursionlimit(100000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15
dy = (-1,0,1,0)
dx = (0,1,0,-1)

def solve():
    W,H = map(int,input().split())
    M = int(input())
    impossible = {tuple(map(int,input().split())) for _ in range(M)}
    dp = [[0] * (W + 1) for _ in range(H + 1)]
    dp[0][0] = 1
    for i in range(H + 1):
        for j in range(W + 1):
            if i + 1 <= H:
                if (j,i,j,i + 1) not in impossible and (j,i + 1,j,i) not in impossible:
                    dp[i + 1][j] += dp[i][j]
            if j + 1 <= W:
                if (j,i,j + 1,i) not in impossible and (j + 1,i,j,i) not in impossible:
                    dp[i][j + 1] += dp[i][j]
    print(dp[-1][-1] if dp[-1][-1] > 0 else "Miserable Hokusai!")

def main():
    N = int(input())
    for _ in range(N):
        solve()
if __name__ == '__main__':
    main()



Measured memory_bytes: 5632
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <iomanip>	
#include <deque>
#include <set>
#define rep(i,a,b) for(int i=a;i<b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
#define fore(i,a) for(auto &i:a)
#define INF INT_MAX/2;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
//ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;


int cnt_digit(ll N)
{
	int digit = 0;

	while (N > 0)
	{
		N /= 10;
		digit++;
	}
	return digit;
}


 // 最大公約数計算
 
ll gcd(ll a, ll b)
{
	if (b == 0) {
		return a;
	}
	else {
		return gcd(b, a % b);
	}
}
// 最小公倍数の計算

ll lcm(ll a, ll b)
{
	ll g = gcd(a, b);

	return a / g * b;
}

struct union_find
{
	vector<int> par, r;
	union_find(int n)
	{
		par.resize(n);
		r.resize(n);
		init(n);
	}


	void init(int n)
	{
		for (int i = 0; i < n; i++) par[i] = i;
		for (int i = 0; i < n; i++) r[i] = 0;
	}

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

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

		if (r[x] < r[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (r[x] == r[y])
			{
				r[x]++;
			}
		}
	}
};



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

	string s;
	cin >> n >> s;
	bool x[26];
	bool y[26];
	int ans = 0;
	rep(i, 0, n-1) {
		rep(j, 0, 26) {
			x[j] = false;
			y[j] = false;
		}

		rep(j, 0, i+1) {
			x[s[j] - 'a'] = true;
		}
		rep(j, i+1, n) {
			y[s[j] - 'a'] = true;
		}
		int tmp = 0;
		rep(j, 0, 26) {
			if (x[j] == true && y[j] == true){
				tmp += 1;

			}
		}
		ans = max(ans, tmp);
	}
	
	cout << ans << endl;
	
	
	
		
	
	
	
	
}
	

Measured memory_bytes: 256
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n, 0);
  for(auto& e: v) cin >> e;
  vector<int> dp(n, 1e9 + 5);
  dp[0] = 0;
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i - j < 0)
        break;
      dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
    }
  }
  cout << dp.back() << '\n';
  return 0;
}


Measured memory_bytes: 1024
input = raw_input()

for i in xrange(len(input)):
  # print "i = " + str(i) + ", input[i] = " + input[i]
  if input[i].islower():
    input = input[:i] + input[i].upper() + input[i + 1:]
  elif input[i].isupper():
    input = input[:i] + input[i].lower() + input[i + 1:]

print input

Measured memory_bytes: 6440
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

int main() {
	double a, b, h, m; cin >> a >> b >> h >> m;
	//角度をラジアンで求める必要がある。
	//(h/12)*2pi,(m/60)*2pi

	long double ang1 = ((long double)(h*60.0+m) / (long double)720) * (long double)2 * (long double)PI;
	long double ang2 = ((long double)m / (long double)60) * (long double)2 * (long double)PI;

	long double ang = ang1 - ang2;

	long double base = a * a + b * b - (long double)2 * a*b*cos(ang);

	long double ans = sqrt(base);

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

	return 0;

}

Measured memory_bytes: 4168
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
string s;
int c=0,f=0;
int main()
{
	cin>>s;
	int l=s.size();
	for(int i=0;i<l;i++)
	{
		if((c==0 && f==0) && s[i]=='C')		c=1;
		if(c==1 && s[i]=='F')	f=1;
	}
	if(c==1 && f==1)	cout<<"Yes"<<endl;
	else	cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
     
int main() {
  long long  N, A, B;
  cin >> N >> A >> B;
  
  if(N % (A + B) <= A){
    cout << N / (A + B) * A + N % (A + B) << endl;
  }
  
  else if(N % (A + B) > A){
    cout << N / (A + B) * A + A << endl;
  }
    
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://arc060.contest.atcoder.jp/tasks/arc060_c>
 問題文============================================================
 N 軒のホテルが一直線上に並んでいます。i (1≤i≤N) 番目のホテルは、座標 xi に位置しています。
 
 旅行者である高橋君には、次の 2 つの信念があります。
 
 高橋君の 1 日の移動距離は L を超えない。
 高橋君は野宿をしない。すなわち、1 日の終わりには必ずいずれかのホテルにいなければならない。
 
 Q 個のクエリが与えられます。j (1≤j≤Q) 番目のクエリとして、異なる 2 つの整数 aj, bj が与えられます。
 各クエリについて、前述の信念をともに守った上で、
 高橋君が aj 番目のホテルから bj 番目のホテルに移動するために必要な最小日数を求めてください。
 なお、高橋君が aj 番目のホテルから bj 番目のホテルに移動できることは保証されます。
 =================================================================
 解説=============================================================
 ================================================================
 */

ll check(ll i,ll d,vector<vector<ll>>& next){
    if(d == 0) return i;
    ll k = 0;
    while(d >= (1<<k)) k++;
    return check(next[k-1][i],d - (1<<(k-1)),next);
}
void solve(){
    ll N; cin >> N;
    vector<ll> x(N); for(auto& in:x) cin >> in;
    ll L; cin >> L;
    ll Q; cin >> Q;
    vector<ll> a(Q),b(Q);
    for(int i = 0; i < Q;i++){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        if(a[i] > b[i]) swap(a[i],b[i]);
    }
    
    ll LOG_N = 0;
    while(N >= (1<<LOG_N)) LOG_N++;
    vector<vector<ll>> next(LOG_N,vector<ll>(N));
    for(int i = 0; i < N;i++){
        next[0][i] = upper_bound(x.begin(), x.end(), x[i]+L) - x.begin() - 1;
    }
    for(int k = 0; k < LOG_N-1; k++){
        for(int i = 0; i < N;i++){
            next[k+1][i] = next[k][next[k][i]];
        }
    }
    for(int i = 0; i < Q;i++){
        ll _a = a[i], _b = b[i];
        ll l = 0, r = N;
        while(r-l>1){
            ll m = (l+r)/2;
            
//            // a番目の要素のm個次の要素を求める
//            ll p = _a;
//            for(ll k = LOG_N-1; k>=0; k--){
//                if((m>>k)&1){
//                    p = next[k][p];
//                }
//            }
//            if(p >= _b){
//                r = m;
//            }else{
//                l = m;
//            }
            if(check(_a,m,next) >= _b) r = m;
            else l = m;
        }
        cout << r << endl;
    }
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve();
    return 0;
}


Measured memory_bytes: 18160
#include <stdio.h>

#define MAX 1210

int main()
{
    char ch, nextch;
    while (scanf("%c", &ch) != EOF) {
        nextch = ch;        
        if ('a' <= ch && ch <= 'z') {
            nextch = ch - ('a' - 'A');
        } else if ('A' <= ch && ch <= 'Z') {
            nextch = ch + ('a' - 'A');
        }
        printf("%c", nextch);
    }
    return 0;
}

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

int main() {
    long long N;
    cin >> N;
    vector<long long> A(N), B(N), C(N);
    for(int i = 0; i < N; i++){
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    for(int i = 0; i < N; i++){
        cin >> B[i];
    }
    sort(B.begin(), B.end());
    for(int i = 0; i < N; i++){
        cin >> C[i];
    }
    sort(C.begin(), C.end());

    long long ans = 0;
    //中段を決めると全て決まる
    for(int i = 0; i < N; i++){
        long long a = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
        long long c = N - (upper_bound(C.begin(), C.end(), B[i]) - C.begin());
        ans += a * c;
    }
    cout << ans << endl;
}


Measured memory_bytes: 2688
import bisect

def lis(n, a):
    l = []
    l.append(a[0])
    length = 1
    for i in range(1, n):
        if l[length - 1] < a[i]:
            l.append(a[i])
            length += 1
        else:
            l[bisect.bisect_left(l, a[i], lo=0, hi=length)] = a[i]

    return length

if __name__ == '__main__':
    n = int(input())
    a = [int(input()) for i in range(n)]

    print(lis(n, a))


Measured memory_bytes: 10348
#include<stdio.h>
int f(int x,int y,int z);
int main()
{
int i,a,b[30],t,m=0;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b[i]);
}
int h=0,w=2;
while(w!=a)
{int y=f(b[h],b[h+1],b[w]);
m=m+y;

h++;w++;
}
printf("%d",m);
}
int f(int x,int y,int z)
{
	int t,m=y;
	if(x<y)
	{
	   t=x;
	   x=y;y=t;	
	}
	if(x<z)
	{
	   t=x;
	   x=z;z=t;	
	}
	if(y<z)
	{
	   t=y;
	   y=z;z=t;	
	}
	if(m==y)
	return 1;
	else
	return 0;
}

Measured memory_bytes: 128
#include<stdio.h>
#include<string.h>
#define min(x,y) ((x<y)?x:y)
#define Max(x,y) ((x>y)?x:y)
#define INF 10000000
int v[3005],sv[3005],u[3005];
int s[3005],d[3005][3005];
int main(){
	int i,j,k,n,m,x,y,z,ans;
	scanf("%d%d%d",&n,&m,&k);
	for(i=0;i<=n;i++){
		for(j=0;j<=n;j++)d[i][j]=INF;
		v[i]=INF;
		u[i]=-1;
	}
	for(i=0;i<m;i++){
		scanf("%d%d%d",&x,&y,&z);
		d[x][y]=d[y][x]=z;
	}
	for(i=0;i<k;i++){
		scanf("%d",&y);
		//for(j=0;j<=n;j++)sv[j]=INF,u[j]=-1;
		v[y]=0;
	}
	while(1){
		x=-1;
		for(j=1;j<=n;j++){
			if(u[j]==-1 && (x==-1 || v[x]>v[j]))x=j;
		}
		if(x==-1)break;
		u[x]=0;
		//v[x]=min(v[x],sv[x]);
		//printf("s%d:%d\n",x,sv[x]);
		for(j=1;j<=n;j++){
			v[j]=min(v[j],v[x]+d[x][j]);
		}
	}
	
	ans=0;
	for(i=1;i<=n;i++){
		//printf("%d:%d\n",i,v[i]);
		for(j=1;j<=n;j++){
			if(v[i]!=INF && v[j]!=INF && d[i][j]!=INF)ans=Max(ans,(v[i]+v[j]+d[i][j]+1)/2);
		}
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 34000
# ABC131D - Megalomania
from itertools import accumulate
from operator import itemgetter as gt


def main():
    N, *AB = map(int, open(0).read().split())
    T, D = zip(*sorted([(i, j) for i, j in zip(*[iter(AB)] * 2)], key=gt(1)))
    T = list(accumulate(T))
    flg = all(t <= d for t, d in zip(T, D))  # all tasks finish within each deadline?
    print("Yes" if flg else "No")


if __name__ == "__main__":
    main()

Measured memory_bytes: 48700
#include <stdio.h>
#include <string.h>

#define N	500000

int max(int a, int b) { return a > b ? a : b; }

void zzz(char *cc, int *zz, int n) {
	int i, l, r;

	for (i = 1, l = r = 0; i < n; i++)
		if (zz[i - l] < r - i)
			zz[i] = zz[i - l];
		else {
			l = i, r = max(r, i);
			while (r < n && cc[r] == cc[r - l])
				r++;
			zz[i] = r - l;
		}
}

int main() {
	static char cc[N + 1], cc_[N + 1], bad[N + 1];
	static int zz[N], zz_[N];
	int n, p, m, i, cnt;

	scanf("%s", cc), n = strlen(cc);
	zzz(cc, zz, n);
	for (p = 1; p < n; p++)
		if (n % p == 0 && zz[p] == n - p)
			break;
	if (p == n) {
		printf("1\n");
		printf("1\n");
		return 0;
	}
	if (p == 1) {
		printf("%d\n", n);
		printf("1\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		cc_[i] = cc[n - 1 - i];
	zzz(cc_, zz_, n);
	for (p = 1; p < n; p++) {
		for (m = p + p; m <= n && zz[p] >= m - p; m += p)
			bad[m] = 1;
		for (m = p + p; m <= n && zz_[p] >= m - p; m += p)
			bad[n - m] = 1;
	}
	cnt = 0;
	for (m = 1; m < n; m++)
		if (!bad[m])
			cnt++;
	printf("2\n");
	printf("%d\n", cnt);
	return 0;
}


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

using namespace std;

typedef long long ll;

#define pii pair<int,int>
#define mp make_pair
#define F first
#define S second

int main()
{
    int n , k , i , j , u , v , m , now , cnt;
    scanf("%d %d",&n,&k);

    cnt = (n-1)*(n-2);
    cnt = cnt/2;

    if(k > cnt)return !printf("-1\n");

    now = cnt-k;

    vector<pii> vec;

    for(i = 2 ; i <= n ; i++)vec.push_back(mp(1,i));

    for(j = 2 ; j <= n && now ; j++)
    {
        for(i = j+1 ; i <= n && now ; i++)
        {
            vec.push_back(mp(j,i));
            now--;
        }
    }

    n = vec.size();
    printf("%d\n",n);
    for(i = 0 ; i < n ; i++)
    {
        printf("%d %d\n",vec[i].F,vec[i].S);
    }


    return 0;
}


Measured memory_bytes: 384
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    Integer[] street = new Integer[n];
    for(int i=0; i<n; i++){
      street[i]=sc.nextInt();
    }
    Arrays.sort(street);
    int sum=0;
    for(int i=0; i<n-1; i++){
      sum+=street[i+1]-street[i];
    }
    System.out.println(sum);
  }
}

Measured memory_bytes: 21972
A, B, C, X, Y = gets.split.map(&:to_i)
CC = 2 * C

E = [A, CC].min
N = [B, CC].min
NE = [A + B, CC].min

if X > Y
    puts Y * NE + (X-Y) * E
else
    puts X * NE + (Y-X) * N
end

Measured memory_bytes: 1788
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad.ST
import Data.Array.ST
import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Foldable

main :: IO ()
main = readLine >>= main'
    where
        main' [n, m] = readContents m >>= printRes . solve n m
        printRes = mapM_ print 

solve :: Int -> Int -> [[Int]] -> [Int]
solve n m bs = runST stActions
    where
        stActions = res <$> (makeTree n >>= calc)
            where
                calc uft = foldlM ff ([c], c, uft) . reverse . map (map pred) $ tail bs
                ff (acc, prev, uft) [a, b] = vals >>= check
                    where
                        vals = (,,) <$> isSame a b uft <*> size a uft <*> size b uft
                        check (f, sa, sb)
                            | f = return (prev : acc, prev, uft)
                            | otherwise = (next : acc, next, uft) <$ unionNodes a b uft
                            where
                                next = prev - sa * sb
                c = cmb2 n
                res (x, _, _) = x
                cmb2 x = (x * (x - 1)) `div` 2


readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine


type UFTree s = STArray s Int (Int, Int)

makeTree :: Int -> ST s (UFTree s)
makeTree n = newListArray (0, n - 1) . zip [0..(n - 1)] $ repeat 1

findRoot :: Int -> UFTree s -> ST s (Int, Int)
findRoot v uft = readArray uft v >>= check
    where
        check res@(root, _)
            | v == root = return res
            | otherwise = findRoot root uft >>= edit
        edit res = res <$ writeArray uft v res
            
unionNodes :: Int -> Int -> UFTree s -> ST s ()
unionNodes a b uft = mapM (`findRoot` uft) [a, b] >>= check
    where
        check [(c, cs), (d, ds)]
            | c == d = return ()
            | cs < ds = writeArray uft c (d, cs + ds) >> writeArray uft d (d, ds + cs)
            | otherwise = writeArray uft d (c, cs + ds) >> writeArray uft c (c, cs + ds) 

isSame :: Int -> Int -> UFTree s -> ST s Bool
isSame a b uft = (== 1) . length . group <$> mapM (`findRoot` uft) [a, b]

size :: Int -> UFTree s -> ST s Int 
size v uft = snd <$> findRoot v uft

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

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int,int>
#define PSI pair<string,int>
#define PIS pair<int,string>
#define PLI pair<long long int,int>
#define PLL pair<long long int,long long int>
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define MLI map<long long int,int>
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)





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




int d,n;
double z,a[12][12];

void find_dis(int x,int y)
{
    int i;
    z=0.00;
    for(i=1;i<=d;i++)z+=((a[x][i]-a[y][i])*(a[x][i]-a[y][i]));
    z=sqrt(z);
}

int main()
{
    FAST();

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    int i,j,x,ans=0;
    cin >> n >> d;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=d;j++)cin >> a[i][j];
    }
    for(i=1;i<n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            find_dis(i,j);
            x=z;
            if(x == z)++ans;
        }
    }
    cout << ans;
    return 0;
}


Measured memory_bytes: 256
import sys
import itertools
input = sys.stdin.readline

N = int(input())
A = [0]*N
evidences = []
for i in range(N):
    A[i] = int(input())
    e = [[int(i) for i in input().split()] for i in range(A[i])] 
    evidences.append(e)

combs = list(itertools.product(range(2), repeat=N))
table = [2]*N
maxtrue = 0

#print(table)
for comb in combs:
    c = list(comb)
    for i in range(N):
        if c[i] == 0:
            continue
        for evids in evidences[i]:
            if c[evids[0]-1] == evids[1]:
                continue
            else:
                break
        else:
            continue
        break
    else:
        #print(c)
        if maxtrue < c.count(1):
            maxtrue = c.count(1)
print(maxtrue)        


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

// clang-format off
#define forn(i, x, y) for(int i = x; i < y; i++)
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(NULL)
// clang-format on

using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;

class LazySegTree {
public:
    int n = 1;
    VLL data, lazy;

    LazySegTree(int N) {
        while (n < N) n *= 2;
        data.resize(2 * n);
        lazy.resize(2 * n);
    }

    void push(int k, int l, int r) {
        data[k] += lazy[k];
        if (r - l > 1) {
            lazy[k * 2] += lazy[k];
            lazy[k * 2 + 1] += lazy[k];
        }
        lazy[k] = 0;
    }

    void incr(int a, int b, LL val) {
        incr1(a, b, val, 1, 0, n);
    }
    void incr1(int a, int b, LL val, int k, int l, int r) {
        push(k, l, r);
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            lazy[k] = val;
            push(k, l, r);
        }
        else {
            int m = (l + r) >> 1;
            incr1(a, b, val, 2 * k, l, m);
            incr1(a, b, val, 2 * k + 1, m, r);
            data[k] = max(data[2 * k], data[2 * k + 1]);
        }
    }

    LL query(int a, int b) {
        return query1(a, b, 1, 0, n);
    }
    LL query1(int a, int b, int k, int l, int r) {
        push(k, l, r);

        if (b <= l || r <= a) return LONG_MIN;
        if (a <= l && r <= b) return data[k];

        int m = (l + r) >> 1;
        LL vl = query1(a, b, 2 * k, l, m);
        LL vr = query1(a, b, 2 * k + 1, m, r);
        return max(vl, vr);
    }
};

int main() {
    IOS;

    int N, M;
    cin >> N >> M;
    unordered_map<int, VI> add;
    unordered_map<int, vector<pair<int, int>>> del;
    int l, r, a;
    forn(i, 0, M) {
        cin >> l >> r >> a;
        add[l].push_back(a);
        del[r].push_back(make_pair(l, a));
    }

    LazySegTree seg(N);
    LL res = 0;
    for (int k = 1; k <= N; k++) {
        for (auto a: add[k])
            seg.incr(0, k, a);

        LL mx = seg.query(0, k);
        res = max(res, mx);
        seg.incr(k, k + 1, mx);

        for (auto d: del[k])
            seg.incr(0, d.first, -d.second);
    }

    cout << res << endl;

    return 0;
}


Measured memory_bytes: 46616
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef uint64_t ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();


int main() {
    int n;
    ll k;
    int bits = 62;
    cin >> n >> k;
    vector<ll> arr(n, 0);
    for(auto& val: arr)
        cin >> val;
    vector<ll> bit_cnt(bits, 0ull);
    for(ll num: arr) {
        for(int i = 0; i < bits; i++) {
            bit_cnt[i] += ((num >> i) & 1ull);
        }
    }
    // for(int i = 0; i < 45; i++)
    //    cout << bit_cnt[i] << " ";
    vector<ll> mask(bits, 0ull);
    for(int i = 0; i < bits; i++) {
        mask[i] = 1ull << i;
        if (bit_cnt[i]-0 >= n - bit_cnt[i]) // more 1s than 0s
            mask[i] = 0;
    }
    ll X = 0;
    for(int i = bits-1; i >= 0; i--) {
        if (X + mask[i] <= k)
            X += mask[i];
    }
    //printf("X = %d\n", X);
    ll ans = 0;
    for(ll num: arr)
        ans += num ^ X;
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 1024
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define Ms(s, n) memset(s, n, sizeof(s))
#define Rp(i,l,m) for(int i=l; i<m; ++i)
#define Rpd(i,l,m) for(int i=l; i>=m; --i)
#define Rpp(i,l,m) for(int i=l; i<=m; i++)
#define RpAll(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define All(a) (a).begin(), (a).end()
#define Fi first
#define Se second
#define Pb push_back
#define Pf push_front
#define Is insert
#define Sz(a) int((a).size())
#define ooi INT_MAX
#define ool LLONG_MAX
typedef long long ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
const int MOD = (int) 1e9 + 7;
const ld PI = acos((ld) -1);
inline ll gcd(ll a,ll b) {ll r;while(b){r=a%b;a=b;b=r;}return a;}
inline ll lcm(ll a,ll b) {return a/gcd(a,b)*b;}
inline ll fpow(ll n,ll k){ll r=1; for(;k;k>>=1){if(k&1)r=r*n%MOD;n=n*n%MOD;}return r;}
const int base = 311;
const int N = 2e5 + 5;

ll n, m, k, test, a[N];
int d[N];
vector<int> v[N], g[N];

void Dfs(int i){
    d[i] = 1;
    for(int j: v[i]){
        if(!d[j]){
            a[j] += a[i];
            Dfs(j);
        }
    }
}

void Solve(){
    cin >> n >> k;
    for(int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        v[x].Pb(y);
        v[y].Pb(x);
    }
    for(int i = 1; i<=k;i++){
        int x, y;
        cin >> x >> y;
        a[x]+=y;
    }
    Dfs(1);
    for(int i = 1; i<=n; i++) cout << a[i] << " ";
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    Solve();
    return 0;
}




















Measured memory_bytes: 26624
#include "bits/stdc++.h"
using namespace std;
typedef long long li;

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

    li n, m;
    cin >> n >> m;

    vector<pair<li, li>> xys;
    for (int i = 0; i < n; ++i) {
        li x, y;
        cin >> x >> y;
        xys.emplace_back(x, y);
    }

    sort(xys.begin(), xys.end());
    li ans = 0;
    li ptr = 0;
    priority_queue<li> revenues;
    for (int today = m; today >= 0; --today) {
        while (ptr < n && xys[ptr].first + today <= m) {
            revenues.push(xys[ptr].second);
            ptr += 1;
        }
        if (not revenues.empty()) {
            ans += revenues.top();
            revenues.pop();
        }
    }
    cout << ans << endl;


    return 0;
}

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

void solve(int N, int K){
    vector<int>  a;
    int d_i, A_ij;
    int ans;
    a = vector<int>(N, 0);
    for (int i=0; i<K; i++){
        cin >> d_i;
            for(int j=0; j<d_i; j++){
                cin >> A_ij;
                if (a[A_ij-1]==0) a[A_ij-1] = 1;
            }
    }
    ans = a.size() - accumulate(a.begin(), a.end(), 0);
    cout << ans <<endl;
    return;
}

int main(){
    int N, K;
    cin >> N >> K;
    solve(N, K);
}

Measured memory_bytes: 3636
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<tuple>


#define MOD 1000000007
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back

using namespace std;

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

int main(){
    int N;
    int ans=0;
    cin>>N;
    for(int i=1;i<N+1;i++){
        int a=i;
        for(int j=1;j<6;j++){
            if(a>=1&&a<10){
                if(j%2==1)ans++;
                break;
            }
            a/=10;
        }
    }
    cout<<ans;
    return 0;
}

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

int n,a,b,c;
int arr[100005][3];
int dp[100005][3];

int main() {
    cin >> n;
    for (int i=1;i<=n;i++) {cin >> arr[i][0] >> arr[i][1] >> arr[i][2];}
    for (int i=1;i<=n;i++) {
        dp[i][0]=max(dp[i-1][1],dp[i-1][2])+arr[i][0];
        dp[i][1]=max(dp[i-1][0],dp[i-1][2])+arr[i][1];
        dp[i][2]=max(dp[i-1][0],dp[i-1][1])+arr[i][2];
    }
    cout << max(max(dp[n][0],dp[n][1]),dp[n][2]) << endl;
}




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

int main(){

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

        while(a % 2 == 0){
            ans++;
            a /= 2;
        }
    }

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fst first
#define snd second
#define rep(n) for(lint I = 0; (I) < (lint)(n); ++(I))
#define repeat(i, n) for(lint i = 0; (i) < (lint)(n); ++(i))
#define repeat_to(i, n) for(lint i = 0; (i) <= (lint)(n); ++(i))
#define repeat_from(i, m, n) for(lint i = (m); (i) < (lint)(n); ++(i))
#define repeat_from_to(i, m, n) for(lint i = (m); (i) <= (lint)(n); ++(i))
#define repeat_reverse_from_to(i, m, n) for(lint i = (m); (i) >= (lint)(n); --(i))
#define el cout<<endl
#define dump(x) cout<<" "<<#x<<"="<<x
#define vdump(v) for(size_t I=0; I<v.size(); ++I){cout<<" "<<#v<<"["<<I<<"]="<<v[I];} cout<<endl
using namespace std;
using lint = long long;
using ld = long double;

/****************** グラフ ******************/
// 諸宣言
using Weight = lint;
using Flow = lint;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
    Edge(int s, int d, Weight w, Flow c) : src(s), dst(d), weight(w), cap(c) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1, Flow c = 1) {
    g[a].emplace_back(a, b, w, c);
    g[b].emplace_back(b, a, w, c);
}
void add_arc(Graph &g, int a, int b, Weight w = 1, Flow c = 1) {
    g[a].emplace_back(a, b, w, c);
}

int main(void) {
    int n;
    cin >> n;
    
    if (n == 1) {
        cout << "POSSIBLE" << endl;
        return 0;
    }
    
    Graph g(n);
    repeat_from(i, 1, n) {
        int a; cin >> a; --a;
        add_arc(g, a, i);
    }
    
    vector<int> x(n);
    repeat(i, n) cin >> x[i];
    
    auto get_min_dp = [] (vector<pair<int, int>> weights, int x) -> int {
        int n = weights.size();
        vector<vector<bool>> dp(n + 1, vector<bool>(x + 1, false));
        dp[0][0] = true;
        int total = 0;
        repeat(i, n) {
            int f = weights[i].fst;
            int s = weights[i].snd;
            total += f;
            total += s;
            repeat(j, x + 1) {
                if (dp[i][j] && j + f <= x) dp[i + 1][j + f] = true;
                if (dp[i][j] && j + s <= x) dp[i + 1][j + s] = true;
            }
            //dump(total); dump(i); vdump(dp[i+1]); el;
        }
        for (int i = x; i >= 0; --i) {
            if (dp[n][i]) {
                return total - i;
            }
        }
        return -1;
    };
    
    
    function<pair<int, int>(int)> dfs = [&] (int node) {
        if (g[node].empty()) {
            return pair<int, int> {x[node], 0};
        }
        vector<pair<int, int>> weights;
        for (auto child: g[node]) {
            weights.emplace_back(dfs(child.dst));
        }
        int other = get_min_dp(weights, x[node]);
        if (other < 0) {
            cout << "IMPOSSIBLE" << endl;
            exit(0);
        }
        return pair<int, int> {x[node], other};
    };
    
    tie(ignore, ignore) = dfs(0);
    cout << "POSSIBLE" << endl;
    return 0;
}

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

using namespace std;

void Main()
{
    array<string, 7> days = {
        "SUN",
        "MON",
        "TUE",
        "WED",
        "THU",
        "FRI",
        "SAT",
    };

    string s;
    cin >> s;

    int ret = -1;
    for (int i = 0; i < 7; ++i)
    {
        if (s == days[i])
        {
            ret = 7 - i;
        }
    }

    cout << ret;
}

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

    std::cout << std::fixed << std::setprecision(10);

#ifdef DEBUG_BUILD
    while (true)
    {
        Main();
        cout << std::endl;
    }
#endif

    Main();
    cout << endl;

    return 0;
}

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

#define F first
#define S second
#define prev azaza
#define MP make_pair
#define PB push_back

using namespace std;
typedef long long ll;
typedef long double ld;

const int max_n = 100111, inf = 1000111222;

int m[max_n];
int cnt[max_n];
int cnt1[max_n];

int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> m[i];
    }
    for (int i = 0; i < n; i += 2) {
        cnt[m[i]]++;
    }
    int mx = 1;
    for (int i = 2; i < max_n; ++i) {
        if (cnt[i] > cnt[mx]) {
            mx = i;
        }
    }
    int mx1 = 1;
    if (mx == 1) mx1 = 2;
    for (int i = 2; i < max_n; ++i) {
        if (i == mx) continue;
        if (cnt[i] > cnt[mx1]) {
            mx1 = i;
        }
    }
    ////////////
    for (int i = 1; i < n; i += 2) {
        cnt1[m[i]]++;
    }
    int mx2 = 1;
    for (int i = 2; i < max_n; ++i) {
        if (cnt1[i] > cnt1[mx2]) {
            mx2 = i;
        }
    }
    int mx3 = 1;
    if (mx2 == 1) mx3 = 2;
    for (int i = 2; i < max_n; ++i) {
        if (i == mx2) continue;
        if (cnt1[i] > cnt1[mx3]) {
            mx3 = i;
        }
    }
    if (mx != mx2) {
        int ans = n - (cnt[mx] + cnt1[mx2]);
        cout << ans << endl;
        return 0;
    } else {
        int ans1 = n - (cnt[mx] + cnt1[mx3]);
        int ans2 = n - (cnt[mx1] + cnt1[mx2]);
        cout << min(ans1, ans2);
        return 0;
    }

    return 0;
}




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

using namespace std;

int main() {
  int K;
  int A, B;

  cin >> K;
  cin >>A >> B;

  bool ans = false;


    for (int i = A; i <= B; i++){
        if (i % K == 0) ans = true;
    }

    if (ans == true) cout << "OK" << endl;
    else cout << "NG" << endl;

}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
#define FOR(i, m, n) for (int i = m; i < (n); i++)
#define RFOR(i, m, n) for (int i = (m - 1); i >= 0; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define print(ele) cout << ele << endl
#define print10(ele) cout << fixed << setprecision(10) << ele << endl

using namespace std;
typedef long long ll;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef vector<ll> lvec;

const int mod = 1e9 + 7;
const ll INF = 1000000000000000000LL;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int ans = min(a, b) + min(c, d);
  print(ans);
  return 0;
}

Measured memory_bytes: 256
n, m = [int(i) for i in input().split()]

l_max = 0
r_min = float('inf')
for _ in range(m):
  l, r = [int(i) for i in input().split()]
  if l > l_max:
    l_max = l
  if r < r_min:
    r_min = r

ans = r_min - l_max + 1

if ans > 0:
  print(ans)
else:
  print(0)


Measured memory_bytes: 3060
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>

using namespace std;

#define INF 1 << 29
#define LL long long int

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

    int n,t;
    cin >> n >> t;

    vector<LL> vec(n);

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

    LL dis = 0;
    LL count = 0;
    LL min = vec[0];

    for(int i = 1; i < n; i++){
        LL tmp = vec[i];
        if(dis < tmp - min){
            dis = tmp - min;
            count = 1;
        }else if(dis == tmp - min){
            count++;
        }

        if(min > tmp){
            min = tmp;
        }
    }

    cout << count << endl;

    return 0;
}

Measured memory_bytes: 1024
from collections import defaultdict
from itertools import permutations as p

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xy = [readInts() for i in range(n)]
l = p(list(range(0,n)))
length = len(list(p(list(range(0,n)))))
sumDis = 0
for i in l:
	for j in range(n-1):
		sumDis += ((xy[i[j+1]][0]-xy[i[j]][0])**2+(xy[i[j+1]][1]-xy[i[j]][1])**2)**0.5

print(sumDis/length)

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

int main(){
    int n;
    cin>>n;
    vector<int> k(n-n/2);
    vector<int> g(n/2);
    rep(i,n){
        if(i%2==0)cin>>k[i/2];
        else cin>>g[i/2];
    }
    if(n%2==0){
        reverse(g.begin(),g.end());
        for(int x:g) cout<<x<<' ';
        for(int x:k) cout<<x<<' ';
    }
    else{
        reverse(k.begin(),k.end());
        for(int x:k) cout<<x<<' ';
        for(int x:g) cout<<x<<' ';
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 2944
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define mod 1000000007
ll fact[50];
#define eps 1e-9
#define INF 1e18
ll power(ll a,ll b,ll c){
  ll ans=1;
  while(b){
    if(b&1){
      ans=ans*a%c;
    }
    a=a*a%c;
    b=b/2;
  }
  return ans;
}
ll ncr(ll n,ll r){
    if(r>n)return 0;
    if(r==n)return 1;
    ll z=fact[n];
    z=z*power(fact[r],mod-2,mod)%mod;
    z=z*power(fact[n-r],mod-2,mod)%mod;
    return z;
}
ll min(ll a,ll b){return a<b?a:b;}
ll max(ll a,ll b){return a>b?a:b;}
int main() {
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll k,n; cin>>k>>n;
        ll sum=0;
        for(int i=0;i<n;++i){
            ll x; cin>>x;
            sum+=x;
        }
        if(sum>k)cout<<-1;
        else cout<<k-sum<<endl;
        
        return 0;
}


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

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

  vector<bool> goods(n, true);

  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  vector<int> from(m);
  vector<int> to(m);
  for (int i = 0; i < m; i++) {
    int left, right;
    cin >> left >> right;

    if (h[left - 1] < h[right - 1]) {
      goods[left - 1] = false;
    }
    if (h[left - 1] == h[right - 1]) {
      goods[left - 1] = false;
      goods[right - 1] = false;
    }
    if (h[left - 1] > h[right - 1]) {
      goods[right - 1] = false;
    }
  }

  int num = 0;
  for (int i = 0; i < n; i++) {
    if (goods[i] == true) { num++; }
  }
  cout << num << endl;
  return 0;
}

Measured memory_bytes: 4584
#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, M;
int L[100000], R[100000];

int main() {
    cin >> N >> M;
    rep(i, M) cin >> L[i] >> R[i];

    // 絞り込む
    int l = -1;
    int r = 100001;
    for (int i = 0; i < M; i++) {
        if (l < L[i]) l = L[i];
        if (r > R[i]) r = R[i]; 
    }

    if (l <= r) cout << r - l + 1 << endl;
    else cout << 0 << endl;
}

Measured memory_bytes: 1024
n,x,y,c = gets.split.map {|i| i.to_i-1 }
x,y = n-y,n-x  if x > n-y
a = Array.new n+1,0
f =->(i,j,k) {
  begin
    s = 0
    s += 1  if j > i+=1
    s += 1  if i < j-=1
    s += 1  if n > k+=1
    a[c+=1] += s
  end while s > 0
}
y.times {|i|
  c = 0
  if x < i
    [y-i,i-x].min.times { a[c+=1] += 1; i+=1 }
    if i < y-1
      a[c+=1] += 2
      f[i+1,y,y-1]
    else
      (n-i).times { a[c+=1] += 1 }
    end
  else
    (x-i).times { a[c+=1] += 1 }
    a[c+=1] += 2
    f[x+1,y,y-1]
  end
}
(m=n-y).times {|i| a[m-i] += i+1 }
puts a[1,n]

Measured memory_bytes: 1788
#include <iostream>
#include<algorithm>
#include <vector>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define max(x,y) (((x)>(y)) ? x:y)
#define min(x,y) (((x)<(y)) ? x:y)
#define FOR(a,b,c) for(b=(c);b<(a);b++)
#define ender {return 0;}
#define tosasuretu(a) (a*(a+1)/2)
inline ll kaizyo(ll);
ll combination(ll, ll);
int main() {
	int w, h;
	cin >> h >> w;
	vector<string> a, ans;
	for (int i = 0; i < h; i++) {
		string get;
		cin >> get;
		bool checker = false;
		for (unsigned int j = 0; j < get.size(); j++) {
			if (get[j] == '#') {
				checker = true;
				break;
			}
		}
		if (checker) {
			a.push_back(get);
		}
	}
	bool checking[100];
	for (unsigned int k = 0; k < a[0].size(); k++) {
		checking[k] = false;
		for (int l = 0; l < a.size(); l++) {
			if (a[l][k] == '#') {
				checking[k] = true;
				break;
			}
		}
	}
	for (unsigned int m = 0; m < a.size(); m++) {
		for (int n = 0; n < a[m].size(); n++) {
			if (checking[n]) {
				cout << a[m][n];
			}
		}
		cout << endl;
	}
	return 0;
}


inline ll kaizyo(ll x) {
	long ans = 1;
	for (int z = 0; z < x; z++) {
		ans *= (x - z);
	}
	return ans;
}


Measured memory_bytes: 256
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n, d;
	int count = 0;
	int x, y;
	cin >> n >> d;	
	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		if (sqrt(pow(x, 2) + pow(y, 2)) <= d)
			count++;
	}
	cout << count;
	return 0;
}


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

int main(int argc, char *argv[]){
    int N, M;
    std::cin >> N >> M;
    std::vector<int> S(N), T(M);
    for(auto &s : S){
        std::cin >> s;
    }
    for(auto &t : T){
        std::cin >> t;
    }

    constexpr long int m = 1000000000 + 7;
    long int dp[N][M];
    long int dpS[N][M+1];
    long int dpT[N+1][M];

    int i,j;
    for(i=0; i<N; ++i){
        dpS[i][0] = 0;
    }
    for(j=0; j<M; ++j){
        dpT[0][j] = 0;
    }
    
    long int delta;
    for(i=0; i<N; ++i){
        for(j=0; j<M; ++j){
            dp[i][j] = dpS[i][j];
            dp[i][j] += dpT[i][j];
            dpS[i][j+1] = dpS[i][j];
            dpT[i+1][j] = dpT[i][j];
            if(i==0 || j==0){
                delta = 1;
            }else{
                delta = dp[i-1][j-1];
            }
            dp[i][j] += delta;
            if(S[i]==T[j]){
                dp[i][j] += delta;
                dpS[i][j+1] += delta;
                dpS[i][j+1] %= m;
                dpT[i+1][j] += delta;
                dpT[i+1][j] %= m;
            }
            dp[i][j] %= m;
        }
    }

    std::cout << dp[N-1][M-1] << std::endl;
    return 0;
}


Measured memory_bytes: 94080
#include<bits/stdc++.h>
using namespace std;
    
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
    
#define int long long
#define double long double
    
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (int)x.size()
    
// #define ff first
// #define ss second
#define mp make_pair
         
#define pb push_back
#define endl "\n" 
         
#define f(i,l,r) for(int i=l;i<=r;i++)
#define rf(i,r,l) for(int i=r;i>=l;i--)    

#define bp __builtin_popcountll
// #define inf 1e15
    
const int N=1e5+5;
const int M=1e9+7;

void solve()
{
	int n;
	cin>>n;
	vector<int> s,e,v;
	for(int i=0;i<n;i++)
	{
		int t1,t2;
		cin>>t1>>t2;
		s.pb(t1);
		e.pb(t2);
		v.pb(t1);
		v.pb(t2);
	}
	sort(all(s));
	sort(all(e));
	sort(all(v));
	if(n%2)
	{
		int tt=n/2;
		int p1=s[tt];
		int p2=e[tt];
		if(p1<=p2)
		{
			int res=p2-p1+1;
			cout<<res;
			return;
		}
		else{
			while(1)
			{
				
			}
			cout<<0;
			return;
		}
	}
	else
	{
		int tt=n/2-1;
		int p1=s[tt];
		int p2=e[tt];
		tt=n/2;
		int q1=s[tt];
		int q2=e[tt];
		// cout<<p1<<" "<<p2<<" "<<q1<<" "<<q2<<endl;
		if(p1<=p2 && q1<=q2)
		{
			double ss=(p1+q1)/2.0;
			double ee=(p2+q2)/2.0;
		    ss=abs(ss-ee);
			ss*=2;
			int x=ss;
			x++;
			cout<<x;
			return;
		}
		else
		{
			while(1)
			{
				
			}
		}
	}
}
signed main()  
{
	FAST
    int t=1;
    // cin>>t;
    for(int tc=1;tc<=t;tc++)
    {
    	// cout<<"Case #"<<tc<<": ";
    	solve();
    }
}


Measured memory_bytes: 9720
#include <stdio.h>
#include <string.h>

int main(void)
{
    char a[11],b[11],c[11];
    scanf("%s%s%s",a,b,c);
    puts(a[strlen(a)-1]==b[0]&&b[strlen(b)-1]==c[0]?"YES":"NO");
    return 0;
}

Measured memory_bytes: 128
k = int(input())

# a,bの最大公約数
def gcd(a, b):
   while b:
       a, b = b, a % b
   return a

ansl = [[gcd(i, j) for i in range(201)] for j in range(201)]

ans = 0
for i in range(1,k+1):
    for j in range(1, k + 1):
        p = ansl[i][j]
        for k in range(1, k + 1):
            ans += ansl[p][k]

print(ans)

Measured memory_bytes: 68684
# -*- coding: utf-8 -*-
'import sys'
'import math'
n=int(input())
A=[int(i) for i in input().split()]
A.sort()
ans=0.0
for i in range(1,n):
    cd = int(A[i])-int(A[i-1])
    if i==n-1: 
        ab=int(A[n-3])+int(A[n-4])
    elif i==n-2: 
        ab=int(A[n-1])+int(A[n-4])
    else:
         ab=int(A[n-1])+int(A[n-2])
    ans=max(ans,ab/cd)
print(ans)

Measured memory_bytes: 7776
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, q = map(int, readline().split())
ab = [list(map(int, readline().split())) for i in range(n - 1)]
px = [list(map(int, readline().split())) for j in range(q)]
graph = [[] for _ in range(n + 1)]
for a, b in ab:
    graph[a].append(b)
    graph[b].append(a)
memo = [0] * (n + 1)
for p, x in px:
    memo[p] += x
stack = [(1, 0)]
while stack:
    x, parent = stack.pop()
    memo[x] += memo[parent]
    for y in graph[x]:
        if y == parent:
            continue
        stack.append((y, x))
print(*memo[1:])


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

int main() {
    int n;
    cin >> n;
    vector<int> v(n), c(n), value(n);
    for(int i = 0; i < n; i++)
    {
        cin >> v.at(i);
    }
    for(int i = 0; i < n; i++)
    {
        cin >> c.at(i);
    }
    for(int i = 0; i < n; i++)
    {
        value.at(i) = v.at(i) - c.at(i);
    }
    sort(value.begin(), value.end());
    reverse(value.begin(), value.end());
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        if (value.at(i) > 0) ans += value.at(i);
        else break;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256