akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>

// using
using namespace std;

// typedef
typedef long long ll;

// define
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REVERSE(c) reverse(ALL(c))
#define UNIQ(c) unique(ALL(c))
#define FOR(i, a, b, c) for (int i = (int)(a); i < (int)(b); i += (int)(c))
#define REP(i, n) FOR(i, 0, n, 1)
#define RFOR(i, a, b, c) for (int i = (int)(a); i >= (int)(b); i -= (int)(c))
#define RREP(i, n) RFOR(i, n, 0, 1)

// functions
int ceil(int a, int b) {
    return (a + b - 1) / b;
}
int round(int a, int b) {
    return (a + b / 2) / b;
}
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
    return a * b / gcd(a, b);
}
template <class T> bool amax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> bool amin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

// main
int main() {
    string s;
    cin >> s;
    int n = s.size();
    string res = "";
    REP(i, n) {
        if (s[i] != 'B') {
            res += s[i];
        } else if (!res.empty()) {
            res.pop_back();
        }
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n; ll k;
    cin >> n >> k;

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

    ll c = 0;
    int now = 0;
    vector<ll> prev_c(n, -1);
    while(prev_c[now] == -1 && k) {
        prev_c[now] = c;
        now = a[now];
        ++c;
        --k;
    }

    if (k) {
        k %= (c - prev_c[now]);

        while(k) {
            now = a[now];
            --k;
        }
    }

    cout << now+1 << endl;
}

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


int main() {
    vector<vector<int>> A(3,vector<int>(3));
    rep(i,3){
        rep(n,3){
            cin >> A[i][n];
        }
    }
    int N;
    cin >> N;
    vector<int> b(N);
    vector<vector<bool>> open(3,vector<bool>(3));
    rep(i,N){
        cin >> b[i];
    }
    rep(i,3){
        rep(n,3){
            rep(m,N){
                if (A[i][n] == b[m]){
                    open[i][n] = true;
                }
            }
        }
    }
    rep(i,3){
        bool flag = true;
        rep(j,3){
            if(!open[i][j]){
                flag = false;
                break;
            }
        }
        if(flag){
            cout << "Yes" << endl;
            return 0;
        }
    }
    rep(i,3){
        bool flag = true;
        rep(j,3){
            if(!open[j][i]){
                flag = false;
                break;
            }
        }
        if(flag){
            cout << "Yes" << endl;
            return 0;
        }
    }
    bool flag = true;
    rep(i,3){
        if(!open[i][i]){
            flag = false;
            break;
        }
    }
    if(flag){
        cout << "Yes" << endl;
        return 0;
    }
    flag = true;
    rep(i,3){
        if(!open[2-i][i]){
            flag = false;
            break;
        }
    }
    if(flag){
        cout << "Yes" << endl;
        return 0;
    }

    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)


int main(){
    string s;
    cin>>s;
    cout<<s.substr(0,s.size()-8)<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <iomanip>
#include <set>
using namespace std;
typedef long long ll;

int main(){
  string sa,sb,sc;
  cin >> sa >> sb >> sc;

  int a = 0, b = 0, c = 0;
  char now = 'a';
  char winner;

  while(true){
    if(now == 'a'){
      if(a == sa.size()){
        winner = 'A';
        break;
      }
      now = sa[a];
      a++;
    }else if(now == 'b'){
      if(b == sb.size()){
        winner = 'B';
        break;
      }
      now = sb[b];
      b++;
    }else if(now == 'c'){
      if(c == sc.size()){
        winner = 'C';
        break;
      }
      now = sc[c];
      c++;
    }
  }
  cout << winner;
  cout << "\n";
  return 0;
}

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

using namespace std;

int main()
{
    int n; cin>>n; int k; cin>>k;
    int *a=new int[n];
    for(int i=0;i<n;i++) cin>>a[i];
    vector<int>dp(n,INT_MAX);
    dp[0]=0;
    for(int i=1;i<n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            if(i-j>=0)
            {
                dp[i]=min(dp[i],dp[i-j]+abs(a[i]-a[i-j]));
            }
            else break;
        }
    }
    cout<<dp[n-1];
    return 0;
}


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

int N;
#define MAX_N 100000
vector<int> A[MAX_N]; string C;
set<int> V; vector<int> B[MAX_N]; bool J[MAX_N];
int R;

bool see(int v, int par) {
  bool res = C[v] == 'W';
  for(int w : A[v]) {
    if(w == par) continue;
    bool res_w = see(w, v);
    res = res || res_w;
  }
  if(res) V.insert(v);
  return res;
}
pair<int, int> farthest(int v, int par) {
  pair<int, int> res = {-1, 0};
  for(int w : B[v]) {
    if(w == par) continue;
    res = max(res, farthest(w, v));
  }
  if(res.first < 0) res = {0, v};
  if(!J[v]) ++res.first;
  return res;
}

int main() {
  cin >> N;
  for(int i = 0; i < N - 1; ++i) {
    int x, y; cin >> x >> y; --x; --y; A[x].push_back(y); A[y].push_back(x);
  }
  cin >> C;
  int v;
  for(v = 0; v < N; ++v) {
    if(C[v] == 'W') { see(v, -1); break; }
  }
  if(v == N) { cout << 0 << endl; return 0; }
  for(int v : V) {
    for(int w : A[v]) {
      if(!V.count(w)) continue;
      B[v].push_back(w);
    }
  }
  if(V.size() == 1) { cout << 1 << endl; return 0; }
  for(int v : V) {
    J[v] = ((int)B[v].size() % 2 == 0) == (C[v] == 'B');
    if(!J[v]) ++R;
  }
  auto res1 = farthest(*(V.begin()), -1);
  auto res2 = farthest(res1.second, -1);
  cout << R + 2 * ((int)V.size() - 1) - 2 * res2.first << endl;
  return 0;
}


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

main(){
  int K,X,s,e,max_x = 1000000, min_x = -1000000;
  cin>>K>>X;
  if(X-K+1>min_x) s = X-K+1;
  else s = min_x;
  if(X+K>max_x) e=max_x;
  else e = X+K;
  for(int i=s;i<e;i++){
    cout<<i<<" ";
  }
  return 0;
}

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

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)

