akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const double EPS = 1e-10;
const int INF = 1001001001;

struct Data
{
	int y, left, right, pos, oxy, cost;
	Data(){}
	Data(int y, int l, int r, int p, int o, int c) : y(y), left(l), right(r), pos(p), oxy(o), cost(c) {}
	bool operator< (const Data& d)const {
		return cost > d.cost;
	}
};

int main()
{
	int w, h;
	while(scanf("%d%d", &w, &h), w){
		int f, m, o;
		scanf("%d%d%d", &f, &m, &o);
		
		int board[11][10] = {{0}};
		rep(i, h) rep(j, w)
			scanf("%d", &board[i+1][j]);
		
		priority_queue<Data> Q;
		Q.push(Data(0, 0, w, 0, o, 0));
		
		int ans = -1;
		bool visited[11][10][11][10][50] = {{{{{false}}}}};
		while(!Q.empty()){
			Data d = Q.top();
			Q.pop();
			if(visited[d.y][d.left][d.right][d.pos][d.oxy])
				continue;
			visited[d.y][d.left][d.right][d.pos][d.oxy] = true;
			if(d.oxy == 0)
				continue;
			if(d.y == h){
				ans = d.cost;
				break;
			}
			if(d.oxy == 1)
				continue;
			
			// go down
			Data down(d.y+1, d.pos, d.pos+1, d.pos, 0, 0);
			down.oxy = min(m, d.oxy - 1 + max(0, board[d.y+1][d.pos]));
			down.cost = d.cost - min(0, board[d.y+1][d.pos]);
			if(down.cost <= f)
				Q.push(down);
			
			const int dx[] = {-1, 1};
			rep(i, 2){
				Data next;
				next.y = d.y;
				next.pos = d.pos + dx[i];
				if(next.pos<0 || w<=next.pos)
					continue;
				
				bool cave = next.pos < d.left || d.right <= next.pos;
				next.left = min(next.pos, d.left);
				next.right = max(next.pos+1, d.right);
				if(cave){
					next.oxy = min(m, d.oxy - (d.y!=0) + max(0, board[d.y][next.pos]));
					next.cost = d.cost - min(0, board[d.y][next.pos]);
				}
				else{
					next.oxy = d.oxy - (d.y!=0);
					next.cost = d.cost;
				}
				
				if(next.cost <= f)
					Q.push(next);
			}
		}
		if(ans == -1 || ans > f)
			puts("NA");
		else
			printf("%d\n", ans);
	}
	return 0;
}

Measured memory_bytes: 1868
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

monster_num=sum(A)
for i in range(N):
    #girigiri
    if A[i] >= B[i]:
        A[i]-=B[i]
        B[i]=0
    #yoyuuari
    else:
        B[i] -= A[i]
        A[i]=0

        if A[i+1] >= B[i]:
            A[i+1]-=B[i]
            B[i]=0
        else:

            B[i]-=A[i+1]
            A[i + 1] = 0
print(monster_num-sum(A))





Measured memory_bytes: 18624
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <tuple>
#include <random>

using namespace std;
typedef long long int ll;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep,)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()

#define INF 10000000000

ll a, b, c, n, z, w, h, ans = 0, cnt = 0, mx = 0, mn = INF;
string s;

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

    cin >> n;
    ll t[n + 1], x[n + 1], y[n + 1];
    t[0] = 0, x[0] = 0, y[0] = 0;
    rep(i, 1, n + 1){
        cin >> t[i] >> x[i] >> y[i];
    }
    rep(i, 1, n + 1){
        ll delta_t = t[i] - t[i - 1], dist = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
        if(dist > delta_t || dist % 2 != delta_t % 2){
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;
}



Measured memory_bytes: 2560
#include <stdio.h>
#include <string.h>

void insertSort(int *seq, int insert_num, int now_place);
int no;

int main(void)
{
	int buyer[10001];
	int in;
	int i;
	unsigned int ans;
	unsigned int tmp;
	
	while (1){
		memset(buyer, 61, sizeof(int) * 10001);
		scanf("%d", &no);
		
		if (no == 0){
			break;
		}
		
		for (i = 0; i < no; i++){
			scanf("%d", &in);
			insertSort(buyer, in, i);
		}
		
		ans = tmp = 0;
		
		for (i = 0; i < no - 1; i++){
			tmp = tmp + buyer[i];
			ans += tmp;
		}
		
		printf("%u\n", ans);
	}
	
	return (0);
}

void insertSort(int *seq, int insert_num, int now_place)
{
	int i;
	
	for (i = 0; i < no; i++){
		if (seq[i] > 60){
			seq[i] = insert_num;
			return;
		}
		
		else if (seq[i] > insert_num){
			memmove(seq + (i + 1), seq + i, sizeof(int) * (now_place - i + 1));
			seq[i] = insert_num;
			return;
		}
	}
}

Measured memory_bytes: 0
import sys
input=sys.stdin.readline
n,m,q=map(int,input().split())
num=[[0]*(n+1) for _ in range(n+1)]
for i in range(m):
	l,r=map(int,input().split())
	num[l][r]+=1
for i in range(n):
	for j in range(n):
		num[i+1][j+1]+=num[i+1][j]
for i in range(n):
	for j in range(n):
		num[i+1][j+1]+=num[i][j+1]
for i in range(q):
	p,q=map(int,input().split())
	ans=num[q][q]+num[p-1][p-1]-num[p-1][q]-num[q][p-1]
	print(ans)




Measured memory_bytes: 13568
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Collections;
import java.util.StringTokenizer;
 
public class Main {
	static int dp[] = { 0 , 0 , 0};
	
	
	public static void main(String[] args) {
		FastScanner sc=new FastScanner();
		int n = sc.nextInt();
		for(int i = 0; i < n ; ++i) {
			int happiness[] = sc.readArray(3);
			int newDp[] = new int[3];
			Arrays.fill(newDp, 0);
			
			for(int prev = 0 ; prev < 3 ; ++prev) {
				for(int curr  = 0 ; curr < 3 ;++curr) {
					if(prev != curr) {
						newDp[curr] = Math.max(newDp[curr], (dp[prev] + happiness[curr]));
					}
				}
			}
			dp = newDp;
		}
		System.out.println(Math.max(Math.max(dp[0], dp[1]), dp[2]));
		
	}
	
	public void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
	}
 
}


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

