akhauriyash/Code-Regression huggingface dataset:
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            var N = cin.ReadInt;
            var A = cin.ReadLongArray(N);
            var listA = new List<long>(A);
            var L = new long[N+1];
            var R = new long[N+1];

            long ans = 0;
            L[0] = 0;
            for(var i=0; i<N; i++)
            {
                L[i + 1] = Gcd(L[i], A[i]);
            }

            R[N] = 0;
            for(var i=N; i>0; i--)
            {
                R[i-1] = Gcd(R[i], A[i-1]);
            }

            for(var i=0; i<N; i++)
            {
                ans = Max(ans, Gcd(L[i], R[i+1]));
            }
            WriteLine(ans);
        }

        static long GcdArray(long[] array)
        {
            var ans = array[0];
            for(var i=1; i<array.Length; i++)
            {
                ans = Gcd(ans, array[i]);
            }
            return ans;
        }

        static long Gcd(long a, long b)
        {
            var v = new[] { a, b };
            while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; }
            return v[0];
        }

        static long Lcm(long a, long b)
        {
            return a / Gcd(a, b) * b;
        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue<string> inputStream;
        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); } }
        public int ReadInt { get { return int.Parse(Read); } }
        public long ReadLong { get { return long.Parse(Read); } }
        public double ReadDouble { get { return double.Parse(Read); } }
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
    }
}

Measured memory_bytes: 31584
import java.util.*

object MainKt {
	@JvmStatic
	fun main(args: Array<String>) {
		val scanner = Scanner(System.`in`)
		val n = scanner.nextInt()
		val q = scanner.nextInt()
		val s = scanner.next()

		val arr = IntArray(n) { i -> 0 }

		var befA = false
		for (i in 0..n-1){
			if (i!=0){
				if (befA && s.get(i)=='C') arr[i] = arr[i-1]+1
				else arr[i] = arr[i-1]
			}
			befA = s.get(i)=='A'
		}

		for (i in 0..q-1) {
			val l = scanner.nextInt()
			val r = scanner.nextInt()
			println(arr[r-1]-arr[l-1])
		}

	}
}

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

int w,h,n,A[11][11],ans,cnt;
bitset<100> B[11][11],p;

void dfs(int d,bitset<100> s,int sco){
  if(d==n){
    ans=min(ans,sco);
    return;
  }
  cnt++;
  if(cnt>=20000000)return;
  if(ans<=sco)return;
  if(!A[d/w][d%w])dfs(d+1,s,sco);
  else{
    int y=d/w;
    int x=d%w;
    if((B[y][x]&s).any()){
      p&=0;
      p.set(y*10+x);//(y*100+x);
      if(!(s&p).any())dfs(d+1,s,sco);
      dfs(d+1,(B[y][x]|s)^B[y][x],sco+1);
    }
    else dfs(d+1,s,sco);
  }
}

int main(){
  while(cin>>w>>h,w){
    memset(A,0,sizeof(A));
    r(i,11)r(j,11)B[i][j]&=0;
    n=h*w;
    bitset<100> b;
    r(i,h)r(j,w)cin>>A[i][j];
    r(i,h)r(j,w)if(A[i][j]){
      int sum=1;
      for(;;sum++){
        int f=0;
        for(int y=i;y<i+sum;y++){
          for(int x=j;x<j+sum;x++){
            if(A[y][x]==0)f++;
          }
        }
        if(f){
          sum--;
          break;
        }
      }
      bitset<100>tmp;
      for(int y=i,k=0;k<sum;k++,y++){
        for(int x=j,l=0;l<sum;l++,x++){
          tmp.set(y*10+x);
        }
      }
      b.set(i*10+j);
      B[i][j]=tmp;
    }
    ans=100;
    cnt=0;
    dfs(0,b,0);
    cout<<ans<<endl;
  }
}


Measured memory_bytes: 3056
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
  string s;
  string s1 = "qwertasdfgzxcvb";
  string s2 = "yuiophjklnm";
  sort(s1.begin(), s1.end());
  sort(s2.begin(), s2.end());
  while(cin >> s, s != "#"){
    bool flag1, flag2;
    int cnt = 0;
    for(int i = 1 ; i < s.size() ; i++){
      if(binary_search(s1.begin(), s1.end(), s[i-1]) &&
	 binary_search(s1.begin(), s1.end(), s[i]) ||
	 binary_search(s2.begin(), s2.end(), s[i-1]) &&
	 binary_search(s2.begin(), s2.end(), s[i])) continue;
      else cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Measured memory_bytes: 0
#define _CRT_SECURE_NO_WARNINGS
//#define DEBUG
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int main(void) {
#ifdef DEBUG
	printf("");
#endif

	char c, cTmp;
	int cnt = 0;
	scanf("%c", &c);
	while (1) {
		scanf("%c", &cTmp);
#ifdef DEBUG
		printf("c=%c, cTmp=%c", c, cTmp);
#endif
		if (cTmp != 'B' && cTmp != 'W') {
			break;
		}
		if (c != cTmp) {
			cnt++;
		}
		c = cTmp;
	}

	printf("%d\n", cnt);

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
const ll mod=1e9+7;
#define pb push_back
ll mat[1003][1003];
ll dp[1003][1003];
ll go(ll n,ll m,ll x=1,ll y=1)
{
	if(dp[x][y]!=-1)
		return dp[x][y];
	if(x==n and y==m)
		return 1;
	if(mat[x][y]==1)
		return 0;
	if(x<1 or x>n or y<1 or y>m)
		return 0;
	return dp[x][y]=(go(n,m,x+1,y)%mod+go(n,m,x,y+1)%mod)%mod;
}
void fast()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}
void input()
{
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
}
int main()
{
	fast();

	ll n,m;
	cin>>n>>m;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			char a;
			cin>>a;
			if(a=='.')
				mat[i][j]=0;
			else
				mat[i][j]=1;
		}
	}
	memset(dp,-1,sizeof(dp));
	cout<<go(n,m)<<endl;
}

