akhauriyash/Code-Regression huggingface dataset:
# 2019/04/27
# AtCoder Beginner Contest 125 - D

# Input
n = int(input())
d = list(map(int,input().split()))

mcnt = 0
absmin = 10**10
abssum = 0

# Sum abs & Count minus & Search abs(min)
for i in range(n):
    abssum += abs(d[i])
    if d[i] < 0:
        mcnt += 1
    if absmin > abs(d[i]):
        absmin = abs(d[i])
if mcnt % 2 == 0:
    ans = abssum
else:
    ans = abssum - (absmin * 2)

# Output
print(ans)

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

int main() {
	int X,A,B;
    cin >> X >> A >> B;
    if(B-A <= 0){
      cout << "delicious" << endl;
    }
    else if(B-A <= X){
      cout << "safe" << endl;
    }
    else{
      cout << "dangerous" << endl;
    }
}


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

typedef long long ll;
typedef long double ld;

#define pb push_back
#define sort(s) sort(s.begin(),s.end())
#define reverse(s) reverse(s.begin(),s.end())
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)

const ll mod = 1e9+7;

//最大公約数
ll gcd(ll a, ll b) {
    return b ? gcd(b, a%b) : a;
}

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

//素数判定
bool isPrime(ll x){
    ll i;
    if(x < 2)return 0;
    else if(x == 2) return 1;
    if(x%2 == 0) return 0;
    for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0;
    return 1;
}

//桁和
int digsum(ll n) {
    int res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

//桁数
int dignum(ll n) {
    int res = 0;
    while(n > 0) {
        res++;
        n /= 10;
    }
    return res;
}

//文字列中の特定の文字カウント
ll stringcount(string s,char c){
    return count(s.cbegin(),s.cend(),c);
}

//階乗
ll ka(ll i){
    ll res=1;
    while(i>0){
        res=res*i;
        i--;
    }
    return res;
}

//ncr
ll ncr(ll x,ll y){
    ll a = x;
    ll b = 1;
    if(y>(x/2)){
        y=x-y;
    }
    for(ll i=1;i<y;i++){
        a*=x-i;
        b*=i+1;
    }
    return a/b;
}

//フィボナッチ数列
ll f(ll x){
    ll dp[x+1];
    dp[1]=1;
    dp[2]=1;
    for(ll i=3;i<=x;i++){
        dp[i]=dp[i-1]+dp[i-2];
    }
    return dp[x];
}

//char->int
int ctoi(char c){
    return c-'0';
}

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1) res.push_back({N, 1});
    return res;
}



