akhauriyash/Code-Regression huggingface dataset:
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    Scanner sc = new Scanner(System.in);

    int M;
    int T;
    int P;
    int R;
    int[][] ps;
    int[] ss;
    int[] as;

    void run() {
        while (true) {
            M = sc.nextInt();
            T = sc.nextInt();
            P = sc.nextInt();
            R = sc.nextInt();
            if ((M | T | P | R) == 0) {
                break;
            }

            ps = new int[T + 1][P + 1]; // penaruty count
            ss = new int[T + 1]; // keikazikan
            as = new int[T + 1];

            Integer[] nums = new Integer[T];
            for (int i = 0; i < T; i++) {
                nums[i] = i + 1;
            }

            for (int i = 0; i < R; i++) {
                int m = sc.nextInt();
                int t = sc.nextInt();
                int p = sc.nextInt();
                int j = sc.nextInt();

                if (j == 0) {
                    ss[t] += m + ps[t][p] * 20;
                    as[t]++;
                } else {
                    ps[t][p] += 1;
                }
            }

            Arrays.sort(nums, new Comparator<Integer>() {
                @Override
                public int compare(Integer a, Integer b) {
                    if (as[a] != as[b]) {
                        return as[b] - as[a];
                    } else if (ss[a] != ss[b]) {
                        return ss[a] - ss[b];
                    } else {
                        return b - a;
                    }
                }
            });

            int idx = 0;
            boolean init = true;
            while (true) {
                if (init) {
                    init = false;
                    System.out.print(nums[idx]);
                    if (idx + 1 >= T) {
                        break;
                    }
                    if (as[nums[idx]] == as[nums[idx + 1]]
                            && ss[nums[idx]] == ss[nums[idx + 1]]) {

                        System.out.print("=");
                    } else {
                        System.out.print(",");
                    }
                } else {
                    System.out.print(nums[idx]);
                    if (idx + 1 >= T) {
                        break;
                    } else {
                        if (as[nums[idx]] == as[nums[idx + 1]]
                                && ss[nums[idx]] == ss[nums[idx + 1]]) {

                            System.out.print("=");
                        } else {
                            System.out.print(",");
                        }
                    }
                }
                idx++;
            }
            System.out.println();
        }
    }

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

Measured memory_bytes: 60764
#include<stdio.h>
int main(){
  int n,m=0,t,i,a;
  scanf("%d %d",&n,&t);
  for(i=0;i<t;i++){
    scanf("%d",&a);
    if(m<a)m=a;
  }
  m-=n-m;
  printf("%d\n",0<m-1?m-1:0);
  return 0;
}


Measured memory_bytes: 128
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

As = [0] * (n + 1)
Bs = [0] * (m + 1)
for i in range(n):
  As[i + 1] = A[i] + As[i]
for i in range(m):
  Bs[i + 1] = B[i] + Bs[i]

l = 0
r = n + m + 1
while r - l > 1:
  p = (l + r) // 2
  best = min(
    As[i] + Bs[p - i] for i in range(p + 1)
    if min(i, n) + min(p - i, m) == p
  )
  if best <= k:
    l = p
  else:
    r = p
print(l)

Measured memory_bytes: 118200
#include <iostream>
#include <cmath>
 
using namespace std;
 
int main(){
    int x=0,a=0,b=0;
    cin >> x >> a >> b;
    if(abs(x-a)>abs(x-b)){
        cout << "B" << endl;
    }else{
        cout << "A" << endl;
    }
    cerr << "エラー出力！！！！" << endl;
    return 0;
}

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

int main() {
    int m, f, r;

    while(true) {
	cin >> m >> f >> r;
	if(m == -1 && f == -1 && r == -1) {
	    break;
	}

	char grade;
	int sum = m + f;

	if(m == -1 || f == -1) {
	    grade = 'F';
	} else if(sum >= 80) {
	    grade = 'A';
	} else if(sum >= 65) {
	    grade = 'B';
	} else if(sum >= 50) {
	    grade = 'C';
	} else if(sum >= 30) {
	    if(r >= 50) {
		grade = 'C';
	    } else {
		grade = 'D';
	    }
	} else {
	    grade = 'F';
	}

	cout << grade << endl;
    }
}

Measured memory_bytes: 1152
fun main(args: Array<String>) {
    val (n, m) = readLine()!!.split(' ').map(String::toLong)
    if (1L < Math.abs(n - m)) {
        println(0)
        return
    }
    // val fact = (1L..Math.min(n, m)).reduce { acc, i -> acc * i % 1000000007L }
    val fact = (2L..Math.min(n, m)).fold(1L) { acc, i -> acc * i % 1000000007L }
    val sqfact = fact * fact % 1000000007L
    val ans = when {
        n < m -> sqfact * m
        n == m -> sqfact shl 1
        else -> sqfact * n
    }
    println(ans % 1000000007L)
}


Measured memory_bytes: 37916
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000000000000
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define pb push_back
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
struct edge{int to,cost;};
int gcd(int a,int b){
	if(b==0)return a;
	return gcd(b,a%b);
}
int lcm(int a,int b){
	return a/gcd(a,b)*b;
}
bool prime(int a){
	if(a==1)return false;
	for(int i=2;i*i<=a;i++){
		if(a%i==0)return false;
	}
	return true;
}
void init_fact(){
	kaijo[0]=1;
	for(int i=1;i<=2000000;i++){
		kaijo[i]=kaijo[i-1]*i;
		kaijo[i]%=MOD;
	}
}
int modpow(int a,int b){
	if(b==0)return 1;
	if(b%2)return modpow(a,b-1)*a%MOD;
	int memo=modpow(a,b/2);
	return memo*memo%MOD;
}
int comb(int a,int b){
	if(!kaijo[0])init_fact();
	return kaijo[a]*modpow(kaijo[a-b],MOD-2)%MOD*modpow(kaijo[b],MOD-2)%MOD;
}
int inv(int x){
	x=modpow(x,MOD-2);
	return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
	double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
	double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
	double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
	double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
	return tc*td<0&&ta*tb<0;
}
double dist(double ax,double ay,double bx,double by){
	return sqrt((bx-ax)*(bx-ax)+(ay-by)*(ay-by));
}
int m1,m2,d1,d2;
signed main(){
	cin>>m1>>d1>>m2>>d2;
	if(m1!=m2)puts("1");
	else puts("0");
	return 0;
}


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

