akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;

ll gcd(ll x, ll y);
int fac(ll n, vector<int> num);

const int INF = 1001001001;

//mint�p�̒萔 (10��9��)
const int mod = 1000000007;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }

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

    vector<vector<int> > miti(n, vector<int> (0));
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        miti[a].emplace_back(b);
        miti[b].emplace_back(a);
    }

    queue<int> room;
    room.push(0);
    vector<int> ans(n, INF);
    while (!room.empty()) {
        int num = room.front(); room.pop();
        for (int i : miti[num]) {
            if (ans[i] != INF) continue;
            ans[i] = num;
            room.push(i);
        }
    }

    cout << "Yes" << endl;
    rep(i, n) {
        if (i == 0) continue;

        cout << ans[i] + 1 << endl;
    }
    return 0;
}

/*���C�u�����ő����*/
//���[�O���b�h�̌ݏ��@
ll gcd(ll x, ll y) {
    ll num[3];
    num[0] = (x > y) ? x : y;
    num[1] = (x <= y) ? x : y;
    num[2] = num[0] % num[1];

    while (num[2]) {
        num[0] = num[1];
        num[1] = num[2];
        num[2] = num[0] % num[1];
    }

    return num[1];
}

/*�f��������*/
//���색�C�u����
int fac(ll n, vector<int> num) {
    ll kazu = n;
    for (int i = 2; i <= sqrt(n); i++) {
        while (1) {
            if (kazu % i != 0) break;

            kazu /= i;
            num[i]++;
        }
    }
    if (kazu == n && n != 1) return 1;
    int ans = 0;
    if (kazu != 1) ans++;
    for (int i = 2; i <= sqrt(n); i++) {
        for (int j = 1; j <= num[i]; j++) {
            num[i] -= j;
            ans++;
        }
    }
    return ans;
}

Measured memory_bytes: 10032
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define repn(a,b,c) for(int a=b; a>=c; a--)
#define ll long long
#define sum(a,b) a=a+b
#define mem(a, b) memset(a, (b), sizeof(a))
#define loop(a,b,c) for(int  a=b ; a<c ; a++)
#define first fi
#define second se
#define INF (int)1e5

ll a[100005];
ll b[100005];
ll c[100005];

ll n;

ll memo[INF + 5];

ll dp[INF + 5][5];


// ll solve(ll i , ll type){

//     if(i > n){
//         return 0;
//     }

//     if(memo[i]!=-1){
//         return memo[i];
//     }

//     ll best;

//     if(type == 1){

//         best = solve(i+1,2)+a[i];

//         best= max(best,solve(i+1,3)+a[i]);

//     }else if(type == 2){


//         best = solve(i+1,1)+b[i];

//         best= max(best,solve(i+1,3)+b[i]);

//     }else if(type == 3){


//         best = solve(i+1,1)+c[i];

//         best= max(best,solve(i+1,2)+c[i]);
//     }


//     memo[i]=best;

//     return best;

// }

int main(){

     ios_base::sync_with_stdio(false);
     cin.tie(0);

     mem(memo , -1);
     mem(dp, 0);

     cin>>n;

     rep(i,1,n){
        cin>>a[i]>>b[i]>>c[i];
     }


     rep(i,1,n){
        ll x = dp[i-1][1];
        ll y = dp[i-1][2];
        ll z = dp[i-1][3];

        dp[i][1]=max(b[i]+y , c[i] + z);
        dp[i][2]=max(a[i]+x , c[i] + z);
        dp[i][3]=max(a[i]+x , b[i] + y);

     }

     // ll ans = max(solve(2,1)+a[1],max(solve(2,2)+b[1],solve(2,3)+c[1] ) );

     // cout<<ans<<endl;


     ll ans=max(dp[n][1],max(dp[n][2],dp[n][3]));

     cout<<ans<<endl;

    return 0;
}





