akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

int d[110][110] = {};
int n, m, inf = 1e9 + 7, a[1010], b[1010], c[1010], ans;
bool used[1010];

void warshall_floyd(int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < n; k++)
            {
                d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int k = 0; k < m; k++)
        used[k] = false;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i != j)
                d[i][j] = inf;
        }
    }
    for (int i = 0; i < m; i++)
    {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--, b[i]--;
        d[a[i]][b[i]] = c[i];
        d[b[i]][a[i]] = c[i];
    }
    warshall_floyd(n);
    for (int i = 0; i < n; i++)
    { //start
        for (int j = 0; j < n; j++)
        { //end
            if (i == j)
                continue;
            for (int k = 0; k < m; k++)
            {
                if (d[i][a[k]] + c[k] + d[b[k]][j] == d[i][j] || d[i][b[k]] + c[k] + d[a[k]][j] == d[i][j])
                    used[k] = true;
            }
        }
    }
    for (int i = 0; i < m; i++)
    {
        if (used[i] == false)
        {
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
import java.util.*;

class Main{
	
	public static void main(String args[]){

		Scanner sc = new Scanner(System.in);
		
		long N = sc.nextLong();
		long M = sc.nextLong();

		if(N==1 || M==1){
			if(N==1 && M==1) System.out.println(1);
			else{
				System.out.println(Math.max(N,M)-2);
			}
		}
		else{
			System.out.println((N-2) * (M-2));
		}		

	}
}

Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)

using ll = long long;
using ull = unsigned long long;
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; }
const ll mod = 1e9+7;
void chmod(ll &M){
  if(M >= mod) M %= mod;
  else if(M < 0){
     M += (abs(M)/mod + 1)*mod;
    M %= mod;
}
}
ll modpow(ll x, ll n){
   if(n==0) return 1;
  ll res=modpow(x, n/2);
 if(n%2==0) return res*res%mod;
else return res*res%mod*x%mod;
}
int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };
long long GCD(long long a, long long b) {
  if (b == 0) return a;
  else return GCD(b, a % b);
}

using namespace std;
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */

int main()
{
    int N;
    cin >> N;
    vector<int> L(N);
    rep(i, N) {
        cin >> L.at(i);
    }
    sort(L.begin(), L.end());
    int ans = 0;
    if (L.size() == 1 || L.size() == 2) cout << 0 <<"\n";
    else { 
    for (int i = 0; i < N-2; i++) {
        for (int j = i+1; j < N-1; j++) {
            for (int k = j+1; k < N; k++) {
        if(((L.at(i) != L.at(j)) && (L.at(j) != L.at(k))) && (L.at(k)< L.at(i) + L.at(j))) ans++;
            }
        }
    }
    cout << ans << endl;
    }
}

Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
typedef pair<int,int> P;
const int MOD = 1000000007;
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template<class T> inline bool chmin(T& a, T b){if(a>b){a = b;return 1;}return 0;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a = b;return 1;}return 0;}
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 


////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////

int l,a[210000],dp[210000][5];
signed main(){
  cin>>l;rep(i,l)cin>>a[i];
  repd(i,1,l+1){
    int mm = dp[i-1][0];
    dp[i][0] = mm + a[i-1];
    chmin(mm, dp[i-1][1]);
    dp[i][1] = min(mm + (a[i-1] == 0 ? 2 : a[i-1]%2), dp[i][0]);
    chmin(mm, dp[i-1][2]);
    dp[i][2] = min(mm + 1 - (a[i-1]%2), dp[i][1]);
    chmin(mm, dp[i-1][3]);
    dp[i][3] = min(mm + (a[i-1] == 0 ? 2 : a[i-1]%2), dp[i][2]);
    chmin(mm, dp[i-1][4]);
    dp[i][4] = min(mm + a[i-1], dp[i][3]);
  }
  int ans = INF;
  cout << dp[l][4] << endl;
}



Measured memory_bytes: 9984
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int N;
ll H[100000];

int L[100000],R[100000];
stack<int> S;


int main(){

	scanf("%d",&N);
	for(int i = 0; i < N; i++)scanf("%lld",&H[i]);

	for(int i = 0; i < N; i++){
		while(S.empty() == false && H[S.top()] >= H[i])S.pop();
		if(S.empty()){
			L[i] = 0;
		}else{
			L[i] = S.top()+1;
		}
		S.push(i);
	}

	while(!S.empty())S.pop();

	for(int i = N-1; i >= 0; i--){
		while(S.empty() == false && H[S.top()] >= H[i])S.pop();
		if(S.empty()){
			R[i] = N-1;
		}else{
			R[i] = S.top()-1;
		}
		S.push(i);
	}

	ll ans = 0;
	for(int i = 0; i < N; i++)ans = max(ans,H[i]*(ll)(R[i]-L[i]+1));

	printf("%lld\n",ans);

	return 0;
}



Measured memory_bytes: 4948
def main():
    _ = int(input())
    a = list(map(int, input().split()))
    ans = []
    max_a = max(a)
    min_a = min(a)
    unify_plus = True
    if max_a + min_a < 0:
        unify_plus = False
    if unify_plus:
        index = -1
        for i in range(len(a)):
            if max_a == a[i]:
                index = i
                break
        for i in range(len(a)):
            if a[i] < 0:
                a[i] += a[index]
                ans.append([index+1, i+1])
        for i in range(1, len(a)):
            if a[i-1] > a[i]:
                a[i] += a[i-1]
                ans.append([i-1+1, i+1])
    else:
        index = -1
        for i in range(len(a)):
            if min_a == a[i]:
                index = i
                break
        for i in range(len(a)):
            if a[i] > 0:
                a[i] += a[index]
                ans.append([index+1, i+1])
        for i in reversed(range(1, len(a))):
            if a[i-1] > a[i]:
                a[i-1] += a[i]
                ans.append([i+1, i-1+1])
    print(len(ans))
    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])

