akhauriyash/Code-Regression huggingface dataset:
template<typename T, typename F> const T bsearch(T l, T h, const T& err, const F& f) {
  if(f(l)) return l;
  while(h - l > err) {
    auto m = (l + h) / 2;
    f(m) ? (h = m) : (l = m);
  }
  return h;
}

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

using ULL = unsigned long long;

ULL L, A, B, M;

ULL power(ULL n, ULL m) {
  if(!m) return 1;
  if(m&1) return n * power(n, m-1) % M;
  return power(n*n % M, m/2);
}

ULL f(ULL n, ULL D) {
  if(!n) return 0;
  if(n&1) return (1 + D*f(n-1,D)) % M;
  auto m = n/2;
  auto x = f(m,D);
  return (x + power(D,m)*x) % M;
}

ULL g(ULL n, ULL D, ULL B) {
  if(!n) return 0;
  if(n&1) return (D*g(n-1,D,B) + B*(n-1)) % M;
  auto m = n/2;
  auto x = g(m,D,B);
  return (power(D,m)*x + x + (B*m % M)*f(m,D)) % M;
}

int main() {
  cin >> L >> A >> B >> M;
  ULL ans = 0;
  for(ULL D=1; D<=1e18; D*=10) {
    auto l = bsearch<ULL>(0, L, 1, [&](auto x) {return D <= A + B*x;});
    auto h = bsearch<ULL>(0, L, 1, [&](auto x) {return 10*D <= A + B*x;});
    if(L <= l) break;
    if(A + B*l < D) break;
    if(A + B*h < 10*D) ++h;
    if(L < h) h = L;
    if(!(l < h)) continue;
//     cerr<<"["<<l<<", "<<h<<")"<<endl;
    auto n = h - l;
    auto b = B % M;
    auto a = (A + b * l % M) % M;
    auto d = 10*D % M;
    (ans *= power(d, n)) %= M;
    (ans += a * f(n, d) % M) %= M;
    (ans += g(n, d, b) % M) %= M;
//     cerr<<"a="<<a<<"; n="<<n<<"; D="<<D<<"; f="<<f(n,d)<<"; g="<<g(n,d,b)<<"; ans="<<ans<<endl;
  }
  cout << ans << endl;
}


Measured memory_bytes: 3608
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// #include <ext/rope>
// using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define bb break
#define cc continue
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p) 
{ 
    ll res = 1;      // Initialize result 
    x = x % p;  // Update x if it is more than or 
    while (y > 0) 
    { 
        if (y & 1) 
            res = (res*x) % p; 
        y = y>>1; // y = y/2 
        x = (x*x) % p; 
    } 
    return res; 
} 
ll modInverse(ll n, ll p) 
{ 
    return power(n, p-2, p); 
} 
struct point
{
    ll x,y;
};
int main()
{
    FAST
    // freopen("input1.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    /*ll tests;
    cin>>tests;
    rep (gg,0,tests)
    {}*/
    ld w,h,x,y;
    cin>>w>>h>>x>>y;
    ll val=0;
    if (x==w/2&&y==h/2)
    {
        val=1;
    }
    cout<<fixed<<setprecision(15)<<(w*h)/2<<" "<<val<<"\n";
}


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

using namespace std;

#define YES cout << "YES" << endl;
#define NO  cout << "NO"  << endl;
#define Yes cout << "Yes" << endl;
#define No  cout << "No"  << endl;
 
#define INF INT_MAX
#define MOD 1000000007
#define PI  acos(-1)

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

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

