akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd



def solve(N: int, X: int):
    a, p = [1], [1]
    for i in range(N):
        a.append(a[i] * 2 + 3)
        p.append(p[i] * 2 + 1)

    def _f(N, X):
        if N == 0:
            return 0 if X <= 0 else 1
        elif X <= 1 + a[N-1]:
            return _f(N-1, X-1)
        elif X < a[N]:
            return p[N-1] + 1 + _f(N-1, X - 2 - a[N-1])
        else:
            return p[N]

    return _f(N, X)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = int(next(tokens))  # type: int
    result = solve(N, X)
    print(result)

if __name__ == '__main__':
    main()


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

template <typename T>
inline T read(){
    T x=0,f=1;char c=getchar();
    while(!isdigit(c)){ if(c=='-') f=-f;c=getchar();}
    while(isdigit(c)){x=(x<<3)+(x<<1)+(c-48);c=getchar();}
    return x*f;
}

#define lint long long int
#define ulint unsigned lint
#define readint read<int>()
#define readlint read<lint>()
const int inf=1e9+1e7;

int x;

int main(void){

    // freopen("testdata.in","r",stdin);
    // freopen("testdata.out","w",stdout);

    scanf("%d",&x);
    x-=400;
    printf("%d\n",8-(x/200));

    return 0;
}

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

string f(int a, int b, int c, int d)
{
    char s[64];
    sprintf(s, "%d %d %d %d", ++a, ++b, ++c, ++d);
    return s;
}

int main()
{
    int h, w;
    cin >> h >> w;

    vector<vector<int>> a(h, vector<int> (w));
    for (auto& x : a) for (auto& y : x) cin >> y;

    vector<string> vs;
    for (int i = 0; i < h - 1; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] % 2) {
                a[i + 1][j]++;
                vs.push_back(f(i, j, i + 1, j));
            }
        }
    }
    for (int i = h - 1, j = 0; j < w - 1; j++) {
        if (a[i][j] % 2) {
            a[i][j + 1]++;
            vs.push_back(f(i, j, i, j + 1));
        }
    }

    cout << vs.size() << endl;
    for (auto x : vs) cout << x << endl;

    return 0;
}


Measured memory_bytes: 9972
#include <stdio.h>
#include <vector>
#include <stack>
#include <functional>
#include <numeric>
#include <complex>
#include <queue>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <unordered_set>
#include <assert.h>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <iostream>
 
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define X first
#define Y second
#define pb(x) push_back(x)
#define FOR(k, n) for (int k = 0; k < n; k++)
#define FORN(k, s, n) for (int k = s; k < n; k++)
#define debug(x) printf("x = %d\n", x);
#define debug2(x, y) printf("x = %d, y = %d\n", x, y);
#define debug3(x, y, z) printf("x = %d, y = %d, z = %d\n", x, y, z);
#define debug4(x, y, z, t) printf("x = %d, y = %d, z = %d, t = %d\n", x, y, z, t);
 
#pragma warning(disable : 6031)
#pragma warning(disable : 4101)
 
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef long double ld;
typedef complex<double> cd;
 
const int INF = 1e9;
const long long LINF = 1e18;
const double PI = acos(-1);
 
// Solution

const int MAXN = 200005;

int n, cur;
pii A[MAXN];

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

    for (int i = 0; i < n; i++) {
        scanf("%d%d", &A[i].X, &A[i].Y);
    }

    sort(A, A + n, [](pii a, pii b) { return a.Y < b.Y; });

    for (int i = 0; i < n; i++) {
        if (A[i].X + cur > A[i].Y) {
            printf("No\n");
            return 0;
        } else {
            cur += A[i].X;
        }
    }

    printf("Yes\n");

    return 0;
}

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

