akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

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;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
template<class T,class Y>T LCM(T a,Y b){return (a*b)/GCD(a,b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

using vi=vector<int>;
using vii=vector<vi>;

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(),(a).end()
#define decimal(x) fixed<<setprecision(x)


int main(){
    int K,N;
    cin>>K>>N;
    vi A(N);
    rep(i,N)cin>>A[i];
    int M=0;
    rep(i,N){
        if(i==0){
            M=max(M,K+A[i]-A[N-1]);
        }
        M=max(M,A[i+1]-A[i]);
    }
    cout<<K-M<<endl;
    return 0;
}

//a=97,z=122

Measured memory_bytes: 1024
import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String n=sc.next();
    if(n.charAt(0)=='1') System.out.print(9);
    if(n.charAt(0)=='9') System.out.print(1);

    if(n.charAt(1)=='1') System.out.print(9);
    if(n.charAt(1)=='9') System.out.print(1);

    if(n.charAt(2)=='1') System.out.print(9);
    if(n.charAt(2)=='9') System.out.print(1);

    System.out.println();

  }
}


Measured memory_bytes: 21716
#include <iostream>

using namespace std;
long long z,x,c,zx;
int main()
{
    cin>>z>>x>>c;
    if(z==x && z!=c)zx=1;
    else if(z==c && z!=x)zx=1;
    else if(x==c && x!=z)zx=1;
    if(zx==1)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<cmath>
using namespace std;
#define INF 10e9
static const int MAX_N=10e5;
	int N;
	int dp[MAX_N+1],a[MAX_N+1];
int main(void){
		cin >> N;
	for(int i=1;i<=N;i++){
		cin >> a[i];
		dp[i]=INF;
	}
	dp[0]=dp[1]=0;
	dp[2]=abs(a[1]-a[2]);
	for(int i=3;i<=N;i++)
		dp[i]=min(dp[i-1]+abs(a[i-1]-a[i]),dp[i-2]+abs(a[i-2]-a[i]));
	cout << dp[N] << endl;
	return 0;
}

Measured memory_bytes: 4736
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
static const int MAX_H = 100;
static const int MAX_W = 8;
static const ll MOD = 1000000007;

int H, W, K;
ll memo[MAX_H + 1][MAX_W + 1];

int main(){
	scanf("%d %d %d", &H, &W, &K);
	memo[0][0] = 1LL;
	for(int i = 0; i < H; i++){
		for(int j = 0; j < (1 << W - 1); j++){
			bool flag = true;
			for(int k = 0; k < W - 2; k++){
				if(j & (1 << k) && j & (1 << (k + 1))) flag = false;
			}
			if(!flag) continue;
			for(int k = 0; k < W; k++){
				if(k == 0){
					if(j & 1) memo[i + 1][k + 1] += memo[i][k];
					else memo[i + 1][k] += memo[i][k];
				}else if(k == W - 1){
					if(j & (1 << (W - 2))) memo[i + 1][k - 1] += memo[i][k];
					else memo[i + 1][k] += memo[i][k];
				}else{
					if(j & (1 << (k - 1))) memo[i + 1][k - 1] += memo[i][k];
					else if(j & (1 << k)) memo[i + 1][k + 1] += memo[i][k];
					else memo[i + 1][k] += memo[i][k];
				}
			for(int k = 0; k < W; k++) memo[i + 1][k] %= MOD;
			}
		}
	}
	printf("%lld\n", memo[H][K - 1] % MOD);
	return 0;
}

Measured memory_bytes: 128
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
#include<limits.h>
#include<unordered_map>
#include<unordered_set>
#include<deque>
#include<cstdio>
typedef long long int ll;
const int INF = 1 << 30;
using namespace std;
#define pp pair<int,int>
#define FOR(i,a,n) for(int i=(ll)a;i<(ll)n;++i)
#define TFOR(i,n)FOR(i,0,n)
#define ALL(x) x.begin(),x.end()
int main() {
	int a, b, h;
	cin >> a >> b >> h;
	cout << (a + b)*h / 2 << endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<list>
#include<map>
#include<numeric>
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define ll long long

using namespace std;

int main() {
	int n;
	string s;
	cin >> n;
	cin >> s;
	rep(i, s.size()) {
		s[i] += n;
		if (s[i] > 90)s[i] -= 26;
	}
	
	cout << s << endl;
	return 0;
}


Measured memory_bytes: 256
n,k=map(int,input().split())
mod=10**9+7

fra=[1]*(2*n+2)
inv=[1]*(2*n+2)
t=1
for i in range(1,2*n+2):
    t*=i
    t%=mod
    fra[i]=t
t=pow(fra[2*n+1],mod-2,mod)
for i in range(2*n+1,0,-1):
    inv[i]=t
    t*=i
    t%=mod
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
if n<=k:
    print(comb(2*n-1,n))
    quit()
ans=0
for i in range(min(n,k)+1):
    ans+=comb(n,i)*comb(n-1,i)
    ans%=mod
print(ans)

Measured memory_bytes: 47340
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		double a[] = new double[N];
		double b = 0;

		for (int i = 0; i < a.length; i++) {
			a[i] = sc.nextDouble();
			b += 1f/a[i];
		}

		System.out.println(1/b);


	}
}