Measured memory_bytes: 7296
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <list>
#include <array>
#include <cstdlib>
#include <stack>
#include <string>
#include <queue>
#include <chrono>
#include <functional>
#include <limits>
#include <cmath>
#include <algorithm>
#include <random>
#include <regex>
#include <tuple>
#include <numeric>
#include <cassert>
#include <utility>
#include <bitset>
#include <complex>
#include <iomanip>
#include <ostream>
#include <sstream>
#include <ctime>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define index INDEX
#ifndef ONLINE_JUDGE
#define print(x) PRINT(x, #x)
template<typename T> void PRINT(T VARIABLE, const string& NAME) {
	cerr << NAME << " = " << fixed << VARIABLE << "\n";
}
#else
#define print(x) 0 
#endif
int generate_random() {
	const int MAX_RANDOM = (int)1e6;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	return uniform_int_distribution<int>(0, MAX_RANDOM)(rng);
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll INFLL = 2 * (ll)1e18 + 100;
#define int ll
#ifdef int 
const int INFINT = INFLL;
#else
const int INFINT = 2 * (int)1e9 + 100;
#endif
const double PI = atan(1) * 4;
const double EPS = 1e-6;
const int SEED = (int)1e3 + 7;
const int MOD = (int)1e9 + 7;
const int NMAX = (int)3 * 1e3 + 5;

string s, t;
string dp[2][NMAX];

int32_t main() {
#ifndef ONLINE_JUDGE
	double START_PROGRAM = clock();
#endif
	srand(time(NULL));
	cout << setprecision(20) << fixed;
	FASTIO;
    cin >> s >> t;
    string sol;
    for1(i, (int)s.size())
    {
        int cur = i & 1;
        int prev = cur ^ 1;
        dp[cur][0] = "";
        for1(j, (int)t.size())
        {
            if(dp[prev][j].size() > dp[cur][j - 1].size())
                dp[cur][j] = dp[prev][j];
            else
                dp[cur][j] = dp[cur][j - 1];

            if(s[i - 1] == t[j - 1] && 
                    dp[prev][j - 1].size() + 1 > dp[cur][j].size())
                dp[cur][j] = dp[prev][j - 1], dp[cur][j].push_back(s[i - 1]);

            if(sol.size() < dp[cur][j].size()) sol = dp[cur][j];
        }
    }
    cout << sol;






#ifndef ONLINE_JUDGE
	double END_PROGRAM = clock();
	double ELAPSED_TIME = (END_PROGRAM - START_PROGRAM) / CLOCKS_PER_SEC;
	cerr << "\n\nElapsed Time: " << ELAPSED_TIME * 1000 << "\n";
#endif
	return 0;
}







Measured memory_bytes: 7808
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
using namespace std;
#define i64  long long
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
#define MEM_SIZE 10000

// int DP[MEM_SIZE][MEM_SIZE] = {0};
// int GMEM[MEM_SIZE][MEM_SIZE] = {0};
template<typename T> void DEBUG(T e){std::cout << e << std::endl;}
template<class T> void DEBUG(string str, T e){std::cout <<str << ">>" << e << std::endl;}
template<typename T> void DEBUG(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ DEBUG(v); } }
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void map_add(map<int,int> & mp, int A)
{
  auto itr = mp.find(A);
  if(itr != mp.end())
  {
    itr->second++;
  }
  else
  {
    mp[A] = 1;
  }
  return;
}
void solve(void)
{
  int W,a,b;
  cin>>W>>a>>b;
  if(a > b) swap(a,b);
  int X = b - a - W;
  cout<<max(X,0LL)<<endl;

  
  return; 
}


int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(9);
  solve();

 
  return 0;
}

Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().split()))
cnt = 0
flag = True

while flag:
    ans = []
    for i in a:
        if i%2 == 1:
            flag = False
        else:
            ans.append(i/2)
    if len(ans) == n:
        cnt += 1
        a = ans

print(cnt)

Measured memory_bytes: 3064
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <vector>
using namespace std;
string st[3];
vector <int> a;
long long cnt[100][10][10];
const int MOD = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 2; i++) {
    cin >> st[i];
  }
  for (int j = 0; j < n; j++) {
    if (st[0][j] == st[1][j]) {
      a.push_back(0);
    } else {
      a.push_back(1);
      j++;
    }
  }
  if (a[0] == 0) {
    for (int cur = 0; cur < 3; cur++) {
      cnt[0][cur][cur]++;
    }
  } else {
    for (int cur = 0; cur < 3; cur++) {
      for (int pv = 0; pv < 3; pv++) {
        if (cur == pv) {
          continue;
        }
        cnt[0][cur][pv]++;
      }
    }
  }
  for (int i = 1; i < (int)a.size(); i++) {
    if (a[i] == 0) {
      for (int pva = 0; pva < 3; pva++) {
        for (int pvb = 0; pvb < 3; pvb++) {
          for (int cur = 0; cur < 3; cur++) {
            if (cur == pva || cur == pvb) {
              continue;
            }
            cnt[i][cur][cur] += cnt[i - 1][pva][pvb];
            cnt[i][cur][cur] %= MOD;
          }
        }
      }
    } else {
      for (int pva = 0; pva < 3; pva++) {
        for (int pvb = 0; pvb < 3; pvb++) {
          for (int cura = 0; cura < 3; cura++) {
            for (int curb = 0; curb < 3; curb++) {
              if (cura == curb || cura == pva) {
                continue;
              }
              if (curb == pvb) {
                continue;
              }
              cnt[i][cura][curb] += cnt[i - 1][pva][pvb];
              cnt[i][cura][curb] %= MOD;
            }
          }
        
        }
      }
    }
  }
  long long ans = 0;
  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
      ans += cnt[(int)a.size() - 1][x][y];
      ans %= MOD;
    }
  }
  cout << ans;
  return 0;
}


Measured memory_bytes: 256
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

sys.setrecursionlimit(1000000)
# MOD = 10 ** 9 + 7
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]

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

L = list(range(L, L + N))

ans = sum(L)
L1 = list(filter(lambda x: x >= 0, L))
L2 = list(filter(lambda x: x < 0, L))

if len(L2) == 0:
    ans -= min(L1)
elif len(L1) == 0:
    ans -= max(L2)
else:
    if abs(min(L1)) < abs(max(L2)):
        ans -= min(L1)
    else:
        ans -= min(L2)

print(ans)


Measured memory_bytes: 3764
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	var str string
	fmt.Sscanf(scanner.Text(), "%s", &str)
	chars := strings.Split(str, "")

	ans := math.MaxInt64
	for i := 0; i < len(chars)-2; i++ {
		c := chars[i : i+3]
		v, _ := strconv.Atoi(strings.Join(c, ""))

		diff := v - 753
		if diff < 0 {
			diff = -diff
		}
		ans = min(ans, diff)
	}
	fmt.Println(ans)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


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

