akhauriyash/Code-Regression huggingface dataset:
def solution(a, k):
    n = len(a)
    p, q = [0], []
    for x in a:
        if x < 0:
            q.append(-x)
        else:
            p.append(x)
    q = [0] + q[::-1]
    foo = min(p[-1], q[-1]) * 2 + max(p[-1], q[-1])
    for x in xrange(max(0, k - len(p) + 1), min(k, len(q) - 1) + 1):
        y = k - x
        foo = min(min(q[x], p[y]) * 2 + max(q[x], p[y]), foo)
    return foo

def main():
    n, k = [int(i) for i in raw_input().strip().split()]
    a = [int(i) for i in raw_input().strip().split()]
    print(solution(a, k))

if __name__ == '__main__':
    main()


Measured memory_bytes: 11884
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
std::vector<int> PRIMES;

void init_primes(int max)
{
  if (max < 2) return;
  std::set<int> candidate;
  PRIMES.push_back(2);
  for (int i = 3; i < max; i+=2) {
    candidate.insert(i);
  }
  for (int i = 3; i < sqrt(max); i+=2) {
    auto it = candidate.find(i);
    if (it != candidate.end()) {
      PRIMES.push_back(i);
      int del_num = i;
      while (del_num < max) {
        it = candidate.find(del_num);
        if (it != candidate.end()) candidate.erase(it);
        del_num += i;
      }
    }
  }
  for (auto it = candidate.begin(); it != candidate.end(); ++it) {
    PRIMES.push_back(*it);
  }
}

int main()
{
  const int max = 1000000;
  init_primes(max);
  int i;
  while (std::cin >> i) {
    auto it = PRIMES.begin();
    int n = 0;
    while (*it <= i && it != PRIMES.end()) {
      n++;
      it++;
    }
    std::cout << n << std::endl;
  }
  return 0;
}

Measured memory_bytes: 25020
#include <stdio.h>
#include <cctype>
#include <limits.h>
#include <math.h>
#include <complex>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)
#define if_range(x, y, w, h) if (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4]={-1, 0, 1, 0}, dy[4]={0, -1, 0, 1};
using namespace std;
//typedef pair<int, int> P;
/** Problem0532 : Time Card **/
int main()
{
	rep(i, 3) {
		int h1, m1, s1, h2, m2, s2;
		cin>>h1>>m1>>s1>>h2>>m2>>s2;
		int time = h2*3600+m2*60+s2 - h1*3600 - m1*60 - s1;
		
		cout << time/3600 << " ";
		time %= 3600;
		cout << time/60 << " " << time%60 << endl;
	}
}

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

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

    if((b-a-1) % 2 == 1){
        cout << "Alice" << endl;
    }else{
        cout << "Borys" << endl;
    }

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
namespace FAST_IO
{
	template<typename T> void read(T &a)
	{
		a=0;
		int f=1;
		char c=getchar();
		while(!isdigit(c))
		{
			if(c=='-')
			{
				f=-1;
			}
			c=getchar();
		}
		while(isdigit(c))
		{
			a=a*10+c-'0';
			c=getchar();
		}
		a=a*f;
	}
	template <typename T> void write(T a)
	{
		if(a<0)
		{
			a=-a;
			putchar('-');
		}
		if(a>9)
		{
			write(a/10);
		}
		putchar(a%10+'0');
	}
	template <typename T> void writeln(T a)
	{
		write(a);
		puts("");
	}
}
int n,a[100006];
int main()
{	
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	cout<<a[n/2+1]-a[n/2];
}

Measured memory_bytes: 640
n = int(input())
aaa = list(map(int, input().split()))
li_a = []
for i in aaa:
  i -= 1
  li_a.append(i)
li_b = list(map(int, input().split()))
li_c = list(map(int, input().split()))
ans = 0
for i in range(n):
  ans += li_b[li_a[i]]
  if i-1 >= 0:
      if li_a[i] == li_a[i-1]+1:
        ans += li_c[li_a[i-1]]
print(ans)


Measured memory_bytes: 38384
#include <iostream>

using namespace std;

int main() {
	int N, K;
	string S;

	cin >> N >> K;
	cin >> S;

	for (int i = 0; i < N; i++) {
		if (i == (K - 1)) {
			S[i] = S[i] - ('A' - 'a');
		}
	}

	cout << S << endl;

}

Measured memory_bytes: 256
from operator import mul
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")

MOD = 998244353
class Combination(object):
	def __init__(self, N, MOD):
		self.fac = [0] * (N + 1)
		self.inv = [0] * (N + 1)
		self.finv = [0] * (N + 1)
		self.fac[0], self.fac[1] = 1, 1
		self.inv[1] = 1
		self.finv[0], self.finv[1] = 1, 1
 
		# 前計算
		for i in range(2, N + 1):
			self.fac[i] = self.fac[i - 1] * i % MOD
			self.inv[i] = self.inv[MOD % i] * (MOD - (MOD // i)) % MOD
			self.finv[i] = self.finv[i - 1] * self.inv[i] % MOD
 
	def com(self, N, k):
		return (self.fac[N] * self.finv[k] * self.finv[N - k]) % MOD


def main():
    N, M, K = map(int, input().split())

    if N == 1:
        print(M)
        return

    Com = Combination(N-1, MOD)
    ans = 0
    for k in reversed(range(K+1)):
        ans += M * pow(M-1, N-k-1, MOD) * Com.com(N-1, k)
        ans %= MOD

    print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 78008
import java.util.*;
public class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        int n = Integer.parseInt(sc.next());
        int m = Integer.parseInt(sc.next());
        
        UnionFind uf = new UnionFind(n);
        for(int i=0; i<m; i++){
            int a = Integer.parseInt(sc.next())-1;
            int b = Integer.parseInt(sc.next())-1;
            uf.unite(a,b);
        }
        
        System.out.println(uf.groupCount() - 1);
    }
}

