akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
 
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); cerr << endl << "Time elapsed: " << (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms"; return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;
 
// debugger
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#define debug(stuff) cout << #stuff << ": " << stuff <<endl
#define debugc(stuff) cout << #stuff << ": "; for(auto x: stuff) cout << x << " "; cout << endl;
// 
 
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define all(v) v.begin(), v.end() 
 
#define print(stuff) cout << stuff << endl
#define printc(stuff) for(auto x: stuff) cout << x << " "; cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define len length
#define ret0 return 0
#define ret return 
 
 
#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)
 
#define vl vector<ll> 
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll> 
#define pq priority_queue<ll>
 
// typedef tree<ll,null_type,less<ll>,rb_tree_tag,
// tree_order_statistics_node_update> indexed_set;
 
#define inf (long long int) 1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN (ll)2e5+5
#define N (1ll<<10)
#define pi 3.141592653589793238462643

void solve(){
    ll a, b;
    cin>>a>>b;
    if(a > b) swap(a, b);
    if(a == 1 && b == 2) print(3);
    else if(a == 2 && b == 3) print(1);
    else if(a == 1 && b == 3) print(2);
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

 
    ll t = 1;
    // cin>>t;
    while(t--){
        solve();
    }
 
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int N,M;
    cin >> N;
    vector<string> s(N);
    for(int i = 0; i < N; i++) cin >> s[i];
    cin >> M;
    vector<string> t(M);
    for(int i = 0; i < M; i++) cin >> t[i];
    sort(s.begin(),s.end());
    sort(t.begin(),t.end());
    int Count = 0;
    int MaxCount = 0;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(s[i] == s[j]) Count++;
        }
        for(int j = 0; j < M; j++){
            if(s[i] == t[j]) Count--;
        }
        MaxCount = max(MaxCount,Count);
        Count = 0;
    }
    cout << MaxCount << endl;
}


Measured memory_bytes: 256
s = gets.to_i

appeared = false
count = 0
index = 0

while count < 2
  if [1, 2, 4].include?(s)
    appeared = s if appeared == false
    count += 1 if s == appeared
  end

  s = s.even? ? s / 2 : s * 3 + 1
  index += 1
end

puts index


Measured memory_bytes: 1788
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <ctype.h>
#include <map>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cctype>
#include <queue>
#define INF 100000000;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define REP1(j,n) for(int j=0;j<(int)n;++j)
#define REPD(i,n) for(int i=n;i>=0;--i)
#define REPD1(j,n) for(int j=n;j=>0;--j)
#define REPS(i,n) for(int i=1;i<=(int)n;++i)
#define REPS1(j,n) for(int j=1;j<=(int)n;++j)
#define rep(i,a,n) for(int i=a;i<=(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define DescSort(a) sort(a.begin(),a.end(),std::greater<int>())

using namespace std;
typedef long long ll;

int main(void) {
	string s;
	cin >> s;
	vector<int> a(4);
	REP(i, 4) a[i] = s[i] - '0';
	vector<char> op = { '+','-' };
	for (int bit = 0; bit < (1 << 3); ++bit) {
		int sum_val = a[0];
		// 0 -> +
		// 1 -> -
		for (int i = 0; i < 3; ++i) {
			if ((bit >> i) & 1) {
				sum_val -= a[i+1];
			}
			else { sum_val += a[i+1]; }
		}
		if (sum_val == 7) {
			cout << a[0];
			for (int k = 0; k < 3;++k) {
				//ビットパターンbitからopの文字を選択
				cout << op[(bit >> k) & 1] << a[k+1];
				if (k == 2) {
					cout << "=7" << endl;
					return 0;
				}
			}
		}
	}

	return 0;
}

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

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

	if (a == 0) {
		cout << 1 << endl;
	}
	else if (b == 0) {
		cout << 2 << endl;
	}
	else if (c == 0) {
		cout << 3 << endl;
	}
	else if (d == 0) {
		cout << 4 << endl;
	}
	else {
		cout << 5 << endl;
	}
}

Measured memory_bytes: 3544
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cassert>
using namespace std;

#define REP(i,n) for (ll i = 0; i < n; i++)
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long ll;

int main ()
{   
    double n,m,d;
    cin >> n >> m >> d;
    if (d == 0) {
        double P = 1.0 / (double)n;
        double ans = 0;
        REP(i,m - 1) ans += P; 
        cout << fixed << setprecision(10) << ans << endl;
    } else {
        double P = 2.0 * (double)(n - d) / (double)(n * n);
        double ans = 0;
        REP(i,m - 1) ans += P;
        cout << fixed << setprecision(10) << ans << endl;
    }

    return 0;
}


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

