akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define ll long long
#define LL long long
#define PB push_back
#define INF 2000000000
#define X first
#define Y second
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int MAX = 600005;

int main()
{
	//freopen("input.txt" , "r" , stdin);
	LL K , A , B;
	LL ans = 1;
	cin >> K >> A >> B;
	if(A >= B)
	{
		cout << K + 1 << endl;
		return 0;
	}
	LL ans1 = K + 1;
	if(A - 1 <= K)
	{
		K -= A - 1;
		ans = A;
	}
	else
	{
		cout << K + 1;
		return 0;
	}

	LL ans2 = A + (B - A) * (K / 2);
	K &= 1;
	if(K)
		ans2 ++;
	cout << max(ans1 , ans2);
	
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
            using namespace std;
            #define rep(i,b) for(i=0;i<b;++i)
            #define repp(i,a,b) for(i=a;i<b;++i)
            #define endl "\n"
            //#define LLONG_MAX 99999999999999
            typedef long long int lli;
            int main(){
                ios_base::sync_with_stdio(false);
                cin.tie(NULL);
                lli t,n,i,j,b,q,m;
                string s1,s2;
                cin>>s1>>s2>>n>>m;
                string s;
                cin>>s;
                if(s==s1){
                    cout<<n-1<<" "<<m<<endl;
                }
                else{
                    cout<<n<<" "<<m-1<<endl;
                }

                return 0;
            }



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

int main(){
	int i=0;
	double m[10000];
	
	while( scanf(" %lf", &m[i]) != EOF ) ++i;
	
	sort(m, m+i);
	printf("%lf\n", m[i-1]-m[0]);

	return 0;
}

Measured memory_bytes: 1256
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

A, B, C = map(int, input().split())
K = int(input())

ans = 'No'
for _ in range(K):
    if A >= B:
        B = B * 2
    else:
        if B >= C:
            C = C * 2
    if A < B and B < C:
        ans = 'Yes'
        break
print(ans)

Measured memory_bytes: 9124
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll=long long;
static const ll mod = 1e9 + 7;
static const ll INF = 1LL << 50;
using namespace std;

int main(){
  ll n,t;
  cin>>n>>t;
  vector<ll>a(n);
  rep(i,n)cin>>a[i];
  ll minv=INF,maxv=0;
  map<ll,ll>m;
  rep(i,n){
    minv=min(a[i],minv);
    maxv=max(maxv,a[i]-minv);
    if(a[i]-minv==maxv)m[maxv]++;
  }
  cout<<m[maxv]<<endl;
}

Measured memory_bytes: 7296
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <random>

using namespace std;

#define int long long

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

    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for (auto &t : a) {
        cin >> t;
    }
    sort(a.begin(), a.end());
    vector<int> suffsum(n + 1);
    reverse(a.begin(), a.end());
    for (int i = 0; i < n; i++) {
        suffsum[i + 1] = suffsum[i] + a[i];
    }
    reverse(a.begin(), a.end());
    auto can = [&](int x) {
        int cnt = 0;
        int sum = 0;
        for (int i = 0; i < n; i++)  {
            int dval = x - a[i];
            int cur = a.end() - lower_bound(a.begin(), a.end(), dval); 
            cnt += cur;
            sum += a[i] * cur + suffsum[cur];
        }
        return make_pair(cnt, sum);
    };
    int l = 0;
    int r = 2 * a.back() + 1;
    while (r - l > 1) {
        int mm = (r + l) >> 1;
        if (can(mm).first > m) {
            l = mm;
        } else {
            r = mm;
        }
    }
    int cnt = can(r).first;
    int sum = can(r).second;
    cout << sum + (m - cnt) * (r - 1) << endl;
}   


