akhauriyash/Code-Regression huggingface dataset:
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

class Program
{
    static void Main()
    {
        var N = int.Parse(ReadLine());
        var u = new int[N - 1];
        var v = new int[N - 1];
        var w = new int[N - 1];
        for (int i = 0; i < N - 1; i++)
        {
            var line = ReadLine().Split(' ');
            u[i] = int.Parse(line[0]) - 1;
            v[i] = int.Parse(line[1]) - 1;
            w[i] = int.Parse(line[2]);
        }

        var tree = new List<Edge>[N];
        for (int i = 0; i < tree.Length; i++) tree[i] = new List<Edge>();
        for (int i = 0; i < N - 1; i++)
        {
            tree[u[i]].Add(new Edge { to = v[i], dist = w[i] });
            tree[v[i]].Add(new Edge { to = u[i], dist = w[i] });
        }

        var ans = new bool[N];
        var q = new Queue<Triple>();
        q.Enqueue(new Triple { from = -1, to = 0, color = false });
        while (q.Count != 0)
        {
            var e = q.Dequeue();
            ans[e.to] = e.color;
            foreach (var a in tree[e.to]) if (e.from != a.to) q.Enqueue(new Triple { from = e.to, to = a.to, color = a.dist % 2 == 0 ? e.color : !e.color });
        }

        foreach (var c in ans) WriteLine(c ? "1" : "0");
    }
}

struct Edge
{
    public int to;
    public int dist;
}

struct Triple
{
    public int from;
    public int to;
    public bool color;
}


Measured memory_bytes: 30200
n = input()
A = map(int, raw_input().split())
q = input()
M = map(int, raw_input().split())

min = min(A)
sum = sum(A)

def solve(i,m):
  if sum < m:
    return False
  if m == 0:
    return True
  if i >= n:
    return False
  res = solve(i+1,m) or solve(i+1, m - A[i])
  return res
 
for m in M:
    if solve(0, m):
      print 'yes'
    else:
      print 'no'
 

Measured memory_bytes: 6452
S=["SUN","MON","TUE","WED","THU","FRI","SAT"]
T=input()
if T=="SUN":
	print(7)
if T=="MON":
	print(6)
if T=="TUE":
	print(5)
if T=="WED":
	print(4)
if T=="THU":
	print(3)
if T=="FRI":
	print(2)
if T=="SAT":
	print(1)


Measured memory_bytes: 3060
using System;
using System.Linq;
public class Program {
  public static void Main() {
    int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
    int A = input[0], B = input[1], C = input[2], X = input[3], Y = input[4];

    int[] types = new int[3];
    types[0] = (2 * Math.Max(X, Y) * C);
    types[1] = (A * X) + (B * Y);
    if (X < Y) {
      types[2] = (2 * X * C) + ((Y-X) * B);
    } else if (X > Y) {
      types[2] = (2 * Y * C) + ((X-Y) * A);
    } else {
      types[2] = int.MaxValue;
    }

    Console.WriteLine(types.Min());
  }
}

Measured memory_bytes: 13396
s=input()
a=[]
  
def convert(s): 
  
    # initialization of string to "" 
    new = "" 
  
    # traverse in the string  
    for x in s: 
        new += x  
  
    # return string  
    return new 
for i in range(len(s)):
  if i%2==0:
    a.append(s[i])
print(convert(a))

Measured memory_bytes: 133912
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);

bool DP[85][85][30000];
bool ok[85][85][2];
bool ok1[85][85][2];
int H, W;
int A[85][85], B[85][85];

void solve(int i, int j, bool ue) {
  if(ue) {
    ok1[i][j][1] = true;
    if(!ok1[i][j][0]) return;
  }
  else {
    ok1[i][j][0] = true;
    if(!ok1[i][j][1]) return;
  }
  if(ue) {
    if(ok[i][j][1]) return;
    ok[i][j][1] = true;
  }
  else {
    if(ok[i][j][0]) return;
    ok[i][j][0] = true;
  }
  if(i+1<H) {
    for(int k=0;k<30000;++k) {
      if(DP[i][j][k]==true) {
        DP[i+1][j][k+A[i+1][j]-B[i+1][j]] = true;
        DP[i+1][j][k-(A[i+1][j]-B[i+1][j])] = true;
      }
    }
    solve(i+1, j, true);
  }
  if(j+1<W) {
    for(int k=0;k<30000;++k) {
      if(DP[i][j][k]==true) {
        DP[i][j+1][k+A[i][j+1]-B[i][j+1]] = true;
        DP[i][j+1][k-(A[i][j+1]-B[i][j+1])] = true;
      }
    }
    solve(i, j+1, false);
  }
}

int main() {
  cin>>H>>W;
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) cin>>A[i][j];
  }
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) cin>>B[i][j];
  }
  DP[0][0][15000+A[0][0]-B[0][0]] = true;
  DP[0][0][15000-(A[0][0]-B[0][0])] = true;
  for(int i=0;i<H;++i) {
    ok1[i][0][0] = true;
  }
  for(int j=0;j<W;++j) {
    ok1[0][j][1] = true;
  }
  solve(0, 0, true);
  int ans = inf;
  /**
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      cout<<i<<" "<<j<<" ";
      for(int k=0;k<30000;++k) {
        if(DP[i][j][k]==true) {
          cout<<k-15000<<" ";
        }
      }
      cout<<endl;
    }
  }
  **/
  for(int i=0;i<30000;++i) {
    if(DP[H-1][W-1][i]==true) {
      ans = min(ans, abs(i-15000));
    }
  }
  cout<<ans<<endl;
}



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