namespace FactorsOfFactorial {
    class Program {
        static void Main(string[] args) {
            var n = int.Parse(Console.ReadLine());
            var a = new int[n+1];
            long ans = 1;
            for (int i = 1; i <= n; i++) {
                for (int j = 2; j <= n; j++) {
                    if (IsPrime(j)) {
                        var tmp = i;
                        while (tmp % j == 0) {
                            tmp /= j;
                            a[j]++;
                        }
                    }
                }
            }
            for (int i = 0; i <= n; i++) {
                if (a[i] > 0) {
                    ans *= (a[i] + 1);
                    ans %= (long)Math.Pow(10, 9) + 7;
                }
            }
            Console.WriteLine(ans);
        }
        static bool IsPrime(int x) {
            if (x == 1) {
                return false;
            }
            for (int i = 2; i * i <= x; i++) {
                if (x % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
}


Measured memory_bytes: 13140
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <set>
#include <map>
#include <deque>
#define NMAX 52
#define MMAX 52

//コメントアウトするとdebug()を実行しない
#define DEBUG

using namespace std;
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

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

int A[MMAX],B[MMAX],C[NMAX],N,M,W,H,ans,dp[MMAX] = {};
string S;
static const int dx[8] = {0,1,1,1,0,-1,-1,-1},
					dy[8] = {1,1,0,-1,-1,-1,0,1};

void solve(){
	for (int i = 0; i < M; ++i)
	{
		dp[A[i]]++;
		dp[B[i]]++;
	}
}

void debug(){

}

void answer(){
	for (int i = 0; i < N; ++i)
	{
		cout << dp[i+1] << endl;
	}
}

int main(){
	cin >> N >> M;
	for (int i = 0; i < M; ++i)
	{
		cin >> A[i] >> B[i];
	}
	
	solve();
	#ifdef DEBUG
	debug();
	#endif
	answer();

	return 0;
}

Measured memory_bytes: 256
// 22:45~

#include <bits/stdc++.h>
using namespace std;

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C,typename=decltype(begin(C()))>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

int dfs(const vvi& g,int u)
{
	int res=1<<u;
	for(int v:g[u]) res|=dfs(g,v);
	return res;
}

int main()
{
	//clock_t t0=clock();
	for(int n,m;~scanf("%d%d",&n,&m) && n|m;){
		array<int,20> cs;
		vvi g(n);
		rep(i,n){
			scanf("%d",&cs[i]);
			int k; scanf("%d",&k);
			rep(j,k){
				int r; scanf("%d",&r);
				g[i].push_back(r);
			}
		}
		
		array<int,20> bs;
		rep(i,n) bs[i]=dfs(g,i);
		
		static array<int,1<<20> sums; sums.fill(0);
		
		rep(i,n) sums[1<<i]=cs[i];
		rep(i,1<<n) sums[i]=sums[i&-i]+sums[i-(i&-i)];
		
		int res=INF;
		per(i,1<<n) if(__builtin_popcount(i)<res){
			int b=0;
			rep(j,n) b|=(i>>j&1)*bs[j];
			if(sums[b]>=m) res=min(res,__builtin_popcount(b));
		}
		printf("%d\n",res);
	}
	//clock_t t1=clock();
	//printf("%.3f[s]\n",1.*(t1-t0)/CLOCKS_PER_SEC);
}

Measured memory_bytes: 5292
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

	vector<pair<int, long long int>> X;
	for (int ii = 0; ii<N; ii++) {
		long long int x;
		cin >> x;
		X.push_back(make_pair(ii, x));
	}

	sort(X.begin(), X.end(), [](const auto& item1, const auto& item2){
		return item1.second < item2.second;
	});

	long long int median_low = X[N / 2 - 1].second;
	long long int median_high = X[N / 2].second;

	sort(X.begin(), X.end(), [](const auto& item1, const auto& item2) {
		return item1.first < item2.first;
	});

	for (auto& x : X) {
		if (x.second <= median_low) {
			cout << median_high << endl;
		}
		else {
			cout << median_low << endl;
		}
	}
}


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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

signed main() {
  ll N;
  cin >> N;
  vector<PLL> v; // {val, id}
  rep(i,N){
    ll val;
    cin >> val;
    v.emplace_back(val, i+1);
  }
  sort(begin(v), end(v));
  for (auto e: v) {
    cout << e.second << endl;
  }
  return 0;
}


Measured memory_bytes: 2544
while True:
    H, W = map(int, input().split(' '))
    if H == 0 and W == 0:
        break
    
    for i in range(H):
        for j in range(W):
            if (i + j ) % 2 == 0:
                print('#', end = '')
            else:
                print('.', end = '')
        print()
    print()


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

int euq(int a, int b) {return b ? euq(b, a%b) : a;}

int main() 
{
    int N;
    cin >> N;
    int res = 0;
    rep(i, N) 
    {
        int a;
        cin >> a;
        res = euq(res, a);
    }
    cout << res << endl;
    return 0;


}


Measured memory_bytes: 256
# require 'marshal'
input = gets.chomp.split
n = input[0].to_i
k = input[1].to_i
v = gets.chomp.split(" ").map(&:to_i)

m = [n,k].min

right = v[-m,m]
left = v[0,m]

right.reverse!

right_sum = []
left_sum = []

rsum = 0
rstack = []
lsum = 0
lstack = []

for i in 1..m do
    r = right[i-1]
    rsum += r
    if r < 0
        rstack.push(r)
    end
    stack = Marshal.load(Marshal.dump(rstack))
    right_sum.push([rsum,stack])

    l = left[i-1]
    lsum += l
    if l < 0
        lstack.push(l)
    end
    stack = Marshal.load(Marshal.dump(lstack))
    left_sum.push([lsum,stack])
end

score_max = 0

# 左から0個とる場合
for r in 1..m do
    score = right_sum[r-1][0]
    stack = right_sum[r-1][1]
    ret = [k-r,stack.size].min
    sum = 0
    stack.sort!
    for i in 1..ret
        sum += stack[i-1]
    end
    score += sum * -1
    if score_max  < score
        score_max = score
    end
end

# 左からl個とる場合 (1 <= l <= m)
for l in 1..m do
    score = left_sum[l-1][0]
    stack = left_sum[l-1][1]
    for r in 1..(m-l) do
        score2 = right_sum[r-1][0] + score
        stack2 = right_sum[r-1][1] + stack.dup
        ret = [k-l-r,stack2.size].min
        sum = 0
        stack2.sort!
        for i in 1..ret
            sum += stack2[i-1]
        end
        score2 += sum * -1
        if score_max  < score2
            score_max = score2
        end
    end

        ret = [k-l,stack.size].min
    sum = 0
    stack.sort!
    for i in 1..ret
        sum += stack[i-1]
    end
    score += sum * -1
    if score_max  < score
        score_max = score
    end
end

p score_max

Measured memory_bytes: 2044
kaisuu = input()
result = list()

suu = [0, 0, 0, 0]
judge = ["AC", "WA", "TLE", "RE"]

for i in range(int(kaisuu)):
    result.append(input())

for i in range(int(kaisuu)):
    if result[i] == "AC":
        suu[0] += 1
    if result[i] == "WA":
        suu[1] += 1
    if result[i] == "TLE":
        suu[2] += 1
    if result[i] == "RE":
        suu[3] += 1

for i in range(4):
    print (judge[i],"x",suu[i])

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

int main()
{
    int X, sum=0;
    cin >> X;

    for (int i = 1; i < 100;i++){
        for (int j = 2; j < 100;j++){
            if(pow(i,j)<=X && sum<=pow(i,j)){
                sum = pow(i, j);
            }
        }
    }
    cout << sum << endl;
}


Measured memory_bytes: 640
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

#define int long long int
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()
#define capi(x) int x;cin>>x

signed main() {
    capi(n);

    set<int> s;
    rep(i, n) {
        capi(a);
        s.insert(a);
    }

    if (s.size() == n) cout << "YES";
    else cout << "NO";

    return 0;
}

Measured memory_bytes: 12392
from collections import defaultdict

def main():

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

    if all(a[0] == a[i] for i in range(n)):
        print(0)
        exit()

    pre = [1<<i for i in range(n)]
    bitcnt = [0]*(1<<n)
    bitcnt[1] = 1
    for i in range(1<<(n-1)):
        bitcnt[2*i] = bitcnt[i]
        bitcnt[2*i+1] = bitcnt[i]+1

    DP = [defaultdict(lambda : float("inf")) for i in range(n)]

    for i in range(n):
        DP[i][1<<i] = 0
    s = defaultdict(lambda : float("inf"))
    for i in range(n-1):
        nxt = [defaultdict(lambda : float("inf")) for i in range(n)]
        for j in range(n):
            last = a[j] if (i-j)%2 == 0 else b[j]
            for state, cost in DP[j].items():
                for to in range(n):
                    if state>>to&1:
                        continue
                    nlast = a[to] if (i+1-to)%2 == 0 else b[to]
                    if last > nlast:
                        continue
                    nstate = state|pre[to]
                    ncost = cost
                    ncost += bitcnt[state>>to]
                    if s[(nstate, nlast)] <= ncost:
                        continue
                    s[(nstate, nlast)] = ncost
                    if nxt[to][nstate] > ncost:
                        nxt[to][nstate] = ncost
        DP = nxt
    mask = (1<<n)-1
    ans = float("inf")
    for i in range(n):
        ans = min(ans, DP[i][mask])
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 133116
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forenska(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-x))
#define GREATER(x) x,vector<x>,greater<x>
#define randIn(L,R) (((LL)rand()*rand())%(R-L)+L)
#define __lcm(a,b) ((a)/__gcd(a,b)*(b))
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<int> vec;
typedef vector<LL> vecL;
typedef vector<pii> vecP;
typedef vector<pLL> vecPL;
typedef vector<string> vecS;
typedef vector<vec> mat;
typedef complex<double> point;
const long double PI=3.14159265358979323846264338327950288419716939937510582097;
const LL INFLL=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f;
const long double EPS=1e-10;
int read()
{
    int x=0;
    char ch=' ';
    bool flag=false;
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')flag=true;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return flag?-x:x;
}

int main()
{
	int A,B,C;
	cin>>A>>B>>C;
	int ans=0;
	while(A%2!=B%2 || B%2!=C%2 || A%2!=C%2)
	{
		if(A%2==B%2)A++,B++,ans++;
		else if(B%2==C%2)B++,C++,ans++;
		else if(A%2==C%2)A++,C++,ans++;
	}
	while(A!=B || B!=C || A!=C)
	{
		if(A<B || A<C)A+=2,ans++;
		if(B<A || B<C)B+=2,ans++;
		if(C<A || C<B)C+=2,ans++;
	}
	cout<<ans<<endl;
	return 0;
}



Measured memory_bytes: 256
def main():
    r, c = [int(x) for x in input().split()]

