akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    string s;
    int k;
    cin >> n >> s >> k;

    char c = s[k-1];

    for(int i = 0;i < n;i++){
        if(s[i] != c){
            s[i] = '*';
        }
    }

    cout << s << endl;
    return 0;
}


Measured memory_bytes: 256
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a] += [b]
    g[b] += [a]

for i in range(n):
    print(len(g[i]))


Measured memory_bytes: 2940
;; #!/usr/bin/env sbcl --script

(define-alien-routine getchar_unlocked char)

(declaim (optimize (speed 3) (safety 0) (debug 0))
         (inline getchar_unlocked))

(deftype int32 () '(signed-byte 32))

(declaim (ftype (function () int32) my-read-int))
(defun my-read-int ()
  (let ((is-negative nil)
        (result 0))
    (declare (type int32 result)
             (type boolean is-negative))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (and (/= cur-char 45)
                  (not (<= 48 cur-char 57)))
       finally (if (<= 48 cur-char 57)
                   (setf result (- cur-char 48))
                   (setf is-negative t)))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (<= 48 cur-char 57)
       do (setf result (+ (ash result 3) (ash result 1) (- cur-char 48))))
    (when is-negative (setf result (- result)))
    result))

(defun main ()
  (let* ((n (my-read-int))
         (k (my-read-int))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (my-read-int)))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         while (< j n)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         if (< nval (aref dp j))
         do
           (setf (aref dp j) nval)))
    (format t "~a~%" (aref dp (- n 1)))))

(main)



Measured memory_bytes: 8680
#include<iostream>
#include<cmath>

using namespace std;

int isprime(int x){
	if(x == 2) return 1;
	if(x < 2 || x % 2 == 0) return 0;

	int i = 3;
	while(i <= sqrt(x)){
		if(x % i == 0) return 0;
		i += 2;
	}
	return 1;
}

int main(){
	int i, *k, n, judge, c = 0;
	
	cin >> n;
	k = new int[n];
	for(i = 0; i < n; i++){
		cin >> k[i];
		if((judge = isprime(k[i])) == 1) c++;
	}
	cout << c << endl;
	return 0;
}

Measured memory_bytes: 3076
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define E "\n"

using namespace std;
const long long MOD = (long long)1e9 + 7;

int n, k, q, a[2019], b[2019];
multiset<int> mst, tst;

void gun(int ss, int ee)
{
	int cnt = ee - ss + 1;
	cnt = cnt - k + 1;
	if (cnt <= 0) return;
	tst.clear();
	for (int i = ss; i <= ee; i++) {
		if (tst.size() == cnt) {
			auto x = tst.end();
			x--;
			if (*x > a[i]) {
				tst.erase(x);
				tst.insert(a[i]);
			}
		}
		else {
			tst.insert(a[i]);
		}
	}
	for (auto y : tst) {
		if (mst.size() == q) {
			auto x = mst.end();
			x--;
			if (*x > y) {
				mst.erase(*x);
				mst.insert(y);
			}
		}
		else {
			mst.insert(y);
		}
	}
}

int fun(int x)
{
	int ss = 0, ee = 0;
	mst.clear();
	while (1) {
		while (ss < n && a[ss] < x) {
			ss++;
		}
		if (ss == n) break;
		ee = ss;
		while (ee + 1 < n && a[ee + 1] >= x) {
			ee++;
		}
		gun(ss, ee);
		ss = ee + 1;
	}
	if ((int)mst.size() == q) {
		auto x = mst.begin();
		int tmp = *x;
		x = mst.end();
		x--;
		tmp = *x - tmp;
		return tmp;
	}
	else return 1e9;
}

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> k >> q;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		b[i + 1] = a[i];
	}
	sort(b, b + n + 1);
	int ans = MOD;
	for (int i = 0; i < n + 1; i++) {
		ans = min(ans, fun(b[i]));
	}
	cout << ans << E;
	//system("pause");
	return 0;
}

Measured memory_bytes: 512
def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    """
    h, n, w <= 3500
    4/N = 1/h+1/n+1/w
    1/h = 4/N-1/n-1/w = 4nw-Nw-Nn / Nnw
    """
    N = read_int()
    for w in range(1, 3501):
        for n in range(1, 3501):
            if (4*n*w-N*w-N*n) != 0 and (N*n*w)%(4*n*w-N*w-N*n) == 0:
                h = (N*n*w)//(4*n*w-N*w-N*n)
                if h > 0:
                    return h, n, w


if __name__ == '__main__':
    print(*solve())


Measured memory_bytes: 65920
S = input()
T = input()

cnt = 0
for i in range(len(S) - len(T) + 1):
    s = S[i:i+len(T)]
    tmp = 0
    for j, t in enumerate(T):
        if s[j] == t:
            tmp += 1
    cnt = max(cnt, tmp)

print(len(T) - cnt)

