akhauriyash/Code-Regression huggingface dataset:
#include<algorithm>     //sort,二分探索,など
#include<bitset>        //固定長bit集合
#include<cmath>         //pow,logなど
#include<complex>       //複素数
#include<deque>         //両端アクセスのキュー
#include<fstream>       //ファイルストリーム（標準入力変更用）
#include<functional>    //sortのgreater
#include<iomanip>       //setprecision(浮動小数点の出力の誤差)
#include<iostream>      //入出力
#include<iterator>      //集合演算(積集合,和集合,差集合など)
#include<map>           //map(辞書)
#include<numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include<queue>         //キュー
#include<set>           //集合
#include<stack>         //スタック
#include<string>        //文字列
#include<unordered_map> //イテレータあるけど順序保持しないmap
#include<unordered_set> //イテレータあるけど順序保持しないset
#include<utility>       //pair
#include<vector>        //可変長配列

//名前
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef map<string, int> msi;
typedef map<string, ll> msll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pllll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<string>> vvs;
typedef vector<vector<bool>> vvb;

//定数
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;

//マクロ
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,s,e) for(int i=s;i<e;i++)
#define repse(i,s,e) for(int i=s;i<=e;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define in(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define inN(x, N) rep(i, N) in(x[i])
#define outl(x) cout << x << endl
#define out2l(x, y) cout << x << " " << y << endl 

//よく使う関数
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

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

    //標準入力をファイルに変更
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());
    ll X, K, D;
    in3(X, K, D);

    if (X < 0) X *= -1;

    ll L = X / D;

    if (K >= L)
    {
        X -= D * L;
        K -= L;

        if (K % 2 == 1) X = abs(X - D);
    }
    else
    {
        X -= D * K;
    }

    outl(X);

    return 0;
}

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

int main() {
    int N;
    cin >> N;

    vector<int> A(N);
    for (int i=0; i<N; i++) {
        cin >> A.at(i);
    }

    int flag = 1, cnt = 0;
    int i = 0;
    while (flag) {
        flag = 0;
        for (int j = N-1; j >= i+1; j--) {
            if (A.at(j-1) > A.at(j)) {
                int tmp = A.at(j-1);
                A.at(j-1) = A.at(j);
                A.at(j) = tmp;

                flag = 1;
                cnt++;
            }
        }
        i++;
    }

    // output
    for (int i=0; i<N-1; i++) cout << A.at(i) << " ";
    cout << A.at(N-1) << endl;
    cout << cnt << endl;

    return(0);
}


Measured memory_bytes: 3112
#include <bits/stdc++.h>
 
using namespace std;
using ll=long long;
 
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m);i>=(int)(n);--i)
#define rrep(i,m) RREP(i,m,0)
#define debug(x) cout << #x << "=" << (x) << endl;
const int MOD=1e9+7;
 
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;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
 
void solve(){
  int num;
  cin >> num;
  int v[num];
  rep(i,num){
    int a;
    cin >> a;
    v[--a]=i+1;
  }
  rep(i,num)
    cout << v[i] << " ";
}
 
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  solve();
  return 0;
}

Measured memory_bytes: 1280
# マスの取り出し
def island(map)
  count = 0
  12.times do |i|
    12.times do |j|
      if map[i][j] == 1
        move(i,j,map)
        count += 1 
      end
    end    
  end
  return count
end

# 移動、何が原因で答えが合わないのか分からない、どうデバッグすればいい？
def move(i, j, map)
  map[i][j] = 0
  [[1, 0],[0, 1],[-1, 0],[0,-1]].each do |x,y|
    #puts i+x
    #puts j+y
    next if i+x < 0 || i+x >= 12 || j+y == -1 || j+y == 12
    if map[i+x][j+y] == 0
     # puts map 
     # puts "next"
      next
    end 
    # 1の時 
    move(i+x, j+y, map)
    #puts "map"
  end  
  return
end

# 入力
map = []
12.times do
  map << gets.chomp.split("").map(&:to_i)
end
#print map 
#print "\n"
puts island(map)

while gets
  map = []
  12.times do
    map << gets.chomp.split("").map(&:to_i)
  end
  puts island(map)
end





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

vector< int > v[26];

int main() {
    vector< string > v;
    int n, k;
    cin >> n >> k;
    while(n--) {
        string s;
        cin >> s;
        v.emplace_back(s);
    }

    sort(v.begin(), v.end());
    for(auto s: v) cout << s;
        cout << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;
//import java.util.Map;
//import java.util.HashMap;

public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int a = in.nextInt()-1;
		int b = in.nextInt()-1;
		System.out.println(a*b);
	}
}


Measured memory_bytes: 21972
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from collections import deque

UNVISITED, VISITED_IN_QUEUE, POPPED_OUT = 0, 1, 2


def generate_adj_matrix(v_info):
    for v_detail in v_info:
        v_adj_list = v_detail[2:]
        # assert len(v_adj_list) == int(v_detail[1])
        for each in v_adj_list:
            init_adj_matrix[int(v_detail[0]) - 1][int(each) - 1] = 1
    return init_adj_matrix