int main(int argc, char *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    string S;
    cin >> S;

    ll a = 0;
    ll b = 0;
    ll c = 0;
    rep (i, S.size()) {
        if (S[i] == 'a') {
            a++;
        }
        if (S[i] == 'b') {
            b++;
        }
        if (S[i] == 'c') {
            c++;
        }
    }

    if (abs(a-b) >= 2 || abs(b-c) >= 2 || abs(c-a) >= 2) {
        NO;
    } else {
        YES;
    }
    return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

class C{
  public:
    string name;
    int p, a, b, c, d, e, f, s, m;
    C(){}
    bool operator < (const C &_) const {
      double t1 = a + b + c + (d + e) * m;
      double t2 = _.a + _.b + _.c + (_.d + _.e) * _.m;
      if((f * m * s - p) / t1 != (_.f * _.m * _.s - _.p) / t2) return (f * m * s - p) / t1 > (_.f * _.m * _.s - _.p) / t2;
      return name < _.name;
    }
};

int main() {
  int N; while(cin >>N && N){
    vector<C> v(N);
    REP(i, N) cin >>v[i].name >>v[i].p >>v[i].a >>v[i].b >>v[i].c >>v[i].d >>v[i].e >>v[i].f >>v[i].s >>v[i].m;
    SORT(v);
    REP(i, N) cout <<v[i].name <<endl;
    cout <<"#" <<endl;
  }
  return 0;
}

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

using namespace std;

int main() {
    // Input
    int N;
    vector<int> A;
    cin >> N;
    A = vector<int>(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    // Process
    sort(A.begin(), A.end());
    bool ans = false;
    for (int i = 1; i < N; i++){
        ans = (A[i  - 1] == A[i]);
        if (ans) break;
    }

    // Output
    printf("%s\n", (!ans ? "YES" : "NO"));

    return 0;
}


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

typedef unsigned long long  ULL;
typedef long long LL;

#define pb push_back
#define mp make_pair
#define reset(a, b) memset(a, b, sizeof(a))
#define rep(i,b) for(int i=0; i<b; i++)
#define F first 
#define S second

 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
    int T;
//    cin>>T;
    T=1;
    while(T--){
       
       string s;
       cin>>s;
       int n=s.length();
       int Q;
       cin>>Q;
       deque<char> q;
       rep(i,n) q.push_back(s[i]);
       
       bool w=true;
       while(Q--){
           int t;
           cin>>t;
            
            if(t==1){
                w=!w;
            }
            
            if(t==2){
                int f;
                cin>>f;
                char c;
                cin>>c;
                
                if(f==1){
                    if(w) q.push_front(c);
                    else q.push_back(c);
                }
                if(f==2){
                    if(w) q.push_back(c);
                    else q.push_front(c);
                }
                
            }
            
       }
       
       if(w) {
           for(auto i=q.begin(); i!=q.end(); i++) cout<<*i;
       }
       else for(auto i=q.rbegin(); i!=q.rend(); i++) cout<<*i;
       
    }
    
    return 0;
}

Measured memory_bytes: 976
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
#define ordered_set tree<int,null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define ordered_set_pair tree<pair<int,int>,null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update> 
#define ordered_set_mutiset tree<int,null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<int,int> pi; 
#define PI 3.1415926535897932384
#define FOR(i,vv,n) for(int i=vv;i<n;i++)
#define FORR(i,n,vv) for(int i=n-1;i>=vv;i--)
#define ve vector 
#define maxind(v) (max_element(v.begin(),v.end())-v.begin())
#define minind(v) (min_element(v.begin(),v.end())-v.begin())
#define maxe(v) *max_element(v.begin(),v.end())
#define mine(v) *min_element(v.begin(),v.end())
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mp make_pair
#define M 1000000007ll
#define INF 1000000000000000000ll
#define PRECISE cout.precision(18);
#define all(v) v.begin(),v.end()
#define BS(v,n) binary_search(all(v),n)
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(all(v),greater <ll>())
#define uni(v) v.resize(unique(all(v))-v.begin())
#define F first
#define S second
#define GET(i,p) get<p>(i)


int main(){


    FAST
//	PRECISE

ll n;
cin>>n;
ve <ll> v(n);
FOR(i,0,n)
cin>>v[i];
ll cm=1000;
ll last=0;
ll lcm=1000;
FOR(i,1,n){
	if(v[i]>v[i-1]){
		ll z=lcm/v[last];
		ll z1=z*v[i]+lcm-z*v[last];
		cm=z1;
	}
	else{
		last=i;
		lcm=cm;
	}
}
cout<<cm;


return 0;
 }
 

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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int n;
  cin >> n;
  vint a(n);
  int sum = 0;
  rep(i, n) cin >> a[i], sum += a[i];
  if(sum%(n*(n+1)/2)) {
    cout << "NO" << endl;
    return 0;
  }
  int rem = sum/(n*(n+1)/2);
  bool flag = true;
  rep(i, n-1) {
    int d = a[i+1]-a[i];
    if(rem-d < 0 || (rem-d)%n) flag = false;
  }
  cout << (flag ? "YES" : "NO") << endl;

  return 0;
}


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

using namespace std;

long long int dp[3001][3001][2];

long long int solve(long int a[], int i,int j,int t)
{
	if(dp[i][j][t]!=-1)
		return dp[i][j][t];
		
    else if(i==j)
    {
        int d;
        if(t==0)
            d = -1;
        else
            d = 1;

        dp[i][j][t] = d*a[i];
    }
    else if(t==1)
        dp[i][j][t] = max(solve(a,i+1,j,0)+a[i], solve(a, i,j-1,0)+a[j]);
    else
        dp[i][j][t] = min(solve(a, i+1,j,1)-a[i],solve(a, i,j-1,1)-a[j]);
    
    return dp[i][j][t];
}

int main()
{
    int n;
    cin>>n;
    long int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
	
	for(int i=0;i<3000;i++)
		for(int j=0;j<3000;j++)
			for(int t=0;t<2;t++)
				dp[i][j][t] = -1;
				
    cout<<solve(a,0,n-1,1);

    return 0;
    

}

Measured memory_bytes: 141184
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>

using namespace std;


int main() {
	int k;
	cin >> k;
	int s;
	cin >> s;
	long long int ans = 0;
	for (int i = 0; i < k+1; i++) {
		for (int j = 0; j < k+1; j++) {
			if (s - i - j < k + 1 && s - i - j>-1)
				ans += 1;
		}
	}
	cout << ans;
}

Measured memory_bytes: 256
package main

import (
  "fmt"
)

func main() {
  var n, count int
  fmt.Scan(&n)
  a := make([]int, n)
  for i := 0; i < n; i++ {
    fmt.Scan(&a[i])
  }

  d := 2
  result := -1
  for {
    for i := 0; i < n; i++ {
      if a[i] % d != 0 {
        result = count
        break
      }
    }
    if result != -1 {
      break
    }
    d *= 2
    count++;
  }
  fmt.Println(result)
}


Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <map>
#include <set>
using namespace std;
const int MAX_V = 1000;
const int INF = 1 << 25;

int V;
struct edge { int to, cap, rev; };

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
	G[from].push_back(edge{ to, cap, int(G[to].size()) });
	G[to].push_back(edge{ from, 0, int(G[from].size() - 1) });
}

void add_edge2(int from, int to, int cap) {
	G[from].push_back(edge{ to, cap, int(G[to].size()) });
	G[to].push_back(edge{ from, cap, int(G[from].size() - 1) });
}