Measured memory_bytes: 9056
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPLL(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
template<class T>inline bool chmax(T &a, const T &b){if(a < b){a = b; return 1;}return 0;}
template<class T>inline bool chmin(T &a, const T &b){if(a > b){a = b; return 1;}return 0;}
typedef long long ll;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, M, K; cin >> N >> M >> K;
  vector<ll> A(N), B(M);
  vector<ll> AA(N), BB(M);
  REPLL(i, N){
    cin >> A[i];
    if(i != 0){
      AA[i] = A[i]+AA[i-1];
    }else{
      AA[i] = A[i];
    }
  }
  REPLL(i, M){
    cin >> B[i];
    if(i != 0){
      BB[i] = B[i]+BB[i-1];
    }else{
      BB[i] = B[i];
    }
  }
  ll ans = 0;
  ll Nidx, Midx;
  ll KK;
  auto itr = lower_bound(AA.begin(), AA.end(), K);
  if(itr == AA.end()){
    itr--;
    Nidx = N;
    KK = K - *itr;
  }else if(itr == AA.begin() && *itr > K){
    auto itrM = lower_bound(BB.begin(), BB.end(), K);
    if(itrM == BB.end()){
      cout << M << endl;
      return 0;
    }else if(itrM == BB.begin() && *itrM > K){
      cout << 0 << endl;
      return 0;
    }else if(*itrM > K){
      itrM--;
      ll res = itrM-BB.begin()+1;
      cout << res << endl;
      return 0;
    }else{
      ll res = itrM-BB.begin()+1;
      cout << res << endl;
      return 0;
    }
  }else if(*itr > K){
    itr--;
    Nidx = itr-AA.begin()+1;
    KK = K - *itr;
  }else{
    Nidx = itr-AA.begin()+1;
    KK = K - *itr;
  }
  while(Nidx > -1){
    auto itrM = lower_bound(BB.begin(), BB.end(), KK);
    if(itrM == BB.end()){
      ll res = M + Nidx;
      ans = max(ans, res);
      break;
    }else if(itrM == BB.begin() && *itrM > KK){
      ll res = Nidx;
      ans = max(ans, res);
    }else if(*itrM > KK){
      itrM--;
      ll res = itrM-BB.begin()+1;
      res += Nidx;
      ans = max(ans, res);
    }else{
      ll res = itrM-BB.begin()+1;
      res += Nidx;
      ans = max(ans, res);
    }
    Nidx--;
    if(itr != AA.begin()){
      itr--;
      KK = K - *itr;
    }else{
      KK = K;
    }  
  }
  cout << ans << endl;
}

Measured memory_bytes: 9536
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    vector<int> a,b;
    cin>>n;
    for(int i=0;i<n;i++){
        int p,q;
        cin>>p>>q;
        a.push_back(p);
        b.push_back(q);
    }
    long long ans=0;
    for(int i=b.size()-1;i>=0;i--){
        if((a[i]+ans)%b[i]!=0)ans+=b[i]-((a[i]+ans)%b[i]);
    }
    cout<<ans<<endl;
    return 0;
}

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

signed main() {
  int N, M;
  cin >> N;
  
  map<int, int> pro;
  int D;
  for( int i = 0; i < N; i++ ){
    cin >> D;
    if( pro.count(D) ) pro.at(D)++;
    else pro[D] = 1;
  }
  
  
  cin >> M;
  int T;
  for( int i = 0; i < M; i++ ){
    cin >> T;
    if( pro.count(T) ){
      pro.at(T)--;
    }else{
      cout << "NO" << endl;
      return 0;
    }
    if( pro.at(T) == -1 ){
      cout << "NO" << endl;
      return 0;
    }
  }
  cout << "YES" << endl;
    
}



Measured memory_bytes: 12800
#include <bits/stdc++.h>
typedef long long ll;
const int N = 2e5 + 100;
const int inf = 0x3f3f3f3f;
using namespace std;
int n, f[N][3], b[N];
ll a[N];
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%lld",&a[i]), b[i]=inf;
    for(int j=0;j<=20;++j) {
        for(int i=1;i<=n;++i)
            if(a[i]&(1<<j)){
                f[i][0] = 0;
            }
            else {
                f[i][0] = f[i-1][0] + 1;
            }
        for(int i=1;i<=n;++i) {
            for(int k=1;k<=2;++k) {
                if(a[i]&(1<<j)) {
                    f[i][k] = f[i-1][k-1] + 1;
                }
                else {
                    f[i][k] = f[i-1][k] + 1;
                }
            }
            b[i] = min(b[i],f[i][1]);
            //cout << f[i][1] <<' ';
        }//puts("");
    }
    ll ans = 0;
    for(int i=1;i<=n;++i) ans += 1LL*b[i];
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 4992
import sys

input = sys.stdin.readline

P = 998244353


def main():
    N = int(input())
    D = list(map(int, input().split()))

    cnt = [0] * (max(D) + 1)
    for d in D:
        cnt[d] += 1

    if not ((D[0] == 0) and (cnt[0] == 1) and (0 not in cnt)):
        print(0)
    else:
        ans = 1
        for i in range(len(cnt) - 1):
            ans *= pow(cnt[i], cnt[i + 1], P)  # a**b mod p
            ans %= P
        print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 14388
#include <bits/stdc++.h>
using namespace std;
long long i,j,n,m,t,k,dp[101][100001],a[200001],b[200001];
int main () {
    cin>>n>>m;
    for (i=1;i<=n;i++) {
        cin>>a[i]>>b[i];
    }
    for (i=1;i<=n;i++) {
        for (j=1;j<=m;j++) {
            if (j-a[i]<0) {dp[i][j]=dp[i-1][j];continue;}
            dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+b[i]);
        }
    }
    cout<<dp[n][m]<<endl;
    return 0;
}

Measured memory_bytes: 80256
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <list>
#include <stack>
#define REP(i, n) for(i=0; i<n; i++)
#define REPR(i, n) for(i=n; i>=0; i--)
#define FOR(i, m, n) for(i=m; i<n; i++)
#define max(a, b) ((a)>(b) ? (a):(b))
#define min(a, b) ((a)<(b) ? (a):(b))
#define INF 10000000000
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
typedef pair<double, double> doup;

ll a[100000], n;

ll calc(ll s) {
    ll i;
    ll sum=0, res=0;
    for(i=0; i<n; i++, s*=-1) {
        sum+=a[i];
        if(sum*s>0) continue;
        if(s>0) {
            res+=s-sum; sum=1;
        }else {
            res+=sum-s; sum=-1;
        }
    }
    
    return res;
}

int main(void) {
    cin >> n;
    ll i;
    for(i=0; i<n; i++) {
        cin >> a[i];
    }
    
    cout << min(calc(1), calc(-1));
}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <map>
#define INF 1000000000
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define LOOP(i,N) for(int i=0;i<N;i++)
#define LOOP1(i,N) for(int i=1;i<=N;i++)
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;

#define int long long

