akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int long long

void solve() {
	int n, k; cin >> n >> k;
    int r, s, p; cin >> r >> s >> p;
    string t; cin >> t;
    vector<int> check(n, 0);
    int ans = 0;
    for(int i = 0; i <= k - 1; ++i) {
        if(t[i] == 'r') {
            ans += p;
        } else if(t[i] == 's') {
            ans += r;
        } else {
            ans += s;
        }
        check[i] = 1;
        for(int j = i + k; j < n; j += k) {
            if(t[j - k] != t[j] || !check[j - k]) {
                check[j] = 1;
                if(t[j] == 'r') {
                    ans += p;
                } else if(t[j] == 's') {
                    ans += r;
                } else {
                    ans += s;
                }
            }
            //cerr << j << endl;
        }
        //cerr << i << ans << endl;
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

Measured memory_bytes: 1232
import java.util.*;

public class Main {
	Scanner sc = new Scanner(System.in);
	int DP(int n){
		int result = 0;
		int x[] = new int [n+1];
		x[0] = 1;
		for(int i = 1;i < n+1;i++){
			for(int j = 1;j <= 3;j++){
				if(i - j >= 0){
					x[i] = x[i] + x[i-j];
				}
			}
		}
		result = ((x[n] / 10) / 365) + 1; 
		return result;
	}
	void doIt() { 
		while(true){
			int n = sc.nextInt();
			if(n == 0)break;
			System.out.println(DP(n));
		}
     }
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
    	new Main().doIt();
    }
}

Measured memory_bytes: 17188
MAX = 510000
MOD = 1000000007

fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX

def comb_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD
        
def comb(n, r):
    if n < r:           return 0
    if n < 0 or r < 0:  return 0
    return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

def solve():
    ans = 0
    an = H + B - A - 1
    ar = B
    bn = W + A - B - 2
    br = W - B - 1
    for i in range(W-B):
        a = comb(an, ar)
        b = comb(bn, br)
        ans += (a * b) % MOD
        an += 1; ar += 1
        bn -= 1; br -= 1
    return ans % MOD

H, W, A, B = map(int, input().split())

comb_init()
print(solve())


Measured memory_bytes: 63488
#include<bits/stdc++.h>
using namespace std;
string s;
int main(){
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='C')
			for(int j=i+1;j<s.size();j++)
				if(s[j]=='F'){
					cout<<"Yes";
					return 0;	
				}		
	}
	cout<<"No";
    return 0;
}

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

import static java.lang.Integer.parseInt;

public class Main {

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

  public void run() {

    try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

      String[] line;
      int dt,dg=0,bdg=90;
      double x=0,y=0,rd;
      while ( true ) {
        line = br.readLine().split(",");
        dt = parseInt(line[0]);
        dg = parseInt(line[1]);

        if(dt == 0 && dg == 0) break;

        rd = Math.toRadians(bdg);
        x += dt * Math.cos(rd);
        y += dt * Math.sin(rd);

        // String loc = String.format("% 03.2f % 03.2f :% 02d % 02d:%d % 03.2f % 03.2f",
        //   location[0],location[1],dt,dg,bdg,dt * Math.cos(rd),dt * Math.sin(rd));
        // System.out.println(loc);
        bdg -= dg;
      }

      System.out.println((int)x + "\n" +(int)y);
    }
    catch(IOException e) {
      System.err.println(e);
    }
  }
}


Measured memory_bytes: 24072
// A.

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[]) {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	if (a + b > c + d) {
		cout << "Left" << endl;
	} else if (a + b == c + d) {
		cout << "Balanced" << endl;
	} else {
		cout << "Right" << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <deque>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <locale>
#include <iomanip>
#include <math.h>

#define nl '\n'

using namespace std;

string s, t;

int main(){

    cin >> s >> t;

    vector<string> v;

    v.push_back(t);
    v.push_back(s);

    for(int i = 0; i < v.size(); i++){
        cout << v[i];
    }









}

//g++ -std=c++14 comp.cpp -o b; ./b.exe

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

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

#define LL long long int
#define LD long double

#define INF 1000000000
#define PI 3.14159265358979323846

#define _CRT_SECURE_NO_WARNINGS

using namespace std;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<LL, LL> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<LL> vll;

//==============================================

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

	priority_queue<LL> pq;

	string s;
	int v;
	while (cin >> s) {
		if (s == "end") break;

		if (s == "insert") {
			cin >> v;
			pq.push(v);
		} else {
			cout << pq.top() << "\n";
			pq.pop();
		}
	}
	return 0;
}

Measured memory_bytes: 11188
#include<stdio.h>
#define MAX 100001
 
int Partition(int*,int,int);
 
int main(void){
  int i,n,p,q;
  int A[MAX];
 
  scanf("%d", &n);
  for(i=0;i<n;i++) scanf("%d", &A[i]);
 
  p=Partition(A,0,n-1);
 
  for(i=0;i<n;i++){
    if(i==p) printf("[%d] ", A[i]);
    else if(i==n-1) printf("%d\n", A[i]);
    else printf("%d ", A[i]);
  }
 
  return 0;
}
 
int Partition(int *A,int p, int r){
  int i,j,tmp,x;
 
  x=A[r];
 
  i=p-1;
  for(j=p;j<=r-1;j++){
    if(A[j]<=x){
      i++;
      tmp=A[i];
      A[i]=A[j];
      A[j]=tmp;
    }
  }
 
  tmp=A[i+1];
  A[i+1]=A[r];
  A[r]=tmp;
 
  return i+1;
 
}

Measured memory_bytes: 992
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
  int a, b, i, len, position, count;
  char s[10];

  while (scanf("%d %d\n%s", &a, &b, s) != EOF) {
    count = 0;
    len = strlen(s);

    if (a + b + 1 != len) {
      puts("No");
      continue;
    }

    for (i = 0; i < len; i++) {
      if (s[i] == '-') {
        count++;
        position = i;
      }
    }
    if (count != 1 || position != a) {
      puts("No");
    } else {
      puts("Yes");
    }
  }

  return 0;
}


