akhauriyash/Code-Regression huggingface dataset:
#include"bits/stdc++.h"
#include<valarray>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#define MAX 2002

int n;

char buf[MAX];
string s;

vector<int> v[MAX];

long long int dp_max[MAX];
long long int dp_min[MAX];
long long int child[MAX];



inline void dfs(int b,int pr=-1){
	child[b]=0;
	dp_max[b]=0;
	dp_min[b]=0;
	if(s[b]=='1'){
		child[b]++;
	}
	vector<pair<long long int,long long int> > mi;
	mi.push_back(make_pair(0LL,0LL));
	for(int go:v[b]){
		if(go==pr)continue;
		dfs(go,b);
		child[b]+=child[go];
		dp_max[b]+=dp_max[go]+child[go];
		mi.push_back(make_pair(dp_min[go]+child[go],dp_max[go]+child[go]));
	}
	long long int sz=0;
	long long int accume=0;
	long long int ss=0;
	long long int max_sum=0;
	for(int i=0;i<mi.size();i++){
		max_sum+=mi[i].second;
	}
	for(int i=0;i<mi.size();i++){
		accume=max_sum-mi[i].second;
		long long int z=mi[i].first;
		z-=accume;
		if(z<=0){
			if(dp_max[b]&1LL){
				dp_min[b]=max(dp_min[b],1LL);
			}
			else{
			}
		}
		else{
			dp_min[b]=max(dp_min[b],z);
		}
	}
	return;
}

