akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3fll
#define ll long long
#define db double
#define eps 1e-8
#define bin(x) cout << bitset<10>(x) << endl
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define endl "\n"
#define pi acos(-1.0)
#define lowbit(x) x &(-x)
const ll maxn = 1e5 + 10;

ll n, mod;
map<int, int> val;
signed main()
{
	ios::sync_with_stdio(0);
	string s;
	cin >> n >> mod >> s;
	ll res = 0;
	ll cnt = 0, cur = 0;
	if (mod == 5 || mod == 2)
	{
		s = '.' +  s;
		_for(i, 1, s.size()){
			if((s[i]  - '0') % mod == 0){
				res += i;
			}
		}
	}
	else
	{
		cnt = 1;
		
	
		val[0] = 1;
		for(int i = s.size() -1; i >= 0;i--){
			cur += cnt * (s[i] - '0');
			cur %= mod;
			res += val[cur];
			val[cur]++;
			cnt  = 10 * cnt % mod;
		}
	}
	cout << res << endl;

	


}

Measured memory_bytes: 916
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N, K = map(int, input().split())
    c = Counter(list(map(int, input().split())))
    total = len(c)
    ans = 0
    for v in sorted(c.values()):
        if total - K > 0:
            ans += v
            total -= 1
    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 32052
#include<iostream>
#include<set>
#include<vector>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>

using namespace std;

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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;


int main(void)
{
  int n;
  string name;
  vector<string> Va,Vm,Vr,Vc,Vh;
  ll count = 0;
  cin >> n;
  rep(i,n){
    cin >> name;
    if('M' == name.front()) Vm.pb(name);
    if('A' == name.front()) Va.pb(name);
    if('C' == name.front()) Vc.pb(name);
    if('R' == name.front()) Vr.pb(name);
    if('H' == name.front()) Vh.pb(name);
  }
  ll m = (ll) Vm.size();
  ll a = (ll) Va.size();
  ll c = (ll) Vc.size();
  ll r = (ll) Vr.size();
  ll h = (ll) Vh.size();
  count = (m*a*r)+(m*a*c)+(m*a*h)+(m*r*c)+(m*r*h)+(m*c*h)+(a*r*c)+(a*r*h)+(a*c*h)+(r*c*h);

  cout << count << endl;

  return 0;
}


Measured memory_bytes: 5748
/* 
 * Author: pawanmsr
 */

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

// Macro //
#define X first
#define Y second
#define pii pair<int,int>
#define pll pair<ll,ll>

// Global //
ll n, k;

// Functions //


// Main //
int main(int argc, char const *argv[])
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    
    /* code */
    cin >> n >> k;
    ll mn = n;
    if (n >= k) {
        n %= k;
        if (n < mn) mn = n;
    }

    n = abs(n-k);
    if (n < mn) mn = n;

    cout << mn << endl;
    return 0;
}


Measured memory_bytes: 256
from heapq import heappop, heappush, heapify
N = int(input())
V = list(map(int, input().split()))

V.sort()

heapify(V)

for i in range(N-1):
    a = heappop(V)
    b = heappop(V)
    heappush(V, (a+b)/2)

ans = heappop(V)

print(ans)


Measured memory_bytes: 3060
# スペース区切りの整数入力
N = int(input())

# スペース区切りの整数をリストに入力
x = [int(x) for x in input().split()]
x.sort()

cost_min = 10**20

for i in range(x[0],x[len(x)-1]+1):
    cost = 0
    for j in x:
        cost += (i-j)**2
    cost_min = min(cost_min,cost)

print(cost_min)

Measured memory_bytes: 38512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define LLINF 9223372036854775807
#define MOD ll(1e9+7)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl


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

    vector<ll> a(n);
    vector<ll> ans(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        ans[a[i]-1] = i+1;
    }
    for(int i = 0; i < n; i++){
        cout << ans[i] << " ";
    }
    cout << endl;

    return 0;

}


Measured memory_bytes: 2432
//14:07:00 

#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define int ll
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define MOD (ll)(1e9+7)
vector<int> fac;

void init()
{
	fac.resize(100010);
	fac[0]=1;
	For(i,1,100005) fac[i]=(fac[i-1]*i)%MOD;
}

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	init();
	int a,b; cin>>a>>b;
	if(abs(a-b)>1)
	{
		cout<<"0\n";
		return 0;
	}
	int ans;
	if((a+b)%2) ans=1;
	else ans=2;
	ans=(ans*fac[a])%MOD;
	ans=(ans*fac[b])%MOD;
	cout<<ans<<"\n";
	return 0;
}











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

int main(){
	string s; cin >> s;
	string a = "CODEFESTIVAL2016";
	int ans = 0;
	for(int i = 0; i < s.size(); ++i){
		if(a[i] != s[i]) ++ans;
	}
	cout << ans << endl;
	return 0;
}

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


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

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

int main() {
	ll a, b, c, d;
    cin >> a >> b >> c >> d;
    cout<< b - (a-1) - b/c + (a-1)/c - b/d + (a-1)/d + b/lcm(c, d) - (a-1)/lcm(c, d) <<endl;
}


Measured memory_bytes: 256
#include <cstdio>
using namespace std;
int v[1001]={0,1};
int main() {
    int n;
    scanf("%d",&n);
    for(int i=2;;i++){
        if(i*i>n)
            break;
        for(int j=i*i;j<=n;j=j*i){
            v[j]=1;
        }
    }
    for(int i=n;i>=1;i--)
        if(v[i]==1){
            printf("%d\n",i);
            break;
        }
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;

#define ff first
#define PI acos(-1.0)
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define MOD 1000000007
#define MOD1 998244353
#define ep 0.000000001
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, 1};
int dy4[] = {1, -1, 1, -1};
//#define fio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)