signed main(){
  int K,A,B;
  cin >> K >> A >> B;
  int ans;
  double S = (B-A)/2;
  if(A>=K){
    ans = K+1;
  }else{
    if(S>=1){
      int N = (K-A+1)/2;
      ans = A + N * (B-A);
      if((K-A+1)%2==1){
        ans += 1;
      }
    }else{
      ans = K+1;
    }
  }
  cout << ans << endl;
  return 0;
}
// ;;


Measured memory_bytes: 256
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int H = sc.nextInt();
        int W = sc.nextInt();
        char x[][] = new char[H][W];
        for (int i = 0; i < H; i++) {
            String tmpword = sc.next();
            for (int j = 0; j < W; j++) {
                x[i][j] = tmpword.charAt(j);
            }
        }
        int a[] = { 0, 0, 1, 1, 1, -1, -1, -1 };
        int b[] = { 1, -1, 1, -1, 0, 1, -1, 0 };
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                int tmp = 0;
                for (int k = 0; k < 8; k++) {
                    int i2 = i + a[k];
                    int j2 = j + b[k];
                    if (i2 >= 0 && i2 < H && j2 >= 0 && j2 < W) {
                        if (x[i2][j2] == '#') {
                            tmp++;
                        }
                    }
                }
                if (x[i][j] == ('.')) {
                    System.out.print(tmp);
                    // x[i][j] = (char)tmp;
                } else {
                    System.out.print("#");
                    // x[i][j] = '#';
                }
            }
            System.out.println("");
        }
    }

}

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

namespace ABC_Training
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = Console.ReadLine();
            if (IsAC(s))
            {
                Console.WriteLine("AC");
            }
            else
            {
                Console.WriteLine("WA");
            }
        }

        private static bool IsAC(string s)
        {
            return IsStartA(s) && ContainsC(s) && OneA(s) && OneC(s) && Small(s);
        }

        private static bool Small(string s)
        {
            var current = s.Where(x => x != 'A' && x != 'C');
            if(current.Count() == 0)
            {
                return false;
            }
            return current.All(x => char.IsLower(x));
            

        }

        private static bool OneC(string s)
        {
            return s.Length - s.Count(x => x != 'C') == 1;
        }

        private static bool OneA(string s)
        {
            return s.Length - s.Count(x => x != 'A') == 1;
        }



        private static bool ContainsC(string s)
        {
            var sl = s.Length;
            var currentString = s.Skip(2).Take(sl - 3);
            return currentString.Any(x => x == 'C') ;
        }

        private static bool IsStartA(string s)
        {
            return s[0] == 'A';
        }
    }
}


Measured memory_bytes: 11348
def main():
    x = int(input())

    while True:
        flag_prime = True
        x_ = int(x**0.5) + 1
        for i in range(2, x_):
            if x % i == 0:
                flag_prime = False
                break
        
        if flag_prime:
            break 
        x += 1  
    
    print(x)


if __name__ == "__main__":
    main()

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
    
int main(){
    int k, t;
    cin >> k >> t;
    int a, m = -1;
    for(int i = 0; i < t; i++){
        cin >> a;
        m = max(m, a);
    }
    cout << max(0, 2 * m - 1 - k) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,n) for(int64_t i=0;i<(n);i++)
#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REV(c) reverse((c).begin(),(c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define COUTDOUBLE(d,i) cout << setprecision((i)) << (d) << endl;
#define VIN(v) for(int64_t i=0;i<(v.size());i++){cin >> (v).at(i);}

using namespace std;
typedef int64_t I;
typedef pair<I,I> P;
typedef vector<I> V;

I MOD = pow(10,9)+7;

// 最大公約数
I gcd(I a, I b) {
  return b != 0 ? gcd(b, a % b) : a;
}

// 最小公倍数
I lcm(I a, I b) {
  return a * b / gcd(a, b);
}

int ctoi(char c){
  return c - '0';
}

int main(){
  I x,y,z,k;
  cin >> x >> y >> z >> k;
  V a(x), b(y), c(z);
  VIN(a);
  VIN(b);
  VIN(c);
  SORT(a);REV(a);
  SORT(b);REV(b);
  SORT(c);REV(c);
  V t;
  REP(i,x*y){
    I l,m,n;
    l = i % x;
    m = (i / x) % y;
    t.push_back(a.at(l) + b.at(m));
  }
  SORT(t);
  REV(t);
  V an;
  REP(i,min(k+1,x*y)){
    REP(j,z){
      an.push_back(t.at(i) + c.at(j));
    }
  }
  SORT(an);
  REV(an);
  REP(i,k){
    cout << an.at(i) << endl;
  }
  return 0;
}



Measured memory_bytes: 43440
#include <iostream>

using namespace std;

int main()
{
    int H,W;
    cin>>H>>W;
    char c;
    char m[H][W];
    for(int i=0;i<H;i++)
    {
        for(int j=0;j<W;j++)
        {
            cin>>m[i][j];
        }
    }
    long long int s[H][W];
    s[0][0]=1;
    int k=W;
    for(int i=1;i<W;i++)
    {
        if(m[0][i]=='.')
        {
            s[0][i]=1;
        }
        else
        {
            k=i;
            break;
        }
    }
    for(int i=k;i<W;i++)
    {
        s[0][i]=0;
    }
    k=W;
    for(int i=1;i<H;i++)
    {
        if(m[i][0]=='.')
        {
            s[i][0]=1;
        }
        else
        {
            k=i;
            break;
        }
    }
    for(int i=k;i<H;i++)
    {
        s[i][0]=0;
    }
    for(int i=1;i<H;i++)
    {
        for(int j=1;j<W;j++)
        {
            if(m[i][j]=='#')
            {
                s[i][j]=0;
            }
            else
            {
                s[i][j]=(s[i-1][j]+s[i][j-1])%1000000007;
            }
        }
    }
    /*for(int i=0;i<H;i++)
    {
        for(int j=0;j<W;j++)
        {
            cout<<s[i][j]<<" ";
        }
        cout<<endl;
    }*/
    cout<<(s[H-1][W-1])%1000000007;
    return 0;
}