Measured memory_bytes: 1792
N, A, B = map(int, input().split())
h = [int(input()) for i in range(N)]
Tmax=2*max(h)//B
def Tenabled(Tin):
    x = [h[i] for i in range(N)]
    for i in range(N):
        x[i] -= B*Tin
    S = 0
    for i in range(N):
        if x[i]>0:
            S += -((-x[i])//(A-B))
    if S<=Tin:
        return True
    else:
        return False


high=Tmax
low=0
t=(high+low)//2
while(low<=high):
    if Tenabled(t):
        high=t
    else:
        low=t+1
    t=(high+low)//2
    if Tenabled(t)==True and Tenabled(t-1)==False:
        break
print(t)


Measured memory_bytes: 119000
#include <iostream>
#include <unordered_map>

using namespace std;

int main()
{
    unordered_map<int, int> AMap;

    int N;    
    cin >> N;

    long long ans = 0;

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

    int Q;
    cin >> Q;
    for( int i = 0; i < Q; ++i )
    {
        int B, C;
        cin >> B >> C;

        int bval = AMap[B];
        AMap[B] -= bval;
        AMap[C] += bval;

        ans += bval * ( C - B );

        cout << ans << endl;
    }

    return 0;
}

Measured memory_bytes: 6960
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <sstream>
#include <cmath>
#include <set>
#include <map>
using namespace std; 

#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define len(x) ((int)(x).size())
#define tmax(a,b,c) max((a),max((b),(c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef vector<int> V;
typedef vector<vector<int> > VV;
typedef long long ll;
const int inf = 2e9;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int N, L;
int A[101010];
V ans;

int main()
{
	cin >> N >> L;
	rep(i,0,N) cin >> A[i];

	int l = 0, r = 0;
	rep(i,1,N) {
		if (A[i]+A[i-1] >= L) {
			l = i-1;
			r = i;
			break;
		}
	}
	if (!l && !r) {
		cout << "Impossible" << endl;
		return 0;
	}

	rep(i,0,l) ans.pb(i+1);
	for (int i=N-1; i>r; i--) ans.pb(i);
	cout << "Possible" << endl;
	for (auto it:ans) cout << it << endl;
	cout << l+1 << endl;

	return 0;
}

Measured memory_bytes: 1784
#include <bits/stdc++.h>
using namespace std;
int main(){
    string s;
    cin>>s;
    int y=(s[5]-'0')*10+s[6]-'0';
    if(y<=4){
        cout<<"Heisei"<<endl;            
    }
  	else{
        cout<<"TBD"<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const int sz = 1e3 + 10, ma = 1e18;
;
int a[sz], b[sz], c[sz], dp[sz][sz][10];
///全探索
void solve1()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a[i] >> b[i] >> c[i];
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            for (int q = 0; q < 8; q++)
            {
                dp[i][j][q] = -ma;
            }
        }
    }
    for (int i = 0; i < 8; i++)
        dp[0][0][i] = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            for (int q = 0; q < 8; q++)
            {
                dp[i + 1][j][q] = max(dp[i + 1][j][q], dp[i][j][q]);
                int here = dp[i][j][q];
                if (q & 1)
                    here += a[i + 1];
                else
                    here -= a[i + 1];
                if (q & 2)
                    here += b[i + 1];
                else
                    here -= b[i + 1];
                if (q & 4)
                    here += c[i + 1];
                else
                    here -= c[i + 1];
                dp[i + 1][j + 1][q] = max(dp[i + 1][j + 1][q], here);
            }
        }
    }
    int res = 0;
    for (int i = 0; i < 8; i++)
    {
        res = max(res, dp[n][m][i]);
    }
    cout << res << endl;
}
void solve2()
{
    int n, m;
    cin >> n >> m;
    int val[1000];
    rep(i, n) cin >> a[i] >> b[i] >> c[i];
    int ans = -ma;
    for (int mask = 0; mask < 8; mask++)
    {
        int A = (mask & 1) ? -1 : 1;
        int B = (mask & 2) ? -1 : 1;
        int C = (mask & 4) ? -1 : 1;
        rep(i, n) val[i] = A * a[i] + B * b[i] + C * c[i];
        sort(val, val + n, greater<int>());
        int cur = 0;
        rep(i, m) cur += val[i];
        ans = max(ans, cur);
    }
    cout << ans << endl;
}
main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve2();
    return 0;
}

Measured memory_bytes: 256
N = 2019
cnt = [0] * N
S = input()
ans = 0
for s in S:
    s = int(s)
    new = [0] * N
    for n in range(N):
        idx = (10 * n + s) % N
        new[idx] += cnt[n]
    cnt = new
    cnt[s] += 1
    ans += cnt[0]
print (ans)

