akhauriyash/Code-Regression huggingface dataset:
def func(square, x = square.size / 2, y = square.size / 2 + 1, num = 1)
  if square[y][x]
    func(square, (x - 1) % square.size, (y + 1) % square.size, num)
  else
    square[y][x] = num
    if num < (square.size ** 2)
      func(square, (x + 1) % square.size, (y + 1) % square.size, num + 1)
    end
  end
end
def main(input = STDIN)
  while (n = input.gets.to_i) != 0
    square = Array.new(n){Array.new(n)}
    func(square)
    puts square.map{|colum|colum.map{|f|f.to_s.rjust(4, " ")}.join}
  end
end
main

Measured memory_bytes: 6136
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 200005;

template<typename V> class segtree {
private:
    int n,sz;
    vector<V> node;
public:
    segtree(vector<V>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1,numeric_limits<V>::max());
        rep(i,sz){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = min(node[i*2+1],node[i*2+2]);
        }
    }
    void update(int k,V a)
    {
    	k += n-1;
    	node[k] = a;
    	while(k>0){
    		k = (k-1)/2;
    		node[k] = min(node[2*k+1],node[2*k+2]);
    	}
    }
    V query(int a,int b,int k=0,int l=0,int r=-1)
    {
        if(r < 0) r = n;
    	if(r <= a || b <= l){
    		return numeric_limits<V>::max();
    	}
    	if(a <= l && r <= b){
    		return node[k];
    	}else{
    		V vl = query(a,b,2*k+1,l,(l+r)/2);
    		V vr = query(a,b,2*k+2,(l+r)/2,r);
    		return min(vl,vr);
    	}
    }
    V get(int k){
        return node[k+n-1];
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};

vector<int> r[MAX_N];
int sm[MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vi b(n);
    rep(i,n){
        cin >> b[i];
    }
    int q;
    cin >> q;
    rep(i,q){
        int a,b;
        cin >> a >> b;
        r[a-1].pb(b);
    }
    rep(i,n){
        sm[i+1] = sm[i] + !b[i];
    }
    vi hoge(n+1,INF);
    segtree<int> sg(hoge);
    sg.update(0,0);
    rep(i,n){
        rep(j,len(r[i])){
            sg.update(r[i][j],sg.query(i,r[i][j]+1));
        }
        sg.update(i+1,min(sg.get(i)+(b[i]?1:-1),sg.get(i+1)));
    }
    cout << sg.get(n) + sm[n] << "\n";
    return 0;
}


Measured memory_bytes: 13312
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <functional>
#include <limits.h>
#include <set>
#include <map>
#include <tuple>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i,N) for (ll i=0;i<N;i++)
#define loop(i,N,M) for(ll i=N;i<M;i++)
#define MAX(v) *max_element(v.begin(),v.end())
#define MIN(v) *min_element(v.begin(),v.end())
#define SORTL2S(type,v) sort(v.begin(),v.end(),greater<type>())
#define SORTS2L(type,v) sort(v.begin(),v.end())
#define SORTMF(v,func) sort(v.begin(),v.end(),func)

//template<typename A, size_t N, typename T>
//void Fill(A(&array)[N], const T &val) {
	//std::fill((T*)array, (T*)(array + N), val);
//}

//解法
void solve() {

	ll N,K,Q;
	cin >> N >> K >> Q;

	vector<ll> ansnum(N,0);

	rep(i,Q){
		ll a;
		cin >> a;
		ansnum[a-1]++;
	}

	rep(i,N){
		if (K-Q+ansnum[i] <= 0) {
			cout << "No" << endl;
		} else {
			cout << "Yes" << endl;
		}
	}

}

int main() {

	//入力

	solve();
	cout << endl;
	return 0;
}

Measured memory_bytes: 1408
#include <bits/stdc++.h>
 
using namespace std;
 
template< class t>
long long int gcd( t g,t h )
{
    return ( h==0 ? g : gcd(h,g%h) );
}
 

struct Comp {
  bool operator()(const std::pair<int, int> &a, const std::pair<int, int> &b) {
    if (a.first != b.first) {
      return a.first < b.first;
    }
    return a.second > b.second;
  }

}comp_functor;
 
 
int main() 
{
    ios_base::sync_with_stdio(false) ; cin.tie(0) ;
    
    long long int i,j,ans=1,c,a[300005],n,f=0,o=1,x;
    
    string s;
    
    cin>>n;
    
    for(i=0;i<n;i++)
    {
        cin>>x;
        ans*=3;
        if(x%2==0)
        {
            o*=2;
        }
        else
        {
             o*=1;
        }
    }
    
    
    cout<<ans-o;
    
    return 0;
}
 

Measured memory_bytes: 256
#include <cstdio>

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

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int ans=0;
        rep(a,10){
            rep(b,10){
                rep(c,10){
                    rep(d,10){
                        if(a+b+c+d==n)
                            ans++;
                    }
                }
            }
        }

        printf("%d\n",ans);
    }
}

Measured memory_bytes: 1024
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]
for i in range(n):
  a.append(a[i]+A[i])
for i in range(m):
  b.append(b[i]+B[i])

ans, j = 0, m
for i in range(n+1):
  if a[i] > k:
    break
  while b[j] > k-a[i]:
    j -= 1
  ans = max(ans, i+j)
  
print(ans)

Measured memory_bytes: 47604
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for(int i=0; i<n; i++)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {if(b>a) return b; else return a;}
int min(int a, int b) {if(b<a) return b; else return a;}


