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

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		int[][] arr1 = new int[n][m];
		for(int i=0;i<n;i++) {
			for(int j=0;j<m;j++) {
				arr1[i][j] = scanner.nextInt();
			}
		}
		int[] arr2 = new int[m];
		for(int i=0;i<m;i++) {
			arr2[i] = scanner.nextInt();
		}
		scanner.close();
		int[] arr3 = new int[n];
		for(int i=0;i<n;i++) {
			arr3[i] = 0;
			for(int j=0;j<m;j++) {
				arr3[i] += arr1[i][j]*arr2[j]; 
			}
			System.out.println(arr3[i]);
		}
	}
}


Measured memory_bytes: 40788
#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
#define MAX_N 100001
#define INF 10E15 + 1
#define REP(i,COUNT) for(int i=0;i<(int)(COUNT);i++)
struct edge{
  long long int to, cost;
};
typedef vector<vector<edge> > AdjList;
AdjList graph;
AdjList graph2;
typedef pair<long long int, long long int> P;
void init(int n);
int find(int n);
void unite(int x,int y);
bool same(int x, int y);
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
vector<long long int> dist;
void dijkstra(int n,long long int s){
  dist = vector<long long int>(n,INF);
  dist[s] = 0;

  priority_queue<P, vector<P> , greater<P> > que;
  que.push(P(0,s));

  while(!que.empty()){
    P p = que.top();
    que.pop();
    int v = p.second;
    if(dist[v] < p.first)
      continue;
    REP(i,graph[v].size()){
      edge e = graph[v][i];
      if(dist[e.to] > dist[v] + e.cost){
        dist[e.to] = dist[v] + e.cost;
        que.push(P(dist[e.to],e.to));
      }

    }

  }
}

int main()
{
  long long int money = 1.0E15,res[100001];
  long long int u,v,n,m,s,t,a,b,distt[100001];
  cin >> n >> m >> s >> t;
  graph = AdjList(n);
  graph2 = AdjList(n);
  REP(i,m){
    cin >> u >> v  >> a >> b;
    graph[u-1].push_back((edge){v-1,a});
    graph[v-1].push_back((edge){u-1,a});
    graph2[u-1].push_back((edge){v-1,b});
    graph2[v-1].push_back((edge){u-1,b});
  }
  dijkstra(n,s-1);
  REP(i,n)distt[i] = dist[i];
  graph = graph2;
  dijkstra(n,t-1);
  res[n-1] = distt[n-1] + dist[n-1];
  for(int i=n-2;i>=0;i--)
    res[i] = min(distt[i] + dist[i],res[i+1]);
  REP(i,n)
    cout << money - res[i] << endl;
  return 0;
}

int par[MAX_N];
int ranks[MAX_N];

//n要素で初期化
void init(int n){
  REP(i,n){
    par[i] = i;
    ranks[i] = 0;
  }

}

//木の根を求める
int find(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return ;
  if(ranks[x] < ranks[y]){
    par[x] = y;
  }else{
    par[y] = x;
    if(ranks[x] == ranks[y]) ranks[x]++;
  }
}

bool same(int x, int y){
  return find(x) == find(y);
}



Measured memory_bytes: 19364
#include<cstdio>
#include<cstring>
#include<algorithm>
int main()
{
	int n,k,i,j;
	int dp[100005];
	int h[100005];
	scanf("%d %d",&n ,&k);
	for(i = 1;i <= n;i++)
		scanf("%d",&h[i]);
	memset(dp,0x3f,(n + 1) * sizeof(dp[0]));
	dp[1] = 0;
	for(i = 1;i <= n;i++)
	{
		for(j = 1;j <= k;j++)
		{
			if(dp[i + j] >= dp[i] + abs(h[i] - h[i + j]))
				dp[i + j] = dp[i] + abs(h[i] - h[i + j]);
		}
	}
	printf("%d\n",dp[n]);
	return 0;
}

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

int main()
{
    int N, K;
    cin >> N >> K;
    //  int64_t hmax = 0, hmin = 1e10;
    vector<int64_t> H(N), A(K);
    rep(i, N)
    {
        cin >> H[i];
    }
    sort(H.begin(), H.end());
    int64_t ans = 1e10;
    //    int A[K];
    for (int64_t i = 0; i < N - K + 1; i++)
    {
        int64_t htmp1 = 0, htmp2 = 1e10;
        for (int64_t j = i; j < K + i; j++)
        {
            //        cout << "i=" << i << ",j=" << j << endl;
            //          if(j==0){
            A[j - i] = H[j];
            //        }
            //      else{}
            //          cout << htmp1 << " " << htmp2 << endl;
        }
        //      cout << htmp1 << " " << htmp2 << endl;
        //        sort(A.begin(), A.end());
        ans = min(ans, A[K - 1] - A[0]);
        if (ans == 0)
            break;
    }

    cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(100000);
  for(int i = 1; i < 200; i++) {
    for(int j = 1; j < 200; j++) {
      for(int k = 1; k < 200; k++) {
        int ans = i*i+j*j+k*k+i*j+j*k+k*i;
        if(ans < 100000) {
          A.at(ans-1)++;
        }
      }
    }
  }
  for(int i = 0; i < N; i++) {
    cout << A.at(i) << endl;
  }
}


Measured memory_bytes: 3708
#include "bits/stdc++.h"
using namespace std;
using ll = long long; using ull = unsigned long long;
//#define int ll
using vb = vector<bool>; using vvb = vector<vb>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<V<T>>;
#define fi first
#define se second
#define maxs(x,y) (x=max(x,y))
#define mins(x,y) (x=min(x,y))
using pii = pair<int,int>; using pll = pair<ll,ll>;
#define FOR(i,a,b) for(ll i = (a); i < (ll)(b); ++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(ll i = (ll)(b)-1;i >= a;--i)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define eb(val) emplace_back(val)
const double PI = acos(-1);
const double EPS = 1e-10;
const ll MOD = 1e9+7;
void cioacc(){//accelerate cin/cout
  cin.tie(0);
  ios::sync_with_stdio(false);
}
signed main(){
  string s;
  cin >> s;
  int a=0,b=0;
  REP(i,s.size()){
    if(s[i]==i%2+'0') a++;
    else b++;
  }
  cout << min(a,b) << endl;
}