    matrix = []
    totals = [0 for _ in range(c+1)]

    for _ in range(r):
        nums = [int(x) for x in input().split()]
        total = sum(nums)

        row = nums + [total]
        matrix.append(row)

        for i in range(c+1):
            totals[i] += row[i]

    matrix.append(totals)

    for m in matrix:
        print(" ".join([str(x) for x in m]))

if __name__ == '__main__':
    main()



Measured memory_bytes: 5680
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

A,B=input().split()
AA=int(A)
BB=int(B[0])*100 + int(B[2])*10 + int(B[3])
 
print((AA*BB)//100)

Measured memory_bytes: 9168
package main

import "fmt"

func main() {
	var times [5]int
	lasti := 0
	lastv := 10
	for i := 0; i < 5; i++ {
		fmt.Scanln(&times[i])

		d := times[i] % 10
		if d != 0 && d < lastv {
			lastv = d
			lasti = i
		}
	}
	// fmt.Println(times)
	// fmt.Println(lasti, lastv)

	total := 0
	for i, t := range times {
		if i == lasti {
			continue
		}
		if t%10 == 0 {
			total += t
		} else {
			total += (t/10 + 1) * 10
		}
		// fmt.Println(total)
	}
	total += times[lasti]
	fmt.Println(total)
}


Measured memory_bytes: 512
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
// scanf("%d", &x); => std::cin >> x;
// printf("%d\n", x); => std::cout << x << "\n";

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

    long long ans = 0;
    for (int i = 1; i < n; i++) {
        if (ary[i] < ary[i - 1]) {
            ans += (ary[i - 1] - ary[i]);
            ary[i] += (ary[i - 1] - ary[i]);
        }
    }

    printf("%lld\n", ans);
    return 0;
}

Measured memory_bytes: 4496
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
#include <numeric>



const int mod = 1000000007;

int gcm(int a, int b)
{

	int r, tmp;

	/* 自然数 a > b を確認・入替 */
	if (a < b) {
		tmp = a;
		a = b;
		b = tmp;
	}

	/* ユークリッドの互除法 */
	r = a % b;
	while (r != 0) {
		a = b;
		b = r;
		r = a % b;
	}

	/* 最大公約数を出力 */
	return b;
}


int main() {
	int N, M;
	scanf("%d %d", &N, &M);
	char *S = new char[N + 1];
	char *T = new char[M + 1];
	scanf("%s", S);
	scanf("%s", T);
	int G = gcm(N, M);
	bool can = true;
	for (int i = 0; i < G; i++) {
		if (S[i*(N / G)] != T[i*(M / G)]) {
			can = false;
		}
	}
	if (can) {
		printf("%lld", (long long)(N)*M / G);
	}
	else {
		printf("-1");
	}
}





Measured memory_bytes: 384
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn = 1e5+10;
vector<int>g[maxn]; 
int vis[maxn];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		vis[a]++;
		vis[b]++;
	 } 
	 int flag = 1;
	 for(int i=1;i<=n;i++)
	 {
	 		if(vis[i]%2)
	 		{
	 			flag=0;
	 			break;
			 }
	  } 
	  if(flag)
	  {
	  	cout<<"YES"<<endl; 
	  }
	  else
	  {
	  	cout<<"NO"<<endl;
	  }
	return 0;
}

