akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define rrp(i,a,b) for (int i = (a); i >= (b); -- i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int,int> pii;
typedef long double db;
typedef long long ll;
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
  x = x < y ? y : x;
}

const int MOD = 998244353;
inline void Add(int& x,int y) {
  x = x + y >= MOD ? x + y - MOD : x + y;
}
inline void Sub(int& x,int y) {
  x = x - y < 0 ? x - y + MOD : x - y;
}
int power(int a,int b) {
  int ret = 1;
  while (b) {
    if (b&1) ret = 1ll * ret * a % MOD;
    a = 1ll * a * a % MOD;
    b >>= 1;
  }
  return ret;
}

const int N = 18;
int a[1 << N], b[1 << N], c[1 << N], d[1 << N], n;
void fwt(int *v,int sgn) {
  rep (i, 0, n-1) rep (s, 0, (1 << n)-1) if ((s >> i)&1) {
    int l = v[s ^ (1 << i)];
    int r = v[s];
    v[s ^ (1 << i)] = (l + r >= MOD ? l + r - MOD : l + r);
    v[s] = (l - r < 0 ? l - r + MOD : l - r);
  }
  if (sgn) {
    int tmp = power(1 << n, MOD - 2);
    rep (s, 0, (1 << n)-1) v[s] = 1ll * v[s] * tmp % MOD;
  }
}
int main() {
  read(n);
  int tmp = 0;
  rep (i, 0, (1 << n) - 1) read(a[i]), Add(tmp, a[i]);
  tmp = power(tmp, MOD - 2);
  rep (i, 0, (1 << n) - 1) a[i] = 1ll * a[i] * tmp % MOD;
  fwt(a, 0);
  rep (s, 0, (1 << n) - 1) {
    if (a[s] == 1) b[s] = 0;
    else {
      b[s] = a[s]; Sub(b[s], 1);
      b[s] = power(b[s], MOD - 2);
    }

    if (a[s] == 1) c[s] = 1;
    else c[s] = 0;
  }
  fwt(b, 1);
  fwt(c, 1);
  d[0] = 0;
  rep (s, 1, (1 << n) - 1) {
    // (a' b - b' a) / (b*b)
    d[s] = (1ll * b[s] * c[0] - 1ll * b[0] * c[s]) % MOD;
    if (d[s] < 0) d[s] = d[s] + MOD;
    d[s] = 1ll * d[s] * power(1ll * c[0] * c[0] % MOD, MOD - 2) % MOD;
  }
  rep (s, 0, (1 << n) - 1)
    printf("%d\n", d[s]);
  return 0;
}


Measured memory_bytes: 6912
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		long[] a = new long[n];
		long v = 1;
		boolean zeroFlg = false;

		long max = (long) Math.pow(10, 18);
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextLong();
			if (a[i] == 0) {
				zeroFlg = true;
				break;
			}
		}
		if (zeroFlg) {
			v = 0;
		} else {
			for (long a1 : a) {
				if (max / v >= a1) {
					v *= a1;
				} else {
					v = -1;
					break;
				}
			}
		}
		System.out.println(v);
	}
}

Measured memory_bytes: 61056
import math
import sys
import numpy as np
from collections import defaultdict, deque, Counter 
from itertools import accumulate, product, permutations,combinations, combinations_with_replacement
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd

max_value = 1000000000
min_value = -1000000000
sys.setrecursionlimit(max_value)


n = int(input())
a_list = [int(input()) for i in range(n)]


argmax = np.argmax(a_list)
max_value = np.max(a_list)
second_max_value = np.max(a_list[:argmax] + a_list[argmax+1:])

for i in range(n):
    if i == argmax:
        print(second_max_value)
    else:
        print(max_value)




Measured memory_bytes: 25768
#include <iostream>
#include <stack>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <functional>
#include <string>
#include <cstdlib>
#include <numeric>
#include <cstdbool>
#include <cstring>
#include <map> 
#include <set>
#include <queue>
#include <iomanip>
#include <sstream>
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<ll, ll> P;
ll ma = 1000000000 + 7;
ll mx = 1000003;
ll h,n, w,m,t; string s;
ll R, C;
ll sy, sx, gy, gx;
char maze[510][510];
ll dis[20][20];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
ll gcd(ll x, ll y) {
	if (x % y == 0) return y;
	return gcd(y, x % y);
}
ll lcm(ll a,ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}
void comb(vector<vector <ll> >& v) {
	for (ll i = 0; i < v.size(); i++) {
		v[i][0] = 1;
		v[i][i] = 1;
	}
	for (ll k = 1; k < v.size(); k++) {
		for (int j = 1; j < k; j++) {
			v[k][j] = (v[k - 1][j - 1] + v[k - 1][j])%ma;
		}
	}
}

