akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a) ; i < (int)(b) ; i++)


#define SZ(x) ((int)(x).size())


typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
struct Vector{
    double x, y;
};

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
using namespace std;

int f(ll x)
{
    if (x == 1) return 1;
    int cnt=0;
    float x_sqrt = sqrt(x);
    
    for (int k = 1 ; k <= x_sqrt;k++) {
        if (x % k == 0) cnt++;
    }
    x_sqrt = (int)x_sqrt;
    if (x_sqrt * x_sqrt == x)
        cnt = (cnt - 1) * 2 + 1;

    else
        cnt *= 2;
    return cnt;
}

ll g(ll x)
{
    return x * (x+1) / 2;
}

int main()
{
    ll N, sum = 0;
    cin >> N;
    vector<ll> Yaku(N);
    repi(i, 1, N + 1) {
        sum+=i*g(N / i);
    }
    
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 81400
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans=0;
  cin >> n;
  int p[n];
  for(int i=0; i<n; i++)
    cin >> p[i];
  for(int i=0; i<n; i++)
    if(p[i]==i+1){
      ans++;
      i++;
    }
  cout << ans;
  return 0;
}

Measured memory_bytes: 3968
import java.io.*
import java.util.*
import java.lang.*
import java.lang.Math.*


fun solve() {
    val N = rd.readInt()
    val A = LongArray(N); val B = LongArray(N)
    for (i in 0 until N) {
        val (a, b) = rd.readLongArray()
        A[i] = a; B[i] = b
    }
    A.reverse()
    B.reverse()

    var sum = 0L
    for (i in 0 until N) {
        val C = B[i] * ((A[i] + B[i]-1)/B[i])
        val press = C - A[i]
        sum += press

        if (i>=N-1) break

        A[i+1] += sum
    }

    val ans = sum
    println(ans)
}


//val rd = debug.Reader("agc", "009", "a", "sample-1")
val rd = Reader()


fun main(args: Array<String>) {
    try {
        var exception: Throwable? = null
        val sth = Thread(null, SolveThread(), "solveThread", 128 * 1024 * 1024)
        sth.setUncaughtExceptionHandler { _, e -> exception = e }
        sth.start(); sth.join()
        if (exception != null) throw exception!! }
    catch (e: Exception) { throw e }
    finally { rd.close() }
}
const val MOD = 1_000_000_007L; const val INF = Int.MAX_VALUE/2; const val LINF = Long.MAX_VALUE/2
data class IntPair(val first: Int, val second: Int)
val errPrintln: (Any) -> Unit = { msg -> System.err.println(msg) }
fun rAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("EAssert Error: $msg"); throw Error(msg) }
}
fun tAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("TAssert Error: $msg"); Thread.sleep(3000) }
}
class SolveThread : Runnable {
    override fun run() { solve() }
}
class Reader {
    private val br = BufferedReader(InputStreamReader(System.`in`))
    val readString: () -> String = { br.readLine() }
    val readInt: () -> Int = { br.readLine().toInt() }
    val readLong: () -> Long = { br.readLine().toLong() }
    val readIntArray: () -> IntArray = { br.readLine().split(' ').map { it.toInt() }.toIntArray() }
    val readLongArray: () -> LongArray = { br.readLine().split(' ').map { it.toLong() }.toLongArray() }
    val readListInt: () -> List<Int> = { br.readLine().split(' ').map { it.toInt() } }
    val readListLong: () -> List<Long> = { br.readLine().split(' ').map { it.toLong() } }
    val readListIntCol: (Int) -> List<Int> = { N -> (0 until N).map { readInt() } }
    val readListLongCol: (Int) -> List<Long> = { N -> (0 until N).map { readLong() } }
    val close: () -> Unit = { br.close() }
    protected fun finalize() { close() }
}


Measured memory_bytes: 62980
#include"bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using pi = pair<int, int>;
const ll INF = 1LL << 60;