Measured memory_bytes: 23376
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s[n];
  for (int i = 0; i < n; i++) cin >> s[i];
  string res = "";
  for (int i = 0; i < n; i++) sort(s[i].begin(), s[i].end());
  for (char c = 'a'; c <= 'z'; c++) {
    int m = 100;
    for (int i = 0; i < n; i++) {
      int t = upper_bound(s[i].begin(), s[i].end(), c) - lower_bound(s[i].begin(), s[i].end(), c);
      m = min(m, t);
    }
    for (int i = 0; i < m; i++) res += c;
  }
  cout << res << "\n";
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define rep(x) for(int i=0;i<x;i++)
#define reprev(x) for(int i=x-1;i>=0;i--)
#define repdef(x,n) for(int i=x;i<n;i++)
#define Debug(x) cout<<x<<"\n";
#define Debug2(x,y) cout<<x<<" "<<y<<"\n";
#define TEST(t) while(t--)
#define mod 1000000007
#define MAX 100005
int fast_exp(ll base,ll exp){
    if(exp==1){
        return base%mod;
    }
    if(exp&1){
        int ans=fast_exp(base,exp-1);
        return (base%mod*ans%mod)%mod;
    }else{
        int ans=fast_exp(base,exp/2);
        return (ans%mod*ans%mod)%mod;
    }
}
int fast_exp_itr(ll base,ll exp,ll p){
    ll ans=1;
    base=base%p;
    while(exp>0){
        if(exp&1){
            ans=((ans%p)*(base%p))%p;
        }
        exp>>=1;
        base=((base%p)*(base%p))%p;
    }
    return ans;
}
int euclid_gcd(int a,int b){
    if(b==0){
        return a;
    }
    return euclid_gcd(b,a%b);
}
int main(int argc, char const *argv[])
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    int arr[n];
    rep(n){
    	cin>>arr[i];
    }
    int count=0;
    rep(n){
    	if(arr[i]>=k){
    		count++;
    	}
    }
    cout<<count<<"\n";
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
 
#define int long long int
#define ld long double
#define mod 1000000007 
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define forn(i,s, e) for (int i = s; i <= e; i++)
#define fornd(i,s, e) for (int i = s ; i >= e; i--)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define print(x) for(auto el: x) cout<<el<<" ";cout<<"\n"
#define PSET(x,y) fixed<<setprecision(y)<<x
#define bits(x) __builtin_popcount(x)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

 

 
template <typename T> 
void swap(T& a, T& b){
    T temp = a;
    a = b;
    b = temp;
}

const int N = 100005; 

void selfadd(int& a, int b){
    a = (a+b)%mod;
}

vector<int> visited;
vector<int> order;
vector<int> * adj;
vector<int> * adjT;
vector<int> dist;


void dfs(int a){
    visited[a] = 1;
    for(int to : adj[a]){
        if(visited[to]==0)
            dfs(to);
    }
    order.push_back(a);
}
void dfs2(int a, int l){

    visited[a] = 1;
    dist[a] = l;
    for(int to : adj[a]){
        if(visited[to]==0)
            dfs2(to,l+1);
        else if(dist[to] < l+1){
            dist[to] = l+1;
            // dfs2(to,l+1);
        }
    }
    // dist[a] 
}



void solve(){
    int n,m;
    cin>>n>>m;

    adj = new vector<int>[n];
    adjT = new vector<int>[n];
    forn(i,0,m-1){
        int a,b;
        cin>>a>>b;
        adj[a-1].pb(b-1);
        adjT[b-1].pb(a-1);
    }
    visited.assign(n,0);
    // dfs(0);
    for(int i=0;i<n;i++){
        if(visited[i]==0)
            dfs(i);
    }
    visited.assign(n,0);
    dist.assign(n,0);
    for(int i= n-1;i>=0;i--){
        if(visited[order[i]]==0){
            dfs2(order[i],0);
        }
    }

    for(int i= n-1;i>=0;i--){
        int u = order[i];
        for(int from : adjT[u]){
            dist[u] = max(dist[u], dist[from]+1);
        }
    }




    int ans = 0;

    for(int i=0;i<n;i++){
        if(dist[i] > ans)
            ans = dist[i];
    }

    cout<<ans<<endl;
}
 
int32_t main()
{
    fast;
    // int t;
    // cin>>t;
    // for(int y=1;y<=t;y++)
    {
        // cout<<"Case #"<<y<<": ";
        solve();
    }
    return 0;
} 

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

const int MOD = 1000000007;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define copy(from, to) copy(from.begin(), from.end(), back_inserter(to))
#define sort(a) sort(a.begin(), a.end())
#define reverse(s) reverse(s.begin(), s.end())
#define p(s) cout << (s) << endl

typedef long long ll;
using namespace std;

template<class T>
void load(vector<T> &v, int n) {
  for (int i = 0; i < n; ++i) {
    T t;
    cin >> t;
    v.push_back(t);
  }
}

template<class T>
T max(vector<T> &v) {
  return *max_element(v.begin(), v.end());
}

template<class T>
T min(vector<T> &v) {
  return *min_element(v.begin(), v.end());
}

void py(bool isUpper = false) {
  if (isUpper) {
    p("YES");
  } else {
    p("Yes");
  }
}

void pn(bool isUpper = false) {
  if (isUpper) {
    p("NO");
  } else {
    p("No");
  }
}