int main()
{
    int a, b, c;
    cin >> a >> b;
    
    c = a + b;
    
    if(c%2==1)
      cout << (c+1)/2 << endl;
    else
      cout << c/2 << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int main(){
  ll h,w;
  cin >> h >> w;
  for(int i = 0;i < w + 2;i++)cout << "#";
  cout << endl;
  for(int i = 0;i < h;i++){
    string s;
    cin >> s;
    cout << "#" << s << "#" << endl;
  } 
  for(int i = 0;i < w + 2;i++)cout << "#";
  cout << endl;
}

Measured memory_bytes: 3604
#include<stdio.h>
int main()
{int a,b,c,i,count=0;
scanf("%d%d%d",&a,&b,&c);

for(i=a;i<=b;i++)
{
   if((c%i)== 0){count=count+1;}
}
printf("%d\n",count);


    return 0;
}



Measured memory_bytes: 2112
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
using pii = pair<int,int>;
#define ALL(x) (x).begin(),(x).end()

int main(){
	int N, K;
	cin >> N >> K;
	vector<ll> A(N);
	for(auto &a : A) cin >> a;

	for(int i=K;i<N;i++) {
		if (A[i-K] < A[i]) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}

Measured memory_bytes: 4800
#include <bits/stdc++.h>
typedef long long ll;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 1 << 30;
const ll INFll = 1ll << 60;
template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

vi dp(1000000, -1);
vvi G(1000000);

ll rec(int v) {
  if (dp[v] >= 0) {
    return dp[v];
  }
  if (G[v].size() == 0) {
    return 0;
  }

  ll ret = -1;
  for (auto nv : G[v]) {
    ret = max(ret, rec(nv) + 1);
  }
  return dp[v] = ret;
}

int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
  }
  ll ans = -1;
  rep(i, N) { ans = max(ans, rec(i)); }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 32768
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int,int>;
/* ちゃんと考えてわかって実装 */

int main(void){
    ll n;
    cin >> n;
    map<ll, ll> mp;
    ll sum = 0;
    for(ll i=0; i<n; i++){
        ll a; cin >> a;
        sum += a;
        mp[a]++;
    }

    ll q; cin >> q;
    for(ll i=0; i<q; i++){
        ll b, c;
        cin >> b >> c;
        sum += mp[b]*(c - b);
        mp[c] += mp[b];
        mp[b] = 0;
        cout << sum << endl;
    }

    /* ll と int の差で落ちたのやばいな、気をつけよ。 */
    return 0;
}

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

using namespace std;

int main(void) {
    int N, M;
    cin >> N >> M;
    if(N >= M) {
        cout << 0 << endl;
    } else {
        vector<int> X(M);
        for(int i=0; i<M; i++) {
            cin >> X.at(i);
        }

        sort(X.begin(), X.end());
        vector< pair<int,int> > diff;
        for(int i=1; i<M; i++) {
            diff.emplace_back(X.at(i) - X.at(i-1), i);
        }
        sort(diff.rbegin(), diff.rend());
        vector<int> cut;
        for(int j=0; j<N-1; j++) {
            cut.push_back(diff.at(j).second);
        }
        cut.push_back(0);
        cut.push_back(M);
        sort(cut.begin(), cut.end());

        int ans = 0;
        for(int j=1; j<=N; j++) {
            ans += X.at(cut.at(j)-1) - X.at(cut.at(j-1));
        }
        cout << ans << endl;
    }
    return 0;
}


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

using namespace std;

int main(void) {
  long n;
  cin >> n;
  long long res = 0;
  for (long b = 1; b < sqrt(n); b++) {
    if (n % b == 0) {
      long m = (n / b) - 1;
      if (m > sqrt(n)) {
      res += m;
      }
    }
  }
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 888
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define PR(a,b) cout << (a) << " " << (b) << endl;
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
const ll MAXL=1000000000000000001LL;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  while(cin >> n) {
    ll sum=0;
    rep(i,n) {
      ll x;
      cin >> x;
      sum+=x;
    }
    pr(sum/n);
  }
  return 0;
}


Measured memory_bytes: 1164
#n = int(input())
n, t = map(int,input().split())
#ls = list(map(int,input().split()))
ls = [list(map(int,input().split())) for i in range(n)]
#ls = [int(input()) for i in range(n)]

p = [i[0] for i in ls if i[1] <= t]
if p == []:
  print("TLE")
  quit()
print(min(p))

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long int
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define INF 0x3f3f3f3f

using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type, less<int>,rb_tree_tag,tree_order_statistics_node_update>   ordered_set;
const int N=1e6+10, mod=1e9+7;


int main(){
	ios_base::sync_with_stdio(0);	cin.tie(0);
	int n; long double res=0;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	res=a[0];
	for(int i=1;i<n;i++){
		res=(res+a[i])/2;
	}
	cout<<fixed<<setprecision(10)<<res;
}
// check for any potential overflow before submitting



Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
    char a[4];
    cin>>a;
    if(a[0]=='R'&&a[1]=='R'&&a[2]=='R')cout<<3;
    else if(a[1]=='R'&&(a[2]=='R'||a[0]=='R'))cout<<2;
    else if(a[0]!='R'&&a[1]!='R'&&a[2]!='R')cout<<0;
    else cout<<1;
    return 0;
}


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

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define ALLTRUE(xs) all_of(ALL(xs),[](bool x){return x;})
#define AND(x,y,z) set_intersection((x).begin(),(x).end(),(y).begin(),(y).end(),inserter((z),(z).end()))
#define ANYTRUE(xs) any_of(ALL(xs),[](bool x){return x;})
#define CONCAT(x,y) (x).insert((x).end(),(y).begin(),(y).end())
#define DUMP(xs) for (auto x:xs) cout<<x<<' ';cout<<endl
#define FOR(i,a,b) for (int i=(int)(a);i<(int)(b);++i)
#define OR(x,y,z) set_union((x).begin(),(x).end(),(y).begin(),(y).end(),inserter((z),(z).end()))
#define OUT(x) cout<<x<<endl
#define REP(i,n) FOR(i,0,n)

struct edge { int to,cost; };
struct ftedge { int from,to,cost; };

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> VECINT;
typedef vector<ll> VECLL;

vector<ll> bellman_ford(int V, int s, vector<ftedge> es) {
  vector<ll> d(V,LLONG_MIN);d[s]=0;
  REP(i,V-1) {
    for (ftedge e:es) {
      if (d[e.from]!=LLONG_MIN && d[e.to]<d[e.from]+e.cost) {
        d[e.to] = d[e.from]+e.cost;
      }
    }
  }
  return d;
}

bool loop_checker(int V, vector<ll> &d, vector<ftedge> es) {
  vector<bool> checker(V,false);
  for (ftedge e:es) {
    if (d[e.from]!=LLONG_MIN && d[e.to]<d[e.from]+e.cost) {
      d[e.to] = d[e.from]+e.cost;
      checker[e.from] = true;
      checker[e.to] = true;
    }
  }

  return checker[V-1];
}

int main() {
  int N,M;cin>>N>>M;
  vector<ftedge> es;
  REP(i,M) {
    int a,b,c;cin>>a>>b>>c;
    a--;b--;
    es.push_back({a,b,c});
  }
  vector<ll> d = bellman_ford(N,0,es);
  loop_checker(N,d,es) ? OUT("inf") : OUT(d[N-1]);

  return 0;
}

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

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

	int N = S.size();

	string X, Y;

	for (int i = 0; i < N; i++)
	{
		if (i % 2 == 0) {
			X.push_back('0');
		}
		else {
			X.push_back('1');
		}
	}

	for (int i = 0; i < N; i++)
	{
		if (i % 2 == 0) {
			Y.push_back('1');
		}
		else {
			Y.push_back('0');
		}
	}

	int ans1 = 0, ans2 = 0;
	for (int i = 0; i < N; i++)
	{
		if (S[i] != X[i]) ans1++;
		if (S[i] != Y[i]) ans2++;
	}

	cout << (ans1 < ans2 ? ans1 : ans2) << endl;
}