int dfs(int v, int t, int f) {
	if(v == t) return f;
	used[v] = true;
	for(edge& e : G[v]) {
		if(!used[e.to] && e.cap > 0) {
			int d = dfs(e.to, t, min(f, e.cap));
			if(d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t, int lim) {
	int flow = 0;
	while(1) {
		fill(used, used + V, false);
		int f = dfs(s, t, lim);
		if(f == 0 || flow == lim) return flow;
		flow += f;
		lim -= f;
	}
	return -1;
}

int M[1000];
int A[1000];
int B[1000];

bool ex[1000][1000];

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

	int N, E, Q;
	cin >> N >> E >> Q;

	int S = N, T = N + 1;
	V = N + 2;
	for(int i = 0; i < E; i++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		add_edge2(a, b, 1);
		ex[a][b] = ex[b][a] = true;
	}
	for(int i = 0; i < Q; i++) {
		cin >> M[i] >> A[i] >> B[i];
		A[i]--, B[i]--;
		if(!ex[A[i]][B[i]]) {
			add_edge2(A[i], B[i], 0);
			ex[A[i]][B[i]] = ex[B[i]][A[i]] = true;
		}
	}
	add_edge(S, 0, 1000000);
	add_edge(N - 1, T, 1000000);

	int f = max_flow(S, T, INF);

	for(int i = 0; i < Q; i++) {
		int m = M[i], u = A[i], v = B[i];
		if(m == 1) {
			for(edge& e : G[u]) {
				if(e.to == v) {
					e.cap = G[v][e.rev].cap = 1;
					f += max_flow(S, T, 1);
					break;
				}
			}
		}
		else {
			for(edge& e : G[u]) {
				if(e.to == v) {
					int c = e.cap;

					if(c == 0) {// u -> v
						if(max_flow(u, v, 1) == 0) {
							max_flow(T, S, 1);
							assert(max_flow(u, v, 1) != 0);
							//assert(max_flow(u, S, 1) == 1);
							//assert(max_flow(T, v, 1) == 1);
							f--;
						}
					}
					else if(c == 2) {// v -> u
						if(max_flow(v, u, 1) == 0) {
							max_flow(T, S, 1);
							assert(max_flow(v, u, 1) != 0);
							//assert(max_flow(v, S, 1) == 1);
							//assert(max_flow(T, u, 1) == 1);
							f--;
						}
					}
					e.cap = G[v][e.rev].cap = 0;
					break;
				}
			}
		}
		cout << f << endl;
	}

}

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

using namespace std;

int main() {
  long long x;
  cin >> x;

  for (long long t = 1; t <= x; t++) {
    if ((1 + t) * t >= x * 2) {
      cout << t << endl;
      return 0;
    }
  }
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
	ll n;
	cin>>n;
	ll arr[n];
	for(ll i=0;i<n;i++){
		cin>>arr[i];
	}
	sort(arr,arr+n);
	reverse(arr,arr+n);
	ll count=0;
	ll index=0;
	for(ll i=1;i<=n-1;i++){
		count+=arr[index];
		if(i%2==1){
			index++;
		}
	}
	cout<<count<<"\n";
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;

int main() {
    string S;
    cin >> S;
    int s = S.size();
    if (S[s - 1] != 's') {
        cout << S << 's' << endl;
    } else {
        cout << S << "es" << endl;
    }
}

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

int main(){
    std::string s,t;
    std::cin>>s>>t;
    int count =0;
    for (int i = 0; i < 3; i++)
    {
        if(s[i]==t[i]){
            count++;
        }
    }
    std::cout<<count;
    return 0;
}

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

long long A[200009], S[200009], X[200009];

int main() {
    int N; scanf("%d",&N);
    for(int i=1; i<=N; i++) {
        scanf("%lld",&A[i]);
        S[i] = S[i-1] + A[i];
        X[i] = X[i-1] ^ A[i];
    }
    long long ans = 0;
    for(int i=1; i<=N; i++) {
        int l = i, r = N, f = i;
        while(l <= r) {
            int m = l+r >> 1;
            if(S[m] - S[i-1] == (X[m] ^ X[i-1])) l = m+1, f = m;
            else r = m-1;
        }
        ans += f - i + 1;
    }
    printf("%lld", ans);
    return 0;
}


Measured memory_bytes: 4992
/******************************************
*******************************************
*******************************************
	PREPARED BY - SOMESH AWASTHI 
	CHANDIGARH UNIVERSITY
*******************************************
*******************************************
*******************************************/

#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;

int main()
{
	io
	#ifdef SOMU
		clock_t startTime = clock();
		freopen("input.txt","r",stdin);
	#endif

	string a, b;
	cin >> a >> b;
	int n = a.size(), m = b.size();
	ll dp[n + 1][m + 1] = {0};
	fr(i, 0, n + 1) {
		fr(j, 0, m + 1) {
			if(i == 0 || j == 0) {
				dp[i][j] = 0;
			}
			else if(a[i - 1] == b[j - 1]) {
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}
			else {
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	}
	string ans;
	int i = n, j = m;
	while(i > 0 && j > 0) {
		if(dp[i][j] == dp[i - 1][j]) {
			i--;
		}
		else if(dp[i][j] == dp[i][j - 1]) {
			j--;
		}
		else {
			ans += a[i - 1];
			i--, j--;
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans;

	#ifdef SOMU
		cout << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
	#endif
	return 0;
}

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

typedef long long ll;
ll index_ary[100005];

int search(ll a[], ll x, ll n){
  for(ll i=0; i<n; i++)
    if(a[i] == x)
      return 1;
  return 0;
}

int main() {
  ll n,k;
  cin >> n >> k;
  
  map<ll,ll> mp;
  ll unique_index = 0;
  for(ll i=0; i<n; i++){
    ll x, y;
    cin >> x >> y;
    mp[x] += y;
    if(search(index_ary, x, unique_index)==0){
      index_ary[unique_index] = x;
      unique_index++;
    }
  }
  
  sort(index_ary, index_ary + unique_index);
  
  ll counter = 0;
  for(int i=0; i<unique_index; i++){
    counter += mp[index_ary[i]];
    if (counter > k-1){
      cout << index_ary[i] << endl;
      return 0;
    }
  }
}

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

using namespace std;
typedef long long ll;

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

	bool flag = false;
	for(int i = 0; i < n ; i++){
		cin >> c;
		if(c == 'Y'){
			flag = true;
		}
	}

	if(flag) cout << "Four" << endl;
	else cout << "Three" << endl;
}

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

int main(){
	int n,y;
	cin>>n>>y;
	for(int i=0;i<=n;++i)
		for(int j=0;i+j<=n;++j)
			if(i*10000+j*5000+(n-i-j)*1000==y){
				cout<<i<<" "s<<j<<" "s<<n-i-j<<"\n"s;
				return 0;
			}
	cout<<"-1 -1 -1\n"s;
}

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

const int N = 1e5 + 5;

int cnt[30], a[30], n;
char s[N];

int main() {
	for (int i = 0; i < 26; i++) cnt[i] = 100;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		scanf("%s", s + 1);
		int m = strlen(s + 1);
		for (int j = 0; j < 26; j++) a[j] = 0;
		for (int j = 1; j <= m; j++) a[s[j] - 'a']++;
		for (int i = 0; i < 26; i++) cnt[i] = min(cnt[i], a[i]);
	}
	for (int i = 0; i < 26; i++)
		for (int j = 1; j <= cnt[i]; j++)
			cout << (char)('a' + i);
}



Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  int sum=0;
  for(int i=0;i<n;i++){
    cin >> a[i];
    sum+=a[i];
    a[i]*=n;
  }
  int t=1000000,ans=0;
  for(int i=0;i<n;i++){
    if(t>abs(sum-a[i])){
      t=abs(sum-a[i]);
      ans=i;
    }
  }
  cout << ans << endl;
}

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

typedef long long ll;
#define pii pair<int , int>
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAXX 1005

int r;

int main()
{
    _FastIO;
    cin >> r;
    cout << r * r << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_map>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<string>
#include<iomanip>
#include<bitset>

using namespace std;

#define INF 1 << 29
#define LL long long int

LL const MOD = 1000000007;

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

    LL n;
    cin >> n;

    vector<LL> factorization;

    LL tmp = n;
    LL x = 2;
    while(tmp > 1){
        if(tmp%x == 0){
            tmp /= x;
            factorization.push_back(x);
            x--;
        }
        x++;
    }

    factorization.erase( unique(factorization.begin(), factorization.end()), factorization.end() );

    LL y = factorization.size();

    LL m = (1 << y);

    LL ans = 0;

    for(LL i = 0; i < m; i++){
        bitset<20> b(i);
        LL t = 1;
        for(int k = 0; k < y; k++){
            if(b[k]){
                t *= factorization[k];
            }
        }
        if(b.count() == 0){

        }else if(b.count()%2 == 0){
            ans -= n/t;
        }else{
            ans += n/t;
        }
    }

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


Measured memory_bytes: 3236
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

int main()
{
	LL N, A, B;
	in >> N >> A >> B;
	std::vector<LL>P(N);
	rep(i, N) in >> P[i];

	std::sort(P.begin(), P.end());
	LL cnt1 = 0, cnt2 = 0, cnt3 = 0;
	rep(i, N)
	{
		if (P[i] <= A) ++cnt1;
		if (A < P[i] && P[i] <= B) ++cnt2;
		if (P[i] > B) ++cnt3;
	}

	out << std::min({ cnt1,cnt2,cnt3 }) << std::endl;
}


Measured memory_bytes: 256
using System;
class p
{
  static void Main()
  {
    string[] input = Console.ReadLine().Split(' ');
    int a = int.Parse(input[0]);
    int b = int.Parse(input[1]);
    
    int[] grp1 = new int[] { 1, 3, 5, 7, 8, 10, 12 };
    int[] grp2 = new int[] { 4, 6, 9, 11 };
    int[] grp3 = new int[] { 2 };
    
    bool r1 = ((0 <= Array.IndexOf(grp1, a)) && (0 <= Array.IndexOf(grp1, b)));
    bool r2 = ((0 <= Array.IndexOf(grp2, a)) && (0 <= Array.IndexOf(grp2, b)));
    bool r3 = ((0 <= Array.IndexOf(grp3, a)) && (0 <= Array.IndexOf(grp3, b)));
    Console.WriteLine((r1 | r2 | r3) ? "Yes" : "No");
  }
}


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

const ll INF = 1e18;

int N;
ll h[100001];
ll dp[100001];

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

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


int main() {
  int A, B;
  cin >> A >> B;

  int count = A + A - 1;
  if(count < A + B) {
    count = A + B;
  }
  if(count < B + B - 1) {
    count = B + B -1;
  }

  cout << count << endl;

  return 0;
}





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

int main() {
    int a, b, k;
    cin >>a >>b >>k;
    for (int i = a; i < a+k; i++) {
        if (i > b) break;
        cout << i << endl;
    }
    for (int i = b-k+1; i <= b; i++) {
        if (i < a+k) continue;
        cout << i << endl;
    }
    return 0;
}

Measured memory_bytes: 256
N=int(input())
H=list(map(int, input().split()))

dp=[float('inf')]*N
dp[0]=0
dp[1]=abs(H[1]-H[0])
for i in range(2,N):
  dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]),dp[i-2]+abs(H[i]-H[i-2]))
print(dp[N-1])

Measured memory_bytes: 52208
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=203,INF=1<<29;
int S[10][10];

int f(int num){
    int a=num/1000;
    num%=1000;
    int b=num/100;
    num%=100;
    int c=num/10;
    num%=10;
    int d=num/1;
    num%=1;
    
    int ret=0;
    ret=S[ret][a];
    ret=S[ret][b];
    ret=S[ret][c];
    ret=S[ret][d];
    
    return ret;
}

bool check(int num){
    int z=num/10000;
    num%=10000;
    int a=num/1000;
    num%=1000;
    int b=num/100;
    num%=100;
    int c=num/10;
    num%=10;
    int d=num/1;
    num%=1;
    
    int ret=0;
    ret=S[ret][z];
    ret=S[ret][a];
    ret=S[ret][b];
    ret=S[ret][c];
    ret=S[ret][d];
    
    if(ret==0) return true;
    return false;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            cin>>S[i][j];
        }
    }
    
    int cnt=0;
    
    for(int i=0;i<=9999;i++){
        bool ok=true;
        int e=f(i);
        int num=i*10+e;
        string T=to_string(num);
        
        while(T.size()<5) T='0'+T;
        
        for(int j=0;j<4;j++){
            if(T[j]!=T[j+1]){
                swap(T[j],T[j+1]);
                int a=stoi(T);
                
                if(check(a)) ok=false;
                
                swap(T[j],T[j+1]);
            }
        }
        
        for(int k=0;k<5;k++){
            for(char j='0';j<='9';j++){
                if(T[k]==j) continue;
                
                string U=T;
                T[k]=j;
                
                int a=stoi(T);
                
                if(check(a)) ok=false;
                
                T=U;
            }
        }
        
        if(ok) cnt++;
    }
    
    cout<<10000-cnt<<endl;
    
    //cout<<f(2016)<<endl;
    
    
}