int main(){
	cin>>n;
	scanf("%s",buf);
	s=buf;
	for(int i=1;i<n;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	long long int ans=LLONG_MAX;
	for(int i=0;i<n;i++){
		dfs(i);
		if(dp_min[i]==0LL){
			ans=min(ans,dp_max[i]/2LL);
		}
	}
	if(ans==LLONG_MAX){
		puts("-1");
		return 0;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 640
require 'pp'
a,d=gets.chomp.split(' ').map{|n| n.to_i}
b,d=gets.chomp.split(' ').map{|n| n.to_i}
c,d=gets.chomp.split(' ').map{|n| n.to_i}
x,d=gets.chomp.split(' ').map{|n| n.to_i}
SUM=Array.new(51){Array.new(51){Array.new(51){false}}}
cnt=0
0.upto(a){|i|
	0.upto(b){|j|
		0.upto(c){|k|
			if(500*i+100*j+50*k==x)
				SUM[i][j][k]=true
				cnt+=1
			end
		}
	}
}
puts cnt



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

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

int MOD=1e9+7;
ll INF=1e18;

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

int main(){
    ll n; cin>>n;
    for(ll i=1; i<=3500; i++){
        for(ll j=1; j<=3500; j++){
            if(((4*i*j-n*i-n*j)>0) && ((n*i*j)%(4*i*j-n*i-n*j)==0)){
                cout << i << " " << j << " " << (n*i*j)/(4*i*j-n*i-n*j) << endl;
                return 0;
            }
        }
    }
}

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

class Pascal{
	vector<vector<int>>C;
public:
	Pascal(){}
	Pascal(int n){
		n=n*2+10;
		C=vector<vector<int>>(n,vector<int>(n));
		for(int i=0;i<n;i++){
			for(int j=0;j<=i;j++){
				if(j==0||j==i)C[i][j]=1;
				else C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
			}
		}
	}
	int nCr(int n,int r){
		if(r<0)return 0;
		return C[n][r];
	}
	int nrC(int n,int r){
		return C[n+r][n];
	}
};
int main(){
	int n,K;cin>>n>>K;
	if(n==K){
		for(int i=1;i<=K;i++){
			if(i==1)puts("1");
			else puts("0");
		}
		return 0;
	}
	Pascal pas(n*2);
	for(int i=1;i<=K;i++){
		if(n-K<i-1){
			puts("0");
		}
		else{
			ll a=pas.nCr(K-1,i-1);
			ll b=(pas.nCr(n-K-1,i)+pas.nCr(n-K-1,i-1)*2%MOD)%MOD+pas.nCr(n-K-1,i-2);b%=MOD;
			printf("%lld\n",a*b%MOD);
		}
	}
}

Measured memory_bytes: 251136
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll INF = LLONG_MAX;

int main()
{
    string s;
    cin >> s;
    string tmp = "keyence";
    int mae = 0, usi = tmp.length() - 1;
    int i;
    i = 0;
    while (tmp[mae] == s[i] && mae <= tmp.length())
    {
        i++;
        mae++;
    }
    i = s.length() - 1;
    while (tmp[usi] == s[i] && usi >= 0)
    {
        i--;
        usi--;
    }
    if (mae > usi)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>

using namespace std;
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(),(x).end()
// int DP[MEM_SIZE][MEM_SIZE] = {0};
// int GMEM[MEM_SIZE][MEM_SIZE] = {0};
template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return;std::cout << e << std::endl;}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class X> void DEBUG(X d, T e){if(DEBUG_OUT == false)return;std::cout <<d<<" ";DEBUG(e);};
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void DFS(int V,int SUM,vector<vector<int> >&GRAPH,vector<int>& used,vector<int>& COUNTER,vector<int> &ANS)
{
  // if(used[V] == 1)return;
  used[V] = 1;
  SUM += COUNTER[V];
  ANS[V] = SUM;
  // DEBUG(V);
  for (int i = 0; i < GRAPH[V].size(); i++)
  {
    if(used[GRAPH[V][i]] == 0)
    {
      DFS(GRAPH[V][i],SUM,GRAPH,used,COUNTER,ANS);
    }
  }
  return;
}

void solve(void)
{
  int N,Q;
  cin>>N>>Q;
  vector<int> COUNTER(N,0);
  vector<vector<int> > GRAPH(N);
  for (int i = 0; i < N-1; i++)
  {
    int A,B;
    cin>>A>>B;
    A--;B--;
    GRAPH[A].push_back(B);
    GRAPH[B].push_back(A);
  }
  for (int i = 0; i < Q; i++)
  {
    int A,B;
    cin>>A>>B;
    A--;
    COUNTER[A] += B;
  }
  vector<int> used(N,0);
  vector<int> ANS(N,0);
  DFS(0,0,GRAPH,used,COUNTER,ANS);
  for (int i = 0; i < ANS.size(); i++)
  {
    if(i != 0) cout<<" ";
    cout<<ANS[i];
  }
  cout<<endl;
  
  return;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

  return 0;
}


Measured memory_bytes: 33664
import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

void main(){
	int n, m;
	{
		int[] tmp = readln.chomp.split.map!(to!int).array;
		n = tmp[0], m = tmp[1];
		}
	
	bool[] flag = new bool[n];
	flag[0] = 1;
	int[] count = new int[n];
	count.fill(1);
	
	foreach(i; 0 .. m){
		int[] tmp = readln.chomp.split.map!(to!int).array;
		int x = tmp[0] - 1, y = tmp[1] - 1;
		
		count[x] -= 1, count[y] += 1;
		if(flag[x]) flag[y] = 1;
		if(count[x] == 0) flag[x] = 0;
		}
	
	int ans = 0;
	foreach(i; 0 .. n){
		if(flag[i]) ans += 1;
		}
	
	ans.writeln;
	
	}
	

Measured memory_bytes: 1788
import java.util.Scanner;

class Main{
    public static void main(String[] args){
        String str[];
        int a, b;
        Scanner scan = new Scanner(System.in);
        str = scan.nextLine().split(" ");
        a = Integer.parseInt(str[0]);
        b = Integer.parseInt(str[1]);
        System.out.println(a*b+" "+(2*a+2*b));
    }
}


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

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

int MOD = 1e9 + 7;
ll INF = 1e18;

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

vector<ll> dis(100010, 0);
vector<vector<P>> tree(100010);

void bfs(int k){
    dis[k]=-1;
    queue<P> que;
    que.push(P(k, 0));

    while(!que.empty()){
        P q=que.front(); que.pop();
        int now_v=q.first;
        ll now_cost=q.second;

        for(int i=0; i<tree[now_v].size(); i++){
            int next_v=tree[now_v][i].first;
            ll path_cost=tree[now_v][i].second;

            if(dis[next_v] == 0){
                dis[next_v]=now_cost+path_cost;
                que.push(P(next_v, dis[next_v]));
            }
        }
    }
}

int main(){
    int n; cin>>n;
    for(int i=0; i<n-1; i++){
        int a, b, c; cin>>a>>b>>c;
        a--, b--;
        tree[a].push_back(P(b, c));
        tree[b].push_back(P(a, c));
    }

    int q, k; cin>>q>>k;
    k--;
    bfs(k);
    for(int i=0; i<q; i++){
        int x, y; cin>>x>>y;
        x--, y--;
        cout << dis[x] + dis[y] << endl;
    }
}

Measured memory_bytes: 10736
A = gets.to_i
B = gets.to_i
C = gets.to_i
X = gets.to_i
#ここまで入力
ans = 0
a = 0 #500円玉の枚数
while a != A+1 do
  b = 0 #100円玉の枚数
  while b != B+1 do
    c = 0 #50円玉の枚数
    while c != C+1 do
      if((a*500+b*100+c*50)==X) #合計金額がXと合致したらansを1増やす
        ans += 1
      end
      c += 1
    end
    b += 1
  end
  a += 1
end
puts ans

Measured memory_bytes: 1788
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

void main() {
    auto S = readln.chomp;
    auto K = readln.chomp.to!int;
    auto N = S.length.to!int;

    auto dp = new long[][][](N+1, 2, K+1);
    dp[0][0][0] = 1;

    foreach (i; 0..N) foreach (j; 0..2) foreach (k; 0..K+1) foreach (d; 0..10) {
        if (d != 0 && k == K) continue;
        if (j == 0 && d > S[i] - '0') continue;
        dp[i+1][j||(d<S[i]-'0')][k+(d!=0)] += dp[i][j][k];
    }

    long ans = dp[N][0][K] + dp[N][1][K];
    ans.writeln;
}


Measured memory_bytes: 256
while 1:
    n = int(input())
    if n == 0:
        break
    S = [input() for i in range(n)]

    used = [1]*n

    for i in range(n):
        for j in range(n):
            if i != j and S[i].find(S[j]) > -1:
                used[j] = 0

    T = [S[i] for i in range(n) if used[i]]
    n = len(T)
    E = [[0]*n for i in range(n)]
    for i in range(n):
        for j in range(n):
            l = len(T[i])
            ti = T[i]; tj = T[j]
            pos = l
            for k in range(l):
                for p in range(l-k):
                    if ti[k+p] != tj[p]:
                        break
                else:
                    pos = k
                    break
            E[i][j] = pos
    ALL = 2**n-1
    memo = {(ALL, i): len(T[i]) for i in range(n)}
    def dfs(state, i):
        if (state, i) in memo:
            return memo[state, i]
        res = 10**18
        for j in range(n):
            if (state >> j) & 1 == 0:
                res = min(res, dfs(state | (1 << j), j) + E[i][j])
        memo[state, i] = res
        return res
    ans = 10**18
    for i in range(n):
        ans = min(ans, dfs(1 << i, i))
    print(ans)

Measured memory_bytes: 30840
n=int(input())
A=list(map(int,input().split()))

mod=1000000007
hat=[0]*3
ans=1
for a in A:
  c=hat.count(a)
  if c==0:
    print(0)
    exit()
  ans=(ans*c)%mod
  i=hat.index(a)
  hat[i]+=1
print(ans)

Measured memory_bytes: 13964
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {

        int N = GetInputInt();
        long[] A = GetInputsLong();

        long anser = 0;
        for (int i = 0; i < N; i++)
        {
            anser ^= A[i];
        }

        if (anser == 0)
        {
            Console.WriteLine("Yes");

        }
        else
        {
            Console.WriteLine("No");
        }

    }

    public static int GetInputInt()
    {
        return int.Parse(Console.ReadLine());
    }
    public static int[] GetInputsInt()
    {
        return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    }
    public static List<int> GetInputsIntList()
    {
        return Console.ReadLine().Split(' ').Select(int.Parse).ToList();
    }
    public static long[] GetInputsLong()
    {
        return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
    }

}


Measured memory_bytes: 22496
#include "bits/stdc++.h"

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

using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

int main() {
	long long N; cin >> N;
	vector<char> ans;
	long long num = 26;
	while (N > 0) {
		int temp = (N - 1) % 26;
		//cout << temp << endl;
		ans.push_back((char)((int)'a' + temp));
		N = (N - 1) / 26;
	}
	rep(i, ans.size()) {
		cout << ans[ans.size() - i - 1];
	}
}



Measured memory_bytes: 3628
A, B, C, K = map(int, input().split())
ans = 0
if A >= K:
    ans += K
else:  # A < K
    if B >= (K - A):
        ans += A
    else:  # B< (K-A)
        if C >= (K - A - B):
            ans += A - (K-A-B)
        else:  # C < (K - A - B)
            ans += A - C
print(ans)


Measured memory_bytes: 9176
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int main() {
  int N, K, Q;
  cin >> N >> K >> Q;

  int a[100000];
  rep(i, N) {
    a[i] = K - Q;
  }

  rep(i, Q) {
    int temp;
    cin >> temp;
    temp--;
    a[temp]++;
  }
  rep(i, N) {
    if(a[i] > 0) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
  return 0;
}

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

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;
const ll INF = 1LL<<60;
//int INF = numeric_limits<int>::max();

int main(int, char**) {
	int n; cin >> n;
	int ret = 0;
	int prev; cin >> prev;
	ret += prev;
	for (int i=1; i<n-1; i++) {
		int b; cin >> b;
		ret += min(prev, b);
		prev = b;
	}
	cout << ret + prev << endl;
	return 0;
}

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


#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1


#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;


li a[200200] = {0};

int main() {
    li n;
    cin >> n;
    rin{cin >> a[i];}

    li res = 0;
    li last = n;
    li last_num = 0;
    for (li i = n - 1; i >= 0; i--) {
        if (a[i] < last_num - (last - i)) {
            puts("-1");
            return 0;
        } else if (a[i] > last_num - (last - i)) {
            res += last_num;
            last_num = a[i];
            last = i;
        }
    }
    if (last_num > last) {
        puts("-1");
        return 0;
    }
    res += last_num;
    cout << res << endl;


    return 0;
}

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

// isDebug?
#ifndef ONLINE_JUDGE
#define _DEBUG
#endif

// Function Macros:
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define setpre(n) cout << fixed << setprecision(n)

// -* https://xn--kst.jp/blog/2019/08/29/cpp-comp/ *- Thx :)
// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << endl
#define debug_4(x1, x2, x3, x4) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                        \
    cout << __FILE__ << ": " << __LINE__ << ": " << __func__ << " -> ";   \
    CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
    (__VA_ARGS__)
#else
#define debug(...)
#endif

// namespace / type / class / struct - alias, def:
using namespace std;

using ll = long long;
using ull = unsigned long long;

// Constants:
const int INF = (1ll << 30) - 1;  // approx 1.07e9
const ll INFLL = 1ll << 60;       // approx 1.15e18
const int MOD = 1e9 + 7;          // 998244353
const long double EPS = 1.0e-14;
const int FACN = 510000;

// Global Variables:
// ll fac[FACN], finv[FACN], inv[FACN];

// Prototype Declarations:
int gcd(int, int);
int lcm(int, int);
template <class T>
inline bool chmin(T&, T);
template <class T>
inline bool chmax(T&, T);

// ================ Main Loop ================
int main() {
    ll x, k, d, ans = 0;
    cin >> x >> k >> d;
    x = abs(x);
    if(log10(abs(k)) + log10(abs(d)) > 15.1 || abs(k * d) >= abs(x)) {
        ll a = x % d;
        ll b = d - x % d;
        // debug(a, b);

        if((k - (x / d)) % 2 == 0) {
            ans = a;
        } else {
            ans = b;
        }

    } else {
        if(x >= 0) {
            ans = x - k * d;
        } else {
            ans = x + k * d;
        }
    }
    ans = abs(ans);
    cout << ans << endl;
    return 0;
}
// ===========================================

// Functions:
// 最大公約数 O(log(min(a, b)))
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
// 最小公倍数 O(log(min(a, b)))
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}

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


Measured memory_bytes: 3860
N = int(input())
H = list(map(int,input().split()))
answer = "Yes"
danger = False
if N == 1:
    answer = "Yes"
else:
    for i in range(N-1):
        j = N-i-1
        diff = H[j] - H[j-1]
        if diff < -1:
            answer = "No"
            break
        elif diff == -1:
            H[j-1] -= 1
print(answer)

Measured memory_bytes: 14396
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <string>
#include <queue>
#include <numeric>
#include <ctype.h>
#include <limits>
#include <iomanip>

#define rep(i,n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
using namespace std;

typedef long long ll;


int main() {
	ll s;
	cin >> s;
	ll x1 = 0, x2 = 1e9, x3 = 0, y1 = 0, y2 = 1, y3 = 1;
	y3 = (s - 1) / x2 + 1;
	x3 = x2 * y3 - s;
	cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) ((int)(x).size())
#define int long long
using namespace std;
typedef vector<int>   vint;
typedef pair<int,int> pint;

int dp[100][2];

signed main()
{
    int N,P; cin>>N>>P;
    vint a(N);
    REP(i,N) cin>>a[i];

    dp[0][0]=1; 
    dp[0][1]=0;
    REP(i,N){
        if(a[i]%2==0){
            dp[i+1][0]=dp[i][0]*2;
            dp[i+1][1]=dp[i][1]*2;
        }
        else{
            dp[i+1][0]=dp[i][0]+dp[i][1];
            dp[i+1][1]=dp[i][0]+dp[i][1];
        }
    }
    cout<<dp[N][P]<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n,a,b;
  cin >> n >> a >> b;
  a--,b--;
  if(a>b) swap(a,b);
  if((a-b)%2==0) pr(abs(a-b)/2);
  else {
    ll x=a+1;
    ll c=b-x;
    x+=c/2;
    ll y=n-b;
    ll d=a+y;
    y+=(n-d-1)/2;
    pr(min(x,y));
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


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

cnt = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

for b in B:
    for i in range(3):
        for j in range(3):
            if b == A[i][j]:
                cnt[i][j] = 1

for i in range(3):
    if sum([cnt[j][i] for j in range(3)]) == 3:
        print('Yes')
        exit()
    if sum([cnt[i][j] for j in range(3)]) == 3:
        print('Yes')
        exit()
if cnt[0][0]+cnt[1][1]+cnt[2][2]==3:
    print('Yes')
    exit()
if cnt[0][2]+cnt[1][1]+cnt[2][0]==3:
    print('Yes')
    exit()

print('No')

Measured memory_bytes: 3064
def main10():
    buf = input("");
    bufs = buf.split();
    num = [];
    for i in range(2):
        num.append(int(bufs[i]));
    if(num[0] + num[1] > 9):
        print("error");
    else:
        print(num[0]+num[1]);


if __name__ == '__main__':
    main10()

Measured memory_bytes: 3060
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const int INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */
int N;
vector<int> A;

const int MAX_A = 1e6 + 1;

ll cnt[MAX_A];

void factrization(ll x) {
    map<int, int> tmp;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0) {
            tmp[i]++;
            x /= i;
            i = 1;
        }
    }
    tmp[x]++;
    for(auto i = tmp.begin(); i != tmp.end(); ++i) {
        if(cnt[i->first] < i->second) {
            cnt[i->first] = i->second;
        }
    }
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

int main() {
    cin >> N;
    A.resize(N);
    rep(i, N) cin >> A[i], factrization(A[i]);
    ll LCS = 1;
    rep(i, MAX_A - 1) {
        rep(j, cnt[i + 1])
            LCS = (LCS * (i + 1))%MOD;
    }
    ll ans = 0;
    rep(i, N) {
        ans = (ans + LCS * modinv(A[i], MOD)) % MOD;
    }
    cout << ans << endl;
}


Measured memory_bytes: 6656
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include <cstring>
#define MAX_N 1000 + 16
 
using namespace std;
 
int N, H, W;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int fld[2 * MAX_N][2 * MAX_N], // 填充遍历用，代表坐标(i, j)处是否空白（压缩后）
dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 };
 
// 压缩坐标，将坐标的值变成“这是第几种值”，返回一共有几种坐标
int compress(int *x1, int *x2, int w)
{
	vector<int>xs;
 
	for (int i = 0; i < N; ++i)
	{
		int tx1 = x1[i], tx2 = x2[i];
		if (1 <= tx1 && tx1 < w) xs.push_back(tx1);
		if (1 <= tx2 && tx2 < w) xs.push_back(tx2);
	}
	xs.push_back(0);
	xs.push_back(w);
	sort(xs.begin(), xs.end());
	xs.erase(unique(xs.begin(), xs.end()), xs.end());
	for (int i = 0; i < N; ++i)
	{
		x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
		x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
	}
	return xs.size() - 1;
}
 
int bfs()
{
	int ans = 0;
	for (int i = 0; i < H; ++i)
	{
		for (int j = 0; j < W; ++j)
		{
			if (fld[i][j]) continue;
			++ans;
			queue<pair<int, int> >que;
			que.push(make_pair(j, i));
			while (!que.empty())
			{
				int nx = que.front().first, ny = que.front().second;
				que.pop();
 
				for (int i = 0; i < 4; ++i)
				{
					int tx = nx + dx[i], ty = ny + dy[i];
					if (tx < 0 || W < tx || ty < 0 || H< ty || fld[ty][tx] > 0) continue;
					que.push(make_pair(tx, ty));
					fld[ty][tx] = 1;
				}
			}
		}
	}
	return ans;
}
 
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
	while (cin >> W >> H, W | H)
	{
		cin >> N;
		for (int i = 0; i < N; ++i)
		{
			cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
		}
 
		memset(fld, 0, sizeof(fld));
 
		W = compress(X1, X2, W);
		H = compress(Y1, Y2, H);
 
		// imos-法
		for (int i = 0; i < N; i++)
		{
			fld[Y1[i]][X1[i]]++;
			fld[Y1[i]][X2[i]]--;
			fld[Y2[i]][X1[i]]--;
			fld[Y2[i]][X2[i]]++;
		}
		// 横向累积
		for (int i = 0; i < H; i++)
		{
			for (int j = 1; j < W; j++)
			{
				fld[i][j] += fld[i][j - 1];
			}
		}
		// 纵向累积
		for (int i = 1; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				fld[i][j] += fld[i - 1][j];
			}
		}// 累积完后，fld中非0部分表示有挡板
		cout << bfs() << endl;
	}
	return 0;
}


