akhauriyash/Code-Regression huggingface dataset:
from collections import Counter
N = int(input())
S = [input() for i in range(N)]
S = [s[0] for s in S if s[0] in 'MARCH']
counter = Counter(S)
if len(counter) < 3:
  print(0)
else:
  cnt = 0
  mar = 'MARCH'
  for a in range(0,3):
    if mar[a] not in counter:
      continue
    for b in range(a+1, min(a+1+3, 5-1)):
      if mar[b] not in counter:
        continue
      for c in range(b+1, min(b+1+3, 5)):
        if mar[c] not in counter:
          continue
        # print(mar[a], mar[b], mar[c])
        cnt += counter[mar[a]] * counter[mar[b]] * counter[mar[c]]
  print(cnt)

Measured memory_bytes: 10868
#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 int gcd(int a,int b){if(a==0) return b;return gcd(b%a,a);}
inline int 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 double EPS = 1e-9;
const ll MOD=998244353;
const ll hell=1000000007;
const int INF = 1e18;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
std::mt19937 rng(seed);

double startTime;
double getCurrentTime() {
    return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, 1, 0, -1 };

const int N=5005;





void solve()
{
    
    string s;
    cin>>s;
    int n=s.length();
    int cnt=0,ans=0;
    for(int i=0;i<n;i++)
        if(s[i]=='?')
            s[i]='D';
    for(int i=0;i<n;i++)
        if(s[i]=='D')
            ans++;
    for(int i=0;i<n-1;i++)
        if(s[i]=='P'&&s[i+1]=='D')
            ans++;
    cout<<s<<endl;
    
   
}



int32_t main()
{
    IOS;
    int t;
    t=1;
    //cin>>t;
    while(t--)
    {
        
        solve();
    }
    return 0;
}


Measured memory_bytes: 3792
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    int a = 0;
    int l, r, d;
    cin >> l >> r >> d;
    for (int i = l; i <= r; i++) {
        if (i % d == 0) a++;
    }
    cout << a << endl;
}

