akhauriyash/Code-Regression huggingface dataset:
import scala.math.{pow, sqrt}

object Main extends App {
  
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val c = Array.fill(n)(sc.nextLong(), sc.nextLong())
  val total = c.permutations.map(path => {
    path.sliding(2).foldLeft(0d)((sum, route) => sum + dist(route))
  }).toList
  println(total.sum / total.length)
  
  def dist(x: Array[(Long, Long)]): Double = {
    sqrt(pow((x(0)._1 - x(1)._1), 2) + pow((x(0)._2 - x(1)._2), 2))
  }
}


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

int main()
{
    int wmax,n,m;
    int val,w;
    int t[1001];
    int i,j,c;
    c = 0;
    while(1)
    {
	cin >> wmax;
	if (wmax == 0)break;
	for (i=0; i<1001; i++)
	{
	    t[i]=0;
	}
	c++;
	m = 0;
	cin >> n;
	for (i=0; i<n; i++)
	{
	    scanf("%d,%d",&val,&w);
	    for (j=wmax; j>w; j--)
	    {
		if (t[j-w] != 0)
		{
		    t[j] = max(t[j],val+t[j-w]);
		    m = max(m,t[j]);
		}
	    }
	    t[w] = max(t[w],val);
	    if (w<=wmax)
		m = max(m,val);
	}
	for (j=0; j<=wmax; j++)
	{
	    if (m==t[j])
		break;
	}
	cout << "Case " << c << ":" << endl;
	cout << m << endl;
	cout << j << endl;
    }
    return 0;
}

Measured memory_bytes: 0
import java.util.Scanner;

public class Main {

    private static final int MOD = 1000000007;
    private static long[] fac = new long[1000000];
    private static long[] finv = new long[1000000];
    private static long[] inv = new long[1000000];

    public static void nCkInit() {
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for (int i = 2; i < 1000000; i++) {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

    public static long nCk(int n, int k) {
        if (n < k) {
            return 0;
        }
        if (n < 0 || k < 0) {
            return 0;
        }
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        nCkInit();
        for (int i = 1; i <= k; i++) {
            long ans = nCk(n - k + 1, i);
            ans %= MOD;
            ans *= nCk(k - 1, i - 1);
            ans %= MOD;
            System.out.println(ans);
        }
    }

}


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

typedef vector<int> vi;
int main() {
	int N, M;
	cin >> N;
	vi vec1(N);
	rep(i, N)
		cin >> vec1.at(i);
	cin >> M;
	vi vec2(M);
	rep(i, M)
		cin >> vec2.at(i);
	vi vec3;
	set_intersection(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), inserter(vec3, vec3.end()));
	rep(i, vec3.size())
		cout << vec3.at(i) << endl;
}



Measured memory_bytes: 4720
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#include<ctime>

using namespace std;

template <typename T>

inline T read()
{
	T x=0,f=1;char c=getchar();
	while(!isdigit(c))
	{if(c=='-') f=-f;c=getchar();}
	while(isdigit(c))
	{x=(x<<3)+(x<<1)+(c-48);c=getchar();}
	return x*f;
}

#define lint long long int
#define ulint unsigned lint

const int inf=1e9+1e7;
const int MAXN=3e5+1e1;

int a[MAXN];
bool vis[MAXN];

int main(void)
{

	// freopen("testdata.in","r",stdin);
	// freopen("testdata.out","w",stdout);

	int n,m,ans=0;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++) a[i]=read<int>();

	for(int i=1;i<=m;i++)
	{
		int u=read<int>();
		int v=read<int>();
		if(a[u]<a[v]) vis[u]=true;
		if(a[u]>a[v]) vis[v]=true;
		if(a[u]==a[v]) vis[u]=vis[v]=true;
	}

	for(int i=1;i<=n;i++) if(!vis[i]) ++ans;

	printf("%d\n",ans);

	return 0;

}

Measured memory_bytes: 4352
#include <iostream>
#include <algorithm>

using namespace std;

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

    int a[200001] = {};
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        ++a[x];
    }

    int ans = 0;
    sort(a, a + n + 1);
    for (int i = 0; i <= n - k; ++i) {
        ans += a[i];
    }
    cout << ans << endl;
}

