akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main()
{
  int N, M;
  cin >> N >> M;
  string s, t;
  cin >> s >> t;
  int g = __gcd(N, M);
  rep(i, g)
  {
    int x = N / g * i;
    int y = M / g * i;
    if (s[x] != t[y])
    {
      cout << -1 << endl;
      return 0;
    }
  }
  long long ans = (long long)N / g * M;
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 512
#include <stdio.h>
#include <string.h>
int main(){
char p[101],s[101],r[202];
scanf("%s",s);
scanf(" %s",p);
strcpy(r,s);
strcat(r,s);
printf("%s\n",strstr(r,p)!=NULL ? "Yes" : "No");
return 0;
}

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

int main(){
    using namespace std;
    constexpr unsigned long MOD = 1000000007;

    // 初期値つき冪剰余 b*a^n mod MOD
    const auto& modpow = [](unsigned long a, unsigned long n, unsigned long b = 1) -> unsigned long {
        unsigned long r{b % MOD};
        while(n){
            if(n & 1)(r *= a) %= MOD;
            (a *= a) %= MOD;
            n >>= 1;
        }
        return r;
    };

    // オイラーのトーシェント関数 φ(n)
    const auto& totient = [](unsigned long n) -> unsigned long {
        unsigned long v{n}, k{n};
        for(unsigned long j{2}; j * j <= k; ++j)if(k % j == 0){
                v = v / j * (j - 1);
                while(k % j == 0)k /= j;
            }
        if(k > 1)v = v / k * (k - 1);
        return v;
    };

    unsigned long N, K;
    cin >> N >> K;

    // ans = Σφ(n) floor(K/i)^N
    unsigned long ans{0};
    for(unsigned long i{1}; i <= K; ++i)ans += modpow(K / i, N, totient(i));
    cout << ans % MOD << endl;

    return 0;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define ll long long int
#define pb(x) push_back(x)
#define TEST int inox,t;cin>>t; for(inox=1;inox<=t;inox++)
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,n) for(i=n-1;i>=0;i--)
#define mp(x,y)  make_pair(x,y)
#define FAST    ios::sync_with_stdio(false); cin.tie(0);
#define modd 1000000007
using namespace std;
 
int main(){
ll n,k,i;
cin>>n>>k;
vector<ll> v(n);
rep(i,n)cin>>v[i];
ll ma=0;
rep(i,n)ma=max(ma,v[i]);
ll l=1,r=ma,sum;
while(l<r){
	ll m=l+(r-l)/2;
	sum=0;
	// for(i=0;i<n;i++){
	// 	sum+=v[i]/m;
	// 	if(sum>k)break;
	// }
	for(i=0;i<n;i++){
		ll x=v[i]/m;
		if(v[i]%m)
		x++;
		sum+=x-1;
	}
	// cout<<sum<<" "<<m<<endl;
	if(sum>k)l=m+1;
	else r=m;
}
cout<<l;
}

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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >
# define PQ priority_queue

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);

string vow = "aeiou";

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

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

const ld pie = 3.14159265358979 ;
const ll mod = 1e9 + 7 ;

bool is_prime ( ll n )
{
    for ( ll i = 2 ; i*i <= n ; i ++ )
    {
        if ( n % i == 0 )
            return false ;
    }

    return true ;
}

void solve ( int test_case )
{
    ll n ;
    cin >> n ;

    for ( ; is_prime ( n ) == false ; n ++ ) ;
    cout << n ;

    line
}

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

    for ( int i=0 ; i < t ; i++ ) solve(i);
    return 0;
}


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

int main(){
  int n,x;
  cin >> n >> x;
  vector<int> l(n);
  vector<int> d(n+1);
  int count = 0;
  d.at(0)=0;
  for(int i=0;i<n;i++){
    cin >> l.at(i);
  }
  for(int i=1;i<n+1;i++){
    d.at(i)=d.at(i-1)+l.at(i-1);
  }
  for(int i=0;i<n+1;i++){
    if(d.at(i)<=x){
      count++;
    }else{
      break;
    }}
    cout << count << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<time.h>
#include<ctime>
#include<functional>
#define int long long
#define itn int
#define f(n) for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int, int> P;
signed main() {
	int a, b;
	cin >> a >> b;
	if (b % 2) cout << (a / b)*(a / b)*(a / b) << endl;
	else cout << (a / b)*(a / b)*(a / b) + ((a + (b / 2)) / b)*((a + (b / 2)) / b)*((a + (b / 2)) / b) << endl;
	return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> s;
    for (int i = 0; i < n; ++i) {
        int ss;
        cin >> ss;
        s.push_back(ss);
    }
    int q;
    cin >> q;
    int count{0};
    for (int i = 0; i < q; ++i) {
        int t;
        cin >> t;
        if (find(begin(s), end(s), t) != end(s)) {
            ++count;
        }
    }
    cout << count << endl;
}

Measured memory_bytes: 3216
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;

const int gmax_n = 2 * 100005;

int par[gmax_n * 2]; //親
int depth[gmax_n * 2];//木の深さ

void init(int n){
    rep(i,n){
        par[i] = i;
        depth[i] = 0;
    }
}

int find(int x){
    if(par[x] == x){
        return x;
    }else {
        return par[x] = find(par[x]);
    }
}

void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(depth[x] < depth[y]){
        par[x] = y;
    }else{
        par[y] = x;
        if(depth[x] == depth[y]) depth[x]++;
    }
}

bool same(int x, int y){
    return find(x) == find(y);
}