Measured memory_bytes: 512
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			int n = sc.nextInt();
			if(n == 0) break;
			if(n == 1) {
				System.out.println("deficient number");
				continue;
			}
			int sum = 1;
			
			for(int i = 2; i * i <= n; i++) {
				if(n % i == 0) {
					sum += i;
					if(i * i != n) {
						sum += n/i;
					}
				}
			}
			if(sum < n) {
				System.out.println("deficient number");
			}
			else if(sum == n) {
				System.out.println("perfect number");
			}
			else {
				System.out.println("abundant number");
			}
			
			
			
			
		}
	}
}

Measured memory_bytes: 18112
#include <iostream>
#include <set>
#include <vector>
using namespace std;
using ll = long long;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll vec_lcm(vector<ll> &a) {
  if (a.size() == 1)
    return a[0];
  ll ans = lcm(a[0], a[1]);
  if (a.size() == 2)
    return ans;
  for (int i = 2; i < a.size(); i++) {
    ans = lcm(ans, a[i]);
  }
  return ans;
}

int lnd2(ll a) {
  int res = 0;
  while (true) {
    if (a % 2 == 0) {
      res++;
      a /= 2;
    } else {
      break;
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  ll m;
  cin >> m;
  vector<ll> a(n);
  set<int> lnd2a;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    // X = a*(k+0.5) = (a/2)*(2p+1)と読み替えると、
    // 2で割れる回数が変わらないことがわかる
    lnd2a.insert(lnd2(a[i]));
    a[i] /= 2;
  }
  if (lnd2a.size() != 1) {
    // 2で割れる回数が同じでなかったら、半公倍数はできない
    cout << 0 << endl;
    return 0;
  }
  ll l = vec_lcm(a);
  ll ans = m / l - m / (2 * l);
  cout << ans << endl;
}


Measured memory_bytes: 1152
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = Integer.parseInt(sc.next());
        int n = Integer.parseInt(sc.next());
        List<Integer> list = new ArrayList<>();
        int p = 0;
        for(int i=0; i<n; i++) {
            p = Integer.parseInt(sc.next());
            list.add(p);
        }
        
        int ans = x;
        int i = 1;
        if(list.contains(x)) {
            while(true) {
                if(list.contains(x-i)) {}
                else {
                    ans = x-i;
                    break;
                }
                if(list.contains(x+i)) {}
                else {
                    ans = x+i;
                    break;
                }
                i++;
            }
        }
        System.out.println(ans);
        
    }
}

Measured memory_bytes: 35764
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <numeric>
#include <cmath>
#include <cassert>

using namespace std;

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

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main()
{
	int n,k;
	cin>>n>>k;
	int ans = 1;
	for (int i = 1; i <= n; i++){
		if(ans+k<=2*ans) ans += k;
		else ans *= 2;
	}
	cout<<ans<<endl;
	return 0;
}

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

int main(){
  ll ans = 1;
  int n,k;
  cin>>n>>k;
  ans*=k;
  for(int i=1;i<n;++i){
    ans*=k-1;
  }
  cout<<ans;
  return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<map>
using namespace std;

map<int, int> mp;

int main()
{
	//freopen("第五次-第三题.txt", "r", stdin);
	int n;
	scanf("%d", &n);
	int tmp;
	while (n--)
	{
		scanf("%d", &tmp);
		if (mp.find(tmp) == mp.end())
		{
			mp[tmp] = 1;
		}
		else
			mp[tmp]++;
	}
	map<int, int>::iterator it;
	int cnt = 0;
	for (it = mp.begin(); it != mp.end(); ++it)
	{
		if (it->second % 2 != 0)
			cnt++;
	}
	printf("%d", cnt);
}

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

using namespace std;
 
using ll = long long;
using ull = unsigned long long;
using ldouble = long double;
 
const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;


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

    vector<ll> a(3);
    for (ll i = 0; i < 3; i++)
    {
        a[s[i] - 'a']++;
    }

    string res = "Yes";
    for (ll i = 0; i < 3; i++)
    {
        if (a[i] != 1)
        {
            res = "No";
        }
    }

    println(res);
}


Measured memory_bytes: 3612
#include<stdio.h>

void PRINT(int* A, int N);

int main(void){
	int N,i,v,j;
	
	scanf("%d",&N);
	
	int insertionSort[N];
	for(i=0;i<N;i++){
		scanf("%d",&insertionSort[i]);
	}
	
	PRINT(insertionSort, N);
	
	for(i=1;i<N;i++){
		v = insertionSort[i];
		j=i-1;
		while(j>=0 && insertionSort[j]>v){
			insertionSort[j+1] = insertionSort[j];
			j--;
		}
		insertionSort[j+1] = v;
		PRINT(insertionSort, N);
	}
	return 0;
}

void PRINT(int* A, int N){
	int i;
	printf("%d",A[0]);
	for(i=1;i<N;i++){
		printf(" %d",A[i]);
	}
	printf("\n");
}

Measured memory_bytes: 604
#include <bits/stdc++.h>
#define int2 long long int

using namespace std;