Measured memory_bytes: 76428
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;

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

    int N, K;
    cin >> N >> K;
    vector<pii> td(N);
    for (int i = 0; i < N; ++i) {
        cin >> td[i].first >> td[i].second;
        td[i].first--;
    }

    auto cmp = [&](pii a, pii b) { return a.second == b.second ? a.first < b.first : a.second > b.second; };
    sort(td.begin(), td.end(), cmp);

    vector<bool> isused(N);
    priority_queue<int, vector<int>, greater<int>> maque;
    priority_queue<int> que;
    ll pt = 0;
    for (int i = 0; i < N; ++i) {
        if (!isused[td[i].first] && maque.size() < K) {
            maque.push(td[i].second);
            pt += td[i].second;
            isused[td[i].first] = true;
        } else
            que.push(td[i].second);
    }

    int cnt = maque.size();
    while (cnt < K) {
        pt += que.top();
        que.pop();
        cnt++;
    }

    cnt = maque.size();
    ll ans = pt + (ll)cnt * cnt;
    for (int i = cnt - 1; i > 0; --i) {
        if (maque.top() < que.top()) {
            pt += que.top();
            que.pop();
            pt -= maque.top();
            que.push(maque.top());
            maque.pop();
        }
        ans = max(ans, pt + (ll)i * i);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1856
#include<iostream>
#include<set>
#include <bitset>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include <cstdio>
#include<algorithm>
#include <sstream>
#include<string>
#include<string.h>
#include <cmath>
#include <iomanip>
#include <string>
#include<list>
#include <limits>
#include <numeric>
#include <type_traits>
#define int long long
#define ll long long
#define mod  1000000007
#define inf 1e17
#define rep(i,j,n) for(int i=j;i<n;i++)
#define P pair<int,int>
double pi = 3.141592653589793;
using namespace std;
//ここから始めよう
signed main(){
    int n;cin>>n;
    cout<<50<<endl;
    int a[55]={};
    for(int i=49;i>=0;i--){
        a[i]=49-i;
    }
    rep(i,0,n%50){
        a[i]=50;
    }
    rep(i,0,50){
        a[i]+=n/50;
        cout<<a[i]<<" ";
    }cout<<endl;
    return 0;
}


Measured memory_bytes: 256
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
s=SI()
def nex(i):
    if i==n-1:
        i=-1
    if lis[i]==1:
        if s[i]=="o":
            if lis[i-1]==1:
                lis[i+1]=1
            else:
                lis[i+1]=0
        else:
            if lis[i-1]==1:
                lis[i+1]=0
            else:
                lis[i+1]=1
    else:
        if s[i]=="o":
            if lis[i-1]==1:
                lis[i+1]=0
            else:
                lis[i+1]=1
        else:
            if lis[i-1]==0:
                lis[i+1]=0
            else:
                lis[i+1]=1

for i in product([0,1],repeat=2):
    lis=[0 for i in range(n)]
    lis[0],lis[1]=i[0],i[1]
    for i in range(1,n-1):
        nex(i)
    #print(lis)
    x=lis[0]
    y=lis[1]
    nex(n-1)
    nex(0)
    if x==lis[0] and y==lis[1]:
        for i in range(n):
            if lis[i]==1:
                print("S",end="")
            else:
                print("W",end="")
        sys.exit()
print(-1)
                
    
    

            
        

    

        
        
        
            
        

                

Measured memory_bytes: 10960
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iostream>
#include <utility>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) 
 
using namespace std;
using ll = long long;
using ld = long double;
using Vl = vector<ll>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vs = vector<string>;

#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, m, n) for(int i = m; i < (n); i++)

ll N,M;
int main() {
    cin>>N>>M;
    ll c=0;
    priority_queue<long long> A;
    REP(i,N) {
        ll a;
        cin>>a;
        A.push(a);
    }
    REP(i,M) {
        ll b;
        b=A.top();
        A.pop();
        A.push(b/2);
    }
    REP(i,N) {
        c+=A.top();
        A.pop();
    }
    cout<<c<<endl;
    return 0;
}

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
S = input()

if N % 2 == 0:
    a = S[:N//2]
    b = S[N//2:]
    if a == b:
        Yes()
        exit()
No()


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

int dfs(vector<int> &dp, vector<vector<int>> &g, int vertex){
	if(dp[vertex] != -1) return dp[vertex];
	int res = 0;
	for(int v : g[vertex]){
		res = max(res, 1 + dfs(dp, g, v));
	}
	return dp[vertex] = res;
}

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL);
	int n, m;
	cin >> n >> m;
	vector<int> dp(n, -1);
	vector<vector<int> >g(n, vector<int>(0));
	for(int i = 0; i <m; ++i){
		int a, b;
		cin >> a >> b;
		g[a-1].push_back(b-1);
	}
	int max_dist = 0;
	for(int i = 0; i < n; ++i){
		max_dist = max(max_dist, dfs(dp, g, i));
	}
	cout << max_dist;
	return 0;
}

Measured memory_bytes: 8832
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

struct Card {
    char suit, value;
};
// 冒泡排序法
void bubble(struct Card A[], int n) {
    // 标记是否需要继续冒泡
    int flag = 1;
    for (int i = 0; flag; i++) {
        flag = 0;
        for (int j = n - 1; j >= i + 1; j--) {
            // 交换两个相邻的元素
            if (A[j].value < A[j - 1].value) {
                swap(A[j], A[j - 1]);
                flag = 1;
            }
        }
    }
}
// 选择排序法
void selection(struct Card A[], int n) {
    for (int i = 0; i < n; i++) {
        int minj = i;
        for (int j = i; j < n; j++) {
            if (A[j].value < A[minj].value)
                minj = j;
        }
        if (i != minj)
            swap(A[i], A[minj]);
    }
}
// 冒泡排序法是稳定排序，选择排序后的花色和其比较则可得知排序是否稳定的
bool isStable(struct Card C1[], struct Card C2[], int n) {
    for (int i = 0; i < n; i++) {
        if (C1[i].suit != C2[i].suit)
            return false;
    }
    return true;
}
// 打印输出
void print(struct Card C[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%c%c%c", C[i].suit, C[i].value, i == n - 1 ? '\n' : ' ');
    }
}
int main() {
    Card C1[100], C2[100];
    int n;

    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i++) {
        scanf("%c%c", &C1[i].suit, &C1[i].value);
        getchar();
        C2[i] = C1[i];
    }
    // 对于C1数列进行冒泡排序，对C2数列进行选择排序，然后比较两者排序后的花色
    // 则可得知选择排序是否稳定
    bubble(C1, n);
    selection(C2, n);

    print(C1, n);
    printf("Stable\n");
    print(C2, n);
    if (isStable(C1, C2, n))
        printf("Stable\n");
    else
        printf("Not stable\n");

    return 0;
}


Measured memory_bytes: 3220
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <stack>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>

#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
//typedef tree<int, nuint_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; 
#define int long long
#define MOD 1000000007
#define what_is(x) cerr << #x << " is " << x << endl;
#define loop(i,a,n) for(int i=a;i<n;i++)

#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, szof(x))
#define sortall(x) sort(aint(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef pair<int, int> pt;

