akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define mod 1000000007
#define semn(i) (i == 0 || i == 3 ? 1 : -1)
using namespace std;

const int NMAX = 1e6 + 5;
int st, dr, up, down;
int fact[2 * NMAX + 10], invfact[2 * NMAX + 10];

inline void add(int & a, int b)
{
    a = a + b;
    if (a < 0)
        a += mod;
    if (a > mod)
        a -= mod;
}
int put(int n, int p)
{
    int ans = 1;
    while (p) {
        if (p & 1)
            ans = 1ll * ans * n % mod;
        n = 1ll * n * n % mod;
        p >>= 1;
    }
    return ans;
}
void precalc()
{
    fact[0] = 1;
    for (int i(1); i <= 2 * NMAX; i++)
        fact[i] = 1ll * fact[i - 1] * i % mod;
    invfact[2 * NMAX] = put(fact[2 * NMAX], mod - 2);

    for (int i(2 * NMAX - 1); i >= 0; i--)
        invfact[i] = 1ll * invfact[i + 1] * (i + 1) % mod;
}
inline int comb(int a, int b)
{
    if (a < 0 || b < 0 || a < b)
        return 0;
    return 1ll * fact[a] * invfact[b] % mod * invfact[a - b] % mod;
}
inline int moduri(int x1, int y1, int x2, int y2)
{
    return comb(abs(x1 - x2) + abs(y1 - y2), abs(x1 - x2));
}
inline int dist(int x1, int y1, int x2, int y2)
{
    return abs(x1 - x2) + abs(y1 - y2);
}
int solve(int x1, int y1, int x2, int y2)
{
    int ans = 0;
    for (int i(up); i <= down; i++) {
        add(ans, 1ll * dist(x1, y1, i, dr) * moduri(x1, y1, i, dr) % mod * moduri(i, dr + 1, x2, y2) % mod);
        add(ans, -1ll * dist(x1, y1, i, st - 1) * moduri(x1, y1, i, st - 1) % mod * moduri(i, st, x2, y2) % mod);
    }
    for (int i(st); i <= dr; i++) {
        add(ans, 1ll * dist(x1, y1, down, i) * moduri(x1, y1, down, i) % mod * moduri(down + 1, i, x2, y2) % mod);
        add(ans, -1ll * dist(x1, y1, up - 1, i) * moduri(x1, y1, up - 1, i) % mod * moduri(up, i, x2, y2) % mod);
    }
    return ans;
}

int main()
{
    precalc();

if (1) {

    vector <pair <int, int>> first(4), last(4);

    cin >> first[0].first >> first[2].first >> up >> down >> last[0].first >> last[2].first;
    first[0].first--;
    first[1].first = first[0].first;
    first[3].first = first[2].first;

    last[2].first++;
    last[1].first = last[0].first;
    last[3].first = last[2].first;

    cin >> first[0].second >> first[1].second >> st >> dr >> last[0].second >> last[1].second;
    first[0].second--;
    first[2].second = first[0].second;
    first[3].second = first[1].second;

    last[1].second++;
    last[2].second = last[0].second;
    last[3].second = last[1].second;

    int ans = 0;

    for (int i(0); i < 4; i++)
        for (int j(0); j < 4; j++)
            add(ans, semn(i) * semn(j) * solve(first[i].first, first[i].second, last[j].first, last[j].second));

    cout << ans;

    return 0;

}

    up = down = st = dr = 1;
    cout << solve(0, 0, 2, 2);

    return 0;
}


Measured memory_bytes: 15872
def all_search(k, a):
    point = 1
    while k > 0:
        point = a[point - 1]
        k -= 1
    return point

def find_loop(n, a):
    before_step = 0
    start_point = 1
    visited = [False] * n
    while True:
        visited[start_point - 1] = True
        start_point = a[start_point - 1]
        before_step += 1
        if visited[start_point - 1]:
            break

    loop_step = 0
    point = start_point
    visited = [False] * n
    while True:
        visited[point - 1] = True
        point = a[point - 1]
        loop_step += 1
        if visited[point - 1]:
            break
    before_step -= loop_step

    return before_step, start_point, loop_step


def solve(n, k, a):
    if n >= k:
        return all_search(k, a)
    else:
        before_step, start_point, loop_step = find_loop(n, a)
        k -= before_step
        k = k % loop_step
        point = start_point
        while k > 0:
            point = a[point - 1]
            k -= 1
        return point


n, k = [int(v) for v in input().split()]
a = [int(v) for v in input().split()]

print(solve(n, k, a))

Measured memory_bytes: 32336
#include <iostream>
#include <cstdio>
#define MAX 100010
using namespace std;
int n, m;
int a[MAX];