int main() {
	ll x, ans = 1, tmp = 0;
	cin >> x;
	while (tmp < x) {
		tmp += ans;
		ans++;
	}

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



Measured memory_bytes: 3640
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO.Compression;
using System.Linq;
using System.Numerics;

namespace Atcoder
{
    class Program
    {
        private const long COMDIV = 1000000007;
        static void Main(string[] args)
        {
            var NMX = Console.ReadLine().Split().Select(int.Parse).ToArray();
            List<int[]> CAs = new List<int[]>();
            for (int i = 0; i < NMX[0]; i++)
            {
                CAs.Add(Console.ReadLine().Split().Select(int.Parse).ToArray());
            }

            // {0, 1, ..., n-1} の部分集合の全探索
            int n = NMX[0];
            int ret = int.MaxValue;
            for (int bit = 0; bit < (1 << n); ++bit)
            {
                int[] cal = new int[NMX[1] + 1];
                for (int i = 0; i < n; ++i)
                {
                    if ((bit & (1 << i)) != 0)
                    {
                        for (var j = 0; j < CAs[i].Length; j++)
                        {
                            cal[j] += CAs[i][j];
                        }
                    }
                }

                bool canInclude = cal.Skip(1).All(x => x >= NMX[2]);
                if (canInclude)
                {
                    ret = Math.Min(ret, cal[0]);
                }

            }

            if (int.MaxValue == ret)
            {
                Console.WriteLine(-1);
                return;
            }
            Console.WriteLine(ret);
        }
        
    }
}

Measured memory_bytes: 28700
#include <cstdio>
using namespace std;
int c[4][4];
int main() {
  for(int i = 1; i <= 3; i++) {
    scanf("%d %d %d", &c[i][1], &c[i][2], &c[i][3]);
  }
  int j1 = c[1][1]-c[1][2]==c[2][1]-c[2][2]&&c[2][1]-c[2][2]==c[3][1]-c[3][2];
  int j2 = c[1][2]-c[1][3]==c[2][2]-c[2][3]&&c[2][2]-c[2][3]==c[3][2]-c[3][3];
  if(j1 && j2) printf("Yes");
  else printf("No");
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define MAXN 8005
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 + c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 998244353,MAXL = 1 << 14;
int N,M,fac[MAXN * 2],invfac[MAXN * 2],inv[MAXN * 2];
int f[MAXL + 5],g[MAXL + 5],dp[2][MAXN],W[MAXL + 5];
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int C(int n,int m) {
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
	if(c & 1) res = mul(res,t);
	t = mul(t,t);
	c >>= 1;
    }
    return res;
}
void update(int &x,int y) {
    x = inc(x,y);
}
void FFT(int *p,int L,int on) {
    for(int i = 1 , j = L >> 1 ; i < L - 1 ; ++i) {
	if(i < j) swap(p[i],p[j]);
	int k = L >> 1;
	while(j >= k) {
	    j -= k;
	    k >>= 1;
	}
	j += k;
    }
    for(int h = 2 ; h <= L ; h <<= 1) {
	int wn = W[(MAXL + on * (MAXL / h)) % MAXL];
	for(int k = 0 ; k < L ; k += h) {
	    int w = 1;
	    for(int j = k ; j < k + h / 2 ; ++j) {
		int u = p[j],t = mul(w,p[j + h / 2]);
		p[j] = inc(u,t);
		p[j + h / 2] = inc(u,MOD - t);
		w = mul(w,wn);
	    }
	}
    }
    if(on == -1) {
	int InvL = fpow(L,MOD - 2);
	for(int i = 0 ; i < L ; ++i) p[i] = mul(p[i],InvL);
    }
}
void Solve() {
    read(N);read(M);
    fac[0] = 1;
    for(int i = 1 ; i <= N + 10 ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[N + 10] = fpow(fac[N + 10],MOD - 2);
    for(int i = N + 9 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    W[0] = 1;W[1] = fpow(3,(MOD - 1) / MAXL);
    for(int i = 2 ; i < MAXL ; ++i) W[i] = mul(W[i - 1],W[1]);
    int cur = 0;
    for(int i = 0 ; i <= N ; ++i) dp[cur][i] = 1;
    for(int i = 1 ; i <= N ; ++i) g[i] = invfac[i + 2];
    int L = 1;
    while(L <= 2 * N) L <<= 1;
    FFT(g,L,1);
    for(int i = 2 ; i <= M ; ++i) {
	memset(dp[cur ^ 1],0,sizeof(dp[cur ^ 1]));
	for(int j = 0 ; j <= N ; ++j) {
	    update(dp[cur ^ 1][j],mul(dp[cur][j],inc(C(j + 1,2),1)));
	}
	memset(f,0,sizeof(f));
	for(int j = 0 ; j <= N ; ++j) f[j] = mul(dp[cur][j],invfac[j]);
	FFT(f,L,1);
	for(int j = 0 ; j < L ; ++j) f[j] = mul(f[j],g[j]);
	FFT(f,L,-1);
	for(int j = 0 ; j <= N ; ++j) {
	    update(dp[cur ^ 1][j],mul(f[j],fac[j + 2]));
	}
	cur ^= 1;
    }
    int ans = 0;
    for(int i = 0 ; i <= N ; ++i) {
	update(ans,mul(C(N,i),dp[cur][i]));
    }
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}


Measured memory_bytes: 512
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;

public class Main {

    public static void main(String[] args) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line = br.readLine();
            String[] strArray = new String[2];
            int[] intArray = new int[2];
            DecimalFormat df = new DecimalFormat("0.00000");
            
            strArray = line.split("\\s");
            
            for (int i = 0; i < strArray.length; i++) {
				intArray[i] =Integer.parseInt(strArray[i]);
			}
   
            System.out.println(intArray[0]/intArray[1] +" " +
            		intArray[0]%intArray[1] + " " +
            		df.format((double)intArray[0]/(double)intArray[1])
            		);
   
            br.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

Measured memory_bytes: 17132
#include <bits/stdc++.h>
using namespace std;
int main (){
  int s,a=0,b=0;
  cin>>s;
  if(s/100>0&&s/100<=12)
    a=1;
  if(s-(s/100)*100>0&&s-(s/100)*100<=12)
    b=1;
  if(b==1&&a==1)
    cout<<"AMBIGUOUS";
  else if(a+b==0)
    cout<<"NA";
  else if(a ==1)
    cout<<"MMYY";
  else if(b == 1)
    cout<<"YYMM";
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <sstream>
#include <set>
#include <map>
#define LLMAX 9223372036854775807

using namespace std;
using ll = long long;

int main() {
	ll argA, argB, argC, argD, argE, argF;
	cin >> argA >> argB >> argC >> argD >> argE >> argF;

	ll maxWaterTry = argF / min(argA, argB) / 100 + 1;
	ll maxSuger = argF / 100 * argE;
	ll maxSugerTry = maxSuger / min(argC, argD) + 1;

	float retDensity = 0.0f;
	ll retSum = argA * 100;
	ll retSuger = 0;
	for (ll i = 0; i <= maxWaterTry; ++i) {
		for (ll j = 0; j <= maxWaterTry; ++j) {
			for (ll k = 0; k <= maxSugerTry; ++k) {
				for (ll l = 0; l <= maxSugerTry; ++l) {
					ll water = (i * argA + j * argB) * 100;
					ll suger = k * argC + l * argD;
					ll limitSuger = water / 100 * argE;

					if (suger > limitSuger) {
						continue;
					}
					if (water + suger > argF) {
						continue;
					}
                    if (water + suger == 0) {
                        continue;
                    }
                    
					float density = ((float) suger) / ((float) (water + suger)) * 100.0f;
					if (density > retDensity) {
						retDensity = density;
						retSum = water + suger;
						retSuger = suger;
					}
				}
			}
		}
	}

	cout << retSum << ' ' << retSuger << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)

using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

int p, q, r;

int main(){
	//IO("input.txt", "output.txt");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> p >> q >> r;
	cout << min(p + q, min(p + r, q + r)) << "\n";
	return 0;
}

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

#define debug(x) cerr << #x << ": " << x << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  const long double PI = acos(-1.0);
  long double R;
  cin >> R;
  cout << setprecision(12) << fixed << 2 * PI * R << '\n';
  return 0;
}

Measured memory_bytes: 3812
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Abc
{
    class A_167_Registration
    {
        public static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            // 文字列の入力
            string s = Console.ReadLine();
            string t = Console.ReadLine();
            //if (s + "." == t[0])
            //{
            //    Console.WriteLine("YES");
            //}
            //else
            //{
            //    Console.WriteLine("NO");
            //}

            string result = "Yes";
            if (t.Length - s.Length != 1)
            {
                result = "No";
            }
            else
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] != t[i])
                    {
                        result = "No";
                        break;
                    }
                }
            }

            Console.WriteLine(result);

            Console.Out.Flush();
        }
    }
}


