akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define st first
#define nd second
#define INF 1000000007LL
using namespace std;

int main(){
#ifndef LOCAL
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#endif
    int n; cin >> n;
    vector<int> ile(100005);
    ll sum = 0;
    for(int i = 0; i < n; i++){
      int x; cin >> x;
      sum += x;
      ile[x] ++;
    }
    int q; 
    cin >> q;
    while (q--) {
      ll a, b; cin >> a >> b;
      sum -= ile[a] * a;
      sum += ile[a] * b;
      ile[b] += ile[a];
      ile[a] = 0;
      cout << sum << '\n';
    }  
}

Measured memory_bytes: 3696
N,M = map(int,input().split())
nes = []
count = 0
for i in range(N) :
    li = list(map(int,input().split()))
    nes.append(li)
favorite = [0] * M
for i in range(N) :
    for j in range(1,nes[i][0]+1) :
        favorite[nes[i][j]-1] += 1
for i in range(M) :
    if favorite[i] == N :
        count+=1
print(count)

Measured memory_bytes: 38384
import string
import itertools

alphabets = string.ascii_lowercase

N = int(input())

res = []


def f(s):
    return max([ord(t) - ord("a") for t in s])


def bfs(s, d):
    if d == N - 1:
        res.append(s)
        return

    for i in range(f(s) + 2):
        bfs(s + alphabets[i], d + 1)


bfs("a", 0)

res = sorted(res)
print("\n".join(res))
# print(list(bfs("a", 0)))


Measured memory_bytes: 15724
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
typedef long long ll;

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