Measured memory_bytes: 1152
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def _get(T, a, b, k, l, r):
    if r <= a or b <= l:
        return set()

    if a <= l and r <= b:
        return T[k]
    else:
        s1 = _get(T, a, b, 2 * k + 1, l, (l + r) // 2)
        s2 = _get(T, a, b, 2 * k + 2, (l + r) // 2, r)
        return s1.union(s2)


def get(T, d, a, b):
    return _get(T, a, b, 0, 0, 2 ** d)


def main():
    N = int(input())
    S = input().strip()
    Q = [input().strip().split() for _ in range(int(input()))]
    d = N.bit_length()
    T = [set() for _ in range(2 ** (d + 1) - 1)]
    for i, s in enumerate(S):
        T[i + 2 ** d - 1].add(s)
    for i in range(2 ** d - 2, -1, -1):
        T[i] =T[2 * i + 1] .union(T[2 * i + 2])

    for s, a, b in Q:
        if s == "1":
            a = int(a) - 1
            i = a + 2 ** d - 1
            if b in T[i]:
                continue
            T[i] = {b}
            while i > 0:
                i = (i - 1) // 2
                T[i] = T[2 * i + 1] .union(T[2 * i + 2])
        elif s == "2":
            a = int(a) - 1
            b = int(b) - 1
            S = get(T, d, a, b + 1)
            print(len(S))


if __name__ == "__main__":
    main()


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


#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1


#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;


int main() {
    string s;
    cin >> s;
    rep(i, sz(s) - 1) {
        if (s.substr(i, 2) == "AC") {
            puts("Yes");
            return 0;
        }
    }
    puts("No");


    return 0;
}

Measured memory_bytes: 256
#include<iostream>
int n=1<<18,p[1<<18],i,j;int main(){p[1]=1;for(;++i*i<n;)if(!p[i]){for(j=i*2;j<n;j+=i)p[j]=1;}for(;std::cin>>n,i=n;){for(j=0;++i<=2*n;)if(!p[i])j++;std::cout<<j<<std::endl;}return 0;}

Measured memory_bytes: 0
import java.io.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
   
class Main {
    public static void main(String[] a) throws IOException {
    	int[][] results = null;
    	double x1;
    	double y1;
    	double x2;
    	double y2;
    	double x3;
    	double y3;
    	double p;
    	double q;
    	double hankei;
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        for(int i = 0; i < num;i++){
        	x1 = scan.nextDouble();
        	y1 = scan.nextDouble();
        	x2 = scan.nextDouble();
        	y2 = scan.nextDouble();
        	x3 = scan.nextDouble();
        	y3 = scan.nextDouble();
        	p =((2*(y2-y1))*(x1*x1-x3*x3+y1*y1-y3*y3)-(2*(y3-y1))*(x1*x1-x2*x2+y1*y1-y2*y2))/((2*(x2-x1))*(2*(y3-y1))-(2*(x3-x1))*(2*(y2-y1)));
        	q =((x1*x1-x2*x2+y1*y1-y2*y2)*(2*(x3-x1))-(x1*x1-x3*x3+y1*y1-y3*y3)*(2*(x2-x1)))/((2*(x2-x1))*(2*(y3-y1))-(2*(x3-x1))*(2*(y2-y1)));
        	hankei = Math.sqrt((x1-p)*(x1-p)+(y1-q)*(y1-q));
        	System.out.printf("%.3f %.3f %.3f\n", p ,q , hankei);
        }
        
       
    }
}

Measured memory_bytes: 28840
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i,n) for(int i=0;i<n;i++)
#define REPP(i,n) for(int i=1;i<=n;i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
    long long c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
long long po(long a, long b){
    if(b==0){
        return 1;
    }
    
    long long z = po(a,b/2);
    z = mod(z*z);
    if(b%2!=0){
        z = mod(a*z);
    }
    return z;
}
 
bool prime_(int n){
  if(n==1){
    return false;
  }else if(n==2){
    return true;
  }else{
    for(int i=2;i<=std::sqrt(n);i++){
      if(n%i==0){
        return false;
      }
    }
    return true;
  }
}
 
long long gcd_(long long a, long long b){
  if(a<b){
    std::swap(a,b);
  }
  if(a%b==0){
    return b;
  }else{
    return gcd_(b,a%b);
  }
}
 
long long lcm_(long long x, long long y){
  return (x/gcd_(x,y))*y;
}
 
//using namespace std;
//using namespace boost::multiprecision;
//using namespace __gnu_pbds;

int main(){
  using namespace std;
  int n;
  cin>>n;
  int a[n];
  REP(i, n) cin>>a[i];
  int ans[n];
  REP(i, n){
    if(i == 0){
      ans[i] = 0;
      REP(j, n){
        if(j == 0) continue;
        ans[i] ^= a[j];
      }
      continue;
    }
    int temp = a[i] ^ a[i-1];
    ans[i] = temp ^ ans[i-1];
  }
  REP(i, n){
    cout << ans[i] << ' ';
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 5236
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<double, double> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;


class UnionFind {
public:
    // 親の番号を格納する。親だった場合-size
    vector<int> parents;

    UnionFind(int n) {
        parents = vector<int>(n, -1);
    }

    // aがどのグループに属しているか
    int root(int a) {
        if (parents[a] < 0) {
            return a;
        }
        return parents[a] = root(parents[a]);
    }

    int size(int a) {
        return -parents[root(a)];
    }

    // aとbをくっつける
    bool connect(int a, int b) {
        int ra = root(a);
        int rb = root(b);
        if (ra == rb) {
            return false;
        }
        // 大きいほうにA
        if (size(ra) < size(rb)) {
            swap(ra, rb);
        }
        parents[ra] += parents[rb];
        parents[rb] = ra;
        return true;
    }

    bool is_union(int a, int b) {
        int ra = root(a);
        int rb = root(b);
        return ra == rb;
    }

    set<int> get_parents() {
        int n = parents.size();
        set<int> r;
        rep(i, n) r.insert(root(i));
        return r;
    }

};

int main() {

    int ma = 10e4 + 1;

    int n;
    cin >> n;
    UnionFind uf(2 * ma);

    vector<P> points(n);
    rep(i, n) cin >> points[i].first >> points[i].second;
    for (P p : points) uf.connect(p.first, p.second + ma);

    map<int, ll> x, y;
    rep(i, 2 * ma) if (i < ma) x[uf.root(i)]++; else y[uf.root(i)]++;

    auto s = uf.get_parents();
    ll res = 0;
    for (int i : s) res += x[i] * y[i];

    cout << res - n << endl;
}

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

#define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* short */
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define ALL(v) begin(v), end(v)

/* REPmacro */
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (s); i >= (n); i--)
#define rep(i, n) FOR(i, 0, n)
#define repi(i, n) FOR(i, 1, n + 1)
#define rrep(i, n) RFOR(i, n - 1, 0)
#define rrepi(i, n) RFOR(i, n, 1)

/* debug */
#define pp(v) cerr << #v "=" << (v) << endl;

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using vs = vector<string>;
using Graph = vvi;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* iostream */
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
#define UNIQUE(v) v.erase( unique(ALL(v)), v.end() );
#define TIME system("date +%M:%S.%N")
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template<class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; }
template<class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }
template<class T> T div_ceil(const T &a, const T &b) { return (a + b - 1) / b; }