Measured memory_bytes: 640
from collections import deque
N = int(input())
A = list(map(int,input().split()))

B_even = deque([A[0]])
if N > 1:
    B_add = deque([A[1],A[0]])

for a_idx in range(2,len(A)):
    if a_idx % 2 == 0 :
        B_even.appendleft(A[a_idx])
        B_even.append(A[a_idx-1])
    else:
        B_add.appendleft(A[a_idx])
        B_add.append(A[a_idx-1])
        
if (N-1) % 2 ==0:
    B = B_even
else:
    B = B_add
    
print(' '.join(map(str,B)))

Measured memory_bytes: 124792
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(A) A.begin(), A.end()

using namespace std;

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

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll N, H; cin >> N >> H;
	vector<ll> a(N, 0LL), b(N, 0LL);
	rep (i, N){
		cin >> a[i] >> b[i];
	} // end rep

	sort(ALL(a), greater<ll>());
	sort(ALL(b), greater<ll>());

	int kb = 0;
	ll sumb = 0LL;
	rep (i, N){
		if (b[i] >= a[0]){
			sumb += b[i];
			++kb;
		} // end if
	} // end rep

	int res = 0;
	for (int i = 0; i < kb; ++i){
		H -= b[i];
		if (H <= 0){
			res = i + 1;
			break;
		} // end if
	} // end for

	if (res == 0){
		res = kb;
		res += (H + a[0] - 1LL) / a[0];
	} // end if

	cout << res << endl;

	return 0;
}

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

int main() {
    string s;
    cin >> s;
    bool n = false;
    bool so = false;
    bool e = false;
    bool w = false;
    for (const char& c : s) {
        if (c == 'N') {
            n = true;
        } else if (c == 'S') {
            so = true;
        } else if (c == 'E') {
            e = true;
        } else if (c == 'W') {
            w = true;
        }
    }
    if ((n != so) || (e != w)) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
}


Measured memory_bytes: 256
#include <stdio.h>

int main() {
  long long int number_n, point_a, point_b, point_c, point_d, flag = 0, i, temp = 1;
  scanf("%lld %lld %lld %lld %lld", &number_n, &point_a, &point_b, &point_c, &point_d);
  point_a--;
  point_b--;
  point_c--;
  point_d--;
  char str[number_n];
  scanf("%s", str);
  if(point_d == number_n) {
    temp = 0;
  }
  if(point_c > point_d) {
    for(i = point_b - 1; i <= point_d + temp; i++) {
      if(str[i] == '.') {
        flag++;
      } else {
        flag = 0;
      }
      if(flag == 3) {
        break;
      }
    }
    if(flag != 3) {
      printf("No\n");
      return 0;
    }
  }
  while(point_b != point_d) {
    if(str[point_b+1] == '.') {
      point_b += 1;
    } else if(str[point_b+2] == '.') {
      point_b += 2;
    } else {
      flag = -1;
      break;
    }
  }
  while(point_a != point_c) {
    if(str[point_a+1] == '.') {
      point_a += 1;
    } else if(str[point_a+2] == '.') {
      point_a += 2;
    } else {
      flag = -1;
      break;
    }
  }
  if(flag == -1) {
    printf("No\n");
  } else {
    printf("Yes\n");
  }
  return 0;
}


Measured memory_bytes: 384
import sys
sys.setrecursionlimit(10 ** 7)


def main():
    s = input()
    compressed = ""
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            compressed += s[i]
    compressed += s[-1]

    print(len(compressed)-1)


if __name__ == '__main__':
    main()


Measured memory_bytes: 3188
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
const int MAXN = 500100;
const ll MOD = 998244353;

int N, M;
ll ninv[MAXN];
ll fac[MAXN];
ll finv[MAXN];
ll npow[MAXN], mpow[MAXN];

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> N >> M;

    ninv[1] = 1;
    for (int i = 2; i <= max (N, M); i++)
    {
        int K = MOD % i;
        ll r = ((ninv[K] * (K - MOD)) / i) % MOD + MOD;
        ninv[i] = r % MOD;
    }

    fac[0] = finv[0] = npow[0] = mpow[0] = 1;
    for (int i = 1; i <= max (N, M); i++)
    {
        fac[i] = (fac[i-1] * i) % MOD;
        finv[i] = (finv[i-1] * ninv[i]) % MOD;
        npow[i] = (npow[i-1] * (N + 1)) % MOD;
        mpow[i] = (mpow[i-1] * (M + 1)) % MOD;
    }

    ll ans = 0;
    for (int i = 0; i <= min (N, M); i++)
    {
        // N choose i, M choose i, i!, (M+1)^(N-i), (N+1)^(M-i)
        ll lchoose = (((fac[N] * finv[i]) % MOD) * finv[N-i]) % MOD;
        ll rchoose = (fac[M] * finv[M-i]) % MOD;
        ll nv = (lchoose * rchoose) % MOD;
        nv = (nv * mpow[N-i]) % MOD;
        nv = (nv * npow[M-i]) % MOD;
        if (i % 2 == 0)
            ans += nv;
        else
            ans -= nv;
    }

    ans = (ans % MOD + MOD) % MOD;
    cout << ans << "\n";
}

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

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

const ll MOD = 1000000007;

ll mod_pow(ll x, ll k) {
	ll res = 1;
	for (; k; k /= 2, x = x * x % MOD) {
		if (k & 1) res = res * x % MOD;
	}
	return res;
}

int N, K;
ll ret;

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

	vi dv;

	for (int i = 1; i * i <= N; ++i) {
		if (N % i == 0) {
			dv.pb(i);
			if (i * i != N) {
				dv.pb(N / i);
			}
		}
	}
	sort(ALL(dv));

	vector<ll> vec(dv.size());

	ll ret = 0;
	rep(i, dv.size()) {
		ll t = mod_pow(K, (dv[i] + 1) / 2);
		rep(j, i) {
			if (dv[i] % dv[j] == 0) {
				t = (t - vec[j]);
				if (t < 0) t += MOD;
			}
		}
		vec[i] = t;

		if (dv[i] & 1) {
			ret = (ret + vec[i] * dv[i]) % MOD;
		} else {
			ret = (ret + vec[i] * dv[i] / 2) % MOD;
		}
	}
	cout << ret << endl;

	return 0;
}

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

