akhauriyash/Code-Regression huggingface dataset:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795


int bit_count(ll v)
{
	int ret=0;

	for(;v>0;)
	{
		ret++;
		v&=v-1;
	}

	return ret;
}


int msb(ll v)
{
	int ret=0;
	if(v & 0xFFFFFFFF00000000LL) v>>=32,ret+=32;
	if(v & 0xFFFF0000LL) v>>=16,ret+=16;
	if(v & 0xFF00) v>>=8,ret+=8;
	if(v & 0xF0) v>>=4,ret+=4;
	if(v & 0xC) v>>=2,ret+=2;
	if(v & 0x2) v>>=1,ret+=1;
	return ret;
}


template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}


template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}


ll solve_i(map<ll,ll>&dp, ll n, ll a, ll b, ll c, ll d)
{
	if(n==0) return 0;
	if(n==1) return d;
	if(n==2) return min(d*2, d+a);

	if(dp[n]>0) return dp[n];

	ll ans=d*bit_count(n)+a*msb(n);
	if(ans/n > d) min_u(ans, n*d);

	for(auto x : vector<pii> {{2,a},{3,b},{5,c}})
	{
		int r, cost;
		tie(r, cost)=x;
		if(n%r==0)
		{
			min_u(ans, cost+solve_i(dp, n/r, a, b, c, d));
		}
		else
		{
			ll updown=n-n/r*r;
			min_u(ans, cost+d*(updown)+solve_i(dp, n/r, a, b, c, d));
			min_u(ans, cost+d*(r-updown)+solve_i(dp, n/r+1, a, b, c, d));
		}
	}
	dp[n]=ans;

//printf("dp[%lld]=%lld\n", n, ans);

	return ans;
}

ll solve(ll n, ll a, ll b, ll c, ll d)
{
	map<ll,ll> dp;
	return solve_i(dp, n, a, b, c, d);
}


int main(void)
{
	int t;
	while(scanf("%d", &t)==1)
	{
		ll n;
		int a, b, c, d;
		for(int i=0;i<t;i++)
		{
			scanf("%lld%d%d%d%d", &n, &a, &b, &c, &d);
			printf("%lld\n", solve(n, a, b, c, d));
		}
	}
	return 0;
}


Measured memory_bytes: 4076
from itertools import product

H, W = map(int, input().split())
table = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        count = 0
        if table[i][j] == '#':
            continue
        for y, x in product((-1, 0, 1), repeat=2):
            if not (0 <= i + y < H and 0 <= j + x < W):
                continue
            if table[i + y][j + x] == '#':
                count += 1
        table[i][j] = str(count)

for row in table:
    print(''.join(row))

Measured memory_bytes: 3188
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n=sc.nextLong();
        long a=sc.nextLong();
        long b=sc.nextLong();
        if((a-b)%2==0){
            System.out.println(Math.abs(a-b)/2);
        }else{
            System.out.println(Math.min((a+b-1)/2,(n*2-a-b+1)/2));
        }

    }

}


Measured memory_bytes: 23124
#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int a[1000005];
int t[1000005];
int main()
{
	int n;
	cin>>n>>s;
	for(int i=0;s[i];i++)
	{
		if(s[i]=='o')
			t[i+1]=0;
		else
			t[i+1]=1;
	}
	t[n+1]=t[1];
	for(int i=0;i<8;i++)
	{
		a[0]=i&1;
		a[1]=(i>>1)&1;
		a[2]=(i>>2)&1;
		for(int j=2;j<=n+1;j++)
			a[j+1]=a[j-2]^t[j];
		if(a[0]^a[n]^a[1]^a[n+1]||a[1]^a[n+1]^a[2]^a[n+2])
			continue;
		for(int j=1;j<=n;j++)
		{
			if(a[j]^a[j-1])
				cout<<'W';
			else
				cout<<'S';
		}
		return 0;
	}
	cout<<"-1";

	return 0;
}

Measured memory_bytes: 6912
#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
#define EPS (1e-10)
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

signed main(){
	string s; cin>>s;
	rep(i,s.size()){
		if(s[i]==',') cout<<' ';
		else cout<<s[i];
	}
	cout<<endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
//#include <bits/stdc++.h>
// a&1 check odd or not and a>>=1 halfs a
// for(int& i : vector) cin>>i input
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define pb(x) push_back(x)
#define F first
#define S second
using namespace std;

void solve(){
	double pi=3.14159;
	double rad;
	cin>>rad;
	cout<<2*rad*pi<<endl;


}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t=1;
	//~ cin>>t;
	while(t--){
		solve();
	}
	return 0;
}



Measured memory_bytes: 3776
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main (void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> a(n); rep (i, n) cin >> a[i];
    map<int, ll> m;
    rep (i, n) m[a[i]]++;
    ll tmp = 0;
    for (auto p : m) tmp += p.second * (p.second - 1LL) / 2LL;
    rep (i, n) cout << tmp - (m[a[i]] - 1LL) << "\n";
    return 0;
}

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

int main() {
	string s;
	cin >> s;
	int n = s.size();
	if (s[n - 1] == '1' || s[n - 2] == '0' || s[0] == '0') { // 不可能
		cout << -1;
		return 0;
	}
	for (int i = 0;i < n >> 1;++ i) {
		if (s[i] != s[n - i - 2]) {
			cout << -1;
			return 0;
		}
	}
	int i;
	for (i = 0;i < (n - 1) >> 1;++ i) {
		cout << 1 << " " << i + 2 << endl;
	}
	
	int lastConnect = 1;
	for (;s[i] == '1';++ i) {
		cout << lastConnect << " " << i + 2 << endl;
		lastConnect = i + 2;
	}
	for (;i < n - 1;++ i) {
		cout << lastConnect << " " << i + 2 << endl;
		if (s[i] == '1') lastConnect = i + 2;
	}
	
	return 0;
}
/*
31536000のコメント解説欄
ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり
また、コンテスト後に https://31536000.hatenablog.com/ で解説していると思うので、良かったら読んでねー


*/

Measured memory_bytes: 1408
#include<stdio.h>

int main(){
   int n, m;
   scanf("%d %d", &n, &m);
   int a[m];
   int days = 0;
   for(int i=0; i<m; i++){
      scanf("%d", &a[i]);
      days += a[i];
   }

   if((n - days) >= 0){
      printf("%d", n-days);
   } else {
      printf("%d", -1);
   }

   return 0;
}