int main() {
  string s;
  cin >> s;

  vector<char> c;
  for (int i = 0; i < s.length(); ++i) {
    if (i % 2 == 0) {
      c.push_back(s[i]);
    }
  }

  p(string(c.begin(), c.end()));
}

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

int main()
{
	int K, S;
	cin >> K >> S;
	int ans = 0;
	for (int i = 0; i < K+1; i++){
		for (int j = 0; j < K+1; j++){
			int k = S - i - j;
			if (0 <= k and k <= K) ans++;
		}
	}

	cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
int dp[110000][2];
const int INF = 1e18;
void chmax(int &a,int b){
	if(a < b)a = b;
}
signed main(){
	int n;cin >> n;
	vector<int> a(n);rep(i,n)cin >> a[i];
	rep(i,110000)rep(j,2)dp[i][j] = -INF;
	dp[0][0] = 0;
	rep(i,n)rep(j,2){
		int b = a[i];
		if(j == 0){
			//not use 
			chmax(dp[i+1][0],dp[i][j]+b);
			//use
			chmax(dp[i+1][1],dp[i][j]-b);
		}
		else if(j == 1){
			//not use 
			chmax(dp[i+1][0],dp[i][j]-b);
			//use
			chmax(dp[i+1][1],dp[i][j]+b);

		}
	}
	cout << dp[n][0] << endl;
	// cout << max(dp[n][0],dp[n][1]) << endl;
	// rep(i,n+1)rep(j,2)trace(i,j,dp[i][j]);
	return 0;
}

Measured memory_bytes: 5900
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repi(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
typedef long long ll;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m;
  cin>>n>>m;
  priority_queue<ll> a;
  ll ans=0;
  rep(i,n){
    ll v;
    cin>>v;
    ans+=v;
    a.push(v);
  }
  rep(i,m){
    int v=a.top();
    a.pop();
    ans-=(v+1)/2;
    a.push(v/2);
  }
  cout<<ans;
  return 0;
}


Measured memory_bytes: 1400
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
    cout << fixed << setprecision(10);
	while(1){
		int n, m; cin >> n >> m;
		if(!n && !m) break;

		vector<int> sum(n, 0);
		vector<int> only(n, 0);
		for(int i=0; i<m; i++){
			int s, k;
			cin >> s >> k;
			for(int j=0; j<k; j++){
				int c; cin >> c;
				c--;
				sum[c] += s;
				if(k == 1){
					only[c] += s;
				}
			}
		}

		int smin = 1000*1000;
		int smax = -1;
		int top_idx = -1;
		for(int i=0; i<n; i++){
			if(smax < sum[i]){
				smax = sum[i];
				top_idx = i;
			} else if (smax == sum[i]){
				if(only[i] > only[top_idx]){
					top_idx = i;
				}
			}
		}
		for(int i=0; i<n; i++){
			if(i == top_idx) continue;
			smin = min(smin, only[i]);
		}

		int ans = smax - smin + 1;
		cout << ans << endl;

	}
	return 0;

}


Measured memory_bytes: 3172
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using bint = boost::multiprecision::cpp_int;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repSE(i,s,n) for (int i = (s); i < (n); i++)
#define ssort(v) sort(v.begin(), v.end())
#define gsort(v) sort(v.rbegin(), v.rend())
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }

int main()
{
    int n;
    cin >> n;
    vector<int> x(n);
    int sum = 0;
    rep(i,n) {
        cin >> x[i];
        sum += x[i];
    }
    int p = sum / n;
    int s1 = 0, s2 = 0;
    rep(i,n) {
        s1 += (x[i] - p) * (x[i] - p);
        s2 += (x[i] - p - 1) * (x[i] - p - 1);
    }
    cout << min(s1, s2) << "\n";
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main{
	public static int total(int t) {
		int sum=0;
		while(t!=0) {
			sum+=t%10;
			t/=10;
		}
		return sum;
	}

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int A=sc.nextInt();
		int B=sc.nextInt();
		int result=0;
		for(int i=1;i<=N;i++) {
			if(A<=total(i)&&total(i)<=B)
				result+=i;
		}
		System.out.println(result);
	}
}

Measured memory_bytes: 35632
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;
const int MOD = 1000000007;
const int INF = 2147483647;
const ld PI = 3.14159265358979323846;

//------------↓------------- M -------------- E ---------------- M --------------- O ---------------↓--------------//
// コンパイル 
// g++ -std=c++1z
//
// -------型変換--------
// int を string に変換
// string s = to_string(n);
// string を int に変換
// int n = stoi(s);
//
// -------二分探索---------
// k以上の値が最初に現れる時のイテレータ
// lower_bound(data.begin(), data.end(), k)
// kより大きい値が最初の現れる時のイテレータ O(logN)
// upper_bound(data.begin(), data.end(), k)
// kがdataに存在するかをtrue or falseで返す O(logN)
// binary_search(data.begin(), data.end(), k)
// 
//
//
//
//
//
// 
//------------↑------------- M -------------- E ---------------- M --------------- O ---------------↑--------------//




int main() {

    int N; cin >> N;
    ll x[N], y[N]; rep(i,N) cin >> x[i] >> y[i];
    ll z = x[0] + y[0];
    ll w = x[0] - y[0];
    ll z_mn = z; ll z_mx = z;
    ll w_mn = w; ll w_mx = w;

    rep(i,N) {
        z = x[i] + y[i];
        w = x[i] - y[i];
        z_mn = min(z_mn, z);
        z_mx = max(z_mx, z);
        w_mn = min(w_mn, w);
        w_mx = max(w_mx, w);
    }
    
    ll ans = max(z_mx - z_mn, w_mx - w_mn);
    cout << ans << endl;


    return 0;
}





