akhauriyash/Code-Regression huggingface dataset:
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        int h = sc.nextInt();
        int m = sc.nextInt();
        double ax = Math.cos(((double)h + (double)m / 60.0) / 12.0 * 2.0 * Math.PI) * a;
        double ay = Math.sin(((double)h + (double)m / 60.0) / 12.0 * 2.0 * Math.PI) * a;
        double bx = Math.cos((double)m / 60.0 * 2.0 * Math.PI) * b;
        double by = Math.sin((double)m / 60.0 * 2.0 * Math.PI) * b;

        double ans = Math.sqrt(Math.pow(Math.abs(ax-bx), 2.0) + Math.pow(Math.abs(ay-by), 2.0));

        System.out.println(ans);
    }




    private static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if (ptr < buflen) {
                return true;
            }else{
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            if (!hasNext()) throw new NoSuchElementException();
            long n = 0;
            boolean minus = false;
            int b = readByte();
            if (b == '-') {
                minus = true;
                b = readByte();
            }
            if (b < '0' || '9' < b) {
                throw new NumberFormatException();
            }
            while(true){
                if ('0' <= b && b <= '9') {
                    n *= 10;
                    n += b - '0';
                }else if(b == -1 || !isPrintableChar(b)){
                    return minus ? -n : n;
                }else{
                    throw new NumberFormatException();
                }
                b = readByte();
            }
        }
        public int nextInt() {
            long nl = nextLong();
            if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
            return (int) nl;
        }
        public double nextDouble() { return Double.parseDouble(next());}
    }
}


Measured memory_bytes: 33292
// in the name of Allah
// Ya Ali!
#include <bits/stdc++.h>
using namespace std;

typedef long long ll ;
const ll maxn = 111111 ;
const ll INF = 1e18 ;

#define f first
#define s second

ll n , w1 , w[maxn] , v[maxn] , dp[110][maxn] , sum = 0 ;

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

 cin >> n >> w1 ;
 for(int i = 1 ; i <= n ; i ++){
    cin >> w[i] >> v[i] ;
    sum += v[i] ;
 }

 for(int i = 1 ; i <= 1e5 + 20 ; i ++){
  dp[0][i] = INF ;
 }

 for(int i = 1 ; i <= n ; i ++){
    for(int j = 0 ; j <= 1e5 + 20 ; j ++){
     dp[i][j] = dp[i-1][j] ;
     if(j >= v[i])dp[i][j] = min(dp[i][j] , dp[i-1][j-v[i]]+w[i]) ;
    }
 }

 ll ans = 0 ;
 for(ll i = 0 ; i <= 1e5 + 20 ; i ++){
    if(dp[n][i] <= w1)ans = max(ans , i);
 }
 cout << ans ;

     return 0;
}


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

typedef long long ll;
using namespace std;