signed main() {
  int x,y;cin>>x>>y;
  int a = x;
  int ans = 0;
  while (a <= y) {
    ans++;
    a *= 2;
  }
  cout << ans << endl;
}


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

int main(){
  int n;
  cin>>n;
  int a[n];
  rep(i,0,n) cin>>a[i];
  int b=a[0];
  rep(i,1,n) b=gcd(b,a[i]);
  if(b!=1) cout<<"not coprime"<<endl;
  else{
    set<int> s;
    bool p=true;
    rep(i,0,n){
      int c=a[i];
      for(int j=2;j*j<=a[i];j++){
        if(c%j==0){
          if(s.count(j)){
            p=false;
            break;
          }
          else{
            s.insert(j);
            while(c%j==0) c/=j;
          }
        }
      }
      if(!p) break;
      if(c!=1){
        if(s.count(c)){
          p=false;
          break;
        }
        else s.insert(c);
      }
    }
    if(p) cout<<"pairwise coprime"<<endl;
    else cout<<"setwise coprime"<<endl;
  }
}

Measured memory_bytes: 7592
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<double, set<int> > S;
inline double sq(double x){return x*x;}
int main(){
  int N,M;
  while(cin>>N>>M){
    double c[20][3];
    double ans = 0;
    REP(i,N)REP(j,3)cin>>c[i][j];
    REP(S, 1<<N)if(__builtin_popcount(S) == M){
      double len = 0;
      REP(i,N)if(S>>i & 1){
        FOR(j,i+1,N)if(S>>j & 1){
          REP(k,3)len += sq(c[i][k]-c[j][k]);
        }
      }
      ans = max(ans, len);
    }
    printf("%.6lf\n",ans);
  }
  return 0;
}

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

int main() {
  long long N; cin >> N;
  
  long long a, b;
  a = sqrt(N);
  while(N%a != 0) a--;
  b = N/a;
  
  cout << a+b-2 << endl;

}

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

l = [0, 0]
for _ in range(N):
    t, a = map(int, input().split())
    if t >= l[0] and a >= l[1]:
        l = [t, a]
    else:
        temp = max((l[0] - 1) // t + 1, (l[1] - 1) // a + 1)
        l = [t * temp, a * temp]
    
print(sum(l))

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
int main(){
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++){
    cin >> h[i];
  }
  vector<int> dp(N, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++){
    for (int j = i + 1; j <= i + K; j++){
      if (j >= N){
        break;
      }
      dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[N - 1];
}

Measured memory_bytes: 1024
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const int mo = 1e9 + 7;
typedef long long ll;
ll n,a,b,ans;
ll c[100000];void solve(){ans = max(abs(c[n - 1] - b), abs(c[n - 2] - c[n - 1]));;}
main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin>>n>>a>>b;
    rep(i,n)cin>>c[i];
    if(n==1)cout<<abs(c[0]-b)<<endl;
    else if(abs(c[n-2]-c[n-1])>abs(c[n-1]-b))cout<<abs(c[n-2]-c[n-1]);
    else cout<<abs(c[n-1]-b)<<endl;
    return 0;
}

Measured memory_bytes: 256
s = input()
s1 = int(s[0:2])
s2 = int(s[2:])

if s1 > 0 and s1 < 13:
    if s2 > 0 and s2 < 13:
        print("AMBIGUOUS")
    else:
        print("MMYY")
else:
    if s2 > 0 and s2 < 13:
        print("YYMM")
    else:
        print("NA")

Measured memory_bytes: 9184
// Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int s1, s2;
		cin >> s1 >> s2;

		if(s1==0&&s2==0)
			break;

		char n_max = 'A';
		int s_max = s1+s2;

		cin >> s1 >> s2;
		if(s1+s2>s_max){
			n_max = 'B';
			s_max = s1+s2;
		}

		cin >> s1 >> s2;
		if(s1+s2>s_max){
			n_max = 'C';
			s_max = s1+s2;
		}

		cin >> s1 >> s2;
		if(s1+s2>s_max){
			n_max = 'D';
			s_max = s1+s2;
		}

		cin >> s1 >> s2;
		if(s1+s2>s_max){
			n_max = 'E';
			s_max = s1+s2;
		}

		cout << n_max << ' ' << s_max << '\n';
	}

	return 0;
}

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

