akhauriyash/Code-Regression huggingface dataset:
#include<cstdio>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<deque>
#include<tuple>
#include<algorithm>
#include<numeric>
#include<limits>
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
#define BEGIN ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define END return EXIT_SUCCESS
#define rep(I,N) for(auto I=0;I<(N);++I)
#define up(I,A,B) for(auto I=(A);I<=(B);++I)
#define dw(I,A,B) for(auto I=(A);I>=(B);--I)
#define all(C) (C).begin(),(C).end()
#define rall(C) (C).rbegin(),(C).rend()
#define ft first
#define sd second
#define mp make_pair
#define mt make_tuple
#define pf push_front
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rs resize
typedef long long ll;
typedef unsigned long long ull;
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>inline void                                      in(T &p){cin>>p;}
template<class T1,class T2>inline void                            in(T1 &p1,T2 &p2){cin>>p1>>p2;}
template<class T1,class T2,class T3>inline void                   in(T1 &p1,T2 &p2,T3 &p3){cin>>p1>>p2>>p3;}
template<class T1,class T2,class T3,class T4>inline void          in(T1 &p1,T2 &p2,T3 &p3,T4 &p4){cin>>p1>>p2>>p3>>p4;}
template<class T1,class T2,class T3,class T4,class T5>inline void in(T1 &p1,T2 &p2,T3 &p3,T4 &p4,T5 &p5){cin>>p1>>p2>>p3>>p4>>p5;}
template<class T>inline void                                      ins(T &p){for_each(all(p),in<decltype(p[0])>);}
template<class T>inline void                             out(T p){cout<<p<<endl;}
template<class T1,class T2>inline void                   out(T1 p1,T2 p2){cout<<p1<<" "<<p2<<endl;}
template<class T1,class T2,class T3>inline void          out(T1 p1,T2 p2,T3 p3){cout<<p1<<" "<<p2<<" "<<p3<<endl;}
template<class T1,class T2,class T3,class T4>inline void out(T1 p1,T2 p2,T3 p3,T4 p4){cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<endl;}
inline                                              void out(double p){printf("%.10f\n",p);}
template<class T>inline                             void outs(T p){for_each(all(p),out<decltype(p[0])>);}
template<class T>inline void outs(vector<T> V){int n=V.size();rep(i,n)cout<<V[i]<<" \n"[i==n-1];}
//const long mod=(long)1e9+7;
inline void solve(void){
  int N,M;in(N,M);
  out(N==M?"Yes":"No");
}
int main(int argc,char**argv){
  BEGIN;
  solve();
  END;
}


Measured memory_bytes: 256
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <random>
#include <cmath>
#include <chrono>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
using namespace std;
using LL = long long;
#define eps 1e-8
#define fi first
#define se second
#define eb emplace_back
#define close std::ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define FOR(i, x, y) for (LL i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (LL i = (x), _##i = (y); i > _##i; --i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define CASET int ___T; scanf("%d", &___T); for(int __CS=1;__CS<=___T;__CS++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937 dlsrand(random_device{}());
mt19937 mrand(std::chrono::system_clock::now().time_since_epoch().count()); 
int rnd(int x) { return mrand() % x;}
LL bin(LL x, LL n, LL MOD) {LL ret = MOD != 1;for (x %= MOD; n; n >>= 1, x = x * x % MOD)if (n & 1) ret = ret * x % MOD;return ret;}
inline LL get_inv(LL x, LL p) { return bin(x, p - 2, p); }
const double PI = acos(-1.0);
constexpr int maxn = 1e6+10;
constexpr int INF = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3f;
constexpr int mod = 1e9+7;
int main()
{
    int a;
    cin >> a;
    cout << ceil(1.0*a/2) <<endl;
    
}

Measured memory_bytes: 384
import math


N,M = map(int,input().split())


if max(N,M) - min(N,M) >= 2:

    print(0)

elif max(N,M) - min(N,M) == 1:

    print ( math.factorial(N)*math.factorial(M) % (10**9+7) )

else:

    print ( 2 * math.factorial(N)*math.factorial(M) % (10**9+7) )

Measured memory_bytes: 5180
n = int(input())
x = [0]*n
y = [0]*n
for i in range(n): x[i],y[i] = map(int,input().split())

d = {(0,0):0}
for i in range(n):
    for j in range(n):
        if i == j: continue
        dx = x[i]-x[j]
        dy = y[i]-y[j]
        d[(dx,dy)] = d.get((dx,dy),0) + 1
        
print(n-max(d.values()))

Measured memory_bytes: 3444
import copy

n = int(input())
lst = [[0]*2 for i in range(n)]
for i in range(n):
    lst[i][0], lst[i][1] = map(int, input().split())

lst.sort(key=lambda x:(x[1]))
lst.sort(key=lambda x:(x[0]))

lst_copy = copy.deepcopy(lst)
saidai = 0
for i in range(n-1):
    for j in range(i+1, n):
        cnt = 0
        p = lst[j][0] - lst[i][0]
        q = lst[j][1] - lst[i][1]
        lst_copy = copy.deepcopy(lst)
        while lst_copy != []:
            xs, ys = lst_copy[0][0], lst_copy[0][1]
            lst_copy.pop(0)
            while [xs+p, ys+q] in lst_copy:
                cnt += 1
                xs, ys = xs+p, ys+q
                lst_copy.remove([xs, ys])
        saidai = max(saidai, cnt)

print(n-saidai)

Measured memory_bytes: 3444
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<bitset>

#define N 305
#define M 8000005

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define mk make_pair
#define pb push_back
#define fi first
#define se second

#define Mo 998244353

using namespace std;

bitset<305>A[305];

int a[N][N],n,m,i,j;

int power(int x,int y)
{
		int s=1;
		for(;y;y>>=1)
		{
				if(y&1) s=1ll*s*x%Mo;
				x=1ll*x*x%Mo; 
		}
		return s;
}

int Guass()
{
		int i,j,k,cnt=0;
		for (i=1;i<=n;++i)
		{
				for (j=cnt+1;j<=m;++j) if (A[j][i]) break;
				if (j>m) continue;
				++cnt;
				swap(A[cnt],A[j]);
				for (j=cnt+1;j<=m;++j)
					if (A[j][i]) A[j]^=A[cnt]; 
		}
		return power(2,n-cnt);
}