int main() {
	int a, b;
	cin >> a >> b;
	while (1) {
		bool check = false;
		for (int i = a; i <= b; i++) {
			if (i % 4 == 0 ) {
				if (i % 100 == 0 && i % 400 != 0) continue;
				check = true;
				cout << i << endl;
			}
		}
		if (check == false) cout << "NA" << endl;
		cin >> a >> b;
		if(a != 0 && b != 0) cout << endl;
		else break;
	}
	return 0;
}


Measured memory_bytes: 3004
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = list(map(int,input().split()))
    count = 0
    #print(A)
    for i in range(N):
        if (i+1) != A[i]:
            count += 1
    if count==0 or count == 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Measured memory_bytes: 3060
#include "bits/stdc++.h"
using namespace std;
#define pb          push_back
#define mp          make_pair
#define ll          long long
#define pii         pair<int, int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int, int>
#define mii         map<pii, int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rev(i,a,b)  for(int i=a;i>=b;i--)
#define lb          lower_bound
#define ub          upper_bound

#define bg(i)       i.begin()
#define fset(it,s)   for(auto it = s.begin(); it != s.end(); it++)
 
#define track(x)                 cerr<<#x<<": "<<x<<endl
#define track2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define track3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define track4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
 
#define ed(i)       i.end()
#define mem(a)	    memset(a, 0, sizeof(a))