typedef long long int ll;

int main()
{
    int th, ts, ah, as;
    cin>>th>>ts>>ah>>as;

    while(1)
    {
        ah -= ts;
        if(ah <= 0)
        {
            cout<<"Yes";
            break;
        }

        th -= as;
        if(th <= 0)
        {
            cout<<"No";
            break;
        }
    }
    
    return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define mod 1000000007
using namespace std;
int n,a[114514];
struct BIT{
	vector<int>bit;
	int n;
	BIT(int x){
		bit.resize(x,0);n=x;
	}
	int sum(int x){
		int s=0;
		while(x>0){
			s+=bit[x];
			x-=x&-x;
		}
		return s;
	}
	void add(int x,int y){
		while(x<=n){
			bit[x]+=y;
			x+=x&-x;
		}
	}
};
signed main(){
	cin>>n;
	rep(i,n)cin>>a[i];
	int ok=0,ng=mod;
	while(ng-ok>1){
		int mid=(ok+ng)/2;
		int b[114514];rep(i,n+1)b[i]=0;
		BIT bit(2*n+10);
		int sum=0;
		bit.add(n+1,1);
		rep(i,n){
			if(a[i]>=mid)b[i]+=1;
			else b[i]-=1;
			b[i+1]=b[i];
			b[i]+=n+1;
			sum+=bit.sum(b[i]);
			bit.add(b[i],1);
        }
        if(n%4==0||n%4==3){
		    if(sum>=(n+1)*n/2/2)ok=mid;
		    else ng=mid;
        }else {
            if(sum>(n+1)*n/2/2)ok=mid;
		    else ng=mid;
        }

	}
	cout<<ok<<endl;
}

Measured memory_bytes: 3432
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define All(x) (x).begin(),(x).end()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

int main() {
    ll A[6];
    rep(i,1){
        cin >> A[i];
    }
    //sort(A,A+);
    cout << A[0]*A[0]*A[0] << endl;
}

Measured memory_bytes: 888
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int a[3];
    cin>>a[0]>>a[1]>>a[2];
    sort(a,a+3);
    cout << a[0]  << " "<< a[1] << " " <<a[2] << endl;
    return 0;
}


Measured memory_bytes: 3092
use std::io::*;
use std::str::FromStr;
use std::cmp::min;

const INF: i64 = 1_i64 << 62;

fn func(x: i64, s: &Vec<i64>) -> (i64, i64) {
    match s.binary_search(&x) {
        Ok(i) => (s[i], s[i]),
        Err(i) => if i == 0 {
            (s[i], s[i])
        } else if i == s.len() {
            (s[i-1], s[i-1])
        } else {
            (s[i-1], s[i])
        }
    }
}

fn calc(x: i64, s: i64, t: i64) -> i64 {
    min((s - x).abs() + (t - s).abs(),
        (t - x).abs() + (t - s).abs())
}

fn main() {
    let a: usize = read();
    let b: usize = read();
    let q: usize = read();
    let s: Vec<i64> = (0..a).map(|_| read()).collect();
    let t: Vec<i64> = (0..b).map(|_| read()).collect();

    for _ in 0..q {
        let x: i64 = read();
        let (s1, s2) = func(x, &s);
        let (t1, t2) = func(x, &t);
        let mut ans = INF;
        ans = min(ans, calc(x, s1, t1));
        ans = min(ans, calc(x, s1, t2));
        ans = min(ans, calc(x, s2, t1));
        ans = min(ans, calc(x, s2, t2));
        println!("{}", ans);
    }
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


Measured memory_bytes: 7420
#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;

long long gcd(long long m, long long n) {
    
    if (m < n) std::swap(m, n);

    long long tmp;
    while (m % n != 0) {
        tmp = n;
        n = m % n;
        m = tmp;
    }

    return n;
}

long long lcm(long long m, long long n) {
    return m * n / gcd(m, n);
}

int main() {

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

    ll a, b;
    cin >> a >> b;

    int ans = 1;
    ll x = gcd(a, b);
    for (ll i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            ans++;
            while (x % i == 0) x /= i;
        }
    }
    cout << ans + (x > 1 ? 1 : 0) << endl;
    return 0;
}


Measured memory_bytes: 256
<?php

$stdin = preg_split('/ /', trim(fgets(STDIN)));
$n = $stdin[0];
$stdin = preg_split('/ /', trim(fgets(STDIN)));

$reverse_array = array_reverse($stdin);
for ($i = 0; $i < $n; $i++) {
    print "{$reverse_array[$i]}";
    if ($i + 1 != $n) {
        print ' ';
    }
}
print "\n";

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

int main()
{
  int a, b, c;
  cin >> a;
  cin >> b;
  cin >> c;
  if (a < b)
  {
    if (b < c)
    {

      cout << "Yes" << endl;
    }
    else
    {
      cout << "No" << endl;
    }
  }
  else
  {
    cout << "No" << endl;
  }
}


Measured memory_bytes: 3088
import math
a, b, x = map(int, input().split())

if a**2*b == x:
    print(0)
elif x > (a**2*b)/2:
    p = a/(2*(b-x/a**2))
    print(90-math.degrees(math.atan(p)))
else:
    p = 2*x/(a*b**2)
    print(90-math.degrees(math.atan(p)))


Measured memory_bytes: 38384
#define _GIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;


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

  cout << n / 3 << endl;

  return 0;
}