int main()
{
		scanf("%d%d",&n,&m);
		for (i=1;i<=n;++i) 
			for (j=1;j<=m;++j) scanf("%d",&a[i][j]);
		for (i=1;i<=m;++i)
			for (j=1;j<=n;++j) A[i][j]=a[j][i];
		printf("%d\n",1ll*(power(2,n)-Guass()+Mo)%Mo*power(2,m-1)%Mo); 
}

Measured memory_bytes: 640
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> 
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
int map[2005][2005][2];
int q[2005][2005][2];
int main()
{
	int n, k;
	long long x, y;
	char kkk;
	int kk;
	cin >> n >> k;
	kk = k * 2;
	for(int i = 1; i <= n; i++) {
		scanf("%lld %lld %c", &x, &y, &kkk);
		if(kkk == 'B')
			map[x % kk][y % kk][0]++;
		else
			map[x % kk][y % kk][1]++;
	}
	int maxx = 0;
	for(int i = 1; i <= kk; i++)
		for(int j = 1; j <= kk; j++) {
			q[i][j][0] = q[i - 1][j][0] + q[i][j - 1][0] - q[i - 1][j - 1][0] + map[i - 1][j - 1][0];
			q[i][j][1] = q[i - 1][j][1] + q[i][j - 1][1] - q[i - 1][j - 1][1] + map[i - 1][j - 1][1];
		}	
	for(int i = 1; i <= k; i++)
		for(int j = 1; j <= k; j++) {
			int sum = 0;
			int sbj = min(kk, i + k - 1);
			int ybj = min(kk, j + k - 1);
			sum += (q[sbj][ybj][0] + q[i - 1][j - 1][0] - q[sbj][j - 1][0] - q[i - 1][ybj][0]);
			sum += q[i - 1][j - 1][0];
			sum += (q[kk][j - 1][0] - q[sbj][j - 1][0]);
			sum += (q[kk][kk][0] + q[sbj][ybj][0] - q[kk][ybj][0] - q[sbj][kk][0]);
			sum += (q[i - 1][kk][0] - q[i - 1][ybj][0]);
			sum += (q[sbj][j - 1][1] - q[i - 1][j - 1][1]);
			sum += (q[kk][ybj][1] + q[sbj][j - 1][1] - q[sbj][ybj][1] - q[kk][j - 1][1]);
			sum += (q[i - 1][ybj][1] - q[i - 1][j - 1][1]);
			sum += (q[sbj][kk][1] + q[i - 1][ybj][1] - q[sbj][ybj][1] - q[i - 1][kk][1]);
			maxx = max(sum, maxx);
		}
	for(int i = 1; i <= k; i++)
		for(int j = 1; j <= k; j++) {
			int sum = 0;
			int sbj = min(kk, i + k - 1);
			int ybj = min(kk, j + k - 1);
			sum += (q[sbj][ybj][1] + q[i - 1][j - 1][1] - q[sbj][j - 1][1] - q[i - 1][ybj][1]);
			sum += q[i - 1][j - 1][1];
			sum += (q[kk][j - 1][1] - q[sbj][j - 1][1]);
			sum += (q[kk][kk][1] + q[sbj][ybj][1] - q[kk][ybj][1] - q[sbj][kk][1]);
			sum += (q[i - 1][kk][1] - q[i - 1][ybj][1]);
			sum += (q[sbj][j - 1][0] - q[i - 1][j - 1][0]);
			sum += (q[kk][ybj][0] + q[sbj][j - 1][0] - q[sbj][ybj][0] - q[kk][j - 1][0]);
			sum += (q[i - 1][ybj][0] - q[i - 1][j - 1][0]);
			sum += (q[sbj][kk][0] + q[i - 1][ybj][0] - q[sbj][ybj][0] - q[i - 1][kk][0]);
			maxx = max(sum, maxx);
		}
	cout << maxx << endl;
	return 0;
}

Measured memory_bytes: 62976
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef array<ll,2> pll;
const ll NMAX=100005;
ll dp[101][NMAX];
int main()
{
    ll n,k,w,v,ans=0;
    cin>>n>>k;
    for(ll i=1;i<=n;i++){
        cin>>w>>v;
        for(ll j=0;j<=k;j++){
            dp[i][j]=max(dp[i-1][j],(j>=w?dp[i-1][j-w]+v:0));
            ans=max(ans,dp[i][j]);
        }
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 81696
import math

K  = int(input())
ans = 0
for a in range(K+1):
    for b in range(a,K+1):
        for c in range(b,K+1):
            if a==b and b ==c:
                dabu = 1
            elif a!=b and b!=c and c!=a:
                dabu = 6
            else:
                dabu = 3
            if a*b*c !=0:
                s = math.gcd(math.gcd(a,b),c) * dabu
                ans = ans +s
print(ans)

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


void solve(long long X){
    for(ll i = 0;i <= 1e3;i++){
        for(ll j = 0;j <= 1e3;j++){
            long long uku = i * i * i * i * i;
            long long q = j * j * j * j * j;
            if(uku - q == X){
                cout<<i<<" "<<j<<endl;
                return;
            }
            if(uku + q == X){
                cout<<i<<" "<<-j<<endl;
                return;
            }
        }
    }
}

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


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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)

vector<vector<int32>> adj;
vector<int32> ans;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n,m;
  cin >> n >> m;
  adj.resize(n);
  REP(i,m){
    int32 a,b;
    cin >> a >> b;
    a--;b--;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  Yes(true);
  ans.resize(n,-1);
  queue<pii> que;
  que.emplace(0,-1);
  while(!que.empty()){
    auto [v,p] = que.front();que.pop();
    if(ans[v] != -1)continue;
    ans[v] = p+1;
    for(auto u : adj[v]){
      que.emplace(u,v);
    }
  }
  FOR(i,1,n)ANS(ans[i]);
  return 0;
}

Measured memory_bytes: 17000
#include <string>
#include <iostream>

using namespace std;

int main() {
    int R, G, B, N;
    cin >> R >> G >> B >> N;

    int ans = 0;
    for (int r=0; r<3001; r++) {
        int balls = R*r;
        if (N < balls) {
            break;
        }
        for (int g=0; g<3001; g++) {
            balls = R*r + G*g;
            if (N < balls) {
                break;
            }
            int left = N - balls;
            if (left%B == 0) {
                ans += 1;
            }
        }
    }

    cout << ans << endl;
}



Measured memory_bytes: 256
#coding:utf-8

s = input()
ans = 'AC'
if s[0] != 'A': ans = 'WA'

num_c = 0
for c in s[2:-1]:
    if c == 'C':
        num_c += 1
    else:
        if c.isupper():
            ans = 'WA'
if num_c != 1: ans = 'WA'

if s[-1].isupper(): ans = 'WA'
if s[1].isupper(): ans = 'WA'
print(ans)

Measured memory_bytes: 3064
#include <iostream>
#define REP(i, a, b) for (auto i = (a); i < (b); ++i)
using namespace std;

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

	int n; cin >> n;
	bool flag = false;
	REP(i, 1, 10)
		if (n % i == 0 && n / i < 10) flag = true;
	cout << (flag ? "Yes" : "No");
}

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

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var b = Console.ReadLine().Split().Select(int.Parse).ToArray();

            var total = 0L;
            total += b[0];
            for (var i = 1; i < n - 1; i++)
            {
                total += Math.Min(b[i], b[i - 1]);
            }
            total += b[n - 2];

            Console.WriteLine(total);
        }
    }
}