int main(){
	int2 N, W;
	scanf("%lld%lld", &N, &W);

	pair<int2, int2> v[N];
	vector<vector<int2> > problem, itens;
	problem.resize(W+1);
	itens.resize(W+1);

	for(int2 i=0; i<W+1; i++){
		problem[i].resize(N+1);
		itens[i].resize(N+1);
		for(int2 j=0; j<N+1; j++){
			problem[i][j] = itens[i][j] = 0;
		}
	}

	for(int2 i=0; i<N+1; i++){
		scanf("%lld%lld", &v[i].first, &v[i].second);
	}


	for(int2 i=1; i<N+1; i++){
		for(int2 j=1; j<W+1; j++){
			int2 aceitaItem=-1, recusaItem;
			if(j-v[i-1].first>=0){
				aceitaItem = v[i-1].second + problem[j-v[i-1].first][i-1];
			}
			recusaItem = problem[j][i-1];
			problem[j][i] = max(aceitaItem, recusaItem);
		}
	}
	
	printf("%lld\n", problem[W][N]);
}

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

string ss;

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>ss;
	int n = ss.size(), ans = 0;
	bool flag = 1;
	int l = 0, r = n-1;
	while(l < r){
		if(ss[l] != ss[r]){
			if(ss[l]=='x'){
				r++;
				ans++;
			}else if(ss[r]=='x'){
				l--;
				ans++;
			}else{
				flag = 0;
			}
		}
		l++, r--;
	}
	if(!flag) ans = -1;
	cout<<ans<<'\n';
	return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#define DUMP(x)  cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define IFOR(i, m, n) for(int i = n - 1; i >= m; i-- )
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define FOREACH(x,a) for(auto& (x) : (a) )
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
/* テンプレートここまで */

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

  vector<int> a(n);
  int ans1 = 0, ans2;
  REP(i, n){
    cin >> a[i];
    ans1 = max(ans1, a[i]);
  }

  double b = ans1/2.0;
  double d = INF;
  REP(i, n){
    if (a[i] != ans1 && abs(a[i] - b) < d){
      ans2 = a[i];
      d = abs(a[i]-b);
    }
  } 

  cout << ans1 << " " << ans2 << endl;
}


Measured memory_bytes: 640
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>

using namespace std;

const double PI = 3.14159265358979;


int main() {

	int n;  cin >> n;
	
	long long* heap = new long long[n + 1];

	for (int i = 1; i <= n; i++)	{
		scanf("%lld", &heap[i]);
	}

	for (int i = 1; i <= n; i++)	{
		//node 2: key = 8, parent key = 7, left key = 2, right key = 3, 
		long long node = i;
		long long key = heap[i];
		long long parent_key = i == 1 ? INT_MAX : heap[i / 2];
		long long left_key = i * 2 <= n ? heap[i * 2] : INT_MAX;
		long long right_key = (i * 2 + 1) <= n ? heap[i * 2 + 1] : INT_MAX;

		
		printf("node %lld: key = %lld, ", node, key);
		if (parent_key != INT_MAX) printf("parent key = %lld, ", parent_key);
		if (left_key != INT_MAX) printf("left key = %lld, ", left_key);
		if (right_key != INT_MAX) printf("right key = %lld, ", right_key);
		printf("\n");
	}

	delete[] heap;
	

	return 0;
}



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

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

using namespace std;

int main(){
	int k;
	string s; cin>>k>>s;
	if(s.length()<=k) cout<<s<<'\n';
	else cout<<s.substr(0,k)+"..."<<'\n';
	return 0;
}


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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;
typedef pair<double,P> DP;

#define INF INT_MAX/3

#define MAX_N 1000

//// combination using double
double comb(int n,int r){
	if(n<r)return 0.0;
	if(n-r<r)r=n-r;
	double res=1;
	rep(i,r){
		res*=n;
		n--;
		res/=i+1;
	}
	return res;
}

int n,m;
double l;
double p[111],t[111],v[111];
DP times[111*55];
double res[111];
double sum[111];

int main(){
	cin.sync_with_stdio(false);
	cin>>n>>m>>l;
	rep(i,n){
		cin>>p[i]>>t[i]>>v[i];
		p[i]/=100;
	}
	int num=n*(m+1);
	rep(i,n)rep(j,m+1){
		times[i*(m+1)+j].fi=(double)j*t[i]+(double)(l/v[i]);
		times[i*(m+1)+j].se=P(i,j);
	}
	sort(times,times+num);
	memset(res,0,sizeof(res));
	rep(i,num){
		double ntimes=times[i].fi;
		int ni=times[i].se.fi,nj=times[i].se.se;
		memset(sum,0,sizeof(sum));
		rep(j,num){
			if(times[j].fi<ntimes||(times[j].se.fi!=ni&&times[j].fi==ntimes))continue;
			int nni=times[j].se.fi,nnj=times[j].se.se;
			if(times[j].se.fi==ni){
				sum[nni]=comb(m,nj)*pow(p[ni],nj)*pow(1-p[ni],m-nj);
			}else{
				sum[nni]+=comb(m,nnj)*pow(p[nni],nnj)*pow(1-p[nni],m-nnj);
			}
		}
		double pp=1;
		rep(i,n)pp*=sum[i];
		res[ni]+=pp;
	}
	rep(i,n)printf("%.10f\n", res[i]);
	return 0;
}

Measured memory_bytes: 3420
import Data.List

main= do
 n_by2 <- (`div`2).(read::String->Int)<$> getLine
 sorted <-  sort.map (read::String->Int).words<$> getLine
 --let group = groupBy(==) sorted
 if sorted!!(n_by2-1)  == sorted!!(n_by2) then print 0 
 else print $  sorted!!(n_by2) - sorted!!(n_by2-1)

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

using namespace std;

string s;
int n, k;


int main () {

	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n >> k >> s;

	k = k - 1;

	for (int i = 0; i < n; ++i) {
		char x = s[i];
		char y = tolower (s[i]);

		if (k == i) cout << y;
		else cout << x;
	}

	cout << endl;







	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<endl;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b){ a = b; return 1; } return 0; }



int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<pll> ba(n);
  rep(i,n){
    ll a,b;
    cin >> a >> b;
    ba[i].fi = b, ba[i].se = a;
  }
  //simulation
  sort(all(ba));
  ll now = 0;
  bool ok = true;
  rep(i, n) {
    now+=ba[i].se;
    if (now > ba[i].fi) ok = false;
  }
  if (ok) puts("Yes");
  else
    puts("No");
  return 0;
}

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


