akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define REPS(i,n) for (ll i = 1; i<=(ll)(n); i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000000000

int main(){
    ll t, n, a, b, c, d, ans; cin >> t;
    REP(i,t){
        cin >> n >> a >> b >> c >> d;
        if (n*d>0) ans = n*d;
        else ans = 9223372036854775807;
        map<ll,ll> minc;
        priority_queue<vector<ll>,vector<vector<ll>>,greater<vector<ll>>> q;
        q.push({0,n,0,n});
        while(q.size()>0){
            ll nowc = q.top().at(0);
            ll nowat = q.top().at(1);
            if (nowat==1) break;
            if (nowat==0){
                q.pop();
                continue;
            }
            if (0<nowc+nowat*d&&nowc+nowat*d<ans) ans = min(ans,nowc+nowat*d);
            //cout << nowc << " " << nowat << " " << q.top().at(2) << " " << q.top().at(3) << endl;
            if (minc[nowat/2]==0||minc[nowat/2]>nowc+nowat%2*d+a){
                q.push({nowc+(nowat%2)*d+a,nowat/2,nowc,nowat});
                minc[nowat/2]=nowc+nowat%2*d+a;
            }
            if (minc[nowat/3]==0||minc[nowat/3]>nowc+nowat%3*d+b){
                q.push({nowc+(nowat%3)*d+b,nowat/3,nowc,nowat});
                minc[nowat/3]=nowc+nowat%3*d+b;
            }
            if (minc[nowat/5]==0||minc[nowat/5]>nowc+nowat%5*d+c){
                q.push({nowc+(nowat%5)*d+c,nowat/5,nowc,nowat});
                minc[nowat/5]=nowc+nowat%5*d+c;
            }
            if (minc[nowat/2+1]==0||minc[nowat/2+1]>nowc+(2-nowat%2)*d+a){
                q.push({nowc+(2-nowat%2)*d+a,nowat/2+1,nowc,nowat});
                minc[nowat/2+1]=nowc+(2-nowat%2)*d+a;
            }
            if (minc[nowat/3+1]==0||minc[nowat/3+1]>nowc+(3-nowat%3)*d+b){
                q.push({nowc+(3-nowat%3)*d+b,nowat/3+1,nowc,nowat});
                minc[nowat/3+1]=nowc+(3-nowat%3)*d+b;
            }
            if (minc[nowat/5+1]==0||minc[nowat/5+1]>nowc+(5-nowat%5)*d+c){
                q.push({nowc+(5-nowat%5)*d+c,nowat/5+1,nowc,nowat});
                minc[nowat/5+1]=nowc+(5-nowat%5)*d+c;
            }
            q.pop();
        }
        cout << min(ans,minc[1]+d) << endl;
    }
}

Measured memory_bytes: 4732
#include <iostream>
using namespace std;
int main(void){
    int p1,p2,ans;
    cin >> p1 >> p2;
    
    ans = p1 - (p2*2);
    
    if(ans < 0) {
        cout << "0" <<endl;
        return 0;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
 
//?????§??¬?´???°????±???????????????°??????(m >= n)
int calcGcd(int m, int n);
//????°???¬?????°????±???????????????°??????
inline int calcLcm(int m, int n, int gcd);
 
int main(){
    int gcd, lcm, x, y;
     
    while(std::cin >> x >> y){
        if(x > y) gcd = calcGcd(x, y);
        else gcd = calcGcd(y, x);
        lcm = calcLcm(x, y, gcd);
         
        std::cout << gcd << " " << lcm << std::endl;
    }
     
    return 0;
}
 
int calcGcd(int m, int n){
    return n == 0 ? m : calcGcd(n, m%n);
}
 
inline int calcLcm(int m, int n, int gcd){
    return m / gcd * n;
}

Measured memory_bytes: 1160
import java.util.*;
public class Main{
  public static void main(String[]args){
      Scanner sc = new Scanner(System.in);

      int n = sc.nextInt();
      int a = sc.nextInt();


      if(n%500<=a){
        System.out.println("Yes");
      }else{
        System.out.println("No");
      }

  }
}

Measured memory_bytes: 21844
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
  long long l,r;
  bool operator < (const node &a)const
  {
    return r<a.r;
  }
};
long long maxr=-1e18,cnt;
node tmp[100001];
int main()
{
  long long n,l,x;
  cin>>n;
  for (int i=1;i<=n;++i)
    {
      cin>>l>>x;
      tmp[i].l=l-x;
      tmp[i].r=l+x;
    }
  sort(tmp+1,tmp+n+1);
  for (int i=1;i<=n;++i)
    {
      if (maxr<=tmp[i].l)
        maxr=max(maxr,tmp[i].r);
      else
	cnt++;
    }
  cout<<n-cnt<<endl;
  return 0;
}

Measured memory_bytes: 1792
package main

import "fmt"

func main() {
	var s []byte
	fmt.Scan(&s)

	r := 0
	b := 0
	for i := 0; i < len(s); i++ {
		if s[i] == '0' {
			r++
		} else {
			b++
		}
	}
	fmt.Println(len(s) - abs(r-b))

}

func abs(n int) int {
	y := n >> 63
	return (n ^ y) - y
}


Measured memory_bytes: 2596
#include "bits/stdc++.h"
#define REP(i,n,N) for(ll i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
typedef long long ll;
using namespace std;
const ll inf=1e18;

ll a,b,c;
int main(){
    while(cin>>a>>b){
        if(a*2>16||b*2>16){
            p(":(");
        }else{
            
            p("Yay!");
        }

        return 0;
    }

}

Measured memory_bytes: 256
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = float('inf')

    for base in range(-100, 101):
        ans = min(ans, sum((a - base)**2 for a in A))

    print(ans)


if __name__ == '__main__':
    solve()

Measured memory_bytes: 3064
from itertools import permutations
n,m = map(int,input().split())
e = [tuple(sorted(map(int,input().split()))) for _ in range(m)]

ans = 0
for i in permutations(range(2, n+1), n-1):
  l = [1] + list(i)
  ans += sum(1 for j in zip(l, l[1:]) if tuple(sorted(j)) in e) == n-1

print(ans)

Measured memory_bytes: 3188
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for(int i=0;i<(n);i++)
#define REP(i, n) for(int i=1;i<=(n);i++)
#define all(V) V.begin(),V.end()
typedef long long lint;
typedef std::pair<lint, lint> P;
constexpr int INF = INT_MAX;
constexpr lint LINF = LLONG_MAX;
constexpr double eps = 1e-9;
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
inline lint gcd(lint a, lint b) {
	while (b) {
		lint c = a;
		a = b; b = c % b;
	}
	return a;
}
inline lint lcm(lint a, lint b) {
	return a / gcd(a, b) * b;
}
bool isprime(lint n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
lint mypow(lint a, lint b) {
	if (!b)return 1;
	if (b & 1)return mypow(a, b - 1) * a;
	lint memo = mypow(a, b >> 1);
	return memo * memo;
}
lint modpow(lint a, lint b, lint m) {
	if (!b)return 1;
	if (b & 1)return modpow(a, b - 1, m) * a % m;
	lint memo = modpow(a, b >> 1, m);
	return memo * memo % m;
}
void printArray(std::vector<int>& vec) {
	rep(i, vec.size() - 1)std::cout << vec[i] << " ";
	std::cout << vec.back() << std::endl;
}
template<typename T>
void printArray(T l, T r) {
	T rprev = r;
	rprev--;
	for (T i = l; i != rprev; i++) {
		std::cout << *i << " ";
	}
	std::cout << *rprev << std::endl;
}
std::string to_string(std::vector<int>& vec) {
	std::string res = "[";
	rep(i, vec.size() - 1)res += std::to_string(vec[i]) + ", ";
	res += std::to_string(vec.back()) + "]";
	return res;
}
signed main() {
	std::string s;
	std::cin >> s;
	if (s[0] == '7'||s[1] == '7' || s[2] == '7')puts("Yes");
	else puts("No");
	return 0;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

int main() {
    int n, m = 12;
    vector<int> c;
    int dp[114514];

    cin >> n;
    
    fill(dp, dp + 114514, INF);

    dp[0] = 0;

  
    c.push_back(9);
    c.push_back(6);
    c.push_back(1);
    
    
    int nine = 9, six = 6;
    for (; nine <= 100000 || six <= 100000;) {
      nine *= 9;
      six *= 6;
      
      if (nine <= 100000)
        c.push_back(nine);
      if (six <= 100000)
        c.push_back(six);
    }
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j + c[i] <= n; j++) {
            dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
        }
    }

    cout << dp[n] << endl;

    return 0;
}


Measured memory_bytes: 640
//Author: kaiyu
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <numeric>
#include <iomanip>
#include <climits>
using namespace std;
typedef long long ll;
#define sz(x) ((long long)(x).size())
#define all(x)  x.begin(),x.end()
#define endl "\n"
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<int,int>
#define tt ll t;cin >> t;while(t--)
#define int3(x,y,z) ll x,y,z;cin >> x >> y >> z;
#define int2(x,y) ll x,y;cin >> x >>y;
#define int1(x) ll x;cin>>x;
#define  fio ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
ll power(ll a,ll b,ll m=mod){ ll ans=1; a=a%m;  while(b>0) {  if(b&1)  ans=(1ll*a*ans)%m; b>>=1;a=(1ll*a*a)%m;}return ans;}

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


bool cmp(pair<string,int>p1,pair<string,int>p2) {
    return p1.ss > p2.ss;
}

const int MAXN = 5 * 100000 + 10;

//#define int long long
const int sum [] = {1,2,3};
//int dp[MAXN][MAXN]; //rows = coins and col = sum
void score() {

}
int32_t main() {
    fio
    int n;
    scanf("%d", &n);
    vector<int> dp(3);
    // dp[i] - the total cost such that we did activity i on the last considered day
    for(int day = 0; day < n; ++day) {
        vector<int> new_dp(3, 0);
        vector<int> c(3);
        for(int i = 0; i < 3; ++i) {
            scanf("%d", &c[i]);
        }
        // int a, b, c - a lot of implementation
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                if(i != j) {
                    new_dp[j] = max(new_dp[j], dp[i] + c[j]);
                }
            }
        }
        dp = new_dp;
    }
    printf("%d\n", max({dp[0], dp[1], dp[2]}));
}