int main(){
    int n, k, l;
    cin >> n >> k >> l;

    init(n * 2);
    rep(i,k){
        int a, b;
        cin >> a >> b;
        a--; b--;
        unite(a,b);
    }

    rep(i,l){
        int a, b;
        cin >> a >> b;
        a--; b--;
        unite(a + n, b + n);
    }

    pair<int, int> p[gmax_n];
    rep(i,n) {
        p[i] = make_pair(find(i), find(i + n));
    }

    map<pair<int, int>,int> m;
    rep(i,n) m[p[i]]++;

    rep(i,n){
        cout << m[p[i]] << endl;
    }
}


Measured memory_bytes: 16640
#include<iostream>
#include<vector>
using namespace std;
#define ll long long int
int main()
{
  ll n,count=0;
  cin>>n;
  vector<ll> a(n,0);
  for(ll i=0;i<n;i++)
  {cin>>a[i];
  if(((i+1)%2)==1)
  if((a[i]%2)==1)
    count++;}
  cout<<count;
}

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

int n,k;
int a[5010];
bool f[5010][5010];

bool check(int x) {
	memset(f,0,sizeof(f));
	f[0][0]=1;
	if (0>=k-a[x]&&0<k) return true;
	for (int i=1;i<=n;i++) {
		int tmp=a[i];
		if (i==x) tmp=0;
		f[i][0]=1;
		for (int j=1;j<k;j++) {
			if (j>=tmp) f[i][j]=f[i-1][j]|f[i-1][j-tmp];
			else f[i][j]=f[i-1][j];
			if (j>=k-a[x]&&j<k&&f[i][j]) return true;
		}
	}
	return false;
}

int main() {
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	int ans=1;
	for (ans=1;ans<=n;ans++) {
		if (ans>1&&a[ans]==a[ans-1]) continue;
		if (check(ans)) break;
	}
	cout<<ans-1<<endl;
}

Measured memory_bytes: 24832
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)


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

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  sort(a.begin(),a.end());
  ll sum=0;
  ll cnt=0;
  rep(i,n){
    if(sum*2<a[i]) cnt=1;
    else cnt++;
    sum+=a[i];    
  }
  cout << cnt << endl;

  
  return 0;
    

}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i,a,b) for (int i = (a); i < (b); i++ )
typedef pair<int,int> P;
typedef long long ll;
const int INF = 100000000;


int main (){
    ll ans = 10000000000;
    int N, K;
    cin >> N >>K;
    vector<ll> v,h(N);
    rep(i,0,N) {
        cin >> h[i];
    }
    sort(h.begin(),h.end());

    rep(i,0,N) {
        if (i+K-1>=N) {
            break;
        }
        else {
            ans = min(ans,h[i+K-1]-h[i]);
        }
    }

    cout << ans << endl;
}




Measured memory_bytes: 1024
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[3];
int main(){
	scanf("%d%d%d",&a[0],&a[1],&a[2]);
	sort(a,a+3);
	printf("%d\n",a[2]*10+a[1]+a[0]);
	return 0;
} 

Measured memory_bytes: 128
// A.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <map>
#include <set>

using namespace std;

typedef long long LL;

bool possible(LL t, vector<LL> a) {
	if (a.empty()) return t == 0;
	LL x = 0;
	int st = 0;
	int sz = (int)a.size();
	sort(a.rbegin(), a.rend());
	for (LL b = 60; b >= 0; --b) {
		LL f = 1LL << b;
		if (!(a[st] & f)) continue;
		for (int i = 0; i < sz; ++i) {
			if (i != st && (a[i] & f) != 0) a[i] ^= a[st];
		}
		++st;
		if (st >= sz) break;
		sort(a.rbegin(), a.rbegin() + sz - st);
	}
	for (LL b = 60; b >= 0; --b) {
		LL f = 1LL << b;
		if ((f & t) != 0 && (f & x) == 0) {
			for (int i = 0; i < sz; ++i) {
				if (a[i] & f) {
					x ^= a[i];
					break;
				}
			}
		}
	}
	return t == x;
}