class UnionFind{
    int n;
    int[] par;
    int[] size;
    
    public UnionFind(int n){
        this.n = n;
        par = new int[n];
        for(int i=0; i<n; i++){
            par[i] = i;
        }
        size = new int[n];
        Arrays.fill(size, 1);
    }
    
    public int find(int x){
        if(par[x] == x){
            return x;
        }else{
            par[x] = find(par[x]);
            return par[x];
        }
    }
    
    public boolean unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x==y) return false;
        
        if(size[x]<size[y]){
            int tmp = x;
            x = y;
            y = tmp;
        }
        size[x] += size[y];
        par[y] = x;
        return true;
    }
    
    public boolean same(int x, int y){
        return find(x)==find(y);
    }
    
    public int size(int x){
        return size[find(x)];
    }
    
    public int groupCount(){
        Set<Integer> s = new HashSet<>();
        for(int i=0; i<n; i++){
            s.add(this.find(i));
        }
        return s.size();
    }
}


Measured memory_bytes: 59532
using System;
class prime_number{
    public static void Main(){
        int n = int.Parse(Console.ReadLine());
        int num, count=0;
        for(int i=0; i<n; i++){
            num = int.Parse(Console.ReadLine());
            if(check_prime(num)){
                count++;
            }
        }
        Console.WriteLine(count);
    }
    public static bool check_prime(int n){
        for(int i=2; i<(int)Math.Sqrt(n)+1; i++){
            if(n%i == 0){
                return false;
            }
        }
        return true;
    }
}


Measured memory_bytes: 17940
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>

#define P 1000000007

int comp(const void *a, const void *b){return *(int*)a-*(int*)b;}

int main(void){
	int n, i;
	int b;
	scanf("%d", &n);
	int ans=1;
	int f=0;
	scanf("%d", &b);
	for(i=1; i<n; i++){
		int a;
		scanf("%d", &a);
		if(f>0){
			if(b>a){
				ans++;
				f=0;
			}
		}else if(f<0){
			if(b<a){
				ans++;
				f=0;
			}
		}else{
			if(b<a){
				f=1;
			}else if(b>a){
				f=-1;
			}
		}
		b=a;
	}
	printf("%d\n", ans);
}


Measured memory_bytes: 128
n=int(input())
p=input()
P=p.split()
x=[]
ans=0
for j in range(n):
    P[j]=int(P[j])
for i in range(n-2):
    x.append([])
    x[i].append(P[i])
    x[i].append(P[i+1])
    x[i].append(P[i+2])