int main(){
  int64_t N,A,B;
  cin >> N >> A >> B;
  int64_t answer=0;
  
  int64_t r;
  r = N % (A+B);
  
  answer += (N/(A+B))*A + min(A,r);
  
  cout << answer << endl;
}

Measured memory_bytes: 3608
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
            c[i] = sc.nextInt();
        }

        long[][] dp = new long[n][3];
        dp[0][0] = a[0];
        dp[0][1] = b[0];
        dp[0][2] = c[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = a[i] + Math.max(dp[i - 1][1], dp[i - 1][2]);
            dp[i][1] = b[i] + Math.max(dp[i - 1][2], dp[i - 1][0]);
            dp[i][2] = c[i] + Math.max(dp[i - 1][0], dp[i - 1][1]);
        }

        long max = 0;
        for (int i = 0; i < 3; i++)
            max = Math.max(max, dp[n - 1][i]);

        System.out.println(max);

    }
}


Measured memory_bytes: 61536
function Main(input){
  input = input.trim().split('\n').map(x=>(x.split(' ').map(y=>parseInt(y))));
  var hasCandy = Array(input[0][0]).fill(0);
  var row = 1;
  while (row < input.length){
    for (let i=0;i<input[row+1].length;i++){
      hasCandy[input[row+1][i] - 1] = 1;
    }
    row += 2;
  }
  var zero = 0;
  for (let i=0;i<hasCandy.length;i++){
    if (hasCandy[i] == 0){
      zero++;
    }
  }
  console.log(zero);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 29696
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;

int main(){
  int n;
  cin>>n;
  for(int i=1;i<=n;i++){
    int a;
    cin>>a;
    if(a%2==0){
    if(a%3!=0&&a%5!=0){
      cout<<"DENIED"<<endl;
      return 0;
    }}
  }
  cout<<"APPROVED"<<endl;
}


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

namespace AtTest.ABC_138
{
    class B
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            double[] array = ReadDoubles();
            double val = 0;
            for(int i = 0; i < n; i++)
            {
                val += 1 / array[i];
            }
            WriteLine(1 / val);
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 11220
d, g = map(int, input().split())
pc = [tuple(map(int, input().split())) for i in range(d)]

ans = float('inf')

for i in range(2**d):
    sum = 0
    cnt = 0
    nokori = set(range(1, d+1))
    for j in range(d):
        bitchk = 2**j
        if i & bitchk:
            sum += pc[j][0]*100*(j+1) + pc[j][1]
            cnt += pc[j][0]
            nokori.discard(j+1)
    if sum < g:
        use = max(nokori)
        # -(-(g-sum)//(use*100))は切り上げ除算
        # pc[use-1][0]はその点数帯の問題数（解ける最大数）
        # -(-(g-sum)//(use*100))でgを超えるのに必要な問題数を出せる。
        # ex) 300点問題が4問残ってるとする。不足している点数(g-sum)が500点のとき
        # -(-500//300)で2問解く必要があることがわかる。
        n = min(pc[use-1][0], -(-(g-sum)//(use*100)))
        # n = (-(-(g-sum)//(use**100)))
        sum += n * use * 100
        cnt += n

    if sum >= g:
        ans = min(ans, cnt)

print(ans)


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

typedef long long ll;

const int N = 50;
ll arr[N + 5];

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll tot =0 ;
	int n;
	cin>>n;
	for(int i = 0;i < n;i++) {
		cin>>arr[i];
		for(int j = 0;j < i;j++) tot += arr[i] * arr[j];
	}
	cout<<tot<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

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

    ll n;
    cin >> n;
    map<ll, ll> mp;
    for(int i = 2; (ll)i * i <= n; ++i){
        while(n % i == 0){
            ++mp[i];
            n /= i;
        }
    }
    if(n != 1)  ++mp[n];
    ll ans = 0;
    for(auto it : mp){
        ll bound = it.second;
        ll lb = 1, ub = it.second + 1;
        while(ub - lb > 1){
            ll mid = (lb + ub) / 2;
            ll val = mid * (mid + 1) / 2;
            if(val <= bound)    lb = mid;
            else    ub = mid;
        }
        ans += lb;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3656
N = int(input())
A = list(map(int, input().split()))

cnt = [0] * (10 ** 5 + 1)

for a in A:
    cnt[a] += 1

ans = 0
for i in range(1, 10 ** 5):
    now = sum(cnt[i-1:i+2])
    ans = max(ans, now)
print(ans)



Measured memory_bytes: 20820
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

struct UnionFind {
	vector<int> v;
	UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
	int  find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
	void unite(int x, int y) { v[find(x)] = find(y); }
};
struct p3 {
	double x;
	double y;
	double z;
};
int main() {
	int n, m;
	cin >> n >> m;
	vector<p3> ps(n);
	REP(i, n)
		cin >> ps[i].x >> ps[i].y >> ps[i].z;

	double ma=0.0;
	REP(k, 1 << n) {
		int cnt = 0;
		REP(i, n)
			if ((k>>i) % 2)
				cnt++;
 		if (cnt != m)
			continue;
		double tmp = 0;
		REP(i, n - 1)
			FOR(j, i + 1, n)
			if(((k>>i)%2) && ((k>>j) % 2))
				tmp += pow(ps[i].x - ps[j].x, 2.0) + pow(ps[i].y - ps[j].y, 2.0) + pow(ps[i].z - ps[j].z, 2.0);
		ma = max(ma, tmp);
	}
		
	printf("%.10lf",ma);
}

Measured memory_bytes: 3216
/*

f(n,k) = answer if there is no restriction
f(n,k) = choose(n+k-1,n)
g(i) = answer with the restriction on i

obviously, use the g(2k+2-i) = g(i) optimization once k+2 is reached

if k was large enough, these formulae are (probably) true
g(2) = g(3) = f(n,k-1)+f(n-1,k-1)
g(4) = g(5) = f(n,k-2)+2*f(n-1,k-2)+f(n-2,k-2)

i have no idea why

*/

#include <stdio.h>

const int MOD=998244353;

long long pascal[4010][4010];

long long f(int n,int k)
{
    if (n+k-1<0) return 0;
    if (n<0) return 0;
    return pascal[n+k-1][n];
}

int n,k;

long long ans[4010];

int main()
{
    #ifdef NOT_DMOJ
    freopen("data.txt","r",stdin);
    #endif // NOT_DMOJ
    pascal[0][0]=1;
    for (int i=1; i<4005; i++) {
        pascal[i][0]=1;
        for (int j=1; j<4005; j++) {
            pascal[i][j]=(pascal[i-1][j-1]+pascal[i-1][j])%MOD;
        }
    }
    scanf("%d%d",&k,&n);
    for (int i=1; i<=k; i++) {
        for (int j=0; j<=i; j++) {
            ans[i]=(ans[i]+f(n-j,k-i)*pascal[i][j])%MOD;
        }
    }
    for (int i=2; i<=2*k; i++) {
        int j=i;
        if (j>2*k+2-j) j=2*k+2-j;       // g(2k+2-i) = g(i) optimization
        printf("%lld\n",ans[j/2]);
    }
}


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

int main()
{
  int n,k,ans=0;
  cin>>n>>k;
  vector<int> v(n);
  for(auto &i:v) cin>>i;
  sort(v.rbegin(),v.rend());
  for(int i=0;i<k;i++) ans+=v[i];
  cout<<ans<<"\n";


  return 0;
}


Measured memory_bytes: 256
class Dice:
    def __init__(self,u,s,e,w,n,d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d
        self.dir = {'W':0,'S':1,'E':2,'N':3}
    
    def rot(self,way):
        if isinstance(way,str):
            way = self.dir[way]
        
        if way == 0:
            c = self.u
            self.u = self.e
            self.e = self.d
            self.d = self.w
            self.w = c
        elif way == 1:
            c = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = c
        elif way == 2:
            c = self.u
            self.u = self.w
            self.w = self.d
            self.d = self.e
            self.e = c
        else:
            c = self.u
            self.u = self.s
            self.s = self.d
            self.d = self.n
            self.n = c
    
u,s,e,w,n,d = map(int, input().split())
dice = Dice(u,s,e,w,n,d)
direct = input()

for d in direct:
    dice.rot(d)

print(dice.u)
    
            
        


Measured memory_bytes: 5608
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
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;
}

int N;
long long a[100010][3];
long long dp[100010][3];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < 3; ++j) cin >> a[i][j];

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        if (j == k) continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }

  long long res = 0;
  for (int j = 0; j < 3; ++j) chmax(res, dp[N][j]);
  cout << res << endl;
}

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

h = list(map(int,input().split()))

ans = 0
minh = min(h)
ans += min(h)

h = list(map(lambda x: x-minh, h))

if set(h) == {0}:
    print(ans)

else:
    while True:
        rightflag = False

        for i in range(N):
            if h[i] == 0:
                continue
            else:
                left = i
                break

        for j in range(left,N):
            if h[j] != 0:
                continue
            else:
                right = j-1
                rightflag = True
                break

        if not rightflag:
            right = N-1

        # print(left,right)

        tmp = min(h[left:right+1])
        ans += tmp

        for pos in range(left,right+1):
            h[pos] -= tmp

        # print(h)
        if set(h) == {0}:
            break

    print(ans)

Measured memory_bytes: 3064
n= int(input())
l =list(map(int,input().split()))
ans= 1

if 0 in l:
    print("0")
else:
    for i in range(n):
        ans*= l[i]
        if ans> 10**18:
            break
    if ans> 10**18:
        print("-1")
    else:
        print(ans)

Measured memory_bytes: 21480
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TypeFamilies #-}
import           Data.Maybe
import           Data.Int
import           Data.List
import           Data.Char
-- import qualified Data.Vector.Unboxing as VU
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as BS
import           Data.List (scanl')
import           Data.IORef
import           Control.Monad
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Control.Applicative
import           Debug.Trace
import           Text.Printf


readInt = BS.readInt . BS.dropWhile isSpace
getVector f = VU.unfoldr f <$> BS.getLine
getIntVector = getVector readInt
getBSVector = getVector BS.uncons
getInt = readLn :: IO Int
getInt2Tuple = (\vec -> (vec VU.! 0, vec VU.! 1)) <$> getIntVector
getInt3Tuple = (\vec -> (vec VU.! 0, vec VU.! 1, vec VU.! 2)) <$> getIntVector


main = do
  n <- getInt
  bs <- getIntVector
  print $ VU.sum $ solve n bs

solve :: Int -> VU.Vector Int -> VU.Vector Int
solve n bs = VU.create $ do
  as <- VUM.new n
  VUM.write as 0 $ bs VU.! 0
  forM_ [1..n-2] $ \i -> do
    VUM.write as i $ min (bs VU.! i) (bs VU.! (i-1))

  VUM.write as (n-1) $ bs VU.! (n-2)
  return as


Measured memory_bytes: 508
#include<iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int main(){
  int n;
  cin>>n;
  int a[n+1];
  for(int i = 1;i<=n;i++){
    cin>>a[i];
  }
  int ans = 0;
  for(int i = 1;i<=n;i++){
    if(a[i]==i){
      if(i != n){
	int tmp = a[i];
	a[i] = a[i+1];
	a[i+1] = tmp;
      }else if(i == n){
	int tmp = a[i];
	a[i] = a[i-1];
	a[i-1] = tmp;
      }
      ans++;
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 768
n = int(input())
setting = []
for _ in range(n-1):
    setting.append(list(map(int, input().split())))

results = []
for i in range(n-1):
    cur = 0
    for j in range(i, n-1):
        if cur < setting[j][1]:
            cur = setting[j][0] + setting[j][1]
        elif 0 == cur % setting[j][2]:
            cur += setting[j][0]
        else:
            cur += setting[j][0] + setting[j][2] - cur % setting[j][2]
    results.append(cur)
results.append(0)
for result in results:
    print(result)


Measured memory_bytes: 3188
#include<bits/stdc++.h>
#define maxn 75
#define rep(i,j,k) for(int i=(j),LIM=(k);i<=LIM;i++)
#define per(i,j,k) for(int i=(j),LIM=(k);i>=LIM;i--)
#define mod 1000000007
#define vi vector<int>
#define pb push_back
using namespace std;

int n,K,fac[maxn << 2],invf[maxn << 2],inv[maxn << 2];
int C(int a,int b){ if(a<0 || b<0 || a-b<0) return 0;return fac[a] * 1ll * invf[b] % mod * invf[a-b]%mod; }
char s[maxn];
vi p;int ans;

bool check(){
	static bool vis[maxn];
	static int loc[maxn];
	memset(vis,0,sizeof vis);
	int j=1;
	for(int i=p.size()-1;i>=0 && p[i];i--){
		for(;j <= K && s[j] != 'r';j++);
		if(j > K) return 0;
		vis[j] = 1 , loc[i] = j , j++;
	}
	j=1;
	for(int i=p.size()-1;i>=0 && p[i];i--){
		j = max(j , loc[i]);
		for(;j <= K && s[j] != 'b';j++);
		if(j > K) return 0;
		vis[j] = 1 , loc[i] = j , j++;
	}
	j=1;
	for(int i=0;i<p.size() && p[i]==0;i++){
		for(;j <= K && (s[j] != 'r' || vis[j]);j++);
		if(j > K) return 0;
		vis[j] = 1 , j++;
	}
	j=1;
	for(int i=p.size()-1;i>=0 && p[i] > 1;i--){
		j = max(j , loc[i]);
		rep(k,2,p[i]){
			for(;j <= K && vis[j];j++);
			if(j > K) return 0;
			vis[j] = 1 , j++;
		}
	}
	return 1;
}

void dfs(int sz,int mx,int lim){
	if(sz > n) return;
	if(check()){
		int sm = fac[p.size()];
		for(int i=0,j;i<p.size();i=j){
			for(j=i;j<p.size() && p[j] == p[i];j++);
			sm = 1ll * sm * invf[j-i] % mod;
		}
		ans = (ans + 1ll * C(n-sz+mx-1,mx-1) * sm) % mod;
		//printf("%d %d %d %d\n",sm,ans,sz,mx);
	}
	else return;
	rep(i,lim,(n-sz+1)/2){
		p.pb(i);
		dfs(sz+max(2*i-1,1)+(sz!=0),mx+2*i+2,i);
		p.pop_back();
	}
}

int main(){//freopen("1.in","r",stdin);//freopen("2.out","w",stdout);
	scanf("%d%d",&n,&K);
	scanf("%s",s+1);
	fac[0] = inv[0] = inv[1] = fac[1] = invf[0] = invf[1] = 1;
	rep(i,2,(maxn << 2) - 1) fac[i] = 1ll * fac[i-1] * i % mod , inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod,
		invf[i] = 1ll * invf[i-1] * inv[i] % mod;
	dfs(0,1,0);
	printf("%d\n",(ans+mod)%mod);
}

Measured memory_bytes: 3740
N = gets.to_i
As = gets.split.map(&:to_i)
Bs = gets.split.map(&:to_i)
Cs = gets.split.map(&:to_i)

ans = N.times.map { |i| Bs[As[i] - 1] }.reduce(&:+)
ans += (N - 1).times
  .select { |i| As[i + 1] - As[i] == 1 }
  .map { |i| Cs[As[i] - 1] }
  .reduce(&:+) || 0

puts ans


Measured memory_bytes: 1916
s=int(input());
for n in range(1,3501):
    for h in range(1,3501):
        x=4*h*n-s*n-s*h
        if x>0:
            if ((s*h*n)/x).is_integer():
                print(int((s*h*n)/x),h,n)
                exit()

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

int main() {
  int n; cin>>n;
  string s; cin>>s;
 
  for(int i=0;i<s.size();i++) {
    int tmp=(int)(s[i]-'A')+n;
    if (tmp>=26) tmp-=26;
    cout<<(char)(tmp+'A');
  }
  cout<<endl;
}
  
    
  

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

int main() {
    string s;
    cin >> s;
    int sum_digit = 0;
    rep(i,s.size()) {
        sum_digit += s[i]-'0';
    }
    if (stoi(s) % sum_digit == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Measured memory_bytes: 256
def main():
    S = input()
    n = len(S)
    c = 0
    if S == 'keyence':
        print('YES')
        exit()
    for i in range(n):
        for j in range(i, n):
            s1 = S[:i] if i > 0 else ''
            s2 = S[j + 1:] if j + 1 < n else ''
            if s1 + s2 == 'keyence':
                print('YES')
                exit()
    print('NO')


if __name__ == '__main__':
    main()


Measured memory_bytes: 8964
#include <iostream>
#include <vector>
#define rep(i, k, n) for (int i = k; i < n; ++i)

using namespace std;

template<class T> inline void chmin (T &a, T b) {if(a > b) {a = b; return;} return;}
const int INF = 1 << 30;

int rec(int i, vector<int> &dp, vector<int> &h) {
  if (i == 0) return 0;

  if (dp[i] < INF) return dp[i];

  int tmp = INF;

  chmin(tmp, rec(i - 1, dp, h) + abs(h.at(i - 1) - h.at(i)));
  if (i > 1) chmin(tmp, rec(i - 2, dp, h) + abs(h.at(i - 2) - h.at(i)));

  return dp[i] = tmp;
}

int main(){
  // input
  int N, ans;
  cin >> N;
  vector<int> h(N);
  rep(i, 0, N) cin >> h[i];

  vector<int> dp(N, INF);
  dp.at(0) = 0;

  ans = rec(N-1, dp, h);

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


Measured memory_bytes: 7296
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i, n) for(int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define INF (1e9)
#define PI (acos(-1))
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

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

template<class T> inline auto __lcm(T& a, T& b) 
{ return a * b / __gcd(a, b); }

template<typename T> void remove(std::vector<T>& vector, unsigned int index)
{ vector.erase(vector.begin() + index); }


int main() {
    ll N;  cin >> N;
    vector<ll> H(N);  rep(i, N) cin >> H[i];

    ll cnt = 0;
    ll ans = 0;
    rep(i, N-1) {
        if (H[i] >= H[i+1]) {
            ++cnt;
            ans = max(ans, cnt);
        } else {
            cnt = 0;
        }
    }

    print(ans);

    return 0;
}

Measured memory_bytes: 1152
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <climits>
#include <cstdio>
#include <iomanip>
#include <utility>

using namespace std;
typedef long long int ll;

int main(int argc, char const *argv[])
{
    char a,b;
    cin>>a>>b;

    if (a == b)
    {
        cout << 'H' << endl;
    }
    else
        cout << 'D' << endl;
}

Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] data = Console.ReadLine().Split(' ');
            int a = int.Parse(data[0]);
            int b = int.Parse(data[1]);
            int c = int.Parse(data[2]);
            int k = int.Parse(Console.ReadLine());

            int num = 0;

            while (true) 
            {
                if (a >= b) 
                {
                    b = b * 2;
                    num++;
                }
                if (a < b) 
                {
                    break;
                }
            }
            while (true)
            {
                if (b >= c)
                {
                    c = c * 2;
                    num++;
                }

                if (b < c)
                {
                    break;
                }
            }

            if (num <= k)
            {
                Console.WriteLine("Yes");
            }
            else 
            {
                Console.WriteLine("No");
            }

        }
    }
}


Measured memory_bytes: 27488
#include<iostream>
using namespace std;
 
int main() {
  long N,A,B;
  cin>>N;
  cin>>A;
  cin>>B;
  long roop_num;
  if(A+B == 0){
      cout<<0;
      exit(0);
  }
  roop_num=N/(A+B);
  long amari;
  amari =N%(A+B);
  if(amari>A){
      cout<<A*(roop_num+1);
  }else{
      cout<<A*roop_num+amari;
  }
    return 0;
}

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

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
ll gcd(ll x, ll y){
  if(y==0) return x;
  else return gcd(y, x%y);
}
ll lcm(ll x,ll y){
  ll g=gcd(x,y);
  return x/g*y;
}
 
int main() {
  ll a,b,c ; 
  cin >> a >> b >> c ;
  if(a%2!=0 || b%2!=0 || c%2!=0 ) cout<<0<<endl;
  else if(a==b && b==c ) cout<<-1<<endl;
  else{
    int cnt=0; 
    int A=0,B=0,C=0;
    while(A%2==0 && B%2==0 && C%2==0){
      A=(a+b)/2;
      B=(b+c)/2;
      C=(c+a)/2;
      a=A,b=B,c=C;
      cnt++;
    }
    cout<<cnt<<endl;
  }
}
  



Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
#include<bitset>
using namespace std;

typedef long long ll;
#define rep(i, n) for(int i = 0;i < (n);i++)
#define repr(i, n) for(int i = (n);i >= 0;i--)
#define repf(i, m, n) for(int i = (m);i < (n);i++)
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 dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
//////////////////////////////////////////////////

ll mod = 1e9+7;
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// nCa (aが小さい時)
long long choose (long long n, long long a, long long mod) {
    long long c = 1, m = 1;
    for (int i = 0; i < a; i++) {
        (c *= n-i) %= mod;
        (m *= i+1) %= mod;
    }
    return (c * modpow(m, mod-2, mod))%mod;
}

int main() {
    ll n, a, b; cin >> n >> a >> b;
    ll ans = modpow(2,n,mod);
    ans -= 1;
    // cout << ans << endl;
    ans = (ans+mod - choose(n,a,mod))%mod;
    // cout << choose(n, a, mod) << endl;
    ans = (ans+mod - choose(n,b,mod))%mod;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<60;
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; }
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int main(){
    string S; cin >> S;

    cout << S.substr(0,3) << endl;
}

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

int main(){
  int count1 = 0, count2 = 0, n = 0;
  string str1, str2;
  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> str1 >> str2;
    if(str1 > str2){
      count1 += 3;
    }
    else if(str1 < str2){
      count2 += 3;
    }
    else {
      count1++;
      count2++;
    }
  }
  cout << count1 << " " << count2 << endl;
}


Measured memory_bytes: 3156
#include <bits/stdc++.h>
 
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n)  for(int i=0;i<(n);++i)
#define REPr(i, n) for(int i=(n)-1;i>=0; --i)
#define FORq(i, m, n) for(int i = (m);i <= (n);++i)
#define FORqr(i, m, n) for(int i = (n);i >=(m);--i)
#define PB push_back
#define MP make_pair
#define DEBUG printf("%s\n","debug")
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x,0,sizeof(x))
#define FILL(x, y) memset(x,y,sizeof(x))
#define MM(x) memset(x,-1,sizeof(x))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
/* (๑╹◡╹) */
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
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;
}
/* (๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹)(๑╹◡╹) */
///////////////////////////////////////////////
const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 