Measured memory_bytes: 16128
implicit none
character(101) :: a,b
integer :: na, nb, ia, ib, i

read *,a
read *,b

na = len_trim(a)
nb = len_trim(b)

if (na > nb) then
   print '(a)','GREATER'
   stop
else if (na < nb) then
   print '(a)','LESS'
   stop
else
   do i = 1,na
      read(a(i:i),*) ia
      read(b(i:i),*) ib
      if (ia > ib) then
         print '(a)','GREATER'
         stop
      else if (ia < ib) then
         print '(a)','LESS'
         stop
      endif
   enddo
endif

print '(a)','EQUAL'
stop
end

Measured memory_bytes: 256
def is_unique(seq):
    return len(seq) == len(set(seq))


N = int(input())
a = []
for i in range(N):
    a.append(input())


for i in range(N-1):
    if a[i][-1:] != a[i+1][0]:
        print("No")
        break
else:
    if is_unique(a) == True:
        print("Yes")
    else:
        print("No")




Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <tuple>
#include <iomanip>


using namespace std;

typedef long long ll;

const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
	return f.second > s.second;
}

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

//-------------------ここから-----------------------------------


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

	bool ok = true;
	int Cindex = -1;
	for (int i = 0; i < s.size(); i++){
		if (i == 0){
			if (s[i] != 'A'){
				ok = false;
				break;
			}
			else {
				continue;
			}
		} else if (i == 2){
			int cnt = 0;
			for (int j = i; j < s.size() - 1; j++){
				if (s[j] == 'C'){
					cnt++;
					Cindex = j;
				}
			}
			if (cnt != 1){
				ok = false;
				break;
			}

		} else {
			if (i != Cindex && (s[i] < 'a' || s[i] > 'z')){
				ok = false;
				break;
			}
		}
	}

	if (ok){
		cout << "AC" << endl;
	}
	else {
		cout << "WA" << endl;
	}
}


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

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
typedef long long  ll;
typedef vector<ll> VL;
typedef pair<ll,ll> PLL;
#define pb push_back
#define F first
#define S second
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(ll i=x;i<y;i++)
#define frr(i,x,y) for(ll i=x-1;i>=y;i--)
#define inf 10000000000000000
const ll mod=998244353;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

void solve()
{
	string str;
	cin>>str;
	ll fg=0;
	fr(i,0,str.size())
	{
		if(str[i]=='7')
			fg=1;
	}
	if(fg==1)
		cout<<"Yes\n";
	else
		cout<<"No\n";
	return;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
	solve();
	return 0;
}


Measured memory_bytes: 3652
import sys
sys.setrecursionlimit(10**6)

def dfs(i, j):  # i行j列の島を探索する
    seen[i][j] = 1

    for k in range(i-1, i+2):
        for l in range(j-1, j+2):
            if k < 0 or k >= h or l < 0 or l >= w: continue
            if seen[k][l]: continue
            if c[k][l] == 0: continue
            dfs(k, l)


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [list(map(int, input().split())) for _ in range(h)]
    
    seen = [[0] * w for _ in range(h)]
    count = 0
    for i in range(h):
        for j in range(w):
            if not seen[i][j] and c[i][j] == 1: 
                dfs(i, j)
                count += 1

    print(count)



Measured memory_bytes: 8024
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
 
int main(){
  long long int a,b=1000000000000,c,d;
  cin>>a;
  c=sqrt(a);
  for(long long int i=1;i<=c;i++){
    if(a%i==0){
      d=i+(a/i)-2;
      b=min(b,d);
    }
  }
  cout<<b;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
#define rep(i, x, n) for(int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
const int ININF = 1000000;

struct UnionFind {
    vector<int> par;

    UnionFind(int n) : par(n, -1) {}
    void init(int n) { par.assign(n, -1); }

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

    bool issame(int x, int y) { return root(x) == root(y); }

    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if(x == y)
            return false;
        if(par[x] > par[y])
            swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) { return -par[root(x)]; }
};
// Kruskal 法で使用する枝
using pint = pair<int, int>;
using Edge = pair<long long, pint>;//<cost,<u,v>>の形
vector<Edge> edges;
int N, M;

int main() {
    cin >> N >> M;
    rep(i, 0, M) {
        long long u, v, w;
        cin >> u >> v >> w;
        // u--;
        // v--;
        edges.push_back(make_pair(w, (make_pair(u, v))));
    }
    //クラスカル法（Kruskal）
    sort(edges.begin(), edges.end());//costが小さい順にソート
    UnionFind uf(N);
    long long res = 0LL;
    // rep(i, 0, M) {
    //     cout << edges[i].first << "," << edges[i].second.first << ","
    //          << edges[i].second.second << endl;
    // }
    rep(i, 0, M) {
        Edge e = edges[i];
        int a = e.second.first;
        int b = e.second.second;
        if(!uf.issame(a, b)) {
            uf.merge(a, b);
            res += e.first;
        }
    }
    cout << res << endl;
}



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

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

  if ((s[0] == s[1] && s[2] == s[3] && s[1] != s[2]) ||
      (s[0] == s[2] && s[1] == s[3] && s[2] != s[1]) ||
      (s[0] == s[3] && s[1] == s[2] && s[3] != s[1])) {
    cout << "Yes" << endl;
    return 0;
  }
  cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define fi first