class EStringsOfImpurity {
public:
    void solve(istream& in, ostream& out) {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        string s,t;
        in>>s>>t;
        vector<vector<int>> v(26);
        fr(i,s.length()) v[s[i]-'a'].push_back(i);
        int l=0,w=-1;
        for(auto u:t){
            u-='a';
            if(v[u].empty()){
                out<<-1<<endl;
                return;
            }
            if(*v[u].rbegin()<=w) ++l,w=*v[u].begin();
            else w=*upper_bound(v[u].begin(),v[u].end(),w);
        }
        out<<s.length()*l+w+1<<endl;
    }
};

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


Measured memory_bytes: 1228
#include <stdio.h>
#include <math.h>

int main(void){
  double x1, x2, y1, y2;
  double dis;

  scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
  dis = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  printf("%lf\n", dis);
  return 0;
}



Measured memory_bytes: 1972
loop do
  h,w = gets.split.map(&:to_i)
  break if h == 0 && w == 0
  h.times do |i|
    puts "#{'#'*w}" if i==0
    puts "#{'#'*w}" if i==h-1
    puts "##{'.'*(w-2)}#" if i!=0 && i!= h-1
  end
    puts ""
end


Measured memory_bytes: 6956
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;

typedef long long ll;


int main(int argc, char const *argv[])
{
	double a,b; cin>>a>>b;
	cout<<round((a+b)/2)<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a,b,c,n,ar[100005];
    cin>>a>>b>>c;
    memset(ar,0,sizeof(ar));
    for(int i=0;i<c;i++){
        cin>>n;
        ar[n]++;
    }
    for(int i=1;i<=a;i++){
        if(ar[i]+b-c>=1){
            cout<<"Yes\n";
        }
        else{
            cout<<"No\n";
        }
    }
}


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

list = []
list.append(int(input()))
list.append(int(input()))
list.append(int(input()))
list.append(int(input()))
list.append(int(input()))
k = int(input())

if((max(list) - min(list)) > k):
    print(":(")
else:
    print("Yay!")


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
#define int        long long 
#define test       int t;cin>>t;while(t--)
#define tezi       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define F(i,a,b)   for(int i=a;i<b;i++)
#define pb         push_back
#define mp         make_pair
#define pii        pair<int,int>
#define vi         vector<int>
#define umap       unordered_map<int,int>
#define MAXN 200005
#define endl "\n"
#define YES cout<<"YES\n";
#define NO  cout<<"NO\n";
#define ll long long


signed main(){
    tezi
    {
        int n,x,t;
        cin>>n>>x>>t;
        if(x>=n)cout<<t<<"\n";
        else{
            if(n%x ==0){
                int a = n/x;
                a *= t;
                cout<<a<<endl;
            }
            else {
                int a = n/x;
                a++;
                cout<<a*t<<endl;
            }
        }    




    }
}

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

using namespace std;
using ll = long long;
using bs = pair<int, string>;
using lsl = pair<ll, bs>;

#define LEFT 1

map<bs, ll> cost;
vector<bs> strings;

bool isPalindrome(string& s, int ini, int fim){
    return ini > fim ? true : (s[ini] == s[fim] && isPalindrome(s, ini + 1, fim - 1));
}

ll dijkstra(){
    set<lsl> q;
    string sFrom, sLeft, sRight;
    q.insert({0, {LEFT, ""}});
    int i, j;

    while(!q.empty()){
        ll dist = q.begin()->first;
        sFrom = q.begin()->second.second;
        bool side = q.begin()->second.first;
        q.erase(q.begin());

        if(side == LEFT)
            sLeft = sFrom;
        else
            sRight = sFrom;

        for(bs p: strings){
            if (side == LEFT)
                sRight = p.second;
            else
                sLeft = p.second;
                
            bool good = true;
            for(i = 0, j = sRight.length() - 1; i < sLeft.size() && j >= 0; i++, j--)
                good &= sRight[j] == sLeft[i];

            if(good){
                string nxt;
                if (i < sLeft.size())
                    nxt = sLeft.substr(i);
                if (j >= 0)
                    nxt = sRight.substr(0, j + 1);

                ll dist2 = dist + p.first;
                bool sideNxt = i < sLeft.size() || nxt.empty();

                bs pNxt = {sideNxt, nxt};
                if (!cost.count(pNxt)){
                    cost[pNxt] = dist2;
                    q.insert({dist2, pNxt});
                }else if(dist2 < cost[pNxt]){
                    q.erase({cost[pNxt], pNxt});
                    cost[pNxt] = dist2;
                    q.insert({dist2, pNxt});
                }
            }
        }
    }

    ll ans = LONG_LONG_MAX;

    for(auto p: cost){
        string s = p.first.second;
        if (isPalindrome(s, 0, (int)s.length() - 1))
            ans = min(p.second, ans);
    }

    if(ans == LONG_LONG_MAX)
        ans = -1;
    return ans;
}

int main(){
    cin.sync_with_stdio(0);
    cin.tie(0);
    int i, j, n;
    string s;
    ll c;
    
    cin >> n;
    strings.resize(n);

    for(i = 0; i < n; i++){
        cin >> s >> c;
        strings[i] = {c, s};
    }

    cout << dijkstra() << endl;
    return 0;
}

Measured memory_bytes: 3740
#include<bits/stdc++.h>
using namespace std;
long long int n,a=1,pa,pb;
char sa[10],sb[10];
int main(){
	scanf("%s",sa);
	scanf("%s",sb);
	if(sa[0] == 'H'){
		printf("%s",sb);
	}
	else{
		if(sb[0] == 'H'){
			printf("D");
		}
		else{
			printf("H");
		}
	}
}

Measured memory_bytes: 256
import java.util.*;

public class Main {

