akhauriyash/Code-Regression huggingface dataset:
Program KnapSack2;
uses math;
var F:array[0..100005] of int64;
    V,W:array[1..105] of longint;
    i,j,n,k,sum :longint;
    res:longint;
    fi,fo:text;
//-----------------------------
begin
//assign(fi,'KnapSack2.inp');reset(fi);
readln(n,k);
for i:= 1 to n do begin
                  Readln(W[i],V[i]);
                  sum:=sum+V[i];
                  end;
for i:=1 to sum do begin
                   F[i]:=1000000000000;
                   end;
for i:=1 to n do begin
                 for j:=sum downto 1 do begin 
                                        if j-V[i]>=0 then begin
                                                          F[j]:=min(F[j],F[j-V[i]]+W[i]);
                                                          end;
                                        end;
                       end;
for i:=sum downto 1 do begin
                       if F[i]<=k then begin
                                       Writeln(i);
                                       break;
                                       end;
                       end;
//readln;
end.





Measured memory_bytes: 896
a, b = map(int, input().split())
c = 0
if a > b:
  c = c + a
  a = a - 1
else:
  c = c + b
  b = b - 1
if a > b:
  c = c + a
  a = a - 1
else:
  c = c + b
  b = b - 1
print(c)

Measured memory_bytes: 2940
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;
 
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
 
template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
    for (auto &e : t) fill(e, v);
}
 
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
 
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
 
 
int N;
vector<long long> A;
 
void solve() {
    long long zero = 0;
    long long cur = zero;
    for (int i = 0; i < N; ++i) {
        cur = A[i] - cur;
    }
    long long start = (cur - zero) / 2;
    cur = start;
    for (int i = 0; i < N; ++i) {
        cout << cur*2 << " ";
        cur = A[i] - cur;
    }
    cout << endl;
}
 
int main() {
    while (cin >> N) {
        A.resize(N);
        for (int i = 0; i < N; ++i) cin >> A[i];
        solve();
    }
}
 

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

int main(){
    int n;
    cin >> n;
    bool flag = false;
    for(int i=0; i<=n; i+=4){
        for(int j=0; j<=n; j+=7){
            if(i+j == n) flag = true;
        }
    }
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

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

int64_t MOD;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

int nth_bit(int64_t num, int n){
    return (num >> n) & 1;
}

int main(){
    int N, K;
    cin >> N >> K >> MOD;

    static int64_t dp[201][11][2048], dp2[201][11][2048];
    dp[0][1][1] = 1;
    int B = 1<<(K+1);
    for(int t=0; t<N; t++){
        memset(dp2, 0, sizeof(dp2));
        for(int i=0; i<2*N; i++) for(int j=1; j<=K; j++) for(int b=0; b<B; b++){
            if(dp[i][j][b] == 0) continue;
            int ad = 0;
            while(nth_bit(b, ad)) ad++;
            if(ad > K) continue;
            int i2 = i+ad;
            int b2 = (b>>ad) + 1;
            for(int j2=1; j2<=K; j2++){
                if(nth_bit(b2, j2)) continue;
                if(i2+j2-i-j > K) continue;
                if(i2+j2 <= i+j) continue;
                if(i2+j2 > 2*N) continue;
                add(dp2[i2][j2][b2|(1<<j2)], dp[i][j][b]);
            }
        }
        for(int i=0; i<2*N; i++) for(int j=1; j<=K; j++) for(int b=0; b<B; b++) dp[i][j][b] = dp2[i][j][b];
    }

    int64_t ans = 0;
    for(int i=0; i<2*N; i++) for(int j=1; j<=K; j++) for(int b=0; b<B; b++) add(ans, dp[i][j][b]);
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 71168
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

int main()
{
	int a[100], n,i;
	cin >> n;
	for (i = 0;i < n;i++)
	{
		cin >> a[i];
	}
	if (n > 0)cout << a[n - 1];
	for (i = n - 2; i >= 0; i--)
	{
		cout << ' ' << a[i];
	}
	cout << endl;
}

Measured memory_bytes: 1160
X = int(input())
ans = list()
min = float("inf")
r = range(-200,200)
for A in r:
  for B in r:
    if A**5-B**5==X:
      if A**5-B**5< min:
        if len(ans)!=0: ans.pop()      
        ans.append([A,B])
        min = A**5-B**5
        break
      elif A**5-B**5==min:
        ans.append([A,B])
        break
ans=ans[1]
A = ans[0]
B = ans[1]
print(A, B)

Measured memory_bytes: 9120
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            var n = cin.ReadInt;
            var a = cin.ReadIntArray(n).OrderBy(x => x).ToArray();
            if (n % 2 == 1) a = a.Skip(1).ToArray();
            for (int i = 0; i < (int)(n & -2); i += 2)
            {
                if (a[i] != a[i + 1])
                {
                    WriteLine("0");
                    return;
                }
            }
            WriteLine(PowMod(2, n >> 1, 1000000007));
        }

        public static long PowMod(long x, long n, long m)
        {
            long ans = 1;
            long mul = x;
            for (; n > 0; n >>= 1, mul = (mul * mul) % m)
            {
                if ((n & 1) == 1) ans = (ans * mul) % m;
            }
            return ans % m;
        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue<string> inputStream;
        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); }}
        public int ReadInt { get { return int.Parse(Read); }}
        public long ReadLong { get { return long.Parse(Read); }}
        public double ReadDouble { get { return double.Parse(Read); }}
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;}
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;}
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;}
    }
}


Measured memory_bytes: 24160
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from functools import lru_cache


@lru_cache(maxsize=2*10**5)
def f2(x):
    if x == 0: return 0
    y = bin(x).count("1")
    return 1 + f2(x % y)