Measured memory_bytes: 6700
#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using ll = long long;
const char newl = '\n';
#define var auto

void solver(int n) {
  struct edge {
    int to, cost;
    edge() {}
    edge(int to, int cost) : to(to), cost(cost) {}
  };

  vector< vector< edge > > G(n);
  vector< int > ps(n);
  vector< int > ds(n);
  vector< int > deg(n);
  for (int i = 1; i < n; i += 1) {
    cin >> ps[i];
    ps[i]--;

    deg[ps[i]]++;
    deg[i]++;
  }
  for (int i = 1; i < n; i += 1) {
    cin >> ds[i];
  }

  ll ans = accumulate(ds.begin(), ds.end(), 0ll);
  for (int i = 1; i < n; i += 1) {
    if (deg[i] == 1 || deg[ps[i]] == 1) continue;
    G[i].push_back(edge(ps[i], ds[i]));
    G[ps[i]].push_back(edge(i, ds[i]));
    ans += 2 * ds[i];
  }

  int st = -1;
  for (int v = 0; v < n; v += 1) {
    if (deg[v] >= 2) {
      st = v;
      break;
    }
  }

  int v = 0;
  vector< ll > dist(n);
  function< void(int, int) > dfs = [&](int v, int p) {
    for (auto e: G[v]) {
      int u = e.to;
      if (u == p) continue;
      dist[u] = dist[v] + e.cost;
      dfs(u, v);
    }
  };

  dist[st] = 0;
  dfs(st, -1);

  for (int v = 0; v < n; v += 1) {
    if (dist[v] > dist[st]) {
      st = v;
    }
  }

  dist = vector< ll >(n);
  dfs(st, -1);

  ans -= *max_element(dist.begin(), dist.end());

  cout << ans << endl;
}

signed main(){
  int n;
  while (cin >> n, n) {
    solver(n);
  }
}



Measured memory_bytes: 3208
#include<iostream>
#include<iomanip>
#include<cassert>
#include<math.h>
#include<complex>
#include<algorithm>
#include<utility>
#include<queue>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<unordered_map>
#include<functional>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF=2e18;
const ll MOD=1e9+7;

ll N;
int main(){
    cin>>N;
    for(ll n=1;n<=N;n++){
        ll ans=0;
        for(ll x=1;x*x<=n;x++){
            for(ll y=x;x*x+y*y<=n;y++){
                for(ll z=y;x*x+y*y+z*z<=n;z++){
                    if(x*x+y*y+z*z + x*y+y*z+x*z==n){
                        if(x==y && y==z)ans+=1;
                        else if(x==y || y==z)ans+=3;
                        else ans+=6;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 3520
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <map>
#include <functional>
#include <cstring>
#include <queue>
#include <cmath>
#include <deque>
#include <list>
#include <iomanip>
#include <fstream>
#include <string>
#include <iterator>
#include <set>
#include <bitset>
using namespace std;

typedef long double LD;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LD, LD> PDD;
typedef pair<LL, LL> PLL;
#define MP make_pair
#define PB push_back
#define X first
#define Y second

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, t) FOR(i,0,t)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())

const int MAXN = 100007;
const int MOD = 1000000007;
const int INF = 1000000000;
const LL LINF = INF * (LL)INF;
const LD PI = acos(-1.0);

bool R[MAXN];
int CNT[MAXN];

int main()
{
	ios_base::sync_with_stdio(0);
	//freopen("In.txt", "r", stdin);
	//freopen("hear.in", "r", stdin);
	//freopen("hear.out", "w", stdout);
	int n, m, ans = 0;
	cin >> n >> m;
	REP(i, n) CNT[i] = 1;
	R[0] = 1;
	REP(i, m)
	{
		int x, y;
		cin >> x >> y;
		--x; --y;
		--CNT[x]; ++CNT[y];
		if (R[x]) R[y] = 1;
		if (CNT[x] == 0) R[x] = 0;
	}
	REP(i, n) ans += R[i];
	cout << ans;
	return 0;
}

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

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define INF 1000000007LL
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(i=a;i<=b;i++)

#define cil(a,b) ( ((a)%(b) == 0)?((a)/(b)):((a)/(b)+1) )

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
typedef vector<pii> vii;

ll mypow(ll a, ll b) { if(b == 0) return 1; ll temp=mypow(a,b/2); temp=(temp*temp)%INF; if(b%2 == 1) temp=(a*temp)%INF; return temp; }
int mygcd(int a, int b) { if(b==0) return a; return mygcd(b,a%b); }
int cntBits(ll a) { int ret=0; while(a>0) { ret++; a/=2; } return ret; }

ll w[102],v[102];
ll ans[102][100010];


int main()
{
	ios::sync_with_stdio(0);
	int tt=1,tcase; 
	//cin >> tt;
	rep(tcase,1,tt)
	{
		ll n,W; cin >> n >> W;
		for(int i=1;i<=n;i++) cin >> w[i] >> v[i];
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=W;j++)
			{
				if(w[i]>j) ans[i][j]=ans[i-1][j];
				else ans[i][j]=max(v[i]+ans[i-1][j-w[i]], ans[i-1][j]);
			}
		}
		cout << ans[n][W] << "\n";
	}
	return 0;
}


Measured memory_bytes: 78464
#include <cstdio>
#include <iostream>
using namespace std;
long long k,a[100];
int n=50;
int main(){
	cin>>k;
	long long tmp;
	if(k%n)tmp=k/n+1;
	else tmp=k/n;
	for(int i=1;i<=n;++i)a[i]=n-1+tmp;
	for(int i=1;i<=tmp*n-k;++i){
		int t=1;
		for(int j=2;j<=n;++j)
		    if(a[j]>a[t])t=j;
		for(int j=1;j<=n;++j)
			if(j==t)a[j]-=n;
			else ++a[j];
	}
	printf("%d\n",n);
	for(int i=1;i<=n;++i)cout<<a[i]<<" ";
}

Measured memory_bytes: 256
N=int(input())
a=[None]*5
for i in range(5):
    a[i] =int(input())
def minarg(l):
    nl=len(l)
    mi=10**20
    idx=-1
    for i in range(nl):
        if l[i]<mi:
            mi=l[i]
            idx=i
    return (idx,mi)
import math
i_min,a_min = minarg(a)
print(math.ceil(N/a_min) + 4)

Measured memory_bytes: 61984
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
    string s;
    cin >> s;

    if( (s.size()%2 == 0)^(s.front() == s.back()) ) {
        cout << "Second";
    }
    else {
        cout << "First";
    }

    cout << endl;
    return 0;
}

Measured memory_bytes: 512
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv,
    std.functional, std.math, std.numeric, std.range, std.stdio, std.string,
    std.random, std.typecons, std.container;

ulong MAX = 1_000_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "y");
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);