Measured memory_bytes: 13396
#include<iostream>
using namespace std;
#define MAX 1000
#define Infty 10000

struct Edge{int cost, left, right; };

void Bubble(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    for(int j=N-1;j>=i;j--){
      if(A[j].cost < A[j-1].cost){
        Edge buffa = A[j];
        A[j] = A[j-1];
        A[j-1] = buffa;
      }
    }
  }
}
void print(struct Edge A[], int N){
  for(int i=0;i<N;i++){
    cout << A[i].cost << A[i].left << A[i].right << endl;
  }
}

int find(int x, int parents[]){
  int c = x;
  while(parents[c]!=c) c = parents[c];
  return c;
}

void uunion(int x, int y, int parents[]){
  int a = find(x,parents);
  int b = find(y,parents);
  if(a!=b) parents[a] = b;
}

int main(){
    int n, M[MAX][MAX], d=0;
    int k = 0;
    struct Edge A[MAX];
    cin >> n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        cin >> M[i][j];
        if(M[i][j]==-1) M[i][j] = Infty;
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(j<i){
          //cout << "k = " << k << endl;
          A[k].cost = M[i][j];
          //cout << "M[1][1]=" << M[i][j] << endl;
          //cout << "A.cost = " << A[k].cost << endl;
          A[k].right = i;
          A[k].left = j;
          k++;
        }
      }
    }
    Bubble(A, k);
    
    int parents[k];
    for(int i=0;i<k;i++) parents[i] = i;
    for(int i=0;i<k;i++){
      if(find(A[i].left,parents)!=find(A[i].right,parents)){
        d += A[i].cost;
        uunion(A[i].left,A[i].right,parents);
      }
    }
    cout << d << endl;

    return 0;
}



Measured memory_bytes: 3488
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;
int main()
{
	long long i = 0;
	long long j = 0;
	long long k = 0;
	long long l = 0;
	long long m = 0;
	long long N;
	long long A[100] = {0};
	long long B[100] = {0};
	long long a = 0;
	long long max = 0;

	cin >> N;

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

	for (j = 0; j < N; j++) {
		cin >> B[j];
	}

	for (k = 0; k < N; k++) {

		for (l = 0; l < k+1; l++) {
			a = a + A[l];
		}
		for (m = k; m < N; m++) {
			a = a + B[m];
		}

		if (max < a)
			max = a;

		a = 0;
	}

	cout << max;

	return 0;
	
}

Measured memory_bytes: 256
s = input()
t = input()

l_s = len(s)
l_t = len(t)

dp = [[0] * (l_t + 1) for i in range(l_s + 1)]

ans = ""
for j in range(l_s):
    for i in range(l_t):
        if s[j] == t[i]:
            dp[j + 1][i + 1] = dp[j][i] + 1
        else:
            dp[j + 1][i + 1] = max(dp[j][i + 1], dp[j + 1][i])

if dp[l_s][l_t] == 0:
    print(" ")
    exit()

ans = ""
i = l_t
j = l_s

while i > 0 and j > 0:
    if s[j - 1] == t[i - 1]:
        ans += s[j - 1]
        i -= 1
        j -= 1
    else:
        if dp[j - 1][i] > dp[j][i - 1]:
            j -= 1
        else:
            i -= 1

print(ans[::-1])

Measured memory_bytes: 112988
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
	public static void main(String[] args) throws InterruptedException {
		Scanner s= new Scanner(System.in);

		int listLen = Integer.parseInt(s.nextLine());
		Thread.sleep(1);
		//String[] listOfStrings = s.nextLine().split(" ");

		HashMap<Integer, Boolean> map = new HashMap<>(listLen * 2);
		//Arrays.stream(s.nextLine().split(" ")).mapToInt(Integer::parseInt).forEach(i -> map.put(i, true));
		//for (String listOfString : listOfStrings) map.put(Integer.parseInt(listOfString), true);
		for (int i = 0; i < listLen; ++i) {
//			System.out.println(s.nextInt());
			map.put(Integer.parseInt(s.next()), true);
		}

		StringBuilder sb = new StringBuilder(listLen * 2);
		int numQueries = s.nextInt();
		for (int i = 0; i < numQueries; ++i) {
			int x = s.nextInt();
			sb.append(map.get(x) != null ? "1\n" : "0\n");
		}
		System.out.print(sb.toString());
	}
}



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

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define FORR(i, a, b) for (long long i = (a); i >= (b); i--)
#define REPR(i, N) for (long long i = n; i >= 0; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ALL(s) (s).begin(), (s).end()
#define pb(a) push_back(a)

#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

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

bool check(string s, string ans, ll i, ll N){
    if(s[i] == 'o'){
        if(ans[i] == 'S' and ans[(i+1)%N] == ans[(i-1+N)%N]){
            return true;
        }else if(ans[i] == 'W' and ans[(i+1)%N] != ans[(i-1+N)%N]){
            return true;
        }
    }else {
        if(ans[i] == 'W' and ans[(i+1)%N] == ans[(i-1+N)%N]){
            return true;
        }else if(ans[i] == 'S' and ans[(i+1)%N] != ans[(i-1+N)%N]){
            return true;
        }
    }
    return false;
}