Measured memory_bytes: 256
import itertools


def read():
    N = int(input())

    return N,


def solve(N, ):
    move = 1000000000000
    a = 1
    b = N
    while a < b:
        b = N // a
        if N % a == 0:
            move = min(move, a + b - 2)
        a += 1
    return move


if __name__ == '__main__':
    input = read()
    print(solve(*input))


Measured memory_bytes: 3060
while True:
    n = int(input())
    if n == 0: break
    ans = minv = 1<<10
    for _ in range(n):
        p, h, w = map(int, input().split())
        BMI = w / ((h*0.01)**2)
        tmp = 22 - BMI if 22 > BMI else BMI - 22
        if tmp < minv:
            ans, minv = p, tmp
    print(ans)

Measured memory_bytes: 7488
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=(ll)1e9+7;
ll ans=1LL,N;
int main(){
    cin >>N;
    for (ll i=1;i<=N;i++){
        ans=(ans*i)%mod;
    }
    cout <<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) FOR(i,0,n)
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define ALL(a) (a).begin(),(a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
struct Edge {
  ll s,t,d;
};
typedef vector<vector<Edge>> Graph;

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

  ll cnt=0;
  REP(i,N) {
    if(S[i]=='(') cnt++;
    else if(cnt) cnt--;
  }
  ll cnt2=0;
  REP(i,N) {
    if(S[N-1-i]==')') cnt2++;
    else if(cnt2) cnt2--;
  }
  REP(i,cnt2) cout<<'(';
  cout<<S;
  REP(i,cnt) cout<<')';
  cout<<endl;


}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int min = in.nextInt();
        int count = 1;
        for(int i=1; i<n; i++) {
            int p = in.nextInt();
            if(p<min) {
                count++;
                min = p;
            }
        }
        System.out.println(count);
    }
}


