akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w))
#define dump(...)
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct{template<class T> operator T(){T x;cin>>x;return x;}} IN;
struct before_main_function {
    before_main_function() {
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
    int n, m;
    cin >> n >> m;
    vector<int> p(n);
    cin >> p;
    vector<vector<int>> t(n);
    REP(i, m) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        t[x].push_back(y);
        t[y].push_back(x);
    }
    
    vector<bool> vis(n, false);
    int ans = 0;
    REP(i, n) {
        if (vis[i]) continue;
        vector<bool> g(n, false);
        set<int> q, qp;
        function<void(int)> dfs = [&](int v) {
            vis[v] = 1;
            q.insert(v);
            qp.insert(p[v]);
            FORE(w, t[v]) {
                if (!vis[w]) dfs(w);
            }
        };
        dfs(i);
        FORE(idx, q) {
            if (CONTAIN(qp, idx + 1)) {
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 18944
#include <cmath>
#include <iomanip>
#include <vector>
#include <iostream>
using namespace std;

const double EPS=1e-10;
const double INF=1e10;

// 点オブジェクト
// ベクトルを表すのにも使う
struct Point {
    double x, y;
    Point(double x=0, double y=0) : x(x), y(y) {}
    double distance (const Point &o) const {
        return sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y));
    }

    Point operator+(const Point &o) const {
        return Point(x+o.x, y+o.y);
    }

    Point operator-(const Point &o) const {
        return Point(x-o.x, y-o.y);
    }

    Point operator*(const double m) const {
        return Point(x*m, y*m);
    }

    Point operator/(const double d) const {
        return Point(x/d, y/d);
    }

    // 外積
    double cross(const Point &o) const {
        return x * o.y - y * o.x;
    }

    // 内積
    double dot(const Point &o) const {
        return x * o.x + y * o.y;
    }

    // ベクトルがx軸となす角
    double atan() const {
        return atan2(y, x);
    }

    // ベクトルの長さの二乗
    double norm() const {
        return dot(*this);
    }
};

// 点の進行方向
// 戻り値:
//     +1: 反時計回り
//     -1: 時計回り
//     +2: c, a, bの順に一直線上
//     -2: a, b, cの順に一直線上
//      0: a, c, bの順に一直線上
int ccw(Point a, Point b, Point c) {
    b = b-a, c = c-a;
    if (b.cross(c) > EPS) return +1; 
    if (b.cross(c) < -EPS) return -1;
    if (b.dot(c) < 0) return +2;
    if (b.norm() < c.norm()) return -2;
    return 0;
}

// 多角形
struct Polygon {
    vector<Point> vertex;

    Polygon() {}
    Polygon(vector<Point> vertex) : vertex(vertex) {}

    inline void add_vertex(Point v) {
        vertex.push_back(v);
    }

    Point next(int v) const {
        return vertex[(v+1) % vertex.size()];
    }
    Point prev(int v) const {
        return vertex[(v-1+vertex.size()) % vertex.size()];
    }

    double area() const {
        double ret = 0;
        for (int i = 0; i < vertex.size(); i++) {
            ret += vertex[i].cross(next(i));
        }
        return abs(ret) * 0.5;
    }
};

int main() {
    int curset = 1;
    int n;
    while (cin >> n, n != 0) {
        Polygon polygon;
        Point pt;
        for (int i = 0; i < n; i++) {
            cin >> pt.x >> pt.y;
            polygon.add_vertex(pt);
        }
        cout << setprecision(1);
        cout << fixed;
        cout << curset++ << ' ' << polygon.area() << endl;
    }
    return 0;
}

Measured memory_bytes: 0
import java.util.*;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        BigInteger a = new BigInteger(sc.next());
        BigInteger b = new BigInteger(sc.next());
        if (a.compareTo(b) > 0) {
            System.out.println("GREATER");
        } else if (a.compareTo(b) < 0) {
            System.out.println("LESS");
        } else {
            System.out.println("EQUAL");
        }
    }
}


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

int main(){
  int r,D,x;
  cin >> r >> D >> x;
  int64_t ans=x;
  for(int i=0;i<10;i++){
    ans=ans*r-D;
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 3608
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace A
{
    public class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var x = int.Parse(Console.ReadLine());
            Console.WriteLine(x == 7 || x == 5 || x == 3 ? "YES" : "NO");
        }
    }
}


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