#define se second
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<int> size;
    UnionFind(int N): par(N),size(N) {

         //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++){
            par[i] = i;
            size[i] = -1;
        }
            
    }
    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
        size[ry] += size[rx];
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
    int getsize(int x){
        return -size[root(x)];
    }
    int getNunion(){//これちょっと遅い sameと組み合わせて順次数えていけば早くなりそう。
        map<int,int> m;
        rep(i,par.size()){
            m[this->root(i)]++;
        }
        return int(m.size());
    }
};

int main(){
    ll N,M;
    cin >> N >> M;
    vector<ll> A(M), B(M);
    UnionFind tree(N);
    rep(i,M){
        cin >> A[i] >> B[i];
        A[i]--;B[i]--;
        tree.unite(A[i], B[i]);
    }
    ll ans = 0;
    rep(i,N){
        cmax(ans, tree.getsize(i));
    }
    cout << ans << endl;
}

Measured memory_bytes: 13984
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
const int mod=1000000007;

ll gcd(ll a,ll b){
	if(a%b==0) return b;
	else{
		a%=b;
		return gcd(b,a);
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;cin >> n;
	vector<ll> a(n);
	rep(i,n) cin >> a.at(i);
	vector<int> b={0};
	ll g=a.at(0),g1=g;
	rep(i,n){
		g=gcd(g,a.at(i));
		if(g1>g){
			b.pb(i);
			g1=g;
		}
	}
	ll ans=g;
	rep(i,b.size()){
		if(b.at(i)==0) g=a.at(1);
		else g=a.at(0);
		rep(j,n){
			if(j==b.at(i)) continue;
			g=gcd(g,a.at(j));
		}
		ans=max(ans,g);
	}
	cout << ans << endl;
}

Measured memory_bytes: 1152
#include <iostream>
#include <algorithm>
#include <string> 
#include <vector>
#include <iomanip>
#include <math.h>
using namespace std;

int main()
{
	int X, A, B;
	cin >> X >> A >> B;
	if (B - A > X) {
		cout << "dangerous" << endl;
	}
	else if (B - A > 0) {
		cout << "safe" << endl;
	}
	else {
		cout << "delicious" << endl;
	}
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
#include<cmath>
#include<climits>
#include<queue>
#include<stack>
#include<numeric>
#include<set>
#include<iomanip>
#include<map>
#include<type_traits>
#include<tuple>
#include<deque>
#include<cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
const int mod = 1000000007;
const int INF = 1001001001;

int main() {
  ll h, w;
  cin >> h >> w;
  ll ans = LLONG_MAX;
  rep(ni, 2) {
    for (int i = 1; i < h; ++i) {
      ll nh = h - i;
      ll area1 = i * w;
      ll area2 = (nh + 1) / 2 * w;
      ll area3 = (nh) / 2 * w;
      ll maxv = max(area1, max(area2, area3));
      ll minv = min(area1, min(area2, area3));
      ans = min(ans, maxv - minv);

      area2 = (w + 1) / 2 * nh;
      area3 = w / 2 * nh;
      maxv = max(area1, max(area2, area3));
      minv = min(area1, min(area2, area3));
      ans = min(ans, maxv - minv);
    }
    swap(h, w);
  }
  cout << ans << endl;

}


Measured memory_bytes: 256
#include <stdio.h>

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

Measured memory_bytes: 580
import numpy as np

N, W = map(int, input().split())

items = [tuple(map(int, input().split())) for _ in range(N)]
dp = np.zeros(W+1, dtype=np.int64)

for i, (w, v) in enumerate(items):
  tmp = np.zeros(W+1, dtype=np.int64)
  tmp[w:] = dp[:-w] + v
  dp = np.maximum(dp, tmp)
print(dp[W])

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

using namespace std;

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

typedef long long LL;
typedef vector<int> VI;

int main() {
  int N;
  cin >> N;
  VI a(N);
  REP(i, N) {
    cin >> a[i];
  }
  int v = a[0];
  for (int i = 1; i < N; i++) {
    v ^= a[i];
  }
  if (v == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Measured memory_bytes: 768
from collections import defaultdict

def main():
    n = int(input())
    d = defaultdict(int)
    count = 0
    for i in range(n):
        s = input()
        if d[s] == 0:
            count+=1
            d[s] += 1
        else:
            continue
    print(count)
    
if __name__ == "__main__":
    main()


Measured memory_bytes: 128760
#include<bits/stdc++.h>
#define ll long long int
#define N (ll)(1e9+7)
#define T int
#define P pair<T,T>
#define umap unordered_map<T,T>
#define uset unordered_set<T>
#define rep(i,n) for(i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define print(v) for(auto x:v) cout<<x<< " "
#define pb push_back
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ct=0;
    int i;
    char last=s[0];
    ++ct;
    for(i=1;i<n;i++)
    {
        if(s[i]!=last)
        {
            last=s[i];
            ++ct;
        }
    }
    cout<<ct<<endl;

    return 0;
}


Measured memory_bytes: 512
//array->vector
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
static const ll MAX = 10000000000;
ll solve(ll H, ll W) {
    ll m = MAX;
    for (ll ah = 1; ah < H; ah++) {
        ll bw = W / 2;
        vector<ll> S1 = {ah * W, (H - ah) * bw, (H - ah) * (W - bw)};
        sort(S1.begin(), S1.end());

        ll bh = (H - ah) / 2;
        vector<ll> S2 = {ah * W, bh * W, (H - ah - bh) * W};
        sort(S2.begin(), S2.end());

        ll t = min(S1[2] - S1[0], S2[2] - S2[0]);
        if (t < m) m = t;
    }
    return m;
}
int main() {
    ll H, W;
    cin >> H >> W;
    cout << min(solve(H, W), solve(W, H)) << endl;
}

Measured memory_bytes: 256
h,w=map(int,input().split())
sy,sx=map(int,input().split())
sy-=1
sx-=1
gy,gx=map(int,input().split())
gy-=1
gx-=1

maze=[list(input()) for _ in range(h)]

from collections import deque
stack = deque([(sy,sx)])
maze[sy][sx]=0

checked=[[0]*w for _ in range(h)]


while len(stack)>0:
    (y,x)=stack.popleft()
    if checked[y][x]==0:
        checked[y][x]=1
        if (y,x)==(gy,gx):
            print(maze[y][x])
            exit()
        for (my,mx) in ((1,0),(0,1),(-1,0),(0,-1)):
            ny=my+y
            nx=mx+x
            if 0<=ny<h and 0<=nx<w:
                if maze[ny][nx]=="." or (maze[ny][nx]!="#" and maze[ny][nx]>maze[y][x]):
                    stack.appendleft((ny,nx))
                    maze[ny][nx]=maze[y][x]
                #ワープ
                elif maze[ny][nx]=="#":
                    ny+=my
                    nx+=mx
                    if 0<=ny<h and 0<=nx<w:
                        if maze[ny][nx]=="." or (maze[ny][nx]!="#"  and maze[ny][nx]>maze[y][x]+1):
                            stack.append((ny,nx))
                            maze[ny][nx]=maze[y][x]+1
        #斜めのワープを入れる
        for (my,mx) in ((1,1),(-1,-1),(-1,1),(1,-1)):
            for k in (1,2):
                ny=my*k+y
                nx=mx*k+x
                if 0<=ny<h and 0<=nx<w:
                    if maze[ny][nx]=="." or (maze[ny][nx]!="#"  and maze[ny][nx]>maze[y][x]+1):
                        stack.append((ny,nx))
                        maze[ny][nx]=maze[y][x]+1
        for (my,mx) in ((1,2),(-1,-2),(-1,2),(1,-2),(2,1),(-2,-1),(-2,1),(2,-1)):
            ny=my+y
            nx=mx+x
            if 0<=ny<h and 0<=nx<w:
                if maze[ny][nx]=="." or (maze[ny][nx]!="#"  and maze[ny][nx]>maze[y][x]+1):
                    stack.append((ny,nx))
                    maze[ny][nx]=maze[y][x]+1

#for item in maze:
    #print(item)
print(-1)

            
            







Measured memory_bytes: 168960
#include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string s;
        cin>>s;
        int i,sum=0;
        for(i=0;i<s.size();i++)
        {
            sum+=s[i]-'0';
        }
        if(sum==0)
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            if(sum%9==0)
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
        }
        }

Measured memory_bytes: 3744
a, b, c, d=input().split()
a=int(a)
b=int(b)
c=int(c)
d=int(d)

while (a>=0 and c>=0):
  c=c-b
  if c<=0 :
    print("Yes")
    break
  else :
    a=a-d
    if a<=0 :
      print("No")
      break
  

Measured memory_bytes: 9192
#include <iostream>
#define rep(S, L) for (int i = S; i < L; ++i)
using namespace std;

int main() {
  int n, a;
  int mina = 1000000000;
  int maxa = 0;
  cin >> n;
  rep(0, n) {
    cin >> a;
    mina = min(mina, a);
    maxa = max(maxa, a);
  }
  cout << maxa - mina << endl;
  return 0;
}


Measured memory_bytes: 3164
n = int(input())
li = []

for i in range(n):
    a = int(input())
    li.append(a)

i = 0
j = 0
count = 0
ans = 0

while i >= 0:
    if i == 1:
        ans += 1
        break
    else:
        j = i
        i = li[i] - 1
        count += 1
        li[j] = 0
if ans == 1:
    print(count)
else:
    print(-1)


Measured memory_bytes: 7084
n = int(input())
a = list(map(int, input().split()))
num = [0] * n
cnt = 0

for i in a:
    num[i - 1] += 1

for i in num:
    cnt += i * (i - 1)//2

for i in range(n):
    tmp = num[a[i]-1]
    ans = cnt - (tmp-1)
    print(ans)


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

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz size()
#define pb(x) push_back(x) 
#define bg begin()
#define ed end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define mp(x,y) make_pair(x,y)
const ll MOD=1000000007;

vector<ll> pw2(51,1); vector<ll> pw10(19,1);
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll myceil(ll x,ll y){if(x<=y) return 1;else if(x%y==0) return x/y;else return x/y+1;}

//a,b x
int main(){
	string s; cin>>s;
	string t="";
	for(int i=s.sz-1;i>=0;i--){
		t+=s[i];
	}
	ll i=0;
	while(i<s.sz){
		if(t[i]=='m' && t[i+1]=='a' && t[i+2]=='e' && t[i+3]=='r' && t[i+4]=='d')
		{i+=5;}
		else if(t[i]=='r' && t[i+1]=='e' && t[i+2]=='m' && t[i+3]=='a' && t[i+4]=='e' && t[i+5]=='r' && t[i+6]=='d')
		{i+=7;}
		else if(t[i]=='e' && t[i+1]=='s' && t[i+2]=='a' && t[i+3]=='r' && t[i+4]=='e')
		{i+=5;}
		else if(t[i]=='r' && t[i+1]=='e' && t[i+2]=='s' && t[i+3]=='a' && t[i+4]=='r' && t[i+5]=='e')
		{i+=6;}
		else break;	
	}
	if(i==s.sz) cout<<"YES";
	else cout<<"NO";
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <numeric>
#include <unordered_map>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <memory>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
constexpr long long MOD = 1e9 + 7;
constexpr long long MAX = 1000000;

//long long MOD = 998244353;
constexpr ll INF = 1e17;
constexpr double PI = 3.141592653589793;
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; }

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

    ll n;
    cin >> n;

    if (n % 2 == 1) {
        cout << 0 << "\n";
        return 0;
    }

    ll ans = 0;
    n /= 2ll;
    while (n) {
        ans += n / 5ll;
        n /= 5ll;
    }

    cout << ans << "\n";

    return 0;
}

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