Measured memory_bytes: 1720
#include<iostream>
#include<vector>
#include<bitset>

typedef long long ll;

using namespace std;
template <class T>
using mtx = std::vector<std::vector<T>>;

struct cum_sum {
	vector<ll> ar;
	vector<ll> sum;
	int N;
	cum_sum() = default;
	cum_sum(int _N) {
		N = _N;
		ar.resize(N), sum.resize(N + 1);
	}
	cum_sum(vector<ll>& _ar) {
		ar = _ar;
	}

	ll& operator[](int _idx) {
		return ar[_idx];
	}

	void setup() {
		for (int i = 0; i < N; i++) {
			sum[i + 1] = sum[i] + ar[i];
		}
	}
	ll getval(int l, int r) {
		//[l, r)
		ll ret = sum[r] - sum[l];
		return ret;
	}
};

struct cum_sum_lessmemory {
	vector<int> sum;
	int N;
	cum_sum_lessmemory() = default;
	cum_sum_lessmemory(int _N) {
		N = _N;
		sum.resize(N + 1);
	}
	
	void setup(bitset<5000 + 10>& _ar) {
		for (int i = 0; i < N; i++) {
			sum[i + 1] = sum[i] + _ar[i];
		}
	}
	ll getval(int l, int r) {
		//[l, r)
		ll ret = sum[r] - sum[l];
		return ret;
	}
};

int N, K;
vector<ll> a;
bitset<5000 + 10> dpl, dpr;//0-idx

