akhauriyash/Code-Regression huggingface dataset:
module WeightedGraph
  (Vertex : sig
    type t
    val compare : t -> t -> int
  end)
  (Weight : sig
    type t
    val compare : t -> t -> int
  end) :
sig
  val prim :
    (* 隣接リスト *)
    (Vertex.t -> (Vertex.t * Weight.t) list) ->
    (* 始点 *)
    Vertex.t ->
    (* 最小全域木に含まれる辺のリスト *)
    (Vertex.t * Vertex.t * Weight.t) list
end =
struct
  module VSet = Set.Make (Vertex)
  module WMap = Map.Make (Weight)

  (*
   * プリム法のメインループ
   * es : 隣接リスト
   * vs : 訪れた頂点の集合
   * q : 訪れた頂点から伸びる辺が重み順に入ったヒープ
   * acc : 最小全域木に使うのが確定した辺を入れるやつ
   *)
  let rec prim_aux es acc vs q =
    match WMap.min_binding q with
    | exception Not_found -> acc
    | (w, uvs) ->
        let rec inner = function
          | [] -> prim_aux es acc vs (WMap.remove w q)
          | (u, v) :: uvs ->
              if VSet.mem v vs
              (* vは既に訪れていた *)
              then inner uvs
              (* vはまだ訪れていなかった *)
              else prim_aux es ((u, v, w) :: acc) (VSet.add v vs) @@
                (* vから伸びる辺をキューに追加 *)
                List.fold_left (fun q (u, w) ->
                  (* 現時点で既に訪れている頂点への辺は追加しない *)
                  if VSet.mem u vs
                  then q
                  else WMap.add w ((v, u) :: try WMap.find w q with Not_found -> []) q) (WMap.add w uvs q) (es v) in
        inner uvs
                
  let prim es s =
    prim_aux es [] (VSet.singleton s) @@
      (* 始点から伸びる辺をキューに入れておく *)
      List.fold_left (fun q (v, w) ->
        WMap.add w ((s, v) :: try WMap.find w q with Not_found -> []) q) WMap.empty (es s)
end

module Int = struct
  type t = int
  let compare = compare
end

module G = WeightedGraph (Int) (Int)

let () = Scanf.scanf "%d\n" (fun n ->
  let xys = Array.to_list @@ Array.init n (fun i -> Scanf.scanf "%d %d\n" (fun x y -> x, y, i)) in
  let es = Array.make n [] in
  begin match List.sort (fun (x, _, _) (x', _, _) -> compare x x') xys with
  | [] -> ()
  | (x, y, i) :: rest ->
      ignore @@ List.fold_left (fun (x, y, i) (x', y', j) ->
        es.(i) <- (j, min (abs (x - x')) (abs (y - y'))) :: es.(i);
        es.(j) <- (i, min (abs (x - x')) (abs (y - y'))) :: es.(j);
        (x', y', j)) (x, y, i) rest
  end;
  begin match List.sort (fun (_, y, _) (_, y', _) -> compare y y') xys with
  | [] -> ()
  | (x, y, i) :: rest ->
      ignore @@ List.fold_left (fun (x, y, i) (x', y', j) ->
        es.(i) <- (j, min (abs (x - x')) (abs (y - y'))) :: es.(i);
        es.(j) <- (i, min (abs (x - x')) (abs (y - y'))) :: es.(j);
        (x', y', j)) (x, y, i) rest
  end;
  G.prim (Array.get es) 0
  |> List.map (fun (_, _, w) -> w)
  |> List.fold_left ( + ) 0
  |> Printf.printf "%d\n")


Measured memory_bytes: 65288
#![allow(unused_imports)]

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 main() {
    input! {
        n: usize,
        init: i32,
        xs: [i32; n],
    }
    let xs = {
        let mut xs: Vec<_> = xs.iter().map(|x| x - init ).collect();
        xs.push(0);
        xs.sort();
        xs
    };
    let min_d = xs.windows(2).map(|w| w[1] - w[0]).min().unwrap();
    for d in (1..min_d + 1).rev() {
        if xs.iter().all(|x| x % d == 0) {
            println!("{}", d);
            return;
        }
    }
}


Measured memory_bytes: 6396
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n,m,l;
  cin>>n>>m>>l;
  int a[n][m];
  int b[m][l];
  long long c[n][l];
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      cin>>a[i][j];
    }
  }
  for(int i=0;i<m;i++){
    for(int j=0;j<l;j++){
      cin>>b[i][j];
    }
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<l;j++){
      c[i][j]=0;
    }
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<l;j++){
      for(int k=0;k<m;k++){
        c[i][j]+=a[i][k]*b[k][j];
      }
    }
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<l;j++){
      if(j==l-1){
        cout<<c[i][j]<<endl;
      }else{
        cout<<c[i][j]<<" ";
      }
    }
  }
  return 0;
}


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

// types
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef vector<ll> vll;