int main(){
    int n;
    cin >> n;
    for(int i=1000;i<=10000;i+=1000){
        if(i>=n){
            cout << i-n << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 3628
#include<bits/stdc++.h>
//#incintude <ext/pb_ds/assoc_container.hpp> 
//#incintude <ext/pb_ds/tree_pointicy.hpp>
//#incintude <functionaint> // for intess
//using namespace __gnu_pbds;

#define max_(x, y) (((x) > (y)) ? (x) : (y))
#define min_(x, y) (((x) < (y)) ? (x) : (y))
using namespace std;
#define Mx 500001
#define inf 1e15

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/


//typedef tree<int,nuintint_type,intess<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

typedef int L;

//doubinte rat;
L A[Mx+5],D[Mx+5],block_size=320,cnt,ans[Mx],n,q,l,r,x,y;


//struct node{int nid,nv;node(int nid_,int nv_){nid=nid_;nv=nv_;}};
//vector<node>V;
struct info{
  L ind,Lid,Rid;
  info(L ind_,L Lid_,L Rid_){
    ind=ind_;
    Lid=Lid_;
    Rid=Rid_;
  }
};
vector<info>V;
bool cmp(info &lf,info &rt){
  L bl1=lf.Lid/block_size,bl2=rt.Lid/block_size;
  
  if(bl1==bl2){
    return lf.Rid<rt.Rid;
  }
  return bl1<bl2;
}
void del_(L id){
  D[A[id]]--;
  if(D[A[id]]==0)cnt--;
}
void add_(L id){
  D[A[id]]++;
  if(D[A[id]]==1)cnt++;
}
void solve(){

  sort(V.begin(), V.end(),cmp);
  L p1=0,p2=-1;
  cnt=0;
  for(L i=0;i<q;i++){
    L idx=V[i].ind,lid=V[i].Lid,rid=V[i].Rid;
    while(p1<lid){
      del_(p1);p1++;
    }
    while(p1>lid){
      p1--;
      add_(p1);
    }
    while(p2<rid){
      ++p2;
      add_(p2);
    }
    while(p2>rid){
      del_(p2);
      p2--;
    }
    ans[idx]=cnt;
  }
  for(L i=0;i<q;i++){
    printf("%d\n",ans[i]);
  }

}
int main()
{
    scanf("%d%d",&n,&q);
    block_size=sqrt(n);
    for(L i=0;i<n;i++){
        scanf("%d",&A[i]);
    }
    for(L i=0;i<q;i++){
      scanf("%d%d",&x,&y);
      V.push_back(info(i,x-1,y-1));
    }
    solve();
    return 0;
}
/// d d push_back() top() pop() insert() upper_bound() intower_bound() memset() max() min() scanf continue


Measured memory_bytes: 15616
def main():
    S = [s for s in input()]
    N = len(S)
    S.pop()
    S.pop()
    N -= 2
    while S and S[:N//2] != S[N//2:]:
        S.pop()
        S.pop()
        N -= 2
    print(N)


if __name__ == '__main__':
    main()


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

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> arr(n);
    for (auto &it: arr) {
        cin >> it;
    }
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            ans += arr[i] * arr[j];
        }
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    float l;
    scanf("%f",&l);
    printf("%f",(l/3)*(l/3)*(l/3));
    return 0;
}

Measured memory_bytes: 512
D,G = map(int,input().split())
X = [list(map(int,input().split())) for _ in range(D)]
ans = 0

for i in range(2**D):
	point = 0
	count = 0
	for j in range(D):
		if (i>>j)&1:
			point += 100*(j+1)*X[j][0]+X[j][1]
			count += X[j][0]
	if 	G > point:
		for k in range(D)[::-1]:
			if (i>>k)&1==0:
				if (G-point) <= 100*(k+1)*(X[k][0]-1):
					count += -((-(G-point)) // (100*(k+1)))
					break
				else:
					count = 10**5
					break
	if ans == 0 or count < ans:
		ans = count

print(ans)

Measured memory_bytes: 3064
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<functional>
#include<iomanip>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<utility>
#include<vector>

#define ll long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define REPR(i,n)   FORR(i,n,0)

using namespace std;

int isprime[100000] = { 0 };

void eratos() {
	REP(i, 100000) {
		isprime[i] = 1;
	}
	isprime[0] = 0;
	isprime[1] = 0;
	REP(i, 100000 - 1) {
		if (isprime[i]) {
			FOR(j, i + 1, 100000) {
				if (j%i == 0) {
					isprime[j] = 0;
				}
			}
		}
	}

}

int main() {
	int N, a[100000];
	cin >> N;

	eratos();
	int x = 2;
	REP(i, N) {
		while (!isprime[x]) {
			x += 5;
		}
		a[i] = x;
		x += 5;
	}

	REP(i, N - 1) {
		cout << a[i] << " ";
	}
	cout << a[N - 1] << endl;


	return 0;
}

Measured memory_bytes: 640
#include <cstdio>
#include <cstring>
char ss[510][20];
bool map[30][30];
bool v[30];
int main(){
	int n;
	while (scanf("%d", &n) && n){
		for (int i = 0; i < n; i ++){
			scanf("%s", ss[i]);
			for (int j = 0; ss[i][j]; j ++){
				ss[i][j] -= 'a' - 1;
			}
		}
		bool mark = false;
		memset(map, 0, sizeof(map));
		for (int i = 0; i < n; i ++)
			for (int j = i + 1; j < n; j ++){
				for (int k = 0; ss[i][k] || ss[j][k]; k ++)
					if (ss[i][k] != ss[j][k]){
						if (ss[i][k] && !ss[j][k])
							mark = true;
						if (ss[i][k] && ss[j][k])
							map[ss[i][k]][ss[j][k]] = 1;
						break;
					}
			}
		for (int k = 1; k <= 26; k ++)
			for (int i = 1; i <= 26; i ++)
				for (int j = 1; j <= 26; j ++)
					if (map[i][k] && map[k][j])
						map[i][j] = true;
		for (int i = 1; i <= 26; i ++)
			for (int j = 1; j <= 26; j ++)
				if (map[i][j] && map[j][i])
					mark = true;
		if (mark)
			puts("no");
		else
			puts("yes");
	}
	return 0;
}

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

int main(void){

    int H, W;

    scanf("%d%d", &H, &W);

    char S[H+2][W+3];

    char T[H+1][W+1];

    for(int i = 1; i <= H; i++){
        scanf("%s", T[i]);
    }

    for(int i = 0; i <= W+1; i++){
        S[0][i] = '.';
        S[H+1][i] = '.';
    }
    for(int i = 1; i <= H; i++){
        S[i][0] = '.';
        for(int j = 1; j <= W; j++){
            S[i][j] = T[i][j-1];
        }
        S[i][W+1] = '.';
    }

    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            if(S[i][j] == '#'){
                printf("#");
            }else{
                int c = 0;
                c += ((S[i-1][j-1] == '#') + (S[i-1][j] == '#') + (S[i-1][j+1] == '#') + (S[i][j-1] == '#') + (S[i][j+1] == '#') + (S[i+1][j-1] == '#') + (S[i+1][j] == '#') + (S[i+1][j+1] == '#'));
                printf("%d", c);
            }
        }
        printf("\n");
    }

    return 0;
}

Measured memory_bytes: 1716
n, x = map(int, input().split())
city = list(map(int, input().split()))
D = []

for i in range(n):
    dis = abs(x - city[i])
    D.append(dis)
    
def gdc(x, y):
    if x%y == 0:
        return y
    x, y = y, x%y
    return gdc(x, y)

for i in range(n-1):
    D[i + 1] = gdc(D[i], D[i + 1])

print(D[-1])

Measured memory_bytes: 14252
#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <limits.h>
//#include <math.h>

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  char g[h+2][w+2];
  for (int i=0; i<h+2; i++) g[i][0] ='.';
  for (int i=0; i<w+2; i++) {
    g[0][i] = '.';
    g[h+1][i] = '.';
  }
  for (int i=0; i<h; i++) {
    scanf("%s", g[i+1]);
  }
  for (int i=1; i<h+1; i++) {
    for (int j=1; j<w+1; j++) {
      if (g[i][j] != '#') continue;
      if (g[i][j-1] != '#' && g[i][j+1] != '#' && g[i-1][j] != '#' && g[i+1][j] != '#') {
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
  return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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;}
//snippets:tmp,cl,cvl,cs,co,coy,con,cov,setp
const ll mod=1e9+7;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll l;
    cin >> l;
    ll r;
    cin >> r;
    ll d;
    cin >> d;
    cout<<r/d-(l-1)/d<<endl;
}


Measured memory_bytes: 3632
import java.util.Scanner;
import java.util.*;

public class Main {
    static int cnt = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int x = Integer.parseInt(n);
        dfs("", n.length(), x);
        System.out.println(cnt);
    }
    public static void dfs(String s, int len, int n) {
        if (s.length() > len) {
            return;
        }
        if (s.length() != 0) {
            if (check(s) && Integer.parseInt(s) <= n) {
                //        System.out.println(s);
                cnt++;
            }
        }
        dfs(s + "3", len, n);
        dfs(s + "5", len, n);
        dfs(s + "7", len, n);
        return;
    }
    public static boolean check(String s) {
        boolean ok3 = false;
        boolean ok5 = false;
        boolean ok7 = false;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '3') {
                ok3 = true;
            } else if (s.charAt(i) == '5') {
                ok5 = true;
            } else if (s.charAt(i) == '7') {
                ok7 = true;
            } else {
                return false;
            }
        }
        if (ok3 && ok5 && ok7) {
            return true;
        }
        return false;
    }
}