Measured memory_bytes: 128
def solve(h):
    dp = {0: 0, 1: abs(h[0] - h[1])}
    for i in range(2, len(h)):
        dp[i] = min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1]))
    return dp[len(h)-1]

N = int(input())
h = list(map(int, input().split()))
print(solve(h))

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

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

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

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 24;

int mns(int val){
    if(val == 0) val = 2;
    else if(val == 2) val = 0;

    return val;
}

int mlt(int lhs, int rhs){
    return min(lhs, rhs);
}

int pls(int lhs, int rhs){
    return max(lhs, rhs);
}

int parse(string & str, int & pos){
    int ret;

    if(str.at(pos) == '('){
        pos++;

        int lhs = parse(str, pos);
        char op = str.at(pos); pos++;
        int rhs = parse(str, pos);

        switch(op){
            case '+': { ret = pls(lhs, rhs); break; }
            case '*': { ret = mlt(lhs, rhs); break; }
            default: break;
        }

        pos++;
    }
    else if(str.at(pos) == '-'){
        int cnt = 0;
        while(str.at(pos) == '-'){
            cnt++;
            pos++;
        }

        ret = parse(str, pos);
        if(cnt % 2 == 1) ret = mns(ret);
    }
    else {
        ret = str.at(pos) - '0';
        pos++;
    }

    return ret;
}

int main(void){
    for(string str_org; cin >> str_org, str_org != ".";){
        int res = 0;

        rep(p, 3){
            rep(q, 3){
                rep(r, 3){
                    string str = str_org;

                    for(auto && c : str){
                        switch(c){
                            case 'P': { c = p + '0'; break; }
                            case 'Q': { c = q + '0'; break; }
                            case 'R': { c = r + '0'; break; }
                            default: break;
                        }
                    }

                    int pos = 0;
                    if(parse(str, pos) == 2){
                        res++;
                    }
                }
            }
        }

        cout << res << endl;
    }

	return 0;
}

Measured memory_bytes: 1200
import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
void main()
{
    immutable buf = readln.chomp.split.to!(int[]);
    auto p = readln.chomp.split.to!(int[]);
    p.sort();
    p.take(buf[1]).sum.writeln;
}


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

#define trace(...) __f( #__VA_ARGS__ , __VA_ARGS__ )
template <typename Arg1>
void __f( const char* name , Arg1&& arg1 ) {
        cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f( const char* na , Arg1&& arg1 , Args&&... args ) {
        const char* c = strchr( na + 1 , ',' );
        cerr.write( na , c - na ) << " : " << arg1 << " , ";
        __f( c + 1 , args... );
}
#define endl "\n"
#define all(V) V.begin(),V.end()
#define Unique(V) sort(all(V)),V.erase(unique(all(V)),V.end())
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long int LL;
typedef pair <int,int> pii;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const long long INF = 1e18;
const int N = 1e5 + 5;

int main( ) {
        IO;
        #ifdef LOCAL
            //freopen( "input.txt" , "r" , stdin );
        #endif // LOCAL
        string s;
        cin >> s;
        int l = s.size();
        int ans = 0;
        for( int i = 0; i < l/2; i++ ) {
                if( s[i] != s[l - i - 1] ) ans++;
        }
        cout << ans << endl;
        return 0;
}










Measured memory_bytes: 256
#include<stdio.h>
#define MAX 45

int F[MAX];

int Fibonacci(int n){
  int i;
  if(n==0||n==1) return F[n]=1;
  if(F[n]>0) return F[n];
  return F[n]= Fibonacci(n-2) + Fibonacci(n-1);
}

int main(){
  int i,n;
  
  scanf("%d",&n);
  F[n]=Fibonacci(n);
  printf("%d\n",F[n]);
  
  return 0;
}

Measured memory_bytes: 596
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
typedef long long ll;

int main() {
    int N; cin >> N;
    vector<int> A(N), B(N);
    for(int i = 0; i < N; ++i) cin >> A[i];
    for(int i = 0; i < N; ++i) cin >> B[i];
    vector<int> C(N), D(N);
    for(int i = 0; i < N; ++i) C[i] = A[i] ^ A[(i + 1) % N];
    for(int i = 0; i < N; ++i) D[i] = B[i] ^ B[(i + 1) % N];
    // find D in C * 2
    for(int i = 0; i < N; ++i) {
        C.push_back(C[i]);
    }
    // KMP algorithm
    vector<int> fail(N, 0);
    for(int i = 1, j = 0; i < N; ++i) {
        while(j > 0 && D[i] != D[j]) j = fail[j - 1];
        if(D[i] == D[j]) fail[i] = ++j;
    }
    vector<int> ret;
    for(int i = 0, j = 0; i < 2 * N; ++i) {
        while(j > 0 && C[i] != D[j]) j = fail[j - 1];
        if(C[i] == D[j]) {
            if(j == N - 1) {
                ret.push_back(i - N + 1);
                j = fail[j];
            } else ++j;
        }
    }
    for(auto &k : ret) {
        if(k >= N) continue;
        int x = A[k] ^ B[0];
        cout << k << " " << x << "\n";
    }    
}


Measured memory_bytes: 9968
from itertools import accumulate
h,w,k=map(int,input().split())
s=[[0]*(w+1)]+[[0]+[int(i) for i in input()] for _ in range(h)]
for i in range(h+1):
  s[i]=list(accumulate(s[i]))
for j in range(w+1):
  for i in range(h):
    s[i+1][j]+=s[i][j]
ans=10**18
for i in range(1<<h-1):
  a=[]
  for j in range(h-1):
    if (i>>j)&1:a+=[j+1]
  a+=[h]
  cnt=len(a)-1
  q=1
  for j in range(1,w+1):
    p=0
    flag=0
    for l in a:
      if s[l][j]-s[l][j-1]-s[p][j]+s[p][j-1]>k:flag=1
      elif s[l][j]-s[l][q-1]-s[p][j]+s[p][q-1]>k:
        q=j
        cnt+=1
        break
      else:p=l
    if flag:break
  else:
    ans=min(cnt,ans)
print(ans)

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

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++) 
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if(i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);