int main()
{
    #ifndef ONLINE_JUDGE
    freopen ("in.txt", "r", stdin);
    freopen ("out.txt", "w", stdout);
    #endif
    int t;
    cin>>t;
    if(t==0)cout<<1<<endl;
    else cout<<0<<endl;
    return 0;
}
//https://thebestoffersintheweb.com/redirect/57a764d042bf8/

Measured memory_bytes: 3632
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int W = scan.nextInt();
		int H = scan.nextInt();
		int x = scan.nextInt();
		int y = scan.nextInt();
		int r = scan.nextInt();
		
		if ((r - x) > 0 || (r + x) > W || (r - y) > 0 || (r + y) > H) {
			System.out.println("No");
		} else {
			System.out.println("Yes");
		}
	}
}

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

int main() {
  int r;

  cin >> r;
  cout << fixed << setprecision(10);
  cout << 2*r*3.14159265358979 << endl;
  return 0;
}

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

const int N = 1e6;
int n, l;
int a[N];

int main() {
#ifdef LC
    assert(freopen("input.txt", "r", stdin));
#endif // LC
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> l;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = 1; i < n; ++i) {
        if (a[i] + a[i + 1] >= l) {
            ans = i;
            break;
        }
    }
    if (!ans) {
        cout << "Impossible\n";
    } else {
        cout << "Possible\n";
        for (int i = 1; i < ans; ++i) {
            cout << i << "\n";
        }
        for (int i = n - 1; i > ans; --i) {
            cout << i << "\n";
        }
        cout << ans << "\n";
    }
    return 0;
}


Measured memory_bytes: 1280
# A = [list(map(int, input().split())) for _ in range(3)]
# X = int(input())
# B = [int(input()) for _ in range(n)]
# A = list(map(int, input().split()))
k, n = map(int, input().split())
A = list(map(int, input().split()))
diff = []
for i in range(n-1):
    diff.append(A[i+1]-A[i])
diff.append((k-max(A))+min(A))
print(k-max(diff))


Measured memory_bytes: 26444
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
  
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
  
using namespace std;
  
typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;
  
const int INF=1<<29;
const double EPS=1e-9;
  
const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};
 
 
int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  cout << max(A * B, C * D) << endl;
  return 0;
}

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

typedef long long ll;

using namespace std;

typedef vector<ll> vll;

ll _sieve_size;
bitset<10000010> bs;
vll primos;

// O(n*log(n)*log(n))
void sieve(ll upperbound){
	//  10^6
	_sieve_size = upperbound+1;
	bs.set();
	bs[0] = bs[1] = 0;
	for(ll i = 2; i <= _sieve_size; i++){
		if(bs[i]){
			for(ll j = i*i; j <= _sieve_size; j+=i){
				bs[j] = 0;
			}
			primos.push_back(i);
		}
	}
}
// n <= 10^12 -> checa primos até 10^6
// n = p*q
// n = x*x
// se p > sqrt(n); q < sqrt(n);

// O(log(n))
map<ll, ll> getPrimeFact(ll n){
	ll ind = 0, pf = primos[0];
	map<ll, ll> ans;
	while(pf*pf <= n){
		if(n % pf == 0){
			ll count = 0;
			while(n % pf == 0){
				n = n/pf;
				count++;
			}
			ans[pf] = count;
		}
		ind++;
		if(ind == primos.size()) break;
		pf = primos[ind];
	}
	if(n != 1) ans[n] = 1;
	return ans;
}

// O(e)
ll func(ll e){
	// s: 1 + 2 + 3 +..+i <= e
	ll sum = 0;
	ll i = 0;
	// e = 2
	while(sum <= e){
		i++;
		sum += i;
	}
	return i-1;
}

int main(){
	ll n;
	cin >> n;
	sieve(1000000);
	map<ll, ll> fats = getPrimeFact(n);
	// [p, e]
	ll ans = 0;
	// O(somatorio(e)) = O(log(n))
	for(auto x : fats){
		// printf("[%lld, %lld]\n", x.first, x.second);
		ll e = x.second;	
		ans += func(e);
	}
	cout << ans << endl;
	return 0;
}



Measured memory_bytes: 5556
#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};

#[fastout]
fn main() {
    input! {//
        n:usize,
        mut xy:[(i64,i64);n],
    }
    // x と t でわけてかんがえる
    xy.sort();

    let mut ans = 0i64;
    let mut xy45 = vec![];
    let mut xy135 = vec![];
    for &(x, y) in xy.iter() {
        xy45.push((x + y, x - y));
        xy135.push((x - y, x + y));
    }
    // dbg!(&xy45);
    xy45.sort();
    xy135.sort();
    let mut a1 = abs(xy45[0].0, xy45[n - 1].0);
    let mut a2 = abs(xy135[0].0, xy135[n - 1].0);
    println!("{}", max(a1, a2));
}
fn abs(i: i64, j: i64) -> i64 {
    return (i - j).abs();
}


Measured memory_bytes: 18212
#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
  	int count = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j*i < n; j++) {
          count++;
      }
    }  
  	cout << count << endl;
    return 0;
}

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

namespace CSharpSample01
{
    
    class Program
    {

        
        
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            int[] W = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            int S1 = 0;
            int S2 = 0;
            int dif_min = 100 * N + 1;
            int ab_val = 0;

            for(int i = 0; i < N; i++)
            {
                S1 += W[i];
            }

            for(int i = N - 1; i > 0; i--)
            {
                S1 -= W[i];
                S2 += W[i];

                ab_val = Math.Abs(S1 - S2);

                if(ab_val < dif_min)
                {
                    dif_min = ab_val;
                }
            }

            Console.WriteLine(dif_min);
        
        }
    }
}

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