Measured memory_bytes: 9088
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            if (array[0] == array[1])
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }
}


Measured memory_bytes: 13268
#include<bits/stdc++.h>
using namespace std;
#define p 1000000007
int main()
{
    string s,t;
    cin>>s;
    cin>>t;
    int n=s.length(),m=t.length();
    int ans=INT_MAX;
    for(int i=0;i<n-m+1;i++)
    {
        int c=0;
        int k=i;
        for(int j=0;j<m;j++)
        {
            if(s[k]!=t[j])
                c++;
            k++;
        }

        ans=min(c,ans);
    }
    cout<<ans;
    return 0;
}





Measured memory_bytes: 3584
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();


        this.CandleCount = inpt[0];
        this.FireCount = inpt[1];

        this.Candles = Reader.ReadLine().Split(' ').Select(a => long.Parse(a)).ToArray();

        this.Dist = new long[this.CandleCount];

        this.Dist[0] = 0;
        for (int i = 1; i < this.CandleCount; i++)
        {
            this.Dist[i] = this.Dist[0] + (this.Candles[i] - this.Candles[i - 1]);
        }

        long ans = long.MaxValue;
        for (int i = 0; i < this.CandleCount; i++)
        {
            int stIdx = i;
            int edIdx = i + FireCount - 1;
            if (edIdx >= this.CandleCount)
            {
                break;
            }
            long mv = Math.Min(Math.Abs(this.Candles[edIdx]), Math.Abs(this.Candles[stIdx]));
            mv += this.Candles[edIdx] - this.Candles[stIdx];
            ans = Math.Min(mv, ans);
        }
        Console.WriteLine(ans);

    }


    long[] Dist;

    long[] Candles;

    int CandleCount;
    int FireCount;


    public class Reader
    {
        static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (sr == null)
                {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"



5 3
-30 -10 10 20 50



";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
    }
}


Measured memory_bytes: 23392
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 3 * 1e3 + 5;

int n;
long long a[MAXN];
long long z;

void read_input()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
        z += a[i];
    }
}

pair<long long,long long> T[MAXN][MAXN];

pair <long long, long long> rec(int l, int r)
{
    int t = (l + n - r - 1) % 2;
    if(l == r)
    {
        if(t == 0) return {a[l], 0};
        if(t == 1) return {0, a[l]};
    }
    if(T[l][r].first != 0 || T[l][r].second != 0)
    {
        return T[l][r];
    }
    long long f;
    if(t == 0) f = rec(l, r - 1).first + a[r];
    else f = rec(l, r - 1).second + a[r];
    long long s;
    if(t == 0) s = rec(l + 1, r).first + a[l];
    else s = rec(l + 1, r).second + a[l];


    if(t == 1)
    {
        if(f > s)
        {
            return T[l][r] = {rec(l, r - 1).first, f};
        }
        else
        {
            return T[l][r] = {rec(l + 1, r).first, s};
        }
    }
    else
    {
        if(f > s)
        {
            return T[l][r] = {f, rec(l, r - 1).second};
        }
        else
        {
            return T[l][r] = {s, rec(l + 1, r).second};
        }
    }
}

void solve()
{
    long long X = rec(0, n - 1).first;
    //printf("%lld\n", X);
    printf("%lld\n", 2 * X - z);
}

int main()
{
    read_input();
    solve();

    return 0;
}


Measured memory_bytes: 139904
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] ss = br.readLine().split(" ");
        int n = Integer.parseInt(ss[0]), k = Integer.parseInt(ss[1]), l = Integer.parseInt(ss[2]);
        UnionFind ufr = new UnionFind(n), uft = new UnionFind(n);
        for(int i = 0; i < k; i++) {
            ss = br.readLine().split(" ");
            ufr.unite(Integer.parseInt(ss[0]) - 1, Integer.parseInt(ss[1]) - 1);
        }
        for(int i = 0; i < l; i++) {
            ss = br.readLine().split(" ");
            uft.unite(Integer.parseInt(ss[0]) - 1, Integer.parseInt(ss[1]) - 1);
        }
        Map<String, Integer> map = new HashMap<>();
        for(int i = 0; i < n; i++) {
            String key = new StringBuilder().append(ufr.find(i)).append(' ').append(uft.find(i)).toString();
            Integer val = map.get(key);
            map.put(key, val == null ? 1 : val + 1);            
        }
        for(int i = 0; i < n; i++) {
            if(i != 0) {
                System.out.print(' ');
            }
            String key = new StringBuilder().append(ufr.find(i)).append(' ').append(uft.find(i)).toString();
            System.out.print(map.get(key));
        }
        System.out.println();
    }
}

class UnionFind {
    int[] parent;
    int[] rank;

    public UnionFind(int size) {
        parent = new int[size];
        rank = new int[size];
        for(int i = 0; i < size; i++) {
            parent[i] = i;
        }
    }

    public int find(int x) {
        return parent[x] == x ? x : find(parent[x]);
    }

    public void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return;
        }
        if(rank[x] < rank[y]) {
            parent[x] = y;
        } else {
            parent[y] = x;
            if(rank[x] == rank[y]) rank[x] ++;
        }
    }

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

    public void show() {
        System.out.println(Arrays.toString(parent));
    }
}

Measured memory_bytes: 111920
n = int(input())
factors = [0]*(n+1)
for i in range(2, n+1):
    tmp = i
    for j in range(2, int(i**0.5)+1):
        if tmp%j==0:
            while tmp%j==0:
                tmp //= j
                factors[j] += 1
        if tmp==1:
            break
    else:
        factors[tmp] += 1
ans = 1
for i in factors:
    ans *= i+1
print(ans%(10**9+7))

Measured memory_bytes: 3064
import java.util.*;

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


        System.out.println(ans-b);

    } 
}

Measured memory_bytes: 23636
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;

using namespace std;
int kaijo(int n){
	int ans = 1;
	if (n != 0){
		for (int i = 0; i < n; i++){
			ans *= (n - i);
		}
	}
	else{
		ans = 1;
	}
	return ans;
}