ll GetDigit(ll num) {
	return log10(num) + 1;
}
ll Combination(int n, int r)
{
	if (n - r < r) r = n - r;
	if (r == 0) return 1;
	if (r == 1) return n;

	vector<ll> numerator(r);
	vector<ll> denominator(r);

	for (int k = 0; k < r; k++)
	{
		numerator[k] = n - r + k + 1;
		denominator[k] = k + 1;
	}

	for (int p = 2; p <= r; p++)
	{
		int pivot = denominator[p - 1];
		if (pivot > 1)
		{
			int offset = (n - r) % p;
			for (int k = p - 1; k < r; k += p)
			{
				numerator[k - offset] /= pivot;
				denominator[k] /= pivot;
			}
		}
	}

	ll result = 1;
	for (int k = 0; k < r; k++)
	{
		if (numerator[k] > 1) result *= numerator[k];
		result %= ma;
	}

	return result;
}
/*
ll bfs() {
	queue<P> que;
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) dis[i][j] = INF;
	}
	que.push(P(sy - 1, sx - 1));
	dis[sy - 1][sx - 1] = 0;

	while (que.size() > 0) {
		P p = que.front();
		que.pop();
		if (p.first == gy - 1 && p.second == gx - 1) break;
		for (ll i = 0; i < 4; i++) {
			ll ny = p.first + dy[i];
			ll nx = p.second + dx[i];
			if (0 <= ny && ny < R && 0 <= nx && nx < C && maze[ny][nx] == '.' && dis[ny][nx] == INF) {
				que.push(P(ny, nx));
				dis[ny][nx] = dis[p.first][p.second] + 1;
			}
		}
	}
	return dis[gy - 1][gx - 1];
}*/

int main() {
	map<ll, ll> mp;
	ll x;
	cin >> x >> n;
	ll res = INF;
	rep(i, n) {
		ll a;
		cin >> a;
		mp[a]++;
		chmin(res, a);
	}
	ll d = INF;
	for(ll i=103;i>=0;i--) {
		if (abs(i - x) <= d && mp[i] == 0) {
			res = i; d = abs(i-x);
		}
	}
	cout << res << endl;
}

Measured memory_bytes: 3732
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>
#include <cassert>

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

int main() {
    string s;
    cin >> s;
    int n = s.size();
    cout << s.substr(0, n-8) << endl;
    return 0;
}

Measured memory_bytes: 3596
const int N = 400005;
const int LG = 21;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <unordered_set>
#include <bitset>
#include <deque>

using namespace std;

