akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<iomanip>
#include<string>
#include<array>
using namespace std;

int main() {
	int n, ans = 0, keta = 0,m;
	cin >> n;
	m = n;
	while (m > 0) {
		m /= 10;
		keta++;
	}

	if (keta == 1){
		cout << n;
	}else if (keta == 2) {
		cout << 9;
	}else if(keta == 3){
		cout << 9 + n - 99;
	}
	else if (keta == 4) {
		cout << 9 + 900;
	}
	else if(keta==5){
		cout << 9 + 900 + n - 9999;
	}
	else {
		cout << 9 + 900 + 90000;
	}
}

Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static void main(String[] args){
        Main start=new Main();
        start.start();

    }
    public Main(){
    }
    public void start(){
        Scanner sc = new Scanner(System.in);
        // 整数の入力
        int N = sc.nextInt();
//        int K=sc.nextInt();
//        System.out.println(N-K);

//        char[][] s=new char[N][];
        int bCount=0;
        int aCount=0;
        int result=0;
        int a=0;
        for(int i=0;i<N;i++){
            char[] t=sc.next().toCharArray();
            for(int j=0;j<t.length-1;j++){
                if(t[j]=='A'&&t[j+1]=='B'){
                    result++;
                }
            }
            if(t[0]=='B'){
                bCount++;
            }
            if(t[t.length-1]=='A'){
                aCount++;
            }
            if(t[0]=='B'||t[t.length-1]=='A'){
                a++;
            }

        }
        System.out.println(result+Math.min(Math.max(a-1,0),Math.min(aCount,bCount)));

    }
    int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
}

Measured memory_bytes: 32320
object Main {
    def main(args: Array[String]) = {
        val List(a, b, c, d, e) = io.Source.stdin.getLines.map(_.toInt).toList
        println(if (a<0 && b>0) a*(-c)+d+b*e else if(b<=0) (b-a)*c else if(a>=0) (b-a)*e)
    }
}

Measured memory_bytes: 43792
import cmath
import itertools
n = int(input())
C = sorted([complex(*map(int,input().split())) for _ in range(n)], key=cmath.phase)
score = 0
for i,j in itertools.product(range(n),range(n)):
    if i>j: continue
    score = max((score,abs(sum(C[:i]+C[j:])),abs(sum(C[i:j]))))
print(score)

Measured memory_bytes: 43228
a,b = map(int,input().split())
nu = 0
if a % 2 == 1:
    nu = a
    a += 1
num = (b-a) // 4
a += 4 * num
ans = a
for i in range(a+1,b+1):
    ans ^= i
ans ^= nu
print(ans)

Measured memory_bytes: 3060
#include <stdio.h>
int main(){
    int a, b, x;
    char op;
    while(1){
        scanf("%d %c %d\n", &a, &op, &b);
        
        if(op == '+'){
            x = a + b;
        }else if(op == '-'){
            x = a - b;
        }else if(op == '*'){
            x = a * b;
        }else if(op == '/'){
            x = a / b;
        }else{
            break;
        }
        printf("%d\n", x);
    }
    
    return 0;
}


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

int main(){
	int N,ans=0,minv=(1<<20);
  	cin>>N;
  	vector<int>a(N);
	for(int i=0;i<N;++i)cin>>a[i];
  	for(int i=-100;i<=100;++i){
      	ans=0;
    	for(int j=0;j<N;++j){
        	ans+=pow((i-a[j]),2);
		 }minv=min(minv,ans);
    }cout<<minv<<endl;
  	return 0;
}

Measured memory_bytes: 256
// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a); i<(b);++i)
#define rep(i, n) FOR(i, 0, n)

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
bool comp(const Pl &a, const Pl &b){
  return a.first < b.first;
}

int main(int argc, char const* argv[])
{
  int n;
  cin >> n;
  vector<Pl> vec;
  rep(i, 2 * n){
    ll x;
    cin >> x;
    vec.pb(mk(x, i / n));
  }
  sort(vec.begin(), vec.end(), comp);
  int pc = 0;
  int bat = 0;
  ll res = 1;
  rep(i, 2 * n){
    if(vec[i].second == 0){
      if(bat > 0){
        res = res * bat % mod;
        bat--;
      }else{
        pc++;
      }
    }else{
      if(pc > 0){
        res = res * pc % mod;
        pc--;
      }else{
        bat++;
      }
    }
  }
  cout << res << endl;
	return 0;
}


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

int main(){
    int dice[7],a;
    string order;
    for(int i=1;i<7;i++){
        cin>>dice[i];
    }
    cin>>order;
    
    for(int i=0;i<order.length();i++){
        if(order[i]=='E'){
            a=dice[1];
            dice[1]=dice[4];
            dice[4]=dice[6];
            dice[6]=dice[3];
            dice[3]=a;
        }
        else if(order[i]=='N'){
            a=dice[1];
            dice[1]=dice[2];
            dice[2]=dice[6];
            dice[6]=dice[5];
            dice[5]=a;
        }
        else if(order[i]=='S'){
            a=dice[1];
            dice[1]=dice[5];
            dice[5]=dice[6];
            dice[6]=dice[2];
            dice[2]=a;
        }
        else if(order[i]=='W'){
            a=dice[1];
            dice[1]=dice[3];
            dice[3]=dice[6];
            dice[6]=dice[4];
            dice[4]=a;
        }
    }
    cout<<dice[1]<<endl;
}

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