signed main() {
	
	//fastio();
	//#define LOCAL_DEFINE
	#ifdef LOCAL_DEFINE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	//https://codeforces.com/contest/1084/problem/D
	// https://codeforces.com/contest/1155/problem/E
	//https://www.spoj.com/problems/MON2012/
	//https://codeforces.com/blog/entry/52726	
	
	int r,g,b,n;
	cin>>r>>g>>b>>n;
	int ans=0;
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			int res=r*i+g*j;
			res=(n-res);

			if(res>=0)
			{
				if(res%b==0)
					ans++;
			}
		}
	}
	cout<<ans;
	return 0;



}


Measured memory_bytes: 256
N, Y = gets.split.map(&:to_i)

(0..N).each do |i|
    (0..N).each do |j|
        break if i + j > N

        if 10000 * i + 5000 * j + 1000 * (N-i-j) == Y
            puts "#{i} #{j} #{N-i-j}"
            exit
        end
    end
end

puts "-1 -1 -1"


Measured memory_bytes: 1788
#include<cstdio>
using namespace std;
const long long MOD = 1000000007;

int main() {
    int n, now = 0, k = 0;
    long long ans = 1;
    scanf("%d", &n);
    for(int i = 0 ; i < n ; i++) {
        int t;
        scanf("%d", &t);
        now++;
        ans = (ans * now) % MOD;
        if(t < 2 * i + 1 - 2 * k) now--, k++;
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>

using namespace std;

int a[100005], dp[100005], n, k;
const int inf = 1e9 + 5;

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 1 ; i <= n ; ++i){
	cin >> a[i];
}
for(int i = 2 ; i <= n ; ++i){
	dp[i] = inf;
	for(int j = max(1, i - k) ; j < i ; ++j){
		dp[i] = min(dp[i], dp[j] + abs(a[i] - a[j]));
	}
}
cout << dp[n] << endl;

return 0;
}



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