signed main() {
    string S;
    cin >> S;

   if(S=="SUN") cout << 7 << endl;
   else if(S=="MON") cout << 6<< endl;
   else if(S=="TUE") cout << 5 << endl;
   else if(S=="WED") cout << 4 << endl;
   else if(S=="THU") cout << 3 << endl;
   else if(S=="FRI") cout << 2 << endl;
   else if(S=="SAT") cout << 1 << endl; 
    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;


int aa[200000],bb[200000],cc[2000000];
 
void t(){
  aa[0]=aa[1]=1;
  bb[0]=bb[1]=1;
  cc[1]=1;
  repi(i,2,200000){
    aa[i]=aa[i-1]*i%MOD;
    cc[i]=MOD-cc[MOD%i]*(MOD/i)%MOD;
    bb[i]=bb[i-1]*cc[i]%MOD;
  }
}
 
int calc(int n,int k){
  if(n<k) return 0;
  if(n<0 || k<0) return 0;
  return aa[n]*(bb[k]*bb[n-k]%MOD)%MOD;
}


int solve(string s,int k){
  if(k==0) return 1;
  if(s=="") return 0;
  int num=1;
  rep(i,k) num*=9;
  int ans=0;
  if(s[0]=='0') ans+=solve(s.substr(1),k);
  else{
    ans+=calc(s.size() - 1,k)*num;
    //cout<<s<<" "<<k<<" "<<ans<<endl;
    ans+=(int)(s[0]-'0'-1)*(calc(s.size() - 1,k-1))*(num/9);
    //cout<<s<<" "<<k<<" "<<ans<<endl;
    ans+=solve(s.substr(1),k-1);
    //cout<<s<<" "<<k<<" "<<ans<<endl;
  }
  return ans;
}

signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  t();
  string a;
  int b;
  cin>>a>>b;
  cout<<solve(a,b)<<endl;
  

  return 0;
}


Measured memory_bytes: 6272
#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;



int main()
{
        string s;
        cin >> s;
        string ans;
        rep(i, 0, len(s)) {
                if (odd(i)) continue;
                else ans += s[i];
        }
        cout << ans << endl;
}

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

int main(){
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(0);
	int n;
	cin >> n;
	int pri = -1;
	bool ans = true;
	int x;
	while(n--){
		cin >> x;
		if(x > pri){
			pri = x - 1;
		}
		else if(x == pri){
			pri = x;	
		}
		else{
			ans = false;
			break;
		}
	}
	cout << (ans?"Yes":"No") << '\n';
}


Measured memory_bytes: 256
/*
 Author:Alokit Kumar
   */
  #include<bits/stdc++.h>
  using namespace std;
  #define int long long 
  int32_t main()
  {
   int n,i,j,k,l=0;
   string s1,s2;
   cin>>s1;
   cin>>s2;
   if(s1[0]==s2[0]) l++;
   if(s1[1]==s2[1]) l++;
   if(s1[2]==s2[2]) l++;
   cout<<l;
  }

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

// types
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;

// macros
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

bool primeCheck(ll x){
  ll now = x;
  for(ll i = 2; i <= sqrt(x); i++){
    while(now%i == 0)
      now /= i;
  }

  return now == x;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;

  vll prime;
  for(ll i = 2; i <= 55555; i++){
    if(primeCheck(i))
      prime.push_back(i);
  }

  ll cnt = 0;
  REP(i, prime.size()){
    if(cnt == n)
      break;
    if(prime[i]%5 == 1){
      cout << prime[i] << " ";
      cnt++;
    }
  }

  cout << endl;
}


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

using namespace std;
typedef long long ll;

void solve();
int n, k;
string s;
int a, b, c, d;

int main()
{
    cin >> s;
    int a = s[0] - '0';
    int b = s[1] - '0';
    int c = s[2] - '0';
    int d = s[3] - '0';
    //solve();
    if (a + b + c + d == 7)
    {
        printf("%d+%d+%d+%d=7\n", a, b, c, d);
    }
    else if (a + b + c - d == 7)
    {
        printf("%d+%d+%d-%d=7\n", a, b, c, d);
    }
    else if (a + b - c + d == 7)
    {
        printf("%d+%d-%d+%d=7\n", a, b, c, d);
    }
    else if (a - b + c + d == 7)
    {
        printf("%d-%d+%d+%d=7\n", a, b, c, d);
    }
    else if (a + b - c - d == 7)
    {
        printf("%d+%d-%d-%d=7\n", a, b, c, d);
    }
    else if (a - b + c - d == 7)
    {
        printf("%d-%d+%d-%d=7\n", a, b, c, d);
    }
    else if (a - b - c + d == 7)
    {
        printf("%d-%d-%d+%d=7\n", a, b, c, d);
    }
    else if (a - b - c - d == 7)
    {
        printf("%d-%d-%d-%d=7\n", a, b, c, d);
    }
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <queue>
#include <stack>
#include <string.h>
#include <list>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0; i < n; i++)
#define repR(i,n) for(int i = n; i >= 0; i--)
#define FOR(i,m,n) for(int i = m; i < n; i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main(){
    int x[3],ans;
    rep(i,3) cin >> x[i];
    sort(x,x+3);
    ans = x[2] * 10 + x[1] + x[0];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(),count=0;
		int[] height = new int[n];
		for(int i=0;i<n;i++) height[i]=sc.nextInt();
		while(!all_zero(n,height)) {
			for(int i=0;i<n;i++) {
				if(i==0 && height[0]!=0) count++;
				if(i!=0 && height[i]!=0 && height[i-1]==0) count++;
			}
			for(int i=0;i<n;i++) {
				if(height[i]!=0) height[i]--;
			}
		}
		System.out.println(count);
	}

	static boolean all_zero(int n, int[] array) {
		for(int i=0;i<n;i++) {
			if(array[i]!=0) return false;
		}
		return true;
	}
}