    public static void main(String args[]) {

        // 入力
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        String s = sc.next();
        sc.close();

        // 主処理
        int left = 0;
        int right = 0;
        String replaced = s;
        while (replaced.contains("()")) {
            replaced = replaced.replace("()", "");
        }

        String[] brackets = replaced.replace(")(", "),(").split(",");
        for (String b : brackets) {
            int countLeft = b.replaceAll("[^(]", "").length();
            int countRight = b.replaceAll("[^)]", "").length();
            int diff = countLeft - countRight;

            if (0 < diff) {
                right += diff;
            } else if (diff < 0) {
                left -= diff;
            }
        }

        String result = "(".repeat(left) + s + ")".repeat(right);

        // 出力
        System.out.println(result);
    }
}


Measured memory_bytes: 39012
#include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;

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

// 多次元 vector 生成
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 <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;
}

int main() {
    // cin.tie(0);
    // ios::sync_with_stdio(false);
    int N;
    cin >> N;
    ll ans = 0;
    REP(i, N) {
        ll a;
        cin >> a;
        ans += a;
    }

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


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

#define ll long long int
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vi vector<ll>
#define pb push_back
#define pii pair<ll,ll>
#define mp make_pair

int main()
{
    fastio;
    ll n;
    cin>>n;
    ll s[n+1],b[n+1],h[n+1];
    for(ll i=1;i<=n;i++)
    {
        cin>>s[i]>>b[i]>>h[i];
    }

    ll ms[n+1],mb[n+1],mh[n+1];

    ms[1] = s[1];
    mh[1] = h[1];
    mb[1] = b[1];

    for(ll i=2;i<=n;i++)
    {
        ms[i] = s[i] + max(mh[i-1],mb[i-1]);
        mh[i] = h[i] + max(mb[i-1],ms[i-1]);
        mb[i] = b[i] + max(mh[i-1],ms[i-1]);
    }

    cout<<max(max(mh[n],mb[n]),ms[n]);    

    return 0;
}

Measured memory_bytes: 4992
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
  int n;
  cin >> n;
  vector<int> w(n);
  int sum = 0;
  rep(i, n){
    cin >> w[i];
    sum += w[i];
  }
  int ans = 1000;
  int temp = 0;
  rep(i, n){
    temp += w[i];
    ans = min(ans, abs(sum - 2 * temp));
  }
  cout << ans << endl;
  
  return 0;
}

Measured memory_bytes: 3624
N = gets.to_i
P = gets.split.map(&:to_i)

A = []
B = [*1..N].map { |b| N * b }.reverse

t = N ** 2

P.each do |pos|
  pos -= 1

  b = B[pos]
  a = t - b + 1
  A[pos] = a

  t += 1
end

puts A.join(' ')
puts B.join(' ')


Measured memory_bytes: 5064
def bSort(a):
    for i in range(len(a)):
        for j in range(len(a)-1, i, -1):
            if a[j] < a[j-1]:
                a[j], a[j-1] = a[j-1], a[j]

    return a

ns = input().split()
s = bSort(ns)
print(int(s[2]) * 10 + int(s[1]) + int(s[0]))

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

int main() {
    int N, A[110], cnt = 0;
    cin >> N;
    for (int i = 0; i < N; ++i) cin >> A[i];

    int flag = 1;
    while (flag) {
        flag = 0;
        for (int i = N - 1; i > 0; --i) {
            if (A[i] < A[i - 1]) {
                int tmp = A[i];
                A[i] = A[i - 1];
                A[i - 1] = tmp;
                ++cnt;
                flag = 1;
            }
        }
    }

    for (int i = 0; i < N; ++i) {
        if (i) cout << ' ';
        cout << A[i];
    }
    cout << endl;
    cout << cnt << endl;
    
    return 0;
}


Measured memory_bytes: 3080
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef map <str,int> mapsi;
typedef map <str,int> :: iterator mapsitr;
typedef map <int , int> mint;
typedef map <ll , ll> mll;
typedef set <int> si;
typedef set <ll> sll;
typedef si :: iterator sitr;
typedef si :: reverse_iterator rsitr;
typedef sll :: iterator sltr;
typedef sll :: reverse_iterator rsltr;
#define mset multiset
typedef mset <int> msi;
typedef mset <ll> msll;
typedef msi :: iterator msitr;
typedef msi :: reverse_iterator msritr;
typedef msll :: iterator msltr;
typedef msll :: reverse_iterator mslritr;
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 126)return char(x-32);
    else if( 65 <= int(x) && int(x) <= 90)return x;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 126)return x;
    else if( 65 <= int(x) && int(x) <= 90)return char(x+32);
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57)return int(x-'0');
    else if( 97 <= int(x) && int(x) <= 126)return int(x-96);
    else if( 65 <= int(x) && int(x) <= 90)return int(x-64);
}
//void IO(){
//	#ifndef ONLINE_JUDGE
//	    freopen("input.txt", "r", stdin);
//	    freopen("output.txt", "w", stdout);
//	#endif
//}
// end of Template

int n,a[200000];
ll sum;
int main(){
	//IO();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];sum+=abs(a[i]-a[i-1]);
    }
    sum+=abs(0-a[n]);

    for(int i=1;i<=n;i++){
        int ans = sum;
        ans-= abs(a[i]-a[i-1]);
        ans-= abs(a[i]-a[i+1]);
        ans+= abs(a[i-1]-a[i+1]);
        cout<<ans<<newl;
    }

}


Measured memory_bytes: 1664
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,a[105][105],p[2100005],f[25][2100005];
vector<int> all[105];
void add(int &x,int v)
{
	x+=v;
	if(x>=mod) x-=mod;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]);
	int N=(1<<n)-1;
	for(int i=1;i<=N;i++) p[i]=p[i>>1]+(i&1),all[p[i]].push_back(i);
	f[0][0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<all[i].size();j++)
		{
			int msk=all[i][j];
			for(int k=0;k<n;k++)
			{
				if((msk&(1<<k))&&a[i][k]) add(f[i][msk],f[i-1][msk^(1<<k)]);
			}
		}
	}
	printf("%d\n",f[n][N]);
	return 0;
}