int main()
{
    int n[4],a = 0,b = 0,c = 0,d = 0;
    for(int i = 0;i < 4;i++){
        cin >> n[i];
    }

    for(int i = 0;i < 4;i++){
        if(n[i] == 1){
            a++;
        }
        else if(n[i] == 9){
            b++;
        }
        else if(n[i] == 7){
            c++;
        }
        else if(n[i] == 4){
            d++;
        }
    }

    if (a == 1 && b == 1 &&c == 1 && d == 1){
        cout << "YES\n";
    }
    else {
        cout << "NO\n";
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
 
int rt[200000];
 
int main(){
    ios::sync_with_stdio(false);
    int n;
    int i , j;
    int max,min;
    cin>>n;
    max=0x80000000;
    for(i=0 ; i<n ; i++){
        cin>>rt[i];
    }
    min=rt[0];
    for(i=1 ; i<n ; i++){
        if((rt[i]-min)>max){
            max=rt[i]-min;
        }
        if((rt[i])<min){
            min=rt[i];
        }
    }
    cout << max << endl;
    return 0;
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main(){
    int n, d;
    cin >> n >> d;
    int ans = ceil(n, d*2+1);
    cout << ans << endl;
}

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

li n;
vector<vector<li>> g;

li grundy(li pos, li parent) {
    li res = 0;
    for (li next : g[pos]) {
        if (next == parent) {
            continue;
        }
        res ^= grundy(next, pos) + 1;
    }
    return res;
}

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

    cin >> n;
    g = vector<vector<li>>(n);
    for (int i = 0; i < n - 1; ++i) {
        li x, y;
        cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    cout << (grundy(0, -1) == 0 ? "Bob" : "Alice") << endl;
    return 0;
}

Measured memory_bytes: 10368
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
cum_sum = [0] * (n+1)
for i in range(n):
    cum_sum[i+1] = cum_sum[i] + A[i]
C = Counter(cum_sum[1:])
ans = C[0]
for k, v in C.items():
    ans += (v*(v-1)//2)
print(ans)


Measured memory_bytes: 43336
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;

/*
*/

bool ok=true;
string s;
ll a[30]={},cnt=0;

void init(){
	cin>>s;
}

int main(void){
	init();
	rep(i,s.size())a[s[i]-'A']++;
	rep(i,26){
		if(a[i]==0){
			//
		}else if(a[i]!=2){
			ok=false;
			break;
		}else{
			cnt++;
		}
	}
	if(cnt!=2)ok=false;
	cout<<(ok ? "Yes" : "No")<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	int time;
	char word;
	while(cin>>time&&time!=0){
		char henkan[128]={0};
		char hena,henb;
		for(int i=0;i<time;++i){
			cin>>hena>>henb;
			henkan[hena]=henb;
		}
		cin>>time;
		for(int i=0;i<time;++i){
			cin>>word;
			if(henkan[word]!=0){
				cout<<(char)henkan[word];
			}else{
				cout<<word;
			}
		}
		cout<<endl;
	}
	return 0;
}

Measured memory_bytes: 3072
#!/usr/bin/env python3
"""
Simple Version
Python TLE https://atcoder.jp/contests/dp/submissions/14906600
PyPy TLE https://atcoder.jp/contests/dp/submissions/14906630
"""
from collections import defaultdict
import sys

sys.setrecursionlimit(10**6)


def solve(N, M, edges):
    longest = {}

    stack = [v for v in edges]

    while stack:
        v = stack.pop()
        if v > 0:
            if v in longest:
                continue
            next_edges = edges.get(v)
            stack.append(-v)
            if next_edges:
                stack.extend(next_edges)
        else:
            next_edges = edges.get(-v)
            if not next_edges:
                ret = 0
            else:
                ret = max(longest[x] for x in next_edges) + 1
            longest[-v] = ret

    return max(longest[v] for v in edges)


def main():
    N, M = map(int, input().split())
    edges = defaultdict(set)
    for i in range(M):
        v1, v2 = map(int, input().split())
        edges[v1].add(v2)

    print(solve(N, M, edges))


T1 = """
4 5
1 2
1 3
3 2
2 4
3 4
"""

T2 = """
6 3
2 3
4 5
5 6
"""

T3 = """
5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3
"""


def _test():
    """
    # >>> as_input(T1)
    # >>> main()
    # 3

    # >>> as_input(T2)
    # >>> main()
    # 2

    >>> as_input(T3)
    >>> main()
    3
    """
    import doctest
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()


Measured memory_bytes: 61120
s = input()

if s[0:3] == 'KIH':
    s = 'A'+s
elif s[0:4] != 'AKIH':
    print('NO')
    exit(0)
# print(s)

if s[4:5] == 'B':
    s = s[0:4]+'A'+s[4:]
elif s[4:6] != 'AB':
    print('NO')
    exit(0)
# print(s)

if s[5:7] == 'BR':
    s = s[0:6]+'A'+s[6:]
elif s[5:7] != 'BA':
    print('NO')
    exit(0)

# print(s)
# print(s[7:8])
# print(s[7:9])
# print(len(s))
if (s[7:9] == 'RA' and len(s) == 9) or (s[7:8] == 'R' and len(s) == 8):
    print('YES')
else:
    print('NO')


Measured memory_bytes: 3064
N = int(input())
x = [0] * N
u = [0] * N
ans = 0

for i in range(N):
    x[i], u[i] = map(str, input().split())
    x[i] = float(x[i])

    if u[i] == 'BTC':
        ans += x[i] * 380000
    else:
        ans += x[i]

print(ans)


Measured memory_bytes: 3060
#include<iostream>
#include <climits>
#include<vector>
#include<numeric>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string s;
	getline(cin, s);
	vector<string> t;
	t.push_back("");
	REP(i, (int)s.size()) {
		if (s[i] == ' ') t.push_back("");
		else t[t.size()-1].push_back(s[i]);
	}
	stack<int> st;
	st.push(stoi(t[0]));
	st.push(stoi(t[1]));
	FOR(i, 2, (int)t.size()) {
		if (t[i] == "+") {
			int tmp = st.top();
			st.pop();
			tmp += st.top();
			st.pop();
			st.push(tmp);
		}
		else if (t[i] == "-") {
			int tmp = st.top();
			st.pop();
			tmp -= st.top();
			st.pop();
			st.push(-tmp);
		}
		else if (t[i] == "*") {
			int tmp = st.top();
			st.pop();
			tmp *= st.top();
			st.pop();
			st.push(tmp);
		}
		else st.push(stoi(t[i]));
	}
	cout << st.top() << "\n";

	return 0;
}


Measured memory_bytes: 3228
function prime(max){
   var arr=[];
   for(var i=0;i<=max;i++)arr[i]=i;
   arr[0]=false;
   arr[1]=false;
   var sqrt=Math.floor(Math.sqrt(max));
   for(var i=2;i<=sqrt;i++){
      if(arr[i]==false)continue;
      for(var j=i+i;j<=max;j+=i){
         arr[j]=false;
      }
   }
   var result=[];
   for(var i=0;i<=max;i++){
      if(arr[i]!==false)result.push(arr[i]);
   }
   return result;
}
var p=prime(999999);
 
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr=(input.trim()).split("\n").map(Number);
for(var i=0;i<Arr.length;i++){
   var a=Arr[i];
   for(var j=0;j<p.length;j++){
      if(p[j]==a){console.log(p[j-1]+" "+p[j+1]);break;}
      if(p[j]>a){console.log(p[j-1]+" "+p[j]);break;}
   }
}

Measured memory_bytes: 28868
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n" 
#define p pair<int, int>
#define arrinp(i, a, start, end); for(ll i = start; i <= end; i++) cin >> a[i];
#define arrout(i, a, start, end); for(ll i = start; i <= end; i++) cout << a[i] << " ";
#define swap(a, b) {a = a + b; b = a - b; a = a - b;
#define forfront(i, start, end) for(ll i = start; i <= end; i++)
#define forback(i, start, end) for(ll i = start; i >= end; i--)
#define out(x) cout << x << " ";
#define outline(x) cout << x << endl;
#define sortvector(v) sort(v.begin(), v.end());
#define sortvectorrev(v) sort(v.rbegin(), v.rend());
int main()
{
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
        int n;
        cin >> n;
        int amt = 0;
        if(n%1000 != 0)
            outline(1000 - n%1000)
        else
        {
            outline(amt)
        }
    return 0;
}

Measured memory_bytes: 3652
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()

int main() {
    ll N;
    cin>>N;
    VL P(N);
    ll flg=0;

    rep(i,0,N){
        cin>>P[i];
        if(i+1!=P[i])flg++;    
    }
    
    cout<<(flg<=2 ? "YES":"NO")<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int[][] array = new int[m][2];
        for (int i = 0; i < m; i++) {
            array[i][0] = sc.nextInt();
            array[i][1] = sc.nextInt();
        }
        Arrays.sort(array, (a, b) -> Integer.compare(a[0], b[0]));
        
        int nowl = -1;
        int nowr = 1000000;
        
        int count = 1;
        for (int i = 0; i < m; i++) {
            if (count == n - 1) break;
            int l = array[i][0];
            int r = array[i][1];
            
            if (r <= nowl || nowr <= l) {
                count++;
                nowl = l;
                nowr = r;
                continue;
            }
            
            if (nowl < l) {
                nowl = l;
            }
            if (r < nowr) {
                nowr = r;
            }
        }
        
        System.out.println(count);
    }
}


Measured memory_bytes: 91276
n=int(input())
a=[int(_) for _ in input().split()]
lim=10**6+7
a.sort()
dp=[True for i in range(lim+1)]
ans=0
for i in range(n):
    if dp[a[i]]:
        if i==n-1 or a[i]!=a[i+1]:
            ans+=1
        for j in range(lim//a[i]+1):
            dp[j*a[i]]=False

print(ans)


Measured memory_bytes: 32144
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);		
        String str = sc.next(); 
        String s = "Yes";
        for(int i=0;i<str.length();i++){
          if((i%2==0 && str.charAt(i)== 'L') || (i%2==1 && str.charAt(i)=='R')){s = "No";break;}
                            }		    
		System.out.println(s);
	}
}


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