int main() {
	cin >> N >> K;
	a.resize(N);
	for (int i = 0; i < N; i++)cin >> a[i];

	vector<cum_sum_lessmemory> left(N + 10, cum_sum_lessmemory(K + 10)), 
		right(N + 10, cum_sum_lessmemory(K + 10));

	dpl = 1, dpr = 1;
	left[0].setup(dpl), right[0].setup(dpr);
	
	for (int i = 0; i < N; i++) {
		if (a[i] < K) {
			dpl |= (dpl << a[i]);
		}
		left[i + 1].setup(dpl);
	}
	for (int i = N; i > 0; i--) {
		dpr |= (dpr << a[i - 1]);
		right[i - 1].setup(dpr);
	}

	int ans = 0;
	for (int i = 0; i < N; i++) {
		//i番目を除外する
		if (a[i] >= K)continue;
		bool f = true;
		for (int j = 0; j < K; j++) {
			if (left[i].getval(j, j + 1) == 0)continue;
			if (j >= K - a[i]) {
				f = false;
				break;
			}
			if (right[i + 1].getval(K - a[i] - j, K - j)) {
				f = false;
				break;
			}
		}
		if (f)ans++;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 196992
#include<iostream>
#include<string>
#include <set>

using namespace std;

int main() {

	set<string> sset;
	string s;
	char c = '0';

	int n;
	cin >> n;
	while (n--) {
		cin >> s;
		if (sset.count(s) != 0 || (c != '0' && c != s.at(0))) {
			cout << "No";
			return 0;
		}
		sset.insert(s);
		c = s.at(s.length() - 1);
	}
	cout << "Yes";
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define LL long long
#define REP(i, n) for(int i=0;i<(n);++i)
#define REPA(i, n) for(int i=1;i<(n);++i)
#define lp(i, n) for(int i=0;i<(n);++i)
#define PII pair<int, int>
#define PLL pair<long long, long long>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define DEKAI ((int)1e9+7)
#define ALL(x) (x).begin(), (x).end()
#define BIT(x) (1<<(x))
#define foreach(i, n) for(auto &i:(n))
using namespace std;
vector<vector<int>> ans;
#define NULI {{-1}}


void read(vector<vector<int>> l){
  if(l.empty()) return;
  int ri=1000,yi=1000,rit;
  lp(i,l.size()){
    if(ri>l[i][0]){
      ri=l[i][0];
      yi=l[i][1];
      rit=i;
    }
    else if(ri==l[i][0]){
      if(yi>l[i][1]){
      ri=l[i][0];
      yi=l[i][1];
      rit=i;
      }
    }
  }
  ans.push_back(l[rit]);
  int left=l[rit][2];
  int under=l[rit][3];
  l.erase(l.begin()+rit);
  int findtoright=1000;
  if(l.empty()) return;
  //cout<<"    "<<under<<endl;
  lp(i,l.size()){
    if(under>l[i][1]&&under<l[i][3]){
      //cout<<" "<<l[i][0]<<endl;
      findtoright=min(l[i][0],findtoright);
    }
  }
  //cout<<l.size()<<endl;
  //cout<<findtoright<<endl;
  vector<vector<int>> next;
  lp(i,l.size()){
    if(l[i][2]<=findtoright){
      if(l[i][3]<=under){
	next.push_back(l[i]);
	l.erase(l.begin()+i);
	i--;
      }
    }
  }
  if(!next.empty())
    read(next);
  if(!l.empty())
    read(l);
}

int main(){
  int check=0;
  while(1){
    int n;
    cin>>n;
    if(n==0) break;
    if(check==1)cout<<endl;
    check=1;
    vector<vector<int>> l;
    lp(i,n){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      if(b>d) swap(b,d);
      if(a>c) swap(a,c);
      l.push_back({a,b,c,d});
    }
    ans.clear();
    read(l);
    lp(i,n){
      lp(j,n){
	if(ans[j]==l[i]){
	  cout<<j+1<<endl;
	  break;
	}
      }
    }
  }
  return 0;
}



Measured memory_bytes: 3024
#include <bits/stdc++.h>
using namespace std;
string comp(string a,string b){
	if(a.size()>b.size()){
		return a;
	}else if(a.size()<b.size()){
		return b;
	}else{
		int c,d;
		for(int i=0;i<a.size();i++){
			c=a.at(i)-'0';
			d=b.at(i)-'0';
			if(c>d){
				return a;
			}else if(c<d){
				return b;
			}
		}
	}return a;
}
int main(){
	int N,M;cin>>N>>M;
	vector<int> A_int(M);vector<char> A(M);
	int cost[9]={2,5,5,4,5,6,3,7,6};char cast[10]={'0','1','2','3','4','5','6','7','8','9'};
	for(int i=0;i<M;i++){
		cin>>A_int[i];
	}sort(A_int.begin(),A_int.end());
	for(int j=0;j<M;j++){
		A[j]=cast[A_int[j]];
	}string dp[N+1][M];
    for(int i=0;i<=N;i++){
		for(int j=M-1;j>=0;j--){
			int temp=cost[A_int[j]-1];
			if(i>temp){
				int a=i-temp;
				string b="0";
				for(int k=M-1;k>=j;k--){
					b=comp(b,dp[a][k]);
				}if(b!="0"){
                    dp[i][j]=b+A[j];
                }else{
                    dp[i][j]="0";
                }
			}else if(i==temp){
				dp[i][j]=A[j];
			}else{
                dp[i][j]="0";
            }
		}
	}string result="0";
	for(int i=0;i<M;i++){
		result=comp(result,dp[N][i]);
	}cout<<result<<endl;
    /*for(int i=0;i<=N;i++){
        for(int j=0;j<M;j++){
            cout<<dp[i][j]<<" ";
        }cout<<endl;
    }*/
}

Measured memory_bytes: 236672
fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();

    let mut ws = s.trim().split_whitespace();
    let a: f64 = ws.next().unwrap().parse().unwrap();
    let b: f64 = ws.next().unwrap().parse().unwrap();
    let x: f64 = ws.next().unwrap().parse().unwrap();

    if x <= 0.5*a*a*b {
        println!("{}", 90.0 - (2.0 * x / (a*b*b)).atan() * 180.0 / std::f64::consts::PI );
    } else {
        println!("{}", ((2.0*a*a*b - 2.0*x) / (a*a*a)).atan() * 180.0 / std::f64::consts::PI );
    }
}

Measured memory_bytes: 4352
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<ll, int> P;
#define MOD (1000000007ll)
#define l_ength size
#define EPS (1e-10)

int main(void){
	int n,i;
	ll a,sum=0ll;
	cin >> n;
	for(i=0; i<n; ++i){
		cin >> a;
		sum += a;
	}
	cout << ((sum%2)?"NO":"YES") << endl;
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <utility>

using namespace std;

template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;

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

struct Puzzle {
    vector<size_t> board;
    size_t cost, mdistance, gap;
    Puzzle(): board(16), cost(0), mdistance(0) {}
    Puzzle(vector<size_t> &b): board(b), cost(0), mdistance(0) {}
    bool operator<(const Puzzle &rhs) const {
        for (size_t i=0; i<16; ++i)
            if (board[i] != rhs.board[i]) 
                return board[i] < rhs.board[i];

        return false;
    }
};

size_t calc_mdistance(Puzzle p, const vector<vector<size_t>> &dtable) {
    size_t mdistance=0;
    for (size_t i=0; i<16; ++i)
        if (p.board[i] < 16)
            mdistance += dtable[i][p.board[i]-1];

    return mdistance;
}

size_t encode(vector<size_t> d) {
    size_t cur=0, enc=0;
    for (size_t i=0; i<15; ++i)
        if (d[i] == 16) {
            d[i] = 0;
            break;
        }

    for (size_t i=0; i<15; ++i) {
        cur = d[i];
        for (size_t j=i+1; j<16; ++j)
            if (cur < d[j])
                --d[j];

        (enc += d[i]) *= 15-i;
    }

    return enc;
}

struct State {
    Puzzle puzzle;
    size_t hcost;
    State() {}
    State(Puzzle &p): puzzle(p) {}
    bool operator<(const State &rhs) const {
        return hcost > rhs.hcost;
    }
};

size_t astar(Puzzle p, const vector<vector<size_t>> &dtable) {
    p.mdistance = calc_mdistance(p, dtable);
    p.cost = 0;
    State init(p);
    init.hcost = p.mdistance;

    set<size_t> visited;
    gp_queue<State> q; q.push(init);
    while (!q.empty()) {
        State s=q.top(); q.pop();
        Puzzle u=s.puzzle;

        if (!u.mdistance) return u.cost;

        visited.insert(encode(u.board));
        size_t sx=u.gap>>2, sy=u.gap&3;
        for (size_t r=0; r<4; ++r) {
            size_t tx=sx+dx[r], ty=sy+dy[r];
            if (!(0<=tx && tx<4 && 0<=ty && ty<4)) continue;

            Puzzle v=u;
            v.gap = tx<<2|ty;
            v.mdistance -= dtable[tx<<2|ty][v.board[tx<<2|ty]-1];
            v.mdistance += dtable[sx<<2|sy][v.board[tx<<2|ty]-1];
            swap(v.board[sx<<2|sy], v.board[tx<<2|ty]);

            if (!visited.count(encode(v.board))) {
                ++v.cost;
                State nstate(v);
                nstate.hcost = v.cost + v.mdistance;
                q.push(nstate);
            }
        }
    }
    return -1;
}

int main() {
    vector<vector<size_t>> dtable(16, vector<size_t>(16));
    for (size_t i=0; i<16; ++i)
        for (size_t j=0; j<16; ++j)
            dtable[i][j] = abs(int(i/4-j/4)) + abs(int(i%4-j%4));

    Puzzle in;
    for (size_t i=0; i<16; ++i) {
        scanf("%zu", &in.board[i]);
        if (!in.board[i]) {
            in.board[i] = 16;
            in.gap = i;
        }
    }

    printf("%zd\n", astar(in, dtable));
    return 0;
}

Measured memory_bytes: 207284
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define mod 1e9+7
#define ll long long int
#define epsilon 1e-7


int main(void){
    int  n;
    scanf("%d", &n);
    int mem[9];
    for(int i = 0;i < 9;i++){
        mem[i] = 0;
    }
    for(int i = 0;i < n;i++){
        int a;
        scanf("%d", &a);
        if(a > 3200)a = 3200;
        mem[a/400]++;
    }
    int count = 0;
    for(int i = 0;i < 8;i++){
        if(mem[i] != 0)count++;
    }
    int mi, ma;
    if(count == 0)mi = 1;
    else mi = count;
    ma = mem[8] + count;
    printf("%d %d\n", mi, ma);
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
#define int long long
#define pb push_back
using namespace std;
using pii = pair<int, int>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<vector<pii>> g(n);
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    --u, --v;
    g[u].pb({ v, w });
    g[v].pb({ u, w });
  }

  struct edge {
    int u, v, w;
    edge() {}
    edge(int _u, int _v, int _w) { u = _u, v = _v, w = _w; }
  };

  auto getE = [&]() {
#define v x.first
#define w x.second
    vector<int> sz(n, 0);
    vector<int> mx(n, 0);
    vector<int> vis(n, 0);
    int rt = -1, tot = n;

    function<void(int, int)> frt = [&](int u, int f) {
      sz[u] = 1, mx[u] = 0;
      for (auto x : g[u])
        if (v ^ f && !vis[v]) {
          frt(v, u), sz[u] += sz[v];
          mx[u] = max(mx[u], sz[v]);
        }
      mx[u] = max(mx[u], tot - sz[u]);
      if (rt == -1 || mx[u] < mx[rt]) rt = u;
    };

    int p = -1;
    vector<int> dis(n, 0);
    function<void(int, int)> dfs1 = [&](int u, int f) {
      if (p == -1 || dis[u] + a[u] < dis[p] + a[p]) p = u;
      for (auto x : g[u])
        if (v ^ f && !vis[v]) dis[v] = dis[u] + w, dfs1(v, u);
    };

    vector<edge> e;
    function<void(int, int)> dfs2 = [&](int u, int f) {
      if (u ^ p) e.pb(edge(u, p, dis[u] + a[u] + dis[p] + a[p]));
      for (auto x : g[u])
        if (v ^ f && !vis[v]) dfs2(v, u);
    };

    function<void(int)> solve = [&](int u) {
      vis[u] = 1, p = -1, dis[u] = 0, dfs1(u, -1), dfs2(u, -1);
      for (auto x : g[u])
        if (!vis[v]) {
          rt = -1, tot = sz[v];
          frt(v, u), solve(rt);
        }
    };

    frt(0, -1), solve(rt);
#undef v
#undef w
    return e;
  };

  auto kruskal = [&](vector<edge> e) {
    vector<int> fa(n, -1);
    function<int(int)> find = [&](int x) {
      if (~fa[x]) return fa[x] = find(fa[x]);
      return x;
    };

    sort(e.begin(), e.end(), [](edge x, edge y) { return x.w < y.w; });
    int ans = 0;
    for (auto x : e) {
      int u = x.u, v = x.v, w = x.w;
      if ((u = find(u)) ^ (v = find(v))) { ans += w, fa[u] = v; }
    }

    return ans;
  };

  cout << kruskal(getE()) << '\n';
  return 0;
}

Measured memory_bytes: 142308
//メモリ数確認したか?
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
typedef long long int ll;
int n,k;
int p[100][100];
int solve(int s,int g)
{
	int k[100];
	bool f[100];
	for(int i=0;i<n;i++){
		k[i]=1e8+1;
		f[i]=false;
	}
	k[s]=0;
	while(true){
		int v=-1;
		for(int i=0;i<n;i++){
			if(!f[i]&&(v==-1||k[i]<k[v]))v=i;
		}
		if(v==-1)break;
		f[v]=true;
		for(int i=0;i<n;i++)k[i]=min(k[i],k[v]+p[v][i]);
	}
	return k[g];
}
int main()
{
	while(true){
	for(int i=0;i<100;i++)for(int j=0;j<100;j++)p[i][j]=1e8+1;
	scanf("%d%d",&n,&k);
	if(n==0&&k==0)return 0;
	for(int i=0;i<k;i++){
		int x,y,z,w;
		scanf("%d",&x);
		if(x==0){
			scanf("%d%d",&y,&z);
			int ans=solve(y-1,z-1);
			if(ans==1e8+1)printf("-1\n");
			else printf("%d\n",ans);
		}
		else{
			scanf("%d%d%d",&y,&z,&w);
			p[y-1][z-1]=min(w,p[y-1][z-1]);
			p[z-1][y-1]=p[y-1][z-1];
		}
	}
	}
    return 0;
}
//メモリ数確認したか?

Measured memory_bytes: 1232
n = int(input())
a = list(map(int,input().split()))
X_a = max(a)
N_a = min(a)
L_a = X_a - N_a + 1
b=[0]*L_a

while True:
  if N_a >= X_a:
    break
  for k in range(1,L_a+1):
    for i in a:      
      b[k-1] += (i-N_a)*(i-N_a)
    N_a +=1
print(min(b))


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

int main() {
  int X,N;
  cin >> X >> N;
  vector<bool> A(101);
//  int div = 10001;
//  int ans = 100000;
  int hozi;
  rep(i,N){
    cin >> hozi;
    A.at(hozi) = true;
  }
  rep(i,101){
    if(X-i<1){
      cout << 0 << endl;
      break;
    }
    else if(A.at(X-i) == false){
      cout << X -i << endl;
      break;
    }
    if(X+i>100){
      cout << 101 << endl;
      break;
    }
    else if(A.at(X+i) == false){
      cout << X+i << endl;
      break;
    }
  }
}

Measured memory_bytes: 3568
# -*- coding: utf-8 -*-

import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            #以後 read は増えない
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if _DEB: logd(f"imid: {imid}")

            if bb_lst[imid] <= limit:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)

### DEBUG I/O ###
_INPUT = """\
3 4 240
60 90 120
80 150 80 150
"""
_DEB = 0

def logd(str):
    if _DEB:
        print(f"[deb] {str}")

### MAIN ###
if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    main()


Measured memory_bytes: 41456
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define elif else if
int a,b,c;
string s;
signed main()
{
	cin>>a>>b>>c;
	if(a*c<=b){
		cout<<c;
	}else{
		cout<<b/a;
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
void dijkstra(vector<multimap<int, int>> &G, int s, int g){
    vector<long long> dis(g+1, -1);
    dis[s] = 0;
    priority_queue <pair<long long, int>,vector<pair<long long, int>>,greater<pair<long long, int>> > q;
    q.push(make_pair(0,s));
    while(!q.empty()){
        auto node = q.top();
        int cur = node.second;
        q.pop();
        if(node.first > dis[cur]) continue;
        for(auto& n:G[cur]){
            int t = n.first;
            int w = n.second;
            if(dis[t] > dis[cur] + w || dis[t] < 0){
                dis[t] = dis[cur] + w;
                q.push(make_pair(dis[t], t));
            }
        }
    }
    for(int i=1;i<dis.size();i++){
        cout << dis[i] % 2 << endl;
    }
}
int main(){
    int n;
    scanf("%d", &n);
    vector<multimap<int, int>> G(n+1);
    for(int i=0;i<n-1;i++){
        int f, g, w;
        scanf("%d %d %d", &f, &g, &w);
        G[f].insert(make_pair(g,w));
        G[g].insert(make_pair(f,w));
    }
    dijkstra(G, 1, n);
    return 0;
}

Measured memory_bytes: 15360
#include <stdio.h>

int main() {

	int X;

	scanf("%d",&X);

	int i;

	int ans;

	for (i = 1; i <= 100000; i++) {
		if ((i)*(i + 1) / 2 >= X) {
			ans = i;
			break;
		}
	}
	printf("%d\n",ans);

	return 0;
}

Measured memory_bytes: 128
N = int(input())
l = []
if N%2 == 0:
    for i in range(N):
        for j in range(i):
            I = i+1
            J = j+1
            if I+J != N+1 :
                l.append((I,J))
else:
    for i in range(N):
        for j in range(i):
            I = i+1
            J = j+1
            if I+J != N :
                l.append((I,J))
print(len(l))
for k in l:
    print("{} {}".format(k[0],k[1]))

Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int n;
   int a[1111];
   int b[1111];
   cin>>n;
   int cnt=0;
   for(int i=1;i<=n;i++)
   {
       cin>>a[i];
       if(a[i]%2==0)
        b[++cnt]=a[i];
   }
   int sum=0;
   for(int i=1;i<=cnt;i++)
   {
       if(b[i]%3==0||b[i]%5==0)
       {
           sum++;
       }
   }
   if(sum==cnt)
    puts("APPROVED");
   else
   puts("DENIED");
    return 0;
}


Measured memory_bytes: 256
while(n,p=gets.split.map(&:to_i))!=[0,0]
	a=Array.new(n,0)
	i=0
	c=p
	while (a[i]!=p)
		c==0 ? (c+=a[i];a[i]=0) : (a[i]+=1;c-=1) 
		i=(i+1)%n
	end
	puts i
end

Measured memory_bytes: 6096
x,y=map(int,input().split())
if(abs(y)>=abs(x)):
    a=abs(y)-abs(x)
    if(x>=0):
        if(y<0):
            a+=1
    else:
        if(y>0):
            a+=1
        else:
            a+=2
else:
    a=abs(x)-abs(y)
    if(x>=0):
        if(y<=0):
            a+=1
        else:
            a+=2
    else:
        if(y>0):
            a+=1
print(a)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

int main() {
	ll n;cin>>n;
	// ll res=0;
	set<ll>s;
	for(ll i=1;i*i<=n-1;i++){
		if((n-1)%i==0){
			if(i>1) s.insert(i);
			if((n-1)/i!=i) s.insert((n-1)/i);
		}
	}
	auto check=[&](ll a){
		ll x=n;
		while(x%a==0) x/=a;
		if(x%a==1) s.insert(a);
	};
	for(ll i=1;i*i<=n;i++){
		if(n%i==0){
			if(i>1) check(i);
			if(i!=n/i) check(n/i);
		}
	}
	cout<<s.size()<<'\n';
}

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

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \
    for(long long hoge = 0; (hoge) < (n); ++(hoge))                            \
    cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

using ll = long long;
using ull = unsigned long long;
const ll INF = LLONG_MAX / 2;
const ll MOD = 1e9 + 7;

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    ll N, K;
    cin >> N >> K;
    ll A[N];
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    if(K == 1) {
        cout << 0 << endl;
        return 0;
    }
    for(int i = 0; i < N; i++) {
        A[i]--;
        A[i] %= K;
    }
    ll ans = 0;
    map<ll, ll> cnt;
    cnt[0] = 1;
    ll sum[N + 1] = {};
    for(int i = 0; i < N; i++) {
        sum[i + 1] = (sum[i] + A[i]) % K;
        ans += cnt[sum[i + 1]]++;
        if(i + 2 - K >= 0) {
            cnt[sum[i + 2 - K]]--;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 15872
#include <bits/stdc++.h> //library
#define rep(i, n) for(int i = 0; i < (int)(n); i++) //for-loop (from 0)
#define reps(i,n) for(int i = 1; i <= (int)(n); i++) //for-loop (from 1)
#define all(x) (x).begin(),(x).end() //start-to-end iteration, mainly in upper_bound, lower_bound
#define SZ(x) ((int)(x).size()) //unsigned to signed

using namespace std;
using ll = long long; //long long
ll M = 1000000000 + 7;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } //compare a,b and replace by greater
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //compare a,b and replace by smaller
ll qp(int a, ll b) { int ans = 1; do { if (b & 1)ans = 1ll * ans*a; a = 1ll * a*a; } while (b >>= 1); return ans; } //power
ll qp(int a, ll b, ll Mod = M) { int ans = 1; do { if (b & 1)ans = 1ll * ans*a%Mod; a = 1ll * a*a%Mod; } while (b >>= 1); return ans; } //power(mod)
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; } //gcd
int dx[4] = { 1,0,-1,0 }; //path
int dy[4] = { 0,1,0,-1 }; //path
ll RS(ll N, ll P, ll Mod = M) { if (P == 0)return 1; else if (P % 2 == 0) { ll sq = RS(N, P / 2, Mod); return sq*sq%Mod; } else { return N*RS(N, P - 1, Mod) % Mod; } } //power(mod)(repeat-square,fast)
ll nPr(ll n, ll r, ll Mod = M) { ll ans = 1; rep(i, r) { ans = ans * (n - i) % Mod; } return ans; } //permutation
ll nCr(ll n, ll r, ll Mod = M) { ll bunbo = 1; ll bunshi = 1; rep(i, r) { bunbo = bunbo * (i + 1) % Mod; bunshi = bunshi * (n - i) % Mod; } return bunshi * RS(bunbo, Mod - 2, Mod) % Mod; } //combination
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2
    vector<int> par_num;

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
        par_num.assign(N,1);
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;  //親がいない=自分自身が根
        return par[x] = root(par[x]); //xの親を、xの親の親につなぎ直す...つまり、ある木のノードを全て根につなぎ直す
    }

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

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};
int main(void) {
  int N,M; cin >> N >> M;
  vector<int> nums(N);
  rep(i,N){
    cin >> nums[i];
    nums[i]--;
  }
  UnionFind tree(N);
  rep(i,M){
    int x,y; cin >> x >> y;
    tree.unite(x-1,y-1);
  }
  int ans = 0;
  rep(i,N){
    if (nums[i]==i) ans++;
    else if (nums[i]!=i && tree.same(nums[i],i)) ans++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 1408
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define itn int
#define retrun return
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
using namespace std;
typedef long long llong;
typedef pair<int, int> pii;
llong gcd(llong a, llong b) {
	if (a < b) std::swap(a, b);
	if (a % b == 0)
		return b;
	else
		return gcd(b, a % b);
}
llong lcm(llong a, llong b) {
	llong g = gcd(a, b);
	return a * b / g;
}
//ここまで共通
llong N, K;
int main() {
	cin >> N >> K;
	N %= K;
	N = min(N, K - N);
	cout << N << endl;
	return 0;
}

Measured memory_bytes: 256
x, y = map(int, input().split())


def calc(xx, yy):
    if yy >= xx:
        return yy- xx
    else:
        return float('inf')


x1, y1 = x, y
x2, y2 = -x, y
x3, y3 = x, -y
x4, y4 = -x, -y

ans1 = calc(x1, y1)
ans2 = calc(x2, y2) + 1
ans3 = calc(x3, y3) + 1
ans4 = calc(x4, y4) + 2

print(min(ans1, ans2, ans3, ans4))

Measured memory_bytes: 3064
// 'By Anki'
 
#include<bits/stdc++.h>
// #include<iostream>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout<<#i<<" = "<<i<<",  ";
#define wre(i) cout<<#i<<"  =  "<<i<<endl;
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000007LL
typedef unsigned long long ull;
#define M 100006LL
using u64= uint64_t;
// using i128= __int128;
// typedef unsigned int uint128_t __attribute__((mode(TI)));
 
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
 
ll fact(ll n)
{
    ll ans=1LL;
    while(n)
    ans*=(n--);
    return ans;
 
}
 
ll phi(ll n){
    ll res=n;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            while(n%i==0)n/=i;
            res-=res/i;
        }
    }
    if(n>1)res-=res/n;
    return res;
}