const int N = 3000 + 7;

int n;
int a[N];

bool read () {
	if (scanf("%d", &n) != 1)
		return false;
	forn(i, n)
		scanf("%d", &a[i]);
	return true;
}

li dp[N][N];

li calc(int l, int r, int st){
	if (l > r)
		return 0;
	if (dp[l][r] != INF64)
		return dp[l][r];
	if (st)
		return dp[l][r] = max(a[l] + calc(l + 1, r, st ^ 1), a[r] + calc(l, r - 1, st ^ 1));
	else
		return dp[l][r] = min(-a[l] + calc(l + 1, r, st ^ 1), -a[r] + calc(l, r - 1, st ^ 1));
}

void solve() {	
	forn(i, n) forn(j, n) dp[i][j] = INF64;
	printf("%lld\n", calc(0, n - 1, 1));
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
	
	int tt = clock();
	
#endif
	
	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

	while(read()) {	
		solve();
		
#ifdef _DEBUG
	cerr << "TIME = " << clock() - tt << endl;
	tt = clock();
#endif

	}
}

Measured memory_bytes: 70912
using System;
using System.Linq;

class Probrem {
    public static void Main() {
        var data = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var d = data[0] / data[1];
        var r = data[0] % data[1];
        var f = (decimal)data[0] / data[1];
        Console.WriteLine(d + " " + r + " " + f);
    }
}


Measured memory_bytes: 9644
import heapq
def main():
    n = int(input())
    a = [int(t)for t in input().split()]

    a = sorted(a)
    ans = 0

    paired = []

    prev = a.pop()
    temp = a.pop()
    heapq.heappush(paired,(-temp,-prev))
    heapq.heappush(paired,(-temp,-prev))
    ans += prev

    
    for _ in range(n-2):
        temp = a.pop()
        pair = heapq.heappop(paired)

        ans -= pair[0]
        heapq.heappush(paired,(-temp,pair[0]))
        heapq.heappush(paired,(-temp,pair[1]))

    print(ans)

if __name__ == "__main__":
    main()

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

int main() {
    int a,b,k;cin>>a>>b>>k;
    int cc = 0;
    for(int i=min(a,b);i>=1;--i){
        if(b%i==0 && a%i==0)++cc;
        if(cc==k){
            cout<<i<<endl;
            return 0;
        }
    }
	return 0;
}

Measured memory_bytes: 3636
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

bool check(const string& s, const string& t) {
  map<char, char> TtoS, StoT;
  for (ll i = 0; i < t.size(); i++) {
    const char tt = t[i];
    const char ss = s[i];
    try {
      auto conv = TtoS.at(tt);
      if (conv != ss) {
        return false;
      }
    } catch (...) {
      TtoS[tt] = ss;
    }

    try {
      auto conv = StoT.at(ss);
      if (conv != tt) {
        return false;
      }
    } catch (...) {
      StoT[ss] = tt;
    }
  }
  return true;
}

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

  string ans = "No";
  if (check(S, T)) {
    ans = "Yes";
  }

  cout << ans << endl;
  return 0;
}


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

int main() {
    int t = 0, n, m;
    cin >> n >> m;
    t = (n-m)*100 + m*1900;
    while (m > 0) {
        t *= 2;
        m--;
    }
    cout << t << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		if ( A+B >=10 ) System.out.println("error");
		else System.out.println(A+B);
	}
}


Measured memory_bytes: 21844
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