void FixDown(int a[], int k, int N){
    while (N >= 2*k){
        int j = 2*k;
        if (j <N && a[j]<a[j+1]) j++;
        if (!(a[k] < a[j])) break;
        swap(a[k], a[j]);
        k = j;
    }
}
/*
void HeapSort(int a[], int l, int r){
    int N = r-l+1;
    int *pHeap = a+l-1;
    for (int k = N/2; k>=1; k--) FixDown(pHeap, k, N);
    while (N>1){
        swap(pHeap[1], pHeap[N]);
        FixDown(pHeap, 1, --N);
    }
}
*/
int main()
{
    cin >> n >> m;
    long long sum = 0;
    //sort first
    for (int i = 0; i<n; i++){
        scanf("%d", &a[i]);
    }
    //HeapSort(a, 0, n-1);//actually we only need to construct the initial heap
    int N = n-1-0+1;
    int *pHeap = a+0-1;
    for (int k = N/2; k>=1; k--) FixDown(pHeap, k, N);
    //m times operation
    for (int i = 0; i<m; i++){
        a[0] /= 2;
        FixDown(a-1, 1, n);
    }
    //add up
    for (int i = 0; i<n; i++){
        sum+=a[i];
    }
    cout << sum;
    return 0;
}


Measured memory_bytes: 640
K = int(input())
AB = list(map(int, input().split()))

min = int(AB[0] / K)
max = int(AB[1] / K)

if (max - min < 1):
    if (AB[0] % K == 0):
        print("OK")
    else:
        print("NG")
else:
    print("OK")


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


int main()
{
    int t=1;
    //cin>>t;

    while( t-- )
    {
        int a,b;
        cin>>a>>b;
        cout<<a*b;
    }
    return 0;
}


Measured memory_bytes: 3600
#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define endl '\n'
#define FASTINOUT ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const ll mod = 1000000007;
const double PI = 3.141592653589793238463;
const int N=4e5+100;
int a[4];
int main()
{
    FASTINOUT;
    int x;
    cin>>x;
    int y[x];
    for (int i=0;i<x;i++){
        cin>>y[i];
        a[y[i]%4]++;
    }
    cout<<(a[0]>=(a[1]+a[3]-(a[2]==0))?"Yes":"No");
    return 0;
}


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

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) : x((x % mod + mod) % mod) {}

    mint operator-() const { return mint(-x); }

    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 { return mint(*this) += a; }

    mint operator-(const mint a) const { return mint(*this) -= a; }

    mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a; }
};

istream &operator>>(istream &is, const mint &a) { return is >> a.x; }

ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
    vector<mint> fact, ifact;

    combination(int n) : fact(n + 1), ifact(n + 1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
    }

    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n] * ifact[k] * ifact[n - k];
    }
};

struct UnionFind {
    vector<int> d;

    UnionFind(int n = 0) : d(n, -1) {}

    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (d[x] > d[y]) swap(x, y);
        d[x] += d[y];
        d[y] = x;
        return true;
    }

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

    int size(int x) { return -d[find(x)]; }
};

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

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

int dfs(int l, char c, string s) {
    if (l == 0 || s == string(l + 1, c))return l;

    string t = "";
    for (int i = 0; i < l; ++i) {
        if (s[i] == c || s[i + 1] == c)t += c;
        else t += s[i];
    }
    return dfs(l - 1, c, t);
}