int main() {
	ll ans = 0;
	int A,B,C;	cin >> A >> B >> C;
	if(A+B>=C){ans = C+B;}
	else{ans = A+B+B+1;}
	cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define lint long long int
#define rep(i,n) for(int i=0;i<int(n);i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define arep(i,a,n) for(int i=a;i<n;i++)
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define fill(a,x) fill(a.begin(),a.end(),x)
#define eb(data) emplace_back(data)
#define pb(data) emplace_back(data)
#define mp make_pair
#define ALNUM 26
#define vint vector<int>
#define vlint vector<lint>
#define F first
#define S second
#define ALL(data) data.begin(),data.end()
#define GEts(s) getline(cin,s);
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
template<typename Rast>inline void out(Rast rast){cout<<rast<<"\n";return;}
template<typename Rast>inline void in(Rast& rast){cin>>rast;return;}
template<typename T>istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;}
template<typename First, typename... Rest>void in(First& first, Rest&... rest){cin >> first;in(rest...);return;}
template<typename First, typename... Rest>void out(First first, Rest... rest){cout << first<<" ";out(rest...);return;}
template<typename T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<typename T1,typename T2>bool chmax(T1& a,T2 b){if(a<b){a=b;return true;}else{return false;}}
template<typename T1,typename T2>bool chmin(T1& a,T2 b){if(a>b){a=b;return true;}else{return false;}}
template<typename T>T lcm(T a, T b){return a * b / gcd(a, b);}
static const double pi = 3.141592653589793;
lint modpow(lint a,lint n,lint p){if(n==1)return a%p;if(n%2==1)return (a*modpow(a,n-1,p))%p;lint t=modpow(a,n/2,p);return (t*t)%p;}//a^n%p
lint MOD=pow(10,9)+7;
//lint MOD=998244353;
lint inf=pow(2,50);
int intinf=pow(2,30);
/**/int dirx[]={1,0};int diry[]={0,1};//*///右、下
/**int dirx[]={0,1,0,-1};int diry[]={-1,0,1,0};//*///四方位
/**int dirx[]={-1,0,1,1,1,0,-1,-1};int diry[]={-1,-1,-1,0,1,1,1,0};//*///八方位