#define ll long long
#define Fi first
#define Se second
#define Pb push_back
#define vi vector<int>

const int INF = 1e9;
const int MOD = 1e9+7;

#define all(v) v.begin(),v.end()
#define output_s(a) printf("%s\n",(a).c_str())

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define Rep(i,n,m) for(int i=(int)(n);i<(int)(m);i++)
#define RepS(i,n,m) for(int i=(int)(n);i<=(int)(m);i++)
#define RRepS(i,n,m) for(int i=(int)(n);i>=(int)(m);i++)
#define RepA(x) for(auto &item:(x))

void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}    
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

//library----------------------------------------------

int main() {
    CIN(N);
    vi p(N),q(N),l(N);
    rep(i,N)cin>>p[i];
    rep(i,N)cin>>q[i];
    RepS(i,1,N)l[i-1]=i;
    int nump,numq,count=0;
    while(next_permutation(all(l))){
        bool flag = true;
        count++;
        rep(i,N){
            if(p[i]!=l[i]){
                flag=false;
                break;
            }
        }
        if(flag)nump=count;
        flag=true;
        rep(i,N){
            if(q[i]!=l[i]){
                flag=false;
                break;
            }
        }
        if(flag)numq=count;
    }
    printf("%d\n",abs(nump-numq));

    return 0;
}

Measured memory_bytes: 256
def examC():
    A, B, C, D, E, F = LI()
    water =set()
    for i in range(30):
        for j in range(30):
            if 100 * (i*A+j*B) > F or (i==0 and j==0):
                continue
            water.add(100 * (A*i+B*j))
    sugar = set()
    for i in water:
        j = 0
        while(True):
            if (j * C) * 100 > i * E or i+(j*C)>F:
                break
            k = 0
            while(True):
                if (j*C+k*D)*100>i*E or i+(j*C+k*D)>F:
                    j +=1
                    break
                sugar.add((i+j*C+k*D,j*C+k*D))
                k +=1
    ans = [0,0]; cur = float(-1)
    for i in sugar:
        if cur<i[1]/i[0]:
            ans = [i[0],i[1]]
            cur = i[1]/i[0]
    print(" ".join(map(str,ans)))
#    print(sugar)


import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Measured memory_bytes: 40048
use std::io;
use std::cmp;
use std::collections::BinaryHeap;

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}


fn main() {
    let hw:Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let h = hw[0];
    let w = hw[1];

    let mut map = vec![vec![false;w+2];h+2];
    let mut count:i32 = 0;

    for i in 0..h {
        let line:Vec<char> = read_line().chars().collect();
        for j in 0..w {
            map[i+1][j+1] = line[j]=='.';
            count += (line[j]=='#') as i32;
        }
    }

    let mut memo:Vec<Vec<i32>> = vec![vec![50000;w+2];h+2];

    let mut pq = BinaryHeap::new();
    pq.push((0, 1, 1));

    let dx:Vec<i32> = vec![-1,0,1,0];
    let dy:Vec<i32> = vec![0,-1,0,1];

    while !pq.is_empty() {
        let &(cost, y, x) = pq.peek().unwrap();
        pq.pop();
        if -cost >= memo[y][x] {
            continue;
        }

        memo[y][x] = -cost;
        for i in 0..4 {
            let ny = (y as i32 + dy[i]) as usize;
            let nx = (x as i32 + dx[i]) as usize;
            if map[ny][nx] {
                pq.push((cost-1, ny, nx))
            }
        }
    }
    if memo[h][w] > (h*w) as i32 {
        println!("-1");
    } else {
        println!("{}", (h*w) as i32 - count - memo[h][w] -1);
    }

}


Measured memory_bytes: 4476
#include <bits/stdc++.h>
#define rep2(i,m,n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define bg begin()
#define ed end()
#define fi first
#define se second
using namespace std;
using ll = long long;
using vint = vector<int>;
using pint = pair<int,int>;
using vpint = vector<pint>;
template <typename T>
T sq(T x){
  return x * x;
}
ll mod = 1e9 + 7;

signed main (){
 int N; 
cin >> N;
vint a(N);
vint table(101010);
ll sum_i =0;
rep(i,N){
  cin >>a.at(i);
  ++table.at(a.at(i));
sum_i += a.at(i);
}
int Q;
cin >> Q;
vpint P(Q);
rep(i,Q){
int B,C;
  cin >> B >> C;
  P.at(i) = make_pair(B,C);
}
rep(i,Q){
// cout << P.at(i).fi << P.at(i).se<< endl;
// cout << table.at(P.at(i).first) << endl;
ll ans;
  if(table.at(P.at(i).fi) > 0){
  ans = sum_i - (P.at(i).fi - P.at(i).se )*table.at(P.at(i).fi );
sum_i = ans;
int num = table.at(P.at(i).fi);
  table.at(P.at(i).fi)-= num;
table.at(P.at(i).se) += num;
}else{
ans = sum_i;
}

  cout << ans <<endl;
}


    return 0;
}


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

const int MAX_N = 1 << 17;
const int INF = (1LL << 31) - 1;