def graph_bfs(v_init):
    vertices_status_list[v_init] = VISITED_IN_QUEUE
    vertices_distance_list[v_init] = 0
    vertices_visit_queue.appendleft(v_init)

    while vertices_visit_queue:
        current_vertex = vertices_visit_queue.popleft()
        for v in range(1, vertices_num + 1):
            if adj_matrix[current_vertex - 1][v - 1] and vertices_status_list[v] is UNVISITED:
                vertices_status_list[v] = VISITED_IN_QUEUE
                vertices_distance_list[v] = vertices_distance_list[current_vertex] + 1
                vertices_visit_queue.append(v)
        vertices_status_list[current_vertex] = POPPED_OUT
    return vertices_distance_list


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num = int(_input[0])
    vertices_info = list(map(lambda x: x.split(), _input[1:]))
    init_adj_matrix = [[0] * vertices_num for _ in range(vertices_num)]
    # assert len(vertex_info) == vertex_num

    # config length = (vertex_num + 1)
    vertices_visit_queue = deque()
    vertices_status_list = [UNVISITED] * vertices_num
    vertices_status_list.insert(0, -1)
    vertices_distance_list = [-1] * (vertices_num + 1)

    adj_matrix = generate_adj_matrix(vertices_info)
    ans = graph_bfs(v_init=1)
    for j in range(vertices_num):
        print(j + 1, ans[j + 1])

Measured memory_bytes: 8080
/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2019-12-05 17:42:39
**/

#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <bitset>
#include <cassert>
#include <cctype>
#include <functional>
#include <iomanip>
#include <random>
#include <utility>

using namespace std;

using int64 = long long;
#define debug(_) cerr << #_ << ": " << (_) << '\n'
template<class T> ostream& operator<<(ostream& os, vector<T>& _) { os << '{'; for (T& x : _) { os << x; if (&x != &_.back()) os << ','; } os << '}'; return os; }
template<class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { return os << '{' << p.first << " " << p.second << '}'; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, W; cin >> n >> W;
  vector<int64> w(n), v(n);
  vector<pair<int64, int64>> w0, w1, w2, w3;
  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
    if (w[i] == w[0]) {
      w0.emplace_back(v[i], w[i]);
    } else if (w[i] == w[0] + 1) {
      w1.emplace_back(v[i], w[i]);
    } else if (w[i] == w[0] + 2) {
      w2.emplace_back(v[i], w[i]);
    } else {
      w3.emplace_back(v[i], w[i]);
    }
  }

  sort(w0.rbegin(), w0.rend());
  sort(w1.rbegin(), w1.rend());
  sort(w2.rbegin(), w2.rend());
  sort(w3.rbegin(), w3.rend());

  int64 ans = 0;

  for (int i = 0; i <= n; ++i) {
    for (int j = i; j <= n; ++j) {
      for (int k = j; k <= n; ++k) {
        for (int l = k; l <= n; ++l) {
          int64 n0 = i, n1 = j - i, n2 = k - j, n3 = l - k;
          //cerr << n0 << " " << n1 << " " << n2 << " " << n3 << '\n';
          int64 sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
          for (int m = 0; m < min(n0, static_cast<int64>(w0.size())); ++m) {
            sum0 += w0[m].first;
          }
          for (int m = 0; m < min(n1, static_cast<int64>(w1.size())); ++m) {
            sum1 += w1[m].first;
          }
          for (int m = 0; m < min(n2, static_cast<int64>(w2.size())); ++m) {
            sum2 += w2[m].first;
          }
          for (int m = 0; m < min(n3, static_cast<int64>(w3.size())); ++m) {
            sum3 += w3[m].first;
          }
          if (w[0] * n0 + (w[0] + 1) * n1 + (w[0] + 2) * n2 + (w[0] + 3) * n3 <= W){
            ans = max(ans, sum0 + sum1 + sum2 + sum3);
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define PI (acos(-1))

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i];if(i<len-1) s<<" ";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i]<<endl;
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){
	s<<"{ ";
	for(auto itr=v.begin();itr!=v.end();++itr) {
		if (itr!=v.begin()) {s<< ", ";}
		s<<(*itr);
	}
	s<<" }";
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){
	s<<"{"<<endl;
	for(auto itr=m.begin();itr!=m.end();++itr){
		s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;
	}
	s<<"}"<<endl;
	return s;
}

const ll mod=1e9+7;
const ll inf=1e15;
const int INF=1e9;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,k;
	cin>>n>>k;
	vector<int> h(n),dp(n,INF);
	rep(i,n){
		cin>>h[i];
	}
	dp[0]=0;
	rep(i,n){
		FOR(j,1,k+1){
			if(i+j<n) chmin(dp[i+j],dp[i]+abs(h[i]-h[i+j]));
		}
	}
	cout<<dp[n-1]<<endl;
}

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

int main() {
    int n; cin >> n;
    vector<int> a(n+1);
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    a[n] = 1e4;
    
    sort(a.begin(),a.end());
    
    int ans = 0;

    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            if(j+1 > n-1) break;

            int l = j;
            int r = n;

            while(l < r -1){
                int m = l + (r-l)/2;
                if(a[i] + a[j] > a[m]) l = m;
                else r = m;
            }

            ans += l-j;

        }
    }
    cout << ans << endl;

}

Measured memory_bytes: 3640
using System;
using System.Collections.Generic;
using System.Linq;

class Solution
{
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var v = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var a = new Dictionary<int, int>();
        var b = new Dictionary<int, int>();

        for (int i = 0; i < v.Length; i++)
        {
            if (i % 2 == 0)
            {
                if (!a.ContainsKey(v[i]))
                {
                    a[v[i]] = 0;
                }
                a[v[i]]++;
            }
            else
            {
                if (!b.ContainsKey(v[i]))
                {
                    b[v[i]] = 0;
                }
                b[v[i]]++;
            }
        }