ll a[100][100];
void sparse(vector<ll>v)
{
    ll n=v.size();
    ll k=log2(n)+1;
    for(int i=0;i<n;i++)a[i][0]=v[i];
    for(int j=1;j<=k;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        a[i][j]=a[i][j-1]+a[i+(1<<(j-1))][j-1];
    }
}
int main()
{
fast
ll t=1LL;

for(;t;t--){
   ll n;
   cin>>n;
   map<ll,ll>m;
   for(ll i=2;i*i<=n;i++){
       while(n%i==0){
           m[i]++;
           n/=i;
       }
   }
   if(n>1)m[n]++;
   ll cn=0LL;
   for(auto x:m){
    //    wr(x.first)wre(x.second);
       ll tmp=0LL;
       ll i=x.second;
       while(i>0){
           tmp++;
           i-=tmp;
           if(i<0)break;
           cn++;
       }
   }
   cout<<cn;
} 
}

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

int main() {
  int N,M,L,R,A,B;
  cin >> N >> M;
  A=1;
  B=N;
  for(int i=0; i<M; i++){
    cin >> L >> R;
    if(A<L){
      A=L;
    }
    if(R<B){
      B=R;
    }
  }
  if(B-A+1>=0){
    cout << B-A+1 << endl;
  }
  else{
    cout << 0 << endl;
  }
}