int main(){
    long long n, a, b;
    cin >> n >> a >> b;
    long long ans = (a + b * (n - 1)) - (a * (n - 1) + b) + 1;
    cout << (ans < 0 ? 0 : ans) << endl;
}


Measured memory_bytes: 256
N = int(input())
S = list(input())
out = []
for i in range(N):
    dic = {}
    for j in range(N):
        if (j <= i):
            dic[S[j]] = 0
        else:
            if S[j] in dic:
                dic[S[j]] = 1
        a = 0
        for it in dic.values():
            a+=it
        out.append(a)

print(max(out))

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

struct Node{
    int key;
    Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(int k){
    Node *x = root;
    Node *y = NIL;
    Node *z;

    z = (Node *)malloc(sizeof(Node));
    z->key = k;
    z->right = NIL;
    z->left = NIL;

    while (x != NIL){
        y = x;
        if (z->key < x->key) x = x->left;
        else x = x->right;
    }
    z->parent = y;
    if (y == NIL) root = z;
    else if (z->key < y->key) y->left = z;
    else y->right = z;
}

Node* find(int k){
    Node *x = root;
    while(x != NIL){
        if (k == x->key) return x;
        else if (k < x->key) x = x->left;
        else x = x->right;
    }
    return NIL;
}

Node* getMinimum(Node *x){
    while(x->left != NIL){
        x = x->left;
    }
    return x;
}

Node* getSuccessor(Node *x){
    if (x->right != NIL) return getMinimum(x->right);
    
    Node *y = x->parent;
    while(y != NIL && x == y->right){
        x = y;
        y = y->parent;
    }
    return y;
}

void deleteNode(int k){
    Node *z = find(k);
    Node *y;
    Node *x;

    if (z == NIL) return;

    if (z->left == NIL || z->right == NIL) y = z;
    else y = getSuccessor(z);

    if (y->left != NIL) x = y->left;
    else x = y->right;

    if (x != NIL) x->parent = y->parent;

    if (y->parent == NIL) root = x;
    else if (y == y->parent->left) y->parent->left = x;
    else y->parent->right = x;

    if (y != z) z->key = y->key;

    free(y);
}

void inorder(Node *u){
    if (u == NULL) return;
    inorder(u->left);
    cout << " " << u->key;
    inorder(u->right);
}

void preorder(Node *u){
    if (u == NULL) return;
    cout << " " << u->key;
    preorder(u->left);
    preorder(u->right);
}

int main(void)
{
    int n;
    string s;
    cin >> n;
    rep(i,n){
        cin >> s;
        if (s == "print"){
            inorder(root); cout << endl;
            preorder(root); cout << endl;
        }
        else{
            int key; cin >> key;
            switch(s[0]){
                case 'i':
                    insert(key);
                    break;
                case 'f':
                    if (find(key) != NIL) cout << "yes" << endl;
                    else cout << "no" << endl;
                    break;
                case 'd':
                    deleteNode(key);
                    break;
                default:
                    break;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 21820
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-8;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int main() {
    ll n, k, ans=0, cnt[MAX_N]{}, ma[MAX_N]{};
    cin >> n >> k;
    priority_queue<P> que;
    REP(i,n){
        ll t, d;
        cin >> t >> d;
        ans += d;
        cnt[t]++;
        ma[t] = max(ma[t],d);
        que.push({-d,t});
    }
    ll x=0;
    REP(i,n+1){
        if(cnt[i]>0){
            x++;
        }
    }
    ans += x*x;
    while(que.size()>k){
        P p = que.top();
        ll d = p.first, t = p.second;
        que.pop();
        if(cnt[t]==1){
            d = -ma[t] - (x*2-1);
        }
        if(que.top().first <= d){
            ans += d;
            cnt[t]--;
            if(cnt[t]==0) x--;
        }
        else{
            que.push({d,t});
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3956
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
#include<stdio.h>
using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    int a[n][m];
    for(int i = 0; i < n; i++){
        for (int j = 0; j < m ;j++){
            int x;
            cin >> x;
            a[i][j] = x;

        }
    }
    int b[m];
    for (int j = 0; j < m ;j++){
        int x;
        cin >> x;
        b[j] = x;

    }

    for(int i = 0; i < n; i++){
        int sums = 0;
        for (int j = 0; j < m ;j++){
            sums += a[i][j] * b[j];
        }
        cout << sums << endl;
    }
}


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

while True:
   n = int(input())
   if n == 0: break
   
   c = 0
   while n != 1:
      if n % 2 == 0:
         n /= 2
      else:
         n = n * 3 + 1
      c += 1
   print(c)


Measured memory_bytes: 5596
#include <cstdio>

int main(){
    int min,max_s=-1000000001,n,r;
    
    scanf("%d",&n);
    scanf("%d",&min);
    for(int i=0;i<n-1;i++){
        scanf("%d",&r);
        if(r-min>max_s) max_s=r-min;
        if(min>r)min =r;
    }
    
    printf("%d\n",max_s);
    
    return 0;
}



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

int main() {
  int X, Y;
  cin >> X >> Y;

  if (X % Y == 0)
    cout << -1 << endl;
  else
    cout << X << endl;
  return 0;
}

Measured memory_bytes: 3604
#include<iostream>
#include<algorithm>
using namespace std;
int n,d[200010];
pair<long long,int> P[200010];
int bit[200010];
int Sum(int x){
	int ret=0;
	while(x)ret+=bit[x],x-=x&-x;
	return ret;
}
void Add(int x,int a){
	while(x<=n+1)bit[x]+=a,x+=x&-x;
}
int main(){
	long long k,a,sum=0;
	cin>>n>>k>>a;sum=a-k;
	P[0]=make_pair(0,0),P[1]=make_pair(sum,1);
	for(int i=2;i<=n;i++){
		cin>>a;sum+=a-k;
		P[i]=make_pair(sum,i);
	}
	sort(P,P+n+1);
	for(int i=0;i<=n;i++)d[P[i].second]=i;
	for(int i=1;i<=n+1;i++)bit[i]=0;
	long long ans=0;
	for(int i=0;i<=n;i++){
		ans+=Sum(d[i]+1);
		Add(d[i]+1,1);
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 4992
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define Rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
typedef long long ll;

ll gcd(ll a, ll b) {
    if (a % b == 0) {
        return (b);
    } else {
        return (gcd(b, a % b));
    }
}

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

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

    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    ll countB = b - (b / c + b / d - b / lcm(c, d));
    ll countA = a - (a / c + a / d - a / lcm(c, d));
    cout << countB - countA << endl;
}

Measured memory_bytes: 3608
extern crate core;

use std::io::{self, Read};

#[macro_export]
macro_rules! input {
  ($s:expr=>$($t:tt)*) => {
    let mut lines=$s.split("\n");
    $(
        line_parse!(lines,$t);
    )*
  };
}

macro_rules! line_parse {
  ($lines:expr,($($name:ident:$t:tt)*)) => {
    let mut line=$lines.next().unwrap().split_whitespace();
    $(value_def!(line,$name,$t);)*
  };

  //複数行
  ($lines:expr,{$n:expr;$name:ident:$t:tt}) => {
    values_def!($lines,$n,$name,$t);
  };
}

macro_rules! value_def {
  ($line:expr, $name:ident, $t:tt) => {
    let $name = value!($line, $t);
  };
}

macro_rules! values_def {
  ($lines:expr, $n:expr, $name:ident, $t:tt) => {
    let $name = {
      let mut vec = Vec::new();
      for i in 0..$n {
        let mut next = $lines.next().unwrap().split_whitespace();
        vec.push(value!(next, $t));
      }
      vec
    };
  };
}

macro_rules! value {
  //配列
  ($line:expr,[$t:tt]) => {
    $line.map(|x|{
      let mut iter=::std::iter::once(x);
      value!(iter,$t)
    }).collect::<Vec<_>>()
  };
  //タプル
  ($line:expr,($($t:tt),*)) => {
    ($(value!($line,$t),)*)
  };
  //文字列
  ($line:expr,#) => {
    $line.next().unwrap()
  };
  //インデックス(-1)
  ($line:expr,@) => {
    $line.next().unwrap().parse::<usize>().unwrap()-1
  };
  //単一値
  ($line:expr,$t:ty) => {
    $line.next().unwrap().parse::<$t>().unwrap()
  };
}

fn main() {
  let mut input = String::new();
  io::stdin().read_to_string(&mut input).unwrap();
  let output = solve(input.trim().to_string());
  println!("{}", output);
}

/*
0-5から2つ選ぶ時
最も大きい2つ-最も小さい2つ+1
n+(n-1) - 0+1 + 1

9-1+1

0 1
0 2
0 3
0 4
0 5

1 5
2 5
3 5
4 5

0-5から3つ選ぶ時
最も小さい3つから最も大きい3つ

0 1 2 - 3
0 1 3 - 4
0 1 4 - 5
0 1 5 - 6

1 2 5 - 7
1 3 5 - 8
1 4 5 - 9

2 3 5 - 10
*/

fn solve(input: String) -> String {
  input!(input=>(n:i64 k:i64));
  let mut result = 0;
  for x in k..=n + 1 {
    result = add(result, (x * n - x * (x - 1) / 2) - x * (x - 1) / 2 + 1);
  }
  result.to_string()
}
const MOD: i64 = 1000000007;

pub fn add(a: i64, b: i64) -> i64 {
  (a + b) % MOD
}

macro_rules! tests {
    ($($name:ident: $input:expr=>$output:expr,)*) => {
        mod tests {
            $(
                #[test]
                fn $name() {
                    assert_eq!($output.trim().to_string(),super::solve($input.trim().to_string()));
                }
            )*
        }
    }
}
tests! {
  test1: "3 2\n" => "10\n",
  test2: "200000 200001\n" => "1\n",
  test3: "141421 35623\n" => "220280457\n",
}


Measured memory_bytes: 2132
use std::cmp::max;


fn main() {
    let (n, mut h) = read_tuple::<i64>();
    let mut katanas = Vec::new();
    let mut max_a = 0;
    for _ in 0..n {
        let (a, b) = read_tuple::<i64>();
        max_a = max(max_a, a);
        katanas.push((a, b));
    }
    katanas.sort_by_key(|&(_, b)| b);
    katanas.reverse();

    let mut count = 0;
    for &(_, b) in katanas.iter().filter(|&&(_, b)| max_a < b) {
        h -= b;
        count += 1;
        if h <= 0 {
            break;
        }
    }
    let rem = if h > 0 {
        h / max_a + if h % max_a != 0 { 1 } else { 0 }
    } else {
        0
    };
    println!("{:?}", count + rem);
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}
fn read_tuple<T: std::str::FromStr + Copy>() -> (T, T) {
    let temp = read_vec();
    (temp[0], temp[1])
}



Measured memory_bytes: 6396
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(10**5+1)]
for i in a:
    d[i] += 1
ans = sum(a)
q = int(input())
for i in range(q):
    b,c = map(int,input().split())
    ans += d[b]*(c-b)
    d[b],d[c] = 0,d[b]+d[c]
    print(ans)

Measured memory_bytes: 20988
#include <bits/stdc++.h>
using namespace std;
#define INF (1<<30)
#define MAX_N 100
typedef pair<int, int> P;

struct edge{
    int v[2], cost;
    edge(){}
    edge(int v0, int u0, int cost0){
        v[0] = v0; v[1] = u0; cost = cost0;
    }
    bool operator < (const edge &e) const {
        return cost > e.cost;
    }
};

int ans = 0, N;
vector<edge> E[MAX_N];

void prim(){
    bool seen[N];
    fill(seen, seen + N, false);
    priority_queue<edge> todo;
    seen[0] = true;
    for(int i = 0; i < E[0].size(); i++)
        todo.push(E[0][i]);
    while(!todo.empty()){
        edge e = todo.top(); todo.pop();
        if(seen[e.v[1]]) continue;
        ans += e.cost;
        seen[e.v[1]] = true;
        for(int i = 0; i < E[e.v[1]].size(); i++)
            if(!seen[E[e.v[1]][i].v[1]]) todo.push(E[e.v[1]][i]);
    }
}

int main(){
    cin >> N;
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++){
            int cost;
            cin >> cost;
            if(i < j && cost != -1){
                E[i].push_back(edge(i, j, cost));
                E[j].push_back(edge(j, i, cost));
            }
        }
    prim();
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3204
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define pb push_back 
#define all(c) (c).begin(),(c).end()

string s;
int main(){
	#ifndef ONLINE_JUDGE
	//	freopen("input.txt", "r", stdin);
	#endif
	cin>>s;
	cout<<s.substr(0,s.size()-8);
	
}




Measured memory_bytes: 256
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>
#include <iostream>
#include <iterator>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const double EPS = 1e-8;

int main()
{
	int n;
	while(scanf("%d", &n), n){
		int id = -1;
		double diff;
		rep(i, n){
			int i, h, w;
			scanf("%d%d%d", &i, &h, &w);
			double H = h / 100.0;
			double d = fabs(22.0 - w / H / H);
			
			if(id == -1){
				diff = d;
				id = i;
			}
			else if(fabs(diff - d) < EPS)
				id = min(id, i);
			else if(diff > d){
				diff = d;
				id = i;
			}
		}
		
		printf("%d\n", id);
	}
	
	return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <iomanip>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);++i)
#define rep(i,a,b) for(int i=a;i<(b);++i)
template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;}
using ll = long long;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
 
 
int N;
double x[100010],y[100010];
char d[100010];
 
double C(double n){
    double ret = 0.0;
    double mnx = 1e18;
    double mny = 1e18;
    double mxx = -1e18;
    double mxy = -1e18;
    REP(i,N){
        if(d[i]=='R'){
            chmax(mxx,x[i]+n);
            chmin(mnx,x[i]+n);
            chmax(mxy,y[i]);
            chmin(mny,y[i]);
        }
        if(d[i]=='L'){
            chmax(mxx,x[i]-n);
            chmin(mnx,x[i]-n);
            chmax(mxy,y[i]);
            chmin(mny,y[i]);
        }
        if(d[i]=='U'){
            chmax(mxx,x[i]);
            chmin(mnx,x[i]);
            chmax(mxy,y[i]+n);
            chmin(mny,y[i]+n);
        }
        if(d[i]=='D'){
            chmax(mxx,x[i]);
            chmin(mnx,x[i]);
            chmax(mxy,y[i]-n);
            chmin(mny,y[i]-n);
        }
    }
    ret = 1.*(mxx-mnx) * (mxy-mny);
    return ret;
}
 
int main() {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cin>>N;
    REP(i,N) cin >> x[i] >> y[i] >> d[i];
    double ans = 1e18;
    double l = 0.0, r = 1e9;
    for(int i = 0; i < 200; ++i){
        double tl = (l*2 + r) / 3;
        double tr = (l + r*2) / 3;
        double vl = C(tl);
        double vr = C(tr);
        chmin(ans,vl);
        chmin(ans,vr);
        if(vr < vl){
            l = tl;
        } else {
            r = tr;
        }
    }
    cout << fixed << setprecision(15) << ans << endl;
    return 0;
}

Measured memory_bytes: 1920
#include<iostream>
#include<algorithm>
using namespace std;
static const int MAX = 40;

int n;
struct Card {char suit, value;};

void bubble(struct Card A[]){
    for (int i = 0; i < n; i++){
        for (int j = n - 1; j > i; j--){
            if (A[j].value < A[j - 1].value) swap(A[j], A[j - 1]);
        }
    }
}

void selection(struct Card A[]){
    int min;
    for (int i = 0; i < n; i++){
        min = i;
        for (int j = i + 1; j < n; j++){
            if (A[min].value > A[j].value) min = j;
        }
        swap(A[min], A[i]);
    }
}

void print(struct Card A[]){
    for (int i = 0; i < n; i++){
        if (i > 0) cout << " ";
        cout << A[i].suit << A[i].value;
    }
    cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[]){
    for (int i = 0; i < n; i++){
        if (C1[i].suit != C2[i].suit) return false;
    }
    return true;
}

int main(){
    Card C1[MAX], C2[MAX];

    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> C1[i].suit >> C1[i].value;
    }

    for (int i = 0; i < n; i++) C2[i] = C1[i];

    bubble(C1);
    selection(C2);
    print(C1);
    cout << "Stable" << endl;
    print(C2);
    if (isStable(C1, C2)){
        cout << "Stable" << endl;
    }else{
        cout << "Not stable" << endl;
    }
    return 0;
}


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

int main(){
    int n;
    cin >> n;
    int sum = 0;
    vector<int> s(n);
    rep(i,n){
        cin >> s[i];
        sum += s[i];
    }
    int m;
    cin >> m;
    vector<pii> t(m);
    rep(i,m){
        int p,x;
        cin >> p >> x;
        t[i].first = p;
        t[i].second = x;
    }
    rep(i,m){
        cout << sum - s[t[i].first-1] + t[i].second << endl;
    }
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
using namespace std;
void kouri(int, int,int);
int  sum_max = 0;
int c[90][90];
int c2[90][90] = { 0 };
int a, b;
int nx[] = { 1,0,-1,0 };
int ny[] = { 0,-1,0,1 };
int main() {
	while (1) {
		sum_max = 0;
		cin >> a;
		cin >> b;
		if (a == 0 && b == 0)return 0;
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				cin >> c[i][j];
			}
		}
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				if (c[i][j] == 1) {
					kouri(i, j, 0);
				}
			}
		}
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				c[i][j] = 0;
			}
		}
		cout << sum_max << endl;
	}
}
void kouri(int x, int y, int sum) {
	/*for (int i = 0; i < a; i++) {
		for (int j = 0; j < b; j++) {
			cout << c[i][j]<<" ";
		}
		cout << endl;
	}
	cout << endl << "--------------------------------" << endl;*/
	c[x][y] = 0;
	sum++;
	if (c[x][y + 1] == 1)kouri(x, y + 1, sum);
	if (c[x][y - 1] == 1)kouri(x, y - 1, sum);
	if (c[x + 1][y] == 1)kouri(x + 1, y, sum);
	if (c[x - 1][y] == 1)kouri(x - 1, y, sum);
	c[x][y] = 1;
	if (sum > sum_max)sum_max = sum;
}

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