// macros
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n,l,r;
  cin >> n;

  ll ans = 0;
  REP(i, n){
    cin >> l >> r;
    ans += r-l+1;
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
ll dp[310][310][310] = {};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,K;
    cin >> N >> K;
    ll H[310];
    H[0] = 0;
    rep(i,0,N) cin >> H[i+1];
    // vector<ll> v;
    // rep(i,0,N) v.push_back(H[i]);
    // v.push_back(0);
    // sort(v.begin(), v.end());
    rep(i,0,N+1){
        rep(j,0,K+1){
            rep(k,0,N+2){
                dp[i][j][k] = INF;
            }
        }
    }
    dp[0][0][0] = 0;
    // rep(i,0,N+1){
    //     if(H[0] == v[i]){
    //         dp[1][0][i] = v[i];
    //     }else{
    //         dp[1][0][i] = INF;
    //     }
    // }
    // rep(i,0,N+1) dp[1][1][i] = 0;

    rep(i,0,N){ // i番目まで見てすでにj回操作した 最後の高さがv[k]
        rep(j,0,K+1){
            rep(k,0,N+1){
                if(dp[i][j][k] == INF) continue;
                // print(i,j,k);
                // 操作しない
                if(H[k] >= H[i+1]){
                    dp[i+1][j][i+1] = min(dp[i+1][j][i+1], dp[i][j][k]);
                }else{
                    dp[i+1][j][i+1] = min(dp[i+1][j][i+1], dp[i][j][k] + H[i+1] - H[k]);
                }
                // 操作する
                if(j == K) continue;
                dp[i+1][j+1][k] = min(dp[i+1][j+1][k], dp[i][j][k]);             
            }
        }
    }
    // print(dp[3][1][1]); // 2 0 2 => 3 1 3
    // print(dp[4][1][4]);
    ll ans = INF;
    rep(h,0,N+1){
        ans = min(ans, dp[N][K][h]);
    }
    print(ans);
    

}


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

#define froop1(a,n) for(long long i = a;i < n;i++)
#define froop2(a,n) for(int i = a;i > n;i--)
int main(){
	long long N;
  cin >> N;
  map<long long,long long> num;
  long long sN = N;
  
  froop1(2,sqrt(N)){
    if(sN == 1){
		break;
    }else{
      while(sN % i == 0){
        if(num.count(i)){
          num[i]++;
        }else{
          num[i] = 1;
        }
        sN /= i;
      }
    }
  }
  if(sN != 1){
    num[sN] = 1;
  }
    
  long long count = 0;
    for(pair<long long,long long> p : num){
      long long p2 = p.second,i = 1;
    while(p2 - i >= 0){
      p2 -= i;
      i++;
      count++;
    }
    }
    cout << count << endl;
return 0;
}

Measured memory_bytes: 3700
import java.util.Arrays;
import java.util.Scanner;

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

        int N = sc.nextInt();
        int[] W = new int[N];
        for (int i = 0; i < W.length; ++i) {
            W[i] = sc.nextInt();
        }

        System.out.println(solve(W));

        sc.close();
    }

    static int solve(int[] W) {
        int total = Arrays.stream(W).sum();
        int result = Integer.MAX_VALUE;
        int leftSum = 0;
        for (int i = 0; i < W.length - 1; ++i) {
            leftSum += W[i];

            result = Math.min(result, Math.abs(total - leftSum - leftSum));
        }

        return result;
    }
}

Measured memory_bytes: 36236
N, M, X = map(int,input().split())
books = []
for _ in range(N):
    books.append(list(map(int,input().split())))
ans = float('inf')
for i in range(2 ** N):
    money = 0
    algo = [0 for _ in range(M)]
    for j in range(N):
        if i & 1 == 1:
            money += books[j][0]
            for k in range(M):
                algo[k] += books[j][k+1]
        i >>= 1
    if all([j >= X for j in algo]):
        ans = min(ans, money)

if ans == float('inf'):
    print(-1)
else:
    print(ans)

Measured memory_bytes: 9228
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
ll l2(ll n)
{
	ll a = 0;
	n /= 2;
	while (n)
	{
		a++;
		n /= 2;
	}
	return a;
}


int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);

	freopen("output.txt", "w", stdout);
#endif

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

	ll  n;
	cin >> n;
	ll arr[n];
	ll u = 0;
	for (ll i = 0; i < n; i++)
	{
		cin >> arr[i];
		if (arr[i] == 0)
		{
			u++;
		}
	}
	if (u)
	{
		cout << 0;
		return 0;
	}
	ll s = 1;
	for (ll i = 0; i < n; i++)
	{


		if (arr[i] <= (1000000000000000000) / s)
		{
			s *= arr[i];

		}
		else
		{
			cout << -1;
			return 0;
		}
	}
	cout << s << endl;
}



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

void solve()
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	int i = 1;
	while(a>0&&c>0)
	{
		if(i%2==1)
		{
			c-=b;
		}
		else
		{
			a-=d;
		}
		i++;

	}
	if(a<=0)
		cout<<"No"<<endl;
	else
		cout<<"Yes"<<endl;
}

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

Measured memory_bytes: 3624
#include <iostream>

using namespace std;

int main() {
    int S;
    int h,m,s;
    cin >> S;

    h = S / 3600;
    m = (S%3600) / 60;
    s = ((S%3600) % 60);

    cout << h << ":" << m << ":" << s << "" <<endl;
    return 0;
}

Measured memory_bytes: 3068
#include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void){
    int x, y; cin >> x >> y;
    if(x == 2 or y == 2) cout << "No" << endl;
    else if(x == 4 or x == 6 or x == 9 or x == 11){
        if(y == 4 or y == 6 or y == 9 or y == 11) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    else if(y == 1 or y == 3 or y== 5 or y == 7 or y == 8 or y == 10 or y == 12) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10000000)

MOD = 10 ** 30 + 7

N = input()
E = [[] for i in range(N)]