class unionfind{
public:
	vector<int> table;
	vector<int> wod;
	void init(int size){
		table.resize(size);
		wod.resize(size);
		rep(i,size)table[i]=i,wod[i]=i;
	};
	int root(int index){
		if(table[index]==index)return index;
		else{
			int hoge=root(table[index]);
			table[index]=hoge;
			return hoge;
		}
	};
	bool same(int x,int y){
		return(root(x)==root(y));
	};
	int marge(int x,int y){
		int yroot=root(y);
		int xroot=root(x);
		if(xroot==yroot)return 0;
		table[yroot]=xroot;
		return 0;
	}
};


int main(){
	cin.tie(0);ios::sync_with_stdio(false);cout<<std::fixed<<std::setprecision(12);
	int n;
	in(n);
	vlint a(n);
	in(a);
	sort(a);
	lint ans=0;
	reverse(a);
	ans+=a[0];
	int count=1;
	for(int i=1;i<n;i++){
		if(count==n-1)break;
		count++;
		ans+=a[i];
		if(count==n-1)break;
		count++;
		ans+=a[i];
	}
	out(ans);
	return 0;
}


Measured memory_bytes: 4780
#include <math.h>
#include <stdio.h>
 
int main(void) {
  long n, p, i = 1, ans;
  
  scanf("%ld", &n);
  scanf("%ld", &p);
  
  if(n <= 1 || p <= 1) {
    printf("%ld", p);
  }
  
  if(n > 1 && p > 1) {
    while(pow(i, n) <= p) {
      if(p % (long)pow(i, n) == 0) {
        ans = i;
      }
      i++;
    }
    printf("%ld", ans);
  }
  
  return 0;
}