if __name__ == "__main__":
    main()

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

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

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

  if(b/a>=c) cout<<c<<endl;
  else cout<<b/a<<endl;
  
    

  return (0);
}


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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(i = 0; i < n; i++)
#define Rep(i, n) for(i = 1; i < n; i++)
#define Ans(s) cout << s << endl;

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

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; }

int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }

typedef long long ll;

// 4,8方向に探索する際に用いる配列
//int dx[4] = { 0,  0, 1, -1 };
//int dy[4] = { 1, -1, 0,  0 };
//int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
//int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

constexpr auto MAX = 100010;


int main()
{
	string s;
	cin >> s;
	int cnt = 0;
	for (int i = 0; i < s.size(); i++)
	{
		if (s[i] == 'x') cnt++;
	}
	if (cnt >= 8) cout << "NO" << endl;
	else cout << "YES" << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
//#include <conio.h>
using namespace std;

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

#define rep(i,n)	for(ll i=0;i<(n);i++)
#define SZ(x) ((ll)x.size())
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define F first
#define S second
#define int ll
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll MOD = INT_MAX;
const double PI = acos(-1);

void openfile(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
}

const int mod = 1e9+7;
int mul(int a,int b){
	return a * b % mod;
}
int add(int a,int b){
	a += b;
	if(a >= mod)	a -= mod;
	return a;
}
int exp_pow(int n, int expo){
	int res = 1,base = n,cur_exp = 1;
	while(cur_exp <= expo){
		if(cur_exp & expo)	res = mul(res, base);
		base = mul(base, base);
		cur_exp <<= 1;
	}
	return res;
}
int C(int n,int m){
	int res = 1;
	if(n == m)	return res;
	if(n-m < m)	m = n-m;
	int res2 = 1;
	rep(i,m)	res = mul(res, n-i);
	rep(i,m)	res2 = mul(res2, i+1);
	
	res2 = exp_pow(res2, mod-2);

	return mul(res, res2);
}

signed main(){
	IOS
	int n,a,b;	cin >> n >> a >> b;

	// ans : (2^n - C(n,a) - C(n,b) - 1) % mod

	int comb = add(exp_pow(2,n), mod - 1);
	comb = add(comb, -C(n,a)+mod);
	comb = add(comb, -C(n,b)+mod);
	cout << comb << endl;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
 
typedef long long int Long;
typedef long double Double;
const int MAXN = 1e5 + 4;
 
int n;
Long b, sumNeg, sumPos, a[MAXN];
priority_queue<Long> pos, neg;
 
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%lld", a + i);
 
	sumPos = sumNeg = 0LL;
	for(int i = 0; i < n; ++i) {
		scanf("%lld", &b);
 
		if(b > a[i])      pos.push(b - a[i]), sumPos += ((b - a[i]) >> 1) << 1;
		else if(b < a[i]) neg.push((a[i] - b) * 2LL), sumNeg += (a[i] - b) * 2LL;
	}

	if(sumNeg <= sumPos) {
		while(neg.size() && pos.size()) {
			Long nval = neg.top(); neg.pop();
			Long pval = pos.top(); pos.pop();

			if(nval > pval) {
				neg.push(nval - pval);
				continue;
			}

			pval -= nval;
			if(pval > 1)
				pos.push(pval);
		}
	}
 	
	puts(neg.size() ? "No" : "Yes");
	return 0;
}


Measured memory_bytes: 512
#!/usr/bin/python3
# -*- coding: utf-8 -*-
x = int(input())
if x < 100:
    print(0)
else:
    n = x // 100
    k = x % 100
    t = k // 5
    if t > n or (t == n and k % 5 > 0):
        print(0)
    else:
        print(1)

Measured memory_bytes: 2940
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
const ll INF = 1ll << 60;
ll h[100010];
ll dp[100010];

int main()
{

    int n;
    cin >> n;

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

    for (int i = 0; i < 100010; ++i)
        dp[i] = INF;
    dp[0] = 0;

    for (int i = 1; i < n; ++i)
    {
        dp[i] = min(dp[i], dp[i - 1] + abs(h[i] - h[i - 1]));
        if (i > 1)
        {
            dp[i] = min(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
        }
    }
    cout << dp[n - 1];

    return 0;
}

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

#define int long long
using ll = long long;
using ld = long double;
using intpair = pair<int, int>;
using intmap = map<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
const int MOD = 1e9 + 7;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
// struct Fast {Fast(){cin.tie(0);ios::sync_with_stdio(0);}} fast;

#define debug(x) cerr << #x << ": " << x << '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

vvi graph(100000);
vi dp(100000, -1);

void dfs(int i){
    if(dp[i] != -1){
        return;
    }else if(graph[i].empty()){
        dp[i] = 0;
    }else{
        for(int to : graph[i]){
            if(dp[to] == -1) dfs(to);
            dp[i] = max(dp[i], dp[to] + 1);
        }
    }
}

signed main(){
    int n, m;
    cin >> n >> m;
    rep(i,m){
        int a, b;
        cin >> a >> b;
        a--; b--;
        graph[a].push_back(b);
    }
    
    rep(i,n) dfs(i);
    
    int ans = 0;
    rep(i,n){
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}

Measured memory_bytes: 11684
#include <algorithm>
#include <iostream>

using namespace std;

const int INF = 1e6;
int n, m;
int edge[110][110];
int dist[110][110];
long long ans;

int main() {
  cin >> n >> m;
  
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      edge[i][j] = INF;
      dist[i][j] = INF;
    }
  }
  
  for (int i = 0; i < n; i++) {
    dist[i][i] = 0;
  }
  
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    edge[a][b] = edge[b][a] = dist[a][b] = dist[b][a] = c;
  }
  
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (edge[i][j] != INF and edge[i][j] == dist[i][j]) {
        ans++;
      }
    }
  }
  ans = m - ans / 2;
  
  cout << ans << endl;
  return 0;
}

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