int main()
{
    ll N;
    string s;
    cin >> N;
    cin >> s;
    string ans;
    string init_pattern[4] = {"SS", "SW", "WS", "WW"};
    for(string p: init_pattern){
        ans = "";
        ans += p;
        FOR(i,2,N){
            if(s[i-1] == 'o'){
                if(ans[i-1] == 'S'){
                    ans += ans[i-2];   
                }else{
                    ans += (ans[i-2] == 'S') ? 'W' : 'S';
                }
            }else {
                if(ans[i-1] == 'S'){
                    ans += (ans[i-2] == 'S') ? 'W' : 'S';
                }else{
                    ans += ans[i-2]; 
                }
            }
        }
        if (check(s, ans, 0, N) && check(s, ans, N-1, N)){
            break;
        }
        ans = "-1";
    }
    cout << ans << endl;
}


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

using namespace std;
#define int unsigned long long
#define NMAX 200005

int n, X;
int x[4*NMAX];

int calc(int trips){
	int ans = trips*X;
	for(int i = 0; i < n; i++){
		int cnt = i/trips + 1;
		if(cnt == 1){
			cnt = 5;
		}else{
			cnt = (cnt + 1LL)*(cnt + 1LL) - (cnt*cnt);
		}
		ans += cnt*x[i];
	}
	return ans;
}

signed main(){
	cin>>n>>X;
	for(int i = 0; i < n; i++) cin>>x[i];
	x[n] = 0;
	reverse(x, x + n + 1);
	for(int i = 1; i <= n; i++) x[i] += x[i-1];
	for(int i = n+1; i < 3*n; i++) x[i] = x[i-1];
	int best = LONG_LONG_MAX;
	for(int i = 1; i <= n; i++){
		int ans = 0;
		for(int cur = 0; cur < n; cur += i){
			int it = cur/i + 1LL;
			if(it == 1){
				ans += 5*(x[cur + i] - x[cur]);
			}else{
				int mult = 2*it + 1;
				ans += mult*(x[cur + i] - x[cur]);
			}
		}
		ans += i*X;
		best = min(best, ans);
	}
	best += n*X;
	cout<<best<<endl;
}


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

void ans(int w,int a,int b) {
    if(a<=b) {
        if(b-(a+w)>=0) {
            cout << b-(a+w)<< endl;
            return;
        }
        else
            cout << 0 << endl;
        ;
        return;
    }
    else
    {
        if(a-(b+w)>=0) {
            cout << a - (b + w) << endl;
            return;
        } else
            cout << 0 << endl;
        return;
    }
}

int main(void) {
    int w,a,b;
    cin>>w>>a>>b;
    ans(w, a, b);
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision
#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns
#define all(a) a.begin(),a.end()
#define ll long long int
#define ld long double
ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}
ll modInverse(ll a, ll m) { return power(a, m-2, m); }
#define ps push_back
#define fs first
#define takeline cin.ignore();
#define sc second
#define N 3000005
#define endl "\n"
#define mod 1000000007
int main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	
	ll a,b,w;
	cin>>w>>a>>b;

	ll an;
	if(a+w<=b+w){
		an=(b-(a+w));
	}
	else{
		an=(a-(b+w));
	}
	cout<<max((ll)0,an);
	return 0;
}

Measured memory_bytes: 256
n = gets.to_i
list = []
n.times do
  list << gets.split.map(&:to_i)
end
judges = Array.new(n, false)
judges[0] = true
list = list.sort_by!{|x, l| x+l}
i = 0
while i < n-1 do
  a = list[i][0] + list[i][1]
  j = i+1
  while j < n do
    break if judges[j]
    b = list[j][0] - list[j][1]
    a - b > 0 ? j += 1 : judges[j] = true
  end
  i = j
end
puts judges.count(true)


Measured memory_bytes: 13432
fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val an = readLine()!!.split(" ").map(String::toInt).sorted()

    val map = mutableMapOf<Int, Int>()
    for (a in an) {
        if (0 <= a - 1) {
            map[a - 1] = (map[a - 1] ?: 0) + 1
        }
        map[a] = (map[a] ?: 0) + 1
        map[a + 1] = (map[a + 1] ?: 0) + 1
    }

    map.maxBy { it.value }!!.value.let(::println)


}


Measured memory_bytes: 58408
#include<iostream>
#include<stdio.h>
#include<string>
#include<cmath>
#include<math.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<unordered_map>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rep2(i,n) for(int i = -1; i < (n); i++)
#define PI 3.141592653589793

using namespace std;
using ll = unsigned long long;


int main() {

	ll n, m, k;

	cin >> n >> m >> k;

	vector<ll> csa(n + 1);
	vector<ll> csb(m + 1);
	vector<ll> ca;
	rep(i, n) {
		ll a;
		cin >> a;
		csa[i + 1] = csa[i] + a;
	}
	rep(i, m) {
		ll b;
		cin >> b;
		csb[i + 1] = csb[i] + b;
	}

	ll c;
	ll cnt = 0;

	rep(i, n + 1) {

		ll l = 0, r = m + 1;

		if (k < csa[i])break;

		ll lim = k - csa[i];

		while (r - l > 1) {

			c = (l + r) / 2;
			if (lim >= csb[c]) l = c;
			else r = c;
			
		}
		ca.push_back(i + l);
		cnt++;
	}

	sort(ca.begin(), ca.end());
	

	cout << ca[cnt-1] << endl;

	return 0;
}

Measured memory_bytes: 8392
#include<bits/stdc++.h>
using namespace std;
long long int maximum=0;
//Recursive approach
/*void functions(long long int *weight,long long int *value,long long int n,long long int w,long long int start,long long int knap,long long int price)
{
    if(price>maximum&&knap<=w)
    maximum=price;
    if(knap>w)
    return;
    for(long long int i=start;i<=n;i++)
    {
        functions(weight,value,n,w,i+1,knap+weight[i],price+value[i]);
    }
}*/
long long int ans[101][100001];
long long int functions(long long int *weight,long long int *value,long long int n,long long int w)
{
    if(n==0||w==0)
    //return 0;
    {
        ans[n][w]=0;
    }
    if(ans[n][w]!=-1)
    return ans[n][w];
    else if(weight[n]<=w)
    ans[n][w]=max(value[n]+functions(weight,value,n-1,w-weight[n]),functions(weight,value,n-1,w));
    else
    ans[n][w]=functions(weight,value,n-1,w);
    return ans[n][w];
}
int main(void){
    long long int n,w;
    cin>>n>>w;
    //ve.resize(n+1);
    long long int weight[n+1],value[n+1];
    weight[0]=value[0]=0;
    for(long long int i=1;i<=n;i++)
    cin>>weight[i]>>value[i];
    //functions(weight,value,n,w,1,0,0);
    //cout<<maximum<<endl;
    for(long long i=0;i<=n;i++)
    for(long long j=0;j<=w;j++)
    ans[i][j]=-1;
    maximum=functions(weight,value,n,w);
    cout<<maximum<<endl;
}