using namespace std;

typedef long long int64;

const int NMAX = 100100;

int gid[NMAX], gc[NMAX];

void Init(int n) {
	for (int i = 0; i < n; ++i) {
		gid[i] = i;
		gc[i] = 1;
	}
}

int GetId(int u) {
	if (gid[u] == u) {
		return u;
	}
	return gid[u] = GetId(gid[u]);
}

void Link(int child, int parent) {
	gid[child] = parent;
	gc[parent] += gc[child];
}

int64 Join(int u, int v) {
	u = GetId(u);
	v = GetId(v);
	if (u == v)
		return 0;
	int64 uc = gc[u];
	int64 vc = gc[v];
	if (random() & 1)
		Link(u, v);
	else
		Link(v, u);
	return uc * vc;
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	Init(n);
	vector<int> us, vs;
	for (int i = 0; i < m; ++i) {
		int u, v;
		scanf("%d%d", &u, &v);
		--u, --v;
		us.push_back(u);
		vs.push_back(v);
	}
	vector<int64> ans;
	int64 cur = int64(n) * int64(n - 1) / 2;
	for (int i = m - 1; i >= 0; --i) {
		ans.push_back(cur);
		cur -= Join(us[i], vs[i]);
		assert(cur >= 0);
	}
	reverse(ans.begin(), ans.end());
	for (int64 t : ans) {
		printf("%lld\n", t);
	}
	return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {
    int S;
    cin >> S;
    auto u = S / 100;
    auto d = S % 100;

    if((d==0 && u==0) || (u==0 && d >= 13) || (u>=13 && d==0) || (u >= 13 && d >= 13))
        cout << "NA" << endl;
    else if(1 <= u && u <= 12 && (d==0 || d >= 13))
        cout << "MMYY" << endl;
    else if((u==0 || u >= 13) && 1 <= d && d <= 12)
        cout << "YYMM" << endl;   
    else
        cout << "AMBIGUOUS" << endl;

    return 0;
}

Measured memory_bytes: 256
//g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <tuple>
#include <iomanip>
#include <random>
#include <math.h>
//#include <ostream>

using namespace std;

#define ll long long
#define rep(i, n) for(ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

ll N,M;
vector<ll> A(N),B(N),C(N);
string S,T;

int main(){
  cin >> N >> M;
  cout << N*M << endl;
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,u) for(int i=start[u];i;i=e[i].next)
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
inline void myassert(bool a,string s){ if(!a)cerr<<s<<endl,exit(0);}
inline int read()
{
	int s=0,f=1;char ch=getchar();
	while(!isdigit(ch) && ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))s=s*10+ch-'0',ch=getchar();
	return ~f?s:-s;
}

