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

typedef long long ll;
using namespace std;
const ll mod = 1000000007;
const ll mod998 = 998244353;
const ll intmax = 2147483647;
const ll llmax = 9223372036854775807;
const char sp = ' ';

int N, D, g[600][600], cnt[4];
bool b[600][600];
vector<pair<int, int>>v;

int main() {
	cin >> N >> D;
	for (int i = -N - N; i <= N + N; i++) {
		for (int j = -N - N; j <= N + N; j++) {
			if (i*i + j*j == D) {
				v.push_back({ i,j });
			}
		}
	}
	for (int i = 0; i < N + N; i++) {
		for (int j = 0; j < N + N; j++) {
			if (!b[i][j]) {
				queue<pair<int, int>>q;
				q.push({ i,j });
				while (!q.empty()) {
					int x, y;
					x = q.front().first;
					y = q.front().second;
					q.pop();
					for (pair<int, int>p : v) {
						int X = x + p.first;
						int Y = y + p.second;
						if (X < 0 || N + N <= X || Y < 0 || N + N <= Y || b[X][Y])continue;
						g[X][Y] ^= (g[x][y] & 1) ^ 1;
						q.push({ X,Y });
						b[X][Y] = true;
					}
				}
			}
		}
	}

	cin >> D;
	v.clear(); 
	for (int i = -N - N; i <= N + N; i++) {
		for (int j = -N - N; j <= N + N; j++) {
			if (i*i + j*j == D) {
				v.push_back({ i,j });
			}
		}
	}
	for (int i = 0; i < N + N; i++) {
		for (int j = 0; j < N + N; j++) {
			if (b[i][j]) {
				queue<pair<int, int>>q;
				q.push({ i,j });
				while (!q.empty()) {
					int x, y;
					x = q.front().first;
					y = q.front().second;
					q.pop();
					for (pair<int, int>p : v) {
						int X = x + p.first;
						int Y = y + p.second;
						if (X < 0 || N + N <= X || Y < 0 || N + N <= Y || !b[X][Y])continue;
						g[X][Y] ^= (g[x][y] & 2) ^ 2;
						q.push({ X,Y });
						b[X][Y] = false;
					}
				}
			}
		}
	}
	for (int i = 0; i < N + N; i++) {
		for (int j = 0; j < N + N; j++) {
			cnt[g[i][j]]++;
		}
	}
	for (int i = 0; i < 4; i++) {
		if (cnt[i] >= N*N) {
			int c = N*N;
			for (int j = 0; j < N + N&&c; j++) {
				for (int k = 0; k < N + N&&c; k++) {
					if (g[j][k] == i) {
						c--;
						cout << j << sp << k << endl;
					}
				}
			}
			return 0;
		}
	}
}