void fnInput(vector<int>& rvnDisk)
{
  int nDiskSiz;
  cin >> nDiskSiz;

  for (int i = 0; i < nDiskSiz; i++)
    cin >> rvnDisk[i];

  sort(begin(rvnDisk), end(rvnDisk) );
}

int fnDiskCount(const vector<int>& cnrvnDisk)
{
  int nDiskCnt = 0, nPreDisk = 0;

  for (int nDisk : cnrvnDisk)
  {
    if (nDisk != nPreDisk)
      nDiskCnt++;

    nPreDisk = nDisk;
  }

  return nDiskCnt;
}

int main()
{
  vector<int> vnDisk(100);
  fnInput(vnDisk);
  cout << fnDiskCount(vnDisk) << endl;

  return 0;
}


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

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

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

    int a, b, c; cin >> a >> b >> c;
    cout << ((a <= c && c <= b) ? "Yes" : "No") << endl;
    return 0;
}


Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
ans = 0
for i in range(N):
    left = min(A[i], B[i])
    B[i] -= left
    ans += left
    right = min(A[i + 1], B[i])
    A[i + 1] -= right
    B[i] -= right
    ans += right
 
print(ans)

Measured memory_bytes: 18624
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define int long long
using namespace __gnu_pbds;
using namespace std;

int n,t,cnt,dp[200010][50];
pair<int,int> c[200010];
vector<int> v;

inline bool cmp(pair<int,int> x,pair<int,int> y)
{
	return y.first*(x.second+1)<x.first*(y.second+1);
}