Measured memory_bytes: 34644
#include <bits/stdc++.h>
#define INF 1e9
#define int long long
#define Rep(i, n, a) for (int i = a; i < n; i++)
#define Repr(i, n, a) for (int i = n; i >= a; i--)
#define rep(i, n) Rep(i, n, 0)
#define repr(i, n) Repr(i, n, 0)
#define all(a) a.begin(), a.end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

#define MAX_P 200010
int fact[MAX_P];

int extgcd(int a, int b, int &x, int &y)
{
    int d = a;
    if (b != 0)
    {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
    else
    {
        x = 1;
        y = 0;
    }
    return d;
}
int mod_inverse(int a, int m)
{
    int x, y;
    extgcd(a, m, x, y);
    return (m + x % m) % m;
}
void make_fact()
{
    fact[1] = 1;
    for (int i = 2; i < MAX_P; i++)
    {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    return;
}
int mod_fact(int n, int p, int &e)
{
    e = 0;
    if (n == 0)
        return 1;
    int res = mod_fact(n / p, p, e);
    e += n / p;
    if (n / p % 2 != 0)
        return res * (p - fact[n % p]) % p;
    return res * fact[n % p] % p;
}
int mod_comb(int n, int k, int p)
{
    if (n < 0 || k < 0 || n < k)
        return 0;
    int e1, e2, e3;
    int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n - k, p, e3);
    if (e1 > e2 + e3)
        return 0;
    return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

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

    int h, w, a, b;
    cin >> h >> w >> a >> b;
    vector<int> cnt(w);
    make_fact();
    rep(i, w) cnt[i] = mod_comb(h - a + i - 1, i, mod);
    int ans = 0;
    Rep(i, w, b)(ans += cnt[i] * mod_comb(a - 1 + w - i - 1, a - 1, mod)) %= mod;
    cout << ans << endl;
}

Measured memory_bytes: 2560
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000

int top, S[MAX];

int isEmpty(){
    return top==0;
}

int isFull(){
    return top>=MAX-1;
}

void push(int x){
    if(isFull()){
        puts("overflow");
        return;
    }
    S[++top] = x;
}

int pop(){
    if(isEmpty()){
        puts("underflow");
        return 0;
    }
    top--;
    return S[top+1];
}
int main(void){
    int a, b;
    char s[100];
    top=0;
    while(scanf("%s", s) != EOF){
        if(s[0] == '+'){
            a=pop();
            b=pop();
            push(a+b);
        }else if(s[0] == '-'){
            a=pop();
            b=pop();
            push(b-a);
        }else if(s[0] == '*'){
            a=pop();
            b=pop();
            push(a*b);
        }else{
            push(atoi(s));
        }
    }
    printf("%d\n", pop());
    return 0;
}


Measured memory_bytes: 2108
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        MyScanner in = new MyScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        A solver = new A();
        solver.solve(1, in, out);
        out.close();
    }

    static class A {
        public void solve(int testNumber, MyScanner in, PrintWriter out) {
            int A = in.nextInt();
            int B = in.nextInt();
            out.println(A * B % 2 == 0 ? "No" : "Yes");
        }

    }

    static class MyScanner {
        private BufferedReader in;
        private StringTokenizer st;

        public MyScanner(InputStream stream) {
            in = new BufferedReader(new InputStreamReader(stream));
        }

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

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

    }
}



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

inline int sum(int namber){
    int ans=0;
    while(namber){
        ans += namber % 10;
        namber /= 10;
    }
    return ans;
}

int main(){
    int N;
    int ans = 200000;
    cin>>N;

    for(int A=1; A<=N/2; A++){
        int tmp_ans;
        int B = N - A;
        tmp_ans = sum(A)+sum(B);
        if(tmp_ans<ans)ans = tmp_ans;

    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().strip().split()))
ans=[0]*9
for i in range(n):
    if 1<=a[i] and a[i]<=399:
        ans[0]=1
    elif 400<=a[i] and a[i]<=799:
        ans[1]=1
    elif 800<=a[i] and a[i]<=1199:
        ans[2]=1
    elif 1200<=a[i] and a[i]<=1599:
        ans[3]=1
    elif 1600<=a[i] and a[i]<=1999:
        ans[4]=1
    elif 2000<=a[i] and a[i]<=2399:
        ans[5]=1
    elif 2400<=a[i] and a[i]<=2799:
        ans[6]=1
    elif 2800<=a[i] and a[i]<=3199:
        ans[7]=1
    elif 3200<=a[i]:
        ans[8]+=1