Measured memory_bytes: 256
Q = int(input())
LR = [list(map(int,input().split())) for _ in range(Q)]
flg = [False] * (10**5+1)

def isPrime(n):
  if n < 2:
    return False
  i = 2
  while i*i <= n:
    if n%i == 0:
      return False
    i += 1
  return True

def Eratosthenes(n):
    dp = [False]*n
    dp[0] = dp[1] = True
    prime = []
    for i in range(2,n):
        if dp[i]:
            continue
        prime.append(i)
        for j in range(i*2,n,i):
          dp[j] = True
    return prime

Era = Eratosthenes(10**5+1)
for e in Era:
  flg[e] = True

cnt = []
tmp = 0
for i in range(10**5+1):
  if flg[i] and flg[(i+1)//2]:
    tmp += 1
  cnt.append(tmp)

for i in range(Q):
  print(cnt[LR[i][1]] - cnt[LR[i][0]-1])

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

int main()
{
  int a, b, c;
  cin >> a >> b >> c;
  if(a + b == c || b + c == a || c + a == b)
  {
    cout << "Yes" << endl;
  }
  else
  {
    cout << "No" << endl;
  }
  return 0;
}

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

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }	//最大公約数(Greatest Common Divisor)
ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }			//最小公倍数(Least Common Multiple)

