akhauriyash/Code-Regression huggingface dataset:
object Main {

  object Scanner {
    private val buf = new Array[Byte](1024); private var ptr = 0; private var len = 0
    @inline private def isPrintableChar(c: Int): Boolean = 33 <= c && c <= 126
    @inline private def hasNextByte(): Boolean =
      if (ptr >= len) { ptr = 0; len = System.in.read(buf); len > 0 } else { true }
    @inline private def hasNext(): Boolean = {
      while (hasNextByte() && !isPrintableChar(buf(ptr))) ptr += 1
      hasNextByte()
    }
    @inline private def readByte(): Byte =
      if (hasNextByte()) { val res = buf(ptr); ptr += 1; res } else { -1 }
    def next(): String = {
      if(!hasNext()) ???
      val sb = new StringBuilder; var b = readByte()
      while (isPrintableChar(b)) { sb.append(b.toChar); b = readByte() }
      sb.toString
    }
    def nextInt(): Int = {
      val n = nextLong()
      if (n < Int.MinValue || Int.MaxValue < n) ???
      n.toInt
    }
    def nextLong(): Long = {
      if(!hasNext()) ???
      var minus = false; var b = readByte()
      if (b == '-') { minus = true; b = readByte() }
      def go (b: Byte, n: Long = 0): Long =
        if ('0' <= b && b <= '9') { go(readByte(), n * 10 + b - '0') }
        else if (minus) { -n } else { n }
      go(b)
    }
    def nextDouble(): Double = next.toDouble
  }

  def main(args: Array[String]): Unit = {
    val H, W = Scanner.nextInt()
    val S = Array.fill(H)(Scanner.next())
    val dp = Array.fill[Short](H, W, 2)(-1) // 未計算の場合-1, dp(i)(j)(0) 縦の数 dp(i)(j)(1)横の数
    var i = 0
    while (i < H) {
      var j = 0
      while (j < W) {
        if (S(i)(j) == '.') { // 障害物ではない場合
          if (dp(i)(j)(0) == -1) { // 縦方向未計算の場合のみ計算
            var di = 0
            while (i + di < H && S(i+di)(j) == '.') { // 障害物に当たるか壁に当たるかまで
              di += 1
            }
            var dii = 0
            val hoge = (di - 1).toShort
            while (dii < di) {
              dp(i+dii)(j)(0) = hoge
              dii += 1
            }
          }
          if (dp(i)(j)(1) == -1) { // 横方向未計算の場合のみ計算
            var dj = 0
            while (j + dj < W && S(i)(j+dj) == '.') { // 障害物に当たるか壁に当たるかまで
              dj += 1
            }
            var djj = 0
            val hoge = (dj - 1).toShort
            while (djj < dj) {
              dp(i)(j+djj)(1) = hoge
              djj += 1
            }
          }
        }
        j += 1
      }
      i += 1
    }

    var max = 0
    i = 0
    while (i < H) {
      var j = 0
      while (j < W) {
        if (S(i)(j) == '.') { // 障害物ではない場合
          max = Math.max(max, dp(i)(j)(0) + dp(i)(j)(1) + 1)
        }
        j += 1
      }
      i += 1
    }
    println(max)
  }

}



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

int main() {

	int N = 0;
	int W = 0;
	cin >> N >> W;

	int * weights = new int[N+1];
	int * values = new int[N+1];
	int ** C = new int*[N+1];
	for(int i = 0; i < N+1; i++){
	    C[i] = new int[W+1];
	}
	
	for(int n = 1; n <= N; n++){
	    cin >> values[n] >> weights[n];
	}
	
	for(int w = 0; w < W+1; w++){
	    C[0][w] = 0;
	}
	
	for(int i = 1; i < N+1; i++){
	    C[i][0] = 0;
	}
     
    for(int i = 1; i < N+1; i++){
        for(int w = 0; w < W+1; w++){
            if(weights[i] <= w){
                if(values[i] + C[i-1][w-weights[i]] > C[i-1][w]){
                    C[i][w] = values[i] + C[i-1][w-weights[i]];
                }else{
                    C[i][w] = C[i-1][w];
                }
            }else{
                C[i][w] = C[i-1][w];
            }
            
        }
    }
    
   
    cout << C[N][W] << endl;
    return 0;
	
}

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