int main() {
	int n;
	scanf("%d",&n);
	while(n){
		map<char,char> trans;
		char c1,c2;
		for(int i=0;i<n;i++){
			scanf(" %c %c",&c1,&c2);
			trans[c1]=c2;
		}
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf(" %c",&c1);
			if(trans.find(c1)!=trans.end()){
				printf("%c",trans[c1]);
			}else{
				printf("%c",c1);
			}
		}
		printf("\n");
		scanf("%d",&n);
	}
	return 0;
}

Measured memory_bytes: 1164
#include <stdio.h>

main(){
  int N, i, p, min;

  scanf("%d",&N);
  int A[N+1];

  for(i = 0; i < N; i++){
    scanf("%d", &A[i]);
    if(i == 1){
      min = A[i-1];
      p = A[i] - A[i-1];
      }
  }
  for(i = 1; i < N; i++){
    if(min > A[i]){
      min = A[i];
    }else if(A[i] - min > p){
      p = A[i] - min;
    }
  }
  printf("%d\n",p);
  return 0;
}

Measured memory_bytes: 1376
#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

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

static const double eps = 10e-8;

bool primeTable[1000001];
int primeBIT[1000001];

int sumBIT(int i,int* bit){
	int s = 0;
	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void addBIT(int i, int x,int* bit,int n){
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}	
}

int main(){
	fill((bool*)primeTable,(bool*)primeTable+1000001,true);
	fill((int*)primeBIT,(int*)primeBIT+1000001,0);

	primeTable[0] = false;
	primeTable[1] = false;

	for(int i=1;i*i<=1000000;i++){
		if(primeTable[i]){
			for(int j=i+i;j<=1000000;j+=i){
				primeTable[j] = false;
			}
		}
	}

	for(int i=1;i<=1000000;i++){
		addBIT(i,primeTable[i],primeBIT,1000000);
	}


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

		int sum=0;
		for(int i=0;i<n;i++){
			int p,m;
			scanf("%d %d",&p,&m);

			int tmp=0;
			tmp = sumBIT(p+m >=1000000 ? 1000000 : p+m,primeBIT) 
				- sumBIT(p-m-1 >= 0 ? p-m-1 : 0,primeBIT);

			tmp > 0 ? sum+=tmp-1 : sum--;
		}

		printf("%d\n",sum >= 0 ? sum : 0);
	}
}

Measured memory_bytes: 5628
// Jai Mata Di
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
void solveTest(){
  string s, t;
  cin>>s>>t;
  int ans = t.length();
  int temp = 0;
  for(int i=0; i<s.length()- t.length() +1 ; i++){
    temp = 0;
    for(int j=0; j<t.length(); j++){
      if(s[i+j]!=t[j]) temp++;
    }
    ans = min(ans, temp);
  }
  cout<<ans<<"\n";
}
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t=1, i=1;
  //cin>>t;//Comment if only one test case
  while(i<=t){
    //cout<<"Case #"<<i<<": ";
    solveTest();
    i++;
  }
  return 0;
}


Measured memory_bytes: 3604
import sys
N = int(input())
ar = list(map(int,input().split()))

mx, mn = -(10**7), 10**7
mxi = mni = -1
for i in range(N):
    if mx < ar[i]:
        mx = ar[i]
        mxi = i
    if mn > ar[i]:
        mn = ar[i]
        mni = i
if mx == mn == 0:
    print(0)
    sys.exit()

sign = 1
ans = []
if mx * mn < 0:
    if mx + mn >= 0:
        for i in range(N):
            if ar[i] < 0:
                ar[i] += mx
                ans.append((mxi+1, i+1))
    else:
        sign = -1
        for i in range(N):
            if ar[i] > 0:
                ar[i] += mn
                ans.append((mni+1, i+1))
elif mx <= 0:
    sign = -1

if sign > 0:
    for i in range(N-1):
        if ar[i] > ar[i+1]:
            ar[i+1] += ar[i]
            ans.append((i+1, i+2))
else:
    for i in reversed(range(1,N)):
        if ar[i] < ar[i-1]:
            ar[i-1] += ar[i]
            ans.append((i+1, i))

print(len(ans))
for a in ans:
    print(str(a[0]) + ' ' + str(a[1]))
#print(ar)


Measured memory_bytes: 3192
// https://atcoder.jp/contests/abc142/tasks/abc142_d
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int c, int d) { return c / gcd(c, d) * d; }

// 素因数分解
map<int, int> enumpr(int n) {
    map<int, int> V;

    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            V[i]++;
            n /= i;
        }
    }
    if (n > 1)
        V[n]++;
    return V;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a, b;
    cin >> a >> b;
    int t = gcd(a, b);
    map<int, int> mp = enumpr(t);
    cout << mp.size() + 1 << endl;

    return 0;
}


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

using namespace std;