Measured memory_bytes: 181948
#include <bits/stdc++.h>
using namespace std;
int a,b,p,q,r;
double A;
int main(){
    cin>>a>>b>>p>>q>>r;
    A=p*a;
    A+=(p-q)*(b-a);
    A=A/(r+q);
    A+=b;
    cout<<A<<endl;
}



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

using namespace std;

int main()
{
  int N;
  cin >> N;
  vector<int> a(N); 
  for (int i = 0; i < N; i++){
      cin >> a[i];
  }

    sort(a.begin(), a.end(), greater<int>());
    
    int ans = 0;
    for (int i = 0; i < N; i++){
        if (i % 2 == 0){
            ans += a[i];
        }else{
            ans -= a[i];
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
N, M = gets.split.map(&:to_i)
E = M.times.map{gets.split.map(&:to_i)}.reverse

R = Array.new(N + 1){|i| i}
C = Array.new(N + 1,1)

def unite!(i,j)
  ca = C[root(i)]
  cb = C[root(j)]
  ri = root(i)
  rj = root(j)
  
  if ri <= rj
    R[rj] = ri
    C[ri] = ca + cb
  else
    R[ri] = rj
    C[rj] = ca + cb
  end
  ca * cb
end


def same?(i,j)
  root(i) == root(j)
end

def root(i)
  R[i] == i ? R[i] : R[i] = root(R[i])
end



T = Array.new(M+1)
T[M] = N * (N - 1) / 2


E.each_with_index do |(a,b),i|
  if same?(a,b)
    T[M-(i+1)] = T[M-i]
  else
    up = unite!(a,b)
    T[M-(i+1)] = T[M-i] - up
  end
end
puts (1 .. M).map{|i| T[i]}


Measured memory_bytes: 14644
#include <bits/stdc++.h>
#define ll long long
#define rw freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);
#define pb push_back
#define F first
#define S second
#define pf push_front
#define lp(i,a,b) for (int i=a;i<=(int)b;++i)
#define lp2(i,a,b) for (int i=a;i>=b;--i)
#define f(x) return cout<<x<<"\n",0
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define p(x) cout<<#x<<" = { "<<x<<" } \n"
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define EPS 1e-6
// to convert string to number use int a = stoi(string);
// to convert number to string use string s = to_string(int);
#define FAST ios::sync_with_stdio(0),ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define clr(c,x) memset(c,x,sizeof(c));
using namespace std;
const int N=2e5+5,mod=(int)1e9+7;
int main()
{
#ifdef ONLINE_JUDGE
    FAST;
#endif
    string s;
    cin>>s;
    int p=0,r=0,w=0;
    lp(i,0,s.size()-1)
    {
        if (s[i]=='g')
        {
            if (r>=p+1) w++,p++;
            else r++;
        }
        else
        {
            if (r<p+1) w--,r++;
            else p++;
        }
    }
    cout<<w<<"\n";
    return 0;
}

Measured memory_bytes: 512
N = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            if L[i] != L[j] != L[k]:
                if L[i] + L[j] > L[k]:
                    ans += 1
print(ans)

Measured memory_bytes: 9180
s=input()
n=len(s)+1
a=[0]*n
b=[0]*n
for i in range(n-1):
    if s[i]=='<':
        a[i+1]=a[i]+1
    if s[-i-1]=='>':
        b[i+1]=b[i]+1

print(sum([max(i,j) for i,j in zip(a,b[::-1])]))

Measured memory_bytes: 90444
#include<iostream>
#include<vector>
using namespace std;
int main(){
	int n;
	long long k;
	cin >> n >> k;
	vector<long long> v(100000,0);
	for(int i=0;i<n;i++){
		int a,b;
		cin >> a >> b;
		v[a-1]+=b;
	}
	for(int i=0;i<100000;i++){
		if(v[i]>=k){
			cout << i+1 << endl;
			return 0;
		}
		v[i+1]+=v[i];
	}
	
}


Measured memory_bytes: 1024
import itertools
n,a,b,c=map(int,input().split())
l = [int(input()) for i in range(n)]
zairyo = ('A','B','C','N')
zairyo_list=[0]*(4**n)
j=0
for i in itertools.product(zairyo, repeat=len(l)):
  zairyo_list[j]=i
  j+=1
mplist=[]
for zl in zairyo_list:
  if 'A' in zl and 'B' in zl and 'C' in zl:
    mp=0
    alist = [l[i] for i, e in enumerate(zl) if e == 'A']
    blist = [l[i] for i, e in enumerate(zl) if e == 'B']
    clist = [l[i] for i, e in enumerate(zl) if e == 'C']
    mp+=(len(alist)+len(blist)+len(clist)-3)*10
    akoho=sum(alist)
    bkoho=sum(blist)
    ckoho=sum(clist)
    mp+=abs(akoho-a)+abs(bkoho-b)+abs(ckoho-c)
    mplist.append(mp)
print(min(mplist))

Measured memory_bytes: 12820
A = input()
for bit in range(1 << 4):
    ans = int(A[0])
    res = [A[0]]
    for i in range(3):
        if bit & (1 << i):
            ans += int(A[i + 1])
            res.append("+")
            res.append(A[i+1])
        else:
            ans -= int(A[i + 1])
            res.append("-")
            res.append(A[i+1])
    if ans == 7:
        for i in range(7):
            print(res[i], end='')
        print("=7")
        break


Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=998244353;
#define N 20002000
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int ans,n,A,B,fac[N],inv[N];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=1LL*ans*a%mod;
		a=1LL*a*a%mod;
		b>>=1;
	}
	return ans;
}
void init(int n){
	fac[0]=1;
	for(int i=1;i<=n;++i){
		fac[i]=1LL*fac[i-1]*i%mod;
	}
	inv[n]=qpow(fac[n],mod-2);
	for(int i=n-1;i>=0;--i){
		inv[i]=1LL*inv[i+1]*(i+1)%mod;
	}
}
inline int C(int n,int m){
	return 1LL*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
inline int calc(int x,int y){
	return (C(x+y,x)-C(x+y,x+1)+mod)%mod;
}
int main(){
	n=read(),A=read(),B=read();
	if(!A&&!B){
		return !printf("1\n");
	}
	init(n<<1);
	for(int i=0;i<=A&&i<=n-B-1;++i){
		ans=(ans+1LL*calc(B-1,i)*C(n-B-i-1+A-i,A-i))%mod;
	}
	if(n==A+B)ans=(ans+calc(B-1,A))%mod;
	printf("%d\n",ans);
	return 0;
}



Measured memory_bytes: 160004
#include <bits/stdc++.h>  
 
using namespace std;
 
//#define int long long
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define FORr(i, j, k) for(int i = j; i >= k; --i)
#define repr(i, j) FOR(i, j, 0)
#define INF (1 << 30)
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
 
const int MOD = 1e9 + 7;
const int dy[]={0, 0, 1, -1};
const int dx[]={1, -1, 0, 0};
 
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

bool flag[100002];

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

    int n, res = 0;
    cin >> n;
    FOR(i, 1, n + 1) {
        int a;
        cin >> a;
        if(a == i) flag[i] = true;
    }
    FOR(i, 1, n + 1) {
        if(flag[i] && flag[i + 1]) {
            ++res;
            ++i;
        }
        else if(flag[i]) {
            ++res;
        }
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 384
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;


int main() {
    long n, l;
    cin >> n >> l;
    vector<long> a (n);
    long lastKnot = 1000000;
    
    // Optimization 2: no need for further input if endpoint found already
    for (long i = 0; i < n; i++) {
        cin >> a[i];
        if (i > 0) if (a[i] + a[i-1] >= l) {
            lastKnot = i;
            break;
        }
    }
    
    // Can be approached from behind: can a rope be fully tied from fully untied state?
    // Let's try completely optimizing it: if one 1st tie is possible, then you can tie it fully 
    // from that position without checking anything anymore, because it will always be > l
    
    
    if (lastKnot != 1000000) {
        ostringstream ss;
        for (int i = 1; i < lastKnot; i++) ss << i << endl;
        for (int i = n-1; i >= lastKnot; i--) ss << i << endl;
        cout << "Possible" << endl << ss.str();
    } else {
        cout << "Impossible";
    }
}

Measured memory_bytes: 2820
program redistribution
  implicit none
  integer(8), parameter :: modu = 1000000007
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 7) then
    write(*,'(i0)') s / 3
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 7)
  write(*,'(i0)') mod(m(1, 1) * 3 + m(1, 2) * 2 + m(1, 3), modu)
contains
  function modmatmul(x, y) result(res)
    integer(8), intent(in) :: x(3, 3), y(3, 3)
    integer(8) :: tmp, res(3, 3)
    integer :: i, j, k
    do j = 1, 3
      do i = 1, 3
        res(i, j) = 0
      end do
      do k = 1, 3
        tmp = y(k, j)
        do i = 1, 3
          res(i, j) = mod(res(i, j) + x(i, k) * tmp, modu)
        end do
      end do
    end do
  end
  function modmatpow(x, n) result(res)
    integer(8), intent(in) :: x(3, 3)
    integer, intent(in) :: n
    integer(8) :: res(3, 3), tmp(3, 3)
    integer :: m, i
    res = 0
    do i = 1, 3
      res(i, i) = 1
    end do
    tmp = x
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = modmatmul(res, tmp)
      tmp = modmatmul(tmp, tmp)
      m = rshift(m, 1)
    end do
  end
end program redistribution

Measured memory_bytes: 2840
#include<bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (int)n; i++)
#define REPvec(itr, mp) for(auto itr = mp.begin();itr != mp.end(); itr ++)
#define all(x) x.begin(), x.end()
#define MOD 1000000007//1e9+7
using namespace std;
typedef long long ll;
typedef vector<int >    vecint;
typedef vector<string > vecstr;
int main()
{
  string s;
  cin >> s;
  int n = s.size() - 2;
  cout << s[0] << n << s[n + 1] << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

int main() {
	int N, K;
	cin >> N >> K;
	vector<int> h(N);
	for (int i = 0; i < N; i++) cin >> h[i];
	vector<int> dp(N, 1000000000);
	dp[0] = 0;
	for (int i = 0; i < N - 1; i++) {
		for (int j = 1; j <= K; j++) {
			if (i + j >= N) break;
			dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
		}
	}
	cout << dp[N - 1] << endl;
}

Measured memory_bytes: 3992
#include<bits/stdc++.h>
using namespace std;
#define test(a) cout << "*" << a << endl;
#define rep1(i,n) for(i=1;i<=n;i++) 
#define ll unsigned long long
int main(){
 ll a[100001];
 ll n;
 ll i;
 ll ans = 1; 
 int zeroFrag = 0;
 int breakFrag = 0;
  
  cin >> n;
  rep1(i,n){
    cin >> a[i];
    if(a[i]==0){
      zeroFrag = 1;
      cout << 0;
      break;
    }
  }
  if(zeroFrag==0){
    rep1(i,n){
      if((long double)1e18/ans  >= a[i]){
        ans *= a[i];
      }
      else{
        breakFrag = 1;
        cout << -1;
        break;
        
      }
    }
  }
  
  if(zeroFrag==0&&breakFrag==0){
    cout << ans;
  }
  
}


Measured memory_bytes: 4372
/*


               BASIC TEMPLATES


*/
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")

#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;

//#
//#  #
//#   #
//#    #
//#   # #
//#  #   ###
//#
#define ll long long
#define pb push_back
#define emb emplace_back
#define ff first
#define ss second
#define FAST cin.tie(0); ios_base::sync_with_stdio (false)
#define pii pair<int,int>
#define TEN6 1000000
#define TEN5 100000
#define TEN4 10000
#define TEN9 1000000000
#define ipv pair<ll,vector<ll> >
#define pll pair<ll,ll>
#define CASE(x) "Case "<<x<<": "
#define _endl "\n"


#define for_r(i,st,lim) for(ll i=st-1;i>=lim;i--)
#define fRead(x) freopen(x,"r",stdin)
#define fWrite(x) freopen (x,"w",stdout)
#define all_set(arr,val) memset(arr,val,sizeof(arr))
#define all(x) x.begin(),x.end()
#define for_(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’.

//upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’.
using namespace __gnu_pbds;
#define ordered_set tree<ll , null_type,less<ll  >, rb_tree_tag,tree_order_statistics_node_update>

#define MOD 998244353 ///////<<<<<<<<<<<<<<<<<<=====MOD value change here!!!!!
#define INF 5e18
#define what_is(x) cerr << #x << " is " << x << endl;

ll BMod(ll B,ll P,ll M)
{
    ll R=1;
    B=B%M;
    while(P)
    {
        if(P&1)
            R=(R*B)%M;
        P >>= 1;
        B=(B*B)%M;
    }
    return R;
}

ll invMOD(ll num,ll M)
{
    return BMod(num,M-2,M);
}


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

}



ll lcm(ll a,ll b)
{
    return a*b / gcd(a,b);
}
ll my_abs(ll x)
{
    if(x<0)x*=-1;
    return x;
}
int main()
{
    //
    // FAST;
    ll n;
    cin>>n;
    ll arr[n+10];
    for_(i,0,n)
    {
        cin>>arr[i];

    }
    ll dp[n+10];
    all_set(dp,0);
    dp[n-2]=my_abs(arr[n-1]-arr[n-2]);
    for_(i,n-2,0)
    {
        dp[i]=min(dp[i+1]+my_abs(arr[i]-arr[i+1]),dp[i+2]+my_abs(arr[i]-arr[i+2]));

    }
    cout<<dp[0]<<_endl;


}

















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

long int a, b, c, k, sum = 0;
void solve() {
	cin >> a >> b >> c >> k;
	if (k > a) {
		sum += a;
		if (k - a > b) {
			sum -= k - a - b;
		}
	}
	else {
		sum += k;
	}
	cout << sum;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	solve();

	return 0;
}

Measured memory_bytes: 3624
#include<iostream>
#define N 32768
long a[N],b[N],c[N]={},n,k,i=N;
int main(){
for(std::cin>>n>>k;--i;a[i]=i<n?i:n)b[i]=n/i;
while(--k){
for(;++i<N;)c[i]=c[i-1]+b[i];
//for(b[--i]=c[n/i];--i;a[i]=c[i])b[i]=(b[i+1]+a[i]*(n/i-n/(i+1)))%0x3B9ACA07;
for(*b=c[n/--i];--i;a[i]=c[i])*b=b[i]=(*b+a[i]*(n/i-n/(i+1)))%0x3B9ACA07;
}
std::cout<<b[1];
}


Measured memory_bytes: 1024
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>
#include<limits>
#include<bitset>
#include<functional>
#include<type_traits>
#include<queue>
#include<stack>
#include<array>
#include<random>
#include<utility>
#include<cstdlib>
#include<ctime>


using namespace std;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI = acos(-1.0);
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define all(v) v.begin(), v.end()

char alfa[] = { 'A','B' ,'C','D' ,'E' ,'F' ,'G' ,'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' ,'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' ,'X' ,'Y','Z' };

inline void finish() {
	cout << -1 << endl;
	exit(0);
}

unsigned GetDigit(unsigned num) {
	return log10(num) + 1;
}

template <typename T>
inline void debug(vector<T> v) {
	cout << "[";
	for (ll i = 0; i < v.size() - 1; ++i) {
		cout << v[i] << ", ";
	}
	cout << v[v.size() - 1] << "]" << endl;
}

void Main();
inline void DebugMain() {
	while (true) {
		Main();
	}
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);

	bool debug = false;
	if (debug) {
		cout << "\x1b[41m" << "Debug Mode " << endl;
	}
	(debug) ? DebugMain() : Main();
	return 0;
}