for i in range(N - 1):
    a, b = map(int, raw_input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    E[b].append(a)

m = {}

def func(num, pre):
    S = 0
    for to in E[num]:
        if to != pre:
            S += func(to, num)
    S *= 1000000007
    S += 1
    S %= MOD
    if not S in m:
        m[S] = 1
    else:
        m[S] += 1
    return S

func(0, -1)

ans = 0
for key in m:
    ans += m[key] * (m[key] - 1) / 2
print ans


Measured memory_bytes: 126344
# coding: utf-8
# Your code here!

a = input()
x = 0
if len(a) == 1:
    x = int(a)
else:
    x = int(a[-1])

if x == 2 or x == 4 or x == 5 or x == 7 or x == 9:
    print("hon")
elif x == 0 or x == 1 or x == 6 or x == 8:
    print("pon")
elif x == 3:
    print("bon")

Measured memory_bytes: 9192
#include<bits/stdc++.h>
using namespace std;
int main(){
   long long a, b, c, d;
   cin >> a >> b >> c >> d;
   while(1){
      c-=b;
      if(c<=0){
         cout << "Yes" <<endl;
         return 0;
      }
      a-=d;
      if(a<=0){
         cout << "No" <<endl;
         return 0;
      }
   }
   return 0;
}



Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define pdd pair<double,double>
#define cpdd const pdd
int main(){
    int n;
    scanf("%d",&n);
    LL a[200005];
    LL b[200005];
    for(int i = 0;i<n;i++){
        scanf("%lld",&a[i]);
    }
    for(int i = 0;i<n;i++){
        scanf("%lld",&b[i]);
    }
    LL ans=0;
    queue<int> q;
    for(int i = 0;i<n;i++){
        if(b[i]>=(a[i]+b[(i+1)%n]+(b[(i-1+n)%n]))){
            q.push(i);
        }
    }
    while(!q.empty()){
        int x=q.front();
        q.pop();
        b[x]-=a[x];
        ans+=b[x]/(b[(x+1)%n]+b[(x-1+n)%n]);
        b[x]%=(b[(x+1)%n]+b[(x-1+n)%n]);
        b[x]+=a[x];
        int y=(x-1+n)%n;
        if(b[y]>=(a[y]+b[(y+1)%n]+b[(y-1+n)%n])){
            q.push(y);
        }
        y=(x+1)%n;
        if(b[y]>=(a[y]+b[(y+1)%n]+b[(y-1+n)%n])){
            q.push(y);
        }
    }
    for(int i = 0;i<n;i++){
        if(a[i]!=b[i]){
            printf("-1\n");
            return 0;
        }
    }
    printf("%lld\n",ans);
}

Measured memory_bytes: 3840
# -*- coding: utf-8 -*-

import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (n, d) = [int(e) for e in lines[0].split(' ')]
    xy = []
    for i in range(1, n+1):
        tmp = lines[i].split(' ')
        xy.append((int(tmp[0]), int(tmp[1])))

    return (n, d, xy)


def solve(n, d, xy):

    result = 0
    for i in range(n):
      x = xy[i][0]
      y = xy[i][1]
      if x * x + y * y <= d * d:
          result = result + 1

    return result    


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()
	

Measured memory_bytes: 95120
#include <iostream>
#include <cstdlib>
#include <bitset>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <math.h>

using namespace std;

#define ll long long int
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define vecpairll vector<pair<long long,long long>>
#define forll(s,a,b) for(ll s = a;s < b;s++)
#define forllde(s,a) for(ll s = a;s >= 0;s--)

#define Pi 3.1415926535897932384626

int main() {
	ll n,ans = 0;
	string s = "Yes";
	bool flag = false;

	cin >> s;

	if (s[0] >= 'A' && s[0] <= 'Z')
		cout << 'A';
	else
		cout << 'a';
}


Measured memory_bytes: 3592
import java.util.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		while(sc.hasNext()){
			StringBuffer s = new StringBuffer(sc.nextLine());
			StringBuffer t = new StringBuffer();
			
			int p = 0;
			for(int i=0;i<27;i++){
				t.delete(0, s.length());
				for(int j=0;j<s.length();j++){
					p = s.codePointAt(j);
					if(p>96 && p<=122){
						p+=i;
						if(p>122)p-=26;
					}
					t.append((char)p);
				}
				if(t.indexOf("the ")!=-1 || t.indexOf("the.")!=-1)break;
				if(t.indexOf("that ")!=-1 || t.indexOf("that.")!=-1)break;
				if(t.indexOf("this ")!=-1 || t.indexOf("this.")!=-1)break;
			}
			System.out.println(t);
		}
	}	
}

Measured memory_bytes: 18032
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <chrono>
#include <random>
#include <unordered_map>
#include <cassert>
#pragma GCC optimize("O3")
#pragma comment(linker, "STACK:36777216")
using namespace std;
using i64 = int64_t;
constexpr i64 MOD = 1e9 + 7;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
using vi = vector<i64>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ii = pair<i64, i64>;

multiset<int> prob;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int d;
        cin >> d;
        prob.insert(d);
    }
    
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        int t;
        cin >> t;
        if (prob.count(t) == 0) {
            cout << "NO" << endl;
            return 0;
        } else {
            prob.erase(prob.find(t));
        }
    }
    cout << "YES" << endl;
}


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

using namespace std;
using ll = long long;

int main() {
  int n, k; cin >> n >> k;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  sort(v.begin(), v.end());
  ll tot = 0;
  for (int i = 0; i < n - k; i++) tot += v[i];
  cout << tot << endl;
  return 0;
}

Measured memory_bytes: 1792
#define ll long long
#define INF 99999999
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#include<bits/stdc++.h>
using namespace std;
int main (){
    string s;
    cin >> s;
    if(s == "ABC")cout << "ARC" << endl;
    else cout <<"ABC" << endl;
    return 0;
}

