akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
  char exp[100] = {}, *el;
  double stack[100] = {}, n;
  int i, j;
  while(fgets(exp, sizeof(exp), stdin) != NULL){
    i = 0;
    for(el = strtok(exp, " \n");el != NULL;el = strtok(NULL, " \n")){
      if(*el == '+') stack[i - 2] += stack[--i];
      else if(*el == '-' && strlen(el) == 1) stack[i - 2] -= stack[--i];
      else if(*el == '*') stack[i - 2] *= stack[--i];
      else if(*el == '/') stack[i - 2] /= stack[--i];
      else stack[i++] = atof(el);
    }
    printf("%.6f\n", stack[0]);
  }
  return 0;
}

Measured memory_bytes: 588
#include <bits/stdc++.h>
#define INF 9999999999999999
typedef long long ll;
using namespace std;
int n, m;
struct edge {
public:
	int to;
	ll cost;
	edge(ll c, int t) {
		to = t;
		cost = c;
	}
};
ll d[100000];
ll a[100000];
bool used[100000];
typedef pair<ll, int> P;
vector<vector<edge> > G;
vector<vector<edge> > G1;

void dijkstra(int s) {
	priority_queue<P, vector<P>, greater<P> >q;
	fill(d, d + n, INF);
	fill(used, used + n, false);
	d[s] = 0;
	q.push(P(0, s));
	while (!q.empty()) {
		P p = q.top();
		q.pop();
		int v = p.second;
		if (used[v]) {
			continue;
		}
		ll dist = p.first;
		used[v] = true;
		d[v] = dist;
		for (int i = 0; i < G[v].size(); i++) {
			edge e = G[v][i];
			if (d[e.to] <= dist + e.cost) {
				continue;
			}
			q.push(P(d[v] + e.cost, e.to));
		}
	}
}


int main() {
	int from, to;
	int s, t;
	ll cost, cost1;
	cin >> n >> m >> s >> t;
	s--;
	t--;
	G = vector<vector<edge> >(n);
	G1 = vector<vector<edge> >(n);
	for (int i = 0; i < m; i++) {
		cin >> from >> to >> cost >> cost1;
		from--;
		to--;
		G[from].push_back(edge(cost, to));
		G[to].push_back(edge(cost, from));
		G1[to].push_back(edge(cost1, from));
		G1[from].push_back(edge(cost1, to));
	}
	dijkstra(s);
	swap(d, a);
	for (int i = 0; i < n; i++) {
		swap(G[i], G1[i]);
	}
	fill(used, used + n, false);
	dijkstra(t);
	ll x[100000];
	for (int i = 0; i < n; i++) {
		x[i] = a[i] + d[i];
	}
	ll ans[100000];
	ans[n - 1] = x[n - 1];

	for (int i = n - 1; 0 < i; i--) {
		ans[i - 1] = min(x[i - 1], ans[i]);
	}
	for (int i = 0; i < n; i++) {
		cout << 1000000000000000 - ans[i] << endl;
	}
	return 0;
}



Measured memory_bytes: 20096
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <array>
#include <bitset>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

int main() {
    int n;
    string s;
    cin >> n >> s;
    int m = 9;
    VVI c(m, VI(m));
    REP(i,n-1){
        c[s[i]-'1'][s[i+1]-'1']++;
    }
    VI ans(m);
    int mi = 1e8;
    VI p(m);
    REP(i,m) p[i] = i;
    do{
        int tmp = 0;
        REP(i,m) REP(j,m){
            int d = abs(i / 3 - j / 3) + abs((i % 3) - (j % 3));
            tmp += d * c[p[i]][p[j]];
        }
        if (tmp < mi || (tmp == mi && p < ans)){
            mi = tmp;
            ans = p;
        }
    }while(next_permutation(ALL(p)));
    REP(i,m){
        cout << ans[i] + 1;
        if (i % 3 == 2) cout << endl;
    }
    return 0;
}


Measured memory_bytes: 3296
#include<stdio.h>

int main(void){
  int num = 1;
  int N = 0;
  int K = 0;
  
  scanf("%d", &N);
  scanf("%d", &K);

  for(int i = 0; i < N; i++){
    if(num * 2 < num + K){
      num *= 2;
    }
    else{
      num += K;
    }
  }
  printf("%d", num);
  return 0;
}


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

using namespace std;

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

    stack<char> st;
    for (char i : s) {
        if (st.empty()) {
            st.push(i);
            continue;
        }

        if ((st.top() == '0' and i == '1') or
            (st.top() == '1' and i == '0')) {
            st.pop();
        } else {
            st.push(i);
        }
    }

    cout << s.size() - st.size() << endl;
    return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define all(i,n) (i),(i+n)

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;