Measured memory_bytes: 3636
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;


int main()
{
  int N;cin >> N;
  int a = 0;
  bool ok = true;
  FOR(i,0,N) {
    string s;cin >> s;
    if(s[0]=='A') a++;
    else {
      a--;
      if(a < 0) ok = false;
    }
  }
  if(a != 0) ok = false;
  puts(ok?"YES":"NO");
  return 0;
}

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

using namespace std;

int main (void) {
    int A, B, X;
    cin >> A >> B >> X;

    if ( A <= X && X <= A + B ) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }

    return 0;
}


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

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353 
#define MEM_SIZE 500010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()


template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T >
struct BinaryIndexedTree {
  vector< T > data;

  BinaryIndexedTree(int sz) {
    data.assign(++sz, 0);
  }

  T sum(int k) {
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for(++k; k < data.size(); k += k & -k) data[k] += x;
  }
};

void solve(void)
{
  int n,k;
  cin>>n>>k;
  vector<int> a1 (n+1,0);
  for (int i = 0; i < n; i++)
  {
    cin>>a1[i+1];
  }
  n++;
  vector<int> b(n,0);
    for (int i = 0; i < n-1; i++)
  {
    a1[i+1] += a1[i];
  }
  
  for (int i = 0; i < n; i++)
  {
    b[i] = a1[i] -i*k;
  }


  auto c = b;
  sort(ALL(c));
  vector<int> d;
  d.push_back(c[0]);
  map<int,int> mp;
  mp[c[0]] = 1;
  int cnt = 2;
  for (int i = 1; i < n; i++)
  {
    if(d.back() == c[i])continue;
    else 
    {
      d.emplace_back(c[i]);
      mp[c[i]] = cnt;
      cnt++;
    }
  }
  
  BinaryIndexedTree<int> BT(MEM_SIZE);
  int res = 0;
  // BT.add(0 + MEM_SIZE/2,1);
  for (int i = 0; i < n; i++)
  {
    int x = mp[b[i]];
    // DEBUG(b[i],x);

    res += BT.sum(x +MEM_SIZE/2);
    // DEBUG(res);
    BT.add(x+ MEM_SIZE/2,1);
  }
  cout<<res<<endl;
  return ;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


Measured memory_bytes: 25984
while True:
	pt=map(int,raw_input().split(" "))
	if pt[0]==-1 and pt[1]==-1 and pt[2]==-1:
		break
	elif pt[0]==-1 or pt[1]==-1:
		print "F"
	elif pt[0]+pt[1]>=80:
		print "A"
	elif 65<=pt[0]+pt[1]<80:
		print "B"
	elif 50<=pt[0]+pt[1]<65:
		print "C"
	elif 30<=pt[0]+pt[1]<50 and pt[2]<50:
		print "D"
	elif 30<=pt[0]+pt[1]<50 and pt[2]>=50:
		print "C"
	elif pt[0]+pt[1]<30:
		print "F"

Measured memory_bytes: 6448
#include <iostream>
#include <string.h>
#include <bits/stdc++.h>
#include<algorithm> 
using namespace std;
// main() is where program execution begins.
// long int X = 0;
// long int Y = 0;
long long int dp[3000][3000];

long int rslt(int i, int j, long int a[], bool flg){
		if(i > j) return 0;
		if(dp[i][j] != 0) return dp[i][j];
		if(flg) return dp[i][j] = max(a[i] + rslt(i+1, j, a, !flg), a[j] + rslt(i, j-1, a, !flg));
		else return dp[i][j] = min(-a[i] + rslt(i+1, j, a, !flg), -a[j] + rslt(i, j-1, a, !flg));
		
}


int main() {
int N;
cin >> N;
long int a[N];
for (int i = 0; i < N; ++i)
{
	cin >> a[i];
}
	cout << rslt(0, N-1, a, true);
}

Measured memory_bytes: 70272
object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val a, b = sc.nextLong

  def xor(l: Long): Long =
    if (l % 2 == 0) (l / 2) % 2 + l
    else ((l + 1) / 2) % 2

  println(xor(a - 1) ^ xor(b))
}

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

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
	
	int red, green, blue, k, temp=0, count=0;
	cin>> red >> green >> blue >> k;
	temp = k;
	for(int i=1; i<=k; i++){
		if(red>=green){
			green = green*2;
			temp--;
		}else{
			break;
		}
	}
	for(int i=1; i<=temp; i++){
		if(green >= blue){
			blue = blue*2;
		}else{
			break;
		}
	}
	if(blue>green && green>red){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}



	/*// G > R
	// B > G
	while(red >= green){
		green = green*2;
		count++;
	}
	while(green >= blue){
		blue = blue*2;
		count++;
	}
	if(count == k && blue>green && green>red){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}*/

	
	return 0;
}

Measured memory_bytes: 3636
N = int(input())

count = 0
for i in range(1, N+1, 2):
    yakusuu_count = 0
    for j in range(1, i+1):
        if i % j == 0:
            yakusuu_count += 1
    if yakusuu_count == 8:
        count += 1
print(count)