Measured memory_bytes: 79104
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
long long C[51][51];
int main()
{
    long long ans = 0;
    C[0][0] = C[1][0] = C[1][1] = 1;
    int n, p, a, b, i, x, j;
    for(i = 2; i <= 50; ++i)
    {
        C[i][0] = C[i][i] = 1;
        for(j = 1; j < i; ++j)
        {
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
    scanf("%d%d", &n, &p);
    a = b = 0;
    for(i = 1; i <= n; ++i)
    {
        scanf("%d", &x);
        if(x % 2 == 1)
            a++;
        else
            b++;
    }
    for(i = 0; i <= a; ++i)
    {
        if(i % 2 == p)
        {
            for(j = 0; j <= b; ++j)
            {
                ans = ans + C[a][i] * C[b][j];
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.lang.*;
import java.io.*;

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

		int dp[][]=new int[n][m];
		for(int i=0;i<n;i++)
		{
			String str=scan.next();
			for(int j=0;j<m;j++)
			{
				if(str.charAt(j)=='.')
					dp[i][j]=1;
				else
					dp[i][j]=0;
			}
		}

		for(int i=1;i<m;i++)
		{
			if(dp[0][i]!=0)
				dp[0][i]=dp[0][i-1];
		}
		for(int i=1;i<n;i++)
		{
			if(dp[i][0]!=0)
				dp[i][0]=dp[i-1][0];
		}

		for(int i=1;i<n;i++)
		{
			for(int j=1;j<m;j++)
			{
				if(dp[i][j]==0)
					continue;
				dp[i][j]=(dp[i][j-1]+dp[i-1][j])%(1000000007);
			}
		}

		System.out.println(dp[n-1][m-1]);

	}
}

Measured memory_bytes: 35516
#include<iostream>
#include <string>

using namespace std;

int main(){

	string S;
	char yes[] = "AC";
	char no[] = "WA";
	int rule[5] = {};
	int count_a=0,count_b =0,i=0;
	int length;

	cin >> S;
	length = S.size();

	//条件１
	if (S[0] == 'A') rule[0] = 1;


	//条件２
	for (i = 2; i < length ; i++){
			if (S[i] == 'C') count_a++;
		}
	if (count_a == 1) rule[1] = 1;



	////条件３
	if ((S[length - 1] >= 'a') && (S[length - 1] <= 'z')) rule[2] = 1;
	
	////条件４
	if ((S[1] >= 'a') && (S[1] <= 'z')) rule[3] = 1;


	////条件５
	for (i = 2; i < length - 1; i++){
		if ((S[i] >= 'A') && (S[i] <= 'Z')) count_b++;
	}
	if (count_b == 1) rule[4] = 1;

	//結果
	for ( i = 0; i < 5; i++)
	{
		
		if (rule[i] == 0)
		{
			cout << no;
			break;
		}
		if (i==4)
		{
			cout << yes;
		}
	}

	return 0;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>

using namespace std;

template<class T> void gc(T &x)
{
    char c;
    int s = 0;
    x = 0;

    c = getchar_unlocked();

    if (c == '-') s = 1;
    else if ('0' <= c && c <= '9')  x = c - '0';

    while (true)
    {
        c = getchar_unlocked();

        if (c < '0' || c > '9') break;
        x = x * 10 + (c - '0');
    }

    if (s) x = -x;
}

int main()
{
    int N;

    gc(N);

    vector<int> P(N);

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

        P[i] = tmp;
    }

    int cnt = 0;
    for (int i = 0; i < N; i++)
    {
        if (P[i] == i + 1)
        {
            if (i != N - 1)
            {
                swap(P[i], P[i + 1]);
                cnt++;
            }
            else
            {
                swap(P[i - 1], P[i]);
                cnt++;
            }
        }
    }

    printf("%d\n", cnt);
    return 0;
}


Measured memory_bytes: 640
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
constexpr int kMod = 1000000007;
typedef long long LL;

int main() {
  int N; std::cin >> N;
  std::map<char, int> m;
  for (int i = 0; i < N; ++i) {
    char c; std::cin >> c;
    if (m.find(c) == m.end()) {
      m[c] = 1;
    }
    ++m[c];
  }

  LL cnt = 1;
  for (const auto& p : m) {
    cnt *= (LL)p.second;
    cnt %= kMod;
  }
  std::cout << ((cnt + kMod) - 1) %kMod << std::endl;
}


Measured memory_bytes: 256
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
  private static PrintWriter out;

  public static void main(String[] args) {
    Main main = new Main();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> out.close()));
    try {
      main.run(args);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }



  private void run(String[] arguments) throws Exception {
    MyScanner sc = new MyScanner();
    out = new PrintWriter(new BufferedOutputStream(System.out));

    int N = sc.nextInt();
    int[] check = new int[N];

    for (int i = 0; i < N-1; i++) {
      check[sc.nextInt()-1]++;
    }

    for (int i = 0; i < check.length; i++) {
      out.println(check[i]);
    }

    out.close();
  }
  
  /*          
   * Form: http://codeforces.com/blog/entry/7018
   */
  private class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    MyScanner() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
}


Measured memory_bytes: 55492
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for i in range(n)]
cd = [list(map(int, input().split())) for i in range(m)]
for i in ab:
    l = []
    for j in cd:
        l.append(abs((j[0] - i[0])) + abs((j[1] - i[1])))
    print(l.index(min(l)) + 1)

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

#define ALL(a) (a).begin(), (a).end()
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define rep(i, n) FOR(i, 0, n)
#define repi(i, n) FOR(i, 1, n + 1)
#define pp(v) cerr << #v "=" << (v) << endl;
#define ppa(v) cerr << "- " << #v << endl; rep(i,v.size()) cerr << #v << "[" << i << "] = " << v[i] << endl;
#define div_ceil(a,b) ((a) + ((b) - 1)) / (b)
#define UNIQUE(v) v.erase( unique(ALL(v)), v.end() );
#define INF 0x3f3f3f3f
#define LLINF 1000111000111000111LL
#define MOD 1000000007
#define TIME system("date +%M:%S.%N")
using Graph = vector<vector<int>>;
using ll = long long;
template<class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; }
template<class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }

// ABC106C 方針
// 2**(5e15)>10**18なので、Sの位置m以降に1以外の数cがあり、m<=kならばその数が答え
// 1以外の数がないか、k<mなら1が答え

int main() {
  string s;cin>>s;
  ll k;cin>>k;k--;
  int m = -1;
  char c = '1';
  rep(i,s.size()) {
    if (s[i] != '1') {
      m = i;
      c = s[i];
      break;
    }
  }
  pp(m);
  pp(k);
  char ans = m <= k ? c : '1';
  cout << ans << endl;
}


Measured memory_bytes: 256
n = int(raw_input())
q = raw_input()

m = -1

for a in range(len(q)):
  dic1 = {}
  dic2 = {}
  buf = 0
  for b in range(len(q)):
    if a > b: dic1[ q[b] ] = 1
    else: dic2[ q[b] ] = 1

  for z in dic1:
    if z in dic2:
      buf += 1
  if m < buf:
    m = buf
print m


Measured memory_bytes: 2568
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pll=pair<ll,ll>;
using plll=pair<ll,pll>;
using graph=vector<vector<int>>;

const ll MOD=1000000007;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

ll abs1(ll a){
  if(a>=0)return a;
  else return -a;
}

ll pow1(ll a,ll b,ll p=LLONG_MAX){
  ll ret=1;
  while(b!=0){
    if((b&1)==1){
      ret*=a;
    }
    a*=a;
    if(a>=p)a%=p;
    b=b>>1;
  }
  return ret;
}

int digit(ll a,ll b){
  int i=0;
  while(a!=0){
    a/=b;
    i++;
  }
  return i;
}

ll gcd1(ll a,ll b){
  if(a==0 && b==0)return 0;
  ll n;
  while(a%b!=0){
    n=a%b;
    a=b;
    b=n;
  }
  return b;
}

ll solveLinear(ll n,ll m,ll c){
  if(n==0 && m==0)return 0;
  ll a[100],b[100];
  a[0]=n;b[0]=m;
  int i=1;
  while(b[i-1]!=0){
    a[i]=b[i-1];
    b[i]=a[i-1]%b[i-1];
    i++;
  }
  n=i;
  if(c%a[n-1]!=0){
    return 0;
  }
  ll x=c/a[n-1],y=0;
  for(i=1;i<n;i++){
    ll cnt=x;
    x=y;
    y=cnt-(a[n-i-1]/b[n-i-1])*y;
  }
  return x;
}

ll modRev(ll a,ll p=MOD){
  a%=p;
  ll n=solveLinear(a,p,1);
  n%=p;
  if(n<0)return n+p;
  return n;
}

ll fact(ll n,ll p=MOD){
  if(n<0)return 0;
  int i;
  ll ret=1;
  for(i=0;i<n;i++){
    ret=ret*(i+1);
    if(ret>=p)ret%=p;
  }
  return ret;
}

ll perm(ll n,ll k,ll p=MOD){
  if(n<0 || k<0 || n<k)return 0;
  int i;
  ll ret=1;
  for(i=0;i<k;i++){
    ret=ret*(n-i);
    if(ret>=p)ret%=p;
  }
  return ret;
}

ll binom(ll n,ll k,ll p=MOD){
  if(n<0 || k<0 || n<k)return 0;
  int i;
  ll ret=1,fact=1;
  for(i=0;i<k;i++){
    ret=ret*(n-i);
    fact*=i+1;
    if(ret>=p)ret%=p;
    if(fact>=p)fact%=p;
  }
  return (ret*modRev(fact))%p;
}

void pushHeap(vector<int>& h,int n){
  int i=h.size();
  h.push_back(n);
  while(i!=0 && h[(i-1)/2]<h[i]){
    swap(h[i],h[(i-1)/2]);
    i=(i-1)/2;
  }
  h[i]=n;
}

int popHeap(vector<int>& h){
  int n=h.size()-1;
  if(n==-1)return 0;
  int i,ret=h[0];
  swap(h[0],h[n]);
  h.pop_back();
  for(i=0;i<n;){
    int j=2*i+1;
    if(j>=n)break;
    if(j!=n-1 && h[j]<h[j+1])j++;
    if(h[i]<h[j]){
      swap(h[i],h[j]);
    }
    i=j;
  }
  return ret;
}

ll r[200000];
ll c[200000];

void dfs(const graph &g,int v,int p){
  c[v]+=r[v];
  for(auto i:g[v]){
    if(i==p)continue;
    r[i]+=r[v];
  }
  for(auto i:g[v]){
    if(i==p)continue;
    dfs(g,i,v);
  }
}

int main(){
  int i,j,k,cnt=0;
  int n,q;
  cin>>n>>q;
  graph g(n);
  for(i=0;i<n-1;i++){
    int a,b;
    cin>>a>>b;
    g[a-1].push_back(b-1);
    g[b-1].push_back(a-1);
  }
  int p[q],x[q];
  for(i=0;i<q;i++){
    cin>>p[i]>>x[i];
  }
  for(i=0;i<n;i++)r[i]=0;
  for(i=0;i<q;i++){
    r[p[i]-1]+=x[i];
  }
  dfs(g,0,-1);
  cout<<r[0];
  for(i=1;i<n;i++)cout<<" "<<r[i];
  cout<<endl;
}


Measured memory_bytes: 27392
using System;
using System.Collections.Generic;
using System.Linq;