Measured memory_bytes: 3636
#include<iostream>
using namespace std;
int main()
{
  int a, b, c, d;
  cin >> a >> b >> c >> d;
    // 整数の入力
  string s;
  if (abs(b - a) <= d && abs(c - b) <= d) {
    s = "Yes";
  } else if(abs(c - a) <= d) {
   s = "Yes"; 
  } else {
   s = "No" ;
  }
   cout << s << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
#include<numeric>
#define LL long long
using namespace std;
 
int main()
{
    LL X,K=0;
    cin >> X;
    K = (360 / gcd(360,X));
    cout << K <<'\n'; 
   return 0;
}
 


Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
struct Node
{
    int src_pos;
    int v;
    bool is_red;
    bool operator<(const Node&o)const{
        return v < o.v;
    }
};
int count_inv(const vector<Node>&v)
{
    int ans = 0;
    for(int i=0;i<v.size();++i)
    {
        for(int j=i+1;j<v.size();++j)
        {
            if(v[j].src_pos < v[i].src_pos)
                ans++;
        }
    }
    return ans;
}
bool cmp(const Node& a,const Node&b)
{
    return a.src_pos < b.src_pos;
}
bool check(vector<Node>&v,int beg,int end)
{
    queue<Node> q0,q1;
    sort(v.begin()+beg,v.begin()+end,cmp);
    for(int i=beg;i<end;++i)
    {
        if(v[i].is_red^(abs(v[i].src_pos-beg)&1))
            q0.push(v[i]);
        else
            q1.push(v[i]);
    }
    if(q0.size()!=end-beg+1>>1)
        return false;
    bool cnt = 0;
    for(int i=beg;i<end;++i)
    {
        if(!cnt){v[i]=q0.front();q0.pop();}
        else{v[i]=q1.front();q1.pop();}
        cnt = !cnt;
    }
    return true;
}
void solve(int N, std::vector<int> A, std::vector<int> B){
    int lim = (1<<N);
    int ans = INT_MAX;
    for(int i=0;i<lim;++i)
    {
        vector<Node> v;
        for(int j=0;j<N;++j)
            if(i>>j&1)
                v.push_back({j,A[j],true});
            else
                v.push_back({j,B[j],false});
        sort(v.begin(),v.end());
        int p = 0;
        bool flag = true;
        while(p<N)
        {
            int e = p+1;
            while(e<N && v[e].v==v[p].v)e++;
            if(!check(v,p,e)){
                flag = false;
                break;
            }

            p = e;
        }
        if(flag)
            ans = min(ans,count_inv(v));
    }
    if(ans!=INT_MAX)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}

int main(){
    int N;
    cin>>N;
    vector<int> A(N);
    for(int i = 0 ; i < N ; i++){
        cin>>A[i];
    }
    std::vector<int> B(N);
    for(int i = 0 ; i < N ; i++){
        cin>>B[i];
    }
    solve(N, std::move(A), std::move(B));
    return 0;
}


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

using namespace std;

int main() {

    string S;
    cin >> S;

    int A = S[0] - '0'; 
    int B = S[1] - '0'; 
    int C = S[2] - '0'; 
    int D = S[3] - '0'; 

    if(A + B + C + D == 7) {
        cout << A << "+" << B << "+" << C << "+" << D << "=" << 7 << endl;
        return 0;
    }

    if(A + B + C - D == 7) {
        cout << A << "+" << B << "+" << C << "-" << D << "=" << 7 << endl;
        return 0;
    }

    if(A + B - C + D == 7) {
        cout << A << "+" << B << "-" << C << "+" << D << "=" << 7 << endl;
        return 0;
    }    

    if(A + B - C - D == 7) {
        cout << A << "+" << B << "-" << C << "-" << D << "=" << 7 << endl;
       return 0;
    }

    if(A - B + C + D == 7) {
        cout << A << "-" << B << "+" << C << "+" << D << "=" << 7 << endl;
        return 0;
    }

    if(A - B + C - D == 7) {
        cout << A << "-" << B << "+" << C << "-" << D << "=" << 7 << endl;
       return 0;
    }

    if(A - B - C + D == 7) {
        cout << A << "-" << B << "-" << C << "+" << D << "=" << 7 << endl;
        return 0;
    }

    if(A - B - C - D == 7) {
        cout << A << "-" << B << "-" << C << "-" << D << "=" << 7 << endl;
        return 0;
    }    
}

Measured memory_bytes: 256
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

s = input()

n = []
for i in s:
    n.append(int(i))

for i in range(2):
    for j in range(2):
        for k in range(2):
            if i % 2 == 1:
                n1 = n[0] + n[1]
                o1 = '+'
            else:
                n1 = n[0] - n[1]
                o1 = '-'
            if j % 2 == 1:
                n2 = n1 + n[2]
                o2 = '+'
            else:
                n2 = n1 - n[2]
                o2 = '-'
            if k % 2 == 1:
                n3 = n2 + n[3]
                o3 = '+'
            else:
                n3 = n2 - n[3]
                o3 = '-'
            if n3 == 7:
                print(n[0], o1, n[1], o2, n[2], o3, n[3], '=7', sep='')
                exit()


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

int main()
{
	int r,c;
	cin >> r >> c;
	int hyou[r + 1][c + 1];
	for(int i = 0;i < r;i++){
		for(int j = 0;j < c;j++){
			cin >> hyou[i][j];
		}
	}
	
	for(int i = 0;i < r + 1;i++)
		hyou[i][c] = 0;
		
	for(int j = 0;j < c + 1;j++)
		hyou[r][j] = 0;	
	
	for(int i = 0;i < r;i++){
		for(int j = 0;j < c;j++){
			hyou[i][c] += hyou[i][j];
		}
	}
	
	for(int i = 0;i < c + 1;i++){
		for(int j = 0;j < r;j++){
			hyou[r][i] += hyou[j][i];
		}
	}
	
	for(int i = 0;i < r + 1;i ++){
		for(int j = 0;j < c + 1;j++){
			cout << hyou[i][j];
			if(j != c) cout << " ";
			else cout << endl;
		}
	}
	
	return 0;
}

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 1001;
int dist[maxn][maxn];

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    int h, w;
    cin >> h >> w;
    vector<pii> q;
    forn(i, h) forn(j, w) {
        char c;
        cin >> c;
        if (c == '#') {
            dist[i][j] = 0;
            q.pb({i, j});
        } else dist[i][j] = 1e9;
    }

    int ans = 0;
//    for (auto ww: q) {
    forn(cur, q.size()) {
        auto ww = q[cur];
        int x = ww.fi, y = ww.se;
        ans = dist[x][y];
        forn(d, 4) {
            int xx = x + dx[d], yy = y + dy[d];
            if (min(xx, yy) < 0 || min(h - xx, w - yy) <= 0 || dist[xx][yy] < 1e8) continue;
            dist[xx][yy] = dist[x][y] + 1;
            q.pb({xx, yy});
        }
    }

    cout << ans << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 14444
//int a = s - '0'; 文字から数字
//小文字から大文字
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//map 全探索
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapのキー：it->first mapのバリュー：it->second
//大文字判定 isupper(文字) 小文字判定 islower(文字)
//do{}while(next_permutation(ALL(配列)))
//小文字に対応する文字コード：S[i] - 'a'
//文字コード→小文字：(char)(数字+'a')
//グラフの距離:隣接行列で扱う
//bool型　初期値はTrue
//島渡りの問題：中間ノードに着目
//数が大きい時の比較はstring型で行う
//全て0になったか調べたい->0になるたびにcntする
//例外処理は最初にする
//x = p^m + q^n...の約数の個数:(n+1)*(m+1)....
//N!のどの素因数で何回割れるか
//⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和
//パズルの問題->一般化して全探索
//stack<ll> s;
//s.push(要素);s.top();s.pop();
//queue<ll> q;
//q.push(要素);q.front();q.pop();
//同じ作業繰り返す系の問題：収束先を見つける
//過半数：N/2.0で判定
//優先度付きキュー
//priority_queue<
//ll,
//vector<ll>
//> q;
#include <bits/stdc++.h>
#define rep(i,N) for(int i = 0; i < N;i++)
#define ALL(a) (a).begin(),(a).end()
#define ll long long int
#define PI 3.14159265358979323846264338327950L
using namespace std;

//割るやつ
const ll MOD = (pow(10, 9) + 7);

// K進数でのNの桁数
ll dig(ll N, ll K) {
	ll dig = 0;
	while (N) {
		dig++;
		N /= K;
	}
	return dig;
}
// a,bの最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

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

//階乗計算
ll f(ll n) {
	if (n == 0 || n == 1) return 1;
	else return (n * f(n - 1));
}

//Nのd桁目の数
ll dignum(ll N, ll d) {
	ll x = pow(10, d);
	N %= x;
	ll y = pow(10, d - 1);
	N /= y;
	return N;
}

//Nをdで何回割れるか
ll divcnt(ll N, ll d) {
	ll ans = 0;
	while (1) {
		if (N%d == 0) {
			ans++;
			N /= d;
		}
		else break;
	}

	return ans;
}

//素数判定
bool prime(ll num) {
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false;
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2) {
		if (num % i == 0) return false;
	}
	return true;
}

//フィボナッチ数列
vector<ll> memo(pow(10, 6) + 1);
ll fibo(ll n) {
	if (n == 1) return 1;
	else if (n == 2) return 1;
	else if (memo[n] != 0) return memo[n];
	else return memo[n] = fibo(n - 1) + f(n - 2);
}

ll RS(ll N, ll P, ll M) {
	if (P == 0) return 1;
	if (P % 2 == 0) {
		ll t = RS(N, P / 2, M);
		return t * t % M;
	}
	return N * RS(N, P - 1, M);
}

vector<int> IntegerToVector(int bit, int N) {
    vector<int> S;
    for (int i = 0; i < N; ++i) {
        if (bit & (1 << i)) {
            S.push_back(i);
        }
    }
    return S;
}