Measured memory_bytes: 3060
#include <iostream>
using namespace std;
int main()
{
  int N, M, C;
  cin >> N >> M >> C;
  int *B = new int[M];
  int *A = new int[N*M];
  for (int i = 0; i < M; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> A[i*M+j];
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int sum = C;
    for (int j = 0; j < M; j++) {
      sum += A[i*M+j]*B[j];
    }
    if (sum > 0) ans++;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
N, X = map(int, input().split())
lst = [abs(X - int(x)) for x in input().split()]

def gcd(x, y):
    return y if x % y == 0 else gcd(y, x % y)

y = lst[0]
for x in lst[1:]:
    y = gcd(max(x, y), min(x, y))
print(y)

Measured memory_bytes: 14240
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef pair<int,int> P;
const int SQ=200;
set<P> es,C;
map<int,int> G[40000];
int N,M;
int a[40000],b[40000],c[40000];
int par[40000];
void init(int N){rep(i,N) par[i]=i;}
int find(int x){
	if(par[x]==x) return x;
	return par[x]=find(par[x]);
}
bool same(int x,int y){
	return find(x)==find(y);
}
void unite(int x,int y){
	x=find(x),y=find(y);
	par[y]=x;
}
int vis[40000];
bool dfs(int v,int t,int I){
	if(v==t) return 1;
	vis[v]=I;
	bool b=0;
	for(auto p:G[v]){
		int u=p.fs;
		if(vis[u]!=I) b|=dfs(u,t,I);
	}
	return b;
}
int main(){
	cin>>N>>M;
	rep(i,M) cin>>a[i]>>b[i]>>c[i];
	rep(i,M){
		if(i%SQ==0){
			es.clear();
			rep(j,i){
				if(a[j]==1) es.insert(P(b[j],c[j]));
				if(a[j]==2) es.erase(P(b[j],c[j]));
			}
			C.clear();
			for(int j=i;j<min(M,i+SQ);j++){
				if(a[j]==2) C.insert(P(b[j],c[j]));
			}
			init(N);
			for(P e:es) if(C.count(e)==0) unite(e.fs,e.sc);
			rep(j,N) G[j].clear();
			for(P e:es) G[find(e.fs)][find(e.sc)]++,G[find(e.sc)][find(e.fs)]++;
		}
		int x=find(b[i]),y=find(c[i]);
		if(a[i]==1) G[x][y]++,G[y][x]++;
		if(a[i]==2){
			G[x][y]--,G[y][x]--;
			if(G[x][y]==0){
				G[x].erase(y);
				G[y].erase(x);
			}
		}
		if(a[i]==3){
			if(dfs(x,y,i+1)) puts("YES");
			else puts("NO");
		}
	}
}

Measured memory_bytes: 5260
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define rep1(i,n) for (int (i) = 1; (i) <= (n); i++)
typedef long long int llint;

using namespace std;

const llint inf = 1 << 29 ;

int main(void) {
	int n, m;
	cin >> n >> m;

	vector<int> a(m);
	vector<int> keys;
	rep(i, m) {
	  int b;
		cin >> a[i] >> b;

	  int bs = 0;
		rep(j, b) {
		  int c;
			cin >> c;
			c--;
			bs |= 1 << c;
		}
		
		keys.push_back(bs);
	}

	vector<llint> dp(pow(2,n),inf);
	dp[0] = 0;
	rep(i, pow(2,n)) rep(j, m) {

	 if ((i | keys[j]) <= pow(2,n) && dp[i] + a[j] < dp[i | keys[j]])  dp[i | keys[j]] = dp[i] + a[j];
	}

	llint ans;
	if ((ans = dp[pow(2, n) - 1]) == inf) cout << -1 << endl;
	else cout << dp[pow(2,n)-1] << endl;
}
  

Measured memory_bytes: 256
import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val w = sc.nextInt()
  val h = sc.nextInt()
  val n = sc.nextInt()
  val xya = (1..n).map { Triple(sc.nextInt(), sc.nextInt(), sc.nextInt()) }


  val array = Array(h) { Array(w) { false } }
  for (i in xya) {
    val x = i.first
    val y = i.second
    val a = i.third
    if (a == 1) {
      for (yi in 0 until h) {
        val ai = array[yi]
        for (xi in 0 until x) {
          ai[xi] = true
        }
      }
    } else if (a == 2) {
      for (yi in 0 until h) {
        val ai = array[yi]
        for (xi in x until w) {
          ai[xi] = true
        }
      }
    } else if (a == 3) {
      for (yi in 0 until y) {
        val ai = array[yi]
        for (xi in 0 until w) {
          ai[xi] = true
        }
      }
    } else if (a == 4) {
      for (yi in y until h) {
        val ai = array[yi]
        for (xi in 0 until w) {
          ai[xi] = true
        }
      }
    }
  }


  var count = 0
  for (ai in array) {
    for (aii in ai) {
      if (aii) count++
    }
  }

  val total = h * w
  println(total - count)

}

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

//{ START
using namespace std;
#define int int64_t
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define reps(i, a, n) for (int i = (a); i > (n); --i)
#define arep(i, x) for (auto &&i : (x))
#define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
//降順はgreater<T>()
#define all(x) (x).begin(), (x).end()
#define rv(s) reverse((s).begin(), (s).end())
//gcd lcmはそのままok
#define gcd(a, b) __gcd(a, b)
#define bits(n) (1 << (n))
#define pcnt(x) __builitin_popcountll(x)
//配列内等要素削除
#define unique(x) (x).erase(unique(x).begin(),(x).end()),(x).end());
#define Fixed(n) fixed << setprecision(n)
//総和
#define sowa(n) ((n) * ((n) + 1)) / 2
#define pb(x, a) (x).push_back(a)
#define fb(x, a) (x).flont_back(a)
#define eb(x, a) (x).emplaes_back(a)
#define F first
#define S second
#define cauto (const auto &)
using P = pair<int, int>;
template <class A, class B>
using pii = vector<pair<A, B>>;
template <class T> //昇順
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> //降順
using max_heap = priority_queue<T>;
template <class A, class B>
using umap = unordered_map<A, B>;
template <class A>
using uset = unordered_set<A>;
template <class T>
bool chmax(T &a, const T &b) //最大値更新 返り値はbool
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) //最小値更新 返り値はbool
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;
//} END

signed main(void)
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    cout<<sowa(n-1)<<'\n';
    return 0;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;
int n;

bool exist(int h) {
    if (n < 7 * h) {
        return false;
    } else if ((n - 7 * h) % 4 == 0) {
        return true;
    } else {
        h++;
        return exist(h);
    }
    return false;
}