def resolve():
    N = int(input())
    S = input()
    cnt0 = S.count("1")

    if cnt0 == 0:
        print(*[1]*N, sep="\n")
        return

    X = int(S, 2)

    dc = {}
    for k, v in (("0", 1), ("1", -1)):
        x = cnt0 + v
        y = X % x if x else 0
        dc[k] = (x, v, y, [1, N-1])

    ans = [0] * N
    for i in reversed(range(N)):
        si = S[i]
        cnt, v, n, m1 = dc[si]

        if cnt == 0:
            ans[i] = 0
            continue

        ni = N - i - 1
        m2 = (m1[0]<<(m1[1]-i)) % cnt
        n = (n + v*m2) % cnt
        m1[0] = m2; m1[1] = i

        ans[i] = 1 + f2(n)

    print(*ans, sep="\n")


if __name__ == "__main__":
    resolve()


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

int main () {
    string S;
    cin >> S;
    int n = S.length();
    long long res = 0;
    for(int i = 0; i<(1<< (n - 1)); i++) {
        long long tmp = 0;
        for(int j = 0; j<n; j++) {
            tmp = tmp * 10 + S[j] - '0';
            if(i & (1<<j)) {
                res += tmp;
                tmp = 0;
            }
        }
        res += tmp;
    }
    printf("%ld", res);
}

Measured memory_bytes: 256
h, w = map(int, input().split())
G = []
l_w = [[0] * w for i in range(h)]
l_h = [[0] * w for i in range(h)]
for i in range(h):
    G.append(input())
for i in range(h):
    cnt = 0
    for j in range(w):
        if G[i][j] == ".":
            cnt += 1
            l_w[i][j] = cnt
        else:
            cnt = 0
    tmp = 0
    for j in range(w - 1, -1, -1):
        if l_w[i][j] > 0 and tmp == 0:
            tmp = l_w[i][j]
        elif l_w[i][j] > 0 and tmp > 0:
            l_w[i][j] = tmp
        elif l_w[i][j] == 0 and tmp > 0:
            tmp = 0
for i in range(w):
    cnt = 0
    for j in range(h):
        if G[j][i] == ".":
            cnt += 1
            l_h[j][i] = cnt
        else:
            cnt = 0
    tmp = 0
    for j in range(h - 1, -1, -1):
        if l_h[j][i] > 0 and tmp == 0:
            tmp = l_h[j][i]
        elif l_h[j][i] > 0 and tmp > 0:
            l_h[j][i] = tmp
        elif l_h[j][i] == 0 and tmp > 0:
            tmp = 0
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, l_w[i][j] + l_h[i][j] - 1)
print(ans)

Measured memory_bytes: 126172
import numpy as np
import random
import time 

random.seed(1111)
start=time.time()
D=int(input())
C=np.array(list(map(int,input().split())))
last_day=np.array([-1 for _ in range(26)])
score=np.array([list(map(int,input().split())) for _ in range(D)])
output=[0 for _ in range(D)]

#初期貪欲法
for day in range(D):
    today_point=C*(day-last_day)
    S_today=sum(today_point)
    for k in range(26):
        today_point[k]=score[day][k]-5*S_today+5*today_point[k]
 
    if day!=D-1:
        max_score=-1*float("inf")
        for k in range(26):
            nday_point=C*((day+1)-last_day)
            S_nday=sum(nday_point)-C[k]*(day-last_day[k])
            
            for i in range(26):
                if k!=i:
                    nday_point[i]=score[day+1][i]-5*S_nday+5*nday_point[i]
                else:
                    nday_point[i]=score[day+1][i]-5*S_nday+5*C[i]
 
                if today_point[k]+nday_point[i]>max_score:
                    max_score=today_point[k]+nday_point[i]
                    idx=k
    else:
        idx=np.argmax(today_point)
    
    last_day[idx]=day
    output[day]=idx+1

def score_cal(output):
    ans=0
    memo=np.array([-1 for _ in range(26)])
    for day in range(D):
        t=output[day]-1
        memo[t]=day
        tmp=C*(memo-day)
        ans=ans+score[day][t]+sum(tmp)
    return ans

  
random_day=[i for i in range(D)]
random_con=[i+1 for i in range(26)]

cnt=0
while time.time()-start<1.85:
    tmp_output=output[:]
    cnt+=1
    d=random.choice(random_day)
    con=random.choice(random_con)
    tmp_output[d]=con
    
    if score_cal(tmp_output)>score_cal(output):
        output[d]=con
      
print(*output,sep="\n")


Measured memory_bytes: 27496
#include <stdio.h>
#define MARK 4
#define NUM 13

int zero(int*);

int main(){
  int card[MARK][NUM];
  char mark;
  int num;
  int roop;
  int i;
  int j;

  zero(&card[0][0]);

  scanf("%d", &roop);
  
  for(i = 0;i < roop;i++){
    scanf("%c", &mark);
    if(mark == '\n'){
      while(1){
	scanf("%c", &mark);
	if(mark != '\n') break;
      }
    }
    scanf("%d", &num);
    --num;
    if(mark == 'S') card[0][num] = 1;
    else if(mark == 'H') card[1][num] = 1;
    else if(mark == 'C') card[2][num] = 1;
    else if(mark == 'D') card[3][num] = 1;
  }

  for(i = 0;i < MARK;i++){
    for(j = 0;j < NUM;j++){
      if(card[i][j] == 0){
	if(i == 0) printf("S");
	else if(i == 1) printf("H");
	else if(i == 2) printf("C");
	else if(i == 3) printf("D");
	printf(" %d\n", j+1);
      }
    }
  }
	

  
  return 0;
}

int zero(int *x){
  int *p = x;
  for(;p < x + MARK*NUM;p++)
    *p = 0;
}



Measured memory_bytes: 2044
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T>
vector<vector<T>> dijkstra(const WeightedGraph<T> &g, int s, int mod) {
    const T INF = numeric_limits<T>::max();
    vector<vector<T>> dist(g.size(), vector<T>(mod, INF));
    using Pi = tuple<T, int, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s][0] = 0; que.emplace(dist[s][0], s, 0);
    while (!que.empty()) {
        T cost; int v, r; tie(cost, v, r) = que.top(); que.pop();
        if (dist[v][r] < cost) continue;
        for (auto &e: g[v]) {
            int nv = e.dst; T nc = e.cost;
            int nr = (r + e.cost) % mod;
            if (dist[nv][nr] <= dist[v][r] + nc) continue;
            dist[nv][nr] = dist[v][r] + nc;
            que.emplace(dist[nv][nr], nv, nr);
        }
    }
    return dist;
}