int main(int argc, char const *argv[])
{
	int a, b;
	cin >> a >> b;
	if(a <= 0 and 0 <= b) cout << "Zero" << endl;
	else if (0 < a) cout << "Positive" << endl;
	else {
		if((b-a+1) % 2) cout << "Negative" << endl;
		else cout << "Positive" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
using System.Text;
using System;

public class hello
{
    public static string ns;
    public static int[] nv;

    public static void Main()
    {
        ns = "mcxi";
        nv = new int[] { 1000, 100, 10, 1 };

        var n = int.Parse(Console.ReadLine().Trim());
        var z = 0;
        var sb = new StringBuilder();
        while (z++ < n)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            var s = getNum(line[0]);
            var t = getNum(line[1]);
            var ans = getAns(s + t);
            sb.Append(string.Format("{0}\n", ans));
        }
        Console.Write(sb);
    }
    static string getAns(int n)
    {
        var ans = "";
        for (int i = 0; i < 4; i++)
        {
            var a = n / nv[i];
            if (a == 1) ans += ns[i];
            if (a > 1) ans += a.ToString() + ns[i].ToString();
            n -= a * nv[i];
        }
        return ans;
    }
    static int getNum(string s)
    {
        var ans = 0;
        for (int i = 0; i < 4; i++)
        {
            var p = s.IndexOf(ns[i]);
            if (p == 0) ans += nv[i];
            else if (p > 0)
            {
                int prep;
                if (!int.TryParse(s[p - 1].ToString(), out prep)) prep = 1;
                ans += prep * nv[i];
            }
        }
        return ans;
    }
}



Measured memory_bytes: 19996
#include<bits/stdc++.h>
using namespace std;
int hai[100];
int top = 0;
int push(int x){
	top++;
	hai[top] = x;
}
int main(){
	string  x;
	while(cin>>x){
		if(x == "+"){
			hai[top-1] += hai[top];
			top--;
		}else if(x == "-"){
			hai[top-1] -= hai[top];
			top--;
		}else if(x == "/"){
			hai[top-1] /= hai[top];
			top--;
		}else if(x == "*"){
			hai[top-1] *= hai[top];
			top--;
		}else{
			push(stoi(x));
		}
		// for(int i = 0;i<top;i++){
		// 	cout<<hai[i]<<" ";
		// }
		// cout<<endl;
	}
	cout<<hai[top]<<endl;
	
}



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

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

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif

    int A, B, C;
    cin >> A >> B >> C;

    int mx = max({A, B, C});
    int r = mx * 3 - (A + B + C);

    if (r & 1) {
        r += 1;
        cout << r / 2 + 1 << endl;
    }
    else {
        cout << r / 2 << endl;
    }

    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
querys = [list(map(int, input().split())) for _ in range(Q)]

num_dic = {}
for i in range(N):
    if A[i] in num_dic:
        num_dic[A[i]] += 1
    else:
        num_dic[A[i]] = 1

tmp = sum(A)
for query in querys:
    if query[0] in num_dic:
        tmp -= num_dic[query[0]] * query[0]
        tmp += num_dic[query[0]] * query[1]

        if query[1] in num_dic:
            num_dic[query[1]] += num_dic[query[0]]
        else:
            num_dic[query[1]] = num_dic[query[0]]
        num_dic[query[0]] = 0

    print(tmp)
    

Measured memory_bytes: 35960
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(input())
    money = [1]
    for i in range(10):
        money.append(money[-1]*6)
    money.append(9)
    for i in range(9):
        money.append(money[-1]*9)
    
    dp = [INF] * (1 + N)
    dp[0] = 0
    for m in money:
        for j in range(m,N + 1):
            dp[j] = min(dp[j],dp[j - m] + 1)
    ans = dp[-1]
    print(ans)
if __name__ == '__main__':
    main()

Measured memory_bytes: 12676
#include<bits/stdc++.h>
using namespace std;
 
#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define pfis(a) printf("%d ",a);
#define pfls(a) printf("%lld ",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define f(i,a,b) for(int i=a;i<b;i++)
#define pb(a) push_back(a);
#define mp(a,b) make_pair(a,b)
#define ll long long
 
const ll mod=1e9+7;
 
int main()
{
    int n;
    sfi(n)
 
    ll arr[n+1];
    f(i,1,n+1)
    	sfl(arr[i])
 
    ll dp[n+1][n+1];
 
    memset(dp,0,sizeof(dp));
 
    for(int i=n;i>0;i--)
    {
    	f(j,i,n+1)
    	{
    		if(i==j)
    			dp[i][j]=arr[i];
    		else
    		{
    			dp[i][j]=max((arr[i]-dp[i+1][j]),(arr[j]-dp[i][j-1]));
    		}
    	}
    }
 
    pfln(dp[1][n])
 
    return 0;
}

Measured memory_bytes: 74108
#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>

int p[]={
  11, 31, 41, 61, 71, 101, 131, 151, 181, 191, 211, 241, 251, 271, 281, 311, 
  331, 401, 421, 431, 461, 491, 521, 541, 571, 601, 631, 641, 661, 691, 701, 
  751, 761, 811, 821, 881, 911, 941, 971, 991, 1021, 1031, 1051, 1061, 1091, 
  1151, 1171, 1181, 1201, 1231, 1291, 1301, 1321, 1361, 1381, 1451, 1471, 
  1481, 1511, 1531,
};

int main() {
  int N;
  scanf("%d", &N);  
  for (int i=0; i<N; ++i)
    printf("%d%c", p[i], i+1<N? ' ':'\n');
}


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

int main(){
    int n, k;
    cin >> n >> k;
    
    int A[n], ma = -1e9;
    for(int i = 0; i < n; i++){
        cin >> A[i];
        ma = max(ma, A[i]);
    }

    sort(A, A + n);

    int c = A[0];
    for(int i = 1; i < n; i++){
        int a, b;
        a = A[i];
        b = c;
        while(1){
            if(a % b){
                int tmp = a;
                a = b;
                b = tmp % b;
            }else break;
        }
        c = b;
    }
    
    if(k <= ma and !(k % c)) cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;

    return 0;
}

Measured memory_bytes: 640
N=int(input())
ans=0
Aend=0
Bstart=0
AB=0
for _ in range(N):
  s=input()
  ans+=s.count('AB')
  if s[-1]=='A' and s[0]=='B':
    AB+=1
  elif s[-1]=='A':
    Aend+=1
  elif s[0]=='B':
    Bstart+=1
if Aend==0 and Bstart==0 and AB>0:
  print(ans+AB-1)
  exit()
print(ans+min(Aend+AB,Bstart+AB))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n,k,c,rui;
string s;

vector<int> choose(string x){
    vector<int>res;
    int now = -1-c;
    for(int i=0;i<n;i++){
        if(x[i]=='o'&&i>now+c){
            res.push_back(i);
            rui++;
            now=i;
        }
    }
    return res;
}

int main(){
    cin >> n >> k >> c;
    cin >> s;
    rui=0;
    vector<int>l=choose(s);
    if(rui>k){
        cout << endl;
        return 0;
    }
    string rs=s;
    reverse(rs.begin(),rs.end());
    rui=0;
    vector<int>r=choose(rs);
    rep(i,k){
        r[i]=n-1-r[i];
    }
    sort(r.begin(),r.end());
    rep(i,k){
        if(l[i]==r[i])cout<<l[i]+1<<endl;
    }
    return 0;
}

Measured memory_bytes: 3764
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long,long long>
#define all(a) a.begin(),a.end()
#define INF 1000000000000000000
using namespace std;
int num[55]={1},pat[50]={1};
int cnt_patty(int n, int x){
    if(x==1){
        if(n==0) return 1;
        else return 0;
    }
    if(x<=num[n-1]+1) return cnt_patty(n-1, x-1);
    if(x==num[n-1]+2) return cnt_patty(n-1, x-1)+1;
    else return cnt_patty(n-1, x-num[n-1]-2)+pat[n-1]+1;
}
signed main(){
    int n,x; cin>>n>>x;
    for(int i=1; i<=n; i++){
        num[i]=num[i-1]*2+3;
        pat[i]=pat[i-1]*2+1;
    }
    cout<<cnt_patty(n, x)<<endl;
}

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

int n, h;
#define maxn 100000
class sword {
public:
	int a;
	int b;
	bool operator<(sword& as) {
		return b < as.b;
	}

}s[maxn+5];

int maxa;
int main() {
	maxa = 0;
	int dmg=0;
	int att=0;
	cin>>n>>h;
	for (int i = 0; i < n; ++i) {
		cin>>s[i].a>>s[i].b;
		
	}
	sort(s, s + n);
	for (int i = 0; i < n; ++i) {
		maxa = s[maxa].a < s[i].a ? i : maxa;
	}
	//大于maxa.a的全扔出去
	for (int snum = n - 1; dmg < h&&s[snum].b> s[maxa].a;--snum) {
		if (snum == maxa)continue;
		dmg += s[snum].b; ++att;
	}
	if (dmg < h) {
		if (s[maxa].a > s[maxa].b) {
			att = att + (h - dmg) / s[maxa].a;
			if ((h - dmg) % s[maxa].a != 0) {
				++att;
			}
		}
		else {
			att += 1; dmg += s[maxa].b;//最后一下是扔出去的
			if (dmg < h) {
				att += (h - dmg) / s[maxa].a;
				if ((h - dmg) % s[maxa].a != 0) {
					++att;
				}
			}
		}
	}
	cout << att;
}



Measured memory_bytes: 1024
import Foundation
import CoreFoundation

#if swift(>=4.0)
#else
extension String {
    func split(separator separator: Character) -> [String] {
        return self.characters.split(separator).map{String($0)}
    }
    var count: Int {
        return self.characters.count
    }
    func uppercased() -> String {
        return self.uppercaseString
    }
    func lowercased() -> String {
        return self.lowercaseString
    }
}
extension Array where Element: Comparable {
    func max() -> Element? {
        return self.maxElement()
    }
    func min() -> Element? {
        return self.minElement()
    }
    func sorted() -> Array {
        return self.sort()
    }
    mutating func sort() {
        self.sortInPlace()
    }
}
extension Array {
    mutating func swapAt(i: Int, _ j: Int) {
        swap(&self[i], &self[j])
    }
    func joined(separator separator: String = " ") -> String {
        return self.map{String($0)}.joinWithSeparator(separator)
    }
    func reversed() -> Array {
        return self.reverse()
    }
    mutating func reverse() {
        self = self.reversed()
    }
    func enumerated() -> EnumerateSequence<[Element]> {
        return self.enumerate()
    }
    init(repeating: Element, count: Int) {
        self.init(count: count, repeatedValue: repeating)
    }
}
func stride<T: Strideable>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T> {
    return start.stride(to: end, by: stride)
}
#endif
func readInt() -> Int? {
    return Int(readLine()!)
}
func readStringSplit(by separator: Character = " ") -> [String] {
    return readLine()!.split(separator: separator)
}
func readIntSplit(by separator: Character = " ") -> [Int] {
    return readLine()!.split(separator: separator).map{Int($0)!}
}
prefix operator * { }
prefix func * <T> (a: [T]) -> (T, T)          { return (a[0], a[1]) }
prefix func * <T> (a: [T]) -> (T, T, T)       { return (a[0], a[1], a[2]) }
prefix func * <T> (a: [T]) -> (T, T, T, T)    { return (a[0], a[1], a[2], a[3]) }
prefix func * <T> (a: [T]) -> (T, T, T, T, T) { return (a[0], a[1], a[2], a[3], a[4]) }
infix operator ** { associativity right precedence 155 }    // 右結合，乗除算より強くビットシフトより弱い
func **(lhs: Int, rhs: Int) -> Int {
    return (0..<rhs).reduce(1){i,_ in i*lhs}
}
func **(lhs: Int, rhs: Double) -> Double {
    return pow(Double(lhs), rhs)
}
//func gcd(x: Int, _ y: Int) -> Int {
//    return y>0 ? gcd(y, x%y) : x
//}
//func lcm(x: Int, _ y: Int) -> Int {
//    return x * y / gcd(x, y)
//}
let MOD = 10**9+7

/*************** テストスペース ***************/

//arr.sort{$0.0==$1.0 ? $0.1<$1.1 : $0.0<$1.0}  // タプル配列のソート

/*************** メインプログラム ***************/

let (A, B, C) = *readIntSplit()
print(max(C-(A-B), 0))


Measured memory_bytes: 7396
#include "bits/stdc++.h"

using namespace std;
using ll = long long;

int direct(int x, int y) {
    if (x == y) return 0;
    if (x > y) return 1;
    return 2;
}

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

    vector<ll> A(N);
    for (int i = 0; i < N; ++i) cin >> A[i];

    int ans = 1, i = 0;
    while (i < N - 1) {
        if (A[i] == A[i + 1]) {
            ++i;
            continue;
        }
        int group = direct(A[i], A[i + 1]);
        int j = i;
        for (; j < N - 1; ++j) {
            if (A[j] == A[j + 1]) continue;
            if (group != direct(A[j], A[j + 1])) {
                ans += 1;
                break;
            }
        }
        i = j + 1;
    }
    cout << ans << endl;
}