// extgcd(a, b, &x, &y): a, b(a = qb + r ---(1))が与えられるとき、ax + by = 1 ---(2)となる(x, y)を求める
// (2)に(1)を代入したとき、(qb + r)x + by = 1 ⇔ b(qx + y) + rx = 1
// ここで、extgcd(b, a % b, &s, &t)、すなわち、bs + rt = 1が成り立つとき
// s = qx + y, t = x ⇔ x = t, y = s - qx = s - a/b * x

long long extgcd(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extgcd(b, a%b, y, x);
    y -= a / b * x;
    return d;
}

int main(){
    long long a, b, x, y; cin >> a >> b;
    extgcd(a, b, x, y);
    cout << x << " " << y << endl;
    return 0;
}


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

int main(void){
    int N;
    long long H, W;
    cin >> N >> H >> W;
    int answer = 0;
    for(int cnt = 0; cnt < N; cnt++){
        long long A, B;
        cin >> A >> B;
        if( A >= H && B >= W){
            answer++;
        }
    }
    cout << answer << endl;

    return 0;
}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;



int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ios_base& scientific (ios_base& str);

int n , k;
cin >> n >> k;
int arr[n];

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

int dp[n+1];
for(int i=1;i<=n;i++){
	dp[i]= 1e9;
}
dp[0]= 0;