int c()
{
    int n;
    cin >> n;
    vector<vector<int>> a(2, vector<int>(n));
    for(int i = 0; i < 2; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> a[i][j];
        }
    }
    int max_count = 0;
    for(int i = 0; i < n; i++)
    {
        int j = 0;
        int count = 0;
        bool flag = true;
        for(int k = 0; k < n; k++)
        {
            count += a[j][k];
            if(k == i && flag) {
                j = 1;
                k--;
                flag = false;
            }
        }
        max_count = max(count, max_count);
    }
    cout << max_count << endl;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> graph(n); //first 行き先, second コスト
    vector<bool> used(n, false);    //使用済みフラグ
    vector<int> result(n, 0);   //値
    auto add_edge = [&](int from, int to, int cost)
    {
        //fromからtoへ重みcostの辺を追加する
        graph[from].push_back({to, cost});
    };
    auto bfs = [&](int s)
    {
        //sからbfsを行いながら数を決定していく
        queue<int> q;
        q.push(s);
        used[s] = true;
        result[s] = 0;
        while (!q.empty())
        {
            int current = q.front();
            q.pop();
            for(int i = 0; i < graph[current].size(); i++)
            {
                int next = graph[current][i].first;
                if(!used[next])
                {
                    q.push(next);
                    used[next] = true;
                    result[next] = result[current] + graph[current][i].second;
                }else{
                    //値に矛盾がないか確認
                    int temp = result[current] + graph[current][i].second;
                    if(result[next] != temp)
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    };
    for(int i = 0; i < m; i++)
    {
        int l, r, d;
        cin >> l >> r >> d;
        add_edge(l - 1, r - 1, d);
        add_edge(r - 1, l - 1, -d);
    }
    //値を割り振り
    for(int i = 0; i < n; i++)
    {
        if(!used[i])
        {
            if(!bfs(i))
            {
                cout << "No" << endl;
                return 0;
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}


Measured memory_bytes: 9344
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

#define ll long long
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)

char c[4] = {'R','Y','G','B'};

int main() {
  ll H, W, d;
  cin >> H >> W >> d;

  if (d%2) {
    rep(i,0,H) {
      rep(j,0,W) {
        cout << c[2*(i%2)+j%2];
      }
      cout << endl;
    }
  }
  else {
    rep(i,0,H) {
      rep(j,0,W) {
        if ((i+j)%2) {
          ll rr = 500+(i+j+1)/2;
          ll cc = 500+(j-i+1)/2;
          bool br = ((rr%d) >= d/2);
          bool bc = ((cc%d) >= d/2);
          ll ind = 2*br+bc;
          cout << c[ind];
        }
        else {
          ll rr = 500+(i+j)/2;
          ll cc = 500+(j-i)/2;
          bool br = ((rr%d) >= d/2);
          bool bc = ((cc%d) >= d/2);
          ll ind = 2*br+bc;
          cout << c[ind];
        }
      }
      cout << endl;
    }
  }

  return 0;
}


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

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define vi vector<int>
typedef long long ll;

using namespace std;

bool f(vector<ll> v , int k , int a , int b){
	int n = sz(v);
	ll dam = 0;
	priority_queue<ll> pq;
	
	rep(i , n){
		v[i] -= 1ll * k * b;
	}

	ll c = a - b;
	ll tot = 0;
	rep(i , n){
		if(v[i] <= 0)continue;

		ll cur = v[i];
		ll need = (cur + c - 1) / c;
		tot += need;
	}
//	cout << nl;
//	cout << tot << nl;
	return k >= tot;
}

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

	int n;
	int a , b;
	cin >> n >> a >> b;
	vector<ll> h;
	rep(i , n){
		ll x;
		cin >> x;
		h.pb(x);
	//	pq.push(x);
	}
	SORT(h);
	reverse(all(h));
	int l = 0 , r = 1e9 + 1;
	while(r - l > 1){
		ll mid = (r + l) / 2;
		if(f(h , mid , a , b)){
			r = mid;
		}else{
			l = mid;
		}
	}
	
	cout << r;

	return 0;
}

Measured memory_bytes: 1908
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
const int N=2e5+10;


int main(){
    //freopen("a.txt", "r", stdin);
    //freopen("answers.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i=0;i<n;i++)
    	cin>>v[i];
    int ans=0;
    for(int i=1;i<n;i++){
    	if(v[i]==v[i-1]){
    		v[i]=0;
    		ans++;
    	}
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>

using namespace std;

int num[31],x;

int main(){
	for(int i=0;i<28;i++){
		cin>>x;
		num[x]=1;
	}
	for(int i=1;i<31;i++){
		if(num[i]==0) cout<<i<<endl;
	}
	return 0;
}

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

template <class T>
struct Primal_Dual {
  using Pa = pair<T, int>;
  int infinity = (int)(1e9);
  struct edge {
    int to;
    T cap, cost;
    int rev;
  };
  int v;
  vector<vector<edge>> edges;
  vector<T> h;
  vector<T> dist;
  vector<int> prevv, preve;
  Primal_Dual(int vsize = 1) {
    v = vsize;
    edges.resize(v);
    h.resize(v);
    dist.resize(v);
    prevv.resize(v);
    preve.resize(v);
  }
  bool add(int from, int to, T cap, T cost) {
    edges[from].push_back((edge){to, cap, cost, (int)edges[to].size()});
    edges[to].push_back((edge){from, 0, -cost, (int)edges[from].size() - 1});
    return 1;
  }
  T solve(int s, int t, T f) {
    T ans = 0;
    h.assign(v, 0);
    while (f > 0) {
      priority_queue<Pa, vector<Pa>, greater<Pa>> qu;
      dist.assign(v, infinity);
      dist[s] = 0;
      qu.push({0, s});
      while (!qu.empty()) {
        Pa now = qu.top();
        qu.pop();
        int nowv = now.second;
        if (dist[nowv] < now.first) continue;
        for (int i = 0; i < (int)edges[nowv].size(); ++i) {
          edge &e = edges[nowv][i];
          if (e.cap > 0 &&
              dist[e.to] > dist[nowv] + e.cost + h[nowv] - h[e.to]) {
            dist[e.to] = dist[nowv] + e.cost + h[nowv] - h[e.to];
            prevv[e.to] = nowv;
            preve[e.to] = i;
            qu.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == infinity) return -1;
      for (int i = 0; i < v; ++i) h[i] += dist[i];
      T d = f;
      for (int i = t; i != s; i = prevv[i])
        d = min(d, edges[prevv[i]][preve[i]].cap);
      f -= d;
      ans += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        edge &e = edges[prevv[i]][preve[i]];
        e.cap -= d;
        edges[i][e.rev].cap += d;
      }
    }
    return ans;
  }
};

using P = pair<int, int>;

int n, m;
int d[] = {1, 0};
vector<string> s;

int solve();
bool isvalid(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '#';
}

int main() {
  cin >> n >> m;
  s.resize(n);
  for (auto &p : s) cin >> p;
  cout << solve() << endl;
  return 0;
}

int solve() {
  Primal_Dual<int> pd(n * m * 2 + 2);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) pd.add(i * m + j + n * m, n * m * 2 + 1, 1, 0);
  int cnt = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (s[i][j] == 'o') {
        ++cnt;
        pd.add(n * m * 2, i * m + j, 1, 0);
        vector<vector<int>> dist(n, vector<int>(m, n * m * 2));
        dist[i][j] = 0;
        queue<P> qu;
        qu.push(P(i, j));
        while (qu.size()) {
          auto [x, y] = qu.front();
          qu.pop();
          pd.add(i * m + j, x * m + y + n * m, 1, n * m - dist[x][y]);
          for (int k = 0; k < 2; ++k) {
            int nx = x + d[k], ny = y + d[1 ^ k];
            if (isvalid(nx, ny) && dist[nx][ny] > dist[x][y] + 1) {
              dist[nx][ny] = dist[x][y] + 1;
              qu.push(P(nx, ny));
            }
          }
        }
      }
  return n * m * cnt - pd.solve(n * m * 2, n * m * 2 + 1, cnt);
}

Measured memory_bytes: 6820
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vd vector<ld>
#define vvd vector<vector<ld>>
#define ALL(a) begin(a), end(a)
#define RALL(a) rbegin(a), rend(a)
#define SORT(a) stable_sort(ALL(a))
#define RSORT(a) stable_sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNIQUE(a) a.erase(unique(ALL(a)), end(a))
#define lambda(i) [](auto& i)
#define compare(i, j) [](auto& i, auto& j)
#define min_of(a) *min_element(ALL(a))
#define max_of(a) *max_element(ALL(a))
#define sum(a) accumulate(ALL(a), 0)
#define pb push_back
#define mp make_pair
#define SZ(a) ll((a).size())
#define REP(i, n) for (decltype(+n) i = 0; i < (n); i++)
#define RREP(i, n) for (auto i = n - 1; i != static_cast<decltype(i)>(-1); i--)
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define RFOR(i, a, b) for (auto i = b - 1; i != a; i--)
#define EACH(i, a) for (auto&& i: (a))
using namespace std;

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

vi cum(vi V) {
  vi U(SZ(V));
  partial_sum(ALL(V), begin(U));
  return U;
}

void solve() {
  ll n;
  ll cur = 0, ans = 0;

  cin >> n;
  vi v(n);
  REP(i, n) {
    cin >> v[i];
    v[i]--;
  }
  while(true) {
    ll tmp = v[cur];
    if (tmp == -1) {
      cout << -1 << endl;
      return;
    }
    v[cur] = -1;
    cur = tmp;
    ans++;
    if (cur == 1) {
      cout << ans << endl;
      return;
    }
  }
  // REP(i, n) {
  //   cout << v[i];
  // }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(15);
  solve();
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = m; i < n; i++)
#define SORT(v) sort(all(v))
#define REV(v) sort((v).begin(), (v).end(), greater<int>())
#define INF 1000000007
#define fcout cout << fixed << setprecision(15)
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a * b / gcd(a, b);};
int mod(int a,int b){return (a+b-1)/b;};
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){std::fill( (T*)array, (T*)(array+N), val );};
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;};



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

	int a, b;
  cin >> a >> b;
  int n = 1;
  int x = 0;
  while(n < b) n += a - 1, x++;
  cout << x << endl;
  return 0;
}


Measured memory_bytes: 256
from itertools import product
H, W, N = list(map(int, input().split()))
A = [tuple(map(int, input().split())) for _ in range(N)]
count = {}
for x, y in A:
    for xx, yy in product(range(x-1, x+2), range(y-1, y+2)):
        if not(2 <= xx <= H-1 and 2 <= yy <= W-1):
            continue
        count[(xx, yy)] = count.get((xx, yy), 0) + 1
ans = [0] * 10
for k, v in count.items():
    ans[v] += 1
ans[0] = (H - 2) * (W - 2) - sum(ans)
print(*ans, sep="\n")


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

F = []
for _ in range(N):
    f = []
    for i in input().split():
        f.append(1 if i == '1' else 0)
    F.append(f)

P = []
for _ in range(N):
    p = []
    for i in input().split():
        p.append(int(i))
    P.append(p)

ans = -1000000000
for i in range(1, 1024):
    val = 0
    S = []
    for j in range(10):
        S.append(1 if i & 2 ** j else 0)
    for k in range(N):
        n = 0
        for l in range(10):
            n += S[l] * F[k][l]
        val += P[k][n]
    if val > ans:
        ans = val

print(ans)


Measured memory_bytes: 3188
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d;
int main(){
scanf("%c%c%c%*c",&a,&b,&c);
d=700;
if(a=='o')d+=100;
if(b=='o')d+=100;
if(c=='o')d+=100;
printf("%d",d);
	return 0;
}


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

string check(string s){
    for(int i=0;i<3;i++){
        if(s[i]==s[i+1]) return "Bad";
    }
    return "Good";
}

int main(){
    string s;
    cin >> s;
    cout << check(s);
    return 0;
}

Measured memory_bytes: 256
#include<iostream>

using namespace std;

int main()
{
  long long n,a,b;
  cin>>n>>a>>b;
  if(a>b)
    cout<<0<<endl;
  else if(n==1 and a!=b)
    cout<<0<<endl;
  else
    cout<<(n-1)*b+a-((n-1)*a+b)+1<<endl;
}

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

#ifdef LOCAL
#define debug(var) cerr << #var << " = " << var;
#else
#define debug(var)
#endif

void init() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
}