signed main()
{
	ios::sync_with_stdio(false);
	cin>>n>>t;
	for(int i=1;i<=n;i++){
		int a,b;
		cin>>a>>b;
		if(a>0) c[++cnt]=make_pair(a,b);
		else v.push_back(b);
	}
	sort(v.begin(),v.end());
	sort(c+1,c+cnt+1,cmp);
	int times=min(n,40ll);
	for(int i=0;i<=cnt;i++) for(int j=1;j<=times;j++) dp[i][j]=t+1;
	for(int i=1;i<=cnt;i++){
		for(int j=1;j<=times;j++){
			dp[i][j]=min(dp[i][j],dp[i-1][j]);
			dp[i][j]=min(dp[i][j],dp[i-1][j-1]+1+c[i].first*(dp[i-1][j-1]+1)+c[i].second);
		}
	}
	int ans=0;
	for(int i=0;i<=times;i++){
		int val=dp[cnt][i];
		if(val>t) continue;
		ans=max(ans,i);
		for(int j=0;j<v.size();j++){
			val+=v[j]+1;
			if(val<=t) ans=max(ans,i+j+1);
			else break;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 81536
#include<stdio.h>

int Euclid(int a,int b){

if (a%b==0) return b;

return Euclid(b,a%b);
}

int main(){
    int a,b,temp;
    scanf("%d %d",&a,&b);
    if (b>a) {
        temp=a;
        a=b;
        b=temp;
    }
    printf("%d\n",Euclid(a,b));
    return 0;
}

Measured memory_bytes: 592
# D
import numpy as np

A, B, X = map(int, input().split())

if X/A < A*B/2:
  print(np.arctan(A*B*B/X/2) * 180 / np.pi)
else:
  print(np.arctan(2*B/A-2*X/A/A/A) * 180 / np.pi)

Measured memory_bytes: 12392
import numpy as np

D=int(input())
c=np.array(list(map(int,input().split())))
s=[list(map(int,input().split())) for _ in range(D)]
t=[np.random.randint(1,27) for _ in range(D)]

MAX=0
for _ in range(1000):
    d=np.random.randint(1,D+1)
    q=np.random.randint(1,27)
    last=np.array([0]*26)
    v=0
    for i in range(1,D+1):
        if i==d:
            v+=s[d-1][q-1]
            last[q-1]=d
            v-=np.dot(c,d-last)
            continue
        v+=s[i-1][t[i-1]-1]
        last[t[i-1]-1]=i
        v-=np.dot(c,i-last)
    if v>MAX:
        MAX=v
        t[d-1]=q

for i in range(D):
    print(t[i])

Measured memory_bytes: 27756
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define int ll

int h, w;
int a[500][501];
int cnt;
vector<int> ans[4];
signed main() {
    cin >> h >> w;
    for (int i=0;i<h;i++) {
        for (int j=0;j<w;j++) {
            cin >> a[i][j];
            a[i][j] = a[i][j] % 2;
        }
    }
    for (int i=0;i<h;i++) {
        for (int j=0;j<w-1;j++) {
            if (a[i][j]) {
                a[i][j]--; a[i][j+1] ^= 1;
                ans[0].push_back(i+1);
                ans[1].push_back(j+1);
                ans[2].push_back(i+1);
                ans[3].push_back(j+2);
                cnt++;
            }
        }
    }
    for (int i=0;i<h-1;i++) {
        if (a[i][w-1]) {
            a[i][w-1]--; a[i+1][w-1] ^= 1;
            ans[0].push_back(i+1);
            ans[1].push_back(w);
            ans[2].push_back(i+2);
            ans[3].push_back(w);
            cnt++;
        }
    }

    cout << cnt << endl;
    for (int i=0;i<ans[0].size();i++) {
        for (int j=0;j<4;j++) {
            cout << ans[j][i];
            if (j != 3) cout << " ";
        }
        cout << endl;
    }
}

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

using namespace std;

typedef long long ll;

#define f first
#define s second
#define mp make_pair
#define pb push_back

const int mod = (int)1e9 + 7, N = 300500;

int n, a[N], b[N], x;
ll sum;
multiset <int> s, ns;
bool ok[2][N];

int main() {
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(false);
	srand(time(NULL));
	
	cin >> n;
	for(int i = 1; i <= n; ++i){
		cin >> a[i] >> b[i];
		sum += a[i] + b[i];
		s.insert(a[i]);
		s.insert(b[i]);
	}

	while((int)s.size() > n){
		sum -= *s.rbegin();
		s.erase(s.find(*s.rbegin()));
	}
	ns = s;

	int cnt0 = 0, cnt1 = 0;
	for(int i = 1; i <= n; ++i){
		if(s.find(a[i]) != s.end()){
			s.erase(s.find(a[i]));
			ok[0][i] = 1;
			cnt0++;
		}
		if(s.find(b[i]) != s.end()){
			s.erase(s.find(b[i]));
			ok[1][i] = 1;
			cnt1++;
		}
		if(ok[0][i] && ok[1][i]){
			cout << sum;
			return 0;
		}
	}

	if(cnt0 == n || cnt1 == n)
		cout << sum;
	else{
		ll ans = (ll)1e18;
		for(int i = 1; i <= n; ++i){
			if(ok[0][i]){
				ns.erase(ns.find(a[i]));
				ans = min(ans, sum - *ns.rbegin() + b[i]);
				ns.insert(a[i]);
			}else{
			    ns.erase(ns.find(b[i]));
				ans = min(ans, sum - *ns.rbegin() + a[i]);
				ns.insert(b[i]);
			}
		}
		cout << ans;
	}

	return 0;
}

Measured memory_bytes: 10624
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <list>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{

	int n;
	int a[100];
	double sum = 0.0;

	cin >> n;

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
		sum += a[i];
	}
		
	double average = sum / n;

	int ans = 0;
	double distance = fabs(a[0] - average);

	for (int i = 1; i < n; ++i)
	{
		double d = fabs(a[i] - average);

		if (d < distance)
		{
			ans = i;
			distance = d;
		}
	}

	cout << ans << endl;

	return 0;
}

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

namespace AOJ
{
  class Program
  {
    static void Main()
    {
      Func<int, int> f = x => x * x;
      foreach (var d in ReadNumbers())
      {
        if ((600 % d) != 0)
          throw new InvalidOperationException();
        Console.WriteLine(Enumerable.Range(1, 600 / d - 1).Select(x => x * d)
                                                          .Select(x => f(x) * d)
                                                          .Sum());
      }
    }

    static IEnumerable<int> ReadNumbers()
    {
      string line;
      while (!string.IsNullOrEmpty(line = Console.ReadLine()))
        yield return int.Parse(line);
    }
  }
}

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

using namespace std;


int main()
{
    long N, T;
    long min = LONG_MAX;

    long cost, time;

    cin >> N >> T;

    for (int i = 0; i < N; i++) {
        cin >> cost >> time;
        if (T >= time) {
            if (min > cost) {
                min = cost;
            }
        }
    }

    if (min == LONG_MAX) {
        cout << "TLE" << endl;
    } else {
        cout << min << endl;
    }
    
    return 0;   
}

Measured memory_bytes: 256
#include <cstdio>

int main (){
    int N;
    scanf("%d",&N);
    int A[N];
    int max=0;
    int min=__INT_MAX__;
    for (int i = 0; i < N; i++){
        scanf("%d",&A[i]);
        if(A[i]>max) max=A[i];
        if(A[i]<min) min=A[i];
    }
    printf("%d",max-min);
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

signed main(){
    int n;
    cin >> n;
    string s[n];
    int t[n];
    REP(i,n){
        cin >> s[i] >> t[i];
    }
    string x;
    cin >> x;
    int tt;
    REP(i,n){
        if(s[i] == x) tt = i;
    }
    int ans = 0;
    for(int i = tt + 1; i < n; i++){
        ans += t[i];
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

signed main(){
    int a,b,c;
    cin >> a >> b >> c;
    if(a + b + c >= 22) cout << "bust" << endl;
    else cout << "win" << endl;
    return 0;
}

Measured memory_bytes: 256
n = gets.to_i
a = n.times.map{gets.chomp}.sort
max = 0
h = Hash.new(0)
a.each{|s|
  h[s] += 1
  max = h[s] if max < h[s]
}
ans = []
h.keys.each{|s|
  if h[s]==max
    ans << s
  end
}
puts ans

Measured memory_bytes: 41320
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s,t;
    cin>>s>>t;
    sort(s.begin(),s.end());
    sort(t.rbegin(),t.rend());
    if(s<t)
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
    return 0;
}


Measured memory_bytes: 256
/*
 * 2677.cc: Breadth-First Search by Foxpower
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E = 17;

/* typedef */

typedef long long ll;

typedef vector<int> vi;
typedef queue<int> qi;

/* global variables */

int prts[MAX_N][MAX_E], ds[MAX_N];
vi clds[MAX_N];

/* subroutines */

int lca(int u, int v) {
  if (ds[u] > ds[v]) swap(u,v);

  for (int i = MAX_E - 1; i >= 0; i--)
    if (((ds[v] - ds[u]) >> i) & 1) v = prts[v][i];
  if (u == v) return u;

  for(int i = MAX_E - 1; i >= 0; i--)
    if (prts[u][i] != prts[v][i]) {
      u = prts[u][i];
      v = prts[v][i];
    }
  
  return prts[u][0];
}

/* main */

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

  prts[0][0] = -1;
  ds[0] = 0;
  for (int i = 1; i < n; i++) {
    int pi;
    cin >> pi;
    pi--;

    prts[i][0] = pi;
    ds[i] = ds[pi] + 1;
    clds[pi].push_back(i);
  }

  for (int i = 0; i < n; i++)
    for (int e = 0; e < MAX_E - 1; e++)
      prts[i][e + 1] = (prts[i][e] < 0) ? -1 : prts[prts[i][e]][e];

  ll sum = 0;
  int pu = -1;
  qi q;
  q.push(0);

  while (! q.empty()) {
    int u = q.front(); q.pop();
    if (pu >= 0) {
      int cu = lca(pu, u);
      int d = ds[pu] + ds[u] - ds[cu] * 2;
      sum += d;
      //printf("(%d,%d) %d %d\n", pu + 1, u + 1, cu + 1, d);
    }
    pu = u;

    vi &cldu = clds[u];
    for (vi::iterator vit = cldu.begin(); vit != cldu.end(); vit++)
      q.push(*vit);
  }

  printf("%lld\n", sum);
  return 0;
}

Measured memory_bytes: 13696
n = input()
op_cnt = len(n) - 1 
for i in range(2 ** op_cnt):
    op = ["-"] * op_cnt  
    for j in range(op_cnt):
        if ((i >> j) & 1):
            op[op_cnt - 1 - j] = "+"  
    formula = ""
    for p_n, p_o in zip(n, op + [""]):  
        formula += (p_n + p_o)
    if eval(formula) == 7:
        print(formula + "=7")
        break

Measured memory_bytes: 9136
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

int main() {
	int n, m; std::cin >> n >> m;
	std::priority_queue<int> queue;
	for (auto i = 0; i < n; ++i) {
		int a; std::cin >> a;
		queue.push(a);
	}
	for (auto i = 0; i < m; ++i) {
		auto top = queue.top(); queue.pop();
		queue.push(top / 2);
	}
	long long int sum = 0;
	while (!queue.empty()) {
		sum += queue.top();
		queue.pop();
	}
	std::cout << sum << std::endl;
}


Measured memory_bytes: 892
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long li;

li n, N;

li Get(li x) {
  li sum = 0;
  while (x) {
    sum += x % 10;
    x /= 10;
  }
  return sum;
}

int main(void) {
  scanf("%lld", &n);
  int len = 0, d[100];
  N = n;
  while (n) {
    d[len++] = n % 10;
    n /= 10;
  }

  li M = d[len - 1];
  for (int i = 0; i < len - 1; ++i) {
    M *= 10;
  }
  --M;

  li ans = max(Get(N), Get(M));
  printf("%lld\n", ans);
}


Measured memory_bytes: 128
using System;
using System.Linq;
class c
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        var ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        var p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int a = 0, b = 0, c = 0;
        a = p.Where(f => f <= ab[0]).Count();
        b = p.Where(f => f >= ab[0] + 1 && f <= ab[1]).Count();
        c = p.Where(f => f >= ab[1] + 1).Count();
        Console.WriteLine(Math.Min(a, Math.Min(b, c)));
    }
}

Measured memory_bytes: 13396
#include<stdio.h>
int p[101];
void MCO(int n){
  int i,j,l,k,q,m[100][100];
  for(i=1;i<=n;i++)
    m[i][i]=0;
  for(l=2;l<=n;l++){
    for(i=1;i<=n-l+1;i++){
      j=i+l-1;
      m[i][j]=2000000000;
      for(k=i;k<=j-1;k++){
	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	if(m[i][j]>q)
	  m[i][j]=q;
      }
      if(l==n)
	printf("%d\n",m[i][j]);
    }
  }
}
int main(void){
  int n,i,r[100],c[100];
  scanf("%d",&n);
  if(n<=1){
    printf("0\n");
    return 0;
  }
  for(i=0;i<n;i++)
    scanf("%d%d",&r[i],&c[i]);
  for(i=0;i<n;i++){
    if(i==0){
      p[i]=r[i];
      p[i+1]=c[i];
    }
    else
      p[i+1]=c[i];
  }
  MCO(n);
  return 0;
}

Measured memory_bytes: 636
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで
// -fsanitize=undefinedでオーバーフロー検出
#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

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

    ll a,b,c,d,m,n,maxi=0,f=0,mini=INF;
    long double sum=0;
    long double minis=1000000000000;
    string str;
    cin>>n;
    pair<long double,long double> p[n];
    rep(i,n) {
        cin>>a>>b;
        p[i]=make_pair(a,b);
    }
    vector<ll> v(n);
    rep(i,n) v[i]=i;
    long long lastsum=0;
    do{
        rep(i,n-1){
            sum+=sqrt((p[v[i]].first-p[v[i+1]].first)*(p[v[i]].first-p[v[i+1]].first)+(p[v[i]].second-p[v[i+1]].second)*(p[v[i]].second-p[v[i+1]].second));
        }
        //cout<<sum-lastsum<<endl;
        lastsum=sum;
        f++;
    }while(next_permutation(all(v)));
    printf("%.12Lf\n",sum/f);
    return 0;
}