int main(){
	int n;
	cin >> n;
	string s;
	int flag = 0;
	REP(i, n){
		cin >> s;
		if (s == "Y"){
			flag = 1;
		}
	}
	if (flag == 1){
		cout << "Four" << endl;

	}
	else{
		cout << "Three" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <vector>
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll, ll> P;

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

vector<vector<int>> g;
vector<ll> c;
vector<ll> sum;

void dfs(int v, ll s) {
	if (sum[v] >= 0) {
		return;
	}
	sum[v] = s + c[v];
	for (auto e : g[v]) {
		dfs(e, sum[v]);
	}
}

int main(void)
{
	int n, q;
	cin >> n >> q;
	g.resize(n);
	c.resize(n, 0);
	sum.resize(n, -1);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	rep(i, q) {
		int p, x;
		cin >> p >> x;
		p--;
		c[p] += x;
	}
	dfs(0, 0);
	rep(i, n) {
		cout << sum[i] << " ";
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 22656
// Author: getharshkumar

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e6 + 6, M = 1e9 + 7;
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (ll &e : v)
        cin >> e;
    if (is_sorted(v.begin(), v.end()))
        return cout << "YES", 0;
    for (ll i = 0; i < n; i++)
        for (ll j = i + 1; j < n; j++)
        {
            swap(v[i], v[j]);
            if (is_sorted(v.begin(), v.end()))
                return cout << "YES", 0;
            swap(v[i], v[j]);
        }
    cout << "NO";
    return 0;
}

Measured memory_bytes: 256
N,W = map(int, input().split())

p = [[] for i in range(4)]
for i in range(N):
    ww,v = map(int, input().split())
    if i == 0:
        w0 = ww
    p[ww % 4].append(v)
w = [0 for i in range(4)]
v = [0 for i in range(4)]
for i in range(4):
    p[i].sort(reverse=True)
    w[(w0+i)%4] = w0+i
maxv = 0
lens = [len(p[i])+1 for i in range(4)]
for i in range(lens[0]):
    sumw = w[0] * i
    if sumw > W:
        break
    v[0] = sum(p[0][:i])
    maxv = max(maxv, sum(v))
    for j in range(lens[1]):
        sumw = w[0] * i + w[1]*j
        if sumw > W:
            break
        v[1] = sum(p[1][:j])
        maxv = max(maxv, sum(v))
        for k in range(lens[2]):
            sumw = w[0] * i + w[1]*j + w[2]*k
            if sumw > W:
                break
            v[2] = sum(p[2][:k])
            maxv = max(maxv, sum(v))
            for l in range(lens[3]):
                sumw = w[0] * i + w[1]*j + w[2]*k + w[3]*l
                if sumw > W:
                    break
                v[3] = sum(p[3][:l])
                maxv = max(maxv, sum(v))
            v[3] = 0
        v[2] = 0
        v[3] = 0
    v[1] = 0
    v[2] = 0
    v[3] = 0    
v[0] = 0
print(maxv)


Measured memory_bytes: 47964
import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int h = sc.nextInt();
        int max = Integer.MIN_VALUE;
        int[][] swords = new int[n][2];
        for (int i = 0; i < n; i++) {
            int[] xs = new int[2];
            xs[0] = sc.nextInt();
            xs[1] = sc.nextInt();
            swords[i] = xs;

            if (xs[0] > max) {
                max = xs[0];
            }
        }
        Arrays.sort(swords, (x, y) -> y[1] - x[1]);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (h <= 0) {
                System.out.println(count);
                return;
            }

            int[] sword = swords[i];
            if (sword[1] <= max) {
                break;
            }
            h -= sword[1];
            count++;
        }

        System.out.println((int) Math.ceil(((double) h) / max) + count);
    }
}


Measured memory_bytes: 96920
#include <iostream> 
#include <cstdlib>
#include <cmath>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++) 

int main() {
    int N;
    cin >> N;

    int A[N];
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        if (A[i] % 2 != 0) {
            cout << "0" << endl;
            return 0;
        }
        int Counter = 0;
        int j = 0;
        while (A[i]%2 == 0) {
            A[i] /= 2;
            Counter += 1;
            j++;
        }
        A[i] = Counter;
    }

    int min = A[0];
    for (int i = 0; i < N; i++) {
        if (min > A[i]) {
            min = A[i];
        }
    }
    cout << min << endl;
}



Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>

using namespace std;

#define x first
#define y second
#define NMAX 54

int n;
long long k, v[NMAX];

void precomputeSizes() {
    v[0] = 1;
    for(int i = 1; i <= n; i++) {
        v[i] = 2 * v[i - 1] + 3;
      //  printf("%d %lld\n", i, v[i]);
    }
}

long long countPatty(int burger_level, long long height_level) {
    if(!burger_level)
        return 1;
        
    if(height_level == 1)    
        return 0;
        
    if(height_level <= v[burger_level - 1] + 1)
        return countPatty(burger_level - 1, height_level - 1);
        
    if(height_level <= v[burger_level - 1] + 2)
        return (1LL << burger_level);
        
    if(height_level < v[burger_level])
        return (1LL << burger_level) + countPatty(burger_level - 1, height_level - 2 - v[burger_level - 1]);

    return (1LL << (burger_level + 1)) - 1;
}

int main () {
    
    scanf("%d%lld",&n,&k);
    
    precomputeSizes();
    printf("%lld\n", countPatty(n, k));
    
    return 0;
}



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

int main ()
{
    int n, m, p;

    while (cin >> n >> m >> p, (n || m || p)) {
        vector<int> x(n);
        int sum = 0;
        for (auto &i : x) cin >> i, sum += i;

        sum *= (100 - p);

        if (x[m-1] == 0) cout << 0 << endl;
        else cout << (int)(sum / x[m-1]) << endl;
    }

    return 0;
}

