akhauriyash/Code-Regression huggingface dataset:
use std::io::*;
use std::str::FromStr;

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

use std::cmp::min;

fn main() {
    let n: usize = read();
    let t: i64 = read();
    let mut a: Vec<i64> = vec![];
    for _ in 0..n {
        let ai = read();
        a.push(ai)
    }

    let mut ans = t * n as i64;

    for i in 1..n {
        let ai_prev = a[i - 1];
        let ai = a[i];
        if ai_prev + t > ai {
            ans -= (ai_prev + t) - ai;
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 6396
from heapq import heappush, heappop, heappushpop
N = int(input())
A = [int(a) for a in input().split()]

def calc(L):
    s = sum(L[:N])
    h = []
    X = [s]
    for i in range(N):
        heappush(h, L[i])
    for i in range(N, 2*N):
        s += L[i] - heappushpop(h, L[i])
        X.append(s)
    return X

B = calc(A)
C = calc([-a for a in A][::-1])[::-1]
print(max([B[i] + C[i] for i in range(N+1)]))

Measured memory_bytes: 119696
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
 

class SegmentTree:
    def __init__(self, a):
        self.padding = 0
        self.n = len(a)
        self.N = 2 ** (self.n-1).bit_length()
        self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n)
        for i in range(2*self.N-2, 0, -2):
            self.seg_data[(i-1)//2] = self.seg_data[i] + self.seg_data[i-1]
    
    def __len__(self):
        return self.n
    
    def update(self, i, x):
        idx = self.N - 1 + i
        self.seg_data[idx] = x
        while idx:
            idx = (idx-1) // 2
            self.seg_data[idx] = self.seg_data[2*idx+1] + self.seg_data[2*idx+2]
    
    def query(self, i, j):
        # [i, j)
        if i == j:
            return self.seg_data[self.N - 1 + i]
        else:
            idx1 = self.N - 1 + i
            idx2 = self.N - 2 + j # 閉区間にする
            result = self.padding
            while idx1 < idx2 + 1:
                if idx1&1 == 0: # idx1が偶数
                    result = result + self.seg_data[idx1]
                if idx2&1 == 1: # idx2が奇数
                    result = result + self.seg_data[idx2]
                    idx2 -= 1
                idx1 //= 2
                idx2 = (idx2 - 1)//2
            return result
    
    @property
    def data(self):
        return self.seg_data[self.N-1:self.N-1+self.n]


N, Q = map(int, readline().split())
C = list(map(int, readline().split()))
data = list(map(int,read().split()))
lr = [[i, data[2*i], data[2*i+1]] for i in range(0, Q)]
lr.sort(key=lambda x:x[2])

ans = [0] * Q
last_appeared = [-1] * (N + 1)
st = SegmentTree([0]*N)

now = 0
for i, l, r in lr:
    l -= 1
    while now < r:
        c = C[now]
        pre_idx, last_appeared[c] = last_appeared[c], now
        if pre_idx >= 0:
            st.update(pre_idx, 0)
        st.update(now, 1)
        now += 1
    ans[i] = st.query(l, r)


print(*ans, sep='\n')

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

int main()
{
	int n;
	ll k;
	cin >> n >> k;
	vector<ll> a(n), f(n), d(n);
	rep(i, 0, n)cin >> a[i];
	rep(i, 0, n)cin >> f[i];
	sort(all(a));
	sort(all(f), greater<ll>());
	rep(i, 0, n)d[i] = a[i] * f[i];
	ll left = -1ll, right = (ll)1e13, mid, N;
	while (right - left > 1ll) {
		N = 0, mid = (right + left) / 2ll;
		rep(i, 0, n) {
			if (d[i] > mid)N += (d[i] - mid + f[i] - 1) / f[i];
		}
		(N <= k ? right : left) = mid;
	}
	printf("%lld\n", right);
	return 0;
}


Measured memory_bytes: 7984
#include<iostream>
#include<string.h>
using namespace std;
int main() 
{
  string s,t;
  cin >> s >> t;
  
  int l = s.length();
  
  for(int i = 0;i<=l; i++){
    if(s[i] != t[i]){
      cout << "No";
      break;
    }
    else if (i == l - 1){
	  cout << "Yes";
      break;
    }
  }
}


Measured memory_bytes: 3628
import java.util.*;

public class Main {
	boolean PrimeJudge (int n){
		if(n==2){return true;}
		if(n%2==0){return false;}
		for(int i=3;i<(int)(Math.sqrt(n))+1;i++){
			if(n%i==0){return false;}
		}
		return true;
	}
	public static void main(String[] args){
	    Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int count=0;
        for(int i=0;i<n;i++){
            if(new Main().PrimeJudge(sc.nextInt())){count++;}
        }
        System.out.println(count);
	}
}


Measured memory_bytes: 40608
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pi 3.14159265358979323846
#define mod 1000000007
int main()
{
    IOS;
    ll a,b,c,d,e;
    cin>>a;
    if(a>=30) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

Measured memory_bytes: 3636
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] S = sc.nextLine().split("");
		String[] T = sc.nextLine().split("");
		int sum = 0;
		for (int i = 0; i < 3; i++) {
			if (S[i].equals(T[i])) {
				sum++;
			}
		}
		System.out.println(sum);
	}
}


Measured memory_bytes: 23764
s = list(map(int,list(input())))

s = s[::-1]
s = s + [0]
cnt = 0
kuri = 0

for i in range(len(s)):
  n = s[i]
  if n < 5:
    cnt += n
  elif n > 5:
    cnt += 10 - n
    s[i+1] += 1
  else:
    if s[i+1] >= 5:
      s[i+1] += 1
    cnt += 10 - n
print(cnt)
    

Measured memory_bytes: 21228
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long int
const int N = 1e5 + 5;

void solve(){
	int a, b, c;
    cin >> a >> b >> c;
    if( a== b and b !=c ){
        cout << "Yes\n";
    } else if( b == c and a != c){
        cout << "Yes\n";
    } else if( a == c and b != a){
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
    
	
}


int main(){
	ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); 
	//int t; cin >> t;
	//while(t--)
		solve();
}


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

int main() {
    int x;
    cin>>x;
    int f=x/100;
    int l=x%100;
    if(f>=1 && f<=12 && l>=1 && l<=12){
    	cout<<"AMBIGUOUS";
    } else if(f>=1 && f<=12){
    	cout<<"MMYY";
    } else if(l>=1 && l<=12){
    	cout<<"YYMM";
    } else{
    	cout<<"NA";
    }
	return 0;
}

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

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back
#pragma GCC optimize ("-O3")

using namespace std;

// #define DEBUG_IS_VALID

#ifdef DEBUG_IS_VALID
#define DEB 1 
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...) if(DEB) DUMPOUT<<"  "<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl<<"    "; if(DEB) dump_func(__VA_ARGS__)
template<typename T1,typename T2>ostream& operator << (ostream& os, pair<T1,T2> p){cout << "(" << p.first << ", " << p.second << ")"; return os;}
template<typename T>ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "}"; return os; }
template<typename T>ostream& operator << (ostream& os, set<T>& st){for(auto itr = st.begin(); itr != st.end(); itr++) cout << *itr << (next(itr)!=st.end() ? ", " : ""); cout << "}"; return os;}
template<typename T1,typename T2>ostream& operator << (ostream& os, map<T1,T2> mp){cout << "{"; for(auto itr = mp.begin(); itr != mp.end(); itr++) cout << "(" << (itr->first) << ", " << (itr->second) << ")" << (next(itr)!=mp.end() ? "," : ""); cout << "}"; return os; }

