akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>

#define INF 2000000001
#define MAX 101
#define NIL -1

using namespace std;

typedef struct {
    int toid;
    int far;
} PATH;

vector<PATH> D[MAX];
int N;
int Dist[MAX];

void dijkstra(){//0??????????????¢??????????????£?????????
    
    bool complete[MAX];
    vector<int> S;
    
    for (int i = 0; i < N; i++) {
        complete[i] = false;
        Dist[i] = INF;
    }
 
    //???????¨????
    Dist[0] = 0;
    S.push_back(0);
    int u=0,minf;
    complete[u] = true;
    
    while (1) {
        if (S.size() == N) break;
        
        //????????????
        for (int j = 0; j < D[S[u]].size(); j++) {
            if (!complete[D[S[u]][j].toid] && Dist[S[u]] + D[S[u]][j].far < Dist[D[S[u]][j].toid]) {
                Dist[D[S[u]][j].toid] = Dist[S[u]] + D[S[u]][j].far;
            }
        }
        
        
        
        u = NIL;
        minf = INF;
        
        //?¨??????´??°
        for (int i = 0; i < S.size(); i++) {
            
            for (int j = 0; j < D[S[i]].size(); j++) {
                if (Dist[D[S[i]][j].toid] < minf && !complete[D[S[i]][j].toid]) {
                    minf = Dist[D[S[i]][j].toid];
                    u = D[S[i]][j].toid;
                }
            }
            
        }
        S.push_back(u);
        complete[u] = true;
        u = S.size()-1;//????????§u????????§??¨????????????????????????
    }
}

int main(){
    cin >> N;
    for (int i = 0; i < N; i++) {
        int bb1,bb2;
        cin >> bb1 >> bb2;
        for (int j = 0; j < bb2; j++) {
            PATH bb3;
            cin >> bb3.toid >> bb3.far;
            D[i].push_back(bb3);
        }
    }
    
    dijkstra();
    
    for (int i = 0; i < N; i++) {
        printf("%d %d\n",i,Dist[i]);
    }
    
    return 0;
}

Measured memory_bytes: 1252
# -*- coding: utf-8 -*-
import sys
import itertools

# 標準入力を取得
A = list(map(int, input().split()))

# 求解処理
ans = sys.maxsize
for A_1, A_2, A_3 in itertools.permutations(A, r=3):
    ans = min(ans, abs(A_2 - A_1) + abs(A_3 - A_2))

# 結果出力
print(ans)


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

int main() {
    int m, d;
    cin >> m >> d;
    int sum = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= d; j++) {
            if (j % 10 < 2 || j / 10 < 2) continue;
            if ((j % 10) * (j / 10) == i) sum++;
        }
    }
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 384
program main
  implicit none
  integer :: n, i, a, z
  character(200000) :: s
  read (*, *) s
  n = len_trim(s)
  do i = 1, n
    if (s(i:i) == "A") then
      a = i
      exit
    end if
  end do
  do i = 1, n
    if (s(i:i) == "Z") z = i
  end do
  write (*, "(i0)") z - a + 1
end program main


Measured memory_bytes: 1084
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	addition_of_big_integers
# CreatedDate:  2020-07-26 14:23:13 +0900
# LastModified: 2020-07-26 14:23:35 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    a, b = map(int, input().split())
    print(a+b)


if __name__ == "__main__":
    main()



Measured memory_bytes: 5952
#include <iostream>
#include <vector>

int main() {

    /*
     *  Knapsack problem
     *  For any given item, we can take it or not take it
     *  let DP[i][j] be the best value we can get with knapsack size i and the first j items.
     *  initialize with all zeroes
     *
     *  Loop through each item, and each knapsack size.
     *  Given a current knapsack size, i, and a current item j with weight `w` and value `v`,
     *  what's better?
     *      - taking the item: `dp[i-w][j-1]` + v
     *      - not taking the item `dp[i][j-1]`
     *  dp[i][j] is the max of those two
     */

    int n, maxw;
    std::cin >> n;
    std::cin >> maxw;

    std::vector<std::vector<long long>> dp(n+1, std::vector<long long>(maxw+1, 0ll));

    for (int i = 1; i <= n; i++) {
        int w, v;
        std::cin >> w;
        std::cin >> v;

        for (int j = 1; j <= maxw; j++) {
            dp[i][j] = std::max((j-w >= 0) ? dp[i-1][j-w]+v : 0, dp[i-1][j]);
        }
    }

    std::cout << dp[n][maxw];

    return 0;
}


Measured memory_bytes: 80256
#include<stdio.h>
int main(){
    long long int n;
    scanf("%lld",&n);
    long long int a[n+1];
    long long int sum=0;
    for(long long int i=1;i<(n+1);i++){
        if(i%3==0 || i%5==0){
            a[i]=0;
        }
        else
        {
            a[i]=i;
        }
        
    }
    for(long long int i=1;i<(n+1);i++){
        sum=sum+a[i];
    }
    printf("%lld",sum);
    return 0;
}

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

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

    int n;
    cin >> n;
    vector<int> o(n), p(n), q(n);

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

    int a, b;
    int cnt = 1;
    do{
        bool same_p = true;
        rep(i, n) {
            if(o[i] != p[i]) same_p = false;
        }
        if(same_p) a = cnt;

        bool same_q = true;
        rep(i, n) {
            if(o[i] != q[i]) same_q = false;
        }
        if(same_q) b = cnt;

        ++cnt;
    } while(next_permutation(o.begin(), o.end()));

    cout << abs(a - b) << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <map>

using namespace std;