// --------------------------------------
int h, a;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> h >> a;

  if(h % a == 0) cout << h / a << endl;
  else cout << h / a + 1 << endl;
}


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

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string S, T;
  cin >> S >> T;

  vector<vector<int>> dp(S.size() + 1, vector<int>(T.size() + 1, 0));
  for (int i = 0; i < (int)S.size(); i++) {
    for (int j = 0; j < (int)T.size(); j++) {
      if (S[i] == T[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
      dp[i + 1][j + 1] = max({dp[i + 1][j + 1], dp[i + 1][j], dp[i][j + 1]});
    }
  }

  string lcs = "";
  int i = (int)S.size(), j = (int)T.size();
  while (i > 0 && j > 0) {
    if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else if (dp[i][j] == dp[i][j - 1]) {
      j--;
    } else {
      lcs += S[i - 1];
      i--, j--;
    }
  }
  reverse(lcs.begin(), lcs.end());

  cout << lcs << "\n";
  return 0;
}


Measured memory_bytes: 35584
#include <iostream>
#include <string>    
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n, a, b;

int main() {
	cin >> n;
	cin >> a >> b;
	ll na = 0;
	ll nb = 0;
	ll p[n];
	for(ll i = 0; i < n; i++){
		cin >> p[i];
		if(p[i] <= a){na++;}
		if(p[i] >= b+1){nb++;}
	}
	ll ans = (na < nb)? na : nb;
	ans = (ans < n-na-nb)? ans : n-na-nb;
	cout << ans;
	return 0;
}

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

using namespace std;

int main() {
    int h, w;
    cin >> h >> w;
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, -1, 0, 1};

    vector<string> s;
    for (int i = 0; i < h; i++) {
        /* code */
        string tmp;
        cin >> tmp;
        s.push_back(tmp);
    }

    for (int i = 0; i < h; i++) {
        /* code */
        // cout << s[i] << endl;
        for (int j = 0; j < w; j++) {
            /* code */
            if (s[i][j] == '#') {
                bool flag = false;
                for (int k = 0; k < 4; k++) {
                    /* code */
                    int x = i + dx[k];
                    int y = j + dy[k];
                    if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '#') {
                        flag = true;
                        break;
                    }
                }

                if (!flag) {
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
    }

    cout << "Yes" << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define fd(i, a, b) for (int i = (a); i >= (b); --i)
#define edge(i, u) for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define mod 1000000007
#define ll long long
#define N 405
#define pb push_back
#define int long long
int n, m, a, b, c, ans;
bool mp[N][N], vis[N][N];
const int xx[4] = {0, 1, 0, -1};
const int yy[4] = {1, 0, -1, 0};
inline void search (int x, int y)
{
    if (mp[x][y]) ++a; else ++b;
    vis[x][y] = 1;
    fo (l, 0, 3)
    {
        int nx = x + xx[l];
        int ny = y + yy[l];
        if (nx && ny && nx <= n && ny <= m && !vis[nx][ny] && (mp[nx][ny] ^ mp[x][y]))
            search(nx, ny);
    }
}
main ()
{
    scanf("%lld %lld", &n, &m);
    fo (i, 1, n)
        fo (j, 1, m)
        {
            char ch = getchar();
            while (ch != '.' && ch != '#') ch = getchar();
            mp[i][j] = (ch == '#');
        }
    fo (i, 1, n)
        fo (j, 1, m)
            if (!vis[i][j])
            {
                a = 0; b = 0;
                search(i, j);
                ans += a * b;
            }
    printf("%lld", ans);
}

Measured memory_bytes: 9344
#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
#include<limits>
#include<numeric>
#include<type_traits>
#include<math.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
#define in(x) cin >> x
#define out(str) cout << str << endl

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

int N;

int main(){
    cin>>N;
    float a[N],ave,sum=0,sa=10000000;
    rep(i,N){
        cin>>a[i];
        sum+=a[i];
    }
    ave=sum/N;
    int min=0;
    rep(i,N){
        double temp;
        if(ave-a[i]<0){
            temp=a[i]-ave;
        }
        else{
            temp=ave-a[i];
        }
        if(temp<sa){
            sa=temp;
            min=i;
        }
    }
    out(min);
}


Measured memory_bytes: 384
#負の数の個数が偶数個なら全部正にできる
#負の数の個数が奇数個なら一個だけ絶対値最小の数だけ負にしてほかは正にする

n=int(input())
lista = [int(i) for i in input().split()]

minabs = 10**9
sumabs = 0
minusnum = 0

for i in range(n):
    num = lista[i]
    if num < 0:
        minusnum += 1
    if abs(num)<minabs:
        minabs = abs(num)
    sumabs += abs(num)
    
if minusnum%2 == 0:
    print(sumabs)
else:
    print(sumabs - 2*minabs)    

Measured memory_bytes: 14284
#include <bits/stdc++.h>
#include<queue>
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef pair<double,double> Pdouble;

const int INF = 2000000000;

int main(){

  int n;
  cin >> n;
  vector<int> x(n);
  for(int i=0; i<n; i++){
    cin >> x.at(i);
  }

  int min=INF;

  for(int i=1; i<=100; i++){
    ll pmin = 0;
    for(int j=0; j<n; j++){
      pmin += (i-x.at(j))*(i-x.at(j));
    }
    if(pmin<=min){
      min = pmin;
    }
  }
  cout << min << endl;
}


Measured memory_bytes: 256
// abc164_a
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()

#define INF ((1 << 29)-1)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int i=0;i<(n-1);i++) cout << (vec)[i] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << endl;}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int S, W;
    cin >> S >> W;
    if (W >= S){
        print("unsafe");
    } else {
        print("safe");
    }


    return 0;
}


Measured memory_bytes: 3600
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    ll n;
    string s, t;
    cin >> n >> s >> t;

    REP(i, n) cout << s[i] << t[i];
    cout << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int dp[3005][3005];
string s1,s2;
int len1,len2;
void printlcs()
{
	int i=len1,j=len2;
	string ans;         
	while(i!=0&&j!=0)
	{
		if(s1[i-1]==s2[j-1])
		 {
		 	ans+=s1[i-1];
		 	 i--;
			 j--;
		 	
		 }
		else if(dp[i-1][j]<dp[i][j-1])
		  j--;
		else if(dp[i][j-1]<=dp[i-1][j])  
		  i--;
	}
	
	for(int i=ans.length()-1;i>=0;i--)
	 printf("%c",ans[i]);               
	 
	printf("\n");
	
 } 
int main()
{
    
       cin>>s1>>s2; 
    
        len1=s1.length();
        len2=s2.length();
       for(int i = 0 ; i <= len1 ; i++)    
            dp[i][0] = 0;
       for(int j = 0 ; j <= len2 ; j++)    
            dp[0][j] = 0;
       for(int i=1;i<=len1;i++)
       {
           for(int j=1;j<=len2;j++)
           {
               if(s1[i-1]==s2[j-1])
               {
                   dp[i][j]=dp[i-1][j-1]+1;
               }
               else
                dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
           }
       }
        
        
        printlcs();        
    
    
    
    return 0;
}

Measured memory_bytes: 38252
n,m = map(int,input().split())
ans = 0
import math
if n >= 2:
    ans += math.factorial(n) // (math.factorial(n - 2) * math.factorial(2))
if m >= 2:
    ans += math.factorial(m) // (math.factorial(m - 2) * math.factorial(2))
print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int main(){
string s,t;
  cin>>s>>t;
  sort(s.begin(),s.end());
  sort(t.begin(),t.end());
  reverse(t.begin(),t.end());
  if(s<t){
cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
  return 0;
}

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

#define SZ(x) (((int)x.size()))
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;

using namespace std;

const int maxn = 2019;
string s;
ll ans, cur, x, p = 1;
int t[maxn];

int main()
{
    cin >> s;
    for (int i = SZ(s) - 1; i >= 0; i--)
    {
        t[cur]++;
        x = s[i] - '0';
        cur += p * x;
        cur %= maxn;
        ans += t[cur];
        p *= 10;
        p %= maxn;
    }
    cout << ans;
	return 0;
}


Measured memory_bytes: 3760
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N; scanf("%d", &N);
    string s; cin >> s;

    ll ans = 0;
    const string pat = "RGB";
    vector< vector<int> > cols(3);
    for(int i=0; i<N; i++) {
        int c = pat.find(s[i]);
        cols[c].emplace_back(i);
    }
    
    for(int i=0; i<N; i++) {
        for(int j=i+1; j<N; j++) {
            if(s[i] == s[j]) continue;
            int c = pat.find(s[i] ^ s[j] ^ 'R' ^ 'G' ^ 'B');
            int ptr = upper_bound(cols[c].begin(), cols[c].end(), j) - cols[c].begin();
            int M = (int)cols[c].size() - ptr;
            int x = j - i;
            if(j + x < N and pat.find(s[j+x]) == c) M--;
            ans += M;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3720
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, a, b) for(int i = (a); i <= (b); i++)
#define PER(i, a, b) for(int i = (a); i >= (b); i--)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAX = 1e5+5;
int N, K, a[MAX];
ll pd[MAX];

ll solve() {
	REP(i, 2, N) {
		pd[i] = (1LL << 60);
		rep(j, max(1, i-K), i)
			pd[i] = min(pd[i], pd[j]+abs(a[i]-a[j]));
	}
	return pd[N];
}

int main(int argc, char ** argv) {
	scanf("%d%d", &N, &K);
	REP(i, 1, N) scanf("%d", &a[i]);
	printf("%I64d\n", solve());
	return 0;
}


Measured memory_bytes: 1408
from sys import stdin

input = stdin.readline

N = int(input())

txy = [[int(j) for j in input().split()] for i in range(N)]


def main():
    pt = 0
    px = 0
    py = 0

    flg = False

    for t, x, y in txy:
        dt = t - pt - abs(x - px) - abs(y - py)
        # 余った時間が奇数 戻れない
        if dt < 0 or dt % 2 == 1:
            break

        pt = t
        px = x
        py = y
    else:
        # break しなかった 完走
        flg = True

    print("Yes" if flg else "No")
    return


main()


Measured memory_bytes: 21080
/*Made by Shivam Solanki*/
#include<bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define ll long long int
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<pii> vp;
typedef vector<pll> vpll;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef set<int> sii;
typedef set<ll> sll;
typedef queue<int> qii;
typedef priority_queue<int> pq;
typedef unordered_map<int,int> umii;
typedef unordered_map<ll,ll> umll;
#define all(x) x.begin(),x.end()
#define rep(i,k,n) for (int i = k; i < n; ++i) 
#define repr(i,k,n) for (int i = n; i>=k; --i)
#define repll(i,k,n) for (ll i = k; i < n; ++i) 
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int INF = 1e9+5;
const int MOD = 1e9+7;


void solve(){
    int n;
    cin>>n;
    int ctr=0;
    queue<ll> q;
    rep(i,1,10){
        q.push(i);
    }
    while(1){
        ll x=q.front();
        q.pop();
        ctr++;
        if(ctr==n){
            cout<<x;
            return;
        }
        if(x%10!=0){
            q.push(10LL*x+x%10-1);
        }
        q.push(10LL*x+x%10);
        if(x%10!=9){
            q.push(10LL*x+x%10+1);
        }
    }
}

int main(){ 
    // #ifndef ONLINE_JUDGE  
        // freopen("input.txt", "r", stdin); 
        // freopen("output.txt", "w", stdout); 
    // #endif 
    fastio;
    int t=1;
    // cin>>t;
    while(t--)
        solve();
    return 0;
}


Measured memory_bytes: 1792
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;

char S[6060];
int pl[3030], pr[3030];
int mat[6060];
int N;

string Do2(int l, int r) {
	string res = "";
	for(int i=l;i<=r;) {
		if(S[i] == 'a') i = mat[i] + 1, res.pb('a'), res.pb('b');
		else ++i;
	}
	return res;
}

string Do(string X, string append) {
	int max_val = 0, n = szz(X), now = 0;
	vector <int> pv;
	rep(i, n) {
		if(X[i] == 'b') ++now;
		else --now;
		if(max_val < now) {
			max_val = now;
			pv.clear();
			pv.pb(i);
		}
		else if(max_val == now) pv.pb(i);
	}
	string res = append;
	for(int e : pv) {
		string tt = "";
		rep(j, max_val) tt.pb('b');
		for(int j=e+1;j<n;j++) tt.pb(X[j]);
		tt += append;
		if(res < tt) res = tt;
	}
	return res;
}

string get_ans(vector <string> &L) {
	int n = szz(L);
	string res = "";
	for(int i=n-1;i>=0;i--) {
		res = Do(L[i], res);
	}
	return res;
}

void solve(){
	scanf("%d%s", &N, S + 1);
	N *= 2;
	int ca = 0, cb = 0;
	for(int i=1;i<=N;i++) {
		if(S[i] == 'a') pl[++ca] = i;
		else pr[++cb] = i;
	}
	for(int i=1;i<=N/2;i++) mat[pl[i]] = pr[i], mat[pr[i]] = pl[i];
	int val[6060] = {};
	for(int i=1;i<=N;i++) val[i] = val[i-1] + (S[i] == 'b' ? 1 : -1);
	int md = 0, p = -1;
	string res = "";
	vector <string> V;
	string last = "";
	for(int i=1;i<=N;i++) {
		if(md == 0) md = val[i], p = i;
		if(i == N || (val[i] == 0 && !(md == -1 && val[i+1] == -1))) {
			string tt = "";
			for(int j=p;j<=i;j++) tt.pb(S[j]);
			if(md == 1) V.pb(tt);
			else {
				if(i == N) last = Do2(p, i);
			}
			md = 0;
		}
	}
	res = get_ans(V);
	res = res + last;
	printf("%s\n", res.c_str());
}

int main(){
	int Tc = 1; //scanf("%d", &Tc);
	for(int tc=1;tc<=Tc;tc++){
		solve();
	}
	return 0;
}

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

using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
typedef long long ll;
typedef pair<int, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = 10e10;
const ll MINF = -10e10;
//const int INF = INT_MAX;
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)

const int mod = 1000000007;

int gcd(ll x, ll y) {
    if (x < y) swap(x, y);
    if (y == 0) {
        return x;
    }
    return gcd(y, x % y);
}

int main() {
    int n;
    cin >> n;
    vector<ll> as(n);
    rep (i, n) {
        int a;
        cin >> a;
        as[i] = a;
    }


    for (int i = 0; i < n - 1; i++) {
        as[i + 1] = gcd(as[i], as[i + 1]);
    }


    cout << as[n - 1] << endl;


}



Measured memory_bytes: 1024
n = int(input())
s = input()#a,b = map(int,input().split())
#L = list()map(int,input().split())
cnt = 0
for i in range(len(s)-2):
    if s[i] == "A" and s[i+1] == "B" and s[i+2] == "C":
        cnt +=1
print(cnt)

Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip> 
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;


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

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

	if ((abs(c - a) <= d) || ((abs(c - b) <= d) && (abs(b - a) <= d))) cout << "Yes\n";
	else cout << "No\n";

	return 0;
}


Measured memory_bytes: 256
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()  {
    int N,a;
    int b = 0, c = 0;
    int A[100005];
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> A[i];
    sort(A,A+N);
    while(b < N){
        int d = A[b], f = 0;
        while(A[b] == d){
            b++;
            f++;
        }
        c += f % 2;
    }
    cout << c;
}

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





typedef long long ll;
const ll mod = 1e9+7;
const int maxn = 1e6 + 5;



int k, n;
int a[maxn];

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

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

    int gap = k-a[n-1]+a[0];
    for (int i=0; i<n-1; i++) {
	gap = max(gap, abs(a[i]-a[i+1]));
    }


    int ans = k - gap;
    cout<<ans<<endl;
  

    return 0;
}


Measured memory_bytes: 1024
#include <cstdio>

int main(){
    int W,H,x,y,r;
    scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
    if(r <= x && x <= W - r && r <= y && y <= H - r){
        printf("Yes");
    }else{
        printf("No");
    }
    printf("\n");

    return 0;
}


Measured memory_bytes: 2744
#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Record {
  int c; // 最後に録画したTVのチャンネル
  int t; // 最後に録画したTVの時刻t
};