#define int long long
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(N);
    __ufact.resize(N);
    __rev.resize(N);
    __rev[1] = 1;
    for (int i = 2; i < N; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < N; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int n;
vector<int> graph[N];
int d1[N], dn[N];

signed main()
{
    cin >> n;
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    vector<int> q = {1};
    d1[1] = 1;
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (d1[u] == 0)
            {
                d1[u] = d1[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
    q = {n};
    dn[n] = 1;
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (dn[u] == 0)
            {
                dn[u] = dn[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
    int cnt1 = 0;
    for (int i = 1; i <= n; i++)
    {
        if (d1[i] <= dn[i]) cnt1++;
    }
    int cnt2 = n - cnt1;
    if (cnt1 > cnt2)
    {
        cout << "Fennec";
    }
    else
    {
        cout << "Snuke";
    }
}

/* Note:
Check constants at the beginning of the code.
N is set to 4e5 but be careful in problems with large constant factor.
Check corner cases.
No def int long long for now.
*/


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

#define ll long long
#define INF 99999999

int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    if(a < b && c < b) cout << a + c << endl;
    else if(b < a && c < a)cout << b+c << endl;
    else cout << b+a << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;

const long INF = (1<<30);
 
int main() {
  int n, m;
  cin >> n >> m;
  cout << n*(n-1)/2 + m*(m-1)/2 << endl;
}

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

int main(){
  stack<char> st;
  string s;
  cin >> s;
  for(int i=0;i<s.size();i++){
    if(s.at(i) == 'B' && !st.empty()){
      st.pop();
    }else if(s.at(i) == '0' || s.at(i) == '1'){
      st.push(s.at(i));
    }
  }
  
  string res="";
  while(!st.empty()){
    res += st.top();
    st.pop();
  }
  
  reverse(res.begin(),res.end());

  cout << res << endl;
  
  return(0);
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdint>
#include <string>
#include <vector>

constexpr intmax_t mod=1e9+7;

int main() {
  size_t N;
  char buf[64];
  scanf("%zu %s", &N, buf);
  std::string S[2]={buf};
  scanf("%s", buf);
  S[1] = buf;

  std::vector<int> a;
  for (size_t i=0; i<N; ++i) {
    if (S[0][i] == S[1][i]) {
      a.emplace_back(3);
    } else {
      a.emplace_back(6);
      ++i;
    }
  }

  intmax_t res=a[0];
  for (size_t i=1; i<a.size(); ++i) {
    if (a[i-1] == 6) {
      if (a[i] == 6) {
        res = (res*3%mod);
      }
    } else {
      if (a[i] == 3) {
        res = (res*2%mod);
      } else {
        res = (res*2%mod);
      }
    }
  }

  printf("%jd\n", res);
}


Measured memory_bytes: 256
import java.util.*;

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

        int H = scanner.nextInt();
        int W = scanner.nextInt();
        int D = scanner.nextInt();

        int[] px = new int[300*300+1];
        int[] py = new int[300*300+1];

        for(int i=1; i<=H; i++){
            for(int j=1; j<=W; j++){
                int A = scanner.nextInt();
                px[A] = i;
                py[A] = j;
            }
        }

        int acc[] = new int[(H+1)*(W+1)];

        for(int i=1; i+D <= H*W; i++){
            acc[i+D] = acc[i] + Math.abs(px[i+D] - px[i]) + Math.abs(py[i+D] - py[i]);
        }

        int Q = scanner.nextInt();
        for(int i=0; i<Q; i++){
            int L = scanner.nextInt();
            int R = scanner.nextInt();

            System.out.println(acc[R] - acc[L]);
        }
    }
}


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

using namespace std;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;

int main() {
    iostream::sync_with_stdio(false);
    int M, D, count = 0, d1, d2;
    cin >> M >> D;
    REP(i, 1, M + 1) {
        REP(j, 1, D + 1) {
            d1 = j % 10;
            d2 = (j - d1) / 10;
            if ((d1 >=2) && (d2 >= 2) && (d1 * d2 == i)) {
                count++;
            }
        }
    }
    cout << count << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
int main(){
  int a[9];
  string s;
  int flag=10;
  cin >> s;
  for(int i=0;i<s.size();i++){
    if(flag==s.at(i)-'0'){
      cout << "Bad" << endl;
      return 0;
    }
    else flag = s.at(i)-'0';
  }
  cout << "Good" << endl;
  return 0;
}


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

using namespace std;
#define fastio  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define ld long double
#define fr(i,n) for(int i = 0; i < n; ++i)
#define fr1(i,n) for(int i=1;i<=n;i++)


int main() {
    int a,b, c;
  	cin>>a>>b>>c;
  	int x=a-b;
  	c-=x;
  	if( c >= 0 )	cout<<c;
  	else cout<<"0";
  	
	return 0;
}


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

int main(){
  int n,l;
  cin >> n >> l;
  int i = 0;
  int ans = abs(l-1), ref, flg;
   for(; i < n; i++){
    ref = abs(l + i);
    if(ans > ref){
      ans = ref;
      flg = i;
    }
   }

  
  int sum = 0;
  for ( int j = 0; j < n; j++){
    if ( j == flg){
      continue;
    }
    else{
    sum += l + j;
    }
  }
  cout << sum << endl;

}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> X(N);
  for (int i = 0; i < N; i++) cin >> X[i];

  long long ans = 1000000;

  for (int p = 1; p <= 100; p++) {
    long long temp_ans = 0;
    for (int i = 0; i < N; i++) temp_ans += pow(X[i] - p, 2);
    if (temp_ans < ans) ans = temp_ans;
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long A,B,i;
    cin >> A >> B;
    for(i=1;i<=B;i++){
        if((A*i)%B == 0){
            cout << A * i << endl;
            break;
        }
    }
    return 0;
}

Measured memory_bytes: 256
class UnionFind:
    def __init__(self, size):
        self.data = [-1] * size
    def find(self, x):
        if self.data[x] < 0:
            return x
        else:
            self.data[x] = self.find(self.data[x])
            return self.data[x]
    def union(self, x, y):
        x, y = self.find(x), self.find(y)
        if x != y:
            if self.data[y] < self.data[x]:
                x, y = y, x
            self.data[x] += self.data[y]
            self.data[y] = x
        return (x != y)
    def same(self, x, y):
        return (self.find(x) == self.find(y))
    def size(self, x):
        return -self.data[self.find(x)]


N, M, K, *ABCD = map(int, open(0).read().split())
AB, CD = ABCD[:2 * M], ABCD[2 * M:]

E = [[] for _ in range(N + 1)]
uf = UnionFind(N + 1)
for a, b in zip(*[iter(AB)] * 2):
    uf.union(a, b)
    E[a].append(b)
    E[b].append(a)

B = [[] for _ in range(N + 1)]
for c, d in zip(*[iter(CD)] * 2):
    B[c].append(d)
    B[d].append(c)

A = []
for i in range(1, N + 1):
    A.append(uf.size(i) - 1 - len(E[i]) - sum(1 for b in B[i] if uf.same(b, i)))

print(*A)

Measured memory_bytes: 125900
#include<iostream>

using namespace std;

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

int cards[4][13] = {0};
char p[4] = {'S','H','C','D'};

char c[52];
int x[52];

for (int i = 0; i < n ; i++)
{
cin >> c[i] >> x[i];
}

for (int i = 0; i < n; i++)
{
if (c[i] == 'S')
cards[0][x[i]-1] = 1;
else if (c[i] == 'H')
cards[1][x[i]-1] = 1;
else if (c[i] == 'C')
cards[2][x[i]-1] = 1;
else
cards[3][x[i]-1] = 1;
}



for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 13; j++)
{
if (cards[i][j] != 1)
   cout << p[i] << ' ' << j + 1 << '\n';
}
}

}

Measured memory_bytes: 1160
#include <stdio.h>
int main(void) 
{
	int a, b, c, d, e, f, g, h, A, B;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	scanf("%d %d %d %d", &e, &f, &g, &h);
	A = a + b + c + d;
	B = e + f + g + h;
	if (A >= B) {
		printf("%d\n", A);
	}
	else
		printf("%d\n", B);
	return 0;
}



Measured memory_bytes: 2140
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
const int mod = 1000000007;
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll c(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }

int main() {
	int d, g, ans;
	cin >> d >> g;
	ans = 1 << 30;
	vector<int> p(d), c(d);
	rep(i, 0, d)cin >> p[i] >> c[i];
	rep(i, 0, 1 << d) {
		int score = 0, id = -1, p_num = 0;
		rep(i2, 0, d) {
			if (i >> i2 & 1) {
				score += p[i2] * (i2 + 1) * 100 + c[i2];
				p_num += p[i2];
			}
			else id = i2;
		}
		if (g > score) {
			int add = (id + 1) * 100;
			int need = (g - score - 1) / add + 1;
			if (need > p[id])continue;
			p_num += need;
		}
		ans = min(p_num, ans);
	}
	printf("%d\n", ans);
	return 0;
}

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

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)+1):((m)-(n)+1)),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