ll calcC(ld lastNum, ld calcNum, ll A, ll B, ll X) {
	if (calcNum < 1) {
		return 0;
	}

	ll ans;

	ans = A * calcNum + B * GetDigit(calcNum);

	if (calcNum == lastNum) {
		return calcNum;
	}

	if (ans > X) {
		return calcC(calcNum, calcNum - (fabs(calcNum - lastNum) / 2), A, B, X);
	}
	else {
		return calcC(calcNum, calcNum + (fabs(calcNum - lastNum) / 2), A, B, X);
	}

}

inline void Main() {
	ll X, Y, N;
	ll A, B;
	ll A1, A2, A3;
	ll M1, D1, M2, D2;
	ll ans;
	ll sum=0,count =0;
	string S;
	string S2;
	bool flag = false;

	

	cin >> S;
	
	
	for (int i = 0; i < S.size()/2; i++) {
		if (S[i] != S[S.size() - i -1]) {
			count++;
		}
	}
	
	cout << count;
}




Measured memory_bytes: 256
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CSharp_Shell
{

	public static class Program
	{
		public static void Main()
		{
			string s = Console.ReadLine();
			int max = 0;
			int ct = 0;
			for (int i = 0; i < 3; i++)
			{
				if (s.Substring(i,1).Equals("R"))
				{
					ct++;
					if(max<ct)max = ct;
				}
				else
				{
					ct = 0;
				}
			}
			
			Console.WriteLine(max);
		}
	}
}