int main() {
    const auto INF = numeric_limits<int>::max();
    int n, m; cin >> n >> m;
    WeightedGraph<int> g(n);
    while (m--) {
        int u, v; cin >> u >> v; u--, v--;
        g[u].emplace_back(v, 1);
    }
    int s, t; cin >> s >> t; s--, t--;
    int res = dijkstra(g, s, 3)[t][0];
    if (res == INF) {
        cout << -1 << endl;
    } else {
        cout << res / 3 << endl;
    }
    return 0;
}

Measured memory_bytes: 11136
#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
int32_t main()
{
    IOS;
    int n, k;
    cin >> n >> k;
    int a[n];
    for(int i = 0;i < n; i++)
        cin >> a[i];
    bool dp[k+1];
    memset(dp,0,sizeof(dp));
    for(int i = 1;i <= k; i++){
        for(int j = 0; j < n; j++){
            if(a[j]<=i && !dp[i-a[j]]){
                dp[i] = true;
            }
        }
    }
    if(dp[k])
        cout << "First" << "\n";
    else
        cout << "Second " << "\n";
    return 0;
}

Measured memory_bytes: 3588
#include <bits/stdc++.h>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin>>n;
    int h[n][3];
    for (int i = 0; i < n; i++) {
        cin>>h[i][0];
        cin>>h[i][1];
        cin>>h[i][2];
    }
    int d[3];
    d[0] = h[0][0];
    d[1] = h[0][1];
    d[2] = h[0][2];
    for (int i = 1; i < n; i++) {
        int dd[3];
        dd[0] = max(d[1], d[2]) + h[i][0];
        dd[1] = max(d[0], d[2]) + h[i][1];
        dd[2] = max(d[0], d[1]) + h[i][2];
        d[0] = dd[0];
        d[1] = dd[1];
        d[2] = dd[2];
    }
    cout<<max(d[0], max(d[1], d[2]))<<endl;
    return 0;
}


Measured memory_bytes: 1408
#include <cstdio>
#include <cstring>
const int nmax=100023;
char ch[nmax];
int n;
int sol[nmax];
int left(int pos)
{
    if(pos==0) return n-1;
    return pos-1;
}
int right(int pos)
{
    if(pos==n-1) return 0;
    return pos+1;
}
int check(int pos)
{
    int ct=0;
    while(1)
    {
        if(sol[left(pos)]!=0&&sol[right(pos)]!=0) ++ct;
        if(ct==n+2) break;
        if(sol[pos]==1)
        {
            if(ch[pos]=='o')
            {
                if(sol[left(pos)]!=sol[right(pos)]&&sol[right(pos)]!=0&&sol[left(pos)]!=0) return 0;
                sol[right(pos)]=sol[left(pos)];
            }
            else if(ch[pos]=='x')
            {
                if(sol[left(pos)]==sol[right(pos)]) return 0;
                sol[right(pos)]=3-sol[left(pos)];
            }
        }
        else
        {
            if(ch[pos]=='x')
            {
                if(sol[left(pos)]!=sol[right(pos)]&&sol[right(pos)]!=0&&sol[left(pos)]!=0) return 0;
                sol[right(pos)]=sol[left(pos)];
            }
            else
            {
                if(sol[left(pos)]==sol[right(pos)]) return 0;
                sol[right(pos)]=3-sol[left(pos)];
            }
        }
        pos=right(pos);
    }
    return 1;
}
void reset()
{
    for(int i=0;i<n;i++) sol[i]=0;
}
int main()
{
   // freopen ("in.in","r",stdin);
    scanf("%d",&n);
    scanf("%s",ch);
    int pos=-1;
    for(int i=0;i<n;i++)
    {
        if(ch[i]=='o')
        {
            pos=i;
            break;
        }
    }
    if(pos==-1)
    {
        printf("-1\n");
        return 0;
    }
    sol[pos]=1,sol[left(pos)]=1,sol[right(pos)]=1;
    if(check(right(pos)))
    {
        for(int i=0;i<n;i++)
        {
            if(sol[i]==1) printf("S");
            else printf("W");
        }
        return 0;
    }
    reset();
    sol[pos]=1,sol[left(pos)]=2,sol[right(pos)]=2;
    if(check(right(pos)))
    {
        for(int i=0;i<n;i++)
        {
            if(sol[i]==1) printf("S");
            else printf("W");
        }
        return 0;
    }
    reset();
    sol[pos]=2;
    sol[left(pos)]=1,sol[right(pos)]=2;
    if(check(right(pos)))
    {
        for(int i=0;i<n;i++)
        {
            if(sol[i]==1) printf("S");
            else printf("W");
        }
        return 0;
    }
    reset();
    sol[pos]=2;
    sol[left(pos)]=2,sol[right(pos)]=1;
    if(check(right(pos)))
    {
        for(int i=0;i<n;i++)
        {
            if(sol[i]==1) printf("S");
            else printf("W");
        }
        return 0;
    }
    printf("-1\n");
}


Measured memory_bytes: 768
n,m,x =map(int, input().split())
Ain = list(map(int,input().split()))
A=[0 for i in range(n+1)]
for i in Ain:
    A[i] = 1
ans1=0
ans2=0
for i in range(x):
    ans1 += A[i]

for i in range(x,n):
    ans2 += A[i]

print(min(ans1, ans2))
    

Measured memory_bytes: 3064
from collections import deque

N, M = [int(x) for x in input().split()]

friends_map = {n : set() for n in range(N)}
remain_user = set()
for m in range(M):
    a, b = [int(x) - 1 for x in input().split()]

    friends_map[a].add(b)
    friends_map[b].add(a)
    remain_user.add(a)
    remain_user.add(b)

stack = deque()
stack.appendleft(0)