int main(int argc, char* argv[]) {
	LL t, n;
	cin >> t;
	for (LL tt = 0; tt < t; ++tt) {
		cin >> n;
		vector<LL> a(n);
		for (LL i = 0; i < n; ++i) {
			cin >> a[i];
		}
		string s;
		cin >> s;
		int ans = 0;
		vector<LL> x;
		for (LL i = n - 1; i >= 0; --i) {
			if (s[i] != '0') {
				if (!possible(a[i], x)) {
					ans = 1;
					break;
				}
			} else {
				x.emplace_back(a[i]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
#define reg register
#define ll long long
inline ll read()
{
	ll x=0,w=0;char ch=getchar();
	while(!isdigit(ch))w|=ch=='-',ch=getchar();
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return w?-x:x;
}
int n;
char s[1000],t[1000];
int main()
{
    n=read();scanf("%s",s+1);scanf("%s",t+1);
    for(reg int i=1;i<=n;++i)cout<<s[i]<<t[i];
	return 0;
}


Measured memory_bytes: 256
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::ops::Bound::*;
use itertools::Itertools;
use num_traits::clamp;
use ordered_float::OrderedFloat;
use proconio::{input, marker::*, fastout};
use superslice::*;

const M: usize = 1_000_000_007usize;

#[fastout]
fn main() {
    input! {
        s: usize
    }

    let mut dp = vec![0usize; s + 1];
    dp[0] = 1;
    for i in 3..=s {
        for j in 0..=i-3 {
            dp[i] += dp[j];
            dp[i] %= M;
        }
    }
    let ans = dp[s];
    println!("{}", ans);
}


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

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }
template<typename T> void show(T& a, char del='\n', char last='\n'){ rep(i, a.size() - 1) cout << a[i] << del; cout << a[a.size() - 1] << last; }

struct Table {
  Table(int len) : table(len, 0) { RLE(); }
  void RLE(){
    rep(i, table.size()){
      if (table[i] < 0) continue;
      int count = 0;
      reps(j, i, table.size()){
        if (table[j] >= 0) count++;
        else break;
      }
      table[i] = count;
    }
  }
  bool insert(int cid){
    int len = cid % 5 == 1 ? 5 : 2;
    int pos = -1;
    rep(i, table.size()){
      if (table[i] < 0) continue;
      if (table[i] >= len){
        pos = i;
        break;
      }
    }
    if (pos == -1) return false;
    rep(i, len){
      table[pos + i] = ~cid;
    }
    return true;
  }
  void remove(int cid){
    rep(i, table.size()){
      if (cid == ~table[i]) table[i] = 0;
    }
    RLE();
  }
  vector<int> table;
};

int main()
{
  Table table(17);
  priority_queue<pair<int, int> > event;
  queue<pair<int, int> > wait;
  int ans[100] = {};
  rep(i, 100){
    int in_time = i * 5;
    event.push(make_pair(-in_time, i * 2));
  }
  while (!event.empty()){
    int now_time = -event.top().first;
    int cid = event.top().second;
    event.pop();
    bool is_in = cid % 2 == 0;
    cid /= 2;
    if (is_in){
      wait.push(make_pair(cid, now_time));
    }
    else {
      table.remove(cid);
    }
    while (!wait.empty()){
      int wc = wait.front().first;
      int wt = wait.front().second;
      if (!table.insert(wc)) break;
      ans[wc] = now_time - wt;
      event.push(make_pair(-(now_time + (17 * (wc % 2)) + (3 * (wc % 3)) + 19), wc * 2 + 1));
      wait.pop();
    }
  }
  int n;
  while (~scanf("%d", &n)){
    printf("%d\n", ans[n]);
  }

  return 0;
}

Measured memory_bytes: 1192
#include <bits/stdc++.h>
using namespace std;
int main(void){
   int n, d, ans = 0;
   cin >> n >> d;
   vector<vector<int>> x(n, vector<int>(10000));
   for(int i = 0; i < n; i++){
       for(int j = 0; j < d; j++){
           cin >> x.at(i).at(j);
           //cout << x.at(i).at(j)<<endl;
       }
   }
   for(int i = 0; i < n-1; i++){
       for(int j = i+1; j < n; j++){ 
           double dis = 0;
        for(int k = 0; k < d; k++){
        dis += pow(abs(x.at(i).at(k) - x.at(j).at(k)), 2.0);   
        }
        double y = sqrt(dis);
        double z = round(y);
        //cout << y << ' ' << z << endl;
        if(y==z) ans++;
       }
       
   }
   cout << ans << endl;
}


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

using namespace std;
const int maxn = 1e5 + 1000;
const int mod = 1e9 + 7;
int s[maxn],t[maxn];
char  str[maxn];
int main() {
    //std::ios::sync_with_stdio(false);
    int te;
    //cin>>str;
    scanf("%s",str);
    for(int i=0;str[i];i++){
        if(str[i]=='A') s[i+1]=s[i]+1;
        else s[i+1]=s[i]+2;
    }
    //cin>>str;
    scanf("%s",str);
    for(int i=0;str[i];i++){
        if(str[i]=='A') t[i+1]=t[i]+1;
        else t[i+1]=t[i]+2;
    }
    int n;
    int a,b,c,d;
    //cin>>n;
    scanf("%d",&n);
    for(int i=0;i<n;i++) {
        //cin>>a>>b>>c>>d;
        scanf("%d %d %d %d",&a,&b,&c,&d);
        if(((s[b]-s[a-1])-(t[d]-t[c-1]))%3==0){
            //cout<<"YES"<<endl;
            puts("YES");
        }
        else {
            //cout<<"NO"<<endl;
            puts("NO");
        }
    }
    return 0;
}


Measured memory_bytes: 1408
defmodule Main do

  def main do
    _ = IO.gets("")
    c = IO.gets("") |> String.trim() |> String.codepoints()

    r =  c |> count(0)
    r2 = c |> Enum.slice(0..r-1) |> count(0)

    r-r2 |> IO.puts()
  end

  def count([], acc), do: acc
  def count([c | t], acc) when c == "R", do: count(t, acc+1)
  def count([_ | t], acc), do: count(t, acc)
end


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

vector <long long int> idx[26];

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

	string s,t;
	long long int res = 0;
	long long int count = 0;
	int pos = 0;
	cin >> s;
	cin >> t;

	for(int i=0;i<s.length();i++)
	{
		idx[s[i]-'a'].push_back(i+1);
	}

	for(int i=0;i<t.length();i++)
	{
		if(idx[t[i]-'a'].size()==0)
		{
			cout << -1 << '\n';
			return 0;
		}
		int index = lower_bound(idx[t[i]-'a'].begin(),idx[t[i]-'a'].end(),pos+1) - idx[t[i]-'a'].begin();
		if(index==idx[t[i]-'a'].size())
		{
			count++;
			index = lower_bound(idx[t[i]-'a'].begin(),idx[t[i]-'a'].end(),1) - idx[t[i]-'a'].begin();
			res = (s.length()*count) + idx[t[i]-'a'][index];
			pos = idx[t[i]-'a'][index];
		}
		else
		{
			res = (s.length()*count) + idx[t[i]-'a'][index];
			pos = idx[t[i]-'a'][index];
		}
		//cout << count << ' ' << pos << ' ' << res << '\n';
	}

	cout << res << '\n';

	return 0;
}

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

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

  vector<vector<int>> a(n, vector<int>(n - 1));
  rep(i,n){
    rep(j, n - 1) { cin >> a[i][j];
      a[i][j]--;
    }
    reverse(a[i].begin(), a[i].end());
  }

  vector<pair<int, int>> q;
  auto check = [&](int i) {
    if (a[i].size() == 0) return;
    int j = a[i].back();
    if (a[j].size() == 0) return;
    if(a[j].back() == i){
      pair<int, int> p(i, j);
      if (p.second < p.first) swap(p.first, p.second);
      q.push_back(p);
    }
  };
  rep(i, n) { check(i); }
  int day = 0;
  while(q.size() >0){
    day++;
    sort(q.begin(), q.end());
    q.erase(unique(q.begin(), q.end()), q.end());
    vector<pair<int, int>> prevQ;
    swap(prevQ, q);
    for(pair<int,int> p : prevQ){
      int i = p.first, j = p.second;
      a[i].pop_back();
      a[j].pop_back();
    }
    for (pair<int, int> p : prevQ) {
      int i = p.first, j = p.second;
      check(i);
      check(j);
    }
  }

  rep(i,n){
      if (a[i].size() != 0){
        puts("-1");
        return 0;
      }
  }
  cout << day << endl;
  return 0;
}

Measured memory_bytes: 4224
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
pair<long long, long long>a[100000];
map<pair<long long, long long>, long long>M;
long long h, w, n;
int main() {
	cin >> h >> w >> n;
	for (long long i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; a[i].first--; a[i].second--; }
	sort(a, a + n);
	long long sum[10] = { (h-2)*(w-2),0,0,0,0,0,0,0,0,0 };
	for (long long i = 0; i < n; i++) {
		for (long long j = -2; j <= 0; j++) {
			for (long long k = -2; k <= 0; k++) {
				long long X = a[i].first + j, Y = a[i].second + k;
				if (X < 0 || Y < 0 || X + 2 >= h || Y + 2 >= w)continue;
				if (M[make_pair(X, Y)] == 1)continue;
				long long cnt = 0;
				for (long long l = 0; l <= 2; l++) {
					for (long long m = 0; m <= 2; m++) {
						long long cx = X + l, cy = Y + m;
						long long D = lower_bound(a, a + n, make_pair(cx, cy)) - a;
						if (a[D] == make_pair(cx, cy))cnt++;
					}
				}
				if (cnt >= 1) {
					sum[0]--; sum[cnt]++; M[make_pair(X, Y)] = 1;
				}
			}
		}
	}
	for (int i = 0; i <= 9; i++)cout << sum[i] << endl;
	return 0;
}

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

using namespace std;
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define sz size
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORN(i,a,b) for(int i=a;i<=b;i++)
#define FORRN(i,a,b) for (int i=(a);i>=(b);i--)
#define FORR(i,a,b) for (int i=(a);i>(b);i--)
typedef unsigned long long ull;
typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false); //Fast I/O
    cin.tie(0);
    cout.tie(0);
	//freopen("input.in","r",stdin);
	//freopen("output.out","w",stdout);

    int a,b,k;
    cin >> a >> b >> k;

    vector<int> A;

    FORN(i,1,min(a,b)){
        if(a%i==0 && b%i==0)
            A.pb(i);
    }

    sort(A.rbegin(),A.rend());

    cout << A[k-1] << endl;

    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
//constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
    */
	ll n, d, x;
	scanf("%lld %lld %lld", &n, &d, &x);
	vector<vector<ll>> p(d, vector<ll>(n));
	for (int i = 0; i < d; i++)for (int j = 0; j < n; j++) scanf("%lld", &p[i][j]);
	vector<ll> dp(200000, -1);
	dp[x] = 0;
	for (int i = 0; i < d - 1; i++) {
		for (int j = 0; j < n; j++) {
			if (p[i][j] >= p[i + 1][j]) continue;
			for (int k = 100000; k >= p[i][j]; k--) {
				if (dp[k] == -1) continue;
				chmax(dp[k - p[i][j]], dp[k] + p[i+1][j]);
			}
		}
		vector<ll> tmp(200000, -1);
		for (int k = 100000; k >= 0; k--) {
			if(dp[k] != -1) tmp[k + dp[k]] = 0;
		}
		dp = tmp;
	}

	ll res = 0;
	for (ll i = 0; i <= 100000; i++) {
		if (dp[i] == 0) chmax(res, i);
	}
	cout << res << endl;
	return 0;
	/*
		おまじないを使ったらscanfとprintf関連注意！！！！！！！！！！！！
	*/
}