void dump_func(){DUMPOUT << endl;}
template <class Head, class... Tail>void dump_func(Head&& head, Tail&&... tail){ DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...);}
template<class T> inline bool chmax(T& a,T const& b){if(a>=b) return false; a=b; return true;}
template<class T> inline bool chmin(T& a,T const& b){if(a<=b) return false; a=b; return true;}

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int mod=1e9+7,INF=1<<29;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll lmod = 1e9+7,LINF=1LL<<59; 
 
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N; cin >> N ;
  vl a(N),s(N+1);
  rep(i,N) cin >> a[i] ;
  sort(ALL(a));
  rep(i,N) s[i+1] = s[i]+a[i];
  int m = N-1;
  rrep(i,N-1){
    if((2*(s[i]))<a[i]){
      // iは吸収できない
      m = i;
      break;
    }
  }
  cout << N-m << endl;
  return 0;
}


Measured memory_bytes: 1792
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    bool f = false;
    for (int i = 0; i < n; ++i) cin >> a[i];

    int now = 1;
    int idx = 1;
    for (; idx <= n; ++idx) {
        now = a[now-1];
        if (now == 2) {
            f = true;
            break;
        }
    }
    int ans = (f) ? idx : -1;
    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 640
import java.util.Scanner;

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


        final int ab = scanner.nextInt();
        final int bc = scanner.nextInt();
        final int ca = scanner.nextInt();

        System.out.println(Math.min(Math.min(ab + bc, bc + ca), ca + ab));                
    }
}

Measured memory_bytes: 20564
#include <stdio.h>

int main(){
  int n,p,i,j,k,ftl[8],d,e,a[2],m;
  ftl[0]=1;
  for(i=1;i<8;i++){
    ftl[i]=ftl[i-1]*i;
  }
  scanf("%d",&n);
  for(i=0;i<2;i++){
    d=(1<<n)-1;
    a[i]=0;
    for(j=n-1;j>=0;j--){
      scanf("%d",&p);
      e=0;
      for(k=0;k<p;k++){
        e+=(d>>k)&1;
      }
      a[i]+=e*ftl[j];
      d^=1<<(p-1);
    }
  }
  m=a[0]-a[1];
  if(m<0) m*=-1;
  printf("%d",m);
  return(0);
}

Measured memory_bytes: 128
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int a[100];
	int b[100];
	
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
		
	}
	int s[100];
	for (int i = 0; i < n; i++)
	{
		s[i] = { a[i] - b[i] };
	}
	sort(s, s + n);
	reverse(s, s + n);
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (s[i] < 0)
		{
			break;
		}
		ans += s[i];;
	}
	cout << ans << endl;
	

	
	
	

	
}

Measured memory_bytes: 256
def solve():
    s = [i for i in input()]
    n = len(s)
    return min([len([True for i, j in zip(s, ["1" if i % 2 == mod else "0" for i in range(n)]) if i != j]) for mod in [0, 1]])


if __name__ == "__main__":
    print(solve())


Measured memory_bytes: 5400
import java.util.*;

public class Main{
  public static void main(String[] args) {
    Scanner sc= new Scanner(System.in);
    int N = sc.nextInt(),max=0;
    String B[] = new String[N];
    for(int i=0; i<N; i++) {
      B[i]=sc.next();
    }
    
    int T = sc.nextInt();
    String R[] = new String[T];
    for(int j=0; j<T; j++) {
      R[j]=sc.next();
    }
    
    for(int k=0; k<N; k++) {
      int n=0;
      for(int l=0; l<N; l++) {
        if(B[k].equals(B[l])) n++;
      }
      for(int m=0; m<T; m++) {
        if(B[k].equals(R[m])) n--;
      }
      if(n>max) max=n;
    }
    System.out.println(max);
  }
}