Measured memory_bytes: 128
import java.util.*;

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

        if(N<=9){
        System.out.print("Yes");
        }else if(N<=18 && N%2==0){
        System.out.print("Yes");
        }else if(N<=27 && N%3==0){
        System.out.print("Yes"); 
        }else if(N<=36 && N%4==0){
        System.out.print("Yes");
        }else if(N<=45 && N%5==0){ 
        System.out.print("Yes");
        }else if(N<=54 && N%6==0){
        System.out.print("Yes");
        }else if(N<=63 && N%7==0){
        System.out.print("Yes");
        }else if(N<=72 && N%8==0){ 
        System.out.print("Yes");
        }else if(N<=81 && N%9==0){
        System.out.print("Yes");
        }else{
        System.out.print("No");
        }
}
}

Measured memory_bytes: 23892
#include <bits/stdc++.h>
#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define precise(i) fixed << setprecision(i)
using llint = long long int;
using namespace std;

int main() {
    fastIO;
    int n, x;
    cin >> n >> x;
    vector<int> L(n);
    rep(i, n) cin >> L.at(i);

    int Di = 0;
    int cnt = 1;
    rep(i, n) {
        Di = Di + L.at(i);
        if (Di <= x)
            cnt++;
    }
    cout << cnt << endl;
}


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

