akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;//チーム数
  int plate[n][3];//順位表
  for(int i=0; i<n; i++){
    plate[i][1] = 0;
    plate[i][0] = i;
    plate[i][2] =0;
  }
  for(int i=0; i<n*(n-1)/2; i++){
    int h,a,hp,ap;
    cin >> h;//ホーム
    cin >> a;//アウェイ
    cin >> hp;//ホームの得点
    cin >> ap;//アウェイの得点
    if (hp>ap){
      plate[h-1][1]+=3;
    }
    if (hp<ap){
      plate[a-1][1]+=3;
    }
    if (hp==ap){
      plate[h-1][1]++;
      plate[a-1][1]++;
    }
  }

  for(int j=0; j<n-1; j++){
    for(int i=n-1; i>j; i--){
      if(plate[i][1]>plate[i-1][1]){
        int b,d;
        b=plate[i-1][1];
        d=plate[i-1][0];
        plate[i-1][0]=plate[i][0];
        plate[i-1][1]=plate[i][1];
        plate[i][0]=d;
        plate[i][1]=b;
      }
    }
  }
  plate[0][2]=1;
  for(int i=1; i<n; i++){
    if (plate[i][1]!=plate[i-1][1]){
      plate[i][2]=i+1;
    }
    else{
      plate[i][2]=plate[i-1][2];
    }
  }
  for(int j=0; j<n-1; j++){
    for(int i=n-1; i>j; i--){
      if(plate[i][0]<plate[i-1][0]){
        int b,d;
        b=plate[i-1][2];
        d=plate[i-1][0];
        plate[i-1][0]=plate[i][0];
        plate[i-1][2]=plate[i][2];
        plate[i][0]=d;
        plate[i][2]=b;
      }
    }
  }
  for(int i=0; i<n; i++){
    cout << plate[i][2] <<endl;
  }
}



Measured memory_bytes: 3120
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<unordered_set>
#include<map>
#include<cstring>
#include<utility>
#include<string>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define int long long
#define pb push_back
#define fr(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define LM LONG_MAX
#define LS LONG_MIN
#define cfM 998244353
#define ccM 1000000007
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define inf 10000000000000000
#define sp setprecision(9)
int max(int b,int a)
{if(b>a)return b;else return a;}
int min(int b,int a)
{if(b<a)return b;else return a;}
int abs(int b)
{if(b<0) return (-1*b);else return(b);}
void print(vector<int>&v)
{
	for(int i=0;i<v.size();i++)
		cout<<v[i]<<" ";
	cout<<endl;}
void print(int arr[],int n)
{
	for(int i=0;i<n;i++)
		cout<<arr[i]<<" ";
}
int32_t main()
{
	int n;
	cin>>n;
	int arr[n][3];
	for(int i=0;i<n;i++)
		cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
	int dp[n][3];
	dp[0][0]=arr[0][0];
	dp[0][1]=arr[0][1];
	dp[0][2]=arr[0][2];
	for(int i=1;i<n;i++)
	{
		dp[i][0]=arr[i][0]+max(dp[i-1][1],dp[i-1][2]);
		dp[i][1]=arr[i][1]+max(dp[i-1][0],dp[i-1][2]);
		dp[i][2]=arr[i][2]+max(dp[i-1][1],dp[i-1][0]);
	}
	//print(dp,n);
	cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]))<<endl;
}


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