for k in range(len(x)):
    if (x[k][0]<x[k][1] and x[k][1]<x[k][2]) or (x[k][1]>x[k][2] and x[k][1]<x[k][0]):
        ans+=1
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pi M_PI
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  ll a,b,c,d;
  cin >> a >> b >> c >> d;
  pr(max(a*b,c*d));
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
    int i,j,k,a,b,c,cnt=0,x,s,a1=500,b1=100,c1=50;
    cin>>a>>b>>c>>x;
    for(i=0;i<=a;i++)
    {
        for(j=0;j<=b;j++)
        {
            for(k=0;k<=c;k++)
            {
                s=a1*i+b1*j+c1*k;
                if(s==x)
                    cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    // 
    int A, B, C, X;
    cin >> A;
    cin >> B;
    cin >> C;
    cin >> X;
    int success_count = 0;
    for (int i = 0; i <= A; i++) {
	    int a_price = 500 * i;
	    for (int j = 0; j <= B; j++) {
		    int b_price = 100 * j;
		    for (int k = 0; k <= C; k++) {
			    int c_price = 50 * k;
			    int value = a_price + b_price + c_price;
			    if (X == value) {
				    success_count += 1;
			    }
		    }
	    }
    }

    cout << success_count << endl;

    return 0;
}


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

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

ll dp[2][3030][3030];

int main() {

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

    int n, t;
    cin >> n >> t;
    vll a(n), b(n);
    rep(i, n) cin >> a[i] >> b[i];

    for(int i = a[0]; i < t; i++) dp[0][0][i] = b[0];
    for(int i = a[n-1]; i < t; i++) dp[1][n-1][i] = b[n-1];

    rep(cur, n) {
        if (cur == 0) continue;
        rep(i, t) {
            dp[0][cur][i] = dp[0][cur-1][i];
            if (i - a[cur] >= 0) {
                dp[0][cur][i] = max(dp[0][cur][i], dp[0][cur-1][i-a[cur]] + b[cur]);
                // cout << cur << " " << i << " " << dp[0][cur][i] << endl;
            }
        }

        int rcur = n - cur - 1;
        rep(i, t) {
            dp[1][rcur][i] = dp[1][rcur+1][i];
            if (i - a[rcur] >= 0) {
                dp[1][rcur][i] = max(dp[1][rcur][i], dp[1][rcur+1][i-a[rcur]] + b[rcur]);
                // cout << rcur << " " << i << " " << dp[1][rcur][i] << endl;
            }
        }
    }

    ll ans = 0;
    rep(i, n) {
        if (i == 0) {
            ans = max(ans, dp[1][1][t - 1] + b[i]);
        } else if (i == n-1) {
            ans = max(ans, dp[0][n-2][t - 1] + b[i]);
        } else {
            rep(j, t) {
                ans = max(ans, dp[0][i - 1][j] + dp[1][i + 1][t - j - 1] + b[i]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 143616
#include<stdio.h>

int main(void)
{
	unsigned long int a, b, c[100];
	int i, j = 0, check = 0;

	scanf("%ld%ld", &a, &b);

	for (i = 0; i < b; i++) {
		scanf("%ld", &c[i]);
	}

	while (1) {
		for (i = 0; i < b; i++) {
			if (a - j == c[i]) {
				check = 1;
				break;
			}
		}
		if (check == 0) {
			printf("%ld", a - j);
			return 0;
		}

		check = 0;

		for (i = 0; i < b; i++) {
			if (a + j == c[i]) {
				check = 1;
				break;
			}
		}
		if (check == 0) {
			printf("%ld", a + j);
			return 0;
		}

		check = 0;

		j++;




	}


	return 0;
}

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

using namespace std;
#define ll long long
#define ull unsigned ll
#define all(x) x.begin(), x.end()
const ll MOD = 1e9 + 7;
const int MAX = 5000010;

unordered_set<int> a[5];

int main()
{
    int a[5];
    vector<pair<int, int>> v(5);
    for (size_t i = 0; i < 5; i++)
    {
        int x;
        cin >> x;
        v[i] = {(x % 10 == 0 ?  0 : 10 - (x % 10)), x};
    }
    sort(all(v));

    int curr = 0;
    for (size_t i = 0; i < 5; i++)
    {
        curr += v[i].second;
        if (i != 4)
            curr += v[i].first;
    }
    cout << curr << endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#define int long long
const int N=100005;
struct D{int w,x,s;}d[N];int n,he[N],to[N],nx[N];
bool cmp(D x,D y){return x.x>y.x;}
inline void wk(int w,int x,int y)
{to[w]=y,nx[w]=he[x],he[x]=w;}
int dfs(int x,int y)
{
	int z=y;
	for(int i=he[x];i;i=nx[i])
		z+=dfs(to[i],y+1);
	return z;
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;++i)
		scanf("%lld",&d[i].x),d[i].w=i,d[i].s=1;
	std::sort(d+1,d+n+1,cmp);
	for(int i=1,x,l,r,I;i<n;++i)
	{
		for(x=0,l=i+1,r=n;l<=r;)
			I=l+r>>1,d[I].x>=d[i].x+2*d[i].s-n?(x=I,l=I+1):(r=I-1);
		if(!x||d[x].x!=d[i].x+2*d[i].s-n)
			return 0&printf("-1");
		d[x].s+=d[i].s,wk(i,d[x].w,d[i].w);
	}
	if(dfs(d[n].w,0)!=d[n].x)return 0&printf("-1");
	for(int i=1;i<=n;++i)
		for(int j=he[i];j;j=nx[j])
			printf("%lld %lld\n",i,to[j]);
	return 0;
}

Measured memory_bytes: 8748
#include<cstdio>
using namespace std;
int main(){
//	freopen("a.in","r",stdin);
	long long a,b,c,d;
	scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
	long long n;
	scanf("%lld",&n);
	if(a*4<c)
		c=a*4;
	if(b*2<c)
		c=b*2;
	if(d<c*2)
	{
		if(n%2==0)
			printf("%lld\n",n/2*d);
		else
			printf("%lld\n",n/2*d+c);
	}
	else
		printf("%lld\n",n*c);
	return 0;
}


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

using namespace std;

int i,j;
long long k,ans[55];

int main(){
	scanf("%lld", &k);
	
	long long t = k / 50;
	k = k % 50;
	
	for(i = 0; i < 50; i++)
	ans[i] = i + t;
	
	for(i = 0; i < k; i++){
		for(j = 0; j < 50; j++){
			if(i == j)
			ans[j] += 50LL;
			else
			ans[j]--;
		}
	}
	
	printf("%d\n", 50);
	
	for(i = 0; i < 50; i++){
		if(i == 0)
		printf("%lld", ans[i]);
		else
		printf(" %lld", ans[i]);
	}
	printf("\n");
}


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

int main(){
  int N;
  cin >> N;
  vector<string> vec(N);
  vector<int> count = {0,0,0,0};
  vector<string> dic = {"AC", "WA", "TLE", "RE"};
  rep(i, N){
    string s;
    cin >> s;
    rep(i, 4){
      if (dic.at(i) == s){
        count.at(i) += 1;
      }
    }
  }
  
  rep(i, 4){
    cout << dic.at(i) << " x " << count.at(i) << endl;
  }
  return 0;
}

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

int main() {
    // 方針
    // L -> R に切り替わるまでを一つの区切りと考える．
    // 一つの区切りに対して， R の個数と L の個数が R -> L に切り替わるところにそれぞれ下記の式のようになる．
    // R -> L の区切りを覚える idx と今 L 側にいてたことを覚える flag を使う

    // 入力
    string S; cin >> S;
    int N = S.size();
    vector<int> ans(N);

    // 変数の準備
    bool flag = true;   // L -> R に切り替わる時
    float cnt_r = 0;    // R の個数
    float cnt_l = 0;    // L の個数
    ll idx = 0;         // R -> L に切り替わる位置
    
    // 文字列 S をみていく
    for (int i = 0; i < N; ++i){
        ans[i] = 0;
        if (S[i] == 'R'){
            if (!flag){
                ans[idx-1] = ceil(cnt_r/2) + floor(cnt_l/2);     // R 側
                ans[idx] = floor(cnt_r/2) + ceil(cnt_l/2);       // L 側
                cnt_r = 1;
                cnt_l = 0;
                flag = true;
            }else{
                cnt_r += 1;
            }
        }else{
            if (flag){
                flag = false;
                idx = i;
            }
            cnt_l += 1;

        }
    }
    ans[idx-1] = ceil(cnt_r/2) + floor(cnt_l/2);     // R 側
    ans[idx] = floor(cnt_r/2) + ceil(cnt_l/2);       // L 側   
    for (int i = 0; i < N; ++i){
        cout << ans[i] << " ";
    }
    cout << "\n";
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
using namespace std;
int main()
{
    int a,b,c=0;
    cin>>a>>b;
    c=b*2+1;
    if (a%c !=0)
    {
        cout<<a/c+1;
    }
   if (a%c ==0)
    {
        cout<<a/c;
    }
    return 0;
}

Measured memory_bytes: 256
def Judge(P):
    for h in range(1,10):
        if P[h]>=2:
            H=P[:]
            H[h]-=2
            flag=True
            for i in range(1,10):
                while H[i]%3:
                    for j in range(3):
                        H[i+j]-=1
                        if H[i+j]<0:
                            flag=False
            if flag:
                return True
    return False

while True:
    try:
        s=map(int,raw_input())
    except EOFError:
        break
    L=[0]*12
    ans=[]
    for i in range(len(s)):
        L[s[i]]+=1
    for i in range(1,10):
        L[i]+=1
        if max(L)!=5 and Judge(L[:]):
            ans.append(str(i))
        L[i]-=1
    print " ".join(ans) if len(ans)!=0 else 0

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

using namespace std;
int n, m;
vector<int> link(2e5 + 5), s(2e5 + 5, 1);

int find(int a){
	if(link[a] == a) return a;
	return link[a] = find(link[a]);
}


void join(int a, int b){
	if(s[a] < s[b]) swap(a, b);
	s[a] += s[b];
	link[b] = a;
}

int main(){
	cin >> n >> m;
	for(int i = 1; i <= n; ++i) link[i] = i;
	for(int i = 0; i < m; ++i){
		int a, b;
		cin >> a >> b;
		a = find(a); b = find(b);
		if(a!=b) join(a, b);
	}
	cout << *max_element(s.begin(), s.begin() + n + 2);
}

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

#define ll unsigned long long

using namespace std;

 

const int mod=998244353;

const int maxn=4e5+7;

ll fac[maxn],inv[maxn];

 

ll power(ll a,ll b){

    ll ans=1;

    while(b){

        if(b&1) ans=ans*a%mod;

        b>>=1;

        a=a*a%mod;

    }

    return ans;

}

 

void init(){

    fac[0]=1,fac[1]=1;

    for(int i=2;i<=maxn;i++) fac[i]=fac[i-1]*1ll*i%mod;

    inv[maxn]=power(fac[maxn],mod-2)*1ll;

    for(int i=maxn-1;i>=0;i--) inv[i]=inv[i+1]*1ll*(i+1)%mod;

}

 

ll c(ll n,ll m){

    if(m==0) return 1;

    if(n<m) return 0;

    if(n==m) return 1;

    return fac[n]*1ll*inv[m]*1ll%mod*inv[n-m]%mod;

}

 

 

int main (){

    init();

    int n,a,b;

    ll k,ans=0;

    scanf("%d %d %d %lld",&n,&a,&b,&k);

    if(k==0) { puts("1"); return 0;}

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

        if((k-a*x)%b) continue;

        ll y=(k-a*x)/b;

        ans=(ans+c(n,x)*1ll%mod*c(n,y)*1ll%mod)%mod;

    }

    printf("%I64d\n",ans);

    return 0;

}

 


Measured memory_bytes: 6528
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
long long dp[51][51][2501];

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, a; cin >> n >> a;
  vector<int> x(n); REP(i, n) cin >> x[i];
  int sum = a * n;
  dp[0][0][0] = 1;
  REP(i, n) REP(j, i+1) REP(k, sum+1) {
    dp[i+1][j][k] += dp[i][j][k];
    if (k + x[i] > sum) break;
    dp[i+1][j+1][k+x[i]] += dp[i][j][k];
  }
  long long ans = 0;
  FOR(i, 1, n+1) {
    ans += dp[n][i][a*i];
    // cout << i << ' ' << dp[n][i][a*i] << '\n';
  }
  cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 49152
#include <cstdio>
#include <cmath>

#define MIN(a, b) (((a) > (b)) ? (b) : (a))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

using int64 = long long;
const int64 MOD = 1000000007;

using namespace std;

// a: base, p: exponent
int64 powmod(int64 a, int64 p) {
	int64 value = 1;
	for(int64 mul = a; p > 0; p >>= 1, mul = (mul * mul) % MOD) {
		if(p & 1) value = (value * mul) % MOD;
	}
	return value;
}

int64 invmod(int64 x) {
	return powmod(x, MOD - 2);
}

struct Factorial {
	int64 *val;
	int64 *inv;
};

Factorial setFact(int N) {
	Factorial fact;
	fact.val = new int64[N];
	fact.inv = new int64[N];
	fact.val[0] = 1;
	for(int i = 1; i < N; i++) {
		fact.val[i] = int64(fact.val[i - 1] * i) % MOD;
	}
	fact.inv[N - 1] = powmod(fact.val[N - 1], MOD - 2);
	for(int i = N - 1; i > 0; i--) {
		fact.inv[i - 1] = int64(fact.inv[i] * i) % MOD;
	}
	return fact;
}

int main() {
	int N, M;
	scanf("%d %d", &N, &M);
	if(abs(N - M) > 1) {
		printf("0\n");
		return 0;
	}
	Factorial f = setFact(MAX(N, M) + 1);
	int64 ans = 1;
	ans = (f.val[N] * f.val[M]) % MOD;
	if(N == M) ans = (ans * 2) % MOD;
	printf("%d\n", ans);
	return 0;
}

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

using namespace std;

int main()
{
    int x, S[]={5,5, 7};
   vector <int> A;
   for(int i=0; i<3; i++)
   {
       cin>>x;
       A.push_back(x);
   }
   sort(A.begin(),A.end());
   for(int i=0; i<3; i++)
   {
       if(A[i]==S[i])
       {
           continue;
       }
       else
       {
           cout<<"NO"<<endl;
           return 0;
       }
   }
   cout<<"YES"<<endl;
   return 0;
}





Measured memory_bytes: 256
def main():
    n = int(input())
    al = list(map(int, input().split())) 
    if 0 in al:
        print(0)
        return
        
    INF = 10**18
    curr_ans = 1
    for a in al:
        curr_ans*=a
        if curr_ans > INF:
            curr_ans = -1
            break

    print(curr_ans)

if __name__ == "__main__":
    main()

Measured memory_bytes: 21696
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <climits>
#include <tuple>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#define rep(i, n) for(Int (i) = 0; (i) < (n); (i)++)
using namespace std;
using Int = int64_t;
constexpr long long int mod = 1e9 + 7;

int main() {

	Int N; cin >> N;
	vector<Int> a(N);
	bool flag = false;
	rep(i, N) {
		cin >> a[i];
		if (a[i] == 1)flag = true;
	}

	if (!flag) {
		cout << -1 << endl;
		return 0;
	}

	Int current = 1;
	Int ans = 0;

	rep(i, N) {

		if (a[i] == current) {
			current++;
		}
		else ans++;

	}

	cout << ans << endl;

}

Measured memory_bytes: 1792
//int a = stoi(c); 文字列をintへ
//小文字から大文字
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//途中の出力をそのまま残さない
//数値計算 個数以外はdouble
//map<キー,値> p は辞書。p[キー] = 値
//map 全探索
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapのキー：it->first mapのバリュー：it->second
//絶対値 abs()
//入力は空白で切れる
//大文字判定 isupper(文字) 小文字判定 islower(文字)
//do{}while(next_permutation(ALL(配列)))
//while(N)で回すとき、Nはコピーを作っておく
//小文字に対応する文字コード：S[i] - 'a'
//文字コード→小文字：(char)(数字+'a')
//グラフの距離:隣接行列で扱う
//等価なものに変換する思考
//bool型　初期値はTrue
//島渡りの問題：中間ノードに着目
//配列はvector<>を使う：意味わからないエラーがなくなる。
//背反な事象にちゃんとわける
//チェックリストはマップを使う
//数が大きい時の比較は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で判定
//ある量からある量を引く系の問題：端から定義してやる
#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
using namespace std;

//円周率
const double PI = 3.14159265358979323846;
//割るやつ
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);
}

int main() {
	ll N; cin >> N;
	ll M; cin >> M;
	vector<ll> A(M); rep(i, M)cin >> A[i];

	ll cnt = 0;
	rep(i, M)cnt += A[i];

	if (cnt > N)cout << -1 << endl;
	else cout << N - cnt << endl;
}
	

Measured memory_bytes: 11012
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[]a = new int[n];
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();
		int[]b = new int[n];
		if(n % 2 == 0) {
			int k = n / 2;
			for(int i = 0; i < n / 2; i++) {
				int id1 = k + i;
				int id2 = k - 1 -  i;
				b[id1] = a[2 * i];
				b[id2] = a[2 * i + 1];
			}
		}else {
			int k = n / 2;
			b[k] = a[0];
			for(int i = 0; i < n / 2; i++) {
				int id1 = k + i + 1;
				int id2 = k - 1 -  i;
				b[id1] = a[2 * i + 1];
				b[id2] = a[2 * i + 2];
			}

		}
		for(int i = 0; i < n; i++) {
			if(i == n - 1) {
				System.out.println(b[i]);
			}else {
				System.out.print(b[i] +" ");
			}
		}
	}
}

Measured memory_bytes: 95300
import scala.io.StdIn._

object Main {
  def main(args: Array[String]) {
    def testResultPrint(test: String): Unit ={
      val Array(test1, test2, test3) = test.split(" ").map(_.toInt)
      val testResult = testCheck(test1, test2, test3)
      if(testResult != "end") {
        println(testResult)
        testResultPrint(readLine())
      }
    }

    def testCheck(t1: Int, t2: Int, t3: Int): String = {
      val t12 = t1 + t2
      if(t1 + t2 + t3 == -3) "end"
      else if(t1 == -1 || t2 == -1 || t12 < 30) "F"
      else if(t12 >= 80) "A"
      else if(t12 >= 65) "B"
      else if(t12 >= 50 || (t12 >= 30 && t3 >= 50)) "C"
      else if(t12 >= 30) "D"
      else "end"
    }

    testResultPrint(readLine())
  }
}

Measured memory_bytes: 43944
#include <cstdio>


int balls[12];

bool DFS(int cur,int left,int right){
    if (cur > 10) return true;
    auto val=balls[cur];
    return (val>left&&DFS(cur+1,val,right))||(val>right&&DFS(cur+1,left,val));
}

int main(){

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

    for(int i=0;i<N;i++){
        for(int i=1;i<=10;i++) scanf("%d",balls+i);
        if (DFS(1,0,0)) puts("YES");
        else puts("NO");
    }

    return 0;
}


Measured memory_bytes: 2624
#abc138c
N = int(input())
list_v = list(map(int, input().split()))

list_v.sort(reverse=True)

while len(list_v)!=2:
    x = list_v.pop()
    y = list_v.pop()
    list_v.append((x+y)/2)
    
x = list_v.pop()
y = list_v.pop()
print((x+y)/2)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#define fr(i1,m) for(int i1=0;i1<m;i1++)
using namespace std;

int main()
{
  long long n,t;
  while(cin>>n>>t)
  {
      long long a[210000]={0},m=0,s=-10,y;
      fr(i,n)
      cin>>a[i];


      fr(i,n)
      {
          if(s<a[i])
          {
              m+=t;
              s=a[i]+t;
          }
          else
          {
              y=s-a[i];
              m=m-y;
                 s=a[i]+t;
                 m+=t;
          }
      }
      cout<<m<<"\n";





  }
    return 0;
}


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

int main(){
  int n,x;

  for(;;){
    cin >> n >> x;
    if ( n == 0 && x == 0) break;

    int count = 0;

    int i,j,k;

    for(i = 1; i <= n-2; i++){

      for(j = i+1; j <= n-1; j++){

        for(k = j+1; k <= n; k++){
          if(i + j + k == x){
            count += 1;
          }
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

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

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
	int a, b; cin >> a >> b;
	cout << a*b;
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
typedef long long ll;
#define pb push_back
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

void solve(){
    int a, p; cin >> a>> p;
    cout << (3*a+p)/2 << endl;
}

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

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

const int INF = 1001001001;

int main(){
  int n;
  cin >> n;
  vector<int> use;
  use.push_back(1);
  int u6 = 6, u9 = 9;
  while(u6 <= n){
    use.push_back(u6);
    u6 *= 6;
  }
  while(u9 <= n){
    use.push_back(u9);
    u9 *= 9;
  }

  vector<int> dp(n+1, INF);
  dp.at(0) = 0;
  rep(i, use.size()){
    int now = use.at(i);
    rep2(j, 1, n+1){
      int add = INF;
      if(j - now >= 0) add = dp.at(j-now) + 1;
      dp.at(j) = min(dp.at(j), add);
    }
  }
  cout << dp.at(n) << endl;
  return 0;
}

Measured memory_bytes: 640
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;

int main(){
    int a, b;
    //char op;
    while( cin >> a >> b, a ){
        for( int i = 0; i < a; i++ ){
            for( int j = 0; j < b; j++ ){
                cout << '#';
            }
            cout << endl;
        }
        cout << endl;
    }
}

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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		var UF = new UnionFind(N);
		for(int i=0;i<M;i++){
			UF.Unite(A[i], B[i]);
		}
		
		Console.WriteLine(UF.Compo - 1);
		
	}
	int N,M;
	int[] A,B;
	public Sol(){
		var d = ria();
		N = d[0]; M = d[1];
		A = new int[M];
		B = new int[M];
		for(int i=0;i<M;i++){
			d = ria();
			A[i] = d[0] - 1;
			B[i] = d[1] - 1;
		}
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}

class UnionFind{
	
	int[] parent;
	int[] mem;
	int compo;
	int N;
	public UnionFind(int n_){
		Initialize(n_);
	}
	
	public void Initialize(int n_){
		N=n_;
		parent=new int[N];
		mem=new int[N];
		for(int i=0;i<N;i++){
			parent[i]=i;
			mem[i]=1;
		}
		compo=N;
	}
	
	public int Parent(int a){
		if(parent[a]==a) return a;
		return parent[a] = Parent(parent[a]);
	}
	
	public bool IsUnited(int a,int b){
		return Parent(a) == Parent(b);
	}
	
	public void Unite(int a,int b){
		a = Parent(a); b = Parent(b);//Parent()を呼ぶことでa以上のノードは全てparentがrootになる
		if(a == b) return;
		if(mem[a] > mem[b]){var t = a; a = b; b = t;}
		parent[a] = b;
		mem[b] += mem[a];
		compo -= 1;
	}
	
	public bool IsRoot(int x){
		return x == parent[x];
	}
	public int MemCnt(int x){
		return mem[Parent(x)];
	}
	
	public void Dump(){
		for(int i=0;i<parent.Length;i++){
			Console.Write(i==0?"{0}":" {0}",parent[i]);
		}
		Console.WriteLine("");
	}
	
	public int Compo{
		get{
			return compo;
		}
	}
	
}


Measured memory_bytes: 42052
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long>vll;
typedef pair<int,int> pi;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

typedef long long type;
using P = pair<type, type>;

struct Edge{
  int to;
  type cost;
    Edge(int a, type b){
        to = a;
        cost = b;
    }
};

class Dijkstra{
private:
    const type _INF = LLONG_MAX/10;
    int V; // 頂点数
    vector<type> d;
    vector<int> prev;
    vector<vector<Edge> > G;
public:
    Dijkstra(int v){
        V = v + 1;
        d.resize(V);
        G.resize(V);
        prev.resize(V);
    }
  
    void shortest_path(int s){
        priority_queue<P, vector<P>, greater<P>> que;
        d.assign(V, _INF);
        prev.assign(V, -1);
        d[s] = 0;
        que.push(P(0, s));
        while(que.size()){
            P p = que.top(); que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;
            for(auto e : G[v]){
                if(d[v] + e.cost < d[e.to]){
                    d[e.to] = d[v] + e.cost;
                    prev[e.to] = v;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
    
    void add_path(int from, int to, type cost){
        G[from].push_back(Edge(to, cost));
    }

    type get_cost(int t){
        return d[t];
    }

    bool enable_move(int t){
        return d[t] != _INF;
    }
    
    vector<int> get_path(int t){
        vector<int> path;
        for(; t != -1; t = prev[t])
            path.push_back(t);
        reverse(path.begin(), path.end());
        return path;
    }
};

int main(){
  ll n,m,s,t;
  cin>>n>>m>>s>>t;
  s--,t--;
  Dijkstra start(n),goal(n);
  ll u,v,a,b;
  for(int i=0;i<m;i++){
    cin>>u>>v>>a>>b;u--,v--;
    start.add_path(u,v,a);
    start.add_path(v,u,a);
    goal.add_path(u,v,b);
    goal.add_path(v,u,b);
  }
  
  start.shortest_path(s);
  goal.shortest_path(t);

  vector<ll> ans(n);
    ll min_num = LLONG_MAX;
    for(int i = n-1; i >= 0; i--){
      min_num = min(min_num, start.get_cost(i) + goal.get_cost(i));
        ans[i] = min_num;
    }
    const ll coin = 1e15;
    rep(i, n){
        cout << coin - ans[i] << endl;
    }
  return 0;
}


Measured memory_bytes: 19840
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define rrep(i, m, n) for(ll i = m; i > n; i--)
#define Rep(i, m, n) for(ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define lmax(x, y) max<ll>(x, y)
#define lmin(x, y) min<ll>(x, y)
#define tmax(x, y, z) lmax((x), lmax((y), (z)))
#define tmin(x, y, z) lmin((x), lmin((y), (z)))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ZERO(a) memset(a, 0, sizeof(a))
const ll INF = 1LL << 60;
const ll MM = 1000000000; const ll MOD = MM + 7; const ll MMM=9223372036854775807;//2^63 -1
#define ADD(a,b) a = (a + ll(b)) % MOD
#define MUL(a,b) a = (a * ll(b)) % MOD
ll GCD(ll x, ll y){ if(y == 0) return x; else return GCD(y, x % y);}
ll LCM(ll x, ll y){ return x / GCD(x, y) * y;}
template<class T> inline bool chmin(T& a, T b){ if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if(a < b){ a = b; return true;} return false;}
template<typename A, size_t N, typename T> void FILL(A (&array)[N], const T &val){ fill( (T*)array, (T*)(array+N), val );}
// struct edge{ ll from, to, cost; };
// struct edge{ ll to, cost; };
// using Graph = vector<vector<ll>>;  // 重み無し
// using Graph = vector<edge>;  // 辺
// using Graph = vector<vector<edge>>;  // 重み付き
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};


int main(){
    ll n, k, x, y; cin >> n >> k >> x >> y;
    if(n-k >= 0) cout << (n-k)*y + k*x << endl;
    else cout << n*x << endl;
}


Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,e,f,g,h,i,j;
    cin>>a;
    set<int>s;
    int ar[a];
    int ans=0,sum=0;
    c=0,d=0;
    for(i=0;i<a;i++)
    {
        cin>>ar[i];
        if(ar[i]>=3200)
        {
            c++;
        }
        else{
            s.insert(ar[i]/400);
        }
    }
    ans=s.size()+c;
    if(s.size()==0){sum=1;}
    else{sum=s.size();}

    cout<<sum<<" "<<ans<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
// using namespace __gnu_pbds;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define fr(x,in,n,r) for(x=in;x<n;x+=r)
#define ifr(x,n) for(ll x=0;x<n;x++)
#define dfr(x,n) for(ll x=n-1;x>=0;x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
typedef long long ll;
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen("input.txt","r",stdin);
    string s;
	cin>>s;
	ll x=(s[0]-'0')*10+(s[1]-'0');
	ll y=(s[2]-'0')*10+(s[3]-'0');
	if(x==0 && y==0){
		cout<<"NA\n";
		return 0;
	}
	if(x==0 && y>12){
		cout<<"NA\n";
		return 0;
	}
	if(x==0 && y<=12){
		cout<<"YYMM\n";
		return 0;
	}
	if(x>12 && y==0){
		cout<<"NA\n";
		return 0;
	}
	if(x<=12 && y==0){
		cout<<"MMYY";
		return 0;
	}
	if(x>12 && y>12){
		cout<<"NA\n";
		return 0;
	}
	if(x>12 && y<=12){
		cout<<"YYMM";
		return 0;
	}
	if(x<=12 && y>12){
		cout<<"MMYY";
		return 0;
	}
	if(x<=12 && y<=12){
		cout<<"AMBIGUOUS\n";
		return 0;
	}
	
	return 0;
}

Measured memory_bytes: 256
#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template <std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

#include <vector>
#include <iostream>
#include <functional>
using namespace std;
using mint = modint<998244353>;
int main() {
	int N;
	cin >> N;
	vector<mint> fact(N + 1), inv(N + 1), factinv(N + 1);
	fact[0] = 1, inv[1] = 1, factinv[0] = 1;
	for (int i = 1; i <= N; ++i) fact[i] = fact[i - 1] * i;
	for (int i = 2; i <= N; ++i) inv[i] = inv[mint::get_mod() % i] * mint(-int(mint::get_mod() / i));
	for (int i = 1; i <= N; ++i) factinv[i] = factinv[i - 1] * inv[i];
	function<mint(int, int)> comb = [&](int x, int y) {
		if (x < y || y < 0) return mint(0);
		return fact[x] * factinv[y] * factinv[x - y];
	};
	mint ans = 0, sum = mint(2).pow(N / 2);
	for (int i = N / 2; i <= N; ++i) {
		mint mul = comb(N, i);
		ans += mul * sum;
		sum *= 2;
		sum -= comb(i, N / 2);
	}
	cout << ans.get() << endl;
	return 0;
}

Measured memory_bytes: 117376
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
	vector<int> data;
	while(1){
		int n;
		cin >> n;
		if(n==0) break;
		data.clear();
		REP(i,n){
			int tmp;
			cin >> tmp;
			data.pb(tmp);
		}
		sort(All(data));
		ll int sum = 0;
		ll int ans = 0;
		REP(i,data.size()-1){
			sum += data[i];
			ans += sum;
		}
		cout << ans << endl;
 	}

 	return 0;
}

Measured memory_bytes: 1320
#pragma region Header
#ifdef DEBUG
#include "debug.hpp"
#else
#define NDEBUG
#define debug(...)
#define IFD(...)
#endif
#include <utility>
#include <cstdint>
#include <cmath>
#include <cassert>
#include <vector>
#include <array>
#include <deque>
#include <queue>
#include <stack>
#include <tuple>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <string>
#include <functional>
#include <algorithm>
#include <numeric>
#include <stdexcept>
#include <iostream>
#include <iomanip>
using namespace std;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
#define rep(i, n) for (int i = 0; i < static_cast<int>(n); ++i)
#define repd(i, n) for (int i = static_cast<int>(n) - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = static_cast<int>(a); i <= static_cast<int>(b); ++i)
#define FORD(i, b, a) for (int i = static_cast<int>(b); i >= static_cast<int>(a); --i)
template <typename T>
int SZ(T const &x)
{
    return static_cast<int>(x.size());
}
template <class T>
bool chmax(T &a, T const &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, T const &b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n;cin>>n;
    vector tr(n, vector<int>());
    vector fl(n, vector<int>());
    rep(i, n)
    {
        int a;cin>>a;
        rep(j,a)
        {
            int x,y;cin>>x>>y;
            --x;
            if (y == 1)
            {
                tr[i].push_back(x);
            }
            else
            {
                fl[i].push_back(x);
            }
        }
    }

    int res = 0;
    rep(syo, 1 << n)
    {
        bool flag = true;
        rep(i, n)
        {
            if (not ((syo >> i) & 1))
            {
                continue;
            }
            for (auto x : tr[i])
            {
                flag = flag && ((syo >> x) & 1);
            }
            for (auto x : fl[i])
            {
                flag = flag && not ((syo >> x) & 1);
            }
        }
        if (flag)
        {
            chmax(res, __builtin_popcount(syo));
        }
    }

    cout << res << endl;

    return 0;
}


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

using namespace std;


#define int             long long


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



  int n;
  cin >> n;
  int a[n];
  int dp[n];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  dp[1] = abs(a[1] - a[0]);
  //cout << dp[0];
  //cout << dp[1];
  for (int i = 2; i < n; i++) {
    // cout << dp[i];
    dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));

  }

  cout << dp[n - 1];



  return 0;
}

Measured memory_bytes: 5144
#include<bits/stdc++.h>
using namespace std;
int main(){
  int i,j=1,k=0,n;
  string s;
  cin>>n;
  for(i=0;i<n;i++){
    cin>>s;
    if(s=="A") k++;
    else k--;
    if(k<0) j=0;
  }
  cout << ((j&(k==0))?"YES":"NO") <<endl;
  return 0;
}

Measured memory_bytes: 3156
s = input()
n = len(s)

ans = 0

for i in range(2**(n-1)):
    k = 0
    for j in range(n-1):
        if ((i >> j) & 1):
            ans += int(s[k:j+1])
            k = j+1
    ans += int(s[k:])

print(ans)

Measured memory_bytes: 3060
import java.util.*;

class Main{
  public static void main(String[] args){
    try(Scanner sc = new Scanner(System.in)){
      int n = Integer.parseInt(sc.nextLine());
      int a = Integer.parseInt(sc.nextLine());
      int hage = n % 500;
      if(a >= hage){
        System.out.println("Yes");
      }else{
        System.out.println("No");
      }
    }
  }
}

Measured memory_bytes: 23124
#include<stdio.h>
int p[1000000];
int main(void){
	int i,j,k,a,l=1;
    p[0]=2;
    for(i=3;i<=999999;i+=2){
        k=1;
        for(j=0;j<l;j++){
            if(p[j]*p[j]>i)break;
            if(i%p[j]==0){
                k=0;
                break;
            }
        }
        if(k==1){
            p[l++]=i;
        }
    }
	while(scanf("%d",&a)!=EOF){
	    for(i=0;i<l;i++)if(p[i]>a)break;
	    printf("%d\n",i);
	}
	return 0;
}

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

using namespace std;

const int maxn=2e5+10;

int t,A,B,C,D,k,a,b;

bool check(int sl) {
	a=A-sl*k,b=B-sl;
	return b<=1ll*a*k;
}

int main() 
{
	scanf("%d",&t);
	while(t--) {
		scanf("%d%d%d%d",&A,&B,&C,&D);
		k=max(ceil(1.0*A/(B+1)),ceil(1.0*B/(A+1)));
		int l=0,r=A/k,ans=0;
		while(l<=r) {
			int mid=(l+r)>>1;
			if(check(mid)) {
				ans=mid;l=mid+1;
			}else r=mid-1;
		}
		check(ans);
	//	cout<<ans<<" "<<a<<" "<<b<<"*\n";
		int L=ans*(k+1)+a-(b/k);ans=L;
		for(int i=C;i<=D;++i) {
			if(i<=ans) {
				if(i%(k+1)==0) putchar('B');
				else putchar('A');
			}else {
				if((A+B-i+1)%(k+1)==0) putchar('A');
				else putchar('B');
			}
		}puts("");
	}
	return 0;
}

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		int S = new Scanner(System.in).nextInt();
		int h = S / 3600;
		int z = S - h * 3600;
		int m = z / 60;
		int s = z - m * 60;
		System.out.println(h + ":" + m + ":" + s);

	}
}


Measured memory_bytes: 26340