akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <algorithm>
#include <tuple>
#include <stdio.h>
#include <bitset>
#include <limits.h>
#include <complex>
#include <deque>
#include <iomanip>
#include <list>
#include <cstring>
using namespace std;
#define pi pair<int,int>
#define pl pair<long long,long long>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)
#define en cout << endl //セミコロンつけろ
//#define MM 1000000000
//#define MOD MM+7
typedef long long ll;
const int MM = 1e9;
const int MOD = MM+7;
const long double PI = acos(-1);
const long long INF = 1e15;
int dx[8] = {-1,0,1,0,-1,-1,1,1};
int dy[8] = {0,-1,0,1,-1,1,1,-1};
// 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122

template<typename T> //最大公約数
T GCD(T u,T v){
    return v ? GCD(v,u%v) : u;
}
template<typename T> //最小公倍数
T LCM(T x,T y){
    T gc = GCD(x,y);
    return x*y/gc;
}

struct Edge
{
    int to, cost;
};
struct Node
{
    int from, to, cost;
};
vector<vector<int> > g(0), revG(0);
vector<int> order(0), marker(0);

void dfs(int num, int count){
    queue<int> que;
    que.push(num);
    while (que.size()){
        int now = que.front();
        marker[now] = count;
        que.pop();
        for (int i = 0; i < g[now].size(); i++){
            int to = g[now][i];
            if (marker[to] == 0){
                que.push(to);
            }
        }
    }
}

int main(){
    int N,M; cin >> N >> M;
    g.resize(N);
    marker.assign(N, 0);
    for (int i = 0; i < M; i++){
        int s, t; cin >> s >> t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    int Q; cin >> Q;
    vector<int> uu(Q), vv(Q);
    for (int i = 0; i < Q; i++){
        int u, v; cin >> u >> v;
        uu[i] = u; 
        vv[i] = v;
    }

    int count = 1;
    for (int i = 0; i < N; i++){
        if (marker[i] == 0){
            dfs(i, count);
            count++;
        }
    }
    for (int i = 0; i < Q; i++){
        if (marker[uu[i]] == marker[vv[i]]){
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}


Measured memory_bytes: 8916
from collections import defaultdict
 
n = int(input())
S = [input() for _ in range(n)]

d = defaultdict(lambda: -1)
for i in range(n):
    s = ''.join(sorted(list(S[i])))
    d[s] += 1
 
ans = 0
for v in d.values():
    ans += v*(v+1)//2
print(ans)

Measured memory_bytes: 25320
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<int, P> Pint;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k>0){
        if(k%2==1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k/=2;
    }
    return ans;
}
int main()
{
	
	int n;
  ll h[101];
  vector<ll> hs;
  cin>>n;
  for(int i=1; i<=n; i++){
    cin>>h[i]; hs.push_back(h[i]);
  }
  sort(hs.begin(), hs.end());
  hs.erase(unique(hs.begin(), hs.end()), hs.end());
  ll dp[2][100][100]={};
  vector<Pint> v;
  for(int i=hs.size()-1; i>=0; i--){
    bool nuo[102]={}, nuo2[102]={};
    for(int j=1; j<=n; j++){
      if(hs[i]<=h[j]) nuo[j]=1;
    }
    int l;
    for(int j=0; j<=n; j++){
      if(!nuo[j] && nuo[j+1]) l=j;
      if(nuo[j] && !nuo[j+1]){
        bool ok=0;
        for(int k=l+1; k<=j; k++){
          if(hs[i]==h[k]){
            ok=1;
            break;
          }
        }
        if(ok) v.push_back(Pint(hs[i], P(l+1, j)));
      }
    }
  }
  bool used[100]={};
  for(int i=0; i<v.size(); i++){
    int l=v[i].second.first, r=v[i].second.second, d0=v[i].first;
    //cout<<l<<" "<<r<<" "<<d0<<endl;
    bool no=1;
    dp[1][l][r]=2;
    ll prod=1, prod1=1;
    int ct=0;
    bool nuo=0;
    int ct2=0;
    for(int j=0; j<i; j++){
      int l1=v[j].second.first, r1=v[j].second.second;
      if(l>l1 || r1>r) continue;
      if(used[j]) continue;
      used[j]=1;
      no=0;
      if(r1!=l1) nuo=1;
      int d1=v[j].first;
      dp[1][l][r]*=(dp[1][l1][r1]*powmod(2ll, d1-d0-1)%MOD);
      dp[1][l][r]%=MOD;
      prod*=(dp[0][l1][r1]+dp[1][l1][r1]*powmod(2ll, d1-d0)%MOD);
      prod%=MOD;
      prod1*=(dp[1][l1][r1]*powmod(2ll, d1-d0)%MOD);
      prod1%=MOD;
      ct+=(r1-l1+1);
      ct2++;
    }
    if(no){
      dp[0][l][r]=(powmod(2ll, r-l+1)-2+MOD)%MOD;
    }else{
      if(nuo) dp[0][l][r]=((prod-prod1+MOD)*powmod(2ll, r-l+1-ct)%MOD+dp[1][l][r]*500000004%MOD*(powmod(2ll, r-l+1-ct+ct2)+MOD-2)%MOD)%MOD;
      else dp[0][l][r]=dp[1][l][r]*500000004%MOD*(powmod(2ll, r-l+1)+MOD-2)%MOD;
    }
    if(r-l+1==n){
      ll ans=dp[0][l][r]+dp[1][l][r]*powmod(2ll, d0-1)%MOD;
      ans%=MOD;
      cout<<ans<<endl;
    }
  }
	return 0;
}

Measured memory_bytes: 384
def main():
    S = input()
    T = input()

    for i in range(len(S)-1):
        if i == 0:
            if S == T:
                print('Yes')
                return
        S = S[-1]+S[:-1]
        if S == T:
            print('Yes')
            return
    print('No')



if __name__ == '__main__':
    main()


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

int main(){
  int n;
  cin >> n;
  map<string, int> mp;
  int ans=0;
  for(int i=0; i<n; ++i){
    string s;
    cin >> s;
    ++mp[s];
    ans = max(ans,mp[s]);
  }
  for(a:mp){
    if(a.second == ans) cout << a.first << endl;
  }
}

Measured memory_bytes: 25216
#include <bits/stdc++.h>
#define sort stable_sort
using namespace std; 

int _;

int n,a[200050];
long long l,r,ans=1e18;

int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",a+i);
		r += a[i];
	}
	for(int i=0;i<n-1;i++)
	{
		l += a[i];
		r -= a[i];
		ans=min(ans,abs(l-r));
	}
	printf("%lld\n",ans);
	return 0;
}