int main(){
  int n,m;
  cin>>n>>m;
  int p=min((int)sqrt(m),m/n);
  int ans=0;
  for(int i=1;i<=p;i++){
    if(m%i==0){
      ans=max(ans,i);
      if(m/i<=m/n){
        ans=m/i;
        break;
      }
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3684
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
vector<int> par(1000000);//それぞれの頂点の親,最大値をsizeにする
vector<int> dis(1000000);//ランク
int n;
class union_find{
  public:
  void init(){
    for(int i = 0;i < n;i++){
      par[i] = i;//最初、親は自分自身とする
      dis[i] = 0;
    }
  }
  //木の根を求める
  int root(int x){
    if(par[x] == x){
      return x;
    }
    else{
      return par[x] = root(par[x]);
    }
  }
  //xとyが同じ集合に属すか否か
  bool same(int x,int y){
    //cout << x << " " << y << endl;
    return (root(x) == root(y));
  }
  //xとyの属する集合を併合
  void unite(int x,int y){
    x = root(x);
    y = root(y);
    if(x == y)return;
    if(dis[x] < dis[y]){
      par[x] = y;
    }
    else{
      par[y] = x;
      if(dis[x] == dis[y])dis[x]++;
    }
  }
};

//union_find tree;
//tree.init() //これで初期化
//tree.unite(a,b) //aとbのグループを併合
//tree.same(a,b) //aとbが同じグループか(trueかfalse)

int main(){
    int e;
    cin >> n;
    e = 2*(n-1);
    vector<pair<pair<int,int>,int> > edge;//辺
    vector<pair<ll,ll>> x(n);
    vector<pair<ll,ll>> y(n);
    for(int i = 0;i < n;i++){
      cin >> x[i].first >> y[i].first;
      x[i].second = i;
      y[i].second = i;
    }
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    for(int i = 1;i < n;i++){
      edge.push_back(make_pair(make_pair(x[i].first - x[i - 1].first,x[i - 1].second),x[i].second));
    }
    for(int i = 1;i < n;i++){
      edge.push_back(make_pair(make_pair(y[i].first - y[i - 1].first,y[i - 1].second),y[i].second));
    }
    sort(edge.begin(),edge.end());//最小からにする
    int res = 0;
    union_find tree;
    tree.init();
    for(int i = 0;i < e;i++){
        int w,a,b;
        w = edge[i].first.first;
        a = edge[i].first.second;
        b = edge[i].second;
        //tie(w,a,b) = edge[i];
        if(!tree.same(a,b)){
            tree.unite(a,b);
            res += w;
        }
    }
  //for(int i = 0;i < e;i++)cout << edge[i].first.first << " " << edge[i].first.second << " " << edge[i].second << endl;
  cout << res << endl;
}

Measured memory_bytes: 17372
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define SCD(n) scanf("%d",&n)
#define SCD2(m,n) scanf("%d%d",&m,&n)
#define SCD3(m,n,k) scanf("%d%d%d",&m,&n,&k)
#define PB push_back
#define MP make_pair
#define ARSCD(A,N) REP(i,N){SCD(A[i]);}
#define ARSCD1(A,N) FORq(i,1,N){SCD(A[i]);}
#define PRINTD(n) printf("%d\n",n)
#define PRINTLLD(n) printf("%lld\n",n)
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
using namespace std;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
//////////////////////////////////////////////////////
int main(){
	int a,b,c,d;
	scanf("%d %d %d %d",&a,&b,&c,&d);
	if (abs(a-c) <= d) {
		printf("Yes\n"); 
		return 0;
	}
	
	if ((abs(a-b) <= d) and (abs(b-c) <= d)){
		printf("Yes\n"); 
		return 0;
	}
	
	printf("No\n");
	
	return 0;
}

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

using namespace std;
using ll = long long;

bool chack(string s){
    int m = (int)s.size() / 2;

    return s.substr(0, m) == s.substr(m, m);

}

int main()
{
    string s;
    cin >> s;
    int n = s.size();
    for(int i = 0; i < n; i += 2){
        s.pop_back();
        s.pop_back();

        if(chack(s)){
            cout << s.size() << endl;
            return 0;
        }
    }
    assert(0);
    
}

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

int main(){
    int n,y;
    cin >> n >> y;
    int p = y / 1000 - n;
    int x_up = p / 9;
    int y_up = p / 4;
    bool flag = true;
    for(int i=x_up;i>=0;i--){
        int amari = p - 9 * i;
        int Y;
        if(amari % 4 == 0){
            Y = amari / 4;
            int z = n - i - Y;
            if(Y >= 0 && z >= 0){
                cout << i << " " << Y << " " << z << endl;
                flag = false;
                break;
            }
        }
    }
    if(flag){
        cout << -1 << " " << -1 << " "<<-1<<endl;
    }
}

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

#define rep(i, n) for(int i = 0; i<(n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

int main() {
    int N, C;
    cin >> N >> C;
    vector<vector<int>> D(C, vector<int>(C, 0));
    rep(i, C) rep(j, C) cin >> D[i][j];

    vector<vector<int>> M(N, vector<int>(N, 0));
    rep(i, N) rep(j, N) cin >> M[i][j], M[i][j]--;

    vector<vector<int>> cost(3, vector<int>(C, 0));
    rep(c, C) {
        rep(i, N) {
            rep(j, N) {
                cost[(i + j) % 3][c] += D[M[i][j]][c];
            }
        }
    }

    int ans = 1 << 30;
    rep(c0, C) {
        rep(c1, C) {
            if (c1 == c0) continue;
            rep(c2, C) {
                if (c2 == c0 || c2 == c1) continue;
                chmin(ans, cost[0][c0] + cost[1][c1] + cost[2][c2]);
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ll L,R;
    cin >> L >> R;
    ll ans = 2018;
    for(ll i = L; i < min(L+2019,R); i++) {
        for(ll j = i+1; j <= min(L+2019,R); j++) {
            ans = min(ans,i*j%2019);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3580
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,n) for(i=0;i<n;i++)
#define rep1(i,n) for(i=1;i<=n;i++)

int main(){
  string s;
  int k;
  int i;
  
  
  cin >> s >> k;
  
  i = 0;
  while(s[i]=='1'){ 
    i++;
  }
  if(k-1<i)
    cout << 1 << endl;
  else if(i<=k-1){
    cout << (int)(s[i]-'0') << endl;
    
  }
 
}

Measured memory_bytes: 256
d,a[];c(int*z){d=*z-*1[&z];}main(){for(;~scanf("%d",a);qsort(a,4,4,c));for(d=4;--d;printf("%d\n",a[d])); 
d=a[3];if(d>=10800)return 1;if(d>=10600)puts("");else if(d>=10400)puts("a");else if(d>=10200)while(1);return 0;}

Measured memory_bytes: 0
/*
教练我想打ACM！
*/
#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
typedef long long ll;
typedef long double ld;

const int inf = 0x3f3f3f3f;
const int maxn = 100000 + 20;

ll n;
bool check(ll x, ll y, ll z) {
  ll left = z * (4 * x * y - n * x - n * y);
  ll right = n * x * y;
  if (left == right) return 0;
  if (left > right) return -1;
  if (left < right) return 1;
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= 3500; ++i) {
    for (int j = 3500; j >= 1; --j) {
      ll lb = 1, ub = 3500;
      while (lb <= ub) {
        ll mid = (lb + ub) / 2LL;
        ll left = mid * (4LL * i * j - n * i - n * j);
        ll rigt = 1LL * n * i * j;
        if (left > rigt) {
          ub = mid - 1;
        } else if (left < rigt) {
          lb = mid + 1;
        } else {
          printf("%d %d %d\n", i, j, mid);
          return 0;
        }
      }
    }
  }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

template <typename T>
void printlnVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << endl;
  }
}

template <typename T>
void printVector(T v)
{
  rep(i, v.size())
  {
    cout << v[i] << " ";
  }
  cout << endl;
}

int main()
{
  long long A, B, C, K;
  cin >> A >> B >> C >> K;
  if (A + B >= K)
  {
    cout << min(A, K) << endl;
  }
  else
  {
    cout << 2 * A + B - K << endl;
  }
  return 0;
}

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

int main()
{
    int n, x, m[100];
    scanf("%d%d", &n ,&x);
    int i;
    int minm = 1001;
    int weight1 = 0;
    for (i=0; i<n; i++)
    {
        scanf("%d", &m[i]);
        weight1 += m[i];
        if (m[i] < minm) {
            minm = m[i];
        }
    }
    int ans = n + (x-weight1)/minm;
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{   int n;
    double a[101],sum=0;
    cin>>n;
    for(int i=1;i<=n;i++)
      cin>>a[i];
    for(int i=1;i<=n;i++)
      sum+=1/a[i];
    cout<<fixed<<setprecision(6)<<1/sum;
    return 0;
}

Measured memory_bytes: 256
from collections import deque
h, w = map(int, input().split())

grid = [list(input()) for _ in range(h)]

dy = (-1, -1, -1, 0, 0, 0, 1, 1, 1)
dx = (-1, 0, 1, -1, 0, 1, -1, 0, 1)

for i in range(h):
    for j, value in enumerate(grid[i]):
        if grid[i][j] == "#":
            continue
        else:
            bom_cnt = 0
            for k in range(9):
                y = i + dy[k]
                x = j + dx[k]
                if (y < 0 or y >= h or x < 0 or x >= w): continue
                if grid[i][j] == "#": continue
                if grid[y][x] == "#":
                    bom_cnt += 1

            grid[i][j] = bom_cnt

for i in range(h):
    for j in range(w):
        print(grid[i][j], end="")
    print()

Measured memory_bytes: 9424
X, K, D = map(int, input().split())
X = abs(X)
num = X//D
if num >= K:
    print(X-K*D)
elif num < K:
    rest_X = X - num*D # < X
    rest_K = K - num
    if rest_K%2 == 0:
        print(rest_X)
    else:
        print(abs(rest_X - D))

Measured memory_bytes: 9136
X, N = [int(i) for i in input().split(" ")]

if N == 0:
    print(X)
    exit()

p = {int(i) for i in input().split(" ")}

all_p = {i for i in range(1000)}
ranges = all_p - p

diff = 1e9
for i in ranges:
    if diff > abs(i - X):
        diff = abs(i - X)

ans_1 = X + diff
ans_2 = X - diff

for i in ranges:
    if i == ans_2:
        print(ans_2)
        break
else:
    print(ans_1)

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

typedef struct{
	int child_len;
	int *child;
	int parent;
}NODE;

int calc_depth(NODE node[],int num,int depth){
	if(node[num].parent==-1){
		return depth;
	}
	int d=calc_depth(node,node[num].parent,depth+1);
	return d;
}

char *calc_type(NODE node){
	if(node.parent==-1){
		return "root";
	}
	if(node.child_len==0){
		return "leaf";
	}
	return "internal node";
}



int main(){
	int N,num;
	NODE *node;

	scanf("%d",&N);
	node=(NODE*)malloc(sizeof(NODE)*N);
	for(int i=0;i<N;i++){
		node[i].parent=-1;
	}
	for(int i=0;i<N;i++){
		scanf("%d",&num);
		scanf("%d",&node[num].child_len);
		node[num].child=(int*)malloc(sizeof(int)*node[num].child_len);
		for(int j=0;j<node[num].child_len;j++){
			scanf("%d",&node[num].child[j]);
			node[node[num].child[j]].parent=num;
		}
	}

	
	for(int i=0;i<N;i++){
		printf("node %d: parent = %d, ",i,node[i].parent);
		printf("depth = %d, ",calc_depth(node,i,0));
		printf("%s, ",calc_type(node[i]));
		printf("[");
		for(int j=0;j<node[i].child_len;j++){
			printf("%d",node[i].child[j]);
			if(j!=(node[i].child_len-1)){
				printf(", ");
			}
		}
		printf("]\n");
	}

	
	return 0;
}


Measured memory_bytes: 7716
S = input()

cN, cS, cW, cE = 0, 0, 0, 0
for ch in S:
    if ch == 'E':
        cE += 1
    elif ch == 'W':
        cW += 1
    elif ch == 'S':
        cS += 1
    else:
        cN += 1

# もし左右，上下の回数が割り切れたらOK
# 仮定: cE > cW, cN > cS

# if (cW == 0 and cE != 0) or (cW != 0 and cE == 0) or \
#     (cS == 0 and cN != 0) or (cS != 0 and cN == 0):
#     print("No")
# elif cW == 0 and cW == 0:
#     if cS != 0 and cN != 0:
#         print("Yes")
#     else:
#         print("No")
# else:
#     if cS != 0 and cN != 0:
#         print("Yes")
#     else:
#         print("No")

if (cW > 0 and cE == 0) or (cW == 0 and cE > 0) or \
    (cS > 0 and cN == 0) or (cS == 0 and cN > 0):
    print("No")
else:
    print("Yes")

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

int main() {
    int a = 700;
    string s;
    cin >> s;
    if (s[0] == 'o')a += 100;
    if (s[1] == 'o')a += 100;
    if (s[2] == 'o')a += 100;
    cout << a << endl;
    return 0;

}

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

// clang-format off
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }
template <typename T> T clamp(T val, T min, T max) { return val < min ? min : (max < val ? max : val); }
char capitalize(char c) { return static_cast<char>(std::toupper(static_cast<unsigned char>(c))); }
// template <typename T> size_t max_index(T begin, T end) { return distance(begin, max_element(begin, end)); }
// template <typename T> size_t min_index(T begin, T end) { return distance(begin, min_element(begin, end)); }
// clang-format on

#define int long long
#define YES "Yes"
#define NO  "No"
#define MOD 1000000000+7
#define forit(v) for(auto it = v.begin(); it != v.end(); ++it)
#define all(a) begin(a), end(a)

using namespace std;

int f(int m) {
  int tmp = 0;
  while (m >= 10) {
    tmp += m % 10;
    m /= 10;
  }
  tmp += m % 10;
  return tmp;
}

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

  int num = 0;
  // num = numeric_limits<int>::max();
  // num = numeric_limits<int>::min();
  bool flag = false;
  // flag = true;

  int n; cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  sort(all(a), greater<int>());
  for (int i = 0; i < n; ++i) {
    if (i % 2 == 0) num += a[i];
    else num -= a[i];
  }

  cout << num << endl;
  // cout << (flag ? YES : NO) << 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 N = sc.nextInt();
        int M = sc.nextInt();
        int X = sc.nextInt();
        int a = sc.nextInt();
        int left = 0;
        while (a < X) {
            a = sc.nextInt();
            left++;
        }
        System.out.println(Math.min(left, M - left));
        
    }

}


Measured memory_bytes: 22868
#include<iostream>
#include<algorithm>
#include<string>

int assign(int num[2]);				// 入力を配列に代入する処理
bool judgeBreak(int num[2]);		// 終了条件を判定する処理(行数が変わらないのにここまでする必要はあるのか？)
void sort(int num[2]);				// 配列を昇順に並び替える処理
std::string makeAns(int num[2]);	// 解答文字列を作成する処理

int main()
{
	while (true)
	{
		int num[2];
		std::string output;

		assign(num);
		
		// 結局4行取られて気に入らない
		if (judgeBreak(num) == false)
		{
			break;
		}

		sort(num);
		output = makeAns(num);

		std::cout << output << std::endl;
	}

	return 0;
}

int assign(int num[2])
{
	for (int i = 0;i < 2;i++)
	{
		std::cin >> num[i];
	}

	return 0;
}

bool judgeBreak(int num[2])
{
	if (num[0] == 0 && num[1] == 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

void sort(int num[2])
{
	std::sort(num, num + 2);
}

std::string makeAns(int num[2])
{
	std::string ans = std::to_string(num[0]) + ' ' + std::to_string(num[1]);
	return ans;
}


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

int main(){
    int N; cin >> N;
    vector<int> v(N);
    for(int i = 0; i < N; i++) cin >> v[i];
    map<int,int> mp1,mp2;
    set<int> st1,st2;
    for(int i = 0; i < N; i++){
        if (i % 2 == 0){
            mp1[v[i]]++;
            st1.insert(v[i]);
        } else {
            mp2[v[i]]++;
            st2.insert(v[i]);
        }
    }
    vector<pair<int,int>> vec1,vec2;
    for (auto s:st1){
        vec1.push_back(make_pair(mp1[s],s));
    }
    for (auto s:st2){
        vec2.push_back(make_pair(mp2[s],s));
    }
    sort(vec1.begin(),vec1.end(),greater<pair<int,int>>());
    sort(vec2.begin(),vec2.end(),greater<pair<int,int>>());
    
    if(vec1[0].second != vec2[0].second)cout << N - vec1[0].first - vec2[0].first << endl;
    else if(vec1[0].first + vec2[1].first > vec1[1].first + vec2[0].first)cout << N - vec1[0].first - vec2[1].first << endl;
    else cout << N - vec1[1].first - vec2[0].first << endl;
    
}

Measured memory_bytes: 13952
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int C2(int x){
    return x*(x-1)/2;
}

int main(){
    int n,k;
    cin >>n >>k;

    int lim = C2(n-1);
    if(lim<k){
        cout << -1 << "\n";
        return 0;
    }

    vector<pair<int,int>> e;
    if(k == 0){
        rep(i,n)rep(j,i) e.pb({j,i});
    }
    else{
        int v = 3;
        while(C2(v-1)<k) ++v;

        int num = C2(v-1);

        rep(i,v-1) e.pb({i,v-1});

        for(int i=v; i<n; ++i){
            rep(j,i)if(j!=i) e.pb({i,j});
        }

        int idx = 0;
        while(k<num){
            e.pb({idx,(idx+1)%(v-1)});
            --num;

            (idx += 2) %= (v-1);
            if(idx==0) idx = 1;
        }
    }

    int m = e.size();
    cout << m << "\n";
    rep(i,m) cout << e[i].fi+1 << " " << e[i].se+1 << "\n";
    return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int H,W,N,K;
int A[51][51];

int ask(int a,int b,int c,int d) {
	int r;
	_P("? %d %d %d %d\n",a,b,c,d);
	fflush(stdout);
	scanf("%d",&r);
	return r;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	scanf("%d%d%d%d",&H,&W,&N,&K);
	ll ret=0;
	ll p2=1;
	FOR(y,H) FOR(x,W) {
		A[y][x]=ask(y,x,y,x);
		ret += A[y][x]*p2%K;
		p2=p2*2%K;
	}
	_P("! %lld\n",ret%K);
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


Measured memory_bytes: 596
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef pair<int,int> P;
typedef long long ll;


int main(void){
    ll x,ans=0;
    cin>>x;
    if((x+5)%11==0)ans=(x+5)/11*2-1;
    else ans=((x+5)/11+1)*2-1;
    if(x%11==0)ans=min(ans,x/11*2);
    else ans=min(ans,(x/11+1)*2);
    cout<<ans<<endl;
    return 0;
}

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll N;
    cin >> N;
    list<ll> A;
    rep(i, N) {
        ll temp;
        cin >> temp;
        A.push_back(temp);
    }
    A.sort();
    ll ANS = 0;
    {
        ll ans = 0;
        list<ll> B, temp;
        temp = A;
        rep(i, N) {
            if(i % 2 == 0) B.push_back(temp.back()), temp.pop_back();
            else B.push_back(temp.front()), temp.pop_front();
        }
        if(abs(B.back() - B.front()) > abs(*----B.end() - B.back())) B.push_front(B.back()), B.pop_back();
        vector<ll> C(B.begin(), B.end());
        rep(i, N - 1) ans += abs(C[i + 1] - C[i]);
        ANS = ans;
    }
    {
        ll ans = 0;
        list<ll> B, temp;
        temp = A;
        rep(i, N) {
            if(i % 2) B.push_back(temp.back()), temp.pop_back();
            else B.push_back(temp.front()), temp.pop_front();
        }
        if(abs(B.back() - B.front()) > abs(*----B.end() - B.back())) B.push_front(B.back()), B.pop_back();
        vector<ll> C(B.begin(), B.end());
        rep(i, N - 1) ans += abs(C[i + 1] - C[i]);
        if(ANS < ans) ANS = ans;
    }
    cout << ANS << endl;
    return 0;
}


Measured memory_bytes: 10668
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int A = scanner.nextInt();
        int B = scanner.nextInt();

        if(A%2!=0 && B%2!=0){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}


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

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

        if (H==0 and W==0){
            break;
        }

        
        for (int i=0; i<H; i++){
            if (i==0 or i==H-1){
                for (int j=0; j<W; j++){
                    cout << "#";
                }
                cout << endl;
            }
            else {
                cout << "#";
                for (int i=0; i<W-2; i++){
                    cout << ".";
                }
                cout << "#" << endl;
            }

        }

        cout << endl;

    }
}


Measured memory_bytes: 3072
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll a,b,c,x;
    cin>>a>>b>>c>>x;
    ll sum=0;
    for(ll i=0;i<=a;i++)
    {
        for(ll j=0;j<=b;j++)
        {
            for(ll k=0;k<=c;k++)
            {
                ll ANS=500*i+100*j+50*k;
                if(ANS==x)
                {
                    sum++;
                }
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

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

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

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
    long long x;
public:
    mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
    mint operator-() const {
        return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res *= a;
    }

    mint operator^(const ll& t)const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD - 2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res /= a;
    }

    friend ostream& operator<<(ostream& os, const mint& m) {
        os << m.x;
        return os;
    }
};

//////////////////////////
ll N,K;
vector<vector<ll>> g;
vector<ll> con;

ll dfs(ll v) {
    ll ret = 0;
    for (ll w : g[v]) {
        ret = max(ret, dfs(w));
    }
    ret++;
    if (ret == K) { ret = 0; con[v] = 1; }
    return ret;
}

int main() {
    cin >> N >> K;
    g.resize(N + 1);
    con.assign(N + 1, 0);
    ll ans = 0;
    
    vector<ll> a(N + 1);
    repn(i, N)cin >> a[i];

    if (a[1] != 1) { ans++; }
    for (ll i = 2; i <= N; i++) {
        g[a[i]].push_back(i);
        if (a[i] == 1) { con[i] = 1; }
    }

    for (ll w : g[1]) { dfs(w); }
    repn(v, N) { ans += con[v]; }
    ans -= g[1].size();
    cout << ans;
	system("PAUSE");
}

Measured memory_bytes: 12160
s = input()

ans="NO"
if s[0:7]=="keyence":
  ans="YES"
elif s[-8:-1]=="keyence":
  ans="YES"
elif s[0:1]=="k" and s[-6:]=="eyence":
  ans="YES"
elif s[0:2]=="ke" and s[-5:]=="yence":
  ans="YES"
elif s[0:3]=="key" and s[-4:]=="ence":
  ans="YES"
elif s[0:4]=="keye" and s[-3:]=="nce":
  ans="YES"
elif s[0:5]=="keyen" and s[-2:]=="ce":
  ans="YES"
elif s[0:6]=="keyenc" and s[-1:]=="e":
  ans="YES"

print(ans)

Measured memory_bytes: 3064
#include <stdio.h>

int main() {
int N,K,s,i;
s=1;
scanf("%d",&N);
scanf("%d",&K);
for (i=0;i<N;i++) {
 if (s<K) s=s*2;
 else s=s+K;
} 
printf("%d\n",s);
return 0;
}

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

int main() {
    double l,a,b,c; cin>>l; a=l/3; b=l/3; c=l/3;
    cout<<fixed<<setprecision(10)<<(a*b*c);
	return 0;
}

Measured memory_bytes: 3780
#include <stdio.h>
typedef long long ll;
using namespace std;

int main(void) {
  ll i, j, k, n, ans = 0;
  scanf("%lld", &n);
  ll dp[n + 1];
  for(i = 0; i <= n; ++i) dp[i] = 0;
  for(i = 2; i <= n; ++i) {
    ll now = i;
    for(j = 2; j <= n; ++j) if(!(now % j)) {
      while(!(now % j)) dp[j]++, now /= j;
      if(now == 1) break;
    }
  }
  for(i = 2; i <= n; ++i) if(dp[i] >= 74) ++ans;
  for(i = 2; i <= n; ++i) for(j = i + 1; j <= n; ++j) {
    if(dp[i] >= 14 && dp[j] >= 4) ++ans;
    if(dp[j] >= 14 && dp[i] >= 4) ++ans;
    if(dp[i] >= 24 && dp[j] >= 2) ++ans;
    if(dp[j] >= 24 && dp[i] >= 2) ++ans;
  }
  for(i = 2; i <= n; ++i) for(j = i + 1; j <= n; ++j) for(k = j + 1; k <= n; ++k) {
    if(dp[i] >= 4 && dp[j] >= 4 && dp[k] >= 2) ++ans;
    if(dp[i] >= 4 && dp[j] >= 2 && dp[k] >= 4) ++ans;
    if(dp[i] >= 2 && dp[j] >= 4 && dp[k] >= 4) ++ans;
  }
  printf("%lld", ans);
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    ll n;
    cin>>n;
    cout<<(n-1)*n/2;
}

Measured memory_bytes: 3600
// Knapsack 2
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define times(t) int t; cin >> t; while(t--)
#define mod 1000000007
#define pb push_back
#define mp make_pair

const int MAXV = 1e5 + 10;
const int MAXN = 1e3 + 10;
const ll INF = 1e13;

ll dp[MAXV];
int N, W, V, wi[MAXN], vi[MAXN];

void solve() {
	cin >> N >> W;
	for(int i=1; i<=N; i++) {
		cin >> wi[i] >> vi[i];
		V += vi[i];
	}
	for(int i=1; i<=V; i++) dp[i] = INF;
	dp[0]=0;
	for(int item=1; item<=N; item++) {
		int w=wi[item], v=vi[item];
		for(int i=V; i>=v; i--) {
			dp[i] = min(dp[i], dp[i-v]+w);
		}
	}
	for(int i=V; i>=0; i--) {
		if(dp[i] <= W) {
			cout << i << endl;
			break;
		}
	}
	return;
}

int main() {
	IOS;
	// #ifndef ONLINE_JUDGE
	// freopen("C:/Gen code/CompProg/input.txt", "r", stdin);
	// freopen("C:/Gen code/CompProg/output.txt", "w", stdout);
	// #endif
	solve();
	return 0;
}

Measured memory_bytes: 4436
/**< 🤣🤣🤣 */
#include<bits/stdc++.h>
using namespace  std ;
typedef long long  ll;
typedef pair<ll, ll> Pll;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define deb(X) cout << (X) << endl
#define ASH(X) cout << (X) << " "
#define debug(x) cout << #x << " " << x << endl;
#define debug2(x,y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define FOR(I,A,B) for(ll I = (A); I <= (B); I++)
#define RFOR(I,B,A) for(ll I = (B); I >=(A); I--)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define SORT(c) (sort(c.begin(),c.end()))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASES int ___T; cin >> ___T; for(int cs=1;cs<=___T;cs++)
#define FAST()  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//template<typename T>T gcd(T x, T y){if(y==0)return x;else return gcd(y,x%y);}
ll fx[] = {1, 1, 0, -1, -1, -1, 0, 1};
ll fy[] = {0, 1, 1, 1, 0, -1, -1, -1};
vector<ll>a1,b1,c1;


/************************************************/

void Execute()
{   ll x, l,y;
   cin >> x >> l;
   bool ok = false;
   FOR(i,1,x)
   {
       y = x-i;
       if((i*2)+(y*4)==l || ((i*4)+(y*2))==l){ ok  = true; break;}

   }
   if(ok==false) deb("No"); else deb("Yes");

}

int32_t main()
{   //  #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    //#endif

    FAST();
    //CASES
   // {
     Execute();
        //cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "sec \n";
    //}
}
/**< 🤣🤣🤣 */


Measured memory_bytes: 3640
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
#include<set>

using namespace std;

typedef pair<int,char> P;

int main()
{
	string s;
	while(cin>>s,s!="#"){
	
		//notじゃないのを入れる,notのを入れる
		set<char> sc1,sc2;
		bool ok=true;
		for(int i=0;i<s.size();i++){
			if(s[i]=='('){ok=true; sc1.clear(); sc2.clear();}
			if(s[i]==')' && ok) break;
			if('a'<=s[i] && s[i]<='z' || 'A'<=s[i] && s[i]<='Z'){
				if(s[i-1]=='~'){
				 if(sc1.find(s[i])!=sc1.end()) ok=false;
				 else sc2.insert(s[i]);
				}else{
					if(sc2.find(s[i])!=sc2.end()) ok=false;
					else sc1.insert(s[i]);
				}

			}
			
		}
	
		if(ok) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	
	}
	
	return 0;
}

Measured memory_bytes: 1188
import sys
from random import randint
from datetime import datetime

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]

def randomize(n):
    return [ randint(1, 26) for _ in range(n) ]

def get_now():
    dt = datetime.now()
    return dt.second + dt.microsecond/1000000

def main():
    start = get_now()
    end = get_now()
    # 開催日数
    D = ii()
    # 満足度の下がりやすさ
    C = lmi()
    # 得られる満足度
    S = lmif(D)
    # 開催するコンテスト
    T = [ i%26+1 for i in range(D) ]

    def scoring():
        # 最後に開かれた日付
        last = [0]*26
        score = 0
        for i in range(D):
            score += S[i][T[i]-1]
            last[T[i]-1] = i+1
            for j in range(26):
                score -= C[j] * (i+1 - last[j])
        return score

    max_score = scoring()
    # print(max_score)
    ans = T.copy()
    # a = 0
    while end - start < 1.95:
        idx, value = randint(0, D-1), randint(1, 26)
        old = T[idx]
        T[idx] = value
        score = scoring()
        if max_score < score:
            ans = T.copy()
            max_score = score
        else:
            T[idx] = old
        # if a % 10000 == 0:
        #     print(a, *T, score, max_score)
        # a += 1
        end = get_now()
    print(*ans, sep='\n')

main()


Measured memory_bytes: 9896
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>

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

using namespace std;

int mat[6][6];
int col[6],row[6];

int main(void){
  int h,w,k;
  cin >> h >> w >> k;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      char c;
      cin >> c;
      if(c == '#')mat[i][j] = 1;
      else mat[i][j] = 0;
    }
  }

  int ans = 0;
  for(int i=0;i<(1<<h);i++){
    int work = i;
    for(int o=0;o<6;o++){
      if(work%2 == 0)col[o] = 0;
      else col[o] = 1;
      work /= 2;
    }

    /*    cout << i << " round" << endl;
    rep(o,6) cout << col[o] << " ";
    cout << endl;
    */
    for(int j=0;j<(1<<w);j++){
      int work1 = j;
      for(int l=0;l<6;l++){
	if(work1%2 == 0)row[l] = 0;
	else row[l] = 1;
	work1 /= 2;
      }
      /*
      cout << "   " << j << " round" << endl << "   ";
      rep(l,6) cout << row[l] << " ";
      cout << endl;
      */
      
      int num = 0;
      for(int l=0;l<h;l++){
	for(int m=0;m<w;m++){
	  if(mat[l][m] == 1 && col[l] == 0 && row[m] == 0)num++;
	}
      }

      if(num == k)ans++;
    }
  }

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


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

int main(void){
    int n;
    cin >> n;
    vector<int> vec(n);
    for(int i=0;i<n;i++) cin >> vec.at(i);
    sort(vec.begin(),vec.end());
    int biggest = 0;
    int count = 0;
    for(int i=0;i<n;i++){
        if(vec.at(i)>biggest){
            count++;
            //cout << vec.at(i) << endl;
            biggest = vec.at(i);
        }
    }
    cout << count << endl;
}

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

int main() {
  string s;
  cin >> s;
  if (s[0] != s[1] && s[1] != s[2] && s[2] != s[3]) {
    cout << "Good" << endl;
  } else {
    cout << "Bad" << endl;
  }
  return 0;
}


Measured memory_bytes: 3604
require 'matrix'

p0, *points = $<.readlines.map {|l| Vector[*l.split(",").map(&:to_f)]}
result = points.each_cons(2).map do |p1, p2|
  a = (p1 - p0).norm
  b = (p2 - p0).norm
  c = (p2 - p1).norm
  z = (a + b + c) / 2
  Math.sqrt(z * (z - a) * (z - b) * (z - c))
end.sum
puts result


Measured memory_bytes: 7160
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author cherrypi59
 */

#pragma GCC optimize ("O2")
#pragma GCC optimize ("tree-vectorize")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define FFOR(i, a, b) FOR(i, a, b+1)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FOR(i, 1, n+1)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define LEN(x) (int)(x).size()
#define DUMP(x) cerr<<__LINE__<<' '<<#x<<"="<<(x)<<endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

using namespace std;
using lint = long long;
using pii =  pair<int, int>;
using pll =  pair<lint, lint>;
template <typename T> using vc = vector<T>;
template <typename T> using vvc = vector<vector<T>>;
template <typename T> inline bool chmax(T &a, T b){if(a<b){a = b; return true;} return false;}
template <typename T> inline bool chmin(T &a, T b){if(a>b){a = b; return true;} return false;}

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

class DReplacing {
public:
    void solve(std::istream& in, std::ostream& out){
        ios_base::sync_with_stdio(false);
        in.tie(nullptr), out.tie(nullptr);
        int N, Q;
        in>>N;
        const int MX = ten(5);
        vc<lint> cnt(MX+1, 0);
        REP(i, N){
            int a;
            in>>a;
            cnt[a]++;
        }
        in>>Q;
        lint ans = 0;
        REP(i, MX+1) ans += i * cnt[i];
        while(Q--){
            int b, c;
            in>>b>>c;
            ans -= b * cnt[b];
            ans += c * cnt[b];
            cnt[c] += cnt[b];
            cnt[b] = 0;
            out<<ans<<'\n';
        }
    }
};


int main() {
	DReplacing solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 3940
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
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;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (int i = 0; i < sz(v); i++) {
        os << v[i];
        if (i < sz(v) - 1) os << " ";
    }
    return os;
}
int main() {
    int n;
    cin >> n;
    vector<pair<int, int>> x(n);
    vector<int> xx(n);
    rep(i, n) {
        cin >> x[i].first, x[i].first--;
        x[i].second = i + 1;
        xx[i] = x[i].first;
    }
    vector<int> ans(n * n, -1);
    sort(all(x));
    queue<int> q, qq;
    for (auto p : x) {
        int idx, num;
        tie(idx, num) = p;
        ans[idx] = num;
        rep(_, num - 1) q.push(num);
        rep(_, n - num) qq.push(num);
    }

    rep(i, n * n) {
        if (ans[i] != -1) {
            if (q.front() == ans[i]) {
                cout << "No" << endl;
                return 0;
            }
        } else if (sz(q)) {
            ans[i] = q.front();
            q.pop();
        }
    }
    rep(i, n * n) {
        if (ans[i] == -1) {
            ans[i] = qq.front();
            qq.pop();
        }
    }

    vector<int> cnt(n), xxx(n, INF);
    rep(i, n * n) {
        cnt[ans[i] - 1]++;
        rep(j, n) {
            if (cnt[j] == j + 1) {
                chmin(xxx[j], i);
            }
        }
    }
    rep(i, n) {
        if (xx[i] != xxx[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    cout << ans << endl;
}

Measured memory_bytes: 4224
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
long long int dp[401][401];
long long int helper(vector<long long int>&slimes,vector<long long int>&prefix,int i,int j){
	if(i > j){
		return 1000000000000000;
	}
	if(i == j){
		return 0;
	}
	if(dp[i][j] > -1){
		return dp[i][j];
	}
	else{
		long long int ans = 1000000000000000;
		for(int k = i;k < j;k++){
			ans = min(ans,prefix[j]-prefix[i-1]+helper(slimes,prefix,i,k)+helper(slimes,prefix,k+1,j));
		}
		dp[i][j] = ans;
		return ans;
	}
}
int main(){
	long long int n;cin>>n;
	vector<long long int>slimes(n+1);
	vector<long long int>prefix(n+1);
	prefix[0] = 0;
	for(int i=1;i<=n;i++){
		cin>>slimes[i];
		prefix[i] = prefix[i-1]+slimes[i];
	}
	for(int i=0;i<401;i++){
		for(int j=0;j<401;j++){
			dp[i][j] = -1;
		}
	}
	cout<<helper(slimes,prefix,1,n);
}


Measured memory_bytes: 1536
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const ll MOD = 1e9+7;
ll LLINF = 1LL << 60;
int INF = INT_MAX;

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

    rep(bit,0,(1<<3)){
        int ans=s[0]-'0';
        string bun="";
        bun+=s[0];
        rep(i,0,3){
            if(bit & (1<<i)){
                ans+=(s[i+1]-'0');
                bun+='+';
            }else{
                ans-=(s[i+1]-'0');
                bun+='-';
            }
            bun+=s[i+1];
        }
        if(ans==7){
            bun+="=7";
            cout<<bun<<endl;
            return 0;
        }
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <map>
#include <queue>
#define INF 10000000

using namespace std;

int main(){
  int n;
  string str,odr;
  cin >> n;
  while(n--){
    cin >> odr;
    cin >> str;
    int size = str.size();
    string tmp = "";
    for(int i = odr.size()-1 ; i >= 0 ; i-- ){
      switch(odr[i]){
      case 'A':
	reverse(str.begin(),str.end());
	break;
      case 'C':
	tmp = "";
	for(int i = 1 ; i < size ; i++ ){
	  tmp += str[i];
	}
	str = tmp+str[0];
	break;
      case 'J':
	tmp = "";
	for(int i = 0 ; i < size-1 ; i++ ){
	  tmp += str[i];
	}
	str = str[size-1] + tmp;
	break;
      case 'M':
	for(int i = 0 ; i < size ; i++ ){
	  if('0' <= str[i] && str[i] <= '8'){
	    str[i] = char(str[i]+1);
	  }else if(str[i] == '9'){
	    str[i] = '0';
	  }
	}
	break;
      case 'P':
	for(int i = 0 ; i < size ; i++ ){
	  if('1' <= str[i] && str[i] <= '9'){
	    str[i] = char(str[i]-1);
	  }else if(str[i] == '0'){
	    str[i] = '9';
	  }
	}
	break;
      case 'E':
	if(size%2==1){
	  str = str.substr((size/2)+1,size - size/2)+ str[size/2] + str.substr(0,(size/2)); 
	}else {
	  str = str.substr((size/2),size - size/2) + str.substr(0,(size/2));
	}
	break;
      }
    }
    cout << str << endl;
  }
  return 0;
}

Measured memory_bytes: 1212
#include<iostream>
#include<utility>

using namespace std;

#define allof(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;



int main(){
    cin.tie(0);
    cin.sync_with_stdio(0);

    int x, a;
    cin >> x >> a;
    if (x < a) {
        cout << 0;
    } else {
        cout << 10;
    }
}


Measured memory_bytes: 256
a, b, c, d = map(int, input().split())

if abs(a - c) <= d:
    print('Yes')
elif (a < b and b < c) or (a > b and b > c):
    if abs(a - b) <= d and abs(b - c) <= d:
        print('Yes')
    else:
        print('No')
else:
    print('No')

Measured memory_bytes: 9192
s = list(input())
tile_len = len(s)
result = [0 for _ in range(tile_len)]
right_accum = 0
right_index = 0
left_accum = 0
for i in range(tile_len):
    if s[i] == 'R':
        left_accum = 0
        right_accum += 1
        right_index = i
    elif left_accum == 0:
        result[right_index] += right_accum - right_accum // 2
        result[right_index+1] += right_accum // 2 + 1 # +1 は自分の分
        right_accum = 0
        left_accum = 1
    else:
        left_accum += 1
        if left_accum % 2 == 0:
            result[right_index] += 1
        else:
            result[right_index+1] += 1
print(*result)

Measured memory_bytes: 7088
#include<bits/stdc++.h>
using namespace std;
#define bs ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define pb push_back

#define rep(var,start,end)       for(int var=start;var<end;var++)
#define erep(var,start,end)      for(int var=start;var<=end;var++)
#define rrep(var,start,end)      for(int var=start;var>=end;var--)

#define pa(x)                    cout<<x<<endl

#define pii                      pair<int,int>
#define pci                      pair<char,int>
#define pll                      pair<ll,ll>
#define test                     int t;cin>>t;while(t--)

ll modularExponentiation(ll x,ll n,ll M){
    if(n==0)
        return 1;
    else if(n%2 == 0)
        return modularExponentiation((x*x)%M,n/2,M);
    return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
}
template<class T> 
T power(T n, T p){ if(p==0) return 1; else return power(n,p-1)*n;} 

void solve(){
    ll a,k;
    cin>>a>>k;
    a=a%k;
  cout<<min(a,abs(a-k));
}

int main()
{
    bs;
    solve();
    
}


Measured memory_bytes: 256