int main()
{
    string s;
    int k, i, c=0;

    cin >> s;
    cin >> k;

    for(i=0; i<s.size(); i++){
        if(s[i] != '1') break;
        c++;
    }

    if(k < i+1) cout << 1 << endl;
    else cout << s[i] << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
const long long N=1e5+10;
const long long big=1e18;

long long num[10000];
long long prime[10000];
long long cnt=0;

int main()
{
    long long x;
    cin>>x;
    for (long long i = 2; i <= x / i; i ++ )
    if (x % i == 0)
    {
        long long s = 0;
        while (x%i==0) x/=i,s ++;
        prime[cnt]=i,num[cnt++]=s;
    }
    if (x > 1) prime[cnt]=x,num[cnt++]=1;

    long long sum=0;

    for(long long i=0;i<cnt;i++)
    {
        long long x=num[i];
        long long cnt=0;
        for(cnt=1;;cnt++)
        {
            if(x<cnt)     break;
            x-=cnt;
        }
        sum+=cnt-1;
    }
    cout<<sum;
}


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

using namespace std;
#define ll long long
int num[10005];
int tnum[5];
int main()
{
    int n;
    cin >> n;
    int tn=100;

    for(int i=1;i<=100;i++)
    {
        for(int j=1;j<=i;j++)
        {
            for(int k=1;k<=j;k++)
            {
                int t=i*i+j*j+k*k+i*j+i*k+j*k;
                if(t<=10000)
                {
                    tnum[1]=i;
                    tnum[2]=j;
                    tnum[3]=k;
                    sort(tnum+1,tnum+4);
                    int tt=unique(tnum+1,tnum+4)-tnum-1;
                    if(tt==1)
                        num[t]+=1;
                    else if(tt==2)
                        num[t]+=3;
                    else
                        num[t]+=6;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
        printf("%d\n",num[i]);
    return 0;
}


Measured memory_bytes: 3684
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define eb          emplace_back
#define c(P)        cout<<P<<"\n"
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define F           first
#define S           second
#define mp(x,y)     make_pair(x,y) 
#define mem(a,val)  memset(a,val,sizeof(a))
#define fr(i,a,n)   for(ll i=a;i<n;i++)
#define frr(i,a,n)  for(ll i=n-1;i>=a;i--)


const ll N   =  405;
const ll mod = 1e9 + 7;

using namespace std;

ll dp[N][N] ; // dp[i][j] - the minimum cost of combining interval [i,j] into one vertex
void solve()
{
// string s;
  ll n,m,k,x=0,y=0,c=0,q,ans=0;
  cin>>n;
 std::vector<ll> a(n);
  for (auto &i : a)
  {
  cin>>i;
  }
  auto sum = [&](ll L , ll R)
  {
    ll s= 0;
    fr(i,L,R+1)
     s+=a[i];
     return s;
  };
  for (ll L= n-1; L >=0 ; --L)
  {
    for(ll R = L;R<n;R++)
    {
      if(L==R)
        dp[L][R] = 0;
      else
      {
        dp[L][R] = 1e15;
        c = sum(L,R);
        for (ll i = L; i <=R-1; ++i)
        {
          dp[L][R] = min(dp[L][R],dp[L][i]+ dp[i+1][R] + c);
        }
      }
    }
  }


  
  cout<<dp[0][n-1]<<"\n";
}

signed main(){
  
      ios_base::sync_with_stdio(false);   
      cin.tie(NULL);
      ll T=1;
      //cin>>T;
      while(T--)
      {
      solve();
      }
      return 0;
    }


Measured memory_bytes: 1536
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a[4];
	for(int i=1;i<=3;i++)cin>>a[i];
	sort(a+1,a+3+1);
	cout<<fabs(a[3]-a[2]+a[2]-a[1])<<endl;
	return 0;	
 } 

Measured memory_bytes: 256
n = int(input())
a=list(map(int,input().split()))

#print(a)

amin = min(a)
amax = max(a)
min_sum=10**8
for x in range(amin,amax+1):
    sum_for_x = sum([(y-x)**2 for y in a])
    if sum_for_x < min_sum:
        min_sum = sum_for_x

print(min_sum)   

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

#define INF 1e9
#define Inf 1e18
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
#define DEBUG

const int maxn=205;
const int mod=998244353;
int ans,n;
int x[maxn],y[maxn],t2[maxn];

bool coli(int i,int j,int k){return (y[j]-y[i])*(x[k]-x[j])==(y[k]-y[j])*(x[j]-x[i]);}

int main(){
#ifndef DEBUG
	freopen("my.in","r",stdin);
	freopen("my.out","w",stdout);
#endif
	scanf("%d",&n);
	t2[0]=1;
	REP(i,n)t2[i]=2LL*t2[i-1]%mod;
	ans=(t2[n]-n-1)%mod;
	if(ans<0)ans+=mod;
	rep(i,n)scanf("%d%d",&x[i],&y[i]);
	rep(i,n)for(int j=i+1;j<n;j++){
		int cnt=0;
		for(int k=j+1;k<n;k++)if(coli(i,j,k))cnt++;
		ans-=t2[cnt];
		if(ans<0)ans+=mod;
	}
	printf("%d",ans);
	return 0;
}

/*
Input:
-----------------
Output:
*/

Measured memory_bytes: 256
#include<stdio.h>

int main(void){
char S[5];
int f,e;

scanf("%s",S);
f=(S[0]-'0')*10+S[1]-'0';
e=(S[2]-'0')*10+S[3]-'0';

if((f<=12)&&(f>=1)){/*前がmm OK*/
   if((e<=12)&&(e>=1)){/*後ろがmmOK*/
      printf("AMBIGUOUS\n");
     }else{printf("MMYY\n");}
  }else{/*前がmmNG*/
   if((e<=12)&&(e>=1)){/*後ろがmmOK*/
      printf("YYMM\n");
     }else{printf("NA\n");}
  }


return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <bitset>
#include <cmath>

using namespace std;

int N,a,b,c,A,B,C;
int l[8];

int main(){
    int m = 10000;
    cin >> N >> A >> B >> C;
    for(int i=0;i<N;i++) cin >> l[i];
    for(int i=0;i<1<<(2*N);i++){
        bitset<16> bt(i);
        a = b = c = 0;
        int n = -30;
        for(int j=0;j<N;j++){
            if(bt[2*j] && bt[2*j+1]){
                a += l[j];
                n += 10;
            }
            if(!bt[2*j] && bt[2*j+1]){
                b += l[j];
                n += 10;
            }
            if(bt[2*j] && !bt[2*j+1]){
                c += l[j];
                n += 10;
            }
        }
        if(a == 0 || b == 0 || c == 0) continue;
        n += abs(A-a) + abs(B-b) + abs(C-c);
        m = min(n,m);
    }
    cout << m << endl;
    return 0;
}

Measured memory_bytes: 256
/*
 * Author   :   jignesh16
 * College  :   SVNIT
 */


#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

#define pb push_back
#define sz size()
#define ff first
#define ss second
#define ll long long int
#define ld long double
#define bp __builtin_popcountll
#define FOR(i,a,b) for(i=a;i<b;i++)
#define ROR(i,a,b) for(i=a;i>b;i--)
#define FORE(i,a,b) for(i=a;i<=b;i++)
#define RORE(i,a,b) for(i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
const int M=1e9+7;

int main()
{
    FAST;
    int n,i;
    string s;
    cin>>n>>s;
    for(i=0;i<s.sz-1;i++)
    {
        if(s[i]==s[i+1])
        {
            s.erase(i,1);
            i=i-1;
        }

    }
//    cout<<s<<"\n";
    cout<<s.sz;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  if (!K) return cout << (long) N * N << "\n", 0;
  long ans = 0;
  for (int i = 1; i <= N; i++) {
    if (i <= K) continue;
    ans += (long) N / i * (i - K);
    ans += max(0, N % i - K + 1);
  }
  cout << ans << "\n";
}

Measured memory_bytes: 3648
#include<stdio.h>

int get_info(void);

int main(void)
{
  int n,h,w,hi,wi,count=0,i;

n = get_info();
h = get_info();
w = get_info();

for(i=0;i<n;i++)
{
  scanf("%d",&hi);
  scanf("%d",&wi);

  if(hi-h>=0&&wi-w>=0)
  {
    count += 1; 
  }
}

printf("%d",count);

  return 0;
}


int get_info(void)
{
int n;

scanf("%d",&n);

return n;
}


Measured memory_bytes: 128
#include <stdio.h>
#include <string.h>
#include <iostream>
 
using namespace std;
 
int main()
{
    int n,k;
    scanf("%d",&n);
    scanf("%d",&k);
    for(int i = 0; i < k; i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        x--;
        y--;
        x = min(x,n - 1 - x);
        y = min(y,n - 1 - y);
        int l = min(x,y);
        printf("%d\n",l % 3 + 1);
    }
    return 0;
}

Measured memory_bytes: 3204
# coding: utf-8
N,M=map(int,input().split())
A=list(map(int,input().split()))

SA=sum(A)

ans=0

for i in range(N):
    if A[i]>=SA/(4*M):
        ans+=1

if ans>=M:
    print("Yes")
else:
    print("No")



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

using namespace std;

#define LL long long

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

    map<char, LL> mp;
    char lst[] = {'M', 'A', 'R', 'C', 'H'};
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        mp[s[0]]++;
    }
    
    LL ans = 0;
    for (int i = 0; i < 5; ++i) {
        for (int j = i + 1; j < 5; ++j) {
            for (int k = j + 1; k < 5; ++k) {
                ans += mp[lst[i]] * mp[lst[j]] * mp[lst[k]];
            }
        }
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())



long long calc(const string &s) {
	int r = 0;
	for (int i = 1; i < s.size(); i++) {
		if (s[i - 1] == s[i]) {
			r++;
			i++;
		}
	}
	return r;
}


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

	string s;
	int k;
	cin >> s >> k;

	if (s.size() == 1) {
		cout << k / 2 << endl;
		return 0;
	}

	long long ans = 1LL << 60;

	if (s.front() == s.back()){
		char c = s.front();
		long long x = 0, y = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] != c)break;
			x++;
		}
		for (int i = s.size() - 1; i >= 0; i--) {
			if (s[i] != c)break;
			y++;
		}

		if (x == s.size()) {
			cout << 1LL * x * k / 2 << endl;
			return 0;
		}

		string r = s.substr(x);
		string t = s.substr(0, s.size() - y);
		string mid = s.substr(x, s.size() - y - x);

		long long ans = calc(r) + calc(t) + calc(mid) * (k - 2) + (x + y) / 2 * (k - 1);

		cout << ans << endl;
	}
	else {
		long long ans = 0;
		for (int i = 1; i < s.size(); i++) {
			if (s[i-1] == s[i]) {
				ans++;
				i++;
			}
		}
		cout << ans * k << endl;
	}




	return 0;
}

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