Measured memory_bytes: 4428
using System;

public class Pencils {
    public static void Main(){
        string[] line = Console.ReadLine().Split(' ');
        int N = int.Parse(line[0]);
        int A = int.Parse(line[1]);
        int B = int.Parse(line[2]);
        int C = int.Parse(line[3]);
        int D = int.Parse(line[4]);
        
        int buyNum1, buyNum2;
        if (N % A == 0)
        {
            buyNum1 = N/A;
        }
        else
        {
            buyNum1 = N/A+1;
        }
        if (N % C == 0)
        {
            buyNum2 = N/C;
        }
        else
        {
            buyNum2 = N/C+1;
        }
        Console.WriteLine(Math.Min(buyNum1*B, buyNum2*D));
    }
}



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

int main() {
  int N,K;
  cin >> N >> K;
  int sum = 0;
  int x;
  for(int i=0;i<N;i++){
    cin >> x;
    if(x < K-x) sum += x*2;
    else sum += (K-x)*2;
  }
  cout << sum << endl;
}


Measured memory_bytes: 256
/**********************************************************************************

https://atcoder.jp/contests/abc144/tasks/abc144_b





*************************************************************************************/

#include<bits/stdc++.h>

using namespace std;

/*****************MACRO*****/

#define input freopen("in.txt","r",stdin)
#define output freopen("out.txt","w",stdout)

#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define count_one(a) __builtin_popcount(a)  // Returns the number of set bits(1) in a number(a). In long long use __builtin_popcountll(a)
#define parity(i)   __builtin_parity(i)  //even parity 0 and odd parity 1
#define blz(a)   __builtin_clz(a) //Returns the number of leading zeroes in a number(a)
#define btz(a)   __builtin_ctz(a) //Returns the number of trailing zeroes in a number(a)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))

#define ll long long
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

/*******CONSTANT***********/
#define INF (1<<30) //infinity value
#define EPS 1e-9
#define MOD 10000007
#define SIZ 1005
#define PI (acos(-1))

void solve()
{
    bool a[82];
    for(int i=1; i<=81; i++){
        a[i]= false;
    }
    for(int i=1; i<=9; i++)
        for(int j=1; j<=9; j++)
            a[i*j]= a[i*j]|true;
//    for(int i=1; i<=81; i++){
//        cout<<"a["<<i<<"]="<<a[i]<<"\n";
//    }
    int n;
    cin>>n;
    cout<<(a[n]?"Yes":"No")<<"\n";
}





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

    int tc,cn;


    //cin>>tc;
    tc=1;
    for(cn=1;cn<=tc;cn++)
    {

        ///cout<<"Case "<<cn<<": "<<ans<<endl;
        solve();

    }

    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const long long mod = 1e9 + 7;

ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}

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

ll powMod(ll n, ll k, ll m) {
	if (k == 0) {
		return 1;
	} else if (k % 2 == 1) {
		return powMod(n, k - 1, m) * n % m;
	} else {
		ll t = powMod(n, k / 2, m);
		return t * t % m;
	}
}

int main() {
	string s, t;
	cin >> s >> t;

	t = t.substr(0, t.size()-1);

	if (s == t) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}

}


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

void solve()
{
	ll n;
	cin>>n;
	ll a[n];
	for (ll i = 0; i < n; ++i)
	{
		/* code */
		cin>>a[i];
	}
	ll m = 1000;
	ll pre = a[0];
	ll maxi = 1000;
	bool f = true;
	ll k;
	for (ll i = 1; i < n; ++i)
	{
		if(f)
		{
			if(a[i]<pre)
				pre = a[i];
			else
			{
				k = m/pre;
				m -= pre*k;
				f = false;
				pre = a[i];
			}
		}
		else
		{
			if(a[i]>pre)
				pre = a[i];
			else
			{
				m += k*pre;
				f = true;
				pre = a[i];
				maxi = max(maxi,m);
			}
		}
	}
	if(!f)
	{
		m += k*pre;
		maxi = max(maxi,m);
	}
	cout<<maxi<<endl;
}

int main()
{
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
}

Measured memory_bytes: 3632
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.StringTokenizer;

class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(in, out);
        out.close();
    }

    static class Task {
        void solve(InputReader in, PrintWriter out) {
            // write your code here
            int N = in.nextInt();
            int K = in.nextInt();
            int[] A = in.nextIntArray(N);

            int ans = (N - 1) % (K - 1) == 0 ?
                    (N - 1) / (K - 1) : (N - 1) / (K - 1) + 1;
            System.out.println(ans);
        }
    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int size) {
            int[] arr = new int[size];
            for (int i = 0; i < size; i++) {
                arr[i] = nextInt();
            }
            return arr;
        }
    }
}