Measured memory_bytes: 1196
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i,h) for(int i = 0; i < h; ++i)
#define rep1(i,h) for(int i = 0; i <= h; ++i)
#define rep2(i,k,h) for(int i = k; i < h; ++i)
#define rep3(l,k,h) for(int l = k; l <= h; ++l)
#define INF LLong_MAX // long long INF
#define inf INT_MAX // int inf

typedef pair<int,int> int_pair;
typedef pair<string,string> string_pair;


int main() {
    int n,res = 0, count = 1, ans = 0;
    cin >> n;
    vector<vector<int>> a(2,vector<int> (n));
    rep(i,2) {
        rep(j,n) {
            cin >> a.at(i).at(j);
        }
    }
    while(count != n+1) {
        for(int i = 0; i < count; i++) {
            res += a.at(0).at(i);
        }
        for(int i = count - 1; i < n; i++) {
            res += a.at(1).at(i);
        }
        ans = max(ans, res);
        res = 0;
        count++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n,  = read_line(int)

m = 1000000007

domino = []
domino.append(input())
domino.append(input())

# print(domino[0])
# print(domino[1])

def compress(line):
    x = [line[0]]
    for c in line[1:]:
        if x[-1] != c:
            x.append(c)
    return x

domino[0] = compress(domino[0])
domino[1] = compress(domino[1])

xs = []
for i in range(len(domino[0])):
    xs.append(domino[0][i] != domino[1][i])
# print(xs)

patterns = None
if xs[0]:
    patterns = 6
else:
    patterns = 3

lastX = xs[0]
for x in xs[1:]:
    if x:
        # T -> T
        if lastX:
            patterns *= 3
        else:
            # F -> T
            patterns *= 2
    else:
        # T -> F
        if lastX:
            patterns *= 1
        else: # F -> F
            patterns *= 2
    lastX = x

print(patterns % m)

# T -> T

# 6 * 4 = 24

# 0|1120
# 1|2002

# 0
# 2

# 1
# 0

# 1
# 2

# 2
# 0

# 2
# 1


# F -> T

# 3 * 2 = 6

# 0|12
# 0|21
# 
# 1|02
# 1|20
# 
# 2|10
# 2|01


# T -> F

# 6 * 1 = 6

# 0|2
# 1|2

# 0|1
# 2|1

# 1|2
# 0|2

# 1|0
# 2|0

# 2|1
# 0|1

# 2|0
# 1|0

# F -> F

# 3 * 2 = 6

# 0 | 12
# 1 | 02
# 2 | 01


Measured memory_bytes: 3064
n = int(input())
a = list(map(int, input().split()))
ans = []
if n % 2 == 0:
    for i in range(n - 1, -1, -2):
        if i % 2 == 1:
            ans.append(a[i])
    for i in range(0, n, 2):
        if i % 2 == 0:
            ans.append(a[i])
else:
    for i in range(n - 1, -1, -2):
        if i % 2 == 0:
            ans.append(a[i])
    for i in range(1, n, 2):
        if i % 2 == 1:
            ans.append(a[i])

print(" ".join(map(str, ans)))

Measured memory_bytes: 29764
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr double INF{1e9};
constexpr int MAGIC_PRIME{1000000000 + 7};
constexpr array<int, 4> dy{-1, 1, 0, 0};
constexpr array<int, 4> dx{0, 0, -1, 1};
constexpr int div_ru(int a, int b)
{
    return (a - b + 1) / b;
}
unsigned euclidean_gcd(unsigned a, unsigned b)
{
    if (a < b)
        return euclidean_gcd(b, a);
    unsigned r;
    while ((r = a % b))
    {
        a = b;
        b = r;
    }
    return b;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int x, y;
    int ans;
    cin >> x >> y;
    if (x%y==0)
        ans = -1;
    else
        ans = x;
    cout << ans << endl;

    return 0;
}
// 　　　　　　_......_
// 　　　　 , ´　 <＊y　♪
// 　　　　 i,（i ﾉﾉﾊ）_）
// 　　　　ﾉ_iﾊﾟ ヮﾟﾉﾊ
// 　　　 　 ,´i'_ｿﾉj,〈つ　))
// 　　　　〈_,ﾉ＾＾＾ヽ.´
// 　　　　 .＾＾i_ｿ_ｿ^´
// 　　　　 ￣￣ ￣　


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

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;

// Library
vector<P> prime_factorize(ll n) {
    vector<P> res;
    for (ll p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        res.push_back(make_pair(p, num));
    }
    if (n != 1) res.push_back(make_pair(n, 1));
    return res;
}
//

int main() {
#ifdef MYLOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif
    int N; cin >> N;
    ll ans = 1;
    map<ll, ll> mp;
    for(int i = 2; i <= N; ++i) {
        vector<P> vec = prime_factorize(i);
        for (auto itr : vec) {
            mp[itr.first] += itr.second;
        }
    }
    for(auto itr : mp){
        ans *= (itr.second + 1);
        ans %= MOD;
    }

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



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    int N, K;
    cin >> N >> K;

    int path = ((N - 1) * (N - 2)) / 2;

    if (K > path) {
        cout << -1 << endl;
        return 0;
    }
    
    int M = N - 1 + (path - K);
    cout << M << endl;

    rep(i, N - 1) {
        cout << 1 << " " << (i + 2) << endl;
    }

    int diff = path - K;
    repd(i, 2, N + 1) {
        repd(j, i + 1, N + 1) {
            if (diff <= 0) break;
            diff--;
            cout << i << " " << j << endl;
        }
        if (diff <= 0) break;
    }
    return 0;
}

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

int main(int argc, const char * argv[])
{

    for (int i=1; i<=9; i++) {
        for (int j=1; j<=9; j++) {
            cout << i << 'x' << j << '=' << i*j << endl;
        }
    }
    return 0;
}

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

int main(){
	int a, b, c, d, e;
	scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
	printf("%d\n", min(a, min(b, c)) + min(d, e) - 50);
	return 0;
}

Measured memory_bytes: 1020
import heapq

INF = 1 << 60
MOD = 1000000007

def dijkstra(s):
    #print(s)
    global d, G, n
    d = [INF] * n
    d[s] = 0
    que = [(0, s)]
    heapq.heapify(que)

    while len(que) > 0:
        #print("que=", que)
        p = heapq.heappop(que)
        v = p[1]
        #print(v)
        if d[v] < p[0]:
            continue
        for e in G[v]:
            if d[e[0]] > d[v] + e[1]:
                d[e[0]] = d[v] + e[1]
                heapq.heappush(que, (d[e[0]], e[0]))

n, m, s, t = map(int, input().split())
u = [0] * m
v = [0] * m
a = [0] * m
b = [0] * m
G = []
for i in range(n):
    G.append([])
for i in range(m):
    u[i], v[i], a[i], b[i] = map(int, input().split())
    G[u[i] - 1].append((v[i] - 1, a[i]))
    G[v[i] - 1].append((u[i] - 1, a[i]))
#print("G=", G)
d = [0] * n
dijkstra(s - 1)
#print("d=", d)
ds = d[:]
G = []
for i in range(n):
    G.append([])
for i in range(m):
    G[u[i] - 1].append((v[i] - 1, b[i]))
    G[v[i] - 1].append((u[i] - 1, b[i]))
d = [0] * n
dijkstra(t - 1)
dst = []
for i in range(n):
    dst.append((d[i] + ds[i], i))
dst.sort()
change = 0
money = 1000000000000000
for i in range(n):
    while dst[change][1] < i:
        change += 1
    print(money - dst[change][0])

Measured memory_bytes: 75872
n = gets.to_i
sum = 0

10.times do |i|
  if  n < 10 ** (i + 1)
    if i.even?
      sum += (n - 10 ** i + 1)
    end

    break
  end

  if i.even?
    sum += 10 ** i * 9
  end
end

puts sum

Measured memory_bytes: 1788
solve n m
  | n <= 1 && m <= 1 = 0
  | n <= 1 = m * (m-1) `div` 2
  | otherwise = n * (n-1) `div` 2 + m * (m-1) `div` 2 

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  print $ solve n m


Measured memory_bytes: 508
import java.util.Scanner;
import java.util.Arrays;

class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        while(true){
            String str = input.nextLine();
            String[] str1 = str.split(" ");
            int x = Integer.parseInt(str1[0]);
            int y = Integer.parseInt(str1[1]);
            if(x==0 && y==0){
                break;
            }else{
                int[] output = {x,y};
                Arrays.sort(output);
                System.out.println(output[0] + " " + output[1]);
            }
            

        }
    }
}