        var a1 = a.OrderByDescending(x => x.Value).First();
        var a2 = new KeyValuePair<int, int>(-1, 0);
        if (a.Any(x => x.Key != a1.Key))
        {
            a2 = a.Where(x => x.Key != a1.Key).OrderByDescending(x => x.Value).First();
        }
        var b1 = b.OrderByDescending(x => x.Value).First();
        var b2 = new KeyValuePair<int, int>(-1, 0);
        if (b.Any(x => x.Key != b1.Key))
        {
            b2 = b.Where(x => x.Key != b1.Key).OrderByDescending(x => x.Value).First();
        }

        int result;
        if (a1.Key != b1.Key || a1.Value == a2.Value || b1.Value == b2.Value)
        {
            result = (n / 2 - a1.Value) + (n / 2 - b1.Value);
        }
        else
        {
            if (a1.Value == b1.Value)
            {
                result = Math.Min(n / 2 - a2.Value, n / 2 - b2.Value) + (n / 2 - a1.Value);
            }
            else if (a1.Value > b1.Value)
            {
                result = (n / 2 - a1.Value) + (n / 2 - b2.Value);
            }
            else
            {
                result = (n / 2 - a2.Value) + (n / 2 - b1.Value);
            }
        }

        Console.WriteLine(result);
    }
}

Measured memory_bytes: 29536
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(false),cin.tie(nullptr);
#define task "random"
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))

const int N=310;

int n,m,l;
ii f[N][N];
vector<ii> ad[N];

main(){
    //freopen(task".inp","r",stdin);
    //freopen(task".out","w",stdout);

    n=in;
    vector<int> a(n+1);
    forinc(i,1,n) a[i]=in;
    forinc(i,1,n)
        forinc(j,i+1,n) m+=a[i]*a[j];
    cout<<m;

    return 0;

    n=in,m=in,l=in;
    forinc(i,1,m){
        int u=in,v=in,c=in;
        if(c>l) continue;
        ad[u].pb({v,c});
        ad[v].pb({u,c});
    }

    forinc(i,1,n)
        forinc(j,1,n)
            f[i][j]=i==j?ii(0,-l):ii(n+1,0);
    forinc(i,1,n){
        using w=tuple<int,int,int>;
        priority_queue<w,vector<w>,greater<w>> q;
        q.push(mt(0,-l,i));
        int c,g,u;
        while(q.size()){
            tie(c,g,u)=q.top(); q.pop();
            //cout<<f[i][u].fi<<" "<<f[i][u].se<<"\n";
            if(f[i][u]<ii(c,g)) continue;
            //cerr<<c<<" "<<g<<" "<<u<<"\n";
            forv(v,ad[u]){
                int C,G;
                if(-g>=v.se) C=c,G=-g-v.se;
                else C=c+1,G=l-v.se;
                if(f[i][v.fi] > ii(C,-G)){
                    f[i][v.fi]=ii(C,-G);
                    q.push(mt(C,-G,v.fi));
                }
            }
        }
        //cout<<f[5][2].fi;
        ///return 0;
        //return 0;
    }

    forinc(i,1,in){
        int u=in,v=in;
        cout<<(f[u][v].fi>n?-1:f[u][v].fi)<<"\n";
    }
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Main {
	public static void main(String[] a) throws IOException {
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in),
				1);
		List<Integer> l = new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			l.add(Integer.valueOf(r.readLine()));
		}
		Collections.sort(l);
		for (int i = l.size() - 1; i >= l.size() - 3; i--) {
			System.out.println(l.get(i));
		}
	}
}

Measured memory_bytes: 9364
s = input()
flag = True
if "N" in s:
    if "S" not in s:
        flag = False
if "S" in s:
    if "N" not in s:
        flag = False
if "E" in s:
    if "W" not in s:
        flag = False
if "W" in s:
    if "E" not in s:
        flag = False
if flag:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#include <cstdlib>  // abs() for integer 絶対値求めやつ
#include <cmath>    // abs() for float, and fabs()
#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i = 0; i < (n);i++)
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
using namespace std;
#define INF 2000000000
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; }
#define che(a,string) cout<<"//"<<string <<"=="<<(a)<<"//" <<endl;

int x , y , z ;
int main(){
	cin >> x >> y >> z;
	x-=z;
	cout<< x/(y+z)<<endl;
	
}



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

using namespace std;

int main() {

	int N,i,j,k,l,count=0;

	while (cin >> N) {
		for (i = 0; i <= 9; i++) {
			for (j = 0; j <= 9; j++) {
				for (k = 0; k <= 9; k++) {
					for (l = 0; l <= 9; l++) {
						if (N == i + j + k + l)
							count++;
					}
				}
			}
		}
		cout << count << endl;
		count = 0;
	}
	
	return 0;

}

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

int n;
int c[20], v[20];
int ans;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n;
	for(int i=0; i<n; ++i) cin >> v[i];
	for(int i=0; i<n; ++i) cin >> c[i];
	for(int i=0; i<n; ++i){
		if(v[i] - c[i] > 0){
			ans += v[i] - c[i];
		}
	}
	cout << ans << "\n";
}

Measured memory_bytes: 256
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <numeric>