Measured memory_bytes: 25768
#include <bits/stdc++.h>
#define ll long long
#define mem( f, x ) memset( f, x, sizeof( f ) )
#define INF 0x3f3f3f3f
#define pii pair<int, int>
#define mk( x, y ) make_pair(x, y)
#define pk push_back
#define fi first
#define se second
using namespace std;
const int M = 2*1e5+50;
const int N = 2*1e5+50;
const int MAXN = 2*1e5+50;
int nxt[N];
int path[N], hh[N];
bool vis[N];
ll n, m;


int main( ){
    while( scanf( "%lld %lld", &n, &m ) != EOF ){
        for( int i = 1; i <= n; i++ ){
            scanf( "%d", nxt+i );
            vis[i] = 0;
        }
        int cnt = 0, cur = 1, pre = 0;
        bool flag = 0;
        while( !vis[cur] ){
            vis[cur] = 1;
            path[++cnt] = cur;
            cur = nxt[cur];
            if( cnt == m ){
                flag = 1;
                printf( "%d\n", cur );
                break;
            }
            
        }
        //cout << "cnt: " << cnt << endl;
        if( flag ) continue;

        for( int i = 1; i <= cnt; i++ )
            hh[path[i]] = i;
        int num = cnt-hh[cur]+1; //num环长，cur: 环入
        m -= hh[cur]-1;
        m %= num;
        printf( "%d\n", path[hh[cur]+m] );
    }
    return 0;
}


Measured memory_bytes: 6108
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <map>

using namespace std;

int main(){
  int N, K;
  long min = 10000000000000;
  cin >> N;
  cin >> K;
  vector<int> h(N);

  for(int i = 0; i < N; i++){
    cin >> h[i];
  }

  sort(h.begin(), h.end());
  for(int i = 0; N - K + 1 != i; i++){
    if(min > abs(h[K - 1 + i] - h[i])){
      min = abs(h[K - 1 + i] - h[i]);
    }
  }

  cout << min << endl;


}



Measured memory_bytes: 640
fun main(args:Array<String>) {
  (0..10).map { 
    (0..it).map{ 2 }.reduce { y, x -> y*x } 
  }.toMutableList().let { m ->
    m.add(1)
    readLine()!!.toInt().let { n ->
      m.filter { n >= it }.maxBy { it }!!.run(::println)
    }
  }
}

Measured memory_bytes: 33672
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include <iomanip>
using namespace std;

int main(){
	int n,m,sum=0;
	cin>>n;
	m=n;
	for(int i=0;m>0;i++){
		sum+=m%10;
		m=m/10;
	}
	if(n%sum==0){
		cout<<"Yes"<<endl;
	}
	else{
			cout<<"No"<<endl;
	}
	return 0;
}

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

int main() {
  string s;
  cin >> s;
  ll r = 0, k = 0;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'B') {
      r += s.size() - 1 - k - i;
      k++;
    }
  }
  cout << r << endl;
}


Measured memory_bytes: 640
class UnionFind:
    def __init__(self, n):
        self.table = [-1 for _ in range(n)]

    def _root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self._root(self.table[x])
            return self.table[x]

    def find(self, x, y):
        return self._root(x) == self._root(y)

    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1<=d2:
            self.table[r2] = r1
            if d1==d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2

n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
    x, y, z = map(int, input().split())
    x -= 1
    y -= 1
    uf.union(x, y)

ans = 0
for v in uf.table:
    if v<0:
        ans += 1
print(ans)

Measured memory_bytes: 5364
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

constexpr int P = 1000000007;