max_group = 1
watch = [0] * N
count = 0
while stack:
    now = stack.popleft()

    if watch[now] == 0:
        remain_user.discard(now)
        watch[now] = 1
        count += 1

        for next_node in friends_map[now]:
            if watch[next_node] == 0:
                stack.appendleft(next_node)

    if not stack:
        max_group = max(count, max_group)

        if remain_user:
            stack.appendleft(remain_user.pop())
            count = 0

print(max_group)


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

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  rep(i,N) {
    cin >> a.at(i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  int alice = 0;
  int bob = 0;
  int turn = 0; //0:Alice, 1:Bob
  rep(i,N) {
    if(turn == 0 ){
      alice += a.at(i);
    }
    else {
      bob += a.at(i);
    }
    //for next loop
    turn = 1 - turn;
  }
  cout << alice - bob << endl;
}


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

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define copy(from, to) copy(from.begin(), from.end(), back_inserter(to));
#define sort(a) sort(a.begin(), a.end())

typedef long long ll;
using namespace std;

template<class T>
void load(vector<T> &v, int n) {
  for (int i = 0; i < n; ++i) {
    T t;
    cin >> t;
    v.push_back(t);
  }
}

int main() {
  vector<int> v;
  load(v, 5);

  int k;
  cin >> k;

  for (int i = 0; i < 4; ++i) {
    for (int j = i + 1; j < 5; ++j) {
      if (abs(v[i] - v[j]) > k) {
        cout << ":(" << endl;
        return 0;
      }
    }
  }
  cout << "Yay!" << endl;
}

Measured memory_bytes: 256
package main

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

var (
	h, w int
	s    []string
	ch   = make(chan [][]int, 4)
)

func calcLeft() {
	ls := make([][]int, h)
	for i := range ls {
		ls[i] = make([]int, w)
		for j := 0; j < w; j++ {
			if s[i][j] == '#' {
				ls[i][j] = 0
				continue
			}
			if j == 0 {
				ls[i][j] = 1
				continue
			}
			ls[i][j] = ls[i][j-1] + 1
		}
	}
	ch <- ls
}

func calcRight() {
	rs := make([][]int, h)
	for i := range rs {
		rs[i] = make([]int, w)
		for j := w - 1; j >= 0; j-- {
			if s[i][j] == '#' {
				rs[i][j] = 0
				continue
			}
			if j == w-1 {
				rs[i][j] = 1
				continue
			}
			rs[i][j] = rs[i][j+1] + 1
		}
	}
	ch <- rs
}

func calcTop() {
	ts := make([][]int, h)
	for i := range ts {
		ts[i] = make([]int, w)
	}
	for j := 0; j < w; j++ {
		for i := 0; i < h; i++ {
			if s[i][j] == '#' {
				ts[i][j] = 0
				continue
			}
			if i == 0 {
				ts[i][j] = 1
				continue
			}
			ts[i][j] = ts[i-1][j] + 1
		}
	}
	ch <- ts
}

func calcDown() {
	ds := make([][]int, h)
	for i := range ds {
		ds[i] = make([]int, w)
	}
	for j := 0; j < w; j++ {
		for i := h - 1; i >= 0; i-- {
			if s[i][j] == '#' {
				ds[i][j] = 0
				continue
			}
			if i == h-1 {
				ds[i][j] = 1
				continue
			}
			ds[i][j] = ds[i+1][j] + 1
		}
	}
	ch <- ds
}

func main() {
	fmt.Scan(&h, &w)

	sc := bufio.NewScanner(os.Stdin)
	s = make([]string, h)
	for i := range s {
		sc.Scan()
		s[i] = sc.Text()
	}

	go calcLeft()
	go calcRight()
	go calcTop()
	go calcDown()

	s1 := <-ch
	s2 := <-ch
	s3 := <-ch
	s4 := <-ch
	ans := 0
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if v := s1[i][j] + s2[i][j] + s3[i][j] + s4[i][j] - 3; v > ans {
				ans = v
			}
		}
	}
	fmt.Println(ans)
}


Measured memory_bytes: 138292
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
#define INF ((long long)1e18)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define MAX 110000

int s[MAX], t[MAX], ans[MAX];
pair<int,int> x[MAX];

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int A, B, Q, a, b, c, d;
	cin>>A>>B>>Q;
	rep(i,A)cin>>s[i];rep(i,B)cin>>t[i];rep(i,Q){cin>>x[i].first;x[i].second = i;}
	sort(x,x+Q);
	for(int i = 0, j = 0, k = 0; i < Q; i++){
		for(;j < A && x[i].first >= s[j];j++);for(;k < B && x[i].first >= t[k];k++);
		a = j?x[i].first-s[j-1]:INF,b = j-A?s[j]-x[i].first:INF,c = k?x[i].first-t[k-1]:INF,d = k-B?t[k]-x[i].first:INF;
		ans[x[i].second] = min(min(max(a,c),max(b,d)),min(a>d?d*2+a:a*2+d,b>c?c*2+b:b*2+c));
	}
	rep(i,Q) cout<<ans[i]<<"\n";
	return 0;
}

Measured memory_bytes: 5376
#include<cstdio>
int n, s;
long long int x[100100];
long long int p[100100];
long long int stime[100100];
void calcans(int l, int r) {
	if (x[r] <= s) {
		for (int i = l; i <= r; i++) {
			stime[i] = s - x[i];
		}
	}
	else if (x[l] >= s) {
		for (int i = l; i <= r; i++) {
			stime[i] = x[i] - s;
		}
	}
	else {
		if (p[l] >= p[r]) {
			p[l] += p[r];
			calcans(l, r - 1);
			stime[r] += stime[l] + x[r] - x[l];
		}
		else {
			p[r] += p[l];
			calcans(l + 1, r);
			stime[l] += stime[r] + x[r] - x[l];
		}
	}
}
int main() {
	scanf("%d%d", &n, &s);
	for (int i = 0; i < n; i++) {
		scanf("%lld%lld", &x[i], &p[i]);
	}
	calcans(0, n - 1);
	long long int ans = 0;
	for (int i = 0; i < n; i++) {
		if (ans < stime[i])ans = stime[i];
	}
	printf("%lld", ans);
	return 0;
}