Measured memory_bytes: 6056
package main

import (
	"fmt"
	"strings"
)

func main() {
	var n string
	fmt.Scanf("%s", &n)
	n = strings.Replace(n, "9", "2", -1)
	n = strings.Replace(n, "1", "9", -1)
	n = strings.Replace(n, "2", "1", -1)
	fmt.Printf("%s\n", n)
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M; cin >> N >> M;
  int A[N], tot = 0;
  for(int i = 0; i < N; i++){
    cin >> A[i];
    tot += A[i];
  }
  int count = 0;
  for(int j = 0; j < N; j++){
    if(A[j] * 4 * M >= tot) count += 1;
  }
  if(count < M) cout << "No";
  else cout << "Yes";
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;

int main(){
    ll n;
    cin >> n;
    ll sum =0;
    ll ans1 = 0;
    ll a[n];
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    for(int i=0;i<n;i++){
        sum += a[i];
        if(i%2==0){
            if(sum <= 0){
                ans1+= abs(1-sum);
                sum = 1;
            }
        }
        else{
            if(sum >= 0){
                ans1 += abs(-1-sum);
                sum = -1;
            }
        }
    }

    ll ans2 = 0;
    sum = 0;
    for(int i=0;i<n;i++){
        sum += a[i];
        if(i%2==1){
            if(sum <= 0){
                ans2+= abs(1-sum);
                sum = 1;
            }
        }
        else{
            if(sum >= 0){
                ans2 += abs(-1-sum);
                sum = -1;
            }
        }
    }

    ll ans = min(ans1,ans2);
    cout << ans << endl;
    return 0;
       
}

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

int main(){
    string s, temp;
    int K;
    cin >> s >> K;
    vector<string> sub(10000);
    
    for(int i=0; i<s.size(); i++){
        for(int j=1; j<=K; j++){
            temp = s.substr(i,j);
            sub.push_back(temp);
        }
    }
    sort(sub.begin(), sub.end());
    sub.erase(unique(sub.begin(),sub.end()),sub.end());

    cout << sub.at(K) << endl;
}

Measured memory_bytes: 1760
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;

public class Main {
	char[] ch;
	int[][] dp;
	static int INF = (int)1e9 + 7;

	public void solve() {
		while (true) {
			/*StringBuilder sb = new StringBuilder();
			for(int i = 0;i < 100000;i++){
				sb.append(i  % 3);
			}*/
			String S = next();

			if (S.equals("#"))
				break;
			ch = S.toCharArray();
			for (int i = 0; i < ch.length; i++) {
				ch[i] = (char) (ch[i] - 1);
			}

			dp = new int[ch.length + 1][2];
			for(int i = 1;i < ch.length;i++){
				int n = (ch[i] - '0') % 3;
				int p = (ch[i - 1] - '0') % 3;
				if(n > p){
					dp[i][0] = Math.min(dp[i - 1][1],dp[i - 1][0] + 1);
					dp[i][1] = INF;
				}else if(n < p){
					dp[i][1] = Math.min(dp[i - 1][0],dp[i - 1][1] + 1);
					dp[i][0] = INF;
				}else{
					dp[i][1] = dp[i - 1][0];
					dp[i][0] = dp[i - 1][1];
				}

			}
			out.println(Math.min(dp[ch.length - 1][0], dp[ch.length - 1][1]));
		}
	}

	public static void main(String[] args) {
		out.flush();
		new Main().solve();
		out.close();
	}

	/* Input */
	private static final InputStream in = System.in;
	private static final PrintWriter out = new PrintWriter(System.out);
	private final byte[] buffer = new byte[2048];
	private int p = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (p < buflen)
			return true;
		p = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (buflen <= 0)
			return false;
		return true;
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrint(buffer[p])) {
			p++;
		}
		return hasNextByte();
	}

	private boolean isPrint(int ch) {
		if (ch >= '!' && ch <= '~')
			return true;
		return false;
	}

	private int nextByte() {
		if (!hasNextByte())
			return -1;
		return buffer[p++];
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = -1;
		while (isPrint((b = nextByte()))) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

Measured memory_bytes: 48580
n, k, q = [int(i) for i in input().split()]
a = []
for i in range(q):
    a.append(int(input())-1)
b = [k-q]*n
for i in a:
    b[i] += 1
for i in range(len(b)):
    print('Yes' if b[i]>=1 else 'No')

Measured memory_bytes: 10452
#include<stdio.h>

struct Node_struct{int id, par, dep, left, right, sib, height;};
typedef struct Node_struct Node;
Node node[200000];

int dfs(int pos, int dep){
  node[pos].dep = dep;
  int a = 0, b = 0;
  if(node[pos].left != -1) a = dfs(node[pos].left, dep + 1);
  if(node[pos].right != -1) b = dfs(node[pos].right, dep + 1);
  node[pos].height = a < b? b:a;
  return node[pos].height + 1;
}

void print(int pos,int type){
  if(type == 0) printf(" %d", pos);
  if(node[pos].left != -1) print(node[pos].left, type);
  if(type == 1) printf(" %d", pos);
  if(node[pos].right != -1) print(node[pos].right, type);
  if(type == 2) printf(" %d", pos);
}

int main(){
  int i, j, k;
  int N;
  int id, left, right;
  int deg;
  int root;
  scanf("%d", &N);
  for(int i=0;i<N;i++) node[i].par = node[i].left = node[i].right = node[i].sib = -1;
  
  for(i=0;i<N;i++){
    scanf("%d %d %d", &id, &left, &right);
    node[id].id = id;
    node[id].left = left;
    node[id].right = right;
    if(left != -1) node[left].par = id, node[left].sib = right;
    if(right != -1) node[right].par = id, node[right].sib = left;
  }
  
  for(i=0;i<N;i++) if(node[i].par == -1) root = i;
  printf("Preorder\n");
  print(root, 0);
  printf("\n");
  
  printf("Inorder\n");
  print(root, 1);
  printf("\n");
  
  printf("Postorder\n");
  print(root, 2);
  printf("\n");  
  
  return 0;
}


Measured memory_bytes: 2032
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using p = pair<int,int>;
int main(int argc, const char * argv[]) {



    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin>>n;
    int a[n];
    rep(i,n)cin>>a[i];
    int mx=a[0];
    ll sum=0;
    for(int i=1;i<n;i++){
        mx=max(mx,a[i]);
        if(mx!=a[i])
            sum+=(mx-a[i]);
    }
    cout<<sum<<endl;


    return 0;
}


Measured memory_bytes: 4400
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
const int MX = 1100;
vector<int> g[MX*MX/2];
int a[1100][1100];
int dp[MX*MX];
int dfs(int now){
	if(g[now].size() == 0)return 0;
	if(dp[now] != -1)return dp[now];
	int ret = 0;
	for(auto next:g[now]){
		ret = max(ret,dfs(next)+1);
	}
	return dp[now] = ret;
}
signed main(){
	rep(i,MX*MX)dp[i] =  -1;
	int n;cin >> n;
	map<P,int> id;
	int tmp = 0;
	for(int i = 0;i < n;i++)for(int j = i+1;j < n;j++){
		id[P(i,j)] = tmp++;
	}
	rep(i,n)rep(j,n-1){
		cin >> a[i][j];
		a[i][j]--;
	}
	vector<int> in(tmp);
	rep(i,n){
		rep(j,n-2){
			int p1 = i,p2 = a[i][j];
			if(p1>p2)swap(p1,p2);
			int ID1 = id[P(p1,p2)];
			// trace(i,p1,p2,ID1);
			p1 = i,p2 = a[i][j+1];
			if(p1 > p2)swap(p1,p2);
			int ID2 = id[P(p1,p2)];
			// trace(i,p1,p2,ID2);
			g[ID1].push_back(ID2);
			in[ID2]++;
		}
	}
	// trace(id);
	//0,1->0,2
	//0->1
	//1,0 -> 1,2
	//0-> 2
	//2,0->2,1
	//1 -> 2
	// rep(i,3)trace(g[i]);
	queue<int> q;
	vector<int> res;
	rep(i,tmp){
		if(in[i] == 0)q.push(i);
	}
	while(q.size()){
		int now = q.front();q.pop();
		for(auto next:g[now]){
			in[next]--;
			if(in[next] == 0)q.push(next);
		}
		res.push_back(now);
	}
	if(res.size() != tmp){
		cout << -1 << endl;
		return 0;
	}
	int ret = 0;
	rep(i,tmp)ret = max(ret,dfs(i));
	
	cout << ret+1 << endl;
	return 0;
}

Measured memory_bytes: 90428
#include <iostream>
#include <string>
#include <algorithm>
#include <climits>

using namespace std;

int main() {
    string str; cin >> str;
    int aIndex = INT_MAX;
    int zIndex = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == 'A')       aIndex = min(i, aIndex);
        else if (str[i] == 'Z')  zIndex = max(i, zIndex);
    }

    cout << zIndex - aIndex + 1 << endl;
    return 0;
}

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