template <class T>
struct MinSegTree3 {
	int n;
	struct S {
		bool flag;
		T dat;
		S() : dat(INF), flag(false) {}
	};

	S node[2 * MAX_N - 1];
	void init(int n_) {
		n = 1;
		while(n < n_) n *= 2;
	}

	void propagate(int k) {
		if(!node[k].flag) return;
		node[k].flag = false;
		if(2 * k + 1 < 2 * n - 1) {
			S& t = node[k];
			S& l = node[2 * k + 1];
			S& r = node[2 * k + 2];
			l.flag = r.flag = true;
			l.dat = r.dat = t.dat;
		}
	}

	void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }

	void update(int a, int b, T x, int k, int l, int r) {
		propagate(k);
		if(r <= a || b <= l) return;
		if(a <= l && r <= b) {
			node[k].dat = x;
			node[k].flag = true;
			propagate(k);
			return;
		}
		update(a, b, x, k * 2 + 1, l, (l + r) / 2);
		update(a, b, x, k * 2 + 2, (l + r) / 2, r);
		// merge
		node[k].dat = min(node[2 * k + 1].dat, node[2 * k + 2].dat);
	}

	T query(int a, int b) { return query(a, b, 0, 0, n); }

	T query(int a, int b, int k, int l, int r) {
		propagate(k);
		if(r <= a || b <= l) return INF;
		if(a <= l && r <= b) return node[k].dat;
		T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
};

MinSegTree3<int> st;

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

	int n, q;
	cin >> n >> q;
	st.init(n);

	while(q--) {
		int a, s, t, x;
		cin >> a;
		if(a == 0) {
			cin >> s >> t >> x;
			st.update(s, t + 1, x);
		}
		else {
			cin >> s >> t;
			cout << st.query(s, t + 1) << endl;
		}
	}
}

Measured memory_bytes: 5220
#include <bits/stdc++.h>
#define int long long 
#define float long double 
#define el '\n'
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
        enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
        ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
        *this << "[";
        for (auto it = d.b; it != d.e; ++it)
                *this << ", " + 2 * (it == d.b) << *it;
        ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int hell = 1e9 + 7 ;

int32_t main(){
    	IOS;
	int n , k ; 
	cin >> n >> k ; 
	vector<int>dp(k+1);
	dp[0] = 1;
	for(int child = 0 ; child < n ; child++ ){
		int limit ; 
		cin >> limit ;
		vector<int>fake(k+1);
		for(int candi = k ; candi >= 0 ; candi-- ){
			int temp = dp[candi];
			int l = candi + 1;
			int r = candi + min(limit,k-candi);
			if(l <= r)
				(fake[l] += temp)%=hell;
			if(r < k)
				(fake[r+1] += hell - temp)%= hell;
		}
		int prefix = 0 ;
		for(int i=0;i<=k;i++){
			( prefix += fake[i] ) %= hell ;
			( dp[i] += prefix ) %= hell; 
		}
	}
	cout << dp[k] << endl ;
    	return 0;
}


Measured memory_bytes: 1792
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <time.h>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vll vector<ll>
typedef long long int ll;
typedef long double ld;
using namespace std;

//-------------------------------------------------------------------
#define P 998244353
#define N_MAX 1000001
typedef long long int ll;
ll fac[N_MAX+1];
ll inv[N_MAX+1];
ll finv[N_MAX+1];

ll comb(ll n, ll k){
    return (((fac[n]*finv[n-k])%P)*finv[k])%P;
}

void init(){
    fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1;
    for(int i = 2; i <= N_MAX; i++){
        fac[i] = (fac[i-1]*i)%P;
        inv[i] = ((-(P/i)*inv[P%i])%P+P)%P;
        finv[i] = (finv[i-1]*inv[i])%P;
    }
}
ll pp(ll a, ll b){
  return (a * b)%P;
}
//-------------------------------------------------------------------
int main(int argc, char const *argv[]) {
  ll v = 1000001;
  ll a, n;std::cin >> n;
  init();
  vector<ll> S(v+1, 0), sq(v+1, 0);
  vector<ll> w(v+1, 0);
  for(int i=0;i<n;i++){
    scanf("%lld", &a);
    S[a] += a;
    sq[a] += a*a;
  }

  for(int i=1;i<v;i++){
    w[i] = (w[i] + inv[i])%P;
    for(int j=2;j*i<v;j++) w[j*i] = (w[j*i] - w[i] + P)%P;
  }
  ll ans = 0;
  for(int i=1;i<v;i++){
    ll tmp = 0, tmpsq = 0;
    for(int j=1;i*j<v;j++){
      tmp = (tmp + S[i*j])%P;
      tmpsq = (tmpsq+sq[i*j])%P;
    }
    //if(i<10) std::cout << tmp << " " << tmpsq << '\n';
    ll k = (pp(tmp, tmp) - tmpsq + P)%P;
    ans = (ans + pp(k, w[i]))%P;
  }

  std::cout << pp(inv[2], ans) << '\n';
  return 0;
}


Measured memory_bytes: 47104
# -*- coding: utf-8 -*-
import sys

#tmp= int(input())
#tmp = input().split()
#W,H,x,y,r = list(map(lambda a: int(a), tmp))


for i in range(0,10000):
    tmp= int(input())
    if(tmp==0):
        sys.exit()

    strout="Case "+str(i+1)+": "+str(tmp)
    print(strout)