Measured memory_bytes: 19020
_, _, m = map(int, input().split()) 

a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = min(a) + min(b)

for _ in range(m):
  xi, yi, ci = map(int, input().split())
  px = a[xi-1] + b[yi-1] - ci
  if px < ans:
    ans = px

print(ans)

Measured memory_bytes: 18736
N,M,Q = gets.split.map(&:to_i)
a = []
Q.times do |i|
  a[i] = gets.split.map(&:to_i)
end

def score(array,a)
  tmp = 0
  for ai,bi,ci,di in a
    tmp += di if array[bi-1] - array[ai-1] == ci
  end
  tmp
end

def dfs(array,a)
  l = array.length
  return score(array,a) if l == N
  result = 0
  l > 0 ? (prev_last = array[-1]) : (prev_last = 0)
  for v in prev_last...M
    array.push(v)
    result = [result, dfs(array,a)].max
    array.pop
  end
  result
end

puts dfs(Array.new,a)


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

int main() {
  char s[3];
  for(int i = 0; i < 3; i++) scanf("%c", &s[i]);
  if(s[0] == s[1] && s[1] == s[2])
    printf("No\n");
  else
    printf("Yes\n");
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    int a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;

    vector<int> water;
    vector<int> sugar;

    for (int i = 0; i <= f; i++) {
        for (int j = 0; j <= f; j++) {
            int x = 100 * (i * a + j * b);
            if (x <= f) water.push_back(x);
        }
    }

    for (int i = 0; i <= f; i++) {
        for (int j = 0; j <= f; j++) {
            int y = i * c + j * d;
            if (y <= f) sugar.push_back(y);
        }
    }

    double mr = -1;
    int mw = 0, ms = 0;

    for (int i = 1; i < water.size(); i++) {
        for (int j = 0; j < sugar.size(); j++) {
            if (!water[i] or water[i] + sugar[j] > f) continue;
            double rate = 100.0 * sugar[j] / (water[i] + sugar[j]);
            if (100 * sugar[j] <= e * water[i] and rate > mr) {
                mw = water[i];
                ms = sugar[j];
                mr = rate;
            }
        }
    }

    cout << mw + ms << " " << ms << endl;

    return 0;
}