template <class T>
using lim = numeric_limits<T>;

const int maxn = 500;

int starts[maxn+1][maxn+1];

int main() {
	ios_base::sync_with_stdio(false) ;
	cin.tie(0);
	
	memset(starts, 0, sizeof starts);
	
	int n, m, Q;
	cin >> n >> m >> Q;
	for (int i = 0; i < m; i++){
		int l, r;
		cin >> l >> r;
		starts[l][r]++;
	}
	
	// how many trains starts below i inclusive but ends before j
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			starts[i][j] += starts[i][j-1] + starts[i-1][j] - starts[i-1][j-1];
		}
	}
	
	while(Q--){
		int p, q;
		cin >> p >> q;
		
		cout << starts[q][q]-starts[p-1][q] << endl;
	}
}



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

using namespace std;

#define rep(i, a) for(int i = 0; i < a; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define MP(a, b) make_pair(a, b)

int H, W, s[52][52], c[52][52], h[52][52];
queue<pair<pair<int, int>, int>> q;

void mypush(int x, int y, int cost)
{
  if (s[x][y] == 0 || h[x][y] == 1) return;
  h[x][y] = 1;
  c[x][y] = cost;
  q.push(MP(MP(x, y), cost));
}

int main()
{
  cin >> H >> W;
  int res = 0;
  rep(i, H) {
    string t;
    cin >> t;
    rep(j, W) {
		s[i+1][j+1] = t[j] == '.';
    	res += s[i+1][j+1];
    }
  }
  c[H][W] = 1e9;
  q.push(MP(MP(1, 1), 0));
  h[1][1] = 1;
  while (!q.empty()) {
    pair<pair<int, int>, int> P = q.front();
    pair<int, int> p = P.first;
    int x = p.first;
    int y = p.second;
    int cost = P.second;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    rep(i, 4) mypush(x+dx[i], y+dy[i], cost+1);
    q.pop();
  }
  if (c[H][W] == 1e9) cout << -1 << endl;
  else cout << res - (c[H][W] + 1) << endl;
  return 0;
}

Measured memory_bytes: 256
// 'By Anki'
 
#include<bits/stdc++.h>
// #include<iostream>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout<<#i<<" = "<<i<<",  ";
#define wre(i) cout<<#i<<"  =  "<<i<<endl;
#define all(v) v.begin(),v.end()
typedef long long ll;
#define lim 1000000007LL
typedef unsigned long long ull;
#define M 100006LL
using u64= uint64_t;
// using i128= __int128;
// typedef unsigned int uint128_t __attribute__((mode(TI)));
 
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
 
ll fact(ll n)
{
    ll ans=1LL;
    while(n)
    ans*=(n--);
    return ans;
 
}
 
ll phi(ll n){
    ll res=n;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            while(n%i==0)n/=i;
            res-=res/i;
        }
    }
    if(n>1)res-=res/n;
    return res;
}

ll a[100][100];
void sparse(vector<ll>v)
{
    ll n=v.size();
    ll k=log2(n)+1;
    for(int i=0;i<n;i++)a[i][0]=v[i];
    for(int j=1;j<=k;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        a[i][j]=a[i][j-1]+a[i+(1<<(j-1))][j-1];
    }
}
int main()
{
fast
ll t=1LL;

for(;t;t--){
   ll a;string b;
   cin>>a>>b;
   ll b1=0;
   for(int i=0;i<b.length();i++)
   {
       if(b[i]=='.')continue;
       b1*=10;
       b1+=b[i]-'0';
   }
//    wr(b1)
    ll ans=(b1*a)/100;
    cout<<ans;
} 
}

Measured memory_bytes: 3656
#include "bits/stdc++.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <tuple>
using namespace std;

#define FOR(i,init,a) for(int i=init; i<a; i++)
#define rep(i,a) FOR(i,0,a)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define rep1(i,a) for(int i=1; i<=a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a,b) cout << a << " " << b << endl;
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl;
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a,n) memset( a, n, sizeof(a))
#define all(a) a.begin(),a.end()
#define chmin(a,b) a=min(a,b);
#define chmax(a,b) a=max(a,b);

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

string s;

int calc(char x){
    string t=x+s;
    int res=0,cnt=0;
    rrep(i,t.size()-1){
        if(t[i]==x){
            chmax(res,cnt);
            cnt=0;
        }else{
            cnt++;
        }
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>s;
    
    int ans=INF;
    rep(i,26){
        chmin(ans,calc('a'+i));
    }
    cout1(ans)
}

Measured memory_bytes: 256
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()

ans = 0
for k in range(K, N+2):
  mi = k*(k-1)//2
  x = N-k
  ma = N*(N+1)//2 - x*(x+1)//2
  ans += ma-mi+1
  ans %= mod

print(ans%mod)

Measured memory_bytes: 10052
#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define fory(v) for(ll y=0; y<v; y++)
#define ll long long
#define double long double
#define pb(a) push_back(a)
const ll INF = 0x3f3f3f3f;
const ll inf = pow(10,18);
ll modulo = 998244353 ;
#define MAX 300100

void deal(){
	ll n, a, b;
	cin>>n>>a>>b;
	cout<<min(n*a, b);
}


int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}