vector<string> sp(string s) {
  vector<string>r;
  int idx=0;
  rep (i, s.size()) {
    if (s[i] == ' ') {
      r.push_back(s.substr(idx, i - idx));
      idx = i + 1;
    }
  }
  r.push_back(s.substr(idx,s.size()));
  return r;
}

int main() {

  int a,b;
  int x=0;
  int y=0;
  int n=0;
  loop {
    cin >> a;
    if (!cin) break;
    char comma; cin >> comma;
    cin >> b;
    x += a * b;
    y += b;
    n++;
  }
  cout << x << endl;
  cout << int(round(floor(y) / floor(n))) << endl;


  return 0;
}

Measured memory_bytes: 1176
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int h,w,k,a[20][1010],pre[20][1010],flag,ans=inf;
char ch;
int main(){
    scanf("%d %d %d",&h,&w,&k);
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            scanf(" %c",&ch);
            pre[i][j]=pre[i][j-1]+ch-'0';
        }
    }
    for(int state=0;state<(1<<h-1);state++){
        int last=0,sum=0,cnt=0,flag=1;
        for(int j=1;j<=w;j++){
            for(int i=1;i<=h;i++){
                sum=sum+pre[i][j]-pre[i][last];
//                printf("%d ",sum);
                if(sum>k){
                    if(j-last==1){//只有一列不能再切不满足条件
                        flag=0;
                        break;
                    }
                    i=0,sum=0,last=j-1;//第二轮循环再来一次
                    cnt++;
                }
                if(i==h||state&(1<<(i-1))){
                    sum=0;
                }
            }
//            printf("\n");
            if(!flag){
                break;
            }
        }
        if(flag){
            ans=min(ans, __builtin_popcountl(state)+cnt);
        }
    }
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include<algorithm> // sort
#include<map> // pair
#include<cmath>
#include<cstring>
#include<set>
using namespace std;
using ll = long long;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define all(x) (x).begin(),(x).end()
const ll mod = 1e9+7;
const int INF = 1e9;
const ll INFLONG = 1e18;
int main(){
  ll l,r;
  cin >> l >> r;
  int minimum = 2019;
  for(ll i=l;i<=r && minimum!=0;i++){
    for(ll j=i+1;j<=r && j-i<=2019;j++){
      if((i*j)%2019 < minimum){minimum = (i*j)%2019;}
    }
  }
  cout << minimum;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
long long mod = 1e9+7;

int main(void) {

  int n; cin >> n;
  vector<int> a(n);
  for(int i=0; i<n; i++ ) {
    cin >> a[i];
  }
  vector<int> b = a;
  sort(b.begin(), b.end());
  int mx = b[n-1];

  for(int i=0; i<n; i++) {
    if(a[i] == mx) cout << b[n-2] << endl;
    else cout << mx << endl;
  }
 
  return 0;
}

// EOF


Measured memory_bytes: 3200
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
int gcd(int x,int y){
	if(!y)return x;
	return gcd(y,x%y);
}
int main(){
	n=read();
	long long sum=0;
	for(register int i=1;i<=n;i++){
		for(register int j=1;j<=n;j++){
			int G=gcd(i,j);
			for(register int k=1;k<=n;k++){
				sum+=gcd(G,k);
			}
		}
	}
	printf("%lld\n",sum);
	return 0;
}

Measured memory_bytes: 3800
n, k = map(int, input().split())
a = list(map(int, input().split()))

plus_a = [0] * n
minus_a = [0] * n
ans = 0
for i in range(n):
    if a[i] > 0:
        plus_a[i] = a[i]
    else:
        minus_a[i] = a[i]
    ans += plus_a[i]

ruiseki_plus = [0] * (n + 1)
ruiseki_minus = [0] * (n + 1)
for i in range(n):
    ruiseki_plus[i+1] = ruiseki_plus[i] + plus_a[i]
    ruiseki_minus[i+1] = ruiseki_minus[i] + minus_a[i]

min_decreasing = -float("inf")
for i in range(n-k+1):
    #黒で塗る場合
    tmp_plus = ruiseki_plus[i+k] - ruiseki_plus[i]
    tmp_minus = ruiseki_minus[i+k] - ruiseki_minus[i]
    min_decreasing = max(min_decreasing, -tmp_plus, tmp_minus)

print(ans + min_decreasing)

Measured memory_bytes: 68384
d=input().split()
for _ in range(int(input())):
 t,f=[d.index(s) for s in input().split()]
 n={0:(1,2,4,3,1),1:(0,3,5,2,0),2:(0,1,5,4,0),3:(1,0,4,5,1),4:(0,2,5,3,0),5:(1,3,4,2,1)}[int(t)]
 print(d[n[n.index(int(f))+1]])

Measured memory_bytes: 5608
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef double db;
typedef vector<ll> VI;
typedef vector<pair<ll,ll> > VII;
typedef map<ll,ll> mll;
typedef pair<ll,ll> pll;
//
#define mp make_pair
#define pb push_back
#define pob pop_back
#define eb emplace_back
#define F first
#define S second
#define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
void fileioe(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout); 
	freopen("error.txt","w",stderr);
}
void error_out() { cerr << endl; }
template <typename Head, typename... Tail>
void error_out(Head H, Tail... T) {
  cerr << " " << H;
  error_out(T...);
}
void time_elapsed(){
  cerr<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
}
#ifndef ONLINE_JUDGE
#define error(...) cerr << "[" << #__VA_ARGS__ << "]:", error_out(__VA_ARGS__)
#define fioe() fileioe()
#define time() time_elapsed()
#else
#define error(...) 1
#define fioe() 1
#define time() 1
#endif
ll M=1000000007;
ll INF=(ll)0x3f3f3f3f3f3f3f3f;