if sum(ans[0:8])==0:
    mi=1
else:
    mi=0
    
print("{} {}".format(sum(ans[0:8])+mi,sum(ans)))


Measured memory_bytes: 3064
import sys

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]

    return i + 1

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    q = partition(A, 0, len(A) - 1)
    A = map(str, A)
    A[q] = "[" + A[q] + "]"
    print " ".join(A)

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

int main(){
	int n, a, b, mn = 10000;
	cin >> n;
	for (int i = 1; i < n; ++i) {
		a = i;
		b = n - i;
		int sum = 0;
		while (a > 0) {
			sum += a % 10;
			a /= 10;
		}
		while (b > 0) {
			sum += b % 10;
			b /= 10;
		}
		mn = min(mn, sum);
	}
	cout << mn << endl;
}

Measured memory_bytes: 256
class UnionFind():
    def __init__(self,size):
        self.table=[-1]*size
    def root(self,x):
        while self.table[x]>=0:
            x=self.table[x]
        return x
    def unite(self,x,y):
        s1=self.root(x)
        s2=self.root(y)
        if s1!=s2:
            if self.table[s1]>self.table[s2]:
                s1,s2=s2,s1
            self.table[s1]+=self.table[s2]
            self.table[s2]=s1
        return
    def same(self,x,y):
        return self.root(x)==self.root(y)
    def size(self,x):
        return -self.table[self.root(x)]
n,*a=map(int,open(0).read().split())
u=UnionFind(n)
b=[0]*n
for i,t in enumerate(a):b[t-1]=i
c=0
for k in b[::-1]:
    l=r=1
    if k>0 and a[k-1]>a[k]:
        l+=u.size(k-1)
        u.unite(k-1,k)
    if k+1<n and a[k+1]>a[k]:
        r+=u.size(k+1)
        u.unite(k+1,k)
    c+=l*r*a[k]
print(c)

Measured memory_bytes: 26316
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
typedef long long ll;
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	if(k>(n-1)*(n-2)/2){
		printf("-1\n");
		return 0;
	} 
	ll now=(n-1)*(n-2)/2;
	ll ans=n-1+now-k;
	printf("%d\n",ans);
	for(int i=2;i<=n;i++){
		printf("1 %d\n",i);
	}
	ll m=0;
	ll nownum=2;
	ll nowend=3;
	while(m<now-k){
		m++;
		if(nowend==n+1){
			nownum++;
			nowend=nownum+1;
		}
		printf("%d %d\n",nownum,nowend);
		nowend++;
	}
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
		string s;
		cin>>s;
		ll R_count=0;
		for(ll p=0;p<s.length();p++)
		{
			if(s[p]=='R')
			{
				ll r=0;
				while(s[p]=='R')
				{
					r++;
					p++;
				}
			//	cout<<"bha"<<endl;
				R_count=max(R_count,r);
			}
			
		}
        cout<<R_count<<endl;
}

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

using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }

class Solver {
  public:
    bool solve() {
        int H, W; cin >> H >> W;
        vector<vector<ll>> C(10, vector<ll>(10)); cin >> C;
        vector<vector<int>> A(H, vector<int>(W)); cin >> A;

        rep(k, 10) rep(i, 10) rep(j, 10) set_min(C[i][j], C[i][k] + C[k][j]);
        
        ll ans = 0;
        rep(i, H) rep(j, W) if(abs(A[i][j]) != 1) ans += C[A[i][j]][1];
        cout << ans << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int k;
int bit[1000010];
void add(int a,int w){
  for(int i = a; i <= k; i += i & -i)bit[i] += w;
}
int sum(int a){
  int ret = 0;
  for(int i = a; i > 0; i -= i & -i) ret += bit[i];
  return ret;
}
int main(){
  long long n,m;cin>>n>>m;
  vector<long long> a(n);
  for(int i = 0; n > i; i++){
    cin>>a[i];
    a[i] -= m;
  }
  vector<long long> b(n+1);
  vector<long long> c(n+1);
  b[0] = 0;
  c[0] = 0;
  for(int i = 1; n >= i; i++){
    b[i] = b[i-1]+a[i-1];
    c[i] = b[i];
  }
  sort(c.begin(),c.end());
  c.erase(unique(c.begin(),c.end()),c.end());
  vector<long long> d(c.size());
  for(int i = 0; n >= i; i++){
    auto z = lower_bound(c.begin(),c.end(),b[i]) - c.begin();
    d[z]++;
  }
  k = c.size();
  for(int i = 1; k >= i; i++){
    //cout << d[i-1] << endl;
    add(i,d[i-1]);
  }
  long long ans = 0;
  for(int i = 0; n >= i; i++){
    
    auto z = lower_bound(c.begin(),c.end(),b[i]) - c.begin();
    ans += max(0,sum(k)-sum(z)-1);
    //cout <<sum(z) << endl;
    //cout <<":" << b[i] << " " << z << endl;
    add(z+1,-1);
  }
  cout << ans << endl;

}


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

using lli = int64_t;

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

    int n; cin >> n;
    lli t; cin >> t;

    lli ans = 0;

    lli last = 0;
    for(lli i = 0, cur = 0; i < n; i++)
    {
        cin >> cur;

        if(cur >= last) ans += t;
        else ans += (cur + t) - last;

        last = cur + t;
    }

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

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

def bellman_ford(N: int, edges: list, src: int) -> list:
    """ ベルマンフォード(頂点数, 辺集合(0-indexed), 始点) """

    # 頂点[ある始点からの最短距離] (経路自体を知りたい時はここに前の頂点も持たせる)
    res = [INF] * N
    res[src] = 0
    # 各辺によるコストの置き換えを頂点数N-1回繰り返す
    for i in range(N-1):
        for src, dest, cost in edges:
            if res[dest] > res[src] + cost:
                res[dest] = res[src] + cost
    # 頂点Nが負の閉路に含まれていないかチェックする
    for i in range(N):
        for src, dest, cost in edges:
            if res[dest] > res[src] + cost:
                res[dest] = -INF
                if dest == N-1:
                    # 該当していたら空リストを返却
                    return []
    # 問題なければ頂点リストを返却
    return res

N, M, P = MAP()
edges = []
for i in range(M):
    a, b, c = MAP()
    # Pは最後に請求される、だけど、ようは動く度に支払ってると思ってもいい
    edges.append((a-1, b-1, -c+P))

res = bellman_ford(N, edges, 0)
if not len(res):
    print(-1)
else:
    # 正負を直す
    ans = -res[N-1]
    # スコアがマイナスなら0
    print(max(0, ans))


Measured memory_bytes: 43312
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
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;}

const ll MOD = 998244353;

ll modpow(ll n, ll p, ll m) {
    ll res = 1;
    while(p>0){
        if(p%2==1) res = (res * n) % m;
        n = (n * n) % m;
        p /= 2;
    }
    return res;
}

int cnt[101010];
int v[101010];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin>>n;
    int mx_dist = 0;
    rep(i,n) {
        int t;cin>>t;
        v[i] = t;
    }
    rep(i,n){
        cnt[v[i]]++; 
    }
    rep(i,n){
        mx_dist = max(mx_dist, v[i]);
    }
    if(v[0] != 0 || cnt[0]!=1){
        cout<<'0'<<endl;
        return 0;
    }
    ll ans = 1;
    for(int i=1; i<=mx_dist; i++){
        // ans *= modpow((ll)cnt[i-1], (ll)cnt[i], MOD)%MOD;
        for(int j=0; j<cnt[i]; j++) ans = ans * cnt[i-1] % MOD;
    }
    cout<<ans%MOD<<endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
const ll INF = 1e9+100;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
const bool debug = 0;
//------------------------------//

int N, M;
int a[112345];
int cnt[112345][20];
int dp[1<<20];

int main() {
	cin >> N >> M;
	REP(i, N) scanf("%d", a + i), a[i]--;
	
	REP(i, N) cnt[i + 1][a[i]]++;
	REP(j, M) REP(i, N) cnt[i + 1][j] += cnt[i][j];
	
	fill(dp, dp + (1<<M), INF);
	
	dp[0] = 0;
	REP(s, 1<<M) { // ??¢????????£???????????°
		int c = 0;
		REP(i, M) if (s >> i & 1) c += cnt[N][i];
		REP(i, M) if (!(s >> i & 1)) {
			int p = cnt[N][i] - (cnt[c + cnt[N][i]][i] - cnt[c][i]);
			chmin(dp[s | 1<<i], dp[s] + p);
		}
	}
	
	cout << dp[(1<<M)-1] << endl;
	
	return 0;
}

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

using namespace std;

#define int long long

int32_t main() {
    ios::sync_with_stdio(false);

//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);

    int n, k, answ = 1, mod = 1000000007;
    cin >> n >> k;

    int a[n], l = 0, r = n-1;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a+n);

    if(a[n-1] == 0 && k%2) {
        cout << 0;
        return 0;
    }
    else if(a[n-1] < 0 && k%2) {
        while(k)
            answ = ((answ%mod) * (a[r]%mod))%mod, k--, r--;
        cout << mod+answ;
        return 0;
    }
    else if(k%2)
        k--, answ = a[r], r--;

    while(k) {
        k -= 2;
        int p1 = a[l] * a[l+1], p2 = a[r] * a[r-1];
        if(p1 > p2)
            l += 2, answ = ((answ%mod) * (p1%mod))%mod;
        else
            r -= 2, answ = ((answ%mod) * (p2%mod))%mod;
    }

    cout << (answ>=0?answ:mod+answ);

    return 0;
}


Measured memory_bytes: 5216
/*apna time ayega*/ 
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    int k=n%10;
    if(k==2 || k==4 || k==5 || k==7 || k==9)
    cout<<"hon"<<endl;
    else if(k==0 || k==1 || k==6 || k==8)
    cout<<"pon"<<endl;
    else
    cout<<"bon"<<endl;
 }
  
	
		
	

Measured memory_bytes: 3660
#include<stdio.h>
 
int main(void)
{
  int A1,A2,A3,sa1,sa2,sa3;
  
  scanf("%d %d %d",&A1,&A2,&A3);

  sa1 = (A1 > A2) ? A1-A2 : A2-A1;
  sa2 = (A2 > A3) ? A2-A3 : A3-A2;
  sa3 = (A1 > A3) ? A1-A3 : A3-A1;
  
  if(sa1<=sa2)
  	if(sa2<=sa3){
  		printf("%d\n",sa1+sa2);
  	}else{
 		printf("%d\n",sa1+sa3);
  	}
  if(sa1>sa2)
  	if(sa1<sa3){
  		printf("%d\n",sa1+sa2);
 	}else{
 		printf("%d\n",sa2+sa3);
 	}
  
  return 0;
}

Measured memory_bytes: 128
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

char C[10];
int S[10];
int len;