int main() {
  int a,b,num1,num2;
  num1=num2=0;
  vector<int> v;
  cin >> a;
  for(int i=0;i<a;i++){
  	cin>>b;
    v.push_back(b);
  }
  sort(v.begin(),v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());

  cout << v.size() <<endl;
 }

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

using namespace std;

using ll = long long;

int main() {
    ll a, b, k;
    cin >> a >> b >> k;

    if (a > k) {
        a -= k;
    }
    else {
        k -= a;
        a = 0;
        if (b > k) {
            b -= k;
        }
        else b = 0;
    }
    
    cout << a << ' ' <<  b << endl;

    return 0;
}

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

//x - 100(10 - N) = R
int main(){
  int N, R;
  cin >> N >> R;
  if(N >= 10){
    cout << R;
  }else{
  cout << R + 100 * (10 - N) <<endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <map>

#define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++)
#define rep(i,n) reps(i,0,n)
using namespace std;
using ll = long long;

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

int main(){
    ll n,x;
    ll num;
    ll ans;
    cin >> n >> x;

    cout << 3*(n-gcd(n,x)) << endl;
    return 0;
}



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

int mod =1e9+7;
long long addself(long long a,long long b)
{
    if(a+b >= mod)
        return a+b-mod;
    return a+b;
}

vector<vector<long long>> matrixprod(vector<vector<long long>> &b ,vector<vector<long long>> &a)
{
    int i,j,k,n=a.size();
    vector<vector<long long>> v(n,vector<long long>(n,0));
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                v[i][j] = (v[i][j] + (b[i][k]*a[k][j])%mod)%mod;
            }
        }
    }
    return v;
}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,i,j,k;
    cin>>n>>k;
    vector<vector<long long >> ans(n,vector<long long>(n,0)),a(n,vector<long long> (n));

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>a[i][j];
            if(i==j)
                ans[i][j]=1;
        }
    }

    while(k>0)
    {
        if(k & 1)
            ans = matrixprod(ans,a);
        k=k>>1;
        a = matrixprod(a,a);
    }
    long long res=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            res = (res+ans[i][j])%mod;
        }
    }

    cout<<res<<endl;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k,i,n) for(ll i=k;i<n;++i)