Measured memory_bytes: 21588
#include <iostream>
#include <string>
using namespace std;
int main(){
	int H,W;
	char c,tail[101][101];
	while(cin>>H>>W&&H!=0&&W!=0){
		bool z[101][101]={0};
		int x=0,y=0,cnt=0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				cin>>tail[i][j];
			}
		}
		for(int i=0;i<H*W;i++){
			if(tail[x][y]=='>'){
				if(z[x][y]==1)break;
				z[x][y]=1;
				y+=1;
			}else if(tail[x][y]=='<'){
				if(z[x][y]==1)break;
				z[x][y]=1;
				y-=1;
			}else if(tail[x][y]=='^'){
				if(z[x][y]==1)break;
				z[x][y]=1;
				x-=1;
			}else if(tail[x][y]=='v'){
				if(z[x][y]==1)break;
				z[x][y]=1;
				x+=1;
			}else{
				cnt=1;
				cout<<y<<" "<<x<<endl;
				break;
			}
		}
		if(cnt==0)cout<<"LOOP"<<endl;
	}
	return 0;
}

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

int main() {
	int N;
	int res = 0;
    cin >> N;
    vector <int> s(N);
    for (int i=0; i<N; i++) cin >> s.at(i);
    sort(s.begin(), s.end());
    for (int i=0; i<N; i++) res += s.at(i);
    bool isok=false;
    if (res%10==0) {
        for (int i=0; i<N; i++) {
            if (s.at(i)%10!=0) {
                res -= s.at(i);
                isok = true;
                break;
            }
        }
        if (!isok) res = 0;
    }
    cout << res;
}

Measured memory_bytes: 3580
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long m = sc.nextLong();
        ArrayList<Store> sl = new ArrayList<>();
        for(int i = 0; i < n; i++){
            long a = sc.nextLong();
            long b = sc.nextLong();
            Store s = new Store(a,b);
            sl.add(s);
        }
        Collections.sort(sl);
        long ans = 0;
        for(Store s : sl){
            if(m <= s.b){
                ans += m * s.a;
                break;
            }else{
                ans += s.b * s.a;
                m -= s.b;
            }
        }
        System.out.println(ans);
    }
}

class Store implements Comparable<Store>{
    long a;
    long b;
    public Store(long a, long b){
        this.a = a;
        this.b = b;
    }
    public int compareTo(Store s2){
        if(this.a > s2.a){
            return 1;
        }else if(this.a < s2.a){
            return -1;
        }else{
            return 0;
        }
    }
}


Measured memory_bytes: 69492
#include <iostream>
#include <stdio.h>

using namespace std;

int main(void) {
  int N, M;
  int *S, *T;
  long long max = 0, tmp;
  long long **dp, **sum;
  int imax = 0, jmax = 0;
  
  
  cin >> N >> M;
  S = new int[N+1];
  T = new int[M+1];
  sum = new long long *[N+1];
  dp = new long long *[N+1];
  for (int i = 0; i < N+1; i++) {
    sum[i] = new long long[M+1];
    dp[i] = new long long[M+1];
  }
  for (int i = 0; i < N; i++) {
    cin >> S[i+1];
  }
  for (int i = 0; i < M; i++) {
    cin >> T[i+1];
  }
  
  for (int i = 0; i <= N; i++) 
    sum[i][0] = 1;
  for (int j = 0; j <= M; j++) 
    sum[0][j] = 1;
  
  max = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      /*sum[i][j] = (sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
        		  + dp[i][j]) % (1000000007);
                  */
      if (S[i] == T[j]) {
        sum[i][j] = sum[i-1][j] + sum[i][j-1];
      } else {
        sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + 1000000007;
      }
      sum[i][j] = sum[i][j] % 1000000007;
    }
  }
  cout << sum[N][M] << endl;
  
  return 0;
}
  