using namespace std;
#define ALL(x)			(x).begin(),(x).end()
#define REP(i, n)		for (int (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n)	for (int (i) = m ; (i) < (ll)(n) ; ++(i))
#define INF				(int)2e9
#define MOD				(1000 * 1000 * 1000 + 7)
#define Ceil(x, n)		(((((x))+((n)-1))/n))		/* Nの倍数に切り上げ割り算 */
#define CeilN(x, n)		(((((x))+((n)-1))/n)*n)		/* Nの倍数に切り上げ */
#define FloorN(x, n)	((x)-(x)%(n))				/* Nの倍数に切り下げ */
#define IsOdd(x)		(((x)&0x01UL) == 0x01UL)			
#define IsEven(x)		(!IsOdd((x)))						
#define M_PI			3.14159265358979323846
typedef long long		ll;
typedef pair<ll, ll>	P;
typedef vector<ll>		VLL;
typedef vector<VLL>		VVLL;
typedef vector<VVLL>	VVVLL;

/*-----------------------------------------------------------------------------
　処理
 -----------------------------------------------------------------------------*/
int main()
{
	VLL a(3);
	REP(i, 3) cin >> a[i];
	sort(ALL(a));

	if ((a[0] == a[1])&&(a[1] != a[2])) {
		cout << "Yes" << endl;
	} else if ((a[0] != a[1])&&(a[1] == a[2])) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
	return 0;
}


Measured memory_bytes: 256
import std.stdio : writeln;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;

immutable INF = 1 << 27;
immutable N = 100;
int n, m;
int[ N + 1 ][ N + 1 ] adj;
int[ N + 1 ][ N + 1 ] two_link;

void init(){
	init_d_cost();
	init_two_link();
}

void init_two_link(){
	for( int i = 1; i <= n ; ++i ){
		for( int j = i + 1; j <= n ; ++j ){
			for( int k = 1; k <= n ; ++k ){
				if( adj[ i ][ k ] * adj[ k ][ j ] ){
					two_link[ i ][ j ] = two_link[ j ][ i ] = k;
				}
			}
		}
	}
}

int[ N + 1 ] d_cost;
void init_d_cost(){
	d_cost = repeat( INF ).take( N + 1 ).array;
	
	d_cost[ n ] = 0;
	int e = n;
	bool[ N + 1 ] done;
	while( e ){
		done[ e ] = true;
		
		for( int i = 1; i <= n; ++i ){
			if( !done[ i ] && adj[ e ][ i ] ){
				int res = d_cost[ e ] + adj[ e ][ i ];
				if( d_cost[ i ] > res ){
					d_cost[ i ] = res;
				}
			}
		}
		
		int min_ = INF;
		int min_idx = 0;
		for( int i = 1; i <= n; ++i ){
			if( !done[ i ] && min_ > d_cost[ i ] ){
				min_ = d_cost[ i ];
				min_idx = i;
			}
		}
		
		e = min_idx;
	}
}

int ans = INF;
bool[ N + 1 ] s_done;
int[ int ] s_note;
void search( int e, int sum ){
	if( e == n ){
		ans = min( ans, sum );
		return;
	}
	
	if( sum >= ans ){
		return;
	}
	
	if( ( e in s_note ) != null ){
		int res = s_note[ e ];
		if( sum >= res ){
			return;
		}
	}
	s_note[ e ] = sum;

	s_done[ e ] = true;
	
	for( int i = 1; i <= n; ++i ){
		if( two_link[ e ][ i ] ){
			int res = sum + d_cost[ i ];
			ans = min( ans, res );
		}
	}
	
	for( int i = 1; i <= n; ++i ){
		if( adj[ e ][ i ] ){
			if( !s_done[ i ] ){
				search( i, sum + adj[ e ][ i ] );
			}
		}
	}
	
	s_done[ e ] = false;
}

void main(){
	while( 1 ){
		n = next!int;
		m = next!int;
		if( ( n | m ) == 0 ){
			break;
		}
		
		ans = INF;
		adj = adj.init;
		two_link = two_link.init;
		s_done = s_done.init;
		s_note = s_note.init;
		
		foreach( i; m.iota ){
			int a = next!int;
			int b = next!int;
			int c = next!int;
			
			adj[ a ][ b ] = adj[ b ][ a ] = c;
		}
		
		init();
		search( 1, 0 );
		
		ans.writeln;
	}
}


import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
import std.traits;
string[] input;
string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}
	
	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}


void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}
	
	return v_;
}

Measured memory_bytes: 3192
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define all(a) a.begin(), a.end()
#define forn(l, r) for(int i = l; i < (r); ++i)
#define F first
#define S second
#define $ ios::sync_with_stdio(0);
///#define int long long int

using namespace std;
using namespace __gnu_pbds;
using ll = long long int;
using ii = pair<int, int>;
using li = pair<ll, int>;
using vi = vector<int>;
using graph = vector<vi>;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll mod =  1000000007;
const int INF = 0x3f3f3f3f;
const int MAXN = 112345;
// const double pi = 3.14159265359;
const double eps = 0.00000001;

int swp[MAXN];
ii gates[MAXN];

int32_t main() {
    $
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        cin >> gates[i].F >> gates[i].S;
        swp[gates[i].F]++;
        swp[gates[i].S + 1]--;
    }
    int ans = -INF, cnt = 0;
    for (int i = 1; i <= n; ++i) {
        swp[i] += swp[i - 1];
        if (swp[i] != m) {
            cnt = 0;
        }
        cnt += (swp[i] == m);
        ans = max(ans, cnt);
    }
    cout << ans << '\n';
}   