int main(){
	vector<long long int>a(3);
	long long int k=0,ans=0;
	for(int i=0;i<3;i++){
		cin>>a[i];
		if(a[i]%2==0){
			k++;
		}
	}
	for(int i=0;i<3;i++){
		if(k==1&&a[i]%2==1){
			a[i]++;
		}
		if(k==2&&a[i]%2==0){
			a[i]++;
		}
	}
	if(k==1||k==2){
		ans++;
	}
	
	sort(a.begin(),a.end(),greater<>());
	ans+=(2*a[0]-a[1]-a[2])/2;
	cout<<ans<<endl;
	
	return 0;
	
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) x&(-x);
typedef long long ll;
const ll inf=1e18;
const int N=2e5+3;
int n,k;
ll a[N],b[N],c[N],d[N];
int sum(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
void modify(int x){
	while(x<N){
		c[x]++;
		x+=lowbit(x);
	}
}
int main(){
	ll ans=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
    	cin>>a[i];
    	a[i]+=a[i-1];
    	b[i]=a[i]-1LL*i*k;
    	d[i]=b[i];
    }
    sort(b+1,b+1+n);
    int m=unique(b+1,b+1+n)-b;
    for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+m,d[i])-b;
    for(int i=1;i<=n;i++){
    	if(d[i]>=0) ans++;
    	ans+=sum(a[i]);
    	modify(a[i]);
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 6528
#include<cstdio>
#define neko 110
#define f(i,a,b) for(register int i=(a);i<=(b);i=-(~i))
int n,flag,ans;
char a[neko],b[neko];
int cmin(int a,int b){return a<b?a:b;}
int main()
{
	scanf("%d%s%s",&n,a+1,b+1),ans=n+n;
	f(i,1,n)
	{
		flag=0;
		if(a[i]==b[1]){f(j,i+1,n)if(a[j]^b[j-i+1]){ans=cmin(ans,n+n-(j-i)),flag=1;break;}}
		else flag=1;
		if(!flag)ans=cmin(ans,n+n-(n-i+1));
	}return printf("%d\n",ans),0;
}

Measured memory_bytes: 128
//BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
typedef vector<ii> vii;
typedef priority_queue<int,vector<int>,greater<int> > PQ;

int Set(int mask, int pos){return mask = mask | (1<<pos);}
bool check(int mask, int pos){return (bool)(mask & (1<<pos));}

int arr[MAX], cnt[32];

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

    int n;

    cin>>n;
    vector<int> ans(n);

    for(int i = 0; i < n; i++){
        cin>>arr[i];
        for(int j = 0; j < 31; j++) {
            if(check(arr[i], j))
                cnt[j]++;
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < 31; j++) {
            if(check(arr[i], j))
                cnt[j]--;
        }

        int res = 0;
        for(int j = 0; j < 31; j++) {
            if(cnt[j] & 1) {
                res += (1<<j);
            }
        }
        ans[i] = res;
        for(int j = 0; j < 31; j++) {
            if(check(arr[i], j))
                cnt[j]--;
        }
    }

    for(int i = 0; i < n - 1; i++)
        cout<<ans[i]<<" ";
    cout<<ans[n-1]<<endl;

    return 0;
}