Measured memory_bytes: 3772
#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using ll = long long int;
int main()
{
    //freopen("inputfile.txt","r",stdin);
    string s;cin>>s;
    int ar[10];
    memset(ar,0,sizeof(ar));
    for(int i=0;i<s.size();i++)
    {
        ar[(int)s[i]-48]++;
    }
    ll ans=0;
    for(int i=1;i<10;i++)
    {
        ans += i * (ar[i]%9);
        ans%=9;
    }
    if(ans==0)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}


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

using namespace std;

const int N = 200005;
int aux[N];

int main() {
    int n, k;
    cin >> n >> k;
    map <int, int> mp;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        mp[x]++;
    }
    int sz = 0;
    for (auto it = mp.begin(); it != mp.end(); it++) {
        aux[sz++] = it->second;
    }
    sort(aux, aux + sz);
    int res = 0;
    for (int i = 0; i < sz - k; i++) {
        res += aux[i];
    }
    cout << res;
    return 0;
}

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

int main(){
  int m,d;
  int cnt;
  string days[7]={"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"};
  while(1){
    cnt = 0;
    cin >> m >> d;
    if(m == 0 && d == 0) break;
    cnt += d;
    for(m--;m>0;m--){
      switch (m){
      case 4:
      case 6:
      case 9:
      case 11:
	cnt += 30;
	break;
      case 2:
	cnt += 29;
	break;
      default:
	cnt += 31;
	break;
      }
    }
    cout << days[cnt%7] << endl;
  }
}

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

int main()
{
    int N, M;
    cin >> N;
    cin >> M;
    int n = N * (N - 1) / 2;
    int m = M * (M - 1) / 2;
    int ans = n + m;
    cout << ans << endl;
}

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

void show(int arr[], int N){
	for(int i=0; i<N; i++)
		if(i<N-1)
			cout<<arr[i]<<' '<<flush;
		else
			cout<<arr[i]<<flush;
	cout<<endl;
}