namespace ITP1_7_B
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            var result = new List<int>();
            while ((input = Console.ReadLine()) != "0 0")
                result.Add(Mystruct.SearchComb(input.Split(' ').Select(x => Convert.ToInt32(x)).ToArray()));
            foreach (var r in result)
                Console.WriteLine(r);
        }
    }
    class Mystruct
    {
        static  public int SearchComb(int[] data) 
        {
            var combo = new List<int[]>();
            int cnt = 0;
            int i, j;         
            for(i = 1; i <= data[0]; i++)
            {
                for (j = 1; j <= data[0]; j++)
                {
                    int k = data[1] - (i + j);
                    if (0 < k && k <= data[0] && i != j && j != k && k != i)
                    {
                        bool judge = true;
                        //Console.Write(i + " " + j + " " + k);
                        foreach (var c in combo)
                        {
                            if ((c[0] == i || c[0] == j || c[0] == k) && ((c[1] == i || c[1] == j || c[1] == k)))
                            {
                                judge = false;
                                break;
                            }                          
                        }
                        //Console.WriteLine(" :" + judge);
                        if (judge)
                        { 
                            combo.Add(new int[] { i, j });
                            cnt++;
                        }
                    }
                }                 
            }
            /*foreach (var c in combo)
            {
                foreach (int x in c)
                {
                    Console.Write(x + " ");
                }
                Console.WriteLine();
            }*/                         
            return cnt;
        }
    }
}

Measured memory_bytes: 9904
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<iomanip>
#include<set>
#include<queue>

using namespace std;

int main()
{
	int all;
	while(true)
	{
		cin >> all;
		if(all==0)break;

		for(int i=0;i<9;i++)
		{
			int tmp;
			cin >> tmp;
			all-=tmp;
		}

		cout << all << endl;
	}





}


Measured memory_bytes: 2972
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

#define loop(n) for(int lp=0; lp<(n); lp++)
#define ascent(i, a, b)  for(int (i)=(a); (i)<(b); (i)++)
#define descent(i, a, b) for(int (i)=(a); (i)<(b); (i)--)

int main() {
  int D, x, y;
  vector<int> p, pp;
  
  cin >> D;
  cin >> x;
  
  string stmp;
  int itmp;

  loop(x) {
    cin >> stmp >> itmp;
    if(stmp == "D") {
      p.push_back(itmp);
    }
  }

  cin >> y;
  loop(y) {
    stmp.clear();
    cin >> stmp >> itmp;
    if( stmp == "DD") {
      pp.push_back(itmp);
    }
  }



  if(p.size())  sort(p.begin(), p.end(), greater<int>());
  if(pp.size()) sort(pp.begin(), pp.end(), greater<int>());

  int i = 0, j = 0;
  vector<int> ans;
  while(D > 0) {
    if(i >= p.size() && j >= pp.size()) break;

    if(i >= p.size()) {
      while(D >= 2 && j < pp.size()) {
	ans.push_back(pp[j]);
	j ++;
	D -= 2;
      }
      break;
    }
    else if(j >= pp.size()) {
      while(D && i < p.size()) {
	ans.push_back(p[i]);
	i ++;
	D --;
      }
      break;
    }

    int ptmp;
    if(i == p.size()-1) {
      ptmp = p[i];
      //ptmp = 2*p[i];
    }
    else {
      //ptmp = 2*p[i];
      ptmp = p[i] + p[i+1];
    }

    if(D == 1) {
      ans.push_back(p[i]);
      break;
    }

    if(ptmp < pp[j]) {
      ans.push_back(pp[j]);
      j ++;
      D -= 2;
    }
    else if(ptmp >= pp[j]) {
      ans.push_back(p[i]);
      i ++;
      D --;
    }    
  }
  
  int sum = 0;
  ascent(i, 0, ans.size()) {
    sum += ans[i];
  }
  cout << sum << endl;
  
  return 0;
}

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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

int main(void){
    ll N,K; cin >> N >> K;
    vector<ll> A(N); REP(i,N) cin >> A[i];
    if(K == 0){
        ll sum = 0;
        REP(i,N) sum += A[i];
        cout << sum << endl;
        return 0;
    }
    ll digit = 0;
    REP(i,60) if(K>>i) digit++; else break;
    ll c = 0;
    REP(i,N) c += A[i]>>digit;
    c <<= digit;

    ll n[digit+1][2]; CLR(n);
    REP(i,digit){
        REP(j,N){
            if(A[j]>>i & 1) n[digit-i][1]++;
            else n[digit-i][0]++;
        }
    }

    ll dp[digit+1][2]; CLR(dp);
    dp[1][0] = n[1][0];
    dp[1][1] = n[1][1];
    FOR(i,1,digit){
        dp[i+1][0] = dp[i][0]*2 + n[i+1][!(K>>(digit-1-i)&1)];
        dp[i+1][1] = dp[i][1]*2 + max(n[i+1][0], n[i+1][1]);
        if(K>>(digit-1-i)&1) dp[i+1][1] = max(dp[i+1][1], dp[i][0]*2 + n[i+1][1]);
    }

    ll ans = c + max(dp[digit][0], dp[digit][1]);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<iostream>

using namespace std;

int main() {

	int a, b, k;

	cin >> a >> b >> k;
	int cnt = 0;
	int x;
	if (a > b) {
		x = a;
	}
	else {
		x = b;
	}
	while (1) {

		if (a % x == 0 && b % x == 0)cnt++;
		if (cnt == k)break;
		x--;
	}
	cout << x << endl;
	return 0;

}

Measured memory_bytes: 3596
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<cmath>
#include<bitset>
#include<cstdio>
#include<iomanip>

#define PII pair<int,int>
#define LLI long long int

using namespace std;

int main(){
	int N;cin>>N;
	vector<int>H(N);for(int i=0;i<N;i++)cin>>H[i];
	vector<int>dp(N,1000010000);
	dp[0]=0;
	dp[1]=abs(H[0]-H[1]);
	for(int i=2;i<N;i++)dp[i]=min(dp[i-1]+abs(H[i]-H[i-1]),dp[i-2]+abs(H[i]-H[i-2]));
	cout<<dp[N-1]<<endl;

	return 0;
}



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

LIMIT = 10 ** 18

zeroflg = 0
for a in A:
    if a == 0:
        zeroflg = 1
        break

if zeroflg == 1:
    res = 0
else:
    res = 1
    for a in A:
        if a == 0:
            res = 0
            break
        if res > LIMIT:
            break
    
        res = res * a

# res = math.exp(sum([math.log(a) for a in A]))

# X = np.array(A)
# res = X.prod()
#
# print(X)

# for a in A:
#     math.log(a)
#
#     res = res * a

if res > LIMIT:
    print('-1')
else:
    print(res)


Measured memory_bytes: 21652
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long N;
  cin >> N;
  
  long A,B;
  A =N /100;
  B =(N - 100 * A) / 10;
  N = N - 100 * A - 10 * B;
  
    if (N == 2 || N == 4 || N == 5 || N == 7 || N == 9){
        cout << "hon" << endl;
    }else if(N == 3){
        cout << "bon" << endl;  
  }else{
      cout << "pon" << endl;
  }
}