int main(){
  
  vector<char> AAA={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  vector<char> BBB={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  
  for(int i=0; i<3; i++){
    string x;
    cin >> x;
    for(int j=0; j<=25; j++){
      if(x.at(0)==AAA.at(j)){
        
        cout << BBB.at(j) ;
      }
    }
  }
  cout << endl;
}



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

const int MAX_N = 2 * 1e5 + 2;
const int MAX_M = 2 * 1e5 + 2;

int D[MAX_N], T[MAX_M];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N;
    map<int, int> cnt;
    for (int i = 0; i < N; i++) {
        cin >> D[i];
        cnt[D[i]]++;
    }
    cin >> M;
    for (int i = 0; i < M; i++) {
        cin >> T[i];
    }
    for (int i = 0; i < M; i++) {
        if (cnt[T[i]] == 0) {
            cout << "NO" << endl;
            return 0;
        }
        cnt[T[i]]--;
    }
    cout << "YES" << endl;
    return 0;
}

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

char const mozi1[]={'.',',','!','?',' '},mozi2[]={'a','b','c'},mozi3[]={'d','e','f'},mozi4[]={'g','h','i'},mozi5[]={'j','k','l'},mozi6[]={'m','n','o'},mozi7[]={'p','q','r','s'},mozi8[]={'t','u','v'},mozi9[]={'w','x','y','z'};
char mozi(int,char);
int main(){
	int n;
	cin>>n;

	for(int i=0;i<n;i++){
		string keitai;

		cin>>keitai;
		int cnt=0;
		for(int i=0;i<keitai.size();i++){

			if(keitai[i]=='0')	continue;

			else{
				int kazu=1;
				while(keitai[i+1]!='0'){
					kazu++;
					i++;
				}
			cout<<mozi(kazu,keitai[i]);
			i++;
			cnt++;
			}
		}
		cout<<endl;
	}
	return 0;
}


char mozi(int kazu,char keitai){
	switch (keitai)
	{
	case '1':
		kazu=kazu%5-1;
		if(kazu==-1)	return mozi1[4];
		return mozi1[kazu];
		
	case '2':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi2[2];
		return mozi2[kazu];
		
	case '3':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi3[2];
		return mozi3[kazu];
		
	case '4':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi4[2];
		return mozi4[kazu];
		
	case '5':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi5[2];
		return mozi5[kazu];
		
	case '6':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi6[2];
		return mozi6[kazu];
		
	case '7':
		kazu=kazu%4-1;
		if(kazu==-1)	return mozi7[3];
		return mozi7[kazu];
		
	case '8':
		kazu=kazu%3-1;
		if(kazu==-1)	return mozi8[2];
		return mozi8[kazu];
		
	case '9':
		kazu=kazu%4-1;
		if(kazu==-1)	return mozi9[3];
		return mozi9[kazu];
	default:
		return '0';
	}
}

Measured memory_bytes: 1204
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(i = 0; i < n; i++)
//2D::rep(i,j)rep(j,j)cin>>bin[i][j];
//vector<int> a(n)
#define Graph vector<vector<int>>
#define A1 cout<<"No"<<endl
#define A2 cout<<"Yes"<<endl
#define A3 cout<<ans<<endl
#define AA(i) cout<<i<<endl;
const int U=2e5+5;
const ll MOD=1000000007;
const int INF = 1001001001;

int main(){
  int N, M; cin >> N >> M;
  vector<int> a(4,0);
  int s, c;

  for(int i=0; i<M; i++){
    cin >> s >> c;
    if(a[N-s+1] == 0){
      if(s == 1 && c == 0 && N>2){
        AA(-1);
        return 0;
      }
      a[N-s+1] = c;
    }
    else if(a[N-s+1] != c){
      AA(-1);
      return 0;
    }
  }

  int ans = a[1] + a[2] * 10 + a[3] * 100;

  if(ans < pow(10, N-1) && N > 1) ans += pow(10, N-1);
  AA(ans);

  return 0;
}

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

int n;
string s;

int stoi(string x){
  stringstream ss(x);
  int n;
  ss >> n;
  return n;
}

string parse(int l, int r){
  string res = "";
  while(l<r){
    while(l<r && s[l]<'0' || '9'<s[l]){
      res += s[l];
      if(n<res.size())return res;
      l++;
    }
    if(l==r)return res;

    string tmp = "";
    do{
      tmp += s[l];
      l++;
    }while('0'<=s[l] && s[l]<='9');


    int time = stoi(tmp);

    int num = 0,dif = 0;
    while(l+dif<r){
      if(s[l+dif] == '(')num++;
      if(s[l+dif] == ')')num--;
      if(!num){
	string hoge;
	if(s[l] == '(' && s[l+dif] == ')')hoge = parse(l+1,l+dif);
	else hoge = parse(l,l+dif+1);
	for(int i=0;i<time;i++){
	  res += hoge;
	  if(n<res.size())return res;
	}
	l += dif+1;
	break;
      }
      dif++;
    }
  }
  return res;
}
    
int main(){
  int cnt = 0;
  while(cin >> s >> n , s!="0"||n){
    string str = parse(0,s.size());

    if(str.size()<n+1)cout << 0 << endl;
    else cout << str[n] << endl;
  }
}

Measured memory_bytes: 4516
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int X,Y; cin >> X >> Y;
    int pa[4] = {300000,200000,100000,0};
    int pb[4] = {300000,200000,100000,0};

    if(X==1 && Y == 1){
       int ans =  pa[0]+pb[0] + 400000;
        cout << ans << endl;
    }else{
        int ans = pa[min(3,X-1)] + pb[min(3,Y-1)];
        cout << ans << endl;
    }

}


Measured memory_bytes: 256
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

// struct UnionFind {
//     vector<int> par;
    
//     UnionFind(int N): par(N) {
//         for (int i=0;i<N;i++) par[i] = i;
//     }

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

//     void unite(int x, int y) {
//         int rx = root(x);
//         int ry = root(y);
//         if (rx == ry) return;
//         par[rx] = ry;
//     }

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