int main() {
    cin >> n;
    if (exist(0)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}


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

struct PrimalDual{
  const int INF = 1<<28;
  typedef pair<int,int> P;
  struct edge{
    int to,cap,cost,rev;
    edge(){}
    edge(int to,int cap,int cost,int rev):to(to),cap(cap),cost(cost),rev(rev){}
  };

  int n;
  vector<vector<edge> > G;
  vector<int> h,dist,prevv,preve;

  PrimalDual(){}
  PrimalDual(int sz):n(sz),G(sz),h(sz),dist(sz),prevv(sz),preve(sz){}
  
  void add_edge(int from,int to,int cap,int cost){
    G[from].push_back(edge(to,cap,cost,G[to].size()));
    G[to].push_back(edge(from,0,-cost,G[from].size()-1));
  }

  int flow(int s,int t,int f){
    int res=0;
    fill(h.begin(),h.end(),0);
    while(f>0){
      priority_queue<P,vector<P>,greater<P> > que;
      fill(dist.begin(),dist.end(),INF);
      dist[s]=0;
      que.push(P(0,s));
      while(!que.empty()){
        P p=que.top();que.pop();
        int v=p.second;
        if(dist[v]<p.first) continue;
        for(int i=0;i<(int)G[v].size();i++){
          edge &e=G[v][i];
          if(e.cap>0&&dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
            dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
            prevv[e.to]=v;
            preve[e.to]=i;
            que.push(P(dist[e.to],e.to));
          }
        }
      }
      if(dist[t]==INF) return -1;
      for(int v=0;v<n;v++) h[v]+=dist[v];
      int d=f;
      for(int v=t;v!=s;v=prevv[v]){
        d=min(d,G[prevv[v]][preve[v]].cap);
      }
      f-=d;
      res+=d*h[t];
      for(int v=t;v!=s;v=prevv[v]){
        edge &e=G[prevv[v]][preve[v]];
        e.cap-=d;
        G[v][e.rev].cap+=d;
      }
    }
    return res;
  }
};

map<int,int>M;

int main(){
  int n;
  cin>>n;
  int a[n],b[n];
  vector<int>v;
  r(i,n){
    cin>>a[i]>>b[i];
    v.push_back(a[i]);
    v.push_back(b[i]);
  }
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  int m=v.size();
  r(i,m)M[v[i]]=i;
  PrimalDual p(n+m+2);
  int src=n+m;
  int sink=n+m+1;
  r(i,n)p.add_edge(src,i,1,0);
  r(i,m)p.add_edge(n+i,sink,1,0);
  r(i,n){
    p.add_edge(i,M[a[i]]+n,1,-b[i]);
    p.add_edge(i,M[b[i]]+n,1,-a[i]);
    p.add_edge(i,sink,1,0);
  }
  cout<<-p.flow(src,sink,n)<<endl;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> l(n);
  rep(i, n) cin >> l[i];
  sort(l.begin(), l.end());

  ll ans = 0;
  for (int i = 0; i < n - 2; i++) {
    for (int j = i + 1; j < n - 1; j++) {
      int r = lower_bound(l.begin(), l.end(), l[i] + l[j]) - l.begin();
      int l = j + 1;
      ans += max(0, r - l);
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(X, S, E) for (int(X) = (S); (X) < (E); ++(X))
#define rrep(X, S, E) for (int(X) = (E)-1; (X) >= (S); --(X))
#define itrep(X, Y) for (auto(X) = (Y).begin(); (X) != (Y).end(); (X)++)
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define print(x) cout << x << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef priority_queue<ll, vl> decendingQueue;              //降順
typedef priority_queue<ll, vl, greater<ll>> ascendingQueue; //昇順
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll INF = 9 * 1e18;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

void solve(long long N, long long M, std::vector<long long> S,
           std::vector<long long> T) {
  vector<vector<mint>> dp(N + 1, vector<mint>(M + 1, 0));
  dp[0][0] = 1; //空集合
  rep(i, 0, N + 1) {
    rep(j, 0, M + 1) {
      if (i - 1 >= 0) {
        dp[i][j] += dp[i - 1][j];
      }

      if (j - 1 >= 0) {
        dp[i][j] += dp[i][j - 1];
      }

      if (i - 1 >= 0 && j - 1 >= 0 && S[i - 1] == T[j - 1]) {
        dp[i][j] += dp[i - 1][j - 1];
      }

      if (i - 1 >= 0 && j - 1 >= 0) {
        dp[i][j] -= dp[i - 1][j - 1];
      }
    }
  }
  cout << dp[N][M].x << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> S(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &S[i]);
  }
  std::vector<long long> T(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &T[i]);
  }
  solve(N, M, std::move(S), std::move(T));
  return 0;
}


Measured memory_bytes: 31616
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
#include <cstdint>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, N) for(int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000
#define MAX 200001
#define PI 3.141592653589793

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const ll MOD = 1000000007;

template <typename T > inline string toString(const T &a) {ostringstream oss; oss << a; return oss.str();};

int par[1000000];

void init(int N){
    rep(i,N) par[i] = -1;
}

int find(int a){
    if(par[a] < 0) return a;
    else{
        return par[a] = find(par[a]);
    }
}

void unite(int a, int b){
    a = find(a);
    b = find(b);
    if(a == b) return;
    else{
        if(par[a] > par[b]) swap(a,b);
        par[a] += par[b];
        par[b] = a;
    }
}

int size(int A) {
     return -par[find(A)];
}

bool same(int a, int b){
    return find(a) == find(b);
}
int main(){
    int N,M;
    cin >> N >> M;
    init(N);
    rep(i,M){
        int A,B;
        cin >> A >> B;
        A--;B--;
        unite(A,B);
    }
    set<int> s;
    rep(i,N){
        s.insert(find(i));
    }
    cout << s.size() -1 << endl;
}

Measured memory_bytes: 7940
import scala.io.StdIn._
import scala.collection._

object Main {

  class Sieve(val n: Int){
    private[this] val top = (n - 3) / 2 + 1
    private[this] val sqrtLimit = (Math.sqrt(n).toInt - 3) / 2

    private[this] val cmpsts = new Array[Int]((top >>> 5) + 1)
    def isPrime(ci: Int): Boolean = (cmpsts(ci >>> 5) & (1 << (ci & 31))) == 0
    def notPrime(ci: Int): Unit = cmpsts(ci >>> 5) |= 1 << (ci & 31)

    def next(i: Int): Int = if((i > top) || isPrime(i)) 2 * i + 3 else next(i + 1)

    def exec: Iterator[Int] = {

      for{
        i <- 0 to sqrtLimit
        if isPrime(i)
        p = 2 * i + 3
        j <- Range((p*p-3)/2, top, (2 * i + 3))
      } notPrime(j)

      /*
      (0 to sqrtLimit).map{ i =>
        val p = 2 * i + 3
        if(isPrime(i))Range((p*p-3)/2, top, p).map{notPrime}
      }
      */
/*
      (0 to sqrtLimit).withFilter(isPrime).map { i =>
        val p = 2 * i + 3
        Range((p*p-3)/2, top, p).map(notPrime)
      }
*/
      if(n==2)Iterator.single(2)
      else Iterator.single(2) ++ Iterator.iterate(3)(p => next((p - 1) / 2)).takeWhile(_ <= n)
    }
  }

  def loop(s: String){
    if(s != null){
       val Array(n,_*) = s.split(" ").map(_.toInt)
       if(n<2) println(0)
       else println(new Sieve(n).exec.size)
       loop(readLine)
    }
  }

  def main(args: Array[String]): Unit = loop(readLine)
}

Measured memory_bytes: 68860
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll=long long;
#define rep(i,a,b) for(ll i=a;i<ll(b);i++)
#define repr(i,a,b) for(ll i=a;i>=ll(b);i--)
#define endl "\n"
#define ALL(x) x.begin(),x.end()
#define ALLR(x) x.rbegin(),x.rend()
#define INF 1e9
#define DEBUG(x) cout<<"debug: "<<x<<endl

ll x, y;
int main() {
    cin >> x >> y;
    int ans = 1;
    rep(i, x, y+1){
        x *= 2;
        if(x>y) break;
        ans++;
    }
    cout << ans << endl;
    return 0;
}


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

int combination(int X){ //組み合わせの数を求める関数
  return X*(X-1)/2;
}

int main(){
  ll n; 
  cin >> n;
  vector<ll>A(200010);
  vector<ll>B(200010);
  ll ans = 0;
  rep(i,n){
    cin >> A[i];
    ans += B[A[i]];
    B[A[i]]++;
  }
  rep(i,n){
    cout << ans - B[A[i]] + 1 << endl;
  }
}

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

typedef long long lint;

const lint INF = 1ll << 30;
inline void chmin(lint &a, lint b){a = min(a, b);}

struct Edge {
	int to, cost;
	Edge(int to, int cost): to(to), cost(cost){}
};

int n, m;
vector<Edge> lg[200];
vector<Edge> sg[200];
int r, z[1000];

lint solve()
{
	static lint ldist[200][200];
	static lint sdist[200][200];
	static lint dp[1001][200];
	
	fill_n(*ldist, 40000, INF);
	fill_n(*sdist, 40000, INF);
	for (int i = 0; i < n; i++) ldist[i][i] = sdist[i][i] = 0;
	for (int i = 0; i < n; i++){
		for (auto &e : lg[i]) chmin(ldist[i][e.to], e.cost);
	}
	for (int i = 0; i < n; i++){
		for (auto &e : sg[i]) chmin(sdist[i][e.to], e.cost);
	}
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				chmin(ldist[i][j], ldist[i][k] + ldist[k][j]);
				chmin(sdist[i][j], sdist[i][k] + sdist[k][j]);
			}
		}
	}
	
	fill_n(*dp, 1000 * 200, INF);
	dp[0][z[0]] = 0;
	for (int i = 0; i < r - 1; i++){
		int src = z[i], dst = z[i + 1];
		for (int j = 0; j < n; j++){
			chmin(dp[i + 1][j], dp[i][j] + ldist[src][dst]);
			for (int k = 0; k < n; k++){
				lint cost = ldist[src][j] + sdist[j][k] + ldist[k][dst];
				chmin(dp[i + 1][k], dp[i][j] + cost);
			}
		}
	}
	
	return *min_element(dp[r - 1], dp[r]);
}