Measured memory_bytes: 3624
import sys
from collections import deque
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())
def LS(): return list(sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

def Main():
    s,w = IL()
    if s<=w:
        print('unsafe')
    else:
        print('safe')
    return

if __name__=='__main__':
    Main()

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

using namespace std;

#define ll long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-12
#define fi first
#define se second
#define pb push_back
#define MEM(a,b) memset((a),(b),sizeof(a))
#define mod(x) ((x)%MOD)
#define pii pair<int,int>
#define wz cout<<"-----"<<endl;
const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;

const ll maxn = 1e5 + 10, MOD = 1e9 + 7;
const int Move[4][2] = {-1,0,1,0,0,1,0,-1};
const int Move_[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}


void init() {

}

ll t0[111];

ll A[maxn];

string t;
bitset<45> a, x, y;
ll sum;
ll n,k;

int main(){
//    freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//    freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
//    ios_base::sync_with_stdio(0);
    init();
    cin >> n >>k;
    x = k;
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        A[i] = x;
        a = x;
        for (int j = 0; j < 45; j++){
            t0[j] += a[j];
        }
    }
//    for (int i = 0; i < 42; i++) cout << t0[i] <<' ';cout << endl;
    for (int i = 0; i < 45; i++) {
        if (t0[i] >= n - t0[i]) {
            y[i] = 0;
        } else y[i] = 1;
    }
//    cout << y <<endl;
    ll sb = 0;
    for (int j = 0; j < 45; j++) if (y[j]) sb += (1ll << j);
    int l = 0;
    for (int i = 45; i >= 0; i--) if (x[i]) {
        l = i;break;
    }

    ll ss = 0;
    for (int i = l; i >= 0; i--) {
        if (y[i]) {
            if (ss + (1ll << i) <= k) {
                ss += (1ll << i);
            }
        }
    }
    for (int i = 0; i < n; i++) sum += ss ^ A[i];
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_geneous = 0; ngtkana_is_geneous < n; ngtkana_is_geneous++)
#define rep(i, begin, end) for(int i = begin; i < end; i++)