Measured memory_bytes: 4676
require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

N, C = get_ints
XV = []
N.times do |i|
  XV[i] = get_ints
end

sorted_xv = XV.sort { |a, b| a[0] <=> b[0] }
sorted_xv_reverse = sorted_xv.reverse.map { |x, v| [C - x, v] }


sum = 0
best = - 10 ** 9
forward_bests = sorted_xv.map do |x,v|
  sum += v
  stock = sum - x
  best = stock if stock > best
  best
end

sum = 0
best = - 10 ** 9
reverse_bests = sorted_xv_reverse.map do |x, v|
  sum += v
  stock = sum - x
  best = stock if stock > best
  best
end

sum = 0
best = - 10 ** 9
forward_to_reverse = sorted_xv.map.with_index do |(x,v), i|
  next -10 ** 9 if i == (sorted_xv.length - 1)

  sum += v
  reverse_best = reverse_bests[-i-2]
  stock = sum - 2 * x + reverse_best
  best = stock if stock > best
  best
end

sum = 0
best = - 10 ** 9
reverse_to_forward = sorted_xv_reverse.map.with_index do |(x,v), i|
  next -10 ** 9 if i == (sorted_xv_reverse.length - 1)

  sum += v
  reverse_best = forward_bests[-i-2]
  stock = sum - 2 * x + reverse_best
  best = stock if stock > best
  best