struct TV {
  int c, s, t;
};

int main() {
  int n, C;
  cin >> n >> C;
  vector<TV> tvs;
  for (int i = 0; i < n; ++i) {
    int s, t, c;
    cin >> s >> t >> c;
    tvs.push_back((TV){c, s, t});
  }
  sort(tvs.begin(), tvs.end(), [&](auto &l, auto &r) {
    if (l.s == r.s) return l.t < r.t;
    return l.s < r.s;
  });
  vector<Record> records;
  records.push_back((Record){-1, -1});
  for (TV &tv: tvs) {
    sort(records.begin(), records.end(), [&](auto &l, auto &r) {
      int sl = l.t;
      int sr = r.t;
      if (l.c != tv.c) ++sl;
      if (r.c != tv.c) ++sr;
      if (sl == sr) { 
        if (r.c == tv.c) return false;
        return true;
      }
      return sl < sr;
    });
    bool need_additional_record = true;
    for (Record &r : records) { 
      if (tv.c == r.c) {
        if (r.t > tv.s) continue;
      }
      else {
        if (r.t >= tv.s) continue;
      }
      need_additional_record = false;
      r.t = tv.t;
      r.c = tv.c;
      break;
    }
    if (need_additional_record) {
      records.push_back((Record){tv.c, tv.t});
    }
    assert(records.size() <= C);
  }
  cout << records.size() << endl;
}