const int maxn=5e3+20;

int n,k;
char str[maxn];
char mn;
int len;

inline string solve(int l,int r)
{
	string s;
	int len=r-l+1;
	REP(i,1,len<<k-1)s+=mn;
	REP(i,r+1,n)if(SZ(s)<n)s+=str[i];
	DREP(i,n,1)if(SZ(s)<n)s+=str[i];
	return s;
}

inline string solve1(int l,int r)
{
	string s;
	int len=r-l+1;
	REP(i,1,len<<k)s+=mn;
	DREP(i,l-1,1)if(SZ(s)<n)s+=str[i];
	return s;
}

inline void init()
{
	n=read();k=read();
	scanf("%s",str+1);
	mn='z'+1; REP(i,1,n)chkmin(mn,str[i]);
	if(k>20){ REP(i,1,n)putchar(mn);exit(0);}
	int slen=0,lstlen=0;
	REP(i,1,n)
	{
		if(i==n || str[i]!=mn)
		{
		 	chkmax(len,slen);
		 	slen=0;
		}else slen++;
	}
	while(lstlen<n && str[n-lstlen]==mn)++lstlen;
	if(len<<k-1 >= n) { REP(i,1,n)putchar(mn);exit(0); }
	if(lstlen<<k >= n) { REP(i,1,n)putchar(mn);exit(0); }
	string s;

	slen=0;
	REP(i,1,n)
	{
		if(i==n || str[i]!=mn)
		{
		 	//chkmax(len,slen);
			if(slen==len)
			{
		 	 	string up=solve(i-len,i-1);
			 	if(s.empty() || s>up)s=up;
			}
			slen=0;
		}else slen++;
	}
	if(lstlen>0)
	{
		string up=solve1(n-lstlen+1,n);
		if(s.empty() || s>up)s=up;
	}
	cout<<s<<endl;
}