long int calc(long int acc, int pos, int last, int delimiter) {
  if (delimiter == len) {
    return 0;
  }
  if (pos == len - 1) {
    long int after = 0;
    for (auto i = last; i < len; i++) {
      after = after * 10 + S[i];
    }
    return acc + after;
  } else {
    long int before = 0;
    for (auto i = last; i <= pos; i++) {
      before = before * 10 + S[i];
    }
    return calc(acc + before, pos + 1, pos + 1, delimiter + 1) +
           calc(acc, pos + 1, last, delimiter);
  }
}

int main() {
  scanf("%s\n", C);

  len = (int)strlen(C);

  for (auto i = 0; i < len; i++)
    S[i] = C[i] - '0';

  printf("%ld\n", calc(0, 0, 0, 0));
  return 0;
}


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

using namespace std;
long long n,i,j,k,x,s1,s2;
long long dp[405][405],pref_sum[405];
const long long inf=1e17L;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            dp[i][j]=inf;
    for(i=1; i<=n; i++)
        dp[i][i]=0;
    for(i=1; i<=n; i++)
    {
        cin>>x;
        pref_sum[i]=x+pref_sum[i-1];
    }
    for(i=1; i<=n; i++)
    {
        int ln=1,col=i+1;
        while(col<=n)
        {
            for(k=ln; k<col; k++)
            {
                s1=pref_sum[k]-pref_sum[ln-1];
                s2=pref_sum[col]-pref_sum[k];
                dp[ln][col]=min(s1+s2+dp[ln][k]+dp[k+1][col],dp[ln][col]);
            }

            col++;
            ln++;
        }
    }
    cout<<dp[1][n];
    return 0;
}


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

int main(){
	
	int a, b, c, d, e, f;
	double x, y;
	
	while(cin >> a >> b >> c >> d >> e >> f){
		y = (double)(a*f-c*d)/(double)(a*e-b*d);
		x = ((double)c-(double)b*y)/(double)a;
		
		printf("%.3f %.3f\n",x,y);
	}
	
	return 0;
}

Measured memory_bytes: 0
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007
#define llMAX 9223372036854775807
#define llMIN -9223372036854775808
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

int main(){
  ll ans=0;
  cin >> n ;

  string s="";
  char moji='a';
  REP(i,n){
    s+=moji;
  }

  cout<<s<<endl;

  vi a(10,10);
  a[0]=0;
  int idx=0;
  for(ll i=0;i<1000000000;i++){
    idx=n-1;

    for(ll k=0;k<n;k++){
      if (a[s[idx]-97]<idx){
        s[idx]++;
        break;
      }else{
        s[idx]='a';
        idx--;
      }
    }

    for(ll k=0;k<n;k++){
      a[k]=10;
    }
    for(ll k=0;k<n;k++){
      a[s[k]-97]=min(k,a[s[k]-97]);
    }
    


    if (idx==-1)break;
    cout<<s<<endl;
    
    
    
  }

  return 0;
}

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

#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb emplace_back

using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;

const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};

template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ") 
{ rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }

struct UnionFind {
	vector<int> p, r;

	UnionFind() {}
	UnionFind(int size) { init(size); }

	void init(int size) {
		p.resize(size);
		r.resize(size);
		for (int i = 0; i < size; ++i) {
			p[i] = i; r[i] = 1;
		}
	}

	int find(int x) {
		if (p[x] == x) return x;
		else return p[x] = find(p[x]);
	}

	void unite(int x, int y) {
		x = find(x); y = find(y);

		if (x == y) return;

		if (r[x] > r[y]) swap(x, y);
		r[x] += r[y];
		p[y] = x;
	}

	bool is_root(int x) {
		return find(x) == x;
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}

	int element_count(int x) {
		x = find(x);
		return r[x];
	}
};

int main() {
	int n;

	while (cin >> n, n) {
		UnionFind uf(n * n);
		rep(i, n) rep(j, n) {
			int x = in(), y = in();
			
			int u = y * n + x;
			int v = i * n + j;

			uf.unite(u, v);
		}

		int ans = 0;
		bool f[10000] = {false};
		rep(i, n * n) {
			if (f[uf.find(i)]) continue;
			f[uf.find(i)] = true;
			ans++;
		}

		print(ans);
	}
}

Measured memory_bytes: 3252
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <unordered_map>

#define REP(i, n) for (int i = 0; i < n; i++)
#define ll long long int

using namespace std;

int main()
{
  int H, W, h, w;
  cin >> H >> W;
  cin >> h >> w;
  cout << (H - h) * (W - w) << endl;
}

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

using namespace std;

const int MAX_N = 2000 + 5;

int N, M;
char s[MAX_N];
int maze[MAX_N][MAX_N];
int b[MAX_N][MAX_N];
int l[MAX_N], r[MAX_N], h[MAX_N];

inline int check(int x, int y)
{
  return maze[x][y] ^ maze[x - 1][y - 1] ^ maze[x][y - 1] ^ maze[x - 1][y];
}