int main(){
	int k;
	scanf("%d",&k);
	long long arr[k];
	for(int i=0;i<k;i++){
		scanf("%lld",&arr[i]);
	}
	if(arr[k-1]!=2){printf("-1");return 0;}
	long long mi=2,ma=3;
	for(int i=k-2;i>=0;i--){
		mi=ceil((double)(mi)/(double)(arr[i]))*arr[i];
		ma/=arr[i];ma*=arr[i];ma+=arr[i]-1;
	}
	if(mi>ma)printf("-1");
	else printf("%lld %lld",mi,ma);
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<n+1;i++)
#define sort(A) sort(A.begin(),A.end())
#define reverse(A) reverse(A.begin(),A.end())
typedef long long ll;
typedef pair<int,int> P;

int main(){
  
  ll n;
  cin >>n;
  if(n%2 == 1){
    cout << 0 << endl;
    return 0;
  }
  ll ans = 0;
  for(int i=1;pow(5,i)<=n;i++){
    ans += n/(long long)(pow((long double)5,i)*2);
    long long l = n/(pow((long double)5,i)*2);
    long long r = n/(long long)(pow((long double)5,i)*2);
    assert(l==r);
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace B
{
    public class Program
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var n = int.Parse(Console.ReadLine());
            var pi = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray();
            var count = 0;
            for (var i = 1; i < pi.Length - 1; i++)
            {
                if ((pi[i - 1] < pi[i] && pi[i] < pi[i + 1]) ||
                (pi[i - 1] > pi[i] && pi[i] > pi[i + 1]))
                    count++;
            }
            Console.WriteLine(count);
        }
    }
}


Measured memory_bytes: 13396
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
const long long mod = 1000000007;
#define dump(x)                             \
    if (dbg) {                              \
        cout << #x << " = " << (x) << endl; \
    }
bool dbg = false;
//---------------------------------------------------------------------------------------------------
/*ここからいじる*/
//---------------------------------------------------------------------------------------------------

void solve(){
    int n;
    cin >> n;
    vector<int> p(n);
    vector<int> q(n);
    vector<int> memo(8);
    for(int i = 0; i < 8; i++){
        if(i == 0){
            memo.at(i) = 1;
        }
        else{
            memo.at(i) = memo.at(i - 1) * (i);
        }
    }
    for(int i = 0; i < n; i++){
        cin >> p.at(i);
        p.at(i)--;
    }
    for(int i = 0; i < n; i++){
        cin >> q.at(i);
        q.at(i)--;
    }
    long long a, b;
    a = 0, b = 0;
    for(int i = 0; i < n; i++){
        a += (p.at(i)) * memo.at(n - i - 1);
        b += (q.at(i)) * memo.at(n - i - 1);
        for(int j = i; j < n; j++){
            if(p.at(j) > p.at(i)){
                p.at(j)--;
            }
            if(q.at(j) > q.at(i)){
                q.at(j)--;
            }
        }
    }
    long long ans;
    ans = abs(a - b);
    dbg = false;
    dump(a);
    dump(b);
    
    cout << ans << endl;
}

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



Measured memory_bytes: 256
import itertools

N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**4
for v in itertools.product([0,1,2,3], repeat=N):
    if 1 not in v:
        continue
    if 2 not in v:
        continue
    if 3 not in v:
        continue
    a,b,c,m = 0,0,0,0
    for i in range(N):
        if v[i] == 1:
            a += L[i]
            m += 10
        if v[i] == 2:
            b += L[i]
            m += 10
        if v[i] == 3:
            c += L[i]
            m += 10
    m += abs(A-a) + abs(B-b) + abs(C-c) -30
    ans = min(ans,m)
print(ans)


Measured memory_bytes: 3064
#include <iostream>
#include <string>
using namespace std;

int main(){
    string s;
    cin >> s;
    bool s_let = false;
    for (int i = 0; i < s.size()-1; ++i){
        for (int j = i + 1; j < s.size(); ++j){
            if (s[i]==s[j]){
                s_let = true;
                break;
            }
        }
    }
    if (s_let) cout << "no" << endl;
    else cout << "yes" << endl;
}

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

#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

const ll MAX = 100;

// ll cache2[MAX];
// void init_cache2() {
//   ll c = 1;
//   for (ll i = 0; i < MAX; i++) {
//     cache2[i] = c;
//     c = (c * 2) % MOD;
//   }
// }

ll N, K;

vector<ll> log_list;

ll calc(ll length) {
  ll count = 0;
  for (ll i = 0; i < log_list.size(); i++) {
    ll a = log_list[i];

    ll plus = (a - 1) / length;

    count += plus;
  }
  return count;
}

int main() {
  // init_cache2();

  scanf("%lld %lld", &N, &K);

  for (ll i = 0; i < N; i++) {
    ll v;
    scanf("%lld", &v);
    log_list.emplace_back(v);
  }

  // cout << log2l(1000000000LL) << endl;

  ll left = 0;
  ll right = 1000000000LL;

  for (;;) {
    if (1 >= right - left) {
      break;
    }
    ll center = (left + right) / 2;
    ll num = calc(center);

    if (K < num) {
      left = center;
    } else {
      right = center;
    }
  }

  cout << right << endl;
}


Measured memory_bytes: 5284
import { readFileSync } from "fs";

const read = () =>
  readFileSync(0, "utf8")
    .split("\n")
    .map((line) => line.split(" "));

const main = (input: string[][]) => {
  const [x1, x2, x3, x4, x5] = input[0].map(Number);

  if (x1 === 0) {
    return 1;
  }
  if (x2 === 0) {
    return 2;
  }
  if (x3 === 0) {
    return 3;
  }
  if (x4 === 0) {
    return 4;
  }
  if (x5 === 0) {
    return 5;
  }

  return 0;
};

console.log(main(read()));


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

#define ll long long
#define mii make_pair<int, int>
#define mll make_pair<ll, ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define all(v) (v).begin(), (v).end()