Measured memory_bytes: 5120
n, a, b = gets.split.map(&:to_i)
ac = a + b
f = 0
bb = 0

ct = n % ac == 0 ? n / ac : n / ac + 1
f = ac * ct
bb = a * ct

if(f > n && f - n > b)
  bb -= f - n - b
end

puts bb

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

using namespace std;
#define sqr(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))
#define GCD(a, b) __gcd(a, b)
#define LCM(a, b) ((a)*(b)/GCD(a,b))
#define PI (3.141592653589793238462643383279502884197169399375105820974944)
#define MP make_pair
#define n1 first
#define n2 second
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PDB pair<db,db>
#define SI set<int>
#define SL set<ll>
#define MS multiset
#define MSI multiset<int>
#define MSLL multiset<ll>
#define PB push_back
#define PF push_front
#define VI vector<int>
#define VPI vector<pair<int,int>>
#define VLL vector<ll>
#define VS vector<string>
#define SZ(x) ((int)x.size())

typedef long long ll;

typedef unsigned long long ull;
typedef long double ld;
typedef double db;
const int INF = 1000000007;
const ll LINF = 1000000000000000555;
const int N = 550555;
const int L = 21;
const int M = 2155;
const int K = 555;

char c[M];
int n, k, cnt = 0;
void rec(int idx){
    if (idx == n){
        cnt++;
        if (cnt == k){
            for (int i = 0; i < n; i++) cout << c[i];
        }
        return;
    }
    if (idx == 0){
        for (int i = 1; i <= 9; i++){
            c[idx] = i+'0';
            rec(idx+1);
        }
    } else {
        int x = c[idx-1] - '0';
        for (int i = max(0, x-1); i <= min(x+1, 9); i++){
            c[idx] = i+'0';
            rec(idx+1);
        }
    }
}

void solve(){
    cin >> k;
    for (int i = 1; i <= 10; i++){
        n = i;
        rec(0);
    }
}

int main() {
    //ios_base::sync_with_stdio(false);
#ifdef dxz05
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif

    int T = 1;
    //cin >> T;

    while (T--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}

template<typename... Ts>
auto make_v(size_t a,Ts... ts){
        return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

class union_find{
        int n_; 
        vector<int> par, rank, size;
public:
        union_find(int n): n_{n} {
                par.resize(n_);
                rank.resize(n_);
                size.resize(n_);

                REP(i, n_){
                        par[i] = i;
                        rank[i] = 1;
                        size[i] = 1;
                }
        } 

        bool same(int x, int y) {
                return find(x) == find(y);
        }

        void unite(int x, int y) {
                x = find(x);
                y = find(y);

                if(x == y) return;

                if(rank[x] > rank[y]){
                        par[y] = x;
                        size[x] += size[y];
                } else {
                        par[x] = y;
                        size[y] += size[x];
                        if(rank[x] == rank[y]) rank[y]++;
                }
        }

        int find(int x) {
                if(par[x] == x) return x;
                else return par[x] = find(par[x]);
        }

        int get_size(int x) {
                return size[find(x)];
        }
};

using graph = vector<vector<int>>;

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

        union_find uf(n);
        graph g1(n), g2(n);

        REP(i, m) {
                int a, b;
                cin >> a >> b;
                --a; --b;
                g1[a].push_back(b);
                g1[b].push_back(a);

                uf.unite(a, b);
        }

        REP(i, k) {
                int a, b;
                cin >> a >> b;
                --a; --b;
                g2[a].push_back(b);
                g2[b].push_back(a);
        }

        REP(i, n) {
                int size = uf.get_size(i) - 1;
                size -= g1[i].size();

                for (int j : g2[i]) {
                        if (uf.same(i, j)) {
                                --size;
                        }
                }

                cout << size << endl;
        }

        return 0;
}


Measured memory_bytes: 13048
#include<bits/stdc++.h>
#include<iomanip>
#include<numeric>

using namespace std;
using ll = long long;
constexpr ll mod = 1e9+7;
constexpr ll md = mod;
constexpr ll inf = 1e15;
ll fact[1000000],inv[1000000];
ll mpow(ll a,ll n){
    ll tmp = 1;
    while(n > 0){
        if(n%2) (tmp *= a)%=mod;
        (a *= a)%=mod;
        n>>=1;
    }
    return tmp;
}
auto prime(ll m){
    vector<ll> v;
    for(int i=2;i*i<=m;++i){
        if(m%i==0){
            while(m%i == 0)v.push_back(i),m/=i;
        }
    }
    if(m != 1)v.push_back(m);
    return v;
}
ll combi(int n,int r){
    if(n == 0 or r == 0)return 1;
    return fact[n] * inv[r]%mod * inv[n-r]%mod;
}
int main(){
    ll n ,m;
    cin>>n>>m;
    auto v = prime(m);
    fact[0] =1;
    for(int i=1;i<=1000000;++i)fact[i] = fact[i-1] * i %mod;
    inv[1000000] = mpow(fact[1000000],mod-2);
    for(int i=1000000-1;i>=0;--i)inv[i] = inv[i+1]*(i+1)%mod;
    auto uni = v;
    uni.erase(unique(uni.begin(),uni.end()),uni.end());
    ll ret = 1;
    for(auto a : uni){
        int num = upper_bound(v.begin(),v.end(),a) - lower_bound(v.begin(),v.end(),a);
        (ret *= combi(num+n-1,num))%= mod;
    }
    cout << ret << endl;
    return 0;
}



Measured memory_bytes: 15872
# -*- coding: utf-8 -*-
N, k = [int(n) for n in input().split()]
W = []

def search(W, k, P):
    tmp = 0
    track = 1
    for w in W:
        if tmp + w <= P:
            tmp += w
        else:
            track += 1
            tmp = w
    if track > k:
        return False
    else:
        return True         
        

for n in range(N):
    W.append(int(input()))

left = max(sum(W) // N, max(W))
right = sum(W)

while right - left > 1 :
    mid = (left + right)//2
    if search(W, k, mid):
        right = mid
    else:
        left = mid
    
if search(W, k, left):
    print(left)
else:
    print(right)  

Measured memory_bytes: 11636
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define uni(x) x.erase(unique(rng(x)),x.end())
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define dup(x,y) (((x)+(y)-1)/(y))
#define v(T) vector<T>
#define vv(T) v(v(T))
using ll = int64_t;
using ull = uint64_t;
using uint = uint32_t;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pil = pair<int,ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(v.size())o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
constexpr double eps = 1e-6;
constexpr ll LINF = 100200300400500600ll;
constexpr int INF = 2*((1<<30)-1)+1;
//--------------------------------------------------------
#define fastio cin.tie(nullptr); std::ios::sync_with_stdio(false);
#define myio ifstream iofi; if(argc>1){iofi.open(argv[1]); std::cin.rdbuf(iofi.rdbuf());}
#define myloopset int loop = 1; if(argc>2) loop =stoi(argv[2]); rep(i,loop)
//--------------------------------------------------------------
template <class T> T chmin(T &a,T b){if(a>b){a=b; return 1;}else return 0;}
template <class T> T chmax(T &a,T b){if(a<b){a=b; return 1;}else return 0;}
constexpr int dx[4] = {0,1,0,-1};
constexpr int dy[4] = {-1,0,1,0};
constexpr int mod = 1000000007;
#define Yes(ifeq) cout<<(( ifeq )? "Yes":"No")<<endl;

void Main(){
  ll n,k;
  cin>>n>>k;
  ll sum=0;
  srep(i,k,n+2){
    ll a = i;
    sum += n*(n+1)/2 - (n-a+1)*(n-a)/2  - (a)*(a-1)/2 + 1;
    if(sum>=mod) sum %=mod;
    // cout<<sum<<endl;
  }
  cout<<sum<<endl;

  
} signed main(int argc,char** argv)
{fastio; myio; myloopset Main();}


Measured memory_bytes: 3648
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)) {
			

			int n = sc.nextInt();
			int m = sc.nextInt();

			int nComb = n * (n-1) / 2;
			int mComb = m * (m-1) / 2;
			
			System.out.println(nComb + mComb);
			
		}
	}

}