Measured memory_bytes: 62936
''' ===============================
    -- @uthor : Kaleab Asfaw
    -- Handle : kaleabasfaw2010
    -- Bio    : High-School Student
    ==============================='''

# Fast IO
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file): self._fd = file.fileno(); self.buffer = BytesIO(); self.writable = "x" in file.mode or "r" not in file.mode; self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b: break
            ptr = self.buffer.tell(); self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0; return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)); self.newlines = b.count(b"\n") + (not b); ptr = self.buffer.tell(); self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1; return self.buffer.readline()
    def flush(self):
        if self.writable: os.write(self._fd, self.buffer.getvalue()); self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file): self.buffer = FastIO(file); self.flush = self.buffer.flush; self.writable = self.buffer.writable; self.write = lambda s: self.buffer.write(s.encode("ascii")); self.read = lambda: self.buffer.read().decode("ascii"); self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout); input = lambda: sys.stdin.readline().rstrip("\r\n")
# Others
# from math import floor, ceil, gcd
# from decimal import Decimal as d
mod = 10**9+7
def lcm(x, y): return (x * y) / (gcd(x, y))
def fact(x, mod=mod):
	ans = 1
	for i in range(1, x+1): ans = (ans * i) % mod
	return ans
def arr2D(n, m, default=0):
	lst = []
	for i in range(n): temp = [default] * m; lst.append(temp)
	return lst
def sortDictV(x): return {k: v for k, v in sorted(x.items(), key = lambda item : item[1])}

def solve(st1, st2):
	n, m = len(st1), len(st2)
	dp = arr2D(n+1, m+1)

	for i in range(1, n+1):
		for j in range(1, m+1):
			if st1[i-1] == st2[j-1]:
				dp[i][j] = dp[i-1][j-1] + 1
			else:
				dp[i][j] = max(dp[i-1][j], dp[i][j-1])

	lcs = ""
	i, j = n, m
	while i != 0 and j != 0:
		if dp[i][j] == dp[i-1][j]:
			i -= 1
		elif dp[i][j] == dp[i][j-1]:
			j -= 1
		else:
			lcs += st1[i-1]
			i -= 1
			j -= 1
	return lcs[::-1]
		
st1 = input()
st2 = input()
print(solve(st1, st2))

Measured memory_bytes: 138740
'use strict';

function main(input) {
  const lines = input.split('\n');
  const first = lines[0].split(' ').map(Number);
  const N = first[0];
  const M = first[1];
    // コマ数が点よりも多い場合
  if (M <= N) {
      console.log(0)
    return ;
  }
  const x = lines[1]
    .split(' ')
    .map(Number)
    .sort((a, b) => a - b);

    // 点ごとの差を取る
  const diffs = [];
  for (let i = 1; i < x.length; i++) {
    diffs.push(x[i] - x[i - 1]);
  }
    // 昇順
  diffs.sort((a, b) => a - b);

    // 
  let sum = diffs.slice(0, M - N).reduce((a, c) => a + c);
  console.log(sum);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));


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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
int main()
{
	int d,w;cin>>d>>w;
  while(d!=0&&w!=0)
  {
    vector<vector<int>> e(d,vector<int>(w));
    int i,j,k,l,m,n;
    int ans=0;
    rep(i,d)rep(j,w)cin>>e[i][j];
    rep(i,d)rep(j,w)
    {
      for(k=i+2;k<d;k++)for(l=j+2;l<w;l++)
      {
        int side=10;
        int inside=0;
        int sum_inside=0;
        for(m=i;m<=k;m++)for(n=j;n<=l;n++)
        {
          if(m==i||m==k||n==j||n==l)
          {
            side=min(side,e[m][n]);
          }
          else
          {
            inside=max(inside,e[m][n]);
            sum_inside+=e[m][n];
          }
        }
        if(side>inside)
        {
          ans=max(ans,side*(k-i-1)*(l-j-1)-sum_inside);
        }
      }
    }
    cout<<ans<<endl;
    cin>>d>>w;
  }
}



Measured memory_bytes: 3048
#include<stdio.h>

int main(){
	int xy[4][2];
	scanf("%d%d%d%d",&xy[0][0],&xy[0][1],&xy[1][0],&xy[1][1]);
	int vect[2]={xy[1][0]-xy[0][0],xy[1][1]-xy[0][1]};
	//if(vect[0]>0&&vect[1]>0){	
		xy[2][0]=xy[1][0]-vect[1];
		xy[2][1]=xy[1][1]+vect[0];
		xy[3][0]=xy[2][0]-vect[0];
		xy[3][1]=xy[2][1]-vect[1];
	/*}
	if(vect[0]<0&&vect[1]<0){	
		xy[2][0]=xy[1][0]-vect[1];
		xy[2][1]=xy[1][1]+vect[0];
		xy[3][0]=xy[2][0]-vect[0];
		xy[3][1]=xy[2][1]-vect[1];
	}
	if(vect[0]<0&&vect[1]>0){	
		xy[2][0]=xy[1][0]-vect[1];
		xy[2][1]=xy[1][1]+vect[0];
		xy[3][0]=xy[2][0]-vect[0];
		xy[3][1]=xy[2][1]-vect[1];
	}*/
	printf("%d %d %d %d\n",xy[2][0],xy[2][1],xy[3][0],xy[3][1]);
	return 0;
}


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

using namespace std;

#define int long long

