akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node{
  struct node *right;
  struct node *left;
  struct node *parent;
  int key;
};

typedef struct node *Node;

void insert(int);
 int find(int);
void delete(int);
void inParse(Node);
void preParse(Node);
Node Minimum(Node);
Node Successor(Node);
Node findNode(int);

Node root;

int main(){

   int i;
   int n;
   int key;
   int judge;
  char str[7];

  scanf("%d",&n);

  for(i=0;i<n;i++){
  scanf("%s",str);
  if(strcmp(str,"insert")==0){
    scanf("%d",&key);
    insert(key);
  }
  
  else if(strcmp(str,"find")==0){
    scanf("%d",&key);
    judge = find(key);
    if(judge==1) printf("yes\n");
    else printf("no\n");
  }

  else if(strcmp(str,"delete")==0){
    scanf("%d",&key);
    delete(key);
  }
  
  else if(strcmp(str,"print")==0){
     inParse(root);
     printf("\n");
     preParse(root);
     printf("\n");
   }
  }

  return 0;
}

void insert(int k){

  Node x = root;
  Node y = NULL;
  Node z;

  z = malloc(sizeof(struct node));

  z->left  = NULL;
  z->right = NULL;
  z->key = k;

  while(x!=NULL){
    y = x;
    if(z->key < x->key) x = x->left;
    else x = x->right;
    z->parent = y;
  }
  if(y==NULL) root = z;
  else if(z->key < y->key) y->left = z;
  else y->right = z;
}

int find(int k){
  
  Node x = root;
  while(x!=NULL){
   if(k == x->key) return 1;
  else{
    if(k < x->key) x = x->left;
    else x = x->right;
   }
  }
  return -1;
}

Node findNode(int k){
  
  Node x = root;
  while(x!=NULL){
   if(k == x->key) return x;
  else{
    if(k < x->key) x = x->left;
    else x = x->right;
   }
  }
  return NULL;
}

void delete(int k){

  Node x = root;
  Node y = NULL;
  Node z;

  z = findNode(k);

  if(z->left ==NULL || z->right == NULL){
    y = z;
  }
  else{
    y = Successor(z);
  }
  if(y->left != NULL) x = y->left;
  else x = y->right;

  if(x != NULL) x->parent = y->parent;

  if(y->parent == NULL){
    root = x;
    }
    else if(y == y->parent->left){
      y->parent->left = x;
    }
    else{
      y->parent->right = x;
    }
    if(y != z){
      z->key = y->key;
    }
}

void inParse(Node n){
  if(n==NULL) return;  
  inParse(n->left);
  printf(" %d",n->key);
  inParse(n->right);
}


void preParse(Node n){
  if(n==NULL) return;
  printf(" %d",n->key);
  preParse(n->left);
  preParse(n->right);
}

Node Minimum(Node n){
  while(n->left!=NULL){
    n = n->left;
  }
  return n;
}

Node Successor(Node n){

  Node p;
  
  if(n->right != NULL)
  return Minimum(n->right);

  p = n->parent;
  p = p->parent;
  return p;
}
  

Measured memory_bytes: 19364
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException{
		Scanner scanner = new Scanner(System.in);
		int cases = scanner.nextInt();
		long result = 0;
		int[] nums = new int[cases];
		BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
		for(int i = 0; i < cases; i++) 
			nums[i] = scanner.nextInt();
		Arrays.sort(nums);
		result = nums[cases - 1];
		int j = cases - 2;
		for(int i = cases - 3; i >= 0; i -= 2) {
			if(i > 0)
				result += nums[j];
			result += nums[j];
			j--;
		}
		log.write("" + result);
		log.flush();
	}
}


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

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1<<30;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int D,G;
  cin>>D>>G;
  vector<vector<int>> v(D,vector<int>(3));
  rep(i,D){
    v[i][0]=i+1; 
    int x,y;
    cin>>x>>y;
    y/=100;
    v[i][1]=x;
    v[i][2]=y;
  }
  G/=100;
  int ans = INF;
  for(int i = 0; i < (1<<D);i++){
    int cnt=0;
    int point=0;
    for(int j = 0; j < D; j++){
      if(i&(1<<j)) {
        cnt+=v[j][1];
        point+=v[j][0]*v[j][1]+v[j][2];
      }
    }
    if(point>=G)chmin(ans,cnt);
    else{
      bool can = true;
      for(int j = D-1; j >=0;j--){
        if(!(i&(1<<j))){
          int rest = G-point-1;
          rest/=v[j][0];
          rest++;
          if(rest>=v[j][1]) can = false;
          cnt+=rest;
          if(can) chmin(ans,cnt);
          break;
        }
      }
    }
  }
  cout<<ans<<endl;
}

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