int main() {
    int N ;
    cin >> N;
    vector<ll>A(N);
    rep(i, 0, N){
        cin >> A[i];
    }
    
    ll ans = 10000000000;
    rep(i, 0, N-1){
        ans = min(ans, gcd(A[i], A[i+1]));
    }
    
    
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000005
bool isPrime(int n){
  for(int i=2;i*i<=n;i++) if(n%i==0) return 0;
  return 1;
}
int main(){
  int n;
  bool d[MAX]={};
  for(int i=2;i<MAX;i++) d[i]=isPrime(i);
  while(cin>>n,n){
    int ans=0;
    for(int i=2;i<=n/2;i++){
      if(d[i]&&d[n-i]) ans++;
    }cout << ans << endl;
  }
}


Measured memory_bytes: 4060
def main():
    n, m = map(int, input().split())
    group = {i: [] for i in range(1, n + 1)}
    for i in range(m):
        p, y = map(int, input().split())
        group[p].append([y, i])
    answer = ["" for _ in range(m)]
    for i in range(1, n + 1):
        group[i].sort(key=lambda x: x[0])
    for i in range(1, n + 1):
        for j, yi in enumerate(group[i]):
            y, k = yi
            answer[k] = str(i).zfill(6) + str(j + 1).zfill(6)
    print("\n".join(answer))


if __name__ == '__main__':
    main()



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

int main(){
  long long n;
  cin >> n;
  vector<long long> p(n);
  long long s = 0;
  for(int i = 0; i < n; i++){
    cin >> p[i];
    s += p[i];
  }
  long long sum = 0;
  long long ans = s;
  for(int i = 0; i < n; i++){
    sum += p[i];
    ans = min(ans, abs(sum - (s - sum)));
  }
  cout << ans << endl;
}

Measured memory_bytes: 4824
#include <stdio.h>

int J(int n, int m) {
    if (n == 1) return 0;
    return (J(n-1, m) + m) % n;
}

int main() {
    for (;;) {
        int n, m;
        scanf("%d%d", &n, &m);
        if (n == 0 && m == 0) return 0;
        printf("%d\n", J(n, m) + 1);
    }
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

ll DP[51][(1 << 20)];
vector<P> Graph[55];
int N;

vector<int> Path(int a,int b) {
  bool seen[N];
  vector<vector<int>> res(N);
  for (int i = 0; i < N; i++) seen[i] = false;
  queue<pair<vector<int>,int>> que;
  vector<int> emp;
  que.push(make_pair(emp,a));
  while (!que.empty()) {
    pair<vector<int>,int> p = que.front();
    que.pop();
    if (seen[p.second]) {
      continue;
    } else {
      seen[p.second] = true;
      res[p.second] = p.first;
    }
    for (int i = 0; i < (int)Graph[p.second].size(); i++) {
      int s = Graph[p.second][i].first;
      if (!seen[s]) {
        vector<int> L = p.first;
        L.push_back(Graph[p.second][i].second);
        que.push(make_pair(L,s));
      }
    }
  }
  return res[b];
}

int main() {
  cin >> N;
  for (int i = 0; i < N-1; i++) {
    int a,b;
    cin >> a >> b;
    a--; b--;
    Graph[a].emplace_back(b,i);
    Graph[b].emplace_back(a,i);
  }
  int M;
  cin >> M;
  int vec[N-1];
  for (int i = 0; i < N-1; i++) vec[i] = 0;
  vector<vector<int>> lis(M);
  for (int i = 0; i < M; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    lis[i] = Path(u,v);
    for (int j = 0; j < (int)lis[i].size(); j++) {
      vec[lis[i][j]] += (1 << i);
    }
  }

  // bitDPの更新
  DP[0][0] = 1;
  for (int i = 0; i < N-1; i++) {
    for (int j = 0; j < (1 << M); j++) {
      DP[i+1][j] += DP[i][j];
      DP[i+1][(j | vec[i])] += DP[i][j];
    }
  }

  ll ans = DP[N-1][(1 << M) - 1];
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 405068
n = int(input())
v = input().split()
o = {"":0}
e = {"":0}
for s in v[::2]:
    if s in o:
        o[s] += 1
    else:
        o[s] = 1

for s in v[1::2]:
    if s in e:
        e[s] += 1
    else:
        e[s] = 1

o = sorted(o.items(), key=lambda x:x[1])
e = sorted(e.items(), key=lambda x:x[1])

if o[-1][0] != e[-1][0]:
    print(n-o[-1][1]-e[-1][1])
else:
    print(n - max(o[-1][1]+e[-2][1], o[-2][1]+e[-1][1]))

Measured memory_bytes: 20016
#include<bits/stdc++.h>
using namespace std;
int a[3];
int main(){
	cin>>a[0]>>a[1]>>a[2];
	sort(a,a+3);
	if(a[0]+a[1]==a[2])cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cmath>
#define INF 1000000000
#define MOD 1000000007
#define ll long long int
using namespace std;

int main(){
  string s; cin>>s;
  if(s[0] == s[2]) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;

}


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

#define forin(i, f, t) for(auto i = f; i < t; i++)

using namespace std;
using ll = long long;


double f(ll n) {
    auto cn = n;
    ll sn = 0;
    while (cn > 0) {
        sn += cn % 10;
        cn /= 10;
    }
    return double(n) / sn;
}

int main() {
    ll k;
    cin >> k;
    
    vector<ll> v;
    ll d = 1;
    for (auto i = 0; i < 15; i++) {
        for (auto j = 1; j < 150; j++) {
            v.push_back(d * (j + 1) - 1);
        }
        d *= 10;
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    
    vector<double> fv(v.size(), 0);
    for (auto i = 0; i < fv.size(); i++)
        fv[i] = f(v[i]);
    
    for (auto i = 0; i < fv.size(); i++) {
        for (auto j = i + 1; j < fv.size(); j++) {
            if (fv[i] > fv[j]) {
                v.erase(v.begin() + i);
                fv.erase(fv.begin() + i);
                i--;
                break;
            }
        }
    }
    
    for (auto i = 0; i < k; i++)
        cout << v[i] << endl;
    
    return 0;
}



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

using namespace std;

int main(){
    int k;
    cin>>k;
    string s;
    cin>>s;
    for (int i = 0; i< s.size(); i++){
        s[i] += k;
        if (s[i] > 'Z'){
            s[i] = 64 + (s[i] - 'Z');
        }
    }
    cout<<s;
    return 0;
}


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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  auto max = [](int a, int b) { return a > b ? a : b; };
  int N, X;
  std::cin >> N >> X;
  std::vector<int> P(N);
  for (int i = 0; i < N; i++) std::cin >> P[i];
  std::sort(P.begin(), P.end());
  int res{0};
  for (int i = 1; i < N; i++) res = gcd(res, P[i] - P[i - 1]);
  int maxd{0};
  for (auto&& v : P) maxd = max(maxd, gcd(std::abs(X - v), res));
  std::cout << maxd;
}


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


int main() {
	int a, b;
	cin>>a;
	cin>>b;
	
	if ((a>0 && a<10) && (b>0 && b<10)){
		cout<<a*b;
		cout<<"\n";
	}
	else cout << "-1\n";
	
	
	return 0;
}

Measured memory_bytes: 256
N = int(input())
S = [input() for i in range(N)]

D = {'AC':0 , 'WA':0, 'TLE':0, 'RE':0 }

for i in range(len(S)):
    D[S[i]] += 1

for k,v in D.items():
    print('{} x {}'.format(k,v))

Measured memory_bytes: 16172
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] a = new int[n];
		long sum = 0l;
		for (int i=0; i<n; i++) {
			a[i] = sc.nextInt();
			sum += 2*Math.min(a[i], Math.abs(a[i] - k));
		}
		System.out.println(sum);
		sc.close();
	}

}


Measured memory_bytes: 27220
#include <stdio.h>

int main(void){
    int N;
    int sum = 0;
    scanf("%d", &N);
    int n = N;
    for (int i= 1.0e9; i > 0; i /= 10){
        sum += n / i;
        n = n % i;
    }
    printf("%s\n", (N % sum == 0) ? "Yes" : "No");
    return 0; 
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define LL long long
using namespace std;

LL to_int(string str){
    LL a = str[0] - '0';
    for(int i = 1; i < str.size(); i++){
        a = a * 10 + (str[i] - '0');
    }
    return a;
}

LL partSum(string str, int back, int front){
    LL delimiter = (!back ? back : back - 1) + (!front ? front : front - 1);
    return to_int(str) << delimiter;
}

int main(){
    string str;
    cin >> str;
    LL all = str.size();
    LL sum = 0;
    for(int part = 1; part <= all; part++){
        for(int pos = 0; pos <= all - part ; pos++){
            sum += partSum(str.substr(pos, part), pos, all - pos - part);   //posからpart文字分
        }
    }
    cout << sum << endl;
    return 0;
}


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

#define FOR(i, j, k) for (int i = j; i < k; i++)
#define REP(i, j) FOR(i, 0, j)
#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

template <class T> bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) {
        is >> x;
    }
    return is;
}

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n;
    cin >> n;

    int ans = 0;

    FOR(i, 1, n + 1) {
        if (1 <= i and i < 10)
            ans++;
        else if (100 <= i and i < 1000)
            ans++;
        else if (10000 <= i and i < 100000)
            ans++;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
     
using namespace std;
     
typedef long long ll;
typedef pair<int, int> pii;
     
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());