long long comb(long long m) {
	return (m * (m - 1)) / 2;
}


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



	cout << a+a*a+a*a*a << endl;
	


}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
#define INF      (int)1e9
#define EPS      (double)1e-9
#define MOD      ((int)1e9+7)
using namespace std;
typedef long long     llong;
typedef vector<int>   vi;
typedef vector<vi >   vvi;
typedef vector<vvi >  vvvi;
typedef pair<int,int> pii;
template<class Type> void line(const Type &a){int cnt=0;for(const auto &elem:a){if(cnt++)cerr<<' ';cerr<<elem;}cerr<<endl;}

int main(){
	int h,w,k;
	cin>>h>>w>>k;
	
	vector<string> s(h);
	REP(i,h) cin>>s[i];
	
	vvi dp(h+1,vi(w,0));
	REP(i,h)REP(j,w) dp[i+1][j]=s[i][j]-'0';
	REP(i,h)REP(j,w) dp[i+1][j]+=dp[i][j];
	//REP(i,h+1) line(dp[i]);
	
	int ans=INF;
	REP(i,1<<(h-1)){
		vi p={0};//横に分ける位置.
		REP(j,h-1){
			if(i>>j&1) p.push_back(j+1);
		}
		p.push_back(h);
		
		int cnt=0;				//縦に割る必要な回数.
		int sum[p.size()]={};	//それぞれの横列にその時含まれるホワイトチョコの数.
		REP(j,w)for(int l=1;l<p.size();++l){
			int hoge=dp[p[l]][j]-dp[p[l-1]][j];
			if(hoge>k){
				cnt=INF;
				goto Exit;//!!!
			}else{
				if(sum[l]+hoge>k){
					cnt++;
					for(int m=1;m<p.size();++m) sum[m]=dp[p[m]][j]-dp[p[m-1]][j];
					break;//for(l).
				}else sum[l]+=hoge;					
			}
		}
		
Exit:
		ans=min(ans,cnt+(int)p.size()-2);
	}
	
	cout<<ans<<endl;
}