int main() {
	ll K;cin >> K;
	string s;
	for(int i = 1; i <= K; i++){
		if(i == 1){
			s = "ACL";
		}
		else{
			s += "ACL";
		}
	}

	cout << s << endl;
}	

Measured memory_bytes: 10880
//Created by Kira

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

#define IOS cin.sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int> 
#define pb push_back
#define vi vector<int> 
using ll = long long;

const int mod = 1e9 + 7;

ll pwr(ll a, ll b);
   
const int nax = 2e5+10;
ll factorial[nax];

ll binom(ll n, ll k){
   if(n < k)return 0ll;
   return factorial[n] * pwr(factorial[k],mod-2) % mod * pwr(factorial[n-k], mod-2) % mod;
}


int main(){
   IOS


   ll n, k; cin>>n>>k;


   factorial[1] = 1ll, factorial[0] = 1ll;
   for(int i = 2; i <= nax; i++)
      factorial[i] = factorial[i-1] * i % mod;
   
   
   ll ans = 0;

   for(ll m = 0ll ; m < min(n,k+1); m++){
      ans += ((binom(n,m) % mod) * (binom(n-1, n-1-m) % mod)) % mod;
      ans %= mod;
   }

   cout<<ans<<"\n";



}

ll pwr(ll a, ll b){
   a %= mod;	//Remove mod if not required
   ll res = 1;
   while(b > 0){
   if(b&1)
       res = res * a % mod;
        a = a * a % mod;
   b >>= 1;
    }

    return res;
}



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

const int MAX = 1e5 + 5;
int n, a[MAX];

int main() {
	scanf("%d", &n);

	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);

	long long ans = 0;
	for(int i = 1; i <= n; i++) {
		ans += a[i] / 2;

		if(a[i] % 2 == 1 && a[i + 1] > 0)
			a[i + 1]++;
	}

	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 768
/*
** prim for MST
*/

# include <iostream>
using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GREY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

void init() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int e;
            cin >> e;
            M[i][j] = (e == -1) ? INFTY : e;
        }
    }
}

int prim() {
    int u, minv;
    int d[MAX], p[MAX], color[MAX];

    for (int i = 0; i < n; i++) {
        d[i] = INFTY;
        p[i] = -1;
        color[i] = WHITE;
    }

    d[0] = 0;

    while(true) {
        minv = INFTY;
        for (int i = 0; i < n; i++) {
            if (d[i] < minv && color[i] != BLACK) {
                minv = d[i];
                u = i;
            }
        }

        if (minv == INFTY)
            break;

        color[u] = BLACK;

        for (int v = 0; v < n; v++) {
            if (color[v] != BLACK && M[u][v] != INFTY) {
                if (M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GREY;
                }
            }
        }
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] != -1) {
            sum += M[i][p[i]];
        }
    }

    return sum;
}

void solve() {
    init();
    cout << prim() << endl;
}

int main() {
    // freopen("infile", "r", stdin);
    // freopen("outfile", "w", stdout);
    solve();
    return 0;
}



Measured memory_bytes: 3116
#include <iostream>