Measured memory_bytes: 1024
#!/usr/bin/env python

def comb(n, x):
    if x > n*3-3 or x < 6:
        return []

    c = []
    for i in range(1, n-1):
        if i > x/3:
            break
        for j in range(i+1, n):
            if j > (x-i)/2:
                break
            rem = x-(i+j)
            if rem > j and rem <= n:
                c += [(i, j, rem)]
    return c

if __name__=='__main__':
    while True:
        n, x = map(int, raw_input().split())

        if [n, x] == [0, 0]:
            break
        print len(comb(n, x))



Measured memory_bytes: 4692
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <tuple>
using namespace std;

#define rep(i,a) for(int i=0; i<a; i++)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define rep1(i,a) for(int i=1; i<=a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a,b) cout << a << " " << b << endl;
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl;
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a,n) memset( a, n, sizeof(a))
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N, ans, a[200009], b[200009];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>N;
    rep(i,N) cin>>a[i];
    rep(i,N) cin>>b[i];
    
    for(ll v=(1<<28); v; v>>=1){
        
        rep(i,N) a[i]&=(v*2-1);
        rep(i,N) b[i]&=(v*2-1);
        
        sort(a,a+N); sort(b,b+N);
        int t=0;
        
        for(int i=N-1,j=0,k=0,l=0;i>=0;i--){
            while(j<N&&a[i]+b[j]<v) j++;
            while(k<N&&a[i]+b[k]<2*v) k++;
            while(l<N&&a[i]+b[l]<3*v) l++;
            t+=k-j+N-l;
            t&=1;
        }
        if(t) ans|=v;
    }
    cout1(ans);
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	ll n;
	cin>>n;
	ll N=n-1;
	ll cnt=0;
	for(ll i=1;i*i<=N;i++)
	{
		if(i*i==N)cnt++;
		else if(N%i==0)cnt+=2;
	}
	for(ll i=2;i*i<=n;i++)
	{
	if(n%i==0)
	{
	ll m=n;
	while(i<=m)
      if(m%i==0)m/=i;
      else m%=i;
    if(m==1)
      cnt++;}
   }
	cout<<cnt<<endl;
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define rep(X,N) for(ll X = 0; X < (N); X++)
#define PI (acos(-1.0))
#define pback push_back
#define mpair make_pair
#define MODN 1000000007
#define ALL(V) (V).begin(),(V).end()