for(int i=1;i<n;i++){
	for(int j=1;j<=k;j++){
		if(i-j>=0)
		dp[i]=min(dp[i], dp[i-j]+abs(arr[i]-arr[i-j]));
	}
}
cout << dp[n-1] << endl;

return 0;
}



Measured memory_bytes: 1024
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// IN THE NAME OF GOD
#include <bits/stdc++.h>

#define ff first
#define endl '\n'
#define in insert
#define ss second
#define ll long long
#define pb push_back
#define ld long double
#define pii pair <int, int>
#define pll pair <long long, long long>

using namespace std;

const int mod = 1e9 + 7, N = 2222, MAXN = 4e6 + 46;
ll n, k, dp[N][N], fact[MAXN], inv[MAXN], f[MAXN];

inline int C(int x, int y){
  return fact[x] * inv[y] % mod * inv[x - y] % mod;
}

int main(){
  //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  if(k == 1) return cout << 1, 0;
  fact[0] = inv[0] = inv[1] = f[0] = f[1] = 1;
  for(int i = 1; i < MAXN; i++){
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
  for(int i = 2; i < MAXN; i++){
    f[i] = (mod - mod / i) * f[mod % i];
    f[i] %= mod;
    inv[i] = f[i] * inv[i - 1];
    inv[i] %= mod;
  }
  dp[0][0] = 1;
  for(int i = 1; i <= n; i++){
    for(int j = i; j >= 1; j--){
      dp[i][j] = dp[i][j + 1];
      dp[i][j] += dp[i - 1][j - 1] * (i * k - j - 1 < k - 2 ? 0 : C(i * k - j - 1, k - 2));
      dp[i][j] %= mod;
    }
    dp[i][0] = dp[i][1];
  }
  cout << (dp[n][0] * fact[n]) % mod;
}
// programmer: a_h_k_81
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //


Measured memory_bytes: 129280
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <iomanip>

#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

using namespace std;

typedef pair<int, int> P;

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

template <typename T>
using vector2 = vector<vector<T>>;
template <typename T>
vector2<T> initVec2(size_t n0, size_t n1, T e = T()){
    return vector2<T>(n0, vector<T>(n1, e));
}

template <typename T>
using vector3 = vector<vector<vector<T>>>;
template <typename T>
vector3<T> initVec3(size_t n0, size_t n1, size_t n2, T e = T()){
    return vector3<T>(n0, vector2<T>(n1, vector<T>(n2, e)));
}

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

    int n, k;
    cin >> n >> k;

    if(k == 1){
        cout << 0 << endl;
        return 0;
    }
    cout << n - k << endl;

    return 0;
}