inline void doing()
{
}

int main()
{
	init();
	doing();
	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();
		long temp,min=0,res=-999999999;

		min = sc.nextLong();
		for(int i=1;i<n;i++) {
			temp=sc.nextLong();

			if(res < temp-min) {
				res = temp -min;
			}
			if(temp < min) {
				min = temp;
			}
		}
		System.out.println(res);

	}
}



Measured memory_bytes: 98420
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int main(){
    int a,b,c,d; cin >> a >> b >> c >> d;
    cout << min(a, b) + min(c, d) << endl;
}

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

using namespace std;

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

    for (int i = s.size() / 2 - 1; i > 0; i--) {
        if (s.substr(0, i) == s.substr(i, i)) {
            cout << 2 * i << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 256
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;


public class Main {
	static Scanner InData = new Scanner(System.in);
	static TreeSet<Integer> S = new TreeSet<Integer>();
	static int qType, qVal, qVal2;
	
	public static void main(String[] args) {
		getQuery();
	}

	private static void getQuery() {
		int qNum = InData.nextInt();
		
		for(int indexOfQuery = 0; indexOfQuery < qNum; indexOfQuery++) {
			qType = InData.nextInt();
			qVal = InData.nextInt();
			doQuery(qType, qVal);
		}
	}

	private static void doQuery(int type, int val) {
		switch(type) {
		case 0:
			S.add(val);
			System.out.println(S.size());
			break;
		case 1:
			System.out.println(S.contains(val) ? 1 : 0);
			break;
		case 2:
			S.remove(val);
			break;
		case 3:
			qVal2 = InData.nextInt();
			SortedSet<Integer> subSet = S.subSet(val, qVal2 + 1);
			for(int idxOfOut : subSet) {
				System.out.println(idxOfOut);
			}
			break;
		}
	}
}


Measured memory_bytes: 184208
n=int(input())
for i in range(n):
  if i==0:
    t,a=map(int,input().split())
  else:
    x,y=map(int,input().split())
    p=(t+x-1)//x
    q=(a+y-1)//y
    r=max(p,q)
    t,a=x*r,y*r
print(t+a)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(),(x).end()
#define P pair<int,int>
typedef long long ll;
using namespace std;

int main(){
    int n,k; cin >> n >> k;
    double ans=0;
    rep(i,n){
        int s=i+1; int times=0;
        while(s<k){
            s*=2;
            times++;
        }
        double c=(double)1/n;
        ans+=c*pow(0.5,times);
    }
    printf("%.20lf",ans);
return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}
int gcd(int a, int b){//ユークリッドの互除法
  if (a < b) gcd(b,a); //aの方がbよりでかいのが前提
  if (b == 0) return a; //aをbで割り切れたらreturn
  else gcd(b, a % b);
}
const int INF = 1<<30;
const int MOD = 1000000007;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};

int main(){
  int n; cin >> n;
  vector<vector<int>> d(n,vector<int>(n,-1));
  rep(i,n){
    int a; cin >> a;
    rep(j,a){
      int x,y; cin >> x >> y;
      x--;
      d[i][x] = y;
    }
  }
  
  int ans = 0;
  for(int bit = 0; bit < (1<<n); bit++){
    bool flag = true;
    bitset<15> bs(bit); int a = bs.count();
    //cout << bs << endl;
    rep(i,n){
      if(bit & (1<<i)){
        rep(j,n){
          int tmp = (bit >> j) & 1;
          //printf("j=%d,tmp=%d\n",j,tmp);
          if(d[i][j] != tmp && d[i][j] != -1){
            //printf("d[i][j] = %d\n",d[i][j]);
            flag = false;
          }
        }
      }
    }
    if(flag) chmax(ans,a);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i,n) for(int i=0;i<(n);++i)
#define fore(i,a) for(auto &i:a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define answer cout << ans << endl;
signed _main(); signed main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
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; }
template<class T> using v = vector<T>;
template<class T> using vv = vector<v<T>>;
const int MOD=1e9+7;
const long long INF = 1LL << 60;
using P=pair<int,int>;

signed _main(){
  int X,Y,A,B,C,ans=0; cin >> X >> Y >> A >> B >> C;
  v<int> a(A),b(B),n;
  rep(i,A){
    cin >> a[i];
  }
  rep(i,B){
    cin >> b[i];
  }
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  sort(b.begin(),b.end());
  reverse(b.begin(),b.end());
  rep(i,min(X,A)){
    n.push_back(a[i]);
  }
  rep(i,min(Y,B)){
    n.push_back(b[i]);
  }
  rep(i,C){
    int tmp;
    cin >> tmp;
    n.push_back(tmp);
  }
  sort(n.begin(),n.end());
  reverse(n.begin(),n.end());
  rep(i,X+Y) ans+=n[i];
  answer
}


Measured memory_bytes: 8048
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		String N = scan.next();
		String T = scan.next();
		String[] n = N.split("");
		String[] t = T.split("");
		int c =0;
		
		for(int i=0; i<N.length(); i++) {
			if(n[i].equals(t[i])){
			}else {
				c++;
			}
		}
		System.out.println(c);
	}
}


Measured memory_bytes: 67528
n,m,x = list(map(int,input().split()))
c = [[] for i in range(n)]

for i in range(n):
    c[i] = list(map(int,input().split()))

check = [0]*n

def dekiru(ls):
    hantei = [0]*m
    for i in range(n):
        if check[i] == 1:
            for j in range(m):
                hantei[j] += c[i][j+1]
    for i in range(m):
        if hantei[i] < x:
            return False
    return True