int main(void) {
  string S;
  cin >> S;
  int N;
  N = S.size();
  int cnt = 0;
  //cout << N;
  for (int i = 0; i < N / 2; i++) {
    //cout << S[i] << S[N - 1 - i] << endl;
    if (S[i] != S[N - 1 - i]) {
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <iomanip>
#include <random>
#include <complex>
#include <functional>

using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")

using ll =  long long;

using pint = std::pair<int, int>;
using pll = std::pair<ll, ll>;


constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

namespace { 
	template<typename T> void chmax(T& a, T b) { a = std::max(a, b); }
	template<typename T> void chmin(T& a, T b) { a = std::min(a, b); }
	template<typename T> void chadd(T& a, T b) { a = a + b; }

	template <typename T> void Cout(T& x, char end = '\n') { std::cout << x << end; }
	template <typename T> void Cout(std::vector<T>& x, char sep = ' ', char end = '\n') { for (std::size_t i = 0, sz = x.size(); i < sz; i++) { std::cout << x[i] << (i == sz - 1 ? end : sep); } }
	template <typename T> void CoutNoSpace(std::vector<T>& x, char end = '\n') { for (std::size_t i = 0, sz = x.size(); i < sz; i++) { std::cout << x[i]; if (i == sz - 1) std::cout << std::endl; } }


	// 標準入出力
	struct inp {
		size_t sz;
		inp(size_t _sz = 1) : sz(_sz) {}
		template <typename T> operator T () const { T a; std::cin >> a; return a; }
		template <typename T> operator std::vector<T>() const { vector<T> a(sz); for (std::size_t i = 0; i < sz; i++) std::cin >> a[i]; return a; }
		template <typename T, typename U> operator std::pair<T, U>() const { T f; U s; std::cin >> f >> s; return std::pair<T, U>(f, s); }
	};

	inp inp1; // input one
}


int main() {

	int n, m;
	cin >> n >> m;
	vector<int> s(m), c(m);
	rep(i, m) cin >> s[i] >> c[i];

	// 丁度n桁
	// 答え
	vector<int> ans(n, -1);
	rep(i, m) {
		if (ans[s[i] - 1] == -1) ans[s[i] - 1] = c[i];
		else {
			if (ans[s[i] - 1] != c[i]) {
				Cout("-1");
				return 0;
			}
		}
	}

	if (n > 1 && ans[0] == 0) Cout("-1");
	else {
		if (n > 1 && ans[0] == -1) ans[0] = 1;
		rep(i, n) {
			cout << max(0, ans[i]);
		}
		cout << endl;
	}

	return 0;
}

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

int main(){
  int n, i, j, k, f, p[11];
  char c[128];

  while(scanf("%d", &n) && n){
    scanf("%s", c);
    memset(p, 0, sizeof(p));
    f = j = 0;
    for(i = 0; i < 100; ++i){
      p[j]++;
      switch(c[i]){
      case 'M':
	break;
      case 'S':
	f += p[j];
	p[j] = 0;
	break;
      case 'L':
	p[j] += f;
	f = 0;
	break;
      }
      j++;
      if(j >= n) j = 0;
    }
    for(i = n - 1; i >= 0; --i)
      for(j = 0; j < i; ++j)
	if(p[j] > p[j + 1])
	  p[j] += p[j + 1], p[j + 1] = p[j] - p[j + 1], p[j] -= p[j + 1];

    for(i = 0; i < n; i++) printf("%d ", p[i]);
    printf("%d\n", f);
  }
  return 0;
}

Measured memory_bytes: 604
#include <bits/stdc++.h>
#define Phuong ios_base::sync_with_stdio(0); cin.tie(0);
#define mpp make_pair
#define pb push_back

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

int main()
{
#ifndef RICARDO
    //freopen("input.inp", "r", stdin);
#endif // RICARDO
    Phuong;
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> b(n);
    for(int i=0; i<n; ++i)
    {
        cin >> a[i];
    }
    int t=0;
    for(int i=0; i<n; ++i)
    {
        cin >> b[i];
        if (b[i]==a[i]) continue;
      	if (t!=0) continue;
        if (b[i]<a[i]) t=1; else t=2;
    }
    if (t==1) swap(a,b);
    int dem=0;
    while (a!=b)
    {
        next_permutation(a.begin(), a.end());
        dem++;
    }
    cout << dem;
    return 0;
}


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

const int S = 2e5 + 5;
char s[S];

int32_t main() {
  scanf("%s", s);
  int len = strlen(s);
  long long ans = 0;

  bool change;
  do {
    change = false;
    for (int i = 0, cntA = 0; i < len-2; ) {
      if (s[i] == 'A') cntA++;
      else cntA = 0;

      if (s[i] == 'A' && s[i+1] == 'B' && s[i+2] == 'C') {
        s[i+1] = 'A';
        s[i+2] = 'A';

        s[i+1-cntA] = 'B';
        s[i+2-cntA] = 'C';
        ans += cntA;
        i += 2;
        cntA--;
        change = true;
      } else {
        i++;
      }
    }
  } while(change);

  printf("%lld\n", ans);
  return 0;
}


Measured memory_bytes: 384
A,B = map(int,input().split())
keta = 0
tmp = B
twos = []
base = 1
while tmp > 0:
    tmp /= 2
    keta += 1
    base *= 2
    twos.append(base)

def func(n):
    if n%2 == 0:
        tmp = (n-1)//2
        ans = tmp%2 ^ n
    else:
        tmp = (n-1)//2 
        ans = tmp%2
    return ans
    
print(func(B)^func(A-1))
    

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

ll poww(ll n){
  return n*n*n*n*n;
}
int main(){
  ll x;

  cin >> x;

  for(int i =-1000; i <= 1000; i++){
    for(int j = -1000; j <= 1000; j++){
      if(poww(i)-poww(j)==x){
        cout << i << " " << j << endl;
        return 0;
        
      }
    }
  }




  
}

Measured memory_bytes: 3632
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <cassert>
#include <functional>
#include <set>
#include <deque>
using namespace std;
typedef long long ll;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const int INF = 1<<29;
const ll MOD = 1000000007;
const int MAX = 200000;

long long 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;
    }
}

// 二項係数計算
long long COM(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;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
//最大公約数
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }
//最小公倍数
int lcm(int x, int y) { return x / gcd(x, y) * y; }
long long com[200005];


int main(){
    ll n; cin >> n;
    ll a,b; cin >> a >> b;
    ll out = 0;
    ll x=1,y=1;
    out = modpow(2,n,MOD)-1;
    com[0] = 1;
    ll mm = max(a,b);
    //catcerr << mm << "\n";
    for(int i=1;i<=mm;i++){
        com[i] = (com[i-1]*(n-i+1)%MOD)*modinv(i,MOD)%MOD;
        //cerr << com[i] << "\t" << modinv(i,MOD) << endl;;
    }
    out = out - com[a];
    if(out<0) out += MOD;
    out = out - com[b];
    if(out<0) out += MOD;
    cout << out << endl;
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define p_b push_back
#define pl pair<ll,ll>
#define m_p make_pair
#define MOD 1000000007
#define F(i,a,b) for(i=a;i<b;i++)
#define RF(i,a,b) for(i=a;i>=b;i--)
#define nfs ios_base::sync_with_stdio(false);cin.tie(NULL)


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin >> n;
    ll tot = n*(n-1);
    tot /= 2;
    cout << tot << endl;

    return 0;

}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
    ll n,s=0,i;
    cin>>n;
    ll a[n];
    for(i=0;i<n;i++){
        cin>>a[i];
        s+=a[i];
    }
    ll ans = 0;
    for(i=0;i<n;i++){
        ans+=a[i]*(s-a[i]);
    }
    cout<<ans/2;
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  map<int, int> memo;
  
  rep(i, n) {
    int a;
    cin >> a;
    if(memo[a] != 0) {
      cout << "NO" << endl;
      return 0;
    } else {
      ++memo[a];
    }
  }
  
  cout << "YES" << endl;
    
  return 0;
}