int main() {
    long long int n, i, p;
    cin>>n;
    
    for(i = sqrt(n); i>=0; i--)
    {
        if (n % i == 0)
        {
            p = n/i;
            break;
        }
    }
    
    cout<<i+p-2<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define sz(x)             (int)((x).size())
#define int               long long
#define M                 1000000007
#define MM                998244353
#define fr                first
#define sc                second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount

template<typename T1,typename T2>
std::istream& operator>>(std::istream& in,pair<T1,T2> &a)
{
	in>>a.fr>>a.sc;
	return in;
}
template<typename T1,typename T2>
std::ostream& operator<<(std::ostream& out,pair<T1,T2> &a)
{
	out<<a.fr<<" "<<a.sc;
	return out;
}
int h,w;
string s[1005];
int dp[1005][1005];

int calc(int x,int y){
	if(x==h-1 && w-1==y){
		return 1;
	}
	if(x==h || w==y)return 0;
	int &ans=dp[x][y];
	if(ans!=-1)return ans;
	if(s[x][y]=='#')ans=0;
	else ans=(calc(x+1,y)+calc(x,y+1))%M;
	return ans;
}

void solve(){
	mem1(dp);
	cin>>h>>w;
	rep(i,0,h)cin>>s[i];
	cout<<calc(0,0);
}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SEIVE
		seive();
	#endif
	#ifdef NCR
		init();
	#endif
	#ifdef DSU
		cleardsu(MAXDSUSIZE);
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


Measured memory_bytes: 9216
#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<algorithm>
#include<tuple>
#include<utility>
#include<cctype>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0;i<n;++i)
#define RUP(a,b) ((a+b-1)/(b))
#define enter "\n"

typedef long long ll;


int dp_a(){
    //入力
    int n,k;
    cin >> n >> k;
    vector<ll> h;
    REP(i,n){
        ll tmp;
        cin >> tmp;
        h.push_back(tmp);
    }
    ll cntdp[110000];
    
    //処理
    REP(i,n){
        cntdp[i]=INT_MAX;
    }
    cntdp[0]=0;
    for(int i=1 ; i<k ; ++i){
        cntdp[i] = abs(h[i]-h[0]);
    }
    REP(i,n){
        for(int j=1 ; j<=k ; ++j){
            if(i-j>=0) cntdp[i]=min(cntdp[i-j]+abs(h[i]-h[i-j]) , cntdp[i]);
        }
    }
    
    //出力
    cout << cntdp[n-1] << enter;
    return 0;
}

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


Measured memory_bytes: 1908
#include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<ll,ll> P;
const ll MOD=1000000009;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-9;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int h,w;
char c[51][51];
int main(){
	cin>>h>>w;
	int co=0;
	bool f=false;
	for(int i=0;i<h;i++){
		cin>>c[i];
	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int cc=0;
			if(c[i][j]=='#'){
				for(int k=0;k<4;k++){
					int y=i+dy[k];
					int x=j+dx[k];
					if(y<0||y>=h||x<0||x>=w)continue;
					if(c[y][x]=='#')cc++;
				}
				if(cc==0)f=true;
			}
		}
	}
	if(f){
		cout<<"No"<<endl;
	}else{
		cout<<"Yes"<<endl;
	}
    return 0;
}


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