ans = 10000000000000000000000

def solv(idx):
    global ans
    if idx == n:
        kane = 0
        for i in range(n):
            if check[i] == 1:
                kane += c[i][0]
        if dekiru(check):
            ans = min(ans,kane)
        return



    check[idx] = 0
    solv(idx+1)
    check[idx] = 1
    solv(idx+1)

solv(0)
print(ans if ans != 10000000000000000000000 else -1)

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

#define rep(i,n) for(int i = 0; i < (n); i++)
#define REP(i,n) for(int i = 1; i < (n); i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define yesorno(a) printf(a ? "yes\n" : "no\n")
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define YESorNO(a) printf(a ? "YES\n" : "NO\n")

using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vii = vector<vi>;

const int dx[8] = {0,1,0,-1,1,1,-1,-1};
const int dy[8] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1; 


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

  int n;
  string s;
  cin >> n >> s;
  int east = 0, west = 0;
  rep(i,n){
    if(s[i] == 'E') east++;
    if(s[i] == 'W') west++;
  }
  int le = 0, lw = 0;
  int ans = inf;
  rep(i,n){
    int re = east - le;
    int rw = west - lw;
    if(s[i] == 'E') re--;
    if(s[i] == 'W') rw--;
    ans = min(ans, re + lw);
    if(s[i] == 'E') le++;
    if(s[i] == 'W') lw++;
  }
  cout << ans << endl;
}


Measured memory_bytes: 720
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

int main(void) {
  int N;

  scanf("%d", &N);

  static char a[333][333];
  REP(i, N) {
    scanf("%s", a[i]);
  }

  int ret = 0;
  REP(B, N) {
    bool ok = true;
    REP(i, N) REP(j, N) {
      int nj = (j + B) % N;
      int ni = (i - B + N) % N;
      if (a[i][j] != a[nj][ni]) { ok = false; break;
      }
    }

    if (ok) ret++;
  }

  ret *= N;
  printf("%d\n", ret);
  return 0;
}


Measured memory_bytes: 384
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-reorder-functions")
#pragma GCC optimize("profile-values")
#pragma GCC optimize("tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")
#pragma GCC target("sse2")
#pragma GCC target("sse3")
#pragma GCC target("ssse3")
#pragma GCC target("sse4.1")
#pragma GCC target("sse4.2")
#pragma GCC target("avx")
#pragma GCC target("avx2")
#pragma GCC target("popcnt")
#pragma GCC target("abm")
#pragma GCC target("mmx")
#pragma GCC target("tune=native")
#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2e5 + 7;
bitset <N> a, diff, start;
int pref[N];

int n, k;
void print() {
    for (int i = 0; i < n; ++i) {
        if (a[i])
            cout << 'A';
        else
            cout << 'B';
    }   
    cout << endl;
}

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < n; ++i)
        a[i] = s[i] == 'A';


    if (n == 1) {
        a[0] = a[0] ^ (k & 1);
        print();
        exit(0);
    }   

    while (!a[n - 1] && k) {
        --k;
        if (a[0]) {
            a[0] = a[0] ^ 1;
        }   
        else {
            for (int i = 0; i + 1 < n; ++i) {
                if (a[i] == a[i + 1]) {
                    a[i] = a[i] ^ 1;
                }   
            }   
            a[n - 1] = 1;
        }   
    }        
    
    if (k == 0) {
        print();
        exit(0);
    }
    
    vector <int> who;
    for (int i = 0; i + 1 < n; ++i) {
        diff[i] = a[i] ^ a[i + 1];
        if (diff[i])
            who.app(i);
    }
    start = diff;
    diff[n - 1] = 1;

    for (int i = 0; i < n; ++i)
        pref[i + 1] = pref[i] + start[i];

    int bt = 0;
    for (int i = 0; bt < n - 1 && k; ++i) {
        --k;

        //j >= n - 1 - bt

        int cnt = diff[0];
        cnt += pref[n - 1] - pref[1 + bt];
        cnt += bt + 1;        

        if (cnt & 1) {
            diff[0] = diff[0] ^ 1;
        }   
        else {
            ++bt;           
            diff >>= 1;
            diff[n - 1] = 1;
        }   
    }   

    a[n - 1];
    for (int i = n - 2; i >= 0; --i)
        a[i] = a[i + 1] ^ diff[i];

    if (a[0] || (a[0] == a[1])) {
        a[0] = a[0] ^ (k & 1);
    }   
    print();
}


Measured memory_bytes: 2444
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()


int main() {
    string s;
    cin>>s;
    bool ok=false;
    rep(i,3){
        if(s[i]=='7') ok=true;
    }
    if(ok) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}



Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
int main(){
  long long int x,n;
  cin>>x>>n;
  long long int input;
  vector<long long int> v(n);
  unordered_set<long long int> s;
  for(int i=0;i<n;i++){
    cin>>input;
    v[i] = input;
    s.insert(input);
  }

  long long int ans=0;
  if(n==0){
    ans = x;
  }
  else{
    long long int minel = *min_element(v.begin(), v.end());
    long mindiff = LONG_MAX;
    long long int m = *max_element(v.begin(), v.end());
    for(long long int i=minel-1;i<=m+1;i++){
      if(s.count(i)){
        continue;
      }
      else{
        long long int diff = abs(x-i);
        if(mindiff == diff){
          ans = min(ans, i);
        }
        else if(mindiff>diff){
          mindiff = diff;
          ans = i;
        }
      }
    }

  }
  cout<<ans<<endl;

}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int dp[n][3], a[3];
    memset( dp,0,sizeof(dp) );
    for(int i=0; i<n; i++) for(int j=0; j<3; j++) cin>>a[j], dp[i][j] = max( (i>0 ? dp[i-1][(j+1)%3] : 0), (i>0 ? dp[i-1][(j+2)%3] : 0) ) + a[j];
    cout<<max( dp[n-1][0],max( dp[n-1][1],dp[n-1][2] ) )<<"\n";
}