void swap(int& a, int& b){
	int t=a;
	a=b;
	b=t;
}


int ShellSort(int* arr, int N);
int InsertionSort_r(int* arr, int N, int r);

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

	int* arr= new int[N];

	for(int i=0; i<N; i++)
		cin>>arr[i];

	int count=ShellSort(arr, N);

	cout<<count<<endl;

	for( int i=0; i<N; i++)
		cout<<arr[i]<<endl;

	delete[] arr;
}


int ShellSort(int* arr, int N)
{
	int counter=0;




	int* range= new int[N];  //range= { 1, 4, 13, 40 ...}


	range[0] = 1;

	int i;
	for( i=1; range[i-1]*3+1 < N; i++){
		range[i] = range[ i-1 ]*3+1;
	}
	int range_len =i; //range ?????????????????§????????\??£????????????

	
	cout<<range_len<<endl;

	for( int i=1; range_len - i >=0; i++ )
		cout<<range[ range_len - i ]<<' '<<flush;
	cout<<endl;







	 

	for( int i=0; range_len -i -1 >=0; i++) 
		counter=InsertionSort_r(arr, N, range[ range_len - i -1 ]);
	
	delete[] range;
	
	return counter;

	
	



	
}

int InsertionSort_r(int arr[], int N, int r)
{

	static int counter=0;
	int compindex;
	int comp;
	int j;
//	bool changed=false; //??????????????¨???????????¨.??????????????????????????????????????????????????????????????????

	
	for( int i=0; i<r; i++) {


		for( int compindex=N- r -1 -i ; compindex> -1; compindex-=r ){
			
			comp=arr[compindex];
			j=r;
			for( ; compindex+j<N; j+=r ){

				if(comp > arr[compindex+j]){
					arr[compindex + j -r] = arr[compindex + j];
					counter++;
//					changed =true;
				}
				else
					break;
				
			}
			arr[compindex + j - r] = comp;

		
//			changed=false;


		}
	}
	return counter;
}

Measured memory_bytes: 6944
n,m = map(int,input().split())
C = list(map(int,input().split()))

INF = 10**9

dp = [INF for _ in range(n+1)] #dp[i] := i円での最小枚数
dp[0] = 0
for i in range(m):
    if C[i] < n+1:
        dp[C[i]] = 1

for i in range(n+1):
    for j in range(m):
        if i-C[j] >= 0:
            dp[i] = min(dp[i],dp[i-C[j]] + 1)

print(dp[n])



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

int main(){
    char s[200001];
    unsigned int i,j;

    scanf("%s",s);

    for(i=0;i<strlen(s);i++){
        if(s[i]=='A'){
            for(j=strlen(s);i<j;j--){
                if(s[j]!='Z'){

                }else{break; }
            }
            break;
        }
    }
    printf("%d",j-i+1);
    return 0;
}

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

int main(){
	
	int n,k;
	cin>>n>>k;
	if(n==2){
		if(k==0){
			cout<<1<<endl;
			cout<<"1 2"<<endl;
		}
		else{
			cout<<-1<<endl;
		}
		return 0;
	}
	if(k>(((n-1)*(n-2)) / 2)){
		cout<<-1<<endl;
		return 0;
	}
	vector<pair<int,int>> ans;
	ans.push_back({1,2});
	//ans.push_back({2,3});
	int curr=0,done = 2;
	int at2 = 1;
	int left = k;
	int leftnums = (n-2);
	for(int i=3;i<=n;i++){
		curr = at2;
		at2++;
		leftnums--;
		ans.push_back({2,i});
		int j=1;
		while(curr>(left-leftnums) && j<i){
			if(j==2){
				j++;
				continue;
			}
			ans.push_back({j,i});
			curr--;
			j++;
		}
		left = left - curr;
		//cout<<curr<<" "<<left<<endl;
	}
	cout<<ans.size()<<endl;
	for(int i=0;i<ans.size();i++){
		cout<<ans[i].first<<" "<<ans[i].second<<endl;
	}
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int s=0,a,b,c;
long long sum=0;
int gcd(int a,int b,int c){
	for(int i=a; i>=1; i--){
		if(a%i==0 && b%i==0 && c%i==0){
			s=i;
			break;
		}
	}
	return s;
}
int main(){
	int k;
	cin>>k;
	for (a=1; a<=k; a++){
   		for (b=a; b<=k; b++){
      	  for (c=b; c<=k; c++){
          	if ( a==b && b==c)	
			  sum += gcd(a, b, c);
			else if ( a==b || b==c || c==a)
			  sum += 3*gcd(a, b, c);
			else 
			  sum += 6 * gcd(a, b, c);
      	  }
   		}
	}

	cout << sum;
	return 0;
} 

Measured memory_bytes: 3616
import java.util.Scanner;

public class Main {

	private static String halfSpace = " ";

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		String strNumOfMountain = scanner.nextLine();

		int numOfMountain = Integer.parseInt(strNumOfMountain);

		String tmp = scanner.nextLine();

		scanner.close();

		String[] strTmp = tmp.split(halfSpace);

		int[] heights = new int[numOfMountain];

		for(int i = 0; i < numOfMountain; i++) {
			heights[i] = Integer.parseInt(strTmp[i]);
		}

		int max = heights[0];
		int NumOfCanSeeOcean = 1;

		for(int i = 1; i < numOfMountain; i++) {
			if(max <= heights[i]) {
				max = heights[i];
				NumOfCanSeeOcean++;
			}
		}

		System.out.println(NumOfCanSeeOcean);

	}

}

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