end

puts [0, forward_bests.max, reverse_bests.max, forward_to_reverse.max, reverse_to_forward.max].max


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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define pb            push_back
template<class T, class S> inline pair<T, S> mp(T x, S y){ return make_pair(x, y); }
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

static const int    INF = 1000000000;
static const ll     MOD = 1000000007LL;
static const double EPS = 1E-12;

int x, y, s;

inline int get(int i, int p){ return i * (100 + p) / 100; }

int main()
{
  while(cin >> x >> y >> s, x){
    int res = -INF;
    rrep(i, 1, s) erep(j, 1, i) if(get(i, x) + get(j, x) == s) maxup(res, get(i, y) + get(j, y));
    cout << res << endl;
  }

  return 0;
}

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

int A[100005], B[100005], C[100005];
int SUM[100005];

int main() {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i]; sort(A, A+N);
  rep(i, N) cin >> B[i]; sort(B, B+N);
  rep(i, N) cin >> C[i]; sort(C, C+N);
  rep(i, N) {
    SUM[i] = lower_bound(A, A+N, B[i]) - A;
  }
  rep(i, N-1) {
    SUM[i+1] += SUM[i];
  }
  long long sum = 0;
  rep(i, N) {
    sum += (lower_bound(A, A+N, B[i]) - A) * (N - (upper_bound(C, C+N, B[i]) - C));
  }
  cout << sum << endl;
  return 0;
}



Measured memory_bytes: 5200
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;

const long INF = 1e9 + 1;

int main() {
  int n;
  string s,t;
  cin >> n >> s >> t;
  
  int cnt = 0;
  for(int i = 0; i < n; i++) {
    cout << s[i] << t[cnt];
    cnt++;
  }

  cout << endl;
}

Measured memory_bytes: 3576
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

const MAX_BIT: usize = 60;
fn main() {
    let n = read::<usize>();
    let mut seq = vec![0; n];
    let mut all = 0;
    for i in 0..n {
        seq[i] = read::<usize>();
        all ^= seq[i];
    }
    seq.sort();
    seq.reverse();
    let mut bases = vec![0; MAX_BIT];
    for i in 0..n {
        let mut v = seq[i];
        v &= !all;
        for j in (0..MAX_BIT).rev() {
            let bit = 1 << j;
            if (v & bit) == 0 || bases[j] == 0 {
                continue;
            }
            v ^= bases[j];
        }
        if v != 0 {
            bases[63 - v.leading_zeros() as usize] = v;
        }
    }
    let mut red = 0;
    for i in (0..MAX_BIT).rev() {
        let bit = 1 << i;
        if (red & bit) != 0 {
            continue;
        }
        red ^= bases[i];
    }
    let blue = red ^ all;
    println!("{}", red + blue);
}


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

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

typedef long long ll;
using namespace std;
int mod = 1000000007;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main(void){
    COMinit();
    int N, K; cin >> N >> K;
    ll A[N]; rep(i, N) cin >> A[i];
    ll ans = 0;
    sort(A, A+N);
    for(int i = N-1; i >= K-1; i--){
        ans = (ans + COM(i, K-1)*(A[i]-A[N-1-i]) % MOD) % MOD;
    }
    if(ans < 0) ans += MOD;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 12928
import sys
from sys import stdin,stdout
import math
import random
import heapq
from collections import Counter
from functools import lru_cache
sys.setrecursionlimit(10**7)
#@lru_cache(maxsize=None) #for optimizing the execution time of callable objects/functions(placed above callable functions)
def mcost(arr,n,dp):
    if n==1:return 0
    if dp[n]!=-1:return dp[n]
    if n==2:
        dp[n]=abs(arr[1]-arr[0])
        return dp[n]
    elif n==3:
        dp[n]=abs(arr[0]-arr[2])
        return dp[n]
    else:
        dp[n]=min(abs(arr[n-1]-arr[n-2])+mcost(arr,n-1,dp),abs(arr[n-1]-arr[n-3])+mcost(arr,n-2,dp))
        return dp[n]


try:
    for _ in range(1):
        n=int(input())
        arr=[int(i) for i in input().split()]
        dp=[-1]*(n+1)
        print(mcost(arr,n,dp))
    
except EOFError as e:
    print(e)


Measured memory_bytes: 168904
function main()
  
  N = parse(Int,readline())
  a = map(x -> parse(Int,x), split(readline()))
  
  Su = 0
  Ar = sum(a)
  ans = 10^10
  
  for i in 1:N-1
    Su += a[i]
    Ar -= a[i]
    ans = min(ans,abs(Su-Ar))
  end
  
  println(ans)
  
end

main()

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

#define rep(i,z) for(int i = 0; i<(int)(z); i++)
#define rep1(i,z) for(int i = 1; i<=(int)(z); i++)
 
using namespace std;

typedef long long ll;
const ll MOD=1e9+7;
using pear = pair<int,int>;

int gcd(int x, int y){
    if(x % y == 0) return y;
    return gcd(y, x % y);
}

int main(){
   ll n, k; cin>>n >>k;
   n %= k;
   cout<< min(n, k-n)<<endl;
}


Measured memory_bytes: 3604