#include<set>
using namespace std;
#define rep(i,n) for (int i = 1; i < (n); i++) 
#define out(n) cout << n << endl;

bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}

int main() {
    long long n,num = 2, divnum;
    int count = 0;
    cin >> n;

    while(n > 1 ){
        if(IsPrime(num) == true){
            divnum = num;
            while((n%divnum) == 0){
                n = n/divnum;
                count++;
                divnum = divnum*num;
            }
        }
        num++;
        if(num > 1000000){
            break;
        }
    }   

    if(n>1000000){
        count++;
    }

    out(count); 
}

Measured memory_bytes: 3680
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int mxn=1e5+5;
int n, m, h[mxn];
bool good[mxn];
int main(){
	fast;
	cin>>n>>m;
	for(int i=0; i<n; i++) cin>>h[i+1];
	memset(good, true, sizeof(good));
	for(int i=0, u, v; i<m; i++){
		cin>>u>>v;
		if(h[u]>=h[v]) good[v]=false;
		if(h[v]>=h[u]) good[u]=false;
	}
	int cnt=0;
	for(int i=1; i<=n; i++) if(good[i]) cnt++;
	cout<<cnt<<endl; return 0;
}

Measured memory_bytes: 4140
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define fr(i,j) for(int i=beg[j];i;i=nex[i])
using namespace std;
int const mn=1e5+3;
int n,a[mn];
int gcd(int x,int y){
	int z;
	while(z=x%y){
		x=y;
		y=z;
	}
	return y;
}
int dfs(){
	int tmp=0,tm2=0,tm3=0;
	fo(i,1,n)if((a[i]&1)&&(a[i]>1))tm2++,tm3=i;
		else if(!(a[i]&1))tmp=!tmp;
	if(tmp)return 1;
	if(tm2!=1)return 0;
	a[tm3]--;
	tmp=a[1];
	fo(i,2,n)tmp=gcd(tmp,a[i]);
	fo(i,1,n)a[i]/=tmp;
	return !dfs();
}
int main(){
	scanf("%d",&n);
	fo(i,1,n)scanf("%d",&a[i]);
	if(dfs())printf("First");
	else printf("Second");
	return 0;
}