void foo() {
    cout << 0 << endl;
    exit(0);
}

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

    int n, m;
    cin >> n >> m;

    int k = n * m;

    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());

    vector<int> b(m);
    for (int j = 0; j < m; j++) {
        cin >> b[j];
    }
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    if (a[0] != k || b[0] != k) foo();
    for (int i = 1; i < n; i++) {
        int l = a[i - 1] - a[i];
        if (l <= 0) foo();
    }
    for (int j = 1; j < m; j++) {
        int l = b[j - 1] - b[j];
        if (l <= 0) foo();
    }

    ll r = 1;
    int c = 0;
    int i = 0, j = 0;
    for (int h = k; h > 0; h--) {
        while (i + 1 < n && a[i + 1] >= h) i++;
        if (a[i] < h) foo();
        while (j + 1 < m && b[j + 1] >= h) j++;
        if (b[j] < h) foo();
        int s = (i + 1) * (j + 1) - c;
        if (a[i] == h) s = j + 1;
        if (b[j] == h) s = i + 1;
        if (a[i] == h && b[j] == h) s = 1;
        if (s <= 0) foo();
        r = r * s % P;
        c++;
    }

    //int j0 = 0;
    //for (int i = 1; i < n; i++) {
    //    int l = a[i - 1] - a[i];
    //    if (l <= 0 || l > m) foo();
    //    int t = a[i - 1];
    //    for (int j = j0; j < j0 + l; j++) {
    //        t--;
    //        if (b[j] != t) {
    //            foo();
    //        }
    //    }
    //    j0 += l;
    //}

    cout << r << endl;

    return 0;
}

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

int main() {

	int d, l, r;

	cin >> l >> r >> d;

	int x = l / d;
	int y = r / d;

	if(l % d == 0)
		cout << y - x + 1 << endl;
	else
		cout << y - x << endl;
}

Measured memory_bytes: 3136
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<climits>
#include<set>
#include<utility>
#include<typeinfo>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;

int main(){

	ll n, m;
	cin >> n >> m;
	if(2*n<=m){
		cout << n+(m-2*n)/4 << endl;
	} else {
		cout << m/2 << endl;
	}

	return 0;
}


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


const int N = pow(10,5)*2;

vector<bool> isp(N+1, true);

void sieve() {
  isp[0] = false;
  isp[1] = false;
  for (int i=2; pow(i,2)<=N; i++) {
    if (isp[i]) for(int j=2; i*j<=N; j++) isp[i*j] = false;
  }
}

int main() {
    sieve();
    int x;
    cin >> x;
    for (int i=x; i<=2*N; i++){
        if (isp[i]){
            cout << i << endl;
            break;
        }
    }
}

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

signed main() {
 
	int n, w;
	cin >> n >> w;
	
	vector<int> value(n), weight(n);
	rep(i, n){
		cin >> value[i] >> weight[i];
	}
	
	int dp[105][10005] = {};
	rep(i, n){
		rep(j, w+1){
			if(j >= weight[i]){
				dp[i+1][j] = max(dp[i][j-weight[i]] + value[i], dp[i][j]);
			}
			else{
				dp[i+1][j] = dp[i][j];
			}
		}
	}
		
	cout << dp[n][w] << endl;
	
    return 0;
}


Measured memory_bytes: 11308
#include <cmath>
#include <iostream>
#include <functional>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iomanip>
#include <math.h>
#include <list>
#include <numeric>
using namespace std;

long long combi(long long n, long long k) {
  if (n == k || k == 0)
    return 1;
  else {
    return combi(n - 1, k - 1) + combi(n - 1, k);
  }
}

int main(){    
  long long K,N,M=100;
  cin >> N >> K;

  if(N<abs(N-K)){
    cout << N << endl;
    return 0;
  }
  else{
    M = N%K;    
    if(M < abs(M-K)){
       cout << M << endl;
    }
    else{
      cout << abs(M-K) << endl;
    }
  }
 
}


Measured memory_bytes: 256
#include<stdio.h>
#define MAX 10000

struct Node {int p, l,r;};
struct Node T[MAX];
int n;

void preparse(int n) {
  if (n == -1) return;
  printf(" %d",n);
  preparse(T[n].l);
  preparse(T[n].r);
}

void inparse(int n) {
  if(n == -1 ) return;
  inparse(T[n].l);
  printf(" %d",n);
  inparse(T[n].r);
}

void postparse(int n) {
  if(n == -1) return;
  postparse(T[n].l);
  postparse(T[n].r);
  printf(" %d",n);
}

int main() {
  int i,v,l,r,root;
  scanf("%d", &n);
  for (i = 0;i < n; i++) {
    T[i].p =-1;
  }

  for (i = 0;i<n;i++) {
    scanf("%d %d %d",&v,&l,&r);
    T[v].l=l;
    T[v].r=r;
    if(l != -1) T[l].p = v;
    if(r != -1) T[r].p = v;
  }

  for (i = 0;i <n;i++) if (T[i].p == -1) root = i;

  printf("Preorder\n");
  preparse(root);
  printf("\n");
  printf("Inorder\n");
  inparse(root);
  printf("\n");
  printf("Postorder\n");
  postparse(root);
  printf("\n");

  return 0;

}

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

typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector< vector<ll> > vvint;
typedef vector< vector< vector<ll> > > vvvint;
typedef vector<string> vstring;
typedef vector< vector<string> > vvstring;
typedef vector<char> vchar;
typedef vector< vector<char> > vvchar;
typedef vector<long double> vdouble;
typedef vector< vector<long double> > vvdouble;
typedef vector< vector< vector<long double> > > vvvdouble;
typedef pair<ll,ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;

#define rep(i,n) for(ll i=0;i<n;i++)
#define repf(i,f,n) for(ll i=f;i<n;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define MOD 1000000007
const double PI=3.14159265358979323846;
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; }
template<typename T> T Ceil(T a, T b){return (a+b-1)/b;}

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