Measured memory_bytes: 3640
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

//typedef pair<int,int> P;



int main(){
	char S[30];
	scanf("%s", S);

	int num[26];
	for(int i=0;i<26;i++) num[i] = 0;

	for(int i=0;i<strlen(S);i++){
		num[S[i]-'a']++;
	}

	bool found = true;
	for(int i=0;i<26;i++){
		if(num[i]>1) found = false;
	}
	if(found){
		bool all = true;
		char min;
		for(int i=0;i<26;i++){
			if(num[i]==0){
				all = false;
				min = 'a'+i;
				break;
			}
		}
		if(!all){
			printf("%s%c\n", S,min);
			return 0;
		}else{
			int m = 0;
			for(int i=strlen(S)-2;i>=0;i--){
				if(S[i]<S[i+1]){
					m = i+1;
					break;
				}
			}
			if(m==0){
				printf("-1\n");
			}else{
				char c = 'z';
				for(int i=m;i<26;i++){
					if(S[m-1]<S[i] && c>S[i]){
						c = S[i];
					}
				}
				S[m-1] = c;
				S[m] = 0;
				printf("%s\n", S);
			}

			return 0;
		}
	}


	return 0;
}


Measured memory_bytes: 128
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;

int early = 10;
int n;
int v[310000], w[310000];
vector< pair<int,int> > poss[310000];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i] >> w[i];
    }

    poss[0].push_back({0, 0});
    for (int i = 1; i < (1<<early) && i <= n; i++) {
        vector<pair<int,int>> tp;
        for (auto p : poss[i/2]) {
            tp.push_back(p);
            if (p.first + w[i] <= 100000) tp.push_back({p.first + w[i], p.second + v[i]});
        }

        sort(tp.begin(), tp.end(), [](pair<int,int> a, pair<int,int> b) { 
            return make_pair(a.first, -a.second) < make_pair(b.first, -b.second);
        });

        for (int j = 0; j < tp.size(); j++) {
            if (j == 0 || tp[j].second > poss[i].back().second) {
                poss[i].push_back(tp[j]);
            }
        }
    }

    // for (auto cp : poss[2]) {
    //     cout << "cp2 " << cp.first << " " << cp.second << endl;
    // }

    int q;
    cin >> q;
    for (; q; q--) {
        int cur, lim; cin >> cur >> lim;

        vector<pair<int,int>> curposs = {{0,0}};
        while (cur >= (1<<early)) {
            vector< pair<int,int> > tp;
            for (auto cp : curposs) {
                tp.push_back({cp.first, cp.second});
                tp.push_back({cp.first + w[cur], cp.second + v[cur]});
            }

            curposs = move(tp);
            cur /= 2;
        }

        // for (auto cp : poss[cur]) {
        //     cout << "cp2 " << cp.first << " " << cp.second << endl;
        // }

        int ans = 0;
        for (auto cp : curposs) {
            auto loc = lower_bound(poss[cur].begin(), poss[cur].end(), pair<int,int>{lim - cp.first + 1, -1});
            if (loc == poss[cur].begin()) continue;
            //cout << "cp2 " << cp.first << " " << cp.second << endl;
            ans = max(ans, cp.second + (--loc)->second);
        }

        cout << ans << '\n';
    }
}

Measured memory_bytes: 18200
from collections import defaultdict
import heapq

vertice,edge,source = map(int,input().split(" "))
link = defaultdict(dict)
weight = defaultdict(dict)
cost = [float("inf") for n in range(vertice)]
cost[source] = 0
for e in range(edge):
    i,j,w = map(int,input().split(" "))
    link[i] = link.get(i,set()) | {j}
    weight[i].update({j:w})

vertice_cnt = 0
while True:
    if vertice_cnt >= vertice:
        print("NEGATIVE CYCLE")
        break
    update = False
    for k,val in link.items():
        for v in val:
            if cost[v] > cost[k] + weight[k][v]:
                cost[v] = cost[k] + weight[k][v]
                update = True
    if not(update):
        for c in cost:
            print(c if c != float("inf") else "INF")
        break
    vertice_cnt += 1

Measured memory_bytes: 8748
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;


int main(void){
  int n,i,a[10005],b[10005];
  ll asum=0,bsum=0,m;
  
  cin >> n;
  for (i=0;i<n;i++) cin >> a[i];
  for (i=0;i<n;i++) cin >> b[i];
  
  for (i=0;i<n;i++) asum+=a[i];
  for (i=0;i<n;i++) bsum+=b[i];
  
  m=bsum-asum;
  
  if (m<0){
    cout << "No" << endl;
    return 0;
  }
  
  if (m==0){
    for (i=0;i<n;i++){
      if (a[i]!=b[i]){
        cout << "No" << endl;
        return 0;
      }
    }
  }
  
  ll ao=0,bo=0;
  for (i=0;i<n;i++){
    if (a[i]==b[i]) continue;
    if (a[i]>b[i]){
      bo+=a[i]-b[i];
    }else{
      if ((b[i]-a[i])%2==0){
        ao+=(b[i]-a[i])/2;
      }else{
        ao+=(b[i]-a[i]+1)/2;
        bo++;
      }
    }
  }
  
  if (ao<=m && bo<=m && (m-ao)*2==m-bo){
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 384
fun main(args: Array<String>) {
  val (sx, sy, tx, ty) = readLine()!!.split(" ").map(String::toInt)

  var ans = ""
  for (i in sy until ty)  ans = ans + "U"
  for (i in sx until tx)  ans = ans + "R"
  for (i in ty downTo sy+1)  ans = ans + "D"
  for (i in tx downTo sx+1)  ans = ans + "L"
  
  ans = ans + "L"
  for (i in sy until ty+1)  ans = ans + "U"
  for (i in sx-1 until tx)  ans = ans + "R"
  ans = ans + "D"

  ans = ans + "R"
  for (i in ty downTo sy)  ans = ans + "D"
  for (i in tx+1 downTo sx+1)  ans = ans + "L"
  ans = ans + "U"
  
  println(ans)
}

Measured memory_bytes: 116232