Measured memory_bytes: 35756
import java.util.*;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        Integer N = Integer.parseInt(scanner.next());
        Integer M = Integer.parseInt(scanner.next());
        Map<Integer, Integer> H = new HashMap<Integer, Integer>();


        for (int i = 0; i < N; i++) {
            H.put(i, Integer.parseInt(scanner.next()));
        }

        boolean[] I = new boolean[N];
        boolean[] C = new boolean[N];
        Arrays.fill(I, true);
        for (int i = 0; i < M; i++) {
            Integer A1 = Integer.parseInt(scanner.next())-1;
            Integer A2 = Integer.parseInt(scanner.next())-1;
            if(H.get(A1) > H.get(A2)) {
                I[A2] = false;
            } else if(H.get(A2) > H.get(A1)) {
                I[A1] = false;
            } else {
                I[A1] = false;
                I[A2] = false;
            }
        }

        Integer count = 0;
        for (boolean i : I) if (i) count += 1;
        System.out.println(count);
    }
}


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

typedef long long ll;

const long long MOD = 1000000007;

void solve(long long N, std::vector<long long> A){
  vector<ll> sA(A.size());
  partial_sum(A.begin(), A.end(), sA.begin());
  for (auto & sai : sA) {
    sai = sai % MOD;
  }
  ll rv = 0;
  for (size_t i = 0; i < sA.size() -1; ++i) {
    ll p = (sA[i] * A[i+1]) % MOD;
    rv = (rv + p) % MOD;
  }
  cout << rv << endl;
}