int main(){
    int n,d;
    cin >> n >> d;
    vector<vector<int>> x(n, vector<int>(d));
    rep(i,n) rep(j,d) cin >> x[i][j];
    int ans = 0;
    rep(i,n) rep(j,i) {
        int sq = 0;
        rep(k,d) {
            int s = x[i][k]-x[j][k];
            sq += s*s;
        }
        int s = sqrt(sq)+0.5;
        if(s*s==sq) ans++;
    }
    cout << ans << endl;
    return 0;
}


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

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
const ll INF = INT_MAX;
const ll MOD = 1e9+7;

ll N,M;
vll G[100010];
ll dp[100010];

void dfs1(ll v,ll p){
  dp[v]=1;
  for(ll u:G[v])if(u!=p){
    dfs1(u,v);
    dp[v] = dp[v]*(dp[u]+1)%M;
  }
}

ll dp2[100010];
void dfs2(ll v,ll p,ll d_p){
  vll prol(G[v].size()+1,1),pror(G[v].size()+1,1);
  for(ull i=0;i<G[v].size();i++){
    if(G[v][i]==p){
      prol[i+1] = (d_p+1)*(prol[i])%M;
    }else{
      prol[i+1] = (dp[G[v][i]]+1)*(prol[i])%M;
    }
  }
  for(ull i=G[v].size();i>0;i--){
    if(G[v][i-1]==p){
      pror[i-1] = (d_p+1)*(pror[i])%M;
    }else{
      pror[i-1] = (dp[G[v][i-1]]+1)*(pror[i])%M;
    }
  }
  dp2[v] = dp[v]*(d_p+1)%M;
  for(ull i=0;i<G[v].size();i++)if(G[v][i]!=p){
    dfs2(G[v][i],v,prol[i]*pror[i+1]%M);
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>N>>M;
  for(ll i=0;i<N-1;i++){
    ll x,y;cin>>x>>y;x--;y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs1(0,-1);
  //debugArray(dp,N);
  dfs2(0,-1,0);
  for(ll i=0;i<N;i++){
    cout<<dp2[i]<<endl;
  }
  return 0;
}


Measured memory_bytes: 18816
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<cmath>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define FOR(i, j, limit) for(int i=j; i< limit; ++i)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ECHO(x) cout<<x<<endl;
#define MEMSET(a, n, x) REP(i, n) a[i]=x;
#define ARRIN(a, n) REP(i, n) cin>>a[i];
#define LL long long
int INF = 2147483647;
LL LINF = 9223372036854775807;
LL MOD = 1000000007;
typedef pair<int, int>P;
typedef pair<LL, LL>PL;
typedef vector<int> vi;
typedef vector<LL> vl;

int main(){
  int n, a, b;
  cin>>n>>a>>b;
  LL h[n];
  ARRIN(h, n);
  int x=a-b;
  LL ng=0, ok=0;
  REP(i, n){
    ok += (h[i]+b-1)/b;
  }
  ++ok;
  LL c, w, t;
  while(ok-ng>1){
    c=(ok+ng)/2;
    w=0;
    REP(i, n){
      t=(h[i]-c*b);
      if(t<=0) continue;
      w += t/x;
      if(t%x) ++w;
    }
    //cout<<ng<<" "<<ok<<" "<<c<<" "<<w<<endl;
    if(w<=c) ok=c;
    else ng=c;
  }

  cout<<ok<<endl;

  return 0;
}


Measured memory_bytes: 1024
#include <stdio.h>
#include <ctype.h>
 
#define MAX1 100000
#define MAX2 1500000

int num[MAX1];
 
void push(int);
int dpth(int);
int input(void);

char *p;
char a[MAX2];
int tree[MAX1][15],x[MAX1];

int main(){
  int n,i,j,id,k,c;

  fread(a,sizeof(a),1,stdin);  
  p = a;

  n = input();
  push(n);
  
  for(i = 0 ; i < n ; i++){
    id = input();
    k = input();
    for(j = 0 ; j < k ; j++){
      c = input();
      num[c] = id;
      tree[id][x[id]++] = c;
    }
  }
 
  for(i = 0 ; i < n ; i++){
    printf("node %d: parent = %d, depth = %d, "
           ,i,num[i],dpth(i));
    
    if(num[i] == -1){
      printf("root, [");
    }
    
    else if(!x[i]){
      printf("leaf, [");
    }
    
    else{
      printf("internal node, [");
    }

    for(j = 0 ; j < x[i] ; j++){
      printf("%d",tree[i][j]);
      if(j < x[i]-1) printf(", ");
    }
    puts("]");
  }
  return 0;
}

void push(int n){
  int i;
  for(i = 0 ; i < n ; i++){
    num[i] = -1;
  }
}

int dpth(int x){
  if(num[x] == -1) return 0;
  return dpth(num[x]) + 1;
}

int input(){
  int x = 0;
  while(!isdigit(*p)) ++p;
  x = *p++&15;
  
  while(isdigit(*p)){
    x = (x<<3)+(x<<1)+(*p++&15);
  }
  
  return x;
}



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

#define pout(n)  printf ("%d\n", n)

#define rep(i,a,n) for (int i = a;i < n;i++)
#define per(i,n,a) for (int i = n-1;i >= a;i--)
#define BIT(bit,a,n) for (int bit = a;bit < (1<<n);bit++) // {0, 1, ..., n-1} の部分集合の全探索 // n番目のフラグが立っている状態は (1<<n)

const int d4x[4] = {1, 0, -1, 0};
const int d4y[4] = {0, 1, 0, -1};
const int d8x[8] = { 1,1,0,-1,-1,-1,0,1 };
const int d8y[8] = { 0,1,1,1,0,-1,-1,-1 };

#define MAX_N (int)2e5+20

typedef long long ll;
using namespace std;

int n,m;
int main() {
    scanf("%d%d",&n,&m);
    int x = 1900*m+100*(n-m);
    int p = pow(2,m);

    pout(x*p);
    return 0;    
}

Measured memory_bytes: 384
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
int n,k;
int maxnum=-inf;
char ch[100005];
int a[3];
int main()
{
   cin>>ch;
   memset(a,0,sizeof(a));
   for(int i=0;i<strlen(ch);i++)
   {
       if(ch[i]=='a') a[0]++;
       else if(ch[i]=='b') a[1]++;
       else a[2]++;
   }
   sort(a,a+3);
   for(int i=2;i>=0;i--)
   {
       if(!a[i]) continue;
       if(a[0]) a[i]-=a[0];
   }
   int flag=1;
   for(int i=0;i<3;i++){
    if(a[i]&&a[i]>1) flag=0;
   }
   if(flag)
    cout<<"YES"<<endl;
   else
    cout<<"NO"<<endl;
    return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

bool solve() {
    int n, m;
    std::cin >> n >> m;
    if (n == 0) return false;

    std::vector<std::vector<int>> to(n);
    while (m--) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        to[u].push_back(v);
        to[v].push_back(u);
    }

    std::vector<int> dist(n, -1);
    dist[0] = 0;
    std::queue<int> que;
    que.push(0);

    while (!que.empty()) {
        int u = que.front();
        que.pop();

        for (auto v : to[u]) {
            if (dist[v] != -1) continue;
            dist[v] = dist[u] + 1;
            que.push(v);
        }
    }

    std::cout << std::count_if(dist.begin(), dist.end(),
                               [](auto x) { return 0 < x && x <= 2; })
              << "\n";
    return true;
}

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

    while (solve()) {}

    return 0;
}



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

int main() {
  ll a;
  cin >> a;
  bool flg = false;
  
  if(a/1 < 10 && a%1 == 0){
    cout << "Yes";
  } else if(a/2 < 10 && a%2 == 0){
    cout << "Yes";
  } else if(a/3 < 10 && a%3 == 0){
    cout << "Yes";
  } else if(a/4 < 10 && a%4 == 0){
    cout << "Yes";
  } else if(a/5 < 10 && a%5 == 0){
    cout << "Yes";
  } else if(a/6 < 10 && a%6 == 0){
    cout << "Yes";
  } else if(a/7 < 10 && a%7 == 0){
    cout << "Yes";
  } else if(a/8 < 10 && a%8 == 0){
    cout << "Yes";
  } else if(a/9 < 10 && a%9 == 0){
    cout << "Yes";
  } else {
    cout << "No";
  }

}

Measured memory_bytes: 256
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashSet<String> set = new HashSet<>();
        int cnt = 0;

        for (int i = 0; i < n; i++) {
            int num = sc.nextInt();
            if (num < 400) {
                set.add("gray");
            } else if (num < 800) {
                set.add("brown");
            } else if (num < 1200) {
                set.add("green");
            } else if (num < 1600) {
                set.add("sky");
            } else if (num < 2000) {
                set.add("blue");
            } else if (num < 2400) {
                set.add("yellow");
            } else if (num < 2800) {
                set.add("orange");
            } else if (num < 3200) {
                set.add("red");
            } else {
                cnt++;
            }

        }

        sc.close();

        if (set.size() == 0) {
            System.out.println("1 " + cnt);
        } else {
            System.out.println(set.size() + " " + (set.size() + cnt));
        }
    }
}