int main() {
    string s;
    cin >> s;
    int ans = 1e9;
    for (int i = 0; i < 26; ++i) {
        int k = s.size() - dfs(s.size() - 1, 'a' + i, s) - 1;
        ans = min(ans, k);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3620
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <iomanip>

using ll = long long;

struct UnionFind {
    std::vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for (int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};


int main() {
    int N, M;
    std::cin >> N >> M;

    UnionFind uf(N);
    for (int i = 0; i < M; i++) {
        int x, y, z;
        std::cin >> x >> y >> z;
        x--;
        y--;
        uf.unite(x, y);
    }

    std::map<int, int> m;
    for (int i = 0; i < N; i++) {
        m[uf.root(i)]++;
    }

    std::cout << m.size() << std::endl;

    return 0;
}


Measured memory_bytes: 5376
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>

#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#define ABS(a) ((a)>0 ? (a) : (a * -1))

using namespace std;
int main(){
	long long n, c, k, ans;
	cin >> n >> c >> k;
	vector<long long> t(n);
	for (long long i = 0; i < n; i++) {
		cin >> t[i];
	}
	sort(t.begin(), t.end());

	long long point0 = 0;
	long long cnt = 0;
	long long cnt1 = 0;
	for (long long i = 0; i < n-1; i++) {
		cnt1++;
		if ((t[i+1] - t[point0]) > k) {
			cnt++;
			cnt1 = 0;
			point0 = i+1;
		}
		if (cnt1 >= c) {
			cnt++;
			cnt1 = 0;
			point0 = i+1;
		}
	}
	cnt++;


	cout << cnt << endl;
}


Measured memory_bytes: 1024
/*
  GOOD is not Enough
  You've got to be GREAT.
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i++)
#define fr(i,k,n) for(int i=k;i>=n;i--)
#define pb push_back
#define pii pair<int,int>
#define dbg(x) cout << (#x) << " is " << (x) << '\n' 
#define F first
#define S second
#define all(a) a.begin(),a.end()
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

void solve()
{ 
  int k,a,b;
  cin >> k >> a>>b;
  fp(i,a,b)
  {
      if(i%k==0)
      {
          cout << "OK\n";return;
      }
  }
  cout << "NG\n";
  return;
}

signed main()
{
  FAST
  int T=1;
  // cin >> T;
  while(T--)
  {
    solve();
  }
  return 0;
}

Measured memory_bytes: 3612
fun main(args: Array<String>) {
    val t = readLine()!!

    var pre = t[0]

    val ans = StringBuffer("")
    repeat(t.length) {
        if (t[it] == '?') {
            ans.append('D')
            pre = 'D'
        } else {
            ans.append(t[it])
            pre = t[it]
        }
    }
    println(ans)
}


Measured memory_bytes: 39880
import Foundation
func toTuple<T>(_ args: [T]) -> (T, T) { (args[0], args[1]) }
func readInts() -> [Int] { readLine()!.split(separator: " ").map { Int($0)! } }

var (n, x) = toTuple(readInts())
var mi: [Int] = []
var ans = 0
for _ in 0..<n { mi.append(Int(readLine()!)!) }
let sum = mi.reduce(0) { $0 + $1 }
x -= sum
ans += n + (x / mi.min()!)
print(ans)

Measured memory_bytes: 13544
#include <bits/stdc++.h>
using namespace std;
//using lint = long long;

struct P
{
    int x,y;

    bool operator<(const P &a)const{
        if(x!=a.x)
              return x>a.x;
        return y>a.y;
    }

};

vector<int> v[111111],v1;

//bitset<4001000> b;
int i,n,d,m,k,a,b,c,h;

int o[111111];
int l[3];
int j[1];
int e;


//int dx[10]={2,2,-2,-2,1,1,-1,-1},dy[10]={1,-1,1,-1,2,-2,2,-2};

int dx[10]={0,1,0,-1,1,1,-1,-1},dy[10]={1,0,-1,0,1,-1,1,-1},dz[10]={0,0,0,0,1,-1};
long long mod=1000000007,mod2=1000000009,mod3=2017;
long long x,z,y;
double pi=3.14159265;
P u[311111];
stack<int> s;
//set<int> s[222][22];
//priority_queue<P> q;
queue<int> q;
//2147483647
//'1'==49;
//'A'==65;
//'a'==97;
//unordered_
//map<int,int> p;
/*
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
unordered_map<long long, int, custom_hash> safe_map;
*/
//list<int> l;

string r,r1;
//char r[2];
//deque<int> de;
//srand((int)time(NULL));
/*bool as(P a,P b)
{
    if(a.x!=b.x)
    return a.x<b.x;
    return a.y<b.y;
}*/



bool as(P a,P b)
{
    if(a.x!=b.x)
        return a.x<b.x;
    return a.y<b.y;

}

int f(int n)
{
    if(o[n]>=0) return o[n];
    o[n]=0;
    for(int h=0;h<v[n].size();h++)
        o[n]=max(o[n],f(v[n][h])+1);
    return o[n];
}


int main()
{
    memset(o,-1,sizeof(o));
    scanf("%d %d",&a,&b);
    for(int t=1;t<=b;t++)
    {
        scanf("%d %d",&n,&m);
        v[m].push_back(n);
    }
    for(int t=1;t<=a;t++)
    {


        d=max(d,f(t));
    //printf("%d ",f(t));
    }
    printf("%d",d);
}





Measured memory_bytes: 11264
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  int n,a[101];
  int i,j;
  cin >> n;
  for(i=0;i<n;i++){
    cin >> a[i];
  }
  for(i=0;i<n-1;i++){
    for(j=0;j<n-1-i;j++){
	int temp=a[j];
	a[j]=a[j+1];
	a[j+1]=temp;
      }
  } 
  for(int i=0;i<n-1;i++){
    cout << a[i] << ' ' ;
  }
  cout << a[n-1] << endl;
  return(0);
}



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

const long long MOD = 1000000007;

void solve(std::string S){
    vector<LL> dp1 = {1,0,0,0};
    vector<LL> dp2(4,0);
    for(auto c: S){
        copy(dp1.begin(), dp1.end(), dp2.begin());
        switch(c){
            case 'A':
                dp2[1]=(dp1[1]+dp1[0])%MOD;
                break;
            case 'B':
                dp2[2]=(dp1[2]+dp1[1])%MOD;
                break;
            case 'C':
                dp2[3]=(dp1[3]+dp1[2])%MOD;
                break;
            case '?':
                dp2[0]=(3*dp1[0])%MOD;
                dp2[1]=((3*dp1[1])%MOD+dp1[0])%MOD;
                dp2[2]=((3*dp1[2])%MOD+dp1[1])%MOD;
                dp2[3]=((3*dp1[3])%MOD+dp1[2])%MOD;
                break;
        }
        swap(dp1, dp2);
    }
    cout << dp1[3] << endl;
}

int main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


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

#define sc second
#define fr first

#define ll long long
#define pi pair<int,int>
#define ppi pair<pi,pi>
#define mp make_pair
using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "


int main()
{

    ios::sync_with_stdio(0);
    cin.tie(0) , cout.tie(0);

    int n; cin >> n;
    bool ok = 1;
    for(int i = 1;i <= n;i++)
    {
        int x; cin >> x;
        if(x % 2 == 0 && x % 3 != 0 && x % 5 != 0)
            ok = 0;
    }
    if(ok )cout << "APPROVED\n";
    else   cout << "DENIED\n";


    return 0;
}


Measured memory_bytes: 256
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn solve(n: u64) -> u64 {
  n * (n - 1) / 2
}

fn main() {
  input! {
    n: u64
  };

  println!("{}", solve(n));
}

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

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

using ll = long long;
using lb = long double;
using P = pair<ll,ll>;
using graph = vector<vector<ll>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 998244353LL;

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

int main(){
    ll N,K;
    cin>>N>>K;
    vector<ll> cnt(200005);
    rep(i,K){
        ll l,r;
        cin>>l>>r;
        cnt[l]++;
        cnt[r+1]--;
    }
    rep(i,200005) cnt[i] = -cnt[i];
    cnt[0]++;
    cnt[1]--;
    vector<ll> ans(200005);
    ans[0] = 1;
    map<ll,ll> m;
    rep(i,200005) {
        if(cnt[i] != 0) m[i] = cnt[i];
    }
    for(ll i = 1; i < 200005; i++){
        for(auto x:m){
            if(x.first<=i&&x.first!=0) ans[i] = (ans[i]-x.second*ans[i-x.first])%mod;
        }
    }
    vector<ll> anss(200005);
    rep(i,200004){
        anss[i+1] = ans[i];
    }
    rep(i,200005) ans[i] = (ans[i] - anss[i])%mod;
    if(ans[N-1]<0) ans[N-1] += mod;
    cout<<ans[N-1]<<endl;
}

Measured memory_bytes: 8016
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
ab = mip(n - 1)
c = wi()
c.sort(reverse=True)

connect = [[] for _ in range(n)]

for x, y in ab:
    connect[x].append(y)
    connect[y].append(x)

flagli = [0] * n
ansli = [0] * n

queue = deque()
queue.append(0)
cnt = 0
while queue != deque():
    kk = queue.popleft()
    flagli[kk] = 1

    ansli[kk] = c[cnt]
    cnt += 1

    for root in connect[kk]:
        if flagli[root] == 0:
            queue.append(root)

sumans = sum(c) - max(c)
print(sumans)
print(*ansli)

Measured memory_bytes: 9452
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase

/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
//WATLEPIYO
vector<llint>fla;
vector<llint>gya;
llint nCr(int x,int y){
	if(x<0||y<0){return 0;}
	return fla[x+y]*gya[x]%mod*gya[y]%mod;
}
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int i,j,H,W,n;cin>>H>>W>>n;
	vector<pair<int,int>>bas(n+2);
	fla.res(H+W);
	gya.res(H+W);
	fla[0]=1;
	for(i=1;i<H+W;i++){fla[i]=fla[i-1]*i%mod;}
	llint bgen=fla[H+W-1];gya[H+W-1]=1;
	for(int h=0;h<30;h++){
		if((mod-2)&(1<<h)){gya[H+W-1]*=bgen;gya[H+W-1]%=mod;}
		bgen*=bgen;bgen%=mod;
	}
	for(i=H+W-1;i>0;i--){gya[i-1]=gya[i]*i%mod;}
	bas[0]=mp(1,1);bas[n+1]=mp(H,W);
	for(i=1;i<=n;i++){cin>>bas[i].fir>>bas[i].sec;}
	SO(bas);
	vector<llint>dp(n+2);dp[0]=mod-1;
	for(i=1;i<n+2;i++){
		for(j=0;j<i;j++){
			dp[i]-=dp[j]*nCr(bas[i].fir-bas[j].fir,bas[i].sec-bas[j].sec);
			dp[i]%=mod;
		}
		dp[i]+=mod;dp[i]%=mod;
	}
	cout<<dp[n+1]<<endl;
	
	return 0;
}


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

using namespace std;

#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define pb emplace_back
#define ll long long
#define ld long double

const int INF = 2e9 + 1;
const ll INFLL = 1e18 + 1;
const int mod = 1e9 + 7;
const int MAXN = 5e5 + 5;

int main() {
    int x;
    cin >> x;
    int ans = 0;
    ans += (x / 500) * 1000;
    x %= 500;
    ans += (x / 5) * 5;
    cout << ans;
    return 0;
}


Measured memory_bytes: 3580
#include <stdio.h>
#include <math.h>
using namespace std;
int main(){
    double r;
    // double r;
	scanf("%lf",&r);
	printf("%lf %lf\n",M_PI*r*r,2*M_PI*r);
    // printf("%lf %lf\n",r*r*pi,2*r*pi);
    return 0;
}


Measured memory_bytes: 2688
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,q,a,b,ans;
map<ll,ll> mp;
int main(void){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        mp[a]++;
        ans+=a;
    }
    cin>>q;
    while(q--){
        cin>>a>>b;
        ans-=mp[a]*a;
        ans+=b*mp[a];
        mp[b]+=mp[a];
        mp[a]=0;
        cout<<ans<<endl;
    }
}