using namespace std;


int main(){

    string s,t;
    cin >> s;
    cin >> t;

    sort(ALL(s));
    sort(ALL(t));
    reverse(ALL(t));

    cout << ((s < t) ? "Yes" : "No") << endl;

    return 0;
}


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

pub fn readn<T: std::str::FromStr>(delimiter: &str) -> Vec<T> {
    let s = read1::<String>();
    s.split(delimiter).map(|e| e.parse().ok().unwrap()).collect::<Vec<T>>()
}

fn dfs(i: usize, g: usize, edge: &Vec<Vec<usize>>, group: &mut Vec<usize>) -> bool {
    let g_inv = g ^ 1;
    group[i] = g_inv;

    for &j in edge[i].iter() {
        if group[j] == 2 {
            if !dfs(j, g_inv, edge, group) {
                return false;
            }
        } else if group[j] == g_inv { return false; }
    }

    true
}

fn main() {
    let nm = readn::<usize>(" ");
    let n = nm[0];
    let m = nm[1];

    let mut edge = vec![Vec::new(); n];
    for _ in 0..m {
        let ab = readn::<usize>(" ");
        edge[ab[0] - 1].push(ab[1] - 1);
        edge[ab[1] - 1].push(ab[0] - 1);
    }

    let mut group = vec![2; n];
    let mut flg = true;
    for i in 0..n {
        if group[i] == 2 {
            if !dfs(i, 0, &edge, &mut group) {
                flg = false;
                break;
            }
        }
    }

    let ans;
    if flg {
        let g1 = group.iter().sum::<usize>();
        let g2 = n - g1;
        ans = g1 * g2 - m;
    } else {
        ans = n * (n - 1) / 2 - m;
    }

    println!("{}", ans);
}

Measured memory_bytes: 12924
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <iomanip>
#include <bitset>
#include <set>
#include <map>
#include <stdio.h>
#include <numeric>

#define rep(i,n) for (long long int i = 0; i < (n); i++)
#define rep1(i,n) for (long long int i = 1; i < (n); i++)
#define FOR(i,a,b) for (int i=(a); i < (b); i++)
#define INF 100000000000
#define MOD 1000000007 //10^9+7
using namespace std;
using ll = long long;
using P = pair<int, int>;

// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0) res += m;
    return res;
}

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

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

int main()
{
    string s;
    cin >> s;

    if (s.length() == 1) {
        cout << 0 << endl;
        return 0;
    } else if (s.length() == 2) {
        if(s[0] == s[1]) 
            cout << 1 << endl;
        else
            cout << 0 << endl;
        return 0;
    }
    string sW ="", sB = "";
    rep(i,s.length()) {
        if(i % 2 == 0) {
            sW += '0';
            sB += '1';
        } else {
            sW += '1';
            sB += '0';
        }
    }
    int diffW = 0, diffB = 0;
    rep(i,s.length()) {
        if(s[i] != sW[i])
            diffW++;
        else
            diffB++;
    }
    
    cout << min(diffW, diffB) << endl;
    return 0;
}


Measured memory_bytes: 768
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
#define int long long int
typedef complex<long double> cd;
const long double pi=acos(-1);
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <db, db> pdd;
typedef vector<int> vi;
typedef vector<vector<int> > matrix;

#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,v) memset(x,v,sizeof(x))

#define fi first
#define se second
#define endl "\n"
#define debug(x) (cerr << #x << ": " << x << "\n")
#define setbits(x) __builtin_popcount(x)
#define setbitsll(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define pii pair<int,int>

inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0)return 1;ll p=power(a,n/2,m);p=(p%m*p%m)%m;if(n%2) return (p%m*a%m)%m;else return p;}
const ll MOD =998244353;
const ll hell=1000000007;
const int INF = 1e17;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
const int N=100005;