void solve() {
    string s;
    int n;
    cin >> n >> s;
    for (int i = 0; i < s.size(); ++i) s[i] = 'A' + (((s[i] - 'A') + n) % 26);
    cout << s;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <random>
#include <cstdio>
#include <cstring>
#include <cmath>

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

ll N;
vector<ll> A;

void input() {
    cin >> N;
    A.resize(N);
    for (auto &v : A)
        cin >> v;
}


void solve() {
    sort(A.begin(), A.end());
    ll maxC = 0;
    vector<ll> as;
    for (int i = 0; i < 2; i++) {
        ll cc = 0;
        as.resize(0);
        for (ll l = 0, r = N - 1; as.size() < N; ) {
            if (i == 0) {
                if (as.size() % 2 == 0) {
                    as.push_back(A[l++]);
                } else {
                    as.push_back(A[r--]);
                }
            } else {
                if (as.size() % 2 == 0) {
                    as.push_back(A[r--]);
                } else {
                    as.push_back(A[l++]);
                }
            }
        }
        for (ll j = 0; j < N - 1; j++) {
            cc += abs(as[j] - as[j + 1]);
        }
        maxC = max(maxC, cc);
        maxC = max(maxC, cc - abs(as[N - 2] - as[N - 1]) + abs(as.back() - as.front()));
    }
    cout << maxC << endl;
}


int main() {
    input();
    solve();
    
    return 0;
}



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

#define x first
#define y second

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;

const int mod=1000000000+7;

int addm(int& a,int b) {return (a+=b)<mod?a:a-=mod;}

template<class T,class U> bool smin(T& a,U b) {return a>b?(a=b,1):0;}
template<class T,class U> bool smax(T& a,U b) {return a<b?(a=b,1):0;}
const ll unset=LLONG_MIN;

ll n;
ll dp[3000001],cs[3000002];
/*ll solve(int i) {
	if (i>=n) return 1;
	ll &r=dp[i];
	if (r!=LLONG_MIN) return r;
	r=solve(i+1);
*/
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	fill(dp+n,dp+3000000,1);
	for (int i=3000000;i>=n;i--) cs[i]=cs[i+1]+dp[i];

	dp[n-1]=n;
	cs[n-1]=cs[n]+dp[n-1];

	for (int i=n-2;i>=0;i--) {
		dp[i]=(cs[i+1+2]-cs[i+1+n+1]+(n-1)*(n-1)+dp[i+1])%mod;
		cs[i]=(cs[i+1]+dp[i])%mod;
	}

	if (dp[0]<0) dp[0]+=mod;
	cout << dp[0] << endl;
	//cerr << dp[1] << endl;

}

Measured memory_bytes: 47104
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()		//sort(all(vi S)) sort(all(string S))
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }

using P = pair<int, int>;		//P.first, P.second
typedef long long ll;
typedef vector<int> vi;
typedef set<int> seti;
typedef vector<string> vs;

const int MOD = 1e9+7;
const int INF = 1e9;
ll w1 = 0;
ll w,v;

int main() {
	ll N,W;
	cin>>N>>W;
	vector<P> wv;
	map<ll, ll> map;
	rep(i,N){
		cin>>w>>v;
		if(i == 0)w1 = w;
		wv.push_back({w,v});
		map[w-w1]++;
	}
	sort(all(wv));
	cerr<<w1<<endl;
	printpair(wv);
	//上位何個まで足したかの累積和配列を4つ持つ
	//w+iの重さのものを上位j個まで足した配列cumsum[i][j]
	ll cumsum[4][108];
	//初期化
	for (int i = 0; i < 4; ++i){
		cumsum[i][0] = 0;
	}
	int cnt = 1;
	for (int i = N-1; i >= 0; --i){
		cumsum[wv[i].first-w1][cnt] = cumsum[wv[i].first-w1][cnt-1] + wv[i].second;
		if(i == 0)break;
		if(wv[i].first == wv[i-1].first){
			cnt++;
		}
		else{
			cnt = 1;
		}
	}
	ll ans = 0;
	rep(i,map[0]+1)rep(j,map[1]+1)rep(k,map[2]+1)rep(l,map[3]+1){
		ll res = cumsum[0][i] + cumsum[1][j] + cumsum[2][k] + cumsum[3][l];
		ll wsum = i * w1 + j * (w1 + 1) + k * (w1 + 2) + l * (w1 + 3);
		if(wsum<= W){
			ans = max(ans, res);
		}
	}
	cout<<ans<<endl;
}

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

#define int				long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )	for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )	FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP				make_pair
#define EB				emplace_back

typedef pair<int, int> P;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

int dp[1001][10001];

signed main() {
	int W, N, cnt = 1;
	while( cin >> W, W ) {
		cin >> N;
		fill( dp[0], dp[1001], 0 );

		int w = 0, vmax = 0;
		vector<P> tre( N );
		REP( i, N ) {
			char c;
			cin >> tre[i].first >> c >> tre[i].second;
		}

		REP( i, N ) {
			REP( j, W + 1 ) {
				if( j - tre[i].second >= 0 ) {
					dp[i + 1][j] = max( dp[i][j], dp[i][j - tre[i].second] + tre[i].first );

					if( vmax < dp[i + 1][j] ) {
						vmax = dp[i + 1][j];
						w = j;
					} else if( vmax == dp[i + 1][j] ) {
						chmin( w, j );
					}


				} else {
					dp[i + 1][j] = dp[i][j];
				}
			}
		}

		cout << "Case " << ( cnt++ ) << ":" << endl;
		cout << dp[N][W] << endl << w << endl;

	}
}


Measured memory_bytes: 81296
import numpy as np
import sys

if __name__ == '__main__':
    n = int(input())
    works = np.array([[int(i) for i in input().split()] for i in range(n)])
    col_num = 1
    sorted_works = works[np.argsort(works[:, col_num])]
    time = 0
    for i in range(n):
        time += sorted_works[i][0]
        if time > sorted_works[i][1]:
            print("No")
            sys.exit()

        else:
            None

    print("Yes")


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

using namespace std;