ll K = 1e9 + 7;

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

    ll n, k;
    cin >> n >> k;
    ll s = n + 1 ,a = n, b = 1, c = -2, ans = 0;
    for (int i = 0; i < k - 1; ++i)
    {
        a = (a + n) % K;
        b = (b + c) % K;
        c -= 2;
        s = (a + b) % K;
    }
    for (int i = k; i <= n + 1; ++i)
    {
        ans = (ans + s) % K;
        a = (a + n) % K;
        b = (b + c) % K;
        c -= 2;
        s = (a + b) % K;
    }
    cout << ans << '\n';
}

Measured memory_bytes: 3636
#include <iostream>
#include <string>
#include <vector>
#include <climits>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <queue>
#include <iomanip>
#include <map>

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

using namespace std;

int N;
int d[512][512];
bool notUsed[512][512];

int main() {
  cin >> N;

  rep(i, N) {
    rep(j, N) {
      cin >> d[i][j];
      notUsed[i][j] = false;
    }
  }

  rep(k, N) {
    rep(i, N) {
      rep(j, N) {
        if (k==i || k==j || j==i) continue;
        if (d[i][j] > d[i][k] + d[k][j]) {
          cout << -1 << endl;
          return 0;
        }
        notUsed[i][j] = notUsed[i][j] || (d[i][j] == d[i][k] + d[k][j]);
      }
    }
  }

  long ans = 0;
  rep(i, N) {
    for (int j=i+1; j<N; j++) {
      if (!notUsed[i][j]) {
        ans += (long)d[i][j];
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1024
#include<iostream>
#include<string>
#include<map>
using namespace std;

int main() {
    string S;
    cin >> S;

    string before = "ATCG";
    string after = "TAGC";
    map<char, char> m;

    for(int i=0; i<before.size(); i++) {
        m[before[i]] = after[i];
    }
    char ans = m[S[0]];

    cout << ans << endl;
}

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

void solve(){
    string  s;
    cin >> s;
    int t=0;
    for(char i:s)
        t += (i-'0')%9 , t%=9;
    cout << (t == 0 ? "Yes" : "No") << "\n";

}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    while(t--)
        solve();
    return  0;
}


Measured memory_bytes: 3768
n, d = map(int, input().split())

x = [list(map(int, input().split())) for _ in range(n)]

def try_square_root_naive(n):
  m = int(n ** .5)
  return m if abs(m * m - n) < 1e-6 else None

ans = 0
for i in range(n-1):
  for j in range(i+1, n):
    temp = 0
    for k in range(d):
      temp += (x[i][k] - x[j][k]) ** 2
    if try_square_root_naive(temp) is not None:
      ans += 1

print(ans)

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

using namespace std;
#define inf 1000000000000000000LL
#define unvisited -1
#define visited 1
#define eps 1e-9
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define uint64 unsigned long long
#define FastSlowInput ios_base::sync_with_stdio(false); cin.tie(NULL);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;

int main(){
	int i,j;
	ll n;
	ll a[6];
	ll minim = inf;
	int minimIdx = 9;
	scanf("%lld",&n);
	for(i=0;i<5;i++){
		scanf("%lld",&a[i]);
		if(minim > a[i]){
			minim = a[i];
			minimIdx = i;
		}
	}
	ll t = minimIdx;
	ll iterasi = (n + minim - 1) / minim;
	ll waktuKeAkhir = iterasi + (5-(minimIdx+1));
	t += waktuKeAkhir;
	// for(i=0;i<5;i++){
		
	// 	ll waktuSampeKeDia
	// 	t = max(t, iterasi + 4LL - i);
	// 	printf("iterasi: %lld i: %d ans: %lld\n",iterasi,i,iterasi + 4LL - i);
	// }

	
	printf("%lld\n",t);

	return 0;
};

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

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}


template <typename T>
struct DisjointSparseTable{
    function<T(T, T)> f;
    vector<vector<T>> v;

    DisjointSparseTable(vector<T>& inp, function<T(T, T)> f) : f(f){
        int n = inp.size();
        int b;
        for(b = 0; (1 << b) <= inp.size(); ++b);
        v.assign(b, vector<T>(n));
        for(int i = 0; i < n; ++i)
            v[0][i] = inp[i];
        for(int i = 1; i < b; ++i){
            int siz = 1 << i;
            for(int j = 0; j < n; j += siz << 1){
                int t = min(j + siz, n);
                v[i][t - 1] = inp[t - 1];
                for(int k = t - 2; k >= j; --k)
                    v[i][k] = f(inp[k], v[i][k + 1]);
                if(t >= n)
                    break;
                v[i][t] = inp[t];
                int r = min(t + siz, n);
                for(int k = t + 1; k < r; ++k)
                    v[i][k] = f(v[i][k - 1], inp[k]);
            }
        }
    }

    T get(int l, int r){
        if(l >= --r)
            return v[0][l];
        int p = 31 - __builtin_clz(l ^ r);
        return f(v[p][l], v[p][r]);
    }
};



signed main(){

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    DisjointSparseTable<int> d_max(a, [](auto x, auto y){return max(x, y);});
    DisjointSparseTable<int> d_min(a, [](auto x, auto y){return min(x, y);});

    vector<int> ma(n), mi(n);
    iota(ma.begin(), ma.end(), 1);
    iota(mi.begin(), mi.end(), 0);
    for(int i = 0; i < n - 1; ++i)
        if(a[i] < a[i + 1])
            mi[i + 1] = mi[i];
    for(int i = n - 2; i >= 0; --i)
        if(a[i] < a[i + 1])
            ma[i] = ma[i + 1];

    set<pair<int,int>> v;
    bool fl = false;
    for(int i = 0; i < n - k + 1; ++i){
        int l = i, r = i + k;
        int ma_p = d_max.get(l, r);
        int mi_p = d_min.get(l, r);
        if(r <= ma[l]){
            fl = true;
            continue;
        }
        if(l && a[l - 1] < mi_p)
            l = mi[l - 1];
        if(r != n && ma_p < a[r])
            r = ma[r];
        v.emplace(l, r);
    }
    cout << v.size() + fl << endl;
}



Measured memory_bytes: 40176
#include <stdio.h>

int main(void) {
	int i, j;
	int n;
	int ball[10];
	int min;
	int B;
	
	scanf("%d", &n);
	while (n--) {
		B = 0;
		for (i = 0; i < 10; i++)
			scanf("%d", &ball[i]);
		for (i = 0; i < 10; i++) {
			min = ball[i];
			for (j = i + 1; j < 10; j++)
				if( ball[j] < min)
					min = ball[j];
			if( ball[i] != min)
				if( B < ball[i])
					B = ball[i];
				else
					break;
		}
		if( i < 10)
			printf("NO\n");
		else
			printf("YES\n");
	}
	return 0;
}

Measured memory_bytes: 580
object Main extends App {
  import scala.io.StdIn._

  val mod = 1000000007
  val s = readLine.trim
  implicit val memo = Array.tabulate(s.length){l ⇒ Array.tabulate(s.length - l){_ ⇒ -1L}}
  val chars = new CharIndex(s)
  println(
    ('a' to 'z').map{c ⇒
      (chars.nearestRight(c, 0), chars.nearestLeft(c, s.length - 1)) match{
        case (Some(l), Some(r)) ⇒ f(l, r, chars)
        case _ ⇒ 0
      }
    }.foldLeft(0L){case (l, r) ⇒ (l + r) % mod}
  )
  def f(exclusiveLeft: Int, exclusiveRight: Int, chars: CharIndex)(implicit memo: Array[Array[Long]]): Long = {
    if (memo(exclusiveLeft)(exclusiveRight - exclusiveLeft) == -1) {
      memo(exclusiveLeft)(exclusiveRight - exclusiveLeft) = if (exclusiveLeft == exclusiveRight) 1
      else ('a' to 'z').map {c ⇒
        (chars.nearestRight(c, exclusiveLeft + 1), chars.nearestLeft(c, exclusiveRight - 1)) match {
          case (Some(l), Some(r)) if l <= r ⇒ f(l, r, chars)
          case _ ⇒ 0
        }
      }.foldLeft(2L){case (l, r) ⇒ (l + r) % mod}
    }
    memo(exclusiveLeft)(exclusiveRight - exclusiveLeft)
  }
  class CharIndex(str: String) {
    private val nearestRightArray = ('a' to 'z').map(c ⇒ Array.tabulate(str.length){i ⇒ if (str(i) == c) i else - 1}.also{a ⇒
      for (i ← (1 until a.length).reverse){
        if (a(i - 1) == -1) a(i - 1) = a(i)
      }
    })
    private val nearestLeftArray = ('a' to 'z').map(c ⇒ Array.tabulate(str.length){i ⇒ if (str(i) == c) i else - 1}.also{a ⇒
      for (i ← (1 until a.length)){
        if (a(i) == -1) a(i) = a(i - 1)
      }
    })
    def nearestRight(char: Char, index: Int): Option[Int] = {
      nearestRightArray(char - 'a')(index) match {
        case i if i == -1 ⇒ None
        case i ⇒ Some(i)
      }
    }
    def nearestLeft(char: Char, index: Int): Option[Int] = {
      nearestLeftArray(char - 'a')(index) match {
        case i if i == -1 ⇒ None
        case i ⇒ Some(i)
      }
    }
    def includeChatType(exclusiveLeft: Int, exclusiveRight: Int): Int = {
      nearestLeftArray.count(a ⇒ a(exclusiveRight) > exclusiveLeft)
    }
  }
  implicit class ScopeFunc[T](value: T){
    def let[A](func: T ⇒ A): A = func(value)
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}


Measured memory_bytes: 470276
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define rep(i, n)   for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    int n, k;   cin >> n >> k;
    vector<int>p(n);
    rep(i, n)   cin >> p[i];
    vector<int>csum(10001);
    for(int i = 1; i < csum.size(); i++){
        csum[i] = csum[i-1] + i;
    }
    vector<double>exp(10001);   //期待値
    for(int i = 1; i < exp.size(); i++){
        exp[i] = (double)csum[i]/i;
    }
    vector<double>pcsum(n+1);   //累積和
    for(int i = 1; i <= n; i++){
        pcsum[i] = pcsum[i-1] + exp[p[i-1]];
    }
    double ans = 0;
    for(int i = k; i <= n; i++){
        ans = max(ans, pcsum[i] - pcsum[i-k]);
    }
    printf("%.10f\n", ans);
}


Measured memory_bytes: 2688
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

vector<int> g[606];
int N,M;

double dp[606];
const double INF=1e9;

int main(){
  cin>>N>>M;
  rep(i,M){
      int s,t;
      cin>>s>>t;
      s--;t--;
      g[s].push_back(t);
  }

  for(int i=0;i<N;i++) dp[i]=INF;
  dp[N-1]=0;
  for(int i=N-1;i>=0;i--){
      double res=0;
      int k=g[i].size();
      for(auto nex:g[i]){
          if(dp[nex]==INF) continue;
          res+=(dp[nex]+1);
      }
      if(k<=0) continue;
      dp[i]=res/k;
  }

  double ans=dp[0];
  for(int i=0;i<N-1;i++){
      double res=-1;
      int k=0;
      int mit=0;
      for(auto nex:g[i]){
          if(res<dp[nex]) mit=nex;
          res=max(res,dp[nex]);
          k++;
      }
      //if(k<=1) continue;

      double sum[606]={};
      for(int j=0;j<N;j++) sum[j]=INF;
      sum[N-1]=0;

      for(int j=N-1;j>=0;j--){
          int k=0;
          double res=0;
          for(auto nex:g[j]){
              if(j==i&&nex==mit) continue;
              if(sum[nex]==INF){
                  k=0;
                  break;
              }
              res+=sum[nex]+1;
              k++;
          }
          if(k<=0) continue;
          sum[j]=res/k;
      }

      bool ok=true;
      for(int j=0;j<N;j++) if(sum[j]==INF) ok=false;
      if(ok==false) continue;
      ans=min(ans,sum[0]);
  }

  cout<<fixed<<setprecision(10)<<ans<<endl;


  return 0;
}


Measured memory_bytes: 1408
#include<iostream>
#include<string>
using namespace std;

int main(){
    string str1, str2;

    int n;
    int a=0,b=0;
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> str1 >> str2;
        if(str1>str2)a+=3;
        else if(str1<str2)b+=3;
        else{
            a++;b++;
        }
    }
    cout << a << " " << b << endl;
    return 0;
}


Measured memory_bytes: 3152
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}