Measured memory_bytes: 35456
import java.util.*;
import java.io.PrintWriter;
public class Main{
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int[] h=new int[n];
		for(int i=0; i<n; i++){
			h[i]=sc.nextInt();
		}
		int[] ans=new int[n];
		for(int i=0; i<m; i++){
			int a=sc.nextInt()-1;
			int b=sc.nextInt()-1;
			if(h[a]>h[b]){
				if(ans[a]!=-1){
					ans[a]=1;
				}
				ans[b]=-1;
			}else if(h[b]>h[a]){
				if(ans[b]!=-1){
					ans[b]=1;
				}
				ans[a]=-1;
			}else{
				ans[a]=-1;
				ans[b]=-1;
			}
		}
		int an=0;
		for(int i=0; i<n; i++){
			if(ans[i]!=-1){
				an++;
			}
		}
		System.out.println(an);
	}
}

Measured memory_bytes: 59772
input = $stdin.read.split("\n")
input.shift

array = []
input.each do |row|
	array << row.gsub('.', '0').gsub('#','1').split('').map!(&:to_i)
end

array = array.reject {|row| row.inject(:+) == 0}
reversed_array = array.transpose
reversed_array = reversed_array.reject {|row| row.inject(:+) == 0}
converted_array = reversed_array.transpose

converted_array.each do |row|
	puts row.join.gsub('0', '.').gsub('1', '#')
end


Measured memory_bytes: 2044
N, M = gets.split.map(&:to_i)
ps = M.times.map { gets.chomp.split }
ac_c = 0
wa_c = 0
ac_cond = Array.new(N)
wa_cond = Array.new(N, 0)
ps.each do |pv, s|
  pv = pv.to_i 
  pv -= 1
  ac = ac_cond[pv]
  wa_count = wa_cond[pv]
  case s
  when 'AC'
    next if ac
    ac_cond[pv] = true
    ac_c += 1
    wa_c += wa_count
  when 'WA'
    wa_cond[pv] += 1
  end
end
puts "#{ac_c} #{wa_c}"

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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)

int main (void) {
    int N,K;
    cin>>N>>K;
    int ans = inf;

    for (int i=0;i<(1<<N);i++) {
        int n = 1;
        for (int j=0;j<N;j++) {
            int temp;
            temp = i % (1<<j);
            if (temp == 0) n*=2;
            else n+=K;
        }
        ans = min(n,ans);
    }
    cout<<ans<<endl;
    return 0;
}

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

ans=10**9

def sum(x):
    r=0
    for i in range(0,N):
        r+=(A[i]-x)**2
    return r

for j in range(-100,101):
    if sum(j)<=ans:
        ans=sum(j)

print(ans)        


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


int main() {
    long long int N;
    std::cin >> N;

    std::vector<long long int> A(N + 1);
    for (long long int i = 0; i < N + 1; i++) {
        std::cin >> A[i];
    }
    std::vector<long long int> B(N);
    for (long long int i = 0; i < N; i++) {
        std::cin >> B[i];
    }

    long long int count = 0;
    for (long long int i = N - 1; i >= 0; i--) {
        if (A[i + 1] >= B[i])  {
            count += B[i];
            B[i] = 0;
        } else {
            count += A[i + 1];
            B[i] -= A[i + 1];
        }
        count += std::min(A[i], B[i]);
        A[i] = std::max(A[i] - B[i], 0ll);
    }
    std::cout << count << std::endl;
}