int main(){
    long double N,K;
    cin >> N >> K;
    vector<long double> p(N);
    for(int i=0;i<N;i++){
        cin >> p[i];
    }

    long double sum=0,ans;

    for(int i=0;i<K;i++){
        sum += p[i];
    }

    ans = sum;

    for(int i=K;i<N;i++){
        sum = sum-p[i-K]+p[i];
        ans = max(ans,sum);
    }

    cout << setprecision(20) << (ans+K)/2.00000000 << endl;
}

Measured memory_bytes: 3328
import java.util.*;
import java.io.*;

import static java.lang.System.in;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long a = sc.nextLong(), b=sc.nextLong(), c=sc.nextLong();
        long ans = 0;
        if(a%2==1||b%2==1||c%2==1) ans = 0;
        else{
            if(a==b&&a==c) ans = -1;
            else{
                ans = help(a,b,c);
            }
        }
        System.out.println(ans);

    }
    static long help(long a, long b, long c){
        if(a%2==1||b%2==1||c%2==1) return 0;
        return 1+help(a/2+b/2,a/2+c/2,b/2+c/2);
    }
}


Measured memory_bytes: 21844
#include<iostream>
#include<cmath>
#include<algorithm>

int cmp(const void *a,const void *b)
{
	return *(int *)a-*(int *)b;
}

using namespace std;

int main()
{
	int a[3];
	for (int i=0;i<3;i++)
	{
		cin>>a[i];
	}
	qsort(a,3,sizeof(int),cmp);
	cout<<a[2]-a[0]<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <list>
#include <numeric>

using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; i++)
#define rep(i,b) Rep(i,0,b)
#define rrep(i,a) for(int i = a; i >= 0; i--)
#define allof(a) (a).begin(), (a).end()

typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

/*
// 最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

// 最小公倍数
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}*/

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

	int n;
	cin >> n;
	//vector<string>s(n);
	vector<int> p(n);
	vector<pair<string, int>>snum(n);
	rep(i, n) {
		cin >> snum[i].first >> p[i];
		snum[i].second = i + 1;
	}
	sort(allof(snum));
	vector<int> ans(n);

	string s = " ";
	int start = 0;
	int end = 0;
	rep(i, n) {
		if (s == snum[i].first) {
			end++;
		}
		if (s != snum[i].first) {
			if (start != end) {
				vector<pair<int, int>> tmp;
				for (int j = start; j <= end; j++) {
					tmp.push_back({ p[snum[j].second - 1], snum[j].second });
				}
				sort(allof(tmp), greater<pair<int, int>>());
				int cnt = 0;
				Rep(j, start, end + 1) {
					ans[j] = tmp[cnt++].second;
				}
			}
			ans[i] = snum[i].second;
			start = i;
			end = i;
			s = snum[i].first;
		}
	}

	if (start != end) {
		vector<pair<int, int>> tmp;
		for (int j = start; j <= end; j++) {
			tmp.push_back({ p[snum[j].second - 1], snum[j].second });
		}
		sort(allof(tmp), greater<pair<int, int>>());
		int cnt = 0;
		Rep(j, start, end + 1) {
			ans[j] = tmp[cnt++].second;
		}
	}

	rep(i, n) {
		cout << ans[i] << endl;
	}

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N,M,x,y;
  cin >> N; //Nに入力
  x = 0; //念のため初期化
  y = 0;
  vector<string> blue(N); 
  for (int i = 0; i < N; i++) {
    cin >> blue[i]; 
  }
  cin >> M;
  vector<string> red(M);
  for (int i = 0; i < M; i++) {
    cin >> red[i];
  }
  for (int i = 0; i < N; i++) {
    x = 0;
    for (int j = 0; j < N; j++) if (blue[i] == blue[j]) x++;   
    for (int j = 0; j < M; j++) if (blue[i] == red[j]) x--;
    if (x > y) y = x;
  }
  if (y < 0) y = 0;
  cout << y << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	int f[200010];
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%d",&f[i]);
	int max1=0,num=1,max2=0,p;
	for(int i=1;i<=n;i++)
	if(f[i]>max1) max2=max1,max1=f[i],num=1,p=i;
	else if(f[i]==max1) num++;
	else max2=max(f[i],max2);
	if(num>=2)
	for(int i=1;i<=n;i++)
	printf("%d\n",max1);
	else if(num==1)
	for(int i=1;i<=n;i++)
	if(i==p) cout<<max2<<endl;
	else cout<<max1<<endl;
}

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

class Dice {
	public:
	int face[6];
	Dice () {}
	void roll_x(bool d) {
		if (d) rotate(0, 4, 5, 1);
		else rotate(1, 5, 4, 0);
	}
	void roll_y(bool d) {
		if (d) rotate(0, 2, 5, 3);
		else rotate(3, 5, 2, 0);
	}
	void roll_z(bool d) {
		if (d) rotate(1, 3, 4, 2);
		else rotate(2, 4, 3, 1);
	}
	void rotate(int a, int b, int c, int d) {
	  int t;
	  t = face[d];
	  face[d] = face[c];
	  face[c] = face[b];
	  face[b] = face[a];
	  face[a] = t;
	}
	void print() {
		cout << face[0] << endl;
	}
};

bool isSameNum(Dice d1, Dice d2)
{
	for (int i=0; i<6; i++) {
		if (d1.face[i]!=d2.face[i]) return false;
	}
	return true;
}

bool isSame(Dice d1, Dice d2)
{
	for (int i=0; i<6; i++) {
		for (int j=0; j<4; j++) {
			if (isSameNum(d1, d2)) return true;
			d1.roll_z(true);
		}
		if (i%2==0) d1.roll_x(true);
		else d1.roll_y(true);
	}
	return false;
}

bool isAllDiff(int n, Dice* dn)
{
	for (int i=0; i<n; i++) {
		for (int j=i+1; j<n; j++) {
			if (isSame(dn[i], dn[j])) return false;
		}
	}
	return true;
}