//
int main()
{
	GO
	//fioe();
	ll n;
	cin>>n;
	set<ll> s;
	for(ll i=0;i<n;i++){
		ll x;
		cin>>x;
		s.insert(x);
	}
	if(s.size()==n){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
	time();
	return 0;
}

Measured memory_bytes: 9600
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
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;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const ll mod=1e9+7;
const int inf=1<<30;

int n,m;
vector<vector<int> > to;
vector<int> deg;
int dp[100010];

int main(){
  cin >> n >> m;
  to.resize(n);
  deg.resize(n);
  rep(i,m){
    int a,b;
    cin >> a >> b;
    a--; b--;
    to[a].push_back(b);
    deg[b]++;
  }
  queue<int> q;
  rep(i,n)if(deg[i]==0) q.push(i);
  while(q.size()){
    int v=q.front(); q.pop();
    for(auto nv:to[v]){
      deg[nv]--;
      if(deg[nv]==0){
        q.push(nv);
        chmax(dp[nv],dp[v]+1);
      }
    }
  }
  int ans=0;
  rep(i,n) chmax(ans,dp[i]);
  cout << ans << endl;
}

Measured memory_bytes: 9600
n,x = gets.chomp.split(" ").map(&:to_i)
a = gets.chomp.split(" ").map(&:to_i)
ans = 0
n.times do |i|
  sum = a[i]
  sum += a[i - 1] if(i - 1 >= 0)
  if (sum > x)
    tabe = (sum - x)
    ans += tabe
    a[i] -= tabe
  end
end
puts ans

Measured memory_bytes: 9604
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<cmath>
using namespace std;
typedef long long ll;

const ll MAX = 1000000;
const ll MOD = 1000000007;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
ll COM(ll n,ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

signed main(){
  // 前処理
  COMinit();
  ll h,w,a,b;cin >> h >> w >> a >> b;
  ll ans=0;//cout << ans << endl;
  ll m=min(a,b);
  for(int i=1;i<=w-b;i++){
    //cout << 1 << endl;
    ll ans_sub=COM(h-a+b+i-2,b+i-1)*COM(w-b+a-i-1,a-1)%MOD;
    ans+=ans_sub;
    ans%=MOD;
  }
  cout << ans << endl;
}


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

int main(){
  int n,k; cin>>n>>k;
  vector<int> p(n);
  for (int i=0; i<n; i++){
    cin>>p[i];
  }
  sort(p.begin(),p.end());
  
  int ans=0;
  for (int i=0; i<k; i++){
    ans+=p[i];
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3580
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};


int main() {
    ll N,K;
    cin >>N>>K;
    
    cout << min(N%K, K- N%K) << endl;


    return 0;
    

}

Measured memory_bytes: 3508
s = int(input())

d = dict()

for i in range(1000000):
    if s in d:
        print(i+1)
        break
    else:
        d[s] = 0
        if s % 2 == 0:
            s = s//2
            continue
        else:
            s = 3*s + 1
            continue
        


Measured memory_bytes: 2940
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>

int main(){
	int *array;
	int n,max=-INT_MAX,min=INT_MAX;
	long long sum=0;
	std::cin >> n;
	array= new int[n];

		for(int i=0;i<n;i++){
		std::cin >> array[i];
		max = std::max(max, array[i]);
		min = std::min(min, array[i]);
		sum += array[i];
		}
	std::cout << min << " " << max << " " << sum << std::endl;
	delete [] array;
}

Measured memory_bytes: 1236
#include<iostream>
#include<utility>

using namespace std;

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;



int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);

    int N, X;
    cin >> N >> X;

    int ans = 1;
    for (int a = 0, pos = 0; a < N; a++) {
        int L;
        cin >> L;
        pos += L;
        if (pos <= X) {
            ans++;
        }
    }

    cout << ans;
}


Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

// https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
  let n: i32 = read();
  let mut a: Vec<i32> = (0..n).map(|_| read()).collect();
  a.sort_by(|x, y| x.cmp(y).reverse());
  let mut allice = 0;
  let mut bob = 0;
  for i in 0..a.len() {
    if i % 2 == 0 {
      allice += a[i];
    } else {
      bob += a[i];
    }
  }
  println!("{}", allice-bob);
}