Measured memory_bytes: 888
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class Main {

    private static final int MOD = (int) (1e9 + 7);

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final String s = fs.next();
        System.out.println(dfs(s.toCharArray(), 0, 0, new Long[s.length()][4]));
    }

    private static long dfs(char[] s, int i, int j, Long[][] dp) {
        if (i == s.length) {
            return j == 3 ? 1 : 0;
        }
        if (dp[i][j] != null) {
            return dp[i][j];
        }
        long res = dfs(s, i + 1, j, dp);
        if (s[i] == '?') {
            res = (res * 3) % MOD;
        }
        if (j < 3 && (s[i] == '?' || s[i] == "ABC".charAt(j))) {
            res = (res + dfs(s, i + 1, j + 1, dp)) % MOD;
        }
        return dp[i][j] = res;
    }

    static final class Util {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Util() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

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


Measured memory_bytes: 76768
#include<bits/stdc++.h>
using namespace std ;
typedef long long int ll;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t; //cin>>t;
    t=1;
    while(t--)
    {
        ll n,k;
        cin>>n;
        ll arr[n+1][4]={0};
        for(ll i=1;i<=n;i++)
            for(ll j=1;j<=3;j++)
                cin>>arr[i][j];
        ll dp1[n+1]={0},dp2[n+1]={0},dp3[n+1]={0};
        for(ll i=1;i<=n;i++)
        {
            dp1[i]=max(dp2[i-1],dp3[i-1])+arr[i][1];
            dp2[i]=max(dp1[i-1],dp3[i-1])+arr[i][2];
            dp3[i]=max(dp1[i-1],dp2[i-1])+arr[i][3];
        }
        cout<<max(max(dp1[n],dp2[n]),dp3[n])<<endl;
    }
    return 0;
}