Measured memory_bytes: 2816
// May this submission get accepted
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define STEP(i, is, ie, step) for (long long i=(is), i##_end=(ie), i##_step = (step); i<=i##_end; i+=i##_step)
#define UNIQUE(v) do { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); } while (false)
template<class T> bool chmax(T &a, const T &b) { if (a < b) {a = b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) {a = b; return true;} return false; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) do { cerr << "[DEBUG] " #q ": ["; REP(DUMPALL_ITR, (q).size()) { cerr << (q)[DUMPALL_ITR] << (DUMPALL_ITR == DUMPALL_ITR_len-1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl; } while (false)
template<class T> T gcd(T a, T b) { if (a < b) std::swap(a, b); while (b) std::swap(a %= b, b); return a; }
template<class T> T lcm(const T a, const T b) { return a / gcd(a, b) * b; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
#define DANCE_ long
#define ROBOT_ unsigned
#define HUMAN_ signed
using  ll = DANCE_ HUMAN_ DANCE_;
using ull = DANCE_ ROBOT_ DANCE_;
using  ld = long double;
using namespace std;

// 標準入出力
struct inp {
    size_t sz;
    inp(size_t _sz = 1) : sz(_sz) {}
    template <typename T> operator T () const { T a; cin >> a; return a; }
    template <typename T> operator vector<T> () const { vector<T> a(sz); for (size_t i = 0; i < sz; i++) cin >> a[i]; return a; }
    template <typename T, typename U> operator pair<T, U> () const { T f; U s; cin >> f >> s; return pair<T, U>(f, s); }
};
inp inp1; // input one
template <typename T> void say(T &x, char end = '\n') { cout << x << end; }
template <typename T> void say(vector<T> &x, char sep = ' ', char end = '\n') { for (size_t i = 0, sz = x.size(); i < sz; i++) { cout << x[i] << (i == sz-1 ? end : sep); } }

// モジュール


// 処理内容
int main() {
    
    ios::sync_with_stdio(false); // stdioを使うときはコメントアウトすること
    cin.tie(nullptr);            // インタラクティブ問題ではコメントアウトすること
    
    ll n = inp1;
    string s = inp1;

    ll ans = 0;
    REP(i, n-2) {
        if (s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C') {
            ans++;
        }
    }
    say(ans);
    
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

int main(void){
  while(true){
    int n, m;
    cin >> n >> m;
    if(n == 0 && m == 0){ break; }

    int d, p;
    long long int ans = 0;
    vector<pair<int, int> > princess;
    for(int i = 0; i < n; i++){
      cin >> d >> p;
      princess.push_back(make_pair(p, d));
      ans += d * p;
    }
    sort(princess.begin(), princess.end());
    reverse(princess.begin(), princess.end());

    for(int i = 0; i < n; i++){
      int hire = min(m, princess[i].second);
      m -= hire;
      ans -= hire * princess[i].first;
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

Measured memory_bytes: 3132
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    multiset<int> A;// 重複ありのセット
    set<int> A_unique;//重複を無視する普通のセット
    for (int i=0; i<n; i++){
        int a;
        cin >> a;
        A.insert(a);
        A_unique.insert(a);
    }
    int n_remove =0;
    for (int a :A_unique){
        int count =A.count(a);//A内のaの出現回数
        if (count <a){
            n_remove+=count;
        }
        else if (count >a){
            n_remove +=count -a;
        }
    }
    cout << n_remove << endl;
}

Measured memory_bytes: 9216
package main

import (
	"fmt"
)

func main() {
	var n int

	fmt.Scan(&n)

	nums := make([]int, n)
	count := 0
	for i := 0; i < n; i++ {
		fmt.Scan(&nums[i])
		if nums[i]%2 == 0 {
			count++
		}
	}

	res := 0
	for count == n {
		count = 0
		for i := 0; i < n; i++ {
			nums[i] /= 2
			if nums[i]%2 == 0 {
				count++
			}
		}
		res++
	}

	fmt.Println(res)
}


Measured memory_bytes: 512
#include <vector>
#include <iostream>
#include <functional>
#include <algorithm>

using namespace std;

bool compare(pair<int,int> &lhs, pair<int,int> &rhs) {
    return lhs.first > rhs.first;
}

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

    vector<int> even(n/2, 0);
    vector<int> odd(n/2, 0);
    for(int i = 0; i < n; i++) {
        if(i%2 == 0) cin >> odd[i/2];
        else cin >> even[i/2];
    }

    vector<pair<int, int>> even_count(100001, make_pair(0,0));
    vector<pair<int, int>> odd_count(100001, make_pair(0,0));
    for(int i = 0; i < n; i++) {
        if(i%2 == 0) {
            even_count[even[i/2]].first++;
            even_count[even[i/2]].second = even[i/2];
        } else {
            odd_count[odd[i/2]].first++;
            odd_count[odd[i/2]].second = odd[i/2];
        }
    }


    sort(even_count.begin(), even_count.end(), compare);
    sort(odd_count.begin(), odd_count.end(), compare);

    if(even_count[0].second == odd_count[0].second) {
        int zero_one = even_count[0].first + odd_count[1].first;
        int one_zero = even_count[1].first + odd_count[0].first;
        cout << n - max(zero_one, one_zero);
    } else {
        cout << n - even_count[0].first - odd_count[0].first << endl;
    }
}

Measured memory_bytes: 2176
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int n;
vector<int> arr[100001];
const int mod = 1e9 + 7;
int mul(int a, int b) {
    return (long long) a * b % mod;
}
pair<int, int> DFS_tree(vector<pair<int, int>> &dp, int vertex, int parent)
{
    for (auto adj : arr[vertex])
    {
        if (adj != parent)
        {
            pair<int, int> temp = DFS_tree(dp, adj, vertex);
            if (dp[vertex].first == -1 && dp[vertex].second == -1)
            {   
                dp[vertex].first = (long long)(temp.first + temp.second)%mod;
                dp[vertex].second = temp.first;
            }
            else
            {
                dp[vertex].first = (mul(dp[vertex].first , temp.first) + mul(dp[vertex].first ,temp.second))%mod;
                dp[vertex].second = mul(dp[vertex].second,temp.first);
            }
        }
    }
    if (dp[vertex].first == -1 && dp[vertex].second == -1){
      return make_pair(1,1);
    } 
    return dp[vertex];
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    vector<pair<int, int>> dp(n + 1);

    for (int i = 0; i < n - 1; ++i)
    {
        int x;
        int y;
        cin >> x >> y;
        arr[x].push_back(y);
        arr[y].push_back(x);
    }
    for (auto &x : dp)
    {
        x.first = -1;
        x.second = -1;
    }
    dp[1] = DFS_tree(dp, 1, -1);
    cout << (dp[1].first + dp[1].second)%mod << endl;
    return 0;
}

Measured memory_bytes: 13440
#include <iostream>
#include <vector>
using namespace std;
int n,m,a,b,c,t;
//int ret_mat[50][50];
//int mat[50][50];
vector<vector<int> > mat_pro(vector<vector<int> >mat,vector<vector<int> >mat2){
  vector<vector<int> > ret(n,vector<int>(n,0));
  int i,j,k,sum;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      sum=0;
      for(k=0;k<n;k++){
        sum+=(mat[i][k]*mat2[k][j])%m;
        sum%=m;
      }
      ret[i][j]=sum;
    }
  }
  return ret;
}
vector<vector<int> > pow_mat(vector<vector<int> >mat,int T){
  vector<vector<int> > ret(n,vector<int>(n,0));
  int i;
  for(i=0;i<n;i++)ret[i][i]=1;//単位行列
  while(T>0){
    if((T&1)==1){
      ret = mat_pro(ret, mat);
    }
    mat = mat_pro(mat , mat);
    T >>= 1;
  }
  return ret;
}
vector<vector<int> > init_mat(){
  vector<vector<int> > mat(n,vector<int>(n,0));
  return mat;
}

vector<vector<int> > set_mat(vector<vector<int> >mat){
  mat[0][0]=b;mat[0][1]=c;
  mat[n-1][n-1]=b;mat[n-1][n-2]=a;
  for(int i=1;i<n-1;i++){
    mat[i][i-1]=a;
    mat[i][i]=b;
    mat[i][i+1]=c;
  }
  return mat;
}
vector<int> Ax(vector<vector<int> > mat,vector<int> v){
  vector<int> s(n,0);
  int i,j,k,sum;
  for(i=0;i<n;i++){
    sum=0;
    for(j=0;j<n;j++){
      sum+=(mat[i][j]*v[j])%m;
      sum%=m;
    }
    s[i]=sum;
  }
  return s;
}
int main(){
  int i;
  while(cin >> n >> m >> a >> b >> c >> t , n){
    vector<int> s(n);
    vector<vector<int> > mat;
    for(i=0;i<n;i++)cin >> s[i];
    mat = init_mat();
    mat = set_mat(mat);
    mat = pow_mat(mat,t);
    s = Ax(mat,s);
    for(i=0;i<n;i++){
      cout << s[i];
      if(i!=n-1)cout << " ";
    }
    cout << endl;
  }
  return 0;
}



Measured memory_bytes: 3168
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Main {

    //グローバル変数を定義

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    void run() {
        Scanner sc = new Scanner(System.in);
        char[] A = sc.next().toCharArray();
        char[] B = sc.next().toCharArray();
        char[] C = sc.next().toCharArray();

        List<String> aArray = new ArrayList<>();
        List<String> bArray = new ArrayList<>();
        List<String> cArray = new ArrayList<>();

        for(char c: A){
            String s = String.valueOf(c);
            aArray.add(s);
        }
        for(char c: B){
            String s = String.valueOf(c);
            bArray.add(s);
        }
        for(char c: C){
            String s = String.valueOf(c);
            cArray.add(s);
        }

        String next = aArray.get(0);
        aArray.remove(0);

        String ans = "";

        while(true){
            if(next.equals("a")){
                if(aArray.size() == 0){
                    ans = "A";
                    break;
                }
                next = aArray.get(0);
                aArray.remove(0);
            } else if(next.equals("b")){
                if(bArray.size() == 0){
                    ans = "B";
                    break;
                }
                next = bArray.get(0);
                bArray.remove(0);
            } else {
                if(cArray.size() == 0){
                    ans = "C";
                    break;
                }
                next = cArray.get(0);
                cArray.remove(0);
            }
        }

        System.out.println(ans);
    }
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i)
#define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i)
#define ALL(t) t.begin(), t.end()
#define RALL(t) t.rbegin(), t.rend()
#define MATINIT(type, row, col, init) \
  vector<vector<type>>(row, vector<type>(col, init));
#define Yes(cond) cout << (cond ? "Yes" : "No") << endl;
#define YES(cond) cout << (cond ? "YES" : "NO") << endl;
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T>
using VEC = std::vector<T>;
template <class T>
using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail>
void DUMP(Head &&head, Tail &&... tail) {
  cerr << head << ", ";
  DUMP(std::move(tail)...);
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (auto p : s) os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> &m) {
  os << "{";
  for (auto p : m) os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "[" << p.first << " " << p.second << "]";
  return os;
}
set<int> f, u;
set<int> af, au;
VEC<VEC<pair<int, int>>> A;

void trueman(int n) {
  if (af.count(n) > 0) return;
  //DUMP("trueman", n);
  af.insert(n);
  REP(i, 0, A[n].size()) {
    //DUMP("A[n][i]", A[n][i]);
    if (A[n][i].second == 1) {
      //DUMP("  ", i);
      trueman(A[n][i].first);
    } else {
      au.insert(A[n][i].first);
    }
  }
}

int main() {
  int N;
  cin >> N;
  A = VEC<VEC<pair<int, int>>>(N, VEC<pair<int, int>>());
  REP(i, 0, N) {
    int a;
    cin >> a;
    A[i] = VEC<pair<int, int>>(a);
    REP(j, 0, a) {
      cin >> A[i][j].first >> A[i][j].second;
      A[i][j].first--;
    }
  }
  int lim = pow(2, N);
  int ans = 0;

  REP(bit, 0, lim) {
    f.clear();
    u.clear();
    int cnt = 0;
    REP(i, 0, N) {
      if ((bit >> i) & 1) {
        f.insert(i);
        cnt++;
      } else {
        u.insert(i);
      }
    }
    af.clear();
    au.clear();
    for (auto v : f) {
      trueman(v);
    }
    set<int> af_au;
    set<int> f_au;
    set_intersection(ALL(af), ALL(au), inserter(af_au, af_au.end()));
    set_intersection(ALL(f), ALL(au), inserter(f_au, f_au.end()));
    //DUMP(f, u);
    //DUMP(af, au);
    //DUMP(af_au);
    if (af_au.empty() && f_au.empty()) {
      //DUMP(ans, cnt);
      ans = max(ans, cnt);
      //DUMP("TRUE");
    } else {
      //DUMP("NOT");
    }
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define vec vector
#define fi first
#define se second
#define eb emplace_back
#define ef emplace_front
#define pof pop_front()
#define pob pop_back()
#define Rep(i,n) for(li i=0,nn=(li)(n);i!=nn;++i)
#define Rep1(i,n) for(li i=1,nn=(li)(n)+1;i!=nn;++i)
#define Size(v) (li)(v.size())
#define SortA(v) sort(v.begin(),v.end())
#define SortD(v) sort(v.rbegin(),v.rend())
#define All(a) (a).begin(),(a).end()
#define Input(a) for(li ii=0,nn=(li)(a.size());ii!=nn;++ii){cin>>(a).at(ii);}
#define Input2(a,b) for(li ii=0,nn=(li)(a.size());ii!=nn;++ii){cin>>(a).at(ii)>>(b).at(ii);}
#define mp make_pair
using li = int64_t;
using intl = int64_t;
using vi = vec<li>;
using vb = vec<bool>;
using vvi = vec<vi>;
using qi = deque<li>;
using pi = pair<li,li>;
using vpi = vec<pi>;
using si = set<li>;
using vsi = vec<si>;
using mii = map<li,li>;
using vmii = vec<mii>;
vec<li> dx = {0, 1, 0, -1, 1, 1, -1, -1};
vec<li> dy = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr li INF = 9223372036854775807;

void Yes(li b){
  if(b)cout<<"Yes";
  else cout<<"No";
}
void yes(li b){
  if(b)cout<<"yes";
  else cout<<"no";
}

int main(){
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout.precision(16);

  li N;
  cin >>N;
  vi A(N),B(N);
  Input2(A,B);

  SortA(A);
  SortA(B);

  li cA,cB;
  if(N&1){
  	 cA = A[(N+1)/2-1];
  	 cB = B[(N+1)/2-1];
  	 cout << cB-cA+1;
  }
  else{
  	 li cAx2 = A[N/2-1]+A[N/2];
  	 li cBx2 = B[N/2-1]+B[N/2];
  	 cout << cBx2-cAx2+1;
  }
  

}

Measured memory_bytes: 6400
#include <stdio.h>

int main(void)
{
	int a;
	int b;
	int c;
	int d;
	int n;
	int count;

	while (scanf("%d", &n) != EOF){
		count = 0;
		for (a = 0; a <= 9; a++){
			for (b = 0; b <= 9; b++){
				for (c = 0; c <= 9; c++){
					for (d = 0; d <= 9; d++){
						if (a + b + c + d == n){
							count++;
						}
					}
				}
			}
		}
		printf("%d\n", count);
	}
	
	return 0;
}

Measured memory_bytes: 596
#include <iostream>
#include <cstdlib>

using namespace std;
#define N 100
#define C 100
#define CHANGE_RATE 0.5

int main()
{
    int d = 0;
    int c[26];
    int s[365][26];
    int t_c[C][365];
    int last[26] = {0};
    int score_c[C] = {0};
    int score = 0;
    int max_c = 0;

    cin >> d;

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

    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < 26; j++)
        {
            cin >> s[i][j];
        }
    }

    for(int i = 0; i < C; i++)
    {
        for(int j = 0; j < 365; j++)
        {
            t_c[i][j] = rand() % 26;
        }
    }

    for(int n = 0; n < N; n++)
    {
        score = 0;
        max_c = 0;
        for(int ca = 0; ca < C; ca++)
        {
            score_c[ca] = 0;
            for(int i = 0; i < 26; i++)
                last[i] = 0;

            for(int i = 0; i < d; i++)
            {
                for(int j = 0; j < 26; j++)
                {
                    if(t_c[ca][i] == j)
                    {
                        last[j] = 0;
                        score_c[ca] += s[i][j];
                    }
                    else
                    {
                        last[j]++;
                        score_c[ca] -= c[j] * last[j];
                    }
                }
            }
        }

        for(int ca = 0; ca < C; ca++)
        {
            if(score < score_c[ca])
            {
                score = score_c[ca];
                max_c = ca;
            }
        }
        //cout << score << endl;

        for(int ca = 0; ca < C; ca++)
        {
            if(ca == max_c) continue;
            for(int i = 0; i < d; i++)
            {
                if((rand() / (double)RAND_MAX) < CHANGE_RATE)
                {
                    t_c[ca][i] = rand() % 26;
                }
                else
                {
                    t_c[ca][i] = t_c[max_c][i];
                }
            }
        }
    }

    for(int i = 0; i < d; i++)
    {
        cout << ((t_c[max_c][i] % 26) + 1) << endl;
    }

    return 0;
}

Measured memory_bytes: 3820
fn main() {
    let (k, q): (usize, usize) = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    };

    let d: Vec<i64> = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let iter = buf.split_whitespace();
        iter.map(|x| x.parse().unwrap()).collect()
    };

    for _ in 0..q {
        let (n, x, m): (usize, i64, i64) = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            let mut iter = buf.split_whitespace();
            (
                iter.next().unwrap().parse().unwrap(),
                iter.next().unwrap().parse().unwrap(),
                iter.next().unwrap().parse().unwrap(),
            )
        };
        let x = x % m;
    
        let mut res = 0;
        let (mut quo, mut rem) = (0, x);
        for i in 0..k {
            if (n - 1) < (i + 1) { continue; }
            let c = (1 + ((n - 1) - (i + 1)) / k) as i64;
            let d = d[i] % m;
            if d == 0 {
                res += c;
            } else {
                quo += (rem + d * c) / m;
                rem = (rem + d * c) % m;
            }
        }
        
        let ans = (n - 1) - (res + quo - x / m) as usize;
        println!("{}", ans);
    }
}