Measured memory_bytes: 1912
import numpy as np
from bisect import bisect_right as br
from bisect import bisect_left as bl
n,k=map(int,input().split())
a=np.sort(np.array(list((map(int,input().split())))))
q=len(a[a==0])
d=a[a<0]
e=a[a>0]
l,r=10**18,-10**18
while l-r>1:
    t=(l+r)//2
    p=0
    if t>=0:
        p+=q*n
    p+=np.searchsorted(a,t//e,side="right").sum()+(n-np.searchsorted(a,(-t-1)//(-d),side="right")).sum()
    p-=np.count_nonzero(a*a<=t)
    p//=2
    if p>=k:
        l=t
    else:
        r=t
print(l)

Measured memory_bytes: 32304
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<long double, long double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int MOD = 1000000007;
//const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[0].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){
    return (a <= x && x < b);
}

int main() {
    int n; cin >> n;
    string s, t; cin >> s >> t;
    string ans = "";
    rep(i,n){
        ans += s[i];
        ans += t[i];
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
def pattern1(A, B):
    s1 = (A // 3) * B
    s2 = ((A - (A // 3)) // 2) * B
    s3 = A * B - s1 - s2
    return max([abs(s1 - s2), abs(s2 - s3), abs(s3 - s1)])


def pattern2(A, B):
    a1 = A // 2
    a2 = A - a1
    b1 = (2 * B + 1) // 3
    b2 = B - b1
    s1, s2, s3 = a1 * b1, a2 * b1, A * b2
    return max([abs(s1 - s2), abs(s2 - s3), abs(s3 - s1)])


def main():
    H, W = list(map(int, input().split(' ')))
    ans = min([
        pattern1(H, W),
        pattern1(W, H),
        pattern2(H, W),
        pattern2(W, H)
    ])
    print(ans)


if __name__ == '__main__':
    main()


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

int count(int n, int x){
    int cnt = 0;
    for(int i = 1; i <= n-2; i++){
        for(int j = i+1; j <= n-1; j++){
            for(int k = j+1; k <= n; k++){
                if(i + j + k == x)cnt++;
            }
        }
    }
    return cnt;
}

int main() {
    int n, x;

    while(1) {
        cin >> n >> x;
        if(n == 0 && x == 0)break;
        cout << count(n, x) << endl;
    }

    return 0;
}


Measured memory_bytes: 3068
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k;
    cin >> n >> k;
    vector<int> h(n);
    long long sm=0;
    for(int i=0; i<n; i++){
        cin >> h[i];
        sm += h[i];
    }
    sort(h.begin(),h.end());
    long long mn=sm;
    for(int i=0; i<n-k+1;i++){
        mn = min(mn,(long long) h[i+k-1]-h[i]);
    }
    cout << mn << endl;


}




Measured memory_bytes: 3728
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define tc int t; cin>>t; while(t--)
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mod 1000000007
#define pb push_back
#define vi vector<int>
#define vc vector<char>
#define eb emplace_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fo(i,x,n) for(int i=x;i<n;i++)
#define csb(x) __builtin_popcount(x)
#define tz(x) __builtin_ctz(x)
#define lsbp(x) __builtin_ffs(x)
#define msbp(x) __lg(x)
#define btoi(str,i) stoi(str,0,i)
#define ff first
#define ss second
const int inf=0x3f3f3f3f3f3f3f3f;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// void zz(){
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
// }
const int mx=1e5+5;
vi v(102,0);
// int dp[101][mx];
int f(int n,int k){
   vector<vi>dp(n+2,vi(k+2,0));
   for(int i=0;i<=k;i++)if(i<=v[1])dp[1][i]=1;
   
   for(int i=2;i<=n;i++){
      for(int j=0;j<=k;j++){
      	if(j==0)
      	dp[i][j]=dp[i-1][j];
      	else{
         dp[i][j]=(mod+dp[i][j-1]+dp[i-1][j]-(j-v[i]-1>=0?dp[i-1][j-v[i]-1]:0))%mod;
         //dp[i][j]=(dp[i][j]+mod)%mod;
      	}
      }
   }
   return dp[n][k];
}
int32_t main(){
// zz();
int n,k;cin>>n>>k;
for(int i=1;i<=n;i++)cin>>v[i];
int ans=f(n,k);
cout<<ans<<endl;

}



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

using namespace std;
const int MAXN = 312345;

int n, d[MAXN];
vector < pair <int, int> > g[MAXN];

void bfs (int s = 0) {
	
	for (int i = 0; i < MAXN; ++i) d[i] = -1;
	d[s] = 0;
	
	queue <int> q;
	q.push(s);

	while (!q.empty()) {

		int u = q.front();
		q.pop();

		for (int i = 0; i < (int)g[u].size(); ++i) {
			int v = g[u][i].first;
			int w = g[u][i].second;

			if (d[v] == -1) {
				d[v] = d[u] + w;
				d[v] %= 2;
				q.push(v);
			}
		}
	}

	for (int i = 0; i < n; ++i) cout << (d[i] % 2) << '\n';



}


int main () {

	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;

	for (int i = 1; i < n; ++i) {

		int u, v, w;
		cin >> u >> v >> w;

		--u; --v;
		w = w % 2;

		g[u].push_back ({v, w});
		g[v].push_back ({u, w});

	}

	bfs ();






	return 0;
}


Measured memory_bytes: 12672
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Map = map<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF=1LL<<60;
ll MOD=1000000007;



int main(){
    ll N;
    cin >> N;
    ll M;
    cin >> M;
    ll Q;
    cin >> Q;
    vector<ll> a(Q,0);
    vector<ll> b(Q,0);
    vector<ll> c(Q,0);
    vector<ll> d(Q,0);
    priority_queue<P> pq;
    for(ll i=0;i<Q;i++){
        cin>>a[i]>>b[i]>>c[i]>>d[i];
        a[i]--;b[i]--;
    ;}
    ll ans=0;
    vector<ll> vec(N+M-1,1);
    for(ll i=0;i<M-1;i++){
        vec[i]=0;
    ;}
    
    do{      
        vector<ll> A(0);
        ll k=0;
        for(ll p=0;p<N+M-1;p++){
            if(vec[p]==0)k++;
            else{
                A.emplace_back(k+1);
            }
        ;}
        ll f=0;
        for(ll i=0;i<Q;i++){
            if(A.at(b[i])-A.at(a[i])==c[i]){f+=d[i];}
        ;}
        ans=max(ans,f);
    }while(next_permutation(vec.begin(),vec.end()));



    cout<<ans<<endl;


    
    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

int main(){
	ll n;cin>>n;
	ll ans=0;
	for(ll a=1;a*a<=n;a++){
		if(n%a==0){
			ll m=n/a-1;
			if(m<=a)break;
			if(m>=1)ans+=m;
		}
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <sstream>
#include <string>
#include <map>
#include <set>
using namespace std;

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


int main() {
	int n;
	cin >> n;
	vector<int> vec(n);
	rep(i, n) cin >> vec.at(i);
	sort(vec.begin(), vec.end(), greater<int>());
	int res = 0;
	for (int i=1; i<n; i++) res += vec.at(i);
	res += vec.at(0)/2;
	cout << res << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
    int n;
    string s,t;
    cin>>n>>s>>t;
    rep(i,n){
        int cnt=0;
        rep(j,n-i) if(s[j+i]==t[j]) cnt++;
        if(cnt==n-i){
            cout<<2*n-cnt;
            return 0;
        }
    }
    cout<<2*n;
}

Measured memory_bytes: 3628
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int N = scan.nextInt();
        if ( N<1 || N>100) {
            System.exit(-1);
        }
        int sum = 0;
        List<Integer> nums = new ArrayList<Integer>();
        for(int i=0; i<N; i++) {
            int num = scan.nextInt();
            if ( num < -100 || num > 100) {
                System.exit(-2);
            }
            nums.add(new Integer(num));
            sum += num;
        }
        int avg = Math.round((float)sum/N);
        int cost = 0;
        for (Integer num: nums) {
            cost += Math.pow(num.intValue() - avg, 2);
        }

        System.out.println(cost);
    }
}

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

#define PI acos(-1.0)
#define nl '\n'

typedef long long int ll;
typedef unsigned long long int  llu;

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

    double x,ans;
    cin>>x;
    ans=2*PI*x;
    cout<<fixed<<setprecision(20)<<ans<<nl;
    return 0;
}


Measured memory_bytes: 3816
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
bool comp(pair<ll,ll>a,pair<ll,ll>b)
{
    if(a.first<=b.first)
        return true;
    else
        return false;
}
ll dp[101][100001];
ll fun(pair<ll,ll>p[],ll ind,ll w,ll n)
{
   // cout<<w<<" "<<ind<<" "<<p[ind].first<<endl;

    if(ind>=n||w==0)
        return 0;
    else if(dp[ind][w]!=0)
            return dp[ind][w];
    else if(p[ind].first>w)
        return fun(p,ind+1,w,n);
    else
    {
        ll x,y;
        x = p[ind].second + fun(p,ind+1,w - p[ind].first,n);
        y = fun(p,ind+1,w,n);
        dp[ind][w] = max(x,y);
        return dp[ind][w];
    }
}
int main()
{
    ll n,w;
    cin>>n>>w;
    pair<ll,ll>p[n];
    for(ll i=0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        p[i].first = x;
        p[i].second = y;
    }

    ll ans = fun(p,0,w,n);
    cout<<ans<<endl;
}


Measured memory_bytes: 76288
#include <bits/stdc++.h>
// Hey! 僕の提出を見てくれてありがとう.
// ロボ子さん可愛いぞ！！！！！君もV沼にはまろう↓
// https://www.youtube.com/channel/UCDqI2jOz0weumE8s7paEk6g
using namespace std;
/*
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace mp = boost::multiprecision;// 任意長整数型
using bint = mp::cpp_int;// 仮数部長が32の浮動小数点数型
using real32 = mp::number<mp::cpp_dec_float<32>>;// 仮数部長が1024の浮動小数点数型
using real1024 = mp::number<mp::cpp_dec_float<1024>>;// ついでに有理数型
using rat = boost::rational<bint>;
*/
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<型>()
string x;
int main(){ //srand((unsigned)time(NULL));
  while(1){
    ll ans = 0;
    cin >> x;
    if(x == "0")break;
    for (int i = 0; i < x.length(); i++) {
      ans += x[i] - '0';
    }
    cout << ans << endl;
  }
}



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

int main()
{
  string s1, s2, s3, ans;
  cin >> s1 >> s2 >> s3;
  cout << (char)toupper(s1.at(0));
  cout << (char)toupper(s2.at(0));
  cout << (char)toupper(s3.at(0)) << endl;
}


Measured memory_bytes: 3584
if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
function main()
  V=zeros(Int,3010,3010)
  dp=zeros(Int,3010,3010,4)
  R,C,K=parse.(Int,split(readline()))
  for i=1:K
    r,c,v=parse.(Int,split(readline()))
    V[r,c]=v
  end
  for i=1:R
    for j=1:C
      for k=1:4
        #sita
        @inbounds dp[i+1,j+1,1]=max(dp[i+1,j+1,1],dp[i,j+1,k])
        @inbounds dp[i+1,j+1,2]=max(dp[i+1,j+1,2],dp[i,j+1,k]+V[i,j])
        #migi
        @inbounds dp[i+1,j+1,k]=max(dp[i+1,j+1,k],dp[i+1,j,k],k>1 ? dp[i+1,j,k-1]+V[i,j] : 0)
      end
    end
  end
  println(maximum(dp[R+1,C+1,:]))
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Measured memory_bytes: 577220
//2020-05-27

#include <iostream>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int a,b,c,k;
    
    cin >> a >> b >> c >> k;
    
    int sum = 0;
    
    if (k<=a){
        sum += k;
    }
    else {
        sum += a;
        k -= a;
        if (k<=b){
            
        }
        else{
            k -= b;
            if (k<c){
                sum -= k;
            }
            else
                sum -= c;
        }
    }
    
    cout << sum;
    return 0;
}


Measured memory_bytes: 3148
n, k = map(int, input().split())
if n % k == 0:
    print(0)
else:
    l = [n]
    p = n // k
    c = n - (p * k)
    half = k // 2
    while c > half:
        c = abs(c - k)
    l.append(c)
    print(min(l))


Measured memory_bytes: 3060
#include<stdio.h>

int F(int);

int main(void){
  int n;

  scanf("%d",&n);
  if(n<0 || n>44)printf("error\n");

  F(n);

  return 0;
}

int F(n){
  int a,b,i,f;
  if(n==0 || n==1){
    printf("1\n");
    return ;
  }
  else{
    a=b=1;
    for(i=2;i<=n;i++){
      f=a+b;
      a=b;
      b=f;
    }
  }
  printf("%d\n",f);
  return ;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define ote(x) cout<<(x)<<endl
#define all(x) (x).begin(),(x).end()
#define rp(i,s,e) for(int i=(s);i<(e);++i)
#define rpz(i,e) rp(i,0,(e))
#define irp(i,s,e) for(int i=(s);i>(e);--i)
#define irpz(i,s) irp(i,(s),-1)
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<class T> T in(){T x;cin>>x;return x;}
// long long a = in<long long>();
template<class T> vector<T> inarr(int s,int e, T init=0){
	vector<T> v;T x;
	for(int i=0;i<s;++i)v.push_back(init);
	for(int i=s;i<e;++i){x=in<T>();v.push_back(x);}
	return v;
}
// vi P = inarr<int>(1,N+1,INT_MAX);
// vi P = inarr<int>(0,N);

int N;
vi A;

string solve(){
	bool ans = true;
	rp(i,0,N){
		if(A[i]%2){//odd
		}
		else{//even
			if(A[i]%3==0 || A[i]%5==0){
			}
			else{
				ans=false;
				break;
			}
		}
	}
	return ans?"APPROVED":"DENIED";
}

int main(){
	cin>>N;
	A = inarr<int>(0,N);
	cout<< solve() <<endl;
	// printf("%f\n",solve());
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FF first.first
#define FS first.second
typedef pair<int,int>P;
typedef pair<P,int>PP;
char map1[1001][1001];
char mapcpy[1001][1001];
int main(void)
{
	int h,w,n;
	int i,j,cnt,sx,sy;
	int xx,yy,cost;
	int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
	queue<PP> que;
	
	cin>>h>>w>>n;
	
	for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cin>>map1[i][j];
			if(map1[i][j]=='S'){
				sx=j;
				sy=i;
			}
		}	
	}
	for(i=0;i<h;i++){
		strcpy(mapcpy[i],map1[i]);
		//cout<<mapcpy[i]<<endl;
	}
	
	cost=0;cnt=1;
	que.push(PP(P(sy,sx),cost));
	while(!que.empty()){
		PP q=que.front();
		que.pop();
		
		//cout<<"yy="<<q.FF<<" "<<"xx="<<q.FS<<"cost="<<cost<<endl;
		if(map1[q.FF][q.FS]!='X'){
			if(map1[q.FF][q.FS]>='0'&&map1[q.FF][q.FS]<='9'){
				//cout<<"IN"<<endl;
				//cout<<endl;
				if(map1[q.FF][q.FS]-'0'==cnt){
					cnt++;
					for(i=0;i<h;i++){
						strcpy(map1[i],mapcpy[i]);
					}
					while(!que.empty()){
						que.pop();
					}
				}
			}




			/*for(i=0;i<h;i++){
				for(j=0;j<w;j++){
					cout<<map1[i][j];
				}	
				cout<<endl;
			}*/

			for(i=0;i<4;i++){
				yy=q.FF+dy[i];
				xx=q.FS+dx[i];
				cost=q.second;
				if(xx>=0&&yy>=0&&xx<w&&yy<h&&map1[yy][xx]!='X'){
					
					que.push(PP(P(yy,xx),cost+1));
				}	
			}
		}
		map1[q.FF][q.FS]='X';
		if(cnt>n) break;
	}
	/*for(i=0;i<h;i++){
		for(j=0;j<w;j++){
			cout<<map1[i][j];
		}	
		cout<<endl;
	}*/
	cout<<cost<<endl;
	
	
	return 0;
}


Measured memory_bytes: 5068
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class AtCoderA
{
    static void Main()
    {
        var sc = new Scanner();

        int k = sc.NextInt();
        string s = sc.Next();

        string answer = "";
        if (s.Length > k)
        {
            answer = s.Substring(0, k) + "...";
        }
        else
        {
            answer = s;
        }

        Console.WriteLine(answer);
    }

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

Measured memory_bytes: 27492
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
int n;
int a[maxn], f[maxn][2];
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d", &a[i]);
		f[i][0] = f[i - 1][1];
		f[i][1] = min(f[i - 1][0], f[i - 1][1]) + 1;
		if(a[i] != a[i - 1])
			f[i][0] = min(f[i][0], f[i - 1][0]);
	}
	printf("%d\n", min(f[n][0], f[n][1]));
	return 0;
}
		

Measured memory_bytes: 3752
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)

Measured memory_bytes: 30456
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
LL lucas[87];
char op[3];
int main() {
	string S;
	cin >> S;
	int A[4];
	for (int i = 0; i < 4; i++) A[i] = S[i] - '0';
	for (int bit = 0; bit < (1 << 3); bit++) {
		int sum = A[0];
		for (int i = 0; i < 3; i++) {
			if (bit&(1 << i)) sum -= A[i + 1], op[i] = '-';
			else sum += A[i + 1], op[i] = '+';
		}
		if (sum == 7) {
			printf("%d%c%d%c%d%c%d=7\n", A[0], op[0], A[1], op[1], A[2], op[2], A[3]);
			return 0;
		}
	}
}

Measured memory_bytes: 256
using System;

namespace A___Hitachi_String
{
    class Program
    {
        static void Main (string[] args)
        {
            var s = Console.ReadLine ();
            if (s.Length % 2 == 1)
            {
                Console.WriteLine ("No");
                return;
            }
            for (var i = 1; i < s.Length; i+=2)
            {
                if (s[i - 1] != 'h' || s[i] != 'i')
                {
                    Console.WriteLine ("No");
                    return;
                }
            }
            Console.WriteLine ("Yes");
        }
    }
}

Measured memory_bytes: 13140
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+7;
int n,l,t,k,cnt,w[N];
int main()
{
    scanf("%d%d%d",&n,&l,&t);
    for(int i=1;i<=n;i++) {scanf("%d%d",&w[i],&k),w[i]+=(k==1)?t:-t,cnt+=w[i]/l; if(w[i]%l<0) cnt--; ((w[i]%=l)+=l)%=l;}
    sort(w+1,w+n+1),((cnt%=n)+=n)%=n; for(int i=cnt+1;i<=n;i++) printf("%d\n",w[i]);
    for(int i=1;i<=cnt;i++) printf("%d\n",w[i]);
    return 0;
}

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

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = int(1e3), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
num ans;
pair<int, int> fm[msz + 5];

bool inline upd(int &a, int b) {
	if (a > b) { a = b; return 1; }
	return 0;
}

void inline bfs(int sx, int sy) {
	static int qx[msz + 5], qy[msz + 5];
	int ql = 0, qr = 0;
	qx[qr++] = sx, qy[qr++] = sy;
	for (; ql != qr;) {
		int x = qx[ql++], y = qy[ql++];
		loop (dr, 4) {
			int cx = x + dx[dr], cy = y + dy[dr];
			if (cx < 1 || cy < 1 || cx > n || cy > n || !upd(d[cx][cy], d[x][y] + !vis[x][y]))  continue;
			qx[qr++] = cx, qy[qr++] = cy;
		}
	}
}

int main() {
	scanf("%d", &n);
	m = n * n;
	int s = 0;
	cont (i, n) cont (j, n) {
		fm[++s] = make_pair(i, j);
		d[i][j] = n;
		upd(d[i][j], i - 1);
		upd(d[i][j], j - 1);
		upd(d[i][j], n - i);
		upd(d[i][j], n - j);
	}
	cont (i, m) {
		int id, x, y;
		scanf("%d", &id);
		x = fm[id].first, y = fm[id].second;
		ans += d[x][y];
		vis[x][y] = 1;
		bfs(x, y);
	}
	printf("%lld\n", ans);
}

Measured memory_bytes: 12004