Measured memory_bytes: 33872
#include<stdio.h>
#include<math.h>
int main()
{
    unsigned long long n, b, r, sum;
    scanf("%llu %llu %llu", &n, &b, &r);
    sum = r + b;
    if(b == 0)
        printf("%llu", b);
    else if(n % sum > b){
        printf("%llu", (n / sum)*b + b);
    }
    else{
        printf("%llu", (n / sum)*b + (n % sum));
    }
    return 0;


}

Measured memory_bytes: 128
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		
		TreeMap<Integer, Integer> a = new TreeMap<>();
		
		for(int i =1; i <= n; i++) {
			a.put(i, 0);
		}
		
		for(int i = 0; i < n - 1; i++) {
			int key = sc.nextInt();
			a.put(key,a.get(key)+1);
		}

		for (Integer key : a.keySet()) {
			System.out.println(a.get(key));
		}

	}
}


Measured memory_bytes: 71332
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int k = sc.nextInt();
    int answer = 0;
    int[] booksA = new int[n + 1];
    int[] booksB = new int[m + 1];
    for (int i = 1; i <= n; i++) {
      booksA[i] = sc.nextInt();
    }
    for (int i = 1; i <= m; i++) {
      booksB[i] = sc.nextInt();
    }
    int fixA = 0;
    int sumTimeA = 0;
    while (fixA < n) {
      if (sumTimeA + booksA[fixA + 1] > k) {
        break;
      } else {
        sumTimeA += booksA[fixA + 1];
        fixA++;
      }
    }
    int posB = 0;
    int sumTimeB = 0;
    for (int a = fixA; a >= 0; a--) {
      while(posB < m){
        if(sumTimeA + sumTimeB + booksB[posB + 1] > k){
          break;
        } else {
          sumTimeB += booksB[posB + 1];
          posB++;
        }
      }
      answer = Math.max(answer, a + posB);
      sumTimeA -= booksA[a];
    }
    System.out.println(answer);
  }
}


Measured memory_bytes: 62336
#include<cstdlib>
#include<iostream>
#include<time.h>
#include<vector>
using namespace std;


int reduce_ene(int day, int except_num, vector<int> &c, vector<int> &last){
    int i;
    int sum_reduce;
    sum_reduce = 0;

    for(i=0;i<26;i++){
        if(i != except_num-1){
            sum_reduce += c[i] * (day+1 - last[i]);
        }
    }
    return sum_reduce;
}

int plus_ene(int day, vector<int> &t, vector<vector <int> > &s){
    return s[day][t[day] - 1];
}


int ene_cal(int D, vector<int> &t, vector<vector <int> > &s, vector<int> &c){
    int day;
    int ene;
    int i;
    ene = 0;
    vector<int> last;
    for(i=0;i<26;i++){
        last.push_back(0);
    }
    for(day=0;day<D;day++){
        ene += plus_ene(day, t, s);  
        ene -= reduce_ene(day, t[day], c, last);
        //cout << ene << endl;
        last[t[day] - 1] = day + 1;
    }
    return ene;

}




int main(){
    clock_t start, end;
    /* init */

    int i,j;
    int D;
    vector<int> c;
    vector<int> t;

    cin >> D;
    int sum_c; 
    sum_c = 0;
    for(i=0;i<26;i++){
        int ci;
        cin >> ci;
        c.push_back(ci);
        sum_c += ci;
    }


    vector<vector <int> > s(D, vector<int>(26));

    for(j=0;j<D;j++){
        for(i=0;i<26;i++){
            int sij;
            cin >> sij;
            s.at(j).at(i) = sij;
    }}
    start = clock();

    for(i=0;i<D;i++){
        int ti;
        ti = rand() % 26 + 1;
        t.push_back(ti);
    }

    //end init
    int ene;


    ene = ene_cal(D, t, s, c);
    int ene_old;
    int t_old;
    int rand_d;
    int rand_q;
    bool flag;
    flag = true;

    while(flag == true){
        rand_d = rand() % D;
        rand_q = rand() % 26;

        t_old = t[rand_d];
        ene_old = ene;
        t[rand_d] = rand_q + 1;
        ene = ene_cal(D, t, s, c);
        if(ene < ene_old){
            t[rand_d] = t_old; 
            ene = ene_old;
        }
        end = clock();
        if(double(end - start) > 1800.00){
            flag = false;
            break;
        }

    }


    for(i=0;i<D;i++){
        cout << t[i] << endl;
    }


    return 0;
}


Measured memory_bytes: 3248
from collections import Counter

p=10**9+7
N=int(input())
A = list(map(int,input().split()))
c=Counter(A)

for item in c:
  if c[item]>2:
    print(0)
    exit()
  if item==0:
    if c[item]>1:
      print(0)
      exit()