int main()
{
	while (scanf("%d %d", &n, &m), n){
		for (int i = 0; i < 200; i++){
			lg[i].clear();
			sg[i].clear();
		}
		for (int i = 0; i < m; i++){
			int x, y, t;
			string sl;
			cin >> x >> y >> t >> sl;
			auto &G = (sl == "L") ? (lg) : (sg);
			x--; y--;
			G[x].push_back(Edge(y, t));
			G[y].push_back(Edge(x, t));
		}
		scanf("%d", &r);
		for (int i = 0; i < r; i++){
			scanf("%d", z + i);
			z[i]--;
		}
		
		printf("%lld\n", solve());
	}
}

Measured memory_bytes: 3640
<?php

class In {

	private $buf = [];
	private $count = 0;
	private $pointer = 0;

	public function nextLine() {
		$ret = '';
		if($this->hasNext()){
			while($this->hasNext()){
				$ret .= $this->next();
			}
		}else{
			$ret = trim(fgets(STDIN));
		}

		return $ret;
	}

	public function next() {
		if(!$this->hasNext()) {
			$str = trim(fgets(STDIN));
			$this->buf = explode(' ',$str);
			$this->count = count($this->buf);
			$this->pointer = 0;
		}

		return $this->buf[$this->pointer++];
	}

	public function hasNext() {
		return $this->pointer < $this->count;
	}

	public function nextInt() {
		return (int)$this->next();
	}