template <class T> using V = vector<T>;

using ll = long long;
using db = double;
using st = string;
using ch = char;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;


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



int main() {
  ll n,k;
  	ci(n>>k);
  vll A(n);
  ll one=0;
  FOR(i,0,n){
    ci(A[i])
    if(A[i]==1){
      one=i;
    }
  }
  
  ll ans=1;
  vll B(k);
  
  if(n==k){
    co(1)
  }
  else if(k==2){
    co(n-1)
  }
  else{
  FOR(i,0,k){
    if((one+k-i)>n||(one-i)<0){
      continue;
    }
    else{
    B[i]+=(one+k-2-i)/(k-1);//左側
    //co(B[i])
    B[i]+=(n-one-2+i)/(k-1);//右側
    //co(B[i])
      B[i]++;
    }
  }
  SORT(B);
      FOR(i,0,n){
        if(B[i]==0){
          continue;
        }
        else{
          co(B[i])
            break;
        }
      }
  }
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i > n; --i)
typedef long long ll;
#define mod 1000000007

int main() {
  int X; cin >> X;
  priority_queue<ll,vector<ll>> Q;
  Q.push(1);
  rep(i, 2, X) {
    ll v = i * i;
    while(v <= X) {
      Q.push(v);
      v *= i;
    }
  }
  cout << Q.top() << endl;
  return 0;
}


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

int main()
{
	int h, w;
	cin >> h >> w;
	string in[100];
	for (int i = 0; i < h; ++i)
	{
		cin >> in[i];
	}
	for (int i = 0; i < h * 2; ++i)
	{
		cout << in[i/2] << endl;
	}
	return 0;
}

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

#include <math.h>
#include <stdlib.h>


#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

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

const int INF = 1001001001;
// long long max => LLONG_MAX

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int n; cin >> n;
    vector<vector<int>> g(n);
    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vector<int> dist_o(n,INF);
    vector<int> dist_e(n, INF);

    //bfs
    queue<int> que;
    dist_o[0] = 0;
    que.push(0);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int nv : g[v]){
            if(dist_o[nv] != INF) continue;
            dist_o[nv] = dist_o[v] + 1;
            que.push(nv);
        }
    }

    dist_e[n-1] = 0;
    que.push(n-1);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int nv : g[v]){
            if(dist_e[nv] != INF) continue;
            dist_e[nv] = dist_e[v] + 1;
            que.push(nv);
        }
    }

    int cnt = 0;
    for(int i = 0; i < n; i++){
        if(dist_o[i] <= dist_e[i]) cnt++;
    }


    if(n/2 < cnt) cout << "Fennec" << endl;
    else cout << "Snuke" << endl;

    return 0;
}


Measured memory_bytes: 6528
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<int,P> PP;

vector<PP> v;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

int main()
{
	int a;
	string s;
	cin >> a >> s;
	if(a>=3200) cout << s << endl;
	else cout << "red" << endl;
	
	return 0;
}


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

int main() {
	int array[1000000];
	int n, count = 0;
	for(int i = 0; i < 1000000; i++) {
		array[i] = 1;
	}
	array[0] = 0;
	array[1] = 0;
	for(int i = 2; i < 1000000; i++) {
		if(array[i] != 0) {
			for(int j = i * 2; j < 1000000; j += i) {
				array[j] = 0;
			}
		}
	}
	while(cin >> n) {
		for(int i = 0; i <= n; i++) {
			count += array[i];
		}
		cout << count << endl;
		count = 0;
	}

	return 0;
}

Measured memory_bytes: 5064
#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define PB push_back

#define MP make_pair
#define all(x) x.begin(),x.end()
#define pri 1000000007
#define pr pair <ll,ll>

using namespace std;

void masterstroke(){
ll n;
int q;
cin>>n>>q;
vector <pair < ll ,pair <ll,ll> > > f; 
for(int i=0;i<n;i++){
	ll x,s,t;
	cin>>s>>t>>x;	
	f.PB(MP(x,MP(s-x,t-1-x)));
	}

sort(all(f));
set < pair<ll,int> > z;
ll ans[q];
for(int i=0;i<q;i++){
    ll d;
    ans[i]=-1;
	cin>>d;
    z.insert(MP(d,i));
}

for(auto i: f){
  
  ll xans=i.ff;
  ll l=i.ss.ff;
  ll r=i.ss.ss;
  //cout<<xans<<" "<<l<<" "<<r<<endl;
  auto fa=z.lower_bound(MP(l,-1));
  if(fa==z.end())continue;
  while(fa!=z.end()&&(fa->ff)<=r){
     auto k=fa;
     ans[fa->ss]=xans;
     fa++;
     z.erase(k);  
  }

}
for(int i=0;i<q;i++){
	cout<<ans[i]<<endl;
}


}

int main() {
    ll t=1;
    //cin>>t;
    for(ll i=0;i<t;i++)masterstroke();
	// your code goes here
	return 0;
}

Measured memory_bytes: 21868
#include <bits/stdc++.h>
using namespace std;
int q,b,c1,c2,limit;

int check(int M){
  int B=b,cnt=M;
  B-=c1*M;
  if(B<0) return 0;
  cnt+=(B/c2);
  return (cnt>=q);
}