void main()
{
    long h, n;
    scan(h, n);
    auto a = aryread();
    writeln(a.sum() >= h ? "Yes" : "No");
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}


Measured memory_bytes: 3864
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, c, d;

    cin >> a >> b >> c >> d;

    if (ceil((double)c/b) > ceil((double)a/d)) cout << "No" << endl;
    else cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;

/*

*/

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt() : x(0) { }
	ModInt(signed sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }

	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }

	ModInt inverse() const {
		signed a = x, b = MOD, u = 1, v = 0;
		while (b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if (u < 0) u += Mod;
		ModInt res; res.x = (unsigned)u;
		return res;
	}
};
typedef ModInt<1000000007> mint;

vector<mint> fact, factinv;
void nCr_computeFactinv(int N) {
	N = min(N, mint::Mod - 1);
	fact.resize(N + 1); factinv.resize(N + 1);
	fact[0] = 1;
	reg(i, 1, N) fact[i] = fact[i - 1] * i;
	factinv[N] = fact[N].inverse();
	for (int i = N; i >= 1; i --) factinv[i - 1] = factinv[i] * i;
}
mint nCr(int n, int r) {
	if (n >= mint::Mod)
		return nCr(n % mint::Mod, r % mint::Mod) * nCr(n / mint::Mod, r / mint::Mod);
	return r > n ? 0 : fact[n] * factinv[n - r] * factinv[r];
}
mint nHr(int n, int r) { return r == 0 ? 1 : nCr(n + r - 1, r); }

mint ps[100010],qs[100010];
ll n,a,b,d;
mint ans,sum,p,q,c;

void init(){
	cin>>n>>a>>b>>d;
	p=a;
	p/=(a+b);
	q=b;
	q/=(a+b);
	c=100;
	c/=(a+b);
	nCr_computeFactinv(2*n);
	ps[0]=1;
	qs[0]=1;
	reg(i,1,n){
		ps[i]=ps[i-1]*p;
		qs[i]=qs[i-1]*q;
	}
}

int main(void){
	init();
	sum=1;
	rep(i,2*(n-1)+1){
		ans+=sum;
		sum*=p+q;
		if(i>=n-1){
			sum-=nCr(i,i-(n-1))*ps[n-1]*qs[i-(n-1)]*p;
			sum-=nCr(i,i-(n-1))*qs[n-1]*ps[i-(n-1)]*q;
		}
		// cout<<sum.get()<<endl;
	}
	ans*=c;
	cout<<ans.get()<<endl;
	return 0;
}

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

#define ll long long int
#define oo (1L<<62)

int Items, Cap;
ll memo [105][100005];
int v[105];
int p[105];

ll mochila(int item, int cap){
	if(cap<0) return -oo;
	if(!cap || item == -1) return 0;
	if(memo[item][cap]) return memo[item][cap];
	return memo[item][cap] = max(mochila(item-1, cap),
								v[item]+mochila(item-1, cap-p[item]));
}

int main() {
	cin>>Items>>Cap;
	for(int i = 0; i<Items; i++)
		cin>>p[i]>>v[i];
	cout<<mochila(Items-1, Cap)<<"\n";
	return 0;
}

Measured memory_bytes: 74888
#include<stdio.h>