int ab(int x){
    return max(x,-x);
}
    

int main(){
    fastIO;
    int n;
    cin >> n;
    int a[n];
    int total = 0;
    int pref =0 ;
    for(int i = 0 ; i < n; i ++ ){
        cin >> a[i];
        total += a[i];
    }
    int res = total;
    for(int i = 0 ; i < n ; i ++ ){
        pref += a[i];
        res = min(res, ab(pref - (total - pref)));
    }
    cout << res << "\n";
    return 0;
}


Measured memory_bytes: 256
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))

a.sort()
f.sort(reverse=True)


def ok(v):
    return sum([max(0, (a[i] * f[i] - v + (f[i] - 1)) // f[i]) for i in range(n)]) <= k


if ok(0):
    print(0)
    exit()
mi = 0
ma = 10 ** 12
while mi+1 < ma:
    m = (mi + ma) // 2
    if ok(m):
        ma = m
    else:
        mi = m
print(ma)


Measured memory_bytes: 137500
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> P;
typedef pair<long long, long long> LLP;

int main() {
    int N;
    string S;
    cin >> N >> S;
    int ans = 1;
    
    for (int i = 1; i < N; i++) {
        if (S[i] != S[i - 1]) ans++;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 512
n = int(input())
coord = []

for i in range(n):
    x, y, h = map(int, input().split())
    coord.append([h, x, y])
    if h > 0:
        x0, y0, h0 = x, y, h

flag = False


for cx in range(0, 101):
    for cy in range(0, 101):
        flag = True

        H = h0 + abs(x0 - cx) + abs(y0 - cy)

        for i in range(n):
            if coord[i][0] != max(H - abs(coord[i][1] - cx) - abs(coord[i][2] - cy), 0):
                flag = False
                break

        if flag is True:
            res_x = cx
            res_y = cy
            res_h = H

print(res_x, res_y, res_h)


Measured memory_bytes: 74308
//----->|try=0; while(!success) try++;|<------
//----->|Belief Yourself,Respect Yourself|<----
//----->|Be Proud Of Yourself,You're Doing Your best|<-----
#include<bits/stdc++.h>
using namespace std;
#define uniq(x)  x.erase(unique(x.begin(),x.end()), x.end()) //Unique value find from vector
#define upper(arr,n,fixed) upper_bound(arr,arr+n,fixed)-arr  //Upper value search;
#define lower(arr,n,fixed) upper_bound(arr,arr+n,fixed)-arr  //Lower value search;
#define max3(a,b,c) max(max(a,b),c)//maximum value find three value;
#define min3(a,b,c) min(min(a,b),c)//minimum value find three value;
#define rep(i,intial,n) for(int i=intial; i<(n) ; i++)
#define REP(i,intial,n) for(int i=intial; i<=(n) ; i++)
#define PI acos(-1.0)//PI Calculation
#define LL long long
#define AND(a,b) ((a) & (b))
#define OR(a,b) ((a)|(b))
#define XOR(a,b) ((a) ^ (b))
#define MP make_pair
#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define INF_MAX 2147483647
#define INF_MIN -2147483647
#define MX 1000005
#define MOD 1000000007
template<typename T> T POW(T b,T p)          //Pow calculation
{
    T r=1;
    while(p)
    {
        if(p&1)r=(r*b);
        b=(b*b);
        p>>=1;
    }
    return r;
}

template<typename T> T BigMod(T b,T p,T m) //BigMod Calculation
{
    T r=1;
    while(p)
    {
        if(p&1)r=(r*b)%m;
        b=(b*b)%m;
        p>>=1;
    }
    return r;
}

//||--------------------------->||Main_Code_Start_From_Here||<---------------------------------||
int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    string s,s1,s2,s3,s4;
    while(cin>>s)
    {
        s1="dream";
        s2="dreamer";
        s3="erase";
        s4="eraser";
        reverse(s1.begin(),s1.end());
        reverse(s2.begin(),s2.end());
        reverse(s3.begin(),s3.end());
        reverse(s4.begin(),s4.end());
        //cout<<"s1 = "<<s1<<" s2 = "<<s2<<endl;
        //cout<<"s3 = "<<s3<<" s4 = "<<s4<<endl;
        string ss="";
        reverse(s.begin(),s.end());
        rep(i,0,s.size())
        {
            ss+=s[i];
            if(ss==s1) ss="";
            if(ss==s2) ss="";
            if(ss==s3) ss="";
            if(ss==s4) ss="";
            if(ss.size()>7)
            {
                break;
            }
        }
        //cout<<"ss = "<<ss<<endl;
        if(ss.size()!=0) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }

}


Measured memory_bytes: 512
def insertionSort(A,n,g,cnt):
	for i in range(g,n):
		v = A[i]
		j = i - g
		while j >= 0 and A[j] > v:
			A[j+g] = A[j]
			j = j - g
			cnt += 1
		A[j+g] = v
	return A,cnt

def shellSort(A,n):
	cnt = 0
	G = [1]
	while G[-1] <= n/9:
		G.append(G[-1]*3 + 1)
	G = G[::-1]
	m = len(G)
	for g in G:
		A,cnt = insertionSort(A,n,g,cnt)
	return A,m,G,cnt

if __name__ == '__main__':
	
	n = int(input())
	A = []
	[A.append(int(input())) for i in range(n)]
	
	'''
	data = [int(d) for d in data.split('\n')]
	n = data.pop(0)
	A = data[:]
	'''
	
	A,m,G,cnt = shellSort(A,n)
	print(m)
	print(' '.join([str(i) for i in G]))
	print(cnt)
	[print(a) for a in A]

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

string S;

int main()
{

    cin >> S;

    for (char c = 'a'; c <= 'z'; c++)
    {
        bool exist = false;
        for (int j = 0; j < S.size(); j++)
        {
            if(c == S[j]){
                exist = true;
                break;
            } 
            
        }
        if(!exist){
            cout << c << endl; 
            return 0;   
        } 
    }

    cout << "None" << endl;
    return 0;
}


Measured memory_bytes: 512
using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

class Solve
{
	const int mod = 1000000007;
	static void Main(string[] args)
	{
		var t = Console.ReadLine().Split().Select(int.Parse).ToArray();
		Console.WriteLine((t[0] + t[1]) % 24);
	}
}


Measured memory_bytes: 13268
#include <bits/stdc++.h>
// Hey! 僕の提出を見てくれてありがとう.
// ロボ子さん可愛いぞ！！！！！君もV沼にはまろう↓
// https://www.youtube.com/channel/UCDqI2jOz0weumE8s7paEk6g
using namespace std;
using ll = long long;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}//a,bの最大公約数(gcd)を求める
ll lcm(ll a,ll b){return abs(a*b)/gcd(a,b);}//a,bの最小公倍数(lcm)を求める
vector<ll> enum_div(ll n){vector<ll> ret;for(int i=1 ; i*i<=n ; i++){if(n%i == 0){ret.push_back(i);if(i!=1 && i*i!=n)ret.push_back(n/i);}}ret.push_back(n);return ret;}
//↑nの約数を求める
vector<bool> IsPrime; void sieve(size_t max){if(max+1 > IsPrime.size())IsPrime.resize(max+1,true);IsPrime[0] = false;IsPrime[1] = false;for(size_t i=2; i*i<=max; ++i)if(IsPrime[i])for(size_t j=2; i*j<=max; ++j)IsPrime[i*j] = false;}
//↑エラトステネスの篩で素数を求める
#define roundup(divisor,dividend) (divisor + (dividend - 1)) / dividend //切り上げ割り算
#define all(x) (x).begin(),(x).end() //xの初めから終わりまでのポインタ
#define size_t ll //size_tは自動でllに変換される
#define pb(x) push_back(x)
#define pri_queue priority_queue //優先度付きキュー
#define syo(x) fixed << setprecision(x) //iostreamで小数をx桁表示
//sortを降順にする時は greater<型>()
void solve(std::string S){
  cout << (S == "ABC" ? "ARC" : "ABC") << endl;
}

int main(){
    std::string S;
	std::cin >> S;
    solve(S);
}


Measured memory_bytes: 3632
#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    int ans = 0;
    rep(i, n) {
        for (int j = i + 1; j < n; ++j) {
            for (int k = j + 1; k < n; ++k) {
                if (i == j || j == k || k == i) continue;
                if (a[i] == a[j] || a[j] == a[k] || a[k] == a[i]) continue;
                if (a[i] + a[j] > a[k] && a[j] + a[k] > a[i] && a[k] + a[i] > a[j]) {
                    //cout << i + 1 << " " << j + 1 << " " << k + 1 << endl;
                    ++ans;
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP2(i, a, b) for (int i = (a); i < (int)(b); i++)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef long long ll;
#define ALL(V) V.begin(), V.end()
template<class T,class U>bool chmin(T&a,const U&b){if(a<=b)return false;a=b;return true;}
template<class T,class U>bool chmax(T&a,const U&b){if(a>=b)return false;a=b;return true;}

VI factorial(int n) {
    VI ans(n, 1);
    REP2(i, 1, n) {
        ans[i] = ans[i-1] * (i+1);
    }
    return ans;
}

int calc_index(int N, VI &A, VI &f) {
    int ans = 0;
    VI digits(N);
    REP(i, N) digits[i] = i+1;
    REP(i, N-1) {
        auto it = find(ALL(digits), A[i]);
        int d = distance(digits.begin(), it);
        ans += d * f[N-2-i];
        digits.erase(it);
    }
    return ans;
}

int Main(int N, VI &P, VI &Q)
{
    int ans = 0;
    VI f = factorial(N);
    int a = calc_index(N, P, f);
    int b = calc_index(N, Q, f);
    return abs(a-b);
}


int main(void){
    int N;
    cin >> N;
    VI P(N), Q(N);
    REP(i, N) cin >> P[i];
    REP(i, N) cin >> Q[i];
    cout << Main(N, P, Q) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T>
using vec = std::vector<T>;


int main() {
    int n;
    cin >> n;
    vec<int> a(n);
    rep(i,n) cin >> a[i];
    sort(all(a));
    int mx = a[n-1];
    int half = mx/2;
    
    int index = lower_bound(all(a),half) - a.begin();
    if(index == 0) cout << mx << " " << a[index] << endl;
    else if(index == n-1) cout << mx << " " << a[n-2] << endl;
    else {
        int a1 = a[index];
        int a2 = a[index-1];
        if(abs(half-a1) <= abs(half-a2)){
            cout << mx << " " << a1 << endl;
        } else {
            cout << mx << " " << a2 << endl;
        }
    }
}


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

int main(void){

  double inputN = 0;
  double result = 0;

  scanf("%lf\n",&inputN);

  for(int i = 1;i <= inputN;i++){
    if(i % 2 == 1){
      result++;
    }
  }

  printf("%lf\n",result/inputN);

}

Measured memory_bytes: 128
n, k = map(int, input().split())
data = list(map(int, input().split()))
mod = 10**9 + 7

invs = []
for idx in range(n):
    result = [0,0]
    for idx2 in range(n):
        if idx2 < idx:
            loc = 1
        elif idx2 > idx:
            loc = 0
        else:
            continue
        if data[idx2] < data[idx]:
            result[loc] += 1
    invs.append(result)
ans = 0
for aft, bef in invs:
    ans += aft*k
    m = (bef+aft)*(k-1)
    ans += (m*k)//2
print(ans%mod)


Measured memory_bytes: 3360
//==========================================c#
/* ===========================================
// null
// 
// 
=========================================== */
using System; //Console, IComparable
using System.IO; //StreamWriter 
using System.Linq; //Select(), Where(), OrderBy(), Count()
using System.Collections.Generic; //List<T>

class Program { 
    static void Main() { 
        //StreamWriter so = new StreamWriter(Console.OpenStandardOutput());
        IOC io = new IOC();
        
        int n = io._int;
        var hash = new Dictionary<string, int>();
        
        for (int i = 0; i < n; i++){
            string s = io._string;
            if (!hash.ContainsKey(s)){
                hash.Add(s, 1);
            }
            else{
                hash[s]++;
            }
        }
        
        var max_cnt = hash.Values.Max();
        
        var ans = new List<string>();
        foreach(var fd in hash){
            if (fd.Value == max_cnt){
                ans.Add(fd.Key);
            }
        }
        
        ans.Sort(StringComparer.Ordinal);
        
        foreach(var fd in ans){
            Console.WriteLine(fd);
        }
        
    } 
}

public class IOC{
    public int _int{get{return int.Parse(Console.ReadLine());}}
    public int[] _inta{get{return Array.ConvertAll(Console.ReadLine().Split(), int.Parse);}}
    public long _long{get{return long.Parse(Console.ReadLine());}}
    public long[] _longa{get{return Array.ConvertAll(Console.ReadLine().Split(), long.Parse);}}
    public string _string{get{return Console.ReadLine();}}
    public string[] _stringa{get{return (Console.ReadLine().Split());}}
    public double _double{get{return double.Parse(Console.ReadLine());}}
    public double[] _doublea{get{return Array.ConvertAll(Console.ReadLine().Split(), double.Parse);}}
}

/* ===========================================
            int n = int.Parse(Console.ReadLine());

for(int i = 0; i < n; i++){
                int sum = Console.ReadLine().Split().Select(int.Parse).Sum();
                so.WriteLine(sum.ToString());
            }
            so.Close();
=========================================== */



Measured memory_bytes: 40284
N,K=map(int,input().split())
#N=10
#K=0
s=0

if(K==0):
    print(N**2)
    exit()

for b in range(K,N+1):
    s+=(b-K)*(N//b)
    s+=max(0,N-b*(N//b)-K+1)

print(s)


Measured memory_bytes: 2940
n, k = map(int, input().split())
p = list(map(int, input().split()))
l = [0] * (n + 1)
ans = []
for i in range(n):
    l[i + 1] = l[i] + (p[i] + 1) / 2
for i in range(n - k + 1):
    ans.append(l[i + k] - l[i])
print(max(ans))

Measured memory_bytes: 25060
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1000000007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

template<typename T>
vector<T> gen_v(size_t a) { return vector<T>(a); }
template<typename T, typename ...Ts>
auto gen_v(size_t a, Ts... ts) { return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...)); }
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int X; cin >> X;
	
	int t = X / 100;
	if (t * 5 >= X % 100) {
		cout << 1 << endl;
	}
	else {
		cout << 0 << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<string>
#include<utility>
#include<vector>

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define per(i, a, n)  for(int i=n-1; i>=a; i--)
#define all(x)  (x).begin(), (x).end()
#define N 100000
using namespace std;

int main(){
  int n;
  int dp[N];
  cin >> n;
  rep(i, 0, N+1)  dp[i] = 100000000;
  dp[0]=0;
  rep(i, 0, N){
    dp[i+1] = min(dp[i+1], dp[i]+1);
    for(int j=6; j+i<=N; j*=6){
      dp[i+j] = min(dp[i+j], dp[i]+1);
    }
    for(int j=9; j+i<=N; j*=9){
      dp[i+j] = min(dp[i+j], dp[i]+1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


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

using namespace std;

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

  if((b-a)%2){
    cout << min(a, n-b+1)+ (b-a-1)/2 << endl;
  }
  else{
    cout << (b-a)/2 << endl;
  }
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
using namespace std;
int main() {
    int a=0;
    string k,b="x";
    cin >> k;
    for (int i=0; i < 15; ++i) {
        if (k[i] == b[0]) {
            ++a;
        }
    }
    if (a > 7) {
        cout << "NO";
    }
    else {
        cout << "YES";
    }

}

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

int main()
{
    char c[12], res = '0';
    int cnt = 0;

    cin >> c;

    int qu = 0;
    for( int i = 11; i >= 0; i-- )
        if( c[i] == '?' )
        {
            qu = i;
            break;
        }

    for( char i = '0'; i <= '9'; i++ )
    {
        c[qu] = i;
        int sum = 0;
        for( int j = 11; j >= 1; j-- )
        {
            int q = 0;
            if( 1 <= j && j <= 6 )
                q = j + 1;
            else
                q = j - 5;

            sum += ( c[11-j] - '0' ) * q;
        }

        if( sum % 11 <= 1 )
            sum = 0;
        else
            sum = 11 - ( sum % 11 );

        if( (int)( c[11] - '0' ) == sum )
        {
            res = c[qu];
            cnt++;
        }
    }
    
    if( cnt >= 2 )
        cout << "MULTIPLE" << endl;
    else
        cout << res << endl;

    return 0;
}


Measured memory_bytes: 3108
///////////////////////////////////////////////////////////////////////////////

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

using namespace std;

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

#define pb push_back

struct T2 {
        long t0;
        long t1;
};

struct T3 {
        long t0;
        long t1;
        long t2;
};

void longin(long &a)
{
        cin >> a;
}

void longinl1(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a;
                cin >> a;
                v.push_back(a);
        }
}

void longinl2(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a, b;
                cin >> a >> b;
                v.push_back({a, b});
        }
}

void longinl3(auto &v, long count)
{
        for (long i = 0; i < count ; ++i) {
                long a, b, c;
                cin >> a >> b >> c;
                v.push_back({a, b, c});
        }
}

void longina(auto &v, long count)
{
        longinl1(v, count);
}

long min(const vector<long> v)
{
        long ret = (long)numeric_limits<double>::infinity();
        for (auto i : v) ret = min(ret, i);
        return ret;
}

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

int main()
{
        long n, q;
        longin(n); longin(q);

        string s;
        cin >> s;

        vector<T2> lrlist;
        longinl2(lrlist, q);

        vector<long> counts;
        vector<long> subs;
        subs.pb(0);
        long count = 0;
        long sub = 0;
        char prev = ' ';
        for (size_t i = 0; i < s.size(); ++i) {
                if (prev == 'A' && s[i] == 'C') {
                        count++;
                        sub++;
                        subs[i] = sub;
                }
                counts.pb(count);
                subs.pb(sub);
                prev = s[i];
        }

        for (auto lr : lrlist) {
                long l = lr.t0;
                long r = lr.t1;
                cout << counts[r-1] - subs[l-1] << endl;
        }

        return 0;
}

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


Measured memory_bytes: 5488
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Set<Character> input = new HashSet<>();
        char[] S = scan.next().toCharArray();
        for (char temp : S) {
            if (input.contains(temp)){
                System.out.println("no");
                return;
            }
            input.add(temp);
        }
        System.out.println("yes");
    }
}

Measured memory_bytes: 21844
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 解説通り実装したソースコード
 * 
 * http://baitop.hatenadiary.jp/entry/2018/06/26/224712 にも参考
 */
public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt(), k = scanner.nextInt(), l = scanner.nextInt();
			UnionFindTree road = new UnionFindTree(n);
			IntStream.range(0, k).forEach(i -> road.unite(scanner.nextInt() - 1, scanner.nextInt() - 1));
			UnionFindTree railway = new UnionFindTree(n);
			IntStream.range(0, l).forEach(i -> railway.unite(scanner.nextInt() - 1, scanner.nextInt() - 1));
			Map<List<Integer>, Integer> map = new HashMap<>();
			@SuppressWarnings("unchecked")
			List<Integer>[] keys = new List[n];
			IntStream.range(0, n).forEach(i -> {
				keys[i] = Arrays.asList(road.root(i), railway.root(i));
				map.put(keys[i], map.getOrDefault(keys[i], 0) + 1);
			});
			System.out.println(
					IntStream.range(0, n).mapToObj(i -> map.get(keys[i]).toString()).collect(Collectors.joining(" ")));
		}
	}

	/**
	 * Union Find Treeの実装<br/>
	 * http://baitop.hatenadiary.jp/entry/2018/06/26/224712 を参考に作成
	 */
	private static class UnionFindTree {
		int[] parent;
		int[] rank;
		int[] sizes;

		UnionFindTree(int n) {
			parent = new int[n];
			rank = new int[n];
			sizes = new int[n];
			Arrays.fill(rank, 0);
			IntStream.range(0, n).forEach(i -> {
				parent[i] = i;
				sizes[i] = 1;
			});
		}

		int root(int x) {
			if (parent[x] == x) {
				return x;
			} else {
				return parent[x] = root(parent[x]);
			}
		}

		void unite(int x, int y) {
			x = root(x);
			y = root(y);

			if (x == y) {
				return;
			} else if (rank[x] < rank[y]) {
				parent[x] = y;
				sizes[y] += sizes[x];
			} else {
				parent[y] = x;
				sizes[x] += sizes[y];
				if (rank[x] == rank[y]) {
					rank[x]++;
				}
			}
		}

		@SuppressWarnings("unused")
		boolean isSame(int x, int y) {
			return root(x) == root(y);
		}

		@SuppressWarnings("unused")
		int size(int x) {
			return sizes[parent[x]];
		}
	}
}


Measured memory_bytes: 95080
from collections import deque

h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dydx = [[0, 1], [1,0], [-1, 0], [0, -1]]

def find_longest_path(xy):
    visited = [[0]*w for _ in range(h)]
    dist = [[0]*w for _ in range(h)]
    visited[xy[1]][xy[0]]=1
    que = deque()
    que.append(xy)
    while que:
        v = que.popleft()
        x,y = v[0],v[1]
        for i in dydx:
            dx = i[0]
            dy = i[1]
            if ((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
                if(grid[y+dy][x+dx]=="."):
                    que.append([x+dx, y+dy])
                    visited[y+dy][x+dx]=1
                    dist[y+dy][x+dx]=dist[y][x]+1
                    u,k = x+dx, y+dy
                    r = dist[y+dy][x+dx]

    return r

def main():
    k=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]==".":
                k = max(k, find_longest_path([j,i]))
    print(k)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3316
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <functional>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <deque>
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repr(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFL 1100000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<Pi> > vvPi;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi> > pqlP;
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
template <class T>
using vec = vector<T>;
template<class T>
using pql = priority_queue<T, vector<T>, greater<T>>;
string debug_show(Pi a) {
	return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
}

template<class T>
struct augEdge {
	T from, to;
	int cost;
	bool operator<(augEdge e) { return cost < e.cost; }
	bool operator>(augEdge e) { return cost > e.cost; }
};
template<class T>
using augGraph = vector<augEdge<T>>;



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

	string s;
	cin >> s;
	int a = s.find('A');
	int b = s.rfind('Z');
	cout << b - a + 1 << endl;

	return 0;
}
 

Measured memory_bytes: 720
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <math.h>
#include <stdio.h>
#include <climits>
#include <cfloat>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <cctype>

using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i=0;i<n;i++){
		a.at(i) = 0;
	}
	for(int i=0;i<n-1;i++){
		int x;
		cin >> x;
		a.at(x-1)++;
	}
	for(int i=0;i<n;i++){
		cout << a.at(i) << endl;
	}
	return 0;
}

Measured memory_bytes: 4000
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;


int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int N, q;
	cin >> N >> q;

	string s;
	cin >> s;

	Int len = s.size();

	//累積和
	vector<Int> a(len + 1, 0);
	for (Int i = 0; i < len; i++) {
		if ((s[i] == 'A') && (s[i + 1] == 'C')) {
			a[i + 1] = a[i] + 1;
			continue;
		}
		a[i + 1] = a[i];
	}

	//答えの出力
	Int l, r;
	for (Int i = 0; i < q; i++) {
		cin >> l >> r;
		l--;
		r--;
		printf("%lld\n", a[r] - a[l]);
	}

}


Measured memory_bytes: 1872
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <iostream>
#include <fstream>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <cmath>
#include <set>
#include <map>
#include <functional>
#include <bitset>
#include <iomanip>
#include <stack>
#include <list>
using namespace std;
#define ll           long long
#define all(v)       (v).begin() , (v).end()
#define vi           vector <ll>
#define vii          vector <pair<ll,ll>>
#define ii           pair<ll,ll>
#define sz(v)	     ((int)((v).size()))
#define lp(i,a,b)    for(int i=a;i<b;i++)
#define pb           push_back
#define pf           push_front
#define F            first
#define S            second
#define endl         "\n"
#define IOS          ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf=2e9,MOD=1e9+7,MAX=1e5+2;


int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    IOS;
    int n;
    cin>>n;
    ll res[100]={2,1};
    lp(i,2,n+1)
        res[i]=res[i-1]+res[i-2];
    cout<<res[n];
}


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

int main() {
    int n, num, cards[4][13] = {};
    char suit;
    cin >> n;

    rep(i,n) {
        cin >> suit >> num;
        if (suit == 'S') cards[0][num-1]++;
        else if (suit == 'H') cards[1][num-1]++;
        else if (suit == 'C') cards[2][num-1]++;
        else if (suit == 'D') cards[3][num-1]++;
    }

    rep(i,4) {
        rep(j,13) {
            if (!cards[i][j]) {
                if (i == 0) cout << "S " << j+1 << endl;
                else if (i == 1) cout << "H " << j+1 << endl;
                else if (i == 2) cout << "C " << j+1 << endl;
                else if (i == 3) cout << "D " << j+1 << endl;
            }
        }
    }
}


Measured memory_bytes: 3104
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
//ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
int main() {
	ll c = 0;
	string s;
	cin >> s;
	ll len = s.size();
	map<char, int> mp;
	rep(i, 0, len) {
		c += i - mp[s[i]]++;
	}
	printf("%lld\n", c + 1);
	return 0;
}


Measured memory_bytes: 640
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h" 
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i,s,e) for (int i = int(s); i < int(e); ++i)
#define REP(i,e) FOR(i,0,e)
#define INF (INT_MAX/2)
#define EPS (1.0e-8)
#define LINF (LLONG_MAX/2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long; using ull = unsigned long long;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vl = vector<ll>; using vvl = vector<vl>;
using vd = vector<double>; using vs = vector<string>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
// functions

#endif


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


    int N;  cin >> N;
    vi T(N); REP(i, N) cin >> T[i];
    vi V(N); REP(i, N) cin >> V[i];

    vi ts(N+1, 0);
    ts[0] = 0;
    FOR(i, 1, N+1) ts[i] = ts[i - 1] + T[i - 1];

    const int TMAX = N*200;
    vi v(TMAX, INF);
    vi vMax(TMAX,INF);

    int End = ts[N];
    vMax[0] = 0;
    vMax[End] = 0;
    REP(i,N) {
        for (int t=ts[i]; t<=ts[i+1]; t++) {
            vMax[t] = min(vMax[t], V[i]);
        }

        for(int t=ts[i]; t<ts[i+1]; t++) {
            v[t] = V[i];
        }
    }

    for(int t = 1; t < End; t++){
        vMax[t] = min(vMax[t], vMax[t-1]+1);
    }

    for(int t = End-1; t >= 1; t--){
        vMax[t] = min(vMax[t], vMax[t+1]+1);
    }

    double ans = 0.0;
    for(int t = 1; t <= End; t++){
        if (vMax[t] != vMax[t-1]) {
            ans += ((double)(vMax[t-1] + vMax[t]))/2.0;
        } else {
            if (vMax[t] == v[t-1]) { // move at the max speed 
                ans += vMax[t];
            } else { // accelerate or decelerate the speed
                ans += vMax[t] + 0.25;
            }
        }
    }

    cout << fixed << setprecision(15) << ans << endl;


    return 0;
}

Measured memory_bytes: 384
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    auto S = sread();
    auto prev = "";
    long ans;
    long i;
    while (i < S.length)
    {
        long j = 1;
        while (i + j < S.length && S[i .. i + j] == prev)
            j++;
        if (i + j != S.length || S[i .. i + j] != prev)
            ans++;
        prev = S[i .. i + j];
        i += j;
        // writeln(prev, i);
    }
    writeln(ans);
}


Measured memory_bytes: 764
/* Justin Choi */
#include <bits/stdc++.h>
using namespace std;

string FILENAME = "filename";
#define io ifstream fin(FILENAME + ".in"); ofstream fout(FILENAME + ".out")
#define fwrite(X) fout << X;
#define fwriteln(X) fout << X << endl;

#define forl(V, N) for(int V = 0; V < N; V++)
#define fore(E, X) for(auto &E:X)
#define println(X) cout << X << endl
#define printe(X) for(auto &E:X) cout << E << " "; cout << endl
#define rem(X) X.top(); X.pop()
#define setmin(A, B) A = min(A, B)
#define setmax(A, B) A = max(A, B)
#define graph unordered_map<int, unordered_set<int>>
#define vi vector<int>
#define pb push_back
#define ll long long

double dp[3000][3000];

int main() {
    int N;
    cin >> N;
    vector<double> coins(N);
    forl(i, N) cin >> coins[i];
    dp[0][1] = coins[0];
    dp[0][0] = 1-coins[0];
    for(int i = 1; i < N; i++) {
        forl(j, i+2) {
            if(j==0) dp[i][0] = dp[i-1][0]*(1-coins[i]);
            else dp[i][j] = dp[i-1][j]*(1-coins[i]) + dp[i-1][j-1]*(coins[i]);
        }
    }
    double ans = 0;
    for(int i = N/2+1; i <= N; i++) ans += dp[N-1][i];
    cout << setprecision(100) << ans << endl;
}

Measured memory_bytes: 68864
#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repr(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFL 1100000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<Pi> > vvPi;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi> > pqlP;
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
template <class T>
using vec = vector<T>;
template<class T>
using pql = priority_queue<T, vector<T>, greater<T>>;

string debug_show(Pi a) {
	return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
}

template<class T>
struct augEdge {
	T from, to;
	int cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
	bool operator>(Edge e) {
		return cost > e.cost;
	}
};

template<class T>
void augDijkstra(map<T,vector<augEdge<T>>> graph, T start, map<T, int> &cost) {
	//vi prev(V);
	pql<pair<int, T>> Q;

	cost[start] = 0;

	Q.push(mp(0, start));	// (cost, index)

	while (!Q.empty()) {
		T pos = Q.top().second;
		int d = Q.top().first;
		Q.pop();
		rep(i, graph[pos].size()) {
			T to = graph[pos][i].to;
			int c = graph[pos][i].cost;
			if (cost.find(to) == cost.end() || cost[to] > cost[pos] + c) {
				cost[to] = cost[pos] + c;
				Q.push(mp(cost[to], to));
				//prev[graph[pos][i].to] = pos;
			}
		}
	}
}

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

	int N, M;
	cin >> N >> M;

	map<Pi, vector<augEdge<Pi>>> G;


	rep(i, M) {
		int p, q, c;
		cin >> p >> q >> c;
		G[mp(p, 0)].push_back(augEdge<Pi>{ mp(p, 0), mp(p, c), 1 });
		G[mp(p, c)].push_back(augEdge<Pi>{ mp(p, c), mp(p, 0), 1 });
		G[mp(q, 0)].push_back(augEdge<Pi>{ mp(q, 0), mp(q, c), 1 });
		G[mp(q, c)].push_back(augEdge<Pi>{ mp(q, c), mp(q, 0), 1 });
		G[mp(p, c)].push_back(augEdge<Pi>{ mp(p, c), mp(q, c), 0 });
		G[mp(q, c)].push_back(augEdge<Pi>{ mp(q, c), mp(p, c), 0 });
	}

	map<Pi, int> cost;

	augDijkstra<Pi>(G, mp(1, 0), cost);

	if (cost.find(mp(N, 0)) == cost.end())cout << -1 << endl;
	else cout << (cost[mp(N, 0)] - 1) / 2 + 1 << endl;

	return 0;
}

Measured memory_bytes: 169140