Measured memory_bytes: 9568
import heapq
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A = [-i for i in A]
    heapq.heapify(A)
    for _ in range(M):
        t = heapq.heappop(A)
        heapq.heappush(A, t/2)
    return sum(int(-i) for i in A)
print(main())



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

using namespace std;
typedef long long ll;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef map <str,int> mapsi;
typedef map <str,int> :: iterator mapsitr;
typedef map <int , int> mint;
typedef map <ll , ll> mll;
typedef set <int> si;
typedef set <ll> sll;
typedef si :: iterator sitr;
typedef si :: reverse_iterator rsitr;
typedef sll :: iterator sltr;
typedef sll :: reverse_iterator rsltr;
#define mset multiset

typedef mset <int> msi;
typedef mset <ll> msll;
typedef msi :: iterator msitr;
typedef msi :: reverse_iterator msritr;
typedef msll :: iterator msltr;
typedef msll :: reverse_iterator mslritr;

#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'

char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 126)return char(x-32);
    else if( 65 <= int(x) && int(x) <= 90)return x;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 126)return x;
    else if( 65 <= int(x) && int(x) <= 90)return char(x+32);
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57)return int(x-'0');
    else if( 97 <= int(x) && int(x) <= 126)return int(x-96);
    else if( 65 <= int(x) && int(x) <= 90)return int(x-64);
}
bool isect (int l1, int r1, int l2, int r2){
    pii p1,p2;
    p1 = mp(l1,r1); p2 = mp(l2,r2);
    if(p1>p2)swap(p1,p2);
    if(p2.fi <= p1.se)return true;
    else return false;
}
ll fexp (ll num1, ll num2, ll MOD){
    if(num2==0)return 1;
    else if(num2==1)return num1;
    else{
        ll temp = fexp (num1,num2/2,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2%2==1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return fexp (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/gcd(num1,num2))*num2;}


// end of Template
int x,a,b;
int main(){

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>x>>a>>b;
    if(abs(x-a) <= abs(x-b))cout<<"A\n";
    else cout<<"B\n";
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define itrep(i,x) for(auto i=(x).begin(); i!=(x).end();i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
//typedef std::pair<int,int> P;
int D=1;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};

using namespace std;

int main(){
    map<ll,int> mp;
    int n;
    cin>>n;
    ll ret=0,sum=0;
    mp[0]++;
    rep(i,0,n){
        ll x;
        cin>>x;
        sum+=x;
        ret+=mp[sum];
        mp[sum]++;
    }
    cout<<ret<<endl;
    
    return 0;
}


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

int main() {
  int n, n_, f = 0;
  cin >> n;
  n_ = n;
  
  int i = 0;
  while((int)pow(10, i) <= n) {
    i++;
  }
  
  for(int j = i-1; j >= 0; j--) {
    f += n_ / (int)pow(10, j);
    n_ = n_ % (int)pow(10, j);
  }
  
  //cout << f << endl;
  if (n % f == 0)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric> // accumulate(v.begin(), v.end(), 0)                                                                                                                         

using namespace std;
#define ll long long

struct pass{
  int src;
  int dst;
};

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

  int C[H*W];
  for(int i=0; i<H*W; i++){
    if(i == 0)
      C[i] = 0;
    else
      C[i] = H*W+1;
  }

  vector<pass> P;

  int S[H][W];
  int n_white = 0;
  for(int i=0; i<H; i++){
    string in;
    cin >> in;
    for(int j=0; j<W; j++){
      if(in[j] == '#')
        S[i][j] = 0;
      else{
        S[i][j] = 1;
        n_white++;
      }
      if(!(i == 0 && j == 0)){
        if(!(i == 0)){
          if(S[i][j] == 1 && S[i-1][j] == 1){
            pass p1 = {i*W+j, (i-1)*W+j};
            pass p2 = {(i-1)*W+j, i*W+j};
            P.push_back(p1); P.push_back(p2);
          }
        }
        if(!(j == 0)){
          if(S[i][j] == 1 && S[i][j-1] == 1){
            pass p1 = {i*W+j, i*W+(j-1)};
            pass p2 = {i*W+(j-1), i*W+j};
            P.push_back(p1); P.push_back(p2);
          }
        }
      }
    }
  }

  for(int i=0; i<H*W; i++){
    for(int j=0; j<P.size(); j++){
      int s = P[j].src;
      int d = P[j].dst;
      if(C[d] > C[s] + 1)
        C[d] = C[s] + 1;
    }
  }

  if(C[H*W-1] == H*W+1)
    cout << -1 << endl;
  else
    cout << n_white - (C[H*W-1] + 1) << endl;

  return 0;
}


Measured memory_bytes: 512
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
// ceil() 切り上げ, floor() 切り捨て
// next_permutation(all(x))

int		main(void)
{
	ll l, r;
	cin >> l >> r;

	ll ans = 20190;
	for (ll i = l; i < r; i++)
	{
		for (ll j = i+1; j <= r; j++)
		{
			ans = min(ans, i*j % 2019);
			if (ans == 0)
			{
				cout << ans << endl;
				return 0;
			}
		}
	}
	cout << ans << endl;
}



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

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

    int len = S.size();
    int cnt = 0;
    for(int i = 0; i < len; i++)
        if(S[i] == '0') cnt++;

    cout << 2 * min(cnt, len-cnt) << endl;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for( (i)=0 ; (i)<(n) ; (i)++ )
#define rep(i,x,n) for( (i)=(x) ; (i)<(n) ; (i)++ )
#define REV(i,n) for( (i)=(n) ; (i)>=0 ; (i)-- )
#define FORIT(it,x) for( (it)=(x).begin() ; (it)!=(x).end() ; (it)++ )
#define foreach(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define rforeach(it,c) for(__typeof((c).rbegin()) it=(c).rbegin();it!=(c).rend();++it)
#define foreach2d(i, j, v) foreach(i,v) foreach(j,*i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define SZ(x) ((int)(x).size())
#define MMS(x,n) memset(x,n,sizeof(x))
#define mms(x,n,s) memset(x,n,sizeof(x)*s)
#define pb push_back
#define mp make_pair
#define NX next_permutation
#define UN(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define CV(x,n) count(all(x),(n))
#define FIND(x,n) find(all(x),(n))-(x).begin()
#define ACC(x) accumulate(all(x),0)
#define PPC(x) __builtin_popcountll(x)
#define LZ(x) __builtin_clz(x)
#define TZ(x) __builtin_ctz(x)
#define mxe(x) *max_element(all(x))
#define mne(x) *min_element(all(x))
#define low(x,i) lower_bound(all(x),i)
#define upp(x,i) upper_bound(all(x),i)
#define NXPOW2(x) (1ll << ((int)log2(x)+1))
#define PR(x) cout << #x << " = " << (x) << endl ;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef pair<int, int> pii;

const int OO = (int) 2e9;
const double eps = 1e-9;

const int N = 100005;

int n;
int a[N];
hash_map<int, int> M;

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
//	freopen("in.txt", "rt", stdin);
//	freopen("out.txt", "wt", stdout);
#endif
	cin >> n;
	int res = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		if (M[a[i]]) {
			M[a[i]] = 0;
			res--;
		} else {
			M[a[i]] = 1;
			res++;
		}
	}
	cout << res << endl;
	return 0;
}


Measured memory_bytes: 6072
#include<iostream>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<algorithm> 
using namespace std;
 
# define p(s) std::cout << s << endl;
# define printIf(j,s1,s2) cout << (j ? s1 : s2) << endl;
# define YES(j) cout << (j ? "YES" : "NO") << endl;
# define Yes(j) std::cout << (j ? "Yes" : "No") << endl;
 
long long int gcd(long long int a,long long int b){
    if(a<b){
        long long int tmp=a;
        a=b;
        b=tmp;
    }

    long long int r = a % b;
    while(r!=0){
        a = b;
        b = r;
        r = a % b;
    }
    return b;
}

long long int lcm(long long int a,long long int b){
    long long int g = gcd(a,b);
    return (a/g)*b;
}

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

    long long int l;cin >> l;
    for(int i=0;i<n-1;i++){
        long long int tmp;cin >> tmp;
        l = lcm(l,tmp);
    }
    p(l)
}

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