int main(){
  while(cin>>q,q){;
    cin>>b>>c1>>c2>>limit;

    int L=0,M,R=limit+1;
    while(L<R) {
      M=(R+L)/2;
      if(!check(M)) R=M;
      else L=M+1;
    }
    if(L<=1) cout <<"NA"<<endl;
    else cout <<(L-1)<<" "<<((b-(L-1)*c1)/c2)<<endl;
    
  }
  return 0;
}

Measured memory_bytes: 1160
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i,n) for(int i = 0;i < n; i++)
#define REP(i,n) for(int i = 1;i <= n; i++)
// ----GCC ONLY---- //
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// ----        ---- //
#define ll long long
#define MOD 10000007
#define PB push_back

using namespace std;



int main(){

    int a[4];

    cin >> a[0] >> a[1] >> a[2];

    int ans = a[0] + a[1] + a[2];

    if(ans <= 21){
        puts("win");
    }else{
        puts("bust");
    }

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

#define REP(i,n) for(int i = 0; i < n; i++)
#define RREP(i,n) for(int i = n - 1; i >= 0; i--)

#define ALL(v) v.begin(),v.end()
#define pb push_back
#define mp make_pair
#define F first
#define S second

#define UNIQUE(v) do{sort(v.begin(), v.end()); v.erase( unique(v.begin(), v.end()), v.end() );} while(0)


#define y0 y3487465
#define y1 y8687969

#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};
ll MOD = 1000000007;

int N;
const int MAX_N = 1000;
int P[MAX_N];
ll X[MAX_N];

vi G[MAX_N];

int main(){
  cin >> N;
  REP(i, N - 1) cin >> P[i];
  REP(i, N) cin >> X[i];
  REP(i, N - 1) {
    G[P[i] - 1].pb(i + 1);
  }

  ll Y[N];
  REP(i, N) Y[i] = 0;

  bool dp[N][5001];

  RREP(i, N) {
    ll s = 0;
    REP(j, G[i].size()) {
      s += X[G[i][j]] + Y[G[i][j]];
    }
    REP(j, G[i].size() + 1) {
       REP(k, X[i] + 1) {
         dp[j][k] = false;
       }
    }
    dp[0][0] = true;
    REP(j, G[i].size()) {
      int ch = G[i][j];
      REP(k, X[i] + 1) {
        if(k >= X[ch]) {
          dp[j + 1][k] = dp[j][k - X[ch]];
        }
        if(k >= Y[ch]) {
          dp[j + 1][k] = dp[j + 1][k] || dp[j][k - Y[ch]];
        }
      }
    }
    bool flag = false;
    RREP(k, X[i] + 1) {
      if(dp[G[i].size()][k]) {
        Y[i] = s - k;
        flag = true;
        break;
      }
    }
    if(!flag) {
      cout << "IMPOSSIBLE" << endl;
      return 0;
    }
  }

  cout << "POSSIBLE" << endl;



  return 0;
}


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

#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define per(i,n) for (int i=(int)(n)-1;i>=0;i--)
#define For(i,n) for (int i=1;i<=(int)(n);i++)
#define Rep(i,n) for (int i=0;i<=(int)(n);i++)
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define all(x) (x).begin(),(x).end()
#define Max(a,b,c) max(a,max(b,c))
#define Min(a,b,c) min(a,min(b,c))
#define cmax(x,y) x=max(x,y)
#define cmin(x,y) x=min(x,y)
#define pii pair<int,int>
#define vi vector <int>
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define pb push_back
#define re return
#define se second
#define fi first
const int INF=0x7fffffff;
const int MAXN=400+3;

int n,a[MAXN];
ll f[MAXN][MAXN],s[MAXN];

ll dfs(int l,int r){
	if (f[l][r] || l==r) re f[l][r];
	f[l][r]=(ll)INFINITY;
	for (int i=l;i<r;i++){
		cmin(f[l][r],dfs(l,i)+dfs(i+1,r));
	}
	re f[l][r]+=s[r]-s[l-1];
}

int main(){
	cin>>n;
	For(i,n){
		cin>>a[i];
		s[i]=s[i-1]+a[i];
	}
	cout<<dfs(1,n)<<endl;
	re 0;
}

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

int main(){
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  
  int HP1 = a, HP2 = c;
  
  while(1){
    HP2 -= b;
    if(HP2 <= 0){
      cout << "Yes" << endl;
      break;
    }
    HP1 -= d;
    if(HP1 <= 0){
      cout << "No" << endl;
      break;
    }
  }
}

    
    
    
    

Measured memory_bytes: 3580
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
int main(){
	double a,b; cin >> a >> b;
	int cnt = 0;
	int h = 1;
	while(h < b){
		cnt++;
		h += (a-1);
	}
	cout << cnt << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>
#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000;/* 1e+9a */
namespace utils{

    class Repeater{
        int st;
        int ed;
        int d;
    public:
        class rep_iterator{
            int i; int d;
        public:
            rep_iterator(int x, int pd) :i(x), d(pd) {}

            int& operator*(){ return i; }
            bool operator==(const rep_iterator& k) const { return i == k.i; }
            bool operator!=(const rep_iterator& k) const { return i != k.i; }
            rep_iterator& operator++(){ i+=d; return *this; }
        };

        Repeater(int N) :st(0), ed(N), d(1) {}
        Repeater(int b, int e) :st(b), ed(e), d(1) {if(st > ed){ st--; ed--; d = -1; } }

        rep_iterator begin() const{ return rep_iterator(st, d); }
        rep_iterator end() const{ return rep_iterator(ed, d); }

    };

    template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>;
    template<class T> using min_queue = p_queue<T, greater<T>>;
    template<class T> using max_queue = p_queue<T, less<T>>;

    template<class T> bool min_update(T& X, const T& A){ if(X > A) {X = A; return true;} return false; }
    template<class T> bool max_update(T& X, const T& A){ if(X < A) {X = A; return true;} return false; }
#define ALL(x) begin(x), end(x)
#define rALL(x) rbegin(x), rend(x)
};
using namespace utils;


void sub_main(istream &cin) {
    ll N;
    cin >> N;
    if(!cin) return;
    vector<int> sumB, sumW;
    sumB.push_back(0);
    sumW.push_back(0);
    for (auto &&i : Repeater(0, N)) {
        char c;
        cin >> c;
        sumB.push_back(sumB.back() + (c == '#'));
        sumW.push_back(sumW.back() + (c == '.'));
    } // end i
    int mini = INF;
    for (auto &&i : Repeater(0, N + 1)) {
        mini = min(mini, sumB[i] + (sumW.back() - sumW[i]));
    } // end i
    cout << mini << endl;

}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef DEBUG
    cerr << "DEBUG MODE" << endl;
    string test_cases = "test_C.txt";
    auto fs = fstream(test_cases, fstream::in);
    int lp = 0;
    while(fs) {
        lp++;
        cout << lp <<  "#------\n";
        sub_main(fs);
    }
    cout << "------#" << endl;
    if(lp <= 1) sub_main(cin);
#else
    sub_main(cin);
#endif
    return 0;
}


Measured memory_bytes: 2036
#include <iostream>
#include <vector>
#define REP(i, a, n) for(int i = (a); i < n; i++)
using namespace std;
typedef pair<int, int> pii;

int N, P[800], D[800];
vector<pii> E[800];
bool used[800];


int dfs(int v, int d, int &p, int &m) {
  used[v] = true;
  if(d > m) m = d, p = v;
  int ret = d;
  REP(i, 0, E[v].size()) {
    int u = E[v][i].first;
    if(used[u] || E[u].size() == 1) continue;
    ret = max(ret, dfs(u, d + E[v][i].second, p, m));
  }
  return ret;
}

int radius() {
  int p = 0, m = 0;
  REP(i, 0, N) used[i] = false;
  dfs(0, 0, p, m);
  REP(i, 0, N) used[i] = false;
  int q = 0, n = 0;
  dfs(p, 0, q, n);
  return n;
}

int main(void) {
  while(cin >> N, N) {
    REP(i, 1, N) cin >> P[i];
    REP(i, 1, N) cin >> D[i];
    REP(i, 0, N) E[i].clear();
    REP(i, 1, N) {
      E[i].push_back(pii(P[i] - 1, D[i]));
      E[P[i] - 1].push_back(pii(i, D[i]));
    }

    int r = radius();
    int sum = 0, d = 0;
    REP(i, 1, N) sum += D[i];
    REP(i, 0, N) if(E[i].size() == 1) d += E[i][0].second;

    cout << ((sum - d) * 3 - r + d) << endl;
  }
  return 0;
}

Measured memory_bytes: 3264
//Daniel Grzegorzewski
#include <bits/stdc++.h>

#define MP make_pair
#define PB push_back
#define ST first
#define ND second

using namespace std;

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef long long LL;

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

bool prime(int x)
{
  if (x < 2)
    return false;
  for (int i = 2; i*i <= x; ++i)
    if (x%i == 0)
      return false;
  return true;
}

int n;
VI v;

int main() {
  init_ios();
  cin >> n;
  for (int i = 2; v.size() < n; ++i)
    if (prime(i) && (i%5) == 1)
      v.PB(i);
  for (int x: v)
    cout<<x<<" ";
  cout<<"\n";
}

Measured memory_bytes: 256
//Heaplax
//别让自己后悔
#include<bits/stdc++.h>
#define N 100001
#define int long long
#define LOG(x) cerr<<#x<<" = "<<x<<endl
#define add_edge(u,v) nxt[++cnt]=head[u],head[u]=cnt,to[cnt]=v
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
char ch;bool fs;void re(int& x)
{
	while(ch=getchar(),ch<33);
	if(ch=='-')fs=1,x=0;else fs=0,x=ch-48;
	while(ch=getchar(),ch>33)x=x*10+ch-48;
	if(fs)x=-x;
}
using namespace std;
int sum,w;
int n,cnt,a[N];
main()
{
	re(n);
	for(int i=1;i<=n;++i)
		re(a[i]),sum+=a[i];
	w=n*(n+1ll)/2;
	if(sum%w)
	{
		puts("NO");
		return 0;
	}
	sum/=w;
	a[0]=a[n];
	for(int i=n;i;--i)a[i]-=a[i-1];
	for(int i=1;i<=n;++i)
	{
		int x=sum-a[i];
		if(x<0 || x%n)
		{
			puts("NO");
			return 0;
		}
		x/=n;
		cnt+=x;
	}
	if(cnt!=sum)puts("NO");
	else puts("YES");
}
/*
a[i]+(n-1)x-(sum-x) =0
a[i]-sum+nx=0
nx=sum-a[i]
*/
/*
3 4 5  1 2
1 1 1 -4 1
*/


Measured memory_bytes: 1024
a=[1,3,5,7,8,10,12]
b=[4,6,9,11]
c=[2]
x,y=map(int,input().split())
if (x in a and y in a) or (x in b and y in b) or (x in b and y in b) == True:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3060
//「何らかの全域木からS--Tパス上の辺を取り除いたもの」を考えればよく、実は「MST - (S--Tパス上の最大コストの辺)」が答えになりそう。
#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;

struct UF {
	int par[4000];
	UF() { for (int i = 0; i < 4000; i++) par[i] = i; }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void unit(int x, int y) { x = root(x); y = root(y); par[x] = y; }
	bool isSame(int x, int y) { return root(x) == root(y); }
};

struct Edge {
	int from, to, cost;
	Edge(int from, int to, int cost) { this->from = from; this->to = to; this->cost = cost; }
	Edge() {}
	bool operator<(const Edge &r) const { return cost < r.cost; }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int n, m, q;
Edges edges;
UF uf;
Graph mst;
int parents[20][4000];
int depth[4000];
int maxScores[20][4000];

void dfs(int p, int v, int ecost, int dep) {
	parents[0][v] = p;
	depth[v] = dep;
	maxScores[0][v] = ecost;
	
	for (int i = 0; i < mst[v].size(); i++) {
		int nv = mst[v][i].to;
		if (nv == p) continue;
		dfs(v, nv, mst[v][i].cost, dep + 1);
	}
}

int maxPath(int u, int v) {
	int ret = 0, i;
	
	if (depth[u] > depth[v]) swap(u, v);
	if (depth[v] > depth[u]) {
		int d = depth[v] - depth[u];
		for (i = 0; i < 20; i++) {
			if ((d >> i) & 1) {
				ret = max(ret, maxScores[i][v]);
				v = parents[i][v];
			}
		}
	}
	if (u == v) { return ret; }
	if (parents[0][u] == parents[0][v]) {
		int res = max(maxScores[0][u], maxScores[0][v]);
		return max(ret, res);
	}
	
	for (i = 19; i > 0; i--) {
		if (parents[i][u] != parents[i][v]) break;
	}
	int res1 = maxPath(parents[i][u], parents[i][v]);
	int res2 = max(maxScores[i][u], maxScores[i][v]);
	int res = max(res1, res2);
	return max(ret, res);
}

signed main() {
	int i, j;
	
	cin >> n >> m;
	for (i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		edges.push_back(Edge(a, b, c));
	}
	sort(edges.begin(), edges.end());
	
	int mstCost = 0;
	mst.resize(n);
	for (i = 0; i < edges.size(); i++) {
		int u = edges[i].from;
		int v = edges[i].to;
		
		if (!uf.isSame(u, v)) {
			uf.unit(u, v);
			mstCost += edges[i].cost;
			mst[u].push_back(Edge(u, v, edges[i].cost));
			mst[v].push_back(Edge(v, u, edges[i].cost));
		}
	}
	
	dfs(0, 0, 0, 0);
	for (i = 1; i < 20; i++) {
		for (j = 0; j < n; j++) {
			parents[i][j] = parents[i - 1][parents[i - 1][j]];
			maxScores[i][j] = max(maxScores[i - 1][j], maxScores[i - 1][parents[i - 1][j]]);
		}
	}
	
	cin >> q;
	for (i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t; s--; t--;
		if (n <= 2) { cout << 0 << endl; continue; }
		int res = maxPath(s, t);
		cout << mstCost - res << endl;
	}
	return 0;
}

Measured memory_bytes: 14700
#![allow(unused_mut)]
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(unused_imports)]
#![allow(dead_code)]
use std::cell::RefCell;
use std::cmp::{max, min, Ordering};
use std::collections::*;
use std::fmt::{Debug, Display, Formatter, Write as FmtWrite};
use std::io::{stderr, stdin, BufRead, Write};
use std::mem::{replace, swap};
use std::ops::*;
use std::rc::Rc;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn gcd(a: u64, b: u64) -> u64 {
    if a == 0 { b }
    else { gcd(b % a, a) }
}

fn main() {
    input!{
        N: u64,
        A: [u64; N],
    }
    let mut nums = A;
    let mut ret = 0;
    if let Some((&head, tail)) = nums.split_first() {
        ret = tail.iter().fold(head, |a,&b| gcd(a,b));
    }
    println!("{}", ret);

}



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


int check(int a,int b,int c){
	if(a <= b && b < c){
		return 1;
	}else if(a > b && b >= c){
		return 1;
	}else{
		return 0;
	}
}

int main(){
	int n;
	cin >> n;
	int num[n];
	for(int i = 0;i < n;i++){
		cin >> num[i];
	}
	int ans = 0;
	for(int i =0;i < n-2;i++){
		ans += check(num[i],num[i+1],num[i+2]);
	}
	cout << ans;
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>

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

int A[100001];

bool end(int *arr, int n) {
  int a = 0;
  REP(i, n) a += arr[i];
  int max = *std::max_element(arr, arr + n);
  return max == a;
}

int main() {
  scanf("%d\n", &N);

  REP(i, N) scanf("%d ", &A[i]);

  while (!end(A, N)) {
    std::sort(A, A+N);
    int* min_ind = std::upper_bound(A, A + N, 0);
    REP(i, N) {
      if (A + i != min_ind) A[i] = A[i] % (*min_ind);
    }
  }

  printf("%d\n", *std::max_element(A, A + N));

  return 0;
}


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

int main(void){
    int X;
    cin >> X;
    if(X >= 30) cout << "Yes" << endl;
    else cout << "No" << endl;
     
    return 0;
}

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

#define ll long long
#define M 1000000007
#define deb(x) cout<<#x<<" "<<x<<endl;
#define foi(a,b) for(int i=a;i<b;i++)
#define foj(a,b) for(int j=a;j<b;j++)
#define fod(a,b) for(int i=a;i>b;i--)
#define MAX  1e9
#define MIN -1e9
#define nl cout<<"\n";
#define show(arr,a,b) for(int i=a;i<b;i++) cout<<arr[i]<<" "; cout<<endl;
#define  mp(a,b) pair<ll,ll> p(a,b)
#define pb push_back
typedef pair<ll,ll> p1;
typedef vector<ll> vecd;
typedef vector<string> vecsd;
typedef map<ll,ll> mpd;
typedef set<ll> setd;
typedef multiset<ll> msetd;
    ll dp[3002][3002];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s1,s2; cin>>s1>>s2;
    foi(0,s1.length()+1){
        foj(0,s2.length()+1)
        dp[i][j]=0;
    }
    foi(0,s1.length()+1){
        foj(0,s2.length()+1){
            if(i==0 || j==0) dp[i][j]=0;
            else{
                if(s1[i-1]==s2[j-1]){
                    dp[i][j]=1+dp[i-1][j-1];
                }
                else{
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
    }
    string ans="";
    ll i=s1.length(),j=s2.length();
    while(i>0 && j>0){
        if(dp[i][j]==(dp[i-1][j-1]+1) && s1[i-1]==s2[j-1]){
            ans=s1[i-1]+ans;
            i--;
            j--;
        }   
        else{
            if(dp[i][j]==dp[i-1][j]) i--;
            if(dp[i][j]==dp[i][j-1]) j--;
        }
    }
    cout<<ans<<"\n";
    // cout<<dp[s1.length()][s2.length()]<<"\n";
    return 0;
}


Measured memory_bytes: 70656
#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
const ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, ans, a, b, c, d, tmp, m, h, w, x, y, sum, k, q;

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

  cin >> n >> s;
  rep(i, 1000) {
    string coc = to_string(i), co = "";
    rep(j, 3 - Size(coc)) co += "0";
    co += coc;
    // Cout(co);
    ll le = -1, ri = -1;
    rep(j, n) {
      if (s[j] == co[0]) {
        le = j;
        break;
      }
    }
    rrrep(j, n - 1, 0) {
      if (s[j] == co[2]) {
        ri = j;
        break;
      }
    }
    if (le == -1 || ri == -1) continue;
    //if (co == "224") cout << le << " " << ri << "\n";
    rep(j, n) {
      if (s[j] == co[1]) {
        if (le < j && j < ri) {
          ans++;
          break;
        }
      }
    }
  }
  Cout(ans);
  return 0;
}

Measured memory_bytes: 512
n, m = map(int, input().split())

# ボール個数、赤いボールがある可能性を管理
b = [1] * n
red = [1] + [0] * (n - 1)

for i in range(m):
    x, y = map(int, input().split())

    # 移す元に赤いボールがある可能性があれば、移す先に赤いボールが渡る可能性がある
    if red[x - 1] == 1:
        red[y - 1] = 1

    # ボール数の足し引き
    b[x - 1] -= 1
    b[y - 1] += 1

    # xのボールが0になったら赤いボールがある可能性が消える
    if b[x - 1] == 0:
        red[x - 1] = 0

ans = len([x * y for x, y in zip(b, red) if x * y > 0])

print(ans)

Measured memory_bytes: 6116
from collections import deque

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

MAP = [input() for i in range(H)]
INF = 10000
DISTANCE = [[INF for i in range(W)] for j in range(H)]
DISTANCE[0][0] = 0
QUEUE = deque([(0,0)])
dxy = [(-1,0),(1,0),(0,-1),(0,1)]
while QUEUE :
    y,x = QUEUE.popleft()
    for dy,dx in dxy :
        ny = y+dy
        nx = x+dx
        if (ny < 0) or (nx < 0) or (ny >= H) or (nx >= W) :
            continue
        if MAP[ny][nx] == "#" :
            continue
        if DISTANCE[ny][nx] < INF :
            continue
        QUEUE.append((ny,nx))
        DISTANCE[ny][nx] = min(DISTANCE[ny][nx], DISTANCE[y][x]+1)

if DISTANCE[-1][-1] < INF :
    path = 1 + DISTANCE[-1][-1]
    not_score = 0
    for y in range(H) :
        not_score += MAP[y].count("#")
    ans = H*W - path - not_score
else :
    ans = -1
print(ans)


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

ll comb(int n, int a){
    ll x = 1, y = 1;
    rep(i, a){
        x *= n-i;
        y *= i+1;
    }
    return x / y;
}

int main() {
    int n;
    cin >> n;
    int a[n];
    rep(i, n) cin >> a[i];

    vector<int> same(n, 0);
    rep(i, n){
        same[a[i]-1]++;
    }
    // for(const auto& u: same){
    //     cout << u << endl;
    // }

    ll all = 0;
    rep(i, n){
        if(same[i] > 1) all += comb(same[i], 2); 
    }

    rep(i, n){
        ll ans = all - same[a[i]-1] + 1;
        cout << ans << " ";
    }


}

Measured memory_bytes: 4096
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int n = s.length();
		int ans = 0;
		for(int i=0;i<n-1;i++) {
			if (s.charAt(i) != s.charAt(i+1)) {
				ans++;
			}
		}
		System.out.println(ans);
	}

}


Measured memory_bytes: 13684
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
#define cool ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) 
#define ll long long
int main()
{ 
    cool;
    ll int amt,t=0,p=100,i;
    cin>>amt;
    
    while(1){
    	if(p>=amt){
    		break;
		}
		else{
			i=(p/100);
			p+=i;
			t++;
		}
	}
    cout<<t;
    return 0;
}


Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
long long DP[3001][3001],X[3001];
int N;
inline long long DFS(int S,int E)
{
	if(S>E)
		return 0;
	if(DP[S][E]!=-9099999999999999999)
		return DP[S][E];
	if((N-E+S-1)%2==0)
		return DP[S][E]=max(X[S]+DFS(S+1,E),X[E]+DFS(S,E-1));
	return DP[S][E]=min(DFS(S+1,E)-X[S],DFS(S,E-1)-X[E]);
}
int main()
{
	for(int i=0;i<3001;i++)
		for(int j=0;j<3001;j++)
			DP[i][j]=-9099999999999999999;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>X[i];
	cout<<DFS(0,N-1)<<endl;
	return 0;
}

Measured memory_bytes: 70912
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	static final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws Exception {

		// --- input ---
		String[] vals = in.readLine().split(" ");
		int num = Integer.parseInt(vals[0]);
		int price = Integer.parseInt(vals[1]);
		in.close();

		// --- resolv ---
		for(int sen=0; sen <= num; sen++) {
			for(int gosen=0; gosen <= num - sen; gosen++) {
				int man = num - sen - gosen;
				if(price == 10000 * man + 5000 * gosen + 1000 * sen) {
					System.out.println(man + " " + gosen + " " + sen);
					return;
				}
			}
		}
		System.out.println("-1 -1 -1");
	}
}