Measured memory_bytes: 25948
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <stdio.h>
using namespace std;

ll floor_sum(ll n,ll m,ll a,ll b){
  ll ans=0;
  if(a>=m){
    ans+=(n-1)*n*(a/m)/2;
    a%=m;
  }
  if(b>=m){
    ans+=n*(b/m);
    b%=m;
  }

  ll y_max=(a*n+b)/m;
  if(y_max==0) return ans;
  ans+=floor_sum(y_max,a,m,a*n-y_max*m+b);
  return ans;
}

int main(){
  int t;
  scanf("%d",&t);
  
  while(t--){
    int n,m,a,b;
    scanf("%d%d%d%d",&n,&m,&a,&b);
    printf("%lld\n",floor_sum(n,m,a,b));
  }
  
  return 0;
}

Measured memory_bytes: 1704
from itertools import permutations


def main():
    N = int(input())
    P = int(''.join(input().split()))
    Q = int(''.join(input().split()))

    d = [int(''.join(list(map(str, x)))) for x in permutations(range(1, N + 1), N)]

    print(abs(d.index(P) - d.index(Q)))


if __name__ == '__main__':
    main()


Measured memory_bytes: 4760
H, W, M = map(int, input().split())
HW_L = set(tuple(int(x)-1 for x in input().split()) for y in range(M))