Measured memory_bytes: 256
all_date = gets.to_i
before_date = gets.to_i
normal_price = gets.to_i
after_price = gets.to_i

before_sum = 0
after_sum = 0
if (all_date - before_date) > -1
    before_sum = before_date * normal_price
    after_sum = (all_date - before_date) * after_price
else
    before_sum = all_date * normal_price
end
puts before_sum + after_sum

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

using namespace std;

#define pb push_back
#define fi first
#define se second
typedef pair<int, int> ii;

const int N = 1e5 + 5;
int n, cnt[2][N], maxx[2];
vector<ii> vec[2];

int main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i += 2) {
        int a; cin >> a;
        cnt[0][a] ++;
        cin >> a;
        cnt[1][a] ++;
    }

    for (int _ = 0; _ < 2; ++_) {
        for (int i = 0; i < N; ++i) if (cnt[_][i]) vec[_].pb(ii(cnt[_][i], i) );
        sort(vec[_].begin(), vec[_].end() );
    }

    if (vec[0].back().se != vec[1].back().se) cout << n - vec[0].back().fi - vec[1].back().fi;
    else {
        if (vec[0].size() == 1 && vec[1].size() == 1) return cout << n / 2, 0;
        int ans = N;
        if (vec[0].size() > 1) ans = min(ans, n - vec[1].back().fi - vec[0][ vec[0].size() - 2 ].fi);
        if (vec[1].size() > 1) ans = min(ans, n - vec[0].back().fi - vec[1][ vec[1].size() - 2 ].fi);
        cout << ans;
    }

    return 0;
}


Measured memory_bytes: 2168
// Panasonic 2020 2020/03/14 B - Bishop
#include <bits/stdc++.h>
#define rep(i ,n) for(int i=0;i<(int)(n);++i)
using namespace std;
typedef long long int int64;
typedef unsigned long long uint64;

int main(){
    int64 w , h; cin >> h >> w;
    int64 x , y;
    if(w % 2 == 0) x = w / 2;
    else x = w / 2 + 1;
    if(h % 2 == 0) y = h / 2;
    else y = h / 2 + 1;
    if (w == 1 || h == 1) { cout << 1 << endl; return 0;}
    if(h % 2 == 0){
        if( w % 2 == 0){
        cout << ( ( 2 * x ) * ( h / 2 ) ) << endl;
            } else {
                cout << ( x + ( x - 1 )) * ( h / 2 ) << endl;
            }
            return 0;
    } else {
        if( w % 2 == 0){
            cout << ( ( 2 * x) * ( h / 2 ) + x ) << endl;
        } else {
        cout << ( (x + ( x - 1 )) * ( h / 2 ) + x ) << endl;
        }
    }    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ll,ii>
#define endl '\n'

long long qexp(long long b,long long p,int m){
    long long res=1;
    while (p){
        if (p&1) res=(res*b)%m;
        b=(b*b)%m;
        p>>=1;
    }
    return res;
}


int main(){
	ios::sync_with_stdio(0);
    cin.tie(0);
	
	ll n;
	cin>>n;
	
	ll ans=0;
	
	for (int x=1;x<=n;x++)
		for (int y=1;y<=n;y++)
			for (int z=1;z<=n;z++)
				ans+=__gcd(x,__gcd(y,z));
				
	cout<<ans;
}

Measured memory_bytes: 3652
#include<stdio.h>
#include<string.h>

#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch){
  if(ch == 'A') return 1;
  else if(ch == 'C') return 2;
  else if(ch == 'G') return 3;
  else if(ch == 'T') return 4;
    else return 0;
}