Measured memory_bytes: 21972
import itertools
N,M,Q=map(int,input().split())
List = []
for i in range (Q):
  List.append(list(map(int, input().split())))
List1 = []
res = 0
mid = 0
for i in range(M):
  List1.append(i)
x = itertools.combinations_with_replacement(List1, N)
tryList= list(x)

for tryElement in tryList:
  mid = 0
  for j in range(Q):
    if tryElement[List[j][1]-1]-tryElement[List[j][0]-1]==List[j][2]:
      mid += List[j][3]
  res = max(mid,res)
print(res)

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


int main(){
        long long int x;
        cin >> x;
        int count = 0;
        long long int amt = 100;
        while(x - amt > 0) {
                long long int CI = amt/100;
                amt += CI;
                count++;
                //cout << "in\n";
        }
        cout << count << endl;
}


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

namespace CompetitiveProgCsLib
{
	class MainProg
	{
		static string R => Console.ReadLine();
		static int RI => int.Parse(R);
		static double RD => double.Parse(R);
		static int[] RIs => R.Split(' ').Select(int.Parse).ToArray();
		static double[] RDs => R.Split(' ').Select(double.Parse).ToArray();
		static void Out(object obj) => Console.WriteLine(obj);
		static void OutLine<T>(IEnumerable<T> obj, string separateChar) => Out(String.Join(separateChar, obj));
		static void Outb(bool b) => Out(b ? "Yes" : "No");
		static void OutB(bool b) => Out(b ? "YES" : "NO");
		static bool IsIn(int y, int x, int h, int w) => 0 <= x && x < w && 0 <= y && y < h;

		static void Main(string[] args)
		{
			var n = RI;
			var h = RI;
			var w = RI;
			Out((n - h + 1) * (n - w + 1));
		}
	}
}


Measured memory_bytes: 13140
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
 
using namespace std;
 