char s[123][123];

void solve() {
	int h, w; scanf("%d%d", &h, &w);
	for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			scanf(" %c", &s[i][j]);
		}
	}
	for (int i = 1; i <= 2*h; ++i) {
		for (int j = 0; j < w; ++j) {
			putchar(s[(i+1)/2-1][j]);
		}
		if (i < 2*h) puts("");
	}
}

int main() {
	init();
	int t = 1; //scanf("%d", &t);
	while (t--) {
		solve();
	}
	return 0;
}


Measured memory_bytes: 256
import java.util.*;
import static java.lang.Math.*;

public class Main {
	
	public static void main(String... args) {
		System.out.println(findSubstring(scanString(), scanInt()));
	}
	
	private static String findSubstring(String s, int K) {
		String last = "";
		TreeSet<String> set = new TreeSet<>();
		for (int i = 0; i < s.length(); ++i) {
			if (!last.isEmpty() && last.charAt(0) < s.charAt(i)) {
				continue;
			}
			int jj = Math.min(s.length(), i + K);
			for (int j = i + 1; j <= jj; ++j) {
				set.add(s.substring(i, j));
				last = trim(K, set);
			}
		}
		return set.last();
	}

	private static String trim(int K, TreeSet<String> set) {
		String last = "";
		while(K < set.size()) {
			last = set.pollLast();
		}
		return last;
	}
	