using namespace std;

int main() {
    cin.tie(nullptr); ios::sync_with_stdio(false);
	
    int N, K; cin>>N>>K;
    int root = 0;
    vector<int> graph[N];
    for (int i = 1; i < N; i++) {
        graph[0].push_back(i);
    }

    const int max = (N-1)*(N-2)/2;
    int n = max - K;
    if (n < 0) {
        cout << -1 << endl;
        return 0;
    }
    for (int i = 1; n > 0 and i < N; i++) for (int j = i; n > 0 and j < N; j++) {
        if (i == j)
            continue;
        n--;
        graph[i].push_back(j);
    }

    cout << (N-1)+max-K << endl;
    for (int i = 0; i < N; i++) for (auto t : graph[i]) {
        std::cout << i+1 << ' ' << t+1 << endl;
    }
	
    return 0;
}


Measured memory_bytes: 384
#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};
fn main() {
    input! {//
        n:usize,
        mut a:usize,mut b:usize,mut c:usize,
        s:[Chars;n],
    }
    let mut var = vec![a, b, c];

    let mut ans = vec![];
    for i in 0..n {
        let q = &s[i];
        let j = q.iter().cloned().map(id).collect::<Vec<_>>();
        if var[j[0]] < var[j[1]] {
            var[j[0]] += 1;
            var[j[1]] -= 1;
            ans.push(q[0]);
        } else if var[j[0]] == var[j[1]] {
            if var[j[0]] == 0 {
                println!("No");
                return;
            }
            if i + 1 < n && (s[i + 1][0] == q[0] || s[i + 1][1] == q[0]) {
                var[j[0]] += 1;
                var[j[1]] -= 1;
                ans.push(q[0]);
            } else {
                var[j[0]] -= 1;
                var[j[1]] += 1;
                ans.push(q[1]);
            }
        } else {
            var[j[0]] -= 1;
            var[j[1]] += 1;
            ans.push(q[1]);
        }
    }
    println!("Yes");
    for &a in ans.iter() {
        println!("{}", a);
    }
}
fn id(x: char) -> usize {
    match x {
        'A' => 0,
        'B' => 1,
        _ => 2,
    }
}