Measured memory_bytes: 1408
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// using Bint = mp::cpp_int;
// using Real = mp::number<mp::cpp_dec_float<1024>>;

int main(){
    string S;
    int K;
    cin >> S >> K;

    map<int, string> sub;
    int sub_max = 0;

    for(int k = 1; k <= 5 && k <= S.size() ; k++){
        for(int i = 0; i < S.size() - k + 1; i++){
            int tmp = 0;
            string s = S.substr(i, k);
            for(int j = 0; j < k; j++){
                tmp *= 27;
                tmp += (s.at(j) - 'a') + 1;
            }
            for(int j = k; j < 5 && j < S.size(); j++){
                tmp *= 27;
            }

            // cout << s << ' '  << tmp << endl;

            if(sub.size() < K){
                sub_max = max(sub_max, tmp);
                sub[tmp] = s;
            } else if (tmp < sub_max){
                sub.erase(sub_max);
                sub[tmp] = s;
                sub_max = 0;
                for(pair<int, string> p : sub){
                    sub_max = max(sub_max, p.first);
                }
            }
        }
    }
    cout << sub[sub_max] << endl;
}

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

int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

int n, q;
long long m;
struct Edge {
    int u, v, w;
} e[200005];
int fa[200005];
int find(int x) {
    if (fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}

int vis[200005];
bool cmp(Edge a, Edge b) { return a.w < b.w; }
int main() {
    n = read(), scanf("%lld\n", &m), q = read();
    for (int i = 1; i <= n; ++i) fa[i] = i;
    for (int i = 1; i <= q; ++i)
        e[i].u = read() + 1, e[i].v = read() + 1, e[i].w = read();
    sort(e + 1, e + 1 + q, cmp);
    for (int i = 1; i <= q; ++i) {
        if (e[i].w == 0)
            fa[find(e[i].u)] = find(e[i].v);
        else if (find(e[i].u) == find(e[i].v))
            return puts("No"), 0;
        else
            vis[find(e[i].u)] = vis[find(e[i].v)] = 1;
    }

    long long res = 0, res1 = 0;
    for (int i = 1; i <= n; ++i)
        if (find(i) == i) {
            res++, res1 += vis[i];
        }
    if (res1 == 2) {
        if (res > 2)
            res1++;
        else
            return puts("No"), 0;
    }
    if (n - res + res1 > m || m > res * (res - 1) / 2 + (long long)(n - res))
        return puts("No"), 0;
    puts("Yes");
    return 0;
}

Measured memory_bytes: 2176
#include<bits/stdc++.h>
using namespace std;
#define int long long 
signed  main()
{
 int k,cnt=0;
  cin>>k;
  queue<int>q;
  for(int i=1;i<=9;i++)q.push(i);
  int ans;
  
  while(cnt<k)
  {
   ans=q.front();
    q.pop();
    int x=ans*10+ans%10;
    if(x%10!=0)q.push(x-1);
    q.push(x);
    if(x%10!=9)q.push(x+1);
    cnt++;
  }
  cout<<ans<<endl;
  
}


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

int main(void)
{
	int n;
	int rsum, lsum;
	int tmp;
	string s;

	cin >> n >> s;
	lsum = rsum = 0;
	rep(i, n)
	{
		if (s[i] == ')')
		{
			if (rsum > 0)
				rsum--;
			else
				lsum++;
		}
		else
			rsum++;
	}
	rep(i, lsum)
		cout << '(';
	cout << s;
	rep(i, rsum)
		cout << ')';
	cout << endl;
	return 0;
}


Measured memory_bytes: 3636
#include<iostream>
#include<cmath>

using namespace std;

long long n, m;
long long answer;

long long int factrial(int n){
  long long int dp[100005];
  for(int i = 1; i <= n; ++i){
    if(i == 1){
      dp[i] = 1;
    }else{
      dp[i] = ((dp[i - 1] * i) % (1000000000 + 7));
    }
  }
  return dp[n];
}

int main(){
  cin >> n >> m;
  if(abs(n - m) >= 2){
    answer = 0;
    //cout <<  << endl;
  }else if(abs(n - m) == 1){
    answer = (factrial(n) * factrial(m));
  }else if(abs(m - n) == 0){
    answer = ((factrial(n) * factrial(m)) * 2);
    //cout << factrial(n) * 2 << endl;
  }
  cout << (answer % (1000000000 + 7)) << endl;
  //cout << factrial(5) << endl;
}

Measured memory_bytes: 1024
from bisect import bisect_left as bl
from bisect import insort_right as ir
from collections import defaultdict

s = input()
t = input()
len_s = len(s)

index = defaultdict(list)
for i, c in enumerate(s):
    ir(index[c], i)

ans = 0
now = 0
for c in t:
    if not index[c]:
        print('-1')
        break
    idx = bl(index[c], now)
    if idx == len(index[c]):
        ans += len_s
        now = index[c][0] + 1
    else:
        now = index[c][idx] + 1
else:
    print(ans + now)

Measured memory_bytes: 7924