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

struct Edge {
    int from, to, cost, tim, com;
    Edge(int s, int t, int c, int h, int r)
        : from(s), to(t), cost(c), tim(h), com(r) {}
};

struct info {
    int nx, cost, tim;
    info(int v, int c, int h) : nx(v), cost(c), tim(h) {}
};
bool operator<(const info &a, const info &b) { return a.cost >= b.cost; }

const int INF = 1e9;
int dp[500];
int dist[500][32];
int l[500], d[500];

int main() {
    vector<int> test({-1, 0, 2, 4});
    // debug(test);
    int n, m, h, k;
    while (cin >> n >> m >> h >> k) {
        if (n == 0 && m == 0 && h == 0 && k == 0) break;
        vector<Edge> g[n];
        for (int i = 0; i < m; i++) {
            int a, b, c, hh, r;
            cin >> a >> b >> c >> hh >> r;
            g[a - 1].push_back(Edge(a - 1, b - 1, c, hh, r - 1));
            g[b - 1].push_back(Edge(b - 1, a - 1, c, hh, r - 1));
        }
        int s, t, p;
        cin >> s >> t >> p;
        s--;
        t--;
        memset(l, 0, sizeof(l));
        memset(d, 0, sizeof(d));
        fill(dp, dp + 500, INF);
        for (int i = 0; i < p; i++) {
            cin >> l[i] >> d[i];
            int setcom = 0;
            for (int j = 0; j < l[i]; j++) {
                int kk;
                cin >> kk;
                setcom |= (1 << (kk - 1));
            }
            // あるパスポートを買った時の値段
            // setcom は会社の集合
            dp[setcom] = min(dp[setcom], d[i]);
        }
        dp[0] = 0;
        for (int i = 0; i < 1 << k; i++) {
            if (dp[i] == INF) continue;
            for (int j = 0; j < 1 << k; j++) {
                dp[i | j] = min(dp[i | j], dp[i] + dp[j]);
            }
        }
        // すべての会社集合について最短経路問題を解く
        int ans = INF;
        for (int bit = 0; bit < 1 << k; bit++) {
            if (dp[bit] == INF) continue;
            // cout << bit << endl;
            priority_queue<info> que;
            que.push(info(s, 0, 0));
            int res = INF;
            for (int i = 0; i < 500; i++) {
                for (int j = 0; j < 32; j++) dist[i][j] = INF;
            }
            dist[s][0] = 0;
            while (!que.empty()) {
                info cur = que.top();
                que.pop();
                if (dist[cur.nx][cur.tim] < cur.cost) continue;
                if (cur.nx == t) {
                    res = min(res, cur.cost);
                    continue;
                }
                for (int i = 0; i < g[cur.nx].size(); i++) {
                    Edge e = g[cur.nx][i];
                    int nxh = cur.tim + e.tim;
                    if (nxh > h) continue;
                    int nxc;
                    if (bit >> e.com & 1)
                        nxc = cur.cost;
                    else
                        nxc = cur.cost + e.cost;
                    if (dist[e.to][nxh] > nxc) {
                        dist[e.to][nxh] = nxc;
                        que.push(info(e.to, nxc, nxh));
                    }
                }
            }
            ans = min(ans, res + dp[bit]);
        }
        cout << (ans == INF ? -1 : ans) << endl;
    }
    return 0;
}


Measured memory_bytes: 3200
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e5 + 10;
int a[N];
int ans[1000][1000];
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int h, w;
	int n;
	cin >> h >> w;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= h; i++)
	{
		if (i & 1)
		{
			for (int j = 1; j <= w; j++)
			{
				while (a[n] == 0) n--;
				a[n]--;
				ans[i][j] = n;
			}
		}
		else
		{
			for (int j = w; j >= 1; j--)
			{
				while (a[n] == 0) n--;
				a[n]--;
				ans[i][j] = n;
			}
		}
	}
	for (int i = 1; i <= h; i++)
	{
		for (int j = 1; j <= w; j++)
		{
			printf("%d%c", ans[i][j], " \n"[j == w]);
		}
	}
	return 0;
}