Measured memory_bytes: 22612
s = gets.chomp.to_s
k = gets.to_i

if k == 1 || s.chars.all?{|i| i == '1'}
  puts s[0]
else
  if s[0] != '1'
    puts s[0]
  else
    ans = '1'
    k.times do |i|
      if s[i] != '1'
        ans = s[i]
        break
      end
    end
    puts ans
  end
end

Measured memory_bytes: 1788
import sys
sys.setrecursionlimit(1000000)
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
G=[[]*n for i in range(n)]
P=[0]*n
V=[0]*n
R=[0]*n
for e in s[1:n]:
    a,b=map(int,e.split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
for e in s[n:]:
    c,d=map(int,e.split())
    P[c-1]+=d

def dsf(i,p):
    V[i]=1
    R[i]+=p+P[i]
    p=0
    if G[i]!=[]:
        for j in G[i]:
            if V[j]!=1:
                dsf(j,R[i])
dsf(0,0)
print(' '.join(map(str,R)))

Measured memory_bytes: 269236
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;

//typedef
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//conversion
inline LL toLong(string s) {LL v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define UNTIL(p) while(!(p))

//constant
const double EPS = 1e-5;
const double PI  = acos(-1.0);

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

#define PUTS(x) cout << (x) << endl;

LL INF = 10000000000;
LL dp[1005][1<<12] = {{0}};
LL keys[1005][2] = {{0}};

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    LL n,m;
    cin>>n>>m;
    VL a(m);
    VVL c(m);
    VL c_bit(m, 0);
    REP(i,m){
        LL b;
        cin>>a[i]>>b;
        REP(j,b){
            LL cj;
            cin>>cj;
            c[i].PB(cj);
            c_bit[i] |= 1<<(cj-1); 
        }
    }
    LL ans = INF;
    //REP(i,m){
    //    PUTS(c_bit[i]);
    //}
    //VL keys(m, 0);
    //LL full = (1 << n) - 1;
    //REP(i,m){
    //    REP(j,m){
    //        keys[j][0] |= c_bit[(i+j)%m];
    //        keys[j][1] += a[(i+j)%m];
    //    }
    //    REP(j,m){
    //        if(keys[j][0] == full){
    //            ans = min(ans, keys[j][1]);
    //        }
    //    }
    //}
    REP(i,1005)REP(j, 1<<12)dp[i][j] = INF;
    REP(i,m){
        dp[i+1][c_bit[i]] = min(dp[i+1][c_bit[i]], a[i]);
        FOR(j,0,1<<n){
            if(dp[i][j] != INF){
                dp[i+1][j] = min(dp[i+1][j],dp[i][j]);
                dp[i+1][j | c_bit[i]] = min(dp[i+1][j | c_bit[i]], dp[i][j] + a[i]);
            }
        }
    }
    //PUTS("---");
    REP(i,m){
        ans = min(ans, dp[i][(1<<n) - 1]);
    }
    ////ans = dp[m][(1<<n) - 1];
    //REP(j,m){
    //    REP(i,1<<n){
    //        cout<<(dp[j][i])<<" ";
    //    }
    //    PUTS("");
    //}
    if(ans == INF){
        PUTS(-1);
    }
    else{
        PUTS(ans);
    }
}

Measured memory_bytes: 32640
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

int main(){
    int n;
    cin >> n;
    vector<int> b(n);
    rep(i,n) cin >> b[i];

    vector<int> ans;
    for(int i = 0; i < n; i++){
        int k = b.size();
        for(int j = 0; j < b.size(); j++){
            if (b[j] == j+1) k = j;
        }
        if (k == b.size()){
            cout << -1 << endl;
            return 0;
        }
        ans.push_back(b[k]);
        b.erase(b.begin() + k );
    }
    reverse(all(ans));
    rep(i,n){
        cout << ans[i] << endl;
    }
    return 0;
}

Measured memory_bytes: 3584
n = int(raw_input())
s = raw_input()
ct = 0
mn = 0
for c in s:
    if c == '(':
        ct += 1
    else:
        ct -= 1
    mn = min(mn,ct)
l = abs(mn)
r = ct + l
s = '('*l + s + ')'*r
print s


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

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; }