Measured memory_bytes: 4352
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <cstdlib>
#include <iomanip>
#define rep(i,x) for(ll i=0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 10010010010;
typedef pair<ll, ll> P;
using graph = vector<vector<ll>>;
const ll mod = 1000000007;
ll gcd(ll a, ll b) {//a>b
    if (a < b)swap(a, b);
    if (a % b == 0) return b;
    else return gcd(b, a % b);
}/*フィボナッチテンプレ
ll memo[50];
ll fibo(ll n) {
    if (memo[n] != 0)return memo[n];
    if (n <= 1)return memo[n] = n;
    else return memo[n] = fibo(n - 1) + fibo(n - 2);
}
*/
bool isp(ll n) {
    bool res = true;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            res = false;
            break;
        }
    }
    return res;
}
ll ctoi(char c) {
    if (c == '0')return 0;
    else if (c == '1')return 1;
    else if (c == '2')return 2;
    else if (c == '3')return 3;
    else if (c == '4')return 4;
    else if (c == '5')return 5;
    else if (c == '6')return 6;
    else if (c == '7')return 7;
    else if (c == '8')return 8;
    else if (c == '9')return 9;
    else return -1;
}int main() {
    string S;
    cin >> S;
    ll ans = 0;
    ll cnt = 0;
    ll s = S.size();
    rep(i, s) {
        if (S[i] == 'W') {
            ans += i - cnt;
            cnt++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e5+1;
int n,m;
pair<int,int>a[N];
map<pair<int,int> , ll > dp;

bool srt(pair<int,int> x, pair<int,int>y){
        if(x.first > y.first)return 1;
        if(x.first == y.first)return x.second >= y.second;
        return 0;
}

int main(){
    scanf("%d%d",&n,&m);
    priority_queue<pair<int,int> > q;
    for(int i = 0; i<n; ++i){
        scanf("%d%d",&a[i].second,&a[i].first);
        q.push({a[i].second,a[i].first});
    }
    sort(a,a+n);
    reverse(a,a+n);
    vector<int>days;
    for(int i = 0; i<=m; ++i)days.push_back(i);
    ll an = 0;
    for(int i = 0; i<n; ++i){
        int need = m-a[i].second;
        if(need < 0)continue;
        int l = 0, r = days.size()-1, mid , bst = -1;
        while(l <= r){
             mid = (l+r)/2;
             if(days[mid] <= need){
                 l = mid+1;
                 bst = mid;
             }else r = mid-1;
        }
        if(bst + 1){
             an += a[i].first;
             days.erase(days.begin()+bst);
        }
    }
    printf("%lld\n",an);
}


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

typedef long long ll;
ll A[200];
string s;

priority_queue<ll, vector<ll>, less<ll> > pq;

bool subspace(vector<ll> W, vector<ll> V) {
    for (ll x : V)
        pq.push(x);
    vector<ll> S;
    while (!pq.empty()) {
        ll x = pq.top();
        pq.pop();
        ll x2 = x;
        for (ll y : S)
            if ((x2^y) < x2)
                x2 = (x2^y);
        if (x2 == x)
            S.push_back(x2);
        else if (x2 != 0)
            pq.push(x2);
    }
    for (ll x : W) {
        for (ll y : S)
            if ((x^y) < x)
                x = (x^y);
        if (x != 0)
            return 0;
    }
    return 1;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> A[i];
        cin >> s;
        bool flag = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '0')
                continue;
            if (i == n - 1 || s[i + 1] == '0') {
                vector<ll> V, W;
                for (int j = i; j >= 0; j--) {
                    if (s[j] == '1')
                        V.push_back(A[j]);
                    else
                        break;
                }
                for (int j = i + 1; j < n; j++)
                    if (s[j] == '0')
                        W.push_back(A[j]);
                if (!subspace(V, W))
                    flag = 1;
                V.clear();
                W.clear();
            }
        }
        cout << flag << "\n";
    }
}


Measured memory_bytes: 3660
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] l = new int[5];
            for (int i = 0; i < 5; i++)
            {
                l[i] = int.Parse(Console.ReadLine());
            }
            int[] t = new int[5];
            for (int i = 0; i < 5; i++)
            {
                t[i] = l[i];
                for (int j = 0; j < 5; j++)
                {
                    if (j != i)
                    {
                        if (l[j] % 10 == 0)
                        {
                            t[i] += l[j];
                        }
                        else
                        {
                            t[i] += l[j] + 10 - l[j] % 10;
                        }
                    }
                }
            }
            Console.WriteLine(t.Min());
        }
    }
}

Measured memory_bytes: 13268
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln =  '\n';

const int mx=100010;
const ll mod=1e9+7;

#define bit(n,k) ((n>>k)&1) //*n no k bit me 1 or 0*/

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

int main(){
  int n;
  cin >> n;
  vector<vector<int>> a(22, vector<int>(22,0));
  rep(i,n)rep(j,n){
    cin >> a[i][j];
  }

  vector<mint> dp(1<<22,0);
  dp[0]=1;
  REP(i,1,(1<<n)){
    int k = __builtin_popcountll(i);
    rep(j,n){
      if(bit(i,j) && a[k-1][j]){
        dp[i] += dp[i^(1<<j)];
      }
    }
  }
  ll ans = dp[(1<<n)-1].x;
  cout << ans << ln;
  return 0;
}

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

