akhauriyash/Code-Regression huggingface dataset:
use proconio::input;
use proconio::marker::{Chars, Usize1};
use std::iter::Iterator;
use std::collections::*;
use std::cmp::*;
use itertools::Itertools;

#[allow(unused_macros)]
macro_rules! max {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      max($x,max!($($xs)+))
    };
  }
  
  #[allow(unused_macros)]
  macro_rules! min {
    ($x:expr) => {
      $x
    };
    ($x:expr, $($xs:tt)+) => {
      min($x,min!($($xs)+))
    };
  }

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn main() {
    input! {
    n: i64,
    k: i64
    }

    let mut ans = 0_f64;
    for i in 1..=n {

      let mut m = 0;
      let mut target = i;
      let mut one = 0_f64;
      while {
        target < k
      }{
        target = target * 2;
        m += 1;
      }
      one += 1 as f64/ n as f64;

      for _ in 0..m {
        one *= 0.5;
      }


      ans += one; 
    }

     println!("{}", ans);
}


Measured memory_bytes: 2112
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(begin, i, end) for (int i = begin; i < (int)(end); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1000000007;

int main()
{
    int N, M;
    cin >> N >> M;
    if (N == 1)
    {
        cout << M << endl;
        return 0;
    }
    int ans = 0;
    for (int i = 1; i * i <= M; i++)
    {
        if (M % i == 0) // iがMの約数
        {
            if (M / i >= N) // iがM/N以下
            {
                ans = max(ans, i);
            }
            if (M / (M / i) >= N) // iがMの約数ならM/iもMの約数
            {
                ans = max(ans, M / i);
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,d,e,Min;
	cin>>a>>b;
	c=a+b;
	d=a-b;
	e=a*b;
	 Min=c;
        if(Min<d)
	 Min=d;
	if(Min<e)
	 Min=e;
	 {
	 	cout<<Min;
	 	return 0;
	 }
}

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

int main() {
    int x, a, b;
    cin >> x >> a >> b;

    if (abs(x - a) < abs(x - b))
        cout << "A";
    else
        cout << "B";
    cout << endl;

    return 0;
}

Measured memory_bytes: 3512
#include <cstdio>
#include <iostream> 
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
 
using namespace std;
using ll = long long;
using ull = unsigned long long;
 
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(int i = 0; i < n; i++)
#define RREP(i, n) for(int i = n - 1; i >= 0; i--)
#define REP2(i, n, k) for(int i = 0; i < n; i += k)


int main()
{
    int n;
    string s1, s2;

    cin >> n >> s1 >> s2;

    int p = 0;
    bool h;
    ll ans;
    if(s1[p] == s2[p]){
        ans = 3;
        h = true;
        p++;
    }
    else{
        ans = 6;
        h = false;
        p += 2;
    }

    while(p < n){
        if(s1[p] == s2[p]){
            if(h){
                ans = ans * 2 % MOD;
            }
            h = true;
            p++;
        }
        else{
            if(h){
                ans = ans * 2 % MOD;
            }
            else{
                ans = ans * 3 % MOD;
            }
            h = false;
            p += 2;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, k, a, b;
vector<int> vec[100009];
long long ans = 1;

void fun(int ud, int ud2, int from, int cur)
{
	if (k - ud <= 0) {
		ans = 0;
		return;
	}
	ans = ans * (k - ud);
	ans = ans % MOD;
	int cnt = 1;
	for (int i = 0; i < (int)vec[cur].size(); i++) {
		if (vec[cur][i] != from) {
			fun(ud + cnt - ud2, ud - ud2 + cnt - 1, cur, vec[cur][i]);
			cnt++;
		}
	}
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 0; i < n - 1; i++) {
		cin >> a >> b;
		a--; b--;
		vec[a].pb(b);
		vec[b].pb(a);
	}
	fun(0, 0, -1, 0);
	cout << ans << "\n";
	//system("pause");
	return 0;
}

Measured memory_bytes: 11648
def resolve():
    '''
    code here
    '''
    import collections
    N = int(input())
    As = [int(item) for item in input().split()]

    cnt = collections.Counter(As)
    cnt = sorted(cnt.items(), reverse=True)
    # print(sorted(cnt.items(), reverse=True))
    edges = []
    lim = 0
    res = 0
    if len(cnt) >=2:
        for i, v in cnt:
            if v >= 4:
                edges.append(i)
                edges.append(i)
                lim += 2
            elif v>=2:
                edges.append(i)
                lim += 1

            if lim >= 2:
                res = edges[0]*edges[1]
                break
    print(res)

if __name__ == "__main__":
    resolve()


Measured memory_bytes: 25768
#include<bits/stdc++.h>
#define ll  long long
#define ff first
#define ss second
#define pb push_back
#define pp pair<ll,ll>
#define maxn 1000005
#define db double
#define mod 1000000007
using namespace std;
void solve()
{	
	ll n;
	cin >> n;
	map<string,ll> mp;
	for(ll i=0;i<n;i++)
	{
		string x;
		cin >> x;
		mp[x]++;
	}
	cout << mp.size() ;
}	
int main()
{
	ll t=1,i=1;
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);	
    std::cout.precision(10);
    std::cout.setf( std::ios::fixed, std:: ios::floatfield ); 
	//cin >> t;
	//cin >> n;
	while(i<=t)
	{
		solve();
		i++;
	}
	return 0;
}




Measured memory_bytes: 19288
import sys
input = sys.stdin.readline

N=int(input())
a = []
for _ in range(N):
    a.append(int(input()))
found = [0]*N

steps = 0
i = 0
found[i] = 1
while True:
    j = a[i] - 1
    if found[j] == 1:
        print(-1)
        exit()
    steps += 1
    i = j
    found[i] = 1
    if i == 1:
        print(steps)
        exit()


Measured memory_bytes: 77708
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

ll X[4] = {1,0,-1,0};
ll Y[4] = {0,1,0,-1};

int main(){
    ll H,W;
    cin >> H >> W;
    vector<string> A(H);
    queue<Pll> que;
    vector<vector<ll>> memo(H, vector<ll>(W));
    vector<vector<ll>> c(H, vector<ll>(W,1e18));
    rep(i,H){
        cin >> A[i];
    }
    rep(i,H){
        rep(j,W){
            if(A[i][j] == '#'){
                que.push(Pll(i,j));
                c[i][j] = 0;
            }
        }
    }
    ll ans = 0;
    while(!que.empty()){
        ll h = que.front().first;
        ll w = que.front().second;
        que.pop();
        if(!(0 <= h && h < H)) continue;
        if(!(0 <= w && w < W)) continue;        
        if(memo[h][w] != 0) continue;
        memo[h][w] = 1;
        rep(i,4){
            if(!(0 <= h+Y[i] && h+Y[i] < H)) continue;
            if(!(0 <= w+X[i] && w+X[i] < W)) continue;    
            que.push(Pll(h+Y[i],w+X[i]));
            c[h+Y[i]][w+X[i]] = min(c[h+Y[i]][w+X[i]], c[h][w] + 1);
        }
    }
    rep(i,H){
        rep(j,W){
            cmax(ans,c[i][j]);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 86196
#include <bits/stdc++.h>
 
#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)
 
struct S002 {
  int n;
  S002& operator > (long& x) {
    n = x;
    return *this;
  }
  S002& operator > (long&& x) {
    n = x;
    return *this;
  }
  S002& operator >= (long& x) {
    if( scanf("%ld", &x) <= 0 ) exit(0);
    return *this;
  }
  S002& operator >= (double& x) {
    if( scanf("%lf", &x) <= 0 ) exit(0);
    return *this;
  }
  S002& operator >= (std::string& s) {
    if( not (std::cin >> s) ) exit(0);
    return *this;
  }
  template<typename a>
  S002& operator >= (std::vector<a>& v) {
    v.resize(n);
    for(long i = 0; i < n; ++i) {
      *this >= v[i];
    }
    return *this;
  }
  template<typename a, std::size_t s>
  S002& operator >= (std::array<a, s>& x) {
    for(long i = 0; i < (long)s; ++i) {
      *this >= x[i];
    }
    return *this;
  }
};

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


struct Solver {
  long h, w, k;
  std::vector<std::string> xss;
  Solver() {
    S002 reader;
    reader >= h >= w >= k >h>= xss;
  }
  long dist[1024][1024];
  long sx, sy;
  long dfs(long x_, long y_, long d) {
    const long inf = 1000000;
    std::queue<std::pair<long, long>> q;
    q.push(std::make_pair(x_, y_));
    dist[y_][x_] = 0;
    long res = inf;
    while( not q.empty() ) {
      std::pair<long, long> temp = q.front(); q.pop();
      long x = temp.first, y = temp.second;
      long t = std::min(std::min(std::min((x+k-1)/k, ((w-1)-x+k-1)/k), (y+k-1)/k), ((h-1)-y+k-1)/k);
      res = std::min(res, t);
      for(long i = 0; i < 4; ++i) {
        long nx = x + dx[i];
        long ny = y + dy[i];
        if( not ( 0 <= nx and nx < w ) ) continue;
        if( not ( 0 <= ny and ny < h ) ) continue;
        if( dist[ny][nx] <= dist[y][x] + 1 ) continue;
        if( xss[ny][nx] == '#' ) continue;
        dist[ny][nx] = dist[y][x] + 1;
        if( dist[ny][nx] > k ) continue;
        q.push(std::make_pair(nx, ny));
      }
    }
    return res;
  }
  void init() {
    for(long i = 0; i < 1024; ++i) {
      for(long j = 0; j < 1024; ++j) {
        dist[i][j] = 1000000;
      }
    }
    for(long i = 0; i < h; ++i) {
      for(long j = 0; j < w; ++j) {
        if( xss[i][j] == 'S' ) {
          sy = i;
          sx = j;
        }
      }
    }
  }
  void solve() {
    init();
    long res = dfs(sx, sy, 0);
    printf("%ld\n", res + 1);
  }
  ~Solver() { fprintf(stderr, "--end--\n"); }
};
 
int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
  return 0;
}



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

using namespace std;
using ll=long long;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, a, n) for(int i = (n); i < (a); i++)

int main(){
  cin.tie(nullptr);ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  double t,x;cin>>t>>x;
  cout<<t/x<<endl;
}


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

int main(){
    int n, k, tall, max=0;
    scanf("%d %d", &n, &k);
    
    for (int i=0; i<n; ++i) {
        scanf("%d", &tall);
        if (tall >= k) {
            ++max;
        }
    }
    
    printf("%d", max);
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define rep2(i,n) for(int i = 1; i <= (n); ++i)
#define rrep2(i,n) for(int i = (n); i > 0; --i)
#define srep(i,s,n) for(int i = s; i < (n); ++i)
#define rsrep(i,s,n) for(int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define aall(a) (a), (a)+countof(a)//for array sorting
#define raall(a) (a), (a)+countof(a), greater<>()
#define show(x) cout<<#x<<" = "<<(x)<<endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f) { if (f) puts("YES"); else puts("NO"); }
#define yns(f) { if (f) puts("Yes"); else puts("No"); }
#define show_ary(...) { cout<<#__VA_ARGS__<<" = "; for (const auto& x : (__VA_ARGS__)) { cout<<x<<" "; } cout<<endl; }
#define show_pair(...) cout<<#__VA_ARGS__<<" = "<<endl; for (const auto& x : (__VA_ARGS__)) { cout<<"  "<<x.fi<<" : "<<x.se<<endl; }
#define out_ary(...) { int n = (__VA_ARGS__).size(); rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n-1 ? " " : "\n"); }
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9+7;


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

    string ans = "";
    for (char c : s) {
        if (c == '0') ans += '0';
        if (c == '1') ans += '1';
        if (c == 'B' && ans != "") {
            ans = ans.substr(0, ans.length()-1);
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001; //check the limits, dummy
 
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);    
	
	int N; cin >> N; ll X; cin >> X;
	
	ll data[N]; F0R(i, N) cin >> data[i];
	ll ans = 0;
	F0R(i, N-1) {
		ll req=data[i] + data[i+1] - X;
		req = max(req, 0ll);
		ll first = min(req, data[i+1]);
		ans += first;
		req -= first;
		data[i+1] -= first;
		
		first = min(req, data[i]);
		ans += first; req -= first; data[i] -= first;
	}


	cout << ans << endl;
	
	return 0;
}
 
// read the question correctly (ll vs int)
// template by bqi343


Measured memory_bytes: 1024
# coding: utf-8
ns = [int(input()) for _ in range(5)]
ans = 0
min = 10
for n in ns:
    a = n % 10
    b = n // 10
    if a == 0:
        ans += b * 10
    else:
        if a < min:
            min = a
        ans += (b + 1) * 10
print(ans - (10 - min))

Measured memory_bytes: 3060
from sys import stdin
input = stdin.readline
class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
 
n, q = map(int, input().split())
tmp = list(map(int, input().split()))
color = [-1]
for v in tmp:
    color.append(v)
qry = [None]*q
ans = [0]*q
p = 0
for _ in ans:
    l, r = map(int, input().split())
    qry[p] = [l-1, r, p]
    p += 1
qry.sort(key=lambda x: x[1])
dic = {}
last_update_idx = 1
bit = Bit(n)
for l, r, i in qry:
    if last_update_idx < r+1:
        st = set()
        for j in range(last_update_idx, r+1):
            if not color[j] in st and color[j] in dic:
                bit.add(dic[color[j]], -1)
            dic[color[j]] = j
            st.add(color[j])
        for v in st:
            bit.add(dic[v], 1)
        last_update_idx = r+1
    ans[i] = bit.sum(r) - bit.sum(l)
for v in ans:
    print(v)

Measured memory_bytes: 225724
#こだわりイロハちゃん
n,k=map(int,input().split())
lists=list(map(int,input().split()))
uselist=[]
for i in range(k):
    uselist.append(str(lists[i]))
flag=True
flag2=True
ans=str(n)
while flag2:
    for i in range(len(ans)):
        if ans[i] in uselist:

            flag=False
            break
        else:

            pass
    if flag:
        flag2=False
    else:
        flag=True
        ans=int(ans)
        ans+=1
        ans=str(ans)
print(ans)

Measured memory_bytes: 3064
import numpy as np
from collections import Counter

def f(n):
    temp = n
    c = Counter()
    for i in range(2, n):
        if temp==1:
            break
        while temp%i==0:
            temp = temp // i
            c.update([i])
    else:
        c.update([temp])
    return c


N = int(input())

c = Counter()
for i in range(2, N+1):
    c.update(f(i))

ans = 1
for _, x in c.items():
    ans = ans*(x+1) % 1000000007
print(ans)

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

int main() {
    int n; scanf("%d", &n);
    
    vector<tuple<ll, ll, char, ll, string>> items;
    
    for (int i = 0; i < n; ++i) {
        ll value, weight, date;
        char type; 
        string name;
        // %cは改行文字が入力バッファに残るため、空白を入れて回避
        scanf("%lld%lld %c%lld%s", &value, &weight, &type, &date, name.c_str());
        items.push_back(make_tuple(value, weight, type, date, name.c_str()));
    }
    sort(items.begin(), items.end());
    
    for (int i = 0; i < n; ++i) {
        printf("%lld %lld %c %lld %s\n", get<0>(items[i]), get<1>(items[i]), get<2>(items[i]), get<3>(items[i]), get<4>(items[i]).c_str());
    }
    return 0;
}



Measured memory_bytes: 11916
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
typedef long long ll;
int main(){
	bool f;
	int b1,b2,b3;
	cin>>b1>>b2>>b3;
	if(b1==1&&b2==0&&b3==0)f=false;
	else if(b1==0&&b2==1&&b3==0)f=false;
	else if(b1==1&&b2==1&&b3==0)f=true;
	else if(b1==0&&b2==0&&b3==1)f=true;
	else f=false;
	if(f)cout<<"Open"<<endl;
	else cout<<"Close"<<endl;
} 

Measured memory_bytes: 1156
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#include <map>
#include <set>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)

i64 NUM = 998244353;

i64 modpow(i64 a, i64 b) {
  i64 ans = 1;
  while(b != 0) {
    if(b % 2 == 1) {
      ans *= a;
      ans %= NUM;
    }
    a = a * a;
    a %= NUM;
    b /= 2;
  }
  return ans;
}

i64 modinv(i64 a) {
  return modpow(a, NUM-2);
}

int main(int argc, char *argv[]) {
  i64 N, M, K, i;
  cin >> N >> M >> K;

  vi factorial(N), inv_factorial(N);
  factorial[0] = factorial[1] = 1;
  inv_factorial[0] = inv_factorial[1] = 1;

  REP(2, N, i) {
    factorial[i] = (factorial[i-1] * i) % NUM;
    inv_factorial[i] = modinv(factorial[i]);
  }

  i64 ans = 0, k;
  REP(0, K+1, k) {
    i64 da = factorial[N-1];
    
    da *= inv_factorial[k];
    da %= NUM;
    
    da *= inv_factorial[N-1-k];
    da %= NUM;
    
    da *= M;
    da %= NUM;
    
    da *= modpow(M-1, N-k-1);
    da %= NUM;

    ans += da;
    ans %= NUM;
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

using ll = long long;

const int Inf = 1e9;
const double EPS = 1e-9;

vector<vector<pair<int, int> > > m;
vector<int> res;

void dfs(int s = 0, int color = 0) {
  res[s] = color;
  for (auto e: m[s]) {
	int next = e.first, d = e.second;
	if (res[next] != -1) continue;
	if (d % 2 == 0) {
	  dfs(next, color);
	} else {
	  dfs(next, !color);
	}
  }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
	int n;
	cin >> n;
	m.resize(n);
	res.resize(n, -1);
	rep (i, n - 1) {
	  int u, v, w;
	  cin >> u >> v >> w;
	  u--, v--;
	  m[u].emplace_back(v, w);
	  m[v].emplace_back(u, w);
	}
	dfs();
	rep (i, n) cout << res[i] << endl;
	
    return 0;
}



Measured memory_bytes: 10212
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int h = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        int c = sc.nextInt();
        sc.close();

        if(w >= x+c && x-c >= 0 && h >= y+c && y-c >= 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}



Measured memory_bytes: 26240
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
#define prev prev228
#define all(x) (x).begin(), (x).end()

int n, x, y, z;
long long ans, ans2 = -1e18;

struct Tr{
    long long a, b, c;
};

bool comp(Tr A, Tr B)
{
    return A.a - A.b > B.a - B.b;
}
vector<Tr> v;

multiset<long long> ss;
long long pr[500000], sf[500000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> x >> y >> z;
    n = x + y + z;
    v.resize(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].a >> v[i].b >> v[i].c;
        v[i].a -= v[i].c;
        v[i].b -= v[i].c;
        ans += v[i].c;
        v[i].c = 0;
    }
    sort(all(v), comp);
    for (int i = 0; i < x; i++)
    {
        ss.insert(v[i].a);
        pr[i] = v[i].a + (i > 0 ? pr[i - 1] : 0);
    }
    for (int i = x; i < n; i++)
    {
        auto it = ss.begin();
        long long z = *it;
        if (v[i].a < z)
        {
            pr[i] = pr[i - 1];
        }
        else
        {
            pr[i] = pr[i - 1] + v[i].a - z;
            ss.erase(it);
            ss.insert(v[i].a);
        }
    }
    ss.clear();
    for (int i = n - 1; i > n - 1 - y; i--)
    {
        ss.insert(v[i].b);
        sf[i] = v[i].b + sf[i + 1];
    }
    for (int i = n - 1 - y; i >= 0; i--)
    {
        auto it = ss.begin();
        long long z = *it;
        if (v[i].b < z)
        {
            sf[i] = sf[i + 1];
        }
        else
        {
            sf[i] = sf[i + 1] + v[i].b - z;
            ss.erase(it);
            ss.insert(v[i].b);
        }
    }
    for (int i = x - 1; i + 1 <= n - y; i++) ans2 = max(ans2, pr[i] + sf[i + 1]);
    cout << ans + ans2 << endl;
}


Measured memory_bytes: 11904
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <random>
#include <vector>
#include <string>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <complex>
#include <ctime>
#include <numeric>
using namespace std;

#define pb push_back
#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define reunique(x) (x).resize(unique(all(x)) - (x).begin())
#define ld long double
#define int long long

void SOLVE() {
	string s;
	cin >> s;
	deque <char> dq;
	for (char c : s) {
		dq.pb(c);
	}
	int Q;
	cin >> Q;
	int rev = 0;
	while (Q--) {
		int tt;
		cin >> tt;
		if (tt == 1) {
			rev ^= 1;
		}
		else {
			int f;
			cin >> f;
			if (rev) f = 3 - f;
			char c;
			cin >> c;
			if (f == 1) dq.push_front(c);
			else dq.push_back(c);
		}
	}
	if (rev) reverse(all(dq));
	for (auto now : dq) cout << now;
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cout << fixed;
	cout.precision(10);
	int Q = 1;
	//cin >> Q;
	while (Q--) {
		SOLVE();
	}

	return 0;
}

Measured memory_bytes: 976
// Template from https://techracho.bpsinc.jp/jhonda/2019_08_05/78537

// input macro for competive-programming from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
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)*}
    };

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

#[allow(unused_macros)]
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<_>>()
    };

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

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

    ($iter:expr, bytes) => {
        read_value!($iter, String).into_bytes()
    };

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

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();

    println!("{}", solve(&s));
}

macro_rules! test {
    ($($input:expr => $output:expr),* $(,)*) => {
        #[test]
        fn solve_test() {
            $(
                assert_eq!(solve($input), $output);
            )*
        }
    };
}

test! {}

fn solve(src: &str) -> String {
    input! {
        source = src,
        a: f64,
        b: f64,
    }

    (f64::ceil((b-1f64)/(a-1f64)) as i64).to_string()
}

Measured memory_bytes: 4352
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
n;//ACe
char s[101][999];//ACe¼
p[101];//wü¿i
r[101];//VsÌCfbNX(-1=BàsÂ)
k[100];//[i]:ACeiÌBàÉKvÈACe
q[100][100];//[i][j]:ACeiÌBàÉKvÈACeXg
G(){//ACe¼ðüÍµAACeCfbNXðÔ·
	int i;
	char t[999];
	scanf("%s",t);
	for(i=0;i<n;i++)
		if(strcmp(s[i],t)==0)
			return i;
	return n;
}
R(o){//ACeoÌÅ¬Ìli
	int m,i;
	if(r[o]!=-1){
		m=0;
		for(i=0;i<k[r[o]];i++)
			m+=R(q[r[o]][i]);
	}else
		m=p[o];
	if(p[o]<m)
		m=p[o];
	return m;
}
main(){
	int i,j,m;
	for(;scanf("%d",&n),n;){
		memset(r,-1,sizeof(r));
		for(i=0;i<n;i++){
			scanf("\n%s%d",s[i],p+i);
		}
		scanf("%d",&m);
		for(i=0;i<m;i++){
			scanf("\n");
			r[G()]=i;
			scanf("%d",k+i);
			for(j=0;j<k[i];j++){
				scanf(" ");
				q[i][j]=G();
			}
		}
		printf("%d\n",R(G()));
	}
	exit(0);
}

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

#define fast                     \
    ios::sync_with_stdio(false); \
    cin.tie(NULL);               \
    cout.tie(NULL)
#define int long long
#define endl '\n'

using namespace std;

const int N = 2e5 + 77;
int vis[N];
vector<int> g[N];

void dfs(int x){
    if(vis[x]) return;
    vis[x] = 1;
    for(auto i : g[x]) dfs(i);
}

int32_t main()
{
    fast;  
    int n , m ; cin >> n >> m ;
    for(int i = 0 ; i < m ; ++i){
        int a , b; cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int ans = 0 ;
    for(int i = 1 ; i <= n ; ++i){
        if(vis[i]) continue;
        ans++;
        dfs(i);
    }
    cout << ans-1 << endl;
    return 0;
}

Measured memory_bytes: 13408
N = int(input())

count = 0

count += int(N/2)

if int(N/2)-N/2 == 0:
    for i in range(1,int(N/2)):
        if int(N/i)-N/i == 0:
            count += int(N/i)-1
        else:
            count += int(N/i)
else:
    for i in range(1,int(N/2)+1):
        if int(N/i)-N/i == 0:
            count += int(N/i)-1
        else:
            count += int(N/i)

print(count)

Measured memory_bytes: 9184
from collections import deque
n, m = map(int, input().split())
candidate = [[] for i in range(n)]
parent = [0] * n
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    candidate[a].append(b)
    candidate[b].append(a)
q = deque([0])
visited = [False] * n
visited[0] = True
while len(q) > 0:
    v = q.popleft()
    for i in candidate[v]:
        if visited[i]:
            continue
        visited[i] = True
        parent[i] = v
        q.append(i)
print("Yes")
for i in range(1, n):
    print(parent[i]+1)

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

public class Main {
	static int w, h;
	static int a[][];
	static int m[][];
	static int sx, sy;
	static int ans;

	public static void markh(int x, int y, int mark) {
		if (x < 0 || w <= x || y < 0 || h <= y || a[y][x] == '#') {
			return ;
		}
		if (0 <= m[y][x]) {
			if (x != sx || y != sy) {
				return ;
			}
		}
		m[y][x] = mark;
		markh(x + 1, y, mark);
		markh(x - 1, y, mark);
		markh(x, y + 1, mark);
		markh(x, y - 1, mark);
	}

	public static void markl (int x, int y, int mark) {
		if (x < 0 || w <= x || y < 0 || h <= y || a[y][x] == '.' || 0 <= m[y][x]) {
			return ;
		}
		m[y][x] = mark;
		markl(x + 1, y, mark);
		markl(x - 1, y, mark);
		markl(x, y + 1, mark);
		markl(x, y - 1, mark);
	}

	public static boolean check() {
		for (int i = 0; i < h; i++) {
			if (0 <= m[i][0]) {
				ans = m[i][0];
				return true;
			}
			if (0 <= m[i][w - 1]) {
				ans = m[i][w - 1];
				return true;
			}
		}
		for (int j = 0; j < w; j++) {
			if (0 <= m[0][j]) {
				ans = m[0][j];
				return true;
			}
			if (0 <= m[h - 1][j]) {
				ans = m[h - 1][j];
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			w = sc.nextInt();
			h = sc.nextInt();
			if ((w | h) == 0) {
				break;
			}
			a = new int[h][w];
			m = new int[h][w];
			for (int i = 0; i < h; i++) {
				Arrays.fill(m[i], -1);
			}
			for (int i = 0; i < h; i++) {
				String s = sc.next();
				for (int j = 0; j < w; j++) {
					a[i][j] = s.charAt(j);
					if (a[i][j] == '&') {
						m[i][j] = 0;
						sx = j;
						sy = i;
					}
				}
			}
			markh(sx, sy, 0);

			while (!check()) {
				for (int i = 0; i < h; i++) {
					for (int j = 0; j < w; j++) {
						if (0 <= m[i][j] && a[i][j] != '#') {
							markl(j + 1, i, m[i][j] + 1);
							markl(j - 1, i, m[i][j] + 1);
							markl(j, i + 1, m[i][j] + 1);
							markl(j, i - 1, m[i][j] + 1);
						}
					}
				}
				for (int i = 0; i < h; i++) {
					for (int j = 0; j < w; j++) {
						if (0 <= m[i][j] && a[i][j] != '.') {
							markh(j + 1, i, m[i][j]);
							markh(j - 1, i, m[i][j]);
							markh(j, i + 1, m[i][j]);
							markh(j, i - 1, m[i][j]);
						}
					}
				}
			}
			System.out.println(ans);
		}
	}
}

Measured memory_bytes: 34680
#include "bits/stdc++.h"
using namespace std;
long long x[200001], r[200001], l[200001];
int main()
{
	long long s, n, mx = 0;
	scanf("%lld%lld", &s, &n);
	for(long long i = 1; i <= n; ++i)
	{
		scanf("%lld", &x[i]);
		r[i] = x[i] * 2 + r[i - 1];
	}
	for(long long i = n; i > 0; --i)
	{
		l[i] = (s - x[i]) * 2 + l[i + 1];
	}
	for(long long i = 1, t, a, b; i <= n; ++i)
	{
		if(i - 1 < n - i)
		{
			t = i - 1;
			a = x[i] + r[i - 1] - r[i - 1 - t] + l[i + 1] - l[i + 2 + t];
			b = s - x[i] + r[i - 1] - r[i - 1 - t] + l[i + 1] - l[i + 1 + t];
		}
		else if(i - 1 > n - i)
		{	
			t = n - i;
			a = x[i] + r[i - 1] - r[i - 1 - t] + l[i + 1] - l[i + 1 + t];
			b = s - x[i] + r[i - 1] - r[i - 2 - t] + l[i + 1] - l[i + 1 + t];
		}
		else
		{
			t = n - i;
			a = x[i] + r[i - 1] - r[i - 1 - t] + l[i + 1] - l[i + 1 + t];	 
			b = s - x[i] + r[i - 1] - r[i - 1 - t] + l[i + 1] - l[i + 1 + t];
		}
		mx = max(mx, max(a, b));
	}
	printf("%lld\n", mx);
}

Measured memory_bytes: 4992
fun main (args:Array<String>){
    val (a , b) = readLine()!!.split(" ").map(String::toInt)
    var stringA = ""
    var stringB = ""
    for(i in 1..b){
        stringA += a
    }
    for(i in 1..a){
        stringB += b
    }
    if(stringA < stringB) print(stringA)
    else print(stringB)
}

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

using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "{";
    rep(i, (int)v.size()) {
        if (i)
            os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

template <typename T, size_t S> void printArray(const T (&array)[S]) {
    for (auto val : array)
        std::cout << val << ", ";
    std::cout << "\n";
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::setprecision(10);

    int a, b, c; std::cin >> a >> b >> c;
    int k; std::cin >> k;

    bool ok = false;
    rep(i, k+1) rep(j, k-i+1) rep(l, k-i-j+1) {
        int aa = a * pow(2, i), bb = b * pow(2, j), cc = c * pow(2, l);
        if (aa < bb && bb < cc) ok = true;
    }
    std::cout << (ok ? "Yes" : "No") << "\n";
}


Measured memory_bytes: 4128
N,M = map(int,input().split())
H = list(map(int,input().split()))

obs_set = set([n for n in range(1,N+1)])
for m in range(M):
    A,B = map(int,input().split())
    if H[A-1]<H[B-1]:
        obs_set.discard(A)
    elif H[A-1] == H[B-1]:
        obs_set.discard(A)
        obs_set.discard(B)
    else:
        obs_set.discard(B)
        
print(len(obs_set))

Measured memory_bytes: 23660
#include <bits/stdc++.h>
using namespace std;
int main(){
  int64_t N,answer;
  cin >> N;
  
  vector<int64_t> lucas = {2,1};
  for(int i=0;i<N-1;i++){
    lucas.push_back(lucas.at(0)+lucas.at(1));
    lucas = {lucas.at(1),lucas.at(2)};
  }
  answer = lucas.at(1);
  cout << answer;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000
long long A,B,N;

long long f(long long x){
	if(x<=0LL)return 0LL;
	return (A*x)/B - A*(x/B);
}

int main(){
	
	cin>>A>>B>>N;
	
	long long ans = max(f(N),f((N/B)*B-1));
	
	cout<<ans<<endl;
	
	return 0;
}


Measured memory_bytes: 3560
const main = stdin => {
  const input = stdin.trim().split('\n')
  const [A, B] = input[0].split(' ').map(x => parseInt(x))

  if (B % A === 0) {
    console.log(A + B)
  } else {
    console.log(B - A)
  }
}
// 標準入力
main(require("fs").readFileSync("/dev/stdin", "utf8"))


Measured memory_bytes: 30560
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <tuple>
#include <cstring>
#include <cctype>
#include <ctype.h>

#define INF 100000000

using namespace std;

int main()
{
    string s;
    cin >> s;
    int x = stoi(s);
    int fx = 0;
    for (int i = 0; i < s.length(); i++)
    {
        fx += s[i] - '0';
    }
    if (x % fx == 0)
    {
        cout << "Yes\n";
    }
    else
    {
        cout << "No\n";
    }
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<fstream>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n-1); }

int N;
vector<int> A;
int X[200000+10];
int Ans;

void solve(void) {
    for(int i = N;i >= 1;i--) {
        int sum = 0;
        for(int j = 1;j*i <= N;j++) {
            sum += X[j*i];
        }
        if(sum % 2 != A[i]) {
            Ans++;
            X[i] = 1;
        }
    }

    cout<<Ans<<endl;
    if(Ans == 0) {
        return;
    } else {
        
        int counter = 0;
        for(int i = 0;i <= N;i++) {
            if(X[i] == 1) {
                counter++;
                if(Ans == counter) {
                    cout<<i<<endl;
                } else {
                    cout<<i<<" ";
                }
            }
        }
    }

    return;
}
    
int main(void) {
    cin>>N;
    int sum = 0;
    A.push_back(0);
    for(int i = 0;i < N;i++) {
        int a;
        cin>>a;
        A.push_back(a);
    }

    solve();

    return 0;
}


Measured memory_bytes: 2548
N=int(input())
S=input()
c=0
d=0
for i in range(N):
    if S[i]=="R":
        c+=1

for j in range(c):
    if S[j]=="W":
        d+=1

for k in range(c,N):
    if S[k]=="R":
        d+=1

if d%2==0:
    print(int(d/2))
else:
    print(int((d+1)/2))

Measured memory_bytes: 9468
#include<vector>
#include<cstdio>
using namespace std;
bool prime[50001];
int ans[50001];
int main(){
    prime[1] = true;
    for(int i = 2; i*i <= 50000; i++){
        if(prime[i] == false){
            for(int j = i; j <= 50000; j+=i){
                prime[j] = true;
            }
            prime[i] = false;
        }
    }
    vector<int> p;
    for(int i = 1; i <= 50000; i++){
        if(!prime[i]) p.push_back(i);
    }
    for(int i = 0; p[i] <= 25000; i++){
        for(int j = i; p[i]+p[j] <= 50000; j++){
            ans[p[i]+p[j]]++;
        }
    }
    int n;
    while(scanf("%d",&n),n){
        printf("%d\n",ans[n]);
    }
    return 0;
}

Measured memory_bytes: 1352
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <utility>
#include <stack>
#include <string.h>
#include <complex>
using namespace std;

const int INF = 1<<29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int,int> P;
struct edge{int to,cost;};

struct State{
    int d,t,s;
    bool operator<( const State& right ) const {
        return d > right.d;
    }
};

int bldg[2][100];
int d[2][100];
int main(){

    while(1){
        int n;
        scanf("%d",&n);
        if(!n)break;

        for(int i=0;i<2;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&bldg[i][j]);
                d[i][j] = INF;
            }
        }

        priority_queue<State> que;
        d[0][0] = 0; d[1][0] = 0;
        que.push((State){0,0,0});
        que.push((State){0,0,1});
        int ans = -1;
        while(que.size()){
            State p = que.top(); que.pop();
            if(d[p.s][p.t] < p.d) continue;
            if(p.t==n-1){
                ans = p.d;
                break;
            }
            if(bldg[p.s][p.t]==0 || (bldg[p.s][p.t]==1&&bldg[p.s][p.t+1]!=1)){
                int nextd = d[p.s][p.t]+1;
                for(int i=1;i<=2&&p.t+i<n;i++){
                    if(bldg[p.s^1][p.t+i]==2){
                        int k = p.t+i-1;
                        while(bldg[p.s^1][k]==2) k--;
                        if(d[p.s^1][k]>nextd){
                            d[p.s^1][k] = nextd;
                            que.push((State){nextd,k,p.s^1});
                        }
                    }else{
                        if(d[p.s^1][p.t+i]>nextd){
                            d[p.s^1][p.t+i] = nextd;
                            que.push((State){nextd,p.t+i,p.s^1});
                        }
                    }
                }
            }else if(bldg[p.s][p.t]==1){
                for(int k=p.t+1;k<n;k++){
                    if(k+1==n || bldg[p.s][k+1]!=1){
                        if(d[p.s][k]>d[p.s][p.t]){
                            d[p.s][k] = d[p.s][p.t];
                            que.push((State){d[p.s][p.t],k,p.s});
                        }
                        break;
                    }
                }
            }
        }
        if(ans == -1) cout << "NA" << endl;
        else cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 1232
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 2002

enum Type{
	VISUAL,
	DANCE,
	VOCAL,
};

struct Info{
	int point[3];
};

int vonus_point[3] = {5,3,2};
Info info[NUM];
double dp_allocate[NUM][NUM],dp_appeal_num[NUM];
double dp_exp[3][NUM];

int main(){

	int N,M;
	scanf("%d %d",&N,&M);

	for(int i = 0; i < N; i++){
		scanf("%d %d %d",&info[i].point[VISUAL],&info[i].point[DANCE],&info[i].point[VOCAL]);
	}

	for(int i = 0; i <= M; i++){
		for(int k = 0; k <= M; k++)dp_allocate[i][k] = 0;
	}

	int self = 0;

	dp_allocate[0][0] = 1;

	for(int count = 1; count <= M; count++){
		for(int vi = 0; vi <= count-1; vi++){
			dp_allocate[count][vi+1] += dp_allocate[count-1][vi]/3.0;
			dp_allocate[count][vi] += dp_allocate[count-1][vi]*2.0/3.0;
		}
	}

	double tmp = 0;
	for(int num = M; num >= 0; num--){
		tmp += dp_allocate[M][num];
		dp_appeal_num[num] = tmp;
	}

	for(int junle = 0; junle <= 2; junle++){
		for(int count = 0; count <= M; count++)dp_exp[junle][count] = 0;
	}

	double rank_1,rank_2,rank_3,rank_lowest,upper_probability;
	int self_point,enemy_point,left,right,m,need_count;

	for(int junle = 0; junle <= 2; junle++){
		for(int count = 0; count <= M; count++){

			rank_1 = 1.0,rank_2 = 0.0,rank_3 = 0.0,rank_lowest = 1.0;

			self_point = info[self].point[junle]*count;

			for(int i = 1; i < N; i++){

				enemy_point = info[i].point[junle];

				left = 0,right = M,m = (left+right)/2;
				need_count = BIG_NUM;

				while(left <= right){
					if(enemy_point*m >= self_point){
						need_count = m;
						right = m-1;
					}else{
						left = m+1;
					}
					m = (left+right)/2;
				}

				if(need_count == BIG_NUM){
					upper_probability = 0;
				}else{
					upper_probability = dp_appeal_num[need_count];
				}

				rank_3 = rank_2*upper_probability+rank_3*(1-upper_probability);
				rank_2 = rank_1*upper_probability+rank_2*(1-upper_probability);
				rank_1 *= (1-upper_probability);
				rank_lowest *= upper_probability;
			}

			dp_exp[junle][count] = (rank_1+rank_2+rank_3)*vonus_point[junle]-rank_lowest;
		}
	}

	double ans = -DBL_MAX;

	int vo;
	for(int vi = 0; vi <= M; vi++){
		for(int da = 0; vi+da <= M; da++){
			vo = M-(vi+da);

			ans = max(ans,dp_exp[VISUAL][vi]+dp_exp[DANCE][da]+dp_exp[VOCAL][vo]);
		}
	}

	printf("%.12lf\n",ans);

    return 0;
}



Measured memory_bytes: 34560
#include <iostream>
#include <cstdio>

using namespace std;

const int INF = 1e9;
int k;
int a[100005];

void f(int p, int q)
{
    if(a[p] <= q) return;
    a[p] = q;
    f(p * 10 % k, q);
    f((p + 1) % k, q + 1);
}

int main()
{
    scanf("%d", &k);
    for(int i = 0; i < k; i++) a[i] = 45;
    f(1, 1);
    printf("%d", a[0]);
    return 0;
}

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

typedef long long ll;

int main() {
    int N, K, ans = 0;
    cin >> N >> K;
    vector<int> l(N);
    rep(i, N) cin >> l[i];
    sort(l.begin(), l.end());
    rep(i, K) ans += l[N - 1 - i];
    cout << ans << "\n";
}

Measured memory_bytes: 256
/* problem flower-> dp at coder Q
*/
#include <bits/stdc++.h>
#define ll long long int
#define FASTIO std::ios::sync_with_stdio(false);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define inf 1e18
#define g(x) cout<<x<<" "<<endl;
#define gg(x,y) cout<<x<<" & "<<y<<" "<<endl
#define ggg(x,y,z) cout<<x<<" & "<<y<<" & "<<z<<" "<<endl
#define all(x) x.begin(),x.end()
#define input_from_file  freopen("input.txt", "r", stdin);
#define mod 1000000007
#define sz 200005
using namespace std;


ll dp[sz];
ll seg[4*sz];
ll h[sz];
ll a[sz];
void updt(ll qs,ll qe,ll s,ll e,ll idx,ll val)
{
  if(qs<=s&&qe>=e)
  {
    seg[idx] = val;
    return;
  }
  if(qs>e||qe<s)return;
  ll mid = (s+e)/2;
  updt(qs,qe,s,mid,2*idx+1,val);
  updt(qs,qe,mid+1,e,2*idx+2,val);
  seg[idx] = max(seg[2*idx+1],seg[idx*2+2]);
  return;

}
ll query(ll qs,ll qe,ll s,ll e,ll idx)
{
    if(s>e)return 0;
  if(qs<=s&&qe>=e)
  {
    return seg[idx];
  }

    if(qs>e||qe<s)return 0;
    ll mid = (s+e)/2;
    ll f1 = query(qs,qe,s,mid,2*idx+1);
    ll f2 = query(qs,qe,mid+1,e,2*idx+2);
    return max(f1,f2);
}

int main()
{


    FASTIO
    cin.tie(NULL);
    ll n;
    cin >> n;
    for(ll i=1;i<=n;i++)cin >> h[i];
    for(ll i=1;i<=n;i++)cin >> a[i];

    for(ll i=1;i<=n;i++)
    {
      ll hi = h[i];
      ll bst = query(1,hi-1,1,n,1);
      dp[i] = max(dp[i],bst+a[i]);
      updt(hi,hi,1,n,1,dp[i]);

    }
    ll ans = 0;
    for(ll i=1;i<=n;i++)ans = max(ans,dp[i]);cout<<ans<<endl;

return 0;
}


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

using namespace std;

#define ll long long

ll check(ll n)
{
    ll ret = 0;

    while(n)
   {
       if(n % 10 != 7) return 0;
       n /= 10;
       ret++;
   }

    return ret;
}
int main()
{
    ll num, sz = 0;
    ll gun = 0;

    cin >> num;

    ll lastd = num % 10;

    ///cout << lastd << endl;

    if(lastd % 2 == 0 || lastd == 5) cout << -1 << endl;
    else
    {

       /// cout << "Entered\n";

        while(check(gun) == 0)
        {
           sz++;
           if(sz >= 10000000)
           {
                sz = -1;
                break;
           }

           ll need = ((7 - (gun % 10)) + 10) % 10;

          ll gg = 10;
          for(ll i = 0; i <= 9; i++)
          {
              if((i * lastd) % 10 == need)
              {
                  gg = i;
                  break;
               }
          }

          if(gg == 10)
          {
              sz = -1;
              break;
          }

          gun += (num * gg);
        ///  cout << need <<' ' << gg << ' ' << gun << endl;

          gun /= 10;

          if(gun == 0) break;
        }

        sz += check(gun);

        cout << sz << endl;
    }
}


Measured memory_bytes: 3644
import itertools as iter
import math as m

def main():
    X = list(map(int, input().split()))
    res = 0
    if X[3] - X[0] > 0:
        res += X[0]
        X[3] -= X[0]
    else:
        res += X[3]
        X[3] = 0

    if X[3] - X[1] > 0:
        X[3] -= X[1]
    else:
        X[3] = 0

    if X[3] - X[2] > 0:
        res -= X[2]
    else:
        res -= X[3]

    print(res)

if __name__ == '__main__':
    main()

Measured memory_bytes: 62268