Measured memory_bytes: 4824
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define int long long int
using namespace std;
#define pii pair <int, int>
#define mii map<int, int>
#define pb push_back
#define deb(x) cout << #x << " " << x << endl
#define deb2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl
#define Loop(s, e, itr) for (int itr = s; itr < e; itr++)
#define loop(n) for(int i = 0; i < n; i++)
#define vin vector<int>
#define w(t) int tc; cin >> tc; for(int t = 1; t <= tc; t++)
#define vec vector
#define mod 1000000007
#define all(x) x.begin(), x.end()


int n, k;
int arr[105];

int dp_first[100005];
int dp_second[100005];


bool second(int);

bool first(int a){
    if(a == 0) return false;
    if(dp_first[a] == -1){
        bool flag = false;
        bool ans = false;
        loop(n){
            if(arr[i] <= a){
                flag = true;
                ans |= second(a - arr[i]);
            }
        }
        dp_first[a] = ans & flag;
    }
    return dp_first[a];
}

bool second(int a){
    if(a == 0) return true;
    if(dp_second[a] == -1){
        bool flag = true;
        bool ans = true;
        loop(n){
            if(arr[i] <= a){
                flag = false;
                ans &= first(a - arr[i]);
            }
        }
        dp_second[a] = ans | flag;
    }
    return dp_second[a];
}


int32_t main(){
    memset(dp_first, -1, sizeof(dp_first));
    memset(dp_second, -1, sizeof(dp_second));
    cin >> n >> k;
    loop(n) cin >> arr[i];
    if(first(k)) cout << "First\n";
    else cout << "Second\n";
}

Measured memory_bytes: 9808
# -*- coding: utf-8 -*-
import sys
from collections import deque

H,W=map(int, sys.stdin.readline().split())
N=input()
A=map(int, sys.stdin.readline().split())


L=[]

cnt=0
flag="r"
q=deque([]) 
for i,a in enumerate(A):
	for u in range(a):
		cnt+=1
		#print "u,i,a :::: ",u,i+1,a,cnt,flag
		if flag=="r":
			q.append(i+1)
		elif flag=="l":
			q.appendleft(i+1)

		if cnt==W:
			cnt=0
			L.append(q)
			q=deque([]) 
			if flag=="r": flag="l"
			elif flag=="l": flag="r"

for u in L:
	for x in u:
		print x,
	print



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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
#define F first
#define S second
#define sz(x) (int)x.size()
#define db(x) cerr << #x << ": " << x << "\n";

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

    ll A, B, N;
    cin >> A >> B >> N;
    ll X =  min(B-1, N);
    cout << floor(A*X/B)-A*floor(X/B) << '\n';
}


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

typedef long long ll;

// chmax, chmin
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; }

int main() {
    ll X;
    cin >> X;
    
    ll ans = 0;
    for ( ll i = 1; i < 40; ++i){
        ll y = i;
        for ( ll j = 2; j <= 10; ++j){
            y *= i;
            if ( y > X ) break;
            chmax(ans,y);
        }
    }
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 3640
#include <bits/stdc++.h>
#define RI register int
typedef long long LL;

using namespace std;

int const MAXN = 1 << 13;
int f[MAXN];

int main() {
  
//  freopen("a.in", "r", stdin);
//  freopen("a.out", "w", stdout);

  int n, m; cin >> n >> m;
  memset(f, 0x7f, sizeof(f));
  f[0] = 0;
  int all = 1 << n;
  for (RI i = 1; i <= m; ++i) {
    int cost, cnt; cin >> cost >> cnt;
    int s = 0;
    for (RI j = 1, x; j <= cnt; ++j)
      cin >> x, s |= (1 << (x - 1));
    for (RI j = 0; j < all; ++j)
      f[j | s] = min(f[j | s], f[j] + cost);
  }
  if (f[all - 1] == 0x7f7f7f7f) puts("-1");
  else printf("%d\n", f[all - 1]);
  
  return 0;
}

// created by Daniel yuan


Measured memory_bytes: 256
use strict;
use warnings;
use feature 'say';
use Data::Dumper;

my ($total_types, $buying_types) = split / /, <STDIN>;
chomp(my $line2 = <STDIN>);
my @fruits = sort { $a <=> $b } split / /, $line2;

my @slice = @fruits[0 .. ($buying_types - 1)];

my $total = 0;
map { $total += $_ } @slice;
say $total;



Measured memory_bytes: 6632
import bisect

n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

ans = 0
for b in B:
    x = bisect.bisect_left(A, b)
    y = bisect.bisect_right(C, b)
    ans += x * (n - y)
    
print(ans)

Measured memory_bytes: 23240
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,k,jc[5555],injc[5555],ans;
int binpow(int a,int t)
{
	int res=1,p=a;
	for (int i=t;i;i>>=1)
	{
		if (i&1) res=1ll*res*p%mod;
		p=1ll*p*p%mod;
	}
	return res;
}
void Init()
{
	jc[0]=injc[0]=1;
	for (int i=1;i<=5000;i++)
	{
		jc[i]=1ll*jc[i-1]*i%mod;
		injc[i]=1ll*injc[i-1]*binpow(i,mod-2)%mod;
	}
}
int C(int n,int k)
{
	if (n<k) return 0;
	return 1ll*jc[n]*injc[k]%mod*injc[n-k]%mod;
}
int main()
{
	scanf("%d%d",&n,&k);
	Init();
	ans=C(n+k-2,n-1)-C(n+k-2,n);
	if (n>k) ans=1ll*ans*binpow(2,n-k-1)%mod;
	printf("%d\n",(ans+mod)%mod);
	return 0;
}

Measured memory_bytes: 256
use itertools::Itertools as _;
use proconio::{input, marker::Chars};

fn main() {
    input! {
        n: Chars,
    }
    if n.into_iter().filter(|c| *c == '7').count() > 0 {
        println!("{}", "Yes");
    } else {
        println!("{}", "No");
    }
}


Measured memory_bytes: 2140