akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define rep(i, n) for ( ll i = 0; i < (n); ++i )
#define reps(i, n) for ( ll i = 1; i <= (n); ++i )
#define _rep(i, n) for ( ll i = (n) - 1; i >= 0; --i )
#define _reps(i, n) for ( ll i = n; i > 0; --i )
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

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

int main( void )
{
	ll n, ans = 1, rem = 1000000000000000000;

	cin >> n;

	ll a[n];
	rep( i, n ) {
		cin >> a[i];
		if ( a[i] == 0 ) {
			cout << 0 << endl;
			return 0;
		}
	}


	rep( i, n ) {
		if ( a[i] > rem ) {
			cout << -1 << endl;
			return 0;
		}

		ans *= a[i];
		rem = 1000000000000000000 / ans;
	}

	cout << ans << endl;

	return 0;
}


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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N;
int A[200][3];
int P[200];
signed main(){
    cin>>N;
    rep(i,N)rep(j,3)cin>>A[i][j];

    rep(j,3){
        rep(i,N){
            int cnt=0;
            rep(k,N)if(A[i][j]==A[k][j])cnt++;
            if(cnt>1)continue;
            P[i]+=A[i][j];
        }
    }
    rep(i,N)cout<<P[i]<<endl;
    return 0;
}

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

int main(){
  long long n;
  cin >> n;
  long long ans = 0;
  
  if(n%2==1){
    cout << ans << endl;
    return 0;
  }
  vector<long long> a(30);
  a.at(0) = n/10;
  for(int i=0;i<29;i++){
    a.at(i+1) = a.at(i)/5;
  }
  /*
  for(int i=0;i<30;i++)
    cout << a.at(i) << endl;
  cout << endl;
  */
  for(int i=0;i<30;i++){
    ans += a.at(i);
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <cmath>
#include <iostream>
#include <stack>
#include <queue>
#include <climits>
#include <iomanip>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 998244353

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

void solve()
{
    string s, t;
    cin >> s >> t;

    int n = s.size();

    int count = 0;

    for(int i = 0; i < n; i++)
    {
        if(s[i] != t[i])
        {
            count++;
        }
    }

    cout << count << endl;

}

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

    int T = 1;
//    cin >> T;
    
    while (T--)
    {
        solve();
    }
    
    return 0;
}



Measured memory_bytes: 3704
#include<iostream>
#include<vector>
#include<map>
#include<stack>
#define FOR(i,a,b) for(int i=(a); i<int(b); ++i)
#define RFOR(i,a,b) for(int i=(b)-1; i>=int(a); --i)
#define rep(i,n) FOR(i,0,n)
#define rep1(i,n) FOR(i,1,int(n)+1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
#define al(d) d.begin(),d.end()
using namespace std;
template <typename T>
void putv(vector<T>& V){
	// cout << "The elements in the vector are: " << endl;
	for(auto x: V)
		cout << x << " ";
	cout << endl;
}
template <class T>
vector<T> getv(int n){
	vector<T> vec;
	rep(i,n){
		T input;
		cin >> input;
		vec.emplace_back(input);
	}
	return vec;
}
using AdjList = vector<vector<int>>;
using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

// Recieve edge(=(int,int)) as an adj list.
AdjList getadj(int N, int M){
	// Initialize all lists of the graph as a empty vector.
	vector<int> init;
	vector<vector<int>> graph(N,init);

	rep(i,M){
		int a, b;
		cin >> a >> b;
		graph[a-1].emplace_back(b-1);
		graph[b-1].emplace_back(a-1);
	}
// rep(i,N)
// 	putv(graph[i]);
	return graph;
}

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
using namespace std;
long long Manhattan_dist(long long x1, long long y1, long long x2, long long y2){return abs(x1-x2) + abs(y1-y2);}

void out(bool ans){
	if(ans == true) cout << "yes" << endl;
	else cout << "no" << endl;
}
void Out(bool ans){
	if(ans == true) cout << "Yes" << endl;
	else cout << "No" << endl;
}
void OUT(bool ans){
	if(ans == true) cout << "YES" << endl;
	else cout << "NO" << endl;
}
unsigned long long gcd(const unsigned long long a, const unsigned long long b){
	return (b==0) ? a : gcd(b,a%b);
}
int main(){
	string s;
	cin >> s;
	map<char,int> cntr;
	int len = s.length();
	rep(i,len){
		if(cntr[s[i]] > 0){
			out(false);
			return 0;
		}
		cntr[s[i]]++;
	}
	out(true);
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  long long x;
  cin >> x;
  if (x % 11 == 0){
    cout << x / 11 * 2 << endl;
  } else if (x % 11 <= 6){
    cout << x / 11 * 2 + 1 << endl;
  } else {
    cout << x / 11 * 2 + 2 << endl;
  }
}

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

int main() {
    ll x;
    cin >> x;
    if (x<600) cout << "8";
    else if (x<800) cout << '7';
    else if (x<1000) cout << '6';
    else if (x<1200) cout << "5";
    else if (x<1400) cout << "4";
    else if (x<1600) cout << "3";
    else if (x<1800) cout << "2";
    else cout << 1;
    return 0;
}

Measured memory_bytes: 3632
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
using namespace std;

typedef long long ll;

#define INF 10e10
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
// 昇順sort
#define sorti(x) sort(x.begin(), x.end())
// 降順sort
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  while(n>0) {
    if (n % 10 == 2) cnt++;
    n /= 10;
  }
  cout << cnt << endl;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
    int N,M,i,j=0;
    int A[40000];
    cin>>N>>M;
    for(i=0;i<M;i++)
    {
        cin>>A[i];
        j=j+A[i];
    }
    if(j>N)
    {
        cout<<"-1"<<endl;
    }
    else
    {
        cout<<N-j<<endl;
    }
    return 0;
}


Measured memory_bytes: 3668
#include <bits/stdc++.h>
#include <unordered_map>
#define mem(ara, a) memset(ara, a, sizeof(ara))
#define fo freopen("output.txt", "w", stdout)
#define fi freopen("bruh.txt", "r", stdin)
#define int long long
#define sp " "
#define pb push_back
#define ff first
#define sz(x) x.size();
#define len(x) x.length();
#define ss second
#define vi vector<int>
#define double long double
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define pii pair<int, int>


