akhauriyash/Code-Regression huggingface dataset:
import java.io.*;
import java.util.*;

public class Main {
    public static PrintWriter out;
    public static FastScanner in;

    private void solve(){
        int[] count = new int[26];
        Arrays.fill(count,Integer.MAX_VALUE);

        int n = in.nextInt();
        for(int i=0;i<n;i++){
            String s = in.nextLine();
            int[] nC = new int[26];
            Arrays.fill(nC,0);
            for(int j=0;j<s.length();j++){
                nC[s.charAt(j)-'a']++;
            }

            for(int j=0;j<26;j++){
                count[j] = Math.min(count[j],nC[j]);
            }
        }

        for(int i=0;i<26;i++){
            char c = (char)('a'+i);
            while(count[i] > 0){
                out.print(c);
                count[i]--;
            }
        }
        out.print("");
    }

    private void runIO() {
        in = new FastScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        solve();
        out.close();
    }
    private static class FastScanner {
        BufferedReader bufferedReader;
        StringTokenizer stringTokenizer;

        private FastScanner() {
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        }

        private String next() {
            if (stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
                try {
                    stringTokenizer = new StringTokenizer(bufferedReader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return stringTokenizer.nextToken();
        }

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

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

        private String nextLine() {
            String ret = "";
            try {
                ret = bufferedReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ret;
        }
    }

    public static void main(String[] args) {
        new Main().runIO();
    }
}


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

int main(){
  int n,m,ans=0;
  cin >> n>>m;
  vector<int> a(n);
  rep(i,n){
    cin >>a[i];
    ans+=a[i];
  }
  double b= ans * 1.0/(4.0*m);
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  rep(i,m){
    if(a[i]<b){
      cout << "No" <<endl;
      return 0;
    }
  }
  cout << "Yes" <<endl;
}

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

int main() {
  int N, K, S;
  cin >> N >> K >> S;
  for (int i=0; i<N; i++) {
    if (i<K) cout << S << " ";
    else if (S<1000000000) cout << S+1 << " ";
    else cout << 1 << endl;
  }
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
#define all(a) (a).begin(),(a).end()
typedef long long ll;
#define rep1(i,n) for(int i=1;i<=int(n);++i)

class modint {
  public:
    using ll = long long;
    ll num;
    static const ll MOD = 2019;
    static vector<modint> factorial;

    modint(): num(0) {};
    modint(ll n_): num((n_ + MOD) % MOD) {}
    modint(const modint &r): num(r.num) {}
    operator ll() const { return (num+MOD)%MOD; }
    friend istream& operator>>(istream& is, modint &r){
        ll num_;
        is >> num_;
        r = num_;
        return is;
    }
    modint operator+(const modint &r) const { return modint(num + r.num); }
    modint operator-(const modint &r) const { return modint(num - r.num); }
    modint operator*(const modint &r) const { return modint(num * r.num); }

    template<typename T>
    modint operator^(const T &r) const {
        if(r == 0) return 1;
        return (((*this)*(*this)) ^ (r/2)) * modint(r&1 ? num : 1);
    }
    modint operator/(const modint &r) const { return num * (r^(MOD-2)); }

    modint operator+=(const modint &r) { return *this = *this + r; }
    modint operator-=(const modint &r) { return *this = *this - r; }
    modint operator*=(const modint &r) { return *this = *this * r; }
    modint operator/=(const modint &r) { return *this = *this / r; }

    template<typename T>
    modint operator^=(const T &r) { return *this = *this ^ r; }

    static modint pow(const modint x, ll r) {
        return x^r;
    }

    static modint fact(int n){
        if((int)factorial.size() <= n) {
            int size = factorial.size();
            factorial.resize(n+1);
            for (int i = size; i <= n; i++) {
                factorial[i] = fact(i-1) * modint(i);
            }
        }
        return factorial[n];
    }

    static modint C(ll n,ll r){
        return n >= r ? fact(n)/(fact(n-r)*fact(r)) : modint(0);
    }
};
vector<modint> modint::factorial = {1};
#define fact(n) modint::fact(n)
#define C(n,r) modint::C(n,r)
#define H(a,b) C(a+b, a)


int main(void){
    string s;
    cin >> s;
    reverse(all(s));
    int n = s.size();
    vector<modint> t(n+1);
    rep1(i,n){
        t[i] = modint::pow(10,i) * modint(s[i-1]-'0') + t[i-1];
    }
    map<int,ll> cnt;
    rep(i,n+1) cnt[t[i]]++;
    ll res = 0;
    for(auto itr : cnt) {
        res += itr.second * (itr.second - 1) / 2;
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 5220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template <class T> inline bool chmin(T &a, T b) {if (a > b){a = b;return true;}return false;}
template <class T> inline bool chmax(T &a, T b) {if (a < b){a = b;return true;}return false;}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin>>n;
    if(n%2==1){
        cout<<0<<endl;
        return 0;
    }
    ll ans = 0;
    ll fi = 5;
    while(n / (fi*2) != 0){
        ans += (n/(fi*2));
        fi *= 5;
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
N,K=map(int,input().split())
MAP=[list(map(int,input().split())) for i in range(N)]

start = 2*N+1
goal = 2*N+2
V=2*N+5

EDGE=[[[0,0] for j in range(V)] for i in range(V)]

# 0 ～ N : 縦
# N+1 ～　2*N : 横
# 2*N+1 : start
# 2*N+2 : goal



for i in range(N):
    EDGE[start][i]=[K,0]
    EDGE[N+i][goal]=[K,0]
    
for i in range(N):
    for j in range(N):
        EDGE[i][N+j]=[1,-MAP[i][j]]
        EDGE[N+j][i]=[0,MAP[i][j]]

EDGE[start][goal]=[float("inf"),0]


BACK=[-1]*V
LA=0

for flow in range(K*N):
    ANS=[float("inf")]*V
    ANS[start]=0

    for rep in range(V):
        flag=0
        for fr in range(V):
            for to in range(V):             
                c,d=EDGE[fr][to]
                if c>0 and ANS[to]>ANS[fr]+d:
                    ANS[to]=ANS[fr]+d
                    flag=1
                    BACK[to]=fr
        if flag==0:
            break

    if ANS[goal]==0:
        break
 
    LA+=ANS[goal]
 
    NOW=goal
    while NOW!=start:
        fr=BACK[NOW]
        EDGE[fr][NOW][0]-=1
        EDGE[NOW][fr][0]+=1

        NOW=fr
 
print(-LA)
ALIST=[["."]*N for i in range(N)]

for i in range(N):
    for j in range(N):
        if EDGE[i][N+j][0]==0:
            ALIST[i][j]="X"

for alist in ALIST:
    print("".join(alist))

Measured memory_bytes: 82008
from heapq import heappush,heappop
n,m = map(int,input().split())
x = []
xa = x.append
for _ in range(n):
  ai,bi = map(int,input().split())
  xa((ai,bi))
x.sort(key=lambda a:a[0])
y = []
count = 0
j = 0
for i in range(1,m+1):
  while x[j][0] == i:
    heappush(y,-x[j][1])
    if j+1 < n:
      j += 1
    else:
      break
  if len(y) != 0:
    count -= heappop(y)
print(count)

  
  

Measured memory_bytes: 77272
#include<bits/stdc++.h>
using namespace std;
#define int long long

#define rep(i,n) for (int i = 0; i < n; i++)
#define repd(i,n) for (int i = n-1; i >= 0; i--)
#define fo(i,a,b) for (int i = a; i < b; i++)
#define fod(i,a,b) for (int i = a; i >= b; i--)
#define umap unordered_map
#define mii map<int,int>
#define pii pair<int,int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define pqmin priority_queue<int,vector<int>,greater<int>>
#define pqmax priority_queue<int>
#define all(v) v.begin(),v.end()
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define w(t) int t; cin>>t; while (t--)
#define ps(x,y) fixed<<setprecision(y)<<x
#define out(x) cout<<x<<endl;
#define print(a, n) rep (i, n) cout<<a[i]<<" "; cout<<endl;
#define clr(a, x) memset(a, x, sizeof a)

const int mod = 1e9+7;
const int mod2 = 998244353;
const int N = 3e5;
const int inf = 1e18;

//=====================================================


template<typename... T>
void read(T&... args) {((cin >> args), ...);}
template<typename... T>
void write(T&&... args) {((cout << args << " "), ...);}
template<typename... T>
void writeline(T&&... args) {((cout << args << " "), ...); cout << endl;}

int add(int a,int b){int res=(a+b)%mod;return (res<0)?res+mod:res;}
int mul(int a,int b){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;}
string binary(int x) {string res = ""; rep(i, 32) res += (1<<i & x)?'1':'0'; return res; }
int in(int x, int y, int z) { if (y > z) swap(y, z); return (x >= y && x <= z);}
struct customCompare {
    bool operator () (pii x, pii y) {
        return x.first > y.second;
    }
};

//=====================================================


int tree[10000000];
int a[300005];

void pointUpdate (int i, int j, int k, int l, int val) {
    if (i > l || j < l) return;
    if (i == j) { tree[k] = a[l] = val; return; }

    int mid = (i+j)/2;
    pointUpdate (i, mid, 2*k, l, val); 
    pointUpdate (mid+1, j, 2*k+1, l, val);
    tree[k] = max(tree[2*k], tree[2*k+1]);
}


int query(int i, int j, int l, int r, int k) {
    if (i > r || j < l) return 0;
    if (i >= l && j <= r) return tree[k];
    int mid = (i+j)/2;
    return max(query (i, mid, l, r, 2*k), query (mid+1, j, l, r, 2*k+1));
}

void solve() {
    int n, k; cin >>n>>k;
    int b[n]; rep(i, n) cin >> b[i];
    int mx = *max_element(b, b+n);

    rep(i, n) {
        int l = max(0LL, b[i]-k);
        int r = min(mx, b[i]+k);

        int q = query(0, mx, l, r, 1);
        pointUpdate(0, mx, 1, b[i], q+1);
    }

    cout << query(0, mx, 1, mx, 1);
}

int32_t main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(0); cout.tie(0);

    solve();

    return 0;                                           
}

Measured memory_bytes: 16540
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
	int N;
	int ia;
	int cost = 0;
	int min_cost = 10000000;
	cin >> N;
	vector<int> a(0);
	int i,j;
	for (i = 0; i < N; i++) {
		cin >> ia;
		a.push_back(ia);
	}

	vector<int>::iterator iter =max_element(a.begin(), a.end());
	size_t index = distance(a.begin(), iter);
	
	vector<int>::iterator iter2 = min_element(a.begin(), a.end());
	size_t index2 = distance(a.begin(), iter2);
	
	for (i = a[index2]; i <= a[index];i++) {
		cost = 0;
		for (j = 0; j < N;j++) {
			cost = cost + (i - a[j])*(i - a[j]);
		}
		if (cost < min_cost) {
			min_cost = cost;
		}

	}

	cout << min_cost << endl;
	return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
#define REP(var, a, b) for (int var = (a); var < (b); var++)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define ALL(c) (c).begin(), (c).end()
#define rALL(c) (c).rbegin(), (c).rend()

int main() {
  //
  int n;
  cin >> n;
  n--;
  vi c(n), s(n), f(n);
  rep(i, n) cin >> c[i] >> s[i] >> f[i];
  rep(i, n) {
    int t = 0;
    REP(j, i, n) {
      if (t < s[j]) {
        t = s[j] + c[j];
        // cout << "-" << endl;
      } else {
        if (t % f[j] == 0) {
          t += c[j];
          // cout << "+" << c[j] << endl;
        } else {
          int tstart = f[j] * (t / f[j] + 1);
          t = tstart + c[j];
          // cout << "s" << endl;
        }
      }
      // cout << " " << t << endl;
    }
    cout << t << endl;
  }
  cout << 0 << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
    using namespace std;
    int main()
    {
         string s;
         cin>>s;
         int flag=0;
         int n = s.length();
         for(int i=0;i<n;i++)
         {
            if(s[i]=='L' and i%2==0)
            {
              flag++;
            }
            if(s[i]=='R' and i%2)
            {
               flag++;
            }
         }
      
       if(flag>0)
       {
         cout<<"No"<<endl;
       }
       else
       {
         cout<<"Yes"<<endl;
       }
    }

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

const int maxn = 1e5 + 10;
const int maxp = 61;
const long long mod = 1e9 + 7;

string s;
int k, n;

vector<string> cnt[30];
vector<string> w;

main()
{
	ios::sync_with_stdio(0); cin.tie(0);
    cin >> s >> k;
    n = s.length();
    for (int i = 0; i < n; ++i)
        for (int j = 1; i + j <= n && j <= 5; ++j)
            cnt[s[i] - 'a'].push_back(s.substr(i, j));
    for (int i= 0; i < 26; ++i)
    {
        sort(cnt[i].begin(), cnt[i].end());
        cnt[i].erase(unique(cnt[i].begin(), cnt[i].end()), cnt[i].end());
        if (k > cnt[i].size())
            k -= cnt[i].size();
        else{
            cout << cnt[i][k - 1] << "\n";
            return 0;
        }
    }
}


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

count_A = 0
count_B = 0
for i in range(N):
    if S[i] == 'c':
        print('No')
    elif S[i] == 'a':
        count_A += 1
        if count_A + count_B <= A + B:
            print('Yes')
        else:
            print('No')
            count_A -= 1
    elif S[i] == 'b':
        count_B += 1
        if count_A + count_B <= A + B and count_B <= B:
            print('Yes')
        else:
            print('No')
            count_B -= 1

Measured memory_bytes: 9252
main = do
  _ <- getLine
  content <- getContents
  let inputs = map (read :: String -> Int) $ lines content
  print $ findMaxProfit 0 0 inputs

findMaxProfit :: Int -> Int -> [Int] -> Int
findMaxProfit m n [] = n - m
findMaxProfit 0 0 (x:xs) = findMaxProfit x 0 xs
findMaxProfit m 0 (x:xs) = findMaxProfit m x xs
findMaxProfit m n (x:xs)
  | m > n && x > n  = max (n - m) (findMaxProfit n x xs)
  | x < m           = max (n - m) (findMaxProfit x 0 xs)
  | x > n           = max (n - m) (findMaxProfit m x xs)
  | otherwise       = max (n - m) (findMaxProfit m n xs)

Measured memory_bytes: 32256
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int N; cin >> N;
  string S; cin >> S;
  int ans=0;
  int count=0;
  for(int i=0;i<N;i++){
    if(S.at(i)=='I'){
      count++;
    }
    if(S.at(i)=='D'){
      count--;
    }
    ans=max(ans,count);
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cstring>
#include<functional>

using namespace std;


int main()
{
	int l, r, d;

	scanf("%d %d %d", &l, &r, &d);

	printf("%d\n", r / d - (l - 1) / d);

	return 0;
}

Measured memory_bytes: 1732
#include<iostream>
#include<algorithm>
using namespace std;
int a[100000];
int main()
{
   int N,y;
   cin>>N;
   for(int i=1;i<=N;i++)
   {
        cin>>y;
        a[y]=i;
        y=0;
   }
   for(int i=1;i<=N;i++){
        cout<<a[i]<<" ";
   }
   cout<<endl;
}

Measured memory_bytes: 1152
#include<bits/stdc++.h>
using namespace std;
int n,l;
int a[200]; 
bool vst[200];
int q[200];
vector<int> p;
int main()
{
	cin>>n;
	l=n;
	for (int i=1;i<=n;i++)
	  cin>>a[i];
	while (l--)
	{
	  int c=0;
	  for (int i=1;i<=n;i++)
	  {
		  if (!vst[i]) q[i]=a[i]-c;
		  else c++;
	  }  
	  bool b=0;
	  for (int i=n;i>=1;i--)
	  {
	  	  if (!vst[i])
	  	  {
	  	  	if (q[i]==1)
	  	  	{
	  	  		vst[i]=1;
	  	  		p.push_back(a[i]);
	  	  		b=1;
	  	  		break;
	  	  	}
	  	  }
	  }
	  if (!b)
	  {
	    puts("-1");
	    return 0;
      }
    }
    for (int i=0;i<p.size();i++)
      cout<<p[i]<<endl;
	return 0;
}


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

int main(){
	int sec;
	cin >> sec;	
	
	int hour = sec / 3600;
	int min = (sec % 3600) / 60;
	sec = (sec % 3600) % 60;

	cout << hour << ":" << min << ":" << sec << endl; 
	return 0;
}

Measured memory_bytes: 1164
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
import math
# from itertools import product, accumulate, combinations, product
import bisect
# import numpy as np
# from copy import deepcopy
#from collections import deque
# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 998244353


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())



def run():
    H, W, D = mapline()
    A = [[-1] * (W+1) for _ in range(H+1)]
    for i in range(1, H+1):
        A[i][1:] = mapline()

    V = [-1] * (H*W + 1)
    for i in range(1, H+1):
        for j in range(1, W+1):
            V[A[i][j]] = (i, j)

    cache = [-1] * (H*W+1)

    Q = intread()
    for _ in range(Q):
        l, r = mapline()
        if cache[r] == -1:
            mostR = r + (H*W - r) // D * D
            mostL = l % D
            if not mostL:
                mostL = D
            ci, cj = V[mostR]
            tmp = 0
            cache[mostR] = 0
            #print(mostL, mostR)
            for v in range(mostR - D, mostL-D, -D):
                #print(v, V[v])
                ni, nj = V[v]
                tmp += abs(ni - ci) + abs(nj - cj)
                cache[v] = tmp
                ci, cj = ni, nj
        #print(cache)
        print(cache[l] - cache[r])


if __name__ == "__main__":
    #print(math.gcd(0, 10))
    run()


Measured memory_bytes: 23284
Mod = 10**9 + 7
L = gets.chomp

non_free, free = [1, 0]
L.chars.each{ |c|
  non_free, free = case c
  when "1"; [non_free*2 % Mod, (non_free + free*3) % Mod]
  when "0"; [non_free % Mod, free*3 % Mod]
  end
}
p (free + non_free) % Mod


Measured memory_bytes: 9468
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n + 1):
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]
n = int(input())
prime = primes(n)
a = [1]*n
for i in range(1,n+1):
    for p in prime:
        while i%p == 0:
            i = i//p
            a[p-1] += 1
ans = 1
for i in a:
    ans *= i
mod = 10**9 + 7
print(ans%mod)

Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn = 250;
using namespace std;

int n,m,a[15][15],va;

void floyd(){
    for(int k = 0 ; k < 10 ; k++){
        for(int i = 0 ; i < 10 ; i++){
            for(int j = 0 ; j < 10 ; j++){
                a[i][j] = min(a[i][j],a[i][k] + a[k][j]);
            }
        }
    }
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        for(int i = 0 ; i < 10 ; i++){
            for(int j = 0 ; j < 10 ; j++){
                scanf("%d",&a[i][j]);
            }
        }
        int sum = 0;
        for(int i = 1 ; i <= n ; i++){
            for(int j = 1 ; j <= m ; j++){
                scanf("%d",&va);
                floyd();
                sum += a[va][1];
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define MAXN 101
#define MAX_SUM_VALUE 100001
#define MOD -1
#define FastCin	ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef long long ll;

const ll INF = 1e18L + 5;

void swap(int *a, int *b){
    int t = *a;
    *a = *b; *b = t;
}

void min_self(ll& a, ll b){
	a = min(a,b);
}

int main(){
	int N, W;
	scanf("%d %d", &N, &W);
	ll sum_values = 0;
	vi weights(MAXN), values(MAXN); 
	for(int i = 0; i < N; i++){
		scanf("%d %d", &weights[i], &values[i]);	
		sum_values += values[i];
	}
	vector<ll> memo(sum_values + 1, INF);
	memo[0] = 0;
	for(int item = 0; item < N; item++){
		for(int already_value = sum_values - values[item]; already_value >= 0; --already_value){
			min_self(memo[already_value + values[item]], memo[already_value] + weights[item]);
		}
	}
	ll ans = 0;
	for(ll i = 0; i <= sum_values; i++)
		if(memo[i] <= (ll)W)
			ans = max(ans, i);
	cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
def main():
    INF = 10 ** 5
    n, ma, mb = map(int, input().split())
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for _ in range(n):
        a, b, c = map(int, input().split())
        for i in range(400 - a, -1, -1):
            dpi = dp[i]
            dpia = dp[i + a]
            for j in range(400 - b, -1, -1):
                if dpia[j + b] > dpi[j] + c:
                    dpia[j + b] = dpi[j] + c
    
    ans = INF
    for i in range(1, 400):
        if ma * i > 400 or mb * i > 400:
            break
        ans = min(ans, dp[ma * i][mb * i])
    if ans >= INF:
        print(-1)
    else:
        print(ans)

main()

Measured memory_bytes: 40432
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lpair;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define rREP(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,n) for(ll i = 0; i < n; i++){ cout << (x[i]) << " ";} cout<<endl;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int a,b;
    cin >> a >> b;
    int x = (a+b) / 2;
    cout << x + (a+b)%2 << endl;
}


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

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









Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define pi 3.14159265358979323846264338327950L
using namespace std;

int main() {
  int N, ans=0, t=0;
  cin >> N;
  vector<int> s(N);
  for (int i=0; i<N; i++) {
    cin >> s[i];
    ans+=s[i];
  }
  if (ans%10==0) {
    sort(s.begin(),s.end());
    for (int i=0; i<N; i++) {
      if (s[i]%10!=0) {
        ans-=s[i];
        break;
      }
      t++;
    }
  }
  if (t==N) {
    ans=0;
  }
  cout << ans << endl;
}

Measured memory_bytes: 3632
#include "bits/stdc++.h"
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define ALLR(X) (X).rbegin(), (X).rend()
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define F first
#define S second

using namespace std;
 
template<class T> void max_self(T & a, const T & b) { if(a < b) a = b; }
template<class T> void min_self(T & a, const T & b) { if(a > b) a = b; }
typedef long long LL;
const int INF = 1e9 + 7;
const double PI = acos(-1.0);
const double EPS = (1e-9);



int main(){
  FAST_IO  
  
  string s, t; cin >> s >> t;
  sort(ALL(s)); sort(ALLR(t));

  if(s < t) cout << "Yes\n";
  else cout << "No\n";

  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int n,id,a,b,root;
int G[25][2];

void pre(){
  stack<int> st;
  st.push(root);
  while(!st.empty()){
    int p=st.top();st.pop();
    if(p==-1)continue;
    cout<<" "<<p;
    st.push(G[p][1]);
    st.push(G[p][0]);
  }
  cout<<endl;
}

void in(){
  stack<P> st;
  st.push(P(root,0));
  while(!st.empty()){
    P k=st.top();st.pop();
    int p=k.first;
    if(p==-1)continue;
    if(k.second==0){
      st.push(P(p,1));
      st.push(P(G[p][0],0));
    }else{
      cout<<" "<<p;
      st.push(P(G[p][1],0));
    }
  }
  cout<<endl;
}

void post(){
  stack<P> st;
  st.push(P(root,0));
  while(!st.empty()){
    P k=st.top();st.pop();
    int p=k.first;
    if(p==-1)continue;
    if(k.second==0){
      st.push(P(p,1));
      st.push(P(G[p][0],0));
    }else if(k.second==1){
      st.push(P(p,2));
      st.push(P(G[p][1],0));
    }else{
      cout<<" "<<p;
    }
  }
  cout<<endl;
}

int main(){
  cin>>n;
  root=n*(n-1)/2;

  for(int i=0;i<n;i++){
    cin>>id>>a>>b;
    G[id][0]=a;
    G[id][1]=b;
    if(a!=-1)root-=a;
    if(b!=-1)root-=b;
  }

  
  cout<<"Preorder"<<endl;
  pre();

  cout<<"Inorder"<<endl;
  in();

  cout<<"Postorder"<<endl;
  post();
  
  return 0;
}

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

int n, k;
int a[100000];

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

signed main() {

	int i;
	cin >> n >> k;
	for (i = 0; i < n; i++) cin >> a[i];
	
	int g = a[0], Max = a[0];
	for (i = 1; i < n; i++) {
		g = gcd(g, a[i]);
		Max = max(Max, a[i]);
	}
	
	if (k <= Max && (Max - k) % g == 0) {
		cout << "POSSIBLE" << endl;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = 405;
const ll base = (ll)1e9+7;
int n, m, a[N], b[N];
ll dp[N][N], pw_sum[N][N];

ll subtract(ll x, ll y) {
	x -= y, x %= base;
	if (x < 0) x += base;
	return x;
}

void add(ll &x, ll y) {
	x += y, x %= base;
	if (x < 0) x += base;
}

ll mul(ll x, ll y) {
	x *= y, x %= base;
	if (x < 0) x += base;
	return x;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
	for (int i = 1; i <= 400; ++i) pw_sum[0][i] = 1;
	for (int i = 1; i <= 400; ++i) {
		for (int j = 1; j <= 400; ++j)
			pw_sum[i][j] = mul(pw_sum[i - 1][j], j);
	}
	for (int i = 0; i <= 400; ++i) {
		for (int j = 1; j <= 400; ++j)
			add(pw_sum[i][j], pw_sum[i][j - 1]);
	}
	dp[0][0] = 1;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j <= m; ++j) if (dp[i][j]) {
			int lo = a[i + 1], hi = b[i + 1];
			for (int k = j; k <= m; ++k) {
				ll tmp1 = subtract(pw_sum[k - j][hi], pw_sum[k - j][lo - 1]);
				ll tmp2 = mul(dp[i][j], tmp1);
				add(dp[i + 1][k], tmp2);
			}
		}
	}
	printf("%lld", dp[n][m]);
	return 0;
}


Measured memory_bytes: 2816
integer w,h,n
integer x,y,a
integer l,r,d,u
read*,w,h,n
l=0;r=w;d=0;u=h
do i=1,n
read*,x,y,a
if (a==1)l=max(l,x)
if (a==2)r=min(r,x)
if (a==3)d=max(d,y)	
if (a==4)u=min(u,y)
end do
print"(I0)",merge(0,(r-l)*(u-d),l>r.or.d>u)
end

Measured memory_bytes: 256
/**
 * @author torith
 * @date 2018-10-24
 */

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main(){

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

    int N;
    cin >> N;

    int up[100] = {};
    int down[100] = {};
    for (int i = 0; i < N; ++i) {
        int t;
        cin >> t;
        if (i == 0) up[i] = t;
        else up[i] += up[i-1] + t;
    }
    int sum = 0;
    for (int i = 0; i < N; ++i) {
        int t;
        cin >> t;
        sum += t;
        if (i != N - 1) down[i+1] = -t + down[i];
    }
    for (int i = 0; i < N; ++i) {
        down[i] += sum;
    }

    int ans = 0;
    for (int i = 0; i < N; ++i) {
        ans = max(ans,up[i]+down[i]);
    }

    cout << ans << "\n";

    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T> bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}

ll N,M;
vector<ll> A;
bool isOK(int happy){
  ll cnt=0;
  for(int i=0;i<N;i++){
    int x=happy-A.at(i);
    cnt+=A.end()-lower_bound(all(A),x);
  }
  if(cnt>=M)  return true;
  else  return false;
}
ll calc(int happy){
  vector<ll> sum(N+1,0);
  for(int i=N-1;i>=0;i--) sum.at(i)=sum.at(i+1)+A.at(i);
  ll ans=0;
  ll cnt=0;
  for(int i=0;i<N;i++){
    int x=happy-A.at(i);
    int tmp=A.end()-lower_bound(all(A),x);
    ans+=tmp*A.at(i)+sum.at(N-tmp);
    cnt+=tmp;
  }
  ans-=(cnt-M)*happy;
  return ans;
}

int main(){
  ios_base::sync_with_stdio(false);cin.tie(0);
  cin >> N >> M;
  A.resize(N);
  for(int i=0;i<N;i++)  cin >> A.at(i);

  sort(all(A));
  int ok=0;
  int ng=220000;
  while(abs(ok-ng)>1){
    int mid=(ok+ng)/2;
    if(isOK(mid))  ok=mid;
    else  ng=mid;
  }
  //cout << ok << endl;
  cout << calc(ok) << endl;

  return 0;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
#define  mod 1000000007
#define  N   10005
#define  inf 1e18
 
int a[N];
vector < int > primes;
 
void foo()
{
        for(int i=2;i<N;i++)
        {
                if(!a[i])
                {
                        primes.push_back(i);
                        for(int j=2*i; j<N; j+=i)
                                a[j] = 1;
                }
        }
}
 
int32_t main()
{
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
 
        int n;
        cin>>n;
 
        vector < int > a(n);
 
        for(int i=0; i<n; i++)
                cin>>a[i];
 
        foo();
 
 
        map < __int128_t , __int128_t > me;
        int flag = 0;
        int ans = 0;
 
        vector < pair < __int128_t , __int128_t > > arr;
 
        for(int i=0;i<n;i++)
        {
                int cur = a[i];
                __int128_t opp = 1;
                __int128_t red = 1;
                for(int j=0;j<primes.size();j++)
                {
                        __int128_t curp = primes[j];
                        int count = 0;
                
                        while(cur%curp==0)
                        {
                                cur/=curp;
                                count++;
                        }
                        count%=3;
                        if(count==1)
                        {
                                opp*=curp*curp;
                                red*=curp;
                        }
                        else if(count==2)
                        {
                                opp*=curp;
                                red*=curp*curp;
                        }
                }
                red*=cur;
                
                if(cur==1)
                {
                        if(opp==1)
                                flag = 1; 
                }
                else
                {
                        int x = sqrt(cur);
                        if(x*x == cur)
                        {
                                opp*= x;
                        }
                        else if(cur <= 1000000)
                        {
                                opp*= cur*cur;
                        }
                }
                
                if(red!=1)
                {
                        me[red]++;
                        arr.push_back({red, opp});
                }
        }
 
        sort(arr.begin(), arr.end());
        arr.erase( unique( arr.begin(), arr.end() ), arr.end() );
 
 
        for(int i=0; i<arr.size();i++)
        {
                ans+= max(me[arr[i].first], me[arr[i].second]);
                me[arr[i].first] =0;
                me[arr[i].second] =0;
        }       
        ans+=flag;
        cout<<ans<<endl;
 
        return 0;
}

Measured memory_bytes: 17132
import decimal

n,k = (int(i) for i in input().split())

total_ans=0
#1
for i in range(1,n+1):
  point=i
  num=0
  ans=1/n
  while point<k:
    point=point*2
    ans=ans/2
  total_ans=total_ans+ans

print(total_ans)

Measured memory_bytes: 5460
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(int i=0;i<(int)n;i++)
ll mod=1e9+7;
#define PI acos(-1)
struct edge{int from, to;};

int main(){
    int n,a,b;
    cin>>n;
    vector<int> P(n),Q(n);
    rep(i,n) cin>>P[i];
    rep(i,n) cin>>Q[i];
    vector<int> array(n);
    array=P;
    sort(array.begin(), array.end());
    int count=0;
    do{
        /*
        for(int i=0;i<array.size();i++){
            cout<<array[i];
            if(i!=array.size()-1) cout<<" ";
        }
        cout<<endl;
        */
       if(array==P) a=count;
       if(array==Q) b=count;
       count++;
    }while(next_permutation(array.begin(), array.end()));

    cout<<abs(a-b)<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};


 
template< unsigned mod >
struct RollingHash {
  vector< unsigned > hashed, power;
 
  inline unsigned mul(unsigned a, unsigned b) const {
    unsigned long long x = (unsigned long long) a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod));
    return m;
  }
 
  RollingHash(const string &s, unsigned base = 10007) {
    int sz = (int) s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);
    power[0] = 1;
    for(int i = 0; i < sz; i++) {
      power[i + 1] = mul(power[i], base);
      hashed[i + 1] = mul(hashed[i], base) + s[i];
      if(hashed[i + 1] >= mod) hashed[i + 1] -= mod;
    }
  }
 
  unsigned get(int l, int r) const {
    unsigned ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
    if(ret >= mod) ret -= mod;
    return ret;
  }
 
  unsigned connect(unsigned h1, int h2, int h2len) const {
    unsigned ret = mul(h1, power[h2len]) + h2;
    if(ret >= mod) ret -= mod;
    return ret;
  }
 
  int LCP(const RollingHash< mod > &b, int l1, int r1, int l2, int r2) {
    int len = min(r1 - l1, r2 - l2);
    int low = -1, high = len + 1;
    while(high - low > 1) {
      int mid = (low + high) / 2;
      if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) low = mid;
      else high = mid;
    }
    return (low);
  }
};
 
using RH1 = RollingHash< 1000000007 >;
using RH2 = RollingHash< 1000000021 >;
 
int main(){
    string S,t;cin >> S >> t;
    string s=S;
    while(s.size()<5*1e6) s=s+s;
    RH1 rhs1(s);
    RH1 rht1(t);
    RH2 rhs2(s);
    RH2 rht2(t);
    int ans = 0;
    bool f=true;
    vector<bool> used(s.size(),false);
    for (int i = 0; i < S.size(); i++) {
        int cnt=0;
        for (int j = i; j < s.size(); j+=t.size()) {
            if(used[j]) break;
            used[j]=true;
            cnt++;
            if(rht1.get(0,t.size())==rhs1.get(j,j+t.size())||rht2.get(0,t.size())==rhs2.get(j,j+t.size())) ans=max(cnt,ans);
            else break;
            if(j>1e6&&i<S.length()){
                cout << -1 << endl;
                return 0;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 148996
def main():
    N, M = map(int, input().split())
    E = [[] for _ in range(N)]
    D = [0] * (N)
    for _ in range(M):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        E[x].append(y)
        # Keeps number of input destinations for each output distination.
        D[y] += 1

    # topological sort
    Q = [i for i in range(N) if D[i] == 0]
    L = []
    while Q:
        s = Q.pop()
        L.append(s)
        for r in E[s]:
            D[r] -= 1
            if D[r] == 0:
                Q.append(r)

    dp = [0] * N
    for v in L:
        for k in E[v]:
            dp[k] = max(dp[k], dp[v] + 1)
    print(max(dp))


main()


Measured memory_bytes: 65824
import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String []args) throws IOException {
    	FastScanner in  = new FastScanner(System.in);
    	PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false); 
    	solve(in, out);
    	in.close();
    	out.close();
    }
    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    static class FastScanner{
	BufferedReader reader;
	StringTokenizer st;
	FastScanner(InputStream stream){reader=new BufferedReader(new InputStreamReader(stream));st=null;}	
	String next(){while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}		    
	     st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
	String nextLine()  {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}	    	  	
	int    nextInt()   {return Integer.parseInt(next());}
	long   nextLong()  {return Long.parseLong(next());}		
	double nextDouble(){return Double.parseDouble(next());}
	char   nextChar()  {return next().charAt(0);}
	int[]  nextIntArray(int n)         {int[] arr= new int[n];   int i=0;while(i<n){arr[i++]=nextInt();}  return arr;}
	long[] nextLongArray(int n)        {long[]arr= new long[n];  int i=0;while(i<n){arr[i++]=nextLong();} return arr;}	
	int[]  nextIntArrayOneBased(int n) {int[] arr= new int[n+1]; int i=1;while(i<=n){arr[i++]=nextInt();} return arr;}	    	
	long[] nextLongArrayOneBased(int n){long[]arr= new long[n+1];int i=1;while(i<=n){arr[i++]=nextLong();}return arr;}	    	
	void   close(){try{reader.close();}catch(IOException e){e.printStackTrace();}}				
    }
    
    /********* SOLUTION STARTS HERE ************/
    
    private static void solve(FastScanner in, PrintWriter out){
        
        int T = 1;//in.nextInt();
        for(int t = 1; t <= T; t++)
        {
            long a = in.nextLong();
            long b = in.nextLong();
            long c = (a*b)/(gcd(a,b));
            out.write(c+"\n");
            
        }
    }
    
    /************* SOLUTION ENDS HERE **********/
}

Measured memory_bytes: 23124
/*
  Contest 081
  B - Shift only
  Rakesh Kumar --> 14/09/2020
 */

#include <bits/stdc++.h>

inline bool all_even(std::vector<int>& v) {
    for (std::size_t i = 0; i < v.size(); ++i) {
        if ((v[i] & 1) == 1)
            return false;
        v[i] >>= 1;
    }
    return true;
}

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

    int n = 0;
    std::cin >> n;
    std::vector<int> v;
    while (n--) {
        int e = 0;
        std::cin >> e;
        v.emplace_back(e);
    }

    int count = 0;
    while (all_even(v)) ++count;
    std::cout << count << std::endl;

    return 0;
}


Measured memory_bytes: 3624
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<type_traits>
#include<math.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

#define mod 1000000007

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

int N,sum=0,ans=0;

int main(){
    cin>>N;
    int p[N];
    rep(i,N){
        cin>>p[i];
        if((i+1)==p[i]){
            sum+=1;
        }
        else{
            if(sum==1){
                ans+=1;
            }
            else if(sum>1){
                ans+=sum/2+sum%2;
            }
            sum=0;
        }
    }
    if(sum==1){
        ans+=1;
    }
    else if(sum>1){
        ans+=sum/2+sum%2;
    }
    out(ans);
}

Measured memory_bytes: 640
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5,mod=1e9+7;;
int f[N],s[N],n,tmpx[N],l[N],r[N],w[N];
struct Node{int x,v;}a[N];
int cmp1(const Node &a,const Node &b){return a.v<b.v;}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].v),tmpx[i]=a[i].x;
	sort(tmpx+1,tmpx+n+1);
	for(int i=1;i<=n;i++)a[i].x=lower_bound(tmpx+1,tmpx+n+1,a[i].x)-tmpx;
	sort(a+1,a+n+1,cmp1);
	for(int i=1;i<=n;i++)w[a[i].x]=i;
	for(int i=1;i<=n;i++)r[i]=max(r[i-1],w[i]);
	l[n+1]=n+1;for(int i=n;i>=1;i--)l[i]=min(l[i+1],w[i]);
	s[0]=1;
	for(int i=1;i<=n;i++)
		f[i]=(s[i-1]-(l[i]==1?0:s[lower_bound(r+1,r+i,l[i]-1)-r-1]))%mod,s[i]=(s[i-1]+f[i])%mod;
	cout<<((s[n]-s[lower_bound(r+1,r+n+1,n)-r-1])%mod+mod)%mod;
}

Measured memory_bytes: 14208
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#include <tuple>

using namespace std;


typedef pair<int, int> Location;
typedef queue<Location> LocationQueue;

#define N 1000
char field[N][N];
int levels[N][N] = {0};
bool visited[N][N] = {false};

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

int solve(int h, int w, LocationQueue &que) {
  /*
  cout << h << " " <<  w << " " << endl;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      cout << field[i][j];
    }
    cout << endl;
  }
  */
  Location loc;
  int i0, j0, i, j, level;
  int max_level = 0;

  while(que.size()){
    loc = que.front(); que.pop();
    i0 = loc.first; j0 = loc.second;
    level = levels[i0][j0];

    if (i0 > 0){
      i = i0 - 1;
      j = j0;
      if(!visited[i][j]
          && (field[i][j] == '.'))
      {
        field[i][j] = '#';
        visited[i][j] = true;
        que.push(make_pair(i,j));
        levels[i][j] = level + 1;
        max_level = max(max_level, level+1);
      }
    }
    if (i0 < h - 1){
      i = i0 + 1;
      j = j0;
      if(!visited[i][j]
          && (field[i][j] == '.'))
      {
        field[i][j] = '#';
        visited[i][j] = true;
        que.push(make_pair(i,j));
        levels[i][j] = level + 1;
        max_level = max(max_level, level+1);
      }
    }
    if (j0 > 0){
      i = i0;
      j = j0 - 1;
      if(!visited[i][j]
          && (field[i][j] == '.'))
      {
        field[i][j] = '#';
        visited[i][j] = true;
        que.push(make_pair(i,j));
        levels[i][j] = level + 1;
        max_level = max(max_level, level+1);
      }
    }
    if (j0 < w - 1){
      i = i0;
      j = j0 + 1;
      if(!visited[i][j]
          && (field[i][j] == '.'))
      {
        field[i][j] = '#';
        visited[i][j] = true;
        que.push(make_pair(i,j));
        levels[i][j] = level + 1;
        max_level = max(max_level, level+1);
      }
    }

//    for(int n = 0; n < 4; n++){
//        i = i0 + dx[n];
//        j = j0 + dy[n];
//
//        if (i >= 0 && i <= h - 1 && j >= 0 && j <= w -1 &&
//            !visited[i][j]
//            && (field[i][j] == '.'))
//        {
//          field[i][j] = '#';
//          visited[i][j] = true;
//          que.push(make_pair(i,j));
//          levels[i][j] = level + 1;
//          max_level = max(max_level, level+1);
//        }
//    }
  }

  return max_level;
}

int main(int argc, char const *argv[]) {
  int h, w;
  char c;
  LocationQueue que;

  cin >> h;
  cin >> w;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c;
      field[i][j] = c;
      if (c == '#') {
        que.push(make_pair(i, j));
        visited[i][j] = true;
      }
    }
  }

  cout << solve(h, w, que);

  return 0;
}


Measured memory_bytes: 11008
#include "bits/stdc++.h"
#include<random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 1e9 + 7, INF = 1e16;

lint H, W, K;
char char_map[10][1000];
int main() {
	cin >> H >> W >> K;
	REP(i, H) {
		REP(j, W) {
			cin >> char_map[i][j];
		}
	}
	lint minv = INF;
	REP(i, 1 << (H - 1)) {
		lint curr_ans = 0, idx = 0;
		lint cnt[10] = { 0 };
		set<lint> st[10];
		st[idx].insert(0);
		REP(j, H - 1) {
			if ((i >> j) % 2 == 1) {
				idx++;
				curr_ans++;
			}
			st[idx].insert(j + 1);
		}
		idx++;
		bool cant = false;
		REP(j, W) {
			lint curr_pls[10] = {0};
			REP(k, idx) {
				for (auto itr : st[k]) {
					if(char_map[itr][j] == '1') curr_pls[k]++;
				}
			}
			bool flag = false;
			REP(k, idx) {
				if (cnt[k] + curr_pls[k] > K) {
					flag = true;
				}
			}
			if (flag) {
				curr_ans++;
				REP(k, idx) {
					if (cnt[k] > K) cant = true;
					if (curr_pls[k] > K) flag = true;
					cnt[k] = curr_pls[k];
				}
			}
			else {
				REP(k, idx) {
					cnt[k] += curr_pls[k];
				}
			}
		}
		if (cant) continue;
		chmin(minv, curr_ans);
	}
	cout << minv << endl;
}


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

int main(void) {
    int n;
    char s[10100];
    scanf("%d%s", &n, s);
    for(int i = 0; s[i] != '\0'; i++) {
        s[i] += n;
        if(s[i] > 'Z') {
            s[i] -= 26;
        }
    }
    printf("%s\n", s);
    return 0;
}


Measured memory_bytes: 128
import sys
from heapq import heappush, heappop


N = int(input())
Px = []
Py = []
for k, s in enumerate(sys.stdin.readlines()):
    x, y = map(int, s.split())
    Px.append((x, k))
    Py.append((y, k))

edge = [[] for _ in range(N)]

Px.sort()
for i in range(N - 1):
    x0, k0 = Px[i]
    x1, k1 = Px[i + 1]
    d = abs(x0 - x1)
    edge[k0].append((k1, d))
    edge[k1].append((k0, d))

Py.sort()
for i in range(N - 1):
    y0, k0 = Py[i]
    y1, k1 = Py[i + 1]
    d = abs(y0 - y1)
    edge[k0].append((k1, d))
    edge[k1].append((k0, d))

q = []
used = set([0])
for np, nd in edge[0]:
    heappush(q, (nd, np))

cnt = 0
while len(used) < N:
    d, p = heappop(q)
    if p in used:
        continue
    else:
        cnt += d
        used.add(p)
        for np, nd in edge[p]:
            heappush(q, (nd, np))

print(cnt)


Measured memory_bytes: 88844
n = int(input())
p = []
for i in range(n):
    a , b = map(int,input().split())
    p.append([a,b])
ans = 0
for i in reversed(range(n)):
    if (p[i][0] + ans) % p[i][1] != 0:
        ans += p[i][1] - ((p[i][0] + ans)%p[i][1])
print(ans)

Measured memory_bytes: 19620