Measured memory_bytes: 39680
#include<iostream>
#include<cstdio>
using namespace std;
char s,o;
int main()
{
	s=getchar();
	while(s!='\n')
	 {
	 	if(s=='C') o='C';
	 	if(s=='F'&&o=='C') {cout<<"Yes";return 0;}
	 	s=getchar();
	 }
	cout<<"No";
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cassert>
#define ll long long
using namespace std;
inline ll read(){
    ll re=0,flag=1;char ch=getchar();
    while(ch>'9'||ch<'0'){
        if(ch=='-') flag=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') re=(re<<1)+(re<<3)+ch-'0',ch=getchar();
    return re*flag;
}
ll n,K,cnt,a[200010],b[200010],tmp[500010],seg[500010],L[200010],R[200010],dp[200010],sum[200010];
void push(ll num){
    if(!seg[num]) return;
    seg[num<<1]=seg[num<<1|1]=seg[num];
    seg[num]=0;
}
void change(ll l,ll r,ll ql,ll qr,ll num,ll val){
    if(ql>qr) return;
    if(l>=ql&&r<=qr){seg[num]=val;return;}
    push(num);
    ll mid=(l+r)>>1;
    if(mid>=ql) change(l,mid,ql,qr,num<<1,val);
    if(mid<qr) change(mid+1,r,ql,qr,num<<1|1,val);
}
ll query(ll l,ll r,ll num,ll pos){
    if(l==r) return seg[num];
    push(num);
    ll mid=(l+r)>>1;
    if(mid>=pos) return query(l,mid,num<<1,pos);
    else return query(mid+1,r,num<<1|1,pos);
}
ll ask(ll pos){
    ll choose=query(1,cnt,1,pos);
    if(!choose) return 0;
    return (dp[choose]+(tmp[L[choose]]-tmp[pos]+K)%K);
}
ll erfen(ll val){
    ll l=1,r=cnt,mid;
    while(l<r){
        mid=(l+r)>>1;
        if(tmp[mid]>=val) r=mid;
        else l=mid+1;
    }
//    assert(val==tmp[l]);
    return l;
}
int main(){
    n=read();K=read();ll i;
    for(i=1;i<=n;i++) {
        a[i]=read();b[i]=read();
        sum[i]=(sum[i-1]+a[i]);
        if(b[i]==2) continue;
        if(2*a[i]>K){
            puts("-1");return 0;
        }
    }
    for(i=n;i>=1;i--){
        if(b[i]==1){
            L[i]=(-2ll*sum[i-1]%K+K)%K;
            R[i]=(-2ll*sum[i]%K+K)%K;
        }
        else L[i]=0,R[i]=K-1;
        tmp[++cnt]=L[i],tmp[++cnt]=R[i];
    }
    sort(tmp+1,tmp+cnt+1);
    cnt=unique(tmp+1,tmp+cnt+1)-tmp-1;
//    for(int i=1;i<=cnt;i++)cout<<tmp[i]<<" "; puts("");
    for(i=n;i>=1;i--){
//    	cout<<L[i]<<" "<<R[i]<<endl;
        L[i]=erfen(L[i]);
        R[i]=erfen(R[i]);
//        cout<<L[i]<<" "<<R[i]<<endl; 
        dp[i]=ask(L[i]);
//        cout<<i<<" "<<dp[i]<<endl;
        if(L[i]>R[i]) change(1,cnt,R[i]+1,L[i]-1,1,i);
        else change(1,cnt,1,L[i]-1,1,i),change(1,cnt,R[i]+1,cnt,1,i);
    }
    ll ans=dp[1];
    for(i=cnt;i>=1;i--) ans=min(ans,ask(i));
    printf("%lld\n",ans+sum[n]*2ll);
}


Measured memory_bytes: 15872
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool cmp(string a,string b){
    return a < b; //按字典序从小到大排列 
} 
int main(){
	int N,L;
	cin>>N>>L;
	string s[110];
	for(int i=0;i<N;i++){
		cin>>s[i];
	}
    sort(s,s+N,cmp);
    for(int i = 0;i < N;i++){
        cout << s[i];
    }
}

Measured memory_bytes: 256
N = int(input())

AC, WA, TLE, RE = 0, 0, 0, 0

for i in range(N):
    S = input()
    if S == 'AC':
        AC += 1
    elif S == 'WA':
        WA += 1
    elif S == 'TLE':
        TLE += 1
    elif S == 'RE':
        RE += 1

print('AC x ' + str(AC))
print('WA x ' + str(WA))
print('TLE x ' + str(TLE))
print('RE x ' + str(RE))


Measured memory_bytes: 9204
#include <stdio.h>

int main() {
    int i, j, k;
    int n, x;
    int count = 0;
    
    while(1) {
        scanf("%d %d\n",&n, &x);
        if(n == 0)
            break;
        for(i = 1; i <= (n-2); i++){
            for(j = (i + 1); j <= (n-1); j++){
                for(k = (j + 1); k <= n; k++){
                    if((i+j+k)==x)
                        count++;
                }
            }
        }
        printf("%d\n",count);
        count = 0;
    }
    
    return 0;
}


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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector< vector<int> > s;
  s.resize(3);
  for (int i=0; i<3; i++) {
    s[i].resize(N);
  }
  for (int i=0; i<N; i++) {
    cin >> s[0][i] >> s[1][i] >> s[2][i];
  }

  vector< vector<long long int> > tot_s;
  tot_s.resize(3);
  for (int i=0; i<3; i++) {
    tot_s[i].resize(N);
    tot_s[i][0] = s[i][0];
  }

  for (int i=1; i<N; i++) {
    tot_s[0][i] = s[0][i] + max(tot_s[1][i-1], tot_s[2][i-1]);
    tot_s[1][i] = s[1][i] + max(tot_s[0][i-1], tot_s[2][i-1]);
    tot_s[2][i] = s[2][i] + max(tot_s[0][i-1], tot_s[1][i-1]);
  }

  long long int ans = max(tot_s[0][N-1], tot_s[1][N-1]);
  ans = max(ans, tot_s[2][N-1]);
  cout << ans << endl;

  return 0;
}

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

namespace Atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] q = ReadLineLongSpilit();
            long n = q[0];
            long a = q[1];
            long b = q[2];

            long re = 0;

            //はみ出た個数
            var t = n % (a + b);

            if (t < a) re = ((n / (a + b)) * a) + t;
            else re = ((n / (a + b)) * a) + a;

            Console.WriteLine(re);
            ReadLine();
        }

        //最大公約数
        static int gcd(int a, int b)
        {
            if (a > b) {
                return gcd(b, a);
            }

            while (b != 0) {
                var remainder = a % b;
                a = b;
                b = remainder;
            }

            return a;
        }

        //最小公倍数
        static int lcm(int a, int b) => a * b / gcd(a, b);

        /// <summary>
        /// コンビネーション
        /// </summary>
        static int nCm(int n, int m)
        {
            if (m == 0) return 1;
            if (n == 0) return 0;
            return n * nCm(n - 1, m - 1) / m;
        }

        public static string ReadLine() => Console.ReadLine();
        public static int ReadLineInt() => int.Parse(Console.ReadLine());
        public static long ReadLineLongInt() => long.Parse(Console.ReadLine());
        public static string[] ReadLineStringSplit(char a = ' ') => ReadLine().Split(a).ToArray();
        public static int[] ReadLineIntSpilit(char a = ' ') => ReadLine().Split(a).Select(x => int.Parse(x)).ToArray();
        public static long[] ReadLineLongSpilit(char a = ' ') => ReadLine().Split(a).Select(x => long.Parse(x)).ToArray();
        static string[] GetAplhaLowerArray() => new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
        static string[] GetAplhaUpperArray() => new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    }
}


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