Measured memory_bytes: 3072
package main

import "fmt"

func main() {
	var s string
	fmt.Scan(&s)
	if len(s) == 2 {
		fmt.Println(s)
	} else {
		r := make([]byte, len(s))
		for i := 0; i < len(s); i++ {
			r[i] = s[len(s)-i-1]
		}
		fmt.Println(string(r))
	}
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int D,N;
    cin >> D >> N;
  int ans = 0;
  if(D == 0){
    ans = 1;
    if(N<=99) cout << ans*N << endl;
    else cout << 101 << endl;
  }
  
  if(D == 1){
   ans = 100;
  if(N<=99) cout << ans * N << endl;
  else cout << 10100 << endl;
  }
  
  if(D == 2){
   ans = 10000;
   if(N<=99) cout << ans * N << endl;
    else cout << 1010000 << endl;
  }

}


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


typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++);
#define _GLIBCXX_DEBAC
/*map<Keyの型, Valueの型> 変数名;
queue<型> 変数名;
priority_queue<型> 変数名;
priority_queue<型, vector<型>, greater<型>> 変数名;*/

using namespace std;

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

int sort_greater(int m,int n) {
	vector<int> a(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end(),greater<int>());
	for (int i = 0; i < n; i++) {
		cout << a[i];
	}
		return 0;
}

int bubblesort(int a[], int n) {
	int sw = 0;
	bool flag = 1;
	for (int i = 0; flag; i++) {
		flag = 0;
		for (int j = n - 1; j >= i + 1; j--) {
			if (a[j] < a[j - 1]) {
				swap(a[j], a[j - 1]);
				flag = 1;
				sw += 1;
			}
		}
	}
	return sw;
}

int a_z()
{
	for (int i = 0; i <= ('Z' - 'A'); i++) {
		cout << (char)('A' + i);
	}
	return 0;
}

int selecttionsort(int a[], int n) {
	int t,sw = 0, minj;
	for (int i = 0; i < n - 1; i++) {
		minj = i;
		for (int j = 0; j < n - 1; j++) {
			if (a[j] < a[minj]) {
				minj = j;
			}
		}
		t = a[i]; a[i] = a[minj]; a[minj] = t;
		if (i != minj) sw += 1;

	}
	return sw;
}
map<string, int> memo;


int main() {
	//sort(a.begin(),a.end());
	ll n, a, b,c;
	cin >> n >> a >> b;
	ll ans = 0;
	c = n % (a + b);
	ans = a * (n / (a + b)) + min(c, a);
	if (a == 0) cout << 0; else cout << ans;
	return 0;
}









Measured memory_bytes: 256
using System;

namespace ABC113B
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            var target = Console.ReadLine().Split(' ');
            int T = int.Parse(target[0]);
            int A = int.Parse(target[1]);
            target = Console.ReadLine().Split(' ');
            int altitude = int.Parse(target[0]);
            double min_temp = Math.Abs(T - altitude * 0.006 - A); 
            int min_ind = 0;
            for(int i = 1; i < N; i++){
                altitude = int.Parse(target[i]);
                double temp = Math.Abs(T - altitude * 0.006 - A); 
                if(temp < min_temp){
                    min_ind = i;
                    min_temp = temp;
                }
            }
            Console.WriteLine("{0}", min_ind + 1);
        }
    }
}


Measured memory_bytes: 13212
// Package main provides
//
// File:  main.go
// Author: ymiyamoto
//
// Created on Fri Aug 16 09:54:04 2019
//
package main

import (
	"fmt"
	"sort"
	"strings"
)

func main() {
	var n int
	fmt.Scan(&n)
	arr := make([]string, n)
	for i := range arr {
		fmt.Scan(&arr[i])
	}

	nums := map[string]int{}
	ans := 0
	for i := len(arr) - 1; i >= 0; i-- {
		fields := strings.Split(arr[i], "")
		sort.Strings(fields)
		str := strings.Join(fields, "")
		ans += nums[str]
		nums[str]++
	}
	fmt.Println(ans)
}