long long getKey(char str[]){
  long long sum = 0, p = 1, i;
  for(i = 0; i < strlen(str); i++){
    sum += p*(getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key){return key%M;}
  int h2(int key){return 1 + (key % (M - 1));}

  int find(char str[]){
    long long key, i, h;
    key = getKey(str);
    for(i = 0;;i++){
      h = (h1(key) + i *h2(key)) % M;
      if(strcmp(H[h],str)== 0) return 1;
      else if(strlen(H[h])== 0) return 0;
    }
    return 0;
  }

  int insert(char str[]){
    long long key, i, h;
    key = getKey(str);
     for(i = 0;;i++){
      h = (h1(key) + i *h2(key)) % M;
      if(strcmp(H[h],str)== 0) return 1;
      else if(strlen(H[h])== 0){
	strcpy(H[h], str);

	return 0;
      }
    }
    return 0;
  }
      
  int main(){
    int i, n, h;
    char str[L],com[9];
    for(i = 0; i < M; i++) H[i][0] = '\0';
    scanf("%d",&n);
    for(i = 0; i < n; i++){
      scanf("%s %s",com,str);

      if( com[0] == 'i'){
	insert(str);
      }
      else {
	if(find(str)){
	  printf("yes\n");
	}else {
	  printf("no\n");
	}}}
    return 0;
  }



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

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

    set<int> ans;
    for(int i=0; i<N; i++){
        ans.insert(A[i]);
    }
    if(ans.size()==N){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}


Measured memory_bytes: 13712
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<cmath>
#include<vector>
#include<algorithm>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 1e3+5;

int main(){
	int h, n; cin >> h >> n;
	int a, sum = 0;
	for(int i = 1; i <= n; i++){
		cin >> a;
		sum += a;
	}
	cout << (sum >= h ? "Yes" : "No") << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
#pragma region header
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i > -1; i--)
using namespace std;

using vi = vector<int>;

#pragma endregion header

void solve(int N, vi a, vi b) {
  int ans = 0;
  vi t(29, 1), u(29);
  rep(i, 28) t[i + 1] = t[i] << 1;
  rep(i, 29) u[i] = t[i] * 2 - 1;
  rrep(k, 29) {
    rep(i, N) a[i] &= u[k];
    rep(i, N) b[i] &= u[k];
    sort(all(b));
    bool odd = false;
    int t1 = t[k], t2 = t[k] * 2, t3 = t[k] * 3, t4 = t[k] * 4;
    rep(i, N) odd ^= ((lower_bound(all(b), t4 - a[i]) - lower_bound(all(b), t3 - a[i])) ^ (lower_bound(all(b), t2 - a[i]) - lower_bound(all(b), t1 - a[i]))) & 1;
    if(odd) ans += t[k];
  }
  cout << ans << endl;
}


#pragma region main
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int N;
  cin >> N;
  vi a(N);
  for(int i = 0 ; i < N ; i++){
    cin >> a[i];
  }
  vi b(N);
  for(int i = 0 ; i < N ; i++){
    cin >> b[i];
  }
  solve(N, move(a), move(b));
}
#pragma endregion main


Measured memory_bytes: 4776
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

int main(){
  int n;
  P a[111111];
  cin>>n;
  for(int i=0;i<n;i++){
    int t;
    cin>>t;
    a[i]=P(t,i);
  }

  sort(a,a+n);

  int ans=0;
  for(int i=0;i<n;i++){
    if(a[i].second%2==i%2)continue;
    ans++;
  }

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


Measured memory_bytes: 1920
#include <stdio.h>
#define N 101
int main() {
  	char c[N];
  	int num,i,j;
  	scanf("%d",&num);
  	scanf("%s", &c);
  	if(num&1) {
      	printf("No");
    	return 0;
    }
  	j=num/2;
  	for(i=0;i<num/2;i++) {
    	if(c[i]!=c[j]) {
          	printf("No");
    		return 0;
        }
      	j++;
    }
  	printf("Yes");
	return 0;
}

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

int main() {
    string alphabet = "abcdefghijklmnopqrstuvwxyz";
    int n,m;
    cin >> n >> m;
    if((500*n)>=m){
        cout << "Yes";
    }else{
        cout << "No";
    }
}

Measured memory_bytes: 3572
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
using namespace __gnu_pbds; 
#define ll          long long int
#define ull         unsigned long long int
#define pb          push_back
#define pii         pair<int,int>
#define F           first
#define S           second
#define vi          vector<int>
#define vli          vector<ll>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define M_PI        3.14159265358979323846
#define mp          make_pair
#define sz(x)       (int)x.size()
#define matrix      vector<vector<ll>>
#define forn(i,a,b)  for(int i=a;i<b;i++)
#define eps 0.0000000001
#define all(a)      (a).begin(),(a).end()
#define ordered_set     tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> 

const long long INF = (long long) 1e11+1;
const long long mod = (long long) 100000000 ;
const long long hell = (long long) 1000000007;


ll power(ll x,ll y)
{
  ll res=1;
  while(y>0){
    if(y%2) res=(x*res);
    x=(x*x);    
    y=y/2;
  }
  return res;
}

ll modinv(ll x,ll m){
  return power(x,m-2);
}

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a,ll b)
{
    while( b != 0)
	{
		ll  t = b;
		b= a %t;
		a = t;
	}
	return a;
}


void solve(){
    double r;
    cin>>r;
    double c=2*M_PI*r;
    cout<<fixed<<setprecision(5)<<c;
}


int main(){
   fastIO();
   int test;
   //cin>>test;
   test=1;
  
   while(test--)
   solve();
}

///Before submit=>
///    *check for integer overflow,array bounds
///    *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){
    
    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;
        
        if(s1.substr(0,s2.length())!=s2)
        return "";
        
        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set
 
struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }  
};
struct pair_hash
{
	template <class T1, class T2>
	std::size_t operator () (std::pair<T1, T2> const &pair) const
	{
		std::size_t h1 = std::hash<T1>()(pair.first);
		std::size_t h2 = std::hash<T2>()(pair.second);

		return h1 ^ h2;
	}
};
*/



Measured memory_bytes: 3796
#include<iostream>
#include<vector>
#include<algorithm>//sort,二分探査など

//QCFium法
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

using namespace std;
using i64 = int64_t;

constexpr i64 INF = 1010000000000000017LL;