#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl

ll f(ll year, ll month, ll day){
    ll count = 0;
    while(year!=1000){
        ll day_per_month;
        if(year%3==0){
            day_per_month = 20;
        }else{
            if(month%2==1){
                day_per_month = 20;
            }else{
                day_per_month = 19;
            }
        }

        day++;
        count++;

        if(day>day_per_month){
            day = 1;
            month++;
            if(month==11){
                year++;
                month = 1;
            }
        }
    }
    return count;
}

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

    // input
    ll N;
    cin >> N;

    while(N--){
        ll y, m, d;
        cin >> y >> m >> d;

        ll ans = f(y, m, d);
        p(ans);
    }
    
    return 0;
}


Measured memory_bytes: 3108
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

ll k, q;
ll d[5000];
ll e[5000];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> k >> q;
    for(int i = 0; i < k; i++) cin >> d[i];
    for(int i = 0; i < q; i++){
        int n, x, m;
        cin >> n >> x >> m;
        n--;
        for(int j = 0; j < k; j++) e[j] = d[j]%m;
        x %= m;
        ll sum = 0;
        ll neg = 0;
        for(int j = 0; j < k; j++) sum += e[j];
        for(int j = 0; j < k; j++){
            if(e[j] == 0) neg++;
        }
        ll rem = n%k;
        ll div = n/k;
        ll tot = x;
        neg*=div;
        tot += div*sum;
        for(int j = 0; j < rem; j++) tot += e[j];
        for(int j = 0; j < rem; j++) {
            if(e[j] == 0) neg++;
        }
        cout << n-tot/m-neg << endl;
    }
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