Measured memory_bytes: 1400
#include <stdio.h>
#define N 200002
typedef long long LL;

LL f(int x)
{
	return (LL)x * (LL)(x - 1) / 2;
}

int read()
{
	char c;
	while((c = getchar()) < 48 || c > 57);
	int x = c - 48;
	while((c = getchar()) > 47 && c < 58) x = x * 10 + c - 48;
	return x;
}

int c[N] = {0}, a[N];

int main() 
{
	int n, x, i;
	LL sum = 0;
		
	n = read();
	for(i = 0; i < n; i++){
		c[a[i] = read()]++;
	}
	for(i = 1; i <= n; i++){
		sum += f(c[i]);
	}
	for(i = 0; i < n; i++){
		printf("%lld\n", sum - c[a[i]] + 1);
	}
			
	return 0;
}

Measured memory_bytes: 3584
#include<bits/stdc++.h>
#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--)
using namespace std;

int main() {
    string s;
    cin >> s;
    int c = 0;
    rep(i, 0, s.size()) {
        if (s[i] == '1') {
            c++;
        }
    }
    cout << c << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <utility>
using namespace std;
int main(){
	int N;
	cin >> N;
	string S;
	cin >> S;
	int red = 0;
	int blue = 0;
	for(int i=0;i<N;i++){
		if(S[i] == 'R')red++;
		else blue++;
	}
	if(red > blue) cout << "Yes" << endl;
	else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for (int (i) = 0; (i) < (int)(n); i++) 
#define FOR(i, a, b) for(int (i) = a; (i) < (int)b; i++)
#define RREP(i, n) for(int (i)=((int)(n)-1); (i)>=0; i--)
#define RFOR(i, a, b) for(int (i) =((int)(b)-1); (i)>=(int)a; i--)
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define FI first
#define SC second
#define MP make_pair
#define PB push_back
#define SZ(x)  (int)x.size()
#define SP(x)  setprecision((int)x)


using namespace std ;
typedef long long ll;
typedef vector<int> vint;
typedef vector<vint> vvint;
typedef vector<string> vstr;
typedef pair<int, int> pii;
const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}


int main()
{
  int h, w ;
  cin >> h >> w ;
  vstr str(h) ;
  char a, b, c, d ;
  
  REP(i,h){
    cin >> str.at(i) ;
  }
  REP(i,h){
    REP(j,w){
      if(str.at(i).at(j)=='.'){
        continue ;
      }
      
      if(i==0){
        b = '.' ;
      }
      else{
        b = str.at(i-1).at(j) ;
      }
      
      if(i==h-1){
        d = '.' ;
      }
      else{
        d = str.at(i+1).at(j) ;
      }
      
      if(j==0){
        a = '.' ;
      }
      else{
        a = str.at(i).at(j-1) ;
      }
      
      if(j==w-1){
        c = '.' ;
      }
      else{
        c = str.at(i).at(j+1) ;
      }
      
      if(a=='.' && b=='.' && c=='.' && d=='.'){
        cout <<"No"<<endl ;
        return 0 ;
      }
    }
  }
  
  cout <<"Yes"<<endl ;
        
    

  return 0 ;
}




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

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;

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;
}
vector<bool> seen;
void dfs(const Graph &G, int v){
    seen[v] = true; // vを訪問済みにする
    
    for(auto next_v: G[v]){
        if(seen[next_v]) continue; // next_vが探索済みならスルー
        dfs(G, next_v); // 再帰的に探索
    }
}