Measured memory_bytes: 5620
#include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=101101;
int a[N];
int n,k;
int main(){
	scanf("%d%d",&n,&k);
	for (int i=1; i<=n; i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	int sum=0;
	for (int i=1; i<=k; i++)
		sum=sum+a[i];
	printf("%d\n",sum);
    return 0;
}

Measured memory_bytes: 1732
#include <iostream>
#include <cstdio>
#include <cassert>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << endl;
#define idbg(x) #x " = " << x
#include "pretty_print.h"
#else
#define dbg(x)
#define idbg(x) ""
#endif

#ifdef _WIN32
#  define LLD "%I64d"
#else
#  define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0? -x : x; }
template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; }
template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; }


const int MOD = 1e+9 + 7;

vector<ll> f(2e+6 + 42);

ll powmod(ll a, ll b) {
    if (b == 0) {
        return 1;
    }
    ll r = powmod(a, b >> 1);
    (r *= r) %= MOD;
    return b & 1? a * r % MOD : r;
}

ll divmod(ll a, ll b) {
    return a * powmod(b, MOD - 2) % MOD;
}

ll C(int n, int k) {
    return divmod(f[n], f[k] * f[n - k] % MOD);
}

ll get(int r, int c) {
    return C(r + c, r);
}


int main(int /* argc */, char** /* argv */)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    assert(freopen("inp", "r", stdin));
    assert(freopen("out", "w", stdout));
    #endif

    f[0] = 1;
    for (int i = 1; i < (int)f.size(); ++i) {
        f[i] = f[i - 1] * i % MOD;
    }

    int r1, c1, r2, c2;
    while (cin >> r1 >> c1 >> r2 >> c2) {
        ll cur = get(r1, c1);
        ll ans = cur;
        for (int i = 1; i <= (r2 + c2) - (r1 + c1); ++i) {
            (cur *= 2) %= MOD;
            if (r1 + i <= r2) {
                cur += get(r1 + i, c1 - 1);
            } else {
                cur -= get(r2, c1 + i - (r2 - r1 + 1));
            }

            if (c1 + i <= c2) {
                cur += get(r1 - 1, c1 + i);
            } else {
                cur -= get(r1 + i - (c2 - c1 + 1) , c2);
            }
            cur %= MOD;
            (ans += cur) %= MOD;
        }
        cout << (ans + MOD) % MOD << endl;
    }

    cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;
    return 0;
}


