akhauriyash/Code-Regression huggingface dataset:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int main()
{
    string s;
    cin >> s;
    int ans = 0;
    int l = 0,r = s.size() - 1;
    while(l < r)
    {
        if(s[l] == s[r])
        {
            l++;
            r--;
        }
        else if(s[l] == 'x')
        {
            l++;
            ans++;
        }
        else if(s[r] == 'x')
        {
            r--;
            ans++;
        }
        else
        {
            ans = -1;
            break;
        }
    }
    cout << ans << "\n";
    
    return 0;
}

Measured memory_bytes: 512
N, M, Q = map(int, input().split())  #数字
Q = [list(map(int, input().split())) for i in range(Q)]  #数字複数列、複数行

import itertools
A = 0
for i in itertools.combinations_with_replacement(range(M),N):
    score = 0
    for j in range(len(Q)):
        if i[Q[j][1]-1] - i[Q[j][0]-1] == Q[j][2]:
            score += Q[j][3]
        if score > A:
            A = score

print(A)

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

#define INF 1001001001
#define LINF 1001001001001001001
#define MOD 1000000007
#define MOD2 998244353

#define all(a)  (a).begin(),(a).end()
#define dig(n)  to_string(n).length()

#define pb push_back
#define eb emplace_back
#define mkp make_pair

#define se second
#define fi first

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<pii,pii> ppii;

int main(){

    bool flag=false;
    ll ans=0,sum=0;

    ll n,a,b;
    cin >>n>>a>>b;
    ll dif=b-a;

    if(dif%2==0){
        ans = (b-a)/2;
    }else{
        ll q = min(b-1,n-a);
        ll p = min(a,n-b+1);

        ans = min(q,p + (dif-1)/2);
    }

    //cout <<fixed<<setprecision(16)<< << endl;
    cout << ans << endl;

    //if(flag)cout << "Yes" <<endl;
    //else cout << "No" <<endl;
    
    return 0;
}


Measured memory_bytes: 256
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]%39;
   a=("0"+a).slice(-2);
   if(a=="00")a="39";
   console.log("3C"+a);
}

Measured memory_bytes: 7644
class UnionFind:
    def __init__(self, N):
        self.N = N

        # the parent of all node is itself
        # self.parent = list(range(N))
        self.parent = [-1] * N

    def root(self, i):
        if self.parent[i] < 0:
            return i

        r = self.root(self.parent[i])
        self.parent[i] = r
        return r

    def unite(self, i, j):
        i = self.root(i)
        j = self.root(j)

        if i == j:
            return

        if i > j:
            i, j = j, i

        self.parent[i] += self.parent[j]
        self.parent[j] = i
        # print(self.parent)

    def same(self, i, j):
        return self.root(i) == self.root(j)

    def size(self, i):
        return -self.parent[self.root(i)]

    def roots(self):
        return [self.root(i) for i in range(self.N)]

N, K, L = map(int, input().split())

road = UnionFind(N)
rail = UnionFind(N)

for _ in range(K):
    p, q = map(int, input().split())
    p += -1
    q += -1
    road.unite(p, q)

for _ in range(L):
    r, s = map(int, input().split())
    r += -1
    s += -1
    rail.unite(r, s)

pair = {}
for i,j in zip(road.roots(), rail.roots()):
    t = (i, j)

    if t in pair:
        pair[t] += 1
    else:
        pair[t] = 1

ans = ' '.join([str(pair[(i, j)]) for i,j in zip(road.roots(), rail.roots())])
print(ans)