int main()
{
  int K, S;
  std::cin >> K >> S;
  int count = 0;
  for ( int x = 0; x <= K; x++ ) {
    for ( int y = 0; y<= K; y++ ) {
      if (x + y <= S && (S-x-y) <= K) {
        count++;
      }
    }
  }
  std::cout << count << std::endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;

int D;
VI c;
VVI s;

VI calc_scores(const VI &t) {
	VI scores(D, 0);
	VI lastday(26, 0);

	int score = 0;
	for (int i = 0; i < D; i++) {
		int day = i + 1;
		int contest = t[i] - 1;
		lastday[contest] = day;
		score += s[i][contest];

		for (int j = 0; j < 26; j++) {
			score -= c[j] * (day - lastday[j]);
		}
		scores[i] = score;
	}

	return scores;
}

int main() {
	cin >> D;

	c.assign(26, 0);
	for (int i = 0; i < c.size(); i++) {
		cin >> c[i];
	}
	s.assign(D, VI(26, 0));
	for (int i = 0; i < D; i++) {
		for (int j = 0; j < s[i].size(); j++) {
			cin >> s[i][j];
		}
	}

	VI t(D, 0);
	for (int i = 0; i < D; i++) {
		cin >> t[i];
	}

	auto scores = calc_scores(t);
	for (auto x: scores) {
		cout << x << endl;
	}
	return 0;
}


Measured memory_bytes: 3220
#include<iostream>

using namespace::std;
string s;
int n,t;

void brute(int i,string g,int k){
	if(k==0){
		if(g>s)
			return;
		//cout<<g<<' '<<(g>s)<<'\n';
		int o=1;
		//for(int j=0;j<n;j++)
		//	o*=max(1,g[j]-'0');
		t+=o;
		return;
	}
	//cout<<g<<'\n';
	for(;i<n-k+1;i++){
		if(k!=1)
			for(int j='1';j<='9' && g<=s;j++){
				g[i]=j;
				if(g<=s) brute(i+1,g,k-1);
			}
		else{
			g[i]='9';
			if(g>s)
				t+=s[i]-'0';
			else t+=9;
		}
		g[i]='0';
	}
}

int main(){
	cin>>s;
	int k;
	n=s.length();
	cin>>k;
	string g="";
	for(int i=0;i<n;i++)
		g+="0";
	brute(0,g,k);
	cout<<t<<'\n';
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    rep(k, K){
        bool fin = true;
        rep(i, N) if(A[i] != N) fin = false;
        if(fin) break;
        vector<int> memo(N+1), tmp(N);
        fill(all(memo), 0);
        fill(all(tmp), -1);
        int now = 0;
        rep(i, N){
            memo[i]++;
            memo[min(N, i+A[i]+1)]--;
            now += memo[i];
            tmp[i] += now;
        }
        fill(all(memo), 0);
        now = 0;
        rep3(i, N-1, 0){
            memo[i+1]++;
            memo[max(0, i-A[i])]--;
            now += memo[i+1];
            tmp[i] += now;
        }
        swap(A, tmp);
    }
    rep(i, N) cout << A[i] << ' '; cout << endl;
}

Measured memory_bytes: 5636
#include <bits/stdc++.h>
using namespace std;
long long t;
int a[4];
int gcd(int x, int y, int z)
{
	int s=min(x, min(y, z));
	while (x%s!=0||y%s!=0||z%s!=0) s--;
	return s;
}
int main()
{
	int k;
	cin >> k;
	a[1]=1, a[2]=1, a[3]=1;
	while (a[0]==0)
	{
		a[3]++;
		int i=3;
		while (a[i]>k)
		{
			a[i]=1;
			i--;
			a[i]++;
		}
		t+=gcd(a[1], a[2], a[3]);
	}
	cout << t << endl;
	return 0;
}

Measured memory_bytes: 3596
from collections import Counter

m = input()
x = int(m)
n = len(m)
d = [3,5,7]
ans2 = []

def dfs( n , s ):
    if s // (10**(n-1)) != 0:
        ans.append(s)
        return
    else:
        for j in d:
            dfs( n , s*10+j )
            
def kak(n,lis):
    for l in range(len(lis)):
        a = [0] * 10
        c = lis[l]
        for _ in range(n):
            b = c % 10
            a[b] += 1
            c //= 10
            
        if a[3] != 0 and a[5] != 0 and a[7] != 0:
            ans2.append(lis[l])
            
for i in range(n+1):
    ans = []
    dfs(i,0)
    kak(i,ans)

ta = 0
for i in ans2:
    if x >= i:
        ta += 1
        
print(ta)

Measured memory_bytes: 46960
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;

template<Int Mod = 1000000007> class modint{
public:
	Int val;
	constexpr modint(const Int x = 0) : val(x % Mod) {if(val < 0) val += Mod;}
	constexpr modint operator+(const modint rhs) const {
		return modint(*this) += rhs;
	}
	constexpr modint operator-(const modint rhs) const {
		return modint(*this) -= rhs;
	}
	constexpr modint operator*(const modint rhs) const {
		return modint(*this) *= rhs;
	}
	constexpr modint operator/(const modint rhs) const {
		return modint(*this) /= rhs;
	}
	constexpr modint operator-() const {
		return modint(Mod - val);
	}
	constexpr modint &operator+=(const modint rhs){
		val += rhs.val;
		if(val >= Mod) val -= Mod;
		return *this;
	}
	constexpr modint &operator-=(const modint rhs){
		if(val < rhs.val) val += Mod;
		val -= rhs.val;
		return *this;
	}
	constexpr modint &operator*=(const modint rhs){
		val *= rhs.val;
		val %= Mod;
		return *this;
	}
	constexpr modint &operator/=(const modint rhs){
		return *this *= inv(rhs);
	}
	constexpr modint &operator++(){
		++val;
		if(val == Mod) val = 0;
		return *this;
	}
	constexpr modint &operator--(){
		--val;
		if(val == -1) val = Mod - 1;
		return *this;
	}
	constexpr modint operator<<(const Int rhs) const {
		return modint(*this) << rhs;
	}
	constexpr modint operator>>(const Int rhs) const {
		return modint(*this) >> rhs;
	}
	constexpr modint &operator<<=(const Int rhs){
		val <<= rhs;
		val %= Mod;
		return *this;
	}
	constexpr modint &operator>>=(const Int rhs){
		val >>= rhs;
		val %= Mod;
		return *this;
	}
	constexpr bool operator==(const modint rhs) const {
		return val == rhs.val;
	}
	constexpr bool operator!=(const modint rhs) const {
		return val != rhs.val;
	}
	constexpr bool operator<(const modint rhs) const {
		return val < rhs.val;
	}
	constexpr bool operator>(const modint rhs) const {
		return val > rhs.val;
	}
	constexpr bool operator<=(const modint rhs) const {
		return val <= rhs.val;
	}
	constexpr bool operator>=(const modint rhs) const {
		return val >= rhs.val;
	}
	friend ostream& operator<<(ostream& os, const modint<Mod>& m){
		return os << m.val;
	}
	friend istream& operator>>(istream& is, modint<Mod>& m){
		return is >> m.val;
	}
	static constexpr modint inv(modint m){
		Int a = m.val, b = Mod, u = 1, v = 0;
		while(b){
			Int t = a / b;
			a -= t * b;
			swap(a, b);
			u -= t * v;
			swap(u, v);
		}
		u %= Mod;
		if(u < 0) u += Mod;
		return modint(u);
	}
	constexpr operator Int() const {
		return val;
	}
};
using mint = modint<>;
vector<mint> fact(1, 1);
mint comb(Int n, Int r){
	if(n < 0 || r < 0 || n - r < 0) return 0;
	while(Int(fact.size()) <= n)
		fact.push_back(fact.back() * mint(fact.size()));
	return fact[n] / fact[r] / fact[n-r];
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	Int n, k; cin >> n >> k;
	mint ans(0);
	for(Int i=0; i<=min(k, n); ++i){
		ans += comb(n-1, i) * comb(n, i);
	}
	cout << ans << "\n";
}

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

#ifdef _DEBUG
#define debug(x) cerr << "line: " << __LINE__ << ", func: " << __func__ << " ->  " << #x << " = " << x << endl
#define INFILE freopen("input.txt", "r", stdin)
#else
#define debug(x)
#define INFILE
#endif

#define ALL(s) begin(s), end(s)
#define RALL(s) rbegin(s), rend(s)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, a, b) for (int i = (a); i >= (b); i--)

using namespace std;

using ll = long long;
using ull = unsigned long long;
using i_i = pair<int, int>;
using ll_ll = pair<ll, ll>;
using d_ll = pair<double, ll>;
using ll_d = pair<ll, double>;
using d_d = pair<double, double>;

static constexpr ll INF = 1LL << 60;
static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288);
static constexpr double EPS = numeric_limits<double>::epsilon();