#define MOD         1000000007
int mult(int a,int b, int p=MOD){return ((a%p)*(b%p))%p;}
int add(int a, int b, int p=MOD){return (a + b + p)%p;}
int fpow(int n, int k, int p = MOD) {int r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;}
int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
void self_max(int &a, int b) {a = max(a, b); }
void self_min(int &a, int b) {a = min(a, b); }
void self_add(int &a, int b, int p = MOD) { a += b; if(a >= p) a -= p;}
void self_sub(int &a, int b, int p = MOD) { a -= b; if(a < 0) a += p;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
const int INF = 1e9 + 5;
#define N	    	400001

void solve(){
    int n, m;
    cin>>n>>m;
    vi in(m);
    int mat[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    char st[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    rep(i, 0, m)
        cin>>in[i];
    string s[n+1];
    rep(i, 1, n+1) {
        for(int no : in) {
            if(i >= mat[no]) {
                string tmp = s[i-mat[no]] + st[no];
                if(i-mat[no] != 0 && sz(s[i-mat[no]]) == 0)
                        continue;
                sort(all(tmp)); reverse(all(tmp));
                if(sz(tmp) > sz(s[i]))
                    s[i] = tmp;
                else if(sz(tmp) == sz(s[i])) {
                    if(tmp > s[i])
                        s[i] = tmp;
                }
            }
        }
    }
    cout<<s[n]<<endl;
}
int main() {
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
//	cin>>t;
	while(t--){
		solve();
	}
}


Measured memory_bytes: 51416
#include "bits/stdc++.h"
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; typedef long long ll; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
template<class T, class U>bool chmax(T& a, U&& b) { return a < b ? a = b, 1 : 0; }
template<class T, class U>bool chmin(T& a, U&& b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

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

    if (N % 2) {
        write(N / 2);
    }
    else {
        write(N / 2 - 1);
    }
}

Measured memory_bytes: 256
"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0].split(" ")[0]);
    const K = parseInt(arg[0].split(" ")[1]);
    
    const A = arg[1].split(" ").map(n=>BigInt(n));
    let temp = BigInt(1);
    let list = [];
    
    for(let i=0; i<K; i++) {
        temp += A[i];
    }
    
    list.push(temp);
    
    for(let i=K; i<N; i++) {
        temp -= A[i-K];
        temp += A[i];
        
        list.push(temp);
    }
    
    let answer = [];
    
    let n = list.length;
    
    for(let i=1; i<n; i++) {
        if(list[i] <= list[i-1]) {
            answer.push("No");
        } else {
            answer.push("Yes");
        }
    }
    
    console.log(answer.join("\n"))
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 84260
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#define ll int
#define mod 100000007
#define ln printf("\n");
#define mx 501
using namespace std;
inline void in(ll &x){x=0;char c;bool f=0;while((c=getchar())>'9'||c<'0')if(c=='-')f=!f;x=c-48;while((c=getchar())<='9'&&c>='0')x=x*10+c-48;if(f)x=-x;}
inline void out(ll x){if(x<0){putchar('-');x=-x;}if(x>9)out(x/10);putchar(x%10+48);}
inline void outn(ll x){out(x);printf("\n");}
ll n,s[mx],c[mx],f[mx];
int main(){
	in(n);
	ll i,j,st;
	for(i=1;i<n;i++){in(c[i]);in(s[i]);in(f[i]);}
	for(j=1;j<n;j++){st=s[j]+c[j];
	for(i=j+1;i<n;i++){if(st>s[i])st+=(f[i]-((st-s[i])%f[i]))%f[i];if(st<s[i])st=s[i];st+=c[i];}
	outn(st);
}
	outn(0);
}

Measured memory_bytes: 256
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	final long MODULO = 1_000_000_000 + 7;

	void run() throws IOException {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();// 積み木の総数
		int m = sc.nextInt();// 操作回数
		/*
		 * dp[i][j][k]:=i回目の操作まで終えたとき(k=0ならば左端に到達せずに、k=1ならば左端に訪れて）j番目に到達する経路数
		 */

		long[][][] dp = new long[2][2][n + 1];
		for (int i = 0; i <= n; ++i) {
			if (i != 0)
				dp[0][0][i] = 1;
			else
				dp[0][1][i] = 1;
		}

		for (int i = 0; i < m; ++i) {
			for (int k = 0; k < 2; ++k) {
				for (int j = 0; j <= n; ++j) {

					// 左に移動
					if (j - 1 >= 0) {
						if (j - 1 > 0)
							dp[(i + 1) % 2][k][j - 1] += dp[i % 2][k][j];
						else
							dp[(i + 1) % 2][1][j - 1] += dp[i % 2][k][j];
					}
					// 右に移動
					if (j + 1 <= n) {
						dp[(i + 1) % 2][k][j + 1] += dp[i % 2][k][j];
					}

					// 左を経由して元の位置に移動
					if (j - 1 >= 0) {
						if (j - 1 > 0) {
							dp[(i + 1) % 2][k][j] += dp[i % 2][k][j];
						} else if (j - 1 == 0) {
							dp[(i + 1) % 2][1][j] += dp[i % 2][k][j];
						}
					}

					// 右を経由して元の位置に移動
					if (j + 1 <= n) {
						dp[(i + 1) % 2][k][j] += dp[i % 2][k][j];
					}

				}
			}
			for (int k = 0; k < 2; ++k) {
				for (int j = 0; j <= n; ++j) {
					dp[(i + 1) % 2][k][j] %= MODULO;
				}
			}
			dp[i % 2] = new long[2][n + 1];
		}

		long ans = 0;
		for (int i = 0; i <= n; ++i) {
			ans = (ans + dp[m % 2][1][i]) % MODULO;
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}

Measured memory_bytes: 61348
#include <stdio.h>

int main(void){
  int n,t,i,ans[100000];
  scanf("%d",&n);
  for(i=0;i<n;i++){
  	scanf("%d",&t);
  	ans[t-1]=i+1;
  }
  for(i=0;i<n;i++){
  	printf("%d ",ans[i]);
  }
  return 0;
}

Measured memory_bytes: 1152
// スタックサイズ: 100MB

#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <numeric>
#include <iomanip>
#include <queue>
using namespace std;

typedef int64_t ll;
const ll INF = 1000000000000000000;
#define fori(i, a, b) for (ll i = (a); i < (b); ++i)
#define ford(i, a, b) for (ll i = (b - 1); (a) <= i; --i)
#define rep(i, n) fori(i, 0, n)
#define vll vector<ll>
#define all(v) (v).begin(), (v).end()


// mod演算ライブラリ (factset以降に関しては, modは素数限定)
ll mod = 1000000007;


ll mul_mod(ll a, ll b) {  // a * b
	return ((a % mod) * (b % mod)) % mod;
}


int main() {

	ll N;
	string S;
	cin >> N;
	cin >> S;
	vll alfa(26, 0);
	rep(i, N) {
		alfa[S[i] - 'a'] += 1;
	}

	ll ans = 1;
	rep(i, 26) {
		ans = mul_mod(alfa[i] + 1, ans);
	}
	cout << ans - 1 << endl;

}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const ll mod = 1e9+7;

int main(){
    string s;
    cin >> s;
    char t = s.at(s.size()-1);
    if(t == 's'){
        s += "es";
    }
    else{
        s += 's';
    }
    cout << s << endl;
}

Measured memory_bytes: 3608
#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;
      
struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y>a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
long long o[200111];
long long l[200011];
int j[1];

long long x,y,z,mod=1000000007;
string r;
//char r[111];

P u[1];
queue<int> q;

stack<int> s;
map<int,int> p;
vector<int> v;

bool as(P a,P b)
{
  if(a.x==b.x)
  return a.y<b.y;
  return a.x<b.x;
}

long long f(long long x,long long y)
{
  long long p=1;
  for(;y;y/=2,x*=x,x%=mod)
    if(y%2) p*=x,p%=mod;
  return p;
}

int main()
{
  o[0]=1;
  l[0]=1;
  for(int t=1;t<=200000;t++)
    o[t]=o[t-1]*t%mod,l[t]=f(o[t],mod-2);
  scanf("%d %d",&a,&b);
  for(int t=0;t<=min(b,a-1);t++)
  {
    y=o[a]*l[t]%mod*l[a-t]%mod;
    y=y*o[a-1]%mod*l[t]%mod*l[a-t-1]%mod;
    x+=y;
    //x+=o[a]*o[t]%mod*o[a-t]%mod*o[a]%mod*o[t]%mod*o[a-t]%mod;
    x%=mod;
    //printf("%lld\n",x);
  }
  printf("%lld",x);
}





Measured memory_bytes: 3456
#include <iostream>
#include<string>
#include<string.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
//#include <vector.h>
//#include <malloc.h>

#include <math.h>
using namespace std;
#include<stdlib.h>
#include<stdio.h>
int main(){
    int n,m;
    cin>>n>>m;
    long long sum =0;
    sum = (n*(n-1))/2;
    sum += (m*(m-1))/2;
    cout<<sum<<"\n";
}


Measured memory_bytes: 256
#include<stdio.h>
#include<map>
using namespace std;
typedef long long ll;
map<ll,int>p;
map<ll,int>::iterator it;
int fa[100010],siz[100010],a[100010],b[100010],h[100010],nex[200010],to[200010],M,n;
void add(int a,int b){
    M++;
    to[M]=b;
    nex[M]=h[a];
    h[a]=M;
}
ll wd[100010],d[100010];
void dfs(int x,int dis){
    siz[x]=1;
    d[1]+=dis;
    for(int i=h[x];i;i=nex[i]){
        if(to[i]!=fa[x]){
            fa[to[i]]=x;
            dfs(to[i],dis+1);
            siz[x]+=siz[to[i]];
        }
    }
}
void dfs(int x){
    for(int i=h[x];i;i=nex[i]){
        if(to[i]!=fa[x]){
            d[to[i]]=d[x]+n-siz[to[i]]*2;
            dfs(to[i]);
        }
    }
}
int get(int x){return fa[x]==x?x:(fa[x]=get(fa[x]));}
int main(){
    int i;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%lld",wd+i);
        p[wd[i]]=i;
    }
    for(i=1;i<=n;i++){
        fa[i]=i;
        siz[i]=1;
    }
    it=p.end();
    for(it--;it!=p.begin();it--){
        if(n-siz[it->second]*2==0||!p.count(it->first-(n-siz[it->second]*2))){
            puts("-1");
            return 0;
        }
        M++;
        a[M]=it->second;
        b[M]=p[it->first-(n-siz[it->second]*2)];
        siz[fa[a[M]]=get(b[M])]+=siz[a[M]];
    }
    M=0;
    for(i=1;i<n;i++){
        add(a[i],b[i]);
        add(b[i],a[i]);
    }
    fa[1]=0;
    dfs(1,0);
    dfs(1);
    for(i=1;i<=n;i++){
        if(wd[i]!=d[i]){
            puts("-1");
            return 0;
        }
    }
    for(i=1;i<n;i++)printf("%d %d\n",a[i],b[i]);
}

Measured memory_bytes: 17280
#include<bits/stdc++.h>
#define debug1 cout<<"?"<<endl;
#define debug2 cout<<"?"<<i<<endl;
typedef long long ll;
#define PII pair<ll,ll>
const int maxn=2e5+9;
const double pi=acos(-1);
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double ep=1e-10;
using namespace std;
int main()
{
	int h,w,k,i,j;
	char s[20][1009];
	scanf("%d%d%d",&h,&w,&k);
	for(i=0;i<h;i++)scanf("%s",s[i]);
	int ans=2e9;
	for(i=0;i<(1<<(h-1));i++)
	{
		int ii=i,jj,tot=0,num=0,ju=0,cnt=0;
		int v[30]={-1},sum[30]={0};
		while(ii)
		{
			if(ii&1)v[++tot]=num;
			num++;ii>>=1;
		}
		v[++tot]=h-1;
		for(j=0;j<w&&!ju;j++)
		{
			for(ii=1;ii<=tot;ii++)
			{
				num=0;
				for(jj=v[ii-1]+1;jj<=v[ii];jj++)
				{
					if(s[jj][j]=='1')sum[ii]++,num++;
				}
				if(num>k)
				{
					ju=1;break;
				}
				if(sum[ii]>k)
				{
					cnt++;j--;
					memset(sum,0,sizeof sum);break;
				}
			}
		}
		if(ju)continue;
		ans=min(ans,cnt+__builtin_popcount(i));
	}
	printf("%d\n",ans);
}

Measured memory_bytes: 256
MOD = 998244353
N, S = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)

dp = Array.new(N + 1){ Array.new(S + 1, 0) }
dp[0][0] = 1

A.each_with_index do |a, i|
  (0 .. S).each do |j|
    dp[i + 1][j] = (dp[i + 1][j] + 2 * dp[i][j]) % MOD
    dp[i + 1][j + a] = (dp[i + 1][j + a] + dp[i][j]) % MOD if j + a <= S
  end
end
puts dp[N][S]


Measured memory_bytes: 84796
n, k = gets.split.map &:to_i

sum = 0
(1..n).each do |i|
  num = Math.log2((k*1.0/i)).ceil
  # puts "if #{i} need #{num}"

  if num > 0
    sum += (0.5)**num/n
  else
    sum += 1.0/n
  end
end
puts sum


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

typedef long long ll;

int main() {
    ll A, B, C, D;
    cin >> A >> B >> C >> D;
    ll ans = 0;
    ll cn, dn, cdn;
    // A -> B のうちCで割り切れるものの個数
    cn = B / C - (A - 1) / C;
    // A -> B のうちDで割り切れるものの個数
    dn = B / D - (A - 1) / D;
    // A -> B のうちCでもDでも割り切れるものの個数
    ll c = C;
    ll d = D;
    if (c < d) swap(c,d);
    ll temp;
    while (c % d != 0) {
        temp = d;
        d = c % d;
        c = temp;
    }
    ll lcd = C*D/d;
    cdn = B / lcd - (A - 1) / lcd;
    ans = B - A + 1 - (cn + dn - cdn);
    cout << ans << endl;
}



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


int main(void){
  	char S[4];
	//char T1 = "ABC";
  	//char T2 = "ARC";

  	scanf("%s", &S);
  
  if(S[1] == 'B'){
    //printf("%s\n",T2);
    printf("ARC\n");

  }
  else if(S[1] == 'R'){
    printf("ABC\n");
  }
  return 0;
}


Measured memory_bytes: 1608
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define MOD ((ll)1000000007)

int dfs(int v, vector<vector<bool>> &graph, vector<bool> &visited)
{
	bool all_visited = true;
	for (size_t i = 0; i < graph.size(); i++)
	{
		if (visited[i] == false)
		{
			all_visited = false;
			break;
		}
	}

	if (all_visited)
	{
		return 1;
	}

	int ret = 0;
	for (size_t i = 0; i < graph.size(); i++)
	{
		if (graph[v][i] == true)
		{
			if (visited[i] == false)
			{
				visited[i] = true;
				ret += dfs(i, graph, visited);
				visited[i] = false;
			}
		}
	}
	return ret;
}

int main(int argc, char const *argv[])
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);

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

	vector<vector<bool>> graph(N, vector<bool>(N, false));

	for (size_t i = 0; i < M; i++)
	{
		int a, b;
		cin >> a >> b;
		graph[a - 1][b - 1] = true;
		graph[b - 1][a - 1] = true;
	}

	vector<bool> visited(N, false);
	visited[0] = true;
	int ans = dfs(0, graph, visited);
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 384
from copy import deepcopy
import itertools
from bisect import bisect_left
from bisect import bisect_right
import math
from collections import deque
from collections import Counter


def read():
    return int(input())


def readmap():
    return map(int, input().split())


def readlist():
    return list(map(int, input().split()))


INT_MAX = 10 ** 10


if __name__ == "__main__":
    N = read()
    B = [INT_MAX]
    B.extend(readlist())
    B.append(INT_MAX)

    print(sum([min(B[i], B[i-1]) for i in range(1, N+1)]))



Measured memory_bytes: 38256
n=int(input())
a=[[int(i) for i in input().split()]for i in range(n)]
b=[]
for i in a:
    b.append([i[0]-i[1],i[0]+i[1]])

c=sorted(b,key=lambda x:(x[1]))

i=1
finish=c[0][1]
cnt=1
while i<n:
    if c[i][0]>=finish:
        finish=c[i][1]
        cnt+=1
    i+=1
print(cnt)

Measured memory_bytes: 40232
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef tree<int,null_type,less<int >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v);
#ifndef ONLINE_JUDGE
#define debug(a) cout<<#a<<" --> "<<(a)<<"\n";
#define debug2(a) cout<<#a<<" --> "<<(a)<<" ";
#define HERE cout<<"here - "<<__LINE__<<"\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 1e9+7;
const int N = 1e5+5;