Measured memory_bytes: 3700
#include<stdio.h>
int main()
{
	int h,w;
	scanf("%d %d",&h,&w);
	int i;
	char a[102][102];
	for(i=0;i<h;i++)
	  scanf("%s",a[i]);
	for(i=0;i<w+2;i++)
	  printf("#");
	printf("\n");
	for(i=0;i<h;i++)
	  printf("#%s#\n",a[i]);
	for(i=0;i<w+2;i++)
	  printf("#");
	printf("\n");
	return 0;
}

Measured memory_bytes: 128
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<Integer> a = new ArrayList<Integer>();
		for (int i = 2; i < 300000; i++) {
			if (i % 7 == 1 || i % 7 == 6) {
				boolean e = false;
				for (int j = 0; j < a.size(); j++) {
					if (i % a.get(j) == 0) {
						e = true;
						break;
					}
				}
				if (!e)
					a.add(i);
			}
		}
		Integer[] ans = a.toArray(new Integer[a.size()]);
		while (true) {
			int n = Integer.parseInt(sc.next());
			if (n == 1) break;
			System.out.print(n + ":");
			for (int i = 0; i < ans.length; i++) {
				if (n % ans[i] == 0) {
					System.out.print(" " + ans[i]);
				}
			}
			System.out.println();
		}
	}
}


Measured memory_bytes: 32864
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef map<int, int> mi;
typedef set<int> si;
#define VV(T) vector<vector< T > >
#define dump(x)  cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl

#define rep(i, n) REP(i, 0, n)                              // 0, 1, ..., n-1
#define REP(i, x, n) for(int i = x; i < n; i++)             // x, x + 1, ..., n-1
#define FOREACH(x,a) for(auto& (x) : (a) )

#define ALL(v) (v).begin() , (v).end()
#define RALL(v) (v).rbegin(), (v).rend()

#define pb push_back
#define pu push
#define mp make_pair
#define fi first
#define sc second

#define COUT(x) cout << (x) << endl
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECCOUT(x) for(auto&youso_: (x) )cout<<youso_<<" ";cout<<endl

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


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

    ll n; cin >> n;
    vector<ll> a(n), b(n), c(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n) cin >> c[i];

    sort(ALL(a));
    sort(ALL(b));
    sort(ALL(c));

    ll sum = 0;
    rep(i, n) {
        auto iter_a = lower_bound(ALL(a), b[i]);
        auto iter_c = upper_bound(ALL(c), b[i]);
        ll idx_a = distance(a.begin(), iter_a);
        ll idx_c = c.size() - distance(c.begin(), iter_c);
        sum += idx_a * idx_c;
    }
    COUT(sum);
}

Measured memory_bytes: 2560
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int n,m;
long long a=1,b=1;