Measured memory_bytes: 3300
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0511

#include <algorithm>
#include <iostream>
#include <vector>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

//テ」ツδ榲」ツつッテ」ツδュテ・ツョツ堙ァツセツゥ
#define ALL(v) (v).begin(),(v).end()
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define REP(i,a,b) for(int i=(int)(a); i<(int)(b); ++i)
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define SORT(x) sort(begin(x),end(x))
#define SELECT_SORT(x, n) sort(begin(x),begin(x)+n)
#define SWAP(type, a, b) do {type tmp = a; a = b; b = tmp;} while (0)
#define rep_split(tok,a_str,re) for(char *tok = strtok((char *)a_str.c_str(),re); tok != NULL; tok = strtok(NULL,re))

//テ・ツョツ堙ヲツ閉ー
#define INF 2000000000000
#define MAX 10

//DEBUGテァツ板ィ
#define BAR() cout << "----------------------------" << endl;
#define DUMP(v) (cerr << #v << ": " << v << endl)
#define PRINT(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;


int main()
{
	int check[31]={0};
	
	int tmp=0;
	rep(i,28){
		cin >> tmp;
		check[tmp]=1;
	}
	
	REP(i,1,31){
		if(check[i]==0){
			cout << i << endl;
		}
	}
	
}

Measured memory_bytes: 1160
#include<stdio.h>
#define N 20000
int main(){
  int n,Rt[1000000],max,i,min,j;

  scanf("%d",&n);

  for(i =0;i < n;i++){
    scanf("%d",&Rt[i]);
  }

  max = Rt[1] - Rt[0];
  min = Rt[0];

  for(j = 1;j < n;j++){
    if(max < Rt[j] - min){max = Rt[j] - min;}
    if(min > Rt[j]){min = Rt[j];}
  }

  printf("%d\n",max);

  return 0;
}