void solve()
{
    string s;
    cin>>s;
    int n=s.length();
    int c[11];
    c[0]=0;c[1]=1;c[2]=2;c[3]=3;c[4]=4;c[5]=5;c[6]=5;c[7]=4;c[8]=3;c[9]=2;c[10]=1;
    int ans=0;
    for(int i=n-1;i>=0;i--)
    {
        if(s[i]<'5')
            ans+=c[s[i]-'0'];
        else if(s[i]>'5')
        {
            ans+=c[10-(s[i]-'0')];
            if(i)
            s[i-1]++;
            else
                ans++;
        }
        else
        {
            if(i>0&&s[i-1]>='5')
            {
                ans+=5;
                s[i-1]+=1;
            }
            else
                ans+=5;
        }
    }
    cout<<ans<<endl;
}
int32_t main()
{
    IOS;
    
    int t;
    t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


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

#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define MOD 1000000007

using namespace std;

typedef long long ll;

int main()
{
    string S, T;
    cin >> S;
    cin >> T;
    int size = S.size();
    bool isTrans = true;
    set<char> transCharSet;
    for (char c = 'a'; c <= 'z'; ++c)
    {
        bool exist = false;
        char transChar;
        for (int i = 0; i < size; ++i)
        {
            if (S[i] == c)
            {
                if (!exist)
                {
                    exist = true;
                    transChar = T[i];
                }
                else
                {
                    if (T[i] != transChar)
                    {
                        isTrans = false;
                        break;
                    }
                }
            }
        }
        if (exist && isTrans)
        {
            if (transCharSet.find(transChar) != transCharSet.end())
            {
                isTrans = false;
                break;
            }
            transCharSet.insert(transChar);
        }
    }

    string ans;
    if (isTrans) ans = "Yes";
    else ans = "No";

    cout << ans << endl;
}


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

namespace ABC128_D
{
    class Program
    {
        static void Main(string[] args)
        {
            var NK = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var N = NK[0];
            var K = NK[1];

            var V = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var revV = V.Reverse();

            var max = Math.Min(V.Length,K);

            long ans = 0;
            for (int left = 0; left <= max; left++)
            {
                for (int right = 0; right <= max; right++)
                {
                    if (left + right > max) break;

                    var lis = V.Take(left).ToList();
                    lis.AddRange(revV.Take(right));
                    lis.Sort();

                    var rest = K - left - right;
                    while (rest > 0 && lis.Any() && lis[0] < 0)
                    {
                        lis.RemoveAt(0);
                        rest--;
                    }

                    ans = Math.Max(ans, lis.Sum());
                }
            }

            Console.WriteLine(ans);
        }
    }
}


Measured memory_bytes: 15572
H, W, M = map(int, input().split())
 
hs = [0] * H
ws = [0] * W
boms = set()
 
for i in range(M):
    r,c = map(int, input().split())
    r -= 1
    c -= 1
 
    hs[r] += 1
    ws[c] += 1
    boms.add((r, c))
mh, mw = 0, 0
for i in range(H):
    mh = max(mh, hs[i])
for j in range(W):
    mw = max(mw, ws[j])
max_h_list = []
max_w_list = []
    
for i in range(H):
    if hs[i] == mh: max_h_list.append(i)    
for j in range(W):
    if ws[j] == mw: max_w_list.append(j)
ans = mh + mw
 
for i in max_h_list:
    for j in max_w_list:
        if (i,j) not in boms:
            print(ans)
            exit()
print(ans-1) 

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

namespace abc156_c
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = CIn.ReadInt();
            var X = CIn.ReadIntArray(N);
            var min = int.MaxValue;
            
            for(var P = 0; P <= 100; P++)
            {
                var a = 0;
                for(var i = 0; i <= N - 1; i++)
                {
                    
                    a =a + (int)Math.Pow(X[i] - P, 2);
                   
                }
                min = Math.Min(min, a);
            }
            Console.WriteLine(min);
        }
    }
    class CIn
    {
        private static readonly Queue<string> fieldBuffer = new Queue<string>();

        public static string Read()
        {
            if (fieldBuffer.Count == 0)
            {
                foreach (var field in Console.ReadLine().Split())
                {
                    fieldBuffer.Enqueue(field);
                }
            }

            return fieldBuffer.Dequeue();
        }

        public static int ReadInt() => int.Parse(Read());
        public static long ReadLong() => long.Parse(Read());
        public static double ReadDouble() => double.Parse(Read());

        public static string[] ReadStringArray(long n)
        {
            var array = new string[n];

            for (var i = 0; i < n; i++)
            {
                array[i] = Read();
            }

            return array;
        }

        public static int[] ReadIntArray(long n)
        {
            var array = new int[n];

            for (var i = 0; i < n; i++)
            {
                array[i] = ReadInt();
            }

            return array;
        }

        public static long[] ReadLongArray(long n)
        {
            var array = new long[n];

            for (var i = 0; i < n; i++)
            {
                array[i] = ReadLong();
            }

            return array;
        }
        public static double[] ReadDoubleArray(long n)
        {
            var array = new double[n];

            for (var i = 0; i < n; i++)
            {
                array[i] = ReadDouble();
            }

            return array;
        }
    }

}


Measured memory_bytes: 11220