signed main(){

    string K; cin >> K;
    int D; cin >> D;
    int N = K.size();
    int mod = 1000000007;

    vector<mat> dp(N + 1, mat(D, vec(2, 0)));
    dp[0][0][0] = 1;
    REP(i, N){
        int n = K[i] - '0';
        REP(j, n){
            REP(k, D){
                dp[i + 1][(k + j) % D][1] += dp[i][k][0] + dp[i][k][1];
                dp[i + 1][(k + j) % D][1] %= mod;
            }
        }
        REP(k, D) REP(l, 2){
            dp[i + 1][(k + n) % D][l] += dp[i][k][l];
            dp[i + 1][(k + n) % D][l] %= mod;
        }
        FOR(j, n + 1, 10){
            REP(k, D){
                dp[i + 1][(k + j) % D][1] += dp[i][k][1];
                dp[i + 1][(k + j) % D][1] %= mod;
            }
        }
    }
    
    cout << (dp[N][0][0] + dp[N][0][1] - 1 + mod) % mod;
    
    return 0;
}

Measured memory_bytes: 55296
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 1e5 + 10;

vector<int> G[maxn];
int A[maxn];
bool flag = true;

long long dfs(int u, int fa)
{
	if (G[u].size() == 1)
		return A[u];
	long long sum = 0;
	long long mx = 0;
	for (int v : G[u])
	{
		if (v == fa)
			continue;
		long long t = dfs(v, u);
		sum += t;
		mx = max(mx, t);
	}
	long long mxp = mx > sum - mx ? sum - mx : sum / 2;
	long long p = sum - A[u];
	long long rem = A[u] - p;
	if (p < 0 || rem < 0 || p > mxp)
		flag = false;
	return rem;
}