int main()
{
	int n;
	cin >> n;
	Dice d[100];
	for (int i=0; i<n; i++) {
		for (int j=0; j<6; j++) cin >> d[i].face[j];
	}
	if (isAllDiff(n, d)) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}




Measured memory_bytes: 3092
// PrintARectangle.cc
// たてH cm よこ W cm の長方形を描くプログラムを作成して下さい。
// 1 cm × 1cm の長方形を '#'で表します。
// 【入力】
// 入力は複数のデータセットから構成されています。各データセットの形式は以下のとおりです：
//   H W
// H, W がともに 0 のとき、入力の終わりとします。
// 【出力】
// 各データセットについて、H × W 個の '#' で描かれた長方形を出力して下さい。
// 各データセットの後に、１つの空行を入れて下さい。
// 【制約】
// 1 ≤ H, W ≤ 300
// 【例】
// 入力：
// 3 4
// 5 6
// 2 2
// 0 0
// 出力：
// ####
// ####
// ####
//
// ######
// ######
// ######
// ######
// ######
//
// ##
// ##
#include<iostream>
using namespace std;

int main( void) {
  int H, W;

  while ( true) {
    cin >> H >> W;

    if ( H == 0 && W == 0) break;

    for ( int h = 1; h <= H; h++ ) {
      for ( int w = 1; w <= W; w++) {
	cout << "#";
      }
      cout << endl;
    }
    cout << endl;
  }
}


Measured memory_bytes: 3088
#include<iostream>
#include<algorithm>
using namespace std ;
typedef long long ll ;
int main()
{
    ll x,y,a,b,c;
    cin >> x >> y >> a >> b >> c ;
    ll p[a+5],q[b+5],r[c+5];
    for(int i=0;i<a;i++){ll x ; cin >> x ; p[i]=x;}
    for(int i=0;i<b;i++){ll x ; cin >> x ; q[i]=x;}
    for(int i=0;i<c;i++){ll x ; cin >> x ; r[i]=x;}
    sort(p,p+a,greater<int>());
    sort(q,q+b,greater<int>());
    sort(r,r+c);
    ll la,lb,lc;
    la=min(x,a);
    lb=min(y,b);
    lc=c;
    while(lc--)
    {
        if(la==0 && lb==0){break;}
        else if (la>0 && lb>0)
        {
            if(p[la-1]<r[lc] && q[lb-1]<r[lc])
            {
               if(p[la-1]<q[lb-1])
               {
                   p[la-1]=r[lc];
                   la--;
               }
               else
               {
                   q[lb-1]=r[lc];
                   lb--;
               }
            }
            else if (p[la-1]<r[lc])
            {
                p[la-1]=r[lc];
                la--;
            }
            else if (q[lb-1]<r[lc])
            {
                 q[lb-1]=r[lc];
                   lb--;
            }
            else{break;}
        }
        else if (la>0)
        {
            if(p[la-1]<r[lc]){p[la-1]=r[lc];}
            else{break;}
            la--;
        }
        else
        {
             if(p[lb-1]<r[lc]){q[lb-1]=r[lc];}
             else{break;}
             lb--;
        }
    }
    /*for(int i=0;i<x;i++)
    {
        cout << p[i] << " " ;
    }
    cout << endl;
    for(int i=0;i<y;i++)
    {
        cout << q[i] << " " ;
    }
    cout << endl;*/
    ll sum=0;
    for(int i=1;i<=max(x,y);i++)
    {
        if(i<=x)
        {
            sum+=p[i-1];
        }
        if(i<=y)
        {
            sum+=q[i-1];
        }
    }
    cout << sum << endl;
    return 0 ;
}

Measured memory_bytes: 2560
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

int main() {
  ll N;

  cin >> N;

  vector<deque<ll> > tbl(N, deque<ll>(N - 1));

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N - 1; j++) {
      cin >> tbl[i][j];
      tbl[i][j]--;
    }  // j
  }    // j

  vector<P> que;

  auto check = [&](ll x) {
    if (tbl[x].size() == 0) return;
    ll xcompe = tbl[x].front();
    if (x == tbl[xcompe].front()) {
      if (x > xcompe) swap(x, xcompe);
      que.push_back(P(x, xcompe));
    }
  };

  for (int i = 0; i < N; i++) {
    check(i);
  }

  ll ans = 0;
  ll games_done = 0;
  while (que.size() > 0) {
    ans++;
    vector<P> prevQ;
    sort(que.begin(), que.end());
    que.erase(unique(que.begin(), que.end()), que.end());
    swap(prevQ, que);
    for (auto& p : prevQ) {
      games_done++;
      tbl[p.first].pop_front();
      tbl[p.second].pop_front();
    }  // p
    for (auto& p : prevQ) {
      check(p.first);
      check(p.second);
    }  // p    
  }  // while

  if (games_done != N * (N - 1) / 2) ans = -1;

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 8832
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>

using namespace std;

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

	vector<int_least32_t> v(3 * n);
	for(auto& x : v) {
		cin >> x;
	}

	sort(v.begin(), v.end(), greater<int_least32_t>());

	int_least64_t sum{ 0 };
	for(auto i = 0; i < 2 * n; i += 2) {
		sum += v[i + 1];
	}

	cout << sum << endl;
}


Measured memory_bytes: 1408
#include "bits/stdc++.h"
using namespace std;

int main() {
	long long A, B;
	cin >> A >> B;
	for (int i = 0; i < 2000; i++) {
		if (i * 2 / 25 == A && i / 10 == B) {
			cout << i << endl;
			return 0;
		}
	}
	cout << -1 << endl;
}

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

int main() {
    long long x;
    cin>>x;
    long long cnt=0;
    long long mon=100;
    while(mon<x){
        long long x=mon*1.01;
        (int64_t) x;
        mon=x;
        cnt++;
    }
    cout<<cnt<<endl;
}

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

using namespace std;

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll; typedef vector<int> VI; typedef pair<int,int> pii;
const ll mod = 1e9+7;