int main(){
    long long N,H;
    cin>>N>>H;
    vector <long long>a(N);
    vector <long long>b(N);
    for(int i=0 ; i<N ; i++){
        cin>>a[i];
        cin>>b[i];
    }
    long long ans=0;
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    for(long long i=N-1 ; i>=0 ; i--){
        if(a[N-1]>=b[i] || H<=0)break;
           H-=b[i];
           ans+=1;
    }
    if(H>0){
        ans+=(long long)(H/a[N-1]);
        if(H%a[N-1]!=0)ans+=1;
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1792
import java.util.*;

public class Main{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int d = sc.nextInt();
    int x = sc.nextInt();
    int a[] = new int [n];
    for(int i = 0;i < n;i++){
		a[i] = sc.nextInt();
    }
    int count = n + x;
    for(int i = 0;i < n;i++){
      for(int j = 1;j < d;j++){
        if(j * a[i] + 1 <= d){
          count++;
        }
      }
    }
    System.out.println(count);
  }
}

Measured memory_bytes: 21844
'''
制約
2 ≤ N ≤ 3×10^5(300,000)

入力例 3
8
WWWWWEEE

出力例 3
3

考え方：
Eを0, Wを1と置くと、
0000000X11111111 と並ぶようにすればよい。
そのために、値を変える数が最小になるリーダーの位置を調べればよい

-> 左端のW(West), 右端のE(East)はほぼ必ず向きを変える必要がある。


A. 全パターン試す
ある地点より左のWの数 + 右のEの数　の合計が最小になるパターンを求める（文字一致でカウント）
-> TLE

B. 全パターン
str.count('W')でやる
-> TLE

C. 「累積和」(cumulative sum)というアルゴリズムを使わないと解けない問題。学習して、使う！


'''

# from collections import deque # キュー、スタックに利用

# sys.stdin.readline() # 標準入力の高速化に利用
import sys
input = sys.stdin.readline

is_debug = False


def main():
    '''メイン処理'''
    n = int(input())
    sss = input()

    s_list = list(sss)

    W_sums = [0] * n # 累積和を入れるリスト
    E_sums = [0] * n # 累積和を入れるリスト
    W_sum = 0 # 左端から数えて、向きを変える必要のあるWの数
    E_sum = 0 # 右端から数えて、向きを変える必要のあるWの数
    
    if is_debug:
        print(W_sums)
    
    for i in range(n):
        if s_list[i] == 'W':
            W_sum += 1
        W_sums[i] = W_sum
        if s_list[n-i] == 'E':
            E_sum += 1
        E_sums[(n-1)-i] = E_sum

    if is_debug:
        print(W_sums)
        print(E_sums)
    
    # W_sums, E_sumsを用いて、向きを変える人数が最小となる値を見つける
    min_cost = float('inf')
    for i in range(n):
        # Wを振り向かせる人数
        if i == 0:
            W_cost = 0
        else:
            W_cost = W_sums[i-1]
        # Eを振り向かせる人数
        if i == n-1:
            E_cost = 0
        else:
            E_cost = E_sums[(i-1)+1]

        if is_debug:
            print(i, W_cost, E_cost)

        cost = W_cost+E_cost
        if cost < min_cost:
            min_cost = cost
    
    print(min_cost)

if __name__ == "__main__":
    # ファイル実行時に、main()関数を実行
    main()



Measured memory_bytes: 20440
# A - Connection and Disconnection
S = input()
K = int(input())
head = 0
head_char = S[0]
head_rec = True
tmp = 0
char = ''
ans = 0
for i in range(len(S)):
    if head_rec==True:
        if S[i]==head_char:
            head += 1
        else:
            head_rec = False
    if S[i]!=char:
        char = S[i]
        ans += tmp//2
        tmp = 0
    tmp += 1
ans += tmp//2
ans *= K
if head==len(S):
    ans = (len(S)*K)//2
elif head_char==char and (tmp*head)%2!=0:
    ans += K-1
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
typedef long long ll;
typedef long double ld;
typedef long long unsigned int llu;
ll MOD = 1000000007;
ll INF = 1000000009;

/*
Xのi番目とYのj番目までのLCS
*/

int lcs[1010][1010];

void solve(){

    int q;
    cin >> q;
    vector<int> v;
    rep(i,q){
        string s,t;
        cin >> s;
        cin >> t;
        rep(i,1010){
            rep(j,1010){
                lcs[i][j]=0;
            }
        }
        for(int i=0; i<s.length(); i++){
            for(int j=0; j<t.length(); j++){
                if(s[i]==t[j]) lcs[i+1][j+1]=lcs[i][j]+1;
                else lcs[i+1][j+1]=max(lcs[i+1][j], lcs[i][j+1]);
            }
        }
        v.emplace_back(lcs[s.length()][t.length()]);
    }

    for(auto e: v){
        cout << e << endl;
    }
}

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



Measured memory_bytes: 7212
def bubbleSort(C, N):
    for i in range(0, N):
        for j in range(i+1, N):
            if int(C[N-j+i][1]) < int(C[N-j+i-1][1]):
                (C[N-j+i], C[N-j+i-1]) = (C[N-j+i-1], C[N-j+i])
    print(" ".join(C))

def selectionSort(C, N):
    count = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if int(C[j][1])<int(C[minj][1]):
                minj = j
        if minj != i:
            (C[i], C[minj]) = (C[minj], C[i])
            count += 1
    print(" ".join(C))

def isStable(C1, C2, N):
    for i in range(N):
        if C1[i] != C2[i]:
            return "Not stable"
    return "Stable"

n = int(input())
card = list(input().split())
card2 = card[:]

bubbleSort(card, n)
print("Stable")
selectionSort(card2, n)
print(isStable(card, card2, n))







Measured memory_bytes: 5608
#include <iostream>
using namespace std;
typedef long long ll;
int main (void) {
    int N;
    ll MARCH[5] = {0};

    cin >> N;
    for (int i = 0; i < N; i++) {
        string S;
        cin >> S;
        if (S[0] == 'M') MARCH[0]++;
        else if (S[0] == 'A') MARCH[1]++;
        else if (S[0] == 'R') MARCH[2]++;
        else if (S[0] == 'C') MARCH[3]++;
        else if (S[0] == 'H') MARCH[4]++;
    }

    ll ans = 0;
    ans += MARCH[0] * MARCH[1] * MARCH[2];
    ans += MARCH[0] * MARCH[1] * MARCH[3];
    ans += MARCH[0] * MARCH[1] * MARCH[4];
    ans += MARCH[0] * MARCH[2] * MARCH[3];
    ans += MARCH[0] * MARCH[2] * MARCH[4];
    ans += MARCH[0] * MARCH[3] * MARCH[4];
    ans += MARCH[1] * MARCH[2] * MARCH[3];
    ans += MARCH[1] * MARCH[2] * MARCH[4];
    ans += MARCH[1] * MARCH[3] * MARCH[4];
    ans += MARCH[2] * MARCH[3] * MARCH[4];

    cout << ans << endl;

    return 0;
}

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

using namespace std;

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

  long long x; cin >> x;
  long long ret = x / 500 * 1000;
  x = x % 500;
  ret += x/5 * 5;
  cout << ret << endl;
}


Measured memory_bytes: 256
import numpy as np

import sys
input = sys.stdin.buffer.readline

n, k = map(int, input().split())
a = np.array(input().split(), np.int64)
f = np.array(input().split(), np.int64)

a.sort()
f.sort()

f_ = f[::-1]