mint fact[200005];
void setFact(int n) {
    fact[0] = mint(1);
    rep(i, n) {
        fact[i+1] = fact[i]*(i+1);
    }
}
mint comb (int n, int k) {
    if (k<0 || k>n) {
        return mint(0);
    } 
    mint ans(1);
    if (k==0 || k==n) {
        return ans;
    }
    ans = fact[n]/(fact[k]*fact[n-k]);
    return ans;
}

vector<ll> to[200005];
mint data[200005];
int buka[200005];


void dfs(int n, int p) {
    if (to[n].size()==1 && to[n][0]==p) {
        data[n] = mint(1);
        buka[n] = 0;
        return;
    }
    for (int e: to[n]) {
        if (e==p) continue;
        dfs(e, n);
        buka[n] += buka[e] + 1;
    }
    data[n] = mint(1);
    int all = buka[n];
    for (int e:to[n]) {
        if (e==p) continue;
        data[n] *= comb(all, buka[e]+1);
        all -= (buka[e]+1);
        data[n] *= data[e];
    }
}

void bfs(int n, int p, int tot) {
    if (p!=-1) {
        mint temp = (data[p]/data[n])/(comb(tot-1, buka[n]+1));
        data[n] *= comb(tot-1, buka[n]);
        data[n] *= temp;
    }
    for (int e: to[n]) {
        if (e==p) continue;
        bfs(e, n, tot);
    }
}