Measured memory_bytes: 5760
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int dp[MAXN];
int arr[MAXN];
int main(){
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> arr[i];
    dp[0] = 0;
    dp[1] = abs(arr[1] - arr[0]);
    
    for(int i = 2; i < n; i++){
        dp[i] = min((dp[i - 1] + abs(arr[i] - arr[i - 1])), (dp[i - 2] + abs(arr[i - 2] - arr[i])));
    }
    //for(int i = 0; i < n; i++) cout << i << " : " << dp[i] << '\n';
    cout << dp[n - 1] << '\n';
}

Measured memory_bytes: 1024
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

import static java.lang.Integer.parseInt;

/**
 * Princess's Marriage - Accepted
 */
public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;

		while ((line = br.readLine()) != null && !line.isEmpty()) {

			int N, M;
			N = parseInt(line.substring(0, line.indexOf(' ')));
			M = parseInt(line.substring(line.indexOf(' ') + 1));
			if ((N | M) == 0) break;

			int[][] dp = new int[N][3];
			int expected = 0;

			for (int i = 0; i < N; i++) {
				line = br.readLine();
				dp[i][0] = parseInt(line.substring(0, line.indexOf(' ')));
				dp[i][1] = parseInt(line.substring(line.indexOf(' ') + 1));
				dp[i][2] = dp[i][0] * dp[i][1];
				expected += dp[i][2];
			}

			Arrays.sort(dp, new Comparator<int[]>() {
				@Override
				public int compare(int[] o1, int[] o2) {
					return Integer.compare(o1[1], o2[1]);
				}
			});

			for (int i = N - 1; i >= 0; i--) {
				if (dp[i][0] <= M) {
					expected -= dp[i][2];
					M -= dp[i][0];
				} else {
					expected -= M * dp[i][1];
					M = 0;
				}
				if (M == 0) break;
			}

			System.out.println(expected);

		} //end while
	} //end main
}

Measured memory_bytes: 50020