int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 1; i <= N; ++i)
		scanf("%d", &A[i]);
	for (int i = 1; i < N; ++i)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	if (N == 2)
	{
		puts(A[1] == A[2] ? "YES" : "NO");
		return 0;
	}
	int rt = 0;
	for (int i = 1; i <= N; ++i)
		if (G[i].size() > 1)
			rt = i;
	puts(dfs(rt, 0) == 0 && flag ? "YES" : "NO");
	return 0;
}


Measured memory_bytes: 13184
import sys
input = sys.stdin.readline
S = list(input())[: -1]
res = 0
for l in range(len(S)):
  for r in range(l + 1, len(S) + 1):
    for i in range(l, r):
      if not S[i] in ["A", "T", "C", "G"]: break
    else: res = max(res, r - l)
print(res)

Measured memory_bytes: 62164
////
////  main.cpp
////  L2-005
////
//
#include <cstring>
#include <vector>
#include <iostream>
#include <unordered_map>
#include<algorithm>
#include <set>
#include <list>
#include<stdio.h>
using namespace std;
using ll=long long;

int main(){
    ll H,W;
    cin>>H>>W;
    ll res=0;
    if(H==1||W==1){
        cout<<1<<endl;
        return 0;
    }
    if (H%2==0) {
        res=H/2*W;
    }else{
        res=H*(W/2);
        if (W%2!=0) {
            res+=H/2+1;
        }
    }
    cout<<res<<endl;
    
}


Measured memory_bytes: 888
#include<bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define ull unsigned long long int  // ranges from (0 - twice of long long int)
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define vll vector<ll>
#define mod 1000000007LL
#define llpair pair<ll,ll>
#define INF 1000000000000000000ll
#define np next_permutation
#define PI acos(-1)
#define deb(x) cout<<#x<<" "<<x<<endl;
//ALWAYS USE ll DATA TYPE FOR INTEGER(FASTER THAN INTEGER AS WELL AS SOMETIMES WITHOUT USE LONG UR ANSWER WILL NOT ACCEPTED) && DOUBLE FOR DECIMAL VALUE
template<typename T> // for removing front element from the vector
void pop_front(std::vector<T> &v)
{
    if (v.size() > 0) {
        v.erase(v.begin());
    }
}
template<typename R> // for removing back element from the vector or can directly
void pop_back(std::vector<R> &v) // use vec.pop_back() function...
{
    if (v.size() > 0) {
        v.erase(v.begin()+v.size()-1);
    }
}

bool isprime(ll n)  //IN  O(sqrt(n))
{
    if(n<=1) return false;
    for(ll i=2;i*i<=n;i++)
        if(n%i==0)
            return false;

    return true;
}

vector<ll> factors(ll n){    //IN  O(sqrt(n))

    vll v;
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            v.pb(i);
            if((n/i)!=i)
            v.pb(n/i);
        }
    }
    return v;
}

ll power(ll a, ll n)  // using Binary Exponential in TC O(logn)
{                     // Self Written By </ME> :)
    ll res=1;
    while(1)
    {
        if(n%2){
            res=res*a; n--;
        }
        if(n==0) break;
        a=a*a; n/=2;
    }
    return res;
}