Measured memory_bytes: 73440
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (int i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define REPALL(i,v) for (int i = 0; i < v.size(); i++)
#define RREPALL(i,v) for (int i = v.size()-1; i >= 0; i--)
#define SORT(v) sort(v.begin(), v.end())
#define MIN_ELEMENT(v) min_element(v.begin(), v.end())
#define MAX_ELEMENT(v) max_element(v.begin(), v.end())
#define COUNT(v,n) count(v.begin(), v.end(), n);
void YES(bool flag) {cout<<(flag ? "YES" : "NO")<<endl;}
void Yes(bool flag) {cout<<(flag ? "Yes" : "No")<<endl;}
void yes(bool flag) {cout<<(flag ? "yes" : "no")<<endl;}
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

vector<int> w;
int main()
{
    int n, s1 = 0, s2 = 0;
    cin>>n;
    w.resize(n);
    REP(i,n) {
        cin>>w[i];
        s2 += w[i];
    }
    //SORT(w);
    int ans = INF;
    REP(i,n-1) {
        s1 += w[i];
        s2 -= w[i];
        ans = min(ans, abs(s1 - s2));
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.io.PrintWriter;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Math.*;
import static java.lang.System.*;

public class Main {
	public static int MAX = 100000;
	public static void main(String[] args) {
		int i,j;
		Scanner sc = new Scanner(in);
		int x = parseInt(sc.next());
		sc.close();
		int ans=0;
		if(x==2) {
			ans=2;
		} else {
			if(x%2==0) x++;
			for(;;) {
				boolean f=true;
				for(i=3;i*i<=x;i+=2) {
					if(x%i==0) {
						f=false;
						break;
					}
				}
				if(f) {
					ans=x;
					break;
				}
				x+=2;
			}
		}
		out.println(ans);
	}
}


Measured memory_bytes: 21844
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

x = [int(input()) for _ in range(5)]
ans, x = sum(i for i in x if i % 10 == 0), [i for i in x if i % 10 > 0]
if len(x) == 0:
    print(ans)
else:
    x.sort(key=lambda i: i % 10, reverse=True)
    print(ans + sum((i // 10 + 1) * 10 for i in x[:-1]) + x[-1])



Measured memory_bytes: 65644
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
map<string,int> mp;
int main(){
	int n;
	cin >> n;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		mp[s]++;
	}
	int maxx = 0;
	map<string,int>::iterator it;
	for(it = mp.begin();it != mp.end(); it++){
        maxx = max(maxx,it->second);
    }
    for(it = mp.begin();it != mp.end(); it++){
        if(it->second == maxx) cout << it->first << endl;
    }
	return 0;
}

Measured memory_bytes: 25216
a = input()
b = a[0] + a[1]
c = a[0] + a[2]
d = a[1] + a[2]
if b == "AB" or b == "BA":
    print("Yes")
elif c == "AB" or c == "BA":
    print("Yes")
elif d == "AB" or d == "BA":
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3060
function B167(_input) {
    var lines = _input.split("\n");
    var first = lines.shift().split(' ').map(v => v*1);
    var sum = 0;

    if (first[0] >= first[3]) {
        console.log(first[3]);
    } else {
        sum = first[0];
        first[3] -= first[0];

        if (first[1] >= first[3]) {
            console.log(sum);
        } else {
            first[3] -= first[1];
            sum -= first[3];
            console.log(sum);
        }
    }
}
B167(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 29852
H,W = map(int,input().split())

dic = {}

for i in range(H):

    s =  input()

    for i in s:
        if i not in dic:
            dic[i] = 1
        else:
            dic[i] += 1

n4 = 0
n2 = 0
n1 = 0

for i in dic:

    n4 += dic[i] // 4
    dic[i] = dic[i] % 4
    n2 += dic[i] // 2
    dic[i] = dic[i] % 2
    n1 += dic[i]


if H % 2 == 0 and W % 2 == 0:

    if n2 == 0 and n1 == 0:
        print ("Yes")
    else:
        print ("No")

elif H % 2 == 0 and W % 2 == 1:

    if n1 == 0 and n2 <= H // 2:
        print ("Yes")
    else:
        print ("No")

elif H % 2 == 1 and W % 2 == 0:

    if n1 == 0 and n2 <= W // 2:
        print ("Yes")
    else:
        print ("No")

else:

    if n1 <= 1 and n2 <= H // 2 + W // 2:
        print ("Yes")
    else:
        print ("No")


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

#define  int        long long int
 #define    sp(x)                 setprecision(x)
#define    bp(x)           __builtin_popcount(x)
 #define  ld         long double
 #define  f(i,j,n)            for(int i = j; i <= n; i++)
#define  r(i,n,j)       for(int i = n; i >= j; i--)
 #define  all(container)       container.begin() , container.end()
#define  sz(container)   (int)container.size()
 #define  ff             first
#define  ss             second
 #define  pii      pair <int , int>
#define  sp(x)       setprecision(x)
 #define  endl    "\n"
#define  mod  1000000007
 #define  pb    push_back
#define  mp    make_pair
 #define  T     int ttt; cin >> ttt; while(ttt--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

int power(int x, int y, int p)  
{  
    int res = 1;    
    x = x % p; 
    if (x == 0) return 0;
    while (y > 0)  
    {     
        if (y & 1)  
            res = (res*x) % p;  
        y = y>>1; 
        x = (x*x) % p;  
    }  
    return res; 
}

int32_t main()
{
    fast
    int l,r,d;
    cin>>l>>r>>d;
    cout<<r/d - (l-1)/d;
}

Measured memory_bytes: 3636
#![allow(unused_imports)]
#![allow(dead_code)]
use std::io::prelude::*;
use std::cmp::{max, min};

const MOD: i64 = 1e9 as i64 + 7; 

fn main(){
    let a: isize = read();
    let b: isize = read();
    let c: isize = read();
    let ans = std::cmp::max(c - (a - b), 0);
    println!("{}", ans);
}

fn read<T>() -> T
where
    T: std::str::FromStr,
{
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


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

using namespace std;

template<class T> inline void chmax(T &x, const T &y) {if (x < y) x = y;}

int n, m, ans, memo[100005];
vector<int> v[100005];

int dfs(int x)
{
  if (memo[x] != 0) return memo[x];
  int dist = 0;
  for (int y: v[x])
    chmax(dist, dfs(y));
  return memo[x] = dist+1;
}

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

  while (m--) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
  }
  
  for (int i = 1; i <= n; ++i) {
    chmax(ans, dfs(i));
  }
  --ans;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 8192
import sys

def possible():
    print("POSSIBLE")
    sys.exit()

def main():
    N, M = map(int, input().split())
    hashdict = dict()
    for i in range(1009):
        hashdict[i] = list()
    alist = list()
    for i in range(M):
        a, b = map(int, input().split())
        if a == 1:
            temp = b % 1009
            hashdict[temp].append(b)
        if b == N:
            alist.append(a)

    for start in alist:
        temp = start % 1009
        if start in hashdict[temp]:
            possible()
    print("IMPOSSIBLE")

if __name__ == "__main__":
    main()

Measured memory_bytes: 11356
//
//  main.cpp
//  B - Reverse and Compare
//
//  Created by wenhan on 2017/8/26.
//  Copyright © 2017年 wenhan. All rights reserved.
//

#include <iostream>
#include <cstdio>
using namespace std;
long long a[27]={0};
int main() {
    string s;
    cin>>s;
    long long t=s.length();
    long long ss=1+(t*(t-1))/2;
    for(int i=0;i<t;i++)
    {
        a[s[i]-'a'+1]++;
    }
    for(int i=1;i<=26;i++)
        if(a[i]!=0)
        ss-=(a[i]*(a[i]-1))/2;
    printf("%lld\n",ss);
    // insert code here...
    //std::cout << "Hello, World!\n";
    return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define maxn 100005
int dp[maxn];
int h[maxn];
int n;
int main(){
    scanf("%d",&n);
    memset(dp,0,sizeof(dp));
    memset(h,0,sizeof(h));
    for(int i=1;i<=n;i++)
        scanf("%d",&h[i]);
    for(int i=1;i<=n;i++){
        if(i-1>=1&&i-2>=1)
            dp[i] = min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1]));
        else if(i-1>=1) dp[i] = dp[i-1]+abs(h[i]-h[i-1]);
        else if(i-2>=1) dp[i] = dp[i-2]+abs(h[i]-h[i-2]);
    }
    printf("%d\n",dp[n]);
    return 0;
}


Measured memory_bytes: 1024
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
s = sr()
ans = inf
a = [0 for i in range(n)]
a1 = [0]
for i,c in enumerate(s):
    if c == 'W':
        a[i]=1
for num in a:
    a1.append(a1[-1]+num)
def sumArea(l, r):
    return a1[r]-a1[l]

for k in range(n+1):
    numW0k = sumArea(0,k)
    numRkn = n-k-sumArea(k,n)
    pre = 0
    if numW0k >= numRkn:
        pre = numRkn+(numW0k-numRkn)
    else:
        pre = numW0k+(numRkn-numW0k)
    ans = min(ans, pre)
print(ans)

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

int64 calc(int64 x, int64 dig){
	vector<int32> v;
	REP(i, dig){
		v.push_back(x%10);
		x /= 10;
	}
	int64 mini = 0, maxi = 0;
	sort(all(v));
	REP(i, dig){
		maxi = maxi*10+v[i];
	}
	for(int32 i = dig-1;i >= 0;i--){
		mini = mini*10+v[i];
	}
	return mini-maxi;
}

int32 memo[1123456];

int main(void){
	int64 n, d;
	while(cin >> n >> d && n+d){
		memset(memo, -1, sizeof memo);
		int64 cnt = 0;
		while(memo[n] == -1){
			memo[n] = cnt;
			n = calc(n, d);
			cnt++;
		}
		cout << memo[n] << " " << n << " " << cnt-memo[n] << endl;
	}
}




Measured memory_bytes: 11840
#include <stdio.h>
#include <math.h>
#define N 250

main(){
  int n,i,*k[N],x;

  scanf("%d",&n);

  for(i=1; i<=n; i++){
    scanf("%d",(k+i));
  }


  for(i=1; i<=n; i++){

    printf("node %d: ",i);
    printf("key = %d, ",*(k+i));

    if(i==1){
      printf("left key = %d, ",*(k+2*i));
      printf("right key = %d, \n",*(k+(2*i+1)));
    }
    

    else if(i!=1 && 2*i+1<=n){
      x=floor(i/2);
      printf("parent key = %d, ",*(k+x));
      printf("left key = %d, ",*(k+2*i));
      printf("right key = %d, \n",*(k+(2*i+1)));
    }
    else if(i!=1 && 2*i<=n){
  x=floor(i/2);
      printf("parent key = %d, ",*(k+x));
      printf("left key = %d, \n",*(k+2*i));
    }
    else{
      x=floor(i/2);
      printf("parent key = %d, \n",*(k+x));
    }
  }
  return 0;
}

Measured memory_bytes: 596
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int N,Q;
vector<int> son[100005];
int nepar[100005];
int main()
{
    while(scanf("%d%d",&N,&Q)==2&&(N!=0&&Q!=0))
    {
        for(int i=1;i<=N;i++)
            son[i].clear();
        long long ans=0;
        nepar[1]=1;
        int x;
        for(int i=2; i<=N; i++)
        {
            scanf("%d",&x);
            son[x].push_back(i);
            nepar[i]=1;
        }
        char str[5];
        while(Q--)
        {
            scanf("%s%d",str,&x);
            if(str[0]=='Q')
            {
                ans+=nepar[x];
            }
            else
            {
                int u=nepar[x];
                nepar[x]=x;
                queue<int> q;
                q.push(x);
                while(!q.empty())
                {
                    int v=q.front();
                    q.pop();
                    for(int i=0; i<son[v].size(); i++)
                    {
                        int t=son[v][i];
                        if(nepar[t]!=u)
                            continue;
                        nepar[t]=x;
                        q.push(t);
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }

    return 0;
}



Measured memory_bytes: 8924
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<set>
#include<map>

using namespace std;
long long MOD = 1000000007LL;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647



int main() {
	vector<long long> que;
	priority_queue<long long> ans;
	int X, Y, Z, K;
	scanf("%d %d %d %d\n", &X, &Y, &Z, &K);
	vector<long long> A;
	vector<long long> B;
	vector<long long> C;

	for (int i = 0; i < X; ++i) {
		long long a;
		scanf("%lld", &a);
		A.push_back(a);
	}
	for (int i = 0; i < Y; ++i) {
		long long b;
		scanf("%lld", &b);
		B.push_back(b);
	}
	for (int i = 0; i < Z; ++i) {
		long long c;
		scanf("%lld", &c);
		C.push_back(c);
	}
	sort(A.rbegin(), A.rend());
	sort(B.rbegin(), B.rend());
	sort(C.rbegin(), C.rend());
	
	for (int i = 0; i < A.size(); ++i) {
		for (int j = 0; j < B.size(); ++j) {
			que.push_back(A[i] + B[j]);
		}
	}
	int queSize = que.size();
	sort(que.rbegin(), que.rend());
	for (int i = 0; i < C.size(); ++i) {
		for (int j = 0; j < min(K, queSize); ++j) {
			ans.push(que[j] + C[i]);
		}
	}

	for (int j = 0; j < K; ++j) {
		printf("%lld\n", ans.top());
		ans.pop();
	}


	return 0;
}

Measured memory_bytes: 43128
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("filed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n: i32 = read();
    let l: i32 = read();
    let left = l;
    let right = l + n - 1;

    let eat = if right <= 0 {
        right
    } else if left >= 0 {
        left
    } else {
        0
    };

    let ans = (right + left) * (right - left + 1) / 2 - eat;

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


Measured memory_bytes: 2184
#include<iostream>
#include<set>
#include <bitset>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include<string>
#include<string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include<list>
#include <limits>
#include <numeric>
#include <type_traits>
#define int long long
#define ll long long
#define mod  1000000007
#define inf 1e17
#define rep(i,j,n) for(int i=j;i<n;i++)
#define P pair<int,int>
double pi = 3.141592653589793;
using namespace std;
//ここから始めよう
signed main(){
    int n,m;cin>>n>>m;
    P a[114514];
    rep(i,0,m){
        cin>>a[i].first>>a[i].second;
    }
    sort(a,a+m);
    int end=inf;
    int p=1;
    int ans=0;
    rep(i,0,m){
        p=a[i].first;
        end=min(end,a[i].second);
        if(p>=end){
            ans++;
            end=a[i].second;
        }
    }ans++;
    cout<<ans<<endl;
    return 0;
}

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

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, (n))
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

using namespace std;

typedef long long LL;

template<typename T>
ostream& operator << (ostream& os, const vector<T>& vec) {
    os << "[ ";
    REP(i, vec.size()) {
        os << vec[i] << " ";
    }
    os << "]";

    return os;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    
    int N, A, B;
    cin >> N >> A >> B;

    vector<double> v(N);
    REP(i, N) cin >> v[i];

    sort(v.begin(), v.end(), greater<LL>());

    double mv = 0;
    int l = 1;
    LL prev = -1;
    REP(i, A) {
        mv += v[i] / A;
        if (prev == v[i]) ++l;
        else {
            prev = v[i];
            l = 1;
        }
    }

    // count right
    int r = 0;
    FOR(i, A, N) {
        if (v[i] != v[A-1]) break;
        ++r;
    }

    // calc combination matrix
    int t = l+r+1;
    LL M[t][t];
    REP(i, t) REP(j, t) M[i][j] = 0;
    M[0][0] = 1;
    REP(i, l+r) {
        REP(j, i+1) {
            M[i+1][j] += M[i][j];
            M[i+1][j+1] += M[i][j];
        }
    }

    LL cnt = 0;
    if (l < A) {
        cnt += M[l+r][l];
    } else {
        // All values are the same.
        REP(i, min(B - A, r) + 1) {
            cnt += M[l+r][A+i];
        }
    }

    cout << fixed << setprecision(20);
    cout << mv << endl << cnt << endl;

}


Measured memory_bytes: 256
#include <iostream>

int main()
{
    int n;
    double x;
    std::string u;
    double sum = 0.0;

    std::cin >> n;

    for (int i = 0; i < n; i++) {
        std::cin >> x >> u;
        if (u == "JPY") {
            sum += x;
        } else {
            sum += x * 380000.0;
        }
    }

    std::cout << sum << std::endl;

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <stack>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define MAX 100
#define MOD 1000000007
#define pb push_back

stack<char> st;

int main() {
  string s;
  cin >> s;

  for (auto itr : s) {
    if (st.size() == 0) {
      st.push(itr);
    } else {
      if (itr == 'T' && st.top() == 'S') {
        st.pop();
      } else {
        st.push(itr);
      }
    }
  }

  cout << st.size() << endl;
}


Measured memory_bytes: 644
/*
 ID: bradyawn
 PROG: contest
 LANG: C++11
 */

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <deque>
#include <string>
#include <cmath>
#include <map>
#include <unordered_map>
#include <utility>
#include <set>
#include <unordered_set>
#include <ctime>
#include <queue>
#include <stack>
#include <bitset>
#include <random>
#include <cstring>
#include <complex>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int,int> i2;
typedef pair<ll,ll> ll2;

int type = 0;

int n, m;

struct S
{
    ll a;
    ll b;
    ll c;
    
    ll f() const
    {
        ll x = a, y = b, z = c;
        
        if (type & (1 << 0)) x *= -1;
        if (type & (1 << 1)) y *= -1;
        if (type & (1 << 2)) z *= -1;
        
        return x+y+z;
    }
    /*
        Intuitively finds a < b given the bitmask
        1 prioritizes min and 0 prioritizes max
     */
    friend bool operator<(const S &one, const S &two)
    {
        return one.f() < two.f();
    }
    
};

S arr[1001];

ll solve()
{
    sort(arr+1, arr+1+n);
    ll mn = 0, mx = 0;
    for (int i = 1; i <= m; i++) mn += arr[i].f();
    for (int i = 1; i <= m; i++) mx += arr[n-i+1].f();
    return max(abs(mx), abs(mn));
}

int main()
{
    //ifstream inf("");
    //ofstream outf("");
    //cout << setprecision(10);
    ios::sync_with_stdio(0); cin.tie(0);
    
    cin >> n >> m;
    
    for (int i = 1; i <= n; i++) cin >> arr[i].a >> arr[i].b >> arr[i].c;
    
    ll ret = 0;
    for (int i = 0; i < 8; i++)
    {
        type = i;
        ret = max(ret,solve());
    }
 
    cout << ret << '\n';
    
    return 0;
    
}





Measured memory_bytes: 256
#include <iostream>
#define MOD 1000000007
using namespace std;

long long power(int a, int b)
{
    if(b==0)
        return 1;
    if(b==1)
        return a%MOD;
    long long res=power(a,b/2)%MOD;
    if(b%2)
        return (((res*res)%MOD)*a)%MOD;
    return (res*res)%MOD;
}

int main() {
	long long n;
	cin>>n;
	if(n==1)
	{
	    cout<<0;
	    return 0;
	}
	long long ans=0;
	
	ans=((power(10,n)+power(8,n)-((2*power(9,n))%MOD))+MOD)%MOD;
	
	cout<<ans;
	
	return 0;
}


Measured memory_bytes: 3600
#include <iostream>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	int a[4][3][10];
	memset(a,0,sizeof(a));

	int n;
	cin >> n;

	rep(i,n)
	{
		int b,f,r,v;
		cin >> b >> f >> r >> v;
		b--;
		f--;
		r--;

		a[b][f][r] += v;
	}

	rep(i,4)
	{
		if(i != 0)
		{
			cout << "####################" << endl;
		}
		rep(j,3)
		{
			rep(k,10)
			{
				cout << " " << a[i][j][k];
			}
			cout << endl;
		}
	}
	return 0;
}

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

#define int                 long long
#define pb                  push_back
#define all(v)              v.begin(),v.end()
#define INF                 (int)1e16
#define speed               ios_base::sync_with_stdio(false);cin.tie(NULL);cout.flush()
#define MOD                 (int)(1e9+7)
#define fi                  first
#define se                  second
#define sq(a)               a*a
#define ld                  long double
#define Error(x)            cout<< #x << " = " << (x) <<endl;
#define Error2(a,b)         cout<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n";
#define Error3(a,b,c)       cout<<"("<<#a<<" , "<<#b<<" , "<<#c<<" ) = ( "<<(a)<<" , "<<(b)<<" , "<<(c)<<")\n";
#define Error4(a,b,c,d)     cout<<"("<<#a<<" , "<<#b<<" , "<<#c<<" , "<<#d<<" ) = \
                            ( "<<(a)<<" , "<<(b)<<" , "<<(c)<<" , "<<(d)<<")\n";
#define popcount            __builtin_popcountll
#define ign                 cin.ignore(numeric_limits<streamsize>::max(),'\n')
#define PI                  acos(-1.0)

int abso(int a) { return a>0 ? a : (-a); }
int cdiv(int a,int b) { return a/b + ( (a%b) > 0 ); }
int mul(int a,int b) { int res=0; while(b){ if(b&1) res=(res+a)%MOD; a=(a+a)%MOD; b=b/2; } return res; }
int bexp(int a,int b) { int res=1; while(b){ if(b&1) res=mul(res,a); a=mul(a,a); b=b/2; } return res; }
int inv(int a) { return bexp(a,MOD-2); }

int power(int a,int b) {
    int res=1;
    while(b) {
        if(b&1)
            res=res*a;
        a=a*a;
        b=b>>1;
    }
    return res;
}

void solve() {
    int n;
    cin>>n;
    string arr[n+1];
    map<string,int> mp;
    for(int i=1;i<=n;i++) {
        cin>>arr[i];
        sort(arr[i].begin(),arr[i].end());
        mp[arr[i]]++;
    }
    int ans=0;
    for(auto it:mp) {
        ans=ans+((it.second)*(it.second-1))/2;
    }
    cout<<ans<<"\n";
}

signed main() {
    speed;
    int t=1;
    //cin>>t;
    while(t--) {
        solve();
    }
}

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

namespace ConsoleApp15
{
    class Class117
    {
        static void Main()
        {
            string S = Console.ReadLine();
            long K = long.Parse(Console.ReadLine());
            string ans = "1";
            for(int i = 0; i < Math.Min(K, S.Length); i++)
            {
                if (S[i] != '1') { ans = S[i].ToString();break; }
            }
            Console.WriteLine(ans);
        }
    }
}


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

int main() {
    int N, K;
    cin >> N >> K;
    long long ans = 0;
    for (int i = 1; i <= N; i++) {
        long long tmp = 0;
        tmp += (N / i) * max(i - K, 0);
        tmp += max(N % i - K + 1, 0);
        if (K == 0 && tmp > 1) tmp--;
        ans += tmp;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
#include <iomanip>

using namespace std;
bool dp[6][100001];
int prices[6] = {100, 101, 102, 103, 104, 105};
int main(){
    int X;
    cin >> X;
    dp[0][0] = true;
    for(int i = 0; i <= X; i++){
        if(i - prices[0] >= 0 and dp[0][i - prices[0]]){
            dp[0][i] = true;
        }
    }
    for(int i = 1; i < 6; i++){
        for(int j = 0; j <= X; j++){
            if(dp[i - 1][j] || (j - prices[i] >= 0 && dp[i][j - prices[i]])){
                dp[i][j] = true;
            }
        }
    }

    if(dp[5][X]){
        cout << 1 << endl;
    } else {
        cout << 0 << endl;
    }
    return 0;
}



Measured memory_bytes: 768
#include <array>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <string>

using namespace std;

int main()
{
    int N, A, B;
    string S;
    cin >> N >> A >> B;
    cin >> S;
    int c = 0;
    int f = 1;
    for(int i=0; i<S.size(); i++) {
        if(S[i]=='a') {
            if(c < A+B) { c++;
            cout << "Yes" << endl;
            } else {
                cout << "No" << endl;
            }
        }
        else if(S[i] == 'b') {
            if(c < A+B && f <= B) {
                c++; f++;
                cout << "Yes" << endl;
            } else {
                cout << "No"<< endl;
            }
        } else {
            cout << "No" << endl;
        }
    }

}


Measured memory_bytes: 896
#include <stdio.h>

int main(){
	int N = 0, M = 0;
	int Nnum = 0, Mnum = 0, ans = 0;
	scanf("%d %d", &N, &M);
	ans = (N*(N - 1)/2) + (M*(M - 1)/2);
	printf("%d\n", ans);
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
vector<int> g[100006];
int a[100006];
/*
vector<int> G[100006];
G[x].push_back(y);
G[y].push_back(x);
*/
 
 
int main () {
    int N, M;
    int ans=0;
    cin >> N >> M;
    for (int i=1; i<=N; i++) {
        cin >> a[i];
    }
    for (int i=1; i<=M; i++) {
        int A, B;
        cin >> A >> B;
        g[A].push_back(B);
        g[B].push_back(A);
    }
    for (int i=1; i<=N; i++) {
    	int c=0;
    	int cnt=100007;
        if (g[i].size()>0) {
            for (int j=0; j<g[i].size(); j++) {
               if (a[i]>a[g[i][j]]) {
               		c++;
               		if (c==1) {
               			cnt=0;
               		}
               		cnt++;
               } 
            }
        }
        else {
        	ans++;
		}
        if (cnt==g[i].size()) {
        	ans++;
        	cnt=100007;
        	c=0;
        }
    }
	cout << ans << endl;
}
   

Measured memory_bytes: 9112
A,V = map(int,input().split())
B,W = map(int,input().split())
T = int(input())

X = abs(B-A)
Y = V-W

if Y <= 0:
    print('NO')
    exit()

if Y == 0:
    print('NO')
else:
    if Y*T >= X:
        print('YES')
    else:
        print('NO')

Measured memory_bytes: 9184
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <time.h>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define all(a) a.begin(),a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template<class T, class S> bool chmax(T &a, const S &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T, class S> bool chmin(T &a, const S &b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll n, ll m) { return (m ? gcd(m, n%m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m)*m; }

bool dp[81][81][30010];

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	//modcalc();

	int h, w;
	cin >> h >> w;
	vector<vector<int>> a(h, vector<int>(w));
	vector<vector<int>> b(h, vector<int>(w));
	vector<vector<int>> c(h, vector<int>(w));
	rep(i, h) rep(j, w) cin >> a[i][j];
	rep(i, h) rep(j, w) cin >> b[i][j];
	rep(i, h) rep(j, w) c[i][j] = abs(a[i][j] - b[i][j]);
	dp[0][0][15000 - c[0][0]] = true;
	dp[0][0][15000 + c[0][0]] = true;
	rep(i, h){
		rep(j, w) {
			rep(k, 30000) {
				if (i != 0) {
					if (k + c[i][j] <= 30000 && dp[i - 1][j][k + c[i][j]]) dp[i][j][k] = true;
					if (k - c[i][j] >= 0 && dp[i - 1][j][k - c[i][j]]) dp[i][j][k] = true;
				}
				if (j != 0) {
					if (k + c[i][j] <= 30000 && dp[i][j - 1][k + c[i][j]]) dp[i][j][k] = true;
					if (k - c[i][j] >= 0 && dp[i][j - 1][k - c[i][j]]) dp[i][j][k] = true;
				}
			}
		}
	}
	int ans = INF;
	for (int i = 0; i < 30000; i++) {
		if (dp[h - 1][w - 1][i]) {
			chmin(ans, abs(i - 15000));
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 189440
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
const long long INF = 1LL << 60;

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

    vector<ll> A(N);
    int cnt = 0, cnt1 = 0, cnt2 = 0;
    rep(i, N) {
        cin >> A[i];
        if (A[i] % 4 == 0) cnt2++;
        else if (A[i] % 2 == 0) cnt1++;
        else cnt++;
    }

    if (cnt1 % 2 == 1) cnt++;

    if (cnt <= cnt2 + 1) cout << "Yes" << endl;
    else cout << "No" << endl; 

    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define lli long long int
#define ld long double
#define fop(i,m,n) for (int i=(m);i<(n);i++)
#define FOP(i,m,n) for (int i=(m)-1;i>=(n);--i)
#define test(x) cout << #x << ' ' << x << endl;
#define printv(a) {\
    for (auto y : a) cout << y << ' ';\
    cout << '\n';\
}
#define eb emplace_back
#define pb push_back
#define mp make_pair 
#define mt make_tuple
#define pii pair<int,int> 
#define pll pair<lli,lli>
#define tiii tuple<int,int,int>
#define all(a) a.begin(), a.end()
#define X first
#define Y second
#define oset tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>
#define readfile freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); 
auto SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(SEED);
const int mod = 998244353, x = 864197532, N = 20, logN = 18, K = 350, all = (1 << N) - 1;

int main () {
	int k;
	cin >> k;
	int ans = 1, cur = 7 % k;
	while (ans < k + 87 && cur != 0) {
		cur = (cur * 10 + 7) % k;
		ans++;
	}
	if (ans == k + 87) ans = -1;
	cout << ans << endl;
}

Measured memory_bytes: 3636
n,k=map(int, input().split())
a=list(map(int, input().split()))
a2=[0]
for i in a:a2.append(a2[-1]+i)
c=[]
for i in range(0,n+1):
    for j in range(i+1,n+1):c.append(a2[j]-a2[i])

ans=0
for i in range(40,-1,-1):
    n2=2**i
    c2=[i for i in c if i&n2==n2]
    if len(c2)>=k:
        ans+=n2
        c=c2
print(ans)

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

#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define ll long long
#define pb push_back
#define zoom                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define pl pair<ll, ll>
#define MOD 1000000007
#define endl "\n"
#define ff first
#define ss second
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl
#define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl
#define trace(v)                                   \
    for (auto it = v.begin(); it != v.end(); it++) \
        cerr << *it << " ";                        \
    cerr << endl;
#define tr(container, it) \
    for (typeof(container.begin()) it = container.begin(); it != container.end(); it++)

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;

void solve()
{
    int n;
    cin>>n;
    vector<int> ans(10001,0);
    for(int i=1;i<=100;i++){
        for(int j=1;j<=100;j++){
            for(int k=1;k<=100;k++){
                int index=((i+j)*(i+j)+(j+k)*(j+k)+(k+i)*(k+i))/2;
                if(index>10000){
                    continue;
                }

                ans[index]++;
            }
        }
    }

    for(int i=1;i<=n;i++){
        cout<<ans[i]<<endl;
    }


}

int main()
{
    zoom;
    ll t=1;
    //cin >> t;

    while (t--)
    {
        solve();
    }
}

Measured memory_bytes: 3672
import java.util.Scanner;
import java.io.IOException;
class Main {
    public static void main(String[] args) throws IOException{
        Scanner scan = new Scanner(System.in);
        int m = scan.nextInt();
 
        Node root = null;
        for(int i = 0;i < m;i++){
            String str = scan.next();
            if(str.equals("insert")){
                int temp = scan.nextInt();
                Node node = new Node(temp);
                if(root == null){
                    root = node;
                    continue;
                }
                insert(root, node);
            }else if(str.equals("find")){
                int temp = scan.nextInt();
                if(root.search(temp)){
                    System.out.println("yes");
                }else{
                    System.out.println("no");
                }
            }else if(str.equals("print")){
                Print(root);
            }
        }
    }
     
    /**  ?????\???????????? */
    public static void insert(Node T, Node Z){
        Node parent = T;
        for(;;){
            if(Z.key < parent.key){
                if(parent.left == null){
                    parent.left = Z;
                    break;
                }
                parent = parent.left;
            }else{
                if(parent.right == null){
                    parent.right = Z;
                    break;
                }
                parent = parent.right;
            }
        }
    }
     
    /** ?????????????????? */
    public static void Print(Node root){
        StringBuilder sb = new StringBuilder();
        root.intermediateOrderCrawl(sb);
        System.out.println(sb.toString());
        sb = new StringBuilder();
        root.precedingOrderCrawl(sb);
        System.out.println(sb.toString());
    }
}
 
/**  ???????????????????????? */
class Node{
    int key;
    Node parent;
    Node left;
    Node right;
     
    public Node(int key) {
        this.key = key;
    }
     
   /**  ??????????????????????????? */
    public void intermediateOrderCrawl(StringBuilder sb){
        if(left != null) left.intermediateOrderCrawl(sb);
        sb.append(" " + String.valueOf(key));
        if(right != null) right.intermediateOrderCrawl(sb);
    }
     
    /** ??????????????????????????? */
    public void precedingOrderCrawl(StringBuilder sb){
        sb.append(" " + String.valueOf(key));
        if(left != null) left.precedingOrderCrawl(sb);
        if(right != null) right.precedingOrderCrawl(sb);
    }
     
   /** findKey search???????????? */
    public boolean search(int findKey){
        if(findKey == key){
            return true;
        }else if(findKey < key){
            if(left == null){
                return false;
            }
            return left.search(findKey);
        }else{
            if(right == null){
                return false;
            }
            return right.search(findKey);
        }
    }
}

Measured memory_bytes: 314744
#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

bool kabe[31][31][31][31];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool vis[31][31];
int memo[31][31];
int w, h;
void solve(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nx = x+dx[i];
    int ny = y+dy[i];

    if (nx < 0 || ny < 0 || nx >= w || ny >= h ||
	(memo[nx][ny] != -1 && memo[x][y]+1 >= memo[nx][ny]) ||
	vis[nx][ny] ||
	kabe[x][y][nx][ny]) {
      continue;
    }

    vis[nx][ny] = true;
    memo[nx][ny] = memo[x][y]+1;
    solve(nx, ny);
    vis[nx][ny] = false;
  }
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0) break;

    memset(kabe, false, sizeof(kabe));
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < 2*h-1; i++) {
      int l = i%2 ? w : w-1;
      for (int j = 0; j < l; j++) {
	int t;
	cin >> t;
	if (t == 1) {
	  if (i%2) {
	    // std::cout << "(" << j << "," << i/2 << ")と(" << j << "," << i/2+1 << ")に壁がある。" << std::endl;
	    kabe[j][i/2][j][i/2+1] = true;
	    kabe[j][i/2+1][j][i/2] = true;
	  }else {
	    // std::cout << "(" << j << "," << i/2 << ")と(" << j+1 << "," << i/2 << ")に壁がある。" << std::endl;
	    kabe[j][i/2][j+1][i/2] = true;
	    kabe[j+1][i/2][j][i/2] = true;
	  }
	}
      }
    }
    memset(memo, -1, sizeof(memo));

    vis[0][0] = true;
    solve(0, 0);

    if (memo[w-1][h-1] == -1) {
      std::cout << 0 << std::endl;
    }else {
      std::cout << memo[w-1][h-1]+2 << std::endl;
    }
  }
}

Measured memory_bytes: 2104
// atcoder 
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
    int N, T;
    cin >> N >> T;
    vector<int> c(N), t(N);
    for (int i=0; i<N; i++) cin >> c[i] >> t[i];
    int c_min = 1001;
    for (int i=0; i<N; i++){
        if (t[i] <= T){
            c_min = min(c_min, c[i]);
        }
    }
    if (c_min == 1001) cout << "TLE" << endl;
    else cout << c_min << endl;

    return 0;
}


Measured memory_bytes: 256
'use strict'

function main(input) {
  input = input
    .trim()
    .split('\n')
    .map(v => v.split(' ').map(Number))

  const N = input[0][0]
  const monsters = input[1].sort((a, b) => a - b)

  let tmp = Number.MAX_SAFE_INTEGER

  const gcd = (a, b) => {
    return b === 0 ? a : gcd(b, a % b)
  }

  for (let i = 0; i < N - 1; i++) {
    const _ = gcd(monsters[i], monsters[i + 1])
    tmp = Math.min(_, tmp)
  }

  console.log(tmp)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'))


Measured memory_bytes: 31276
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);

#define REP(i,n)	for(int i=0;i<(int)n;++i)
#define ALL(c)		(c).begin(),(c).end()
#define CLEAR(v)	memset(v,0,sizeof(v))
#define MP(a,b)		make_pair((a),(b))
#define ABS(a)		((a)>0?(a):-(a))
#define FOR(i,s,n)	for(int i=s;i<(int)n;++i)

int n, p;
ll c[51][51];

int main(int argc, char **argv) {
	REP(i, 51) c[i][0] = c[i][i] = 1;
	FOR(i, 1, 50) REP(j, i) c[i + 1][j + 1] = c[i][j + 1] + c[i][j];
	cin >> n >> p;
	int o = 0, e = 0;
	REP(i, n) {
		int a;
		cin >> a;
		if (a % 2 == 0) ++e;
		else ++o;
	}
	if (p == 0) {
		ll res = 0;
		for (int i = 0; i <= o; i += 2) res += c[o][i];
		res *= (ll)1 << e;
		cout << res << endl;
	}
	else {
		ll res = 0;
		for (int i = 1; i <= o; i += 2) res += c[o][i];
		res *= (ll)1 << e;
		cout << res << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
 
using namespace std;
 
int main(){
	
char a,b,c;
char d,e,f;
int ai,bi,ci;

cin >> a >> b >> c;
cin >> d >> e >> f;

if(a==d){
	ai=1;
}else{
	ai=0;
}

if(b==e){
	bi=1;
}else{
	bi=0;
}

if(c==f){
	ci=1;
}else{
	ci=0;
}

cout << (ai+bi+ci) << endl;
   
return 0;
}


Measured memory_bytes: 3540
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
 
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <numeric>
#include <time.h>
#include <stack>
#include <bitset>

#define prev asasddlsa
#define rank aljds

#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i >= l; i++)
 
using namespace std;
 
typedef long long ll;
typedef double dbl;
 
template<typename T>
void print(const vector<T>& s){
    for (auto x : s)
        cout << x << " ";
    cout << endl;
}
 
template<class T>   
void print(const T* s, int n){
    for (int i = 0; i < n; i++)
        cout << s[i] << ' ';
    cout << endl;
}

template<class T>
void print(vector<vector<T>> s){
    for (int i = 0; i < s.size(); i++)
        print(s[i]);
}

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

int solve(){
    string s;
    cin >> s;
    cout << s.substr(0, 3) << endl;
    return 0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(10);
    cout << fixed; 
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; i++){
        // cout << "Case #" << i + 1 << ": ";
        solve();
    }
    return 0;
}


Measured memory_bytes: 3656