akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using namespace atcoder;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int n, d;
    cin >> n >> d;
    two_sat ts(n);
    VVI p(n, VI(2));
    rep(i, n) cin >> p[i][0] >> p[i][1];
    rep(i, n) rep(j, i) {
        rep(s, 2) rep(t, 2) {
            if (abs(p[i][s] - p[j][t]) < d) {
                ts.add_clause(i, !s, j, !t);
            }
        }
    }
    if (ts.satisfiable()) {
        cout << "Yes" << endl;
        auto ans = ts.answer();
        rep(i, ans.size()) cout << p[i][ans[i]] << endl;
    } else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 3796
#include <stdio.h>

int
main(int argc, char *argv[])
{
  int r, c;
  int i, j;
  scanf("%d%d", &r, &c);

  int a[r+1][c+1];
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  // c
  for (i = 0; i < r; i++) {
    a[i][c] = 0;
    for (j = 0; j < c; j++) {
      a[i][c] += a[i][j];
    }
  }

  // r
  for (j = 0; j < c; j++) {
    a[r][j] = 0;
    for (i = 0; i < r; i++) {
      a[r][j] += a[i][j];
    }
  }

  // total
  int total = 0;
  for (i = 0; i < r; i++) {
    total += a[i][c];
  }
  a[r][c] = total;

  // print
  for (i = 0; i < r+1; i++) {
    for (j = 0; j < c+1; j++) {
      printf("%d", a[i][j]);
      if (j == c) {
        printf("\n");
      } else {
        printf(" ");
      }
    }
  }
}

Measured memory_bytes: 1748
N,K = map(int,input().split())
A = [0] + list(map(int,input().split()))

B = [1] + [A[1]]
S = set(B)
i = 1
while i+1 == len(S):
    B.append(A[B[i]])
    S.add(A[B[i]])
    i += 1

for i in range(len(B)-1):
    if B[i] == B[-1]:
        a = i
        break

if K <= a:
    print(B[K])
else:
    print(B[a+ ((K-a) % (len(B)-a-1))])

Measured memory_bytes: 126536
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<math.h>
#include<map>
#include<iomanip>
#include<queue>

using ll = long long;
const int inf = 99999999;

using namespace std;