int main(void){
    ll n;
    cin>>n;
    vector<ll>a(n);
    rep(0,i,n){
        cin>>a[i];
    }
    ll x=1,ans=0;
    rep(0,i,n){
        --x;
        x=a[x];
        ++ans;
        if(x==2){
            cout<<ans<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
}


Measured memory_bytes: 1024
def sumDig(n) :
    s = 0
    for c in str(n) :
        s += (ord(c) - ord('0'))
    return s
N, A, B = list(map(int, input().split()))
ans = 0
for i in range(1, N+1) :
    if A <= sumDig(i) <= B :
        ans += i
print(ans)


Measured memory_bytes: 2940
import java.util.*;
import java.io.*;
public class Main {

	public static void main(String[] args) throws Exception{
		int num = 998244353;

		// TODO Auto-generated method stub
 		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 		PrintWriter out = new PrintWriter(System.out);
 		StringTokenizer st = new StringTokenizer(bf.readLine());
 		int n = Integer.parseInt(st.nextToken());
 		int k = Integer.parseInt(st.nextToken());
 		int[] array = new int[n];
 		StringTokenizer st1 = new StringTokenizer(bf.readLine());
 		for(int j = 0;j<n;j++){
 			array[j] = Integer.parseInt(st1.nextToken());
 		}
 		long l = 1;
 		long r = 1000000000;
 		while(l<r){
 			long mid = (l+r)/2;
 			long count = 0;
 			for(int j = 0;j<n;j++){
 				count+=(array[j]-1)/mid;
 			}
 			if (count > k)
 				l = mid+1;
 			else
 				r = mid;
 		}
 		
 		
 		out.println(l);
	 		
 		out.close();
 		
 		
 		
 	}
}
 	
 
//StringJoiner sj = new StringJoiner(" "); 
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is

 		
 		
 		
 		
	




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


struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for (int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main() {

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

    UnionFind tree(N);

    int a, b;
    for (int i = 0;i<M ; i++) {
        cin >> a >> b;
        tree.unite(a-1,b-1);
    }


    int ans = -1;

    set<int> s;

    for (int i = 0;i<N ; i++) {
        int r = tree.root(i);
        if (s.find(r) == s.end()) {
            s.insert(r);
            ans++;
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 7436
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.parseInt;

/**
 * Marked Ancestor
 */
public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		String[] words;

		Main main = new Main();

		while ((line = br.readLine()) != null && !line.isEmpty()) {

			int N, Q;
			N = parseInt(line.substring(0, line.indexOf(' ')));
			Q = parseInt(line.substring(line.indexOf(' ') + 1));
			if ((N | Q) == 0) break;

			Node[] nodes = new Node[N + 1];
			for (int i = 0; i < nodes.length; i++) {
				nodes[i] = main.new Node(i);
			}

			nodes[1].p = nodes[0];
			for (int i = 2; i <= N; i++) {
				int p = parseInt(br.readLine());
				nodes[i].p = nodes[p];
			}

			nodes[1].marked = true;
			long sum = 0;
			for (int i = 0; i < Q; i++) {
				line = br.readLine();
				char o = line.charAt(0);
				int v = parseInt(line.substring(line.indexOf(' ') + 1));
				if (o == 'M') {
					nodes[v].marked = true;
				} else {
					Node node = nodes[v];
					while (node.id != 0 && !node.marked) {
						node = node.p;
					}
					sum += node.id;
				}
			}
			System.out.println(sum);
		} //end while
	} //end main

	class Node {
		int id;
		boolean marked = false;
		Node p;

		Node(int id) {
			this.id = id;
		}
	}
}

Measured memory_bytes: 69932
#include<iostream>

using namespace std;

const int MAX_N = 1000000;
bool isprime[MAX_N];

void init() {
    fill(isprime,isprime+MAX_N,true);
    isprime[0] = isprime[1] = false;
    for (int i = 2; i < MAX_N; i++) {
        if (isprime[i]) {
            for (int k = 2; i * k < MAX_N; k++) {
                isprime[i * k] = false;
            }
        }
    }
}

int nprimes(int n) {
    int res = 0;
    for (int i = 0; i <= n; i++) {
        res += isprime[i];
    }
    return res;
}

int main() {
    init();
    int n;
    while (cin >> n) {
        cout << nprimes(n) << "\n";
    }
}


Measured memory_bytes: 4044
N, M = map(int, input().split())
A = [None] * M
K = [None] * M
for i in range(M):
    a, b = map(int, input().split())
    C = map(int, input().split())
    A[i] = a
    K[i] = sum([2 ** (i - 1) for i in C])


INF = (1 << 32) - 1
dp = [INF] * (2 ** N)
dp[0] = 0

for i in range(M):
    k = K[i]
    a = A[i]
    for j in range(2 ** N):
        dp[j | k] = min(dp[j | k], dp[j] + a)

ans = dp[2 ** N - 1] if dp[2 ** N - 1] < INF else -1
print(ans)


Measured memory_bytes: 45160
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int maxn=1e5+2;
const LL MOD=1e9+7;

int n;
int A[maxn],T[maxn];
int h[maxn];
bool flag=1;
int main() {
	scanf("%d",&n);
	FOR (i,1,n) {
		scanf("%d",&T[i]);
		if (T[i]>T[i-1]) h[i]=T[i];
	}
	FOR (i,1,n) {
		scanf("%d",&A[i]);
	}
	for (int i=n;i>=1;--i) {
		if (A[i]>A[i+1])
			if (h[i]!=0&&h[i]!=A[i]) {
				flag=0;
				break;
			}
			else h[i]=A[i];
	}

	for (int i=1;i<=n;i++)
		if (T[i]<h[i]) { flag=0; break; }
	for (int i=1;i<=n;i++)
		if (A[i]<h[i]) { flag=0; break; }

	if (!flag) {
		puts("0");
	}
	else {
		LL ans=1;
		for (int i=1;i<=n;i++) if (!h[i])
			ans=ans*min(T[i],A[i])%MOD;
		cout<<ans<<endl;
	}
	return 0;
}



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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] A = Console.ReadLine().Split(' ') ;
            int total = 0;
            for (int i = 0; i < N; i++)
            {
                if (int.Parse(A[i]) % 2 == 1)
                {
                    total++;
                }
            }
            string ans = "YES";
            if (total % 2 == 1)
            {
                ans = "NO";   
            }

            Console.WriteLine(ans);
            Console.ReadLine();

        }
    }
}