Measured memory_bytes: 9600
xp1, yp1, xp2, yp2 = map(int, input().split())
q = int(input())

xp = complex(xp2- xp1, yp2 - yp1)

for _ in range(q):
    x, y = map(int, input().split())
    xpi = complex(x - xp1, y - yp1)
    dot = xp.real * xpi.real + xp.imag * xpi.imag
    v1 = dot * xp / (abs(xp) ** 2)
    v2 = v1 - xpi
    ans = complex(x, y) + v2 * 2
    print("{:.9f} {:.9f}".format(ans.real, ans.imag))



Measured memory_bytes: 5640
use std::io;

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

fn line_to_vec_i32(line: &str) -> Vec<i32> {
  line
    .trim()
    .split_whitespace()
    .map(|s| { s.parse::<i32>().unwrap_or(0) })
    .collect()
}

fn main() {
  let line = get_line();
  let v = line_to_vec_i32(&line);
  let (a, b, c) = (v[0], v[1], v[2]);
  let mut cnt = 0;
  for i in a..(b + 1) {
    if c % i == 0 {
      cnt += 1;
    }
  }
  println!("{}", cnt);
}



Measured memory_bytes: 5032
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <cmath>
#include <string>
#define FI first
#define SE second
#define PF push_front
#define PB push_back
#define PPF pop_front
#define PPB pop_back
#define MA make_pair
#define ll long long
#define PA pair<int,int>
#define VE vector<int>
#define VP vector<PA>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
#define YES(i) cout<<(i?"YES":"NO")<<endl
#define Yes(i) cout<<(i?"Yes":"No")<<endl
using namespace std;
//
const int INF=1e9+7;
const int mod=1e9+7;
//
//
struct poi{
  int X;int Y;int Z;
  bool operator<(const poi&R)const{
    return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
  }
};
//
//
int main(){
	ll N,P;
	ll A[100000];
	cin>>N>>P;
	FOR(i,0,N){
		cin>>A[i];
	}
	bool F=true;
	int now=0;
	while(F&&now<N-1){
		if(A[now]+A[now+1]>=P){
			F=false;
		}else{
			now++;
		}
	}
	if(F){
		cout<<"Impossible"<<endl;
	}else{
		cout<<"Possible"<<endl;
		VE V;
		int mem=now+1;
		while(now>=0){
			V.PB(now+1);
			now--;
		}
		now=mem;
		while(now<N-1){
			V.PB(now+1);
			now++;
		}
		reverse(V.begin(),V.end());
		FOR(i,0,(int)V.size()){
			cout<<V[i]<<endl;
		}
	}
	return 0;
}


Measured memory_bytes: 2168
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep1(i,n) for(int i=1; i<=(n); ++i)
#define repi(i,a,b) for(int i=a; i<=(b); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define mp make_pair
#define sz(v) (int)v.size()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
using ll = long long;
using namespace std;
struct aaa{aaa(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);};}aaaaaaa;
int MOD = 1e9 + 7;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return (a*b)/gcd(a,b);}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int N,M;

int main(){
    cin >> N >> M;
    cout << (1900*M+100*(N-M))*(int)pow(2,M) << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
#include<bitset>
using namespace std;

typedef long long ll;
#define rep(i, n) for(int i = 0;i < (n);i++)
#define repr(i, n) for(int i = (n);i >= 0;i--)
#define repf(i, m, n) for(int i = (m);i < (n);i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1;} return 0;}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
//////////////////////////////////////////////////


int main() {
    string s; cin >> s;
    if (s[(int)s.size()-1] == 's') {
        cout << s << "es" << endl;
    } else {
        cout << s << "s" << endl;
    }
    return 0;
}

Measured memory_bytes: 3640
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();

    int min = 100000;   
    int target = 0;
    int sumA = 0;
    int sumB = 0;

    for(int a=1;a<N;a++){
      // aの計算
      target = a;
      sumA = 0;
      while (target>0){
        sumA += target%10;
        target = target/10;
      }
      // bの計算
      target = N-a;
      sumB = 0;
      while (target>0){
        sumB += target%10;
        target = target/10;
      }
      
      if(sumA + sumB < min){
        min = sumA + sumB; 
      }
    }
    System.out.println(min);
  }
}

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