void TEST_CASES(int cas)
{
    int n,k;
    scanf("%d %d",&n,&k);
    vector<int>a(n),dp(k+1, -1);
    for(int &i:a)
    {
        scanf("%d",&i);
    }
    int mini = a[0];
    for(int i=0;i<mini;i++)
        dp[i] = 0;//losing
    for(int i=mini;i<=k;i++)
    {
        dp[i] =0;//losing assume
        for(int j:a)
        {
            if(j>i)
                break;
            if(dp[i-j]==0)
            {
                dp[i] = 1;
                break;
            }
        }
    }
    if(dp[k])
    {
        printf("First\n");
    }
    else{
        printf("Second\n");
    }


}


/*

*/

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t=1,cas=0;
    //scanf("%d",&t);
    while(t--)
    {
        TEST_CASES(++cas);
    }

    return 0;
}



//os.order_of_key(v): returns how many elements strictly less than v
//os.find_by_order()

template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
    os<<"{"<<p.first<<", "<<p.second<<"} ";
    return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
    os<<"[ ";
    for(int i=0; i<v.size(); i++)
    {
        os<<v[i]<<" " ;
    }
    os<<" ]";
    return os;
}

template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
    os<<"[ ";
    for(T i:v)
    {
        os<<i<<" ";
    }
    os<<" ]";
    return os;
}
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v)
{
    for(auto i:v)
    {
        os<<"Key : "<<i.first<<" , Value : "<<i.second<<endl;
    }
    return os;
}


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

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

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s;
	cin >> s;
	cout << (s[0] == s[2] ? "Yes" : "No") << endl;
	return 0;
}

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