int main(){
	cin>>n>>m;
	if(max(n,m)-min(n,m)>1){
		cout<<0<<endl;
	}else{
		if(n==m){
			for(int i=n;i>0;i--){
				a*=i;
				a=a%1000000007;
			}
			b=a;
			a=(a*b*2)%1000000007;
			cout<<a<<endl;
		}else{
			for(int i=n;i>0;i--){
				a*=i;
				a=a%1000000007;
			}
			for(int i=m;i>0;i--){
				b*=i;
				b=b%1000000007;
			}			
			a=(a*b)%1000000007;
			cout<<a<<endl;
		}
	}
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <unordered_map>
typedef uint64_t u64;

static const auto XYZ = ~(u64)0;
static const auto NMAX = 40;
static const auto MOD = 1000000007;
static inline void ADD(int &a, int b) {if((a += b) >= MOD) a -= MOD;}

static int N, X[3];

static u64 f(int n, const int *buf) {
	if(n == 0) return 0;
	auto m = 0, s = 0;
	for(auto i = 0; i < n; i++) {
		if(m == 3) return f(n - 1, buf + 1);
		if(s + buf[i] >= X[m]) {
			if(s + buf[i] != X[m]) return f(n - 1, buf + 1);
			m++;
			s = 0;
		} else {
			s += buf[i];
		}
	}
	if(m == 3) return XYZ;
	u64 x = 0;
	for(auto i = n; --i >= 0; ) x = x << 3 | buf[i];
	return x;
}

static u64 cat(u64 x, int y) {
	if(y > X[0] && y > X[1] && y > X[2]) return 0;
	auto n = 0; int buf[NMAX];
	while(x != 0) {
		buf[n++] = x & 7;
		x >>= 3;
	}
	buf[n++] = y;
	return f(n, buf);
}

int main() {
	scanf("%d%d%d%d", &N, X, X + 1, X + 2);
	auto ans = 0;
	std::unordered_map<u64, int> A, B;
	A.emplace(0, 1);
	for(auto n = 0; n < N; n++) {
		ans = ans * (u64)10 % MOD;
		for(const auto &p : A) {
			for(auto i = 1; i <= 10; i++) {
				auto q = cat(p.first, i);
				ADD(q == XYZ ? ans : B[q], p.second);
			}
		}
		A.swap(B);
		B.clear();
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 2048
#include <bits/stdc++.h>
#define rep(i,N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf{int(1e9)};
int main(void){
  string s;
  cin >> s;

  vector <int> ans;
  int N = s.length();
  rep(i,N){
    if (s[i] == '0') ans.push_back(0);
    else if (s[i] == '1') ans.push_back(1);
    else if (s[i] == 'B' && ans.size() != 0) ans.pop_back();
  }
  rep(i,ans.size()) cout << ans[i];
}


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

using namespace std;

vector < vector <int> > tablica;

int main(){
	string prvi , drugi , rjesenje="";
	cin>>prvi;
	cin>>drugi;
	int velPrvog=prvi.length() , velDrugog=drugi.length();
	vector <int> pomoc(velDrugog+1,0);
	tablica.insert(tablica.begin(),velPrvog+1,pomoc);
	for (int i=0;i<=velPrvog;i++){
		for (int j=0;j<=velDrugog;j++){
			if (i==0 || j==0) tablica[i][j]=0;
			else if (prvi[i-1]==drugi[j-1]){
				tablica[i][j]=tablica[i-1][j-1]+1;
			}
			else tablica[i][j]=max(tablica[i-1][j],tablica[i][j-1]);
		}
	}
	/*
	for (int i=0;i<=velPrvog;i++){
		for (int j=0;j<=velDrugog;j++){
			cout<<tablica[i][j];
		}
		cout<<endl;
	}*/
	int i=velPrvog , j=velDrugog;
	while (i>0 && j>0){
		//cout<<tablica[i][j]<<" susjedi "<<tablica[i][j-1]<<' '<<tablica[i-1][j]<<endl;
		if (tablica[i][j]==tablica[i-1][j]){
			i--;
		}
		else if (tablica[i][j]==tablica[i][j-1]){
			j--;
		}
		else{
			i--;
			j--;
			rjesenje=prvi[i]+rjesenje;
		}
	}
	
	cout<<rjesenje;
	return 0;
}


Measured memory_bytes: 35584
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
typedef long long ll;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define FOR(i,n) for(int i=0;i<n;i++)
#define all(s) s.begin(),s.end()
#define lla(s) s.rbegin(),s.rend()
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const ll INF = 1e18;



signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,ans = 0;
    cin>>n;
    vector<int>a(n),app(n+1,0);
    int st = 1,count = 0;
    rep(i,0,n){
        cin>>a[i];
        if(a[i]==st){count++;st++;}
    }
    if(count==0){cout<<-1<<"\n";}
    else{
        cout<<(n-count)<<"\n";
    }
}

Measured memory_bytes: 3328
/*   ꧁༒☬महाकाल☬༒꧂ ꧁༒ঔमहाकालঔ༒꧂
                   
                   ঔৣ۝महाकालᴳᵒᵈ۝ঔৣ            
 
 */ 
#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template <typename T, typename cmp=less<T>> using oset =tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long 
#define mod 1000000007
#define inf 100000000000000000
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define _it iterator
#define all(_x) _x.begin(),_x.end()
#define f first
#define s second
#define pb push_back
void mahakal(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	freopen("error.txt","w",stderr);
	#endif
}
bool check(ll* a , ll* b ,ll n , ll k , ll mid){
	ll ans = 0 ;
	for(ll i=0;i< n ;i++){
		if(a[i]*b[i]>mid){
			ans += a[i]-mid/b[i];
		}
	}
	return ans <= k;
}
int main(){
	//mahakal(),fast;
	
	ll n , k ;
	cin>> n >> k ;
	ll a[n],b[n];
	for(ll i=0;i< n ;i++)cin>>a[i];
	for(ll i=0;i< n ;i++)cin>>b[i];
	sort(a,a+n);
	sort(b,b+n,greater<ll>());
	ll ans = 0;
	ll l=0 , r=inf;
	while(l<=r){
		ll mid=(l+r)/2;
		bool ps=check(a,b,n,k,mid);
		if(ps){
			ans=mid;
			r=mid-1;
		}
		else {
			l=mid+1;
		}
	}
	cout<<ans<<endl;

	return 0;
}

Measured memory_bytes: 3328
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

#define MOD 1000000007

using ll = long long;
const int limit = 100010;
using edge = struct { int to; ll cost;};

ll depth[limit];
vector<edge> tree[limit];

void dfs(int v, int p, ll d){
    depth[v] = d;
    for(auto &e : tree[v]){
        if(e.to == p){
            continue;
        }else{
            dfs(e.to,v,d+e.cost);
        }
    }
}
int main(void) {
    int n;
    cin >> n;
    for(int i=0;i<n-1;i++){
        int a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        tree[a].push_back({b,c});
        tree[b].push_back({a,c});
    }
    int q,k;
    cin >> q >> k;
    k--;
    dfs(k,-1,0);
    for(int i=0;i<q;i++){
        int x,y;
        cin >> x >> y;
        x--;
        y--;
        cout << depth[x] + depth[y] << endl;
    }
    return 0;
}


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

#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define rev(a) (a).rbegin(),(a).rend()
#define pll pair<ll,ll>
#define rep(i,a,b) for(long long i = (a); i <= (b); i++)

const int mod = 1e9+7;
const int N = 1e2+5; 

ll arr[N][N];

ll n,m,x; 

int main(){
	fast;
	cin >> n >> m >> x;
	rep(i,1,n){
	    rep(j,0,m){
	        cin >> arr[i][j];
	    }
	}
	
	bool flag = false;
	ll cost = 1e18;
	
	ll last = (1 << n);
	
	rep(i,1,last-1){
	    ll tmpFlag = true;
	    vector<ll> r;
	    ll tmp = i;
	    ll cnt = 1;
	    while(tmp){
	        if(tmp & 1){
	            r.pb(cnt);
	        }
	        tmp >>= 1;
	        cnt++;
	    }
	    
	    rep(i,1,m){
	        bool intFlag = false;
	        ll total = 0;
    	    for(auto x : r){
    	        total += arr[x][i];
    	    }
    	    
    	    if(total < x){
    	        intFlag = false;
    	    }
    	    else{
    	        intFlag = true;
    	    }
    	    
    	    if(!intFlag){
    	        tmpFlag = false;
    	    }
	    }
	    
	    flag |= tmpFlag;
	    if(tmpFlag){
	        ll tmpS = 0;
	        for(auto x : r){
	            tmpS += arr[x][0];
	        }
	        
	        cost = min(cost,tmpS);
	    }
	}
	
	if(flag) cout << cost;
	else cout << "-1\n" << endl;
}

Measured memory_bytes: 3652