int main() {
  long long N;
  scanf("%lld",&N);
  std::vector<long long> A(N);
  for(int i = 0 ; i < N ; i++){
    scanf("%lld",&A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


Measured memory_bytes: 6448
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
using ull= unsigned long long;

int main(){
  int x,y,z;
  cin >>x>>y>>z;
  cout <<z<<" "<<x<<" "<<y<<endl;
}

Measured memory_bytes: 3536
#include <bits/stdc++.h>
 
int main() {
  int a,b;
  std::cin >> a >> b;
  if((a+b)%2==0) std::cout << (a+b)/2 << std::endl;
  else std::cout << (a+b)/2+1 << std::endl;
  
  return 0;
}

Measured memory_bytes: 256
fn get_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().to_string()
}

fn main() {
    let abc: Vec<i64> = get_line().split(' ').map(|x| x.parse().unwrap()).collect();

    println!("{}",
        if abc[2] - abc[1] == abc[1] - abc[0] {
            "YES"
        }
        else {
            "NO"
        }
    );
}


Measured memory_bytes: 4352
import string
import sys
S = input()
T = input()
sLst = [0] * 26
tLst = [0] * 26

for i in range(26):
    sLst[i] = S.count(string.ascii_lowercase[i])
    tLst[i] = T.count(string.ascii_lowercase[i])
sLst.sort(reverse=True)
tLst.sort(reverse=True)

for i in range(26):
    if sLst[i] != tLst[i]:
        print('No')
        sys.exit()

print('Yes')

Measured memory_bytes: 4164
#!/usr/bin python3
# -*- coding: utf-8 -*-

from fractions import gcd   #for python3.4
from math import gcd        #for python3.8

def Gcd(lt):
    l = len(lt)
    ret = [1]*l
    ret[0] = lt[0]
    for i in range(1,l):
        ret[i] = gcd(ret[i-1], lt[i])
    return ret

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    g = Gcd(A)[-1]
    if K<=max(A) and K%g==0:
        print('POSSIBLE')
    else:
        print('IMPOSSIBLE')

if __name__ == '__main__':
    main()

Measured memory_bytes: 21280
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define N 403
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod=1000000007;
inline int read(){
	int x=0,w=0;char ch=getchar();
	while (!isdigit(ch))w|=ch=='-',ch=getchar();
	while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return w?-x:x;
}
int n,m,f[N][N][N],ans;
vector<pii>v[N];
inline void add(int &a,int b){a+=b;if (a>=mod)a-=mod;}
inline bool check(int i,int j,int k,int m1,int m2,int m3,int M2){
	return (k<m1&&(m2==i||(j<m2&&k>=M2))&&(m3==-1||(j>=m3&&k>=m3)));
}
signed main(){
	n=read(),m=read();
	for (int i=1;i<=m;++i){
		int l=read(),r=read(),x=read();
		v[r].pb(mp(l,x));
	}
	f[0][0][0]=1;
	for (int i=0;i<n;++i){
		int m1=i+2,m2=i+2,M2=-1,m3=-1;
		for (int j=0;j<v[i+1].size();++j){
			if (v[i+1][j].se==1)m1=min(m1,v[i+1][j].fi);
			if (v[i+1][j].se==2)M2=max(M2,v[i+1][j].fi),m2=min(m2,v[i+1][j].fi);
			if (v[i+1][j].se==3)m3=max(m3,v[i+1][j].fi);
		}
		for (int j=0;j<=i;++j)
			for (int k=j?j+1:0;k<=i;++k){
				if (check(i+2,j,k,m1,m2,m3,M2))add(f[i+1][j][k],f[i][j][k]);
				if (check(i+2,j,i,m1,m2,m3,M2))add(f[i+1][j][i],f[i][j][k]);
				if (check(i+2,k,i,m1,m2,m3,M2))add(f[i+1][k][i],f[i][j][k]);
			}
	}
	for (int j=0;j<n;++j)
		for (int k=j?j+1:0;k<n;++k)
			add(ans,f[n][j][k]);
	printf("%d\n",ans%mod);
    return 0;
}

Measured memory_bytes: 190720
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;

const int MAXN = 2005;

int N, X;
int a[MAXN];
int pref[MAXN][MAXN];

int main() {
    scanf("%d%d", &N, &X);
    for (int i = 0; i < N; i++)
        scanf("%d", a + i);
        
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
            pref[i][j] = a[(i - j + N) % N];
            if (j) pref[i][j] = min(pref[i][j], pref[i][j - 1]);
        }
                    
    ll sol = (ll)1e18;
    for (int i = 0; i < N; i++) {
        ll sum = (ll)i * X;
        for (int j = 0; j < N; j++)
            sum += pref[j][i];
        sol = min(sol, sum);
    }

    printf("%lld\n", sol);
        
    return 0;
}



Measured memory_bytes: 15872
n = int(input())
t = list(map(int,input().rstrip().split(' ')))
m = int(input())
s = sum(t)
ans = list()
for i in range(m):
  p,x = map(int,input().rstrip().split(' '))
  ans.append(s-t[p-1]+x)

for a in ans:
  print(a)

Measured memory_bytes: 3060
n,d=map(int,input().split())
p=list()
cnt=0

for i in range(n):
  p1=list(map(int,input().split()))
  p.append(p1)

for i in range(n):
  q=p[i]
  r=q[0]**2+q[1]**2
  if r<=d**2:
    cnt+=1
    
print(cnt)

Measured memory_bytes: 45420
/*
組み合わせの数(1 から n までの数の中から、重複無しで３つの数を選びそれらの合計が x となる組み合わせの数を求めるプログラム)
2018/02/10
*/

#include<stdio.h>
int main(void){
  int n,x;
  int i,j,k;
  int count;/*i+k+j = nを満たす回数*/
  while(1){
    scanf("%d %d",&n,&x);
    if(n == 0 && x == 0)break;
    count =0;
    for(i=1;i<=n-2;i++)
      for(j=i+1;j<=n-1;j++)
	for(k=j+1;k<=n;k++)
	  if(i+j+k == x)count++;
    printf("%d\n",count);
  }
  return 0;
}


Measured memory_bytes: 2108
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
using namespace std;
typedef long long ll;
#define free  freopen ("input.txt", "r", stdin);freopen ("output.txt", "w", stdout);
#define Fast  ios::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second

bool vowel(char che)
{
    if (che == 'A' || che == 'E' || che == 'I' || che == 'O' || che == 'U' || che == 'Y')
        return true;
    else
        return false;
}

const int N = 2e3+5;
const int mod = 1e9+7;
ll a[N], b[N], c[N], d, dp[N][N], dis;
char o;
bool mark[N], h;
string s;
//set <ll> st1, st2;
//deque <int> dq;
//vector <ll> vm, vp, vt[N];
//int a1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    Fast
    ll t, n, m, k = 0, f = 0, sum = 0, ans = 0;
    cin >> n;
    m = n-3;
    k = 0;
    for (int i = 0; i <= 2000; i++){
        dp[0][i] = 1;
        dp[i][i] = 1;
    }
    for (int i = 1; i <= 2000; i++){
        for (int j = 1; j < i; j++){
            dp[j][i] = dp[j][i-1]+dp[j-1][i-1]%mod;
        }
    }
    while (m >= 0){
        k ++;
        ans += dp[k-1][m+k-1];
        ans %= mod;
        m -= 3;
    }
    cout << ans << endl;
}


Measured memory_bytes: 26288
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <chrono>
#include <cmath>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<long long,long long> ii;

struct custom_hash {
    typedef long long ll;
    typedef unsigned long long ull;
    const ull FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    static ull splitmix64(ull x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(ll x) const {
        return splitmix64(x + FIXED_RANDOM);
    }
    size_t operator()(const pair<int,int> &i)const{
        return splitmix64((((ll)i.first)^(((ll)i.second)<<32))+FIXED_RANDOM);
    }
};

int n,k,d;
long long arr[200005];
long long pre[200005];
unordered_map<int, vector<ii> > m;

vector<ii> compress_ranges(vector<ii> ranges){
    sort(ranges.begin(),ranges.end());
    vector<ii> r;
    r.push_back(ranges[0]);
    for (int x=1;x<ranges.size();x++){
        if (r.back().second>=ranges[x].first){
           r.back().second=max(r.back().second,ranges[x].second);
        }
        else{
            r.push_back(ranges[x]);
        }
    }
    return r;
}


int main(){
    scanf("%d%d%d",&n,&k,&d);
    
    arr[1]=pre[1]=k;
    for (int x=2;x<=n;x++){
        arr[x]=arr[x-1]+d;
        pre[x]=arr[x]+pre[x-1];
    }
    d=abs(d*2);
    
    long long small,big;
    int offset;
    for (int x=0;x<=n;x++){
        small=pre[x]-(pre[n]-pre[x]);
        big=(pre[n]-pre[n-x])-pre[n-x];
        if (small>big) swap(small,big);
        offset=(d)?small%d:0;
        if (offset<0) offset+=d;
        small-=offset,big-=offset;
        if (d) small/=d,big/=d;
        if (!m.count(offset)) m[offset]=vector<ii>();
        
        m[offset].push_back(ii(small,big));
    }
    
    long long ans=0;
    vector<ii> temp;
    for (auto it:m){
        temp=compress_ranges(it.second);
        for (auto range:temp){
            ans+=range.second-range.first+1;
        }
    }
    
    printf("%lld\n",ans);
}

Measured memory_bytes: 20936
#include <iostream>
#include <vector>
#include <numeric>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> vec(n, 1);
  while (k--) {
    int d;
    cin >> d;
    while (d--) {
      int tmp;
      cin >> tmp;
      vec[tmp-1] = 0;
    }
  }
  cout << accumulate(vec.begin(), vec.end(), 0) << endl;
}