Measured memory_bytes: 3400
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn=new Scanner(System.in);
		int n=scn.nextInt();
		int m = scn.nextInt();
		int val1=n*(n-1)/2;
		val1+=m*(m-1)/2;
		System.out.println(val1);

	}

}

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

import static java.lang.System.in;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), Q = sc.nextInt();
        int[] pos = new int[n+1];
        for(int i=0;i<=n;i++) pos[i]=i;
        boolean[] vis = new boolean[n+1]; // records the cup number
        vis[1]=true;vis[2]=true;
        int cur = 1; // current position of cup #1
        for(int i=0;i<Q;i++){
            int a =sc.nextInt(), b = sc.nextInt();
            if(a==cur) cur = b;
            else if(b==cur) cur = a;
            swap(pos,a,b);
            if(cur<n) vis[pos[cur+1]] = true;
            if(cur>1) vis[pos[cur-1]] = true;

        }
        int ans = 0;
        for(int i=1;i<=n;i++) ans += vis[i]?1:0;
        System.out.println(ans);
    }
    static void swap(int[] pos, int i, int j){
        int c = pos[i];
        pos[i] = pos[j];
        pos[j] = c;
    }
}


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

signed main(){
    int n;
    cin >> n;
    int ans=0;
    for(int i=1;i<=n;i++){
        if(i%3==0 || i%5==0) continue;
        else ans+=i;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3588
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>

using namespace std;


int main(){
    int a,p;
    cin >> a >> p;

    int tmp = a*3;

    int x = (tmp+p)/2;

    cout << x  << endl;
    

    return 0;
}



Measured memory_bytes: 888
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <assert.h>
#include <numeric>

using namespace std;

bool S[300001];
int ldp[300001];
int rdp[300001];

int solve() {
    long N;
    cin >> N;
    ldp[0] = 0;
    for (int i = 0; i < N; i++) {
        char c;
        cin >> c;
        
        S[i] = c == 'W';
        if (i > 0) {
            ldp[i] = ldp[i-1]+(S[i-1] == true);
        }
    }
    
    int ans = 300001;
    for (int i = N-2; i >= 0; i--) {
        rdp[i] = rdp[i+1]+(S[i+1] == false);
        ans = min(rdp[i]+ldp[i], ans);
    }
    ans = min(rdp[N-1]+ldp[N-1], ans);
    
    return ans;
}

int main() {
    cout << solve() << endl;
#ifdef DEBUG
    while (1) {
        cout << solve() << endl;
    }
#endif
}


Measured memory_bytes: 2944
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N,K;
  int x;
  cin >> N >> K;
  int total = 0;
  for (int i = 0; N > i; i++){
    cin >> x;
    if (x *2 < (K-x)*2){
      total +=x*2;
    }
    else{
      total +=(K-x)*2;
    }
  }
  cout << total << endl;
}

Measured memory_bytes: 256
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    private static final PrintStream so = System.out;
    private static final Scanner     sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a);
        if (n == 2) {
            so.println(a[1] - a[0]);
            return;
        }
        
        int mid = a.length/2;
        long ans1 = calc(a, mid, true);
        long ans2 = calc(a, mid, false);
        
        so.println(Math.max(ans1, ans2));
    }
    
    static long calc(int[] a, int mid, boolean leftFirst) {
        long sum = 0;
        int l = 0;
        int r = a.length-1;
        int tail = a[mid];
        while (l < mid || r > mid) {
            if (l == mid) {
                sum += Math.abs(tail - a[r]);
                tail = a[r];
                r--;
            } else if (r == mid) {
                sum += Math.abs(tail - a[l]);
                tail = a[l];
                l++;
            } else if (leftFirst) {
                sum += Math.abs(tail - a[l]);
                tail = a[l];
                l++;
                leftFirst = false;
            } else {
                sum += Math.abs(tail - a[r]);
                tail = a[r];
                r--;
                leftFirst = true;
            }
        }
        return sum;
    }
}