Measured memory_bytes: 11736
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
    return x*f;
} 
#define MAXN (10000000+10)
int a[MAXN],f[MAXN];
int p[MAXN];
int main()
{
//  freopen("bzoj4282.in","r",stdin);
//  freopen(".out","w",stdout);

    int n;
    cin>>n;
    int siz=0;
    For(i,n) {
        a[++siz]=read();
        p[a[i]]=i;
    }
    int t=0,c=0;
    For(i,n) if(i==1|| p[i-1]<p[i]) c=max(c,++t);
    else t=1;
    cout<<n-c<<endl;
    return 0;
}

Measured memory_bytes: 3072
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter,mul
from fractions import gcd
from functools import reduce
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n=ii()
arr=limii()

cou=Counter(arr)
ans=0
for j in cou.values():
    if j>=2:
        ans+=cmb(j,2)

for i in arr:
    if cou[i]>2:
        print(ans-cmb(cou[i],2)+cmb(cou[i]-1,2))
    elif cou[i]==2:
        print(ans-1)
    else:
        print(ans)



Measured memory_bytes: 123608
from collections import deque
N = input()
d = [float('inf')] * N
m = [[] for i in xrange(N)]
Q = deque([])

def bfs(s):
    Q.append(s)
    d[s] = 0

    while(len(Q) != 0):
        u = Q.popleft()
        for v in m[u]:
            if( d[v] == float('inf')):
                d[v] = d[u] + 1
                Q.append(v)

if __name__ == '__main__':

    for i in range(N):
        v = map(int, raw_input().split())
        for j in v[2:]:
            m[i].append(j-1)
    
    bfs(0)

    for i in range(N):
        if d[i] == float('inf'):
            d[i] = -1
        print (i+1), d[i]

Measured memory_bytes: 6824
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main()
{
	int x, y, z;
	cin >> x >> y >> z;
	swap(x, y);
	swap(x, z);
	cout << x << " " << y << " " << z << endl;
}

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

int main() {
	// your code goes 
	long long int x;
	long long int n,d;
	cin>>n>>d;
	long int ans=0;
	for(long int i=0;i<n;i++)
	{
	    long long int y;
	    cin>>x>>y;
	    x*=x;
	    y*=y;
	    if(x+y<=d*d)
	    ans++;
	}
	cout<<ans<<endl;
	return 0;
}


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

#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long
#define endl '\n'
#define eps  1e-9
const double pi=acos(-1);
const int mod=1e9+7;
//____________________


int32_t main()
{ 
  io;
  
  int R,C,r,c;
  cin>>R>>C;
  cin>>r>>c;

  int tot=R*C;

  int paint=r*C + c*R - r*c;

  cout<<tot-paint;
  
 return 0;
}

Measured memory_bytes: 256
from sys import stdin
from collections import deque
input = stdin.readline


def main():
    n, m = map(int, input().split())

    nexts = [[] for i in range(n)]
    
    for _ in range(m):
        ai, bi = map(int, input().split())
        ai -= 1
        bi -= 1
        nexts[ai].append(bi)
        nexts[bi].append(ai)

    blues = [-1] * n # 1 is in blues
    blues[0] = 0 # 0 is not in blues
    checked = [0] * n

    red_deque = deque([0])

    while len(red_deque) > 0:
        rd = red_deque[0]
        red_deque.popleft()
        if checked[rd] == 0:
            checked[rd] = 1
            for nxt in nexts[rd]:
                if blues[nxt] == -1:
                    blues[nxt] = rd
                    red_deque.append(nxt)

    print("Yes")
    for blue in blues[1:]:
        print(blue + 1)
    

if __name__ == "__main__":
    main()

Measured memory_bytes: 93436
#include "stdio.h"
#include "math.h"
#include <algorithm>
using namespace std;

#define SUQARE_SIZE  100000

int block[SUQARE_SIZE];
int blockSize;
int n;

void updateArr(int arr[],int left,int num){
    for(int i = left;i < left + blockSize;++i){
        if(i >= n){
            break;
        }
        arr[i] += num;
    }
}