Measured memory_bytes: 2224
def qsort(nums):
    if len(nums) <= 1:
        return nums

    n = nums[0]

    lowers = [x for x in nums if x < n]
    equals = [x for x in nums if x == n]
    uppers = [x for x in nums if x > n]

    return qsort(lowers) + equals + qsort(uppers)

nums = list(map(int, input().split(' ')))

print(' '.join([str(x) for x in qsort(nums)]))

Measured memory_bytes: 7620
/* ＵＴＦ－８ */
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <array>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <numeric>
#include <type_traits>
#include <limits>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define USE_COLORED 1

#ifdef USE_COLORED
static const char stdout_pre[]  = "\033[1m"; // bold black
static const char stderr_pre[]  = "\033[31;1m"; // bold red
static const char stdxxx_post[] = "\033[m";

// 当然ながら GCC(g++)/libstdc++ 依存
#include <ext/stdio_filebuf.h>
using stdio_filebuf = __gnu_cxx::stdio_filebuf<char>;
class colored_stdio_filebuf : public stdio_filebuf {
private:
	const char* const pre_;
	const char* const post_;

public:
	colored_stdio_filebuf(FILE* fp, const char* pre, const char* post) : stdio_filebuf(fp, std::ios_base::out), pre_(pre), post_(post)
	{
	}

protected:
	std::streamsize xsputn(const char* s, std::streamsize count) override
	{
		stdio_filebuf::xsputn(pre_, strlen(pre_));
		auto ret = stdio_filebuf::xsputn(s, count);
		stdio_filebuf::xsputn(post_, strlen(post_));
		return ret;
	}
};
#endif