Measured memory_bytes: 8324
#include <iostream>
#include <deque>
#include <stack>
#include <string>
#include <cstring>

using namespace std;

#define MAX_N 2001 
enum T {FROM, TO, NONE};

string s;
int dp[MAX_N][MAX_N];
T which_drop[MAX_N][MAX_N];
int drop(int from, int to) {
    if (dp[from][to] >= 0) return dp[from][to];
    if (to - from < 0) return 0;
    if (to == from) {
        return 0;
    }
    if (s[from] == s[to]) {
        which_drop[from][to] = NONE;
        return drop(from+1, to-1);
    }

    int drop_first = drop(from+1, to)+1;
    int drop_last = drop(from, to-1)+1;

    if (drop_first < drop_last) {
        which_drop[from][to] = FROM;
        return dp[from][to] = drop_first;
    } else {
        which_drop[from][to] = TO;
        return dp[from][to] = drop_last;
    }
}

string org(int from, int to) {
    string ret = "";
    if (from == to) {
        ret.push_back(s[from]);
        return ret;
    }
    if (to - from < 0) {
        return ret;
    }
    switch (which_drop[from][to]) {
        case FROM:
            ret.append(org(from+1, to));
            break;
        case TO:
            ret.append(org(from, to-1));
            break;
        case NONE:
            ret.push_back(s[from]);
            ret.append(org(from+1, to-1));
            ret.push_back(s[to]);
            break;
    }
    //cout << from << ' ' << to << ':';
    //cout << which_drop[from][to] << endl;
    //cout << ret << endl;
    return ret;
} 