left = -1
right = (a*f_).sum()
while left + 1 < right:
  mid = (left + right) // 2
  r = np.maximum(0, a - mid//f_).sum() <= k
  if r:
    right = mid
  else:
    left = mid
print(right) 


Measured memory_bytes: 26184
#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"


signed main(){
  cin.tie(0);	
  ios::sync_with_stdio(false);
  int n;
  cin>>n;
  int mp[10][10]={0};
 
  rep(i,n){
    int num=i+1;
    string s=to_string(num);
    mp[s[0]-'0'][s[s.size()-1]-'0']++;
  }
  int ans=0;
  /*rep(i,10){
    rep(j,10){
      cout<<mp[i][j]<<" ";
    }
    cout<<endl;
    }*/
  rep(i,9){
    rep(j,9){
      ans+=mp[i+1][j+1]*mp[j+1][i+1];
    }
  }
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cerr << #x << ": " << x << endl
const int INF = 1000000000;
int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  rep(i, n) cin >> x[i];
  sort(all(x));
  int begin = x[0];
  int end = x[n - 1] + 1;
  int ans = INF;
  for(int p = begin; p < end; p++) {
    int sum = 0;
    rep(i, n) sum += (x[i] - p) * (x[i] - p);
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
# https://atcoder.jp/contests/atc001/tasks/unionfind_a
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]
    
    def groups(self):
        groups = dict()
        for i in range(self.n):
            p = self.find(i)
            if not groups.get(p):
                groups[p] = set()
            groups[p].add(i)
        return list(groups.values())

def main():
    n,m = map(int, input().split())
    uf = UnionFind(n)
    for _ in range(m):
        a,b = map(lambda x: int(x) -1, input().split())
        uf.union(a,b)
    print(len(uf.groups())-1)

if __name__ == '__main__':
    main()

Measured memory_bytes: 34288
S = input()

V = {}
V[S[0]] = 1
for i in range(1,4):
  if S[i] not in V:
    V[S[i]] = 1
  else:
    V[S[i]] += 1
ret = 'No'
if len(V) == 2:
  check = True
  for k,v in V.items():
    if v != 2:
      check = False
      break
    
  if check == True:
    ret = 'Yes'
    
print(ret)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

int main(){
	int N;
	cin >> N;
	vector<ll> A(N);
	REP(i, N) cin >> A[i];
	REP(i, N) A[i] -= (i+1);
	sort(A.begin(), A.end());
	ll b;
	if (N%2==0){
		b = (A[N/2-1] + A[N/2])/2;
	}else{
		b = A[N/2];
	}
	ll res = 0;
	REP(i, N){
		res += abs(A[i] - b);
	}
	cout << res << endl;
    return 0;
}

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

struct Node{
	int idx;
	int v[2];
	int h[2];
};

//A B C D E F G H I
//0 1 2 3 4 5 6 7 8 

struct Node nodes[400000];
int connecth[9][2] = {{1, 8}, {2, 0}, {3, 1}, {4, 2}, {5, 3}, {6, 4}, {7, 5}, {8, 6}, {0, 7}}; 
int connectv[9][2] = {{6, 3}, {7, 4}, {8, 5}, {0, 6}, {1, 7}, {2, 8}, {3, 0}, {4, 1}, {5, 2}};
map<vector<int>, int> nummap;

void init(){
	vector<int> num(9);
	iota(num.begin(), num.end(), 0);
	int idx = 0;
	do{
		nummap[num] = idx;
		nodes[idx].idx = idx;
		idx++;
	}while(next_permutation(num.begin(), num.end()));

	iota(num.begin(), num.end(), 0);
	idx = 0;
	do{
		nummap[num] = idx;
		int zi = 0;
		for(int i = 0; i < 9; i++) if(num[i] == 0) zi = i;

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

			swap(num[zi], num[connecth[zi][i]]);
			nodes[idx].h[i] = nummap[num];
			swap(num[zi], num[connecth[zi][i]]);

			swap(num[zi], num[connectv[zi][i]]);
			nodes[idx].v[i] = nummap[num];
			swap(num[zi], num[connectv[zi][i]]);
		}
		idx++;
	}while(next_permutation(num.begin(), num.end()));
}

typedef pair<int, int> P;
int CH, CV;
int dijkstra(int si, int ei){
	vector<int> dist(400000, 1000000000);
	dist[si] = 0;
	priority_queue<P, vector<P>, greater<P> > queue;
	queue.push({0, si});
	while(queue.size()){
		P pp = queue.top();
		queue.pop();
		if(pp.second == ei) return pp.first;

		int vd = pp.first + CV;
		int hd = pp.first + CH; 
		for(int i = 0; i < 2; i++){
			if(dist[nodes[pp.second].h[i]] > hd){
				dist[nodes[pp.second].h[i]] = hd;
				queue.push({hd, nodes[pp.second].h[i]});
			}
			if(dist[nodes[pp.second].v[i]] > vd){
				dist[nodes[pp.second].v[i]] = vd;
				queue.push({vd, nodes[pp.second].v[i]});
			}
		}
	}
	return 1000000000;
}

int main(){
	init();
	while(cin >> CH >> CV, CH != 0 && CV != 0){
		vector<int> start(9), end(9);
		for(int i = 0; i < 9; i++){
			cin >> start[i];
		}
		for(int i = 0; i < 9; i++){
			cin >> end[i];
		}
		int si = nummap[start];
		int ei = nummap[end];
		cout << dijkstra(si, ei) << endl;
	}
}

Measured memory_bytes: 60104
def main():
    from math import ceil
    n, a, b, *k = map(int, open(0).read().split())
    k.sort()

    l = 0
    r = k[-1]
    m = (l + r) // 2
    while l + 1 < r:
        c = sum(ceil((i - m * b) / (a - b)) if i - m * b > 0 else 0 for i in k)
        if c <= m:
            r = m
        else:
            l = m
        m = (l + r) // 2

    print(r)


if __name__ == '__main__':
    main()


Measured memory_bytes: 20132
n = int(input())
arr = list(map(int, input().split()))
c, v = 0, 0
for i in arr:
  if i % 2 == 0:
    c += 1
    if i % 3 == 0 or i % 5 == 0:
      v += 1
print('APPROVED' if c == v else 'DENIED')

Measured memory_bytes: 3060