void update(int arr[],int l, int r,int value)
{
    // 左端
    if(l % blockSize != 0 && l != 0 && l < r){
        int blockNumber = l / blockSize;
        if(block[blockNumber] != 0){
            updateArr(arr,blockNumber * blockSize,block[blockNumber]);
            block[blockNumber] = 0;
        }
        do{
            arr[l] += value;
            l++;
        }while(l % blockSize != 0 && l < r);
    }
    // blockに該当する部分
    while ((l + blockSize) <= r)
    {
        block[l / blockSize] += value;
        l += blockSize;
    }
    if(l <= r){
        int blockNumber = r / blockSize;
        if(block[blockNumber] != 0){
            updateArr(arr,blockNumber * blockSize,block[blockNumber]);
            block[blockNumber] = 0;
        }
        // 右端
        do{
            arr[l] += value;
            l++;
        }while(l <= r);
    }
}

int query(int arr[],int index)
{
    int blockNumber = index / blockSize;
    if(block[blockNumber] != 0){
        return block[blockNumber] + arr[index];
    }
    else{
        return arr[index];
    }
}

int main(){
    int q,command;
    scanf("%d %d",&n,&q);
    int a[n];
    blockSize = sqrt(n);
    
    for(int i = 0;i < n;++i){
        a[i] = 0;
        block[i] = 0;
    }
    
    for(int i = 0;i < q;++i){
        scanf("%d",&command);
        if(command == 0){
            int l,r,value;
            scanf("%d %d %d",&l,&r,&value);
            l--;
            r--;
            update(a,l,r,value);
        }else{
            int index;
            scanf("%d",&index);
            index--;
            printf("%d\n",query(a, index));
        }
    }
}



Measured memory_bytes: 3448
using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;
using TextReader = System.IO.TextReader;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            var S = N.ToString().Sum(c => c - '0');
            return N % S == 0 ? "Yes" : "No";
        }


        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader(Console.In)).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader
    {
        private string[] line = Array.Empty<string>();
        private int linePosition;
        private TextReader textReader;
        public ConsoleReader(TextReader tr) { textReader = tr; }
        public int Int => int.Parse(String);
        public long Long => long.Parse(String);
        public double Double => double.Parse(String);
        public string String
        {
            get
            {
                if (linePosition >= line.Length)
                {
                    linePosition = 0;
                    line = textReader.ReadLine().Split();
                }
                return line[linePosition++];
            }
        }
        public class SplitLine
        {
            private string[] splited;
            public SplitLine(ConsoleReader cr) { splited = cr.textReader.ReadLine().Split(); }
            public int[] Int => String.Select(x => int.Parse(x)).ToArray();
            public long[] Long => String.Select(x => long.Parse(x)).ToArray();
            public double[] Double => String.Select(x => double.Parse(x)).ToArray();
            public string[] String => splited;
        }
        public SplitLine Split => new SplitLine(this);
        public class RepeatReader
        {
            ConsoleReader cr; int count;
            public RepeatReader(ConsoleReader cr, int count) { this.cr = cr; this.count = count; }
            public IEnumerable<T> Select<T>(Func<ConsoleReader, T> func) { for (int i = 0; i < count; i++) yield return func(cr); }
            public IEnumerable<string> String { get { for (int i = 0; i < count; i++) yield return cr.String; } }
            public IEnumerable<int> Int { get { for (int i = 0; i < count; i++) yield return cr.Int; } }
            public IEnumerable<double> Double { get { for (int i = 0; i < count; i++) yield return cr.Double; } }
        }
        public RepeatReader Repeat(int count) => new RepeatReader(this, count);
    }
}

Measured memory_bytes: 11348
#include<stdio.h>

int main(){
  int i,j,A[100],N,temp=0,count=0,minj;
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&A[i]);
  }

  for(i=0;i<N-1;i++){
    minj=i;
    for(j=i;j<N;j++){
      if(A[j] < A[minj]){
	minj=j;
	
      }
    }
    if(i!=minj){
      temp=A[i];
      A[i]=A[minj];
      A[minj]=temp;
      count++;
    }
  }
  for(i=0;i<N;i++){
    if(i>0)printf(" ");
    printf("%d",A[i]);
  }
  printf("\n");
  printf("%d\n",count);
  return 0;
}
      
  



Measured memory_bytes: 2104
#include <bits/stdc++.h>
 
using namespace std;
 
int main(){
    int n, k; cin >> n >> k;
    vector<int> h(n);
    for(int i = 0; i < n; i++){
        cin >> h[i];
    }
    
    //dp[i] denotes the minimum cost to reach ith stone
    // 0 < i < n - 1 are the stones
    //We are asked to dind the cost to reach last stone i.e dp[n - 1]
    //Initially there is infinite cost to reach ith stones
    vector<int> dp(n, INT_MAX);
  
    //To reach first stone there is no cost. SO dp[0] = 0
    dp[0] = 0;
   //To reach second stone the frog can jump from 0th stone costing dp[1] = abs(h[1] - h[0])
    dp[1] = abs(h[1] - h[0]);
    
    //For every other stones 2 <= i < n, the frog can jump from the (i - 1), (i - 2)...(i - k)th stones 
    for(int i = 2; i < n; i++){
      for(int j = i - 1, jump = 0; j  >= 0 && jump < k; j--, jump++){
        dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i]));
      }
    }
    //Print minm cost to reach (n - 1)th stone
    cout << dp[n - 1];
    return 0;
}