	public function nextDouble() {
		return (double)$this->next();
	}
}

class Out {
	public function println($str = '') {
		echo $str . PHP_EOL;
	}
}

$in = new In();
$out = new Out();

$N = $in->nextInt();

if ((int)($N/100) === $N%10) {
	$out->println('Yes');
} else {
	$out->println('No');
}


?>


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

int main(void){
  int h,w;
  int n;
  vector< int > a;
  int res[110][110];
  cin >> h >> w;
  cin >> n;
  a.resize(n);
  for(int i = 0;i < n;++i){
    cin >> a[i];
  }
  int now = 0,cnt = 0;
  for(int i = 0;i < h;++i){
    for(int j = 0;j < w;++j){
      if(i % 2 == 0){
        res[i][j] = now+1;
      }else{
        res[i][w-1-j] = now+1;
      }
      ++cnt;
      if(cnt == a[now]){
        ++now;
        cnt = 0;
      }
    }
  }
  for(int i = 0;i < h;++i){
    for(int j = 0;j < w;++j){
      if(j != 0) cout << " ";
      cout << res[i][j];
    }
    cout << endl;
  }
  return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
// #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <math.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=(a);i<(b);i++)
// #define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())

template<class T> inline void chmin(T& a, T b) {if (a > b) a = b;}
template<class T> inline void chmax(T& a, T b) {if (a < b) a = b;}
const ll inf = 1LL<<60;

int main(){
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL);
  int now=0,ans=0;

  int n;cin>>n;
  for(int i=1;i<=n;i++){
    now=0;
    for(int j=2;j<=1000;j++){
      now=pow(i,j);
      if (now<=n){
        ans=max(ans,now);
      }
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 512
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

typedef  long long ll;

ll ans[100001]={0};

// int func(int n){
//   if(ans[n] != 0)
//     return ans[n];
//   else if(n >= 6){
//     int min = func(n-1);
//     int six = 6;
//     int nine= 9;
//     for(int i = 0; i < 5; i++){
//       if(n >= six){
//         int box = func(n-six);
//         if(box < min)
//           min = box;
//       }
//       six*=six;
//     }
//     for(int i = 0; i < 6; i++){
//       if(n >= nine){
//         int box = func(n-nine);
//         if(box < min)
//           min = box;
//       }
//       nine *= nine;
//     }
//     ans[n] = min+1;
//     return min+1;
//   }
//   else{
//     ans[n] =  n;
//     return n;
//   }
// }

int main(){
  int N;

  cin >> N;

  //func(N);
  for(int i = 1; i <= N; i++){
      ll mini = ans[i-1];
      for(ll j = 6; j <= i; j*=6){
        if(mini > ans[i-j])
          mini = ans[i-j];
      }
      for(ll j = 9; j<=i; j*=9){
        if(mini > ans[i-j])
          mini = ans[i-j];
      }
      ans[i] = mini + 1;
    
  }
  cout << ans[N] << endl;
  
  return 0;
}




Measured memory_bytes: 1024
#include <algorithm>
#include <iostream>

using namespace std;

int stores[100000];

int main() {
  for (;;) {
    int d;
    cin >> d;
    if (!d) return 0;
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n-1; i++)
      cin >> stores[i];
    stores[n-1] = 0;
    stores[n] = d;
    sort(stores, stores+n+1);
    int result = 0;
    for (int i = 0; i < m; i++) {
      int destination;
      int* j;
      pair<int*, int*> range;
      cin >> destination;
      j = lower_bound(stores, stores+n+1, destination);
      result += min(destination - *(j-1), *j - destination);
    }
    cout << result << endl;
  }
}

Measured memory_bytes: 1252
n = int(input())
A, B, C = [], [], []

for i in range(n):
  a, b, c = map(int, input().split())
  A.append(a)
  B.append(b)
  C.append(c)

dp_a, dp_b, dp_c = [0] * n, [0] * n, [0] * n
dp_a[0] = A[0]
dp_b[0] = B[0]
dp_c[0] = C[0]

for i in range(1, n):
  dp_a[i] = max(dp_b[i-1], dp_c[i-1]) + A[i]
  dp_b[i] = max(dp_c[i-1], dp_a[i-1]) + B[i]
  dp_c[i] = max(dp_a[i-1], dp_b[i-1]) + C[i]
print(max(dp_a[-1], dp_b[-1], dp_c[-1]))

Measured memory_bytes: 32364
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <cmath>
#include <map>
using namespace std;
typedef long long LL;

int main() {
  int n, k;
  cin >> n >> k;
  int x[n];
  for (int i=0; i<n; i++) cin >> x[i];

  LL ans = 100000000000000;
  for (int i=0; i<=n-k; i++) {
    LL tmp;
    if (x[i+k-1] <= 0) tmp = abs(x[i]);
    else tmp = min(abs(x[i]) + x[i+k-1] - x[i], abs(x[i+k-1]) + x[i+k-1] - x[i]);
    ans = min(ans, tmp);
  }
  cout << ans << endl;
}


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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<int, int> PII;
typedef map<int, int> MII;
typedef priority_queue<int> P_QI;
typedef priority_queue<string> P_QS;
typedef vector<int> VI;
typedef vector<long long> VLL;
typedef vector< int > :: iterator $$;

#define SET(arr) memset(arr, -1, sizeof(arr))
#define CLR(arr) memset(arr, 0, sizeof(arr))

#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)

#define $ vector< int > :: iterator
#define m_p make_pair
#define p_b push_back
#define rep(i, n) for(int i=0; i<(n); i++)
#define forn(i, n) for (int i = 1; i <=(int)(n); ++i)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define forba(i, b, a) for (int i = (int)(b); i >= (int)(a); --i)

#define forv(i,v) rep(i,SZ(v))
#define forit(i,s) for(auto i=(s).begin();i!=(s).end();++i)

#define Boost ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define _ <<" "<<
#define CS(i) cout<<"Case "<<(int)(i)<<": "

const int MOD = 1e9+7;
const int inf = 1e9+9;
const long long INF = 1e18+3;
const ld eps = 1e-9;
const ld PI=acos(-1.0);
const ld E=2.71828182845904523536;

int main() {
Boost;
//READ("in.txt");
//WRITE("out.txt");
    int n,ans=0,b=0,a=0,ba=0;
    cin>>n;
    string s[n];
    rep(i,n) cin>>s[i];

    rep(i,n){
         forv(j,s[i]){
            if(j==SZ(s[i])-1) ;
            else if(s[i][j]=='A' && s[i][j+1]=='B') ans++;
         }
         if(s[i][0]=='B') b++;
         if(s[i][SZ(s[i])-1]=='A') a++;
         if(s[i][0]=='B' && s[i][SZ(s[i])-1]=='A') ba++;
    }

    //cout<<a _ b _ ba _ ans<<endl;
    if(a==b){
        if(a==ba && a>0) ans+=a-1;
        else ans+=a;
    }
    else ans+=min(a,b);
    cout<<ans<<endl;

return 0;
}






Measured memory_bytes: 1408
import java.util.*;
import java.math.*;
import java.io.*;


public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        BigDecimal d = new BigDecimal("0");
        int n = sc.nextInt();
        for(int i = 0; i < n; i++) {
            BigDecimal x = new BigDecimal(sc.next());
            String u = sc.next();
            if(u.equals("JPY")) d = d.add(x);
            else d = d.add(x.multiply(new BigDecimal("380000")));
        }

        System.out.println(d.toPlainString());

    } 
}