#define _overload3(_1,_2,_3,name,...) name
#define repi(i,start,end) for(int i=(int)(start);i<(int)(end);++i)
#define _rep(i,n) repi(i,0,n)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,start,end) for(int i=(int)(start);i>(int)(end);--i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

#define v(T) vector<T>
#define vv(T) v(v(T))
#define vvv(T) v(v(v(T)))
typedef v(i64) vi;

#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >

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

template<typename T> ostream &operator<<(ostream &os, const vector< T > &v){
  rep(i,v.size()) os << v[i] << (i + 1 != v.size() ? " " : "");
  return os;
}
template<typename T> istream &operator>>(istream &is, vector< T > &v){
  for(T &in : v) is >> in;
  return is;
}

struct uf{
  private:
    vi par, rank, size, c;
  public:
    uf(i64 n){ init(n); }
    void init(i64 n){
      par.resize(n);
      rank.resize(n,0);
      size.resize(n,1);
      c.resize(n,0);
      rep(i,n) par[i] = i;
    }
    i64 root(i64 x){
      if(par[x] == x) return x;
      return par[x] = root(par[x]);
    }
    bool same(i64 x, i64 y){ return root(x) == root(y); }
    void unite(i64 x, i64 y){
      x = root(x), y = root(y);
      if(x==y) return;
      if(rank[x]==rank[y]) rank[x]++;
      if(rank[x]<rank[y]) swap(x,y);
      par[y] = x;
      size[x] += size[y];
      c[x] += c[y];
    }
    void setc(i64 x, i64 y) { c[x] = y;}
    i64 num(i64 x){ return size[root(x)]; }
    i64 numc(i64 x) { return c[root(x)];}
};

void Main()
{
  i64 N, K;
  cin >> N >> K;
  vi P(N);
  cin >> P;
  vi C(N);
  cin >> C;

  uf uni(N);

  rep(i,N) {
    uni.setc(i,C[i]);
  }
  rep(i,N) {
    uni.unite(i,P[i]-1);
  }

  i64 ans = -1 * INF;

  rep(i, N) {
    i64 tmpans;
    i64 psum = 0;
    i64 x = i;
    rep(j, uni.num(i)){
      psum += C[x];
      if(j>=K) break;
      tmpans = psum;
      if(uni.numc(i)>0) tmpans += uni.numc(i) * ((K-j-1)/uni.num(i));
      chmax(ans, tmpans);
      x = P[x] - 1;
    }
  }

  cout << ans << "\n";
}

int main()
{
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
//  cout << fixed << setprecision(15);
  Main();
}

Measured memory_bytes: 3800
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define dcout cout<<fixed<<setprecision(15);
#define mp make_pair
#define pb push_back

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

constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template< typename S, typename T >
inline void chmax(S &a, const T &b) { if(a < b) a = b; }
 
template< typename S, typename T >
inline void chmin(S &a, const T &b) { if(a > b) a = b; }
 