Measured memory_bytes: 4036
#include <bits/stdc++.h>
using namespace std;
class binary_indexed_tree {
  public:
    int n;
    vector<int> tree;
    binary_indexed_tree (int size) {
      n = size;
      tree.resize(n, 0);
    }
    int get (int x) {
      int ret = 0;
      for (; x >= 0; x &= x + 1, x--){
        ret += tree[x];
      }
      return ret;
    }
    void add (int x) {
      for (; x < n; x |= x + 1) {
        tree[x]++;
      }
    }
};
int n;
vector<int> a;
bool medianOrLess (int x) {
  long long inv = 0;
  binary_indexed_tree bit(2 * n + 1);
  int crr = n;
  bit.add(crr);
  for (int i = 0; i < n; i++) {
    if (a[i] >= x) {
      crr++;
    } else {
      crr--;
    }
    inv += bit.get(crr);
    bit.add(crr);
  }
  if (inv * 2 >= (long long) n * (n + 1) / 2) {
    return true;
  } else {
    return false;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int lb = 0;
  int ub = 1e9 + 1;
  while (ub - lb > 1) {
    int md = (lb + ub) >> 1;
    if (medianOrLess(md)) {
      lb = md;
    } else {
      ub = md;
    }
  }
  cout << lb << '\n';
  return 0;
}

Measured memory_bytes: 1420
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using pii = pair<int, int>;

//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;

void	solve()
{
	string t;
	cin >> t;

	for (char c : t) {
		if (c == '?')
			cout << 'D';
		else
			cout << c;
	}
	cout << endl;

}

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

	solve();
}


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

int N, x1, y1, x2, y2, X, Y;

bool CanReceive(int x, int y, int _X = X, int _Y = Y)
{
    return x == _X || y == _Y || x + y == _X + _Y || x - y == _X - _Y;
}

int main()
{
    cin >> N;
    
    for(int i = 0; i < N; ++i)
    {
        cin >> x1 >> y1 >> x2 >> y2 >> X >> Y;
        
        if((x1 == X && y1 == Y) || (x2 == X && y2 == Y))
            cout << 0 << endl;
        else if(CanReceive(x1, y1) && CanReceive(x2, y2))
            cout << 1 << endl;
        else if(CanReceive(x1, y1) || CanReceive(x2, y2))
            cout << 2 << endl;
        else if(CanReceive(x1, y1, x2, y2) ||
            CanReceive(x1, y2) || CanReceive(x2, y1) ||
            CanReceive(x1, y2 + x1 - x2) || CanReceive(x1, y2 - x1 + x2) ||
            CanReceive(x2, y1 + x1 - x2) || CanReceive(x2, y1 - x1 + x2) ||
            CanReceive(x1 + y1 - y2, y2) || CanReceive(x1 - y1 + y2, y2) ||
            CanReceive(x2 + y1 - y2, y1) || CanReceive(x2 - y1 + y2, y1) ||
            (!((x1 + y1 - x2 - y2) & 1) &&
            (CanReceive((x1 + x2 - y1 + y2) / 2, (x2 - x1 + y1 + y2) / 2) ||
             CanReceive((x1 + x2 + y1 - y2) / 2, (x1 - x2 + y1 + y2) / 2))))
            cout << 3 << endl;
        else
            cout << 4 << endl;
    }
}

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

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

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(1e5) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()

priority_queue<P> que;
vector<int> a;
int sum = 0;

void qpush(int k) {
    if (a[k] <= 0) return;
    if (a[k] > a[k - 1] && a[k] > a[k + 1]) {
        que.push({a[k] - sum, k});
    }
}

int main() {
    int n, l;
    cin >> n >> l;
    a.resize(n + 2, 0);
    REP(i, n) { cin >> a[i + 1]; }
    FOR(i, 1, n+1) { qpush(i); }
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        sum += l - p.first - sum;
        a[p.second] = 0;
        qpush(p.second - 1);
        qpush(p.second + 1);
    }
    cout << sum << endl;
    return 0;
}