int main(){
  int i;
  int num[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
  int m,d,ans;

  while(scanf("%d %d",&m,&d) && (m||d)){
    ans = 0;
    for(i=0;i<m-1;i++)ans += num[i];
    ans+=d;
    if(ans%7==0)printf("Wednesday\n");
    if(ans%7==1)printf("Thursday\n");
    if(ans%7==2)printf("Friday\n");
    if(ans%7==3)printf("Saturday\n");
    if(ans%7==4)printf("Sunday\n");
    if(ans%7==5)printf("Monday\n");
    if(ans%7==6)printf("Tuesday\n");
  }
  return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <set>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define sz(x) int(x.size())

typedef long long ll;

using namespace std;

static const int MAX = 100001;

/******* c++14 don't include *******/

template<typename T>
T gcd(T a, T b){if( b == 0)return a; return gcd(b, a%b); }
template<typename T>
T lcm(T a, T b){return a*b/gcd(a,b); }

/*******                      *******/

int main(int argc, const char * argv[]) {
    // input from txt
    
    /////////
    //write//
    /////////
    
    string s;
    cin >> s;
    
    bool humiyasui = true;
    rep(i, s.size()){
        int u = i + 1;
        if(u%2==1){
            //kisu
            if(s[i] == 'L'){
                humiyasui = false;
                break;
            }
        }
        else{
            if(s[i] == 'R'){
                humiyasui = false;
                break;
            }
        }
    }
    
    if(humiyasui)cout << "Yes";
    else cout << "No";
    cout << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<complex>
#include<algorithm>
#include<vector>
#include<math.h>
#include<set>
#include<map>
#include<queue>

int main()
{
	int n, m, x, a, sum{};
	scanf("%d%d%d", &n, &m, &x);
	for (int i = 0; i < m; i++)
	{
		scanf("%d", &a);
		if (a > x) sum++;
	}
	printf("%d\n", std::min(sum, m - sum));

	return 0;
}

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

#define LL long long
#define VI vector<int>
#define VB vector<bool>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }

// 4近傍（右, 下, 左, 上）
const std::vector<int> dy = { 0, -1, 0, 1 };
const std::vector<int> dx = { 1, 0, -1, 0 };

using namespace std;

int main() {
    int N, Ma, Mb;
    cin >> N >> Ma >> Mb;
    vector<int> A(N), B(N), C(N);
    FOR(i, 0, N) {
        cin >> A[i] >> B[i] >> C[i];
    }

    vector<vector<vector<int>>> dp(N + 1, vector<vector<int>>(500, vector<int>(500, INT_MAX)));
    dp[0][0][0] = 0;
    FOR(i, 0, N) {
        int a = A[i];
        int b = B[i];
        int c = C[i];

        FOR(j, 0, dp[0].size()) {
            FOR(k, 0, dp[0][0].size()) {
                if (dp[i][j][k] == INT_MAX) { continue; }

                // not use
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k]);

                // use
                dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c);
            }
        }
    }

    int ans = INT_MAX;
    FOR(a, 1, dp[0].size()) {
        FOR(b, 1, dp[0][0].size()) {
            if (a * Mb == b * Ma) {
                ans = min(ans, dp[N][a][b]);
            }
        }
    }

    cout << (ans == INT_MAX ? -1 : ans) << endl;

    return 0;
}