long long inf = 2e18;
long long minf = -inf;
int ans = 0;
using namespace std;

signed main() {
int cn=0;
int a,b,a1,a2;
cin >> a >> b;
a1=a*(a-1)/2;
a2=b*(b-1)/2;
cout << a1+a2 << endl;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
	
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        int K = sc.nextInt();
        int X = sc.nextInt();
        int Y = sc.nextInt();
        
        int ans = 0;
        ans += N-K >= 0 ? K*X : N*X;
        if (N <= K) {
        	 System.out.println(ans);
        	 return;
        } else {
        	N -= K;
        }
        System.out.println(ans + N*Y);
    }
}

Measured memory_bytes: 21972
#include <bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define MK make_pair
#define PB push_back
#define fi first
#define se second

typedef long long LL;
typedef pair<int, int> PII;
const int N=2e5 +10 , K =20;
const LL oo = 1ll<<60;
const double log4= log(4);
LL dp[N][K], f[N], g[N];
int a[N],n;
double loga[N];
template<typename T>
void upmin(T &a, T b){ if ( a>b) a=b;}

void DP(LL *f){
	for (int i=1; i<=n; ++i) loga[i] = log(a[i]);
	for (int i=1; i<=n; ++i)
		for (int j=0; j<=16; ++j) dp[i][j] = i==n ? j : oo;
	f[n]= 0;
	#ifdef che
	debug("t4= %lf\n", (loga[n]-loga[n-1])/log4);
	#endif
	for (int i=n-1; i; --i){
		for (int j=0; j<=16; ++j){
			int t= int(floor( (loga[i+1]- loga[i])/log4))+j;
			t=min(16, t);
			if ( t<0 ) upmin(dp[i][0], dp[i+1][j] + (n-i)*-t);
				else upmin( dp[i][t], dp[i+1][j] + t);
			#ifdef che
			printf("j=%d, t=%d\n", j,t);
			#endif
		}
		#ifdef che
		for (int j=0; j<=16; ++j)
			printf("dp[%d][%d] = %lld\n", i,j, dp[i][j]);
		#endif
		f[i] = oo;
		for (int j=16; ~j; --j){
			upmin(f[i], dp[i][j]);
			if ( j) upmin(dp[i][j-1], dp[i][j]-1);
		}
	}
}
int main(){
	#ifdef che
	freopen("input.txt","rt",stdin);
	freopen("output.txt","wt",stdout);
	#endif
	scanf("%d", &n);for (int i=1; i<=n; ++i) scanf("%d", a+i);
	DP(f);
	reverse(a+1, a+n+1);
	DP(g);
	LL ans=oo;
	for (int i=1; i<=n ; ++i) ans= min(ans, 2*(f[i] + g[n-i+2]) + i-1);
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 36992
// B - Cut and Count
#include <bits/stdc++.h>
using namespace std;
#define rp(i,s,e) for(int i=(s);i<(e);++i)

int main(){
	int N; string S; cin>>N>>S;
	int maxi = 0;
	rp(sep, 1, N){
		set<char> l,r;
		rp(i, 0, sep) l.insert(S[i]);
		rp(i, sep, N) r.insert(S[i]);
		int count = 0; for(auto a:l) if(r.count(a)) count++;
		maxi = max(maxi, count);
	}
	cout<< maxi <<endl;
}

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

int main(){
    int N, K;
    int x[100000];
    int dist[100000];
    cin >> N >> K;
    for (int i = 0; i < N; i++){
        cin >> x[i];
        if (i >= K - 1) {
            dist[i] = x[i] - x[i - K + 1] + min(abs(x[i]), abs(x[i - K + 1]));
        }
    }
    int ans = 300000000;
    for (int i = K - 1; i < N; i++){
        if (ans > dist[i]) ans = dist[i];
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 896
#include<stdio.h>

#define MAX 500000
#define SENTINEL 2000000000

int L[MAX/2+2],R[MAX/2+2];
//int cnt=0;  cnt=nlogn

void merge(int A[],int n,int left,int mid,int right){ //分割と統治
    int n1 = mid-left;
    int n2 = right-mid;
    for(int i=0;i<n1;i++) L[i]=A[left+i];
    for(int i=0;i<n2;i++) R[i]=A[mid+i];
    L[n1]=R[n2]=SENTINEL;  //番兵
    int i=0,j=0;
    for(int k=left;k<right;k++){
        //cnt++;
        if(L[i]<=R[j]){
            A[k]=L[i++];
        }else{
            A[k]=R[j++];
        }
    }
}

void mergeSort(int A[],int n,int left,int right){
    if(left+1<right){  //要素数が2つ以上
        int mid = (left+right)/2;
        mergeSort(A,n,left,mid);
        mergeSort(A,n,mid,right);
        merge(A,n,left,mid,right);
    }
}


int main(void){
    int n,m,i,ans;
    scanf("%d%d",&n,&m);
    if(n>=m){
        printf("0\n");
        return 0;
    }
    int x[m];
    for(i=0;i<m;i++){
        scanf("%d",&x[i]);
    }
    mergeSort(x,m,0,m);
    
    int L[m-1];
    for(i=0;i<m-1;i++){
        L[i] = x[i+1] - x[i];
    }
    mergeSort(L,m-1,0,m-1);
    
    ans = x[m-1] - x[0];
    
    i=m-2;
    while(i>=m-n){
        ans -= L[i];
        i--;
    }
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
 size_t strlen(const char *s);
#define rep(i,n) for(long long int i=0;i<n;i++)
#define sz(x) ((int)(x).size())

using namespace std;
std::vector<int> graph[10050];//graph頂点数のsize
const int N=200010;
const int P=1000000007;
using ll = long long;
vector<ll> prime;

//絶対値
int ze(int a, int b){
	if(a>b){
		return a-b;
	}else{
		return b-a;
	}
}
//小さい方
long long int min(long long int a,long long int b){
	if(a>b){
		return b;
	}else{
		return a;
	}
}
//大きい方
long long int max(long long int a,long long int b){
	if(a>b){
		return a;
	}else{
		return b;
	}
}
// a^n mod 
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod 
long long modinv(long long a, long long mod) {
    return modpow(a, mod - 2, mod);
}

//mod掛け算
long long mt(long long a, long long b, long long mod){
	return (a*b)%mod;
}

//メモ

/*
	memset(b, '\0', sizeof(b));
初期化*/


/*
	string s;
	s.size()
*/

/*
	char s[N];
	strlen(s)
*/
/*
	if(  <  )puts("Yes");
	else puts("No");
*/

/*文字列比較
if(strcmp(name[i],s)==0)
*/

/*
	graph[a].push_back(b);//graph[a][i番目に入れた]==b
	graph[j].size()	//forで回すときとか

*/


int main(){
  ll n;
  cin>>n;
  vector<ll> a(n+1);
  vector<ll> b(n+1);
  vector<ll> c(n+1);
  rep(i,n+1){
    cin>>a.at(i);
  }
  for(ll i=n;i>=0;i--){
    if(i==n){
      b.at(i)=a.at(n);
    }else{
      b.at(i)=b.at(i+1)+a.at(i);
    }
  }
  bool finish=false;
  rep(i, n+1){
    if(i==0){
      c.at(0)=1;
      if(a.at(0)!=0&&n>0){
        finish=true;
        break;
      }
    }else{
      c.at(i)=min((c.at(i-1)-a.at(i-1))*2,b.at(i));
      if(c.at(i)<=0&&i<n||c.at(i)<0){
        finish=true;
        break;
      }
    }
  }
  if(c.at(n)!=a.at(n)){
    finish=true;
  }
  ll ans=-1;
  if(!finish){
    ans=0;
    rep(i, n+1){
      ans+=c.at(i);
//      cout<<i<<","<<c.at(i)<<endl;
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 5848
import fractions
A,B,C,D=map(int,input().split())
N=B-A+1
cd=int(C*D/fractions.gcd(C,D))
bc=B//C
ac=(A-1)//C
bd=B//D
ad=(A-1)//D
bcd=B//(cd)
acd=(A-1)//(cd)
print(N-(bc+bd-ac-ad-(bcd-acd)))

Measured memory_bytes: 5076
#include <iostream>
 
using namespace std;
int main()
{
    int n,w[100],sf,sb,min=100000000000;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d",&w[i]);
    }
    
    for(int i=0;i<n;i++){
        sf=0;
        sb=0;
        for(int j=0;j<i;j++){
            sf+=w[j];
        }
        for(int j=i;j<n;j++){
            sb+=w[j];
        }
        if(abs(sf-sb)<min) min=abs(sf-sb);
        
    }
    cout<<min<<endl;
    
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repk(i,k, n) for (ll i = k; i < (ll)(n); ++i)
#define MAX 500001
#define MOD 1000000007
typedef long long  ll;

using namespace std;

// 最大公約数のll版
ll gcd(ll a, ll b) {
	if (a < b) swap(a, b);
	return b == 0 ? a : gcd(b, a%b);
}

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

int max;
int cnt[MAX];

int main() {
	ll a,b;
	cin >>a >> b;


	
	
	cout << lcm(a,b) << endl;

	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(void) {
	int D;
	scanf("%d",&D);
	int c[26];
	int s[D][26];
	int t[D];
	int v[D];
	int dissatisfaction=0, last;
	int ds[D];
	for(int i=0;i<26;i++) {
		scanf("%d",&c[i]);
	}
	for(int i=0;i<D;i++){
		for(int j=0;j<26;j++){
			scanf("%d",&s[i][j]);
		}
	}
	for(int i=0;i<D;i++){
		scanf("%d",&t[i]);
	}
	for(int j=0;j<D;j++) {
		for(int i=0;i<26;i++){
			int k=j;
			while(k>=0 && t[k]!=i+1) k--;
			last = k+1;
			dissatisfaction += c[i]*((j+1)-last);
		}
		ds[j] = dissatisfaction;
		dissatisfaction = 0;
	}
	v[0] = s[0][t[0]-1]-ds[0];
	for(int i=1;i<D;i++){
		v[i]=v[i-1]+s[i][t[i]-1]-ds[i];
	}
	for(int i=0;i<D;i++) {
		printf("%d\n",v[i]);
	}
}

Measured memory_bytes: 1772
if __name__ == '__main__':
    H, W = list(map(int, input().split()))
    N = int(input())
    a = list(map(int, input().split()))

    flatten = []
    for i in range(0, N):
        flatten.append([(i + 1) for j in range(a[i])])
    flatten_2 = []
    for tmp in flatten:
        flatten_2.extend(tmp)
    # print(flatten_2)
    ans = [[] for i in range(H)]

    for i in range(0, H):
        if i % 2 == 0:
            ans[i] = list(map(str, flatten_2[i * W:(i + 1) * W]))

        else:
            ans[i] = flatten_2[i * W:(i + 1) * W]
            ans[i].reverse()
            ans[i] = list(map(str, ans[i]))

    for i in range(H):
        print(" ".join(ans[i]))

Measured memory_bytes: 5364
#include<bits/stdc++.h>
using namespace std;
int main(){
long long int a,v;
  cin>>a>>v;
  long long int b,w;
  cin>>b>>w;
  long long int t;cin>>t;
  if(a==b)cout<<"YES"<<endl;
  else if(w>=v)cout<<"NO"<<endl;
  else {
    long long int z= abs(a-b)/(v-w);
      if(z<t)cout<<"YES"<<endl;
      else {
        if(z==t){
          if(abs(a-b)%(v-w)==0)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
        }
        else cout<<"NO"<<endl;
      }
  }
}

Measured memory_bytes: 3604
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <string.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
int main(){
	int n;
	cin>>n;
	long long cnt=0,ans=0;
	long long a,b;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		long long t1=1;
		long long t2=1;
		if(!i){
			cnt=a;
			ans=b;
			continue;
		}
		t1=cnt/a;
		if(cnt%a)t1++;
		t2=ans/b;
		if(ans%b)t2++;
		cnt=a*max(t1,t2);
		ans=b*max(t1,t2);
	}
	cout<<cnt+ans<<endl;
	return 0;
}

Measured memory_bytes: 256
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
A.sort()

log=[0]*10**6
ans=len(A)

for a in A:
    if log[a-1]==0:
        log[a-1]=-1
        for index in range(a-1+a,10**6,a):
            log[index]=1
    elif log[a-1]==-1:
        log[a-1]=1
        ans-=2
    else:
        ans-=1

print(ans)

Measured memory_bytes: 33404
import java.util.*;

public class Main implements Runnable {

    private static int MOD = 1_000_000_007;

    public static void main(String[] args) {
        // Run with 32MB stack
        Thread thread = new Thread(null, new Main(), "", 32 * 1024 * 1024);
        thread.start();
    }

    @Override
    public void run() {
        final Scanner scanner = new Scanner(System.in);
        solve(scanner);
    }

    static void solve(Scanner scanner) {
        N = Integer.parseInt(scanner.next());
        P = new int[N];
        for (int i = 0; i < N; i++) {
            P[i] = Integer.parseInt(scanner.next());
        }
        Q = new int[N];
        for (int i = 0; i < N; i++) {
            Q[i] = Integer.parseInt(scanner.next());
        }

        permuteAndProcess(new LinkedHashSet<>());
        assert (A > 0);
        assert (B > 0);
        System.out.println(Math.abs(A - B));
    }

    static int[] P, Q;
    static int N, A = -1, B = -1;
    static int curr = 0;
    static void permuteAndProcess(LinkedHashSet<Integer> visited) {
        if (visited.size() == N) {
            curr++;
            List<Integer> order = new ArrayList<>(visited);
            boolean f = true;
            for (int i = 0; i < N; i++) {
                if (order.get(i) + 1 != P[i]) {
                    f = false;
                }
            }
            if (f) {
                A = curr;
            }

            for (int i = 0; i < N; i++) {
                if (order.get(i) + 1 != Q[i]) {
                    return;
                }
            }
            B = curr;
        }

        for (int i = 0; i < N; i++) {
            if (!visited.contains(i)) {
                visited.add(i);
                permuteAndProcess(visited);
                visited.remove(i);
            }
        }
    }
}



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

string N;
int K;

ll dp[150][2][5];

void solve()
{
	dp[0][0][0]=1;
	const int n = N.length();
	for(int i=0;i<n;++i)for(int smaller=0;smaller<2;++smaller)for(int num=0;num<=K;++num){
		for(int k=0;k<=(smaller?9:N[i]-'0');++k){
			if(k == 0) dp[i+1][smaller|(k<N[i]-'0')][num] += dp[i][smaller][num];
			else{
				if(num < K) dp[i+1][smaller|(k<N[i]-'0')][num+1] += dp[i][smaller][num];
			}
		}
	}
	ll ans = dp[n][0][K]+dp[n][1][K];
	cout << ans << '\n';
}

int main()
{
	cin >> N;
	cin >> K;
	solve();
	return 0;
}

Measured memory_bytes: 256
n, l = map(int, input().split())

total = sum(list(range(l, n+l)))
pie = []
min_abs = 9999999
for i in range(n):
    a = list(range(l, n+l))
    a.pop(i)
    if abs(total-sum(a)) < min_abs:
        min_abs = abs(total-sum(a))
        ans = sum(a)
print(ans)

Measured memory_bytes: 3060
n = gets.to_i
b = gets.chomp.split.map(&:to_i)
ans = b[0] + b[n-2]
(0..n-3).each do |i|
  if b[i] < b[i+1]
    ans += b[i]
  else
    ans += b[i+1]
  end
end

p ans


Measured memory_bytes: 1788
typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;

int main() {
    string s,t;
    std::cin >> s>>t;
    ll ans = t.size();
    for (int i = 0; i < s.size()-t.size()+1; i++) {
        ll diff = 0;
        for (int j = 0; j < t.size(); j++) {
            if(s[i+j]!=t[j])diff++;
        }
        ans = min(diff,ans);
    }
    std::cout << ans << std::endl;
}


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

int main(){
  int A, B, m;
  cin >> A >> B >> m;
  vector<int> a(A), b(B);
  rep(i,A) cin >> a[i];
  rep(i,B) cin >> b[i];
  vector<P> xy(m);
  vector<int> c(m);
  rep(i,m) cin >> xy[i].first >> xy[i].second >> c[i];

  //割引券を使わない場合
  int amin = *min_element(a.begin(), a.end());
  int bmin = *min_element(b.begin(), b.end());
  int ans = amin + bmin; 
  //割引券を使う場合
  rep(i,m){
   int cost = a[xy[i].first - 1] + b[xy[i].second - 1] - c[i];
   ans = min(ans, cost);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 2304
#pragma GCC target("avx2")
#pragma GCC optimize("O3")

# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
 
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
 
int n, m, k;
int cnt[N];
string s;
int a[N];
vector < int > g[N];
int u[N];
int ans, res;
int cur;



int main () {
   SpeedForce;
	cin >> n;
	ld res = (n + 1) / 2;
	res /= n;
	cout << setprecision(10) << res;
   return Accepted;
}

// B...a

Measured memory_bytes: 18688
#include <cstdio>
#include <vector>

const int MH = 405;

int N, H, deg[MH];
std::vector<int> G[MH];

int ok, vis[MH];
void DFS(int u) {
	vis[u] = 1;
	if (deg[u]) ok = 1;
	for (int v : G[u]) if (!vis[v]) DFS(v);
}

int main() {
	scanf("%d%d", &N, &H);
	for (int i = 1; i <= N; ++i) {
		int a, b, c, d, x, y;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		if (!c) x = a;
		else x = c + H;
		if (!d) y = b + H;
		else y = d;
		++deg[x], --deg[y];
		G[x].push_back(y), G[y].push_back(x);
	}
	for (int i = 1; i <= H; ++i)
		if (deg[i] < 0 || deg[i + H] > 0) return puts("NO"), 0;
	for (int i = 1; i <= 2 * H; ++i) if (!G[i].empty() && !vis[i]) {
		ok = 0, DFS(i);
		if (!ok) return puts("NO"), 0;
	}
	puts("YES");
	return 0;
}

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

int main(){
  string s;
  cin>>s;
  rep(i,3){
    if(s.at(i)==s.at(i+1)){
      cout<<"Bad"<<endl;
      return 0;
    }
  }
  cout<<"Good"<<endl;
  return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int main() {
	Int q1,b,c1,c2,q2;
	while(q1=in()){
		cin>>b>>c1>>c2>>q2;
		int i;
		for(i=b/c1;i>0;i--){
			if(b>=c1*i+c2*(q1-i)&&i<=q2)break;
		}
		if(i==0)cout<<"NA\n";
		else cout<<i<<" "<<(b-c1*i)/c2<<endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
signed main(){
    int n,m;cin >>n >>m;
    vector<P> res;
    rep(i,n){
        int a,b;cin >> a >> b;
        res.emplace_back(a,b);
    }
    sort(res.begin(),res.end());
    int cnt = 0;
    int ans = 0;
    rep(i,n){
        auto hoge = res[i];
        int val = hoge.first,num = hoge.second;
        if(cnt + num >= m){
            ans += (m-cnt)*val;
            break;
        }
        cnt += num;
        ans += num*val;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 2420
n = int(input())
a = list(map(int,input().split()))
s = [0] * (n+1)
for i in range(n):
    s[i+1] = a[i]+s[i]
s.sort()
ans = 0
x = s[0]
y = 0
for i in range(1,n+1):
    if x != s[i]:
        ans += (1+y)*y//2
        x = s[i]
        y = 0
    else:
        y += 1
print(ans+(1+y)*y//2)

Measured memory_bytes: 31416
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FastScanner scanner = new FastScanner();
        PrintWriter out = new PrintWriter(System.out, false);
        int n= scanner.nextInt(), m = scanner.nextInt();
        ArrayList<Integer>[] list = new ArrayList[m+1];
        for(int i = 0; i <= m; i++) list[i] = new ArrayList<>();
        for(int i = 0; i < n; i++ ){
            int t = scanner.nextInt();
            if (t > m) {
                scanner.nextInt();
                continue;
            }
            list[t].add(scanner.nextInt());
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
        long ans = 0;
        for(int i = 1; i <= m; i++) {
            for(int val: list[i]) {
                queue.offer(val);
            }
            ans += !queue.isEmpty() ? queue.poll() : 0;
        }
        out.println(ans);
        out.flush();
    }
    
    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        
        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }
        
        public FastScanner() {
            this(new InputStreamReader(System.in));
        }
        
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


Measured memory_bytes: 55016
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

const double PI=4*atan(1.0);
const int MAX_SIZE = 10000;

// àÏ
double dot(P a, P b) {
  return (a.real() * b.real() + a.imag() * b.imag());
}
// OÏ
double cross(P a, P b) {
  return (a.real() * b.imag() - a.imag() * b.real());
}
// ½p`ÌÊÏö®
double calcPolygonArea(vector<P> p){
  double sum=0;
  for(int i = 0; i < p.size(); i++)
    sum+=cross(p[i],p[(i+1)%(p.size())]);
  return sum/2;
}

P points[50];
int n,m;
double dp[41][41][41][41];

const double INF=1000000;

// idxÈ~ÅleftÂIñÅÅÉfirstIdxÌàÌðIð
double calcMaxS(int idx,int left,int prvSelIdx,int firstIdx){
  if(!EQ(dp[idx][left][prvSelIdx][firstIdx],0))
    return dp[idx][left][prvSelIdx][firstIdx];
  double maxSum=-INF;
  bool ok=false;
  if(left==0){
    maxSum=(cross(points[prvSelIdx],points[firstIdx]));
    ok=true;
  }
  else if(idx==n){
    maxSum=INF;
    ok=true;
  }
  else{
    double res=calcMaxS(idx+1,left-1,idx,firstIdx);
    if(!EQ(res,INF)){
      maxSum=max(maxSum,res+(cross(points[prvSelIdx],points[idx])));
      ok=true;
    }
    double res2=calcMaxS(idx+1,left,prvSelIdx,firstIdx);
    if(!EQ(res2,INF)){
      maxSum=max(maxSum,res2);
      ok=true;
    }
  }
  if(!ok)maxSum=INF;
  return dp[idx][left][prvSelIdx][firstIdx]=maxSum;
}

int main(){
  while(cin>>n>>m&&!(n==0&&m==0)){
    double maxS=0;
    memset(dp,0,sizeof(dp));
    vector<double>tmp;
    for(int i = 0; i < n; i++){
      double p;
      cin>>p;
      tmp.push_back(p);
    }
    sort(tmp.begin(),tmp.end());
    for(int i = 0; i < n; i++)
      points[i]=P(cos(tmp[i]*2*PI),sin(tmp[i]*2*PI));
    for(int i = 0; i < n-m+1; i++){
      double res=abs(calcMaxS(i+1,m-1,i,i));
      if(!EQ(res,INF))
	maxS=max(maxS,res/2);
    }
    printf("%.6f\n",maxS);
  }
  return 0;
}

Measured memory_bytes: 22000
//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber

#include <bits/stdc++.h>

using namespace std;

// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;

// constants...
const double PI = acos(-1);

// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

// chess moves...
// int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right
// int dy[] = {+1, -1, +0, +0};
// int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
// int dx[] = {-2, -2, -1, -1,  1,  1,  2,  2}; ///Knight's Move
// int dy[] = {-1,  1, -2,  2, -2,  2, -1,  1};

// structs...
struct hash_pair { 
    template <class T1, class T2> 
    size_t operator()(const pair<T1, T2>& p) const
    { 
        auto hash1 = hash<T1>{}(p.first); 
        auto hash2 = hash<T2>{}(p.second); 
        return hash1 ^ hash2; 
    } 
}; 

// functions...
ll gcd(ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll lcm(ll a, ll b)
{
    return (a/gcd(a, b)*b);
}
ll comb(ll a, ll b)
{
    ll x = max(a-b, b), ans=1;

    for(ll K=a, L=1; K>=x+1; K--, L++){
        ans *= K;
        ans /= L;
    }

    return ans;
}


void task()
{
    //code here...
    
    int a, b, c, d;
    
    cin >> a >> b >> c >> d;
    
    while(1){
		c -= b;
		if(c<=0){
			cout << "Yes\n";
			break;
		}
		
		a -= d;
		if(a<=0){
			cout << "No\n";
			break;
		}
	}
}

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

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

    task();

    return 0;
}




Measured memory_bytes: 3548
from collections import deque

n = int(input())
ab = [list(map(int, input().split())) for _ in range(n-1)]
c = sorted(list(map(int, input().split())))

l = [[] for _ in range(n)]
for a, b in ab:
    a, b = a-1, b-1
    l[a].append(b)
    l[b].append(a)
t = [-1]*n
q = deque([0])
w = [0]*n
w[0] = c.pop()
ans = 0
while q:
    v = q.pop()
    for i in l[v]:
        if i == t[v]:
            continue
        t[i] = v
        q.appendleft(i)
        w[i] = c.pop()
        ans += min(w[v], w[i])
print(ans)
print(*w)

Measured memory_bytes: 13244
n = gets.to_s.to_i

a = Array.new(n+1){|i| i }
c = Array.new(10){ Array.new(10){ 0 } }

1.upto(n) do |i|
  
  f = i.to_s[0].to_i
  e = i.to_s[-1].to_i
  
  c[f][e] += 1
end

ans = 0
1.upto 9 do |i|
  1.upto 9 do |j|
    ans += c[i][j] * c[j][i]
  end
end

puts ans

Measured memory_bytes: 3324
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long int ll;
typedef vector<int > vecint;
typedef vector<bool > vecbool;
typedef vector<string > vecstr;
typedef vector<vector<int > > vec2int;
typedef vector<vector<bool > > vec2bool;
int dx[4] {-1,+1, 0, 0};
int dy[4] { 0, 0,-1,+1};
bool allblack(const vec2bool& g, const int h , const int w){
  REP(i,h){
    REP(j,w){
      if(g[i][j]) return false;
    }
  }
  return true;
}
int main()
{
  int h, w;
  cin >> h >> w;
  vec2bool g(h,vecbool(w,false));
  vec2bool looked(h,vecbool(w,false));
  queue<pair<int, int>> q1, q2;
  queue<pair<int, int>> q3;
  char tmp;
  int n = 0;
  REP(i,h){
    REP(j,w){
      cin >> tmp;
      if(tmp == '#'){
	g[i][j] = false;
	looked[i][j] = true;
	q1.push(make_pair(i, j));
	n ++;
      }else{
	g[i][j] = true;
      }
    }
  }

  // cout << n << endl;
  ////////////////////////////
  int ans = 0;
  //q1: outmost black 
  //q2: white to be changed
  int i, j, x, y;
  while(true){
    while(!q1.empty()){
      i = q1.front().first;
      j = q1.front().second;
      q1.pop();
      REP(k,4){
	x = i + dx[k];
	y = j + dy[k];
	if(0 <= x &&x<= h - 1&&0 <= y && y<= w - 1){
	  if(g[x][y]&&!looked[x][y]){
	    q2.push(make_pair(x, y));
	    looked[x][y] = true;
	  }
	}
      }
    }
    if(q2.empty()){break;}
    ans ++;
    while(!q2.empty()){
      i = q2.front().first;
      j = q2.front().second;
      q2.pop();
      g[i][j] = false;
      n++;
      q1.push(make_pair(i,j));
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 8848
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n; 
	cin>>n;
    vector<int> d(n); 
	for(int i = 0; i<n; ++i)cin>>d[i];
    sort(d.begin(),d.end());
	cout<<d[n/2]-d[n/2-1];
	return 0;
}

Measured memory_bytes: 640
# -*- coding: utf-8 -*-
import math

N = int(input())
# print(N)
ai = input().split(' ')
# print(ai)
num = 0
for i in range(N):
    if i % 2 == 0:
        if int(ai[i]) % 2 == 1:
            # print('asd' + str(i))
            # print('hoge' + str(ai[i*2]))
            num += 1

print(num)

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

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<int, pi> ppi;
const int INF = 0x3f2f1f0f;
#define mp make_pair

const int MAX_N = 8e2 + 10;

int N, M, K; char Mp[MAX_N][MAX_N];
bool Vis[MAX_N][MAX_N]; int Dis[MAX_N][MAX_N];
int main() {
	cin >> N >> M >> K;
	for(int i=0; i<N; i++) scanf("%s", Mp[i]);
	
	pi st;
	for(int i=0; i<N; i++) for(int j=0; j<M; j++) 
		if(Mp[i][j] == 'S') st = mp(i, j);
	/*
	N = M = 800; K = 1;
	for(int i=0; i<N; i++) for(int j=0; j<M; j++) Mp[i][j] = '#';
	st = mp(N/2, M/2);
	//*/

	for(int i=0; i<N; i++) for(int j=0; j<M; j++) Dis[i][j] = INF;
	queue<pi> Q; Q.push(st); Vis[st.first][st.second] = true; Dis[st.first][st.second] = 0;
	while(!Q.empty()) {
		int x, y; tie(x, y) = Q.front(); Q.pop();
//		printf("%d %d\n", x, y);
		if(Dis[x][y] == K) continue;
		for(int k=0; k<4; k++) {
			int nx = x + "1012"[k] - '1', ny = y + "0121"[k] - '1';
			if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
			if(Mp[nx][ny] == '#') continue;
			if(Vis[nx][ny]) continue; Vis[nx][ny] = true;
			Dis[nx][ny] = Dis[x][y] + 1;
			Q.push(mp(nx, ny));
		}
	}
	priority_queue<ppi, vector<ppi>, greater<ppi> > PQ;
	for(int i=0; i<N; i++) for(int j=0; j<M; j++) Dis[i][j] = INF;
	for(int i=0; i<N; i++) for(int j=0; j<M; j++) if(Vis[i][j])
		PQ.push(mp(K-1, mp(i, j))), Dis[i][j] = K-1;
	while(!PQ.empty()) {
		int c, x, y; pi temp; tie(c, temp) = PQ.top(); PQ.pop(); tie(x, y) = temp;
//		printf("[%d %d %d]\n", c, x, y);
		if(x == 0 || y == 0 || x == (N-1) || y == (M-1)) {
			printf("%d\n", c / K + 1);
			break;
		}
		for(int k=0; k<4; k++) {
			int nx = x + "1012"[k] - '1', ny = y + "0121"[k] - '1';
			if(nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
			if(Dis[nx][ny] > c + 1) {
				Dis[nx][ny] = c+1;
				PQ.push(mp(c+1, mp(nx, ny)));
			}
		}
	}
	return 0;
}

Measured memory_bytes: 17772
package main

import "fmt"

func solve(N int, X int, a []int, p []int) int {
	if N == 0 {
		if X <= 0 {
			return 0
		}
		return 1
	} else if X <= 1+a[N-1] {
		return solve(N-1, X-1, a, p)
	} else {
		return p[N-1] + 1 + solve(N-1, X-2-a[N-1], a, p)
	}
}

func main() {
	// Code for D - Christmas
	var N, X int
	fmt.Scan(&N, &X)

	a := make([]int, N)
	p := make([]int, N)
	a[0] = 1
	p[0] = 1
	for i := 1; i < N; i++ {
		a[i] = 2*a[i-1] + 3
		p[i] = 2*p[i-1] + 1
	}

	fmt.Println(solve(N, X, a, p))
}


Measured memory_bytes: 1796
n=int(input())
a=list(map(int, input().split()))
b=sorted(a)
c=0
for i in range(n):
 x=a.index(b[i])
 j=0
 while x>i:
  j+=1
  y=a.index(b[x])
  c+=a[y]
  a[x],a[y]=a[y],a[x]
  x=y
 c+=min(b[i]*j,b[i]*2+b[0]*(j+2))
print(c)


Measured memory_bytes: 5620
#include <iostream>
#include <algorithm>
#define N 50001
#define INFINITY 2000000000
using namespace std;
const int M = 21;
void getTheNumberOfCoin();
int n,m,T[M][N],C[M],i,j,minv;
int main(){
  cin >> n >> m;
  for(i=1;i<=m;i++) cin >> C[i];
  getTheNumberOfCoin();
  return 0;
}
void getTheNumberOfCoin(){
  for(i=1;i<=n;i++) T[0][i]=INFINITY;
  for(i=1;i<=m;i++) T[i][0]=0;
  for(i=1;i<=m;i++){
    for(j=1;j<=n;j++){
      if(j>=C[i]){
	T[i][j]=min(T[i-1][j],T[i][j-C[i]]+1);
      }
      else{
	T[i][j]=T[i-1][j];
      }
    }
  }
  minv=INFINITY;
  for(i=1;i<=m;i++){
    minv=min(minv,T[i][n]);
  }
  cout << minv << endl;
}

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

int main(){
  string s, t, u;
  cin >> s >> t;
  ll a, b;
  cin >> a >> b;
  cin >> u;
  if(s==u){
    a--;
  }
  else{
    b--;
  }
  cout << a << " " << b << endl;
}


Measured memory_bytes: 256
N, M = [int(s) for s in input().split()]
rewards = list(filter(lambda x: x[0] <= M
                , sorted([[int(s) for s in input().split()] for _ in range(N)])))[::-1]

import heapq
a = []
heapq.heapify(a)

sums = 0
for day in reversed(range(M)):
    while(rewards):
        if M-day >= rewards[-1][0]:
            r = rewards.pop()
            heapq.heappush(a, -1 * r[1])
        else:
            break
    
    if a:
        sums += -1 * heapq.heappop(a)

print(sums)


Measured memory_bytes: 22612
n,h=map(int,input().split())
A,B=[0]*n,[0]*n
for i in range(n):
  A[i],B[i]=map(int,input().split())
A_max=max(A)
B.sort(reverse=True)
ans=0
for i in B:
  if i > A_max:
    h-=i
    ans+=1
    if h <= 0:
      h=0
      break
  else:break
if h<0:
  h=0
q = h / A_max
p = q % 1
ans += int(q)
if p>0:
  print(ans+1)
else:
  print(ans)

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

/* 平面幾何の基本要素 */
 
#define EPS (1e-10)
#define equals(a,b) (fabs((a) - (b)) < EPS )
 
class Point { // 点を表すクラス
public:
  double x,y;
  
  Point(double x=0,double y=0): x(x),y(y) {}
  
  Point operator + (Point p) { return Point(x + p.x, y + p.y); }
  Point operator - (Point p) { return Point(x - p.x, y - p.y); }
  Point operator * (double a) { return Point(a * x, a * y); }
  Point operator / (double a) { return Point(x / a, y / a); }
  
  double abs() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  
  bool operator < (const Point &p) const {
    return x != p.x ? x < p.x : y < p.y;
  }
  
  bool operator == (const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};

typedef Point Vector; // ベクトルを表す構造体

double norm(Vector a){
  return a.x * a.x + a.y * a.y;
}

struct Segment { // 線分を表す構造体
  Point p1,p2;
};

typedef Segment Line; // 直線を表す構造体

class Circle { // 円を表すクラス
public:
  Point c;
  double r;
  Circle(Point c = Point(), double r = 0.0): c(c),r(r) {}
};
typedef vector < Point > Polygon; // 多角形

double dot(Vector a, Vector b) {
  return a.x * b.x + a.y * b.y;
}
 
double cross(Vector a, Vector b) {
  return a.x * b.y - a.y * b.x;
}

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
 
int ccw(Point p0, Point p1, Point p2) {
  Vector a = p1 - p0;
  Vector b = p2 - p0;
 
  if( cross(a,b) > EPS ) return COUNTER_CLOCKWISE;
  if( cross(a,b) < -EPS ) return CLOCKWISE;
  if( dot(a,b) < -EPS ) return ONLINE_BACK;
  if( a.norm() < b.norm() ) return ONLINE_FRONT;
 
  return ON_SEGMENT;
}

bool intersect(Point p1,Point p2,Point p3,Point p4){
  return ( ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0 && 
	   ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0);
}

bool intersect(Segment s1,Segment s2){
  return intersect(s1.p1,s2.p1,s1.p2,s2.p2);
}

int contains(Polygon g,Point p){
  int n = g.size();
  bool x = false;
  for(int i=0;i<n;i++){
    Point a = g[i] - p, b = g[(i+1)%n] - p;
    if(abs(cross(a,b)) < EPS && dot(a,b) < EPS) return 1;
    if(a.y > b.y) swap(a,b);
    if(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x;
  }
  return (x ? 2 : 0);
}

/*
double distance( Point p1,Point p2){
  return sqrt( (p1.y - p2.y) * (p1.y - p2.y) + (p1.x - p2.x) * (p1.x - p2.x));
}
*/

double distance( double y1, double x1, double y2, double x2){
  return sqrt( (y1 - y2) * (y1 - y2) + (x1 - x2) * (x1 - x2));
}

Point project(Segment s,Point p){
  Vector base = s.p2 - s.p1;
  double r = dot(p - s.p1 , base) / norm(base);
  return s.p1 + base * r;
}

Point reflect(Segment s,Point p){
  return p + (project(s,p)-p)*2.0;
}

int main(){
  Point p1,p2,r;
  char ch;
  while(cin >> p1.x >> ch >> p1.y >> ch >> p2.x >> ch >> p2.y >> ch >> r.x >> ch >> r.y){
    Segment s;
    s.p1 = p1;
    s.p2 = p2;
    Point ans = reflect(s,r);
    cout << ans.x << " " << ans.y << endl;
  }
}

Measured memory_bytes: 1288
import java.util.Scanner;

public class Main {

    private static final long MOD = 1_000_000_007;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long k = sc.nextLong();
        long ans = 0;
        for (long i = k; i <= n + 1; i++) {
            long min = (i - 1) * i / 2;
            long max = n * (n + 1) / 2 - (n - i) * (n - i + 1) / 2;
            ans = (ans + max - min + 1) % MOD;
        }
        System.out.println(ans);
    }

}


Measured memory_bytes: 35804
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();//９頂点
		int M = sc.nextInt();//１３辺
		
		List<Integer>[] edge = new ArrayList[N];//９頂点
		
		for(int i = 0; i < N; i++)
		    edge[i] = new ArrayList<>();//リストにリストを入れている
		    
		for (int i = 0; i < M; i++) {//辺の情報を入れている１３
		    int a = sc.nextInt()-1;//-1でインデックスを調整している
		    int b = sc.nextInt()-1;
		    edge[a].add(b);//各頂点から何処の頂点に繋がっているかの情報を入れている
		    edge[b].add(a);
		}
		int[] ans = new int[N];//頂点の数分の配列9
		Arrays.fill(ans, -1);
		
		Queue<Integer> s = new ArrayDeque<>();//[  ]
		ans[0] = 0;
		s.offer(0);//０をセット
		
		while(s.size() > 0) {
			int x = s.poll();//キューの先頭を取得して削除する
			for(int i : edge[x]) {//リストの中に入っている繋がっている頂点の数分回す拡張なので中の頂点の値
				if(ans[i] != -1) continue;//指定した頂点にまだ到達していなければ
				
					ans[i] = x+1;//0 + 1
					//ans[i] = ans[x] + 1;//0 + 1現在地の原点からのステップ＋１をしている
					s.add(i);//キューに１が追加される				
				
			}
		}
		System.out.println("Yes");
		for(int i = 1; i < N; i++)
			System.out.println(ans[i]);//[1][2][3]
	}

}


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

int main() {
	long long n,T;cin>>n>>T;
	vector<long long> t(n);
	long long ans=0;
	for(int i=0;i<n;i++){
		cin>>t[i];
	}
	for(int i=0;i<n-1;i++){
		if(t[i+1]-t[i]<T){
			ans+=t[i+1]-t[i];
		}else{
			ans+=T;
		}
	}
	ans+=T;
	cout<<ans<<endl;


	return 0;
}


Measured memory_bytes: 1792
#include<stdio.h>
int main(void){
	int tim[24][60],i,k,n,x,y,flg[24],flg2;
	for(i=1;i<24;i++){
		flg[i]=0;
		for(k=0;k<60;k++) tim[i][k]=0;
	}
	for(i=0;i<2;i++){
		scanf("%d",&n);
		for(k=0;k<n;k++){
			scanf("%d %d",&x,&y);
			flg[x]=1; tim[x][y]=1;
//printf("x=%d y=%02d\n",x,y);
		}
	}
	flg2=0;
	for(i=0;i<24;i++){
		if(flg[i]==1){
//printf("flg[%d]=%d\n",i,flg[i]);		
			for(k=0;k<60;k++){
				if(tim[i][k]==1) {
					if(flg2==1) printf(" %d:%02d",i,k);
					else{
						printf("%d:%02d",i,k); flg2=1;
					}
				}
			}
		}
	}
	printf("\n");
	return 0;
}


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

int main(){
    int n,m;
    cin >> n >> m;
    int memn=0,memm=0;
    if(n!=0){
        memn = n * (n-1) / 2;
    }
    if(m!=0){
        memm = m * (m-1) / 2;
    }
    cout << memn + memm << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <set>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int N, A;
  set<int> S;
  int ret = 1;
  cin >> N;
  for (int i=0; i<N; i++) {
    cin >> A;
    if (S.find(A) == S.end()) {
      S.insert(A);
    } else {
      ret = 0;
    }
  }
  if (ret) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

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

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  while (true) {
    c -= b;
    if (c <= 0) {
      cout << "Yes" << endl;
      return 0;
    }
    a -= d;
    if (a <= 0) {
      cout << "No" << endl;
      return 0;
    }
  }
}


Measured memory_bytes: 3624