int main() {
    while (cin >> s) {
        memset(dp, -1, sizeof(dp));
        memset(which_drop, -1, sizeof(which_drop));
        drop(0, s.size()-1);
        //cout << drop(0, s.size()-1) << endl;
        cout << org(0, s.size()-1) << endl;
    }
    return 0;
}

Measured memory_bytes: 32712
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <fstream>

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase(unique(ALL(v)), v.end());

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 28);
const ll LINF = 2e18;
const double PI = acos(-1);

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M;
	ll C;
	cin >> N >> M >> C;
	vll B(M);
	REP(i, M)
	cin >> B[i];
	int ans = 0;
	REP(i, N)
	{
		ll cur = 0;
		REP(j, M)
		{
			ll a;
			cin >> a;
			cur += a * B[j];
		}
		cur += C;
		if (cur > 0)
			ans++;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <iomanip>
#include <array>
#include <numeric>
#include <regex>
#include <bitset>
#include <deque>
#include <assert.h>

using namespace std;
using ll = long long;

const int INF = 1e9;
const double PI = acos(-1.0);
const ll MOD = 1e9 + 7;

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << ": " << x << endl;
#define popcnt __builtin_popcount


ll N, K;
ll A[100010];

ll f(ll X) {
	ll ret = 0;
	for (int i = 0; i < N; i++)
	{
		ret += (X^A[i]);
	}
	return ret;
}


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

	ll opt = 0LL;
	for (int k = 40; k >= 0; k--) {
		if ((opt + (1LL << k)) > K)continue;

		ll cnt = 0;
		for (int i = 0; i < N; i++) {
			cnt += (A[i] >> k) & 1;
		}

		if (cnt < N - cnt)opt += (1LL << k);

	}

	cout << f(opt) << endl;

	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <stack>
#include <functional>
#include <math.h>
#include <iomanip>
using namespace std;
#define Would
#define you

int main() {
	int a, b, ans = 0;
	cin >> a >> b;
	for (int i = a; i <= b; ++i) {
		if (i % 10 == (i - i % 10000) / 10000 && (i % 100 - i % 10) / 10 == (i % 10000 - i % 1000) / 1000) ans += 1;
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <climits>
using namespace std;

int n;
int a[100][100];
int b[5050][100];

int main() {
  int c, d, e, f, g, h;

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

  for (int i=0; i<n; i++) {
    b[i][0] = a[i][0];
  }
  for (int i=0; i<n; i++) {
    for (int j=1; j<n; j++) {
      b[i][j] = b[i][j-1] + a[i][j];
    }
  }
  c = n;
  for (int i=1; i<n; i++) {
    d = n-i+1;
    for (int j=i; j<n; j++) {
      for (int k=0; k<n; k++) {
        b[c][k] = b[c-d][k] + b[j][k];
      }
      c++;
    }
  }

  h = -INT_MAX;
  e = (n*(n+1))/2;
  for (int i=0; i<e; i++) {
    f = g = 0;
    for (int j=0; j<n; j++) {
      if (b[i][j] < f) {
        f = g = b[i][j];
      } else if (b[i][j] > g) {
        g = b[i][j];
        if (g-f > h) {
          h = g-f;
        }
      }
    }
  }

  if (h < 0) {
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
        if (h < a[i][j]) {
          h = a[i][j];
        }
      }
    }
  }

  cout << h << endl;

  return 0;
}

Measured memory_bytes: 3172
#include <iostream>
#define NMAX 100005

using namespace std;

int ans = 0,N,A[NMAX] = {},B[NMAX] = {};
int INF = (int)1e9;
string S;

void solve(){
	for (int i = 0; i < N; ++i)
	{
		if(B[i] == INF){
			ans = A[i] + B[i];
			return;
		}
	}
}

void answer(){
	cout << ans << "\n";
}

int main(){
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> A[i] >> B[i];
		INF = min(INF,B[i]);
	}
	solve();
	answer();

	return 0;
}

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