int main()
{
  // freopen("in.txt", "w", stdout);
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; ++i) {
    scanf("%s", s + 1);
    for (int j = 1; j <= M; ++j) {
      if (s[j] == '#') maze[i][j] = 1;
    }
  }
  for (int i = 2; i <= N; ++i) {
    for (int j = 2; j <= M; ++j) {
      b[i][j] = check(i, j);
      // if (b[i][j] == 1) printf("b[%d][%d]=1\n", i, j);
    }
  }
  for (int i = 1; i <= M + 1; ++i) {
    h[i] = 0;
    l[i] = 1;
    r[i] = M + 1;
  }
  int ans = 0;
  for (int i = 2; i <= N + 1; ++i) {
    int ll = 1, rr = M + 1;
    for (int j = 1; j <= M + 1; ++j) {
      if (b[i - 1][j] == 1) {
        h[j] = 1;
        l[j] = 1;
        ll = j;
      } else {
        h[j] = h[j] + 1;
        l[j] = max(l[j], ll);
      }
    }
    for (int j = M + 1; j >= 1; --j) {
      if (b[i - 1][j] == 1) {
        r[j] = M + 1;
        rr = j;
      } else {
        r[j] = min(r[j], rr);
      }
      // printf("(%d,%d)\tl:%d\th:%d\tr:%d\n", i, j, l[j], h[j], r[j]);
      ans = max(ans, h[j] * (r[j] - l[j]));
    }
  }
  printf("%d\n", ans);
  return 0;
}


Measured memory_bytes: 31616
#include <bits/stdc++.h>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(x))
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

signed main(){
	int n,x,y; cin>>n>>x>>y;
	// ll d[n+5][n+5]; fill(ALL(d),INF);
	int ans[n+5]; memset(ans,0L,sizeof(ans));
	// int ans = 0;
	reps(i,n){
		for(int j=i+1;j<=n;j++){
			int d = min(abs(i-j), abs(x-i)+abs(y-j)+1);
			ans[d]++;
		}
	}
	reps(i,n-1) cout << ans[i] << endl;
	return 0;
}


Measured memory_bytes: 256
func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main() {
    let ans = [
        "SUN": 7,
        "MON": 6,
        "TUE": 5,
        "WED": 4,
        "THU": 3,
        "FRI": 2,
        "SAT": 1,
    ]
    let s = readLine()!
    print(ans[s]!)
}

main()


Measured memory_bytes: 7888
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappop, heappush

def rle(lst):
    res = []
    pre, cnt = lst[0], 1
    for i in range(1, len(lst)):
        if lst[i] == pre:
            cnt += 1
        else:
            res.append((pre, cnt))
            pre = lst[i]
            cnt = 1
    res.append((pre, cnt))
    return res

def solve():
    n, k = MI()
    S = list(input())

    R = rle(S)

    score = 0
    for r in R:
        score += r[1] - 1

    l = len(R)
    for _ in range(k):
        if l > 2:
            score += 2
            l -= 2
        elif l == 2:
            score += 1
            l -= 1
        else:
            break
    print(score)






if __name__ == '__main__':
    solve()


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


#define ll long long int
int main()
{


    int n;
    cin>>n;
int a[n];
    for(int i=0;i<n;i++) cin>>a[i];
    ll sum=0,mx=a[0];

    for(int i=1;i<n;i++){
      if(a[i]>=a[i-1]) mx+=a[i]-a[i-1];
    }
    cout<<mx<<endl;


}


Measured memory_bytes: 3580
#include <vector>
#include <stdio.h>
#include <queue>
using namespace std;

class TopologicalSort {
public:
  int n;
  vector< vector<int> > G;
  vector<bool> used;
  vector<int> indeg;
  vector<int> out;

  TopologicalSort(int _n): n(_n), G(n), used(n), indeg(n), out(0) {}

  void bfs(int start) {
    queue<int> q;
    q.push(start);
    while (!q.empty()) {
      int cur = q.front(); q.pop();
      out.push_back(cur);
      for (int v: G[cur]) {
	indeg[v]--;
	if (indeg[v] == 0 && !used[v]) {
	  used[v] = true;
	  q.push(v);
	}
      }
    }
  }
  
  void add_edge(int from, int to) {
    G[from].push_back(to);
  }
  
  vector<int> build() {
    fill(indeg.begin(), indeg.end(), 0);
    fill(used.begin(), used.end(), 0);
    for (int i = 0; i < n; ++i)
      for (int v: G[i]) indeg[v]++;

    for (int i = 0; i < n; ++i) {
      if (indeg[i] == 0 && !used[i]) bfs(i);
    }
    return out;
  }
};

int main() {
  int v, e;
  scanf("%d %d", &v, &e);

  TopologicalSort tp = TopologicalSort(v);
  for (int i = 0; i < e; ++i) {
    int s, t;
    scanf("%d %d", &s, &t);
    tp.add_edge(s, t);
  }
  vector<int> ans = tp.build();
  for (size_t i = 0; i < ans.size(); ++i) {
    printf("%d\n", ans[i]);
  }
  return 0;
}



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

int main(void){
  int n, x, count = 0;

  cin >> n >> x;
  while( n != 0 || x != 0 ){
    for( int i = 1; i <=  n-2; i++ ){
      for( int j = i+1; j <= n-1; j++){
	if( x-i-j > j && x-i-j <= n ){
	  count++;
	}
      }
    }
    cout << count << endl;

    count = 0;
    cin >> n >> x;
  }
  
  return 0;
}

Measured memory_bytes: 2980
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass
{
	public const long Giri = 1000000007;
	public const long DpInf = 99999999999999;

	
	public static void Main(string[] args)
	{
		var p = Input();
		var b = p[0][0] + "" + p[1][0] + "" + p[2][0];
		Console.WriteLine(b.ToUpper());
		//.ToInt();
		
	}
	
	static long GCD(long a, long b)
	{
		var v = new[] { a, b };
		while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; }
		return v[0];
	}
	public static string[] Input() { return Console.ReadLine().Split(); }
}