Measured memory_bytes: 3560
<?php

function divisors($N) {
    $res = array();
    for ($d = 1; $d*$d <= $N; $d++) {
        if ($N % $d == 0) {
            array_push($res, $d);
            if ($N / $d != $d) {
                array_push($res, $N / $d);
            }
        }
    } 
    return $res;
}

$input = explode(' ', trim(fgets(STDIN)));
$N = (int)$input[0];
$M = (int)$input[1];

$max = 0;
foreach (divisors($M) as $d) {
    if ($M / $d >= $N) {
        $max = max($max, $d);
    }
}

echo $max;


Measured memory_bytes: 4604
S=''.join(list(reversed(input())))
T=''.join(list(reversed(input())))
for i in range(len(S)-len(T)+1):
  for j in range(len(T)):
    if S[i+j]!="?" and T[j]!=S[i+j]:break
  else:
    print(''.join(list(reversed((S[:i]+T+S[i+j+1:]).replace("?","a")))))
    exit()
print("UNRESTORABLE")


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

int main() {
  int n;
  cin>>n;
  vector<int>a(n);
  for(int i=0;i<n;i++) cin>>a[i];
  sort(a.begin(),a.end());
  int num=a[0];
  int vol=0;
  int ans=0;
  for(int i=0;i<n;i++){
    if(num==a[i]) {
      vol++;
      if(i==n-1){
        if(vol-num<0) ans+=vol;
        else ans+=vol-num;
      }
    }
    else{
      if(vol-num<0) ans+=vol;
      else ans+=vol-num;
      num=a[i];
      vol=1;
      if(i==n-1){
        if(vol-num<0) ans+=vol;
        else ans+=vol-num;
      }
    }
  }
  cout<<ans<<endl; 
}

Measured memory_bytes: 768
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll,ll>
#define vpii vector<pair<ll,ll> >
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a,i) memset(a,i,sizeof(a))
#define all(x) x.begin(),x.end()
#define itit(it,a) for(auto it=(a).begin(); it!=(a).end(); it++)
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define repr(i,a,b) for(ll i=a; i>b; i--)
#define reprr(i,a,b) for(ll i=a; i>=b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x,ll y,ll z) {return max(max(x,y),z);}
ll min3(ll x,ll y,ll z) {return min(min(x,y),z);}
const ll M=2e5+10,M2=1e6+10,mod=1e9+7,inf=1e17+10;
void add(int& a, int b) {a+=b;if(a>=mod){a-=mod;}}
#define trace1(x)			 	 cerr<<#x<<": "<<x<<endl
#define trace2(x, y)		 	 cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)		 	 cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)	 	 cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)	 cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
ll power(ll x,ll n){ll result=1;while(n>0){if(n%2==1)result=(result*x); x=((x)*(x));n=n/2;}return result;}

ll n,q;
vector<ll> adj[200001];
ll value[200001];