Measured memory_bytes: 17408
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void){ 
	int cnt = 0,i;
	char str[256],data[256];
	char end[100] = "END_OF_TEXT";
	scanf("%s",str);
	for(i = 0;i < strlen(str);i++){
		if(isupper(str[i])){
			str[i] = tolower(str[i]);
		}
	}
	while(1){
		scanf("%s",data);
		if(strcmp(data,end)==0){
			break;
		}
		for(i = 0;i < strlen(data);i++){
			if(isupper(data[i])){
				data[i] = tolower(data[i]);
			}
		}
		if(strcmp(str,data)==0){
			cnt++;
		}
//		puts(data);
	}
	printf("%d\n",cnt);
	
	
	return 0;
}

Measured memory_bytes: 552
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <numeric>

using namespace std;


int main()
{
    string s,sr,s1,s1r,s2,s2r, ans;
    cin >> s;
    sr = s;
    reverse(sr.begin(), sr.end());

    int n = s.length();
    int p1 = (n-1)/2;
    int p2 = (n+3)/2;

    s1 = s.substr(0,p1);
    s1r = s1;
    reverse(s1r.begin(), s1r.end());
    s2 = s.substr(p2-1,n-p2+1);
    s2r = s2;
    reverse(s2r.begin(), s2r.end());

    //cout << s << ' ' << s1  << ' ' << s2  << endl;
    if(s == sr && s1 == s1r && s2 == s2r)
    {
        ans = "Yes";
    }
    else
    {
        ans = "No";
    }

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


Measured memory_bytes: 256
def run(S):
    '''
    Bをすべて右に、Wをすべて左に移動する回数を求める
    '''
    cnt = 0
    n = len(S)
    cnt_B = 0
    cnt_W = 0
    for i in range(len(S)):
        if S[i] == 'B':
            cnt_B += 1
            cnt += n - (i+cnt_B)
        else:
            cnt += i - cnt_W
            cnt_W += 1
    return int(cnt / 2)


def main():
    S = input()
    print(run(S))


if __name__ == '__main__':
    main()


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

using namespace std;

#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define np(v) next_permutation(v.begin(), v.end())
#define pll pair < long long, long long>
#define all(a) a.begin(), a.end()
#define ull unsigned long long
#define vll vector <long long>
#define pii pair < int, int >
#define sz(a) (int)a.size()
#define sqr(x) ((x) * (x))
#define y1 stupid_cmath
#define vi vector <int>
#define pb push_back
#define mp make_pair
#define ll long long
#define f first
#define s second

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

int n, x;
int a[100100];

int main(){
    ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> x;
	for(int i = 1; i <= n; ++i){
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	int ans = 0;
	bool was = 0, was1 = 0;
	for(int i = 1; i <= n; ++i){
		if(x >= a[i]){
			x -= a[i];
			ans++;
		}else{
			x = 0;
		}
	}
	if(x != 0) ans--;
	cout<< ans << endl;
}

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

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  if (N%2 != 0) cout << "No" << endl;
  else {
    bool ans = true;
    for (ll i = 0; i < N/2; ++i) {
      if (S[i] != S[i+N/2]) ans = false;
      if (!ans) break;
    }
    if (ans) cout << "Yes" << endl;
    else cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
string s;
int l;
vint num(11);
int solve(int x){
    int ret=0;
    if(x==l){
        bool f = false;
        bool z = false;
        vint tfs(3);
        REP(i,l){
            if(num[i]==0){
                if(z){
                    return 0;
                }else{
                    f=true;
                }
            }else{
                z=true;
                if(f==false&&(s[i]-'0')<num[i]){
                    return 0;
                }
                if(f==false){
                    if((s[i]-'0')>num[i]){
                        f=true;
                    }
                }
            }
            if(num[i]==3){
                tfs[0]=1;
            }else if(num[i]==5){
                tfs[1]=1;
            }else if(num[i]==7){
                tfs[2]=1;
            }
        }
        REP(i,3){
            if(tfs[i]==0){
                return 0;
            }
        }
        return 1;
    }
    num[x]=0;
    ret += solve(x+1);
    num[x]=3;
    ret += solve(x+1);
    num[x]=5;
    ret += solve(x+1);
    num[x]=7;
    ret += solve(x+1);
    return ret;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> s;
    l = (int)s.size();
    int ans = solve(0);
    cout << ans << endl;    
    return 0;
}

Measured memory_bytes: 256
import java.util.*;
class Main{
  public static void main(String args[]){
    Scanner s=new Scanner(System.in);
    String S=s.next();
    int r=0;
    char b=S.charAt(0);
    for(int i=1;i<S.length();i++){
      if(S.charAt(i)==b){
        r++;
        b=(S.charAt(i)=='0'?'1':'0');
      }
      else b=S.charAt(i);
    }
    System.out.println(r);
  }
}


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

int main()
{

  long long x ;
  cin >> x ;

  long long x1 ;
  long long x2 ;
  x1 = 0 ;
  x2 = 0 ;

  x1 = x/500 ;
  x-= x1*500 ;

  x2 = x/5 ;

  cout << x1*1000+x2*5 << endl;

    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ld dp[555][111][111];
int h, n, p, m, k;
vi hr, sum;

ld solve(int hei, int bar, int use)
{
	assert(!(bar < 0 || bar >= n || use < 0));
	if (hei == h)
	{

		assert(use == 0);
		if (bar == p) return 1.0;
		else return 0.0;
	}
	if (dp[hei][bar][use] >= 0)
	{
		return dp[hei][bar][use];
	}
	ld res = 0;
	if (hr[hei] != -1)
	{
		if (hr[hei] == bar) res = solve(hei + 1, bar + 1, use);
		else if (hr[hei] == bar - 1) res = solve(hei + 1, bar - 1, use);
		else res = solve(hei + 1, bar, use);
	}
	else
	{
		ld useexp = use / (ld)(h - hei - (m - sum[hei]));
		if (use > 0)
		{
			if (bar == 0)
			{
				res += useexp * (1.0 / (n - 1)) * solve(hei + 1, bar + 1, use - 1);
				res += useexp * (1.0 - 1.0 / (n - 1)) * solve(hei + 1, bar, use - 1);
			}
			else if (bar == n - 1)
			{
				res += useexp * (1.0 / (n - 1)) * solve(hei + 1, bar - 1, use - 1);
				res += useexp * (1.0 - 1.0 / (n - 1)) * solve(hei + 1, bar, use - 1);
			}
			else
			{
				res += useexp * (1.0 / (n - 1)) * solve(hei + 1, bar + 1, use - 1);
				res += useexp * (1.0 / (n - 1)) * solve(hei + 1, bar - 1, use - 1);
				res += useexp * (1.0 - 2.0 / (n - 1)) * solve(hei + 1, bar, use - 1);
			}
			if(useexp < 1) res += (1.0 - useexp) * solve(hei + 1, bar, use);
		}
		else
		{
			res += solve(hei + 1, bar, use);
		}
	}
	dp[hei][bar][use] = res;
	return res;
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	REP(i, 555)REP(j, 111)REP(k, 111) dp[i][j][k] = -1;
	cin >> h >> n >> p >> m >> k;
	p--;
	hr.resize(h, -1);
	sum.resize(h + 1);
	REP(i, m)
	{
		int a, b; cin >> a >> b;
		a--; b--;
		hr[a] = b;
		sum[a]++;
	}
	REP(i, h) sum[i + 1] += sum[i];
	ld ans = 0;
	REP(i, n)
	{
		chmax(ans, solve(0, i, k));
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 56796
#include<iostream>
using namespace std;
long mod=1e9+7;
long power(long a,long b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
int N;
int cnt[1<<20];
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		int a;cin>>a;cnt[a]++;
	}
	for(int i=0;i<20;i++)for(int j=0;j<1<<20;j++)if(!(j>>i&1))cnt[j]+=cnt[j|1<<i];
	long ans=0;
	for(int i=0;i<1<<20;i++)
	{
		long now=power(2,cnt[i]);
		ans+=__builtin_popcount(i)%2?-now:now;
	}
	cout<<(ans%mod+mod)%mod<<endl;
}



Measured memory_bytes: 7172
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>

typedef long long int ll;

using namespace std;

int main(){
    while(true){
        int a,b;
        cin >> a >> b;
        if (a==0 && b==0){
            break;
        }
        cout << (a<b?a:b) << " " << (a<b?b:a) << endl;
    }
    return 0;
}



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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rREP(i, k, n) for (int i = (int)(n)-1; i >= k; i--)

#define fi first 
#define se second

#define vi vector<int>
#define pb push_back
#define mp make_pair

#define pcnt __builtin_popcount

typedef long long ll;

const int inf = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int main() {
  int num = 0;
  char S[4];
  cin >> S;

  rep(i, 4) {
    if(S[i]=='+') num++;
    if(S[i]=='-') num--;
  }

  cout << num << endl;

  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main ()
{
  int H, W, i, j;
  while(1){
    scanf("%d %d",&H, &W);
    if(H == 0 && W == 0)break;
    for(i = 0;i<H;i++){
      for(j = 0;j<W;j++){
	if(i%2 == 0){
	  if(j%2 ==0){
	    printf("#");
	  }
	  else{
	    printf(".");
	  }
	}
	else{
	  if(j%2 ==0){
	    printf(".");
	  }
	  else{
	    printf("#");
	  }
	}
      }
      printf("\n");
    }
    printf("\n");
  }
  return 0;
}

Measured memory_bytes: 572
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define all(ppp) ppp.begin(),ppp.end()
#define out(ppp) cout<<(ppp)<<endl
#define out1(ppp) cout<<(ppp) 

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define pb push_back

typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<vpi> vvpi;

typedef set<pi> spi;

const int INF=INT_MAX;

ll solve(ll n){
    if(n==1){
        return 1;
    }else if(n<1){
        return 0;
    }
    return 1+2*solve(n/2);
}



int main(){
    ll H;
    cin>>H;
    ll ans=solve(H);
    out(ans);
    return 0;
}


Measured memory_bytes: 256
def main():
    n=int(input())
    lst=tuple(tuple(map(int,input().split())) for _ in range(n))

    dp=[[0]*n for _ in range(n)]
    for i in range(1,n):
        for j in range(0,n-i):
            dp[j][j+i]=dp[j+1][j+i]+lst[j][0]*lst[j][1]*lst[j+i][1]
            for k in range(1,i):
                nwork=dp[j][j+k]+dp[j+k+1][j+i]+lst[j][0]*lst[j+k][1]*lst[j+i][1]
                if dp[j][j+i]>nwork : dp[j][j+i]=nwork
    
    print(dp[0][n-1])

main()


Measured memory_bytes: 5724
import java.io.*;
import java.util.*;

class Main{
    public static void main(String[] args){
	BufferedReader sc=new BufferedReader(new InputStreamReader(System.in));
	try {
	    String st;
	    while((st=sc.readLine())!=null){
		double s, now, n = Double.valueOf(st);
		s = n; now = n;
		for(int i=2; i<=10; i++){
		    if(i%2==0)
			now *= 2;
		    else
			now /= 3;
		    s+=now;
		}
		System.out.println(s);
	    }
	}catch(Exception e){
	    System.out.println("Error");
	}
    }
}

Measured memory_bytes: 17084
n,k=gets.split(" ").map{|e| e.to_i}
a=[]
n.times{
	a<<gets.to_i
}
a.sort!
k-=1
ans=a[k]-a[0]
(k..(n-1)).each{|t|
	ans=[ans,a[t]-a[t-k]].min
}
puts ans

Measured memory_bytes: 4016
import java.util.Scanner;

public class Main {
  private static int countPower(int n) {
    int count = 0;
    while (n % 2 == 0) {
      ++count;
      n /= 2;
    }
    return count;
  }

  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int ans = 100;
    for (int i = 0; i < n; ++i) {
      int a = sc.nextInt();
      ans = Math.min(ans, countPower(a));
    }

    System.out.println(ans);
    
    sc.close(); 
  }
}


Measured memory_bytes: 22996
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
 
int n;
long long W, dp[110][100100], weight[110], value[110];

int main(){
  cin >> n >> W;
  for(int i = 0; i < n; ++i) cin >> weight[i] >> value[i];
  
  for(int w = 0; w <= W; ++w) dp[0][w] = 0;
  
  for(int i = 0; i < n; ++i){
    for(int w = 0; w <= W; ++w){
      if(w >= weight[i]) dp[i+1][w] = max(dp[i][w-weight[i]] + value[i], dp[i][w]);
      else dp[i+1][w] = dp[i][w];
    }
  }
  cout << dp[n][W] << endl;
}

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

using namespace std;

#define rep(i, x)   for (__typeof(x) i = 0; i < x; i++)
#define mp             make_pair
#define pb             push_back
#define fi             first
#define se             second
#define sz(x)          (int)((x).size())
#define all(x)         (x).begin(), (x).end()
#define sci(x)         int x;       scanf("%d", &x);
#define scii(x, y)     int x, y;    scanf("%d %d", &x, &y);
#define sciii(x, y, z) int x, y, z; scanf("%d %d %d", &x, &y, &z);
#define TC(x)          sci(x); while(x --)
#define eprintf(...)   fprintf(stderr, __VA_ARGS__)
#define debug(x)       { cerr << #x << " = " << x << endl; }
#define repi(i, x)  for (__typeof(x) i = x-1; i >= 0; i --)
#define fore(itr, x)   for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr ++)
#define forei(itr, x)  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr --)
typedef long long      ll;
typedef pair<int, int> ii;
typedef pair<ii, int>  iii;
typedef vector<int>    vi;
typedef vector<ii>     vii;
typedef vector<iii>    viii;
typedef vector<vector<int> > vvi;
const   int            infi = numeric_limits<int>::max();
const   double         eps = 0;
const   int            ms  = 0;



int lowerbound(vector<ll>& a, ll x, int N) {
    int low = -1;/////
    int high = N;//////array.size /// not N-1
    
    while (abs(high - low) > 1){
        int mid = (high + low)/ 2;
        if(a[mid] >= x){///////////
            high = mid;
        }
        else{
            low = mid;
        }
    }
    
    return high;
    
}


int main()
{
    ios::sync_with_stdio(false);//endl->"\n"
    cin.tie(0);

    ll A, B, Q;
    cin >> A >> B >> Q;
    vector<ll> S_(A), T_(B), X(Q);
    rep(i, A)cin >> S_[i];
    rep(i, B)cin >> T_[i];
    rep(i, Q)cin >> X[i];
    //sort(all(S));
    //sort(all(T));

    vector<ll> S, T;
    S.pb(-(numeric_limits<ll>::max()/10));
    rep(i, A)S.pb(S_[i]);
    S.pb(numeric_limits<ll>::max()/10);

    T.pb(-(numeric_limits<ll>::max()/10));
    rep(i, B)T.pb(T_[i]);
    T.pb(numeric_limits<ll>::max()/10);

    rep(q, Q){
        ll s0, s1;
        ll x = X[q];
        int si = lowerbound(S, X[q], S.size());
        s0 = S[si-1];
        s1 = S[si];
        ll t0;
        ll t1;
        int ti = lowerbound(T, X[q], T.size());
        t0 = T[ti-1];
        t1 = T[ti];
        ll d = min(t1-s0+min(x-s0, t1-x), s1-t0 + min(x-t0, s1-x));
        d = min(d, min(x-min(t0, s0), max(t1, s1)-x));
        cout << d << "\n";
    }

    return 0;
}

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

using namespace std;
using ll = long long;
const int MAXN = 20;
int ar[MAXN][MAXN];
int c[MAXN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, x;
  cin >> n >> m >> x;
  for(int i = 0; i < n; ++i) {
    cin >> c[i];
    for(int j = 0; j < m; ++j) {
      cin >> ar[i][j];
    }
  }
  const int INF = 1.01e9;
  int res = INF;
  for(int mask = 0; mask < (1 << n); ++mask) {
    int cost = 0;
    vector<int> of(m);
    for(int i = 0; i < n; ++i) {
      if(mask & (1 << i)) {
        cost += c[i];
        for(int j = 0; j < m; ++j) of[j] += ar[i][j];
      }
    }
    bool ok = true;
    for(int i: of) {
      ok &= (i >= x);
    }
    if(ok) res = min(res, cost);
  }
  cout << (res == INF ? -1 : res) << '\n';
  return 0;
}


Measured memory_bytes: 3672
#include<iostream>
#include<cstdio>
#include<cstring>


const int INF = 0x3f3f3f3f;

using namespace std;

char maze[1010][1010];
int H, W, N;
int d[1010][1010];
int dx[4] = { 0,0,1,-1 }, dy[4] = { 1, -1, 0, 0 };
int sx[10], sy[10];
int ans = 0;

struct QNode
{
	int data_sx[1010];
	int data_sy[1010];
	int front, rear;
	int maxsize;
};
typedef struct QNode *Queue;

Queue Create()
{
	Queue Q;
	Q = (Queue)malloc(sizeof(struct QNode));
	Q->front = Q->rear = 0;
	Q->maxsize = 1010;
	return Q;
}
void push(Queue Q,int x,int y)
{
	if ((Q->rear + 1) % Q->maxsize == Q->front)
		return;
	Q->rear = (Q->rear + 1) % Q->maxsize;
	Q->data_sx[Q->rear] = x;
	Q->data_sy[Q->rear] = y;
}
int pop1(Queue Q)
{
	if (Q->front == Q->rear)
		return -1;
	Q->front = (Q->front + 1) % Q->maxsize;
	return Q->data_sx[Q->front];
}
int pop2(Queue Q)
{
	return Q->data_sy[Q->front];
}


void bfs(int i)
{
	Queue Q;
	Q = Create();
	memset(d, 0x3f, sizeof(d));
	push(Q, sx[i], sy[i]);
	d[sx[i]][sy[i]] = 0;
	if (i == N)
		return;
	while (Q->front != Q->rear)
	{
		int x = pop1(Q), y = pop2(Q);
		if (x == sx[i + 1] && y == sy[i + 1])
			return;

		for (int j = 0; j < 4; j++)
		{
			int nx = x + dx[j], ny = y + dy[j];
			if (nx >= 0 && nx < H && ny >= 0 && ny < W && d[nx][ny] == INF && maze[nx][ny]!='X')
			{
				push(Q, nx, ny);
				d[nx][ny] = d[x][y] + 1;
			}
		}
	}
}

void solve()
{
	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			if (maze[i][j] == 'S')
				sx[0] = i, sy[0] = j;
			else if (maze[i][j] >= '1'&&maze[i][j] <= '9')
				sx[maze[i][j] - '0'] = i, sy[maze[i][j] - '0'] = j;
		}
	}
	for (int i = 0; i < N; i++)
	{
		bfs(i);
		ans += d[sx[i + 1]][sy[i + 1]];
	}
	cout << ans << endl;
}

int main()
{
	cin >> H >> W >> N;

	for (int i = 0; i < H; i++)
		for (int j = 0; j < W; j++)
			cin >> maze[i][j];
	solve();
}


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

int main() {
    int N;
    cin >> N;
    vector<long long> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    deque<long long> que; que.push_back(A[0]);
    int l = 1, r = N-1;
    while (l != r) {
        long long L = que.front(), R = que.back();
        long long a1 = abs(A[l]-L), a2 = abs(A[l]-R), a3 = abs(A[r]-L), a4 = abs(A[r]-R);
        long long mx = max({a1, a2, a3, a4});
        if (mx == a1) {
            que.push_front(A[l]);
            l++;
        } else if (mx == a2) {
            que.push_back(A[l]);
            l++;
        } else if (mx == a3) {
            que.push_front(A[r]);
            r--;
        } else if (mx == a4) {
            que.push_back(A[r]);
            r--;
        }
    }
    long long M = A[l];
    long long a1 = abs(M-que.front()), a2 = abs(M-que.back());
    if (a1 < a2) que.push_back(M);
    else que.push_front(M);
    
    long long now = que.front(); que.pop_front();
    long long ans = 0;
    while (!que.empty()) {
        long long nxt = que.front(); que.pop_front();
        ans += abs(now-nxt); now = nxt;
    }
    
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 2428
#include <bits/stdc++.h>
using namespace std;
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; }
using ll = long long;
using P = pair<int, int>;
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 k, n; cin >> n >> k;
    vector<int> pvec(n, 0);
    for(int i = 0; i < n; ++i) cin >> pvec.at(i);
    sort(pvec.begin(), pvec.end());
    int sum = 0;
    for(int i = 0; i < k; ++i) sum += pvec.at(i);
    cout << sum << endl;
}

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

using namespace std;

#define MAXN 100000
#define MAXV 2500

int prime[MAXV+5];
long long x[MAXN+5], y[MAXN+5];
vector<long long> primes;
map<long long,long long> gs;
set<long long> v;

int main()
{
    for (long long i = 2; i <= MAXV; ++i) {
        if (prime[i] == 0) {
            primes.push_back(i);
            int t = i*i;
            while (t <= MAXV) {
                prime[t] = 1;
                t += i;
            }
        }
    }

    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        long long a;
        scanf("%lld", &a);

        long long tmp = a;
        long long k = 1, n = 1;
        for (auto &p: primes) {
            int cnt = 0;
            while (tmp % p == 0) {
                tmp /= p;
                ++cnt;
            }
            if (cnt % 3 == 1) {
                k *= 1LL*p*p;
                n *= 1LL*p;
            }
            if (cnt % 3 == 2) {
                k *= 1LL*p;
                n *= 1LL*p*p;
            }

        }
        n *= tmp;

        long long s = sqrt(tmp);
        while (s*s < tmp) ++s;
        while (s*s > tmp) --s;
        if (s*s == tmp) {
            k *= s;
        }
        else {
            k *= tmp*tmp;
        }

        x[i] = k; y[i] = n;
        if (n > 1) {
            gs[k]++;
        }
    }

    int ans = 0;
    int nocube = 1;
    for (int i = 0; i < n; ++i) {
        if (x[i] == 1) {
            if (nocube) {
                ++ans;
                nocube = 0;
            }
            continue;
        }
        if (v.find(x[i]) == v.end() && v.find(y[i]) == v.end()) {
            ans += max(gs[x[i]], gs[y[i]]);
            v.insert(x[i]);
            v.insert(y[i]);
        }
    }
    printf("%d\n", ans);

    return 0;
}


Measured memory_bytes: 23680