int main(void) {
    ll N;
    string S;
    cin >> N >> S;
    vector<int> RL(2LL*N,0);

    for (ll i=0;i<2LL*N;i++) {
        ll j = 2*N-i-1;
        if (j%2) {
            if (S[i] == 'W') S[i] = 'B';
            else S[i] = 'W';
        }
    }
    for (ll i=0;i<2LL*N;i++) {
        if (S[i] == 'B') RL[i]++;
    }
    if (count(RL.begin(), RL.end(), 1) != N || RL[0] == 1) {
        cout << 0 << endl;
        return 0;
    }
    ll ans = 1LL, X = 0LL;
    for (ll i=0;i<2LL*N;i++) {
        if (!RL[i]) X++;
        else {
            ans = ans * X-- % MOD;
        }
    }
    for (ll i=1;i<=N;i++) {
        ans = ans * i % MOD;
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1412
# -*- coding: utf-8 -*-
info = list(map(int, input().split(' ')))
N, X = info[0], info[1]
info = list(map(int, input().split(' ')))
len = 0

count = 1

for for1 in info:
  len += for1
  if len <= X:
    count += 1
  else:
    break

print(count)


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

int main() {
  int a;
  cin >> a;
  if (a % 2 == 1) {
    cout << (a / 2) * (a / 2 + 1) << endl;
  }
  else {
    cout << (a / 2) * (a / 2) << endl;
  }
}

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

using namespace std;
typedef long long ll;

int main(){
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  
  if(K <= A) cout << K << endl;
  else if (K <= A+B) cout << A << endl;
  else cout << 2*A + B - K << endl;
  
  return 0;
}

Measured memory_bytes: 3632
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

   private static final String INPUT_FILE_PATH = "";

   static int[] parent, sz;

   static int find(int x) {
      if (parent[x] == x) return x;
      return parent[x] = find(parent[x]);
   }

   static void solve(InputReader in, PrintWriter out) {
      int n = in.nextInt(), m = in.nextInt();

      parent = new int[n + 1];
      sz = new int[n + 1];
      for (int i = 1; i <= n; i++) {
         parent[i] = i;
         sz[i] = 1;
      }

      for (int i = 0; i < m; i++) {
         int a = in.nextInt(), b = in.nextInt();

         int ra = find(a), rb = find(b);
         if (ra == rb) continue;
         parent[rb] = ra;
         sz[ra] += sz[rb];
      }

      int ans = 1;
      for (int i = 1; i <= n; i++) {
         ans = Math.max(ans, sz[i]);
      }
      out.println(ans);
   }

   public static void main(String[] args) throws Exception {
      InputStream inputStream = INPUT_FILE_PATH.isEmpty()
         ? System.in
         : new FileInputStream(new File(INPUT_FILE_PATH));
      OutputStream outputStream = System.out;

      InputReader inputReader = new InputReader(inputStream);
      PrintWriter printWriter = new PrintWriter(outputStream);

      solve(inputReader, printWriter);

      printWriter.close();
   }

   private static class InputReader {
      private BufferedReader br;
      private StringTokenizer st;

      public InputReader(InputStream inputStream) {
         this.br = new BufferedReader(new InputStreamReader(inputStream), 32768);
         this.st = null;
      }

      public String nextToken() {
         while (st == null || !st.hasMoreTokens()) {
            try {
               st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
         return st.nextToken();
      }

      public int nextInt() {
         return Integer.parseInt(nextToken());
      }

      public long nextLong() {
         return Long.parseLong(nextToken());
      }

      public double nextDouble() {
         return Double.parseDouble(nextToken());
      }
   }

}


Measured memory_bytes: 58216
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n;
    cin >> n;
    map<int,int> mp;
    for (int i=0; i<n; i++) {
        int d;
        cin >> d;
        mp[d]++;
    }

    int m;
    cin >> m;
    bool ans = true;
    for (int i=0; i<m; i++) {
        int t;
        cin >> t;
        if (mp[t] <= 0) ans = false;
        mp[t]--;
    }
    
    cout << (ans? "YES" : "NO") << endl;
}


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

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

  int ans=1;
  for (int i = 0; i < n; i++)
  {
    if(ans<k) ans*=2;
    else ans+=k;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int N;
    int t[110000],x[110000],y[110000];
    cin >> N;
    t[0] = x[0] = y[0] = 0;
    for(int i = 0;i < N; ++i) cin >> t[i+1] >> x[i+1] >> y[i+1];

    bool can = true;
    for (int i = 0;i < N; ++i){
        int dt = t[i+1] - t[i];
        int dist = abs(x[i+1] - x[i]) + abs(y[i+1] - y[i]);
        if(dt < dist) can = false;
        if(dist % 2 != dt % 2) can = false;
    }
    if (can) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 1408
#include "bits/stdc++.h"
using namespace std;

int main() {
    long long r,d,x2000;
    cin >> r >> d >> x2000;
    for (int i = 0; i < 10; i++) {
        x2000 = r*x2000-d;
        cout << x2000 << endl;
    }
    return 0;
}

Measured memory_bytes: 256
from itertools import groupby
from collections import defaultdict

mod = 10**9+7
N = int(input())
C = [int(input()) for _ in range(N)]
C = [k for k, g in groupby(C)]
num = defaultdict(int)
dp = [1] + [0] * len(C)
for i, c in enumerate(C):
    dp[i+1] = num[c] = (dp[i] + num[c]) % mod
print(dp[-1])

Measured memory_bytes: 87560
#include<bits/stdc++.h>
#define int long long int
#define rep(i,a,b)	for(i=a;i<b;i++)
using namespace std;
vector<vector<int> >adj(100005);
int in[100005];
bool vis[100005];
int dist[100005];
void dfs(int x)
{
	if(vis[x])	return;
	vis[x]=true;
	for(auto i: adj[x]){
		if(!vis[i]){
			dfs(i);
		}
		dist[x]=max(dist[x],dist[i]);
	}
	if(adj[x].size()!=0)	dist[x]++;
}
int32_t main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	
	int n,m;
	cin >> n >> m;
	int x,y;
	for(int i=0;i<100005;i++){
		in[i]=0;
		dist[i]=0;
		vis[i]=false;
	}
	for(int i=1;i<=m;i++){
		cin >> x >> y;
		adj[x].push_back(y);
		in[y]++;
	}
	for(int i=1;i<=n;i++){
		if(in[i]==0 && !vis[i]){
			dfs(i);
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		// cout << dist[i] <<" ";
		ans=max(ans,dist[i]);
	}
	cout << ans ;


	return 0;
}

Measured memory_bytes: 16532
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long int ll;
#define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i];

int main(){
  int N;
  cin >> N ;
  int d[110] ;
  cinf(N,d) ;
  int num[110] ={0} ;
  for(int i=0;i<N;i++)
    num[d[i]] ++ ;
  
  int ans=0 ;
  for(int i=0; i<=100;i++){
    if(num[i])
      ans ++ ;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<array>
#include<map>

int powMod(int base,int exp,int mod){
	if(exp<0)exp+=mod-1;
	int ans=1;
	for(;;){
		if(exp&1)ans=(int64_t)ans*base%mod;
		exp>>=1;if(exp==0)return ans;
		base=(int64_t)base*base%mod;
	}
}
struct prg{
	static int constexpr NPRIME=3;
	using value_t=std::array<int,NPRIME>;
	static std::array<int,NPRIME> constexpr PRIMES{{1073741789,1073741783,1073741741}};
	static int constexpr BASE=250007;

	value_t value;
	int offset;
	constexpr prg():value{},offset{}{}

	static prg const left,right,inc,dec;
private:
	constexpr prg(value_t v,int o):value(v),offset(o){
		for(int p=0;p<prg::NPRIME;++p){
			value[p]=value[p]%PRIMES[p];
			if(value[p]<0)value[p]+=PRIMES[p];
		}
	}
};

prg operator*(prg A,prg B){
	prg AB;
	AB.offset=A.offset+B.offset;
	for(int p=0;p<prg::NPRIME;++p){
		AB.value[p]=(A.value[p]+1LL*powMod(prg::BASE,A.offset,prg::PRIMES[p])*B.value[p])%prg::PRIMES[p];
	}
	return AB;
}

std::ostream& operator<<(std::ostream& stream,prg p){
	return stream<<p.value[0]<<'|'<<p.offset;
}

std::array<int,prg::NPRIME> constexpr prg::PRIMES;
prg const prg::left {{ 0, 0, 0},-1};
prg const prg::right{{ 0, 0, 0}, 1};
prg const prg::inc  {{ 1, 1, 1}, 0};
prg const prg::dec  {{-1,-1,-1}, 0};

int main(){auto&O=std::cout;
#ifdef _GLIBCXX_DEBUG
std::stringstream I(R"(
48
-+><<><><><>>>+-<<>->>><<><<-+<>><+<<>+><-+->><<
)");
#else
auto&I=std::cin;std::ios::sync_with_stdio(0);I.tie(0);
#endif

int n;I>>n;
std::string st;st.reserve(n);I>>st;
std::vector<prg> prefix(n+1);
for(int i=0;i<n;++i){
	prefix[i+1]=prefix[i]*(
		st[i]=='<'?prg::left:
		st[i]=='>'?prg::right:
		st[i]=='+'?prg::inc:
		st[i]=='-'?prg::dec:throw 0
	);
}
std::map<prg::value_t,int> value_count;
value_count.emplace(prefix[n].value,1);
int64_t ans=0;
for(int i=n;i--;){
	// consider programs B starting at (i) have the same value as whole program
	// so (AB) = (A)*(B) = prefix[i]*(B)
	auto it=value_count.find((prefix[i]*prefix[n]).value);
	if(it!=value_count.end()){
		ans+=it->second;
	}
	++value_count[prefix[i].value];
}
O<<ans<<'\n';

}

Measured memory_bytes: 20096
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>

#define ll long long
#define ull unsigned long long

using namespace std;

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

	int left = 0;
	int right = N-1;
	for (int ii = 0; ii < M; ++ii){
		int L, R;
		cin >> L >> R;
		L--;
		R--;
		if (left < L){
			left = L;
		}
		if (R < right){
			right = R;
		}
	}

	int ans = right - left + 1;
	if (ans > 0){
		cout << ans << endl;
	} else {
		cout << 0 << endl;		
	}


	return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"

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

const long long MOD = 1e9+7;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

int main() {
    int n, m;
    cin >> n >> m;
    string ans(n, '0');
    vector<bool> done(n);
    rep(i, m) {
        int s;
        char c;
        cin >> s >> c;
        s--;

        if (done.at(s) && ans.at(s) != c) {
            cout << -1 << endl;
            return 0;
        }

        ans.at(s) = c;
        done.at(s) = true;
    }

    if (!done.at(0) && n != 1) ans.at(0) = '1';

    if (n == 1) cout << ans << endl;
    else if (ans.at(0) == '0') cout << -1 << endl;
    else cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

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

    ll s[n + 1];
    ll t[m + 1];
    for (ll i = 0; i < n; i++)
    {
        cin >> s[i + 1];
    }
    for (ll i = 0; i < m; i++)
    {
        cin >> t[i + 1];
    }

    ll dp[n + 1][m + 1];
    for (ll i = 0; i <= n; i++)
    {
        for (ll j = 0; j <= m; j++)
        {
            if (i == 0 || j == 0)
            {
                dp[i][j] = 1;
            }
            else
            {
                if (s[i] == t[j])
                {
                    dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % mod;
                }
                else
                {
                    dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % mod;
                    if (dp[i][j] < 0)
                    {
                        dp[i][j] += mod;
                    }
                }
            }
        }
    }

    cout << dp[n][m] << endl;

    return 0;
}


Measured memory_bytes: 31616
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int a, b, c;
	cin >> a ;
	if (a<1200)
	cout << "ABC" << endl;
	else if (a<2800)
	cout << "ARC" << endl;
		else cout << "AGC" << endl;
}	

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
    int a,s[1002],i;
    while(cin>>a) {
        for(i=0; i<a; i++)
            cin>>s[i];
        sort(s,s+a);
        cout<<s[a-1]-s[0]<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=10;
int a[maxn];
int main(){
	for (int i=1;i<=5;i++){
		cin>>a[i];
	}
	for (int i=1;i<=5;i++){
		if (a[i]==0){
			cout<<i<<endl;
			return 0;
		}
	}
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
using namespace std;
int a,b;
int main(){
	int i;
	cin>>a>>b;
	for(i=0;i<=10000;i++)
	{
		if((int)((double)i*0.08)==a&&(int)((double)i*0.10)==b)
		{
			cout<<i<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    ll n, sum = 0, ans = 0;
    cin >> n;
    vector<int> a(n);
    rep(i, n)cin >> a[i];
    map<ll, ll> mp;
    mp[sum]++;
    rep(i, n){
        sum += a[i];
        ans += mp[sum];
        mp[sum]++;
    }
    cout << ans << endl;
}   


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

using namespace std;

typedef pair<int,int> P;
typedef pair<int,pair<int,int>> PP;
typedef long long ll;

const double EPS = 1e-9;
const int INF = 1e9;
const int MOD = 1e9+7;

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

double dist(int x1,int y1,int x2,int y2){
	return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}

int main(void) {
	int N;
	cin >> N;
	vector<int> f(N),a(N),t(N),x(N),y(N);
	for(int i=0;i<N;i++){
		cin >> f[i] >> a[i] >> t[i] >> x[i] >> y[i];
	}

	int fa = INF,fb = INF;
	double da = -1,db = -1;
	for(int i=1;i<N;i++){
		if(t[i] == t[i-1] && a[i] != a[i-1]){
			int d = dist(x[i],y[i],x[i-1],y[i-1]);
			int df = f[i] - f[i-1];
			if(t[i] == 0){
				if(da == d && fa > df){
					fa = df;
				}
				else if(da <  d){
					da = d;
					fa = df;
				}
			}else if(t[i] == 1){
				if(db == d && fb > df){
					fb = df;
				}
				else if(db < d){
					db = d;
					fb = df;
				}
			}
		}
	}
	if(da == -1){cout << -1 << " " << -1 << endl;}
	else cout << fixed << setprecision(10) << sqrt(da) << " " << 1.*fa/60. << endl;
	if(db == -1){cout << -1 << " " << -1 << endl;}
	else cout << fixed << setprecision(10) << sqrt(db) << " " << fb/60. << endl;

	return 0;
}

Measured memory_bytes: 1280
#include<stdio.h>

int S(int T[],int n, int key);


int main(){
  int i,n,T[10000+1],q,key,sum =0;

  scanf("%d",&n);
  for(i=0;i<n;i++) scanf("%d",&T[i]);

  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%d",&key);
    if(S(T,n,key))sum++;
  }
  printf("%d\n",sum);

  return 0;
}


int S(int T[], int n, int key){
  int i = 0;
  T[n] = key;
  while(T[i] != key)i++;
  return i !=n;
}



Measured memory_bytes: 2048
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...)
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
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; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s; cin >> s;
    int ans = 0, now = 0;
    rep (i, 3) {
        if (s[i] == 'R') now++; else now = 0;
        chmax(ans, now);
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3648
x1, x2 = map(int, input().split())

l1 = [1,3,5,7,8,10,12]
l2 = [4,6,9,11]
l3 = [2]

if (x1 in l1) & (x2 in l1):
  print("Yes")
elif (x1 in l2) & (x2 in l2):
  print("Yes")
elif (x1 in l3) & (x2 in l3):
  print("Yes")
else:
  print("No")
  

Measured memory_bytes: 3060
import math
import itertools
n,m,q=map(int,input().split())
A=[0]*q
B=[0]*q
C=[0]*q
D=[0]*q
for i in range(q):
  A[i],B[i],C[i],D[i]=map(int,input().split())
l = [i for i in range(1,m+1)]
ans=0
for v in itertools.combinations_with_replacement(l,n):
  a=0
  for i in range(q):
    if v[B[i]-1]-v[A[i]-1]==C[i]:
      a=a+D[i]
  if a>ans:
    ans=a
print(ans)

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> pair_t;
using Graph = vector<vector<ll>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = 1 << 29;
/****** define constant end ********/
/****** define variable start ******/
ll n;
string s;
/******* define variable end *******/
int solve() {
	cin >> n >> s;
	ll ans = 0;
	vl r(n+1);
	vl g(n+1);
	vl b(n+1);
	r[0] = 0;
	g[0] = 0;
	b[0] = 0;
	REP(i, n){
		if(s.substr(i, 1) == "R"){
			r[i+1] = r[i] + 1;
			g[i+1] = g[i];
			b[i+1] = b[i];
		}else if(s.substr(i, 1) == "G"){
			r[i+1] = r[i];
			g[i+1] = g[i] + 1;
			b[i+1] = b[i];
		}else{
			r[i+1] = r[i];
			g[i+1] = g[i];
			b[i+1] = b[i] + 1;
		}
	}
	REP(i, n){
		for(ll j = i+1; j < n; j++){
			ll dist = j - i;
			if(s.substr(i, 1) != s.substr(j, 1)){
				if(s.substr(i, 1) == "R" && s.substr(j, 1) == "G"){
					ans += b[n] - b[j];
				}else if(s.substr(i, 1) == "G" && s.substr(j, 1) == "R"){
					ans += b[n] - b[j];
				}else if(s.substr(i, 1) == "R" && s.substr(j, 1) == "B"){
					ans += g[n] - g[j];
				}else if(s.substr(i, 1) == "B" && s.substr(j, 1) == "R"){
					ans += g[n] - g[j];
				}else{
					ans += r[n] - r[j];
				}
				if(j + dist < n){
					if(s.substr(j + dist, 1) != s.substr(i, 1) && s.substr(j + dist, 1) != s.substr(j, 1)){
						ans--;
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}


Measured memory_bytes: 3708
using namespace std;
#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(ll i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<n;i++)
#define ll long long
#define ld long double
#define pi 3.1415926535897932384626433832795028841971693993
#define ee 2.718281828459045
#define mod 1000000007
#define MOD mod
#define sec setprecision
using namespace std;
ll gcd(ll a, ll b) {
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
ll lcm(ll a, ll b)
{
	return a * b / gcd(a, b);
}
int ketasuu(ll h) {
	if (h < 0)return(-(ketasuu(-h)));
	else if (h < 10)return(1);
	else return(1 + ketasuu(h / 10));
}
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void cominit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (ll i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}
vector<ll> soinnsuubunnkai(ll n) {
	vector<ll>a;
	while (n % 2 == 0) {
		a.push_back(2);
		n /= 2;
	}
	ll f = 3;
	while (f * f <= n) {
		if (n % f == 0) {
			a.push_back(f);
			n /= f;
		}
		else f += 2;
	}
	if (n != 1)
		a.push_back(n);
	return(a);
}
// 二項係数計算
long long com(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
ll pow(ll a, ll b) {
	a = a % mod;
	if (b == 0)return(1);
	else if (b % 2 == 0)return(pow((a * a)%mod, b / 2));
	else return(a * (pow(a, b - 1)));
}
void solve() {
	
}
int main() {
	int n;
	cin >> n;
	vector<int>a(n);
	vector<int>b(9,0);
	rip(i, n) {
		cin >> a[i];
		int k = a[i] / 400;
		if (k <= 7)b[k]++;
		else b[8]++;
	}
	int count = 0;
	for (int i = 0; i <= 7; i++)if (b[i] != 0)count++;
	cout << max(count,1) <<" " << count + b[8] << endl;
}


Measured memory_bytes: 256
x = int(input())
nums = []
i = 0
while i ** 5 <= 10 ** 14:
    nums.append(i ** 5)
    i += 1

for i in range(len(nums)):
    a5 = nums[i]
    for j in range(len(nums)):
        b5 = nums[j]
        if x == a5 - b5:
            print(i, j)
            exit()
        if x == a5 + b5:
            print(i, -j)
            exit()


Measured memory_bytes: 63728
#include <stdio.h>

int main(void)
{
	long long int integer[10000], max = 0, min = 0, sum = 0;
	int i, j,n=0;

	scanf("%d", &n);

	for (i = 0; i < n; i++)
	{
		scanf("%lld", &integer[i]);

		sum = sum + integer[i];
	}

	max = integer[0];
	min = integer[0];

	for (j = 0; j < n - 1; j++)
	{
		if (max < integer[j + 1])
		{
			max = integer[j + 1];
		}

		if (min > integer[j + 1])
		{
			min = integer[j + 1];
		}
	}

	printf("%lld %lld %lld\n", min, max, sum);

	return 0;
}


Measured memory_bytes: 2112
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
	char s[1024], t[1024];
	
	scanf("%s", s);
	
	while (1){
		int len = strlen(s);
		bool update = false;
		for (int i = 1; i + 2 <= len - 2; i++){
			if (strncmp(&s[i], "mew", 3) == 0){
				update = true;
				int idx = 0;
				for (int j = 0; j < i; j++){
					t[idx++] = s[j];
				}
				for (int j = i + 3; j < len; j++){
					t[idx++] = s[j];
				}
				t[idx] = 0;
				break;
			}
		}
		if (!update){
			return (!printf("%s\n", strcmp(s, "mew") ? "Rabbit" : "Cat"));
		}
		strcpy(s, t);
	}
}

Measured memory_bytes: 976
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    vector<vector<char>> grid(H, vector<char>(W));
    rep(i, H) rep(j, W) cin >> grid[i][j];
    
    vector<vector<int>> dist(H, vector<int>(W, 0));
    queue<int> q;
    dist[0][0] = 1;
    q.push(0);
    
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    while (q.size() > 0) {
        int x = q.front() % W;
        int y = q.front() / W;
        q.pop();
        rep(i, 4) {
            int X = x + dx[i];
            int Y = y + dy[i];
            if (X < 0 || W <= X || Y < 0 || H <= Y) continue;
            if (grid[Y][X] == '#') continue;
            if (dist[Y][X] != 0) continue;
            dist[Y][X] = dist[y][x] + 1;
            q.push(Y * W + X);
        }
    }
    
    if (dist[H - 1][W - 1] == 0) {
        cout << -1 << endl;
        return 0;
    }
    int sum = 0;
    rep(i, H) rep(j, W) {
        if (grid[i][j] == '#') ++sum;
    }
    cout << H * W - sum - dist[H - 1][W - 1] << endl;
    return 0;
}


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

using namespace std;

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

bool check(vector<int>& h) {
  int i;
  REP(i,h.size()) if (h[i] != 0) return true;
  return false;
}

int main() {
  int i,N;
  vector<int> h;

  cin >> N;
  REP(i,N) {
    int x;
    cin >> x;
    h.emplace_back(x);
  }

  int res = 0;
  while (check(h)) {
    REP(i,N) {
      if (h[i] != 0) {
        int mn = (1<<29);
        for (int j = i; j < N && h[j] > 0; j++) mn = min(mn, h[j]);
        for (int j = i; j < N && h[j] > 0; j++) h[j] -= mn;
        res += mn;
        break;
      }
    }
  }

  cout << res << endl;

  return 0;
}


Measured memory_bytes: 256
#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
using namespace std;
typedef int64_t ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> vp;
typedef vector<PL> vpl;
typedef vector<vector<P>> vvp;
const int INF = 1001001001;
const ll LINF = 1e18;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define REP(i, n) for(ll i = 0; i < n; i++)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second

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

bool p_comp_fs(const P p1, const P p2){ return p1.first < p2.first;};
bool p_comp_fg(const P p1, const P p2){ return p1.first > p2.first;};
bool p_comp_ss(const P p1, const P p2){ return p1.second < p2.second;};
bool p_comp_sg(const P p1, const P p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}


int main(){
    ll a, b, q; cin >> a >> b >> q;
    vl s(a+1, 0), t(b+1, 0), x(q, 0); s[0] = -LINF; t[0] = -LINF;
    REP(i, a) cin >> s[i+1]; REP(i, b) cin >> t[i+1]; REP(i, q) cin >> x[i];
    s.push_back(LINF); t.push_back(LINF);
    REP(i, q){
        auto sit = lower_bound(s.begin(), s.end(), x[i]);
        auto si = distance(s.begin(), sit);
        auto tit = lower_bound(t.begin(), t.end(), x[i]);
        auto ti = distance(t.begin(), tit);
        ll ret = LINF;
        FOR(j, si-1, si+1){
            FOR(k, ti-1, ti+1){
                ll sp = s[j]; ll tp = t[k];
                ret = min(ret, abs(x[i]-sp) + abs(sp-tp));
                ret = min(ret, abs(x[i]-tp) + abs(sp-tp));
            }
        }
        cout << ret << endstr;
    }
    
    return 0;
}


Measured memory_bytes: 3680
#include <bits/stdc++.h>
#ifdef __mr__
    #include "prettyprint.hpp"
#else
    #define endl                    '\n'
#endif
#define len(x)                      (uint)(x).size()
#define int                         int32_t
#define uint                        uint32_t
#define ulong                       uint64_t
#define long                        int64_t
#define maxof(x)                    numeric_limits<x>::max()
#define minof(x)                    numeric_limits<x>::min()
#define uset                        unordered_set
#define umap                        unordered_map
using namespace std;
const uint mod7 = uint(1000000007);
const uint mod9 = uint(1000000009);
const int inf31 = maxof(int);
const uint inf32 = maxof(uint);
const long inf63 = maxof(long);
const ulong inf64 = maxof(ulong);


int main(int argc, char const *argv[]) {
    #ifndef __mr__
        ios::sync_with_stdio(0);cin.tie(0);
    #endif
    long x, y;
    cin >> x >> y;
    if (y == 0) {
        if (x < 0) {
            cout << y - x << endl;
        } else {
            cout << x - y + 1 << endl;
        }
    } else if (x == 0) {
        if (y < 0) {
            cout << x - y + 1 << endl;
        } else {
            cout << y - x << endl;
        }
    } else if (y >= x) {
        if (x >= 0) {
            cout << y - x << endl;
        } else if (y < 0) {
            cout << y - x << endl;
        } else if (y >= -x) {
            cout << y + x + 1 << endl;
        } else {
            cout << -y - x + 1 << endl;
        }
    } else {
        if (y >= 0) {
            cout << min(x + y + 1, x - y + 2) << endl;
        } else if (x <= 0) {
            cout <<  min(-x + -y + 1, x - y + 2) << endl;
        } else if (x >= -y) {
            cout << y + x + 1 << endl;
        } else {
            cout << -y - x + 1 << endl;
        }
    }
    return 0;
}




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

const int sz = 200005;

int a[sz], b[sz], n;

int main(){
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  reverse(b, b + n);
  for(int i = 0; i < n;i++){
    if(a[i] == b[0]) cout << b[1] << endl;
    else cout << b[0] << endl;
  }
}


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

typedef ostringstream OSS; typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;  typedef pair<LL, LL> PLL;
typedef vector<int>    VI;   typedef vector<VI>   VVI;   typedef vector<VVI>   VVVI;
typedef vector<LL>     VLL;  typedef vector<VLL>  VVLL;  typedef vector<VVLL>  VVVLL;
typedef vector<double> VD;   typedef vector<VD>   VVD;   typedef vector<VVD>   VVVD;
typedef vector<string> VS;   typedef vector<VS>   VVS;   typedef vector<VVS>   VVVS;
typedef vector<bool>   VB;   typedef vector<VB>   VVB;   typedef vector<VVB>   VVVB;
typedef vector<PII>    VPII; typedef vector<VPII> VVPII; typedef vector<VVPII> VVVPII;
typedef vector<PLL>    VPLL; typedef vector<VPLL> VVPLL; typedef vector<VVPLL> VVVPLL;

typedef unsigned int  UI;
typedef vector<UI>    VUI;
typedef vector<VUI>   VVUI;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x)  (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define RANGEBOX(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define RANGE(x, l, r) ((l) <= (x) && (x) <= (r))
#define rep(i, N)  for (int i = 0; i < (int)(N); i++)
#define rrep(i, N) for (int i = N - 1; i >= 0; i--)
#define REP(i, init, N)  for (int i = (init); i < (int)(N); i++)
#define RREP(i, N, last) for (int i = (init - 1); i >= last; i--)
#define MAXUD(orig, target) orig = max(orig, target)
#define MINUD(orig, target) orig = min(orig, target)
#define DUMP(x) cerr << #x << " = " << (x) << endl

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a)   { OSS oss; oss << a; return oss.str(); };

template<typename T=int> inline void dump(vector<T> vs, bool ent=false) { rep(i, vs.size()) cout << vs[i] << (i+1==vs.size() ? '\n' : ' '); if (ent) cout << endl; }
template<typename T = int> inline void dump(vector<vector<T>> vs, bool ent=false) { rep(i, vs.size()) dump<T>(vs[i]); if (ent) cout << endl; }

const int    INF  = 0x3f3f3f3f;
const LL     INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int    DX[] = {1,  0, -1, 0};
const int    DY[] = {0, -1,  0, 1};
const double EPS  = 1e-12;
// const double PI   = acos(-1.0);

template<typename T = int> inline T in() { T x; cin >> x; return x; }
template<typename T = int> inline vector<T> in(int n) { vector<T> xs(n); rep(i, n) cin >> xs[i]; return xs; }
template<typename T = int> inline vector<vector<T>> in(int n, int m) { vector<vector<T>> xs(n, vector<T>(m)); rep(i, n) rep(j, m) cin >> xs[i][j]; return xs; }

int main(void) {
    VI xs = in(3);
    sort(ALL(xs));
    cout << (xs[0] + xs[1] == xs[2] ? "Yes" : "No") << endl;

    return 0;
}



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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const ll mod = 1000000007;

inline ll modPow(ll a, ll b){
	ll ret = 1;
	while(b){
		if(b&1)(ret *= a) %= mod;
		(a *= a) %= mod;
		b >>= 1;
	}
	return ret;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout<<setprecision(32);

	string s;
	cin>>s;
	int n = s.size();
	vector<int> a(n), c(n), qf(n), qb(n);
	for(int i = 0; i < n; i++){
		if(s[i] == 'A')a[i] = 1;
		if(s[i] == '?')qf[i] = 1;
		if(i){
			a[i] += a[i - 1];
			qf[i] += qf[i - 1];
		}
	}
	for(int i = n - 1; i >= 0; i--){
		if(s[i] == 'C')c[i] = 1;
		if(s[i] == '?')qb[i] = 1;
		if(i + 1 < n){
			c[i] += c[i + 1];
			qb[i] += qb[i + 1];
		}
	}
	ll ans = 0;
	for(int i = 1; i + 1 < n; i++){
		if(s[i] == 'B' || s[i] == '?'){
			ll tmp1 = a[i - 1];
			if(qf[i - 1]){
				tmp1 = (qf[i - 1] + 3*a[i - 1])*modPow(3, qf[i - 1] - 1)%mod;
			}
			ll tmp2 = c[i + 1];
			if(qb[i + 1]){
				tmp2 = (qb[i + 1] + 3*c[i + 1])*modPow(3, qb[i + 1] - 1)%mod;
			}
			ans += tmp1*tmp2%mod;
			if(ans >= mod)ans -= mod;
		}
	}
	cout<<ans<<'\n';

	return 0;
}

Measured memory_bytes: 2000
from collections import deque

# print('input >>')
S = deque(input())
Q = int(input())
queries = []
for _ in range(Q):
    queries.append(input())

is_reverse = False

for q in queries:
    ql = list(q.split())
    if len(ql) == 1:
        is_reverse = not is_reverse
    else:
        if ql[1] == '1': # 先頭に加える
            if not is_reverse:
                S.appendleft(ql[2])
            else:
                S.append(ql[2])
        else: # 末尾に加える
            if not is_reverse:
                S.append(ql[2])
            else:
                S.appendleft(ql[2])

# print('-----output-----')
if not is_reverse:
    print(''.join(list(S)))
else:
    print(''.join(list(S)[::-1]))

Measured memory_bytes: 96088
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include "bits/stdc++.h"

#define REP(i,x,y) for(int i=(x);i<(y);i++)
#define RREP(i,x,y) for(int i=(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
//#define int long long

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

#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

typedef long long ll;

const ll MOD = 1e9 + 7;

class EGluttony {
public:
	void solve(std::istream& in, std::ostream& out) {
	    ll N, K;
	    in >> N >> K;
	    vector<ll> A(N), F(N);
	    REP(i,0,N) in >> A[i];
	    REP(i,0,N) in >> F[i];

	    sort(all(A));
	    sort(all(F), greater<ll>());

	    ll left = 0, right = (ll)1e12 + 1;
	    while (left + 1 < right) {
	        ll middle = (left + right) / 2 - 1;
	        ll cost = K;
	        bool possible = true;
	        REP(i,0,N) {
	            ll aGoal = middle / F[i];
	            if (aGoal >= A[i]) continue;
	            if (A[i] - aGoal <= cost) {
	                cost -= (A[i] - aGoal);
	            } else {
	                possible = false;
	                break;
	            }
	        }
	        if (possible) {
	            right = middle+1;
	        } else {
	            left = middle+1;
	        }
	    }
	    out << left << "\n";
	}
};


signed main() {
	EGluttony solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323846;
long long ans = 0;
int n;
string s;

void solve(int pos, long long sum) {
    if (pos == n) {
        ans += sum;
        return;
    }
    long long el = 0;
    for (int i = pos; i < n; ++i) {
        el *= 10; el += (s[i] - '0');
        solve(i + 1, sum + el);
    }
    return;
}

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

    cin >> s;
    n = s.size();
    solve(0, 0);
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256