DIV = 10**9 + 7

# 0 1 2 3 4 5
# 最初の0で選べるのは3色
# その時点で青・赤・黄とも選択可能だから。

# 0 0 0 
# A (Aとは異なるので)B (A,Bとは異なるので)C
# A,B,Cに3色割り振るので 3 * 2 * 1 = 6

# そこまでにA,B,Cがいくつ出てきたかを記録する。
# 次に選べるもの（複数あればどれでもいい）を選定する
# そのときの候補の数を場合の数に掛け算

ABC = [0] * 3 # A,B,Cが順にいくつ使われたのかの管理

ans = 1
for i in range(N):
  num = A[i]
  first = True
  cnt = 0
  for j in range(len(ABC)):
    if ABC[j] == num:
      cnt += 1
      if first:
        ABC[j] += 1
        first = False
  ans = ((ans % DIV) * cnt)%DIV
  
print(ans)

Measured memory_bytes: 14396
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <math.h>
#include <queue>
#include <stdio.h>
using namespace std;
using i64 = int64_t;
#define rep(i, n) for(int i=0;i<n;++i)
#define rep2(i, x, n) for(int i=x;i<n;++i)

int main()
{
    string s;
    int k;
    cin >> s >> k;
    int n = s.size();

    vector<string> h(k);
    rep(i,k) h[i] = "";

    rep(i,n)
    {
        if(h[k-1]!="" && s.substr(i,1)>=h[k-1]) continue;
        rep(j,k)
        {
            if(i+j+1>n) continue;

            string t = s.substr(i,j+1);
            rep(l,k)
            {                
                if(t==h[l]) break;
                if(h[l]=="" || t<h[l])
                {
                    for(int m=k-1;m>l;--m)
                        h[m] = h[m-1];
                    h[l] = t;
                    break;
                }
            }
        }
    }
    cout << h[k-1] << endl;
    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = [0] + [int(i) for i in input().split()]

S = abs(A[N] - A[0])
for i in range(N):
    S += abs(A[i] - A[i + 1])

for i in range(1, N + 1):
    j = (i + 1) % (N + 1)
    ans = S - abs(A[i - 1] - A[i]) - abs(A[i] - A[j]) + abs(A[i - 1] - A[j])
    print(ans)


Measured memory_bytes: 14172
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  int[] d = new int[n];
  int sum = 0;
  for(int i = 0; i<n;i++){
    d[i] = sc.nextInt();
    sum += d[i];
  }
  int ans = 0;
  for(int i = 0;i<n;i++){
    ans += d[i]*(sum - d[i]);
  }
  System.out.println(ans/2);
}}
  


Measured memory_bytes: 23892
#include <stdio.h> 

int main (){

int N, M;
scanf ("%d %d",&N, &M);

int x =0;
 
for (int a=1; a<N; a++){
x += a;
}

int y =0;
for (int b=1; b<M ; b++){
y += b;
}

if (N + M >=2){
    printf ("%d\n",x+y);
}
else printf ("0\n");

return 0;
}

Measured memory_bytes: 1700
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
N_MOVE = 4
EMPTY = 0
ROCK = 1
START = 2
GOAL = 3

INF = 100000

def in_field(field, x, y):
    return y >=0 and y < len(field) and x >= 0 and x< len(field[0])