	private static final Scanner SC = new Scanner(System.in);

	private static int scanInt() {
		return SC.nextInt();
	}

	private static  String scanString() {
		return SC.next();
	}
}


Measured memory_bytes: 25680
import sys, re
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from collections import deque, defaultdict, Counter
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop, heapify
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
c = [LIST() for _ in range(10)]
A = [LIST() for _ in range(H)]

#ワーシャルフロイド法  注意 PyPy非対応!!!!
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

graph = csr_matrix(c) #隣接行列からcsr行列をつくる
c = floyd_warshall(graph, directed=True) #無向の場合directed=True
#dist_matrixの中身はfloatになっているので注意！
	

#for i in range(10):
#	print(c[i])

import numpy as np
#距離
def distance(p1, p2):
	p1 = np.array(p1)
	p2 = np.array(p2) 
	return np.linalg.norm(p1-p2)
	

ans = 0
for i in range(H):
	for j in range(len(A[i])):
		if A[i][j] != -1:
			ans += c[A[i][j]][1]

print(int(ans))



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

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()      //昇順
#define RALL(obj) (obj).rbegin(), (obj).rend()   //降順
#define print(obj) cout << obj << endl
#define scan(obj) cin >> obj

typedef long long ll;

using namespace std;

int main(void){
    string s;
    cin >> s;
    if(s[0] == s[1] || s[1] == s[2] || s[2] == s[3]) print("Bad");
    else print("Good");
}


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

int N, K, H[100000];
int DP[100001];

int main(void){
    cin >> N >> K;
    for(int i=0;i<N;i++)    cin >> H[i];
    
    DP[0] = 0;
    
    for(int i=1;i<N;i++){
        int res = 1000000000;
        for(int j=1;j<=K;j++){
            if( 0 <= i - j ) res = min(res, DP[i-j] + abs(H[i-j] - H[i]));
        }
        DP[i] = res;
    }
    
    cout << DP[N-1] << endl;
}

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

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  // int mx = (int)1e6 + 1;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int mx = *max_element(a.begin(), a.end());
  vector<int> cnt(mx + 1);
  for (int i = 0; i < n; i++) {
    cnt[a[i]]++;
  }
  vector<bool> ok(mx + 1, 1);
  for (int i = 0; i < n; i++) {
    if (cnt[a[i]] != 0) {
      for (int j = a[i] + a[i]; j <= mx; j += a[i]) {
        ok[j] = 0;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[a[i]] == 1 && ok[a[i]]) {
      ans++;
    }
  }
  cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 8208
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main{
  public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String str = in.readLine();
    System.out.println(str.substring(2,3).equals(str.substring(3,4))
                       && str.substring(4,5).equals(str.substring(5,6))?"Yes":"No");
  }
}

Measured memory_bytes: 21076
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  vector<int> height(s.size() + 1);
  map<int, queue<int>> sameHeight;
  int h = 0; 
  height[0] = h;
  sameHeight[h].push(0);
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '\\') {
      --h;
    } else if (s[i] == '/') {
      ++h;
    }
    height[i + 1] = h;
    sameHeight[h].push(i + 1);
  }
  vector<int> res;
  for (int i = 0; i + 1 < height.size(); i++) {
    sameHeight[height[i]].pop();
    if (height[i] > height[i+1] && sameHeight[height[i]].size() > 0) {
      int H = height[i];
      int j = sameHeight[H].front();
      int w = 0;
      for (int k = i + 1; k <= j; k++) {
        w += (H - height[k - 1]) + (H - height[k]);
        if (k != j) {
          sameHeight[height[k]].pop();
        }
      }
      w /= 2;
      res.push_back(w);
      i = j - 1;
    }
  }
  cout << accumulate(res.begin(), res.end(), 0) << endl;
  cout << res.size();
  for (int w: res) {
    cout << " " << w;
  }
  cout << endl;
  return 0;
}