int main(){
  int N,M; cin>>N>>M;
  vector<vector<ll>> xyz(N,vector<ll>(3)); //xyz[N][3]
  rep(i,N) rep(j,3) cin>>xyz[i][j];
  vector<vector<ll>> SS(8,vector<ll>());//SS[8][0]
  for(int i=0;i<8;i++){
    for(int j=0;j<N;j++){
      ll S=0;
      for(int k=0;k<3;k++){
        if((i/(1<<k))%2==0) S+=xyz[j][k];
        else S-=xyz[j][k];
      }
      SS[i].push_back(S);
    }
    sort(SS[i].begin(),SS[i].end(),greater<>());
  }
  ll ans=0;
  rep(i,8){
    ll now=0;
    rep(j,M){
      now+=SS[i][j];
    }
    ans=max(ans,now);
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 384
#include <iostream>

namespace myspace{

	template<typename Type>
	class vector{
	public:
		void push_back(Type);
		int size();
		Type &operator[](int);
		void resize(int);
		void reserve(int);
		vector();
		vector(const vector&);
		~vector();
	private:
		int s;
		int* box;
		int capa;
	};
	
	template<typename Type>
	vector<Type>::vector()
	{
		box = NULL;
		s = 0;
		capa = 0;
	}
	
	template<typename Type>
	vector<Type>::vector(const vector &X)
	{
		s = X.s;
		capa = X.capa;
		box = new int[X.s];
		for(int i=0; i<X.s; i++)box[i] = X.box[i];
	}
	
	template<typename Type>
	vector<Type>::~vector()
	{
		if(box)delete[] box;
	}
	
	template<typename Type>
	Type &vector<Type>::operator[](int i)
	{
		return box[i];
	}
	
	template<typename Type>
	int vector<Type>::size()
	{
		return s;
	}
	
	template<typename Type>
	void vector<Type>::resize(int input)
	{	
		if(capa < input)reserve(input);
		s = input;
	}
	
	template<typename Type>
	void vector<Type>::reserve(int input)
	{	
		if(input <= capa)return;
		
		int* box2 = box;
		box = new int[input];
		for(int i=0; i<s; i++)box[i] = box2[i];
		delete[] box2;
		capa = input;
	}
	
	template<typename Type>
	void vector<Type>::push_back(Type input)
	{
		if(s+1 > capa){
			reserve(s * 2);
		}
		
		resize(s+1);	
		box[s] = input;
	}
}//??????????????????myspac

using namespace myspace;
using namespace std;


void Output(vector<int> A)
{
    int i;
    for(i=0;i<A.size()-1;i++){
        cout << A[i] << " ";
    }
    cout << A[i] << endl;
}
 
void InsertionSort(vector<int> A,int n)
{
    for(int i=0;i<n;i++)
    {
        int v = A[i];
        int j = i - 1;
        while(j>=0 && A[j]>v){
            A[j+1] = A[j];
            j--;
        }
        A[j+1] = v;
        Output(A);
    }
}
 
int main()
{   
    int n;
    cin >> n;
     
    vector<int> A;
	A.resize(n);
	for(int i=0;i<n;i++){
		int a;
		cin >> A[i];
	}
    InsertionSort(A, n);
     
    return 0;
}

Measured memory_bytes: 3076
#include <cstdint>
#include <iostream>

void Call_A_CirclePond(std::istream &input_stream);

void Call_A_CirclePond(std::istream &input_stream) {
  input_stream.tie(0);
  std::ios::sync_with_stdio(false);
  try {
    double input;
    input_stream >> input;
    std::cout << (6.28318530717958623200 * input);
  } catch (...) {
    std::cerr << "ERROR: CallQuickSort()" << std::endl;
    throw;
  }
}

int main() {
  Call_A_CirclePond(std::cin);
  return 0;
}


Measured memory_bytes: 3840
x,k,d = map(int,input().split())
x = abs(x)
if x - k*d > 0:
    print(x-k*d)
else:
    rest = k - x//d
    if rest%2==0:
        print(x-d*(x//d))
    else:
        print(abs(x-d*(x//d+1)))

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

int main(){
	int N;
	int a[110];
	cin>>N;
	for(int i=0;i<N;i++)cin>>a[i];
	sort(a,a+N);
	reverse(a,a+N);
	int sum_a=0,sum_b=0;
	for(int i=0;i<N;i++){
		if(i%2==0){sum_a+=a[i];}
		else{
			sum_b+=a[i];
		}
	}
	cout<<sum_a-sum_b<<endl;

	return 0;
}

Measured memory_bytes: 256
import bisect
import heapq
import itertools
import math
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
A, B = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(M)]))

graph = np.zeros((N, N), dtype=bool)
for a, b in zip(A, B):
    graph[a - 1][b - 1] = True
    graph[b - 1][a - 1] = True

cnt = 0
for order in itertools.permutations(range(1, N)):
    ok = graph[0][order[0]]
    for v, u in zip(order[:-1], order[1:]):
        ok &= graph[v][u]
    cnt += ok
print(cnt)


Measured memory_bytes: 13656
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
typedef long  l;
typedef pair<int,int> P;
const int INF=1000000009;
const double PI=3.141592653589;

int bitCount(int bits) {
    int cnt = 0;
    for(int mask = 1; mask != 0; mask<<=1) {
        if( (bits & mask) != 0 )
            ++cnt;
    }
    return cnt;
}

int main(){
  int n; cin>>n;
  vector<vector<P>> x(n);
  rep(i,n){
    int a; cin>>a;
    rep(j,a){
      int b,c; cin>>b>>c;
       b--;
      x[i].push_back({b,c});
    }
  }

  int ans=0;
  for(int bit=0;bit<(1<<n);bit++){
    bool z=1;
    // cout<<bitset<8>(bit)<<endl;
    rep(i,n){
      rep(j,x[i].size()){
        int b=x[i][j].first;
        int c=x[i][j].second;
        int d=(1<<b)&bit;
        int f=(1<<i)&bit;
        if(f>0){
          // cout<<"d "<<endl;
          // cout<<bitset<8>(d)<<endl;
          if(d>0&&c==0||d==0&&c==1)
          z=0;
          // if(c)
          // cout<<"Y"<<endl;
          // else
          // cout<<"z"<<endl;
        }
      }
    }
    if(z){
      // cout<<"kotae "<<bitset<8>(bit)<<endl;
      ans=max(ans,bitCount(bit));
      // cout<<ans<<endl;
    }
  }
  cout<<ans<<endl;
  // rep(i,n){
  //   rep(j,x[i].size()){
  //     cout<<x[i][j].first<<" "<<x[i][j].second;
  //   }
  //   cout<<endl;
  // }


  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<utility>
#include<cstdio>
#include<set>
#include<string>
using namespace std;
#define rep(i,x,n) for(int i=x;i<n;i++)
using ll=long long;
typedef pair<int,int> P;

string ans;

void ps(int n,char a){
    for(int i=0;i<n;i++) ans+=a;
    return;
}

int sumd(int n){
    int res=0;
    while(n>0){
        res=n%10;
        n/=10;
    }
    return res;
}

int F(ll a,ll b){
    string s,t;
    s=to_string(a);
    t=to_string(b);
    return max(s.size(),t.size());
}

double dis(int x1,int y1,int x2,int y2){
    int s=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return pow(s,0.5);
}
int main(){
    string s;cin>>s;
    int n=s.size();
    ll ans=0;
    for(int bits=0;bits<(1<<(n-1));bits++){
        int i=0;
        for(int j=0;j<=n-1;j++){
            if((bits>>j)&1||j==n-1){
                ans+=stoll(s.substr(i,j+1-i));
                i=j+1;
            }
        }
    }
    cout<<ans<<endl;
}    

Measured memory_bytes: 760
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <functional>
#include <string>
#include <sstream>
#include <complex>
#include <vector>	// V[i],  push_back(x), pop_back(), insert(index, x), erase(index)	
#include <list>		// push_front(x), push_back(x), pop_front(), pop_back(), insert(index, x)
#include <queue>	// push(x), front(), pop()
#include <deque>
#include <stack>	// push(x), top(), pop()
#include <map>		// M[key],  insert(key, val), erase(key), find(key), 
#include <set>		// insert(key), erase(key), find(key)

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

#define pi 3.141592653589793)
#define mod 1000000007

int N, A[100002], B[100002], C[100002];

void solve() {
	cin >> N;
	for (int i = 0; i < N; i++) cin >> A[i];
	for (int i = 0; i < N; i++) cin >> B[i];
	for (int i = 0; i < N; i++) cin >> C[i];

	sort(A, A + N);
	sort(B, B + N);
	sort(C, C + N);

	long long ans = 0;
	for (int i = 0; i < N; i++) {
		ans += (lower_bound(A, A + N, B[i]) - A) * (C + N - upper_bound(C, C + N, B[i]));
	}
	cout << ans << endl;
}

int main() {
	solve();
	return 0;
}

Measured memory_bytes: 1408
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
struct tile
{
	int c[4];
	tile () {}
	tile (int c0,int c1,int c2,int c3)
	{
		c[0]=c0;
		c[1]=c1;
		c[2]=c2;
		c[3]=c3;
		work();
	}
	int way()
	{
		int ret=1;
		if(c[0]==c[2]&&c[1]==c[3])
		{
			ret*=2;
			if(c[0]==c[1])
				ret*=2;
		}
		return ret;
	}
	bool operator<(const tile &t)const
	{
		for(int i=0;i<4;i++)
			if(c[i]!=t.c[i])
				return c[i]<t.c[i];
		return false;
	}
	void work()
	{
		int cc[4],h;
		bool ok[4];
		for(int i=0;i<4;i++)
		{
			cc[i]=c[i];
			ok[i]=true;
		}
		for(int i=0;i<4;i++)
		{
			h=1000;
			for(int j=0;j<4;j++)
				if(ok[j]&&cc[(j+i)&3]<h)
					h=cc[(j+i)&3];
			for(int j=0;j<4;j++)
				if(cc[(j+i)&3]!=h)
					ok[j]=false;
		}
		for(int i=0;i<4;i++)
			if(ok[i])
			{
				for(int j=0;j<4;j++)
					c[j]=cc[(j+i)&3];
				break;
			}
	}
}t[404];
map <tile,int> cnt;
tile tt[10];
int tn;
int main()
{
	int n;
	long long ans=0,now=0;
	tile nd;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<4;j++)
			scanf("%d",&t[i].c[j]);
		t[i].work();
		cnt[t[i]]++;
	}
	for(int i=1;i<=n;i++)
	{
		cnt[t[i]]--;
		for(int j=i+1;j<=n;j++)
		{
			cnt[t[j]]--;
			for(int k=0;k<4;k++)
			{
				tn=0;
				now=1;
				for(int kk=7;kk>=4&&now;kk--)
				{
					nd=tile(t[i].c[(7-kk)&3],t[j].c[(kk+k)&3],t[j].c[(kk-1+k)&3],t[i].c[(8-kk)&3]);
					if(cnt[nd])
					{
						now*=cnt[nd];
						now*=nd.way();
						tt[++tn]=nd;
						cnt[nd]--;
					}
					else
						now=0;
				}
				ans+=now;
				for(int kk=1;kk<=tn;kk++)
					cnt[tt[kk]]++;
			}
			cnt[t[j]]++;
		}
		cnt[t[i]]++;
	}
	printf("%lld",ans/3);
	return 0;
}

Measured memory_bytes: 20224
_n=gets.to_i
h=Hash.new(0)
res=0
a=gets.split.each_with_object([]) do |e, m|
  i = e.to_i
  m << i
  res+=i
  h[i]+=1
end
q=gets.to_i
q.times do
  b,c = gets.split.map &:to_i 
  if h.key?(b)
    v=h[b]
    h[c]+=v
    dec = v * b
    inc = v * c
    res-=dec unless dec.nil?
    res+=inc unless inc.nil?
    h.delete(b)
  end
  puts res
end


Measured memory_bytes: 25480
#include <iostream>
#include <queue>

#define inRange(x, y, w, h) (0 <= x && x < w && 0 <= y && y < h)

using namespace std;

struct Point {
	int x;
	int y;
};

int main( void )
{
	int dx[] = { 0, 0, -1, 1 };
	int dy[] = { -1, 1, 0, 0 };
	int w, h;
	int map[60][60];
	while(cin >> w >> h, w | h) {
		w = 2 * w - 1; h = 2 * h - 1;
		for(int y=0; y < h; ++y)
			for(int x=0; x < w; ++x) map[y][x] = 9999;
		for(int y=0; y < h; ++y) {
			int input;
			for(int x=(y+1)%2; x < w; x+=2) { cin >> input; map[y][x] = (input)? 0 : 9999; }
		}
		for(int y=1; y < h; y+=2)
			for(int x=1; x < w; x+=2) {
				for(int d=0; d < 4; ++d) {
					int mx = x + dx[d];
					int my = y + dy[d];
					if(inRange(mx, my, w, h) && !map[my][mx]) {
						map[y][x] = 0;
						break;
					}
				}
			}

		Point tmp;
		queue<Point> q;
		tmp.x = tmp.y = 0; map[0][0] = 1;
		q.push(tmp);
		while(!q.empty()) {
			tmp = q.front(); q.pop();
			for(int d=0; d < 4; ++d) {
				int mx = tmp.x + dx[d];
				int my = tmp.y + dy[d];
				if(inRange(mx, my, w, h) && map[tmp.y][tmp.x] + 1 < map[my][mx]) {
					Point tmp2;
					tmp2.x = mx;
					tmp2.y = my;
					map[my][mx] = map[tmp.y][tmp.x] + 1;
					q.push(tmp2);
				}
			}
		}
		if(map[h-1][w-1] != 9999)
			cout << (map[h-1][w-1] + 1) / 2 << endl;
		else
			cout << 0 << endl;
		
	}
	return 0;
}

Measured memory_bytes: 924
/****************************************************
*   Template for coding contests                    *
*   Author    :    Sanjeev Sharma                   *
*   Email     :    thedevelopersanjeev@gmail.com    *
*****************************************************/
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC target ("sse4")
#pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

#define deb(x) cout << #x << " is " << x << "\n";
#define int long long
#define mod 1000000007

const double PI = 2 * acos(0.0);
const long long INF = 1e18L + 5;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve() {
	long double a, b, h, m;
	cin >> a >> b >> h >> m;
	long double theta = (30.0 * h) - ((11.0 * m) / 2.0);
	cout << fixed << setprecision(20) << sqrt((a * a) + (b * b) - (2 * a * b * cos((PI * theta) / 180)));
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}


Measured memory_bytes: 3860
<?php
fscanf(STDIN, "%d %d", $N, $M);
$A = array_map('intval' , explode(" ", trim(fgets(STDIN))));
rsort($A);
 
while($M > 0) {
    $base = $A[0] / 2;
    foreach ($A as $k => $v) {
        if ($base <= $v) {
            if ($M > 0) {
                $A[$k] /= 2;
                $M--;
            } else {
                break;
            }
        }
    }
    rsort($A);
}
 
$ans = 0;
foreach ($A as $v) {
    $ans += intval($v);
}
echo $ans;

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

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
double const PI = 3.1415926535897932384626433;
using P = pair<int, int>;

int main() {
    int n;
    cin >> n;
    cout << 800 * n - 200 * (n / 15) << endl;
    return 0;
}

Measured memory_bytes: 3548
//****************************//
/**********  main *************/
//****************************//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
 
using namespace std;
typedef long long ll;
 
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,a,b) for(i=a;i<b;i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
 
 
int main() {
 
	std::cin.tie(0);
 
    int k,n;
    cin >> k >> n;

    int i,tmp,res,ttmp,t_res;
    int p_res=0,n_res=0;
    int ma,mi;

    vector<int> v(n);
    vector<int> dv(n);
    vector<int> tv(n);

    for(i=0;i<n;i++){
        cin >> v[i]; 
        dv[i]=v[i]+k;
        tv[i]=v[i]+2*k;
    }
    std::copy(dv.begin(),dv.end(),std::back_inserter(v));
    std::copy(tv.begin(),tv.end(),std::back_inserter(v));

    res = 10000000;
    t_res = 10000000;
    for(i=0;i<n;i++){
        tmp = v[i+n+(n-1)] - v[i+n];
        ttmp = v[i+n]-v[i+1];
        t_res = min(tmp,ttmp);
        res = min(t_res,res);
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 6512
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    K,T=map(int,input().split())
    A=list(map(int,input().split()))

    mv=max(A)
    mi=A.index(mv)

    rem=sum(A)-mv
    print(max(0,mv-1-rem))
    

    

    
resolve()

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

namespace ABC070
{
    public class QuestionB
    {
        public static void Main(string[] args)
        {
            var abcd = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var a = abcd[0];
            var b = abcd[1];
            var c = abcd[2];
            var d = abcd[3];

            var lower = Math.Max(a, c);
            var upper = Math.Min(b, d);
            var result = Math.Max(0, upper - lower);
            Console.WriteLine(result);
        }
    }
}


Measured memory_bytes: 28204
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);            //fast input and output
	cin.tie(NULL);
	cout.tie(NULL);
	int n,prev[3],neww[3],best=0;
	cin>>n;

	for(int i=0;i<3;i++)
	{
		cin>>prev[i];
		best=max(best,prev[i]);
	}

	for(int i=1;i<n;i++)
	{
		for(int j=0;j<3;j++)
		{
			int a;	
			cin>>a;
			neww[j]=0;
			for(int k=0;k<3;k++)
			{
				if(j==k)
					continue;
				neww[j]=max(neww[j],a+prev[k]);
				best=max(best,neww[j]);
			}
		}
		for(int j=0;j<3;j++)
			prev[j]=neww[j];
	}
    cout<<best;
    return 0;
}

Measured memory_bytes: 3552
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int a[3];
    for(int i = 0; i < 3; i++)cin >> a[i];
    sort(a, a + 3);
    if(a[0] == 5 && a[1] == 5 && a[2] == 7)cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}

Measured memory_bytes: 896
def inv(bit,m)
    bs = bit.to_s(2)
    if bs.length < m
        bs = "0" * (m - bs.length) + bs
    end
    ret = ""
    bs.each_char do |c|
        ret << (c == "0" ? "1" : "0")
    end
    ret.to_i(2)
end
conv = ('0'..'9').to_a + ('A'..'Z').to_a
loop do
    n, m, q = gets.split.map(&:to_i)
    break if n == 0 && m == 0 && q == 0
    cor = Array.new(m,2**n-1)
    prev = 0
    q.times do
        input = gets.chomp.split
        s = input[0].to_i(2)
        b = prev ^ input[1].to_i(2)
        bs = b.to_s(2)
        if bs.length < m
            bs = "0" * (m - bs.length) + bs
        end
        inv = inv(s,n)
        bs.each_char.with_index do |bit,i|
            if bit == "1"
                cor[i] &= s
            else
                cor[i] &= inv
            end
        end
        prev = input[1].to_i(2)
    end
    ret = ""
    cor.each do |c|
        if c & (c-1) != 0
            ret << "?"
        else
            t = n - 1 - Math.log2(c).to_i
            ret << conv[t]
        end
    end
    puts ret
end

Measured memory_bytes: 6208
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

from bisect import bisect
from math import factorial
def rank(l):
  r = 0
  nums = [_ for _ in range(1,n+1)]
  for i in range(n):
    r += bisect(nums, l[i]) * factorial(len(nums)-1)
    nums.remove(l[i])
  return r
print(abs(rank(p)-rank(q)))

Measured memory_bytes: 3064
n, x = map(int, input().split())
p = [1] * -~n
l = [1] * -~n
for i in range(1, n + 1):
    p[i] = p[i - 1] * 2 + 1
    l[i] = l[i - 1] * 2 + 3

def rec(m, y):
    if y == 0:
        return 0
    if m == 0:
        return 1
    h = l[m] // 2
    if h < y:
        return 1 + p[m - 1] + rec(m - 1, y - h - 1)
    else:
        return rec(m - 1, y - 1)
print(rec(n, x))


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

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

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF 1001001001

int main(int, char**)
{
  int n;
  cin >> n;

  vector<P> times;
  rep(i,n) {
    int s, t;
    cin >> s >> t;
    times.emplace_back(s,t);
  }
  sort(times.begin(), times.end(), [&](P a, P b)->bool{ return a.second < b.second; });
  int ans = 0;
  int now = 0;
  rep(i,n) {
    int s, t;
    tie(s,t) = times[i];
    if (now < s) { ++ans; now = t; }
  }
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 3800
//2020-06-03 
#include <bits/stdc++.h>
using namespace std;
#define loop(v, st, en) for (int v = st; v < en; v++)
#define loopdesc(v, st, en) for (int v = st; v >= en; v--)
#define INF 1E9
#define clr(v, d) memset(v, d, sizeof(v))
#define pb push_back
#define MP make_pair
#define P(x) cout << #x  <<" = { "<< x<<" }\n"
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
const int M=2e5+5;



int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input.txt", "r", stdin);
    int n ; cin>>n;
    n= n%10;
    if(n==2||n==4||n==5||n==7|n==9)cout <<"hon\n";
    else if(n==3)cout<<"bon\n";
    else cout<<"pon\n";
    return 0;
}

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

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define all(v)                                    v.begin(),v.end()

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX;
const long long INF = LLONG_MAX;

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
#define rtn                                       if(0)return;
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{	rtn
	const char* comma = strchr (names + 1, ',');
	cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}


void solve()
{
	int n, k;
	cin >> n >> k;
	int p[n + 1], c[n + 1];
	lop(i, 1, n + 1) cin >> p[i];
	lop(i, 1, n + 1) cin >> c[i];

	int ans = -INF;
	lop(i, 1, n + 1)
	{
		vector<bool> vis(n + 1, 0);

		int nx = p[i];
		int score = 0, rem = k, len = 0, res = -INF;

		while ( rem > 0 and !vis[nx] )
		{
			rem--;
			vis[nx] = 1;
			score += c[nx];
			nx = p[nx];
			len++;
			res = max(res, score);
		}

		// bug(i, res, score, rem, len);
		ans = max(ans, res);

		if ( !vis[nx] ) ans = max(ans, res);
		else
		{
			// bug('h', res, score, rem);
			if ( score < 0 or rem == 0) ans =  max(ans, res);
			else
			{
				int cycle = (rem / len);
				int total = score + score * (max(0ll, cycle - 1));


				int y =  (rem % len);
				if ( cycle) y += len;

				nx = p[i];
				int temp = 0, mx = 0;
				while ( y > 0 )
				{
					temp += c[nx];
					y--;
					nx = p[nx];
					mx = max(temp, mx);
				}

				total += mx;
				ans = max(ans, total);
			}
		}
	}

	cout << ans << endl;

}

int32_t main()
{
	ios_base::sync_with_stdio(0);   cin.tie(0);   cout.tie(0);

	int test = 1;
	// cin >> test;
	while (test--)solve();


}





















Measured memory_bytes: 3692
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <numeric>
#include <iomanip>

int main(void) {
	long long n;

	std::cin >> n;

	long long max = 0;
	for (long long i = 1; i < 100; i++) {
		long long tmp = i;
		for (size_t j = 2; j < 100; j++) {
			tmp *= i;
			if (tmp <= n) {
				max = std::max(max, tmp);
			}
		}
	}

	std::cout << max << std::endl;

	return 0;
}

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

long long int gcd(long long int a, long long int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

long long int lcm(long long int a, long long int b) {
    return a / gcd(a,b) * b;// divide first to avoid overflow
}

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

    long long int c_div = b/c - (a-1)/c;
    long long int d_div = b/d - (a-1)/d;
    long long int cd_div = b/lcm(c,d) - (a-1)/lcm(c,d);

    cout << (b - a + 1) - (c_div + d_div - cd_div) << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#define ll long long
#define mod 998244353
int main(){
  ll d[100001],cnt[100001]={0},n,i,max=0,ans=1;
  scanf("%lld",&n);
  for(i=0;i<n;i++){
  	scanf("%lld",&d[i]);
    cnt[d[i]]++;
    max=max<d[i]?d[i]:max;
  }
  if(d[0]!=0){
   	printf("0");
    return 0;
  }
  for(i=1;i<n;i++){
  	if(d[i]==0){
      ans=0;
      break;
    }else{
      //printf("%lld %lld\n",d[i],cnt[d[i]-1]);
    	ans=ans*cnt[d[i]-1]%mod;
    }
  }
  printf("%lld",ans);
  return 0;
}

Measured memory_bytes: 1664
N=int(input())
def make_divisors(n): 
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

cnt=0
for i in range(1, N+1):
    divsors = make_divisors(i)
    if i % 2 != 0 and len(divsors) == 8:
        cnt += 1

print(cnt)

Measured memory_bytes: 3064
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] dp = new int[3];
		for(int j=0; j<3; j++) {
			dp[j] = sc.nextInt();
		}
		for(int i=1; i<n; i++) {
			int[] M = new int[3];
			for(int j=0; j<3; j++) {
				int curr = sc.nextInt();
				for(int k=0; k<3; k++) {
					if(j!=k) {
						M[j] = Math.max(M[j], dp[k]+curr);
					}
				}
			}
			dp = M;
		}
		System.out.println(Math.max(dp[0], Math.max(dp[1], dp[2])));
		
		
		

	}

}