int main(){
	int N;
	ll ans = 0;
	cin >> N;
	vector<int> A(N), B(N), C(N);
	rep(i, N) cin >> A.at(i);
	rep(i, N) cin >> B.at(i);
	rep(i, N) cin >> C.at(i);
	sort(A.begin(), A.end());
	sort(C.begin(), C.end());
	rep(i, N){
		int b = B.at(i);
		int aindex = lower_bound(A.begin(), A.end(), b) - A.begin();
		int cindex = upper_bound(C.begin(), C.end(), b) - C.begin();
		ans += (ll)aindex * (ll)(N - cindex);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1536
package main

import (
  "fmt"
)

func main() {
  var h1, h2, m1, m2, k int
  fmt.Scanf("%d %d %d %d %d", &h1, &m1, &h2, &m2, &k)
  
  awake := (h2-h1)*60 + (m2-m1)
  ans := awake - k
  if ans < 0 {
    ans = 0
  }
  fmt.Printf("%d\n", ans)
}

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

int main() {
int n,k,a,b,ans;
  ans=0;
  a=0;
  cin>>n>>k;
  vector<int> vec(n);
  for(int i=0;i<n;i++){
 cin>>a;
    for(int j=0;j<a;j++){
      cin>>b;
    vec.at(b-1)=1;
    }
  }
  for(int i=0;i<n;i++){
  if(vec.at(i)==0){ans++;}
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3624
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

struct UnionFindTree {
    n: usize,
    par: Vec<usize>,
    rank: Vec<usize>,
}

impl UnionFindTree {
    pub fn new(n: usize) -> UnionFindTree {
        let mut par: Vec<usize> = Vec::new();
        for i in 0..n {
            par.push(i);
        }
        let rank = vec![0; n];
        UnionFindTree{
            n: n,
            par: par,
            rank: rank,
        }
    }

    fn find(&mut self, x: usize) -> usize {;
        if self.par[x] == x {
            x
        } else {
            let y = self.par[x];
            self.par[x] = self.find(y);
            self.par[x]
        }
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.find(x) == self.find(y)
    }

    fn unite(&mut self, x: usize, y: usize) {
        if self.same(x, y) {
            return;
        }
        let mut x = self.find(x);
        let mut y = self.find(y);
        if self.rank[y] > self.rank[x] {
            std::mem::swap(&mut x, &mut y);
        }
        self.par[x] = y;
        if self.rank[x] == self.rank[y] {
          self.rank[x] = self.rank[y] + 1;
      }
    }
}

fn main() {
    let nm: Vec<usize> = read_vec();
    let (n, m) = (nm[0], nm[1]);
    let mut p: Vec<usize> = read_vec();
    let mut uft = UnionFindTree::new(n + 1);
    for _i in 0..m {
        let xy = read_vec::<usize>();
        let (x, y) = (xy[0] - 1, xy[1] - 1);
        uft.unite(x, y);
    }

    let mut ans:i32 = 0;
    for i in 0..p.len() {
        p[i] -= 1;
    }
    for i in 0..p.len() {
        if uft.same(p[i], i) {
            ans += 1;
        }
    }
    println!("{:?}", ans);
}


Measured memory_bytes: 8444
#define _USE_MATH_DEFINES

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <climits>
#include <ctime>

using namespace std;
using ll = long long;
using ull = unsigned long long;

vector<ll> fen;

ll fen_get(int pos) {
	ll sum = 0;
	for (; pos >= 0; pos = (pos & (pos+1)) - 1)
		sum += fen[pos];
	return sum;
}

void fen_add(int pos, int val) {
	for (; pos < fen.size(); pos = pos | (pos+1))
		fen[pos] += val;
}

void fen_init(int sz) {
	fen.assign(sz, 0);
	for (int i = 0; i < sz; ++i)
		fen_add(i, 1);
}

void solve() {
	string s;
	getline(cin, s);
	ll n = s.size();
	if (n == 1) {
		cout << 0;
		return;
	}
	unordered_map<char,vector<ll>> ind;
	ll cnt = 0;
	for (int i = 0; i < n; ++i)
		ind[s[i]].push_back(i), cnt ^= (1 << (s[i]-'a'));
	if (__builtin_popcount(cnt) > 1)
	//if (__popcnt(cnt) > 1)
		cout << -1;
	else {
		ll k = n / 2;
		ll sum = 0;
		vector<ll> left_part;
		vector<ll> right_part;
		for (auto & x : ind) {
			vector<ll> & v = x.second;
			for (int i = 0; i < v.size() / 2; ++i) {
				sum += v[i];
				left_part.push_back(v[i]);
			}
			for (int i = v.size() / 2; i < v.size(); ++i) {
				right_part.push_back(v[i]);
			}
		}
		sum -= k*(k-1)/2;
		sort(left_part.begin(), left_part.end());
		sort(right_part.begin(), right_part.end());
		reverse(right_part.begin(), right_part.end());
		vector<ll> p(n - k);
		for (int i = 0; i < right_part.size(); ++i) {
			int pos = right_part[i];
			char c = s[pos];
			int t1 = lower_bound(ind[c].begin(), ind[c].end(), pos) - ind[c].begin();
			int t2 = ind[c].size() - t1 - 1;
			if (t1 == t2)
				p[i] = k;
			else
				p[i] = lower_bound(left_part.begin(), left_part.end(), ind[c][t2]) - left_part.begin();
		}
		fen_init(n - k);
		for (int i = 0; i < n - k; ++i) {
			fen_add(p[i], -1);
			sum += fen_get(p[i]);
		}
		cout << sum;
	}
}

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

	solve();

#ifdef LOCAL
	cerr << double(clock()) / CLOCKS_PER_SEC << " sec" << endl;
#endif
	return 0;
}

Measured memory_bytes: 6132
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <bitset>
#include  <algorithm>
typedef long long ll;
using namespace std;
const int maxn=101000;
int n,k;
int num[maxn];
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&num[i]);
    int pos=lower_bound(num+1,num+n+1,0)-num;
    if(n==1){
        printf("%d\n",abs(num[1]));
    }else if(num[n]<=0){
        printf("%d\n",abs(num[n-k+1])); //n n-k+1
    }else if(num[1]>=0){
        printf("%d\n",num[k]);
    }else{
        int ans=5e8+1000;
        int f=min(k,n-pos+1);
        for(int i=0;i<=f;i++){
            int j=k-i,ff=pos-1;
            if(ff<j) continue;//pos<j+1;pos>=j+1
            ans=min(ans,abs(i==0?0:num[pos+i-1])+2*abs(j==0?0:num[pos-j]));
            ans=min(ans,2*abs(i==0?0:num[pos+i-1])+abs(j==0?0:num[pos-j]));
        }
        printf("%d\n",ans);
    }
    return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define REP(i,x,n) for(int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);
//const ll mod = 998244353;

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


int main(void)
{
    ll n, k;
    cin >> n >> k;
    map<ll,ll> mp;
    rep(i,n){
        ll a, b;
        cin >> a >> b;
        mp[a] += b;
    }
    for(auto i:mp){
        k -= i.second;
        if(k <= 0){
            cout << i.first << endl;
            return 0;
        }
    }
    return 0;
}


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

public class Moji
{
    public string s { get; set; }
    public string smd { get; set; }
}

public class hello
{
    public static void Main()
    {
        while (true)
        {
            var s0 = Console.ReadLine().Trim();
            if (s0 == ".") break;
            var s1 = Console.ReadLine().Trim();
            if (s0 == s1) { Console.WriteLine("IDENTICAL");  goto next; }
            var dq0 = new List<string>();
            var dq1 = new List<string>();
            var aa0 = new Moji { s = s0 };
            var aa1 = new Moji { s = s1 };
            var a0 = GetDQ(dq0, aa0);
            var a1 = GetDQ(dq1, aa1);
            if (!a0  | !a1) { Console.WriteLine("DIFFERENT"); goto next; }
            if (aa0.smd != aa1.smd) { Console.WriteLine("DIFFERENT"); goto next; }
            if (dq0.Count() != dq1.Count()) { Console.WriteLine("DIFFERENT"); goto next; }
            var dcount = 0;
            for (int i = 0; i < dq0.Count(); i++)
            {
                if (dq0[i] != dq1[i]) dcount++;
                if (dcount > 1) { Console.WriteLine("DIFFERENT"); goto next; }
            }
            Console.WriteLine("CLOSE");
            next:;
        }
    }
    public static bool GetDQ(List<string> dq, Moji m)
    {
        var si = 0;
        var p = new List<int>();
        for (int i = 0; i < m.s.Length; i++)
        {
            var pos = m.s.IndexOf('\"', si);
            if (pos != -1)
            {
                p.Add(pos);
                si = pos + 1;
            }
            else break;
        }
        if (p.Count == 0)
        {
            m.smd = m.s;
            return false;
        }
        for (int i = 0; i <= p.Count / 2; i += 2)
            dq.Add(m.s.Substring(p[i] + 1, p[i + 1] - p[i] - 1));
        var rmoji = m.s.Substring(0, p[0]);
        if (p.Count() == 2) goto one;
        for (int i = 1; i < p.Count()-1; i+=2)
        {
            rmoji += m.s.Substring(p[i] + 1, p[i + 1] - p[i] - 1);
        }
        one:;
        rmoji += m.s.Substring(p[p.Count()-1]);
        m.smd = rmoji;
        return true;
    }
}

Measured memory_bytes: 9424
from math import ceil,floor

numA,numB = map(int,input().split())
for i in range(10000):
  testA = floor(i * 0.08)
  testB = floor(i * 0.10)
  if testA == numA and testB == numB:
    print(i)
    break
else:
  print("-1")

Measured memory_bytes: 3060
#include<cstdio>
#include<algorithm>
#include<cmath>
 
using namespace std;

int main(void){
	int a =0; int b =0;
	scanf("%d %d",&a,&b);
	int ans =a*b;
	if(ans%2==0)	printf("Even\n");
	else	printf("Odd\n");

	return 0;

}


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

const long INFTY = 1000000001;

long partition(pair<char, long> A[], long p, long r){
	long x = A[r].second;
	long i = p - 1;
	long j;
	for (j = p; j < r; j++){
		if (A[j].second <= x){
			i++;
			swap(A[i], A[j]);
		}
	}
	swap(A[i + 1], A[r]);
	return i + 1;
}

void quicksort(pair<char, long> A[], long p, long r){
	if (p < r){
		long q = partition(A, p, r);
		quicksort(A, p, q - 1);
		quicksort(A, q + 1, r);
	}
}

void merge(pair<char, long> A[], long left, long mid, long right){
	long n1 = mid - left;
	long n2 = right - mid;
	vector< pair<char, long> > L(n1 + 1), R(n2 + 1);
	for (long i = 0; i < n1; i++) L[i] = A[left + i];
	for (long i = 0; i < n2; i++) R[i] = A[mid + i];
	L[n1].second = R[n2].second = INFTY;
	long i = 0, j = 0;
	for (long k = left; k < right; k++){
		if (L[i].second <= R[j].second){
			A[k] = L[i];
			i++;
		}
		else {
			A[k] = R[j];
			j++;
		}
	}
}

void mergeSort(pair<char, long> A[], long left, long right){
	if (left + 1 < right){
		long mid = (left + right) / 2;
		mergeSort(A, left, mid);
		mergeSort(A, mid, right);
		merge(A, left, mid, right);
	}
}

bool isStable(pair<char, long> A[], pair<char, long> B[], long n){
	for (long i = 0; i < n; i++){
		if (A[i].first != B[i].first) return 0;
	}
	return 1;
}

int main(){
	long n;
	cin >> n;
	pair<char, long> A[100000];
	for (long i = 0; i < n; i++) cin >> A[i].first >> A[i].second;
	pair<char, long> B[100000];
	for (long i = 0; i < n; i++) B[i] = A[i];
	quicksort(A, 0, n - 1);
	mergeSort(B, 0, n);
	if (isStable(A, B, n)) cout << "Stable" << endl;
	else cout << "Not stable" << endl;
	for (long i = 0; i < n; i++){
		cout << A[i].first << " " << A[i].second << endl;
	}
	return 0;
}

Measured memory_bytes: 5704