Measured memory_bytes: 22996
unittest
{
	assert( [ "3", "2 2 4" ].solve == "4 0 4" );
	assert( [ "5", "3 8 7 5 5" ].solve == "2 4 12 2 8" );
	assert( [ "3", "1000000000 1000000000 0" ].solve == "0 2000000000 0" );
}



import std.conv;
import std.range;
import std.stdio;

void main()
{
	stdin.byLineCopy.solve.writeln;
}

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	
	auto anss = new long[ n ];
	anss[ 0 ] = as[ 0 ];
	foreach( aa; as[ 1 .. $ ].chunks( 2 ) )
	{
		anss[ 0 ] += -aa[ 0 ] + aa[ 1 ];
	}
	foreach( i, a; as[ 0 .. $ - 1 ] )
	{
		anss[ i + 1 ] = a * 2 - anss[ i ];
	}
	return anss.to!( string[] ).join( " " );
}


Measured memory_bytes: 12352
import Control.Applicative
import Data.List

main = do
  contents <- fmap (read :: String -> Int) . lines <$> getContents
  mapM_ print $ reverse $ solve $ dataCut contents

dataCut :: [Int] -> [[Int]]
dataCut = group . sort

solve :: [[Int]] -> [Int]
solve x = let modeNum = maximum $ fmap length x
              in fst $ split modeNum x ([], [])

equalMode :: Int -> [Int] -> Bool
equalMode modeNum x = modeNum == (length x)

split :: Int -> [[Int]] -> ([Int], [Int]) -> ([Int], [Int])
split _ [] (s, t) = (s, t)
split n (x:xs) (s, t)
  | n == length x = split n xs ((head x):s, t)
  | otherwise = split n xs (s, (head x):t)

Measured memory_bytes: 4156
while True:
    h,w = map(int,raw_input().split())
    if h==0 and w==0:
        break
    sside="" #square side
    inside=""
    for x in xrange(1,w+1):
        sside += "#"
        if x==1 or x==w:
            inside += "#"
        else:
            inside += "."
    for x in xrange(1,h+1):
        if x==1 or x==h:
            print sside
        else:
            print inside
    print

Measured memory_bytes: 6344
// https://atcoder.jp/contests/practice2/tasks/practice2_i
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

void solve() {
    string s;
    cin >> s;
    auto sa = suffix_array(s);
    int ans = (int)s.size() * (s.size() + 1) / 2;
    for (auto x : lcp_array(s, sa)) {
        ans -= x;
    }
    cout << ans << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    solve();
}