public static class Smarter
{
	public static int ToInt(this string str) { return int.Parse(str); }
	public static long ToLong(this string str) { return long.Parse(str); }
	public static int[] ToInt(this string[] str, int offset = 0)
	{
		var r = new int[str.Length];
		for(int i = 0; i < str.Length; i++) r[i] = str[i].ToInt() + offset;
		return r;
	}
}

Measured memory_bytes: 11092
#include <bits/stdc++.h>
using namespace std;
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; }
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MOD (1000000007)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int a,b,c,d;

void input(){
    cin >> a >> b >> c >> d;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    cout << min(a, b) + min(c, d) << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(void){
	int m,d,i;
	int W;
	int s;
	
	while(1){
		s=0;
		scanf("%d %d",&m,&d);
		if(m==0 || d==0)break;
		for(i=1;i<m;i++){
			if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)s+=31;
			else if(i==4 || i==6 || i==9 || i==11 )s+=30;
			else if(i==2)s+=29;
		}
		s=s+d;
		if(s%7==0)printf("Wednesday\n");
		else if(s%7==1)printf("Thursday\n");
		else if(s%7==2)printf("Friday\n");
		else if(s%7==3)printf("Saturday\n");
		else if(s%7==4)printf("Sunday\n");
		else if(s%7==5)printf("Monday\n");
		else if(s%7==6)printf("Tuesday\n");
	}
return 0;
}

Measured memory_bytes: 584
#include<stdio.h>

int main(void){
	int n;
	int k;
	int h[100000];
	int i;
	int c;

	scanf("%d %d",&n,&k);
	c = 0;

	for(i=0;i<n;i++){
		scanf("%d",&h[i]);
		if(h[i] >= k){
			c++;
		}
	}

	printf("%d\n",c);



		

	return 0;
}




Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
short n;
long long c,x,b,a[102][100002];
pair<long long,long long>p[102];
int main (){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
     cin>>n>>x;
     for(int i=0;i<n;i++){
     	cin>>c>>b;
     	p[i]=make_pair(c,b);
	 }
	 for(int j=0;j<x+1;j++){
     	 	if(p[0].first>j)
			     a[0][j]=0;
     	 	else 
     	 	     a[0][j]=p[0].second;
		  }
     for(int i=1;i<n;i++){
     	 for(int j=0;j<x+1;j++){
     	 	    if(p[i].first>j)
     	 	         a[i][j]=a[i-1][j];
     	 	    else
     	 	    	 a[i][j]=max(p[i].second+a[i-1][j-p[i].first],a[i-1][j]);
		  }
	 }
	 cout<<a[n-1][x];
return 0;
}

Measured memory_bytes: 78336
#include <bits/stdc++.h>
using namespace std;
int main () {
  int N, M;
  cin >> N >> M;
  int k, a;
  vector<vector<int>> data(M,vector<int> (N, -1));
  for(int i = 0; i < M; i++){
    cin >> k;
    for(int j = 0; j < k; j++){
      cin >> a;
      data.at(i).at(j) = a;
    }
  }
  vector<int> sub(M), on(N);
  for(int i = 0; i < M; i++)cin >> sub.at(i);
  int cal, count, co = 0, ans = 0;
  for(int i = 0; i < pow(2, N); i++){
    cal = i;
    co = 0;
    for(int j = 0; j < N; j++){
      on.at(j) = cal % 2;
      cal /= 2;
    }
    for(int j = 0; j < M; j++){
      count = 0;
      for(int k = 0; k < N; k++){
        if(data.at(j).at(k) == -1)break;
        else {
          if(on.at(data.at(j).at(k) - 1) == 1)count++;
        }
      }
      if(count % 2 == sub.at(j))co++;
    }
    if(co == M)ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

public static class P
{
    public static void Main()
    {
        int n = Reader.NextUInt;
        int last = 0;
        int cur = 0;
        int max = 0;
        for (int i = 0; i < n; i++)
        {
            var h = Reader.NextUInt;
            if (last >= h)
            {
                cur++;
                max = Max(max, cur);
            }
            else
            {
                cur = 0;
            }
            last = h;
        }
        Console.WriteLine(max);
    }
}

static class Reader
{
    const int BUF_SIZE = 1024;
    static Stream Stream;
    static byte[] buffer = new byte[BUF_SIZE];
    static int ptr = 0;
    static Reader()
    {
        Stream = Console.OpenStandardInput();
    }

    private static void Read()
    {
        Stream.Read(buffer, 0, BUF_SIZE);
    }

    private static byte Next { get { if (ptr == buffer.Length) { Read(); ptr = 0; } return buffer[ptr++]; } }

    public static int NextUInt
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get
        {
            int res = 0; byte next;
            do { next = Next; } while (next < 48);
            do { res = res * 10 + next - 48; next = Next; } while (48 <= next);
            return res;
        }
    }
}


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

int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N){
    cin >> A[i];
  }
  
  int M;
  cin >> M;
  vector<int> new_A(N);
  rep(i, M){
    new_A = A;
    int p, x;
    cin >> p >> x;
    new_A[p-1] = x;
    int sum =  std::accumulate(new_A.begin(), new_A.end(), 0);
    cout << sum << endl;
  }
  
  
}

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

bool binarySearch(int a[], int n, int key) {
	int left = 0;
	int right = n;
	int mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (key == a[mid])
			return true;
		else if (key > a[mid]) 
			left = mid + 1;
		else
			right = mid;
	}
	return false;
}

int main() {
	int n, q,a[100000+1],num=0,key;
	cin >> n;
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		scanf("%d", &key);
		if (binarySearch(a, n, key))num++;
	}
	cout << num << endl;
	return 0;
}


Measured memory_bytes: 3516