Measured memory_bytes: 16000
/* Created on 2019/8/13 14:28:53*/
#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define max (a, b) a >= b?a:b
#define min (a, b) a <= b?a:b
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
const int maxn = 110, inf = 0x3f3f3f3f;
int p[maxn], t[maxn << 2], n, m, c, v;
void pushup(int k) {
	t[k] = t[k << 1] + t[k << 1 | 1];
}
void build(int k, int l, int r) {
	if (l == r)t[k] = p[l];
	else {
		int mid = (l + r) >> 1;
		build(k << 1, l, mid);
		build(k << 1 | 1, mid + 1, r);
		pushup(k);
	}
}
void updata(int c, int v, int l, int r, int k) {
	if (l == r) {
		t[k] = v;
		return;	
	}
	int mid = (l + r) >> 1;
	if (c <= mid) updata(c, v, l, mid, k << 1);
	else updata(c, v, mid + 1, r, k << 1 | 1);
	pushup(k);
}
int main() {
	ios::sync_with_stdio;
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> p[i];
	build(1, 1, n);
	cin >> m;
	while (m--) {
		cin >> c >> v;
		int g = p[c];
		updata(c, v, 1, n, 1);
		cout << t[1] << "\n";
		updata(c, g, 1, n, 1);
	}
	//cout << "Time : " << 1.0*clock() / CLOCKS_PER_SEC << "s.\n";
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, N) for(int i=0, i##_len=(N); i<i##_len; i++)
#define reps(i, N) for(int i=1, i##_len=(N); i<=i##_len; i++)
#define rrep(i, N) for(int i=(N)-1; i>=0; i--)
#define rreps(i, N) for(int i=(N); i>=1; i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
template<class T>bool amax(T &a, const T &b) { if (a<b) {a=b; return true;} return false; }
template<class T>bool amin(T &a, const T &b) { if (a>b) {a=b; return true;} return false; }
using ll = long long;
using namespace std;
constexpr int inf = 1e9;
constexpr int mod = 1e9+7;

ll l, r;

int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    cin >> l >> r;
    ll ans = 2018;
    r = min({r, l+4038});
    for(ll i=l; i<=r; i++){
        for(ll j=i+1; j<=r; j++){
            ans = min({ans, i*j%2019});
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
cum_sum = [0]*n

total = 0
for i in range(n):
    total += a[i]
    cum_sum[i] = total

max_sum = cum_sum[-1]

ans = abs(cum_sum[0]-(max_sum-cum_sum[0]))
for i in range(n-1):
    ans = min(ans, abs(cum_sum[i]-(max_sum-cum_sum[i])))

print(ans)


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

int32_t main(){
	ios::sync_with_stdio(false); cin.tie(0);
	int n;
	cin >> n;
	vector< pair <int, int> > atv(n);
	for(int i = 0; i < n; ++i){
	    cin >> atv[i].second >> atv[i].first;
	}
	sort(atv.begin(), atv.end());
	int cur = 0;
	for(int i = 0; i < n; ++i){
	    cur += atv[i].second;
	    if(cur > atv[i].first){
	        cout << "No\n";
	        return 0;
	    }
	}
	cout << "Yes\n";
}

Measured memory_bytes: 3328
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int main(){
    cin>>n;
    for(int i=0;i<=10;i++){
        if(pow(2,i)<=n){
        	ans=pow(2,i);
		}
    }
    cout<<ans<<endl;
    return 0;
} 

Measured memory_bytes: 512
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string S[101]; int x[101], dp[101];
int main() {
	int n; cin >> n;
	for (int i = 0; i < n; i++) {
		S[i] = "";
		while (S[i] == "") {
			getline(cin, S[i]);
		}
		S[i] = S[i].substr(0, S[i].size() - 1);
	}
	for (int i = 0; i < n; i++) {
		int OK = 0;
		if (S[i].size() >= 4) {
			if (S[i].substr(0, 4) == "goto") {
				OK = 1;
				for (int j = 0; j < n; j++) {
					string T = S[i].substr(5, S[i].size() - 5);
					if (T == S[j]) {
						x[i] = j;
					}
				}
			}
		}
		if (OK == 0)x[i] = i;
	}
	for (int i = 0; i < 101; i++)dp[i] = 1 << 30;
	dp[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			dp[j + 1] = min(dp[j + 1], dp[j] + 1);
			dp[x[j] + 1] = min(dp[x[j] + 1], dp[j]);
		}
	}
	cout << dp[n] << endl;
	return 0;
}

Measured memory_bytes: 1224
#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;

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

const int dh[4]={1, 0, -1, 0};
const int dw[4]={0, 1, 0, -1};

int H, W;
vector<string> G;
vector<vector<int> > dist(50, vector<int>(50, -1));
queue<int> hque, wque;

void bfs() {
    while(!hque.empty() && !wque.empty()) {
        int ch, cw;
        ch = hque.front();
        cw = wque.front();
        // cout << "ch = " << ch << ", cw = " << cw << endl;
        hque.pop();
        wque.pop();
        
        rep(dir, 4) {
            int nh, nw;
            nh = ch + dh[dir];
            nw = cw + dw[dir];
            // cout << "nh = " << nh << ", nw = " << nw << endl;
            
            if(nh<0 || nh>=H || nw<0 || nw>=W) continue;
            if(G[nh][nw]!='.') continue;
            if(dist[nh][nw]!=-1) continue;
            
            hque.push(nh);
            wque.push(nw);
            dist[nh][nw] = dist[ch][cw] + 1;
            // cout << "dist[" << nh << "][" << nw << "]=" << dist[nh][nw] << endl;
        }
        // cout << endl;
    }
}

int black_counter(int h, int w){
    int counter=0;
    rep(i, h) {
        rep(j, w) {
            if(G[i][j]=='#') {
                counter++;
            }
        }
    }
    return counter;
}

int main() {
    cin >> H >> W;
    G.resize(H);
    rep(i, H) {
        cin >> G[i];
    }
    
    int ans=0;
    dist[0][0] = 1;
    hque.push(0);
    wque.push(0);
    bfs();
    if(dist[H-1][W-1]==-1) {
        cout << dist[H-1][W-1] << endl;
        return 0;
    }
    ans = H*W - dist[H-1][W-1];
    ans -= black_counter(H, W);
    cout << ans << endl;

    return 0;
    
}

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

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;

#define N 100010
#define M 200010
#define A 110
#define endl "\n"
#define mod 1000000007 

ll arr[500010];

int main(){
	
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	
	ll n, k;
	
	cin >> n >> k;
	
	ll ans = n;
	
	ll r = n % k;
	
	ans = min(ans, abs(r - k));
	
	if(n % k == 0) ans = 0;
	
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;

const ll maxN = 1e18;
const ll minN = -1e18;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);

//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 2e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))

/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/

struct Flower {
    int height, val;
};

int n;
Flower a[limit];
ll BIT[limit];

void update(int x, ll val) {
    for (int i = x; i <= n; i += i & -i) {
        BIT[i] = max(BIT[i], val);
    }
}

ll get(int x) {
    ll ans = 0;

    for (int i = x; i > 0; i -= i & -i) {
        ans = max(ans, BIT[i]);
    }

    return ans;
}

void solveProblem() {
    ll ans = 0;
    REP(i, 1, n) {
        ll curAns = get(a[i].height - 1);
        ans = max(ans, curAns + a[i].val);
        update(a[i].height, curAns + a[i].val);
    }

    cout << ans << endl;
}

void fastInput() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

void readInput() {
    cin >> n;
    REP(i, 1, n) cin >> a[i].height;
    REP(i, 1, n) cin >> a[i].val;
}

int main() {
    fastInput();
    readInput();
    solveProblem();
}



Measured memory_bytes: 6740
import java.util.Scanner;


public class Main {
	
	private int n;
	
	public static void main(String[] args) {
		new Main().solve();
	}
	
	
	public void solve(){
		Scanner scanner = new Scanner(System.in);
		
		double[] start = {0,0};
		double[] end = {100,0};
		n = scanner.nextInt();
		System.out.println(start[0]+" "+start[1]);
		koch(n,start,end);
	}
	
	public void koch(int i,double[] start,double end[]){
		
		if(i==0){
			System.out.println(end[0] + " "+end[1]);
			return;
		}
		double[] s = new double[2];
		double[] t = new double[2];
		double[] u = new double[2];
		for(int j=0;j<2;j++){
			s[j] = (2*start[j]+end[j])/3.0;
			t[j] = (start[j]+2*end[j])/3.0;
		}
		u[0]=(s[0]+t[0])/2 + Math.sqrt(3.0)*(s[1]-t[1])/2.0;
		u[1]=Math.sqrt(3.0)*(-s[0]+t[0])/2.0 +(s[1]+t[1])/2.0;
		koch(i-1, start,s);
		koch(i-1, s, u);
		koch(i-1, u, t);
		koch(i-1, t, end);	
	}
}



Measured memory_bytes: 32244
#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

std::vector<int> buf1;
std::vector<int> buf2;

template <typename Iter>
size_t merge(Iter begin, Iter mid, Iter end) {
  buf1.clear();
  buf2.clear();
  copy(begin, mid, back_inserter(buf1));
  copy(mid, end, back_inserter(buf2));
  auto it1 = buf1.begin();
  auto it2 = buf2.begin();
  size_t reverse_count = 0;
  for (auto it = begin; it != end; ++it) {
    assert(!(it1 == buf1.end() && it2 == buf2.end()));
    auto cur = &it1;
    if (it1 == buf1.end()) {
      cur = &it2;
    } else if (it2 == buf2.end()) {
      cur = &it1;
    } else if (*it1 < *it2) {
      cur = &it1;
    } else {
      reverse_count += distance(it1, buf1.end());
      cur = &it2;
    }
    *it = **cur;
    ++(*cur);
  }
  return reverse_count;
}

template <typename Iter>
size_t heapSort(Iter begin, Iter end) {
  auto mid = next(begin, distance(begin, end) / 2);
  if (mid == begin) {
    return 0;
  }
  size_t reverse_count = 0;
  reverse_count += heapSort(begin, mid);
  reverse_count += heapSort(mid, end);
  reverse_count += merge(begin, mid, end);
  return reverse_count;
}



int main() {
  int n;
  cin >> n;
  vector<int> nums;
  for (int t, i = 0; i < n; ++i) {
    cin >> t;
    nums.push_back(t);
  }
  auto reverse_count = heapSort(nums.begin(), nums.end());
  cout << reverse_count << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i<n; i++){
    int a;
    cin >> a;
    int counter = 0;
    while (a%2 == 0){
      counter++;
      a = a/2;
    }
    vec.at(i) = counter;
  }
  sort(vec.begin(), vec.end());
  cout << vec.at(0) << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using vi = vector<int>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using P = pair<int,int>;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const int INF = 1e9;

int main(){
    int h, w, si, sj, ti, tj;
    cin >> h >> w >> si >> sj >> ti >> tj;
    --si; --sj; --ti; --tj;
    vs maze(h);
    rep(i,h) cin >> maze[i];
    vvi dist(h, vi(w,INF));
    dist[si][sj] = 0;
    deque<P> q;
    q.emplace_back(si,sj);

    while (!q.empty()){
        int i = q.front().first, j = q.front().second;
        q.pop_front();
        int d = dist[i][j];
        for (int ei = -2; ei <= 2; ei++){
            for (int ej = -2; ej <= 2; ej++){
                int ni = i+ei, nj = j+ej;
                if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
                if (maze[ni][nj] == '#') continue;
                if (dist[ni][nj] <= d+1) continue;
                dist[ni][nj] = d+1;
                q.emplace_back(ni,nj);
            }
        }
        rep(k,4){
            int ni = i+di[k], nj = j+dj[k];
            if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
            if (maze[ni][nj] == '#') continue;
            if (dist[ni][nj] <= d) continue;
            dist[ni][nj] = d;
            q.emplace_front(ni,nj);
        }
    }
    int ans = dist[ti][tj];
    if (ans == INF) ans = -1;
    cout << ans << endl;
}



Measured memory_bytes: 9500
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define int long long
#define REP(i,s,e) for((i)=(s);(i)<(e);(i)++)
#define RREP(i,s,e) for((i)=((s)-1);(i)>=(e);(i)--)

signed main(){
    int a,b,c;
    cin>>a>>b>>c;

    cout<<a+b+c-max(a,max(b,c))<<endl;
}



Measured memory_bytes: 256
#include <iostream>
#include<cmath>
#include<vector>
#include<tuple>
#include <ctime>
#include<algorithm>
#include <initializer_list>
#include <cstring> 
 
#define tii tuple<int,int>
#define vi vector<int>
#define vii vector<vi>
#define ll long long
#define vl vector<ll>
#define vll vector<vl>
 
#define mymod 1000000007
#define mx 99999999
#define mn -99999999
#define sz 101
 
using namespace std;
ll n,k;
 
int main() {
    clock_t tstart = clock();
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    // Start
    cin>>n>>k;
    vl p(n);
    vl c(n);
    for(auto &x:p) cin>>x;
    for(auto &x:p) x--;
    for(auto &x:c) cin>>x;
    ll ans = *max_element(c.begin(),c.end());
    for(ll i=0;i<n;i++) {
        vl vec; vec.push_back(c[i]);
        ll j = p[i];
        ll s = c[i];
        while(j != i) {
            s += c[j];
            vec.push_back(s);
            j = p[j];
        }
        ll len =  vec.size();
        for(ll j1=0;j1<len;j1++){
            ll rest=k-j1-1;
            if(rest<0) break;
            ans=max(ans,vec[j1]+(rest/len)*max(s,0LL));
        }
    }
    cout<<ans;
    // cout<<"\n\nTotal Time Taken : "<<(double)(-tstart + clock())/CLOCKS_PER_SEC<<"sec\n\n";
    return 0;
}
 


Measured memory_bytes: 3876
#include <bits/stdc++.h>
using namespace std;
int x,n,p,maxv=1000,dist,ans;
int main() {
    cin >> x >> n;
    bool s[110] = {false};
    for( int i = 0;i<110;i++){
        cin >> p;
        s[p] = true;
    }
    for(int i = 0;i<110;i++){
        if(s[i]){
            continue;
        }
        dist = abs(x - i);
        if(maxv == dist){
            cout << ans << endl;
            return 0;
        }
        if(maxv > dist){
            maxv = dist;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}


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

int main() {
    int N, M;
    int b[16], c[16], p[16];
    scanf("%d%d", &N, &M);
    for(int i = 0; i < N; ++i){
        scanf("%d", b + i);
        c[i] = b[i];
    }
    for(int i = 0; i < M; ++i){
        scanf("%d", p + i);
    }
    int ans = 10000000;
    bool success;
    for(int n = 0; n < 2; ++n){
        success = true;
        int d = n, i = 0;
        int n_swap = 0;
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < p[j]; ++k, ++i){
                int l;
                for(l = i; l < N && b[l] != d; ++l);
                if(l < N){
                    n_swap += l - i;
                    swap(b[i], b[l]);
                }else{
                    success = false;
                    break;
                }
            }
            if(!success) break;
            d = 1 - d;
        }
        if(success){
            ans = min(n_swap, ans);
        }
        for(int i = 0; i < N; ++i){
            b[i] = c[i];
        }
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 1192
#include<bits/stdc++.h>
#define test int t; cin>>t; while(t--)
typedef long long ll;
using namespace std;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;

  cin>>n;
  if(360%n==0)
  cout<<360/n<<endl;
 else
  {
      ll m=360;
      while(true)
      {

          m+=360;
          if(m%n==0)
          {
              cout<<m/n<<endl;
              return 0;
          }
      }


  }
 return 0;
}

Measured memory_bytes: 3596
#include <iostream>
#include <vector>
#include <string>
#include <limits.h>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long int ll;

ll a, b, c, d, e;
ll N, H, K, W;
ll dp[10000100];
ll h[10000100];

void input()
{
    cin >> N;
    cin >> K;
    for (int i = 1; i <= N; i++)
    {
        int hi;
        cin >> hi;
        h[i] = hi;
    }
}
void solve()
{
    for (int i = 1; i <= N; i++)
    {
        if (i == 1)
            dp[i] = 0;
        else
        {
            ll cost = INT64_MAX;
            for (int j = 1; j <= K; j++)
            {
                if (i - j <= 0)
                    break;
                cost = min(cost, dp[i - j] + abs(h[i] - h[i - j]));
            }
            dp[i] = cost;
        }
    }
}

int main()
{
    input();
    solve();
    cout << dp[N] << endl;
}

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

int main() {
  ll int n, a, b;
  cin >> n >> a >> b;
  
  ll int set = n / (a + b);
  ll int amari = n % (a + b);
  ll int answer = set * a;
  if(amari <= a) answer += amari;
  else answer += a;
  
  cout << answer << endl;
  return 0;
}


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

using  namespace  std;

#define PI              acos(-1.0)
#define lcm(a,b)        (a * (b / __gcd(a,b)))
#define en '\n'

#define READ_IN freopen("Input.txt","r",stdin);
#define READ_OUT freopen("Output.txt","w",stdout);


void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define trace(x...)
#endif

typedef long long int           ll;
typedef unsigned long long int  llu;

int main(void)
{
    int n; cin >> n;

    string a,b;
    cin >> a >> b;

    for(int i=0;i<a.size();i++)
    {
        cout << a[i] << b[i];
    }
    cout << endl;





    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long A = sc.nextLong();
		long B = sc.nextLong();
		long C = sc.nextLong();

		if(A == B && B == C && A%2 == 0 && B%2 == 0 && C%2 == 0) {
			System.out.println("-1");
			return;
		}
		long ans = 0;
		long tmpA = A;
		long tmpB = B;
		long tmpC = C;
		while(true) {
			if(tmpA%2 != 0 || tmpB%2 != 0 || tmpC%2 != 0) {
				break;
			}
			ans++;
			long tmpAbk = tmpA;
			long tmpBbk = tmpB;
			long tmpCbk = tmpC;
			tmpA = tmpBbk/2 + tmpCbk/2;
			tmpB = tmpAbk/2 + tmpCbk/2;
			tmpC = tmpAbk/2 + tmpBbk/2;
		}
		System.out.println(ans);
	}
}

Measured memory_bytes: 21844