def move_to_rock(field, x, y, direction):
    while(True):
        x += dx[direction]
        y += dy[direction]
        if not in_field(field, x, y):
            return None
        elif field[y][x] == ROCK:
            x -= dx[direction]
            y -= dy[direction]
            break
        elif field[y][x] == GOAL:
            break
    return x, y


def dfs(depth, field, x, y):
    if depth > 10:
        return None

    cost = INF
    for r in range(N_MOVE):
        nx, ny = x + dx[r], y + dy[r]
        if not in_field(field, nx, ny):
            continue
        if field[ny][nx] == ROCK:
            continue
        next_pos = move_to_rock(field, x, y, r)
        if next_pos is None:
            continue
        nx, ny = next_pos
        if field[ny][nx] == GOAL:
            return depth
        rock_pos_x, rock_pos_y = nx+dx[r], ny+dy[r]
        assert field[rock_pos_y][rock_pos_x] == ROCK
        field[rock_pos_y][rock_pos_x] = EMPTY
        result = dfs(depth+1, field, nx, ny)
        if result is not None:
            cost = min(cost, result)
        field[rock_pos_y][rock_pos_x] = ROCK
    return cost


def find_start_pos(field):
    h = len(field)
    w = len(field[0])
    for y in range(h):
        for x in range(w):
            if field[y][x] == START:
                return x, y
    return None  # ?


def solve():
    w, h = map(int, input().split())
    if w == 0 or h == 0:
        return None
    field = []
    for y in range(h):
        field.append(list(map(int, input().split())))
    x, y = find_start_pos(field)
    res = dfs(1, field, x, y)
    return res


if __name__ == '__main__':
    while(True):
        answer = solve()
        if answer is None:
            break
        elif answer == INF:
            print(-1)
        else:
            print(answer)



Measured memory_bytes: 5628
#include <iostream>
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <stdlib.h>
#include <cassert>
#include <time.h>
#include <numeric>
#include <iomanip> 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef vector<int> vi;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(v) (int)v.size()
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fill(a,v) memset(a,v,sizeof(a));
#define all(v) v.begin(),v.end()
#define forn(i, a, b) for(int i = a; i < b; i++)


#define trarr(a,n) forn(i,0,n) cerr<<a[i]<<" \n"[i==n-1];
#define trmat(g,n,m) forn(i,0,n)forn(j,0,m) cerr<<g[i][j]<<" \n"[i==m-1];
#define tr(...) cerr<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
template<typename S, typename T> 
ostream& operator<<(ostream& out,pair<S,T> const& p){out<<'('<<p.ff<<", "<<p.ss<<')';return out;}
template<typename T>
ostream& operator<<(ostream& out,vector<T> const& v){
int l=v.size();for(int i=0;i<l-1;i++)out<<v[i]<<' ';if(l>0)out<<v[l-1];return out;}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}

const ld PI = acos(-1);
const ld EPS = 1e-9;
const int inf = 1e9+10;
const ll INF = 1e18+10;
const int P = 1e9+7;
const int N = 2e5+10;

vector<int> adj[N];
int dpar[N] = {0};
vector<int> topSort;
bool visited[N] = {0};
void dfs(int u){
	visited[u] = 1;
	for(auto v: adj[u])
		if(!visited[v])
			dfs(v);
	topSort.pb(u);
}
int dp(int u){
	if(dpar[u]!=0) return dpar[u];
	int ret = 0;
	for(auto v: adj[u]){
		ret = max(ret, dp(v));
	}
	return dpar[u] = ret+1;
}
void solve(int _t){
	int n, m;
	cin >> n >> m;
	int u, v;
	forn(i, 0, m){
		cin >> u >> v;
		adj[u].pb(v);
	}
	int ans = 0;
	for(int i = 1; i <= n; i++){
		if(!visited[i]) dfs(i);
	}
	for(int i = sz(topSort)-1; i >= 0; i--){
		ans = max(ans, dp(topSort[i]));
	}
	cout<<ans-1<<endl;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t = 1;
	// cin >> t;
	forn(test, 1, t+1){
		solve(test);
	}
	return 0;
}

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

int main(){

 string s,t;
 cin>>s;
 cin>>t;
 
  
  int N=s.length();
 if(t.length()==N+1 && t.substr(0,N)==s){cout<<"Yes"<<endl;return 0;}
 cout<<"No"<<endl;

}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false),cin.tie(nullptr);
#endif
#define all(c) c.begin(),c.end()
template<typename T>void print(T t){for(auto &it:t)cout<<it<<" ";cout<<endl;}
void solve(){
  int a,b;
  cin>>a>>b;
  int s1=a+b;
  int s2=a*b;
  int s3=a-b;
  cout<<max(s1,max(s2,s3))<<endl;
}
int32_t main(){
  IOS;
  solve();
  #ifdef LOCAL_DEFINE
  cerr<<"time elapsed "<<(double)clock()/LCOKS_PER_SEC<<endl;
  #endif
}

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

using namespace std;
typedef long long ll;

ll f(ll n)
{
    return n * (n-1) / 2;
}

int main(){
    ll n;
    cin >> n;

    vector<ll> as(n);
    vector<ll> cnt(n+1, 0);
    for(size_t i = 0; i < n; ++i)
    {
        cin >> as.at(i);
        cnt.at(as.at(i))++;
    }

    ll total = 0;
    for(size_t i = 0; i <= n; ++i)
        total += f(cnt.at(i));

    for(size_t i = 0; i < n; ++i)
    {
        cout << total - cnt.at(as.at(i)) + 1 << endl;
    }

    return 0;
}


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

int ri() {
    int n;
    scanf("%d", &n);
    return n;
}

int main() {
	std::string s[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
	int res = 0;
	std::string a;
	std::cin >> a;
	int cur = std::find(s, s + 7, a) - s;
	std::cout << 7 - cur << std::endl;
    return 0;
}


Measured memory_bytes: 256