int main()
{
#ifdef USE_COLORED
	if (getenv("DEBUG")) {
		std::cout.rdbuf(new colored_stdio_filebuf(stdout, stdout_pre, stdxxx_post));
		std::cerr.rdbuf(new colored_stdio_filebuf(stderr, stderr_pre, stdxxx_post));
	} else
#endif
	{
		int fd = open("/dev/null", O_WRONLY);
		dup2(fd, 2);
		close(fd);
	}
	
	int N, L;
	std::vector<std::string> S;
	
	std::cin >> N >> L;
	S.resize(N);
	for (auto& s : S)
		std::cin >> s;

	std::sort(S.begin(), S.end());

	for (auto& s : S) {
		std::cout << s;
	}
	std::cout << "\n";
}


Measured memory_bytes: 512
#include <stdio.h>

int main(){
	int a,b;
  	int i;
  	scanf("%d %d", &a,&b);
	
	if(a >b){
    	for(i=0;i<a;i++){
        	printf("%d",b);
        }
    }
  	else{
    	for(i=0;i<b;i++){
        	printf("%d",a);
        }
    }
  
  	printf("\n");
	return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <string>
#include <math.h>
#include <cstdlib>
#include <istream>
#include <sstream>
#include <cctype>
#include <iomanip>
//円周率
#define PI 3.14159265358979323846
//cout << fixed << setprecision(20) << *** << endl;
#define rep(i,N) for(int i=0;i<(int)N;++i)
typedef long long ll;
using namespace std;

//ユークリッド
ll gcd(ll a,ll b){
	ll t;
	if(a<b){
		t=a;
		a=b;
		b=t;
	}
	if(a%b==0)return b;
	return gcd(b,a%b);
}
//階乗
ll Factorial(ll a){
	ll b=1;
	for(ll i=a;i>=1;i--){
		b=b*i;
	}
	return b;
}
//素数判定
bool prime(ll n){
	for(ll i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}
//桁数把握
ll keta(ll a){
	ll count=0;
	while(a!=0){
		a=a/10;
		count++;
	}
	return count;
}

//三項演算子　[条件] ? [処理(true)]:[処理(false)]
//printf("%d",N<=100 ? 1:0);

//bit全探索(補集合をすべて列挙)
vector<vector<ll>>bits(ll N){
	vector<vector<ll>> V;
	for(int t=0;t<(1<<N);t++){
		vector<ll> vv;
		for(int i=0;i<N;i++){
			if(t&(1<<i))vv.push_back(i);
		}
		V.push_back(vv);
	}
	return V;
}

//数字(アルファベット順)⇒アルファベット
char AbcNumToChar(ll a){
	if(a==0)return 'a';
	if(a==1)return 'b';
	if(a==2)return 'c';
	if(a==3)return 'd';
	if(a==4)return 'e';
	if(a==5)return 'f';
	if(a==6)return 'g';
	if(a==7)return 'h';
	if(a==8)return 'i';
	if(a==9)return 'j';
	if(a==10)return 'k';
	if(a==11)return 'l';
	if(a==12)return 'm';
	if(a==13)return 'n';
	if(a==14)return 'o';
	if(a==15)return 'p';
	if(a==16)return 'q';
	if(a==17)return 'r';
	if(a==18)return 's';
	if(a==19)return 't';
	if(a==20)return 'u';
	if(a==21)return 'v';
	if(a==22)return 'w';
	if(a==23)return 'x';
	if(a==24)return 'y';
	if(a==25)return 'z';
	return '0';
}

//スペース含めた1行読み込み
//getline(cin,s);

//1～Xのサイコロを振った時の出目の期待値
double kitaichi(ll N){
	double d=0,t;
	for(ll i=1;i<=N;i++){
		t=i/(double)N;
		d=d+t;
	}
	return d;
}


//配列1の固まりの数を数える
//例　11011 ⇒　2
//例　0110111011 ⇒　3
//例　111 ⇒　1
/*
rep(i,Vsize()-1){
	//初手1ならプラス、その他は0,1をカウント
	if(V[i]==1&&j==0)ANS++;
	if(VV[i]==0&&VV[i+1]==1)ANS++;
}
*/

int main()
{
	ll L,R,d,ANS=0;
	cin>>L>>R>>d;
	for(ll i=L;i<=R;i++){
		if(i%d==0)ANS++;
	}
	cout<<ANS<<endl;
}

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

using namespace std;
using ll = long long;
using ld = long double;

namespace NyaGadget
{	// DFSライブラリ

	struct DFSedge
	{
		ll vertex;	// 頂点
		DFSedge(ll v) { vertex = v; }
	};

	template <class TE = DFSedge> struct DFS
	{
		using Graph = vector<vector<TE>>;
		Graph graph;

		DFS(ll size)
		{
			graph.resize(size);
		}

		vector<TE>& operator [](int n) { return graph[n]; }

		void CreateEdge(ll v1, ll v2)
		{
			graph[v1].push_back(DFSedge(v2));
			graph[v2].push_back(DFSedge(v1));
		}
	};

}

#define all(collection) (collection).begin(), (collection).end()  // begin to end
#define ceil(a, b) ceil((ld) a / (ld) b);                         // div ceiling
#define each(e, collection) for(auto& e: collection)              // for each
#define rep(i, begin, end) for (ll i = begin; i < end; i++)       // repeat
#define repr(i, begin, end) for (ll i = begin; end < i; i--)      // repeat reverse
#define size(collection) ((ll) (collection).size())               // collection size

using namespace NyaGadget;

/**
@brief 深さ優先探索
@param v 頂点
@param r 根
@param g 処理するグラフ
@param memo 前処理したメモ(加算するべきカウンタの値)
@param ans 出力する答えを格納する配列
@note
 DFSで全てのノードを処理する。
**/
void Solve(ll r, ll v, DFS<>& dfs, vector<ll>& memo, vector<ll>& ans)
{
	if (r != -1)
		ans[v] = ans[r] + memo[v];

	for (const auto& e : dfs[v])
	{	// 後戻りしないようにする
		if (e.vertex == r)
			continue;

		Solve(v, e.vertex, dfs, memo, ans);
	}
}

int main(void)
{
	ll n, q; cin >> n >> q;

	ll u, v;
	DFS<> dfs(n);
	rep(ei, 0, n - 1)
	{
		cin >> u >> v;
		--u; --v;
		dfs.CreateEdge(u, v);
	}

	vector<ll> p(q), x(q);
	rep(i, 0, q)
	{
		cin >> p[i] >> x[i];
		p[i]--;
	}

	// 頂点xで加算するべきカウンタの値をメモしておく。
	vector<ll> memo(n);
	rep(i, 0, q)
		memo[p[i]] += x[i];

	vector<ll> ans(n);
	ans[0] = memo[0];
	Solve(-1, 0, dfs, memo, ans);

	for (auto& e : ans)
		cout << e << " ";

	return 0;
}


Measured memory_bytes: 35200
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define MOD 1000000007
int main()
{
	ll n;
	cin>>n;
	ll arr[n];
	for(ll i=0;i<n;i++)
	{
		cin>>arr[i];
	}

	ll dp[n];
	dp[0]=0;
	dp[1]=abs(arr[1]-arr[0]);
//	cout<<dp[1]<<endl;
	for(int i=2;i<n;i++)
	{
		dp[i]=min((dp[i-1]+llabs(arr[i]-arr[i-1])),(dp[i-2]+llabs(arr[i]-arr[i-2])));
	//	cout<<dp[i]<<" ";
	}

	cout<<dp[n-1]<<endl;
	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>

void swap(int *pa, int *pb);

int main(void)
{
 int a, b, c;
  scanf("%d", &a);
  scanf("%d", &b);
  scanf("%d", &c);
    if(a>b){
    swap(&a, &b);
    }
 
    if(b>c){
    swap(&b, &c);
    }
   
    if(a>b){
    swap(&a, &b);
    }
printf("%d %d %d\n",a ,b ,c);

return 0;
}
 
void swap(int *pa, int *pb)
{
int tmp;
tmp = *pa;
*pa = *pb;
*pb = tmp;

} 
    

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

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(D: int, T: int, S: int):
    if T >= D/S:
        print(YES)
    else:
        print(NO)
    return


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

if __name__ == '__main__':
    main()


Measured memory_bytes: 9188
package main

import "fmt"

func lcm(x, y int) int {
	gcd := func(x, y int) int {
		for y != 0 {
			x, y = y, x%y
		}
		return x
	}
	return x * y / gcd(x, y)
}

func main() {
	var a, b int
	fmt.Scan(&a, &b)

	fmt.Println(lcm(a, b))
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (int i = j; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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; }
//name[i][j]-> vvl name(i,vl(j))
//map<int,int>mp
//余りを切り上げる =>  (a + b - 1) / b 

signed main(){
	string s; cin>>s;
	if(s[0]==s[1] && s[1]==s[2]) cout<<"No";
	else cout<<"Yes";
	
	return 0;
}

Measured memory_bytes: 256
N = int(input())
*A, = map(int, input().split())
def dfs(A):
    st = []
    V = [0]*(N+1)
    cur = 0
    for i, a in enumerate(A):
        b = a
        p = 1
        while st and st[-1][0] < b:
            c, d, cnt = st.pop()
            while c < b:
                cur += cnt*2
                c *= 4; d *= 4
            b = d
            p += cnt
        if st:
            st.append((a, b, p))
        else:
            st.append((a, 0, p))
        V[i+1] = cur
    return V
P = dfs(A)
A.reverse()
Q = dfs(A)
ans = 10**18
for i in range(N+1):
    ans = min((P[i] + i) + Q[-1-i], ans)
print(ans)

Measured memory_bytes: 38276
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <iomanip>
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const ll MOD=1000000007;
const ll MAX_N=500010;
const ll INF=9223372036854775807;
vector<int> G[MAX_N];
ll dp[MAX_N][2];

void dfs(int u, int p){
    dp[u][0]=dp[u][1]=1;
    for(int v:G[u]){
        if(v==p) continue;
        dfs(v,u);
        dp[u][1]*=dp[v][0];
        dp[u][1]%=MOD;
        dp[u][0]*=dp[v][0]+dp[v][1];
        dp[u][0]%=MOD;
    }
}

int main(){
    int n,a,b;
    cin>>n;
    for(int i=0;i<n-1;i++){
        cin>>a>>b; a--; b--;
        G[a].push_back(b); G[b].push_back(a);
    }
    dfs(0,-1);
    ll ans=(dp[0][0]+dp[0][1])%MOD;
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 25344
HP_1,PW_1,HP_2,PW_2=map(int,input().split())

turn = 0

while 0 < HP_1 and 0 < HP_2:
	if turn % 2 == 0:
		HP_2 -= PW_1
	else:
		HP_1 -= PW_2
	turn += 1

if HP_2 <= 0:
	ans = "Yes"
else:
	ans = "No"

print(ans)


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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int  ,int > pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn  = 3e6;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) return(cout << x , 0);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ms(x , y) memset(x , y , sizeof x);
#define file_init freopen("input.txt", "r+", stdin); freopen("output.txt", "w+", stdout);
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n;
map < ll , ll > cnt;

int main(){
    migmig
    cin >> n;
    for(int i = 0 ; i < n ; i ++){
		int x;
		cin >> x;
		cnt[x]++;
	}
	ll mx1 = 0 , mx2 = 0;
	for(auto i : cnt){
		if(i.second >= 2){
			mx1 = max(mx1 , i.first);
		}
	}
	cnt[mx1]-=2;
	for(auto i : cnt){
		if(i.second >= 2){
			mx2 = max(mx2 , i.first);
		}
	}
    cout << mx1 * mx2;
    
    
    
    return(0);
}

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

int main(){
    string N;
    cin >> N;
    if (N.at(0) == '7' || N.at(1) == '7' || N.at(2) == '7') cout << "Yes" << endl;
    else cout << "No" << endl;
}


Measured memory_bytes: 3624
N=int(input())
def eratosthenes(n):
    prime = [2]
    limit = int(n**0.5)
    data = [i + 1 for i in range(2, n, 2)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]
l=eratosthenes(55555)
cnt=0
for i in l:
   if i%5==1:
      print(i,end=" ")
      cnt+=1
   if cnt==N:
      exit()

Measured memory_bytes: 10548
N,W = map(int, input().split())
w = [0] * N
v = [0] * N

for i in range(N):
    w[i], v[i] = map(int, input().split())

dp = [[0 for i in range(W+1)] for j in range(N+1)]

for i in range(N):
  for k in range(W+1):
    if w[i]<=k:
      dp[i+1][k] = max(dp[i][k-w[i]]+v[i],dp[i][k])
    else:
      dp[i+1][k] = dp[i][k]

print(dp[-1][-1])



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

int main(){
    string s;
    while(cin >> s){
        int tmp = 0, ansjoi = 0, ansioi = 0;
        while(tmp < s.size() - 2){
            if(s[tmp] == 'J' && s[tmp + 1] == 'O' && s[tmp + 2] == 'I'){
                tmp += 2;
                ansjoi++;
            }
            else if(s[tmp] == 'I' && s[tmp + 1] == 'O' && s[tmp + 2] == 'I'){
                tmp += 2;
                ansioi++;
            }
            else tmp++;
        }
        cout << ansjoi << endl << ansioi << endl;
    }
    return 0;
}



Measured memory_bytes: 3128
#include<iostream>
#include<cstdio>
#define ll long long
#define mod 998244353
using namespace std;
int n,m,x,tot=0;
int cnt[1000010],vis[1000010],prime[1000010];
ll f[1000010],ans=0;
inline int read(){
	int ans=0,k=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=(ans<<3)+(ans<<1)+(c-'0');c=getchar();}
	return ans*k;
}
inline ll ksm(ll x,int i){
	ll res=1ll;
	for(;i;i>>=1,x=x*x%mod)
		if(i&1)res=res*x%mod;
	return res;
}
int main(){
	n=read();
	for(register int i=1;i<=n;i++){
		x=read();
		m=max(m,x);
		ans=(ans-x+mod)%mod;
		cnt[x]++;
	}
	f[1]=1;
	for(register int i=2;i<=m;i++){
		if(!vis[i]){
			prime[++tot]=i;
			f[i]=1ll*(mod+1-i)*ksm(i,mod-2)%mod;
		}
		for(register int j=1;j<=tot&&i*prime[j]<=m;j++){
			vis[i*prime[j]]=1;
			if(i%prime[j]==0){
				f[i*prime[j]]=f[i]*ksm(prime[j],mod-2)%mod;
				break;
			}
			f[i*prime[j]]=f[i]*f[prime[j]]%mod;
		}
	}
	for(register int i=1;i<=m;i++){
		ll sum=0,res=0;
		for(register int j=i;j<=m;j+=i){
			sum=(sum+1ll*j*j%mod*cnt[j]%mod*cnt[j]%mod+2ll*res%mod*j%mod*cnt[j]%mod)%mod;
			res=(res+j*cnt[j]%mod)%mod;
		}
		ans=(ans+f[i]*sum%mod)%mod;
	}
	printf("%lld",ans*ksm(2,mod-2)%mod);
	return 0;
} 

Measured memory_bytes: 17664
WHITE = 0
GRAY = 1
BLACK = 2

INF = 2000000000

def prim():
    d[0] = 0
    p[0] = -1
    
    while True:
        mincost = INF
        for i in range(n):
            if color[i] != BLACK and d[i] < mincost:
                mincost = d[i]
                u = i
        if mincost == INF:
            break
        color[u] = BLACK
        
        for v in range(n):
            if color[v] != BLACK and m[u][v] != -1:
                if m[u][v] < d[v]:
                    d[v] = m[u][v]
                    p[v] = u
                    color[v] = GRAY
            

n = int(input())

m = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
    nums=list(map(int,input().split()))
    for j in range(n):
        m[i][j] = nums[j]

color = [WHITE] * n
d = [INF] * n
p = [-1] * n


prim()

sum = 0
for i in range(n):
    sum += d[i]

print(sum)






















Measured memory_bytes: 5772
class Dice:
    def __init__(self,l1,l2,l3,l4,l5,l6):
        self.l1 = l1
        self.l2 = l2
        self.l3 = l3
        self.l4 = l4
        self.l5 = l5
        self.l6 = l6
        self.top = 1
        self.front = 2
        self.right = 3

    def get_top(self):
        return eval("self." + 'l' + str(self.top))

    def move(self, order):
        if order == 'S':
            pretop = self.top
            self.top = 7 - self.front
            self.front = pretop
        elif order == 'E':
            pretop = self.top
            self.top = 7 - self.right
            self.right = pretop
        elif order == 'N':
            pretop = self.top
            self.top = self.front
            self.front = 7 - pretop
        elif order == 'W':
            pretop = self.top
            self.top = self.right
            self.right = 7 - pretop

l = list(map(int,input().split()))
orders = input()
d = Dice(l[0],l[1],l[2],l[3],l[4],l[5])
for order in orders:
    d.move(order)
print(d.get_top())

Measured memory_bytes: 7796
#include <bits/stdc++.h>
using namespace std;
int n;
long long u[200005], a[220005];
int main() {
    cin >> n;
    long long ans = 0;
    for(int i = 1; i <= n; i ++) cin >> u[i], a[u[i]] ++;
    for(int i = 1; i <= n; i ++) ans += a[i] * (a[i] - 1) / 2;
    for(int i = 1; i <= n; i ++) cout << ans - a[u[i]] + 1 << endl;
}


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

int main(){
	int num[3] = {1,0,0};
	char input1, input2, q;
	while( cin >> input1 >> q >> input2 ){
		int pt1, pt2;
		if( input1 == 'A' ) pt1 = 0;
		else if( input1 == 'B' ) pt1 = 1;
		else if( input1 == 'C' ) pt1 = 2;
		if( input2 == 'A' ) pt2 = 0;
		else if( input2 == 'B' ) pt2 = 1;
		else if( input2 == 'C' ) pt2 = 2;
		
		int tmp = num[pt1];
		num[pt1] = num[pt2];
		num[pt2] = tmp;
	}
	if( num[0] == 1 ) cout << "A" << endl;
	if( num[1] == 1 ) cout << "B" << endl;
	if( num[2] == 1 ) cout << "C" << endl;
	//for( int i = 0; i < 3; i++ ) cout << num[i] << endl;
}

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


int main()
{
    string s;
    cin>>s;
    int c=0;
    vector<int> v;
    int arr[3]={0};
    for(int i=0;i<3;i++)
        if(s[i]=='R')
        arr[i]++;
    for(int i=0;i<3;i++)
      {if(arr[i])
            {
                c++;
            }
        else
        {
            c=0;
        }
        v.push_back(c);
      }
      sort(v.begin(),v.end());
      cout<<v[v.size()-1];
}


Measured memory_bytes: 3596
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define dup(x,y) (((x)+(y)-1)/(y))
#define v(T) vector<T>
#define vv(T) v(v(T))
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<pii> vp;
typedef vector<T> vt;
inline int getInt() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MX = 200005;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
//---------------------------------------------------------------
#define fastio cin.tie(nullptr); std::ios::sync_with_stdio(false);
#define myio ifstream iofi; if(argc>1){iofi.open(argv[1]); std::cin.rdbuf(iofi.rdbuf());}
//--------------------------------------------------------------
// [gcd]
template<class T> T gcd(T a,T b)
{if (a < b) swap(a,b);return b ? gcd(b, a % b) : a;}
template<class T> T lcm(const T a, const T b)
{return (a * b / gcd(a, b));}
void Main(){
  int n,m;
  cin>>n>>m;
  v(pll) a(n);
  cin>>a;
  sort(rng(a));
  ll ans=0;int s=0;
  while(0<m){
    if(a[s].se<m){
      ans+= a[s].fi*a[s].se;
      m-=a[s].se;
    }else{
      ans+= a[s].fi*m;
      break;
    }
    s++;
  }
  cout<<ans;
} int main(int argc,char** argv)
{fastio; myio; Main();}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

const int N=110;
const int INF=123456789;

int n,p[100];
int dp1[N][50000],dp2[N][50000];

int main()
{
    while(scanf(" %d", &n),n)
    {
        rep(i,n) scanf(" %d", &p[i]);

        fill(dp1[0],dp1[N],-INF);
        fill(dp2[0],dp2[N],INF);

        dp1[0][0]=0;
        dp2[0][0]=0;
        rep(i,n)
        {
            rep(j,50000)
            {
                // ??????
                // ???????????????????????????
                int r = (1000-p[i]%1000)%1000;
                int add=0;
                if(r>=500)
                {
                    add=1;
                    r-=500;
                }
                if(j+r<50000)
                {
                    if(dp1[i+1][j+r] < dp1[i][j]+add)
                    {
                        dp1[i+1][j+r] = dp1[i][j]+add;
                        dp2[i+1][j+r] = dp2[i][j]+p[i];
                    }
                    else if(dp1[i+1][j+r] == dp1[i][j]+add)
                    {
                        dp2[i+1][j+r] = min(dp2[i+1][j+r],dp2[i][j]+p[i]);
                    }
                }

                // ?°?????????????
                int need = p[i]%1000;
                if(need>=500) need-=500;
                else need+=500;
                if(need<=j)
                {
                    if(dp1[i+1][j-need] < dp1[i][j]+1)
                    {
                        dp1[i+1][j-need] = dp1[i][j]+1;
                        dp2[i+1][j-need] = dp2[i][j]+p[i];
                    }
                    else if(dp1[i+1][j-need] == dp1[i][j]+1)
                    {
                        dp2[i+1][j-need] = min(dp2[i+1][j-need],dp2[i][j]+p[i]);
                    }
                }

                // ????????????
                if(dp1[i+1][j] < dp1[i][j])
                {
                    dp1[i+1][j] = dp1[i][j];
                    dp2[i+1][j] = dp2[i][j];
                }
                else if(dp1[i+1][j] == dp1[i][j])
                {
                    dp2[i+1][j] = min(dp2[i+1][j],dp2[i][j]);
                }
            }
        }

        int x=0;
        rep(i,50000) x = max(x,dp1[n][i]);

        int y=INF;
        rep(i,50000)
        {
            if(dp1[n][i] == x) y = min(y,dp2[n][i]);
        }

        printf("%d %d\n", x,y);
    }
    return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef int _lcint;
#define REP(i, n) for (_lcint i = 0; i < n; i++)
#define FOR(i, a, b) for (_lcint i = (_lcint)(a); i < (_lcint)(b); i++)
#define FORREV(i, a, b) for (_lcint i = (_lcint)(b)-1; i >= (_lcint)(a); i--)
#define DEBUG(x) cout << #x << ": " << x << endl;
#define DEBUG_VEC(v)  \
  cout << #v << ": "; \
  REP(i, v.size()) cout << " " << v[i] << endl;

#define ALL(a) (a).begin(), (a).end()
#define CHMIN(a, b) a = min((a), (b))
#define CHMAX(a, b) a = max((a), (b))

struct Info {
  ll A, B;
};

bool operator<(const Info &left, const Info &right) { return left.A < right.A; }

unsigned long long XOR(unsigned long long A, unsigned long long B) {
  return A ^ B;
}

int f(char *buf, int N, int l, int u) {
  while (buf[l] == 'x') {
    l--;
    if (l < 0) {
      return 0;
    }
  }

  while (buf[u] == 'x') {
    u++;
    if (u >= N) {
      return 0;
    }
  }

  if (buf[l] != buf[u]) {
    buf[l] = buf[u] = 'x';
    return 2 + f(buf, N, l, u);
  }
  return 0;
}

int main() {
  char S[100000];
  scanf("%s", S);
  int L = strlen(S);

  int count = 0;
  FOR(i, 0, L - 1) {
    count += f(S, L, i, i + 1);
    while (S[i + 1] == 'x') {
      i++;
      if (i >= L) {
        cout << count << endl;
      }
    }
  }

  cout << count << endl;

  return 0;
}


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

using namespace std;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

vector <pair <int, int> > vec;
int n, cnt[3];

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= 2 * n; i++) {
        int x; scanf("%d", &x);
        if (i <= n) vec.push_back({x, 1});
        else vec.push_back({x, 2});
    }

    sort(vec.begin(), vec.end());
    int ans = 1;

    for (auto i: vec) {
        if (cnt[3 - i.second]) {
            ans = (1LL * ans * cnt[3 - i.second]) % mod;
            cnt[3 - i.second]--;
        }
        else cnt[i.second]++;
    }

    printf("%d", ans);
    return 0;
}


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

#define int long long
#define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define pb push_back
#define ins insert
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define tc(T) int T; cin>>T; while(T--)
#define q(q) int q; cin>>q; while(q--)
#define pi 3.1415926535897932384626433832
const int pinf=((int)9e18);
const int ninf=((int)-9e18);
const int mod=1000000007;

using namespace std;

int32_t main()
{
    fast_io();

    //FILE *fptr1=freopen("in.txt","r",stdin);
    //FILE *fptr2=freopen("out.txt","w",stdout);


    int a,b;
    cin>>a>>b;

    if((a+b)%2)
        cout<< "IMPOSSIBLE";
    else
        cout<< (a+b)/2;


    return 0;
}


Measured memory_bytes: 3584
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<cmath>
#include<map> 
#include<string>
#include<queue>
#include<stack> 
#include<bitset>
#define int long long
using namespace std;
int n,k,l=1,r,a[200005];
bool check(int x)
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		ans+=a[i]/x-(a[i]%x==0);
	}
	return ans<=k;
}
signed main()
{
	scanf("%lld %lld",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		r=max(r,a[i]);
	}
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))
		{
			r=mid;
		}
		else
		{
			l=mid+1;
		}
	}
	cout<<l;
}

Measured memory_bytes: 5240