int main()
{
	
	cin.tie(0); ios_base::sync_with_stdio(false);
	int n;
	cin >>n;
	unordered_set<int> st;
	
	rep(i,n){
		int a;
		cin >>a;
		if(st.count(a)) st.erase(a);
		else st.insert(a);
	
	}
	
	cout << sz(st) <<"\n";
	return 0;
}


Measured memory_bytes: 4388
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int main(){

	int A,B;
	scanf("%d %d",&A,&B);

	if(A <= 8 && B <= 8){

		printf("Yay!\n");

	}else{

		printf(":(\n");
	}

	return 0;
}


Measured memory_bytes: 3756
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ll long long
#define out(a) printf("%d",a)
#define max a>b?a:b
#define min a<b?a:b
#define writeln printf("\n")
using namespace std;
int a,b;
int read()
{
	int s=0,t=1; char c;
	while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
	while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
	return s*t;
}
ll readl()
{
	ll s=0,t=1; char c;
	while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
	while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
	return s*t;
}
int main()
{
	a=read(); b=read();
	if (a+b==15) cout<<'+';
	else if (a*b==15) cout<<'*';
	else cout<<'x';
	return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
class Solve {
public:
    void solve() {
        Int A, B, M;
        cin >> A >> B >> M;
        vector<Int> a(A), b(B);
        for (int i = 0; i < A; ++i) cin >> a[i];
        for (int i = 0; i < B; ++i) cin >> b[i];
        vector<Int> x(M), y(M), z(M);
        Int ans = *min_element(all(a)) + *min_element(all(b));
        for (int i = 0; i < M; ++i) {
            cin >> x[i] >> y[i] >> z[i];
            x[i]--;
            y[i]--;
            Int tmp = a[x[i]] + b[y[i]] - z[i];
            chmin(ans, tmp);
        }
        cout << ans << endl;


    }
};


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

    Solve().solve();


    return 0;
}


Measured memory_bytes: 4224
#include <stdio.h>

void swap(int *a, int *b) {
  int temp;
  temp = *a;
  *a = *b;
  *b = temp;
}

int main(void) {
  int x, y, z;
  
  scanf("%d %d %d", &x, &y, &z);
  swap(&x, &y);
  swap(&x, &z);
  printf("%d %d %d\n", x, y, z);
}

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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 60)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64,i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }



i64 n, k;
void solve(){
	cin >> n >> k;
	
	cout << min(n%k, abs(n%k-k)) << endl;
}

int main(){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <numeric>
#include <sstream>
#include <queue>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define PUSH(n,v) for(int i=0; i<(n); i++) {int j; cin >> j; v.push_back(j);}
#define ALL(v) v.begin(), v.end()
#define print(s) cout << (s) << endl

const int INF = 1e9;

int main() {
    int n,m;
    cin >> n >> m;
    vector<int> v;
    PUSH(m,v);
    vector<vector<int>> dp(m+1,vector<int>(n+1,INF));
    REP(i,m+1) dp[i][0] = 0;
    REP(i,m) {
        REP(j,v[i]) dp[i+1][j] = dp[i][j];
        FOR(j,v[i],n+1) {
            dp[i+1][j] = min(dp[i][j], dp[i+1][j-v[i]]+1);
        }
    }
    cout << dp[m][n] << endl;
    return 0;
}

Measured memory_bytes: 7144
//In the Name of God
//Ya Ali

#include <bits/stdc++.h>

#define int long long

#define err(A) cout << #A << " = " << (A) << endl

using namespace std;

const int maxn = 128;

int n, k;
int a[maxn];

int32_t main()
{
  ios::sync_with_stdio(0); cin.tie(0);

  string s;
  
  cin >> s >> k;

  char c = '.';

  n = -1;
  for (int i = 0; i < s.size(); i ++)
    if (s[i] == c)
      a[n] ++;
    else
      {
	c = s[i];
	a[++ n] = 1;
      }
  n ++;
    
  if (n == 1)
    return cout << (k * s.size() * 1ll / 2) << endl, 0;
  if (s.back() == s.front())
    {
      int ans = 0;
      for (int i = 1; i < n - 1; i ++)
	ans += a[i] / 2 * k;
      ans += ((a[0] + a[n - 1]) / 2 * (k - 1) + a[0] / 2 + a[n - 1] / 2);
      return cout << ans << endl, 0;
    }
  else
    {
      int ans = 0;
      for (int i = 0; i < n; i ++)
	ans += a[i] / 2 * k;
      return cout << ans << endl, 0;
    }
  
}


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

int	main()
{
	int	n;
	cin >> n;
	int	fib[50] = {};
	fib[0] = 1;
	fib[1] = 1;
	for (int i = 2; i <= n; ++i) {
		fib[i] = fib[i - 1] + fib[i - 2];
	}
	cout << fib[n] << endl;

}



Measured memory_bytes: 3096
from sys import stdin, exit

N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]

num = []
count = []
for k in a:
    if k not in num:
        num.append(k)
        count.append(1)
    else:
        count[num.index(k)] += 1
    if len(num) > 3:
        print('No')
        exit()
if len(num) == 1 and num[0] == 0:
    print('Yes')
    exit()
if len(num) == 2 and num[0] ^ num[1] == num[1] and N % 3 == 0 and count[1] == 2 * count[0]:
    print('Yes')
    exit()
if len(num) == 2 and num[0] ^ num[1] == num[0] and N % 3 == 0 and count[0] == 2 * count[1]:
    print('Yes')
    exit()
if len(num) <= 2:
    print('No')
    exit()
if (num[0] ^ num[1] == num[2] or num[0] ^ num[2] == num[1] or num[1] ^ num[1] == num[0]) and N % 3 == 0 and count[0] == count[1] == count[2]:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 60784
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

int N;
VI G[100011];
int fix[100011];
int high[100011];
int par[100011];

bool ok() {
    REP (v, N) {
	if (fix[v] != -1 && fix[v] != high[v]) return false;
	EACH (e, G[v]) if (abs(high[*e] - high[v]) != 1) return false;
    }
    return true;
}