void dfs(ll x,ll p){
	value[x]+=value[p];
	for(auto u: adj[x]){
		if(u!=p){
			dfs(u,x);
		}
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll ans=0;
	cin >> n >> q;
	rep(i,0,n-1){
		ll a,b;
		cin >> a >> b;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	rep(i,0,q){
		ll p,x;
		cin >> p >> x;
		p--;
		value[p]+=x;
	}
	dfs(0,-1);
	for(ll i=0; i<n; i++){
		cout << value[i] << " ";
	}
	return 0;
}

Measured memory_bytes: 20096
#include <bits/stdc++.h>
using namespace std;
 
const long long mod = 1e9 + 7 ;
const int N = 3e5+122; 

int n, m, k, t;
int a[N];
long long ans = 1;
vector<int> v[N];

int main ( ) {
	cin >> n;
	for(int i =1 ; i <= n; i++) {
		cin >> m;
		if(m==0) {
			ans *= (3 - a[m]);
			ans%=mod;
			a[m]++;
		}else {
			ans *= a[m-1]-a[m];
			ans%=mod;
			a[m]++;
		}	
	}
	cout<<ans;
	return 0;

}

Measured memory_bytes: 8576
/**
 *    author        : Sudipta Banik Trisha 
 *    created       : September 13, 2020 6:00 PM
 *    Problem Name  : A - Not
 *    Problem Limit : 2000 ms , 1024 MB
 *    Problem Url   : https://atcoder.jp/contests/abc178/tasks/abc178_a
 *    @genarated by : ai-virtual-assistant
**/

#include<bits/stdc++.h>
using namespace std;

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

    int x;
    cin >> x;

    if(x == 1) cout << "0" << endl;
    else cout << "1" << endl;



    return 0 ;
}


Measured memory_bytes: 3664
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <map>
 
const int MOD = 1e9+7;
typedef long long ll;
using namespace std;
const ll M = 998244353;
 
int main(void) {
    string s;
    cin >> s;
    if(s[2]==s[3] && s[4]==s[5]) cout << "Yes" << endl;
    else cout << "No" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
 
using namespace std;
 
#define SZ(x) (int)(x.size())
 
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
 
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
 
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
 
  ll n, t; cin >> n >> t;
 
  vector<ll> h(n), su(n+1);
  for(int i=0;i<n;++i) {
    cin >> h[i];
    su[i+1] = su[i] + h[i];
  }
 
  ll ma = 0;
  ll argma = 0;
  for(int i=0;i<n;++i) {
    if(ma <= h[i]) {
      ma = h[i];
      argma = i;
    }
 
    if(t - su[i+1] < 0) {
      cout << 1 << endl;
      continue;
    } else if(t - su[i+1] == 0) {
      cout << 2 << endl;
      continue;
    }
 
    ll ans = (t - su[i+1]) / h[argma] + 2;
    cout << ans << endl;
  }
 
}


Measured memory_bytes: 4416
A, B = gets.split.map(&:to_i)
ans = A % 3 == 0 || B % 3 == 0 || (A+B) % 3 == 0
if ans then
    puts "Possible"
else
    puts "Impossible"
end
=begin
    N, M = gets.split.map(&:to_i)
    AS = N.times{ gets.to_i }
=end


Measured memory_bytes: 1788
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Char 

main :: IO ()
main = do
    n <- readLn
    s <- T.getLine
    putStrLn $ solve n s

solve :: Int -> T.Text -> String
solve n s = T.unpack (T.map (\x -> chr ((mod ((ord x - 65) + n) 26) + 65)) s)

Measured memory_bytes: 1020
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>//sort,reverse
#include<utility>//pair
#include<queue>//queue,priority_queue
#include<deque>//double_ended queue
using namespace std;
typedef pair<int,int> P;//Pでpair<-,->を表す。
typedef long long LL; 

int main()
{ 
    int n;
    cin>>n;
    int a[n];
    int x[n][n];
    int y[n][n];
    for(int i=0;i<n;i++){
        cin>>a[i];
        for(int j=0;j<a[i];j++){
            cin>>x[i][j]>>y[i][j];
        }
    }
    int h[n];//nが正直者なら1嘘つきなら0
    int N=1;
    int N2=0;
    int max=0;
    int flag=1;
    for(int i=0;i<n;i++){N=2*N;}
    for(int l=N-1;l>=0;l--){
        int k=l;
        for(int i=0;i<n;i++){
            h[i]=k%2;
            k=k/2;
        }
        flag=1;
        for(int i=0;i<n;i++){
            if(h[i]==1){
                for(int j=0;j<a[i];j++){
                    if(h[x[i][j]-1]!=y[i][j]){flag=0;}
                }
            }
        }
        if(flag==1){
            N2=0;
            for(int i=0;i<n;i++){
                if(h[i]==1){N2++;}
            }
            if(max<N2){max=N2;}
        }
    }
    cout<<max<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
import java.util.Stack;

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

        int ans = solve(s);
        System.out.println(ans);
    }

    static int solve(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            if (stack.isEmpty()) {
                stack.add(s.charAt(i));
                continue;
            }

            if (s.charAt(i) == '0') {
                if (stack.peek() == '0') {
                    stack.add(s.charAt(i));
                } else {
                    stack.pop();
                }
            } else {
                if (stack.peek() == '1') {
                    stack.add(s.charAt(i));
                } else {
                    stack.pop();
                }
            }
        }
        return s.length() - stack.size();
    }
}


Measured memory_bytes: 26300
#include<bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T &x)
{
	x=0;
	int 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^48),ch=getchar();
	x*=f;
	return;
}	
template<typename T>
void write(T x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>=10) write(x/10);
	putchar(x%10+'0');
	return;
}
const int MAXN=200010;
int n,k;
int a[MAXN];
int main()
{
    read(n),read(k);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=k+1;i<=n;++i)
    if(a[i]>a[i-k]) printf("Yes\n");
    else printf("No\n");
	return 0;
}