else:
  ans = pow(2,N//2,p)
  
print(ans)

Measured memory_bytes: 14820
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include <tuple>

using namespace std;
#define ll long long
#define ld long double
#define int long long
#define all(a) (a).begin(),(a).end()
#define EPS (1e-13)
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define mkp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define ssp(i,n) (i==n-1?"\n":" ")
#define tpop(p) p.top();p.pop()
const int mod = 1e9 + 7;
const int Mod = 998244353;
const ll Inf = 3 * 1e18;
const int inf = 15 * 1e8;
ll read() {
	ll u, k = scanf("%lld", &u);
	return u;
}
ll gcd(ll i, ll j) {
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}
ll bitcount(ll x) {
	int sum = 0;
	for (int i = 0; i < 20; i++)
		if ((1ll << i) & x) sum++;
	return sum;
}
/*const int fn_ = 2000000;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++) {
			fact_[i] = fact_[i - 1] * i % Mod;
			comp_[i] = mod_pow(fact_[i], Mod - 2, Mod);
		}
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//---------------------------------------------------
int n;
int a[200000];
signed main() {
	cin >> n;
	rep(i, n) cin >> a[i];
	a[0]--;
	for (int i = 1; i < n; i++) {
		if (a[i] > a[i - 1]) {
			a[i]--;
		}
		else if (a[i] < a[i - 1]) {
			cout << "No\n";
			return 0;
		}
	}
	cout << "Yes\n";
}

Measured memory_bytes: 1024
import sys
input = sys.stdin.readline
from math import factorial
mod=10**9+7
n,m=map(int,input().split())
if n>m:
    n,m=m,n
if n+1==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(n*m%mod)
elif n==m:
    n=factorial(n)%mod
    m=factorial(m)%mod
    print(2*n*m%mod)
else:
    print(0)


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

int main() {
  int N; cin >> N;
  vector<int> b(N);
  for (int i{}; i < N; ++i)
    cin >> b[i];

  vector<int> ans;
  while (!b.empty()) {
    bool gotten{};
    for (int i = b.size()-1; i >= 0; --i) {
      if (b[i] == i+1) {
        ans.push_back(b[i]);
        b.erase(b.begin() + i);
        gotten = true;
        break;
      }
    }
    if (!gotten) {
      cout << -1 << endl;
      return 0;
    }
  }

  for (auto i{ans.rbegin()}; i != ans.rend(); ++i) {
    cout << *i << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
    int a, b, c;
    cin >> a >> b >> c;

    int t = a-b;
    c -= t;
    if (c < 0) c = 0;
    cout << c << endl;
    return 0;
}

Measured memory_bytes: 256
# ABC95 B - Bitter Alchemy
n,x = map(int,input().split())
m = [int(input()) for i in range(n)]
s = sum(m)
c1 = 0
c2 = 0
while x > s:
    x -= s
    c1 += n
    break

c2 = x//min(m)

print(c1 + c2)




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

const int M = 111;

void fail(int n) {
    cout << n << '\n';
    exit(0);
}

int main() {
    int N; cin >> N;
    vector<int> cats(M);
    stack<int> st;
    auto push = [&](int c) -> bool {
        if(cats[c]) return false;
        st.push(c);
        cats[c] = 1;
        return true;
    };
    auto pop = [&](int c) -> bool {
        if(st.empty()) return false;
        if(st.top() != c) return false;
        st.pop();
        cats[c] = 0;
        return true;
    };
    rep(i, N) {
        int c; cin >> c;
        if(c > 0) {
            if(!push(c)) fail(i + 1);
        } else {
            if(!pop(-c)) fail(i + 1);
        }
    }
    cout << "OK" << '\n';
    return 0;
}



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

int main() {
  int a, b;
  cin >> a >> b;
  
  if ((a + b) % 2 == 0) {
    cout << (a + b) / 2 << endl;
  }
  else {
    cout << (a + b) / 2 + 1 << endl;
  }
}

Measured memory_bytes: 256
[n,m] = [int(i) for i in input().split()]
data = []
ans = [-1] * n
flag = False
for _ in range(m):
    data.append([int(i) for i in input().split()])
for q in data:
    if ans[q[0]-1] == -1 or ans[q[0]-1] == q[1]:
        ans[q[0]-1] = q[1]
    else:
        flag = True
        break
if flag:
    print(-1)
elif ans == [0] or ans == [-1]:
    print(0)
elif ans[0] == 0:
    print(-1)
else:
    fa = ["0" if ans[i] == -1 else str(ans[i]) for i in range(n)]
    if fa[0] == "0":
        fa[0] = "1"
    print("".join(fa))

        

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

bool solve() {
    ll X, Y;
    cin >> X >> Y;
    return abs(X-Y) > 1;
}

int main() {
    string ans = solve() ? "Alice" : "Brown";
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int stack[11];
int sp=0;//indicate the space to put track

void push( int n ){
	stack[sp]=n;
	sp++;
}

int pop(){
	sp--;
	return stack[sp];
}

int main(){
	int n;

	while( scanf("%d", &n) != EOF ){
		if( n == 0 ){
			printf("%d\n", pop() );
		}else{
			push( n );
		}
	}

	return 0;
}

Measured memory_bytes: 600
N, X = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

x.append(X)
x.sort()

D = 0
for i in range(N):
    D = gcd(D, x[i + 1] - x[i])
print(D)


Measured memory_bytes: 14252
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<algorithm>

using namespace std;
int main()
{
	int N;
	cin >> N;
	int a[110];
	for (int i = 1; i <= N; i++)
	{
		cin >> a[i];
	}
	int max = 0;
	max = a[1];
	int num = 1;
	for (int i = 2; i <= N; i++)
	{
		if (a[i] >= max)
		{
			num++;
			max = a[i];
		}
	}
	cout << num << endl;
	//system("pause");
	return 0;
}

Measured memory_bytes: 256
a,b = map(int,input().split())

if b -a >= 2019:
    print(0)
    exit()
a %= 2019
b %= 2019

if b <= a:
    b += 2019
ans = 10 ** 10
for i in range(a,b):
    for j in range(i+1,b+1):
        ans = min(ans,(i * j) % 2019)
print(ans)


Measured memory_bytes: 3060
str=input()
k=len(str)
str=str[0:k-2]
while True:
    n=int(len(str)/2)
    a=str[:n]
    b=str[n:]
    if a==b:
        print(len(a)*2)
        break
    k=len(str)
    str=str[0:k-2]


Measured memory_bytes: 3060
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;

class Graph {
  public HashSet<Integer>[] adjacency;

  Graph(int n) {
    adjacency = new HashSet[n];
    for (int i = 0; i < n; i++) {
      adjacency[i] = new HashSet<Integer>();
    }
  }

  public void add_edge(int from, int to, boolean is_directed) {
    adjacency[from].add(to);
    if (!is_directed) {
      adjacency[to].add(from);
    }
  }
}


public class Main {

  static int n, m, ans;
  static Graph g;

  public static void main(String[] args) throws Exception {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter out = new PrintWriter(System.out);

    String[] in = br.readLine().split(" ");
    n = Integer.parseInt(in[0]);
    m = Integer.parseInt(in[1]);

    g = new Graph(n);

    for (int i = 0; i < m; i++) {
      in = br.readLine().split(" ");
      int a = Integer.parseInt(in[0]);
      int b = Integer.parseInt(in[1]);
      a--; b--;
      g.add_edge(a, b, false);
    }

    ans = 0;

    boolean[] seen = new boolean[n];

    for (int i = 0; i < n; i++) {
      if(seen[i]) continue;
      Queue<Integer> que = new ArrayDeque<>();
      que.add(i);
      int tmp = 0;

      while(!que.isEmpty()){

        int now = que.poll();
        if(seen[now]) continue;
        tmp++;
        seen[now] = true;

        for (int next : g.adjacency[now]) {
            if(seen[next]) continue;
            que.add(next);
        }
      }

      ans = Math.max(ans, tmp);
    }

    out.println(ans);
    out.flush();
  }
}


Measured memory_bytes: 106644
#include<stdio.h>

int main(void) {

	int a, b, c;

	scanf("%d %d %d", &a, &b, &c);

	if (a < b && b < c) {
		printf("Yes\n");
	}
	
	else {
		printf("No\n");
	}


	return(0);

}


Measured memory_bytes: 2040
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
  ll f=false;
  string s;
  cin>>s;
  if(s[0]=='7') f=true;
  if(s[0]=='5') f=true;
  if(s[0]=='3') f=true;
  if(f) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <map>
#include <queue>
#include <cmath>
#include <iomanip>
#include <functional>

#define MOD (1000000007)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> tup;
const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];


ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}


// テーブルを作る前処理
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;
}

map<ll,ll> prime;//素因数分解でそれぞれの素数がいくつ出てきたかを保存するmap

//O(√n)
//整列済み(mapはkeyで自動で整列される)
void prime_factorize(ll n){
    if(n<=1) return;
    ll l=sqrt(n);
   for(ll i=2;i<=(ll)(l);i++) { //
        if(n%i==0){
        prime_factorize(i);prime_factorize(ll(n/i));return;
        }
    }
    //mapでは存在しないkeyの場合も自動で構築される
    prime[n]++;return;
}


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

    ll lastA = 0, firstB = 0, lastA_firstB = 0;
    ll count = 0;
    for(int i = 0; i < N; i++) {
        string S;
        cin >> S;

        auto found = S.find("AB");
        while(found != string::npos) {
            count++;
            found = S.find("AB", found + 2);
        }

        if(S[0] == 'B' && S[S.size()-1] == 'A') lastA_firstB++;
        else if(S[0]=='B') firstB++;
        else if(S[S.size()-1] == 'A') lastA++;
    }

    // cout << count  << endl;
    // cout << firstB << "," << lastA << "," << lastA_firstB << endl;

    if(lastA_firstB >= 1) {
        count += lastA_firstB - 1;
        if(firstB > 0) {
            count += 1;
            firstB--;
        } 
        if(lastA > 0) {
            count += 1;
            lastA--;
        }
    }


    count += min(firstB, lastA);

    cout << count << endl;

    

    return 0;    
}



Measured memory_bytes: 256
/*
 * b.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

/* typedef */

/* global variables */

int gcd(int x, int y) {
  return (y == 0) ? x : gcd(y, x % y);
}

/* subroutines */

/* main */

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

  if (a < b) swap(a, b);

  int ans = gcd(a, b);
  printf("%d\n", ans);

  return 0;
}