Measured memory_bytes: 1408
/*
* Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
*       Example:-   $ g++ -std=c++11 c++Template.cpp
*
* Author : Akshay Pratap Singh
* Handle: code_crack_01
*
*/
 
/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for(int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int  ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

int main(){

    int a, b, c, x, y;
    cin >> a >> b >> c >> x >> y;

    int na=0, nb=0, nc=0;

    ll res1 = min(a, 2*c)*x + min(b, 2*c)*y;

    int nc2 = min(2*x, 2*y);
    ll res2 = max(x-nc2/2, 0)*min(a, 2*c) + max(y-nc2/2, 0)*min(b, 2*c) + nc2*c;

    int nc3 = max(x, y)*2;
    ll res3 = c*nc3;

    ll res = min(res1, res2);
    res = min(res, res3);

    cout << res << endl;
    return 0;
}


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

using namespace std;

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for(int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef pair<int, int> pii;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;
typedef vector<pair<ll, ll>> vpll;


int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int l, r, d;
    cin >> l >> r >> d;
    int cnt = 0;
    forab(i, l, r + 1) {
        if (i % d == 0) cnt++;
    }
    cout << cnt << "\n";
    return 0;
}

Measured memory_bytes: 3608
import copy
from collections import deque,Counter
N,M,K=map(int,input().split())
friend=[[] for _ in range(N)]
block=[[] for _ in range(N)]
for i in range(M):                  #このようにすれば
    a,b=map(int,input().split())
    friend[a-1].append(b-1)         #友達リストも
    friend[b - 1].append(a - 1)
for i in range(K):
    a,b=map(int,input().split())
    block[a-1].append(b-1)          #ブロックリストも
    block[b - 1].append(a - 1)      #作れてしまう
#最初の例だとfriend:[[1,2][0,2][0,1,3][2]]block:[[][][][0]]となる
#木構造f[i][j]において、iの要素がf[i][j]の要素とつながっていることを意味する


#f=[list(map(int,input().split())) for _ in range(M)]
#b=[list(map(int,input().split())) for _ in range(K)]
groupid=[0]*N
l=[]
gone=[]
id=1
v=0
v1=0
v2=0
seen=[0]*N
jointelement=[0]*N
jointnum=0
#print(friend,block)
for i in range(N):
    id=i+1
    d=deque()
    if seen[i]==0:
        d.append(i)
    while len(d)>0:
        v=d.popleft()
        seen[v]=1
        groupid[v]=id
        #print(friend[v])
        for i in friend[v]:
            if (seen[i]==0)and(groupid[i]==0):
                d.append(i)
        #print(seen,d,groupid)
c = Counter(groupid)
values = list(c.values())  # aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key = list(c.keys())  # 先のvalue値に相当する要素のリスト(要素1,要素2,…)
for i in range(len(key)):
    l.append([key[i], values[i]])  # lは[要素i,n_i]の情報を詰めたmatrix
#print(l)
ans=[0]*N
for i in range(N):
    jointnum=c[groupid[i]]
    for j in block[i]:
        if groupid[i]==groupid[j]:
            jointnum=jointnum-1
    ans[i]=jointnum-len(friend[i])-1
print(*ans)






"""なんだこの
while len(f)>0:
    if len(d)==0:
        for i in range(len(gone)):
            groupnum[gone[i]-1]=max(len(gone),groupnum[gone[i]-1])
        gone=[]
        d.append(f[0][0])
    i=0
    v = d.popleft()
    gone.append(v)
    #print(gone)
    while i<len(f):
        if (f[i][0]==v)and(gone.count(f[i][1])==0)and(d.count(f[i][1])==0):
            d.append(f[i][1])
            f.pop(i)
            i=i-1
        elif (f[i][1]==v)and(gone.count(f[i][0])==0)and(d.count(f[i][0])==0):
            d.append(f[i][0])
            f.pop(i)
            i=i-1
        i=i+1
        #print(d,gone)
        #input()
print(groupnum)
for i in range(M):
    groupnum[f2[i][0]-1]=groupnum[f2[i][0]-1]-1
    groupnum[f2[i][1] - 1] = groupnum[f2[i][1] - 1] - 1
for i in range(K):
    groupnum[b2[i][0]-1]=groupnum[b2[i][0]-1]-1
    groupnum[b2[i][1] - 1] = groupnum[b2[i][1] - 1] - 1
for i in range(N):
    groupnum[i]=groupnum[i]-1
print(*groupnum, sep=" ")


for i in range(M):
    v1=f[i][0]
    v2=f[i][1]
    if (groupid[v1-1]==0)and(groupid[])
"""








Measured memory_bytes: 117292
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
 
int main() {
    int a, b, c, ans;
    cin >> a >> b >> c;
    if(c <= a-b) {
        ans = 0;
    }
    else {
        ans = c-(a-b);
    }
    cout << ans;
    
    cout << endl;
    return 0;
}

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

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;

  while(1) {
    if (c - b <= 0) {
      cout << "Yes" << endl;
      break;
    } else if (a - d <= 0) {
      cout << "No" << endl;
      break;
    } else {
      c -= b;
      a -= d;
    }
  }
}


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

long long dp[100005];
long long a[100005];

int main()
{
    long long n;

    cin >> n;

    for(long long i = 1 ; i<=n ; i++)
    {
        cin >> a[i];
    }

    dp[1] = 0;
    dp[2] = abs(a[2] - a[1]);

    for(long long i = 3 ; i<=n ; i++)
    {
        dp[i] = min(dp[i-1] + abs(a[i] - a[i-1]) , dp[i-2] + abs(a[i] - a[i-2]));
    }

    cout <<  dp[n];

    return 0;
}


Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;