Measured memory_bytes: 4396
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < k; i++) {
			int d = sc.nextInt();
			for (int j = 0; j < d; j++) {
				arr[sc.nextInt() - 1]++;
			}
		}
		System.out.println(Arrays.stream(arr).filter(s -> s == 0).count());
	}

	static class FastScanner {
		private BufferedReader reader;
		private StringTokenizer tokenizer;

		public FastScanner(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
			tokenizer = null;
		}

		public String next() {
			if (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

		public String nextLine() {
			if (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					return reader.readLine();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}

			return tokenizer.nextToken("\n");
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public long[] nextLongArray(int n) {
			long[] a = new long[n];
			for (int i = 0; i < n; i++)
				a[i] = nextLong();
			return a;
		}
	}
}

Measured memory_bytes: 35468
fn read1<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

pub fn readn<T: std::str::FromStr>(delimiter: &str) -> Vec<T> {
    let s = read1::<String>();
    s.split(delimiter).map(|e| e.parse().ok().unwrap()).collect::<Vec<T>>()
}

fn main() {
    let n = read1::<usize>();
    let xy = (0..n).map(|_| readn::<f64>(" ")).collect::<Vec<Vec<f64>>>();

    let mut ans = 0.0;
    for i in 0..n {
        for j in i+1..n {
            ans += 2.0 * ((xy[i][0] - xy[j][0]) * (xy[i][0] - xy[j][0]) + (xy[i][1] - xy[j][1]) * (xy[i][1] - xy[j][1])).sqrt();
        }
    }

    println!("{:?}", ans / n as f64);
}

Measured memory_bytes: 4352
import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;

public class Main {
 
	public static void main(String[] args) throws IOException {
		//File file = new File("input.txt");
		//Scanner sc = new Scanner(file);
		
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		if(n == 1) System.out.println("Hello World");
		else System.out.println(sc.nextInt() + sc.nextInt());
	}
}

Measured memory_bytes: 21972
#include <stdio.h>
 
#define N 20
#define Q 200
 
int solve(int, int);
 
int a[N];
int n, flag =0;

main(){
  int i,q;
  int j[Q];
 
  scanf("%d",&n);
  for(i = 0;i < n;i++){
    scanf("%d",&a[i]);
  }
 
  scanf("%d",&q);
  for(i = 0;i < q;i++){
    scanf("%d",&j[i]);
  }
   
  for(i = 0;i < q;i++){
    solve(0,j[i]);
    if(flag == 0)printf("no\n");
    else printf("yes\n");
    flag = 0;
  }
  return 0;
}
 
int solve(int i,int m){
  if(m == 0){
    flag = 1;
    return 1;
  }
  else if(i < n){
    solve(i + 1,m - a[i]);
    solve(i + 1,m);
  }
}

Measured memory_bytes: 580
module Main where

main :: IO ()
main = do
    k <- read <$> getLine 
    print $ ans !! (k-1)

ans = [1..9] ++ concatMap f ans

f :: Integer -> [Integer]
f n 
    |   m == 0  =   [10*n + m,10*n + m + 1]
    |   m == 9  =   [10*n+m-1,10*n+m]
    |   otherwise   =   [10*n+m-1,10*n+m,10*n+m+1]
    where m = n `mod` 10

Measured memory_bytes: 8828
#include<bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
#define REP(i,x,n) for (int i=x; i<n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
const int mod =1'000'000'007;

int main(){
    int x, n;
    cin >> x >> n;
    vi p(102,1);
    rep(i,n){
        int pi;
        cin >> pi;
        p.at(pi)--;
    }

    int min = 100;
    int ans;
    REP(i,0,102){
        if (p.at(i) == 0) continue;
        int res;
        res = abs(i - x);
        if (res < min){
            ans = i;
            min = res;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3608
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int h,w; cin >> h >> w;
    vector<vector<int>> c(10,vector<int> (10));
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
        {
            cin >> c[i][j];
        }
    }
    for(int i = 0;i < 10; i++)
    {
        for(int j = 0;j < 10; j++)
        {
            for(int k = 0; k < 10; k++)
            {
                c[j][k] = min(c[j][k],c[j][i]+c[i][k]);
            }
        }
    }
    int ans = 0;
    vector<vector<int>> A(h,vector<int> (w));
    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w; j++)
        {
            cin >> A[i][j];
            if(A[i][j] != -1)
            {
                ans += c[A[i][j]][1];
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
typedef  long long  ll;
ll inf=LLONG_MAX,mod=1000000007,BS,k;

int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        #ifndef ONLINE_JUDGE
            freopen("input.txt","r",stdin);
            freopen("output.txt","w",stdout);
        #endif
        ll n;cin>>n;
        ll arr[n],suf[n];
        for(ll i=0;i<n;i++)cin>>arr[i];
        suf[n-1]=arr[n-1];
        for(ll i=n-2;i>=0;i--)
        suf[i]=(arr[i]+suf[i+1])%mod;
        ll sum=0;
        // sum=suf[n-1];
        for(ll i=n-2;i>=0;i--){
            sum=(sum+(arr[i]*(suf[i+1]))%mod)%mod;
        }
        cout<<sum;

}


Measured memory_bytes: 6772
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			int n = sc.nextInt();
			double ave = 0.0;
			double a = 0.0;
			double b;
			if (n == 0)
				break;
			int[] score = new int[n];
			for (int i = 0; i < n; i++) {
				score[i] = sc.nextInt();
				ave = ave + score[i];
			}
			ave = ave / n;
			for (int j = 0; j < n; j++) {
				a = a + (score[j] - ave) * (score[j] - ave);
			}
			a = a / n;
			b = Math.sqrt(a);
			System.out.println(b);
		}
	}

}


Measured memory_bytes: 31232
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a)  (a).begin(),(a).end()
ll ans=0;
void Main(){
    ll n;cin>>n;
    ll k;cin>>k;
    n%=k;
    ans=min(n,k-n);
    cout<<ans<<endl;
}
int main(){
    Main();
    /*
    東方風神録は神が出てくるので当然神ゲー
    */
    return 0;
}

Measured memory_bytes: 256
INF = 10 ** 7
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  heapq import heappop,heapify,heappush

def prim(G):
    ans = 0
    used = [0] * len(G)
    used[0] = 1
    q = []
    for e,c in G[0]:
        heappush(q,(c,e))
    
    while len(q):
        c1,e1 = heappop(q)
        if used[e1]:
            continue
        used[e1] = 1
        for e2,c2 in G[e1]:
            if used[e2] == 0:
                heappush(q,(c2,e2))
        ans += c1
    return ans

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    P = []
    Q = []
    for i in range(n):
        x,y = map(int,input().split())
        P.append((i,x))
        Q.append((i,y))
    
    P.sort(key = lambda x:x[1])
    Q.sort(key = lambda x:x[1])
    for index in range(n - 1):
        i,x1 = P[index]
        j,x2 = P[index + 1]
        G[i].append((j,x2 - x1))
        G[j].append((i,x2 - x1))
        i,y1 = Q[index]
        j,y2 = Q[index + 1]
        G[i].append((j,y2 - y1))
        G[j].append((i,y2 - y1))
    
    ans = prim(G)
    print(ans)