Measured memory_bytes: 49012
#include <bits/stdc++.h>
#define ll long long
#define pq priority_queue
using namespace std;
const ll INF=(ll)1e9;
const ll MOD=(ll)1e9+7;
const ll MAX=510000;
vector<int> dx={1,0,-1,0},dy={0,1,0,-1};
template<class T> inline bool chmax(T& a, T b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a>b) { a=b; return 1; } return 0; }

int main(){
    ll N,d=0,sum=0;
    cin>>N;
    ll A[N];
    map<ll,ll> m;
    for(ll i=0;i<N;i++){
        cin>>A[i];
        m[A[i]]++;
    }
    for(auto a:m) d+=a.second-1;
    if(d==N) cout<<"1"<<endl;
    else cout<<N-d-d%2<<endl;
}

Measured memory_bytes: 7296
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
  set<int>s;
  for(int i=0;i<n;i++){
int temp;
    cin>>temp;
  s.insert(temp);
  }
if(n==s.size())
  cout<<"YES";
  else
    cout<<"NO";
  
}

Measured memory_bytes: 9600
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define RREP(i, n) for(int (i)=(n)-1, (i)>=0; (i)--)
#define ios() cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
ll const MOD = 1e9+7;
ll const INF = 1e18;
using namespace std;

int f(int n){
    if(n%2 == 0){
        return n / 2;
    }else{
        return 3*n + 1;
    }
}