int main() {
    int n;
    long long s = 0;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i<n; i++) {
        cin >> a[i];
        s+=a[i];
    }
    sort(a.begin(), a.end());
    cout << a[0] << ' ' << a[n-1] << ' ' << s << endl;
    return 0;
}



Measured memory_bytes: 3168
#include <bits/stdc++.h>
#define INF (1<<30)
#define MOD 1000000007
#define l_ength size
using ll = long long;
using namespace std;

int main(){
	int n, x;
	cin >> n >> x;
	vector<int> m(n);
	for( int &i : m ){
		cin >> i;
	}
	sort( m.begin(), m.end() );
	x -= accumulate( m.begin(), m.end(), 0 );
	cout << n + x/m.front() << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
  ll N,M;
  cin>>N>>M;
  vector<vector<ll>>vec(N,vector<ll>(2));
  for(ll i=0;i<N;i++){
    cin>>vec.at(i).at(0)>>vec.at(i).at(1);
  }
  sort(vec.begin(),vec.end());
  ll a=0;
  for(ll i=0;i<N;i++){
    if(M>vec.at(i).at(1)){
      a+=vec.at(i).at(1)*vec.at(i).at(0);
      M-=vec.at(i).at(1);
    }
    else{
      a+=vec.at(i).at(0)*M;
      cout<<a<<endl;
      return 0;
    }
  }
}



Measured memory_bytes: 8800
#include<stdio.h>

int MAX = 501;
int SIZE=(1<<30);

int main(){

  int n, i, j, num, sum, A[MAX][MAX], x, y, min, d[MAX], p[MAX], s[MAX];

  scanf("%d", &n);

  for ( i = 0; i< n; i++ ){

    for ( j = 0; j < n; j++ ){

      scanf("%d", &num);

      A[i][j] = num;
      if(num ==-1) A[i][j] = 10000;
    }
  }

  for ( i = 0; i < n; i++ ) {

    d[i] = SIZE;
    p[i] = -1;
    s[i] = 0;

  }

  d[0] = 0;

  while(1){

    min = MAX;
    x = -1;

    for ( i = 0; i < n; i++ )
      {
	if ( min > d[i] && ! s[i] )
	  {
	    x = i;
	    min = d[i];
	  }
      }
    if ( x == -1 ) break;
    s[x] = 1;

    for ( y = 0; y < n; y++ )
      {
	if ( !s[y] && A[x][y] != MAX )
	  {
	    if ( d[y] > A[x][y] )
	      {
		d[y] =  A[x][y];
		p[y] = x;
	      }
	  }
      }
  }

  sum = 0;

  for ( i = 0; i < n; i++ )
    {
      if ( p[i] != -1 ) sum += A[i][p[i]];
    }

  printf("%d\n", sum);

  return 0;
}

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

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

#define fr first
#define sc second
#define rep(i,x) for(int i = 0 ; i < x ; i ++)

const int INF = 1000000000;

int main(){
	static int n;
	static char c[4002];
	static int a[4002];
	scanf("%d",&n);
	for(int i = 1 ; i <= 2*n ; i ++){
		scanf("\n%c %d",&c[i],&a[i]);
	}
	
	static int dp[2002][2002];
	static int d[2][2002][2002];
	for(int i = 0 ; i < 2002 ; i ++){
		for(int j = 0 ; j < 2002 ; j ++){
			dp[i][j] = INF;
		}
	}
	dp[0][0] = 0;
	for(int i = 1 ; i <= n ; i ++){
		set<int> s;
		for(int k = 1 ; k <= 2*n ; k ++){
			if(c[k] == 'W' && a[k] == i)break;
			if(c[k] == 'W'){
				if(a[k] > i)dp[0][0] ++;
			}
			else {
				s.insert(a[k]);
			}
		}
		for(int j = 0 ; j <= n ; j ++){
			s.erase(j);
			d[0][i][j] = s.size();
		}
	}
	for(int i = 1 ; i <= n ; i ++){
		set<int> s;
		for(int k = 1 ; k <= 2*n ; k ++){
			if(c[k] == 'B' && a[k] == i)break;
			if(c[k] == 'B'){
				if(a[k] > i)dp[0][0] ++;
			}
			else {
				s.insert(a[k]);
			}
		}
		for(int j = 0 ; j <= n ; j ++){
			s.erase(j);
			d[1][j][i] = s.size();
		}
	}
	for(int i = 0 ; i <= n ; i ++){
		for(int j = 0 ; j <= n ; j ++){
			if(i < n)dp[i+1][j] = min( dp[i+1][j] , dp[i][j]+d[0][i+1][j] );
			if(j < n)dp[i][j+1] = min( dp[i][j+1] , dp[i][j]+d[1][i][j+1] );
		}
	}
	
	/*rep(i,n+1){
		rep(j,n+1){
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}*/
	
	cout << dp[n][n] << endl;
}



Measured memory_bytes: 47360
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORS(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) FORR(i,n,0)
#define si(n) scanf("%d", &n)
#define sc(s) scanf("%s", s)

using namespace std;
typedef pair<int, int> P;