if __name__ == '__main__':
    main()


Measured memory_bytes: 84108
#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 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 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);

  string s; std::cin >> s;
  set<char> se;
  rep(i, s.size()) se.insert(s[i]);
  std::cout << (se.size() == 2 ? "Yes" : "No") << "\n";
}


Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

int main()
{
    int64_t x, y;
    cin >> x >> y;

    if (x % y == 0)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << (x * (y - 1)) << endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int cnt = 0;
vector<int> G;

void insertionSort (int A[], int n, int g) {
  for (int i = g; i < n; i++) {
    int v = A[i];
    int j = i - g;

    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }

    A[j + g] = v;
  }
}

void shellSort (int A[], int n) {
  for (int h = 1;;) {
    if (h > n)
      break;
    G.push_back(h);
    h = 3 * h + 1;
  }

  cout << G.size() << endl;

  for (int i = G.size() - 1; i >= 0; i--)
    if (i != 0)
      cout << G[i] << " ";
    else
      cout << G[i] << endl;

  for (int i = G.size() - 1; i >= 0; i--)
    insertionSort(A, n, G[i]);
}

int main () {
  int n, A[1000001];

  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> A[i];
  
  shellSort(A, n);

  cout << cnt << endl;

  for (int i = 0; i < n; i++)
    cout << A[i] << endl;

  return 0;
}



Measured memory_bytes: 7040
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using lli = long long;

int N, cnt[61][2];
lli arr[300'010], ans, MOD = 1e9 + 7;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> arr[i];
		lli tmp = arr[i];
		for (int j = 0; tmp>0; ++j, tmp/=2)
		{
			cnt[j][tmp & 1]++;
		}
	}
	sort(arr, arr + N);
	for (int i = 0; i < N; ++i)
	{
		lli cur = 1;
		for (int j = 0; j < 61; ++j)
		{
			ans = (ans + cur * cnt[j][(arr[i]&1)^1]) % MOD;
			if(arr[i]) cnt[j][(arr[i] & 1)]--;
			cur = (cur * 2) % MOD;
			arr[i] /= 2;
		}
	}
	cout << ans;
}

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

using namespace std;
const int maxn = 1e2;
char s[maxn];
typedef long long ll;
int main()
{
    cin >> s;
    for (int i = 0; i < strlen(s) - 8; i++)
        cout << s[i];
    cout << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
//#pragma GCC optimize "-O3"
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned short us;
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const ll infll=(1ull<<63)-1,mod=1e9+7;
const int inf=(1ull<<31)-1;
int k,x;

int main()
{
	//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(false); cin.tie(0);cout.precision(18);
	cin>>k>>x;
	for (int i=x-k+1;i<x+k;i++)
		cout<<i<<" ";
	return 0;
}


Measured memory_bytes: 256
N= int(input())
 
C = [100,101,102,103,104,105]
 
#100以上の桁の計算
N_100 = int(N/100)
#下二桁の計算
N_XX = int((N%100)/5)
N_flag = (N%100)%5
if N_100 > N_XX:
    print(1)
elif N_100 ==N_XX and N_flag==0:
    print(1)
else:
      print(0)

Measured memory_bytes: 3060
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            var f = Console.ReadLine().ToArray();
            var result = 0;
            var tmp = 0;
            for (var i = 0; i < 3; i++)
            {
                if (f[i] == 'S')
                {
                    if (tmp > result) result = tmp;
                    tmp = 0;
                }
                else
                {
                    tmp++;
                }
            }

            Console.WriteLine(result > tmp ? result : tmp);
        }
    }
}

Measured memory_bytes: 26336
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <deque>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <tuple>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
const int INF = 1000000000;
const int MOD = 1000000007;

int main(){
	ll n,m;
	cin >> n >> m;
	vector<ll> x(n);
	vector<ll> y(m);
	for(ll i = 0;i < n;i++){
		cin >> x[i];
	}
	for(ll i = 0;i < m;i++){
		cin >> y[i];
	}

	ll x_sum = 0;
	for(ll i = 0;i < n-1;i++){
		ll l = x[i+1] - x[i];
		l *= (i+1) * (n-1-i);
		l %= MOD;
		x_sum += l;
		x_sum %= MOD;
	}

	ll y_sum = 0;
	for(ll i = 0;i < m-1;i++){
		ll l = y[i+1] - y[i];
		l *= (i+1) * (m-1-i);
		l %= MOD;
		y_sum += l;
		y_sum %= MOD;
	}

	cout << x_sum * y_sum % MOD << endl;
}


Measured memory_bytes: 1792
N = int(input())
D = list(map(int, input().split()))
MOD = 998244353

#以下が an mod p を求めるソースコードです.
def power_func(a, n, p):
    res = 1 #これは答え
    while n > 0:
        if n & 1:
            res = res * a % p
        a = a * a % p #1が立っているかどうかに関わらず次のaの累乗を用意しておく
        n = n >> 1
    return res

bucket = [0] * (max(D) + 1)
for dist in D:
    bucket[dist] += 1
# print(bucket)

if bucket[0] != 1 or D[0] != 0:
    print(0)
else:
    ans = 1
    for i in range(len(bucket) - 1):
        ans = (ans * power_func(bucket[i], bucket[i + 1], MOD)) % MOD
    print(ans)


Measured memory_bytes: 52952