void MAIN() {
    scanf("%d", &N);
    REP (i, N-1) {
	int x, y;
	scanf("%d%d", &x, &y);
	x--; y--;
	G[x].push_back(y);
	G[y].push_back(x);
    }

    memset(fix, -1, sizeof fix);
    int K;
    scanf("%d", &K);
    REP (i, K) {
	int v, p;
	scanf("%d%d", &v, &p);
	v--;
	fix[v] = p;
    }

    VI ord;
    ord.reserve(N);
    ord.push_back(0);
    REP (i, N) {
	int v = ord[i];
	EACH (e, G[v]) if (*e != par[v]) {
	    par[*e] = v;
	    ord.push_back(*e);
	}
    }

    REP (i, N) {
	int v = ord[N-1-i];
	if (fix[v] == -1) high[v] = 1<<29;
	else high[v] = fix[v];

	EACH (e, G[v]) if (*e != par[v]) {
	    amin(high[v], high[*e] + 1);
	}
    }
    REP (i, N) {
	int v = ord[i];
	EACH (e, G[v]) if (*e != par[v]) {
	    amin(high[*e], high[v] + 1);
	}
    }

    if (ok()) {
	puts("Yes");
	REP (i, N) printf("%d\n", high[i]);
    } else {
	puts("No");
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



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

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int l = sc.nextInt();
    String[] line = new String[n];
    for (int cnt = 0; cnt < n; cnt++) {
      line[cnt] = sc.next();
    }
    Arrays.sort(line);
    for (int cnt = 0; cnt < n; cnt++) {
      System.out.print(line[cnt]);
    }
    sc.close();
  }
}

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

using namespace std;
#define LL long long 

char d[105][105];

int main(){
    int r,g,b;
    int H, W;
    cin >> H >> W;
    for(int i = 0; i <= H+1; i++){
        for(int j = 0; j <= W+1; j++){
            if(i == 0 || j == 0 || i == H+1 || j == W+1){
                d[i][j] = '#';
            }else{
                cin >> d[i][j];
            }
        }
    }
    for(int i = 0; i <= H+1; i++){
        for(int j = 0; j <= W+1; j++){
            cout << d[i][j];
        }
        cout << endl;
    }
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>  
#include <stdbool.h>   
#define inf (INT_MAX-1)
#define INF 9223372036854775807
#define PI 3.14159265358979323846;
#define EPS 1e-10
#define sq(n) ((n)*(n))
#define rep(i,n) for(i=0;i<n;i++)
#define rev(i,n) for(i=n-1;i>=0;i--)
/*
#define sort(a,n) qsort(a,n,sizeof(TYPE),cmp)
#define sort_r(a,n) qsort(a,n,sizeof(TYPE),cmp_r)
*/
#define chsort(s,n) qsort(s,n,sizeof(char),cmp)
#define chsort_r(s,n) qsort(s,n,sizeof(char),char_cmp_r)
#define TYPE long long
#define ll long long
#define MEMSET(a) memset(a,0,sizeof(a))
#define MEMSET_U(a) memset(a,-1,sizeof(a))
#define bool    _Bool
const int mod = (int)1e09 + 7;

//#define DEBUG1
//#define DEBUG2
//#define DEBUGF
#define DUMMY

int in(void) { int i; scanf("%d", &i); return i; }
long long llin(void) { long long i; scanf("%lld", &i); return i; }
double din(void) { double i; scanf("%lf", &i); return i; }
void chin(char s[]) { scanf("%s", s); }

void print(int a) { printf("%d\n", a); }
void llprint(long long a) { printf("%lld\n", a); }
void dprint(double a) { printf("%.10f\n", a); }
void print2(int a, int b) { printf("%d %d\n", a, b); }
int Max(int a, int b) { if (a > b) { return a; }return b; }
int Min(int a, int b) { if (a < b) { return a; }return b; }
long long llmax(long long a, long long b) { return a > b ? a : b; }
long long llmin(long long a, long long b) { return a < b ? a : b; }
double dmax(double a, double b) { return a > b ? a : b; }
double dmin(double a, double b) { return a < b ? a : b; }
//long long llmax(long long a, long long b) { return a > b ? a : b; }
//long long llmin(long long a, long long b) { return a < b ? a : b; }
//double dmax(double a, double b) { return a > b ? a : b; }
int cmp(const void *a, const void *b) { return *(TYPE *)a - *(TYPE *)b; }
int cmp_r(const void *a, const void *b) { return *(TYPE *)b - *(TYPE *)a; }
int char_cmp(const void *a, const void *b) { return strcmp((char *)a, (char *)b); }
int char_cmp_r(const void *a, const void *b) { return strcmp((char *)b, (char *)a); }
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }



int main() {
	char A[102], B[102];
	int jadge;
	scanf("%s%s", A, B);

	if (strlen(A) < strlen(B)) {
		jadge = 3;
	}
	else if (strlen(A) > strlen(B)) {
		jadge = 1;
	}
	else if(strlen(A) == strlen(B))
	{
		for (int i = 0; i < strlen(A); i++) {
			if (A[i] > B[i]) {
				jadge = 1;
				break;
			}
			if (A[i] < B[i]) {
				jadge = 3;
				break;
			}
			jadge = 2;
		 }
	}

	if (jadge == 1) { puts("GREATER"); }
	else if (jadge == 2) { puts("EQUAL"); }
	else if (jadge == 3) { puts("LESS"); }

#ifdef DEBUGF
	getch();
#endif
	return 0;
}

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

int main(){
	unsigned o;

	while ( cin >> o ){
		bool first = true;
		for ( int i = 0; i < 10; ++i ){
			if ( o & 1 << i ){
				if ( first ){
					cout << ( 1 << i );
					first = false;
				}else{
					cout << " " << ( 1 << i );
				}
			}
		}
		cout << endl;
	}

	return 0;
}

Measured memory_bytes: 0