Measured memory_bytes: 512
import java.io.BufferedInputStream;
import java.util.Scanner;

/**
 * Created by Harry on 4/4/20.
 */
//https://atcoder.jp/contests/dp/tasks/dp_n
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        int N = scanner.nextInt();
        int[] A = new int[N];
        for(int i=0; i<N; i++){
            A[i] = scanner.nextInt();
        }
        long[] preSum = new long[N+1];
        for(int i=1; i<=N; i++){
            preSum[i] = preSum[i-1]+A[i-1];
        }

        long[][] dp = new long[N][N];
        for(int j=1; j<N; j++){
            for(int i=j-1; i>=0; i--){
                dp[i][j] = Long.MAX_VALUE/2;
                for(int k=j; k>=i+1; k--){
                    dp[i][j] = Math.min(dp[i][j], dp[i][k-1]+dp[k][j]+preSum[j+1]-preSum[i]);
                }
            }
        }

        System.out.print(dp[0][N-1]);
    }
}


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

int main() {
    int k;
    cin >> k;
    int ans=0;
    ll x=0;
    ll y=7;
    x=7;
    x %= k;
    if(x==0){
        cout << 1 << endl;
        return 0;
    }
    for(int i = 2; i < 50000000; i++) {
        y *= 10;
        y %= k;
        x += y;
        x %= k;
        if(x==0){
            ans = i;
            break;
        }
    }

    if(ans == 0) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3596
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingDeque;

public class Main {
	static class Pair {
		int fst, snd;

		Pair(int fst, int snd) {
			this.fst = fst;
			this.snd = snd;
		}
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int M = s.nextInt();
		int[] bits = new int[N];
		int[] targets = new int[M];
		for (int i = 0; i < N; i++) {
			bits[i] = s.nextInt();
		}
		for (int i = 0; i < M; i++) {
			targets[i] = s.nextInt();
		}

		int initial = bitToNum(N, bits);
		Pair ans = getAnswers(targets);
		
		if (initial == ans.fst || initial == ans.snd) {
			System.out.println("0");
			return;
		}
		
		boolean[] reached = new boolean[1 << N];
		Arrays.fill(reached, false);

		Queue<Pair> q = new LinkedBlockingDeque<Pair>();
		q.add(new Pair(initial, 0));
		reached[initial] = true;
		while (!q.isEmpty()) {
			Pair pair = q.poll();
			for (int r = 0; r < N - 1; r++) {
				int l = r + 1;
				if (isSameBit(pair.fst, r, l)) {
					continue;
				}
				// System.out.println("l = " + l + " , r = "+ r);
				// System.out.println("lmsk = " + (pair.fst & (1 << l)) + " , rm = "+ (pair.fst & (1 << r)));
				int mask = (1 << l) + (1 << r);
				int value = pair.fst ^ mask;
				// System.out.printf("from: %d, to: %d mask: %d%n", pair.fst, value, mask);
				if (value == ans.fst || value == ans.snd) {
					System.out.println(pair.snd + 1);
					return;
				}
				if (!reached[value]) {
					reached[value] = true;
					// System.out.println(":- " + value + ", " + pair.snd);
					q.add(new Pair(value, pair.snd + 1));
				}

			}
		}
	}

	private static boolean isSameBit(int value, int r, int l) {
		int left = value & (1 << l);
		int right = value & (1 << r);
		return (left == 0 && right == 0) || (left > 0 && right > 0);
	}

	private static Pair getAnswers(int[] targets) {
		List<Integer> xs = new ArrayList<Integer>();
		int bit = 0;
		for (int i = 0; i < targets.length; i++) {
			for (int j = 0; j < targets[i]; j++) {
				xs.add(bit);
			}
			bit = (bit == 0) ? 1 : 0;
		}
		List<Integer> ys = new ArrayList<Integer>();
		bit = 1;
		for (int i = 0; i < targets.length; i++) {
			for (int j = 0; j < targets[i]; j++) {
				ys.add(bit);
			}
			bit = (bit == 0) ? 1 : 0;
		}

		return new Pair(bitToNum(xs), bitToNum(ys));
	}

	private static int bitToNum(List<Integer> xs) {
		int N = xs.size();
		int[] bits = new int[N];
		for (int i = 0; i < N; i++) {
			bits[i] = xs.get(i);
		}
		return bitToNum(N, bits);
	}

	private static int bitToNum(int N, int[] bits) {
		int initial = 0;
		for (int i = 0; i < N; i++) {
			initial += (bits[N - i - 1] << i);
		}
		return initial;
	}
}

Measured memory_bytes: 20892
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <bitset>
#include<vector>
#include<utility>
#include<fstream>
#include<queue>
#include <iomanip>
#include<numeric>
#include<set>
#include<math.h>

using namespace std;
typedef long long ll;
#define rep(i,k) for(i=0;i<k;i++)
#define P pair<ll,ll>
#define PI 3.141592653589793284626433832
ll mod = 1000000007;
P p[200005];

double i, b, c, k, j, n, m,ss, z = 0,t, pa[305][305] = {}, l;
double a,q;
vector<vector<ll>> v(305);
string s;
char ca[10000];


int main() {
	cin >>a>>b>>c;
	double max;
	max = a * a * b;
	if (c >= (max / 2)) 
		t = 2 * (a * a * b - c) / (a * a * a);
	else t = (b * b*a) / (2 * c);
		z = atan(t);
		q = (z * 180) / PI;
	



	cout << std::setprecision(10)<<q;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll INF = LLONG_MAX;
const ll MINF = LLONG_MIN;
const int INT_INF = INT_MAX;

int main()
{
    int n;
    cin >> n;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
    {
        cin >> p[i];
    }
    int ma = INT_MAX;
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        ma = min(ma, p[i]);
        if (ma == p[i])
            cnt++;
    }
    cout << cnt << endl;
}

Measured memory_bytes: 1024
def can_reach_to_0(s, comm, pos):
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos < 0:
            return True
        if pos >= N:
            return False
    return False

def can_reach_to_N(s, comm, pos):
    N = len(s)
    for t, d in comm:
        if s[pos] == t:
            if d == 'L':
                pos -= 1
            else:
                pos += 1
        if pos >= N:
            return True
        if pos < 0:
            return False
    return False

N, Q = list(map(int, input().split()))
s = input()
comm = [input().split() for _ in range(Q)]

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_0(s, comm, mid):
        l = mid + 1
    else:
        r = mid - 1
left = r # いける右端index

l = 0
r = N-1
while l <= r:
    mid = int((l+r)/2)
    if can_reach_to_N(s, comm, mid):
        r = mid - 1
    else:
        l = mid + 1
right = l # いける左端index
print(right-left-1)


Measured memory_bytes: 41288
N = int(input())
W = list(map(int, input().split()))
ans = 10**9
for i in range(0,N-1):
    S1 = sum(W[0:i+1])
    S2 = sum(W[i+1:N])
    ans = min(ans,abs(S1-S2))
print(ans)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9, MOD = 1e9 + 7; /// think
const ll LINF = 1e18;


const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
    return 0 <= x && 0 <= y && x < n && y < m;
}