int main() {
  int N;
  string s;
  cin >> N >> s;
  N %= 26;
  int c = s.size();
  for (int i = 0; i < c; i++) {
    char p = s.at(i) + N;
    if (p > 'Z') {
      p -= 26;
    }
    s.at(i) = p;
  }
  cout << s <<endl;
}

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

int main() {
    uint64_t a, b;
    cin >> a >> b;
    uint64_t sum_a = 0;
    
    uint64_t i;
    for (i = 0; i < 1000; i++) {
        sum_a += i;
        if (a < sum_a) break;
    }
    uint64_t sum_b = sum_a;

    i++;
    for (; i < 1000; i++) {
        sum_a = sum_b;
        sum_b += i;
        if ((sum_a - a) == (sum_b - b)) break;
    } 
    cout << sum_a - a << endl;

    return 0;
}



Measured memory_bytes: 256
#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=100000;

int n;
int a[3][MAXN];

int x[MAXN];
int y[MAXN];
int z[MAXN+1];

int bt[MAXN+1];
int opos[MAXN];
int bget(int idx) { ++idx; int ret=0; while(idx>0) { ret+=bt[idx]; idx-=idx&-idx; } return ret; }
void bmod(int idx,int by) { idx++; while(idx<=n) { bt[idx]+=by; idx+=idx&-idx; } }


bool solve() {
	REP(i,n) {
		int p=a[0][i],q=a[1][i],r=a[2][i];
		if((p-1)%3==0&&q==p+1&&r==q+1) { x[i]=+r/3; continue; }
		if((r-1)%3==0&&q==r+1&&p==q+1) { x[i]=-p/3; continue; }
		//printf("error in col %d\n",i);
		return false;
	}
	//REP(i,n) printf("%2d ",x[i]); puts("");
	REP(i,n) if(abs(abs(x[i])-1-i)%2!=0) {
		//printf("error in moving col %d to %d\n",i,abs(x[i])-1);
		return false;
	}
	REP(par,2) {
		REPE(i,n) z[i]=0,bt[i]=0;
		REP(i,n) opos[abs(x[i])-1]=i;
		for(int i=par;i<n;i+=2) {
			int npass=bget(opos[i]);
			int j=opos[i]+2*npass; /*printf("\t\ti=%d j=%d opos=%d npass=%d\n",i,j,opos[i],npass);*/ assert(j>=i&&(j-i)%2==0);
			int ninv=npass+(j-i)/2;
			y[i]=ninv%2==0?+x[opos[i]]:-x[opos[i]];
			//printf("\t%d: originally at %d, now at %d, passed %d times -> total inv=%d\n",i,opos[i],j,npass,ninv);
			z[i+1]++,z[j+1]--;
			bmod(0,+1); bmod(opos[i]+1,-1);
		}
		//printf("z="); REPE(i,n) printf("%2d ",z[i]); puts("");
		int cur=0; for(int i=1-par;i<=n;i+=2) { cur+=z[i]; if(i<n) y[i]=cur%2==0?+x[i]:-x[i]; } assert(cur==0);
		REP(i,n) x[i]=y[i];
		//REP(i,n) printf("%2d ",x[i]); puts("");
	}
	REP(i,n-2) if(x[i]<0) x[i]=-x[i],x[i+2]=-x[i+2];
	REP(i,n) if(x[i]<0) {
		//printf("error in parity\n");
		return false;
	}
	return true;
}

void run() {
	scanf("%d",&n);
	REP(i,3) REP(j,n) scanf("%d",&a[i][j]);
	printf("%s\n",solve()?"Yes":"No");
}

int main() {
	run();
	return 0;
}


Measured memory_bytes: 3328
#include <iostream>
using namespace std;
int main(void){
    int c[4];
    fill(c,c+4,0);
    for(int i=0;i<3;i++){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        c[a]++;
        c[b]++;
    }
    bool f=true;
    for(int i=0;i<4;i++){
        if(c[i]==0||c[i]>2)f=false;
    }
    if(f){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO";
    }
    return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()

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

using ll = long long;

ll N;
std::map<ll, ll> mp;

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

  rep( i, N )
  {
    ll A;
    std::cin >> A;

    ++mp[A];
  }

  ll ans = 0;

  for( auto it = mp.begin(); it != mp.end(); ++it )
    ans += (it->second & 1);

  std::cout << ans << std::endl;

  return 0;
}

Measured memory_bytes: 6528
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int N, i, mid;
    int x[200001], x_[200001];

    cin >> N;
    for (i = 0; i < N; i++)
    {
        cin >> x[i];
        x_[i] = x[i];
    }

    sort(x, x + N);

    mid = x[N / 2 - 1];

    for (i = 0; i < N; i++)
    {
        if (mid < x_[i])
        {
            cout << x[N / 2 - 1] << endl;
        }
        else
        {
            cout << x[N / 2] << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 3712