int main(){
	
	string a;
	cin >> a;
	bool bb=false;

	for(int i=0;a[i]!='\0';i++){
		if(a[i]=='C'){
			for(int j=i;a[j]!='\0';j++){
				if(a[j]=='F'){
					bb=true;
					break;
				}
			}
			break;
		}
	}
	if(bb){
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}
	return 0;

}

Measured memory_bytes: 384
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <array>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <utility>
#include <iomanip>
#include <numeric>
#include <climits>
#include <algorithm>
#include <cstdint>
using namespace std;

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

    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if (x >= k)
            cnt++;
    }
    cout << cnt;
}

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

#define rep(i,n) for(int i = 0;i < (int)(n);i++)
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort(ALL(V))

int main()
{
    int N;
    cin >> N;
    int count = 0;
    vector<int> v(N);
    rep(i,N)
    {
        cin >> v[i];
    }
    int a,b,c;
    rep(i,N - 2)
    {
        a = v[i];
        b = v[i + 1];
        c = v[i + 2];
        if((a < b && b < c) || (c < b && b < a))
        {
            count++;
        }
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include"bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
const long long mod = 1000000007;
const long long inf = 1ll << 61;
typedef pair<int, int> P;
typedef pair<double,P> PP;
struct edge { int to; int cost; };
int dis[114514];
vector<edge>G[114514];
int col[114514];
void dfs(int now) {
	rep(i, G[now].size()) {
		if (dis[G[now][i].to] == inf) {
			dis[G[now][i].to] = dis[now] + G[now][i].cost;
			if (G[now][i].cost & 1)col[G[now][i].to] = col[now] * -1;
			else col[G[now][i].to] = col[now];
			dfs(G[now][i].to);
		}
	}
	return;
}
signed main() {
	int n; cin >> n;
	rep(i, n - 1) {
		int a, s, d; cin >> a >> s >> d;
		a--; s--;
		G[a].push_back({ s,d });
		G[s].push_back({ a,d });
	}
	fill(dis+1, dis + n, inf);
	col[0] = -1;
	dfs(0);
	rep(i, n)cout << (col[i]==-1?0:1) << endl;
}

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

const int INF = 1001001001;

/*struct mint {
    ll x; // typedef long long ll;
    mint(ll 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 { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/

vector<int> to[100005];

int main() {
    int n, m;
    cin >> n >> m;
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        to[a].emplace_back(b);
        to[b].emplace_back(a);
    }
    
    queue<int> q;
    vector<int> dist(n, INF);
    vector<int> pre(n, -1);
    dist[0] = 0;
    q.push(0);
    while (!q.empty()) {
        int v = q.front(); q.pop();
        for (int u : to[v]) {
            if (dist[u] != INF) continue;
            dist[u] = dist[v] + 1;
            pre[u] = v;
            q.push(u);
        }
    }

    cout << "Yes" << endl;
    rep(i, n) {
        if (i == 0) continue;
        int ans = pre[i];
        ans++;
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 10664
import java.util.Scanner;

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

		int K = sc.nextInt();
		int X = sc.nextInt();
		int A[] = new int[100];
		int B[] = new int[100];
		int p_cnt = X+1;
		int m_cnt = X-1;

		for(int i=0;i<K-1;i++) {
			A[i] = p_cnt++;
			B[i] = m_cnt--;
		}
		

		for(int j=K-2;j>=0;j--) System.out.print(B[j]+" ");
		System.out.print(X+" ");
		for(int k=0;k<(K-1);k++) System.out.print(A[k]+" ");



	}
}

Measured memory_bytes: 21844
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        char[] chars = s.next().toCharArray();
        ArrayList<Integer> list = new ArrayList<>();
        for (char c : chars) {
            list.add(Character.getNumericValue(c));
        }

        int zero = 0;
        int one = 0;
        for (int num : list) {
            if (num == 0) zero++;
            else one++;
        }
        System.out.println(Math.min(zero, one) * 2);
    }
}

Measured memory_bytes: 26952
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

ll a[234567];

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n,m,v,p; cin>>n>>m>>v>>p;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	int lo=0; int hi=n;
	int ans=0;
	ll oriv=v;
	while(lo<=hi)
	{
		int mid=(lo+hi)>>1;
		//can person mid make the cut?
		vector<ll> nw(n);
		for(int i=0;i<n;i++) nw[i]=a[i];
		nw[mid]+=m;
		v=oriv;
		v--;
		v-=mid;
		v-=(p-1);
		bool pos=1;
		if(v>0)
		{
			ll totvotes = m*v;
			for(int i=n-p;i>mid;i--)
			{
				ll mxspent = min(m,nw[mid]-nw[i]);
				if(mxspent<0) totvotes=ll(1e18);
				nw[i]+=mxspent;
				totvotes-=mxspent;
			}
			//cerr<<mid<<' '<<totvotes<<'\n';
			if(totvotes>0) pos=0;
		}
		for(int i=n-p;i>mid;i--)
		{
			if(nw[mid]<nw[i])
			{
				pos=0;
			}
		}
		if(pos)
		{
			ans=n-mid;
			hi=mid-1;
		}
		else
		{
			lo=mid+1;
		}
	}
	cout<<ans<<'\n';	
}


Measured memory_bytes: 1792
A, B ,C = map(int, input().split())
K = int(input())

for i in range(K):
    if A == max(A,B,C):
        A *= 2
    if B == max(A,B,C):
        B *= 2
    if C == max(A,B,C):
        C *= 2
    

print(A+B+C)

Measured memory_bytes: 3060
Imports System.Collections.Generic
Imports System.Console

Module Program
    Sub Main()
        Dim a As Integer = NextInt()
        Dim b As Integer = NextInt()

        Dim ab As Integer = Concatenate(a, b)
        Dim sqrt As Integer = CInt(Math.Sqrt(ab))

        WriteLine(New String() {"No", "Yes"}(B2I(sqrt ^ 2 = ab)))
    End Sub

    Function Concatenate(a As Integer, b As Integer) As Integer
        Return CInt(CStr(a) & CStr(b))
    End Function

    Function NextInt() As Integer
        Return CInt(NextString())
    End Function

    Function B2I(ok As Boolean) As Integer
        If ok Then Return 1
        Return 0
    End Function

    Function NextString() As String
        Dim result As New List(Of Char)
        Do
            Dim tmp As Integer = Read()
            If tmp < 0 Then Exit Do
            Dim nextChar As Char = Chr(tmp)

            If Not Char.IsWhiteSpace(nextChar) Then
                result.Add(nextChar)
            ElseIf nextChar <> vbCr Then
                Exit Do
            End If
        Loop
        Return New String(result.ToArray())
    End Function

End Module


Measured memory_bytes: 19920
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>

int main(){
	int n, a, b;
	std::vector<int> aList;

	std::cin>>n>>a>>b;

	std::cout<<std::min(a, b)<< " "<<std::max(0, b-(n-a))<<std::endl;
	
	return 0;
}

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

using namespace std;

int main() {

	long long	n;
	cin >> n ;

	string s;
	for (int i = 0; i < n; i++) {
		cin >> s;
		if (s == "Y") {
			cout << "Four" << endl;
			return 0;
		}
	}
	cout << "Three" << endl;
}

Measured memory_bytes: 256
use proconio::input;
use std::collections::BTreeSet;
use std::ops::Bound::Excluded;
use std::ops::Bound::Unbounded;
use rand::Rng;

const C:usize = 26;
const N_ITER:usize = 100000;

fn main() {

    input!{
        d:usize,
        c:[i32;C],
        s:[[i32;C];d],
    };
    let mut t = vec![0;d];
    let mut occs = vec![BTreeSet::<i32>::new();C];
    for occ in &mut occs {
        occ.insert(-1);
    }
    let mut ans = 0;
    for (day,&ti) in t.iter().enumerate() {
        ans += s[day][ti];
        occs[ti].insert(day as i32);
        for j in 0..26 {
            let sc = (c[j] as i32) * ((day as i32) -*occs[j].iter().last().unwrap());
            ans -= sc;
        }
    }
    for occ in &mut occs {
        occ.insert(d as i32);
    }    

    let sum_lr = |l,r,n| n*(l+r)/2;
    let mut rng = rand::thread_rng();
    
    for _ in 0..N_ITER {
        let d = rng.gen::<usize>()%d;
        let q = rng.gen::<usize>()%C;
        let pre_q = t[d];
        let pre_ans = ans;
        let mut try_change = |d:usize,q:usize|{
            let pre_q = t[d];
            ans-=s[d][pre_q];
            ans+=s[d][q];
            t[d]=q;
            let d = d as i32;
            // for pre_q
            let mut calc = |q:usize,is_ins:bool| {
                if is_ins {
                    occs[q].insert(d);
                }
                else {
                    occs[q].remove(&d);
                }
                let pre_d = *occs[q].range((Unbounded,Excluded(&d))).last().unwrap();
                let nex_d = *occs[q].range((Excluded(&d),Unbounded)).next().unwrap();
                let lbig = d - pre_d;
                let rbig = nex_d - pre_d -1;
                let lsml = 0;
                let rsml = nex_d-d-1;
                let sc = sum_lr(lbig,rbig,nex_d-d)-sum_lr(lsml,rsml,nex_d-d);
                sc
            };
            
            ans -= calc(pre_q,false)*c[pre_q];
            ans += calc(q,true)*c[q];
            ans
        };
        let now_ans = try_change(d,q);
        if now_ans<pre_ans {
            try_change(d,pre_q);
        }
    }

    for tv in t {
        println!("{}",tv+1);
    }
    
    
}




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

#define MAXN 200010

int n,x,y,dep[MAXN],fa[MAXN],ans;
vector<int> edger[MAXN],edgeb[MAXN];

void dfs(int u,int father)
{
	fa[u]=father;
	for(auto v : edgeb[u])
	{
		if(v==father)continue;
		dep[v]=dep[u]+1;
		dfs(v,u);
	}
}

bool go(int u,int v)
{
	for(int cnt=0;u!=v && ++cnt<=2;dep[u]<dep[v]?v=fa[v]:u=fa[u]);
	return u==v;
}

void solve(int u,int father,int dis)
{
	ans=max(ans,dep[u]);
	if(dep[u]<=dis)return;
	for(auto v : edger[u])
	{
		if(v==father)continue;
		if(go(u,v))solve(v,u,dis+1);
		else
		{
			puts("-1");
			exit(0);
		}
	}
}

int main()
{
	scanf("%d%d%d",&n,&x,&y);
	for(int i=1;i<n;++i)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		edger[u].push_back(v);
		edger[v].push_back(u);
	}
	for(int i=1;i<n;++i)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		edgeb[u].push_back(v);
		edgeb[v].push_back(u);
	}
	dfs(y,0);
	solve(x,0,0);
	printf("%d\n",ans*2);
}

Measured memory_bytes: 32252
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <set>

using namespace std;
int main(){
    int n,m;
    cin >> n>> m;
    if(m==1){
        cout << 0 << endl;
    }
    else{
        cout << n-m << endl;
    }
}

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

using namespace std;
typedef pair<int, int> P;
vector<P> x, y;


struct UnionFind
{
	vector<int> par;
	vector<int> height;

	UnionFind(int n)
	{
		for (int i = 0; i < n; i++)
		{
			par.push_back(i);
			height.push_back(0);
		}
	}

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

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

		if (height[x] < height[y])
			par[x] = y;
		else
		{
			par[y] = x;
			if (height[x] == height[y])
				height[x]++;
		}
	}

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

struct edge
{
	int u;
	int v;
	int cost;
};

bool comp(const edge& e1, const edge& e2)
{
	return e1.cost < e2.cost;
}


int kruskal(int V, int E, edge* es)
{
	sort(es, es + E, comp);
	UnionFind uf(V);

	int res = 0;
	for (int i = 0; i < E; i++)
	{
		edge e = es[i];
		if (!uf.same(e.u, e.v))
		{
			uf.unite(e.u, e.v);
			res += e.cost;
		}
	}

	return res;
}

edge es[200010];

int main()
{
	int N;
	int tmpx, tmpy;

	cin >> N;

	for (int i = 0; i < N; i++)
	{
		cin >> tmpx >> tmpy;
		x.push_back(P(tmpx, i));
		y.push_back(P(tmpy, i));
	}

	sort(x.begin(), x.end());
	sort(y.begin(), y.end());

	for (int i = 0; i < N - 1; i++)
	{
		es[i].u = x[i].second;
		es[i].v = x[i + 1].second;
		es[i].cost = x[i + 1].first - x[i].first;
	}
	for (int i = 0; i < N - 1; i++)
	{
		es[i + N - 1].u = y[i].second;
		es[i + N - 1].v = y[i + 1].second;
		es[i + N - 1].cost = y[i + 1].first - y[i].first;
	}

	int cost;

	cost = kruskal(N, 2*N - 2, es);

	cout << cost << endl;

	return 0;
}

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

typedef long long int ll;
ll dp[100005],N,H[100005],k;
ll fun(ll i)
{
    ll j,min =100000000000;
    if(i>N)
    return 1000000000000000;
   else if(i==N)
    {
        return 0;
    }
  
    else
    {
        if(dp[i]==-1)
        {
            for(j=1;j<=k;j++)
            {
            ll temp= (abs(H[i]-H[i+j])+fun(i+j));
             if(temp<min)
             min = temp;
            }
            dp[i]=min;
        }
        
        else
        return dp[i];
    }
    return dp[i];
    
}




int main()
{ 
    ll i,ans;
    cin>>N>>k;
    for(i=1;i<=N;i++)
    {
        cin>>H[i];
    }
    dp[N]=0;
    memset(dp,-1,sizeof dp);
    ans = fun(1);
    cout<<ans;
    return 0;
}

Measured memory_bytes: 6528
#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;

int main() {
    int N;
    cin >> N;

    vector <long long> A(N);
    const long long MOD = 1000000000 + 7;
    long long sum = 0;
    long long ans = 0;

    for (int i = 0; i < N; ++i) {
        cin >> A[i];
        sum += A[i];
    }

    for (int i = 0; i < A.size(); ++i) {
        sum -= A[i];
        sum %= MOD;

        ans += (A[i] * sum)%MOD;
        ans %= MOD;
    }

    cout << (ans + MOD)% MOD << endl;

    return 0;
}

Measured memory_bytes: 4844
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()

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

const int INF = 1001001001;
const vector<int> di = {-1,  0, 1, 0};
const vector<int> dj = { 0, -1, 0, 1};

void chmin(int &a, int b) { if (a > b) a = b; }

ll GCD(ll a, ll b) {
    if (b == 0) return a;
    else return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return a * b / GCD(a, b); }


int main(){
    int N, P;
    cin >> N >> P;
    string S;
    cin >> S;

    ll ans = 0;

    if (P == 2 || P == 5) {
        rep(r, N) if ((S[r] - '0') % P == 0) ans += r + 1;
        cout << ans << endl;
        return 0;
    }

    vector<int> d(N + 1); // 累積和
    int ten = 1;

    for (int i=N-1;i>=0;--i) {
        int a = (S[i] - '0') * ten % P;
        d[i] = (d[i + 1] + a) % P;
        ten *= 10; ten %= P;
    }

    vector<int> cnt(P);
    for (int i=N;i>=0;--i) {
        ans += cnt[d[i]];
        cnt[d[i]]++;
    }
    cout << ans << endl;
    return 0;
}

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

int main(){
	int x1,x2,y1,y2;
	cin >> x1 >> y1 >> x2 >> y2;
	int dx = x2 - x1;
	int dy = y2 - y1;
	int x3 = x2 - dy;
	int y3 = y2 + dx;
	int x4 = x1 - dy;
	int y4 = y1 + dx;
	cout << x3 << " " << y3 << " " << x4 << " " << y4 << endl;
	return 0;
}



Measured memory_bytes: 256