int main(){

    int a, b;
    cin >> a >> b;
    if((a != 1) && (b != 1)) cout << 1 << endl;
    else if((a != 2) && (b != 2)) cout << 2 << endl;
    else if((a != 3) && (b != 3)) cout << 3 << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()

#define endl "\n"
#define pb push_back
#define elif else if
#define cout_db cout << fixed

#define MOD 1000000007LL
#define PI 3.1415926

typedef long long ll;

template <typename T>
inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <typename T>
inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template <typename T>
inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template <typename T>
inline T nHr(T a,T b){return nCr(a+b-1,b);}
template <typename T>
inline T POW(T a,T b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}

int n, a[100000], b[100000], c[100000];

ll fact(ll n){
	return (1+n)*n/2;
}
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, a[100000], MAX = -1;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if(a[i] > MAX) MAX = a[i];
	}
	double half = (double)MAX / 2.0;
	double mindiff = 1000000001.0;
	int nd;
	for (int i = 0; i < n; i++) {
		if(MAX == a[i]) continue;
		if(abs((double)a[i]-half) < mindiff){
			mindiff = abs((double)a[i]-half);
			nd = a[i];
		}
	}
	cout << MAX << ' ' << nd << endl;
	
	return 0;
}

Measured memory_bytes: 640
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <functional>
#include <string>
#include <cstdlib>
#include <numeric>
#include <cstdbool>
#include <map> 
#include<set>
#include<queue>