Measured memory_bytes: 50528
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

struct UnionFind {
    int n, cc, *u;
    UnionFind() : n(0), cc(0), u(NULL) {}
    UnionFind(int n_) : n(n_), cc(n_) {
	u = new int[n_];
	memset(u, -1, sizeof (int) * n);
    }
    UnionFind(const UnionFind &y) : n(y.n), cc(y.cc) {
	u = new int[y.n];
	memcpy(u, y.u, sizeof (int) * n);
    }
    ~UnionFind() {
	delete[] u; u = NULL;
	n = cc = 0;
    }
    friend void swap(UnionFind &x, UnionFind &y) {
	swap(x.n, y.n); swap(x.cc, y.cc); swap(x.u, y.u);
    }
    UnionFind& operator=(UnionFind y) { 
	swap(*this, y);
	return *this;
    }
    int root(int x) {
	int y = x, t;
	while (u[y] >= 0) y = u[y];
	while (x != y) { t = u[x]; u[x] = y; x = t; }
	return y;
    }
    bool link(int x, int y) {
	x = root(x); y = root(y);
	if (x == y) return false;
	if (u[y] < u[x]) swap(x, y);
	u[x] += u[y]; u[y] = x; cc--;
	return true;
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -u[root(x)]; }
    int count() { return cc; }
};
const int MAXN = 100111;

int N, M;
int A[MAXN];
VI Z[MAXN];