Measured memory_bytes: 1144
package main

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

func main() {
	sc := newInputScanner(true, 1024)
	S := sc.nextStr()
	N := len(S)
	allRunes := []rune(S)
	leftPart := allRunes[0 : (N-1)/2]
	rightPart := allRunes[((N-1)/2 + 1):N]
	// fmt.Println(string(leftPart))
	// fmt.Println(string(rightPart))
	if isPalindrome(allRunes) && isPalindrome(leftPart) && isPalindrome(rightPart) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func isPalindrome(runes []rune) bool {
	length := len(runes)
	for i := 0; i < length/2; i++ {
		if runes[i] != runes[length-1-i] {
			return false
		}
	}
	return true
}

type inputScanner struct {
	*bufio.Scanner
}

func newInputScanner(enabledScanWords bool, sizeInKB int) *inputScanner {
	scanner := bufio.NewScanner(os.Stdin)
	if enabledScanWords {
		scanner.Split(bufio.ScanWords)
	}
	capacity := sizeInKB * 1024
	buf := make([]byte, capacity)
	scanner.Buffer(buf, capacity)
	return &inputScanner{
		Scanner: scanner,
	}
}

func (inputSc *inputScanner) nextInt() int {
	i, e := strconv.Atoi(inputSc.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (inputSc *inputScanner) nextUInt64() uint64 {
	i, e := strconv.ParseUint(inputSc.nextStr(), 10, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func (inputSc *inputScanner) nextStr() string {
	inputSc.Scan()
	return inputSc.Text()
}

type debuggable struct {
}

const (
	DEBUG_ON = false
)

func (d *debuggable) debug(f func()) {
	if DEBUG_ON {
		f()
	}
}


Measured memory_bytes: 640
#include<iostream>
#include<string>
#include <bits/stdc++.h>
#include<vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <math.h>
#include <iomanip>
using namespace std;
using ll = long long;

int main(){
  ll a,b,c,d;
  cin >> a >> b >> c >> d;
  if(a*c>=a*d && a*c>=b*c && a*c>=b*d){
    cout << a*c << endl;
  }else if(a*d>=a*c && a*d>=b*c && a*d>=b*d){
    cout << a*d << endl;
  }else if(b*c>=a*c && b*c>=a*d && b*c>=b*d){
    cout << b*c << endl;
  }else{
    cout << b*d << endl;
  }
}


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

#define         pb         push_back
#define         sf(x)      scanf("%d",&x)
#define         sfl(x)     scanf("%lld",&x)
#define         pf(x)      printf("%d\n",x)
#define         pfl(x)     printf("%lld\n",x)
#define         endl       '\n'
#define         pii        pair<int,int>
#define         mapii      map<int,int>
#define         mapll      map<ll,ll>
#define         mapci      map<char,int>
#define         mapcl      map<char,ll>
#define         mapsi      map<string,int>
#define         mapsl      map<string,ll>
#define         pll        pair<ll,ll>
#define         vi         vector<int>
#define         vl         vector<ll>
#define         vd         vector<double,double>
#define         all(c)     c.begin(),c.end()
#define         F          first
#define         S          second
#define         mp         make_pair
#define        ftc(x)      cerr << #x << ": " << x << " " << endl;
#define         PI         acos(-1)
//   cout<<std::setprecision(12)<<
mapll mapa;
int main()
{

    ll n,i,j,k,l,m;

    cin>>n;

    ll ar[n+10];


    for(i=1;i<=n;i++)
    {
        cin >> ar[i];
        mapa[ar[i]]++;
    }
    ll ans = 0;
//    for(i=1;i<=n;i++)
//    {
//        k = mapa[ar[i]];
//        cout<<ar[i]<<" "<<k<<endl;
//        ans += (k*(k-1))/2;
//    }
    for(auto x : mapa)
    {
        k = x.S;
       // cout<<x.F<<" "<<" "<<x.S<<endl;
        ans += (k*(k-1))/2;
    }
    for(i=1;i<=n;i++)
    {
        k = mapa[ar[i]];
        if(!k)cout<<ans<<endl;
        else
        {
            l = ans - (k*(k-1))/2;
            k--;
            l += (k*(k-1))/2;

            cout<<l<<endl;
        }
    }

}


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

using namespace std;

const int OO = 1e9;
const double EPS = 1e-9;

#define ndl cout << '\n'
#define sz(v) int(v.size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define present(a, x) (a.find(x) != a.end())
#ifdef LOCAL
#define db(...) ({cout << "> Line " << __LINE__  \
		<< ": "; _db(#__VA_ARGS__, __VA_ARGS__);})
#define RNG() rng()
#else
#define db(...) true
#define RNG() true
#endif

template<class T>
void _db(const char *dbStr, T e) {
	cout << dbStr << " = " << e << endl;
}
template<class T, class... L>
void _db(const char *dbStr, T e, L... r) {
  while(*dbStr != ',') cout << *dbStr++;
  cout << " = " << e << ','; _db(dbStr + 1, r...);
}
template<class S, class T>
ostream& operator<<(ostream& o, const map<S, T>& v) {
	o << "["; int i = 0;
	for (const pair<S, T>& pr : v) o << (!i++ ? "" : ", ") << "{"
	<< pr.fs << " : " << pr.sc << "}"; return o << "]";
}
template<template <class, class...> class S, class T, class... L>
ostream& operator<<(ostream& o, const S<T, L...>& v) {
	o << "["; int i = 0;
	for (const auto& e : v) o << (!i++ ? "" : ", ") << e;
	return o << "]";
}
template<class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& pr) {
	return o << "(" << pr.fs << ", " << pr.sc << ")";
}
ostream& operator<<(ostream& o, const string& s) {
	for (const char& c : s) o << c;
	return o;
}

template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = VV<V<T>>;
using ll = long long;
using pii = pair<int, int>;
using vi = V<int>;
using vii = V<pii>;
using vvi = VV<int>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using si = set<int>;
using usi = unordered_set<int>;

const int MX = 3005, MOD = 998244353;
int N, S, mem[MX][MX][2];
vi A;

int dp(int i, int sum, bool active) {
	if (i == N + 1)
		return 0;
	if (sum > S)
		return 0;
	int& ret = mem[i][sum][active];
	if (ret != -1)
		return ret;

	ret = 0;

	if (active) {
		ret = (dp(i + 1, sum + A[i], 1) + dp(i + 1, sum, 1)) % MOD;
		if (sum + A[i] == S)
			ret = (ret + N - i + 1) % MOD;
	} else {
		assert(sum == 0);
		ret = dp(i + 1, sum, 0);
		ret = (ret + ((dp(i + 1, sum + A[i], 1) * 1LL * i) % MOD)) % MOD;
	}

	return ret;
}



int main() {
	#ifdef LOCAL
	auto stTime = clock();
//	freopen("in.txt", "r", stdin);
	mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
	#endif
	ios::sync_with_stdio(false);
	cout.precision(10);
	cin.tie(0);

	cin >> N >> S;

	A.assign(N + 1, 0);

	int ans = 0;

	for (int i = 1; i <= N; ++i) {
		cin >> A[i];
		if (A[i] == S)
			ans += i * (N - i + 1);
	}

	memset(mem, -1, sizeof(mem));

	db(ans, dp(1, 0, 0));

	cout << (ans + dp(1, 0, 0)) % MOD;





	#ifdef LOCAL
	cout << "\n\n\nExecution time: " <<
		(clock() - stTime) * 1e3 / CLOCKS_PER_SEC << " ms" << endl;
	#endif
    return 0;
}


Measured memory_bytes: 71040
// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define repba(i,b,a) for(int i=b;i>=a;--i)
#define repab(i,a,b) for(int i=a;i<b;++i)
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define viip vector<pair<int,pair<int,int> > > 
#define mp make_pair
#define vip vector<pair<int,int> >
#define pb push_back 
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a > mod)a %= mod;
}
int n;
const int maxn = 2e5 + 10;
ll s[4*maxn];

void update(int p,ll x,int id=1,int l = 0,int r=n){
	if(r - l < 2){
		s[id] = x;
		return;
	}
	int mid = (l+r)/2;
	if(p < mid)update(p,x,2*id,l,mid);
	else update(p,x,2*id+1,mid,r);
	s[id] = max(s[2*id],s[2*id+1]);
}
ll query(int x,int y,int id = 1,int l = 0,int r = n){
	if(x >= r || l >= y)return 0;
	if(x <= l && r <= y)return s[id];
	int mid = (l+r)/2;
	return max(query(x,y,2*id,l,mid),query(x,y,2*id+1,mid,r));
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin>>n;
	vi h(n);
	vi a(n);
	ll sum = 0;
	rep(i,n){
		cin>>h[i];
	}	
	rep(i,n){
		cin>>a[i];
		sum += a[i];
	}
	ll dp[n];
	dp[n-1] = a[n-1];
	update(h[n-1]-1,a[n-1]);
	ll ans = dp[n-1];
	repr(i,n-1){
		ll temp = query(h[i],n);
		dp[i] = temp + a[i];
		update(h[i]-1,dp[i]);
		ans = max(ans,dp[i]);
	}
	// rep(i,n){
	// 	cout<<dp[i]<<' ';
	// }
	cout<<ans;
	return 0;
}

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

using namespace std;
#define DBG(x)                         \
    (void)(cout << "L" << __LINE__     \
                << ": " << #x << " = " \
                << (x) << '\n')

typedef long long ll;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const int maxn = 110000;
int main()
{
    //检查有没有特判0,有没有越界限.
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setiosflags(ios::fixed) << setprecision(12);
    ll N;
    cin >> N;
    ll ans = 1e18;
    for (ll i = 1; i * i <= N; i++)
    {
        if (N % i == 0)
        {
            ans = min(ans, i + N / i - 2);
        }
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3660
#include <iostream>
#include <cstdio>
using namespace std;
char maps[1001][1001];
int n;
bool rectn(int y,int x,int k){
	for(int i=0;i<k;i++)if(i+y>=n||i+x>=n||maps[y+k-1][i+x]=='*'||maps[i+y][x+k-1]=='*')return false;
	return true;
}
int rect(int y,int x){
	for(int k=1;1;k++)if(k-1>=n||!rectn(y,x,k))return k-1;
}
void solve(){
	int ans=0;
	for(int i=0;i<n;i++){
		if(ans>=n-i)break;
		for(int j=0;j<n;j++){
			if(ans>=n-j)break;		
			if(maps[i][j]=='.')ans=max(ans,rect(i,j));
		}
	}
	cout<<ans<<endl;
}

int main(){
	while(cin>>n&&n){
		for(int i=0;i<n;i++)cin>>maps[i];
		solve();
	}
	return 0;
}

Measured memory_bytes: 0
#include <stdio.h>
int bubble_sort(int x[],int n) //配列ｘがソートしたい配列、ｎはソートする配列の数
{
 int i,j,temp;
 for(i=0; i<n-1; i++){
     for(j=n-1; j>i; j--){
         if(x[j-1]>x[j]){     //前の要素のほうが大きかったら
            temp = x[j-1];
            x[j-1]=x[j];
            x[j]=temp;           //値を入れ替える。
            }
        }
 }
}  //実用性あり！
int main(void)
{
 int n,a,b,c,i;
 scanf("%d",&n);
 for(i=0; i<n; i++){
       int s[3]={0};
      scanf("%d %d %d",&a,&b,&c);
      s[0]=a; s[1]=b; s[2]=c;
      bubble_sort(s,3);
      if(s[2]*s[2] == s[1]*s[1]+s[0]*s[0]) printf("YES\n");
      else printf("NO\n");
      }
return 0;
}

Measured memory_bytes: 580
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include<bits/stdc++.h>

using namespace std;
 
#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first                 
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back  
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()           
 
typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  3e5+5;
const ll   mod   =  1e9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N)+1;

ll test = 1, n, k, p[N], c[N];

ll calc(int st){
	ll x = p[st], i = 1, res = -INF;
	vll pre(n+2,0);
	while(x != st){
		pre[i] = pre[i-1] + c[x];
		i++;
		x = p[x];
	}
	pre[i] = pre[i-1] + c[x];
	ll y = k%i;
	if(y){
		ll mx = pre[1];
		for(int j = 1; j <= y; j++){
			mx = max(mx,pre[j]);
		}
		if(pre[i] < 0) pre[i] = 0;
		res =  pre[i]*(k/i)+mx;
	}
	ll mx = pre[1];
	for(int j = 1; j <= i; j++){
		mx = max(mx,pre[j]);
	}
	if(pre[i] < 0) pre[i] = 0;
	if((k/i) != 0) res = max(res, pre[i]*((k/i)-1)+mx);
	return res;
}

void solve(){
	cin >> n >> k;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) cin >> c[i];
	ll ans = -INF;
	for(int i = 1; i <= n; i++) ans = max(ans,calc(i));
	deb1(ans) 
}

int main(){
	GODSPEED;
	//cin>>test;
	for(int i = 1; i <= test; i++){
		solve();
	}
	#ifndef ONLINE_JUDGE
	cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
	#endif
}

Measured memory_bytes: 3664
/*input
2
*/

#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>;

int main() {
  int k;
  cin >> k;
  int a = 7%k, b = a;
  for (int i = 1; i <= k; i++) {
    if (b == 0) {
      cout << i << endl;
      return 0;
    }
    b = (b*10%k+a)%k;
  }
  cout << -1 << endl;
}

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

//typedef
//------------------------------------------
typedef int64_t LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//container util
//------------------------------------------
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define rwhole(f,x,...) ([&](decltype((x)) whole) { return (f)(rbegin(whole), rend(whole), ## __VA_ARGS__); })(x)
#define EACH(i,c) for(decltype((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) whole(sort, c)
#define RSORT(c) rwhole(sort, c)

//constant
//--------------------------------------------
constexpr double EPS = 1e-10;
constexpr double PI  = M_PI;
constexpr int MOD = 1000000007;

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

//debug
//--------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//IO accelerate
//--------------------------------------------
struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

//template
//--------------------------------------------
template<typename T>
istream& operator >>(istream& is, vector<T>& vec) {
  for(T& x: vec) is >> x;
  return is;
}
template<typename T>
ostream& operator <<(ostream& os, const vector<T>& vec) {
  for(int i=0; i<vec.size(); i++){
    os << vec[i] << ( i+1 == vec.size() ? "" : "\t" );
  }
  return os;
}
template<typename T>
ostream& operator <<(ostream& s, const vector<vector<T>>& vv) {
	for (int i = 0; i < vv.size(); ++i) {
		s << vv[i] << endl;
	}
	return s;
}

// 多重vector
// auto dp=make_v<int>(4,h,w) みたいに使える
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 多重vectorのためのfill
// fill_v(dp,0) みたいに使える
template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

template<class T,class U> void chmax(T&a,U b){if(a<b)a=b;}
template<class T,class U> void chmin(T&a,U b){if(b<a)a=b;}
LL gcd(LL a, LL b) { return b?gcd(b,a%b):a;}
LL lcm(LL a, LL b) { return a/gcd(a,b)*b;}

//main code

int main(int argc, char *argv[])
{
	int n,m;
	cin >> n >> m;
	if (n == m) {cout << "Yes" << endl;}
	else {cout << "No" << endl;}
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000001

int main(){
	
	int S,W;
	cin>>S>>W;
	
	if(W>=S)cout<<"unsafe"<<endl;
	else cout<<"safe"<<endl;
		
	return 0;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, n) for (int i = a; i < n; i++)
#define sz(v) (v).size()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_taple
using namespace std;

signed main() {
	int D;
	cin >> D;
	cout << "Christmas";
	for (int i = 0; i < 25-D; i++) {
		cout << " Eve";
	}
	cout << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pair<ll,ll>>
#define vvll vector<vector<ll>>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define mod 1000000007
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
using namespace std;


//

//

void solve()
{
	ll n;	cin>>n;
	vll b;
	b.resize(n,0);
	for(ll i=1;i<n;i++)
	{
		ll x;	cin>>x;
		b[x-1]++;
	}

	for(ll i=0;i<n;i++)
		cout<<b[i]<<endl;


}


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


Measured memory_bytes: 4748
#include<bits/stdc++.h>
using namespace std;
int a;
#define pb push_back
vector<int> V;

int main(){
cin >> a;
while (a){
V.pb(abs(a%2));
if(a%2!=0){
if (V.size()%2)a--;
else a++;
}
a /= 2;
}
reverse(V.begin(),V.end());
if(V.empty())cout << 0;
for (auto i : V){
cout << i;
}
}


Measured memory_bytes: 256
#include<bits/stdc++.h>//author::@whohet-->Het Patel
#define fasterthanlight ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
#define int long long
#define uint unsigned long long
#define ld long double
#define pii pair<int,int>
#define vi vector<int>
#define vs vector<string>
#define vvi vector<vector<int>>
#define mii map<int,int>
#define msi map<string,int>
#define mis map<int,string>
#define pb push_back
#define mod 1000000007
#define PI 3.141592653589793238

using namespace std;

//author::@whohet-->Het Patel

signed main()
{
	fasterthanlight;int x;
	cin>>x;
	int t,sum=0;
	t=x/500;
	sum=sum+(t*1000);
	x=x%500;
	t=x/5;
	sum=sum+(t*5);
	cout<<sum<<endl;
	
	
	
	
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,t,a,h;cin>>n>>t>>a;
  vector<pair<double,int>>x(n);
  for(int i=0;i<n;i++){
    cin>>h;
    x.at(i).first=abs(t-h*0.006-a);
    x.at(i).second=i+1;
  }
  sort(x.begin(),x.end());
  cout<<x.at(0).second<<endl;
}


Measured memory_bytes: 256
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Array

main = do
    n<-readLn::IO Int
    abcs<-replicateM n $ unfoldr (B.readInt.B.dropWhile(==' '))<$>B.getLine::IO[[Int]]

    print $ f n abcs

f n abcs = maximum $ dp ! n
    where
    dp = array (0,n) $ (0,[0,0,0]):zipWith proc [1..] abcs
    proc i [a,b,c] = (i, [max (v+b) (w+c), max (u+a) (w+c), max (u+a) (v+b)])
        where
        [u,v,w] = dp ! (i-1)


Measured memory_bytes: 68988