Measured memory_bytes: 42112
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define PI 3.141592653589793238
#define all(a) a.begin(),a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
typedef complex<double> com;
template<class T, class S> bool chmax(T &a, const S &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T, class S> bool chmin(T &a, const S &b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll n, ll m) { return (m ? gcd(m, n%m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m)*m; }


signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	//modcalc();

	int n;
	cin >> n;
	int ans = 0;
	ans += n / 500 * 1000;
	n %= 500;
	cout << ans + n / 5 * 5 << endl;
}

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

using namespace std;

void rec(string &s, int &a, int &b) {
  if (s == "") return;
  if (s.back() == 'h') {
    a -= (a > 0);
    s = s.substr(0, s.size() - 5);
  } else {
    a += (a < b);
    s = s.substr(0, s.size() - 4);
  }
  a <<= 1, b <<= 1;
  rec(s, a, b);
}

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

int main(void) {
  string s;
  while (cin >> s, s != "#") {
    int a = 0, b = 1;
    rec(s, a, b);

    a *= 90;
    int g = gcd(a, b);
    a /= g, b /= g;
    if (a == 0) {
      cout << 0 << endl;
    } else if (b == 1) {
      cout << a << endl;
    } else {
      cout << a << "/" << b << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 3072
void main()
{
    int n = readln.chomp.to!int;
    int[] a = readln.split.to!(int[]);
    a[] -= 1;

    int cnt;
    foreach (i; 0 .. n)
    {
        if (i == a[a[i]]) ++cnt;
    }

    writeln(cnt / 2);
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;

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

int mn[500010],n;
char s[500010];

inline int rd()
{
	int x=0;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar());
	for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x;
}

int main()
{
	scanf("%s",s+1);
	n=strlen(s+1)+1;
	for (int i=2;i<=n;i++) if (s[i-1]=='<') mn[i]=mn[i-1]+1;
	for (int i=n-1;i;i--) if (s[i]=='>') mn[i]=max(mn[i],mn[i+1]+1);
	ll ans=0;
	for (int i=1;i<=n;i++) ans+=mn[i];
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 2688
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long 
int main() 
{ 
    // added the two lines below 
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    float D,T,S;
    cin>>D>>T>>S;
    if(D/S<=T)
       cout<<"Yes"<<"\n";
    else
	   cout<<"No"<<"\n";
	return 0;
}

Measured memory_bytes: 3708
a,b,c,x,y=map(int,input().split())
if x>y:
  ans=min(a*(x-y)+c*2*y,c*2*x)
else:
  ans=min(b*(y-x)+c*2*x,c*2*y)
ans=min(ans,a*x+b*y)

print(ans)


Measured memory_bytes: 38384
#include <stdio.h>

int main()
{
  int x,y,i;
  int count =0;

  scanf("%d %d",&x,&y);

  for(i=0; i<=x; i++)
   {
     if(y == i*2 + (x-i)*4) {
       count++;
     }
   }

  if(count != 0)
    printf("Yes\n");
  else
    printf("No");

  return 0;
}


Measured memory_bytes: 2120
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL<<61;
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; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P){if(P==0) return 1;
  if(P%2==0){ll t = jou(N, P/2);return t*t % mod;
  }return (N * jou(N, P-1)) % mod;}  //jou(n,mod-2)で逆元
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる




int main(){
  ll n,l,t;
  cin>>n>>l>>t;

  vector<ll> to;
  vector<ll> han;
  ll now=0;
  rep(i,n){
    ll x,w;
    cin>>x>>w;
    if(x==0 && w==2 && n>1) now=1; 
    if(w==1) to.pb(x);
    else han.pb(x);
  }

  vector<ll> owa;
  rep(i,to.size()){
    owa.pb((to[i]+t)%l);
    now-=(t+to[i])/l;
  }

  rep(i,han.size()){
    owa.pb(((han[i]-t)%l+l)%l);
    if(han[i]==0) now+=t/l;
    else now+=(l-han[i]+t-1)/l;
  }

  sort(all(owa));

  now%=n;
  if(now<0) now+=n;

  rep(i,n){
    ll x=n-now+i;
    if(x<n) cout << owa[x] << endl;
    else cout << owa[x-n] << endl;
  }


}

Measured memory_bytes: 5440
#include <iostream>
using namespace std;
typedef long long ll;
ll n,m,MOD=998244353,a,b,ans,bik[300001],A[300001],B[300001];
ll f(ll x,ll y){
    if(y==0)return 1;
    if(y%2==0)return f(x,y/2)*f(x,y/2)%MOD;
    return x*f(x,y-1)%MOD;
}
ll comb(ll x, ll y){
    return bik[x]*f(bik[x-y],MOD-2)%MOD*f(bik[y],MOD-2)%MOD;
}
int main(void){
    bik[0]=1;
    for(ll i=1;i<=300000;i++){
        bik[i]=bik[i-1]*i;
        bik[i]%=MOD;
    }
    cin>>n>>a>>b>>m;
    for(ll i=0LL;i<=min(n,(m/a));i++){
        A[i]=comb(n,i);
    }
    for(ll i=0LL;i<=min(n,(m/b));i++){
        B[i]=comb(n,i);
    }
    for(ll i=0LL;i<=min(n,(m/a));i++){
        if((m-i*a)%b==0&&(m-i*a)/b<=n){
            ans+=A[i]*B[(m-i*a)/b]%MOD;
            ans%=MOD;
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 7296
from collections import deque

n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(m)]
g=[[] for _ in range(n+1)]

for a,b in ab:
    g[a].append(b)
    g[b].append(a)

q=deque([(1,0)])
visit=[0]*(n+1)

while len(q)>0:
    u,cnt=q.popleft()

    visit[u]=1

    if u==n and cnt==2:
        print('POSSIBLE')
        exit(0)
    
    for v in g[u]:
        if visit[v] or cnt==2:
            continue

        q.append((v,cnt+1))

print('IMPOSSIBLE')

Measured memory_bytes: 140808
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll,ll> pll;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
#define MOD 1000000007
#define ARRAY_MAX 100005

const int INF = 1e9+7;


int main(){

    int n,m;
    while(cin >> n >> m,n|m){

        vector<int> h(n+10,0),w(m+10,0);
        vector<int> hs(n+10,0),ws(m+10,0);
        for(int i = 0;i < n;i++){
            cin >> h[i];
        }
        for(int i = 0;i < m;i++){
            cin >> w[i];
        }
        for(int i = 1;i <= n;i++){
            hs[i] = hs[i-1] + h[i-1];
        }
        for(int i = 1;i <= m;i++){
            ws[i] = ws[i-1] + w[i-1];
        }

        map<int,int> H,W;
        for(int i = 0;i <= n;i++){
            for(int j = i+1;j <= n;j++){
                H[hs[j]-hs[i]]++;
            }
        }
        for(int i = 0;i <= m;i++){
            for(int j = i+1;j <= m;j++){
                W[ws[j]-ws[i]]++;
            }
        }
        int ans = 0;
        for(auto itr:H){
            ans += H[itr.first] * W[itr.first];
        }
        cout << ans << endl;
        
    }



    return 0;
}


Measured memory_bytes: 34268
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector> 
#include <numeric>
#include <cmath>
#include <queue>


#define CHMAX(a,b) a = std::max(a,b)
#define CHMIN(a,b) a = std::min(a,b)
#define CHABS(a) a = std::abs(a) 
#define COUT(a) std::cout << a << std::endl
#define CERR(a) std::cerr << a << std::endl
#define FOR(n) for(lli i = 0; i < n; i++)

using namespace std;
using lli = long long int;
using pll = pair<lli, lli>;
using tlll = tuple<lli, lli, lli>;

lli mod197 = 1000000007LL;

// ax + by = gcd(a,b) 最大公約数
template< typename T >
T extgcd(T a, T b, T& x, T& y) {
	T d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1;
		y = 0;
	}
	return d;
}

lli X[30][30];

int main(void) {

	lli N, D;
	cin >> N >> D;
	FOR(N) {
		for (int j = 0; j < D; j++) {
			cin >> X[i][j];
		}
	}

	lli ans = 0;

	FOR(N) {
		for (int j = i + 1; j < N; j++) {
			lli sum = 0;
			for (int k = 0; k < D; k++) {
				sum += pow(X[i][k] - X[j][k],2);
			}
			if (sum == (lli)sqrt(sum) * (lli)sqrt(sum)) {
				ans++;
			}
		}
	}

	COUT(ans);

	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <functional>
#include <queue>
#include <unordered_set>
#include <climits>
#include <set>
#include <list>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

// https://ei1333.github.io/luzhiled/snippets/math/divisor.html
vector< int > divisor(int n) {
	vector< int > ret;
	for(int i = 1; i * i <= n; i++) {
		if(n % i == 0) {
			ret.push_back(i);
			if(i * i != n) ret.push_back(n / i);
		}
	}
	sort(begin(ret), end(ret));
	return (ret);
}

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

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

int main()
{
	int N;
	int A[1001001];
	cin >> N;
	for(int i = 0; i < N; i++){
		cin >> A[i];
	}
	
	bool s1[1001001];
	for(int i = 0; i < 1001001; i++){
		s1[i] = false;
	}
	
	int gcd = A[0];
	
	bool isPairwiseCoprime = true;
	for(int i = 0; i < N; i++){
		map<int, int> div = prime_factor(A[i]);
		for(auto it = div.begin(); it != div.end(); it++){
			int num = (*it).first;
			if(num == 1){
				continue;
			}
			if(s1[num]){
				isPairwiseCoprime = false;
			}else{
				s1[num] = true;
			}
		}
		
		if(gcd > 1){
			gcd = myGCD(gcd, A[i]);
		}
	}
	
	if(isPairwiseCoprime){
		cout << "pairwise coprime" << endl;
	}else if(gcd == 1){
		cout << "setwise coprime" << endl;
	}else{
		cout << "not coprime" << endl;
	}
	
	return 0;
}


Measured memory_bytes: 8508
#include <iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int main(){
    int n,t,dp[100010]={},h[100010];
    cin>>n>>t;
    for(int i=0;i<n;i++){
        cin>>h[i];
    }
    dp[0]=0;
    for(int k=0;k<t;k++){
        dp[k+1]=abs(h[k+1]-h[0]);
    }
    for(int i=t;i<n;i++){
        for(int k=1;k<=t;k++){
            if(k==1){
                dp[i]=dp[i-k]+abs(h[i]-h[i-k]);
            }else{
                dp[i]=min(dp[i],dp[i-k]+abs(h[i]-h[i-k]));
            }
        }
    }
    cout<<dp[n-1]<<endl;
}


Measured memory_bytes: 1024
import java.util.Scanner;

class Problem {
    private int A;
    private int B;
    private int C;

    Problem() {
        Scanner sc = new Scanner(System.in);
        A = sc.nextInt();
        B = sc.nextInt();
        C = sc.nextInt();
    }

    int solve() {
        return Math.min(B / A, C);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Problem().solve());
    }
}


Measured memory_bytes: 23252
#include <stdio.h>
int main (){
  int n,a[100],i,k,t,sum,Sum,min,max;
  
  while (1){
    scanf("%d",&n);
    if(n==0)break;
    sum=0;
    for (i=0;i<n;i++){
      scanf("%d",&a[i]);
      sum=sum+a[i];
    }
    if(a[0]>a[1]){
      k=a[0];
      a[0]=a[1];
      a[1]=k;
    }
    max=a[1];
    for (i=1;i<n;i++){
      if(max<a[i]){
	k=max;
	max=a[i];
	a[i]=k;
      }
    }
    if (a[n-1]<a[n-2]){
      k=a[n-1];
      a[n-1]=a[n-2];
      a[n-2]=k;
    }
    min=a[n-2];
    for (i=n-2;i>=0;i--){
      if(min>a[i]){
	k=min;
	min=a[i];
	a[i]=k;
      }
    }
    Sum=sum-(min+max);
      printf("%d\n",Sum/(n-2));
  }
  
  return 0;
}
      

Measured memory_bytes: 600
n=int(input())
from itertools import combinations_with_replacement as comb_rplc
from itertools import permutations 
adj=[0]*10**5
for x,y,z in comb_rplc(range(1, 100), 3):
  a=x**2+y**2+z**2+x*y+z*x+y*z
  if x==y and x==z:
    adj[a]+=1
  elif x==y or y==z or x==z:
    adj[a]+=3
  else:
    adj[a]+=6
for i in range(1,n+1):
  print(adj[i])

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

int main(void) {

  int n;
  cin >> n ;
  vector<int> a(n) ;
int cnt =0;
  for(int i= 0; i <n;i++){
    cin >> a[i];
    if(a[i] == i +1){}
    else{cnt++;}
  }
  
  if (cnt == 0 || cnt == 2){cout << "YES";}
  else cout << "NO";
  
  return 0;
 
}



Measured memory_bytes: 256