#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vec vector<int>
#define vecll vector<ll>
#define pb(x) push_back(x)

using namespace std;
///////////////////////////////////////////////////////////



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

    ll x;
    ll a[200005], b[200005];
    a[0] = 0; b[0] = 0;
    for (ll i = 1; i <= n; i++) {
        cin >> x;
        a[i] = a[i-1] + x;
    }
    for (ll i = 1; i <= m; i++) {
        cin >> x;
        b[i] = b[i-1] + x;
    }

    ll ans = 0;
    ll j = m;
    for (ll i = 0; i <= n; i++) {
        if (a[i] > k)
            break;
        while (b[j] > k - a[i])
            j--;
        ans = max(ans, i + j);
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 6712
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
	char s[15], t[15];
	cin >> s >> t;
	char* find = strstr(t, s);
	if (find == NULL)
	{
		cout << "No" << endl;
		return 0;
	}
	else
	{
		if (find - t == 0 && strlen(t) - strlen(s) > 0)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}

Measured memory_bytes: 3628
#include<bits/stdc++.h>
#include <queue>
#include <cstring>
#define mp make_pair
#define pb push_back
#define p push

#define FOR(a,c) for(int a = 0;a<c;a++)
#define ll long long int

#define getI(a) scanf("%d",&a)
#define getII(a,b) scanf("%d%d",&a,&b)
#define getIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define getL(a) scanf("%lld",&a)
#define getLL(a,b) scanf("%lld%lld",&a,&b)
#define getLLL(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define BEND(c) (c).begin(),(c).end()
#define BEND2(c) (c).rbegin(),(c).rend()
#define fi first
#define se second
#define pi 3.1415926535897932384626
#define getS(a) scanf("%s",a)
#define pfi(a) printf("%d",a);
#define pfii(a,b) printf("%d%d",a,b);
#define pfl(a) printf("%lld",a);
#define pfll(a,b) printf("%lld%lld",a,b);
#define pfd(a) printf("%lf",a);
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);

#define sag (sol|1)
#define sol (root<<1)
#define ort (bas+son)/2
#define SIZE 1000000007
#define MAXN 100005

using namespace std;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<vi> graph ;


int main(){
   IOS
  ll n;
   cin >> n;
   vl vec;
   ll cur =0;

    for (int i = 0; i <n ; ++i) {
        cin >>cur;
        vec.pb(cur);

    }
    sort(vec.rbegin(), vec.rend());
    ll res = 0, h = 0;
    while(vec[0] > 0){
        ll zero = 0, one = 0;
        for(int i = 0; i < n; i++){
            if(vec[i]%2) one++;
            else zero++;
            vec[i] /= 2;
        }
        ll v = 1;
        for(int u = 0; u < h; u++){
            v = (v*2)%SIZE;
        }
        res = (res + ((one % SIZE * zero % SIZE) % SIZE)* v % SIZE ) % SIZE;
        h++;
    }
    cout << res;
    return 0;
}


Measured memory_bytes: 5872
import math
#def main(S, b):
if __name__ == '__main__':
    #a, b, c, d = map(int, input().split())

    # x = int(input())
    #a, b = map(int, input().split())

    # a = list(map(int, input().split()))

    s = input()

    if (s == "ABC"):
        print("ARC")
    else:
        print("ABC")
    # s = sum(a)

    # flug = 1
    # for i in range(1000):
    #     if (k * i >= a and k * i <= b):
    #         flug = 0
    #         break
    # #print(n * 2 * pi)

    # if (flug == 1):
    #     print("NG")
    # else:
    #     print("OK")


Measured memory_bytes: 8952
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()
#define INF ((int)(1e9))

using namespace std;

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

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int N; cin >> N;
	vector<int> a(N, 0);
	rep (i, N){
		cin >> a[i];
	} // end rep

	int res = INF;
	for (int cand = -100; cand <= 100; ++cand){
		int curr = 0;
		rep (i, N){
			curr += (a[i] - cand) * (a[i] - cand);
		} // end rep
		res = min(res, curr);
	} // end for

	cout << res << endl;	
		
	return 0;
}

Measured memory_bytes: 256
def bomb(y, x, a):
    a[y][x] = '0'
    b = [[-3, 0], [-2, 0], [-1, 0], [1, 0], [2, 0], [3, 0],
         [0, -3], [0, -2], [0, -1], [0, 1], [0, 2], [0, 3]]
    for dx, dy in b:
        if 0 <= x + dx < 8 and 0 <= y + dy < 8 and a[y + dy][x + dx] == '1':
            bomb(y + dy, x + dx, a)

    return a


for i in range(int(input())):
    input()
    a = [list(input()) for _ in range(8)]
    x = int(input())
    y = int(input())

    bomb(y - 1, x - 1, a)
    print('Data %d:' % (i+1))
    [print(*x, sep='') for x in a]



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