#define LOCAL
using std::to_string;
auto to_string(std::string s) -> std::string {
  return '"' + s + '"';
}
auto to_string(char c) -> std::string {
  return "'" + std::string{c} + "'";
}
auto to_string(const char* s) -> std::string {
  return to_string((std::string) s);
}
auto to_string(bool b) -> std::string {
  return (b ? "true" : "false");
}
template <typename T, typename U>
auto to_string(std::pair<T, U> p) -> std::string {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <size_t N>
auto to_string(std::bitset<N> bs) -> std::string {
  std::string res{};
  for (size_t i = 0; i < N; i++)
    res.insert(res.begin(), bs.test(i) ? '1' : '0');
  return res;
}
template <typename T>
auto to_string(T v) -> std::string {
  bool flg = false;
  std::string res = "{";
  for (auto const&x : v) {
    if (flg) res += ", ";
    else flg = true;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  std::cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T, typename U>
inline auto cmn (T& a, U b) {if (a > b) {a = b; return true;} return false;}
template<typename T, typename U>
inline auto cmx (T& a, U b) {if (a < b) {a = b; return true;} return false;}

int main()
{
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  std::string s; std::cin >> s;
  int n = s.length();
  constexpr int inf = 1 << 30;
  auto dp = std::vector<int>(n + 1, -inf);
  auto ep = std::vector<int>(n + 1, -inf);
  dp.at(0) = 0;
  ep.at(0) = 0;
  rep(i, 1, n + 1)
  {
    dp.at(i) = ep.at(i - 1) + 1;
    if (i >= 2 && s.at(i - 2) != s.at(i - 1))
    {
      cmx(dp.at(i), dp.at(i - 1) + 1);
    }
    if (i >= 2)
    {
      ep.at(i) = dp.at(i - 2) + 1;
      if (i >= 4 && (s.at(i - 4) != s.at(i - 2) || s.at(i - 3) != s.at(i - 1)))
      {
        cmx(ep.at(i), ep.at(i - 2) + 1);
      }
    }
  }
  std::cout << std::max(dp.at(n), ep.at(n)) << std::endl;
  return 0;
}

Measured memory_bytes: 2196
#include <iostream>
#include <string>

using namespace std;

class Node {
public:
	int key;
	Node* prev;
	Node* next;
	Node(int _key, Node* _prev, Node* _next) {
		key = _key;
		prev = _prev;
		next = _next;
	}
};

class List {
public:
	Node* head;
	Node* last;
	List() {
		head = NULL;
		last = NULL;
	}
	void insert(int key) {
		auto node = head;
		head = new Node(key, NULL, node);
		if (node)
			node->prev = head;
		else
			last = head;
	}
	void deleteNode(Node* node) {
		if (! node)
			return;
		
		if (node == head)
			head = node->next;
		else
			node->prev->next = node->next;
		if (node == last)
			last = node->prev;
		else
			node->next->prev = node->prev;
		delete node;
	}
	Node* find(int key) {
		Node* current = head;
		while (current) {
			if (current->key == key) {
				break;
			} else {
				current = current->next;
			}
		}
		return current;
	}
	void deleteFirst() {
		deleteNode(head);
	}
	void deleteLast() {
		deleteNode(last);
	}
};

int main() {
	int n;
	cin >> n;
	
	auto list = List();
	string name;
	int key;
	for (int i=0; i<n; i++) {
		cin >> name;
		if (name == "insert") {
			cin >> key;
			list.insert(key);
		} else if (name == "delete") {
			cin >> key;
			list.deleteNode(list.find(key));
		} else if (name == "deleteFirst") {
			list.deleteFirst();
		} else if (name == "deleteLast") {
			list.deleteLast();
		}
	}
	
	auto current = list.head;
	while (current) {
		if (current != list.head)
			cout << ' ';
		cout << current->key;
		current = current->next;
	}
	cout << endl;
}

Measured memory_bytes: 34392
using System;
class _0024
{
    public static void Main()
    {
        string str;
        while ((str = Console.ReadLine()) != null)
        {
            Console.WriteLine(Math.Ceiling((4.9 * Math.Pow(double.Parse(str) / 9.8, 2.0) + 5.0) / 5.0));
        }
    }
}

Measured memory_bytes: 5716
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#include <fstream>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll mod = 1e9 + 7;

vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, 1, -1};

vector<vector<ll>> g;
vector<ll> dp;
vector<ll> dps;
vector<ll> ret;
vector<bool> used;
vector<ll> fac;
vector<ll> inv;

ll comb(ll n, ll k)
{
    return fac[n] * inv[k] % mod * inv[n - k] % mod;
}

long long ppow(const long long &v, const long long &n, const long long &mod)
{
    long long ret = 1;
    long long num = n;
    long long now = v;
    
    while (num > 0)
    {
        if (num & 1)
        {
            ret = ret * now % mod;
        }
        now = now * now % mod;
        num = num >> 1;
    }
    
    return ret;
}

ll dfs1(ll cur)
{
    if (dps[cur] != -1)
    {
        return dps[cur];
    }
    used[cur] = 1;
    ll ret = 1;

    for (int i = 0; i < g[cur].size(); ++i)
    {
        if (!used[g[cur][i]])
        {
            ret += dfs1(g[cur][i]);
        }
    }
    dps[cur] = ret;
    return ret;
}

ll dfs2(ll cur)
{
    if (dp[cur] != -1)
    {
        return dp[cur];
    }
    used[cur] = 1;
    ll ret = fac[dps[cur] - 1];
    for (int i = 0; i < g[cur].size(); ++i)
    {
        if (!used[g[cur][i]])
        {
            ret *= (dfs2(g[cur][i]));
            ret %= mod;
            ret *= inv[dps[g[cur][i]]];
            ret %= mod;
        }
    }
    dp[cur] = ret;
    return ret;
}

ll dfs3(ll cur, ll par, ll n)
{
    if (ret[cur] != -1)
    {
        return dp[cur];
    }
    used[cur] = 1;
    ret[cur] = dp[cur] * par % mod * comb(n - 1ll, n - dps[cur]) % mod;
    for (int i = 0; i < g[cur].size(); ++i)
    {
        if (!used[g[cur][i]])
        {
            ll npar = ret[cur] * ppow(dp[g[cur][i]], mod - 2, mod) % mod * ppow(comb(n - 1ll, dps[g[cur][i]]), mod - 2, mod) % mod;
            dfs3(g[cur][i], npar, n);
        }
    }
    return ret[cur];
}


int main() {
    
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    
    ll n;
    cin >> n;
    
    g = vector<vector<ll>>(n, vector<ll>());
    for (int i = 0; i < n - 1; ++i)
    {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    
    fac = vector<long long>(n + 1, 1);
    inv = vector<long long>(n + 1, 1);
    used = vector<bool>(n);
    dp = vector<ll>(n, -1);
    dps = vector<ll>(n, -1);
    ret = vector<ll>(n, -1);
    
    for (int i = 1; i <= n; ++i)
    {
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = ppow(fac[i], mod - 2, mod);
    }
    dfs1(0);
    used = vector<bool>(n);
    dfs2(0);
    used = vector<bool>(n);
    dfs3(0, 1, n);
    
    for (int i = 0; i < n; ++i)
    {
        cout << ret[i] << endl;
    }
    
    
    return 0;
}

Measured memory_bytes: 22640
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define REP(i, n) for(int i = 0;i < (n);++i)
#define RREP(i, n) for(int i = (n)-1; i >= 0; --i)

#define endl '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.1415926
#define coutdb cout<<fixed<<setprecision(15)

const int dx[]={1,0,-1,0,1,1,-1,-1}, dy[]={0,-1,0,1,1,-1,1,-1};
const long long MOD = 1e9+7;

// typedef int_fast64_t ll;
#define int int_fast64_t

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

typedef pair<int, int> INTP;
typedef vector<int> INTV;
struct edge2 {int to, cost;};
struct edge3 {int from, to, cost;};

signed main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N; cin >> N;
  for(int i = 1; i <= 9; i++){
    for(int j = 1; j <= 9; j++){
      if(N == i*j){
        cout << "Yes" << endl;
        return 0;
      }
    }
  }
  cout << "No" << endl;
}

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

int main(){
    int N;
    cin >> N;
    vector<string> name(N);
    for (int i=0;i<N;i++){
        cin >> name.at(i);
    }
    int c0=0,c1=0,c2=0,c3=0;
    for (int i=0;i<N;i++){
        if (name.at(i)=="AC"){
            c0++;
        }
        if (name.at(i)=="WA"){
            c1++;
        }
        if (name.at(i)=="TLE"){
            c2++;
        }
        if (name.at(i)=="RE"){
            c3++;
        }
    }
    cout << "AC x " << c0 << endl;
    cout << "WA x " << c1 << endl;
    cout << "TLE x " << c2 << endl;
    cout << "RE x " << c3 << endl;
}

Measured memory_bytes: 6328
#include <iostream>
 using namespace std;
 long long N, A[87], CurrentMoney = 1000;
 int main() {
 cin >> N;
 for (int i = 1; i <= N; i++) cin >> A[i];
 for (int i = 1; i <= N - 1; i++) {
 long long Stocks = 0;
 if (A[i] < A[i + 1]) Stocks = CurrentMoney / A[i];
 CurrentMoney += (A[i + 1] - A[i]) * Stocks;
 }
 cout << CurrentMoney << endl;
 }


Measured memory_bytes: 3548
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] t = new int[N+1];
		int[] x = new int[N+1];
		int[] y = new int[N+1];
		boolean ok = true;
		for(int i = 1; i <= N; i++) {
			t[i] = sc.nextInt();
			x[i] = sc.nextInt();
			y[i] = sc.nextInt();
			if(t[i]-t[i-1] < Math.abs(x[i]-x[i-1]) + Math.abs(y[i]-y[i-1]) || (t[i] - x[i] - y[i]) % 2 != 0) {
				ok = false;
				break;
			}
		}
		System.out.println(ok ? "Yes" : "No");
	}

}

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