Measured memory_bytes: 20052
#include<map>
#include<iostream>
#include<deque>
#include<algorithm>
#include<string>
#include<cctype>
#include<iomanip>
#include<vector>
#include<queue>
 
using namespace std;
#define REP(i,b,e) for(ll i=(ll)b;i<(ll)e;i++)
#define rep0(i,n) REP(i,0ll,n)
#define rep1(i,n) REP(i,1ll,n+1)
 
#define shosu setprecision(10)
 
typedef long long ll;
typedef pair<int,int> P;
ll longinf=1ll<<60;
int inf=1<<29;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};


int A,B;

int main(){
      cin>>A>>B;
      if((A*B)%2==1) cout<<"Yes"<<endl;
      else cout<<"No"<<endl;
      return 0;
}

Measured memory_bytes: 256
#pragma region
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue> 
#include <stack>
#include <set>
#include <list>

using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()
#pragma endregion


int main()
{
	int n, m; cin >> n >> m;
	ll MOD = 1e9 + 7;
	vector<ll> x(n), y(m);
	rep(i, n)cin >> x[i];
	rep(i, m)cin >> y[i];
	ll v = 0, h = 0;
	rep(i, n)
	{
		v += x[i] * (2 * i + 1 - n);
		v %= MOD;
		if (v < 0)v += MOD;
	}
	rep(i, m)
	{
		h += y[i] * (2 * i + 1 - m);
		h %= MOD;
		if (h < 0)h += MOD;
	}
	ll res = (v*h) % MOD;
	cout << res << endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <map>
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;

ll common_counter(vector<ll> v, ll& most_common) {
    ll max = 0;
    map<ll,ll> m;
    for (auto vi = v.begin(); vi != v.end(); vi++) {
        m[*vi]++;
        if (m[*vi] > max) {
            max = m[*vi]; 
            most_common = *vi;
        }
    }
    return max;
}

ll common_counter_ignore(vector<ll> v, ll ignore) {
    ll max = 0;
    map<ll,ll> m;
    for (auto vi = v.begin(); vi != v.end(); vi++) {
        if (*vi == ignore) continue;
        m[*vi]++;
        if (m[*vi] > max) {
            max = m[*vi];
        }
    }
    return max;
}

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ll も int も longlongにしてしまう
    #define int long long

    int n; cin >> n;
    vector<int> even;
    vector<int> odd;
    vector<int> v(n); REP (i, n) cin >> v[i];
    REP(i, n) {
        if (i % 2 == 1) {
            odd.push_back(v[i]);
        } else {
            even.push_back(v[i]);
        }
    }

    int even_most = -1;
    int even_com_c = common_counter(even, even_most);
    int odd_most = -1;
    int odd_com_c = common_counter(odd, odd_most);

    if (even_most != odd_most) {
        cout << n - even_com_c - odd_com_c << endl;
        return 0;
    }

    cout << n - max(odd_com_c + common_counter_ignore(even, even_most), even_com_c + common_counter_ignore(odd, even_most)) << endl;
    
    return 0;
}

Measured memory_bytes: 5716
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
int main(){
	lli n,wei;
	cin>>n>>wei;
	lli w[n+1],val[n+1];
	for(lli i=1;i<=n;i++)
	cin>>w[i]>>val[i];
	w[0]=0;
	val[0]=0;
	lli dp[n+1][wei+1];
	for(lli i=0;i<=n;i++){
		for(lli j=0;j<=wei;j++)
		dp[i][j]=0;
	}
	for(lli i=1;i<=n;i++){
		for(lli j=0;j<=wei;j++){
			if(j-w[i]>=0)
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+val[i]);
			else
			dp[i][j]=dp[i-1][j];
		}
	}
//	for(lli i=0;i<=n;i++){
//		for(lli j=0;j<=wei;j++)
//		cout<<dp[i][j]<<" ";
//		cout<<"\n";
//	}
	cout<<dp[n][wei];
	return 0;
}


Measured memory_bytes: 79104
#include<stdio.h>
#include<iostream>
using namespace std;

int main(){
    int a, b, d, r;
    double f;
    cin >> a >> b;
    
    d = a / b;
    r = a % b;
    f = 1.0 * a / b;
    
    printf("%d %d %.8f\n", d, r, f);
    return 0;
}


Measured memory_bytes: 3192