w_l = [0] * W
h_l = [0] * H

for _hi, _wi in HW_L:
    w_l[_wi] += 1
    h_l[_hi] += 1

max_h = max(h_l)
max_w = max(w_l)

h_idx = [i for i, x in enumerate(h_l) if x == max_h]
w_idx = [i for i, x in enumerate(w_l) if x == max_w]

ans = max_h + max_w - 1

for hi in h_idx:
    for wj in w_idx:
        if (hi, wj) not in HW_L:
            ans = max_h + max_w
            break
    else:
        continue
    break

print(ans)


Measured memory_bytes: 83500
program sequence_sum
  implicit none
  integer(8) :: n, num, tmp, a(0:100000) = 0
  integer :: x, m, b(0:100000) = 0, period, offset, rem, i
  read(*,*) n, x, m
  tmp = m
  if (n <= 200000_8) then
    num = x
    rem = int(n)
    do i = 2, rem
      x = mod(int(x, 8) * x, tmp)
      if (x == 0) exit
      num = num + x
    end do
    write(*,'(i0)') num
    stop
  end if
  a(1) = x
  b(x) = 1
  do i = 2, m + 1
    x = int(mod(int(x, 8) * x, tmp))
    a(i) = a(i - 1) + x
    if (b(x) > 0) then
      offset = b(x) - 1
      period = i - offset - 1
      exit
    end if
    b(x) = i
  end do
  num = (n - offset) / period
  rem = int(n - offset - num * period)
  write(*,'(i0)') (a(offset + period) - a(offset)) * num + a(offset + rem)
end program sequence_sum

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

using namespace std;

int main(void){
//string S;
char C;
cin >> C;

cout << (char)((int)C + 1) << endl;
/*vector<int> P(N);

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

vector<int> PP;

PP.push_back(P[0]);
for(int i = 1; i < P.size(); i++){
  if(P[i] <= PP.back()){
    PP.push_back(P[i]);
  }
}
cout << PP.size() << endl;
*/
return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define MOD (1000000007)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()

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 gcd(int a, int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

const ll INF = 1LL << 60;

int main(){
  int n; cin >> n;
  vi a(n); rep(i, n) cin >> a[i];
  int sum = 0; rep(i, n) sum += a[i];

  int cost1 = 0, cost2 = 0;
  // cost を最小にするようなxは実数なら微分法より x = sum÷n(実数) と分かる
  // しかし許されるのは整数だからminを与えるのは sum/n(切り捨てに注意) か sum/n + 1 のいずれか
  //両方試せばよい
  rep(i, n){
    cost1 += (a[i] - sum/n)*(a[i] - sum/n);
    cost2 += (a[i] - sum/n - 1)*(a[i] - sum/n - 1);
  }
  int ans = min(cost1,cost2);

  std::cout << ans << '\n';

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,t,a;
cin >> n >> t >> a;
vector<int>vec(n);
for(int i = 0;i<n;i++){
    cin >> vec[i];
}
for(int i =0;i<n;i++){
    vec[i] = (1000*t - vec[i]*6)-1000*a;
    if(vec[i] < 0){
        vec[i] = -1*vec[i];

    }
}

int min = vec[0];
int count = 0;
for(int i=0;i<n;i++){
    if(min > vec[i]){
        min = vec[i];
        count = i;
    }
}
cout << count+1 << endl;


}


Measured memory_bytes: 256
// In the name of GOD
#include<bits/stdc++.h>
using namespace std;

#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";

#define     IOS         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define 	setprecn(x) cout<<fixed<<setprecision(x);
#define 	ll          long long int
#define 	pb          push_back
#define 	pll         pair<ll,ll>
#define 	vll         vector<ll>
#define 	ff          first
#define 	ss          second
#define 	all(a)      (a).begin(),(a).end()
#define 	sz(x)       (ll)x.size()
#define 	mod         1000000007
#define 	inf         10000000000000007
#define 	rep(i,n)    for(ll i=0;i<n;i++)
#define 	repe(i,a,b) for(ll i=a;i<b;i++)
#define 	lbnd        lower_bound
#define 	ubnd        upper_bound
#define 	bs          binary_search

ll power(ll a,ll b,ll m=mod)
{
	if(b==0)
		return 1;
	if(b==1)
		return a;
	ll c=power(a,b/2,m);
	if(b%2==0)
		return((c%m)*(c%m))%m;
	return ((((c%m)*(c%m))%m)*(a%m))%m;
}

/********************************************************/
//	d.erase(unique(all(d)), d.end());



int main()
{
	IOS;
	ll n,k,m,i,j,c=0,cs=0,t;
	t=1;

	// cin>>t;
	string s;
	while(t--)
	{
		cin>>n>>m;
		// n=s.length();
		// ll a[n];
		c=(n>9?m:(m+100*(10-n)));
		
		cout<<c;

		
		cout<<"\n";
	}
	return 0;
}

Measured memory_bytes: 256
n, x, y = map(int, input().split())
x -= 1
y -= 1

cnt_list = [0] * (n - 1)

for i in range(n):
    for j in range(n):
        if j <= i:
            continue
        
        #i < j,x < yのとき、i→y→x→jが最短になることはない
        num = min([j - i, abs(x - i) + 1 + abs(y - j)]) - 1 #距離 - 1、距離ゼロはif文で排除
        cnt_list[num] += 1

for n in cnt_list:
    print(n)

Measured memory_bytes: 3444
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric, std.container, std.range;

void main()
{
    auto mk = readln.split.to!(int[]);
    auto M = mk[0];
    auto K = mk[1];
    if (K == 0) {
        int[] rs;
        foreach (i; 0..2^^M) {
            rs ~= i;
            rs ~= i;
        }
        writeln(rs.to!(string[]).join(" "));
        return;
    }
    if (K >= 2^^M) {
        writeln(-1);
        return;
    }
    uint x;
    foreach (i; 0..2^^M) if (i != K) x ^= i;
    if (x != K) {
        writeln(-1);
        return;
    }
    int[] rs;
    foreach (i; 0..2^^M) if (i != K) rs ~= i;
    rs ~= K;
    foreach_reverse (i; 0..2^^M) if (i != K) rs ~= i;
    rs ~= K;
    writeln(rs.to!(string[]).join(" "));
}

Measured memory_bytes: 13760
#include <cstdio>
#include <iostream>
#define pf printf
using namespace std;

int st[30], a, b;

int main()
{
    for(int i=0, i1; i < 28; ++i) {
        cin >> i1;
        ++st[i1-1];
    }
    for(int i=0; i < 30; ++i) if (!st[i]) (!a) ? a = i+1 : b = i+1;
    (a < b) ? pf("%d\n%d\n",a,b) : pf("%d\n%d\n",b,a);
    return 0;
}

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

#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define For(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define sz(x) ((int)(x).size())
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;
typedef long long ll;

const int N = 1e5+10;

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

  int n,W; cin>>n>>W;
  vector<ll> dp(N,INFL);
  dp[0] = 0;
  For(i,1,n) {
    int w,v; cin>>w>>v;
    for(int j=1000*i;j>=v;j--) {
      dp[j] = min(dp[j],dp[j-v]+w);
    }
  }
  ll ans = 0;
  rep(i,N) if(dp[i]<=W) ans = i;
  cout<<ans<<endl;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>

using namespace std;

int main(){

  string s;
  cin >> s;
  
  int zero=0,one=0;
  
  for(int i=0;i<s.length();i++){
  
    if(s[i]=='0'){
      zero++;
    }
    else{
      one++;
    }
    
  }

  cout << 2*min(zero,one) << endl;
}

Measured memory_bytes: 512
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  int n;
  ll cnt = 0;
  vector<ll> a, b;
  priority_queue<pair<ll, int> > q;
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    q.push({b[i], i});
  }
  while (!q.empty()) {
    int x, y;
    x = q.top().first;
    y = q.top().second;
    q.pop();
    if (x != a[y]) {
      if (x - b[(n + y - 1) % n] + b[(n + y + 1) % n] < a[y]) {
        cout << -1 << endl;
        return 0;
      }
      int z = (b[y] - max(a[y], (b[(n + y - 1) % n] + b[(n + y + 1) % n]))) /
              (b[(n + y - 1) % n] + b[(n + y + 1) % n]);
      z = max(z, 1);
      cnt += z;
      b[y] -= z * (b[(n + y - 1) % n] + b[(n + y + 1) % n]);
      q.push({b[y], y});
    }
  }
  cout << cnt << endl;
}

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