ll ts(ll arr[],int l,int r,int x) // ternary search to find x in sorted array arr
{
    
    while(l<=r)
    {
        ll mid1= l+(r-l)/3;
        ll mid2= r -(r-l)/3;
        if(arr[mid1]==x || arr[mid2]==x)
            return 1;
        if(x<arr[mid1])
            r=mid1-1;
        else if(x>arr[mid2])
            l=mid2+1;
        else
        {
            l=mid1+1;
            r=mid2-1;
        }
    }

    return -1;
}

ll f(ll a)
{
    if(a%2==0)
    {
        return ((a/2)*(a-1));
    }
    else
        return (((a-1)/2)*a);
}


int main() {
    auto start = chrono::high_resolution_clock::now();
    fio;
    ll T=1;
    //cin>>T;
    while(T--)
    {
      ll n,m; cin>>n>>m;
      ll ans = f(n) + f(m);
      cout<<ans<<"\n";

    }
    
    auto finish = chrono::high_resolution_clock::now();
    cerr << "Time elapsed: " << (chrono::duration<long double>(finish-start)).count() << "s\n";
    return 0;
}





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

#define int long long

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef pair < int , int > pp;
const int mod = 1e9 + 7;
const int N   = 2e3 + 3;

pair < int , pp > E[N];
int ata[N],H[N],n,e,X,i,j,x,y,z,tot,a,b;

int find(int x){
    return ata[x] == x ? x : ata[x] = find(ata[x]);
}

signed main(){
    cin >> n >> e >> X;
    for(i=1;i<=e;i++){
        cin >> x >> y >> z;
        E[i] = mp(z,mp(x,y));
    }
    sort(E+1 , E+e+1);

    for(i=1;i<=n;i++) ata[i] = i;
    for(i=1;i<=e;i++){
        z = E[i].st;
        if(z == E[i+1].st) continue;
        for(j=i; E[j].st == z; j--){
            x = E[j].nd.st;
            y = E[j].nd.nd;
            if(find(x) != find(y))
                H[j] = 1;
        }
        for(j=i; E[j].st == z; j--){
            x = E[j].nd.st;
            y = E[j].nd.nd;
            if(find(x) != find(y)) tot += E[i].st;
            ata[ find(x) ] = find(y);
        }
    }

    b = a = 1;
    if(X != tot) a = 2;
    for(i=1;i<=e;i++){
//      if(H[i]) continue;
        for(j=1;j<=n;j++) ata[j] = j;
        for(j=1; j <= e && find(E[i].nd.st) != find(E[i].nd.nd) ; j++){
            x = E[j].nd.st;
            y = E[j].nd.nd;
            z = E[j].st;
            ata[ find(x) ] = find(y);
        }

        if(X == tot+E[i].st-z) a = (a+a) % mod;
        if(X < tot+E[i].st-z) b = (b+b) % mod;
    }

    cout << (a-2) * b % mod;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

string s;

int main(){
	cin>>s;
	int A=0,B=0,t=0;
	for(int i=0;i<s.size();i++){
		char c=s[i];
		if(c=='(') A++,B++;
		if(c==')') A--,B=max(0,B-1);
		if(c=='*') t+=B,B=0;
	}
	cout<<t<<endl;
}



Measured memory_bytes: 3176
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i, n) for(int i=0;i<n;i++)
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define szz(x) (int)(x).size()
#define Fi first
#define Se second
typedef long double ldouble;

typedef pair<double, int> pdi;
int N, R;
int C[200010], P[200010];
double A[200010];
int W[200010], T[200010], chk[200010];
vector <int> E[200010];
int par[200020]; int Find(int x) { return par[x] == x ? x : par[x] = Find(par[x]); }

void solve(){
	scanf("%d", &N);
	R = 1;
	rep(i, N-1){
		int x, y;
		scanf("%d", &x);
		y = i + 2;
		E[x].pb(y);
		P[y] = x;
	}
	int cc = 0;
	for(int i=1;i<=N;i++) {
		int x; scanf("%d", &x);
		cc += C[i] = (x == 0);
		W[i] = 1;
		par[i] = i;
	}
	priority_queue<pdi> pq;
	for(int i=1;i<=N;i++)pq.push(pdi(A[i] = C[i], i));
	ll ans = 0;
	for(int i=1;i<=N;i++)ans += C[i];
	while(!pq.empty()){
		pii t = pq.top();
		pq.pop();
		if(t.Se == R || chk[t.Se])continue;
		chk[t.Se] = 1;
		par[t.Se] = Find(P[t.Se]);
		int pt = Find(t.Se);
		ans += (ll)(T[pt] + 1) * C[t.Se];
		C[pt] += C[t.Se];
		W[pt] += W[t.Se];
		T[pt] += T[t.Se] + 1;
		A[pt] = (double) C[pt] / W[pt];
		pq.push(pdi(A[pt], pt));
		/*
		for(auto e : E[t.Se]){
			E[P[t.Se]].pb(e);
			P[e] = P[t.Se];
		}
		C[P[t.Se]] += C[t.Se];
		W[P[t.Se]] += W[t.Se];
		T[P[t.Se]] += T[t.Se] + 1;
		A[P[t.Se]] = (double)C[P[t.Se]] / W[P[t.Se]];
		 */
		pq.push(pdi(A[P[t.Se]], P[t.Se]));
	}
	printf("%lld\n", ans - (ll)cc*(cc+1)/2);
}

int main() {
	//DM(); return 0;
	int tc = 1; // scanf("%d", &tc);
	for(int t=1;t<=tc;t++) {
	//	printf("Case #%d: ", t);
		solve();
	}
	return 0;
}


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

using namespace std;
#define booster ios_base::sync_with_stdio(false); cin.tie(NULL);