int main(){
    int a;
    cin >> a;
    int i = 1;
    set<int> st;
    while(1){
        if(st.find(a) != st.end()){
            cout << i << endl;
            return 0;
        }
        st.insert(a);
        a = f(a);
        i++;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <fstream>

#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>

#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>

#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000;/* 1e+9a */


void solve(istream &cin) {
    const int match[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
    ll N, M;
    cin >> N >> M;
    if(!cin) return;
    vector<int> can;
    vector<pair<ll, ll>> dp(N+1, {-1, -1});
    for (int i = 0; i < M; ++i) {
        int A;
        cin >> A;
        can.push_back(A);
        if(match[A] < dp.size()){
            dp[match[A]] = max(dp[match[A]], {0, A});
        }
    } // end i

    for (int i = 0; i < dp.size(); ++i) {
        if(dp[i].first != -1){
            for (ll k = 0; k < can.size(); ++k) {
                int sc = can[k];
                if(i + match[sc] < dp.size()){
                    pair<ll, ll> x = dp[i];
                    x.first += 1;
                    x.second = sc;
                    dp[match[sc] + i] =
                            max(dp[match[sc] + i], x);
                }
            } // end k
        }
    } // end i

    int x = dp[N].first;
    while(N > 0){
        auto s = dp[N];
        assert(s.first != -1);
        assert(x == s.first);
        x--;
        cout << s.second;
        N -= match[s.second];
    }
    assert(x == -1);
    cout << endl;
}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef DEBUG
    cerr << "DEBUG MODE" << endl;
    string test_cases = "test.txt";
    auto fs = fstream(test_cases, fstream::in);
    while(fs) {
        solve(fs);
    }
#else
    solve(cin);
#endif
    return 0;
}


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

int main(){
        int N,K;
        int *h,*l;

        cin >> N >> K;
        h = new int[N];
        l = new int[N];
        for (int i=0; i<N; i++){
                cin >> h[i];
        }
        sort(h,h+N);
        for (int i=K-1; i<N; i++){
                l[i] = h[i] - h[i-K+1];
        }
        sort(l+K-1,l+N);
        cout << l[K-1] << endl;
        return 0;
}


Measured memory_bytes: 1024