int main(){
    ll n;
    cin >> n;
    setFact(n);
    
    rep(i, n-1) {
        ll a, b;
        cin >> a >> b;
        a--; b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    
    dfs(0, -1);
    bfs(0, -1, n);
    rep(i, n) {
        cout << data[i].x << endl;
    }

    
    return 0;
}


Measured memory_bytes: 18544
#include<bits/stdc++.h>
using namespace std;
long long int gcd(long long int A,long long int B){
  if(B>A){
    long long int R;
    R=B;
    B=A;
    A=R;
  }
  if(A%B==0) return B;
  else return gcd(A%B,B);
}

int main(){
  int N;
  cin >> N;
  long long int A[N];
  for (int i=0;i<N;i++) {
    cin >> A[i];
  }
  long long int ans=A[0];
  for (int i=1;i<N;i++){
    ans=gcd(ans,A[i]);
  }
  cout << ans;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  v[0] = 2;
  v[1] = 1;
  int now = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    swap(v[a], v[b]);
    if (v[a] == 2) {
      now = a;
    }
    if (v[b] == 2) {
      now = b;
    }
    if (now - 1 >= 0)
      v[now - 1] = 1;
    if (now + 1 < n)
      v[now + 1] = 1;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    if (v[i])
      ans++;
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}

int main()
{ll r,D,X;
cin>>r>>D>>X;
rep(m,10){
X=r*X-D;
cout<<X<<endl;}
    
}




Measured memory_bytes: 256
#include<stdio.h>
int main(){
  int a,b,c;
  int cnt=0;
  int i;
  scanf("%d %d %d",&a,&b,&c);
  for(i = a;i <= b;i++){
    if(c % i == 0){
      cnt++;
    }
  }
  printf("%d\n",cnt);
  return 0;
}
  
    



Measured memory_bytes: 2104