Measured memory_bytes: 15608
import java.util.Scanner;
public class Main{
	static Scanner sc=new Scanner(System.in);
	public static void main(String[] args) {
		int n=sc.nextInt();
		for(int i=0;i<n;i++){
			int K=sc.nextInt();
			int p=sc.nextInt();
			int count=0;
			for(int j=0;j<K;j++){
				if(count==p)count=0;
				count++;
				
			}
			System.out.println(count);
		}
		
	}
}

Measured memory_bytes: 20424
#!/usr/bin/env python3
import sys


def solve(A: int, B: int, C: int, K: int):
    if K % 2 == 1:
        print(B-A)
    else:
        print(A-B)


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(A, B, C, K)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
#include<iostream>
#include<algorithm>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,d,x,use=0;
	cin>>n>>d>>x;
	int a[n],day[n];
	fill(day,day+n,0);
	rep(i,n)cin>>a[i];
	rep(i,d){
		rep(j,n){
			if(i==day[j]){
				use++;
				day[j]+=a[j];
			}
		}
	}
	cout<<x+use<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	int n,ans,sum;
	while(cin>>n,n){
		ans=0;
		for(int i=1;i<=n/2;i++){
			sum=0;
			for(int j=0;sum<=n;j++){
				sum+=(i+j);
				if(sum==n)
					ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;
ul over = 1000000007;

int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  ll h, w, d;
  cin >> h >> w >> d;
  char cmap[5] = {'N', 'R', 'G', 'B', 'Y'};
  int color[h][w];
  for (ll i = 0; i < h; ++i) {
    for (ll j = 0; j < w; ++j) color[i][j] = 0;
  }
  for (ll i = 0; i <= h+w; ++i) {
    for (ll j = 0; j <= i; ++j) {
      ll xi = i-j, xj = j;
      if (xi < 0 || h <= xi) continue;
      if (xj < w) color[xi][xj] += (i/d)%2;
      xj = w - j - 1;
      if (0 <= xj && xj < w) color[xi][xj] += ((i/d)%2)*2;
    }
  }
  for (ll i = 0; i < h; ++i) {
    for (ll j = 0; j < w; ++j) {
      cout << cmap[color[i][j]+1];
    }
    cout << endl;
  }
  return 0;
}


Measured memory_bytes: 1536
import sys
import os
import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

MOD = 10 ** 9 + 7


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    S = iss()
    print('Yes' if len(set(S)) == 2 and all([S.count(s) == 2 for s in set(S)]) else 'No')


if __name__ == '__main__':
    main()


Measured memory_bytes: 3064
N, M = map(int, input().split())
A = [list(input()) for i in range(N)]
B = [list(input()) for i in range(M)]

for i in range(N - M + 1):
    for j in range(N - M + 1):
        for k in range(M):
            for l in range(M):
                if A[k + i][l + j] != B[k][l]:
                    break
            else:
                continue
            break
        else:
            print('Yes')
            exit()
print('No')

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end() // 昇順ソート
#define rall(v)(v).rbegin(), (v).rend() // 降順ソート
#define INF 1LL << 60
typedef long long int LL;
typedef long long int ll;
#define pll pair < ll, ll >
#define F first
#define S second
const int MOD = 1000000007;
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; }
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

int main() {
    LL N,K;cin >> N >> K;
    vector<LL> A(N);
    vector<LL> F(N);
    rep(i,N)cin >> A[i];
    rep(i,N)cin >> F[i];

    sort(all(A));
    sort(rall(F));

    LL ub = 1e18;
    LL lb = -1;
    while(ub-lb > 1){
        LL mid = (ub+lb)/2;
        LL tmp = 0;
        rep(i,N){
            LL a = mid/F[i];
            LL sa = A[i] - a;
            if(sa > 0){
                tmp += sa;
            }
        }
        if(tmp <= K){
            ub = mid;
        }
        else{
            lb = mid;
        }
    }

    cout << ub << endl;

}

Measured memory_bytes: 3328
n = input()

a = []
b = []

count = 0

for i in range(n):
    a_,b_ =[int(i) for i in raw_input().split()]
    a.append(a_)
    b.append(b_)
    

c = zip(a,b)
c.sort()
a, b = zip(*c)
a = list(a)
b = list(b)

before_rank = 1
before_score = 10000000000
for i in range(n):

    count += min(a[i]-before_rank, before_score-b[i])
    before_rank = a[i]
    before_score = b[i]

count += before_score + 1


print(count)


Measured memory_bytes: 27940
#include <bits/stdc++.h>
using namespace std;
const long long MOD=1e9+7;
int main(){
  int n;
  cin>>n;
  int a[100010];
  for(int i=0;i<n;++i)cin>>a[i];
  sort(a,a+n);
  bool judge=true;
  if(n&1){
    if(a[0]!=0)judge=false;
  }
  for(int i=1;i<=n/2;++i){
    if(n&1){
        if(a[i*2]!=i*2||a[i*2-1]!=i*2)judge=false;
    }else{
        if(a[(i-1)*2]!=(i-1)*2+1||a[i*2-1]!=(i-1)*2+1)judge=false;
    }
  }
  if(!judge){
    cout << 0 << endl;
  }else{
    long long ans=1;
    for(int i=0;i<n/2;++i){
        ans*=2;
        ans%=MOD;
    }
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 640
#include<stdio.h>
int main(){
  int h,n;
  scanf("%d%d",&h,&n);
  int sum=0;
  int b;
  for(int i=0;i<n;i++){
    scanf("%d",&b);
    sum+=b;
  }
  if(sum<h)
    printf("No");
  else
    printf("Yes");
}
  


Measured memory_bytes: 1732
import Data.Traversable
import Data.List

main = do
  n <- getLine
  vs <- map read . words <$> getLine
  cs <- map read . words <$> getLine
  putStrLn . show $ solve $ zip vs cs

solve :: [(Int, Int)] -> Int
solve [] = 0
solve ((v,c):xs)
  | v - c < 0 = solve xs
  | otherwise = (v-c) + solve xs


Measured memory_bytes: 636
#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include <cassert>
#include <cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int,int>;

vector<int>bitSearch(int bit,int n){
    vector<int>S;
    rep(i,n)if(bit&(1<<i))S.push_back(i);
    return S;
}



int main(){
    int d,n;cin>>d>>n;
    int i = 1;
    int h;
    while(n){
        h = pow(100,d)*i;
        int l = h;int cnt = 0;
        while(l){
            if(l%100 == 0)cnt++;
            l /= 100;
        }
        if(cnt == d)n--;
        i++;
    }
    cout<<h;
}



Measured memory_bytes: 512
# a = int(input())
# n, *a = map(int, open(0))
A,B = map(int, input().split())
# A = list(map(int, input().split()))
# S = input()
cnt = 0
if A-B*2 < 0:
    print(0)
else:
    print(A-B*2)

Measured memory_bytes: 2940
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <map>
using namespace std;
using ll = long long;
const double INF = (1<<30) * 1.0;
const int MOD = 1e9 + 7;
int n, ans;
vector<pair<double, double>> v;
double calc(double a, double b)
{
    pair<double, double> p = v[a], q = v[b];
    double w = p.first - q.first;
    double h = p.second - q.second;
    if(w == 0) return INF;
    return h / w;
}
void dfs(vector<int> used, vector<double> grad)
{
    if(grad.size() == n / 2)
    {
        map<double, int> mp;
        for(int i = 0; i < grad.size(); i++) mp[grad[i]]++;
        int cnt = 0;
        for(auto x : mp) cnt += x.second * (x.second - 1) / 2;
        ans = max(ans, cnt);
        return;
    }
    int pos;
    for(int i = 0; i < n; i++)
    {
        if(used[i]) continue;
        pos = i;
        used[i] = true;
        break;
    }
    for(int i = 0; i < n; i++)
    {
        if(used[i]) continue;
        used[i] = true;
        grad.push_back(calc(pos, i));
        dfs(used, grad);
        grad.pop_back();
        used[i] = false;
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        double x, y; cin >> x >> y;
        v.push_back(make_pair(x, y));
    }
    sort(v.begin(), v.end());
    vector<int> used(n, 0);
    vector<double> tmp;
    dfs(used, tmp);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3316
#include <iostream>

int main() {
	int A,B,C,D,a,b;
	std::cin >>A>>B>>C>>D;
	if (A < B) {
		a = A;
	}
	else if (A > B) {
		a = B;
	}
	else {
		a = A;
	}
	if (C < D) {
		b = C;
	}
	else if (C > D) {
		b = D;
	}
	else {
		b = C;
	}
	std::cout << a + b;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
 
int main(void){
	int d, r;
	double a, b, f;

	scanf("%lf %lf", &a, &b);
	d = a/b;
	r = (int)a%(int)b;
	f = a/b;

	printf("%d %d %f\n", d, r, f);

	return 0;
}

Measured memory_bytes: 648
import math


N = int(input())
A = []
B = []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

add = [0] * N
cnt = 0
for i in range(N - 1, -1, -1):
    if (A[i] + add[i]) % B[i] != 0:
        d = int(math.ceil(1.0 * (A[i] + add[i]) / B[i]))
        click = B[i] * d - (A[i] + add[i])
        add[i] += click
        cnt += click
    if i != 0:
        add[i - 1] = add[i]

print(cnt)


Measured memory_bytes: 15776
#include <bits/stdc++.h>
using namespace std;
int main(){
  int a,b;
  cin>>a>>b;
  vector<int> s(b);
  for(int i=0;i<b;i++)cin>>s[i];
  int ma=0;
  s.push_back(s[0]+a);
  for(int i=0;i<b;i++){
    ma=max(ma,s[i+1]-s[i]);

  }

  cout<<a-ma<<endl;
}


Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <vector>
using namespace std;
int main(){
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int a,b;
  cin>>a>>b;
  if(a<=5){
    b=0;
  }
  else if(a<=12){
    b/=2;
  }
  cout<<b<<"\n";
  
  return 0;
}

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

const ll MOD = 1000000007;

ll K,X,Y;
int xy[200010][2];

ll get_dist( ll x0,ll y0, ll x1, ll y1 ) {
  return( abs(x0-x1) + abs(y0-y1) );
}

int main() {
  cin >> K;
  cin >> X >> Y;
  // XとYは両方正の数にして扱いやすくする。
  int xinv = 1, yinv = 1;
  if ( X < 0 ) xinv = -1;
  if ( Y < 0 ) yinv = -1;
  X = X * xinv;
  Y = Y * yinv;

  ll cx=0, cy=0;

  // K偶数、距離奇数なら不可
  if ( (K%2==0) && ((get_dist(cx,cy,X,Y) % 2)==1) ) {
    cout << "-1" << endl;
    return 0;
  }

  int cnt = 0;
  // 2K未満になるまで近づく
  while( (get_dist(cx,cy,X,Y)) >= K * 2 ) {
    int dx,dy;
    dx = X-cx;
    if ( dx > K ) dx = K;
    dy = K - dx;
    cx += dx;
    cy += dy;
    xy[cnt][0] = cx;
    xy[cnt][1] = cy;
    cnt++;
  }
  
  int dist = get_dist(cx,cy,X,Y);
  if (dist == K) {
    // 丁度ゴールする場合
    xy[cnt][0] = X;
    xy[cnt][1] = Y;
    cnt++;
  } else {
    if (dist % 2 == 1 ) {
      // 距離奇数の場合
      //  距離偶数になるように一手動かす。K は必ず奇数なので、とりあえず動かせばOK
      //  ゴールを飛び越える場合もあるが、その時は Y 方向にのみ飛び出すはず。
      int dx,dy;
      dx = X-cx;
      if ( dx > K ) dx = K;
      dy = K - dx;
      cx += dx;
      cy += dy;
      xy[cnt][0] = cx;
      xy[cnt][1] = cy;
      cnt++;
      dist = get_dist(cx,cy,X,Y);
    }
    // 距離偶数の場合
    //  中点を求めて、そこから外側に残りを伸ばす。
    int dx,dy;
    int disth = dist / 2;
    dx = X-cx;
    if ( dx > disth ) dx = disth;
    dy = disth - dx;
    if ( cy > Y ) dy = -dy;
    cx += dx;
    cy += dy;
    if ( dy == 0 ) {
      // 　　　　　　　G
      //               │
      // S ────●─┘	● 中点
      //           │
      //           │
      //           ★ 		★ K-中点までの距離分だけ外に逃がした点
      cy -= (K - disth);
    } else {
      cx += (K - disth);
    }
    xy[cnt][0] = cx;
    xy[cnt][1] = cy;
    cnt++;
    xy[cnt][0] = X;
    xy[cnt][1] = Y;
    cnt++;
  }

  cout << cnt << endl;
  REP(i,cnt) {
    cout << xy[i][0]*xinv << " " << xy[i][1]*yinv << endl;
  }
  
}


Measured memory_bytes: 3840
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    double x, a, b;
    cin >> x >> a >> b;
    if (abs(x - a) < abs(x - b)) {
        cout << "A" << endl;
    } else {
        cout << "B" << endl;
    }

    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int n = sc.nextInt();
        int maxIndex = 0;
        int[] ps = new int[n];
        for (int i = 0; i < n; i++) {
            ps[i] = sc.nextInt();
            if (ps[i] > ps[maxIndex]) {
                maxIndex = i;
            }
        }
        ps[maxIndex] /= 2;
        int ans = 0;
        for (int p : ps) {
            ans += p;
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 21972
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <random>
#include <cmath>
#include <chrono>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
using namespace std;
using LL = long long;
#define eps 1e-8
#define fi first
#define se second
#define eb emplace_back
#define close std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define FOR(i, x, y) for (LL i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (LL i = (x), _##i = (y); i > _##i; --i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define CASET int ___T; scanf("%d", &___T); for(int __CS=1;__CS<=___T;__CS++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937 dlsrand(random_device{}());
mt19937 mrand(std::chrono::system_clock::now().time_since_epoch().count()); 
int rnd(int x) { return mrand() % x;}
LL bin(LL x, LL n, LL MOD) {LL ret = MOD != 1;for (x %= MOD; n; n >>= 1, x = x * x % MOD)if (n & 1) ret = ret * x % MOD;return ret;}
inline LL get_inv(LL x, LL p) { return bin(x, p - 2, p); }
const double PI = acos(-1.0);
constexpr int maxn = 1e6+10;
constexpr int INF = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3f;
constexpr int mod = 1e9+7;
int n , k,a[maxn],dp[maxn];
int main()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    memset(dp, 0x3f, sizeof dp);
    dp[1] = 0;
    for(int i = 2; i <= n; i++){
        for(int j = 1; j <= k; j++){
            if(i-j<=0)continue;
            dp[i] = min(dp[i-j]+abs(a[i]-a[i-j]),dp[i]);
        }
    }
    /*for(int i = 1; i <=n ; i++){
        printf("dp[%d]===%d\n",i,dp[i]);
    }*/
    cout << dp[n] << endl;
    
    
}

Measured memory_bytes: 6400
#include <bits/stdc++.h>
#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 equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll INF = 1e18;
const long double EPS = 1e-10;

int getketa(int n) {
  int res = 0;
  while (n > 0) {
    res = n % 10;
    n /= 10;
  }
  return res;
}

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

  vector<vector<int>> x(10, vector<int>(10, 0));
  FOR(i, 1, n + 1) {
    int fi = getketa(i), bi = i % 10;
    x[fi][bi]++;
  }

  int ans = 0;
  FOR(i, 1, 10) {
    FOR(j, i, 10) {
      if (i != j) ans += x[i][j] * x[j][i] * 2;
      else ans += x[i][j] * x[i][j];
    }
  }
  cout << ans << endl;



  
  return 0;
}

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

typedef long long ll;
typedef std::pair<ll, ll> P;
typedef std::tuple<ll, ll, ll> t3;
constexpr ll INF = 1e15;
constexpr ll mod = 1e9+7;

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

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


class Divisor {
private:
	vector<ll> F;
public:
	Divisor(ll N) {
		for (ll i = 1; i * i <= N; i++) {
			if (N % i == 0) {
				F.push_back(i);
				if (i * i != N) F.push_back(N / i);
			}
		}
		sort(begin(F), end(F));
	}
	int size() { return F.size(); }
	ll operator[](int k) { return F[k]; }
	const vector<ll>& factors() { return F; }
};

ll ceil(ll v, ll a) {
	if (v % a == 0) return v;
	return (v / a + 1)* a;
}

int main() {
	ll n;
	cin >> n;
	vector<P> ps(n);
	rep(i, n) {
		cin >> ps[i].first >> ps[i].second;
	}
	ll x = ps[0].first;
	ll y = ps[0].second;
	for (int i = 1; i < n; i++) {
		ll dx = ceil(x, ps[i].first);
		ll dy = ceil(y, ps[i].second);
		ll dx1 = dx / ps[i].first;
		ll dy1 = dy / ps[i].second;
		x = max(dx1, dy1) * ps[i].first;
		y = max(dx1, dy1) * ps[i].second;
	}
	cout << x + y << endl;
	return 0;
}


Measured memory_bytes: 3620
#include <stdio.h>
int main() {
  int n;
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (i % 3 != 0 && i % 5 != 0) {
      sum = sum + i;
    }
  }
  printf("%lld", sum);
  return 0;
}

Measured memory_bytes: 1712