int n;
int l;

int main(void){
    scanf("%d %d", &n, &l);
    int sum = 0;
    int m = 500;
    int a;
    FORS(i, 1, n){
        sum += l+i-1;
        if (abs(l+i-1) < m){
            m = abs(l+i-1);
            a = l+i-1;
        }
    }
    sum = sum - a;
    printf("%d\n", sum);


    return 0;
}



Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>

#define UPPER_BOUND 100000
#define UPPER_BOUND_INDEX 12
#define POW_BOUND 7+1

typedef enum { false, true } bool;

int change[UPPER_BOUND_INDEX];

int min = 100000;

void change_array(void)
{
    int i, index6, index9, pow6[POW_BOUND], pow9[POW_BOUND];
    
    
    index6 = index9 = 0;
    
    
    for(i=0; i<POW_BOUND; i++)
    {
        pow6[i] = pow(6, i);
        pow9[i] = pow(9, i);
    }
    
    for(i=0; pow6[index6]<=UPPER_BOUND || pow9[index9]<=UPPER_BOUND; i++)
    {
        if(pow6[index6] < pow9[index9])
        {
            change[i] = pow6[index6];
            
            index6++;
                
            continue;
        }
        if(pow6[index6] > pow9[index9])
        {
            change[i] = pow9[index9];
            
            index9++;
            
            continue;
        }
        if(pow6[index6] == pow9[index9])
        {
            change[i] = pow6[index6];
            
            index6++;
            index9++;
            
            continue;
        }
    }
    /*for(i=0; i<UPPER_BOUND_INDEX; i++)
    {
        printf("%d\n", change[i]);
    }*/
}

int change_index(int N)
{
    int i;
    
    
    for(i=UPPER_BOUND_INDEX-1; i>=0; i--)
    {
        if(change[i] <= N)
            break;;
    }
    
    return i;
}

bool DIV(int R, int index, int num)
{
    int i;
    
    
    if(num >= min)
    {
        return false;
    }
    else if(index == 0)
    {
        if(min > num + R)
            min = num + R;
            
        return true;
    }
    else
    {
        if(R >= change[index])
        {
            for(i=R/change[index]; i>=0; i--)
            {
                if(!DIV(R-change[index]*i, index-1, num+i))
                {
                    return true;
                }
            }
        }
        else
        {
            DIV(R, index-1, num);
        }
    }
}

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


    change_array();

    DIV(N, change_index(N), 0);


    printf("%d\n", min);


    /**/system("pause");
    return 0;
}


Measured memory_bytes: 376
// AtCoder Beginner Contest 121 - Problem A: White Cells (https://atcoder.jp/contests/abc121/tasks/abc121_a)
#include <bits/stdc++.h>

using namespace std;

int solve(int H, int W, int h, int w)
{
    // Princípio da Inclusão/Exclusão
    int total = H * W;
    total -= h*W;
    total -= w*H;
    total += h*w;

    return total;
}

int main()
{
    ios::sync_with_stdio(false);

    int H, W, h, w;
    cin >> H >> W >> h >> w;

    auto ans = solve(H, W, h, w);

    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256
class Sieve:
    """
    N以下の素数を列挙するエラトステネスのふるいです。
    O(NloglogN)でできます。

    Attributes
    sieveList : その数の素因数のうち最小のものの配列
    primes : N以下の素因数のリスト
    """
    def __init__(self, N):
        self.sieveList = [-1 for _ in range(N+1)]
        self.sieveList[1] = 1
        self.primes = []

        for i in range(2, N+1):
            #すでに何らかの数で割られていたらcontinue
            if self.sieveList[i] != -1:
                continue
            self.primes.append(i)
            for j in range(i, N+1, i):
                if self.sieveList[j] != -1:
                    continue
                self.sieveList[j] = i

    def getPrimes(self):
        return self.primes

    def getSieveList(self):
        return self.sieveList

class FastFactorization(Sieve):
    """
    高速素因数分解です。
    与えられたL個の数がA以下の時、
    エラトステネスでO(AloglogA)＋素因数分解でO(LlogA) = O(AloglogA+NlogA)でできます。
    """

    def __init__(self, N):
        """
        前処理でスーパークラスのエラトステネスを使ってN以下の素因数を列挙します。
        """
        super().__init__(N)
        self.N = N
        self.sieveList = self.getSieveList()

    def factorize(self, x):
        if x > self.N:
            raise ValueError("与えられた数がエラトステネスのリストの上限を超えている")
        p = set()
        # p = []
        while x > 1:
            divisor = self.sieveList[x]
            x //= divisor
            p.add(divisor)
            # p.append(divisor)
        return p

import math
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))

#is setwise?
gcd = A[0]
for i in range(1, N):
    gcd = math.gcd(gcd, A[i])
isSetwise = (gcd == 1)

#is pairwise?
maxNum = max(A)
factorizor = FastFactorization(maxNum)
factors = defaultdict(int)
isPairwise = True
for i in range(N):
    p = factorizor.factorize(A[i])
    for x in p:
        if factors[x]:
            isPairwise = False
            break
        else:
            factors[x] = 1

if isPairwise:
    print("pairwise coprime")
elif isSetwise:
    print("setwise coprime")
else:
    print("not coprime")


Measured memory_bytes: 216588