using namespace std;

int n, cnt[2][100005],smx[100005];
pair <int, int> mx[2];
vector <int> v[2];

int main(){
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		cnt[i % 2][x]++;
	}
	
	for (int i = 1; i <= 100000; i++) {
		for (int j = 0; j < 2; j++) {
			int tmp = cnt[j][i];
			
			if (tmp > mx[j].first) {
				swap(tmp, mx[j].first);
				mx[j].second = i;
			}
			
			smx[j] = max(smx[j], tmp);
		}
	}
	
	int cool;
	
	if (mx[0].second != mx[1].second)
		cool = mx[0].first + mx[1].first;
	else
		cool = max(mx[0].first + smx[1], mx[1].first + smx[0]);
	
	printf("%d\n", n - cool);
}


Measured memory_bytes: 1024
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

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

int main()
{
	int n;
	cin>>n;
	vi hist(6);
	rep(i,n){
		int a,b;
		scanf("%d.%d",&a,&b);
		int h=a*10+b;
		if(h<1650)
			hist[0]++;
		else if(h<1700)
			hist[1]++;
		else if(h<1750)
			hist[2]++;
		else if(h<1800)
			hist[3]++;
		else if(h<1850)
			hist[4]++;
		else
			hist[5]++;
	}
	rep(i,6){
		printf("%d:",i+1);
		rep(j,hist[i])
			putchar('*');
		puts("");
	}
	
	return 0;
}

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

using namespace std;

int main() {
  int n;
  cin>>n;
  pair<int,int> temp;
  int x;
  vector<pair<int,int> > v;
 for(int i=1;i<=n;i++)
 {
     
     cin>>x;
     temp.first=x;
     temp.second=i;
     v.push_back(temp);
 }
sort(v.begin(),v.end());
 
for (const auto &p : v) {
    cout << p.second<<" ";
}
 
 
}

Measured memory_bytes: 1780
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public final class Main {
	public static void main(String[] args) throws java.lang.Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int k = Integer.parseInt(br.readLine());
		int x = Integer.parseInt(br.readLine());
		int y = Integer.parseInt(br.readLine());
		Solver s = new Solver();
		System.out.println(s.solve(n, k, x, y));
	}

}

class Solver {

	int solve(int n, int k, int x, int y) {

		if (n <= k) {
			return n * x;
		} else {
			return k * x + (n - k) * y;
		}

	}

}


Measured memory_bytes: 20948
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>

using namespace std;

int main(){
    int N,M;
    cin >> N >> M;
    vector<int> X(M,0);
    for(int i=0; i<M; i++){
        cin >> X[i];
    }
    sort(X.begin(), X.end());

    priority_queue<int, vector<int>, greater<int> > que;
    for(int i=0; i<M-1; i++){
        que.push(X[i+1]-X[i]);
    }

    int ans=0;
    for(int i=0; i<M-N; i++){
        ans+=que.top();
        que.pop();
    }
    cout << ans << endl;
}

Measured memory_bytes: 1276
#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>
#include <utility>


using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// const ll INF = 1LL <<60;
const int INF = 10000;

//最大公約数
ll gcd(ll x, ll y) {
  ll tmp = 0;
  if (x < y){
    tmp=x;
    x=y;
    y=tmp;
  }
  while (y > 0) {
    ll r = x % y;
    x = y;
    y = r;
  }
  return x;
}

//最大公倍数
ll lcm(ll x,ll y){
  return x/gcd(x,y)*y;
}

const int MAX = 510000;
const int MOD = 1000000007;

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

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

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


//階乗
ll kaijo(ll k){
    ll sum = 1;
    for (ll i = 1; i <= k; ++i)
    {
        sum *= i;
        sum%=1000000000+7;
    }
    return sum;
}

//for(int i = ; i < ; i++){}

ll lmax(ll s,ll t){
  if(s>t){
    return s;
  }
  else{
    return t;
  }
}