void MAIN() {
    scanf("%d%d", &N, &M);
    REP (i, N) scanf("%d", A+i);

    UnionFind U(N);
    REP (i, M) {
	int x, y;
	scanf("%d%d", &x, &y);
	U.link(x, y);
    }

    int cc = N - M;
    if (cc == 1) {
	puts("0");
	return;
    }
    int ee = (cc - 1) * 2;
    int cnt = 0;
    LL ans = 0;
    VI Y;
    if (N < ee) {
	puts("Impossible");
	return;
    }


    REP (i, N) Z[U.root(i)].push_back(A[i]);
    REP (i, N) if (Z[i].size()) {
	sort(Z[i].begin(), Z[i].end());
	ans += Z[i][0];
	cnt++;
	for (int j=1; j<cc-1 && j <(int)Z[i].size(); j++) {
	    Y.push_back(Z[i][j]);
	}
    }

    sort(Y.begin(), Y.end());
    REP (i, ee-cnt) ans += Y[i];
    printf("%lld\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 5504
s = input()
q = int(input())
length = len(s)
que = [[''] * 3 for i in range(q)]
for i in range(q):
    c = input()
    if c[0] == '1':
        que[i][0] = 1
    else:
        n, f, c = map(str, c.split())
        que[i][0], que[i][1], que[i][2] = int(n), int(f), c

rev_cnt = 0
left = [''] * 200000
right = [''] * 200000
left_cnt = 0
right_cnt = 0
for com in que:
    if com[0] == 1:
        rev_cnt += 1
        left, right = right, left
        left_cnt, right_cnt = right_cnt, left_cnt
    else:
        if com[1] == 1:
            left[left_cnt] = com[2]
            left_cnt += 1
        else:
            right[right_cnt] = com[2]
            right_cnt += 1

l = list(reversed(left[0:left_cnt]))
if rev_cnt % 2 == 0:
    center = list(s)
else:
    center = list(reversed(list(s)))
ans = l + center + right[0:right_cnt]
for c in ans:
    print(c, end='')
print()


Measured memory_bytes: 33880
#include"bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
const long long mod = 1000000007;
const long long inf = 11451419198109130;
typedef pair<int, int> P;
typedef pair<P, int> PP;
typedef pair<int, PP> PPP;
struct edge { int to; int to2; int cost; };

signed main() {
	int a, b; cin >> a >> b;
	cout << max({a*2-1,b*2-1,a+b}) << endl;
}	


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>

#include <numeric>

#include <map>

using namespace std;

typedef long long LL;

int n;

bool ok(float x, float y) {
    //cout << x << y << endl;
    float z = x-y;
    return z<1.0-1e-10 && z >= 0;
}

int main() {

    float a, b, c;
    cin >> a >> b;
    for (c = 1; c <= 10000; ++c) {
        //cout << c << endl;
        if (ok(c*0.08, a) && ok(c*0.10,b)) {
            cout << int(c) << endl;
            return 0;
        }
    }
    cout << -1 << endl;

}

Measured memory_bytes: 256
import numpy as np
N = int(input())
total = []
dp = np.zeros((N,3), dtype = int)
for i in range(N):
  tmp = list(map(int,input().split()))
  if i == 0:
    dp[i][0] = tmp[0]
    dp[i][1] = tmp[1]
    dp[i][2] = tmp[2]
  else:
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + tmp[0]
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + tmp[1]
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + tmp[2]
print(max(dp[N-1][0],dp[N-1][1],dp[N-1][2]))


Measured memory_bytes: 14780
#include <stdio.h>
int main(){
	int ans = 0;
	int A=0,B=0,C=0,X=0;
	scanf("%d",&A);
	scanf("%d",&B);
	scanf("%d",&C);
	scanf("%d",&X);
	for(int i=0;i<=A;i++){
		for(int j=0;j<=B;j++){
			for(int k=0;k<=C;k++){
				int tmp = i*500 + j*100 + k*50;
				if(tmp == X) ans++;
				}
		}
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int main(void){
	int K, X;
	cin >> K >> X;

	for(int i = X - K + 1; i <= X + K - 1; i++){
		cout << i << (i==X+K ? '\n' : ' ');
	}

	return 0;
}

Measured memory_bytes: 256
#%%
#%%
import sys
sys.setrecursionlimit(10 ** 9)

N, Q = [int(i) for i in input().split()]
path = [[] for _ in range(N - 1)]
query = [[] for _ in range(Q)]
for i in range(N - 1):
    path[i] = [int(i) - 1 for i in input().split()]
for i in range(Q):
    query[i] = [int(i) for i in input().split()]
    query[i][0] -= 1
#%%
add = [0] * N
ans = [0] * N
for i, j in query:
    add[i] += j
    
children = [[]  for _ in range(N)]
for i, j in path:
    children[i].append(j)
    children[j].append(i)
    
def DFS(id, parent, value):
    value += add[id]
    ans[id] = value
    for i in children[id]:
        if i == parent:
            continue
        DFS(i, id, value)
    
DFS(0, -1, 0)

for i in ans:
    print(i, end=" ")


Measured memory_bytes: 289876
#include <bits/stdc++.h>
//#include <stdlib.h>
 
using namespace std;
/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

const int MAX_MEM = 6e8;
int mpos = 0;
char mem[MAX_MEM];
inline void * operator new ( size_t n ) {
char *res = mem + mpos;
mpos += n;
assert(mpos <= MAX_MEM);
return (void *)res;
}
inline void operator delete ( void * ) { }
*/
#define DIM 1000009
#define INF ((long long)1e18+9ll)
#define pairll pair<long long,long long>
#define fi first
#define se second
#define ld long double
#define amen exit(0)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
#define y1 trymax_lox
#define y2 dsfjkjfkkv
#define MODULO 1000000007
#define pi acos(-1)
#define p 29

long long i,j,k,l,n,m,res;

long long hsh[DIM],d[DIM],cnt[DIM];

int main(){
  /*  #ifndef LOCAL
        freopen("robot.in","r",stdin);
        freopen("robot.out","w",stdout);
    #endif*/
 
    //fast;	

	cin>>n;
	cout<<360/__gcd(360ll,n)<<endl;

    amen;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int K; cin >> K;
    string S; cin >> S;
    int size_S = S.length();
    if(K >= size_S){
        cout << S << endl;
    }
    else{
      string ans = S;
      ans = ans.erase(K);
      string ten = "...";
      ans = ans + ten;
      cout << ans << endl;
    }
}

Measured memory_bytes: 3636
s=input()
t=input()
cnt=0
mod=-1
tmp_s=""
for cha in t:
    if cha not in s:
        print(-1)
        exit()
    elif cha in tmp_s:
        idx=tmp_s.find(cha)
        mod+=idx+1   
    else:
        idx=s.find(cha)
        if idx<=mod:
            cnt+=1  
        mod=idx
    
    if mod==len(s)-1:
        tmp_s=" "
    else:
        tmp_s=s[mod+1:]

print(len(s)*cnt+mod+1)

Measured memory_bytes: 3540
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	sc.Split(bufio.ScanWords)
	h := nextInt()
	//完全二分木のノード数を求める
	depth := 0
	ans := 0
	for h > 0 {
		ans += int(math.Pow(2.0, float64(depth)))
		depth++
		h /= 2
	}
	fmt.Println(ans)
	//fmt.Println(int(math.Pow(2.0, float64(depth))) - 1)
}

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

#define rep(i,n) for (int i = 0; i< (n); ++i)
#define vvec(m,n) vector<vector<int>> (int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using ll = long long;

int main(){
    char X, Y;
    cin >> X >> Y;
    char res;
    if(X > Y) res = '>';
    else if(X == Y) res = '=';
    else res = '<';
    cout << res << endl;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var stdInScanner = bufio.NewScanner(os.Stdin)

func nextInt() int {
	stdInScanner.Scan()
	i, _ := strconv.Atoi(stdInScanner.Text())
	return i
}

func isPrime(n int) bool {
	switch {
	case n < 2:
		return false
	case n == 2 || n == 3 || n == 5:
		return true
	case n&1 == 0:
		return false
	case n%2 == 0 || n%3 == 0 || n%5 == 0:
		return true
	}

	f := true
	v := 7
	m := int(math.Pow(float64(n), 0.5)) + 1
	for v < m {
		if n%v == 0 {
			return false
		}
		if f {
			v += 4
		} else {
			v += 2
		}
		f = !f
	}
	return true
}

func main() {
	stdInScanner.Split(bufio.ScanWords)

	N := nextInt()
	n := N

	if N == 1 {
		fmt.Println(0)
		return
	}

	answer := 0

	for i := 2; i*i <= N; i++ {
		e := 0
		for n%i == 0 {
			e++
			n /= i
		}
		for x := 1; x <= e; x++ {
			answer++
			e -= x
		}
	}
	if n != 1 {
		answer++
	}
	fmt.Println(answer)
}


Measured memory_bytes: 1776
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<numeric>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i = m; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a)  (a).rbegin(),(a).rend()
#define dup(x,y) (((x)+(y)-1) / (y)); //整数型のまま切り上げ演算する
#define PI 3.14159265359
#define MOD 1000000007
typedef long long LL;
template<class T> inline void chmax(T& a, T b) { if (a < b) { a = b; } }
template<class T> inline void chmin(T& a, T b) { if (a > b) { a = b; } }
template<class T> T gcd(T x, T y) { if (y == 0) { return x; } else if (x == 0) { return y; }return gcd(y, x % y); }
template<class T> void print_vector(vector<T> vec) { for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } }

bool all_zero(vector<LL> vec) {
	bool is_all_zero = true;
	rep(i, vec.size()) {
		if (vec[i] != 0) {
			is_all_zero = false;
		}
	}

	return is_all_zero;
}

vector<LL> all_decrement(vector<LL> vec) {
	rep(i, vec.size()) {
		vec[i]--;
		chmax(vec[i], 0LL);
	}
	return vec;
}

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

	vector<LL> h(N + 1);
	REP(i, 1, N+1) cin >> h[i];

	LL ans = 0;
	bool area_decision_mode = true;
	while (!all_zero(h)) {
		for (LL i = 0; i <= N; i++) {
			if (h[i] == 0 and area_decision_mode) {
				area_decision_mode = false;
			}
			else if (h[i] != 0 and !area_decision_mode) {
				ans++;
				area_decision_mode = true;
			}
			else {
				continue;
			}
		}
		h = all_decrement(h);
	}

	cout << ans << endl;


	return 0;
}



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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

template<typename T>
class tRMQ {
  vector<T> data;
  T unit;

public:
  static const long long INF = 100000000000;
  int n;
  function<T(const T &, const T &)> f;

  tRMQ(int _, T u, function<T(T, T)> bi) {
    unit = u;
    f = bi;
    n = 1;
    while (n < _) {
      n <<= 1;
    }
    data.resize(n * 4);
    for (int i = 0; i < n * 4; i++)
      data[i] = unit;
  }

  void update(int index, T val) {
    int i = index + n - 1;
    data[i] = val;
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = f(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  // [a, b)
  T query(int a, int b, int k, int l, int r) {
    if (a < 0 || r <= a || b <= l)
      return unit;
    if (a <= l && r <= b)
      return data[k];
    else
      return f(query(a, b, k * 2 + 1, l, (l + r) / 2),
               query(a, b, k * 2 + 2, (r + l) / 2, r));
  }

  T query(int a, int b) { return query(a, b, 0, 0, n); }
};

tRMQ<ll> minrmq(int n) {
  return tRMQ<ll>(n, 10000000000000000LL, [](ll r, ll l) { return min(l, r); });
}

tRMQ<ll> maxrmq(int n) {
  return tRMQ<ll>(n, -10000000000000000LL,
                  [](ll r, ll l) { return max(l, r); });
}

tRMQ<ll> sumrmq(int n) {
  return tRMQ<ll>(n, 0, [](ll l, ll r) { return l + r; });
}

ll dp[2020][2020] = {0};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, m, l;
  cin >> n >> m >> l;
  vector<ll> k(n), s(m);
  for (int i = 0; i < n; i++) cin >> k[i];
  for (int i = 0; i < m; i++) cin >> s[i];
  sort(k.begin(), k.end());
  auto r = sumrmq(m);
  for (int i = 0; i < m; i++) r.update(i, s[i]);
  ll INF = 1LL << 60;
  for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = INF;
  dp[0][0] = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < n - 1; j++) {
      int next = max(i, j) + 1;
      dp[i][next] = min(dp[i][next], dp[i][j] + r.query(k[j] - 1, k[next]) / l);
      dp[next][j] = min(dp[next][j], dp[i][j] + r.query(k[i] - 1, k[next]) / l);
    }
  }
  ll ans = INF;
  for (int i = 0; i < n - 1; i++) ans = min(ans, dp[n - 1][i] + r.query(k[i] - 1, k[n - 1]) / l);
  for (int i = 0; i < n - 1; i++) ans = min(ans, dp[i][n - 1] + r.query(k[i] - 1, k[n - 1]) / l);
  cout << ans << endl;

  return 0;
}



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