void solve() {

    string s;
    cin >> s;
    int r=0;
    int si=0;

    for(int i=0;i<3;i++) {
        if(s[i]=='R') r++;
        else si++;
    }
    if(r==0) cout<<"0"<<endl;
    else if(r==3) cout<<"3"<<endl;
    else  if(r==2) {
        if(s[1]=='S' ) cout<<"1"<<endl;
        else cout<<"2"<<endl;
    }
    else cout<<"1"<<endl;




}


int main() {

    booster;
#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif


    int t;
    t = 1;


    while (t--) solve();


    return 0;
}

Measured memory_bytes: 3640
import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())

n=int(input())

a=list(mii())
a.sort()

if a[0]==a[n-1]:
    if a[0]==0:
        print("Yes")
    else:
        print("No")
elif n%3:
    print("No")
else:
    a1,a2,a3=a[0],a[n//3],a[n//3*2]
    if a1!=a[n//3-1] or a2!=a[n//3*2-1] or a3!=a[n-1]:
        print("No")
    elif a1^a2!=a3 or a1^a3!=a2 or a2^a3!=a1:
        print("No")
    else:
        print("Yes")






Measured memory_bytes: 14692
S = list(input())
T = list(input())

S.reverse()
T.reverse()
flag = False

for i in range(len(S)-len(T)+1):
    for j in range(len(T)):
        if S[j+i] == T[j] or S[j+i] == '?':
            pass
        else:
            break

        if j == len(T)-1:
            flag = True
            for k in range(len(T)):
                if S[k+i] == '?':
                    S[k+i] = T[k]
            break
    if flag:
        break

if flag:
    for i in range(len(S)):
        if S[i] == '?':
            S[i] = 'a'
    S.reverse()
    print(''.join(S))

else:
    print('UNRESTORABLE')





Measured memory_bytes: 3188
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

ans = 0
for i in range(N):
    if i >= K:
        if T[i] == T[i-K]:
            T[i] = 'd'
    if T[i] == 'r':
        ans += P
    elif T[i] == 's':
        ans += R
    elif T[i] == 'p':
        ans += S
print(ans)


Measured memory_bytes: 49520
#pragma region _head

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<iomanip>
#include<queue>
#include<stack>
#include<numeric>
#include<utility>
#include<regex>

#pragma region _define

#define int LL
#define f(i,a,b) for(LL i=a;i<b;i++)
#define f_vI(v,n) f(i,0,n)cin>>v[i]
#define f_v2I(v1,v2,n) f(i,0,n)cin>>v1[i]>>v2[i]
#define f_v3I(v1,v2,v3,n) f(i,0,n)cin>>v1[i]>>v2[i]>>v3[i]
#define f_vO(v,n) f(i,0,n)cout<<v[i]<<endl
#define ei else if
#define all(a) a.begin(),a.end()
#define size(s) ((LL)s.size())
#define F first
#define S second
#define check() cout<<"! ! !"
#define endl "\n"
#define _y() cout<<"Yes"<<endl
#define _Y() cout<<"YES"<<endl
#define _n() cout<<"No"<<endl
#define _N() cout<<"NO"<<endl
#define INF 1<<29
#define MOD 1000000007

#pragma endregion

#pragma region _using

using namespace std;

using LL = long long;
using st = string;
using vi = vector<LL>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vc = vector<char>;
using vs = vector<st>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using si = stack<LL>;
using ss = stack<st>;
using pi = pair<LL, LL>;
using v_pi = vector<pi>;

#pragma endregion

#pragma region _graph

#define node 50  //グラフの最大位数　適宜変更
//bool graph[node][node]; bool visited[node] = { false };
#define link(a,b,m) f(i,0,m){cin>>a[i]>>b[i]; a[i]--; b[i]--; graph[a[i]][b[i]]=graph[b[i]][a[i]]=true;}

#pragma endregion

LL gcd(LL a, LL b) {
	LL r;
	while ((r = a % b) != 0) {
		a = b;
		b = r;
	}
	return b;
}
LL lcm(LL a, LL b) {
	return (a / gcd(a, b) * b);
}

//素数判定
bool is_prime(int n) {
	for (int i = 2; i*i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
}

//階乗計算
LL factorial(LL n) {
	LL a = 1, ret = 1;
	while (a < n) {
		a++;
		ret *= a;
		//ret %= MOD;
	}
	return ret;
}

#pragma endregion 



/*****************************************************************************/
signed main(void) {
	cin.tie(0); ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	int ans = 0;
	if (m>=n*2) {
		ans += n;
		m -= 2 * n;
		n = 0;
	}
	else {
		cout << m / 2;
		return 0;
	}
	ans += m / 4;
	cout << ans;
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <climits>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

#define fast_cin() ios_base::sync_with_stdio(false)

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

int N, M;
int tot(int p, int q);

int main()
{
	int K;
	cin >> N >> M >> K;
	for (int p = 0; p <= N; ++p) {
		for (int q = 0; q <= M; ++q) {
			if (tot(p, q) == K) {
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;
}

int tot(int p, int q)
{
	return p * M + q * N - 2 * p * q;
}


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

int main() {
  vector<int> v(4);
  for (int i = 0; i < 3; i++) {
    int a, b;
    cin >> a >> b;
    v[a - 1]++;
    v[b - 1]++;
  }
  int r = 0;
  for (auto e : v) {
    r += e % 2;
  }
  cout << (r == 0 || r == 2 ? "YES" : "NO") << endl;
}


Measured memory_bytes: 256
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math


def solve(n):
  ret = 0
  for i in range(1,math.ceil(math.sqrt(n))):
    s,t = divmod(n, n//i-1)
    if s==t:
      ret += n//i-1
  return ret


if __name__ == "__main__":
  N = int(input())
  print(solve(N))

Measured memory_bytes: 39760