static map<type_index, const char* const> scanType = {
    {typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}};

template <class T> static void scan(vector<T>& v);
[[maybe_unused]] static void scan(vector<string>& v, bool isWord = true);
template <class T> static inline bool chmax(T& a, T b);
template <class T> static inline bool chmin(T& a, T b);
template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val);

ll_ll dp[410][410];  // first -> value, second -> cost

int main(int argc, char* argv[]) {
  int n;
  cin >> n;
  vector<ll> v(n);
  scan(v);

  REP(i, 0, n) { dp[i][i + 1] = ll_ll(v[i], 0); }

  REP(i, 0, n + 1) {
    REP(first, 0, n - i + 1) {
      int last = first + i;
      if (i <= 1) continue;

      ll_ll result{INF, INF};
      REP(middle, first + 1, last) {
        ll_ll v1 = dp[first][middle];
        ll_ll v2 = dp[middle][last];

        ll cost = v1.first + v2.first + v1.second + v2.second;
        if (cost < result.second) {
          result.first = v1.first + v2.first;
          result.second = cost;
        }
      }

      dp[first][last] = result;
    }
  }

  cout << dp[0][n].second << endl;
  return 0;
}

template <class T> static void scan(vector<T>& v) {
  auto tFormat = scanType[typeid(T)];
  for (T& n : v) {
    scanf(tFormat, &n);
  }
}

static void scan(vector<string>& v, bool isWord) {
  if (isWord) {
    for (auto& n : v) {
      cin >> n;
    }
    return;
  }

  int i = 0, size = v.size();
  string s;
  getline(cin, s);

  if (s.size() != 0) {
    i++;
    v[0] = s;
  }

  for (; i < size; ++i) {
    getline(cin, v[i]);
  }
}

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