int main() {
    string S;
    cin >> S;
    int ans = 0;
    if (S == "RRR") ans = 3;
    else if (S == "RRS" || S == "SRR") ans = 2;
    else if (S == "SSS") ans = 0;
    else ans = 1;
    cout << ans << endl;
    return 0;
}

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

struct Directions
{
	int E[6] = { 2, 1, 5, 0, 4, 3 };
	int W[6] = { 3, 1, 0, 5, 4, 2 };
	int N[6] = { 4, 0, 2, 3, 5, 1 };
	int S[6] = { 1, 5, 2, 3, 0, 4 };
};

class Dice
{
private:
	int num[6];
	char dir_command[105];
	Directions dir_array;
	void swap(int*);
public:
	Dice();
	int condition(int);
	void roll(int);
	void show_top();
	void show_member();
};

Dice::Dice()
{
	Directions direction;
	for (int i = 0; i < 6; i++)
		cin >> this->num[i];
	cin >> this->dir_command;
}

int Dice::condition(int i)
{
	if (dir_command[i] == '\0')
		return 0;
	else
		return 1;
}

void Dice::roll(int i)
{
	switch (this->dir_command[i])
	{
	case 'E':
		this->swap(dir_array.E);
		break;
	case 'W':
		this->swap(dir_array.W);
		break;
	case 'N':
		this->swap(dir_array.N);
		break;
	case 'S':
		this->swap(dir_array.S);
		break;
	}
	return;
}

void Dice::swap(int *dir)
{
	int tmp[6];
	for (int i = 0; i < 6; i++)
		tmp[dir[i]] = num[i];
	for (int i = 0; i < 6; i++)
		num[i] = tmp[i];
	return;
}

void Dice::show_top()
{
	cout << this->num[0] << endl;
	return;
}

int main()
{
	int a;
	Dice dice;

	for (int i = 0; dice.condition(i); i++)
	{
		dice.roll(i);
	}
	dice.show_top();

	return 0;
}


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

int main(){
    std::string S, T;
    std::cin >> S >> T;
    std::size_t n = S.size();
    for(int start = 0; start < n; start++){
        int i;
        for(i = 0; i < n; i++){
            if(S[(start + i) % n] != T[i]) break;
        }
        if(i == n){
            std::cout << "Yes" << std::endl;
            return 0;
        }
    } 

    std::cout << "No" << std::endl;
    return 0;
}


Measured memory_bytes: 256