//lcm//
ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd(y%x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

//a^n mod p//
ll modpow(ll a, ll n, ll p) {
	if(n==0) return (ll)1;
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p;
  ll t = modpow(a, n / 2, p);
  return (t * t) % p;
}

//inversemod//
ll modinv(ll a, ll m) {
	if(m==0)return (ll)1;
	ll b = m, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];
// 
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 
ll COM(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//LARGE n
ll com(ll n,ll m){
    if(n<m || n<=0 ||m<0){
		return 0;
	}
	if( m==0 || n==m){
		return 1;
	}
	ll k=1;
	for(ll i=1;i<=m;i++){
       k*=(n-i+1); 
	   k%=MOD;
	   k*=modinv(i,MOD);
	   k%=MOD;
	}
	return k;
}

//radP
ll rad(ll u, ll p){
ll cnt=0;
	while(u%p==0){
		u/=p;
		cnt++;
	}
	return cnt;
}


////////////////////////////////////////////////////////////////////

int main() {
ll n,t;
cin>>n>>t;

ll p[n+1];
rep(i,n){
	cin>>p[i];
}

ll val=0;
for(int i=1;i<=n-1;i++){
if(p[i+1]-p[i]>t)val+=t;
else val+=p[i+1]-p[i];
}

val+=t;
cout<<val<<endl;


return 0;
}




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

int main(){
int H,W;
  cin>>H>>W;
  
  char C[220][110];
  for(int i=0; i<H; i++){
   for(int j=0; j<W; j++){
    cin>>C[i][j]; }}
  
  for(int i=0; i<H; i++){
    cout<<C[i]<<endl;
    cout<<C[i]<<endl;  
  }
  
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstddef>
#include<queue>
#include<math.h>
#include <cerrno>
#include <cstring>
#include <cfenv>
using namespace std;

int main()
{
    long long H, attack = 1, intnum = 0;
    double num = 0.0;
    cin >> H;
    num = floor(log2(H));
    intnum = num;
    intnum += 1;
    for(long long i; i < intnum; ++i) attack *= 2;
    attack -= 1;
    cout << attack << endl;
    return 0;
}


Measured memory_bytes: 512
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int x; long long num;
}a[100005];
long long ans,dis[100005];
inline int read()
{
	char ch=getchar(); int ans=0;
	while (ch<'0'||ch>'9') ch=getchar();
	while (ch<='9'&&ch>='0') ans=ans*10+ch-48,ch=getchar();
	return ans;
}
bool cmp(node t1,node t2){return t1.x<t2.x;}
int main()
{
	int n=read(),s=read();
	for (int i=1; i<=n; i++) a[i].x=read(),a[i].num=read();
	sort(a+1,a+1+n,cmp);
	int l=1,r=n,t=2;
	while (a[l].x<s&&a[r].x>s)
		if (a[l].num<a[r].num) a[r].num+=a[l].num,dis[r]+=dis[l]+((t!=0)?(a[r].x-a[l].x):0),t=0,l++;
		else a[l].num+=a[r].num,dis[l]+=dis[r]+((t!=1)?(a[r].x-a[l].x):0),t=1,r--;
	if (a[l].x>s) ans=dis[r]+a[r].x-s;
	else ans=dis[l]+s-a[l].x;
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 1792
#include <bits//stdc++.h>
using namespace std;
int main(void){
    int a,b,c,i,j,h;
    string s,t;
    cin>>s>>t;
    a=s.size();
    b=t.size();
    int chart[b+1][a+1];
    for(i=0;i<b+1;i++){
		for(j=0;j<a+1;j++){
			if(i==0)
			chart[i][j]=1;
			else if(j==0)
			chart[i][j]=0;
			else if(s[j-1]==t[i-1])
			chart[i][j]=(chart[i][j-1]+chart[i-1][j-1])%1000000007;
			else
			chart[i][j]=chart[i][j-1];
		}
	}
	cout<<chart[b][a]<<endl;
}



Measured memory_bytes: 7096
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#define _CRT_SECURE_NO_WARNINGS
#define TLong long long 

// fact(n)
int fact(int n){
	if(n == 1)	return 1;
	else return (n * fact(n - 1));
}

// combination(n,r)
int combination(int n, int r){

	if(r == 0 || r == n)	return 1;
	else if(r == 1)	return n;
	return (combination(n - 1, r - 1) + combination(n - 1, r));
}

// gcd(a,b)
int gcd(int a,int b){
	int ret;
	ret = (b == 0) ? a : gcd(b, a % b);
	return ret;
}

// lcm(a.b)
int lcm(int a,int b){
	return (a * b) / gcd(a,b);
}

// qsort(array, size, sizeof(int),comp);
int comp(const int *a,const int *b){
	return (*a - *b);
}

// append(array,*size,addNum)
void append(int *array,int *size,int num){
	realloc(array, sizeof(int) * (*size + 1));
	array[(*size)] = num;
	++(*size);
}

// pop(array,*size) & nongetpop(array,*size)
int pop(int *array,int *size){
	int temp = array[(*size - 1)];
	realloc(array,sizeof(int) * (*size - 1));
	--(*size);
	return temp;
}

void nongetpop(int *array,int *size){
	realloc(array,sizeof(int) * (*size - 1));
	--(*size);
}

int main(int argc, char const *argv[]){
	char s1[12],s2[12],s3[12];
	scanf("%s %s %s",s1,s2,s3);
	if(s1[strlen(s1) - 1] == s2[0] && s2[strlen(s2) - 1] == s3[0]){
		puts("YES");
	}
	else puts("NO");
	return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000
#define int long long int

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dx[]={1, 0};
int dy[]={0, 1};
int const MOD = 1000000007;
ll mod_pow(ll x, ll n) {return (!n)?1:(mod_pow((x*x)%MOD,n/2)*((n&1)?x:1))%MOD;}
int madd(int a, int b) {return (a + b) % MOD;}
int msub(int a, int b) {return (a - b + MOD) % MOD;}
int mmul(int a, int b) {return (a * b) % MOD;}
int minv(int a) {return mod_pow(a, MOD-2);}
int mdiv(int a, int b) {return mmul(a, minv(b));}

namespace std {
    bool operator<(const P& a, const P& b) {
        return a.X != b.X ? a.X < b.X : a.Y < b.Y;
    }
}

int w, h;
char board[110][110];
string dp[110][110];

string comp(string s1, string s2) {
    if(s1.length() == s2.length()) return max(s1, s2);
    else if(s1.length() > s2.length()) return s1;
    else return s2;
}

signed main() {
    while(cin >> w >> h, w||h) {
        repq(i,0,h) repq(j,0,w) dp[i][j] = "";
        repq(i,1,h) repq(j,1,w) cin >> board[i][j];
        string ans = "";
        repq(i,1,h) repq(j,1,w) {
            if('A' <= board[i][j] && board[i][j] <= 'Z') dp[i][j] = "";
            else {
                string s1 = dp[i-1][j], s2 = dp[i][j-1];
                string ma = comp(s1, s2);
                // cout << "s1 = " << s1 << ", s2 = " << s2 << ", ma = " << ma << endl;
                if(ma == "") {
                    if(board[i][j] != '0') dp[i][j] += board[i][j];
                }
                else dp[i][j] = ma + board[i][j];
            }
            ans = comp(ans, dp[i][j]);
        }
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 3468