Measured memory_bytes: 3784
N,K = [int(hoge) for hoge in input().split()]
A = [int(hoge) for hoge in input().split()]
#割り当てを考えた後は、n等分するのが最適→にぶたん？
NG = 0
OK = 10**9 + 100
#小数点以下切り上げ→最小となるOKのintを求める
def Maruta(L):#長さLにできるか
    Kaisu = sum([a//L - 1 +  bool(a%L) for a in A])
    return Kaisu<=K

while OK - NG > 1:
    Test = (OK + NG)//2
    if Maruta(Test):
        OK = Test
    else:
        NG = Test
print(OK)
    

Measured memory_bytes: 168432
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using namespace std;
using ll = long long;
using P = pair<int, int>;


int main(){
    ll l, r;
    cin >> l >> r;

    ll ans = 1e9;

    for(ll i = l; i <= min(r, l+5000); i++){
        for(ll j = l; j <= i; j++){
            if(i == j) continue;
            else ans = min(ans, (i*j)%2019);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int a[15],t;
int map[2][15];
stack<int>s[2];
void dfs(int g){
	if(g==10){
		t=1;
		return;
	}
	if(t==1)
	return;
	for(int i=0;i<2;i++){
		if(s[i].size()==0||s[i].top()<a[g]){
			s[i].push(a[g]);
			dfs(g+1);
			s[i].pop();
		}
	}
}
int main(){
	int loop;
	cin>>loop;
	while(loop--){
		for(int i=0;i<10;i++)
		cin>>a[i];
		t=0;
		for(int i=0;i<2;i++)
		while(!s[i].empty())
		s[i].pop();
		dfs(0);
		for(int i=0;i<2;i++)
		while(!s[i].empty())
		s[i].pop();
		if(t==1)
		cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
} 


Measured memory_bytes: 2996
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define mod 1000000007


int main() {
    int N; cin >> N;
    vector<vector<bool>> dp(N+1, vector<bool>(100000));
    dp[0][0] = 1;
    
    for(int i=0; i<N; i++){
        int s; cin >> s;
        for(int j=0; j<=100000; j++){
            if(dp[i][j]) dp[i+1][j] = true;
            if(j-s>=0 && dp[i][j-s]) dp[i+1][j] = true;
        }
    }
    
    for(int j=100000; j>=0; j--){
        if(j%10==0 || !dp[N][j]) continue;
        cout << j << endl;
        return 0;
    }
    
    cout << 0 << endl;
    return 0;
}


Measured memory_bytes: 1536
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
typedef long long ll;
const int inf = 100100100;



//conbinatin (2002,2) = 

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

  rep(i,n+1){
    rep(j,n+1-i){
      int k = n-i-j;
        if(10000*i+5000*j+1000*k == y) {
          cout << i << " " << j << " " << k  << endl;
          return 0;
        }
      }
    }
  cout << "-1 -1 -1" << endl;
}


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

int main() {
    int A, B, C;
    cin >> A >> B >> C;
    int diff = max(A, max(B, C)) * 2 - min(A, min(B, C)) - (A + B + C - max(A, max(B, C)) - min(A, min(B, C)));
    (diff % 2 == 0) ? (cout << diff / 2) : (cout << (diff + 3) / 2);
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define mpr make_pair
#define PB push_back
#define FS first
#define SC second
typedef pair < int , int > pii;
typedef long long LL;
const LL MOD=998244353ll;
int n,a[200005],sum[1000005],MX=5;
int nprm[1000005],mu[1000005];
int pct,prms[1000005];
int powM(int V,int T){
	int R=1;
	while(T){
		if(T&1) R=(LL)R*(LL)V%MOD;
		T>>=1;
		V=(LL)V*(LL)V%MOD;
	}
	return R;
}
int main(){
	int i,j,k;
	scanf("%d",&n);
	for(i=0;i<n;++i){
		scanf("%d",a+i);
		sum[a[i]]+=a[i];
		sum[a[i]]%=MOD;
		MX=max(MX,a[i]);
	}
	for(i=1;i<=MX;++i){
		for(j=i+i;j<=MX;j+=i){
			sum[i]+=sum[j];
			sum[i]%=MOD;
		}
	}
	mu[1]=1;
	for(i=2;i<=MX;++i){
		if(!nprm[i]){
			prms[pct++]=i;
			mu[i]=-1;
		}
		for(j=0;j<pct && i*prms[j]<=MX;++j){
			nprm[i*prms[j]]=1;
			if(!(i%prms[j])) break;
			mu[i*prms[j]]=-mu[i];
		}
	}
	int res=0;
	for(i=1;i<=MX;++i){
		int rv=powM(i,MOD-2ll);
		for(j=i;j<=MX;j+=i){
			int M=mu[j/i];
			if(!M) continue;
			int V=(LL)rv*(LL)sum[j]%MOD*(LL)sum[j]%MOD;
			if(M==-1)
				res=(res+MOD-V)%MOD;
			else if(M==1)
				res=(res+V)%MOD;
		}
	}
	for(i=0;i<n;++i){
		res+=MOD-a[i];
		res%=MOD;
	}
	printf("%lld\n",res*((MOD+1ll)/2ll)%MOD);
	return 0;
}

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

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

int main()
{
    ll N;
    cin >> N;
    vec A(N);
    REP(i,0,N) cin >> A[i];

    ll c = 1, d = 1;
    REP(i,0,N) {
        d *= 3;
        if(A[i] % 2 == 0) c *= 2;
    }
    PR(d-c);

    return 0;
}

/*



*/

Measured memory_bytes: 3632
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	MAX   = 100
	INFTY = 1 << 21
	NIL   = -1
)

const (
	WHITE = iota
	GRAY
	BLACK
)

type Graph [MAX][MAX]int

func (g *Graph) prim(n int) int {
	var (
		u, minv     = 0, 0
		d, p, color [MAX]int
	)

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

	for {
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && g[u][v] != INFTY {
				if d[v] > g[u][v] {
					d[v] = g[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += g[i][p[i]]
		}
	}

	return sum
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	var g Graph
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			sc.Scan()
			e, _ := strconv.Atoi(sc.Text())
			if e == -1 {
				g[i][j] = INFTY
				continue
			}
			g[i][j] = e
		}
	}

	return fmt.Sprint(g.prim(n))
}

func main() {
	fmt.Println(answer(os.Stdin))
}



Measured memory_bytes: 1224
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) x.begin(), x.end()


using namespace std;
const int MAXN = 1000005;
const int INF = 0x7f7f7f7f;
typedef long long ll;


int main() {
#ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    //freopen("../output.txt", "w", stdout);
#endif
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    rep(i, n){
        cin >> p[i];
    }
    sort(all(p));
    ll ans= 0;
    rep(i, k){
        ans += p[i];
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3628