ll lmin(ll s,ll t){
  if(s<t){
    return s;
  }
  else{
    return t;
  }
}
// ここから開始
int main(){
  int n;
  cin>>n;
  for(int i = 1; i <= 9; i++){
    for(int j = 1; j <= 9; j++){
      if(i*j==n){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
  }
  cout<<"No"<<endl;
  return 0;
}



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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
//#define TEST

int main(int argc, char** argv) {
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) {
        freopen("d:\\code\\cpp\\stdin", "r", stdin);
        //freopen("d:\\code\\cpp\\stdout", "w", stdout);
    }

    int A, P;
    cin >> A >> P;
    int sol = (A*3 + P)/2;
    cout << sol << '\n';


    if (argc == 2 && atoi(argv[1]) == 123456789)
        cerr << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve(args);
    }

    public void solve(String[] args) {
        MyScanner scanner = new MyScanner();
        int n = scanner.nextInt();
        int d = scanner.nextInt();
        int ans;
        int tmp;
        tmp = d*2 +1;
        ans=n/tmp;
        if(n%tmp!=0) {
        	ans++;
        }
        System.out.println(ans);



    }

    private static long gcd(long m, long n) {
        if(m < n) return gcd(n, m);
        if(n == 0) return m;
        return gcd(n, m % n);
    }

    private static long lcm(long m, long n) {
        return m * n / gcd(m, n);
    }

    private class MyScanner {
        String[] s;
        int i;
        BufferedReader br;
        String reg = " ";
        MyScanner () {
            s = new String[0];
            i = 0;
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        public String next() {
            try {
                if (i < s.length) return s[i++];
                String line = br.readLine();
                while (line.equals("")) {line = br.readLine();}
                s = line.split(reg, 0);
                i = 0;return s[i++];
            } catch (Exception e) {e.printStackTrace();}
            return null;
        }
        public int nextInt() {
            try {return Integer.parseInt(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
        public double nextDouble() {
            try {return Double.parseDouble(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
        public long nextLong()  {
            try {return Long.parseLong(next());} catch (Exception e) {e.printStackTrace();}
            return -1;
        }
    }
}

Measured memory_bytes: 21204
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int H, W, n, h[40], w[40], sz[40];
int m, no[10];
char in[20][21], pc[40][20][21];
bool v[20][20], u[20][20];

bool cmp(int a, int b){
	return sz[a] > sz[b];
}
bool rec(int c){
	if(c == m) return 1;
	memset(u, 0, sizeof(u));
	rep(i, H) rep(j, W) if(in[i][j] == '.' && !u[i][j]){
		for(int k = c; k < m; k++) rep(t, 4){
			int p = no[k] + t * n;
			for(int y = max(0, i - h[p] + 1); y <= i && y + h[p] <= H; y++)
			for(int x = max(0, j - w[p] + 1); x <= j && x + w[p] <= W; x++){
				rep(a, h[p]) rep(b, w[p]){
					if(pc[p][a][b] == '#' && in[y + a][x + b] == '#') goto FAIL;
				}
				rep(a, h[p]) rep(b, w[p]){
					if(pc[p][a][b] == '#') u[y + a][x + b] = 1;
				}
				FAIL:;
			}
		}
		if(!u[i][j]) return 0;
	}
	rep(t, 4){
		int p = no[c] + t * n;
		for(int y = 0; y + h[p] <= H; y++)
		for(int x = 0; x + w[p] <= W; x++){
			bool ok = 1;
			rep(a, h[p]) rep(b, w[p]) if(in[y + a][x + b] == '#' && pc[p][a][b] == '#') ok = 0;
			if(ok){
				rep(a, h[p]) rep(b, w[p]) if(pc[p][a][b] == '#') in[y + a][x + b] = '#';
				bool res = rec(c + 1);
				rep(a, h[p]) rep(b, w[p]) if(pc[p][a][b] == '#') in[y + a][x + b] = '.';
				if(res) return 1;
			}
		}
	}
	return 0;
}

int main()
{while(cin >> H >> W, H){int sp = 0;rep(i, H){cin >> in[i];rep(j, W) if(in[i][j] == '.') sp++;}cin >> n;
		rep(i, n){cin >> h[i] >> w[i];sz[i] = 0;rep(j, h[i]){	cin >> pc[i][j];
		rep(k, w[i]) if(pc[i][j][k] == '#') sz[i]++;}
			int s = i;rep(j, 3){
				int t = s + n;

				h[t] = w[s];
				w[t] = h[s];
				sz[t] = sz[s];

				rep(y, h[s]) rep(x, w[s])
					pc[t][x][h[s] - y - 1] = pc[s][y][x];
				s = t;
			}
		}
		int q;
		cin >> q;
		while(q--){
			cin >> m;
			int sum = 0;
			rep(i, m) cin >> no[i], sum += sz[--no[i]];
			sort(no, no + m, cmp);

			memset(v, 0, sizeof(v));
			cout << (sum == sp && rec(0) ? "YES" : "NO") << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 3152
#include<bits/stdc++.h>
#define int long long
#define Inf 1000000000LL
using namespace std;
signed main(){
  int n,sum=0,a[1000000],z=0;
  string s[10000],b;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>s[i]>>a[i];
  }
  cin>>b;
  for(int i=0;i<n;i++){
    if(b==s[i]){
      z++;
    }else if(z>0){
      sum+= a[i];
   }
  }
  cout<<sum<<"\n";
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
 
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
 
int n, k;
int a[N];
int fact[N], invfact[N];
 
int pow(int a, int b, int m)
{
	int ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}
 
int modinv(int k)
{
	return pow(k, MOD-2, MOD);
}
 
void precompute()
{
	fact[0]=fact[1]=1;
	for(int i=2;i<N;i++)
	{
		fact[i]=fact[i-1]*i;
		fact[i]%=MOD;
	}
	invfact[N-1]=modinv(fact[N-1]);
	for(int i=N-2;i>=0;i--)
	{
		invfact[i]=invfact[i+1]*(i+1);
		invfact[i]%=MOD;
	}
}
 
int nCr(int x, int y)
{
	if(y>x)
		return 0;
	int num=fact[x];
	num*=invfact[y];
	num%=MOD;
	num*=invfact[x-y];
	num%=MOD;
	return num;
}
 
int32_t main()
{
	IOS;
	precompute();
	cin >> n >> k;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	sort(a + 1, a + n + 1);
	int ans = 0;
	for(int i = k; i <= n; i++)
	{
		ans += a[i] * nCr(i - 1, k - 1);
		ans %= MOD;
		ans += MOD;
		ans %= MOD;
	}
	for(int i = 1; i + k - 1 <= n; i++)
	{
		ans -= (a[i] * nCr(n - i, k - 1)) % MOD;
		ans += MOD;
		ans %= MOD;
		ans += MOD;
		ans %= MOD;
	}
	cout << ans;
	return 0;
}

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

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
    ll n, m, i, j, k,l,now=-1;
    cin >> n >> l;
    vector<ll> a(n);
    for(i=0;i<n;i++){
        cin >> a[i];
        if(i>0 && a[i]+a[i-1]>=l){
            now = i;
        }
    }
    vector<ll> ret;
    ret.push_back(now);
    ll piv=now-1;
    while(piv>0){
        ret.push_back(piv--);
    }
    piv = now+1;
    while(piv<n){
        ret.push_back(piv++);
    }
    reverse(ret.begin(),ret.end());
    if(now==-1){
        cout << "Impossible" << endl;
    }else{
        cout << "Possible" << endl;
        for(i=0;i<n-1;i++){
            cout << ret[i] << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 2548
#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define hell 998244353
#define hell1 1000000006
#define pb push_back
#define x first
#define y second
#define MAXL 100005
#define pll pair<ll,ll>
#define pii pair<int,int>
#define L LLONG_MAX
#define all(v) (v).begin(),(v).end()
#define tcFile freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
using namespace std;
//vector<int>prime(15000005,0);
ll mM(ll a,ll b,ll p=hell){ll res = 0;if(a>=p)a %= p;while(b){if (b & 1ll)res = (res + a);if(res>=p) res%= p;a = (2 * a); if(a>=p)a%=p;b >>= 1ll;}return res;}
ll fpow(ll x,ll y,ll p=hell){if(x>=p)x=x%p;ll res=1;while(y){if(y&1ll)res*=x;if(res<0)res+=p;if(res>=p)res%=p;y=y>>1ll;x*=x;if(x<0)x+=p;if(x>=p)x%=p;}return res;}
ll inv(ll n,ll p=hell){return fpow(n,p-2,p);}
ll modmul(ll x,ll y,ll p = hell){if(x<0)x+=p;if(x>=p)x%=p;if(y<0)y+=p;if(y>=p)y%=p;return (x*y)%p;}
ll modadd(ll x,ll y,ll p = hell){if(x<0)x+=p;if(x>=p)x%=p;if(y<0)y+=p;if(y>=p)y%=p;ll temp=x+y;if(temp>=p)temp%=p;return temp;}
/*void sieve(){
    prime[0] = -1;
    prime[1] = 0;
    for(int i=2;i<=15000000;i++)
        if(!prime[i])
            for(int j=i;j<=15000000;j+=i)
            	if(!prime[j])
            		prime[j] = i;
}*/
int main(){
	ios
	int t = 1,tc = 0;
	//cin >> t;
	while(t--){
		ll n, w;
		cin >> n >> w;
		vector<pll>a(n+1);
		for (int i = 1; i <= n; i++)
			cin >> a[i].x >> a[i].y;
		vector<vector<ll>>dp(n+2, vector<ll>(w+1, 0));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= w; j++) {
				dp[i][j] = dp[i-1][j];
				if (a[i].x <= j)
					dp[i][j] = max(dp[i][j], dp[i-1][j-a[i].x] + a[i].y);
			}
		ll ans = 0;
		for (int i = 1; i <= w; i++)
			ans = max(ans, dp[n][i]);
		cout << ans << "\n";
	}
}


Measured memory_bytes: 83944
#include<iostream>
#include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;


int N;
int dat[800010], n_dat, idx[200001];


int query(int a, int b, int k, int l, int r){
    if(r <= a || b <= l) return INT_MAX;

    if(a <= l && r <= b) return dat[k];

    int vl = query(a, b, k*2+1, l, (l+r)/2);
    int vr = query(a, b, k*2+2, (l+r)/2, r);
    return min(vl, vr);
}

long long solve(int l, int r){
    //printf("l = %d, r = %d\n", l, r);
    if(l >= r){
        return 0;
    }
    int target = query(l, r, 0, 0, n_dat);
    int target_idx = idx[target];
    long long ret = 0;
    ret += (long long)target * (target_idx - l + 1) * (r - target_idx);
    ret += solve(l, target_idx);
    ret += solve(target_idx + 1, r);
    return ret;
}

int main(){
    cin >> N;
    for(int i = 0; i < 800010; i++){
        dat[i] = INT_MAX;
    }
    n_dat = 1;
    while(N > n_dat){
        n_dat *= 2;
    }
    //printf("n_dat = %d\n", n_dat);

    for(int i = 0; i < N; i++){
        cin >> dat[n_dat - 1 + i];
        idx[dat[n_dat-1+i]] = i;
        int k = n_dat - 1 + i;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }
    //printf("initialized\n");
    long long ans = 0;
    ans = solve(0, N);
    cout << ans << endl;

}

Measured memory_bytes: 13568