vector<vector<int>> ki(300000);
bool reached[300000]={};
long long c[300000]={};
void miria(int a){
    reached[a]=true;
    if(ki[a].empty()) return;
    for(int i:ki[a]){
        if(reached[i]) continue;
        c[i]+=c[a];
        miria(i);
    }
}

int main(void){
    int n,q;
    int a,b;
    cin>>n>>q;
    
    for(int i=1;i<=n-1;i++){
        cin>>a>>b;
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    for(int i=1;i<=q;i++){
        cin>>a>>b;
        c[a]+=b;
    }
    miria(1);
    for(int i=1;i<=n;i++){
        cout<<c[i]<<endl;
    }
}

Measured memory_bytes: 23680
import math


def calcDist(A, B):
    sqSum = 0
    for i in range(len(A)):
        sqSum += (A[i] - B[i]) ** 2
    return math.sqrt(sqSum)


N, D = list(map(int, input().split()))

X = []
for i in range(N):
    X.append(list(map(int, input().split())))


count_int = 0
for i in range(N):
    for j in range(i + 1, N):
        if (calcDist(X[i], X[j]).is_integer()):
            count_int += 1

print(count_int)


Measured memory_bytes: 3064
#include<iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int,int> P;
int main()
{
  int n,m,b,c,i,t;
  long long res=0;
  P p;
  priority_queue<int,vector<int>,greater<int>> a;
  priority_queue<P> cb;
  cin >> n >> m;
  for(i=0;i<n;i++){
    cin >> t;
    a.push(t);
  }
  for(i=0;i<m;i++){
    cin >> b >> c;
    cb.push(P(c,b));
  }
  while(!cb.empty()){
    p=cb.top(); cb.pop();
    for(i=0;i<p.second;i++){
      t=a.top();
      if(t>=p.first) break;
      a.pop();
    }
    for(;i>0;i--)
      a.push(p.first);
  }
  while(!a.empty()){
    t=a.top(); a.pop();
    res+=t;
  }
  cout << res << endl;
  return 0;
}

Measured memory_bytes: 1784
N = int(input())
S = [input() for _ in range(N)] #N行に1文字入力したい
A = S.count("AC")
B = S.count("WA")
C = S.count("TLE")
D = S.count("RE")
print("AC", "x", str(A))
print("WA", "x", str(B))
print("TLE", "x", str(C))
print("RE", "x", str(D))

Measured memory_bytes: 16200
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
using P = pair<int, int>; 
using ll = long long;
 
int main()
{
  int a;
  cin >> a;
  cout << (a < 1200 ? "ABC" : "ARC") << endl;
  return 0;
}


Measured memory_bytes: 3572
n = int(input())
xy  = [list(map(int, input().split())) for _ in range(n)]

xy.sort()
lstm = []
lstp = []
for x, y in xy:
    lstm.append(x-y)
    lstp.append(x+y)
    
lstm.sort()
lstp.sort()
ans = max(lstm[-1] - lstm[0], lstp[-1] - lstp[0])
print(ans)

Measured memory_bytes: 62408
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "iomanip"
#include "random"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 0.00000001;
const long double PI = 3.1415926535897932384626433;

long long int N, M, K, L, R, H, W;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> N >> M;
	cout << max(N - M, (long long int)0) << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
struct edge{int to; ll cost;};
using P = pair<ll,int>;
const ll INF = 1e17;

void dijkstra(int n, int s, vector<vector<edge>>& g, vector<ll>& d){
  priority_queue<P,vector<P>,greater<P>> que;
  rep(i,n) d[i] = INF;
  d[s] = 0;
  que.push(P(0,s));
  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second;
    if(d[v] < p.first) continue;
    for(auto e : g[v]){
      if(d[e.to] > d[v] + e.cost){
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main(){
  int v, e, r;
  cin >> v >> e >> r;
  vector<vector<edge>> g(v);
  rep(i,e) {
    int s, t; ll d;
    cin >> s >> t >> d;
    g[s].push_back(edge{t,d});
  }
  vector<ll> d(v,INF);
  dijkstra(v,r,g,d);
  rep(i,v) cout << (d[i] == INF ? "INF" : to_string(d[i]))  << endl;
  return 0;
}


Measured memory_bytes: 19520
//love yjl forever
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int INF=0x3f3f3f3f;
int n;
int main()
{
/*	freopen("","r",stdin);
	freopen("","w",stdout);*/
	int i;
	string s;
	cin>>s;
	int f=0;
	for(i=0;i<s.size();i++)
	{
		if(f==0)
		{
//			cout<<1<<endl;
//			cout<<(s[i]=='h')<<endl;
			if(s[i]!='h')
			{
				cout<<"No"<<endl;
				return 0;
			}
			f=1;
		}
		else if(f==1)
		{
			if(s[i]!='i')
			{
				cout<<"No"<<endl;
				return 0;
			}
			f=0;
		}
//		cout<<1<<endl;
	}
	if(f==1)
	{
		cout<<"No"<<endl;
	}
	else
		cout<<"Yes"<<endl;
}

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

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rrep(i, n) for(int i = n - 1; i > -1; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
const long long INF = 1000000000;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n;
    cin >> n;
    vector<int> d(n);
    rep(i, n) cin >> d[i];
    sort(all(d));
    cout << d[n / 2] - d[n / 2 - 1] << endl;
}

Measured memory_bytes: 640
#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, s, n)    for (i64 i = (s), i##_limit = (n); i < i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(type, ...)  type __VA_ARGS__; read(__VA_ARGS__);
#ifndef DBG
#define dump(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> makeV(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeV(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(makeV<T>(initValue, args...))>(sz, makeV<T>(initValue, args...));
}

template <class T>
inline istream& operator>> (istream &is, vector<T> &vec) {
    for (auto &e : vec) is >> e;
    return is;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &os, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) os << e << delim[&e == &vec.back()];
    return os;
}

template <class Container>
struct Rev {
    Container &x_;
    inline Rev(Container &x): x_(x) {}
    inline auto begin() { return rbegin(x_); }
    inline auto end() { return rend(x_); }
};
// }}} End Header

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    var(int, N);

    map<i64, i64> cnt;
    rep(i, 0, N) {
        var(int, a);
        cnt[a] += 1;
    }

    vector<i64> a;
    for (const auto &e : cnt) {
        if (e.second >= 4) {
            a.push_back(e.first);
            a.push_back(e.first);
        } else if (e.second >= 2) {
            a.push_back(e.first);
        }
    }

    sort(all(a), greater<>());

    if (a.size() < 2) {
        print(0);
    } else {
        print(a[0] * a[1]);
    }

    return 0;
}


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

const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

//RadianをDegreeに変換する
double toDeg(double rad)
{
  return rad / (2 * PI) * 360;
}
//約数を列挙する
vector<int> divisor(int n)
{
  vector<int> ret;
  for (int i = 1; i * i <= n; i++)
  {
    if (n % i == 0)
    {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int main()
{
  ifstream fIn("input.txt");
  if (fIn.is_open())
    cin.rdbuf(fIn.rdbuf());

  double a, b, x;
  cin >> a >> b >> x;
  //2次元へ変換する
  double s = x / a;
  double rad;
  if (a * b / 2 <= s)
  {
    double h = (a * b - s) * 2 / a;
    rad = atan2(h, a);
  }
  else
  {
    double w = 2 * s / b;
    rad = atan2(b, w);
  }
  double deg = toDeg(rad);
  printf("%.10f\n", deg);
  return 0;
}


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

int main() {
    int h, w;
    cin >> h >> w;

    vector<vector<char>> map(h, vector<char>(w));
    vector<vector<int>> visit(h, vector<int>(w));
    queue<P> q;

    rep(y, h) {
        string s;
        cin >> s;
        rep(x, w) {
            map[y][x] = s[x];
            visit[y][x] = -1;
            if (map[y][x] == '#') {
                q.push(P(y, x));
                visit[y][x] = 0;
            }
        }
    }
    auto bfs = [&]() {
        while(!q.empty()) {
            P v = q.front(); q.pop();
            auto check = [&](int y, int x) {
                if (x < 0 || x >= w || y < 0 || y >= h) {
                    return;
                }
                if (map[y][x] == '#') {
                    return;
                }
                if (visit[y][x] != -1) {
                    return;
                }
                visit[y][x] = visit[v.first][v.second] + 1;
                map[y][x] = '#';
                q.push(P(y, x));
            };
            check(v.first + 1, v.second);
            check(v.first, v.second + 1);
            check(v.first - 1, v.second);
            check(v.first, v.second - 1);
        }
    };
    bfs();
    int ans = 0;
    rep(y, h) rep(x, w) {
        ans = max(visit[y][x], ans);
    }
    cout << ans << endl;
    return 0;
}



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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

const ll MAX_A = (int)(6e5 + 5);

int n;

vector<bool> primes(MAX_A);

int main(void) {
  // Here your code !
  scanf("%d", &n);

  for (ll i = 0; i < MAX_A; ++i) {
    primes[i] = true;
  }

  for (ll i = 2; i < MAX_A; ++i) {
    if (!primes[i]) continue;

    for (ll j = i * i; j < MAX_A; j += i) {
      // printf("%d %d\n", i, j);

      primes[j] = false;
    }
  }

  int cnt = 0;

  for (ll i = 2; i < MAX_A; ++i) {
    if (cnt == n) break;

    if (primes[i] && i % 5 == 1) {
      printf("%d ", i);
      cnt += 1;
    }
  }

  printf("\n");

  return 0;
}


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

lli x, y;

int main(void){
	cin >> x >> y;
	lli ans = 1e18;
	rep(i, 2){
		rep(j, 2){
			lli xc, yc;
			xc = x;
			yc = y;
			lli c = 0;
			if(i == 1) xc*=-1, c++;
			if(j == 1) yc*=-1, c++;
			if(xc <= yc) c+=yc-xc;
			else c+=1e18;
			ans = min(ans, c);
		}
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 1000000007
#define PI 3.141592653589793

int main(){
    string s;
    cin >> s;
    cout << s[0] << s.size()-2 << s[s.size()-1] << endl;
}


Measured memory_bytes: 256
#include<iostream>
int main(){
  int no_of_coins; 
  const int yen = 500; 
  std::cin >> no_of_coins; 
  int X;
  std::cin >> X;
  if(no_of_coins*yen >= X)
    std::cout << "Yes"; 
  else std::cout << "No"; 
  return 0; 
}

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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

std::string S;
int N, K;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    
    std::cin >> N >> K >> S;
    
    S[K - 1] = std::tolower(S[K - 1]);

    std::cout << S << std::endl;
}


Measured memory_bytes: 892
#include <iostream>
using namespace std;
int w,h;
int in[100][100];
bool seen[100][100];
int ans =0;

void solve(int i,int j){
    //cout << i << " " << j << endl;
    if(i < 0 || j < 0 || i >= h || j >= w){
        return;
    }
    if(seen[i][j]){
        return;
    }
    if(in[i][j] == 0){
        return;
    }
    seen[i][j] = true;
    solve(i-1,j-1);
    solve(i-1,j);
    solve(i-1,j+1);
    solve(i,j-1);
    solve(i,j+1);
    solve(i+1,j-1);
    solve(i+1,j);
    solve(i+1,j+1);

}
int main(){
    while(1){
    cin >> w >> h;
    if(w == 0 && h == 0){
        break;
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin >> in[i][j];
            seen[i][j] = false;
        }
    }
    ans = 0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(seen[i][j] == true || in[i][j] == 0){
            }
            else{
                solve(i,j);
                ans++;
            }
            
        }
    }
   
     cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 3176
N,M,X,Y=map(int,raw_input().split())
x=map(int,raw_input().split())
y=map(int,raw_input().split())

x_max=max(x)
y_min=min(y)

if y_min-x_max>=1:
	for i in range(x_max+1,y_min+1):
		if X<i<=Y:
			print "No War"
			quit()
	else:
		print "War"
	
else:
	print "War"

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

typedef long long ll;
#define rep(i,x) for(int i=0;i<x;i++)
#define put(ans)    cout << ans << endl;

int enum_divisors(int N)
{
    vector<int> res;
    for (int i = 1; i * i <= N; ++i)
    {
        if (N % i == 0)
        {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N / i != i)
                res.push_back(N / i);
        }
    }
    return res.size();
}

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

    if(n%2==1)  cout << "No" << endl;
    else{
        string s;   cin >> s;
        string t(s.substr(0,s.size()/2));
        t = t+t;
        if(s==t)    cout  << "Yes" << endl;
        else
        {
            cout << "No" << endl;
        }
        
    }

    return 0;
}

Measured memory_bytes: 3624
rooms = [[[0 for x in range(10)] for x in range(10) ] for x in range(10)]

num = int(input())

for i in range(num):
	b,f,r,v = map(int, input().split())
	rooms[b-1][f-1][r-1] += v #インデックスに対して実際の配列は0~n-1までなのでそのズレ対策
	
	#同じ部屋番号が来ても大丈夫なように+=を使う
	
for l in range(4):
	for j in range(3):
		'''
		for k in range(10):
			print(rooms[l][j][k],end = "")
		'''
		print(" "+" ".join(map(str, rooms[l][j])))
		# if k==9: print()
	if l != 3:
		print("#"*20)


Measured memory_bytes: 5620
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
int main()
{
    ll n;
    cin >> n;
    vector<char>a[n];
    vector<string>b;
    rep(i, n) {
        string t;cin>>t;
        string s;
        ll ex=0;
        rep(j, t.size()) {

            if (t[j]!='.') {
                s.push_back(t[j]);
            }
            else {
                ex=(ll)t.size()-j-1;
            }
        }
        ll no=9-ex;
        while (no--) {
            s.push_back('0');
        }
        b.push_back(s);
    }
    ll ans=0;
    vector<P>co;
    rep(i, n) {
        ll now=stoll(b[i]);
        ll sa=now;
        ll b=0;
        ll c=0;
        while (now%2==0) {
            b++;
            now/=2;
        }
        now=sa;
        while (now%5==0) {
            c++;
            now/=5;
        }
        co.push_back(P(b, c));
    }
    vector<vector<ll>>d(45, vector<ll>(45));
    rep(i, n) {
        rep(j, co[i].first+1) {
            rep(k, co[i].second+1) {
                d[j][k]++;
            }
        }
    }
    rep(i, n) {
        ans+=d[max(0LL, 18-co[i].first)][max(0LL, 18-co[i].second)];
    
    }
    ll ch=0;
    rep(i, n) {
        if (co[i].first>=9&&co[i].second>=9) {
            ch++;
        }
    }
    cout<<(ans-ch)/2<<endl;
}

Measured memory_bytes: 21512
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
#include<iomanip>

using namespace std;

int main() {
	long long a, b, c, ans = 0;
	cin >> a >> b >> c;
	if (a == b && b == c) {
		if (a % 2 == 1) {
			cout << 0 << endl;
			return 0;
		}
		cout << -1 << endl;
		return 0;
	}
	for (;;) {
		if (a % 2 == 1 || b % 2 == 1 || c % 2 == 1) {
			cout << ans << endl;
			return 0;
		}
		long long aa, bb, cc;
		aa = (b + c) / 2;
		bb = (c + a) / 2;
		cc = (a + b) / 2;
		a = aa;
		b = bb;
		c = cc;
		ans++;
	}
	return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <string>

int main()
{
  using namespace std;
  string s;
  cin >> s;

  for (int i = 0; i < s.size(); i += 2) {
    if (s[i] == 'L') {
      cout << "No" << endl;
      return 0;
    }
  }
  for (int i = 1; i < s.size(); i += 2) {
    if (s[i] == 'R') {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  return 0;
}


Measured memory_bytes: 256
from numpy.linalg import matrix_power
from numpy import dot, array

class mint:
    def __init__(self, n):
        self.n = n % mod

    def __add__(self, other):
        return mint(self.n + other.n)

    def __mul__(self, other):
        return mint(self.n * other.n)

    def __repr__(self):
        return str(self.n)


L, A, B, mod = map(int, input().split())

ans = array([mint(0), mint(A), mint(B)])

size = 0
for d in range(1, 19):
    x = pow(10, d, mod)
    T = array([
        [mint(x), mint(0), mint(0)],
        [mint(1), mint(1), mint(0)],
        [mint(0), mint(1), mint(1)],
    ])

    cnt = min(L - size, max(0, 10 ** d - 1 - A) // B - size + 1)
    if cnt > 0:
        ans = dot(ans, matrix_power(T, cnt))

    size += cnt
    if size >= L:
        break

print(ans[0])

Measured memory_bytes: 18172