void printv(vint &v){
    for(auto e:v) cout<<e<<" ";
        cout<<endl;
    }

int main() {
    cout<<fixed<<setprecision(10);

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

    set<ll> s;
    rep(i,k){
        ll d; cin>>d;
        rep(j,d){
            ll a; cin>>a;
            s.insert(a);
        }
    }

    cout<<n-s.size()<<endl;

    return 0;

}

Measured memory_bytes: 3640
#include <bits/stdc++.h>
using namespace std;
const int size_max = 100001;
int v[size_max];
int kolvo[size_max] = {0};
int main ()
{
	int n; cin >> n;
	long long sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
		kolvo[v[i]]++;
		sum += v[i];
	}
	int q; cin >> q;
	while(q--)
	{
		int b, c; cin >> b >> c;
		sum = sum + (kolvo[b] * (c - b));
		kolvo[c] += kolvo[b];
		kolvo[b] = 0;
		cout << sum << '\n';
	}
}


Measured memory_bytes: 4384
from sys import stdin
readline = stdin.readline
def i_input(): return int(readline().rstrip())
def i_map(): return map(int, readline().rstrip().split())
def i_list(): return list(i_map())

def main():
    X, N = i_map()
    if N == 0:
        print(X)
        exit()
    P = i_list()
    p = list(map(lambda x: abs(x - X), P))
    p.sort()
    for i, j in enumerate(p, 1):
        if i // 2 != j:
            ans = X - (i // 2)
            if ans in P:
                ans = X + (i // 2)
            break
    else:
        if N % 2 == 1:
            ans = X - ((N + 1) // 2)
        else:
            ans = X - (N // 2)
            if ans in P:
                ans = X + (N // 2)
    print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 61988
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,vl>;
using vvl = vector<vector<ll>>;
ll INF=1LL<<60;
ll MOD=998244353;



int main(){
    ll N;
    cin >> N;
    ll S;
    cin >> S;
    vector<ll> A(N+1,0);
    for(ll i=0;i<N;i++){
        cin>>A[i+1]
    ;}
    ll ans=0;
    //L確定
    vector<vector<ll>> dp(N+1, vector<ll>(S+1, 0));
    for(ll i=1;i<=N;i++){
        //L=iのときを追加
        dp[i][0]++;
        if(A[i]<=S){
            dp[i][A[i]]++;
        }
        //以下、L<iのとき
        //A[i]採用する方
        for(ll j=A[i];j<=S;j++){
            dp[i][j]+=dp[i-1][j-A[i]];
            dp[i][j]%=MOD;
        ;}
        //A[i]採用しない方
        for(ll j=0;j<=S;j++){
            dp[i][j]+=dp[i-1][j];
            dp[i][j]%=MOD;
        ;}
        ans+=(MOD+dp[i][S]-dp[i-1][S])*(N-i+1)%MOD;
        ans%=MOD;
    ;}
    cout<<ans<<endl;

    return 0;
}


Measured memory_bytes: 70784
use std::io;

fn read_i32_exn() -> i32 {
	let mut n = String::new();
	io::stdin().read_line(&mut n).expect("Failed to input");
	(*n).trim().parse::<i32>().unwrap()

}

fn main() {
   	let x = read_i32_exn();
	println!("{}",match x { 3|5|7 => "YES", _x => "NO",});

}


Measured memory_bytes: 4352
N,M=map(int, input().split())
a=[0]*(N+1)
w=[0]*(N+1)

for _ in range(M):
  p,s=input().split()
  p=int(p)
  if s=='AC':
    a[p]=1
  else:
    if a[p]==0:
      w[p]+=1

a2=0
w2=0
for n in range(N+1):
  if a[n]==1:
    a2+=a[n]
    w2+=w[n]
      
print(a2, w2)


Measured memory_bytes: 10380
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <utility>
#include <functional>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <numeric>
#include <complex>

using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const int INF = (1<<30);
const ll INFL = (1LL<<62);
const ll MOD = 1000000007;
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> ll mod_pow(ll a, ll n, T mod) { mod=(ll)mod; ll res=1, p=a%mod; while(n){ if(n&1) res=res*p%mod; p=p*p%mod; n>>=1; } return res; }

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> C(N, 0), S(N, 0), F(N, 1);
    rep(i, N-1){
        cin >> C[i] >> S[i] >> F[i];
    }
    rep(i, N){
        if(i==N-1){
            cout << 0 << endl;
            return 0;
        }
        int a = S[i];
        for(int j=i; j < N; ++j){
            a = max(S[j], a);
            if(a%F[j] != 0) a = a+F[j]-(a%F[j]);
            a += C[j];
        }
        cout << a << endl;
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;

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

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    return a / g * b;
}

ll mulMod(ll a, ll b)
{
    return (((a % MOD) * (b % MOD)) % MOD);
}

const string YES = "YES";
const string NO = "NO";

void solve(long long r, long long g, long long b)
{
    if ((100 * r + 10 * g + b) % 4 == 0)
    {
        cout << YES << endl;
    }
    else
    {
        cout << NO << endl;
    }
}

int main()
{
    long long r;
    scanf("%lld", &r);
    long long g;
    scanf("%lld", &g);
    long long b;
    scanf("%lld", &b);
    solve(r, g, b);
    return 0;
}


Measured memory_bytes: 256
#include<iostream> 
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath> 
#include<algorithm> 
using namespace std;
const int maxn=500010,inf=1e9;
int n,ans,N,l,r;
char s[maxn],s2[maxn],s3[maxn];
inline void read(int &k)
{
	int f=1;k=0;char c=getchar();
	while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
	while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar();
	k*=f;
}
int main()
{
	scanf("%s",s+1);int n=strlen(s+1);
	for(int i=1;i<=n;i++)
	if(s[i]!='x')s2[++N]=s[i],s3[++N]=s[i];
	for(int i=1;i<=N;i++)
	if(s2[i]!=s3[N-i+1])return puts("-1"),0;
	l=1;r=n;
	while(l<r)
	{
		if((s[l]=='x'&&s[r]!='x'))ans++,l++;
		else if((s[l]!='x'&&s[r]=='x'))ans++,r--;
		else l++,r--;
	}
	printf("%d\n",ans);
}

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

bool a[1000000];

int main() {
    int s;
    cin >> s;
    a[s] = true;

    for (int i = 1; ; i++) {
        if (s % 2 == 0) s /= 2;
        else s = 3 * s + 1;

        if (a[s] == true) {
            cout << i + 1 << endl;
            break;
        } else {
            a[s] = true; 
        }
    }
}

Measured memory_bytes: 256
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define or(a,b) a > b ? a : b
#define and(a,b) a > b ? b : a
#define not(n) 1 - (n - 1)

int p[3];
char str[81];

int formula(int i){
	if(isupper(str[i]))
		return p[str[i] - 'P'];
	else if(isdigit(str[i]))
		return str[i] - '0';
	else if(str[i] == '-')
		return not(formula(i + 1));
	else if(str[i] == '('){
		int j = i;
		int cen = 0;
		while(1){
			if(str[++j] == '(')
				cen++;
			else if(str[j] == ')')
				cen--;
			if(cen == 0 && (str[j] == '*' || str[j] == '+')){
				if(str[j] == '*')
					return and(formula(i + 1),formula(j + 1));
				else
					return or(formula(i + 1),formula(j + 1));
			}
		}
	}
}

int main(void){
	int count;
	gets(str);
	while(str[0] != '.'){
		str[strlen(str)] = '\0';
		count = 0;
		for(p[0] = 0;p[0] <= 2;p[0]++){
			for(p[1] = 0;p[1] <= 2;p[1]++){
				for(p[2] = 0;p[2] <= 2;p[2]++){
					if(formula(0) == 2)
						count++;
				}
			}
		}
		printf("%d\n",count);
		gets(str);
	}
	return 0;
}

Measured memory_bytes: 524
#include <iostream>
#include <deque>

using namespace std;

int main() {
    deque<int> dq={};
    
    int n;
    cin>>n;
    
    while(n){
        int q,d;
        cin>>q;
        
        switch(q){
            case 0:
                cin>>d;
                int x;
                cin>>x;
                if(d==0){
                dq.push_front(x);
                }else if(d==1){
                    dq.push_back(x);
                }
                break;
            case 1:
                int p;
                cin>>p;
                cout<<dq.at(p)<<endl;
                
                break;
            case 2:
                cin>>d;
                if(d==0){
                    dq.pop_front();
                }else if(d==1){
                    dq.pop_back();
                }
                break;
        }
        --n;
    }
    
}
    


Measured memory_bytes: 4580
//C++ おまじない
#include <bits/stdc++.h>
using namespace std;
int main()
{
    // input
    int n,m;
    cin >> n >> m;
  	int a;
    for (int i=0; i<m; i++) {
      cin >> a;
      n-=a;
    }
    // output
  	if (n<0) {
      cout << -1 <<endl;
    } else {
      cout << n << endl;
    }
    return 0;
}


Measured memory_bytes: 3644
#include<bits/stdc++.h>
using namespace std;
int main(){
  int x;
  cin>>x;
  int b=0;
  for(int i=1;i<101;i++){
    for(int j=2;j<11;j++){
      int a=pow(i,j);
      if(x>=a){
         b=max(b,a);     
      }
    }
  }
  cout<<b<<endl;
}

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


int main() {
  int64_t a, b;
  cin >> a >> b;
  
  if (a*b <= 0){
    cout << "Zero" << endl;
  }
  else if (a > 0 || (b - a) % 2 == 1){
    cout << "Positive" << endl;
  }
  else{
    cout << "Negative" << endl;
  }
}

Measured memory_bytes: 256
package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	s := getNextString(scanner)
	q := getNextInt(scanner)
	seg := newSegment(n)
	for i := 0; i < n; i++ {
		seg.update(i, 1<<uint(s[i]-'a'), seg.or)
	}
	for i := 0; i < q; i++ {
		switch getNextInt(scanner) {
		case 1:
			seg.update(getNextInt(scanner)-1, 1<<uint(getNextString(scanner)[0]-'a'), seg.or)
		case 2:
			fmt.Fprintln(writer, pcount(seg.query(getNextInt(scanner)-1, getNextInt(scanner), 0, seg.or)))
		}
	}
}
func pcount(x uint32) int {
	y := 0
	for x > 0 {
		if x&1 == 1 {
			y++
		}
		x >>= 1
	}
	return y
}

type segment [][]uint32

func newSegment(n int) segment {
	seg := make(segment, 0)
	for n > 1 {
		seg = append(seg, make([]uint32, n))
		n = (n + 1) >> 1
	}
	seg = append(seg, make([]uint32, 1))
	return seg
}
func (seg segment) update(i int, v uint32, f func(x, y uint32) uint32) {
	seg[0][i] = v
	for h := 1; h < len(seg); h++ {
		i >>= 1
		if i<<1+1 < len(seg[h-1]) {
			seg[h][i] = f(seg[h-1][i<<1], seg[h-1][i<<1+1])
		} else {
			seg[h][i] = seg[h-1][i<<1]
		}
	}
}
func (seg segment) query(l, r int, s uint32, f func(x, y uint32) uint32) uint32 {
	for h := 0; h < len(seg) && l < r; h++ {
		if l&1 == 1 {
			s = f(s, seg[h][l])
			l++
		}
		l >>= 1
		if r&1 == 1 {
			s = f(s, seg[h][r-1])
		}
		r >>= 1
	}
	return s
}
func (seg segment) or(x, y uint32) uint32 {
	return x | y
}


Measured memory_bytes: 7720
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

const int MAX_N = 200050;
int n;
vi adj[MAX_N];
bool vis[MAX_N];
int diameter = 0;

int dfs(int here) {
	vis[here] = true;
	vector<int> heights;
	for(auto there : adj[here]) {
		if(!vis[there]) {
			heights.pb(dfs(there));
		}
	}
	if(heights.empty()) return 0;
	sort(all(heights));
	if(heights.size() >= 2) {
		diameter = max(diameter, 2 + heights[heights.size()-2] + heights[heights.size()-1]);
	}
	return heights.back()+1;
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	cin >> n;
	rep(i,0,n-1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	memset(vis, false, sizeof(vis));
	int height = dfs(0);
	diameter = max(diameter, height);
	bool dp[MAX_N];
	memset(dp, false, sizeof(dp));
	dp[0] = true;
	dp[1] = false;
	rep(i,2,diameter+1) {
		dp[i] = !dp[i-1] || !dp[i-2];
	}
	if(dp[diameter]) {
		cout << "First" << '\n';
	} else {
		cout << "Second" << '\n';
	}
	return 0;
}


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

const int ha=1e9+7;
int f[2000010],n;

void add(int &x,const int &y){x=(x+y>=ha)?(x+y-ha):(x+y);}
void dev(int &x,const int &y){x=(x-y<0)?(x-y+ha):(x-y);}

int main()
{
    scanf("%d",&n);
    int sum=n-1;f[n]=n;
    for(int i=n+1;i<=2*n;i++) f[i]=1;
    for(int i=n-1;i>=1;i--)
    {
        add(f[i],f[i+1]);
        f[i]=(f[i]+1ll*(n-1)*(n-1))%ha;
        add(f[i],sum);
        dev(sum,f[i+n+1]);
        add(sum,f[i+2]);
    }
    printf("%d\n",f[1]);
    return 0;
}

Measured memory_bytes: 8064
N = int(input())
S = [[] for i in range(N)]
march = [[] for i in range(5)]
for i in range(N):
    S[i] = input()
for i in range(N):
    if S[i][0] == "M":
        march[0].append(S[i])
    elif S[i][0] == "A":
        march[1].append(S[i])
    elif S[i][0] == "R":
        march[2].append(S[i])
    elif S[i][0] == "C":
        march[3].append(S[i])
    elif S[i][0] == "H":
        march[4].append(S[i])
        
c = [0]*5
for i in range(5):
    c[i] = len(march[i])

ans = 0
for i in range(3):
    for j in range(i+1,4):
        for k in range(j+1,5):
            ans += c[i]*c[j]*c[k]

print(ans)

Measured memory_bytes: 62112
def main():
    import numpy as np
    import math

    A, B = map(int, input().split())
    gcd = int(np.gcd(A, B))

    num_to_search = int(math.sqrt(gcd)) + 1

    def factorize_prime(number):
        RV = [1]
        
        if (number != 1):      
            while (number % 2 == 0):
                number //= 2
                RV.append(2)
            
            for i in range(3, num_to_search + 1):
                if (number % i == 0):
                    while (number % i == 0):
                        number //= i
                        RV.append(i)
        
        if (number != 1):
            RV.append(number)
        
        return(RV)

    
    cnt = factorize_prime(gcd)
    ans = len(set(cnt))
    


    
    print(ans)





if __name__ == '__main__':
    main()




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

vector<long long> enum_divisors(long long n) {
    vector<long long> res;
    for (long long i = 1LL; i * i <= n; i++) {
        if (n % i != 0) continue;
        res.emplace_back(i);
        if (i != n / i) res.emplace_back(n / i);
    }
    sort(res.begin(), res.end());
    return res;
}

int main() {
    int n; cin >> n;
    int cnt = 0;
    for (int i = 1; i <= n; i += 2) cnt += enum_divisors(i).size() == 8;
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 256
import os
import sys

weather_list = ['Sunny', 'Cloudy', 'Rainy']

#today_weather = sys.argv[1]
today_weather = input()

weather_index = weather_list.index(today_weather)

weather_index = weather_index+1 if len(weather_list)-1!=weather_index else 0

print(weather_list[weather_index])

Measured memory_bytes: 2940
constexpr bool isDebugMode = false;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
    #include <atcoder/all>
    using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b , a % b) : b;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }

void solve();

int main(){
    while(testcase--) solve();
    return 0;
}

void solve(){
        ll N; cin >> N;
    vector<string> s(N);
    rep(i, N) cin >> s[i];
    ll cnta = 0;
    ll cntb = 0;
    ll cntab = 0;
    ll cntba = 0;
    rep(i, N){
        rep(j, s[i].size() - 1){
            if(s[i][j] == 'A' && s[i][j + 1] == 'B') cntab++;
        }
        if(s[i][0] == 'B' && s[i][s[i].size() - 1] == 'A') cntba++;
        if(s[i][0] == 'B' && s[i][s[i].size() - 1] != 'A') cntb++;
        if(s[i][0] != 'B' && s[i][s[i].size() - 1] == 'A') cnta++;

    }
    // pt(cntab, cnta, cntb);
    ll ans = 0;
    pt(cntab + (cntba == 0 ? min(cnta, cntb) : (cnta + cntb == 0 ? cntba - 1 : cntba + min(cnta, cntb))));
    return;
}

Measured memory_bytes: 3696
#include <iostream>
#include <algorithm>
#include <limits>

using ll = long long;

constexpr int MAX_N = 500000;

int N;
std::pair<ll, ll> arts[MAX_N];

int main(){

	std::cin >> N;

	for(int i = 0; i < N; ++i){

		std::cin >> arts[i].first >> arts[i].second;

	}

	std::sort(arts, arts + N);

	for(int i = 0; i < N; ++i){
		if(i > 0){
			arts[i].second += arts[i - 1].second;
		}
	}

	ll x = -arts[0].first;
	ll ans = arts[0].second;

	for(int i = 1; i < N; ++i){

		x = std::min(x, arts[i - 1].second - arts[i].first);
		ans = std::max(ans, arts[i].second - arts[i].first - x);

	}

	std::cout << ans << std::endl;

	return 0;
}



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

n = int(input())
s = input()

w = []
e = []
for i in range(len(s)):
    e.append(0)
    if i == 0:
        w.append(0)
        continue
    if s[i - 1] == 'W':
        w.append(w[i - 1] + 1)
    else:
        w.append(w[i - 1])

totalmin = float('inf')
for i in range(len(s) - 1, -1, -1):
    if i == len(s) - 1:
        e[i] = 0
    else:
        if s[i + 1] == 'E':
            e[i] = e[i + 1] + 1
        else:
            e[i] = e[i + 1]
    total = e[i] + w[i]
    totalmin = min(totalmin, total)

print(totalmin)


Measured memory_bytes: 18048
s = input()

def xor(a, b):
  if a == b:
    return False
  else:
    return True
    
if len(s) % 2 == 0:
  even_string = 1
else:
  even_string = 0
  
if s[0] == s[len(s) - 1]:
  same_char = 1
else:
  same_char = 0
  
if xor(even_string, same_char):
  print("Second")
else:
  print("First")

Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod=1e9+7;

ll x[4]={1,0,-1,0};
ll y[4]={0,1,0,-1};
ll n,m;

bool legal(ll x,ll y){
    return (x>=0&&y>=0&&x<n&&y<m);
}

int main() {
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=clock();
    
    ll t=1;
    //cin>>t;
    while(t--){
        ll cx,cy,dx,dy;
        cin>>n>>m>>cx>>cy>>dx>>dy; cx--,cy--,dx--,dy--;
        vector<string> s(n);
        for(ll i=0;i<n;i++)
            cin>>s[i];
        deque<pair<ll,ll>> dq;
        vector<vector<ll>> d(n,vector<ll>(m,mod));
        d[cx][cy]=0;
        dq.push_front({cx,cy});
        while(!dq.empty()){
            pair<ll,ll> p=dq.front();
            dq.pop_front();
            for(int i=0;i<4;i++){
                ll a=p.first+x[i], b=p.second+y[i];
                if(a<0 || b<0 || a>=n ||b>=m) continue;
                if(d[a][b]>d[p.first][p.second] && s[a][b]=='.'){
                    dq.push_front({a,b});
                    d[a][b]=d[p.first][p.second];
                }
            }
            for(int i=p.first-2;i<p.first+3;i++){
                for(int j=p.second-2;j<p.second+3;j++){
                    if(i<0 || j<0 || i>=n ||j>=m) continue;
                    if(d[i][j]>d[p.first][p.second]+1 && s[i][j]=='.'){
                        dq.push_back({i,j});
                        d[i][j]=d[p.first][p.second]+1;
                    }
                }
            }
        }
        if(d[dx][dy]==mod)
            cout<<-1;
        else
            cout<<d[dx][dy];
    }
    
    cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
    T = clock();
    return 0;
}

Measured memory_bytes: 12892
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<complex>
#include<string>
#include<cstring>
using namespace std;
#define rep2(x,from,to) for(long long x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
#define INF 100000000
#define debug(x) cout<<#x<<": "<<x<<endl

#define all(x) x.begin(),x.end()
typedef pair<long long,long long> P;
typedef pair<long long,P> PP;
long long n;
long long ans;
long long a,b;
long long x[100006];

int main()
{
	cin>>n;
	cin>>a>>b;
	rep(i,n)
	{
		cin>>x[i];	
	}
	rep(i,n-1)
	{
		ans+=min(b,(x[i+1]-x[i])*a);
	}
	cout<<ans<<endl;
}

Measured memory_bytes: 1152
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########
def main(n,m,c,a,b):
    res = 0
    for ai in a:
        if check(ai,b,c) : res += 1
    return res
    
def check(ai,b,c):
    temp = 0
    for i in range(m):
        temp += ai[i] * b[i]
    temp += c
    return temp > 0

n,m,c = mi()
b = mi()
a = mix(n)
print(main(n,m,c,a,b))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N,K;
  cin >> N >> K;
  int L=0;
  for (int i=0;i<N;i++){
    int x;
    cin >> x;
    if (K-x <= x)
      L+=K-x;
    if (K-x > x)
      L+=x;
  }
  cout << L*2 << endl;
}

Measured memory_bytes: 256