Measured memory_bytes: 17496
#include <iostream>
#include <algorithm>
#include <vector> //動的配列
#include <string>
#include <list> //双方向リスト
#include <map> //連想配列
#include <set> //集合
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <bitset>
#include <numeric>
#include <tuple>



typedef long long ll;


using namespace std;

typedef pair<int, int> P;
#define FOR(i,a,b) for(int i=(int)(a) ; i < (int) (b) ; ++i )
#define rep(i,n) FOR(i,0,n)
#define sz(x) int(x.size())


template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

//小さい順から取り出すヒープ
//priority_queue<ll, vector<ll>, greater<ll> > pque1;




int main(){
    int n;
    cin>>n;
    cout<<(n-1)/2<<endl;
    return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
int a,b,k,s;
int main()
{
	cin>>a>>b>>k;
	for(int i=max(a,b);i>0;i--)
	{
		if(a%i==0&&b%i==0)
		s++;
		if(s==k)
		{
			cout<<i;return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <functional>
using namespace std;

int main() {
  char b;
  cin >> b;
  if (b=='A') cout << 'T' << endl;
  else if (b=='T') cout << 'A' << endl;
  else if (b=='C') cout << 'G' << endl;
  else if (b=='G') cout << 'C' << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
string S;
void dfs(int x)
{
	if(x==0)
		return ;
	if(x%2==0)
	{
		S+="0";
		dfs(x/(-2));
		return ;
	}
	S+="1";
	dfs((x-1)/(-2));
	return ;
}
int main()
{
	int N;
	cin>>N;
	if(!N)
	{
		cout<<N;
		return 0;
	}
	dfs(N);
	reverse(S.begin(),S.end());
	cout<<S;
	return 0;
}

Measured memory_bytes: 256
N=int(input())
c_0=0
c_1=0
c_2=0
c_3=0
for i in range(N):
  s_i=input()
  if s_i=="AC":
    c_0=c_0+1
  elif s_i=="WA":
    c_1=c_1+1
  elif s_i=="TLE":
    c_2=c_2+1
  elif s_i=="RE":
    c_3=c_3+1
print("AC x "+str(c_0))
print("WA x "+str(c_1))
print("TLE x "+str(c_2))
print("RE x "+str(c_3))

Measured memory_bytes: 9164
n = gets.to_i

info = [nil]

(n-1).times do
  info.push(gets.chomp.split(' ').map(&:to_i))
end

time = Array.new(n+1,0)

1.upto n-1 do |index|
  # 待ち時間を足す
  time[index] = info[index][1]

  # 駅の発車間隔に合ってなかったら発車まで待つ
  time[index]+= time[index]%info[index][2]
  unless time[index]%info[index][2] == 0
    time[index]+= info[index][2]-time[index]%info[index][2]
  end

  # 次の駅に移動する
  time[index]+= info[index][0]
  break if index == n-1
  (index+1).upto (n-1) do |j|
    # 次の駅の列車が発車時間に達していなかったらその時間まで待つ
    if time[index] < info[j][1]
      time[index] = info[j][1]
    end

    # 駅の発車間隔に合ってなかったら発車まで待つ
    unless time[index]%info[j][2] == 0
      time[index]+= info[j][2]-time[index]%info[j][2]
    end

    # 次の駅に移動する
    time[index]+= info[j][0]
  end
end

(1..n).each do |i|
  puts time[i]
end

Measured memory_bytes: 1788
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
 
template <class T> using V = vector<T>;
 
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;
}
 
const long long INF = 1LL << 60;
 
using ll = long long;
using db = double;
using st = string;
using ch = char;
using bl = bool;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using vbl = V<bl>;

 
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rFOR(i,a,b) for(ll i=(a);i>=(b);i--)
#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define M(a,b) max(a,b)
#define rM(a,b) min(a,b)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define co(a) cout<<a<<endl;
#define ci(a) cin>>a;
  
 

ll sum(ll n) {
ll m=0;
  while(n){
    m+=n%10;
    n/=10;
  }
  return m;
}
 
 
const int MAX = 510000;
const int MOD = 1000000007;
 
long long fac[MAX], finv[MAX], inv[MAX];
 
void Comuse() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
 
#define comuse Comuse()
 
ll combi(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
 
 
 
ll lcm(ll a,ll b){
  ll n;
  n=a/gcd(a,b)*b;
  return n;
}




 
/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/
 
 
signed main() {
  ll n,h;
    cin>>n>>h;
  vpll A(2*n);
  ll now=0;
  FOR(i,0,n){
    ll a,b;
      cin>>a>>b;
    A[now].fi=a;
    A[now].se=0;
    now++;
    A[now].fi=b;
    A[now].se=1;
    now++;
  }
  SORT(A);
  REV(A);
  now=0;
  ll ans=0;
  while(h>0){
    if(A[now].se==1){
      h-=A[now].fi;
      ans++;
      now++;
    }
    else{
      h-=A[now].fi;
      ans++;
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3328
# include <bits/stdc++.h>
# define sz(x) (int)((x).size())
# define F first
# define S second
# define pb push_back
# define ub upper_bound
# define lb lower_bound
# define all(x) x.begin(), x.end()
# define pqueue priority_queue
# define mset multiset
# define umap unordered_map
# define Speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
# define rep(i,n) for(int i=0;i<n;++i)
# define forn(i,n) for(int i=1;i<=n;++i)
# define lr(i,l,r) for(int i=l;(l>r?i>=r:i<=r);(l>r?--i:++i))

using namespace std;

template<class T> void umin(T &a,T b){a=min(a,b);}
template<class T> void umax(T &a,T b){a=max(a,b);}

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

const int dx[]={1,0,-1,0,-1,-1,1,1},dy[]={0,1,0,-1,-1,1,-1,1}; 
const int mod = 1e9+7;
const ll N = 1e3+4;





void solve(){
	int h,w;
	cin>>h>>w;
	int c[10][10],ans=0;
	for(int i=0;i<=9;++i){
		for(int j=0;j<=9;++j){
			cin>>c[i][j];
		}
	}	
	rep(k,10){
		rep(i,10){
			rep(j,10){
				if(c[i][j]>c[i][k]+c[k][j])c[i][j]=c[i][k]+c[k][j];
			}
		}
	}
	for(int i=1;i<=h;++i){
		for(int j=1;j<=w;++j){
			int x;
			cin>>x;
			if(x!=-1){
				ans+=c[x][1];
			}
		}
	}
	cout<<ans;

}

int main(){
	Speed();
	int T=1; 
	//cin>>T;	
	while(T--)
		solve();
	return 0;
}

Measured memory_bytes: 3640
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    T, A = 1, 1
    for _ in range(N):
        t, a = map(int, input().split())

        n = max((T+t-1)//t, (A+a-1)//a)
        T = t * n
        A = a * n

    return T + A


if __name__ == '__main__':
    print(main())


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int a;
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    priority_queue<int, vector<int>, less<int> > q;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a);
        q.push(a);
    }
    for(int i=0;i<m;i++)
    {
        int tmp = q.top();
        tmp /= 2;
        q.pop();
        q.push(tmp);
    }
    ll ans = 0;
    while(!q.empty())
    {
        ans += q.top();
        q.pop();
    }
    printf("%lld\n",ans);
}


Measured memory_bytes: 892
N = int(input())
M = len(str(N))

def nsin(X,n):
    if(int(X/n)):
        return nsin(int(X/n),n)+str(X%n)
    return str(X%n)

res = 0
for m in range(3,M+1):
    for i in range(3 ** m):
        tri = nsin(i, 3).zfill(m)
        nl = []
        if tri.count('0') > 0 and tri.count('1') > 0 and tri.count('2') > 0:
            for j in range(m):
                if tri[j] == '0':
                    nl.append('3')
                elif tri[j] == '1':
                    nl.append('5')
                else:
                    nl.append('7')
            x = ''.join(nl)
            if int(x) <= N:
                res += 1

print(res)


Measured memory_bytes: 3064
import java.lang.Integer
import java.lang.Math

fun main(args: Array<String>) {
  val INF = 1000000000
  val n = readLine()!!.toInt()
  val a = readLine()!!.split(" ").map { it.toInt() }.toIntArray()
  val b = readLine()!!.split(" ").map { it.toInt() }.toIntArray()
  val m = 1 shl n
  val dp = Array(m) { IntArray(51) { INF } }
  dp[0][0] = 0
  for (k in 0 until m) {
    for (i in 1..50) dp[k][i] = Math.min(dp[k][i],dp[k][i-1])
    val x = Integer.bitCount(k)
    for (i in 0 until n) {
      if (((k shr i) and 1) == 1) continue
      val v = if ((x-i)%2 == 0) a[i] else b[i]
      val j = k or (1 shl i)
      dp[j][v] = Math.min(dp[j][v], dp[k][v]+Integer.bitCount(k shr i))
    }
  }
  val x = dp[m-1].min()!!
  println(if (x == INF) -1 else x)
}

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

using namespace std;

#define F first
#define S second
#define modulo ll (1e9 + 7)

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

void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

const int N = 59, M = 5e3 + 9, OO = 0x3f3f3f3f;
const ll llOO = 0x3f3f3f3f3f3f3f3f;


int main(){
    init();

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

    cout << "NO";

}

Measured memory_bytes: 384
#include <iostream>
#include <cstdio>
#include <vector>
#include <deque>
#include <algorithm>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
 
int T[110];
int v[110];

int hantei(int t, int ve, int i){
    int nv;
    if(t <= T[i]){
        nv = (v[i] + (T[i]-t)); 
    }
    //else if(t > T[i+1]){
    //    nv = (v[i] + (t-T[i+1]));
    //}
    else if(t <= T[i+1]){
        nv = v[i]; 
    }
    else return 1;

    if(nv > ve) return 1;
    else if(nv < ve) return -1;
    else return 0;
}

int main(){
    int N;
    cin >> N;
    T[0] = 0;
    REP(i,N+1){
        if(i == 0) continue;
        int t;
        cin >> t;
        if(i != 0)
            T[i] = 2*t + T[i-1];
    }
    REP(i,N){
        int ve;
        cin >> ve;
        v[i] = 2*ve;
    }

    int ve = 0;
    double S = 0;
    REP(t, T[N]){
        int acc = 1;
        REP(i,N+1){
            int h = hantei(t+1, ve, i);
            acc = min(acc, h);
        }
        int tmp = ve;
        if(acc == 1) ve+=1;
        else if(acc == -1) ve-=1;
        S += tmp + (ve-tmp)*0.5;
        //printf("t:%d\tve:%d\n", t, ve);
        //cout << S/4 << endl;
    }
    //cout << S/4 << endl;
    printf("%lf\n", S/4);
    return 0;
}


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

int main(){
  int r;
  cin>>r;
  if(r<1200){
    cout<<"ABC"<<endl;
    return 0;
  }else if(r<2800){
    cout<<"ARC"<<endl;
    return 0;
  }else{
    cout<<"AGC"<<endl;
    return 0;
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>


using namespace std;

int top = 0, S[1000];

void push(int x){
    S[++top] = x;
}


int pop(){
    top--;
    return S[top+1];
}




int main(){

    int a, b;
    char s[1000];
    
    while (scanf("%s", s) != EOF){ 
        if(s[0] == '+'){
            a = pop();
            b = pop();
            push(a + b); 
        }
        else if(s[0] == '-'){
            b = pop();
            a = pop();
            push(a - b); 
        }
        else if(s[0] == '*'){
            a = pop();
            b = pop();
            push(a * b); 
        }
        else{
            push(atoi(s));
        }
    }
    printf("%d\n", pop());
    return 0;

}


Measured memory_bytes: 3224
//Taxis
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;
static const int MAX = 5000;

int N, K;
int C[MAX + 1], R[MAX + 1], min_cost[MAX + 1];
vector<int> G[MAX + 1], H[MAX + 1];

void solve() {
    priority_queue<pair<int, int> > PQ;
    min_cost[1] = 0;
    PQ.push(make_pair(0, 1));
    while(!PQ.empty()) {
        pair<int, int> p = PQ.top();
        PQ.pop();
        if(p.first > min_cost[p.second]) continue;
        int cost = p.first + C[p.second];
        for(auto u: H[p.second]) {
            if(min_cost[u] > cost) {
                min_cost[u] = cost;
                PQ.push(make_pair(cost, u));
            }
        }
    }
}

void bfs(int u) {
    bool visited[N + 1];
    queue<pair<int, int> > Q;
    for(int i = 0; i < N + 1; i++) visited[i] = false;
    Q.push(make_pair(u, 0));
    visited[u] = true;
    int r = R[u];
    while(!Q.empty()) {
        pair<int, int> q = Q.front();
        Q.pop();
        for(auto j: G[q.first]) {
            if(!visited[j]) {
                visited[j] = true;
                if(q.second < r) {
                    H[u].push_back(j);
                    Q.push(make_pair(j, q.second + 1));
                }
            }
        }
    }
}

int main() {
    cin >> N >> K;
    for(int i = 1; i < N + 1; i++) cin >> C[i] >> R[i];
    int a, b;
    for(int j = 0; j < K; j++) {
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(int i = 0; i < MAX + 1; i++) min_cost[i] = INT_MAX;

    for(int i = 1; i < N; i++) bfs(i);
    solve();

    cout << min_cost[N] << endl;
}


Measured memory_bytes: 70572
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N, M = map(int, input().split())
    s = []
    for _ in range(N):
        a, b = map(int, input().split())
        s.append((a, b))

    p = []
    for _ in range(M):
        c, d = map(int, input().split())
        p.append((c, d))

    for a, b in s:
        ans = 0
        min_dist = INF
        for i, (c, d) in enumerate(p):
            dist = abs(a - c) + abs(b - d)
            if dist < min_dist:
                ans = i + 1
                min_dist = dist
        print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3944
#include <iostream>

using namespace std;

int main(void) {
	int x, y;
	cin >> x >> y;

	
	enum d { right , top , left , bottom };
	int range[4] = { 0,0,0,0 };
	int dCnt = right, colCnt = 1;
	int fib = 1, p_fib = 0, pp_fib = 0;

	while (!(range[left] <= x && x <= range[right] && range[bottom] <= y && y <= range[top])) {
		pp_fib = p_fib;
		p_fib = fib;
		fib = p_fib + pp_fib;

		if (dCnt < 2) range[dCnt] += fib;
		else range[dCnt] -= fib;
		dCnt = (dCnt + 1) % 4;
		colCnt = colCnt % 3 + 1;
	}

	cout << colCnt << endl;

	return 0;
}


Measured memory_bytes: 3116
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for(int i = m; i < n; i++) 
#define mod 1000000007
typedef long long ll;
int main(){
    int N,M;
    cin>>N>>M;
    vector<int>A(N);
    int sum=0;
    rep(i,N){
        cin>>A[i];
        sum+=A[i];
    }
    int count=0;
    double a=(double)1/(4*M);
    rep(i,N){
        if((double)A[i]>=(double)sum*a){
            count++;
            //cout<<sum<<" "<<A[i]<<" "<<(double)sum*a<<count<<endl;
        }
    }
    if(count>=M){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(void){
    int n;
    cin >> n;
    int a[110];
    for(int i = 0;i<n;i++){
        cin >> a[i];
    }
    for(int i = n-1;i >=0;i--){
        if(i != n-1) cout << " ";
        cout << a[i];
    }
   cout << endl;
    
}



Measured memory_bytes: 3072
#include <iostream>

using namespace std;

int main() {
  int x, y;
  cin >> x >> y;
  if ((x + y) % 2 == 0) {
    cout << (x + y) / 2 << endl;
  } else {
    cout << (x + y) / 2 + 1 << endl;
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<string>
#include<stdio.h>
#include<math.h>
#include<unordered_map>
#include<map>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using lst = list<int>;


int main()
{
	ll L, R;

	cin >> L >> R;

	vec use(2019, 0);

	for (int i = 0; i < 2019 && i + L <= R; ++i)
	{

		use[(i + L) % 2019] = 1;

	}
	int ans = 2019;
	for (int i = 0; i < 2019; ++i)
	{
		if (!use[i])continue;

		for (int j = i + 1; j < 2019; ++j)
		{
			if (!use[j])continue;

			ans = min(ans, (i*j) % 2019);
		}
	}

	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
import bisect
for i in range(n):
    for j in range(i+1,n):
        a = l[i]+l[j]
        x = bisect.bisect_left(l,a)
        if x>j:
            ans += max(0,x-j-1)
print(ans)


Measured memory_bytes: 3188