int main() {
  int n, dim; cin >> n >> dim;
  vector<vector<int>> x(n, vector<int>(dim));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < dim; j++) {
      cin >> x.at(i).at(j);
    }
  }
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int sum_of_square = 0;
      for (int d = 0; d < dim; d++) {
        sum_of_square += pow(x.at(i).at(d) - x.at(j).at(d), 2);
      }
      if (sum_of_square == pow(int(sqrt(sum_of_square)), 2)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int ijk[110][110][32];
int v[110][110][32];
int t[110][110];
int ch[110][110];
char str[110][110];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int ABS(int a){return max(a,-a);}
int main(){
	int a,b;scanf("%d%d",&b,&a);
	for(int i=0;i<a;i++)scanf("%s",str[i]);
	int sz=0;
	int sr,sc,gr,gc;
	for(int i=0;i<a;i++)for(int j=0;j<b;j++)ch[i][j]=-1;
	for(int i=0;i<a;i++)for(int j=0;j<b;j++){
		t[i][j]=max(t[i][j],1);
		if(str[i][j]=='M')ch[i][j]=sz++;
		if(str[i][j]=='S'){
			sr=i;sc=j;
		}
		if(str[i][j]=='G'){
			gr=i;gc=j;
		}
		if(str[i][j]=='#'){
			for(int k=0;k<a;k++)for(int l=0;l<b;l++){
				t[k][l]=max(t[k][l],4-max(ABS(k-i),ABS(l-j)));
			}
		}
	}
	for(int i=0;i<a;i++)for(int j=0;j<b;j++)for(int k=0;k<(1<<sz);k++){
		ijk[i][j][k]=999999999;
	}
	ijk[sr][sc][0]=0;
	priority_queue<pair<pair<int,int>,pair<int,int> > >Q;
	Q.push(make_pair(make_pair(0,0),make_pair(sr,sc)));
	while(Q.size()){
		int cost=-Q.top().first.first;
		int bit=Q.top().first.second;
		int row=Q.top().second.first;
		int col=Q.top().second.second;
		Q.pop();
		if(v[row][col][bit])continue;
		v[row][col][bit]=1;
		for(int i=0;i<4;i++){
			if(row+dx[i]<0||row+dx[i]>=a||col+dy[i]<0||col+dy[i]>=b||str[row+dx[i]][col+dy[i]]=='#')continue;
			int tb=bit;
			if(~ch[row+dx[i]][col+dy[i]])tb|=(1<<ch[row+dx[i]][col+dy[i]]);
			if(!v[row+dx[i]][col+dy[i]][tb]&&ijk[row+dx[i]][col+dy[i]][tb]>cost+t[row][col]){
				ijk[row+dx[i]][col+dy[i]][tb]=cost+t[row][col];
				Q.push(make_pair(make_pair(-cost-t[row][col],tb),make_pair(row+dx[i],col+dy[i])));
			}
		}
	}
	printf("%d\n",ijk[gr][gc][(1<<sz)-1]);
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

char s[125];

int main(){
  scanf("%s",s);
  int n = strlen(s);
  int ans = 0;
  REP(l,n)FOR(r,l+1,n+1){
    bool ok = true;
    FOR(i,l,r){
      if(!(s[i]=='A' || s[i]=='T' || s[i]=='G' || s[i]=='C'))ok=false;
    }
    if(ok)CHMAX(ans,r-l);
  }
  printf("%d\n",ans);
  return 0;
}


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

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;
ll dp[10001][101][2];
int D;
string k;

ll solve(int i, int sum, int f)
{
	if (i == k.size())
		return sum % D == 0;

	if (dp[i][sum % D][f] != -1)
		return dp[i][sum % D][f];

	ll ans = 0;
	for (int d = 0; d <= (f ? k[i] - '0' : 9); d++)
		ans = (ans + solve(i + 1, sum + d, f && d == (k[i] - '0'))) % MOD;
	return dp[i][sum % D][f] = ans;
}

int main()
{
	cin >> k >> D;
	memset(dp, -1, sizeof(dp));
	cout << (solve(0, 0, 1) - 1 + MOD) % MOD << endl;
	return 0;
}


Measured memory_bytes: 16896
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int count_substr(const string &s, const string &t)
{
	int c = 0;
	string::size_type offset = 0, pos;
	while ((pos = s.find(t, offset)) != string::npos) {
		++c;
		offset = pos + t.length();
	}

	return c;
}

int main()
{
	vector<string> g[2][2];
	int N;
	cin >> N;

	int ans = 0;
	for (int i = 0; i < N; ++i) {
		string s;
		cin >> s;
		g[s.front() == 'B'][s.back() == 'A'].push_back(s);
	}
	
	string t;
	if (!g[0][1].empty()) {
		t += g[0][1].back();
		g[0][1].pop_back();
	}
	for (auto &x : g[1][1]) {
		t += x;
	}
	if (!g[1][0].empty()) {
		t += g[1][0].back();
		g[1][0].pop_back();
	}
	for (auto &x : g[0][0]) {
		t += x;
	}

	for (int i = 0; i < g[0][1].size() || i < g[1][0].size(); ++i) {
		if (i < g[0][1].size()) {
			t += g[0][1][i];
		}
		if (i < g[1][0].size()) {
			t += g[1][0][i];
		}
	}

	ans = count_substr(t, "AB");
	cout << ans << endl;
}


Measured memory_bytes: 896
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof a);
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define per(i,a,b) for(int i = a; i >= b; i--)
#define __ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef double db;
const db EPS = 1e-9;
using namespace std;

void dbg() {cout << "\n";}
template<typename T, typename... A> void dbg(T a, A... x) {cout << a << ' '; dbg(x...);}
#define logs(x...) {cout << #x << " -> "; dbg(x);}

ll a,b;

int main()
{
	scanf("%lld%lld",&a,&b);
	if(a <= 0 && b >= 0) printf("Zero\n");
	else{
		ll cnt = 0;
		if(a < 0) cnt += -a;
		if(b < 0) cnt += (b+1ll);
		if(cnt%2 != 0) printf("Negative\n");
		else printf("Positive\n");
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <cstdio>
#include <cmath>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using P1 = pair<int, P>;
#define FOR(t, a, b) for (int t = (a); t < (b); ++t)
#define REP(t, n) FOR(t, 0, n)

int main()
{
    int n;
    cin >> n;
    ll ans = 0;
    for (int i = 0; i < n; i++)
    {
        ll temp;
        cin >> temp;
        while (temp % 2 == 0)
        {
            ans++;
            temp = temp / 2;
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
	long long i,a,b,j,c,d=0;
	scanf("%lld%lld%lld",&a,&b,&c);
	for(i=1;i<=a;i++)
		if(i%10+i/10%10+i/100%10+i/1000%10+i/10000%10>=b&&i%10+i/10%10+i/100%10+i/1000%10+i/10000%10<=c)d+=i;
	printf("%lld\n",d);
	return 0;
}

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

const int DIM = 200005;
const int MOD = 1000000007;

int psm[DIM];

int logPower (int x, int n)
{
	int y = 1;
	for (; n; n >>= 1) {
		if (n & 1) 
			y = 1LL * y * x % MOD;
		x = 1LL * x * x % MOD;
	}
	return y;
}

int main (void)
{
#ifdef HOME
	freopen("blocks.in", "r", stdin);
	freopen("blocks.out", "w", stdout);
#endif
	int N; cin >> N;
	for (int i = 1; i <= N; ++i) 
		psm[i] = (psm[i - 1] + logPower(i, MOD - 2)) % MOD;
	int ans = 0;
	for (int i = 1; i <= N; ++i) {
		int x; cin >> x;
		ans = (1LL * (psm[i] + psm[N - i + 1] - 1) * x + ans) % MOD;
	}
	for (int i = 1; i <= N; ++i) 
		ans = 1LL * ans * i % MOD; 
	cout << ans;
	return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MOD 1000000007
#define fi first
#define se second
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repe(i,n) for(int i = 0 ; i <= n ; i++)
#define repb(i,a,b) for(int i = a ; i < b ; i++)
#define repeb(i,a,b) for(int i = a ; i <= b ; i++)
#define rfor(i,n,a) for(int i = n ; i >= a ; i--)
#define pb push_back
#define popb pop_back()
#define endl "\n"
#define pii pair<int,int> 
#define pll pair < long long, long long >
typedef priority_queue<ll, vector<ll>, greater<ll>> minheap;
typedef priority_queue<ll> maxheap;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a,b) cout << a << " " << b << endl
#define p3(a,b,c) cout << a << " " << b << " " << c << endl
#define p4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl
#define sortv(v) sort(v.begin(),v.end())
#define rsortv(v) sort(v.begin(),v.end(), greater<>());
#define sortby(v,prop) sort( v.begin( ), v.end( ), [ ]( const auto& lhs, const auto& rhs ){ return lhs.prop < rhs.prop; });
#define rsortby(v,prop) sort( v.begin( ), v.end( ), [ ]( const auto& lhs, const auto& rhs ){ return lhs.prop > rhs.prop; });

ll modPower(ll num,ll r){
	if(r==0) return 1;
	if(r==1) return num%MOD;
	ll ans=modPower(num,r/2)%MOD;
	if(r%2==0) {
		return (ans*ans)%MOD;
	} return (((ans*ans)%MOD)*num)%MOD;
}

ll nCr(ll n, ll r) { 
    ll res = 1; 
    if (r > n - r) {
        r = n - r; 
    }
    rep(i,r) { 
        res *= (n - i); 
        res /= (i + 1); 
    } 
    return res; 
}

bool isPrime(long long n)
{
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
 
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
 
    return true;
}


/*-------------------------------------------------*/

void solve() {

	char c;
	cin >> c;
	if(c >= 'a') {
		p1('a');
	} else {
		p1('A');
	}

}


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

Measured memory_bytes: 3632
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=1LL<<60;
ll a[200005],b[200005],s[3][200005],L[200005],R[200005],t[200005];
ll read() {
	char c=getchar(); ll x=0,f=1;
	while (c<'0' || c>'9') {
		if (c=='-') f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9') {
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
int main() {
	ll n=read();
	for (ll i=1; i<=n; i++) {
		a[i]=read();
		if (a[i]>0) b[i]=(a[i]+1)%2+1;
		else b[i]=0;
	}
	for (ll i=1; i<=n; i++) s[0][i]=s[0][i-1]+a[i];
	for (ll i=1; i<=n; i++) s[1][i]=s[1][i-1]+(b[i]!=1);
	for (ll i=1; i<=n; i++) s[2][i]=s[2][i-1]+2-b[i];
	ll mi=0;
	for (ll i=1; i<=n; i++) {
		mi=min(mi,s[0][i-1]-s[2][i-1]);
		L[i]=s[2][i-1]+mi;
	}
	mi=0;
	for (ll i=n; i>=0; i--) {
		mi=min(mi,(s[0][n]-s[0][i])-(s[2][n]-s[2][i]));
		R[i]=s[2][n]-s[2][i]+mi;
	}
	t[n+1]=inf;
	for (ll i=n; i>=0; i--) t[i]=min(t[i+1],R[i]+s[1][i]);
	ll ans=inf;
	for (ll l=1; l<=n; l++) ans=min(ans,L[l]-s[1][l-1]+t[l-1]);
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 12800
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
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; }
const long long INF = 1LL << 60;
long long dp[100010];
long long h[100010];

int main()
{
    int n, a; cin >> n;
    map<ll, ll> ma;
    Rep(i, 1, n + 1)
    {
        cin >> a;
        ma[a]++;
    }

    ll ans = 0;
    for (auto &x : ma)
    {
        if (x.second % 2 != 0)
            ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 6528
/// referenced: https://beta.atcoder.jp/contests/abc108/submissions/3146147
#include<bits/stdc++.h>
using namespace std;
void print(int u,int v,int w){
    printf("%d %d %d\n",u,v,w);
}
int L;
main(){
  cin>>L;
  int n=1,r=0;
  while(2*n<=L){
    n*=2;
    ++r;
  }
  int N=r+1;
  int M=2*r+__builtin_popcount(L-n);
  printf("%d %d\n",N,M);
  for(int i=0;i<r;++i){
    print(i+1,i+2,0);
    print(i+1,i+2,1<<i);
  }
  for(int t=r-1;L>n;--t){
    if(L-(1<<t)>=n){
      L-=1<<t;
      print(t+1,N,L);
    }
  }
}


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

int main() {
  int N;
  cin >> N;
  int x = 0;
  for (int i = 0; i < N; i++){
    int l, r;
    cin >> l >> r;
    x = x + r - l + 1;
    if (i == N - 1){
      cout << x << endl;
    }
  }
}

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

using namespace std;

int n, m, ok[100009], ap[100009], t[100009];
vector < int > v[100009], ord;

void dfs (int nod, int tata)
{
    int i;
    ap[nod] = 2;
    for (i=0; i<v[nod].size(); i++)
        if (ap[v[nod][i]] == 0)
        {
            t[v[nod][i]] = nod;
            dfs (v[nod][i], nod);
        }
        else
            if (v[nod][i] != tata && ap[v[nod][i]] == 2)
            {
                cout<<v[nod][i]<<" "<<nod<<'\n';
                if(ok[v[nod][i]])
                    ok[v[nod][i]]=0;
                else ok[v[nod][i]]=1;
            }
    ap[nod] = 1;
    ord.push_back(nod);
}

int main ()
{
    int x,y,i;
    cin>>n>>m;
    if (m%2==1)
        cout<<-1<<'\n';
    else
    {
        for (i=1; i<=m; i++)
        {
            cin>>x>>y;
            v[x].push_back (y);
            v[y].push_back (x);
        }
        dfs (1, -1);
        for (i=0; i<ord.size(); i++)
            if (ord[i] != 1)
            {
                if (ok[ord[i]])
                    cout<<ord[i]<<" "<<t[ord[i]]<<'\n';
                else
                    {cout<<t[ord[i]]<<" "<<ord[i]<<'\n';
                    if (ok[t[ord[i]]])
                         ok[t[ord[i]]]=0;
                    else ok[t[ord[i]]]=1;
                    }
            }
    }
    return 0;
}


Measured memory_bytes: 16376
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
const int INF = 1000010;
int main(){
    int k,n; cin >> k >> n;
    int Min=-1;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    rep(i,n){
        if(i==n-1){
            //Min = max(Min,abs(a[0]-a[n-1]));
            Min = max(Min,k-abs(a[0]-a[i]));
        }
        else{
            Min = max(Min,abs(a[i+1]-a[i]));
        //Min = max(Min,k-abs(a[i+1]-a[i]));
        }
    }
        cout << k-Min << endl;
    return 0;
}

Measured memory_bytes: 1152
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define SORTD(a) sort(a.rbegin(), a.rend());
#define UNIQUE(a) unique(a.begin(), a.end())
#define ll long long
#define INF 1000000000000000000
#define INT_MAX 2147483647

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>
#include <numeric>
#include <algorithm>
#include <queue>
#include <set>
#include <utility>
#include <ios>
#include <iomanip>
#include <sstream>
#include <map>

using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

const ll mod = 1e9+7;

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

	vi c(n-1, 0);
	vi s(n-1, 0);
	vi f(n-1, 0);

	REP(i, n-1) {
		cin >> c[i] >> s[i] >> f[i];
	}

	REP(i, n - 1) {

		int now = s[i] + c[i];
		FOR(j, i+1, n-1) {
			if (now < s[j]) {
				now = s[j] + c[j];
			}
			else {
				now = s[j] + f[j] * ceil((double)(now - s[j]) / f[j]) + c[j];
			}
		}
		cout << now << endl;
	}
	cout << "0" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
using namespace std;
typedef long long int ll;

int N;
int W;
ll w[110];
ll v[110];
ll dp[110][100010];


void chmax(ll &a, ll b){
  if (a < b) a = b;
}

int main(){
  cin >> N >> W;
  
  for (int i = 0; i < N; i++){
    cin >> w[i] >> v[i];
  }
  
  for (int i = 0; i < N; i++){
    for (int sum_w = 0; sum_w <= W; sum_w++){
      if (sum_w - w[i] >= 0){
        chmax(dp[i+1][sum_w], dp[i][sum_w - w[i]] + v[i]);
      }
      chmax(dp[i+1][sum_w], dp[i][sum_w]);
    }
  }
  
  cout << dp[N][W];
  return 0;
}


Measured memory_bytes: 79360
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace a4
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = Convert.ToInt32(Console.ReadLine());
            var S = Console.ReadLine();

            var R = 0L;
            var G = 0L;
            var B = 0L;
            for (var i = 0; i < S.Length; i++)
            {
                var c = S[i];
                if (c == 'R') R += 1;
                if (c == 'G') G += 1;
                if (c == 'B') B += 1;
            }

            var res = R*G*B;
            for (var a = 0; a < S.Length; a++)
            {
                for (var s = 1; (a+s*2) < S.Length; s++)
                {
                    if (S[a] != S[a + s] &&
                        S[a + s] != S[a + s * 2] &&
                        S[a] != S[a + s * 2])
                    {
                        res -= 1;
                    }
                }
            }
            Console.WriteLine(res);
        }
    }
}


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

using namespace std;

#define ll long long

int main(){
   
   string S;
   ll N,sum=0;
   cin>>S;
   N=stoi(S);
   for(int i=0;i<S.size();i++){
       sum+=stoi(S.substr(i,1));
   }
   if(N%sum==0)cout<<"Yes"<<endl;
   else cout<<"No"<<endl;
   
   return 0;
}

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

typedef long long ll;
#define mod 1000000007
ll fact(ll n,ll r)
{
    if(n<r) return 0;
    if(n-r<r)
    {
        r=n-r;
    }
    ll ans=1;
    for(ll i=0;i<r;i++)
    {
        ans*=n-i;
        ans/=i+1;
    }
    return ans;
}
int main() 
{
    ll n;
    cin>>n;
    vector<ll> arr(n);
    unordered_map<ll,ll> m;
    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
        if(m.find(arr[i])==m.end())
        {
            m[arr[i]]=1;
        }
        else
        {
            m[arr[i]]++;
        }
    }
    ll ans=0;
    for(auto i=m.begin();i!=m.end();i++)
    {
        if(i->second>1)
        {
            ans+=fact(i->second,2);
        }
    }
    for(ll i=0;i<n;i++)
    {
        ll temp=ans;
        if(m[arr[i]]>1)
        {
            temp-=fact(m[arr[i]],2);
            temp+=fact(m[arr[i]]-1,2);
        }
        cout<<temp<<endl;
    }
    return 0;
}

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

vector<long long>vec;

	
int main(){
	long long x,y,z,k;
	cin>>x>>y>>z>>k;
	long long a[x],b[y],c[z];

	for(int i=0;i<x;++i)
		cin>>a[i];
	for(int i=0;i<y;++i)
		cin>>b[i];
	for(int i=0;i<z;++i)
		cin>>c[i];
		
	sort(a,a+x,greater<long long>());
	sort(b,b+y,greater<long long>());
	sort(c,c+z,greater<long long>());
	
	for(long long i=0;i<x;++i)
		for(long long j=0;j<y;++j)
			for(long long l=0;l<z;++l){
				if((i+1)*(j+1)*(l+1)<=k){
					vec.push_back(a[i]+b[j]+c[l]);
					continue;}
				else break;
			}
	sort(vec.begin(), vec.end(), greater<long long>());
	for(long long i=0;i<k;++i)
		cout<<vec[i]<<endl;
	return 0;
}

Measured memory_bytes: 1400
#include<bits/stdc++.h>
    using namespace std;
    int main()
    {
      int n;
      cin >> n;
      bool flag=false;
      while(n!=0)
      {
        if(n%10==7)
        {
          flag = true;
          break;
        }
        n/=10;
      }
      (flag==true)?cout<<"Yes":cout<<"No";
      return 0;
    }

Measured memory_bytes: 3592
#include<iostream>
using namespace std;
int main(){
  int N;
  int M;
  int C;
  cin>>N>>M>>C;
  int i;
  int B[M];
  for(i=0;i<=M-1;i++){
  cin>>B[i];
  }
  int j;
  int A[N][M];
  for(i=0;i<=N-1;i++){
  	for(j=0;j<=M-1;j++){
    cin>>A[i][j];
    }
  }
  int CHECK;
  int ans=0;
  for(i=0;i<=N-1;i++){
   CHECK=0;
    CHECK+=C;
    for(j=0;j<=M-1;j++){
    CHECK+=A[i][j]*B[j];
    }
    if(CHECK>0)ans++;
  }
  cout<<ans;
}

Measured memory_bytes: 3620
#include<algorithm>
#include <bits/stdc++.h>
#include <math.h>
#include <queue>
using namespace std;
int main(void){
	int x,a,b;
	cin>>x>>a>>b;
	if(a>=b)cout<<"delicious"<<endl;
	else if(b-a<=x)cout<<"safe"<<endl;
	else cout<<"dangerous"<<endl;
	return 0;
}

Measured memory_bytes: 256
import java.io.File;
import java.io.IOException;
import java.util.*;
 
public class Main {
 
	public static void main(String[] args) throws IOException {
		//File file = new File("input.txt");
		//Scanner sc = new Scanner(file);
		
		Scanner sc = new Scanner(System.in);
		
		int N = sc.nextInt();
		int A = sc.nextInt();
		System.out.println(N*N-A);
		
	}
}

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

using namespace std;

long long x,t,a;

int main()
{
    cin>>x>>t;
    a=x-t;
    if(a>=0)
    {
        cout<<a<<endl;
    }
    else
    {
        cout<<'0'<<endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    n%=10;
    if(n==2 || n==4 || n==5 || n==7 || n==9)cout<<"hon"<<endl;
    else if(n==3)cout<<"bon"<<endl;
    else cout<<"pon"<<endl;
    return 0;
}

Measured memory_bytes: 3620
#include <iostream>
using namespace std;
int n,r,p,c;
int main(){
	while(cin>>n>>r){
		if(n==0&&r==0) break;
		int a[n],b[n];
		for(int i=0;i<n;i++){
			a[i]=n-i;
			b[i]=n-i;
		}
		for(int i=0;i<r;i++){
			cin>>p>>c;
			for(int j=0;j<c;j++){
				a[j]=b[p+j-1];
			}
			for(int j=0;j<p-1;j++){
				a[c+j]=b[j];
			}
			for(int j=0;j<n;j++){
				b[j]=a[j];
			}
		}
		cout<<a[0]<<endl;
	}
    return 0;
}

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


int main(){

    cout << fixed << setprecision(20);
 
    string s;
    cin>>s;
    int k;
    cin>>k;
    int n = s.size();
    ll dp[n+1][k+1][2]={};
    dp[0][0][0]=1;
    for(int i=0;i<n;i++){
        for(int j=0;j<=k;j++){
            dp[i+1][j][1] += dp[i][j][1];
            if(s[i]=='0') dp[i+1][j][0] += dp[i][j][0];
            if(s[i]!='0') dp[i+1][j][1] += dp[i][j][0];
            for(int h=1;h<=9;h++){
                if(j+1<=k && s[i]==('0'+h)) dp[i+1][j+1][0] += dp[i][j][0];
                if(j+1<=k){
                    dp[i+1][j+1][1] += dp[i][j][1];
                    if('0'+h < s[i]) dp[i+1][j+1][1] += dp[i][j][0];
                }
            }
        }
    }
    ll ans = dp[n][k][0] + dp[n][k][1];
    cout << ans << endl;


}


Measured memory_bytes: 256
#include <stdio.h>

int main()
{
	int x, y;
	int tmp;
	
	scanf ("%d %d", &x, &y);
	
	 if(x < y) {
	 	tmp = x;
	 	x = y;
	 y = tmp;
	 }
	 
	while (y != 0) {
		tmp = x % y;
		x = y;
		y = tmp;
	}
	
	printf ("%d\n", x);
	return 0;
}


Measured memory_bytes: 2124
#include <iostream>

using namespace std;

int main(void) {
	int a, b;
	cin >> a >> b;
	if (a >= 13)
		printf("%d\n", b);
	else if(a > 5)
		printf("%d\n", b >> 1);
	else
		printf("0\n");
	return 0;
}

Measured memory_bytes: 256
import sys
n, k = map(int, input().split())
s = input()

if n == 1:
    print(0)
    sys.exit()

# L...L|R...R|L...L|...と分解
# 奇数番目をflip
s1 = []
flip_count = 0
for i in range(n):
    if flip_count < k and s[i] == s[0]:
        s1.append('R' if s[0]=='L' else 'L')
    else:
        s1.append(s[i])
    if i < n-1 and s[i] == s[0] and s[i+1] != s[0]:
        flip_count += 1

# 偶数番目をflip
s2 = []
flip_count = 0
for i in range(n):
    if flip_count < k and s[i] != s[0]:
        s2.append('L' if s[0]=='L' else 'R')
    else:
        s2.append(s[i])
    if i < n-1 and s[i] != s[0] and s[i+1] == s[0]:
        flip_count += 1

ans1, ans2 = 0, 0
i = 0
while True:
    if i == 0:
        if s1[i] == 'R' and s1[i+1] == 'R': ans1 += 1
    elif i == n-1:
        if s1[i] == 'L' and s1[i-1] == 'L':
            ans1 += 1
            break
    else:
        if s1[i] == 'R' and s1[i+1] == 'L':
            i += 1
        else:
            ans1 += 1
    i += 1
    if i > n-1:
        break

i = 0
while True:
    if i == 0:
        if s2[i] == 'R' and s2[i+1] == 'R': ans2 += 1
    elif i == n-1:
        if s2[i] == 'L' and s2[i-1] == 'L':
            ans2 += 1
            break
    else:
        if s2[i] == 'R' and s2[i+1] == 'L':
            i += 1
        else:
            ans2 += 1
    i += 1
    if i > n-1:
        break

print(max(ans1, ans2))


Measured memory_bytes: 56500
#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

string s;

int get(char x){
  int res = 0;
  string t = s;
  while(1){
    bool ok = 0;
    for(int i=0;i<t.size() - 1;i++){
      if(t[i] != t[i + 1]){
        ok = 1;
      }
    }
    if(!ok){
      break;
    }
    string tt = "";
    for(int i=0;i<t.size() - 1;i++){
      if(t[i] == x || t[i + 1] == x){
        tt += x;
        continue;
      }
      tt += t[i];
    }
    t = tt;
    res++;
  }
  return res;
}

void solve(){

  cin >> s;

  int ans = 9999999;
  for(char x = 'a'; x <= 'z'; x++){
    ans = min(ans, get(x) );
  }
  cout << ans;
}

int main(){
  int tt = 1;
  while(tt--){
    solve();
  }

return 0;
}


Measured memory_bytes: 256
/*                        ______
 _______   /\     |``\   |        |  /
    |     /  \    |__/   |____    |/
    |    / _ _\   |  \   |        |\
    |   /      \  |   \  |______  |  \

            Dept. of CSE
        Comilla  University

*/
#include<bits/stdc++.h>
#define pi 2*acos(0.0)
#define ll long long
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Node struct node
#define spc " "
#define  E  2.71828182845904523536
#define pb push_back
#define pp pop_back
#define valid(nx,ny) nx>=0 && nx<n && ny>=0 && ny<m
#define ee endl
#define pii pair<int,int>
#define infinity 10000000000000000
#define mod 1000000007
int fx[]={0,0,+1,-1};
int fy[]={+1,-1,0,0};

using namespace std;
///check n=1;
///check corner case;
const int maX=1e3+1;
vector<ll>Graph[maX],cost[maX];
ll visit[maX];


int main()
{
   ll a,b,c,k,i,j;
   cin>>a>>b>>c>>k;
   c=a+b;
   if(k>=c)k-=c,i=a;
   else{
    i=k;
    k=0;
   }
   if(k>0){
    i-=k;
    c-=k;
   }
   cout<<i<<endl;
return 0;
}


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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

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

//-----------------------------------------------------------------------------

int m;
string s;

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

	cin>>m;
	REP(i,m) {
		cin>>s;
		set<string> st;
		int sz=(int)s.size();
		REP(i,sz) {
			string a,b,c,d;
			a=s.substr(0,i),b=s.substr(i);
			c=a,d=b;
			reverse(ALL(c));reverse(ALL(d));
			st.insert(a+b);st.insert(b+a);
			st.insert(a+d);st.insert(d+a);
			st.insert(c+b);st.insert(b+c);
			st.insert(c+d);st.insert(d+c);
		}
		cout<<st.size()<<endl;
	}

	return 0;
}



Measured memory_bytes: 3188
#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int a,b;
    cin >> a >> b;
    int d,r;
    double f;
    d = a/b;
    r = a%b;
    f = double(a)/double(b);
    //cout << d << " " << r << " " << f << endl;
    printf("%d %d %.5lf",d,r,f);
}


Measured memory_bytes: 3228
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <stack>

using namespace std;

int main() {
  while( true ) {
	string input;
	getline( cin, input );
	if ( input == "." ) break;

	bool success = true;
	stack<char> s;
	for ( char ch : input ) {
	  if ( ch == '[' or ch == '(' ) {
		s.push( ch );
	  } else if ( ch == ']' ) {
		if ( s.empty() or s.top() != '[' ) {
		  success = false;
		  break;
		}
		s.pop();
	  } else if( ch == ')' ) {
		if ( s.empty() or s.top() != '(' ) {
		  success = false;
		  break;
		}
		s.pop();
	  }
	}
	if ( not s.empty() ) success = false;
	
	if ( success ) {
	  cout << "yes" << endl;
	} else {
	  cout << "no" << endl;
	}
  }
}

Measured memory_bytes: 1180
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define int long long
#define For(i,x,y) for(int i = x; i <= y; ++i)
#define FOR For
#define rev_For(i,x,y) for(int i = x; i >= y; --i)
#define REV_FOR rev_For
#define ll long long
#define Size(v) ((int)v.size())
#define _section "\n--------------------\n"
#define fi first
#define se second
#define pb push_back
using namespace std;
// using namespace __gnu_pbds;


int n, k;
const int MOD = 1e9 + 7;
const int nax = 1e5 + 5;
int store[nax];

int binpow(int x, int y) {
	if (y == 0) return 1 % MOD;
	if (y == 1) return x % MOD;

	if (y & 1) {
		int mid = binpow(x, y / 2);
		return ((mid * mid) % MOD * x) % MOD;
	}
	else {
		int mid = binpow(x, y / 2);
		return (mid * mid) % MOD;
	}
}

void input() {
	cin >> n >> k;
}

void solve() {
	int sum = 0;
	// xet so bi chia
	for (int i = k; i >= 1; --i) {
		int high = k / i;
		store[i] = binpow(high, n);

		int j = i;
		for (int t = 2; j * t <= k; ++t) {
			int temp = j * t;
			// cout << i << ' ' << temp << '\n';

			store[i] -= store[temp];
		}
	}

	for (int i = 1; i <= k; ++i) {
		sum = (sum + store[i] * i) % MOD;
	}

	cout << sum;
}

signed main() {
#ifdef _DEBUG
	// freopen("INPUT.inp", "r", stdin);
	//freopen("OUTPUT.out", "w", stdout);
	clock_t start, end;
	start = clock();
#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
//------------------------------------------------------------------------------------
//----------------------------------MAIN PROGRAM--------------------------------------
	int t = 1;
	while (t--) {
		input();
		solve();
	}
//--------------------------------------END-------------------------------------------
//------------------------------------------------------------------------------------
#ifdef _DEBUG
	end = clock();
	cerr << "\n----------\nTime Elapsed: " << fixed << double(end - start) / double(CLOCKS_PER_SEC) << setprecision(6) << '\n';
#endif
	return 0;
}

Measured memory_bytes: 4364