template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) {
  std::fill((T*)arr, (T*)(arr + N), val);
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const string YES = "Possible";
const string NO = "Impossible";
#define pb push_back
#define mp make_pair

int K;

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

void solve()
{
	ll ans = 0;
	for(int i=1;i<=K;++i)for(int j=1;j<=K;++j)for(int k=1;k<=K;++k){
	    ans += gcd(gcd(i,j), k);
	}
	cout << ans << '\n';
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> K;
	solve();
	return 0;
}


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

int main() {
	// your code goes here
int H,W;
while(1){
cin>>H;
cin>>W;
if(H==0&&W==0){break;}
    for(int i=1;i<=H;i++){
        	for(int j=1;j<=W;j++){
		    cout<<"#";
		    if(j==W){cout<<endl;}
		    
		    }
    }
    cout<<endl;
}
	return 0;
}

Measured memory_bytes: 1156
#include<stdio.h>

int main(void)
{
	int i,j,n,A[100],min,temp,count=0;
	
	scanf("%d",&n);
	for(i=0;i<=n-1;i++)
	{
		scanf("%d",&A[i]);
	}
	for(i=0;i<=n-1;i++)
	{
		min=i;
		for(j=i;j<=n-1;j++)
		{
			if(A[min]>A[j])
			{
				min=j;
			}
		}
		if(i!=min)
		{
		   temp=A[min];
		   A[min]=A[i];
		   A[i]=temp;
		   count++;
		}
	}
	printf("%d",A[0]);
	for(i=1;i<=n-1;i++)
	{
		printf(" %d",A[i]);
	}
	printf("\n");
	printf("%d\n",count);
	return 0;
}

Measured memory_bytes: 1792
//include
//------------------------------------------
#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 <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

int main() {

    int N, Y;

    cin >> N >> Y;

    for(int x = 0; x <= N; x++){
        for(int y = 0; y <= N; y++) {
            int z = N - x - y;
            if(z >= 0 && z <= N && 10000*x+5000*y+1000*z == Y) {
                cout << x << ' ' << y << ' ' << z << endl;
                return 0;
            }
        }
    }
    cout << -1 << ' ' << -1 << ' ' << -1 << endl;
    return 0;
}


Measured memory_bytes: 256
n,k = map(int, input().split())
d = []
a_s = []
for _ in range(k):
    d.append(int(input().strip()))
    a = list(map(int, input().split()))
    a_s += a
a_s = set(a_s)
count = 0
for i in range(1, n + 1):
    if i not in a_s:
        count += 1
print(count)


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

using namespace std;

int solve(const vector< vector<int> >& g, int p){
	vector<int> r;
	for(const int& t : g[p]){
		r.push_back(solve(g, t));
	}
	sort(r.begin(), r.end());
	int res = 0;
	int n = r.size();
	for(int i=0;i<n;i++){
		res = max(res, r[i]+n-i);
	}
	return res;
}

int main(){
	int N;
	while(cin >> N){
		vector< vector<int> > g(N);
		for(int i=1;i<N;i++){
			int a; cin >> a;
			g[a-1].push_back(i);
		}
		cout << solve(g, 0) << endl;
	}
}


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

using namespace std;

void dfs(int u, long long cur, const vector<vector<int>> &g,
         const vector<int> &add, vector<long long> &res, vector<int> &vis) {
    vis[u] = 1;
    cur += add[u];
    res[u] = cur;
    for (int v : g[u]) {
        if (!vis[v]) {
            dfs(v, cur, g, add, res, vis);
        }
    }
}
int main() {
    std::ios::sync_with_stdio(false); 
    cin.tie(nullptr);

    int N, Q;
    cin >> N >> Q;
    vector<vector<int>> g(N);
    for (int i = 0; i < N - 1; ++i) {
        int a, b;
        cin >> a >> b; 
        g[a - 1].push_back(b - 1);
        g[b - 1].push_back(a - 1);
    }
    vector<int> add(N, 0);
    for (int i = 0; i < Q; ++i) {
        int p, x;
        cin >> p >> x;
        add[p - 1] += x;
    }
    vector<long long> res(N, 0);
    vector<int> vis(N, 0);
    dfs(0, 0, g, add, res, vis);
    for (auto x : res) {
        cout << x << " ";
    }
    cout << "\n";

    return 0;
}

Measured memory_bytes: 32128
#include<stdio.h>
int main()
{
    int N,i,flag;
    scanf("%d",&N);
    int A[N];
    for(i=0;i<N;i++)
    {
    scanf("%d",&A[i]);
    if(A[i]%2==0)
    {
      if(A[i]%3==0|| A[i]%5==0)
      {
          flag=1;
      }
      else
      {
          flag=0;
          break;
      }
    }
    }
    if(flag==1)
        printf("APPROVED\n");
    else
        printf("DENIED\n");
    return(0);
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RNG(i,from,to) for(int i=(from); i<(int)(to); i++)
#define gcd(i,j) __gcd((i), (j))
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vecll;
template<typename S, typename T> string to_string(pair<S, T> p) { return "("+to_string(p.first)+","+to_string(p.second)+")"; }
template<typename T> string to_string(T p[2]) { return "["+to_string(p[0])+","+to_string(p[1])+"]"; }
template<typename T> void println(T e){ cout << to_string(e) << endl; }
template<typename T> void println(const vector<T>& v){ cout << "[";for(const auto& e : v){ cout << to_string(e) << ","; }cout << "]";cout << endl; }
template<typename T> void println(const vector<vector<T>>& vv){ for(const auto& v : vv){ println(v); } }
template<typename Iter> void println(Iter first, Iter last){ for(auto it=first; it != last; it++){ cout << *it << " "; }; cout << endl; }
template<typename T> T mod_pow(T x, T n, const T &p) { T ret = 1; while(n > 0) { if(n & 1) (ret *= x) %= p; (x *= x) %= p; n >>= 1; } return ret; }
template<typename T> T mod_inv(T x, const T &p) { return mod_pow(x, p-2, p); }
const ll DVSR = 1e9+7;


int main(int argc, char const *argv[])
{
  ll M; cin >> M;
  cout << (48 - M) << endl;
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
int main(void){
    long int a,b,c,k,sum=0;
    
    scanf("%ld",&a);
    scanf("%ld",&b);
    scanf("%ld",&c);
    scanf("%ld",&k);
    
    if(k<=a){
        sum=k;
    }else if(k<=(a+b)){
        sum=a;
    }else{
        c=k-a-b;
        sum=a-c;
    }
    
    printf("%ld\n",sum);
    
    return 0;
}

Measured memory_bytes: 2152
#include <stdio.h>

const long long Mod = 1000000007;

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

int main()
{
	int N, A, B, C, D;
	scanf("%d %d %d %d %d", &N, &A, &B, &C, &D);
	
	int i, j, k;
	long long dp[2][1001] = {{1}, {1}}, fact[1001], fact_inv[1001], tmp;
	for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = N - 1, fact_inv[N] = div_mod(1, fact[N], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;
	for (i = A; i <= B; i++) {
		for (k = 1; k <= N; k++) dp[i%2][k] = dp[1-i%2][k];
		if (i * C > N) break;
		for (j = 1, tmp = 1; j < C; j++) tmp = tmp * fact[i*j] % Mod * fact_inv[i*(j-1)] % Mod * fact_inv[i] % Mod;
		for (; j <= D && i * j <= N; j++) {
			tmp = tmp * fact[i*j] % Mod * fact_inv[i*j-i] % Mod * fact_inv[i] % Mod;
			for (k = i * j; k <= N; k++) dp[i%2][k] = (dp[i%2][k] + dp[1-i%2][k-i*j] * fact[k] % Mod * fact_inv[k-i*j] % Mod * fact_inv[i*j] % Mod * tmp % Mod * fact_inv[j]) % Mod;
		}
	}
	
	printf("%lld\n", dp[1-i%2][N]);
	fflush(stdout);
	return 0;
}

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

int main() 
{
    char c;
    cin >> c;
    if (c == 'A')
        cout << 'T';
    if (c == 'T')
        cout << 'A';
    if (c == 'C')
        cout << 'G';
    if (c == 'G')
        cout << 'C';
    return 0;
}

Measured memory_bytes: 256
#標準入力
N, M = map(int, input().split())
A = list(map(int, input().split()))
#heapqをimportして、Aの要素をすべてマイナスにしてheapify
import heapq
def minus(n):
    return -n
A = list(map(minus, A))
heapq.heapify(A)
#その最小値を出力して削除し、その半分を追加することをM回やる
for i in range(M):
    heapq.heappush(A, -(-heapq.heappop(A)//2))
#Aの要素を合計してminusして出力する
print(minus(sum(A)))


Measured memory_bytes: 73068
#include <bits/stdc++.h>
    using namespace std;
    #define FOR(i,n) for(int i=0;i<n;i++)
    #define why(n,x) int n;while(cin >>n,n!=x)
    #define iFOR(i,x,n)for(int i=x;i<n;i++)
    #define unless(flg) if ( !(flg) )
    #define read cin<<
    #define echo cout<<
    #define fin <<'\n'
    #define __ <<" "<<
    #define ___ <<" "
    #define bash push_back
    #define ALL(x) x.begin(),x.end()
    #define SWAP(a,b) ((a != b) && (a += b,b = a - b,a -= b))
    #define cinf(n,x) for(int UNCH=0;UNCH<(n);UNCH++) cin >> x[UNCH];
    #define fcin(n,x) for(int UNCH=1;UNCH<=(n);UNCH++) cin>> x[UNCH];
    #define abs Abs
    typedef long long ll;
    typedef pair < int , int > pii;
    typedef vector < int > vit;
    typedef map < string , int > mstit;
    typedef vector < pii > vpi ;
    typedef greater < pii > gpi;
    typedef priority_queue < pii , vpi , gpi > dijk ;
    const int INF = 0x3f3f3f3f;
    const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
    const int SMOD = 1000000007;
    const int NMOD = 1000000009;
    int dx[]={1,0,-1,0,1,1,-1,-1};
    int dy[]={0,-1,0,1,-1,1,-1,1};
    ll Abs(ll x){return (x<0 ? -x : x);}
    bool inside(int x,int y,int w,int h){return (x>=0 && y>=0 && x<w && y<h);}
    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 qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
    int gcd(int a,int b){return b?gcd(b,a%b):a;}
    struct xyz{
    xyz(){
    cin.tie(0), ios::sync_with_stdio(false);
    cout <<fixed<<setprecision(12);
    };
    }xyzzy;
     
     
    signed main(){
    int n;
    int h[105]={};
    cin >> n;
    iFOR(i,1,n+1) cin >> h[i];
    int miv = INF;
    int sum = 0;
    iFOR(i,1,n+1){
    sum+=max(h[i]-h[i-1],0);
    }
    cout << sum fin;
    return 0;
    }
     

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int main() 
{ 
int a,b,c;
int a1,a2;
 cin>>a>>b>>c;
 a1=b-a;
 a2=c-b;
 if(a1==a2)
 cout<<"YES";
 else
 cout<<"NO";
    

   return 0; 
} 

Measured memory_bytes: 3632
#include <bits/stdc++.h>
 
using namespace std;
 
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileio freopen("in.in", "r", stdin),freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pss printf
#define MOD 1000000007
#define INF 1e18
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout<<n1<<endl
ll n,a[10005],b[10005];
int main() {
slld(n);
for(int i=1;i<=n;i++)
    slld(a[i]);
for(int i=1;i<=n;i++)
    slld(b[i]);
ll x=0,y=0;
for(int i=1;i<=n;i++)
{
    if(a[i]>b[i])
        x+=a[i]-b[i];
    else
        y+=(b[i]-a[i])/2;
}
if(x<=y)
pss("Yes\n");
else
pss("No\n");
    return 0;
           } 


Measured memory_bytes: 384
#include<stdio.h>
#include<math.h>

void QSort(long long x[ ], long long left, long long right);
void Swap(long long x[ ], long long i, long long j);

int main(){
  long long n,k;
  scanf("%lld%lld",&n,&k);
  
  long long a[n];
  long long b[n];

  for(int i=0;i<n;i++){
    b[i]=0;
  }
  
  
  for(int i=0;i<n;i++){ 
    scanf("%lld",&a[i]);
    b[a[i]-1]++;
  }
  QSort(b,0,n-1);

  long long hiku=0;
  for(long long i=0;i<k;i++){
    hiku=hiku+b[i];
  }

  printf("%lld\n",n-hiku);
  
  
  return 0;
}

  /* クイックソートを行う */
void QSort(long long x[ ], long long left, long long right)
{
    int i, j;
    long long pivot;
 
    i = left;                      /* ソートする配列の一番小さい要素の添字 */
    j = right;                     /* ソートする配列の一番大きい要素の添字 */
 
    pivot = x[(left + right) / 2]; /* 基準値を配列の中央付近にとる */
 
    while (1) {                    /* 無限ループ */
 
        while (x[i] > pivot)       /* pivot より大きい値が */
            i++;                   /* 出るまで i を増加させる */
 
        while (pivot > x[j])       /* pivot より小さい値が */
            j--;                   /*  出るまで j を減少させる */
        if (i >= j)                /* i >= j なら */
            break;                 /* 無限ループから抜ける */
 
        Swap(x, i, j);             /* x[i] と x[j]を交換 */
        i++;                       /* 次のデータ */
        j--;
    }
   
 
    if (left < i - 1)              /* 基準値の左に 2 以上要素があれば */
        QSort(x, left, i - 1);     /* 左の配列を Q ソートする */
    if (j + 1 <  right)            /* 基準値の右に 2 以上要素があれば */
        QSort(x, j + 1, right);    /* 右の配列を Q ソートする */
}
 
  /* 配列の要素を交換する */
void Swap(long long x[ ], long long i, long long j)
{
    long long temp;
 
    temp = x[i];
    x[i] = x[j];
    x[j] = temp;
}


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

int found(int n,int x,vector<int> v)
{
  for(int i=0;i<n;i++)
    if(v[i]==x+1)
      return 1;
  return 0;
}

int main()
{
  int n;
  for(;;){
    cin >>  n;
    if(n==0) break;
    vector<int> v(n),w(n);
    for(int i=0;i<n;i++)
      cin >> v[i];
    int l=0;
    for(int i=0;i<2*n;i++)
      if(found(n,i,v)==0){
        w[l]=i+1;
        l++;
      }
    sort(v.begin(),v.end());
    sort(w.begin(),w.end());
    int a=0,b=0;
    int card=0;
    for(;;){
      int c=card;
      for(int i=0;i<n;i++){
        if(v[i]>card){
          card=v[i];
          v[i]=0;
          a++;
          break;
        }
      }
        if(a==n)break;
        if(c==card) card=0;
      c=card;
      for(int i=0;i<n;i++){
        if(w[i]>card){
          card=w[i];
          w[i]=0;
          b++;
          break;
        }
      }
        if(b==n)break;
        if(c==card) card=0;
    }
    cout << n-b << endl;
    cout << n-a << endl;
  }

  return 0;
}



Measured memory_bytes: 3120
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

void Yes(){
	cout<<"Yes"<<endl;
	exit(0);
}
 
void No(){
	cout<<"No"<<endl;
	exit(0);
}

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

ll extgcd(ll a, ll b, ll& x, ll & y) {
    ll d = a;
    if(b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

bool is_prime(int n) {
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            return false;
        }
        return n != 1;
    }
}

vector<int> divisor(int n) {
    vector<int> res;
    for(int i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            res.push_back(i);
            if(i * i != n) res.push_back(n / i);   
        }
    }
}

map<int, int> prime_factor(int n) {
    map<int, int> res;
    for(int i = 2; i * i <+ n; i++) {
        while(n % i == 0) {
            res[i] ++;
            n /= i;
        }
    }
    if(n != 1) res[n] = 1;
    return res;
}

ll mod_inverse(ll a, ll m) {
    ll x, y;
    extgcd(a, m, x, y);
    return (m + x % m) % m;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N; cin >> N;
  ll A[100100]; rep(i, N) cin >> A[i];
  ll inverse[100100];
  rep(i, N) {
      inverse[i] = mod_inverse(i + 1, mod);
  }
  ll imos[100100];
  imos[0] = 0;
  rep(i, N) {
    imos[i + 1] = imos[i] + inverse[i];
    imos[i + 1] %= mod;
  }
  ll ans = 0;
  rep(i, N) {
      ll res = imos[N - i] - imos[0] + imos[i + 1] - imos[1];
      res %= mod;
      res *= A[i];
      res %= mod;
      ans += res;
      ans %= mod;
  }
  rep1(i, N) {
      ans *= i;
      ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 2560
namespace KMP {
   typedef char TYPE;
   #define ran 555555
   
   int prev[ran];
   
   int prepare(TYPE B[], int LB) {
       prev[0] = -1;
       for(int i=1;i<LB;i++) {
           int d=prev[i-1];
           while(d>=0 && B[i]!=B[d+1]) d=prev[d];
           prev[i]=(B[d+1]==B[i]) ? d+1 : -1;
       }
       return true;
   }
   
   int match(TYPE A[], int LA, TYPE B[], int LB, int M[], int &LM) {
       prepare(B, LB);
       int d=-1,ret=0;
       LM=0;
       for(int i=0;i<LA;i++) {
           while(d>=0&&B[d+1]!=A[i]) d=prev[d];
           if (B[d+1]==A[i]) d++;
           if (d==LB-1) {
               M[LM++] = i-LB+1;
               d=prev[d];
           }
       }
       return true;
   }
}

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int l;
char s[555555];
int prev1[555555],prev2[555555];
bool alleq(){
	for(int i=0; i<l; i++)
		if(s[i]!=s[0])
			return false;
	return true;
}
int main(){
	scanf("%s",s);
	l = strlen(s);
	if(alleq()){
		printf("%d\n%d\n",l,1);
		return 0;
	}
	KMP::prepare(s,l);
	memcpy(prev1,KMP::prev, sizeof(int)*l);
	reverse(s,s+l);
	KMP::prepare(s,l);
	memcpy(prev2,KMP::prev, sizeof(int)*l);
	if(l % (l-1-prev1[l-1]) != 0 || l == l-1 - prev1[l-1]){
		printf("%d\n%d\n",1,1);
		return 0;
	}
	int cnt=0;
	for(int i=1; i<l; i++){
		int len1 = i,len2 = l-len1;
		if((len1%(len1-1-prev1[len1-1]) !=0 || len1 == len1 - 1 - prev1[len1-1])
			&& 
			(len2%(len2-1-prev2[len2-1]) !=0 || len2 == len2 - 1 - prev2[len2-1]))
			cnt++;
	}
	printf("2\n%d\n",cnt);
	return 0;
}

Measured memory_bytes: 6528
#include <cstdio>
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>

using namespace std;



int main() {
    int d,d1,i,j,k;
    int c[26];
    int s[365][26];
    cin >> d;
    d1 = 0;
    for(i = 0; i < 26; ++i) cin >> c[i];
    for(i = 0; i < d; ++i) for(j = 0; j < 26; ++j) cin >> s[i][j];

    int arr[365] = {};

    int t1,t2,s1,s2, diff, count;
    count = 0;
    int max, loc;
    int a[26];
    for(int i = 0; i < 26; ++i) a[i] = 1;
    int b[26] = {};
    for(i = 0; i < 365; ++i) {
        for(j = 0; j < 26; ++j) {
            b[j] = s[i][j]+a[j]*c[j];
            a[j]++;
        }
        max = *max_element(b,b+26);
        loc = find(b,b+26,max)-b;
        arr[i] = loc;
        a[loc] = 1;
    }
    
    
    while(true) {


        label1:
        for(i = 0; i < d; ++i) {
            for(j = 0; j < 26; ++j) {
                if(arr[i]==j) continue;
                if(count>20000000) goto label2;
                ++count;
                for(s1 = i+1; s1 < d; ++s1) {
                    if(arr[s1]==arr[i]) break;
                }
                for(t1 = i-1; t1 >= 0; --t1) {
                    if(arr[t1]==arr[i]) break;
                }
                for(s2 = i+1; s2 < d; ++s2) {
                    if(arr[s2]==j) break;
                }
                for(t2 = i-1; t2 >= 0; --t2) {
                    if(arr[t2]==j) break;
                }
                diff = s[i][j]-s[i][arr[i]]+(((s1-i)*(s1-i-1))/2+((i-t1)*(i-t1-1))/2-((s1-t1)*(s1-t1-1))/2)*c[arr[i]] + (-((s2-i)*(s2-i-1))/2-((i-t2)*(i-t2-1))/2+((s2-t2)*(s2-t2-1))/2)*c[j];
                if(diff>0) {
                    arr[i] = j;
                    goto label1;
                }
            }
        }
    }
    label2:
    for(i = 0; i < d; ++i) {
        cout<<(arr[i]+1)<<"\n";
    }


    return 0;
}

Measured memory_bytes: 3716
#include <iostream>
#include <utility>
#include <cstdio>

using namespace std;

void comb(int cur, int depth, int total, int *result, int N, int X) {
    if (depth == 3) {
        if (total == X) { ++(*result); }
    }
    else {
        for (int i = cur; i <= N; ++i) {
            comb(i + 1, depth + 1, total + i, result, N, X);
        }
    }
}

int main(void) {

    while (true) {
        int N, X;
        cin >> N >> X;

        if (N == 0 && X == 0) { break; }

        int result = 0;
        comb(1, 0, 0, &result, N, X);

        printf("%d\n", result);
    }

    return 0;
}

Measured memory_bytes: 1144