int main(){
    int n;
    cin>>n;
    vector<int> p(n);
    int count = 0;
    int min_p = 10000000;

    for (size_t i = 0; i < n; i++)
    {
        cin>>p.at(i);
    }
    

    for (size_t i = 0; i < n; i++)
    {
        min_p = min(min_p,p.at(i));
        if(p.at(i)==min_p){
            count++;
        }


    }

    cout << count;
}

Measured memory_bytes: 1024
N = int(input())
W = list(map(int, input().split()))
t1 = W[0]
t2 = 0

for i in range(N-1):
    t2 += W[i+1]
fir = t2 - t1

for j in range(N-1):
    t = t2-t1
    if t <= 0:
        if fir > -t:
            print(-t)
            break
        else:
            print(fir)
            break

    else:
        t1 += W[j+1]
        t2 -= W[j+1]
        fir = t


Measured memory_bytes: 3060
use std::io::*;
use std::str::FromStr;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

use std::cmp::{max, min};

fn main() {
    let n: usize = read();
    let d: i64 = read();
    let mut pair: Vec<(i64, i64)> = vec![];
    for _ in 0..n {
        pair.push((read(), read()));
    }

    let mut ans = 0;
    for i in 0..pair.len() {
        let pi = pair[i];
        if d * d >= pi.1 * pi.1 + pi.0 * pi.0 {
            ans += 1;
        }
    }
    println!("{}", ans);
}


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

int main(){
  int A, B, C;
  cin >> A >> B >> C;
  vector<int> v = {A, B, C};
  sort(all(v));
  int res = 0;
  int diff = v[2] - v[1];
  v[0] + diff, v[1] + diff, res += diff;
  res  += (v[1] - v[0]) / 2;
  if ((v[1] - v[0]) % 2 == 1) res += 2;
  cout << res << endl;
  
  
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main () {
  long A, V, B, W, T;
  cin >> A >> V >> B >> W >> T;
  if(abs(A - B) <= T * V - T * W)cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}

Measured memory_bytes: 3620
from collections import defaultdict

INF = 10 ** 10

(n, m) = map(int, raw_input().split())

hs = [0] + map(int, raw_input().split())
assert len(hs) == n + 1

g = defaultdict(list)

for i in xrange(m):
    (a, b) = map(int, raw_input().split())
    g[a].append(b)
    g[b].append(a)


tot = 0
for i in xrange(1, n + 1):
    maxi = -INF
    for nxt in g[i]:
        maxi = max(maxi, hs[nxt])
    if hs[i] > maxi:
        tot += 1
print tot


Measured memory_bytes: 104932