typedef long long ll;


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

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

const long long INF = 1000000000000000;
const ll inf = -1e18;
typedef pair<int, int> P;
ll ma = 1000000000 + 7;
ll h, w, n,k; string s;
char maze[60][60];
int dis[101][101];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
ll dp[100010][2];
int gcd(int x, int y) {
	if (x % y == 0) return y;
	return gcd(y, x % y);

}
int main() {
	cin >> n;
	vector<ll> a(n);
	rep(i, n) {
		cin >> a[i];
	}
	dp[0][0] = inf;
	dp[0][1] = inf;
	for(ll i=1;i<n;i++) {
		if (i == 1) {
			dp[i][0] = a[i - 1] + a[i];
			dp[i][1] = -a[i - 1] - a[i];
		}
		else {
			dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
			dp[i][1] = max(dp[i - 1][0] - 2 * a[i - 1] - a[i], dp[i - 1][1] + 2 * a[i - 1] - a[i]);
		}
	}
	cout << max(dp[n-1][0], dp[n-1][1]) << endl;
}




Measured memory_bytes: 2560
/*
 * yaswanth phani kommineni
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n';

void solve(){
	ll a,b,c,d;
	cin >> a >> b >> c >> d;
	ll ans;
	ans = max(a*c,b*d);
	ans = max(ans,a*d);
	ans = max(ans,b*c);
	cout << ans << endl;
}

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int z;
	z = 1;
	//cin >> z;
	for(int z1=1;z1<=z;z1++){
		solve();
	}
	return 0;
}


Measured memory_bytes: 3648
# 問題文をよく読みましょう
n,m = map(int, input().split())
cost, time = 10000, 10000
for i in range(n):
    c,t = map(int, input().split())
    # 時間がmより小さい
    if m >= t:
            cost = min(cost,c)
            
if cost == 10000:
    print('TLE')
else:
    print(cost)


Measured memory_bytes: 2940
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
using namespace std;
typedef long long ll;
int str[200005];
//pair<int,int> pa;
//vector<pa> vec;
struct tn
{
	ll cou;
	ll cheng;
}f[200005];
int main()
{
	int n;
	cin>>n;
	
for(ll i=1;i<=n;i++) f[i].cou=0;
	for(ll i=1;i<=n;i++)
	 {
	 	cin>>str[i];
	 	f[str[i]].cou++;
	 }
	 ll all=0;
	for(ll i=1;i<=n;i++)
	{
		
		ll cc=f[i].cou;
		f[i].cheng=cc*(cc-1)/2;
	//	cout<<f[i].cheng<<endl;
	//	if(biaoji[i]==0) 
		
		all+=f[i].cheng;
	
		
	}
	for(ll i=1;i<=n;i++)
	{
		ll tmp=str[i];
		ll ans=all;
		ans=ans-(f[tmp].cou)*(f[tmp].cou-1)/2;
		ans+=(f[tmp].cou-1)*(f[tmp].cou-2)/2;
		
		cout<<ans<<endl;
		
	}
}

Measured memory_bytes: 6528
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct AB{long long a; int b;};
int main(){
    long long N,M;
    cin>>N>>M;
    vector<AB> ab(N);
    for(int i=0;i<N;i++)
        cin>>(ab[i].a)>>(ab[i].b);
    sort(ab.begin(), ab.end(),[](auto& x, auto& y){return y.a<x.a;}); // bigger
    // for(int i=0;i<N;i++){
    //     cout<<ab[i].a<<" "<<ab[i].b<<endl;
    // }
    long long sum=0;
    while(M>0){
        auto top = ab.back();
        ab.pop_back();
        auto a = top.a; //cost
        auto b = top.b; //how many
        if(M<b) b=M;
        // cout<<"a:"<<a<<" b:"<<b<<" M:"<<M<<endl;
        sum+=a*b;
        M-=b;
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
#define ll long long 
#define pb push_back
#define bg begin()
#define en end()
#define vvl(n,m) vector<vector<ll> > a( n , vector<ll> (m))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
ll gcd(ll a,ll b){
    if(a==0)
    return b;
    else
    return gcd(b%a,a);
}
ll maxSubArraySum(ll a[], ll size) 
{ 
    ll max_so_far = -1000000000000000, max_ending_here = 0; 
  
    for (ll i = 0; i < size; i++) 
    { 
        max_ending_here = max_ending_here + a[i]; 
        if (max_so_far < max_ending_here) 
            max_so_far = max_ending_here; 
  
        if (max_ending_here < 0) 
            max_ending_here = 0; 
    } 
    return max_so_far; 
} 
ll countPairs(ll arr[], ll n) 
{ 
    map<ll, ll> map; 
    for (ll i = 0; i < n; i++) {
        if (map.find(arr[i]-i) == map.end()) { 
            map[arr[i]-i] = 1; 
        } 
        else { 
            map[arr[i]-i]++; 
        } 
    }
    ll res = 0; 
    for (auto x : map) { 
        ll cnt = x.second; 

        res += ((cnt * (cnt - 1)) / 2); 
    } 
  
    return res; 
} 
ll findSubarraySum(ll arr[], ll n, ll sum) 
{ 
    unordered_map<ll, ll> prevSum; 
  
    int res = 0; 
  
    int currsum = 0; 
  
    for (int i = 0; i < n; i++) { 
        currsum += arr[i]; 
        if (currsum == sum)  
            res++;         
  
        if (prevSum.find(currsum - sum) !=  
                                  prevSum.end())  
            res += (prevSum[currsum - sum]);
        prevSum[currsum]++; 
    } 
  
    return res; 
} 
ll sod(ll x){
    ll r,sum=0;
    while(x>0){
        r=x%10;
        x/=10;
        sum+=r;
    }
    return sum;
}
long long modpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
int main()
{
    fast
    ll d,t,s;
    cin>>d>>t>>s;
    if(d<=t*s)
    cout<<"Yes";
    else
    cout<<"No";
}

Measured memory_bytes: 3656
n, k = map(int, input().split())
a = list(map(int, input().split()))
s = []
ord = [-1 for i in range(n+1)]
v = 1
c = 1
l = 0
while ord[v] == -1:
    ord[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord[v]
l = ord[v]
if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])


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

int main(){
  int n,ma,mb;cin>>n>>ma>>mb;
  int a[n],b[n],c[n];
  for(int i=0;i<n;i++){
    cin>>a[i]>>b[i]>>c[i];
  }

  int dp[n+1][40*10+1][40*10+1];

  for(int i=0;i<n+1;i++){
    for(int j=0;j<40*10+1;j++){
      for(int k=0;k<40*10+1;k++){
	dp[i][j][k]=114514810;
      }
    }
  }

  dp[0][0][0]=0;

  for(int i=0;i<n;i++){
    for(int j=0;j<40*10+1;j++){
      for(int k=0;k<40*10+1;k++){
	if(dp[i][j][k]==114514810) continue;
	dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k]);
	dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i]);
      }
    }
  }  
  
  int ans=114514810;
  
  for(int j=1;j<40*10+1;j++){
    for(int k=1;k<40*10+1;k++){
      if(j*mb==k*ma) ans=min(ans,dp[n][j][k]);
    }
  }

  if(ans==114514810){ans=-1;}

  cout<<ans<<endl;
    
  
  return 0;
}


Measured memory_bytes: 25984
n=int(input())
s=input()
a=s.count("R")
b=s.count("G")
c=s.count("B")
sum=0
for i in range(n-2):
  for j in range(i+1,n-1):
    if 2*j-i<=n-1 and s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
      sum=sum+1
print(a*b*c-sum)
      

Measured memory_bytes: 67948
n,m=map(int,input().split())
if n >2 and m >2:
    ans = (n-2)*(m-2)
elif n ==2 or m ==2:
    ans = 0
elif n == 1 and m == 1:
    ans = 1
elif n == 1:
    ans = m-2
else:
    ans = n-2

print(ans)

Measured memory_bytes: 9008
#include <iostream>
	#include <cstring>

	using namespace std;
	int main()
	{
		char a[10000];
		cin >> a;
		if(a[strlen(a)-1]=='s')
		{
			for(int i=0;i<strlen(a);i++)
			{
				cout << a[i];
			}
			cout << 'e' << 's' << endl;
		}
		else
		{
			for(int i=0;i<strlen(a);i++)
			{
				cout << a[i];
			}
			cout << 's' << endl;
		}

		return 0;
	
	}

Measured memory_bytes: 3640
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<iomanip>

#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

using namespace std;

#define debug(x) cout<<(#x)<<"="<<(x)<<", ";
#define debug1(x) debug(x) cout<<'\n';
#define debug2(x1, x2) debug(x1) debug(x2) cout<<'\n';
#define debug3(x1, x2, x3) debug(x1) debug(x2) debug(x3) cout<<'\n';
#define debug4(x1, x2, x3, x4) debug(x1) debug(x2) debug(x3) debug(x4) cout<<'\n';
#define debug5(x1, x2, x3, x4, x5) debug(x1) debug(x2) debug(x3) debug(x4) debug(x5) cout<<'\n';

#define fcout cout<<fixed<<setprecision(15)
#define repp(i,j,k) for(int i=j; i<k; ++i)
#define rep(i,j) repp(i,0,j)
#define rrep(i,j,k) for(int i=j; i>=k; --i)
#define all(x) (x).begin(), (x).end()
#define sort(x) sort(x.begin(), x.end())
#define lb(x,val) lower_bound(x.begin(), x.end(), val)
#define ub(x,val) upper_bound(x.begin(), x.end(), val)
#define pb(x) emplace_back(x);

typedef long long int lli;
typedef pair<int,int> pii;
typedef pair<lli,lli> pll;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef tuple<int, int, lli> tiil;

//const lli M=998244353ll;
const lli M=1000000007ll;
const double D=1.0;

int main(){
	fast_io
	int n,a,b; cin>>n>>a>>b;
	cout<<min(n*a,b);
	
	return 0;
}

Measured memory_bytes: 256
def run():
  b = 10**9+7
  bit = 60
  N = int(input())
  a_li = list(map(int, input().split()))
  ans = 0
  dp = [[0]*bit for i in range(N)]
  for i in range(bit):
    dp[0][i] = (a_li[0] >> i) & 1
  for i in range(1, N):
    tem = 0
    for j in range(bit):
      a_j = (a_li[i] >> j) & 1
      dp[i][j] = dp[i-1][j] + a_j
      if a_j == 1:
        tem += (i-dp[i-1][j]) << j
      else:
        tem += dp[i-1][j] << j
    #print(ans, tem)
    ans += tem
  #print(dp)
  print(ans%b)

if __name__ == '__main__':
  run()

Measured memory_bytes: 203592
#include "bits/stdc++.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
typedef pair<int,int> pii;
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
template<class T> inline bool chmax(T &a, T b){if(a < b){a = b;return true;}return false;}
template<class T> inline bool chmin(T &a, T b){if(a > b){a = b;return true;}return false;}
/*----------------------------------------------------------------*/
// const int MOD = 998244353;
// const int MOD = 1000000007;
vector<pii> p;
/*----------------------------------------------------------------*/
signed main(){
	
	int n;
	cin >> n;
	int cnt[300][300] = {};
	rep(i,n){
		string a;
		cin >> a;
		// cerr << a << endl;
		int m = a.size();
		int z = 0;
		int f = 0;
		int x2 = 0;
		int x5 = 0;

		rep(j,m){
			if(a[m-1-j] == '.'){
				f = 1;
				a.erase(m-1-j,1);
				break;
			}else{
				z++;
			}
		}
		if(f==0)z = 0;
		
		x2 = -z;
		x5 = -z;
		cerr << a << endl;
		int A = stoll(a);
		while(A%2 == 0){
			A /= 2;
			x2++;
		}
		while(A%5 == 0){
			A /= 5;
			x5++;
		}
		p.push_back(MP(x2,x5));
		// cerr << x2 <<" " << x5 << endl;
		cnt[x2+150][x5+150]++;
	}

	int s[310][310] = {};
	rep(i,301)rep(j,301)s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + cnt[i][j];

	int ans = 0;
	rep(i,n){
		int x2 = p[i].first;
		int x5 = p[i].second;
		// cerr << x2 << " " << x5 << endl;
		int y2 = 150-x2;
		int y5 = 150-x5;
		int pl = s[300][300]-s[y2][300]-s[300][y5]+s[y2][y5];
		if(x2>=0&&x5>=0)pl--;
		ans += pl;
		// cerr << x2 <<" " << x5 << " : " << pl << endl;
	}
	cout << ans/2 << endl;
	


	


	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=gnu++17 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://abc174.contest.atcoder.jp/tasks/abc174_b
// rm -r -f test;oj d https://atcoder.jp/contests/agc047/tasks/agc047_a

Measured memory_bytes: 8184
n=gets.to_i
a=gets.split.map(&:to_i)

b, c, f=a[0], 0, -1
(1...n).each do |i|
  e=a[i]
  if f==-1 # ?
    if b<e
      f=0
    elsif b>e
      f=1
    else
      f=-1
    end
  elsif f==0 # increasing
    if b<=e
      #
    else
      f=-1
      c+=1
    end
  elsif f==1 # dec
    if b>=e
      #
    else
      f=-1
      c+=1
    end
  else # yabai
    exit(1)
  end
  b=e
end

puts c+1


Measured memory_bytes: 8452
//競技プログラミング　テンプレ
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib> //絶対値を返す関数 abs()に必要

using namespace std;

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

const int max_v = 300;
const int INF = 1000000;



int main() {
	int n, z, w, ans;
	cin >> n >> z >> w;
	vector <int> A(n);
	REP(i,n) {
		cin >> A[i];
	}

	if(n==1){
		ans = abs(A[n-1]-w);
		cout << ans << endl;
		return 0;
	}

	else{
		ans = max( abs( A[n-1] - w), abs( A[n-2]-A[n-1]));
		cout << ans << endl;
	}
	return 0;
}


Measured memory_bytes: 256
# 10000 円札と、5000 円札と、1000 円札が合計で n 枚あって、
# 合計金額が y 円であったという。
# このような条件を満たす各金額の札の枚数の組を 1 つ求めなさい。
# そのような状況が存在し得ない場合には -1 -1 -1 と出力しなさい。

input = gets.chomp.split

# 札の枚数を取得
n = input[0].to_i

# 合計金額を取得
y = input[1].to_i

# 組み合わせを返す関数を定義
def judge(n, y)
  (n+1).times do |i|
    (n+1).times do |j|
      k = n - (i + j)
      break if k < 0
      sum = 10000 * i + 5000 * j + 1000 * k
      return "#{i} #{j} #{k}" if sum == y
    end
  end
  
  # 組み合わせが無かった場合
  return "-1 -1 -1"
end

# 結果を出力
puts judge(n, y)

Measured memory_bytes: 1788
#include <bits/stdc++.h>
//#include "./libb/lib.hpp"
 
#define rep(i, n) for(int i = 0; i < (n); ++i)
 
using namespace std;
const uint64_t MOD = 1000000007;
const double PI = acos(-1);
 
int main()
{
	int temp = 0;
	cin >> temp;

	if (temp >= 30)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}


Measured memory_bytes: 3632