int main() {
  int A, B, S1, S2, S3;
  cin >> A >> B;

  // ここにプログラムを追記
  
  S1 = A+B;
  S2 = A-B;
  S3 = A*B;
  
  if(S2 <= S1 && S3 <= S1)
    cout << S1 << endl;
  else if(S1 <= S2 && S3 <= S2)
    cout << S2 << endl;
  else if(S1 <= S3 && S2 <= S3)
    cout << S3 << endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <queue>
using namespace std;
int cnt=1,trie[40000][10],fail[40000];
inline void advance(int &a,int b)
{
    if (!trie[a][b-1])
        trie[a][b-1]=++cnt;
    a=trie[a][b-1];
}
bool out[40000];
int dp[41][40000];
const int MOD=1000000007;
inline void add(int &a,int b)
{
    a+=b;
    if (a>=MOD)
        a-=MOD;
}
int main()
{
    int n,x,y,z;
    scanf("%d%d%d%d",&n,&x,&y,&z);
    for (int i=0;i<(1<<(x-1));++i)
        for (int j=0;j<(1<<(y-1));++j)
            for (int k=0;k<(1<<(z-1));++k)
            {
                int cur=1;
                for (int l=0,t=0;l<x;++l)
                {
                    ++t;
                    if (l==x-1||((i>>l)&1))
                    {
                        advance(cur,t);
                        t=0;
                    }
                }
                for (int l=0,t=0;l<y;++l)
                {
                    ++t;
                    if (l==y-1||((j>>l)&1))
                    {
                        advance(cur,t);
                        t=0;
                    }
                }
                for (int l=0,t=0;l<z;++l)
                {
                    ++t;
                    if (l==z-1||((k>>l)&1))
                    {
                        advance(cur,t);
                        t=0;
                    }
                }
                out[cur]=true;
            }
    fail[1]=1;
    queue<int> q;
    for (int i=0;i<10;++i)
        if (trie[1][i])
        {
            fail[trie[1][i]]=1;
            q.push(trie[1][i]);
        }
    while (!q.empty())
    {
        int f=q.front();
        q.pop();
        for (int i=0;i<10;++i)
            if (trie[f][i])
            {
                int val=fail[f];
                while (val!=1&&!trie[val][i])
                    val=fail[val];
                if (trie[val][i])
                    val=trie[val][i];
                q.push(trie[f][i]);
                fail[trie[f][i]]=val;
            }
    }
    dp[0][1]=1;
    for (int i=0;i<n;++i)
        for (int j=1;j<=cnt;++j)
        {
            if (out[j])
            {
                add(dp[i+1][j],(long long)dp[i][j]*10%MOD);
                continue;
            }
            for (int k=0;k<10;++k)
            {
                int val=j;
                while (val!=1&&!trie[val][k])
                    val=fail[val];
                if (trie[val][k])
                    val=trie[val][k];
                add(dp[i+1][val],dp[i][j]);
            }
        }
    int ans=0;
    for (int i=1;i<=cnt;++i)
        if (out[i])
            add(ans,dp[n][i]);
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 7808
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main()
{
    int n, ans;
    cin >> n;
    ans = n % 1000;
    if(ans != 0) ans = 1000 - ans;
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3588
#include <iostream>

using namespace std;
int mhp(int a[100001][3],int n,int dp[100001][3],int act)
{
    if(n==0)return 0;
    else if(dp[n][act]!=-1)return dp[n][act];
    else
    {
        if(act==0)
        {
            dp[n][act]=a[n][act]+max(mhp(a,n-1,dp,1),mhp(a,n-1,dp,2));
            return dp[n][act];
        }
        else if(act==1)
        {
            dp[n][act]=a[n][act]+max(mhp(a,n-1,dp,0),mhp(a,n-1,dp,2));
            return dp[n][act];
        }
        else if(act==2)
        {
            dp[n][act]=a[n][act]+max(mhp(a,n-1,dp,0),mhp(a,n-1,dp,1));
            return dp[n][act];
        }
    }
}
int main()
{
   int n;
   cin>>n;
   int a[100001][3],dp[100001][3];
   for(int i=1;i<=n;i++)
   for(int j=0;j<3;j++){cin>>a[i][j];dp[i][j]=-1;}
   cout<<max(mhp(a,n,dp,0),max(mhp(a,n,dp,1),mhp(a,n,dp,2)));
    return 0;
}



Measured memory_bytes: 8832
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8

macro_rules! input {
    (source = $s:expr; $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
#[allow(unused_imports)]
use std::cmp::*;
trait Each: Iterator {
    fn each<F>(self, mut f: F)
    where
        Self: Sized,
        F: FnMut(Self::Item),
    {
        self.fold((), move |(), item| f(item));
    }
}
impl<T: Iterator> Each for T {}

const SEPARATOR: &'static str = " ";

fn solve(source: &str) -> String {
    let mut result: Vec<String> = vec![];
    macro_rules! p {
        ([$x: expr]) => {
            $x.iter().each(|&x| p!(x))
        };
        ([$x: expr]; $s:expr)=>{
            p!($x.iter().map(|x|x.to_string()).collect::<Vec<_>>().join($s));
        };
        ($($x: expr),* $(,)*) => {
            $(result.push($x.to_string()));*
        };
    }
    //---------------------------------------------------------------//

    input! {
        source = source;
        n:usize,
        d:[usize;n]
    }
    let mut ans = 0;
    for i in 0..n {
        for j in i+1..n {
            ans += d[i] * d[j];
        }
    }
    p!(ans);
    //---------------------------------------------------------------//
    return result.join(SEPARATOR);
}
fn main() {
    let mut s;
    let source = {
        use std::io::Read;
        s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s.as_str()
    };
    println!("{}", solve(source));
}
#[test]
fn testcase() {
    assert_eq!(
        solve(
            r"3
3 1 2
"
        ),
        r"11"
    );
    assert_eq!(
        solve(
            r"7
5 0 7 8 3 3 2
"
        ),
        r"312"
    );
}


Measured memory_bytes: 4352
import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n = I()
ukv = [LI() for _ in range(n)]
graph = {i:collections.deque() for i in range(1,n+1)} #1_indexed
for x in ukv:
    u,k,*v = x
    for y in v:
        graph[u].append(y)
seen = [0]*(n+1) #1_indexed
stack = []
time = 0
seentime = [0]*(n+1) #1_indexed
donetime = [0]*(n+1) #1_indexed
def dfs(j):
    global time
    seen[j] = 1
    stack.append(j)
    time += 1
    seentime[j] = time
    while stack:
        s = stack[-1]
        if not graph[s]:
            stack.pop()
            time += 1
            donetime[s] = time
            continue
        g_NO = graph[s].popleft()
        if seen[g_NO]:
            continue
        seen[g_NO] = 1
        stack.append(g_NO)
        time += 1
        seentime[g_NO] = time
for j in range(1,n+1):
    if seen[j]:
        continue
    dfs(j)
for k,a,b in zip(range(1,n+1),seentime[1:],donetime[1:]):
    print(k,a,b)


Measured memory_bytes: 6096
#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 ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

#define MAX_N 200000

template <int MOD = 1000000007>
struct Mint {
    int x;

    Mint() : x(0) {}
    Mint(long long t) {
        x = t % MOD;
        if (x < 0) x += MOD;
    }

    Mint pow(int n) {
        Mint res(1), t(x);
        while (n > 0) {
            if (n & 1) res *= t;
            t *= t;
            n >>= 1;
        }
        return res;
    }

    Mint inv() const {
        int a = x, b = MOD, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return Mint(u);
    }

    Mint &operator+=(Mint a) {
        x += a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator-=(Mint a) {
        x += MOD - a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator*=(Mint a) {
        x = int(1LL * x * a.x % MOD);
        return *this;
    }

    Mint &operator/=(Mint a) {
        return (*this) *= a.inv();
    }

    Mint operator+(Mint a) const {
        return Mint(x) += a;
    }
    Mint operator-(Mint a) const {
        return Mint(x) -= a;
    }
    Mint operator*(Mint a) const {
        return Mint(x) *= a;
    }
    Mint operator/(Mint a) const {
        return Mint(x) /= a;
    }

    Mint operator-() const {
        return Mint(-x);
    }

    bool operator==(const Mint a) {
        return x == a.x;
    }
    bool operator!=(const Mint a) {
        return x != a.x;
    }
    bool operator<(const Mint a) {
        return x < a.x;
    }

    friend ostream &operator<<(ostream &os, const Mint &a) {
        return os << a.x;
    }

    friend istream &operator>>(istream &is, Mint &a) {
        int t;
        is >> t;
        a = Mint<MOD>(t);
        return (is);
    }
};

int n;
vector<int> G[MAX_N];
int siz[MAX_N];
Mint<> cnt[MAX_N];

int dfs(int v, int pre) {
    int res = 1;
    cnt[v] = Mint<>(2).pow(n - 1);
    cnt[v] -= 1;
    for (auto to : G[v]) {
        if (to == pre) continue;
        res += dfs(to, v);
        cnt[v] -= Mint<>(2).pow(siz[to]) - 1;
    }
    cnt[v] -= Mint<>(2).pow(n - res) - 1;
    return siz[v] = res;
}

signed main() {
    cin >> n;
    int a, b;
    rep(i, n - 1) {
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    dfs(0, -1);
    Mint<> sum = 0;
    rep(v, n) {
        sum += cnt[v];
    }
    cout << sum / Mint<>(2).pow(n) << endl;
    return 0;
}

Measured memory_bytes: 23936
#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int maxn=3e5+10;

int n,m,t[maxn];
LL L[maxn],R[maxn],C[maxn],sum[maxn],tmp[maxn];

struct Point{
	LL x,y;
	Point() {}
	Point(LL x,LL y):x(x),y(y) {}
	Point operator + (const Point &p) const {return Point(x+p.x,y+p.y);}
	Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
};
LL Cross(Point A,Point B) {return A.x*B.y-A.y*B.x;}
LL Dot(Point A,Point B) {return A.x*B.x+A.y*B.y;}

namespace my_queue{
	int top;
	Point P[maxn];

	void init() {top=0;}

	void push(Point x)
	{
		while (top>=2&&Cross(P[top]-P[top-1],x-P[top])>=0) top--;
		P[++top]=x;
	}

	LL query(LL k)
	{
		while (top>=2&&P[top].y-P[top-1].y<=k*(P[top].x-P[top-1].x)) top--;
		return P[top].y-k*P[top].x;
	}
};

void DP(LL f[])
{
	int i;
	sum[0]=0;
	for (i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	my_queue::init();
	my_queue::push(Point(0,0));
	for (i=1;i<=n;i++)
	{
		f[i]=max(f[i-1],my_queue::query(i)+(LL)i*(i+1)/2-sum[i]);
		my_queue::push(Point(i,(LL)i*(i-1)/2+sum[i]+f[i]));
	}
}

void solve(LL L[],LL R[],int s,int t)
{
	int i,mid=(s+t)>>1;
	my_queue::init();
	for (i=s-1;i<mid;i++)
		my_queue::push(Point(i,(LL)i*(i-1)/2+sum[i]+L[i]));
	for (i=mid;i<=t;i++)
		tmp[i]=my_queue::query(i)+(LL)i*(i+1)/2-sum[i]+R[i+1];
	for (i=t-1;i>=mid;i--)
		tmp[i]=max(tmp[i],tmp[i+1]);
	for (i=mid;i<=t;i++)
		C[i]=max(C[i],tmp[i]);
	if (s!=t) solve(L,R,s,mid),solve(L,R,mid+1,t);
}

int main()
{
#ifdef h10
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	int i,p,x;
	scanf("%d",&n);
	for (i=1;i<=n;i++)
		scanf("%d",&t[i]);
	DP(L);
	for (i=1;i<=n-i+1;i++)
		swap(t[i],t[n-i+1]);
	DP(R);
	for (i=1;i<=n-i+1;i++)
	{
		swap(t[i],t[n-i+1]);
		swap(R[i],R[n-i+1]);
	}

	memset(C,-0x3f3f3f3f,sizeof(C));
	for (i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	solve(L,R,1,n);
	for (i=1;i<=n-i+1;i++)
	{
		swap(t[i],t[n-i+1]);
		swap(L[i],L[n-i+1]);
		swap(R[i],R[n-i+1]);
		swap(C[i],C[n-i+1]);
	}
	for (i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	solve(R,L,1,n);
	for (i=1;i<=n-i+1;i++)
	{
		swap(t[i],t[n-i+1]);
		swap(L[i],L[n-i+1]);
		swap(R[i],R[n-i+1]);
		swap(C[i],C[n-i+1]);
	}

	scanf("%d",&m);
	while (m--)
	{
		scanf("%d%d",&p,&x);
		printf("%lld\n",max(L[p-1]+R[p+1],C[p]-x+t[p]));
	}
}


Measured memory_bytes: 16128
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n, m = ns()
    table = [0 for _ in range(n)]

    for _ in range(m):
        a, b = ns()
        a -= 1
        b -= 1
        table[a] ^= 1
        table[b] ^= 1

    ans = max(table)

    print("NO" if ans else "YES")





if __name__ == '__main__':
    main()


Measured memory_bytes: 5992
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<climits>
#include<set>
#include<cmath>
#define ll long long
#define PI 3.14159265358979323846
using namespace std;
ll factorial(ll n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
double dist(vector<ll> x,vector<ll> y,int from ,int to) {
	return sqrt(pow(x[from]-x[to],2)+pow(y[from]-y[to],2));
}
double keisan(vector<ll>x, vector<ll>y,
	vector<bool>a, int from) {
	double res = 0;
	int count = 0;
	for (int i = 0; i < a.size(); i++) {
		if (!a[i]) {
			a[i] = true;
			res += keisan(x,y,a,i)+dist(x,y,from,i);
			a[i] = false;
			count++;
		}
	}
	if (count!=0) {
		return res/count;
	}
	else {
		return 0;
	}
}


int main() {
	double n, sum = 0;
	cin >> n;
	vector<ll>x(n), y(n);
	for (ll i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
	}
	/*
	for (ll i = 0; i < n - 1; i++) {
		for (ll j = i + 1; j < n; j++) {
			sum += sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
		}
	}
	printf("%.9f\n", sum * 2 * factorial(n - 1) / factorial(n));
	*/
	double ans = 0;
	vector<bool> a(n,false);
	for (int i = 0; i < n;i++) {
		a[i] = true;
		ans += keisan(x,y,a,i);
		a[i] = false;
	}
	printf("%.9f\n",ans/n);
}

Measured memory_bytes: 256
#include <iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    if (a == 1) {
        if (b == 2) {
            cout << "3" << endl;
        }
        else if (b == 3) {
            cout << "2" << endl;
        }
    }
    if (a == 2) {
        if (b == 1) {
            cout << "3" << endl;
        }
        if (b == 3) {
            cout << "1" << endl;
        }
    }
    if (a == 3) {
        if (b == 1) {
            cout << "2" << endl;
        }
        if (b == 2) {
            cout << "1" << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 256
from collections import Counter
N = int(input())
dic = Counter()
for _ in range(N):
  dic[input()] += 1
M = int(input())
for _ in range(M):
  dic[input()] -= 1
 
if dic.most_common():
  freq = dic.most_common()[0][1]
  print(max(freq, 0))
else:
  print(0)

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

int main(){
  int res = 0;
  int num;
  int tar;
  int height;
  cin >> num >> tar;
  for(int i = 0; i < num; i ++){
    cin >> height;
    if(height >= tar){
      res ++;}
  }
  cout << res << endl;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int D, G, p[11], c[11];
    cin >> D >> G;
    for(int i = 0; i < D; ++i){
        cin >> p[i] >> c[i];
    }

    int ans = 1e9;
    for(int mask = 0; mask < (1 << D); ++mask){
        int s = 0, num = 0, rest_max = -1;
        for(int i = 0; i < D; ++i){
            if(mask & (1 << i)){
                s += 100 * (i+1) * p[i] + c[i];
                num += p[i];
            }else{
                rest_max = i;
            }
        }
        if(s < G){
            int s1 = 100 * (rest_max + 1);
            int need = (G - s + s1 - 1) / s1;
            if(need >= p[rest_max]){
                continue;
            }
            num += need;
        }
        ans = min(ans, num);
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
 
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
 
int main(){
  int r;
  cin >> r;
  double pi = acos(-1);
  cout << 2*r*pi << endl;
}

Measured memory_bytes: 3784
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << ' ' << '=' << ' ' << (x) << endl;
#define fs first
#define sc second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef priority_queue<ll> PQI;
typedef priority_queue<P> PQP;
typedef vector<vector<P>> pvec;
typedef vector<vector<ll>> vvec;
typedef vector<ll> vec;
const vector<int> dx = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> dy = {1, 0, -1, 0, 1, -1, 1, -1};
const int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30);
const int INF = (1LL << 60);
const double EPS = (1 >> 30);
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}



signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    //---------------------------------------------
    
    rep(i,100000){
        int x;
        cin>>x;
        if(x==0) break;
        cout<<"Case "<<i+1<<": "<<x<<endl;
    }

}


Measured memory_bytes: 3220
// Aizu 0254: Scone
// 2017.9.12 bal4u@uu

#include <stdio.h>
#include <string.h>

int s[30003], f[100002];

int main()
{
	int n, m, i, a, ans, max;
	long long sum;

	while (scanf("%d%d", &n, &m) && n) {
		memset(f, -1, sizeof(int)*m);
		for (sum = 0, max = ans = s[0] = 0, i = 1; i <= n; i++) {
			scanf("%d", &a), sum += a; a %= m; if (a > max) max = a;
			if (a == m-1) ans = a;
			if ((s[i] = s[i-1] + a) >= m) s[i] -= m; f[s[i]] = i;
		}
		if (ans > 0) goto Done;
		if (max == 0) { ans = 0; goto Done; }
		if (sum < m) { ans = (int)sum; goto Done; }
		for (ans = m-1; ans > max; ans--) {
			for (i = 0; i <= n; i++) {
				if ((a = s[i] + ans) >= m) a -= m;
				if (f[a] >= i) goto Done;
			}
		}
Done:	printf("%d\n", ans);
	}
	return 0;
}

Measured memory_bytes: 2364
<?php /*atcoder.php*/

$at = new Atcoder();
$at->main();
unset($at);

class Atcoder{
	private function lineIn(){
		$buf = fgets(STDIN);
		return $this->killNewLine($buf);
	}
	private function killNewLine($buf = null){
		$buf = str_replace("\r\n", "", $buf);
		$buf = str_replace("\n", "", $buf);
		$buf = str_replace("\r", "", $buf);
		return $buf;
	}

	public function main(){
		$s = $this->lineIn();
		
		switch ($s){
			case ("A"):
			echo "T\n";
			break;
			case ("C"):
			echo "G\n";
			break;
			case ("G"):
			echo "C\n";
			break;
			case ("T"):
			echo "A\n";
			break;
			default:
			break;
		}
	}
}
?>


Measured memory_bytes: 4856
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli mw(vector<pair<lli,lli>>v,lli N,lli W)
{
    lli max_val = N*1000;
    lli inf = 1e9+1;
    lli dp[N+1][max_val+1];
    for(lli w=0;w<=max_val;w++)
    {
        dp[1][w] = inf;
    }
    dp[1][0] = 0;
    dp[1][v[1].second] = v[1].first;
    for(lli i=2;i<=N;i++)
    {
        for(lli w = 0;w<=max_val;w++)
        {
            dp[i][w] = dp[i-1][w];
            if(v[i].second>w)
            continue;
            dp[i][w] = min(dp[i][w],v[i].first+dp[i-1][(w-v[i].second)]);
        }
    }
    lli max = 0;
        for(lli w = 0;w<=max_val;w++)
        {
            if(dp[N][w]<=W)
            max = w;
        }
    return max;
}
int main() 
{
    lli n,w;
    cin>>n>>w;
    vector<pair<lli,lli>>v(n+1);
    for(lli i=1;i<=n;i++)
    {
        cin>>v[i].first>>v[i].second;
    }
    cout<<mw(v,n,w)<<"\n";
    //cout<<v[1].first<<" "<<v[1].second<<"\n";
}


Measured memory_bytes: 78336
# coding: utf-8
# author: ismdeep
# dateime: 2019-02-09 20:04:13
# filename: a.py.py
# blog: https://ismdeep.com
s = input()
n = int(s.split()[0])
k = int(s.split()[1])
if 2 * k - 1 <= n:
    print('YES')
else:
    print('NO')


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

#define M 1000000
#define L 12

char H[M][L];

int getChar(char ch){
  if(ch == 'A') return 1;
  else if(ch == 'C') return 2;
  else if(ch == 'G') return 3;
  else if(ch == 'T') return 4;
}

long long getKey(char str[]){
  long long sum = 0,p = 1,i;
  for(i = 0;i < strlen(str); i++){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){
  return key%M;
}
int h2(int key){
  return 1+(key%(M-1));
}

int find(char str[]){
  int i;
  long long key,h;
  key = getKey(str);
  for(i=0;;i++){
    h = (h1(key)+i*h2(key))%M;
    if(strcmp(H[h],str) == 0){
      return 1;
    }
    else if(strlen(H[h]) == 0){
      return 0;
    }
  }
  return 0;
}

int insert(char str[]){
  int i;
  long long key,h;
  key = getKey(str);
  for(i=0;;i++){
    h = (h1(key) +i*h2(key))%M;
    if(strcmp(H[h],str) == 0){
	return 1;
    }
    else if(strlen(H[h]) == 0){
      strcpy(H[h],str);
      return 0;
    }
  }
  return 0;
}

int main(){
  int i,n,h;
  char str[L],com[9];
  for(i=0;i<M;i++){
    H[i][0] = '\0';
  }

  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf("%s%s",com,str);

    if(com[0] == 'i'){
      insert(str);
    }
    else{
      if(find(str)){
	printf("yes\n");
      }
      else{
	printf("no\n");
      }
    }
  }
  return 0;
}

Measured memory_bytes: 12324
N,M=map(int,input().split())
a=[int(input()) for i in range(M)]
a=set(a)
dp=[0]*(N+1)
dp[0]=1
inf=10**9+7
if 1 not in a:
  dp[1]=1
else:
  dp[1]=0
for i in range(2,N+1):
  if i in a:
    continue
  else:
    dp[i]=dp[i-1]+dp[i-2]
ans=dp[N]%inf
print(ans)


Measured memory_bytes: 470264
#include <bits/stdc++.h>
using namespace std;
int N, A[3][3], b[10];
bool appear[3][3];
int main() {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) cin >> A[i][j];
}
cin >> N;
for(int i = 0; i < N; i++) cin >> b[i];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
appear[i][j] = false;
for(int k = 0; k < N; k++) {
if(A[i][j] == b[k]) appear[i][j] = true;
}
}
}
string ANS = "No";
for(int i = 0; i < 3; i++) {
if(appear[i][0] and appear[i][1] and appear[i][2]) ANS = "Yes";
}
for(int i = 0; i < 3; i++) {
if(appear[0][i] and appear[1][i] and appear[2][i]) ANS = "Yes";
}
if(appear[0][0] and appear[1][1] and appear[2][2]) ANS = "Yes";
if(appear[0][2] and appear[1][1] and appear[2][0]) ANS = "Yes";
cout << ANS << endl;
return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  cout << n * n * n << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[]a = new int[n];
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();
		long c1 = 0;
		long c2 = 0;
		long sign = 1;
		long sum = 0;
		for(int i = 0; i < n; i++) {
			sum += a[i];
			if(sum * sign <= 0) {
				c1 += Math.abs(sum) + 1;
				sum = sign;
			}
			sign *= -1;
		}
		sign = -1;
		sum = 0;
		for(int i = 0; i < n; i++) {
			sum += a[i];
			if(sum * sign <= 0) {
				c2 += Math.abs(sum) + 1;
				sum = sign;
			}
			sign *= -1;
		}
		System.out.println(Math.min(c1, c2));
	}
}

Measured memory_bytes: 51560
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
// 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(1e9 + 7)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0); 
    cout.tie(0);
	
    int r;
    cin >> r;
    cout << 3*r*r << '\n';
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
  int n,p[20],ans=0;
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> p[i];
  }
  for(int i=1; i<n-1; i++) {
    if((p[i-1]<p[i]&&p[i]<p[i+1])||(p[i-1]>p[i]&&p[i]>p[i+1])) {
      ans++;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
n = gets.to_i

zero = (%w(A C G T).product(*([%w(A C G T)] * 2)).map(&:join) - %w(AGC ACG GAC)).map do |str|
  [str, 0]
end.to_h

hash = Hash.new do |h, i|
  ret = zero.dup
  h[i-1].each do |k, v|
    %w(A C G T).each do |c|
      str = "#{k}#{c}"
      unless [/AGC/, /ACG/, /GAC/, /AG.C/, /A.GC/].any? {|reg| str =~ reg}
        ret[str[1..-1]] += v
      end
    end
  end
  h[i] = ret
end

hash[3] = (%w(A C G T).product(*([%w(A C G T)] * 2)).map(&:join) - %w(AGC ACG GAC)).map do |str|
  [str, 1]
end.to_h

ans = hash[n].inject(0) {|memo, (key, value)| memo + value}

puts (ans % (10**9 + 7))


Measured memory_bytes: 4732
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define max(x, y) (x) > (y) ? (x):(y)


using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;

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

LL lcm(LL a, LL b) {
	return a / gcd(a, b) * b;
}

bool pairbool(pLL &a, pLL &b)
{
	return (a.first < b.first);
}

int main(void)
{
	LL N, a, b, sum = 0L;
	vector<pLL> x;
	cin >> N;
	for (int i = 1; i <= N;i++) {
		cin >> a >> b;
		x.push_back(pLL(b, a));
	}
	sort(x.begin(), x.end());// , pairbool);
	for (int i = 0; i < N; i++) {
		sum += x[i].second;
		//cout << sum << endl;
		if (sum > x[i].first) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;

}

Measured memory_bytes: 6128
from itertools import permutations

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = [i for i in range(1,N+1)]
p = 0
q = 0
for i, l in enumerate(permutations(L)):
    if list(l) == P:
        p = i
    if list(l) == Q:
        q = i
print(abs(p-q))


Measured memory_bytes: 3064
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()
ans = 0
if N % 2 == 0:
  l = A[:N//2]
  h = A[N//2:]
  for i,j in zip(l, h):
    ans += (j - i) * 2 
  ans = ans - h[0] + l[-1]
else:
  l = A[:N//2]
  h = A[N//2+1:]
  m = A[N//2]
  for i,j in zip(l, h):
    ans += (j - i) * 2 
  ans = max(ans - h[0] + m, ans + l[-1] - m)
print(ans)

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

using namespace std;

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

    long r,d,g;

    cin >> r >> d >> g;

    for (int i=0;i<10;i++){
        g = r*g-d;
        cout << g << endl;
    }



    return 0;
}

Measured memory_bytes: 3652
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;


int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n, x;
    cin >> n >> x;
    for(int i = 0; i <= n; i++){
        if(i * 2 + (n - i) * 4 == x) return cout  << "Yes", 0;
    }
    cout << "No";

    return 0;

}



Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int m,n,i,w,x,y,z,t;
  string str;
  t=0;
  x=0;
  y=0;
  z=0;
  cin>>n;
  for(i=0; i<n; i++){
    cin>>str;
    w=str.size();
    if(str.at(w-1)=='A'&&str.at(0)=='B'){
      z++;
    }
    else if(str.at(0)=='B'){
      x++;
    }
    else if(str.at(w-1)=='A'){
      y++;
    }
      for(m=0; m<w-1; m++){
      if(str.at(m)=='A'&&str.at(m+1)=='B'){
        t++;
        
      }
    }
  }
  
  if(x+y==0&&z>0){
    t=t+z-1;
  }
  else if(x>=y){
    t=t+y+z;
  }
  else{
    t=t+x+z;
  }
  cout<<t;
}

Measured memory_bytes: 256
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme cout<<-1<<endl;return 0
//ios::sync_with_stdio(false);
//<< setprecision(5)
const int mod=1000000007;
const long double big=1e7+10;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int main(void){
	ios::sync_with_stdio(false);
	int i,j,h,w;cin>>h>>w;
	vector<string>ma(h+2);
	for(j=0;j<w+2;j++){ma[0].pub('.');}
	for(j=0;j<w+2;j++){ma[h+1].pub('.');}
	for(i=1;i<=h;i++){cin>>ma[i];ma[i].insert(ma[i].begin(),'.');ma[i].pub('.');}
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			int gen=0;
			if(ma[i][j]=='#'){continue;}
			if(ma[i-1][j-1]=='#'){gen++;}
			if(ma[i-1][j]=='#'){gen++;}
			if(ma[i-1][j+1]=='#'){gen++;}
			if(ma[i][j-1]=='#'){gen++;}
			if(ma[i][j]=='#'){gen++;}
			if(ma[i][j+1]=='#'){gen++;}
			if(ma[i+1][j-1]=='#'){gen++;}
			if(ma[i+1][j]=='#'){gen++;}
			if(ma[i+1][j+1]=='#'){gen++;}
			ma[i][j]=gen+'0';
		}
	}
	for(i=1;i<=h;i++){
		ma[i].pob();ma[i].erase(ma[i].begin());cout<<ma[i]<<endl;}
	return 0;
}

Measured memory_bytes: 888
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
     
using namespace std;
     
typedef long long ll;
typedef pair<ll, int> pii;
     
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     
mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count());

const int N = (int)1e5 + 9;
vector<int> T[N];
int k;
int res;

int dfs(int u){ 
    int ret;
    int mx = 0;
    for(auto x : T[u]){
        ret = dfs(x);
        if(u == 1){
            if(ret == k){
                res ++ ;
            }
        }
        else{
            if(ret + 1 == k){
                res ++ ;
            }
            else{
                mx = max(mx, ret + 1);
            }
        }
    }
    return mx;
    
}

int main(){
    fastIO;
    int n;
    cin >> n >> k;
    int p;
    cin >> p;
    if(p != 1)
        res ++ ;
    for(int i = 2; i <= n; i ++ ){
        cin >> p;
        T[p].push_back(i);
    }
    dfs(1);
    cout << res;
    return 0;
}

Measured memory_bytes: 8320
N = int(input())
s = input()
t = input()
ans = N
if s == t:
    print(ans)
    exit()
if N == 1:
    print(ans*2)
    exit()
ans += 1
for i in range(1, N):
    if s[i:] == t[:-1*i]:
        print(ans)
        exit()
    ans += 1
print(ans)

Measured memory_bytes: 9060
#include <bits/stdc++.h>
#define repr(i,from,to) for(int (i)=(from);(i)<(to);(i)++)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
const bool debug=true;
// const bool debug=false;
#define DEBUG if(debug==true)
#define vprint(x) for(auto a:(x)) cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
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;
}
int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h,w,a,b;cin>>h>>w>>a>>b;
    rep(i,h){
        rep(j,a){
            cout << int(i<b ? 0 : 1);
        }
        repr(j,a,w){
            cout << int(i<b ? 1 : 0);
            if(j==w-1) cout << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 1280
#include<stdio.h>
int main(void)
{
	int a,b,c;
	int tmp,i,j;
	
	scanf("%d %d %d",&a,&b,&c);
	
	if(a>b)
	{
		tmp = a;
		a = b;
		b = tmp;
	}
	
	if(a>c)
	{
		tmp = a;
		a = c;
		c = tmp;
	}
	
	if(b>c)
	{
		tmp = b;
		b = c;
		c = tmp;
	}
	
	printf("%d %d %d\n",a,b,c);
	
	return 0;
}

Measured memory_bytes: 1024
def main():
    A, B, C, D, E, F = (int(i) for i in input().split())

    water = set()
    for a in range(0,F+1,100*A):
        for b in range(0,F+1,100*B):
            if a + b != 0 and a+b <= F:
                water.add(a+b)

    max_consist = 0
    ans = (0,0)
    for w in water:
        ab = w //100
        sugar_max = E * ab
        x = sugar_max // C
        y = sugar_max // D
        s = max(C*i + D*j for i in range(x+1) for j in range(y+1) if C*i + D*j <= sugar_max and w + C*i + D*j <= F)
        if w + s <= F:
            cur_consist = (100*s)/(w+s)
            if max_consist <= cur_consist:
                max_consist = cur_consist
                ans = (w + s,s)

    print(*ans)
if __name__ == '__main__':
    main()

Measured memory_bytes: 39536
from collections import deque
import itertools
n,k=map(int,input().split())
v=deque(list(map(int,input().split())))
a=0
ans=0
while a<=n and a<=k:
  w=deque(itertools.islice(v,0,None))
  b=0
  A=deque([0])
  for i in range(a):
    p=w.popleft()
    A.append(p)
  while a+b<=n and a+b<=k:
    x=deque(itertools.islice(w,0,None))
    B=deque(itertools.islice(A,0,None))
    for j in range(b):
      B.append(x.pop())
    B=deque(sorted(B))
    c=0
    while c<k-a-b and B[0]<0 and c<a+b:
      B.popleft()
      c=c+1
    V=sum(B)
    if V>ans:
      ans=V
    b=b+1
  a=a+1
print(ans)

Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using pll=pair<ll,ll>;

#define fi first
#define se second
#define pb push_back
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define MOD 1000000007

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; }

int main() {
	int N;
	cin >> N;
	vector<string> S(N);
	ll ans = 0, counta = 0, countb = 0, countab=0;
	rep(i,N) {
		cin >> S[i];
		int n = S[i].length();
		if(S[i][0] == 'B') countb++;
		if(S[i][n-1] == 'A') counta++;
		if(S[i][0] == 'B' && S[i][n-1] == 'A') countab++;
	}
	rep(i,N) rep(j,S[i].length()-1) {
		if(S[i][j] == 'A' && S[i][j+1] == 'B') ans++;
	}
	if(countab == 0) ans += min(counta, countb);
	else if(countab == counta && countab == countb) ans += countab-1;
	else {
		/*if(counta > countab) {
			ans += 1;
			counta--;
		}
		if(countb > countab) {
			ans += 1;
			countb--;
		}
		ans += countab-1;
		counta -= countab;
		countb -= countab;
		ans += min(counta,countb);*/
		ans += min(counta-countab+1, countb-countab+1) + countab-1;
	}

	cout << ans << endl;

  return 0;
}


Measured memory_bytes: 768
k,n = gets.chomp.split.map(&:to_i)
a = gets.chomp.split.map(&:to_i)

def calc_distance(houses, start_point=nil, end_point=nil)
  return (houses[-1] - houses[0]) if start_point.nil?

  houses[(start_point - 1)] + end_point - houses[start_point]
end

distances = a.slice(0, a.length - 1).map.with_index { |e, i| a[(i+1)] - e }
start_point = distances.index(distances.max) + 1

puts  [calc_distance(a, start_point, k), calc_distance(a)].min

Measured memory_bytes: 18308
#include<bits/stdc++.h>
using namespace std;
using ll = long long; 
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<vector<int>>;
using VVV = vector<vector<vector<int>>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VVVL = vector<vector<vector<ll>>>;
template<class T> using pq = priority_queue<T>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
const ll BetsuMOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
int main() { // 問題文はしっかり読め!!!
	ll k, a, b;
	cin >> k >> a >> b;
	if (b - a <= 1) {
		cout << k + 1 << endl;
	}
	else {
		ll ans = 0;
		k -= a;
		if (k <= 0) {
			cout << k + a + 1 << endl;
			return 0;
		}
		ans += b;
		k--;
		ll cycle = k / 2;
		ans += (b - a) * cycle;
		if (k % 2) ans++;
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include <set>
#include <cmath>
#include <array>
#include <bitset>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<n+1;i++)
int main(){
    int N,W;
    cin>>N>>W;
    ll values[101][100001];
    ll val;
    int w;
    rep(i,W+1) values[0][i] = 0;
    Rep(i,N){
        cin>>w>>val;
        for(int ww=0;ww<W+1;ww++){
            if (ww < w) values[i][ww] = values[i-1][ww];
            else values[i][ww] = max(values[i-1][ww-w] + val,values[i-1][ww]);
        }
    }
    cout<<values[N][W];

    return 0;
}

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

A=[0]*N
B=[0]*N
for i in range(N):
    a,b=map(int, input().split())
    A[i]=a
    B[i]=b
count=0
#print(A,B)
for i in range(N-1,-1,-1):
    X=(A[i]+count)%B[i]
    if X!=0:
        res=B[i]-(A[i]+count)%B[i]
        count+=res

print(count)

Measured memory_bytes: 10868
#include <bits/stdc++.h>
#define INF 99999999
using namespace std;

typedef long long ll;

int main(){
	int n, x, m, sum = 0, ming = INF;
	cin >> n >> x;
	for(int i = 0; i < n; i++){
		cin >> m;
		sum += m;
		ming = min(ming, m);
	}
	x-=sum;
	cout << n+x/ming << endl;
    return 0;
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val a = readLine()?.split(" ")?.map(String::toInt) ?: return
    var i = 1
    var count = 0
    repeat(n) {
        if (a[it] == i) {
            i++
        } else {
            count++
        }
    }
    if (count == n) {
        println(-1)
    } else {
        println(count)
    }
}

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


int main(){
    int n,m; cin >> n >> m;
    vector<vector<int>> g(3*n);
    
    int u,v;
    rep(i,m){
        cin >> u >> v;
        u--;v--;
        g[u].push_back(v + n); //区間によってグループ分けをする
        g[u + n].push_back(v + 2*n);
        g[u + 2*n].push_back(v);
    }

    int s,t; cin >> s >> t;
    s--;t--;

    //幅優先探索
    int f[3*n]; //頂点を探索していく、未探索なら-1
    rep(i,3*n) f[i] = -1;
    f[s] = 0;
    queue<int> q;
    q.push(s);
    int now; //現在見ている頂点
    int size;
    int next;
    while(!q.empty()){
        now = q.front();
        q.pop();
        size = g[now].size();
        rep(i,size){
            next = g[now][i];
            if(f[next] == -1){
                q.push(next);
                f[next] = f[now] + 1;
            }
        }
    }

    if(f[t] == -1) cout << -1 << endl;
    else cout << f[t]/3 << endl;
}

Measured memory_bytes: 17792
#include <bits/stdc++.h>
#define ii pair<int,int>
#define ll long long int
#define ull unsigned long long int
#define pll pair<long int,long int>
#define vi vector<int>
#define vii vector<ii>
#define vvii vector<vii>
#define mod 1000000007
#define take(a,n) for(int i=0;i<n;i++)cin>>a[i]
using namespace std;


int main() {
	//code
    int n; string s; cin>>n; cin>>s; int ans=INT_MAX;
    vi w(n,0); vi e(n,0);
    w[0]=s[0]=='W'?1:0; e[0]=1-w[0];
    for(int i=1;i<n;i++){
        if(s[i]=='W')
        {w[i]+=w[i-1]+1;e[i]=e[i-1];}
        if(s[i]=='E')
        {e[i]+=e[i-1]+1;w[i]=w[i-1];}
        
    }
    for(int i=0;i<n;i++){
        if(s[i]=='W')
        ans=min(ans,w[i]-1+e[n-1]-e[i]);
        else
        ans=min(ans,e[n-1]-e[i]+w[i]);
    }
    cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 3076
#include<iostream>
#include<cmath>
#include<algorithm>
#include <cassert>
#include<string>
using namespace std;
int main() {
	int a = 0, b = 0, c = 0, d[100000], e[100000], f = 0, g = 0, h = 0, count = 0;
	char rikka, toka[100000];
	string saya, homu, kyok, mami,mado[100];
	bool gg = false;
	int manji, nk[26] = {false};
	cin >> a>>b;
	for (int i = 0; i < a; i++) {
		cin >> d[i];
	}
	sort(d, d + a);
	for (int j = 0; j < a; j++) {
		if (j == a - 1) {
			if (d[j] == b) {
				cout << a;
			}
			else { cout << a - 1; }
		}
		else if (d[j] <= b) {
			count++;
			b =b- d[j];
		}
		else { cout << count;
		return 0;
		}
	}
	return 0;
}

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


def search(i):
    if a[-1] < i:
        return 0
    mi = 0
    ma = n - 1
    while mi != ma:
        m = (mi + ma) // 2
        if a[m] >= i:
            ma = m
        else:
            mi = m + 1
    return n-mi

def countmax(i):
    ans = 0
    for j in a:
        ans += search(i - j)
    return ans

mi = 0
ma = a[-1] * 2
while mi != ma:
    md = (mi + ma) // 2 + 1
    #print(md, countmax(md))
    if countmax(md) > m:
        mi = md
    else:
        ma = md - 1

ans = 0
for j in a:
    i = search(mi + 1 - j)
    ans+=b[i]+i*j
ans += (m-countmax(mi+1))*mi
print(ans)


Measured memory_bytes: 59928