Measured memory_bytes: 2688
import heapq
n,m=map(int,input().split())
a=[-int(i) for i in input().split()]
heapq.heapify(a)
for i in range(m):
    k=heapq.heappop(a)
    heapq.heappush(a,-(abs(k)//2))
print(-sum(a))  

Measured memory_bytes: 14188
n,x=gets.split.map(&:to_i)
a=gets.split.map(&:to_i).sort

if x==a.inject(:+)
  puts n
elsif x<a[0]
  puts 0
else
  cnt,sum=0,0
  a.each do |i|
    sum+=i
    cnt+=1
    if (x < sum) ||(n==cnt)
      cnt -=1
      break
    end
  end
  puts cnt
end

Measured memory_bytes: 3836
// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

const int MAX_M = 300000;
const int MAX_H = 300000;
int h, w, m;
int cntr[MAX_H];
int cntc[MAX_H];
pii p[MAX_M];

int main() {
    fastio();

    cin >> h >> w >> m;

    forn(i, m) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        p[i] = mp(a, b);
        cntr[a]++;
        cntc[b]++;
    }

    int mxr = *max_element(cntr, cntr + h);
    int mxc = *max_element(cntc, cntc + w);

    ll cmxr = count(cntr, cntr + h, mxr);
    ll cmxc = count(cntc, cntc + w, mxc);

    ll c = 0;
    for (int i = 0; i < m; i++) {
        auto [a, b] = p[i];
        if (mxr == cntr[a] and mxc == cntc[b]) {
            c++;
        }
    }

    ll ans = mxr + mxc;
    if (c == cmxr * cmxc) {
        ans--;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 8312
n,m,x = map(int,input().split())
c_lst = [0]*n
a_lst = [0]*n 
for i in range(n):
    lst = list(map(int,input().split()))
    c_lst[i] = lst[0]
    a_lst[i] = lst[1:]

ryoukin = float("inf")

for i in range(2**n):
    temporary = 0
    rikaido = [0]*m
    for j in range(n):
        if i&(1<<j):
            temporary += c_lst[j]
            for k in range(m):
                rikaido[k] += a_lst[j][k]
    
    for j in rikaido:
        if j >= x:
            continue
        else:
            break #この段階でrikaidoがxを超えないものがあれば次へ（？）
    else:#breakでループ抜けなかった場合
        if temporary < ryoukin:
            ryoukin = temporary

if ryoukin != float("inf"):
    print(ryoukin)
else:
    print("-1")

Measured memory_bytes: 68408
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<sstream>
#include<numeric>
#include<iostream>
#include<algorithm>
#include<functional>

#define For(i,x,y) for (int i=x;i<y;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

#define dprintf(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> Vi;
typedef pair<int,int> pii;

int IN(){
	int c,f,x;
	while (!isdigit(c=getchar())&&c!='-');c=='-'?(f=1,x=0):(f=0,x=c-'0');
	while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return !f?x:-x;
}

const int N=500000;

int A[N];
int n,x;

int main(){
	n=IN()*2-1,x=IN();
	if (x==1||x==n){
		puts("No");
		return 0;
	}
	puts("Yes");
	A[(n+1)/2]=x;x=x%n+1;
	For(i,(n+1)/2+1,n+1) A[i]=x,x=x%n+1;
	For(i,1,(n+1)/2) A[i]=x,x=x%n+1;
	For(i,1,n+1) printf("%d\n",A[i]);
}

Measured memory_bytes: 2304
#include <stdio.h>

int main(void)
{
	int x, y, i, j ;
	int con=0;


	for(;;){
		int con=0;
		scanf("%d %d", &x, &y);
		if (x == 0 && y == 0) {
			break;int con=0;
		}

		for (i = 0; i < x; i++) {
			
			for (j = 0; j < y; j++) {
				if (con % 2 == 0) {
					printf("#");
				}
							
				else {
					printf(".");
				}
				con++;
			}
			if (i % 2 == 0) {
				con = 1;
			}
			else
				con = 0;
				printf("\n");
		}
		printf("\n");
	}
	return 0;
}


Measured memory_bytes: 2048
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<utility>
#include<queue>
#include<stdio.h>
//#include<sys/time.h>
#include<random>
#include<fstream>
#define INF 2000000000
using namespace std;
typedef std::pair<int,int> P;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))
#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))
int n,ans;
signed main(){
	string s;
	cin>>s;
	if(s[s.size()-1]=='s')cout<<s<<"es"<<endl;
	else cout<<s<<"s"<<endl;
}

Measured memory_bytes: 3592
N_before, N_after = map(int, input().split())
S = input()
judge = 'Yes'

if (S[: N_before].count('-') >= 1):
    judge = 'No'

if (S[N_before] != '-'):
    judge = 'No'

if (S[N_before + 1 :].count('-') >= 1):
    judge = 'No'

print(judge)

Measured memory_bytes: 3060
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

typedef pair<int, int> ip;

int n, x;
map<int, int> d[2];

int main() {
	scanf("%d", &n);
	int r[2] = { 0 };
	for (int i = 0; i < n; i++) {
		scanf("%d", &x);
		int w = d[i % 2][x]++;
	}
	vector<ip> v0, v1;
	for (map<int, int>::iterator it = d[0].begin(); it != d[0].end(); it++)
		v0.push_back(ip(it->second, it->first));
	for (map<int, int>::iterator it = d[1].begin(); it != d[1].end(); it++)
		v1.push_back(ip(it->second, it->first));
	v0.push_back(ip(0, -1));
	v1.push_back(ip(0, -2));
	sort(v0.rbegin(), v0.rend());
	sort(v1.rbegin(), v1.rend());
	int w;
	if (v0[0].second != v1[0].second) w = v0[0].first + v1[0].first;
	else w = max(v0[0].first + v1[1].first, v0[1].first + v1[0].first);
	printf("%d\n", n - w);
	return 0;
}

Measured memory_bytes: 6136
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
string s;
int read()
{
	char chr;
	int f=1;
	while (((chr=getchar())<'0')||(chr>'9'))
	{
		if (chr=='-')
		{
			f=-1;
		}
	}
	int res=chr-'0';
	while (((chr=getchar())>='0')&&(chr<='9'))
	{
		res=res*10+chr-'0';
	}
	return res*f;
}
int main()
{
	cin>>s;
	s.erase(s.length()-8,s.length()-1);
	cout<<s<<"\n";
	return 0;
}

Measured memory_bytes: 256
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
f = int(input())
g = int(input())
h = int(input())
i = int(input())
j = int(input())

print(a+b+c+d+e+f+g+h+i+j)


Measured memory_bytes: 5588
import java.io.*;
import java.util.*;
import java.util.Map.Entry;


public class  Main{
	public static void main(String[] args) throws Exception{
		MScanner sc=new MScanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);
		boolean ok=true;
		int n=sc.nextInt();
		while(n-->0) {
			int x=sc.nextInt();
			if(x%2==0) {
				if(x%3==0 || x%5==0) {
					
				}
				else {
					ok=false;
				}
			}
		}
		pw.println(ok?"APPROVED":"DENIED");
		pw.flush();
	}
	static class MScanner {
		StringTokenizer st;
		BufferedReader br;
		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
 
		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int[] takearr(int n) throws IOException {
	        int[]in=new int[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public long[] takearrl(int n) throws IOException {
	        long[]in=new long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public Integer[] takearrobj(int n) throws IOException {
	        Integer[]in=new Integer[n];for(int i=0;i<n;i++)in[i]=nextInt();
	        return in;
		}
		public Long[] takearrlobj(int n) throws IOException {
	        Long[]in=new Long[n];for(int i=0;i<n;i++)in[i]=nextLong();
	        return in;
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public char nextChar() throws IOException {
			return next().charAt(0);
		}
 
		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}

Measured memory_bytes: 21584
#include <bits/stdc++.h>
#define maxn 100001
#define ll long long
using namespace std;
ll n,a[maxn],ans;
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	for (int i=1;i<=n;i++)
	cin>>a[i];
	for (int i=1;i<=n;i++)
	ans+=abs(a[i]-a[i-1]);
	ans+=abs(a[n]-0);
	for (int i=1;i<n;i++){
		ll temp=ans;
		temp-=(abs(a[i]-a[i-1])+abs(a[i+1]-a[i]));
		cout<<temp+abs(a[i+1]-a[i-1])<<endl;
	}
	cout<<ans-abs(a[n]-a[n-1])-abs(a[n]-a[0])+abs(a[n-1]-a[0]);
} 

Measured memory_bytes: 2048
#include<bits/stdc++.h>
using namespace std;
int main(){
  long long n;
  cin >> n;
  long long ans=1e18;
  for(long long i=1;i*i<=n;i++){
    if(n%i==0){
      ans=min(ans,i-1+n/i-1);
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
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);
}
/*-------------------------------------------*/

int n;
int a[200009];

bool b[200009];

bool f(int mid) {
    rep(i, 2 * n - 1) b[i] = (a[i] >= mid);
    int l = n - 1, r = n - 1;
    while(l - 1 >= 0 && b[l - 1] != b[l])
        l--;
    while(r + 1 < 2 * n - 1 && b[r] != b[r + 1])
        r++;
    if(n - 1 - l > r - n + 1)
        return b[r];
    else
        return b[l];
}

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

    cin >> n;
    rep(i, 2 * n - 1) cin >> a[i];

    int l = 1, r = 2 * n - 1;
    while(l + 1 < r) {
        int mid = (l + r) >> 1;
        (f(mid) ? l : r) = mid;
    }

    cout << l << endl;

    return 0;
}

Measured memory_bytes: 4632
#include <bits/stdc++.h>
#define MAXN 1050
#define MAXM
#define MOD 1000000007
#define INF (1<<28)
#define LLINF (1LL<<50)
#define ri register int
#define il inline
#define fir first
#define sec second
#define pb push_back
#define mp make_pair
#define all(p) for(vector <pii> :: iterator it = (p).begin(); it != (p).end(); ++it)
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair <int, int> pii;
typedef pair <int, LL> pil;
typedef pair <LL, int> pli;
typedef pair <LL, LL> pll;
int n, m, fa[MAXN], f[MAXN][15], g[MAXN][15], dep[MAXN], vis[MAXN*2];
int cnt[3];
LL x, sum, ans;
vector <pii> adj[MAXN];
struct edge{
	int u, v, w;
	bool operator < (const edge &x) const {
		return w < x.w;
	}
}E[MAXN*2];
int get(int u) {
	return u == fa[u] ? u : fa[u] = get(fa[u]);
} 
int calc(int u, int v) {
	int ret = 0;
	if(dep[u] < dep[v]) swap(u, v);
	for(ri i = 12; ~i; --i) if((dep[u]-dep[v]) & (1<<i)) ret = max(ret, g[u][i]), u = f[u][i];
	if(u == v) return ret;
	for(ri i = 12; ~i; --i) 
		if(f[u][i] != f[v][i]) ret = max(ret, max(g[u][i], g[v][i])), u = f[u][i], v = f[v][i];
	return max(ret, max(g[u][0], g[v][0]));
}
void dfs(int u) {
	for(ri i = 1; i <= 12; ++i) 
		f[u][i] = f[f[u][i-1]][i-1], g[u][i] = max(g[u][i-1], g[f[u][i-1]][i-1]);
	all(adj[u]) {
		if(it->sec != f[u][0]) f[it->sec][0] = u, g[it->sec][0] = it->fir, dep[it->sec] = dep[u]+1, dfs(it->sec);
	}
}
LL Pow(LL x, LL y) {
	LL ret = 1;
	while(y) {
		if(y & 1) ret = ret*x%MOD;
		x = x*x%MOD;
		y >>= 1;
	}
	return ret;
}
int main() {
	int u, v, w, fu, fv;
	scanf("%d%d%lld", &n, &m, &x);
	for(ri i = 1; i <= n; ++i) fa[i] = i;
	for(ri i = 1; i <= m; ++i) scanf("%d%d%d", &u, &v, &w), E[i] = edge{u, v, w};
	sort(E+1, E+m+1);
	for(ri i = 1; i <= m; ++i) {
		u = E[i].u, v = E[i].v, w = E[i].w;
		if((fu = get(u)) != (fv = get(v))) vis[i] = 1, fa[fu] = fv, adj[u].pb(mp(w, v)), adj[v].pb(mp(w, u)), sum += w;
	}
	dfs(1);
	if(x < sum) return printf("0"), 0;
	LL diff = x-sum, cur;
	for(ri i = 1; i <= m; ++i) {
		if(vis[i]) continue;
		u = E[i].u, v = E[i].v, w = E[i].w;
		cur = w-calc(u, v); 
		if(cur < diff) ++cnt[0];
		else if(cur == diff) ++cnt[1];
		else ++cnt[2];
	}
	if(diff == 0) {
//		while(1); 
		//MST already have two colors
		ans = (Pow(2, n-1)-2)*(Pow(2, m-n+1))%MOD;
		//MST have same color
		ans += 2*(Pow(2, cnt[1])-1)%MOD*Pow(2, cnt[2])%MOD;
		ans %= MOD;
	}
	else {
		//MST and all smaller should have same col, one equal should have different col, others whatever
		ans = 2*(Pow(2, cnt[1])-1)%MOD*Pow(2, cnt[2])%MOD;
	}
	printf("%lld", ans);
	return 0;
}

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

using namespace std;

const int MOD=1e9+7;
//const int MOD=998244353;

const double PI=3.14159265359;


int main(){
    
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin>>s;
    
    int  n=s.size();
    long long ans=0;
    
    vector<int> cs(n);
    cs[0]=s[0]=='W';
    
    for(int i=1; i<n; i++){
        cs[i]=cs[i-1]+(s[i]=='W');
    }
    for(int i=0; i<n; i++){
        if(s[i]=='B'){
            ans+=cs[n-1]-(i?cs[i-1]:0);
        }
    }
    cout<<ans;
    
    
    return 0;
}

Measured memory_bytes: 1300
#include<bits/stdc++.h>
using namespace std;
const int N=2003;
int n,m,ans,f1[N][N],f2[N][N],f3[N][N],f4[N][N];
char mp[N][N];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%s",mp[i]+1);
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    if(mp[i][j]=='.')f1[i][j]=f1[i-1][j]+1,f2[i][j]=f2[i][j-1]+1;
    for(int i=n;i;i--)
    for(int j=m;j;j--)
    if(mp[i][j]=='.')f3[i][j]=f3[i+1][j]+1,f4[i][j]=f4[i][j+1]+1;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    if(mp[i][j]=='.')ans=max(ans,f1[i][j]+f2[i][j]+f3[i][j]+f4[i][j]-3);
    printf("%d\n",ans);
}

Measured memory_bytes: 70280
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

vector<ll> P;
vector<ll> C;

set<ll> roop(ll x,ll &point) {
set<ll> S;
  S.insert(x);
  ll k=x;
  x=P[x-1];
  point+=C[x-1];
 
  while(x!=k) {
S.insert(x);
    x=P[x-1];
    point+=C[x-1];
  }
  return S;
}
 


int main() {
  ll N,K;
  cin>>N>>K;
 P=vector<ll>(N);
  for(ll i=0;i<N;i++) {
cin>>P[i];
  }
  C=vector<ll>(N);
  for(ll i=0;i<N;i++) {
    cin>>C[i];
  }

  
  
  
  
  
  vector<set<ll>> A(0);
  vector<vector<ll>> B(2,vector<ll>(0));
  
  vector<ll> ans(0);
 for(ll i=1;i<=N;i++) {
   ll index=-1;
 for(ll j=0;j<A.size();j++) {
   if(A[j].count(i)) {
index=j;
   }
 }
   if(index<0) {
     ll point=0;
   A.push_back(roop(i,point));
     B[0].push_back(A.back().size());
     B[1].push_back(point);
   index=A.size()-1;
   }
   
   
   
   
   ll place=P[i-1];
   ll point=C[place-1];
   ll m=point;
   if(K<B[0][index]) {
for(ll j=0;j<K-1;j++) {
  place=P[place-1];
  point+=C[place-1];
  m=max(m,point);
}
     
     ans.push_back(m);
   }
   
   else if(B[1][index]<=0) {
     for(ll j=0;j<B[0][index]-1;j++) {
         place=P[place-1];
  point+=C[place-1];
  m=max(m,point);
}
     ans.push_back(m);
   }
   
   else {
ll maxroop=K/(B[0][index])*B[1][index];
   place=i;
     point=0;
     m=0;
     for(ll j=0;j<K%B[0][index];j++) {
       place=P[place-1];
       point+=C[place-1];
       m=max(point,m);
     }
     maxroop+=m;
     
ll minroop=((K/(B[0][index]))-1)*B[1][index];
    
     if((K/(B[0][index]))-1==0) {
place=P[i-1];
point=C[place-1];
m=point;
for(ll j=0;j<B[0][index]-2;j++) {
   place=P[place-1];
       point+=C[place-1];
       m=max(point,m);
     }
     minroop+=m;
     }
     
     else {
        place=i;
     point=0;
     m=0;
     for(ll j=0;j<B[0][index];j++) {
       place=P[place-1];
       point+=C[place-1];
       m=max(point,m);
     }
     minroop+=m;
     }
     
     ans.push_back(max(maxroop,minroop));
   }
   
 }
  sort(all(ans));
  cout<<ans.back()<<endl;
}

 

       
     

     



  
  
 
   
     
     



  
  
  
  
  
  
  
  
  

Measured memory_bytes: 4064
/*! at-abc163c.cc
 *  2020-04-30
 *  GCC+17
 *  Type (opt):
 *  Notes (opt):
 */
#include <cstdio>
using llu = long long unsigned;
using ll = long long ;

int main(void)
{
    int co[1000000];

    llu n, i, a;
    scanf("%llu", &n);
    for (i=1; i<n; i++) {
//        printf("%d", i);
        scanf("%llu", &a);
        co[a]++;
    }
    for (i=1; i<n+1; i++)
        printf("%i\n",co[i]);

    return 0;
}


Measured memory_bytes: 2516
l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',  'y', 'z']
print(l[l.index(input()) + 1])


Measured memory_bytes: 2940
<?php
$n = int();
for($i = 0; $i < $n; $i++){
  list($a[], $b[]) = ints();
}
$sum = 0;
for($i = 0; $i < $n; $i++){
  for($j = 0; $j < $n; $j++){
    if($i != $j){
      $sum += $l[$i][$j] = sqrt(($a[$i]-$a[$j])**2 + ($b[$i]-$b[$j])**2);
    }
  }
}
echo $sum/$n;

function str(){
  return trim(fgets(STDIN));
}
function ints(){
  return array_map("intval", explode(" ", trim(fgets(STDIN))));
}
function int(){
  return intval(trim(fgets(STDIN)));
}
function o(...$val){
    if(count($val)==1)$val = array_shift($val);
    $trace = debug_backtrace();
    echo $trace[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0){
            echo "empty array";
        }elseif(!is_array(current($val))){
            echo "array:";
            echo implode(" ", $val)."\n";
        }else{
            echo "array:array\n";
            foreach($val as $row){
                echo implode(" ", $row)."\n";
            }
        }
    }else{
        echo $val."\n";
    }
}

Measured memory_bytes: 4728
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1000000007; 
const int INF = 1000000000; //1e9
const int NIL = -1;
const long long LINF = 1000000000000000000; // 1e18
const double EPS = 1E-10;

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




int main(){
    int N, A; std::cin >> N;
    std::vector<int> cnt(N, 0);
    for(int i(0), i_len(N-1); i < i_len; ++i){
        std::cin >> A;
        ++cnt[A-1];
    }
    for(int i(0); i < N; ++i){
        std::cout << cnt[i] << "\n";
    }
    return 0;
}


Measured memory_bytes: 4048
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>

using namespace std;

#define INF LLONG_MAX / 5
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define enld endl

signed main() {
	int N;
	cin >> N;
	vector<int>A(N);
	vector<int>B(N);
	vector<int>C(N);
	rep(i, N) {
		cin >> A[i] >> B[i] >> C[i];
	}
	vector<vector<int>>dp(N, vector<int>(3));
	rep(i, N) {
		if (i == 0) {
			dp[i][0] = A[i];
			dp[i][1] = B[i];
			dp[i][2] = C[i];
		}
		else {
			dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + A[i];
			dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + B[i];
			dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + C[i];
		}
	}
	cout << max(max(dp[N - 1][0], dp[N - 1][1]), dp[N - 1][2]) << enld;
}

Measured memory_bytes: 11092
// ABC101C - Minimization

#include <bits/stdc++.h>
using namespace std;

int main() {
	int N, K;
	cin >> N >> K;
//	for (int i = 0; i < N; i++) {
//		int t;
//		cin >> t;
//	}

	int ans;
//	ans = ((N - K) + (K - 2))/ (K - 1) + 1;
	ans = (N - 2)/(K - 1) + 1;

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#[allow(unused_imports)]
use itertools::Itertools;
#[allow(unused_imports)]
use num::*;
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::collections::*;

fn solve() {
    input! {
        n: usize, m: usize,
        psv: [(Usize1, String); m]
    };

    let mut ac_count = 0;
    let mut wa_count = 0;
    let mut solved = vec![false; n];
    let mut wa_counts = vec![0; n];
    for (p, s) in psv {
        let s = &s[..];
        match s {
            "AC" => {
                if !solved[p] {
                    ac_count += 1;
                    wa_count += wa_counts[p];
                    solved[p] = true;
                }
            }
            "WA" => {
                if !solved[p] {
                    wa_counts[p] += 1;
                }
            }
            _ => unreachable!(),
        }
    }
    println!("{} {}", ac_count, wa_count);
}

fn main() {
    std::thread::Builder::new()
        .name("big stack size".into())
        .stack_size(256 * 1024 * 1024)
        .spawn(|| {
            solve();
        })
        .unwrap()
        .join()
        .unwrap();
}


Measured memory_bytes: 8464
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define INF 1e+18

int main() {
  int v, e; cin >> v >> e;
  vector<vector<ll>> dist(v, vector<ll>(v, INF));
  rep(i, v) {
    dist[i][i] = 0;
  }
  rep(i, e) {
    int s, t, d; cin >> s >> t >> d;
    dist[s][t] = d;
  }
  

  rep(k, v) {
    rep(i, v) {
      rep(j, v) {
        if(dist[i][k] != INF && dist[k][j] != INF) {
          dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        }
      }
    }
  }
  rep(i, v) {
    if(dist[i][i] < 0) {
      cout << "NEGATIVE CYCLE" << endl;
      return 0;
    }
  }
  rep(i, v) {
    rep(j, v) {
      if(dist[i][j] == INF) {cout << "INF";}
      else { cout << dist[i][j];}
      if(j == v-1) {cout << endl;}
      else {cout << " ";}
    }
  }
  return 0;
}


Measured memory_bytes: 3252
N,L=gets.chomp.split(" ").map(&:to_i)
####味はL,L+1,L+2,...,L+(N-1)
####味の合計値を求める
sum=(2*L+N-1)*N/2
####味が0のリンゴがあるか？
if L*(L+N-1)<=0
  print(sum)
elsif L<0  ###味がすべて負なら、L+N-1を食べる
  print(sum-(L+N-1))
else        ###味がすべて正なら、Lを食べる
  print(sum-L)
end
  


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

const string M[] = {"",
                    ".,!? ",
                    "abc",
                    "def",
                    "ghi",
                    "jkl",
                    "mno",
                    "pqrs",
                    "tuv",
                    "wxyz"};
  
int main() {
  int T;
  cin >> T;
  cin.ignore();
  while(T--) {
    string s;
    getline(cin, s);
    for(int i = 0, j = -1, k = -1; i < s.size(); ++i) {
      int d = s[i] - '0';
      if(d == 0) {
        if(j != -1) {
          cout << M[j][k];
          j = k = -1;
        }
      } else {
        j = d;
        k = (k + 1) % M[j].size();
      }
    }
    cout << endl;
  }
  return 0;
}

Measured memory_bytes: 1216
#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int mx=0,mn=INT_MAX,n,a;
    cin>>n;
    while(n--){
        cin>>a;
        mx=max(mx,a);
        mn=min(mn,a);
    }
    cout<<mx-mn<<endl;


    return 0;
}


Measured memory_bytes: 3564
#include<cstdio>
#include<cstring>

const int MAXN = 100 + 10;
int a[MAXN][MAXN];

int main()
{
    int n, h, w;
    int ans = 0;
    scanf("%d %d %d", &n, &h, &w);
    if(n<h || n<w)
        printf("%d", ans);
    else 
    {
        ans = (n + 1 - h) * (n + 1 - w);
        printf("%d", ans);
    }
    return 0;
}

Measured memory_bytes: 1664
#include <stdio.h>
#include <math.h>

int main(void)
{
	double v, y;
	double n;
	
	while (scanf("%lf", &v) != EOF){
		y = (v * v) / 19.6;
		n = (y + 5) / 5;
		printf("%d\n", (int)ceil(n));
	}
	return (0);
}

Measured memory_bytes: 632
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<pair<int, int> > G[10010];
vector<int> ans, c;

int ind = 0;

void dfs(int from, int to){
	ans[to] = c[ind++];

	for(int i = 0; i < G[to].size(); ++i){
		if(G[to][i].second != from){
			dfs(G[to][i].first, G[to][i].second);
		}
	}
}

int main(){
	int N; cin >> N;
	ans.resize(N); c.resize(N);
	vector<pair<int, int> > siz(N);

	for(int i = 0; i < N-1; ++i){
		int from, to;
		cin >> from >> to;
		from--; to--;
		G[from].push_back(make_pair(from, to));
		G[to].push_back(make_pair(to, from));
	}
	for(int i = 0; i < N; ++i){ cin >> c[i]; }

	sort(c.begin(), c.end(), greater<int>());

	ans[0] = c[0];
	ind++;
	for(int i = 0; i < G[0].size(); ++i){
		dfs(G[0][i].first, G[0][i].second);
	}

	int sum = 0;
	for(int i = 1; i < N; ++i){
		if(ans[i] == 0) ans[i] = c[N-1];
		sum += c[i];
	}

	cout << sum << endl;
	cout << ans[0];
	for(int i = 1; i < N; ++i){
		cout << " " << ans[i];
	}
	cout << endl;

	return 0;
}


Measured memory_bytes: 1408
#include<bits/stdc++.h>
 
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ldb;
 
#define pb push_back
#define popb pop_back
#define popf pop_front
#define pf push_front
#define si size()
#define mp make_pair 
#define mt make_tuple
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound

#define ask(i,m,n) for(lli i=m;i<n;i++)
#define anket ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
 
#define endl "\n"
#define o0(a) cout<<a<<" "
#define o1(a) cout<<a<<"\n"
#define o2(a,b) cout<<a<<" "<<b<<"\n"
#define o3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n"
#define o4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"
using namespace std;

#define prime1 1000000007
#define prime2 998244353

lli modadd(lli n,lli m , lli mod){
    lli sum = ((n+m)%mod+mod)%mod;
    return sum;
}

lli modsub(lli n,lli m,lli mod){
    lli diff = ((n-m+mod)%mod+mod)%mod;
    return diff;
}

lli modpro(lli n,lli m, lli mod){
    lli pro = ((n*m)%mod+mod)%mod;
    return pro;
}

lli powmod(lli x,lli y,lli mod){
    x %= mod;
    y %= mod;
    lli res = 1;
    while (y > 0) {
        if (y & 1)
            res = modpro(res , x,mod);
        y = y >> 1;
        x = modpro(x , x,mod);
    }
    return res;
}

lli moddiv(lli n,lli m,lli mod){
    //return n/m mod (mod) or n*(m)^-1 mod (mod)
    lli a = powmod(m,mod-2,mod);
    //this is because of fermats thm i.e. a^-1 = a^p-2 mod(p) check
    //multiplying p on both sides
    lli res = modpro(a,n,mod);
    return res;
}




int main() {
	anket;
	string s;
	cin>>s;
	lli n = s.size();
	lli flag=0;
	ask(i,0,(n-1)/2){
	    if(s[i]!= s[n-i-1]){
	        flag=1;
	        break;
	    }
	}
	lli i=0,j=(n-1)/2-1;
	while(i<j){
	    if(s[i]!= s[j]){
	        flag=1;
	        break;
	        
	    }
	    else{
	        i++;
	        j--;
	    }
	}
	if(flag){
	    o1("No");
	}
	else{
	    o1("Yes");
	}
	return 0;
}




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

int main() {
    cin.tie(0)->sync_with_stdio(false);

	int h, w;
	cin >> h >> w;
	int n;
	cin >> n;
	queue<int> a;
	REP(i, n) {
		int tmp;
		cin >> tmp;
		REP(j, tmp)
			a.push(i + 1);
	}

	vector<vector<int> > res(h, vector<int>(w));
	REP(i, h) REP(j, w) {
		res[i][j] = a.front();
		a.pop();
	}

	REP(i, h) {
		if (i % 2)
			reverse(res[i].begin(), res[i].end());
		REP(j, w) {
			cout << res[i][j];
			if (j != w - 1) cout << ' ';
		}
		cout << '\n';
	}
    return 0;
}

Measured memory_bytes: 3684
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    while(true){
      int sum=0;
      String S = sc.next();
      if(S.equals("0")){
        break;
      }else{
        char[] c = S.toCharArray();
        for(int i=0;i<c.length;i++){
          sum += (int)(c[i]-'0');
        }
          System.out.println(sum);
      }
    
    }
  }
}


Measured memory_bytes: 26488