int main(){
	
	int n;
	cin >> n;
	
	ll ans = 0;
	for(int a = 1; a < n; a++){
		for(int b = 1; b*a < n; b++){
			int c = n - a*b;
			if(c <= 0)break;
			ans++;
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
 


Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template<typename T,typename U> using P=pair<T,U>;
template<typename T> using V=vector<T>;
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}

template<typename T>auto&operator<<(ostream&s,const vector<T>&v){s<<"[";bool a=1;for(auto e:v){s<<(a?"":" ")<<e;a=0;}s<<"]";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const pair<T,U>&p){s<<"("<<p.first<<","<<p.second<<")";return s;}
template<typename T>auto&operator<<(ostream&s,const set<T>&st){s<<"{";bool a=1;for(auto e:st){s<<(a?"":" ")<<e;a=0;}s<<"}";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const map<T,U>&m){s<<"{";bool a=1;for(auto e:m){s<<(a?"":" ")<<e.first<<":"<<e.second;a=0;}s<<"}";return s;}
#define DUMP(x)  cerr<<#x<<" = "<<(x)<<endl;

struct edge { int to, cost; };

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

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

   V<int> dp(n, INF);
   dp[0] = 0;
   for (int i = 0; i < n; i++) {
      if (i+1 < n) {
         chmin(dp[i+1], dp[i] + abs(h[i] - h[i+1]));
      }
      if (i+2 < n) {
         chmin(dp[i+2], dp[i] + abs(h[i] - h[i+2]));
      }
   }
   cout << dp[n-1] << endl;

   return 0;
}


Measured memory_bytes: 1792
import sys

H, W = map(int, input().split())
K = int(input())
cumsum = [[0]*(W+1) for _ in [0]*(H+1)]
d1, d2 = 10**12, 10**6

for y, _up in enumerate(cumsum[:-1], start=1):
    acc = 0
    for x, (up, c) in enumerate(zip(_up[1:], sys.stdin.readline()), start=1):
        if c == "J":
            acc += d1
        elif c == "O":
            acc += d2
        else:
            acc += 1
        cumsum[y][x] = acc + up

ans = []
append = ans.append
for y1, x1, y2, x2 in (map(int, l.split()) for l in sys.stdin):
    x1 -= 1
    y1 -= 1
    v = cumsum[y2][x2] - cumsum[y1][x2] - cumsum[y2][x1] + cumsum[y1][x1]
    append("%d %d %d"%(v//d1, v%d1//d2, v%d2))

print(*ans, sep="\n")


Measured memory_bytes: 61416
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int mn=1001,i,n,t,cc,tt;
	scanf("%d %d",&n,&t);
	for(i=1;i<=n;i++)
	{
		scanf("%d %d",&cc,&tt);
		if(tt<=t)
		{
			mn=min(mn,cc);
		}
	}
	if(mn==1001)printf("TLE");
	else printf("%d",mn);
	return 0;
}

Measured memory_bytes: 384
n, m = map(int, input().split())


A =[]

for x in range(n):
    A.append(list(map(int, input().split())))

B =[]

for x in range(m):
    B.append(int(input()))

#print(A)
#print(B)

ans = 0
for x in range(n):
  for y in range(m):
    ans += A[x][y]*B[y]
  print(ans)
  ans = 0
  
  


Measured memory_bytes: 6008
#include<iostream>
#include<algorithm>
#define max(a, b) (a>b?a:b)
using namespace std;

int main(){
    int n, m, a, b;
    cin >> n >> m;
    int res = m;
    for(int i = 0;i < n;i++){
	cin >> a >> b;
	m += a-b;
	res = max(res, m);
	if(m < 0){cout << 0<< endl;return 0;}
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 1160
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <bitset>
#include <map>
#include <math.h>
#include <utility>

using namespace std;

int main(){

    int N, D;
    cin >> N >> D;

    vector<vector<int>> X(N, vector<int>(D));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < D; j++){
            cin >> X[i][j];
        }
    }

    int count = 0, dist = 0;
    for (int i = 0; i < N-1; i++){
        for (int j = i+1; j < N; j++){
            // cout << "i: " << i << ", j: " << j << endl;
            int sum = 0;
            for (int k = 0; k < D; k++){
                sum += (X[i][k] - X[j][k]) * (X[i][k] - X[j][k]);
            }
            // cout << sum << endl;
            dist = (int)(sqrt(sum)+0.5);
            if(sum == dist*dist) count++;
        }
    }

    cout << count << endl;

    return 0;
}


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

int main(){
  int n;
  string ans = "Yes";
    cin >> n;
  int t, x, y, prevt=0, pos=0;
    for(int i=0; i<n; i++){
      cin >> t >> x >> y;
      t = (t - prevt) - (abs(pos-(x+y)));
      if(t<0 || t%2==1) ans = "No";

    }

    cout << ans << endl;


}


Measured memory_bytes: 256
def dfs(current):
    for maps in Graph[current]:
        if maps in already:
            continue
        else:
            already.append(maps)
            dfs(maps)

n,m = map(int,input().split())
ans = 0
Edges = []
for _ in range(m):
    Edges.append([int(j)-1 for j in input().split()])

for i in range(m):
    egdes = Edges[0:i]+Edges[i+1:]
    Graph = [[] for _ in range(n)]
    for edge in egdes:
        Graph[edge[0]].append(edge[1])
        Graph[edge[1]].append(edge[0])
    already = []
    dfs(0)
    if len(already) != n:
        ans += 1
print(ans)

Measured memory_bytes: 9196
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

""" Input
N = int(input())
N, A, B = map(int, input().split())
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

S = input().rstrip()
print(S.replace('?', 'D'))


Measured memory_bytes: 9440
#include<stdio.h>
int main(void)
{
	int n,a[5000],sum=0, max;
	int i;

	while (1) {
		scanf("%d",&n);
		if (n == 0)break;

		for (i = 0; i < n; i++)
			scanf("%d",&a[i]);
		sum = 0;
		max = a[0];
		for (i = 0; i < n; i++) {
			sum += a[i];
			if (max < sum)
				max = sum;
			if (sum < 0)
				sum = 0;
		}
		printf("%d\n",max);
	}
	return 0;
}

Measured memory_bytes: 616
#include <iostream>
#include <iomanip>
using namespace std;
int main(){

char s[4]={};

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

int a=0;

for(int i=0;i<4;i++){
  if(s[i]=='+'){
  a++;
}
else if(s[i]=='-'){
  a--;
}
}

cout << a << endl;

}


Measured memory_bytes: 256
#define _USE_MAT_DEFINES
#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <stack>
#include <deque>
#include <numeric>
#include <functional>
using namespace std;

static const int MOD = 1e9 + 7;

int main() {

    int A, B;
    cin >> A >> B;

    int ans = -1;
    if ((1 <= A&&A <= 9) && (1 <= B&&B <= 9))ans = A*B;
    cout << ans;
}

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

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }


int main() {

// A
    int a,b; cin >> a >> b;

    if (a >= 10 || b >= 10) {
        cout << -1 << endl;
    } else {
        cout << a * b << endl;
    }



// B
//    int n; cin >> n;
//    if(n > 81) {
//        cout << "No" << endl;
//        return 0;
//    }
//
//
//    EFOR(i, 2, 9) {
//        if(n%i == 0 && n/i <= 9) {
//            cout << "Yes" << endl;
//            return 0;
//        }
//    }
//
//    cout << "No" << endl;

    return 0;
}



Measured memory_bytes: 256
// AOJ 1125

#include <iostream>

using namespace std;

// x, i, w, s
// y, j, h, t

int main(void)
{
	int n;
	while (cin >> n, n){
		int w, h, x, y, s, t;
		cin >> w >> h;
		int field[w][h];

		for (int i = 0; i < w; i++){
			for (int j = 0; j < h; j++){
				field[i][j] = 0;
			}
		}
		for (int i = 0; i < n; i++){
			cin >> x >> y;
			x--; y--;
			field[x][y] = 1;
		}
		cin >> s >> t;

		int ret = 0;
		for (int i = 0; i <= (w - s); i++){
			for (int j = 0; j <= (h - t); j++){
				int cnt = 0;
				// cout << "start : " << i+1 << " " << j+1 << endl;
				// cout << "end   : " << i + s << " " << j + t << endl;
				for (int k = 0; k < s; k++){
					for (int l = 0; l < t; l++){
						cnt += field[i + k][j + l];
					}
				}
				ret = max(ret, cnt);
			}
		}

		cout << ret << endl;
	}
}

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

int main(){
  int x; cin >> x;
  int r = x % 100;
  int y = 0;
  for (int i = 5; i > 0; i--){
    y += r / i; r %= i;
  }
  if (y > x / 100) cout << 0;
  else cout << 1;
  cout << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std; long long int l[100000][3]; int n; int naj=-1; long long int dp[100000][3];
int main(void) {
	int n; cin >> n;
	for (int i=0; i<n; i++){
		int a,b,c; 
		cin >> a >> b >> c; 
		l[i][0]=a; 
		l[i][1]=b; 
		l[i][2]=c;
	}
	dp[0][0]=l[0][0]; dp[0][1]=l[0][1]; dp[0][2]=l[0][2];
	for (int i=1; i<n; i++){
		dp[i][0]=max(dp[i-1][1]+l[i][0],dp[i-1][2]+l[i][0]);
		dp[i][1]=max(dp[i-1][0]+l[i][1],dp[i-1][2]+l[i][1]);
		dp[i][2]=max(dp[i-1][1]+l[i][2],dp[i-1][0]+l[i][2]);
	}
	cout << max(max(dp[n-1][0],dp[n-1][1]), dp[n-1][2]);
	return 0;
}



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

int main() {
	int N, T, mn = 1001;
	cin >> N >> T;
	int c[N], t[N];
	bool can = false;

	for (int i = 0; i < N; i++) {
		cin >> c[i] >> t[i];
		if (t[i] <= T) {
			mn = min(mn, c[i]);
			can = true;
		}
	}
	if(!can) cout << "TLE" << endl;
	else cout << mn << endl;

	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
int R=1,C=1,H[2000010],N[2000010];
char S[200010][20];
//評価関数（いまはMIN）
int hyouka(int a,int b){
  if(C<b)return 1;
  if(C<a||b==0)return 0;
  return strcmp(S[N[H[a]]],S[N[H[b]]])<0?1:0;
}
//挿入関数
void hin(int a){
  int i=C++;
  for(N[H[0]=R]=a;hyouka(0,i/2);i/=2)H[i]=H[i/2];
  H[i]=R++;
}
//取り出す関数
int hout(){
  int rt=H[1],i,j=2,k=H[--C];
  for(i=1;hyouka(i,C);i=j)H[i]=H[j=i*2+1-hyouka(i*2,i*2+1)];
  H[j/2]=k;
  return rt;
}
int main(){
  int n,i,m=0,d[200010],b[200010]={1};
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",S[i]);
    hin(i);
  }
  for(i=0;i<n;i++)d[i]=N[hout()];
  //for(i=0;i<n;i++)printf("!!%s\n",S[d[i]]);
  for(i=1;i<n;i++){
    if(strcmp(S[d[i]],S[d[i-1]])==0)b[i]=b[i-1]+1;
    else                            b[i]=1;
    if(m<b[i])m=b[i];
  }
  //for(i=0;i<n;i++)printf("%d\n",b[i]);
  for(i=0;i<n;i++){
    if(b[i]==m)printf("%s\n",S[d[i]]);
  }
  return 0;
}


Measured memory_bytes: 13568
int main(void)
{
	long long x,c,a,cnt;
	scanf("%lld",&x);
	c=100;
	cnt=0;
	while(c<x){
		a=c*1/100;
		c=c+a;
		cnt++;
	}
		printf("%lld\n",cnt);
	return 0;
}



Measured memory_bytes: 1708
#include<iostream>
#include<math.h>
#include<vector>

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

using namespace std;
typedef long long ll;


int main(){
    int n = 0; cin >> n;
    vector<char> ishi(n);
    rep(i, n) cin >> ishi[i];

    int cnt = 0;
    int i = 0, j = n-1;
    while(i < j){
        if(ishi[i] == 'R' && ishi[j] == 'R'){
            ++i;
            continue;
        }
        else if(ishi[i] == 'W' && ishi[j] == 'W'){
            --j;
            continue;
        }
        else if(ishi[i] == 'W' && ishi[j] == 'R'){
            ishi[i++] = 'R';
            ishi[j--] = 'W';
            ++cnt;
        }
        else{
            ++i;
            --j;
        }
    }
    cout << cnt << endl;
    return 0;
}

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

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

using namespace std;

class mint{
	static const int MOD=1e9+7;
	int x;
public:
	mint():x(0){}
	mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }

	mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
	mint& operator-=(const mint& m){ x-=m.x; if(x<   0) x+=MOD; return *this; }
	mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
	mint& operator/=(const mint& m){ return *this*=inverse(m); }
	mint operator+(const mint& m)const{ return mint(*this)+=m; }
	mint operator-(const mint& m)const{ return mint(*this)-=m; }
	mint operator*(const mint& m)const{ return mint(*this)*=m; }
	mint operator/(const mint& m)const{ return mint(*this)/=m; }

	friend mint inverse(const mint& m){
		int a=m.x,b=MOD,u=1,v=0;
		while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
		return u;
	}

	friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=mint(t); return is; }
	friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
	int to_int()const{ return x; }
};

mint pow(mint m,long long k){
	mint res=1;
	for(;k>0;k>>=1,m*=m) if(k&1) res*=m;
	return res;
}

class Eratosthenes_sieve{
	vector<int> pf_min,p;
public:
	Eratosthenes_sieve(int n):pf_min(n+1){
		for(int i=2;i<=n;i++) if(pf_min[i]==0) {
			pf_min[i]=i;
			if(1LL*i*i<=n) for(int j=i*i;j<=n;j+=i) if(pf_min[j]==0) pf_min[j]=i;
			p.emplace_back(i);
		}
	}

	const vector<int>& primes()const{ return p; }

	bool is_prime(int a)const{
		assert(a<=(int)pf_min.size()-1);
		return a>0 && pf_min[a]==a;
	}

	map<int,int> prime_factorize(int a)const{
		assert(a<=(int)pf_min.size()-1);
		map<int,int> pf;
		for(;a>1;a/=pf_min[a]) ++pf[pf_min[a]];
		return pf;
	}
};

int main(){
	Eratosthenes_sieve S(1e6);

	int n; scanf("%d",&n);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	map<int,int> pf;
	rep(i,n){
		for(auto q:S.prime_factorize(a[i])){
			pf[q.first]=max(pf[q.first],q.second);
		}
	}

	mint L=1;
	for(auto q:pf) L*=pow(mint(q.first),q.second);

	mint ans=0;
	rep(i,n) ans+=L/a[i];
	cout<<ans<<'\n';

	return 0;
}


Measured memory_bytes: 4984
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a7=1000000007;
ll inf=100000000000000000;

int main()
{
  ll n;cin>>n;
  string s;cin>>s;
  ll ans=1;
  for(ll i=1;i<n;i++){
    if(s[i]!=s[i-1])ans++;
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 512
#include <stdio.h>

int
main(void)
{
	char line[80];
	int n;
	int a;
	int b;
	int c;
	bool r;

	if (gets(line) == NULL) {
		return 1;
	}
	if (sscanf(line, "%d", &n) != 1) {
		return 1;
	}
	
	for (int i = 0; i < n; i++) {
		if (gets(line) == NULL) {
			return 1;
		}
		if (sscanf(line, "%d %d %d", &a, &b, &c) != 3) {
			return 1;
		}
		r = false;
		if (a > b && a > c) {
			r = (a*a == b*b + c*c);
		} else if (b > a && b > c) {
			r = (b*b == a*a + c*c);
		} else if (c > a && c > b) {
			r = (c*c == a*a + b*b);
		}
		printf("%s\n", (r ? "YES" : "NO"));
	}

	return 0;
}

Measured memory_bytes: 1012
s = input()
q = int(input())
add_head = ""
add_tail = ""
rev = False
for _ in range(q):
    query = input().split()
    if query[0] == "1":
        rev = not rev
    else:
        if query[1] == "1":
            if rev:
                add_tail = add_tail + query[2]
            else:
                add_head = add_head + query[2]
        else:
            if rev:
                add_head = add_head + query[2]
            else:
                add_tail = add_tail + query[2]
                
if not rev:
    print(add_head[::-1] + s + add_tail)
else:
    print(add_tail[::-1] + s[::-1] + add_head)

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


int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s[2];
    cin >> s[0] >> s[1];
    if (s[0][0] == s[1][2] && s[0][1] == s[1][1] && s[0][2] == s[1][0]) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }

    return 0;
}


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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cout<<(#v)<<":";for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1e9;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

string s, t;
int n, m;
int dp[4001][4001];

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

    while (cin >> s >> t) {
        n = s.size(); m = t.size();
        int ans = 0;
        rep(i, n) {
            rep(j, m) {
                if (i == 0 || j == 0) dp[i][j] = 0;
                if (s[i] == t[j]) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                    chmax(ans, dp[i + 1][j + 1]);
                }
                else {
                    dp[i + 1][j + 1] = 0;
                }
            }
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 65808
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

string s;

int main() {
  cin >> s;
  cout << "2018";
  for(int i = 4; i < s.size(); ++i) cout << s[i];

  return 0;
}

Measured memory_bytes: 256
// スタックサイズ: 100MB

#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iomanip>
#include <queue>
using namespace std;

typedef int64_t ll;
const ll INF = 1000000000000000000;
#define fori(i, a, b) for (ll i = (a); i < (b); ++i)
#define ford(i, a, b) for (ll i = (b - 1); (a) <= i; --i)
#define rep(i, n) fori(i, 0, n)
#define vll vector<ll>
#define all(v) (v).begin(), (v).end()



int main() {

	string s, tmp;
	ll N;
	cin >> s;
	N = s.size();

	ll ans = 0;
	rep(bit, 1ull << (N - 1)) {
		ll pre = 0;
		rep(i, N - 1) {
			if ((bit >> i) & 1ull) {
				tmp = s.substr(pre, i - pre + 1);
				pre = i + 1;
				ans += stoll(tmp);
			}
		}
		tmp = s.substr(pre, N - pre);
		ans += stoll(tmp);
	}
	cout << ans << endl;
}

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

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?b:gcd(b%a,a); }

#define ll long long int 
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;

#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (302)
ll n, m, dp[MAXN][MAXN][MAXN];
vector<pi> v[MAXN];
ll many(ll a,ll b,ll c,ll d) {
	return (a>=d)+(b>=d)+(c>=d);
}
int main()
{
	FAST
	cin>>n>>m;
	FOR(i,0,m-1) { ll a, b, c; cin>>a>>b>>c;
		v[b].eb(a, c);
	}
	const ll mod=1e9+7;
	dp[0][0][0]=1;
	ll ans=0;
	FOR(i,0,n) FOR(j,0,n) FOR(k,0,n) {
		if(i == j && i && j) continue;
		if(i == k && i && k) continue;
		if(j == k && j && k) continue;
		ll l=max({i, j, k}) + 1;
		if(l==n+1) { ans += dp[i][j][k], ans %= mod; continue; }
		dp[l][j][k] += dp[i][j][k], dp[l][j][k] %= mod;
		dp[i][l][k] += dp[i][j][k], dp[i][l][k] %= mod;
		dp[i][j][l] += dp[i][j][k], dp[i][j][l] %= mod;
		for(auto p:v[l]) {
			if(many(l, j, k, p.f) ^ p.s) dp[l][j][k]=0;
			if(many(i, l, k, p.f) ^ p.s) dp[i][l][k]=0;
			if(many(i, j, l, p.f) ^ p.s) dp[i][j][l]=0;
		}
	}
	cout<<ans<<'\n';
}


Measured memory_bytes: 215424
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#define INF ((int)1e9)
#define LL long long
using namespace std;
int getbi(){
	int res = 0;
	string s;
	cin >> s;
	for(int i=0;i<s.size();++i){
		res *=2;
		res += s[i] - '0';
	}
	return res;
}
int un(int n,int m){
	vector<string> line(n);
	for(int i=0;i<n;++i){
		cin >> line[i];
	}
	int res = 0;
	string zero = "";
	for(int i=0;i<m;++i)zero+="0";
	for(int i=1;i<(1<<n);++i){
		string nn = zero;
		int count = 0;
		for(int j=0;j<n;++j){
			if((i & (1 << j)) == 0)continue;
			++count;
			for(int k=0;k<m;++k)nn[k] = (line[j][k] == nn[k] ? '0' : '1');
		}
		if(zero == nn)res = max(res, count);
	}
	line.clear();
	return res;
}

int mo(int n,int m){
	vector<int> line(1<<m, -1);
	line[0] = 0;
	for(int i=0;i<n;++i){
		int nn;
		nn = getbi();
		vector<int> nline(1<<m, -1);
		for(int j=0;j<(1<<m);++j){
		  nline[j] = max(nline[j], line[j]);		  
		  if(line[j] == -1)continue;
		  nline[nn ^ j] = max(nline[nn ^ j], line[j]+1);
		}
		line  = nline;
	}
	return line[0]; 
}
int solve(int n,int m){
	if(n <= 20){
		return un(n, m);	
	}else{
		return mo(n, m);
	}
}
int main(){
	int n,m;
	while(true){
		cin >> n >> m;
		if(n == 0 && m == 0)break;
		cout << solve(n, m) << endl;
	}
	return 0;
}



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

int main(){
  char ac;
  cin >> ac;
  
  
  string cap={"abcdefghijklmnopqrstuvwxyz"};
  string CAP={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  
  for(int i=0; i<26; i++){
  if(ac==cap[i]){
    cout << 'a' << endl;
  }
  else if(ac==CAP[i]){
    cout << 'A' << endl;
  }
  }
}


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

struct edge{
	int v,nxt;
}e[siz];

int n,m;
int head[siz];
int c[siz];
ll cnt[2];

inline
int dif(int x){return x==1?0:1;}

void dfs(int now,int fc=0);
void add_edge(int u,int v,bool o=true);

int main()
{
	memset(c,0x7f,sizeof c);
	scanf("%d%d",&n,&m);
	int rt=-1;
	for(int i=1,u,v;i<=m;i++){
		scanf("%d%d",&u,&v);
		add_edge(u,v);
		if(rt==-1)
			rt=u;
	}
	dfs(rt);
	for(int i=1;i<=n;i++)
		if(c[i]!=c[0])
			cnt[c[i]]++;
	printf("%lld\n",(cnt[0]*cnt[1])-m);
}

void dfs(int now,int fc)
{
	c[now]=dif(fc);
	const int sc=dif(c[now]);
	for(int i=head[now];i!=0;i=e[i].nxt){
		if(c[e[i].v]==c[0]){
			dfs(e[i].v,c[now]);
			continue;
		}
		if(c[e[i].v]!=sc){
			printf("%lld\n",(((ll)n*(n-1))>>1)-m);
			exit(EXIT_SUCCESS);
		}
	}
}

void add_edge(int u,int v,bool o)
{
	static int cnt;
	e[++cnt]={v,head[u]};
	head[u]=cnt;
	if(o)
		add_edge(v,u,false);
}

Measured memory_bytes: 3328
s=list(map(int,input().split()))
if len(s)>1:c=s[0];d=s[1]
else:c=s[0];d=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
e=0;i=0
while i<d-e:
    p=i;q=0
    for j in range(c):
        if a[j]==b[p]:q+=1;p+=1
    e=max(e,q)
    i+=1
print(e)

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

int main() {
    while(1){
        int H,W;
        cin >> H >> W;
        if(H == 0 && W == 0) break;
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(i==0 || i==H-1 || j==0 || j==W-1) cout << "#";
                else cout << ".";
            }
            cout << endl;
        }
        cout << endl;
    }
    return 0;
}



Measured memory_bytes: 3088
#include <bits/stdc++.h>
#include <numeric>
using namespace std;
#define ll long long
#define mod 100000007
#define rep(i, n) for (int i = 0; i < n; ++i)
using Graph = vector<vector<int>>;
#define grepq  priority_queue<double, std::vector<double>, std::greater<double>>

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

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




int main(){
    string S;
    cin >> S;
    sort(S.begin(), S.end());
    for (int i = 1; i < S.size();i++)
    {
        if(S[i] == S[i-1]){
            cout << "no" << endl;
            return 0;
        }
    }
    cout << "yes" << endl;
}


Measured memory_bytes: 256
#include<stdio.h>
int main()
{
	int a[3],b[2],r=0;
	int i;
	for(i=0;i<3;i++){
		scanf("%d",&a[i]);
	}
	if(a[0]<a[1]&&a[0]<a[2]){
		r+=a[0];
	}else if(a[0]>a[1]&&a[2]>a[1]){
		r+=a[1];
	}else{
		r+=a[2];
	}
	for(i=0;i<2;i++){
		scanf("%d",&b[i]);
	}
	if(b[0]>b[1]){
		r+=b[1];
	}else{
		r+=b[0];
	}
	printf("%d\n",r-50);
	return 0;
}

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

int main(){
    long long a,b,c;
    cin >> a >> b >> c;
    for (int i = 1; i < b; i++){
        for (int j = 1; j < b; j++){
            if (a*i*j % b == c){
                cout << "YES" << endl;
                return 0;
            }
        }
    }
    cout << "NO" << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int gcd(int a,int b){
  if(a<b)swap(a,b);
  if(b==0) return a;
  return gcd(b,a%b);
}

map<int,int> ma;

template<typename T> 
bool factorize(T x){
    for(T i=2;i*i<=x;i++){
      if(x%i==0){
        if(ma[i]!=0) return false;
        ma[i]+=1;
      }
      while(x%i==0){
          x/=i;
      }
    }
    if(x!=1) {
      if(ma[x]!=0) return false;
      ma[x]++;
    }
    return true;
}

int n;
int a[1000010];

void solve(){
  cin >> n;
  rep(i,n){
    cin >> a[i];
  }
  int g=0;
  rep(i,n){
    g=gcd(g,a[i]);
  }
  bool ans=true;
  rep(i,n){
    if(!factorize(a[i])) ans=false;
  }
  if(ans){
    cout << "pairwise coprime" << endl;
    return;
  }
  if(g==1){
    cout << "setwise coprime" << endl;
    return;
  }
  cout << "not coprime" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

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

int main(){
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	if(a==b)
		printf("%d\n",c);
	else if(a==c)
		printf("%d\n",b);
	else if(b==c)
		printf("%d\n",a);
	return 0;
}


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

using namespace std;
//using namespace atcoder;
 
#ifdef DEBUG
    #include "debug.h"
    #define dump(...)                                                              \
        DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
                << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
                << endl                                                            \
                << "    ",                                                         \
            dump_func(__VA_ARGS__)
#else
    #define dump(...)
#endif

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

#define int long long
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

//*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)


void solve(){
    int N, X, M;
    cin >> N >> X >> M;
    vector<int> A;
    set<int> alln;
    
    int head = 0, loop = 0;
    for(int _=0; _<N; _++){
        if(_==0){
            A.push_back(X);
            alln.insert(X);
            continue;
        }
        int t =A.back()*A.back() % M;
        A.push_back(t);
        if(alln.find(t)!=alln.end()){
            dump(_,t); dump(A);
            for(int i=0; i<(int)A.size()-1; i++){
                if(A[i]==t){ head = i+1; loop = _-i; goto b;}
            }
        }
        alln.insert(t);
    }
b:
    
    int headsum = 0, loopsum=0;
    vector<int> loopelement;
    for(int i=0; i<head; i++) headsum += A[i];
    for(int i=head; i<head+loop; i++){
        loopsum += A[i];
        loopelement.push_back(A[i]);
    }

    dump(head, loop, loopelement);

    
    int ans = 0;
    if(loop == 0){
        rep(i,N) ans += A[i];
    }else{
        ans += ((N-head)/loop) * loopsum + headsum;
        for(int i=0; i<(N-head)%loop; i++) ans += loopelement[i];
    }
    cout << ans << endl;
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed;
    solve();
}


Measured memory_bytes: 6612
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,a,b) for(int i =(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define REPm(i,n) for(int i=(n)-1;i>=0;i--)
#define REP1(i,n) for(int i=1;i<=(n);i++)
#define mp make_pair
typedef long long ll;
int gcd(int a,int b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}
int main(){
    int A,B,C;
    cin >> A >> B >> C;
    int ans = 0;
    if(A%2 == 1 || B%2 == 1 || C%2 == 1) ans = 0;
    else{
        if(A == B && B == C) ans = -1;
        else{
            int dif1 = abs(B-A);
            int dif2 = abs(C-B);
            int g = gcd(dif1,dif2);
            while(g%(1<<ans) == 0) ans++;
            ans--;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		class qube{
			Scanner sc = new Scanner(System.in);
			int [] face = new int[6],faces;
			Integer i,dump=0;
			Integer a,b,n;
			int ec=0,nc=0;


			qube(){
				for(i=0;i<6;i++){
					face[i]=sc.nextInt();
				}
				n = sc.nextInt();
				faces = face.clone();
			}

			void swap(int a,int b){
				dump = this.face[a];
				this.face[a] = this.face[b];
				this.face[b] = dump;

			}
			void scn(){
				a = sc.nextInt();
				b = sc.nextInt();
			}
			void pt(){
				System.out.printf("%d\n", this.face[2]);
			}

			void move(String es){
				if(es.equals("E")){
					swap(0,2);
					swap(0,5);
					swap(0,3);
				}
				else if(es.equals("N")){
					/*if(nc == 4){
						this.move("E");
						ec = 0;
						
					}
					/*if(nc == 6){
						this.move("E");
						ec = 0;
					}*/
					swap(4,5);
					swap(4,1);
					swap(4,0);
					//nc++;
				}

			}

		}
		Integer j,k;

		qube q = new qube();
		for(j=0;j<q.n;j++){
			q.scn();

			for(k=0;;k++){
				if(q.b.equals(q.face[1])){
					break;
				}
				
				q.move("N");
				if(k.equals(3)){
					q.move("E");
				}
			}
			while(true){
				if(q.a.equals(q.face[0])){
					break;
				}
				q.move("E");
			}


			q.pt();
			q.face = q.faces.clone();
		}

	}

}


Measured memory_bytes: 26532
#include <iostream>
#include <algorithm>

using namespace std;

struct Team {
    int id;
    int score;

    Team() {}
    bool operator <(const Team &other) const {
        return other.score < score; // Reversed order
    }
} teams[100];
int rank_of[100];

int main() {
    int N;
    cin >> N;
    for(int i = 0; i < N; ++i) {
        teams[i].id = i;
    }
    for(int i = 0; i < N*(N-1)/2; ++i) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        --a; --b;
        if(c == d) {
            teams[a].score += 1;
            teams[b].score += 1;
        }
        else if(c < d) {
            teams[b].score += 3;
        }
        else if(c > d) {
            teams[a].score += 3;
        }
    }

    sort(teams, teams+N);
    int rank = 1;
    int prevscore = teams[0].score;
    for(int i = 0; i < N; ++i) {
        if(teams[i].score < prevscore) rank = i+1;
        rank_of[teams[i].id] = rank;
        prevscore = teams[i].score;
    }
    for(int i = 0; i < N; ++i) {
        cout << rank_of[i] << endl;
    }
    return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

int main(){
  ll N; cin >> N;
  ll D; cin >> D;
  vector<pair<ll,ll>> XY(N); rep(i,N) cin >> XY[i].first >> XY[i].second;
  int ans = 0;
  rep(i,N){
    ll x = XY[i].first;
    ll y = XY[i].second;
    if(x*x+y*y<=D*D) ans++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 6424
#include <bits/stdc++.h> 
using namespace std; 
int main() { 
 	int a,b;
	 cin >> a >> b;
	if ((a * b)%2 == 0){
		cout << "Even" <<endl;
	}
		else{
			cout <<"Odd" <<endl;
		}
			return 0;
		
	}

Measured memory_bytes: 256
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;


int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    vector<int> A;
    for (int i = 0; i < 5; ++i) {
        int x;
        cin >> x;
        A.push_back(x);
    }
    int k = 0;
    cin >> k;

    for (int i = 0; i < A.size(); ++i) {
        for (int j = i + 1; j < A.size(); ++j) {
            if (abs(A[i] - A[j]) > k) {
                cout << ":(" << "\n";
                return 0;
            }
        }
    }

    cout << "Yay!" << "\n";
    
    return 0;
}

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

#define int long long
#define db(a, ...) cerr<<#a<<" = "<<a<<'\n'
#define db2(a, b) cerr<<#a<<" = "<<a<<", ", db(b)
#define db3(a, b, c) cerr<<#a<<" = "<<a<<", ", db2(b, c)
#define db4(a, b, c, d) cerr<<#a<<" = "<<a<<", ", db3(b, c, d)

using namespace std;

typedef pair<int, int> ii;

const int INF = 1e9, MAX = 2e5 + 5;

int n, k;
int d[MAX];

int32_t main() {
#ifdef ACM
    freopen("D.in", "r", stdin);
#endif
    cin >> n >> k;
    multiset<int> S1;
    multiset<ii> S2;
    for (int i = 1; i <= k; i++) {
        S1.insert(0);
        S2.insert({i, 0});
    }
    int l, r;
    for (int i = 1; i <= n; i++) {
        cin >> l >> r;
        int minD = INF;
        vector<ii> V;
        for (auto it = S2.lower_bound({l, 0}); it != S2.end() && it->first <= r; it++) {
            int newD = it->second + r - it->first + 1;
            minD = min(minD, newD);
            V.emplace_back(it->second, it->first);
        }
        for (ii it:V) {
            S1.erase(S1.find(it.first));
            S2.erase(S2.find({it.second, it.first}));
        }
        if (r < k && minD < INF) {
            d[r + 1] = minD;
            S1.insert(minD);
            S2.insert({r + 1, d[r + 1]});
        }
        if (S1.size() == 0)
            cout << -1 << '\n';
        else
            cout << *S1.begin() + i << '\n';
    }
}

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

int main() {
    for (int i = 0; i < 9; ++i) {
        string s;
        int a, b;
        cin >> s >> a >> b;
        cout << s << ' ' << a + b << ' ' << 200 * a + 300 * b << endl;
    }
}


Measured memory_bytes: 3204
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int N;
    cin >> N;
    vector<string> W(N);
    set<string> sets;
    for (int i = 0; i < N; ++i) {
        cin >> W[i];
        sets.insert(W[i]);
    }
    if (sets.size() != N) {
        cout << "No" << endl;
        return 0;
    }
    for (int i = 1; i < N; ++i) {
        if (W[i-1].back() != W[i].front()) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <utility>
#include <queue>
#include <cassert>
#define maxW 200005

using namespace std;
typedef long long ll;
int H,W,n;
vector <int> blocks[maxW];
int main(){
  scanf("%d%d%d",&H,&W,&n);
  for(int i = 1;i <= n;i++){
    int x,y;
    scanf("%d%d",&x,&y);
    blocks[y].push_back(x);
  }
  for(int i = 1;i <= W;i++){
    blocks[i].push_back(H + 1);
    sort(blocks[i].begin(),blocks[i].end());
  }
  int cx = 1;
  int ans = blocks[1][0] - 1;
  for(int i = 2;i <= W;i++){
    cx++;
    while(cx <= H && binary_search(blocks[i].begin(),blocks[i].end(),cx)) cx++;
    if(cx <= H){
      ans = min(ans,*lower_bound(blocks[i].begin(),blocks[i].end(),cx) - 1);
    }else break;
  }
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 11392
#include<stdio.h>
int main (void){
  int x[3000], y[3000];
  int i, a;
  a = 0;
  for (i = 0;i < 3000;i++){
    scanf ("%d %d",&x[i] ,&y[i]);
    if (x[i] == 0 && y[i] == 0)
      break;
    a++;
  }
  for (i = 0;i < a;i++){
    if (x[i] <= y[i])
      printf ("%d %d\n",x[i] ,y[i]);
    else
      printf ("%d %d\n",y[i] ,x[i]);
  }
  return 0;
}

Measured memory_bytes: 620
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<list>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E=1e18+7;
const ll MOD=1000000007;


//1-index
template<typename T>
class max_heap{
private:
    vector<T> tree;
    bool F(T,T); //me,parent true->change
    bool G(T,T); //child,child true->left false->right
    
    void heap(ull where){
        if(where==1){return;}
        if(F(tree[where],tree[where/2])){
            swap(tree[where],tree[where/2]);
            heap(where/2);
        }
    }
    
    ull last(){return tree.size()-1;}
    
    void merge(ll where){
        ll l=where*2;
        ll r=where*2+1;
        if(tree.size()-1<l){
            if(where==last()){tree.erase(--tree.end()); return;}
            tree[where]=tree[last()];
            heap(where);
            tree.erase(--tree.end());
            return;
        }
        if(tree.size()-1<r){
            tree[where]=tree[l];
            merge(l);
            return;
        }
        if(G(tree[l],tree[r])){
            tree[where]=tree[l];
            merge(l);
        }
        else{
            tree[where]=tree[r];
            merge(r);
        }
    }
    
public:
    max_heap(){tree.resize(1);}
    
    void push(T a){
        tree.push_back(a);
        heap(tree.size()-1);
    }
    
    T front(){
        assert(!empty());
        T ret=tree[1];
        merge(1);
        return ret;
    }
    
    void clear(){tree.clear();}
    
    ull size(){return tree.size();}
    
    bool empty(){
        if(tree.size()==1){return true;}
        return false;
    }
};


int main(){
    max_heap<ll> MAX;
    while(1){
        string c;
        cin>>c;
        if(c=="end"){break;}
        if(c=="insert"){
            ll b;
            cin>>b;
            MAX.push(b);
        }
        if(c=="extract"){
            cout<<MAX.front()<<endl;
        }
    }
    
    return 0;
}

//max_heap
template<typename T>
bool max_heap<T>::F(T where,T parent){
    if(where>parent){return true;}
    return false;
}

template<typename T>
bool max_heap<T>::G(T a,T b){
    if(a>b){return true;}
    return false;
}


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

using namespace std;

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

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

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

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

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

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

int main() {

	int h, w, d; cin >> h >> w >> d;

	vector<int> x(h*w+1);
	vector<int> y(h*w+1);

	for (int i = 1; i < h+1; i++) {
		for (int j = 1; j < w+1; j++) {
			int a; cin >> a;
			x[a] = j;
			y[a] = i;
		}
	}

	vector<int> table(h*w+1);
	for (int i = 1; i < h*w-d+1; i++) {
		table[i + d] = table[i] + (abs(x[i + d] - x[i]) + abs(y[i + d] - y[i]));
	}

	int q; cin >> q;
	for (int i = 0; i < q; i++) {
		int l, r; cin >> l >> r;

		cout<<table[r] - table[l]<<endl;
	}

	



	return 0;
}

Measured memory_bytes: 2176