template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }

main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int x;
    cin >> x;
    int cnt = x / 500;
    x %= 500;
    cout << cnt * 1000 + (x / 5) * 5 << '\n';
}


Measured memory_bytes: 3648
if __name__ == "__main__":
    # UUR
    # DDL
    # LU UUR RD
    # RD DDL LU

    sx, sy, tx, ty = map(int, input().split())

    result = (
        "U" * (ty - sy)
        + "R" * (tx - sx)
        + "D" * (ty - sy)
        + "L" * (tx - sx)
        + "LU"
        + "U" * (ty - sy)
        + "R" * (tx - sx)
        + "RD"
        + "RD"
        + "D" * (ty - sy)
        + "L" * (tx - sx)
        + "LU"
    )

    print(result)



Measured memory_bytes: 3060
#include<iostream>
using namespace std;
#define MAX 100005
#define NIL -1
struct Node{int p,l,r;
};
Node T[MAX];
int n,D[MAX];
void print(int u)
{
	int i,c;
	cout<<"node "<<u<<": ";
	cout<<"parent = "<<T[u].p<<", ";
	cout<<"depth = "<<D[u]<<", ";
	if(T[u].p==NIL)cout<<"root, ";
	else if(T[u].l==NIL)cout<<"leaf, ";
	else cout<<"internal node, ";
	cout<<"[";
	for(i=0,c=T[u].l;c!=NIL;i++,c=T[c].r)
	{
		if(i)cout<<", ";
		cout<<c;
	}
	cout<<"]"<<endl;
}
int rec(int u,int p)
{
	D[u]=p;
	if(T[u].r!=NIL)rec(T[u].r,p);
	if(T[u].l!=NIL)rec(T[u].l,p+1);
}
int main()
{
	int i,j,d,v,c,l,r;
	cin>>n;
	for(i=0;i<n;i++)T[i].p=T[i].l=T[i].r=NIL;
	for(i=0;i<n;i++)
	{
		cin>>v>>d;
	    for(j=0;j<d;j++)
	    {
	    	cin>>c;
	    	if(j==0)T[v].l=c;
	    	else T[l].r=c;
	    	l=c;
	    	T[c].p=v;
	    }
	}
	for(i=0;i<n;i++)
	{
		if(T[i].p==NIL)r=i;
	}
	rec(r,0);
	for(i=0;i<n;i++)print(i);
	return 0;
}

Measured memory_bytes: 7768
///
// File:  a.go
// Author: ymiyamoto
//
// Created on Sun May 13 15:33:53 2018
//
package main

import (
	"fmt"
)

var a, b, c, d int

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func main() {
	fmt.Scan(&a, &b, &c, &d)
	if abs(a-c) <= d {
		fmt.Println("Yes")
		return
	} else if abs(a-b) <= d && abs(b-c) <= d {
		fmt.Println("Yes")
		return
	}
	fmt.Println("No")
}


Measured memory_bytes: 512