akhauriyash/Code-Regression huggingface dataset:
n = int(input())
s = input()
east = [0] * n
west = [0] * n
before_east, before_west = 0, 0

for i in range(n):
    if s[i] == 'E':
        east[i] = before_east+1
        west[i] = before_west
        before_east = east[i]
        before_west = west[i]
    else:
        east[i] = before_east
        west[i] = before_west+1
        before_east = east[i]
        before_west = west[i]

total_east = east[-1]
total_west = west[-1]

mini = float('inf')
for i in range(n):
    if i==0:
        left_east, left_west = 0, 0
        if s[0]=='E':
            right_east = total_east-1
            right_west = total_west
        else:
            right_east = total_east
            right_west = total_west-1
    else:
        left_east = east[i-1]
        left_west = west[i-1]
        right_east = total_east-east[i]
        right_west = total_west-west[i]

    tmp = left_west+right_east

    if tmp<mini:
        mini = tmp

print(mini)

Measured memory_bytes: 17568
#include <stdio.h>

int main(void) {
  int a,b;
  scanf("%d", &a);
  scanf("%d", &b);
  if (a == b) {
    printf("a == b\n");
  } else if (a > b) {
    printf("a > b\n");
  } else {
    printf("a < b\n");
  }
  return 0;

}



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

#define rep(i, n) for(int i = 0; i<(n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

int dpx[5000][16200];
int dpy[5000][16200];

int main() {
    string s;
    int x, y;
    cin >> s;
    cin >> x >> y;

    vector<int> X;
    vector<int> Y;

    int cnt = 0;
    bool is_front = true;
    rep(i, s.size()) {
        if (s[i] == 'F') {
            cnt++;
        } else {
            if (is_front) {
                X.push_back(cnt);
            } else {
                Y.push_back(cnt);
            }
            is_front = !is_front;
            cnt = 0;
        }
    }

    if (cnt) {
        if (is_front) {
            X.push_back(cnt);
        } else {
            Y.push_back(cnt);
        }
    }


    dpx[1][8100 + X[0]] = 1;
    for (int i = 1; i < X.size(); i++) {
        rep(j, 16200) {
            if (dpx[i][j]) {
                dpx[i + 1][j + X[i]] = 1;
                dpx[i + 1][j - X[i]] = 1;
            }
        }
    }

    dpy[0][8100] = 1;
    rep(i, Y.size()) {
        rep(j, 16200) {
            if (dpy[i][j]) {
                dpy[i + 1][j + Y[i]] = 1;
                dpy[i + 1][j - Y[i]] = 1;
            }
        }
    }

    if (dpx[X.size()][8100 + x] && dpy[Y.size()][8100 + y]) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
//    rep(i, X.size()) cout << X[i] << " ";
//    cout << endl;
//
//    rep(i, XX.size()) cout << XX[i] << " ";
//    cout << endl;
//
//    rep(i, Y.size()) cout << Y[i] << " ";
//    cout << endl;
//
//    rep(i, YY.size()) cout << YY[i] << " ";
//    cout << endl;
}

Measured memory_bytes: 504192
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int n,r;
    cin >> n >> r;

    if(n >= 10) {
        cout << r << endl;
    } else {
        cout << r + 100 * (10 - n) << endl;
    }
}


Measured memory_bytes: 256
/*
ID: anonymo14
TASK: wormhole
LANG: C++                 
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {//227
	int n,m,x,y;
	cin>>n>>m>>x>>y;
	int mx = -100;
	while(n--) {
		int val;
		cin>>val;
		mx=max(mx,val);
	}
	int my = 100;
	while(m--) {
		int val;
		cin>>val;
		my=min(my,val);
	}
	if(my>mx&&x<my&&my<=y) {
		cout<<"No War";
	}else cout<<"War";
}

int main() {
	
	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}	


Measured memory_bytes: 3640
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (int i = f; i < t; i++)
#define pr(i, f, t) for (int i = t - 1; i >= f; i--)
#define ca(n, a) rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\n" : " ")
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
int main()
{
    ll k, n;
    cin >> k >> n;
    ll a[n];
    rp(i, 0, n) cin >> a[i];
    ll aa = 0;
    rp(i, 0, n - 1) aa = max(aa, (a[(i + 1)] - a[i]));
    aa = max(aa, (a[0] + k - a[n - 1]) % k);
    cout << k - aa << endl;
}

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

int main()
{
  int N, M;
  std::cin >> N >> M;
  int min = -1, max = N+1;
  for (int i = 0; i < M; i++)
  {
    int l, r;
    std::cin >> l >> r;
    if (min < l) min = l;
    if (max > r) max = r;
  }
  if (min > max) std::cout << 0 << std::endl;
  else std::cout << max - min + 1 << std::endl;
}

Measured memory_bytes: 384
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = [0] * n
    ans[0] = sum([a[i] * (-1) ** i for i in range(n)])
    for i in range(1, n):
        ans[i] = (a[i - 1] - ans[i - 1] // 2) * 2
    print(*ans)


if __name__ == "__main__":
    main()


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

#define ll long long
#define F first
#define S second
#define ii pair < int , int >
#define ever (;;)

const int N = 5010;

bool vis[N];
ll n,k,c[N],p[N];
ll ans = -1e18;
vector <ll> v;

void dfs(int u)
{
    if( vis[u] )
        return;

    vis[u] = 1;
    v.push_back(c[u]);
    dfs(p[u]);
}

int main()
{
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%lld",&p[i]);
    for(int i=1;i<=n;i++)
        scanf("%lld",&c[i]);

    for(int i=1;i<=n;i++)
    {
        if( !vis[i] )
        {
            v.clear();
            dfs(i);

            ll sz = (int)(v.size());
            ll curr = 0;
            for(int i=0;i<sz;i++)
            {
                v.push_back(v[i]);
                curr += v[i];
            }
            for(int i=0;i<sz;i++)
                v.push_back(v[i]);


            if( sz >= k )
            {
                for(int i=0;i<sz;i++)
                {
                    ll inc = 0;

                    for(int j=i;j<min(i+k,3*sz);j++)
                    {
                        inc += v[j];
                        ans = max( ans , inc );
                    }
                }

                continue;
            }

            ll moves = k%sz + sz;
            curr *= k/sz-1;

            ans = max( ans , curr );

            if( curr <= 0 )
            {
                curr = 0;
                moves = k;
            }

            for(int i=0;i<sz;i++)
            {
                ll inc = 0;

                for(int j=i;j<min(i+moves,3*sz);j++)
                {
                    inc += v[j];
                    ans = max( ans , curr+inc );
                }
            }
        }
    }

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


Measured memory_bytes: 4020
using namespace std;
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#define all(x) (x).begin(),(x).end()

void solve(){

}
int main(){
    string S ,T, v;
    cin >> S >> T ;
    v = S;
    int tmp;
    bool ans = true;
    for (int i = 0; i < S.size(); i++){
        if (S[i] != T[i]) {
            tmp = S[i];
            S[i] = T[i];
            for (int k = 0; k < S.size(); k++){
                if (i == k) continue;
                if (S[k] == tmp ){
                    S[k] = T[i];
                    if (S[k] == v[k]){
                        cout << "No" << endl;
                        exit(0);
                    }
                } else if (S[k] == T[i]) {
                    S[k] = tmp;
                    if (S[k] == v[k]){
                        cout << "No" << endl;
                        exit(0);
                    }
                }
            }
        }
    }

    for (int i = 0; i < S.size(); i++){
        if (S[i] != T[i]){
            ans = false;
        }
    }

    if (ans){
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}


Measured memory_bytes: 904
from numpy import*
n,a=open(0)
n=int(n)
a=cumsum(int64([0]+a.split()))
dp=zeros((n,n+1),'i8')
for k in range(1,n):
  l=arange(n-k)
  r=arange(k+1,n+1)
  m=array([arange(i+1,i+k+1)for i in range(n-k)]).T
  dp[l,r]=amin(dp[l,m]+dp[m,r],0)+a[r]-a[l]
print(dp[0,n])

Measured memory_bytes: 16356
#include<iostream>
using namespace std;
int a[4],ans[105],ak;
main()
{
    for(int i=1;i<=3;i++){
        cin>>a[i];
        ans[a[i]]++;
    }
    for(int i=1;i<=103;i++){
        if(ans[i]>0) ak++;
    }
    cout<<ak<<endl;
    return 0;
}

Measured memory_bytes: 384
#include<iostream>
#include<string>

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

using namespace std;

int main(){
    string S;
    long long count =0;
    long long Anum=0;
    
    cin >> S;
    
    rep(i,S.size()-1){
        if(S[i] == 'A'){
            Anum++;
        }else if((S[i]=='B')&&(S[i+1]=='C')){
            i++;
            count += Anum;
        }else{
            Anum=0;
        }
    }
    cout << count;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

signed main(){

    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n; cin >> n;
    vector<int> b(n-1);
    rep(i,n-1) cin >> b[i];
    int sum = 0;
    sum += b[0] + b[n-2];
    rep(i,n-2) sum += min(b[i], b[i+1]);
    cout << sum << endl; 
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s, t, ans;
int n, m, pd[3001][3001], fwl[3001][3001], fwc[3001][3001];
int main()
{
    cin>>s;
    cin>>t;
    n=s.size()-1;
    m=t.size()-1;
    for(int i=0; i<=n; i++)
        for(int j=0; j<=m; j++)
            if(s[i]==t[j])
                pd[i][j]=(i && j) * pd[i-1][j-1]+1, fwl[i][j] = i-1, fwc[i][j] = j-1;
            else
            {
                fwl[i][j] = i-1;
                fwc[i][j] = j;
                pd[i][j] = (i > 0) * pd[i-1][j];
                if((j > 0) * pd[i][j-1] > pd[i][j])
                    pd[i][j]=(j > 0) * pd[i][j-1], fwl[i][j] = i, fwc[i][j] = j-1;
            }
    int i = n;
    int j = m;
    while(i >= 0)
    {
        if(fwl[i][j] == i-1 && fwc[i][j] == j-1)
            ans.push_back(s[i]), --i, --j;
        else
            if(fwc[i][j] == j-1)
                --j;
            else
                --i;
    }
    for(int i=ans.size()-1; i>=0; i--)
        cout<<ans[i];
    return 0;
}


Measured memory_bytes: 105856
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
    ll n;
    cin >> n;
    ll a[n];
    rep(i,n){
        cin >> a[i];
        a[i]-=i+1;
    }
    sort(a,a+n);
    ll cur=(n-1)/2;

    ll res=1LL<<60;
    for(int j=-1;j<=1;j++){
        if(cur+j>n||cur+j<0)continue;
        ll sum=0;
        rep(i,n){
            sum+=abs(a[i]-a[cur+j]);
        }
        if(sum<res)res=sum;
    }
    cout << res << endl;
    return 0;
}


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

const int MAXN = 500+5;
int cost[MAXN][MAXN];
bool occ[MAXN][MAXN];
int ans, N;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void update(int x, int y)
{
    int val = cost[x][y] + occ[x][y];
    for(int i=0;i<4;i++){
        int x1 = x+dx[i];
        int y1 = y+dy[i];
        if(x1<=0 || y1<=0 || x1>N || y1>N) continue;
        if(cost[x1][y1]>val){
            cost[x1][y1] = val;
            update(x1, y1);
        }
    }
}

int main()
{
    cin >> N;
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            int temp1 = min(i, N-i+1);
            int temp2 = min(j, N-j+1);
            cost[i][j] = min(temp1, temp2) - 1;
            occ[i][j] = true;
        }
    }

    ans = 0;
    int cnt = N*N;
    while(cnt--)
    {
        int indx; cin >> indx;
        int x = indx/N+1;
        int y = indx%N;
        if(y==0) y+=N, x-=1;
        ans+=cost[x][y];
        occ[x][y] = false;
        update(x, y);
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 20160
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <iterator>  // std::back_inserter()
#include <tuple>  // std::tuple std::get
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>  // std::copy()
#include <functional>  // std::greater<T>()
#include <utility>  // std::swap()
#include <numeric>  // accumulate(ALL(vec), 0)  0 は初期値
#include <bitset>  // static_cast<std::bitset<8>>
#include <sstream>  // std::stringstream
#include <cmath>
#include <climits>  // INT_MIN
#include <cctype>  // std::isdigit()
#include <iomanip>  // std::setprecision()
using namespace std;

#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for (ll i=0; i < n; i++)  // 0 ~ n-1
#define REPN(i, n) for (ll i=1; i <= n; i++)  // 1 ~ n
#define MIN(vec) min_element(ALL((vec)))  // イテレータのため、値を取り出すときは * を先頭につける
#define MAX(vec) max_element(ALL((vec)))
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define SUM(vec) accumulate(ALL((vec)), 0LL)  // 0 は初期値
#define COPY(vec1, vec2) copy(ALL(vec1), back_inserter(vec2))  // vec1をvec2にコピーする vec2は空にしておく必要あり
#define zero_pad(num) setfill('0') << std::right << setw(num)
#define space_pad(num) setfill(' ') << std::right << setw(num)

typedef long long ll;

const ll MOD7 = 1000000007;  // 1 000 000 007
const ll MOD9 = 1000000009;  // 1 000 000 009

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

    // input
    ll N;
    cin >> N;
    vector<ll> vec(N);
    REP(i, N) cin >> vec[i];
    
    // calculation
    ll res, partial, distance_0;
    
    vector<ll> res_vec;
    bool flag_plus;
    for (ll n = 0; n < 2; ++n) {
        res = 0;
        partial = 0;
        flag_plus = (n == 0); // plus 始まりか minus 始まりかのフラグ
        for (ll i = 0; i < N; ++i) {
            partial += vec[i];  // 累積和の更新
            distance_0 = abs(partial) + 1;  // 0 までの距離 + 1
            if (flag_plus) {
                // partial を + にしたいとき
                if (partial <= 0) {
                    partial += distance_0;
                    res += distance_0;
                }
            } else {
                // partial を - にしたいとき
                if (0 <= partial) {
                    partial -= distance_0;
                    res += distance_0;
                }
            }
            flag_plus = !flag_plus;
        }
        res_vec.push_back(res);
    }
    
    cout << *MIN(res_vec) << "\n";
    
    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    string s;

    cin >> n >> s;

    if (n % 2) {
        cout << "No" << endl;
        return (0);
    }

    for (int i = 0; i < n/2; i++) {
        if (s[i] != s[n/2+i]) {
            cout << "No" << endl;
            return (0);
        }
    }

    cout << "Yes" << endl;

}


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

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

  reverse(n.begin(), n.end());

  if(n[0]=='3') cout << "bon";
  else if(n[0]=='0' || n[0]=='1' || n[0]=='6' || n[0]=='8') cout << "pon";
  else cout << "hon";


  return 0;
}

Measured memory_bytes: 3592
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust)
import Data.List (sort)
import Control.Monad (forM, replicateM)

getInts :: IO [Integer]
getInts = map (fst . fromJust . C.readInteger) <$> C.words <$> B.getLine

rsort :: (Ord a) => [a] -> [a]
rsort = reverse . sort

main :: IO ()
main = do
    [n, m] <- getInts
    xs <- sort <$> getInts
    ys <- rsort <$> replicateM (fromIntegral m) getPair
    print $ sum $ solve xs ys
    where
        getPair = reverse <$> getInts
        solve [] _ = []
        solve xs [] = xs
        solve xs ([_,0]:ys) = solve xs ys
        solve (x:xs) zs@([a,b]:ys)
            | x < a = a : solve xs ([a,b-1]:ys)
            | otherwise = x : solve xs zs

Measured memory_bytes: 74108
program abc174b
  use iso_fortran_env
  implicit none
  integer(int64) :: n, d
  integer(int64),allocatable :: xy(:,:), x(:), y(:)
  read *, n, d
  allocate(xy(2,n))
  read *, xy
  x = xy(1,:)
  y = xy(2,:)
  print *, count(x*x+y*y<=d*d)
end program abc174b


Measured memory_bytes: 9364
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

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

int main() {
    int N;
    cin >> N;
    vector<lli> dp(N + 1, 1LL << 60);
    dp[0] = 0;
    REP(i, 0, N) {
        dp[i + 1] = min(dp[i + 1], dp[i] + 1);
        for (int j = 6; j + i <= N; j *= 6) {
            dp[i + j] = min(dp[i + j], dp[i] + 1);
        }
        for (int j = 9; j + i <= N; j *= 9) {
            dp[i + j] = min(dp[i + j], dp[i] + 1);
        }
    }
    cout << dp[N] << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <string>
#define REP(i,n) for (int i=0;i<(n);i++)
using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    int ans = 1;
    REP(i, n){
        if(ans < k){
            ans = ans * 2;
        }else{
            ans += k;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+5;
ll n,k,c,l[N],r[N],idxl[N],idxr[N];
char a[N];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>k>>c;
    for(ll i=1;i<=n;i++)cin>>a[i];
    for(ll i=1,prv=-1e9,cur=0;i<=n;i++){
        idxl[i]=prv;
        if(a[i]=='x'){
            l[i]=cur;
            continue;
        }
        if(i-prv>c){
            cur++;
            prv=i;
            l[i]=cur;
        }
    }
    for(ll i=n,nxt=1e9,cur=0;i>=1;i--){
        idxr[i]=nxt;
        if(a[i]=='x'){
            r[i]=cur;
            continue;
        }
        if(nxt-i>c){
            cur++;
            nxt=i;
            r[i]=cur;
        }
    }
    for(ll i=1;i<=n;i++){
        ll L=idxl[i],R=idxr[i];
        if(L==-1e9 || R==1e9){
            if(L==-1e9 && R==1e9)cout<<i<<"\n";
            else if(L==-1e9){
                if(r[R]<k)cout<<i<<"\n";
            }
            else{
                if(l[L]<k)cout<<i<<"\n";
            }
        }
        else{
            if(abs(R-L)>c){
                if(l[L]+r[R]<k)cout<<i<<"\n";
            }
            else{
                if(l[L]+r[R]<k+1)cout<<i<<"\n";
            }
        }
    }
}

Measured memory_bytes: 7936
from collections import deque
n,m=map(int, input().split( ))
e=[[] for _ in range(n)]

for _ in range(m):
    ai,bi=map(int, input().split( ))
    ai-=1;bi-=1
    e[ai].append(bi);e[bi].append(ai)

d=[-1]*n
p=[i for i in range(n)]
d[0]=0

q=deque()

q.append(0)

while q:
    v=q.popleft()
    for u in e[v]:
        if d[u]<0:
           d[u]=d[v]+1
           p[u]=v
           q.append(u)
        elif d[u]>d[v]+1:
            d[u]=d[v]+1
            p[u]=v
            q.append(u)
print("Yes")
for i in range(1,n):
    print(p[i]+1)

Measured memory_bytes: 95728
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
int main() {
   int k,x;
   cin >> k >> x;
   int l = x + k - 1;
   int m = x - k + 1;
   for(m;m<=l;m++){
      printf("%d ",m);
   }
   return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
 
int main(){
  int64_t N;
  cin >> N;
  int retval = 10, cnt = 0;
  int64_t B;
  for (int A=1; A<=(int)sqrt(N);A++){
    if (N%A) continue;
    B = N/A;
    cnt = 0;
    while (B>0){
      B /= 10;
      cnt++;
    }
    if (cnt < retval){
      retval = cnt;
      //cout << A << " " << B << endl;
    }
  }
  cout << retval << endl;
}
 

Measured memory_bytes: 256
N,M = map(int,input().split())
R = [0 for _ in range(N+1)]
R[1] = 1
B = [1 for _ in range(N+1)]
B[0] = 0
for _ in range(M):
    x,y = map(int,input().split())
    if R[x]==1:
        R[y] = 1
    B[x] -= 1
    B[y] += 1
    if B[x]==0:
        R[x] = 0
print(sum(R))

Measured memory_bytes: 51932
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
ll inf = llmax / 2;
double eps = 1e-11;

ll frsum[200000];
ll bksum[200000];

int main() {
    int N;
    cin >> N;
    int a[3 * N];
    for (int i = 0; i < 3 * N; i++) {
        cin >> a[i];
    }
    priority_queue<int, vector<int>, greater<int> > frque;
    priority_queue<int> bkque;
    ll fs = 0;
    ll bs = 0;
    for (int i = 0; i < N; i++) {
        frque.push(a[i]);
        bkque.push(a[3 * N - 1 - i]);
        fs += a[i];
        bs += a[3 * N - 1 - i];
    }
    frsum[0] = fs;
    bksum[N] = bs;
    for (int i = 0; i < N; i++) {
        ll frmin = frque.top();
        if (frmin < a[N + i]) {
            frque.pop();
            fs -= frmin;
            frque.push(a[N + i]);
            fs += a[N + i];
        }
        frsum[i + 1] = fs;
        ll bkmax = bkque.top();
        if (bkmax > a[2 * N - 1 - i]) {
            bkque.pop();
            bs -= bkmax;
            bkque.push(a[2 * N - 1 - i]);
            bs += a[2 * N - 1 - i];
        }
        bksum[N - 1 - i] = bs;
    }
    ll ans = llmin;
    for (int i = 0; i <= N; i++) {
        if (ans < frsum[i] - bksum[i]) {
            ans = frsum[i] - bksum[i];
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3956
#include<iostream>
using namespace std;
int main()
{
	int n;cin>>n;
	for(int i=0;i<n;i++)
	{
		int r,t;cin>>r>>t;
		if(r%100==0&&t%30==0)
		{
			cout<<r/100+5*(t/30)<<endl;
		}
		else if(t%30==0)
		{
			cout<<r/100+5*(t/30)<<" "<<r/100+1+5*(t/30)<<endl;
		}
		else if(r%100==0)
		{
			cout<<r/100+5*(t/30)<<" "<<r/100+5*(t/30+1)<<endl;
		}
		else
		{
			cout<<r/100+5*(t/30)<<" "<<r/100+1+5*(t/30)<<" ";
			cout<<r/100+5*(t/30+1)<<" "<<r/100+1+5*(t/30+1)<<endl;
		}
	}
	return 0;
}

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

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

using namespace std;
using lint=long long;

int main(){
	int n; cin>>n;
	vector<int> a(3*n);
	rep(i,3*n) cin>>a[i];

	multiset<lint> L(a.begin(),a.begin()+n);
	vector<lint> left(n+1);
	rep(i,n) left[0]+=a[i];
	rep(i,n){
		L.emplace(a[n+i]);
		lint x=*L.begin();
		L.erase(L.begin());
		left[i+1]=left[i]+a[n+i]-x;
	}

	multiset<lint> R(a.begin()+2*n,a.end());
	vector<lint> right(n+1);
	for(int i=n-1;i>=0;i--) right[n]+=a[2*n+i];
	for(int i=n-1;i>=0;i--){
		R.emplace(a[n+i]);
		lint x=*--R.end();
		R.erase(--R.end());
		right[i]=right[i+1]+a[n+i]-x;
	}

	lint ans=-1e15;
	rep(i,n+1) ans=max(ans,left[i]-right[i]);
	cout<<ans<<endl;

	return 0;
}


Measured memory_bytes: 12416
#include <bits/stdc++.h>
#define mod 1000000007
#define pb push_back
#define ll long long
using namespace std;
int main(){
	ll n,a,b;
	cin>>n>>a>>b;
	if(a>b){
		cout<<0<<endl;
		return 0;
	}
	else if(n==1 && a!=b){
		cout<<0<<endl;
		return 0;
	}
	else{
		ll ma = a + b*(n-1);
		ll mi = a*(n-1) + b;
		cout<<ma-mi+1<<endl;
		return 0;
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
#include <assert.h>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;

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

    return 0;
}

Measured memory_bytes: 256
-- 1_5_A
main =  do  cs <- getLine
            case cs of
                "0 0"   -> return ()
                cs      ->  do  putStrLn $ unlines $ make_a_rectangle $ map (\x -> read x::Int) $ words cs
                                main

make_a_rectangle :: [Int] -> [String]
make_a_rectangle xs = replicate (head xs) $ replicate (last xs) '#'

Measured memory_bytes: 3872
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000; // 10^9

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<int> x(n);
	for (int i = 0; i < n; i++) {
		cin >> x[i];
	}
	int l = x[0], r = x[0];
	for (int i = 0; i < n; i++) {
		l = min(l, x[i]);
		r = max(r, x[i]);
	}
	int ans = inf;
	for (int i = l; i <= r; i++) {
		int cost = 0;
		for (int j = 0; j < n; j++) {
			cost += (x[j] - i) * (x[j] - i);
		}
		ans = min(ans, cost);
	}
	cout << ans << '\n';
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
static const int dx[4] = { 0, 1, 0, -1 };
static const int dy[4] = { 1, 0, -1, 0 };
static const char dir[4] = { 'u', 'r', 'd', 'l' };
static const ll INF = 1 << 21;
static const ll MOD = 1e9 + 7;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N)
    {
        cin >> A[i];
    }
    ll ans = 0;
    for (int bit = 0; bit < 60; ++bit) {
        ll x0 = 0;
        ll x1 = 0;
        for (int i = 0; i < N; ++i) {
            // bit桁目を取り出す
            ll x = (A[i] >> bit) & 1;
            // bit桁目が0, 1どちらかを数える
            if (x == 0) {
                ++x0;
            } else {
                ++x1;
            }
        }
        // 指数
        ll p = 1;
        for (int i = 0; i < bit; ++i) {
            p *= 2;
            p %= MOD;
        }
        // x0*x1が0, 1の組み合わせの数
        ll cnt = (x0 * x1) % MOD;
        ans += cnt * p;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 2560
import java.io.{BufferedReader, InputStreamReader}

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val Array(n, k, q) = in.readLine.split("\\s+").map(_.toInt)

    val points = Array.fill[Int](n)(k - q)

    (1 to q).foreach { _ =>
      val a = in.readLine.toInt
      points(a - 1) += 1
    }

    (1 to n).foreach { nc =>
      System.out.println(
        if (points(nc - 1) > 0) {
          "Yes"
        } else {
          "No"
        }
      )
    }
  }
}


Measured memory_bytes: 39612
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pint = pair<int,int>;
ll mod = 1000000007,mod2 = 998244353;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL<<60;
ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

bool is_prime[1000010];

int main() {
    ll x,k,d,k2;
    cin >> x >> k>> d;
    x = abs(x);
    k2 = k;
    ll tmp = x-x/d*d;
    k -= x/d;
    if(k <= 0){
        cout << x-k2*d << endl;
        return 0;
    }else{
        if(k%2 == 1) cout << abs(tmp-d) << endl;
        else cout << tmp << endl;
    }
    return 0;
}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int main() {
    string s;
    cin >> s;
    s[3] = '8';
    cout << s << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i=0; i<N; ++i) {
    cin >> vec.at(i);
  }
  sort(vec.begin(),vec.end());
  int a=0;
  for (int i=N-1; i>=0; --i) {
    if ((N-1-i)%2 == 0) {
      a += vec.at(i);
    }else{
      a -= vec.at(i);
    }
  }
  cout << a << endl;
}

Measured memory_bytes: 256
/// input macro from https://qiita.com/tanakh/items/1ba42c7ca36cd29d0ac8
macro_rules ! read_value {($ next : expr , ($ ($ t : tt ) ,* ) ) => {($ (read_value ! ($ next , $ t ) ) ,* ) } ; ($ next : expr , [$ t : tt ; $ len : expr ] ) => {(0 ..$ len ) . map (| _ | read_value ! ($ next , $ t ) ) . collect ::< Vec < _ >> () } ; ($ next : expr , chars ) => {read_value ! ($ next , String ) . chars () . collect ::< Vec < char >> () } ; ($ next : expr , usize1 ) => {read_value ! ($ next , usize ) - 1 } ; ($ next : expr , $ t : ty ) => {$ next () . parse ::<$ t > () . expect ("Parse error" ) } ; }
macro_rules ! input_inner {($ next : expr ) => {} ; ($ next : expr , ) => {} ; ($ next : expr , $ var : ident : $ t : tt $ ($ r : tt ) * ) => {let $ var = read_value ! ($ next , $ t ) ; input_inner ! {$ next $ ($ r ) * } } ; }
macro_rules ! input {(source = $ s : expr , $ ($ r : tt ) * ) => {let mut iter = $ s . split_whitespace () ; let mut next = || {iter . next () . unwrap () } ; input_inner ! {next , $ ($ r ) * } } ; ($ ($ r : tt ) * ) => {let stdin = std :: io :: stdin () ; let mut bytes = std :: io :: Read :: bytes (std :: io :: BufReader :: new (stdin . lock () ) ) ; let mut next = move || -> String {bytes . by_ref () . map (| r | r . unwrap () as char ) . skip_while (| c | c . is_whitespace () ) . take_while (| c |! c . is_whitespace () ) . collect () } ; input_inner ! {next , $ ($ r ) * } } ; }
fn main() {
    input!(h: usize, w: usize, m: usize, s: [(usize, usize); m]);

    let mut x_count = vec![0; h];
    let mut y_count = vec![0; w];

    for &(x, y) in &s {
        x_count[x - 1] += 1;
        y_count[y - 1] += 1;
    }

    let mut x_vec = vec![0; h];
    let mut x_max = 0;
    let mut x_len = 0;
    for (i, &count) in x_count.iter().enumerate() {
        if x_max < count {
            x_vec[0] = i as u64;
            x_len = 1;
            x_max = count;
        }
        else if x_max == count {
            x_vec[x_len] = i as u64;
            x_len += 1;
        }
    }

    let mut y_vec = vec![0; w];
    let mut y_max = 0;
    let mut y_len = 0;
    for (i, &count) in y_count.iter().enumerate() {
        if y_max < count {
            y_len = 1;
            y_max = count;
        }
        else if y_max == count {
            y_len += 1;
        }
    }

    if s.iter()
        .filter(|&&(x, y)| (x_count[x - 1] == x_max) && y_count[y - 1] == y_max)
        .count()
        == (x_len * y_len)
    {
        println!("{}", x_max + y_max - 1);
    }
    else {
        println!("{}", x_max + y_max);
    }
}


Measured memory_bytes: 11488
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
typedef long long ll;
typedef vector<ll> vl;
#define rp(i, f, t) for (int i = f; i < t; i++)
#define pr(i, f, t) for (int i = t - 1; i >= f; i--)
#define ca(n, a) rp(ca_i, 0, n) cout << a[ca_i] << ((ca_i == n - 1) ? "\n" : " ")
#define za(n, a) rp(za_i, 0, n) a[za_i] = 0
#define be(a) a.begin(), a.end()
#define ce(a) cout << (a) << endl
long long gcd(long long a, long long b)
{
    return b ? gcd(b, a % b) : a;
}
int main()
{
    ll ans = 0;
    ll k;
    cin >> k;
    rp(a, 0, k)
    {
        rp(b, 0, k)
        {
            rp(c, 0, k)
            {
                ans += gcd(gcd(a + 1, b + 1), c + 1);
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
#define ll long long int 
using namespace std ;
int main(){
  int n ;
  cin >> n ;
  ll b[n] ;
  for(int i = 0 ; i < n - 1 ; i++) cin >> b[i] ;
  ll ans = b[0] ;
  for(int i = 1 ; i < n - 1 ; i++){
    //if(b[i] > b[i - 1]) ans += b[i - 1] ;
    //else ans += b[i - 1] ;
    ans += min(b[i] , b[i - 1]) ;
  }
  cout << ans + b[n - 2] ;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int mod(int a, int b) {
    if(a>b) return a-b;
    else return b-a;
}
int main( ){
    int n,k;
    cin>>n>>k;
    int arr[n],dp[n];
    for(int i=0;i<n;i++)
        cin>>arr[i];
    dp[0]=0;
    dp[1]=mod(arr[1],arr[0]);
    for(int i=2;i<n;i++) {
            dp[i]=INT_MAX;
        if(i>=k) {
            for(int j=1;j<=k;j++) {
                dp[i]=min(mod(arr[i],arr[i-j])+dp[i-j] , dp[i]);
            }
        } else {
            for(int j=1;(i-j)>=0;j++) {
                dp[i]=min(mod(arr[i],arr[i-j])+dp[i-j] , dp[i]);
            }
        }
    }
    cout<<dp[n-1];
    return 0;
}



Measured memory_bytes: 1024
program name
    implicit none
    character(100000):: s, f
    integer(4):: len_s,i, diff

    read*, s
    len_s = len_trim(s)

    f(1:1) = '0'
    do i=2,len_s
        f(i:i) = char(ichar('0')+ichar('1') - ichar(f(i-1:i-1)))
    end do

    diff = 0
    do i=1,len_s
        if (s(i:i) == f(i:i)) diff=diff+1
    end do

    print*, min(diff, len_s-diff)
end program name

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

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

    return 0;
}

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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
    string s;
    int n;
    cin >> s >> n;
    deque<char> data(all(s));
    int now = 0;
    rep(i, n) {
        int t;
        cin >> t;
        if (t == 1)
            now++;
        else {
            int F;
            char c;
            cin >> F >> c;
            if (F == 1) {
                if (now % 2 == 0)
                    data.push_front(c);
                else
                    data.push_back(c);
            } else {
                if (now % 2 == 1)
                    data.push_front(c);
                else
                    data.push_back(c);
            }
        }
    }
    if (now % 2 == 1) reverse(all(data));
    cout << string(all(data)) << endl;
}

Measured memory_bytes: 4032
from heapq import*
i=input
for s in[0]*int(i()):
 n,x,*y=int(i()),[]
 for _ in'_'*n:k,l,r=t=[*map(int,i().split())];x+=[t]*(l>r);y+=[[n-k,r,l]]*(l<=r)
 for x in x,y:
  h=[]
  for k,l,r in sorted(x):s+=l;heappush(h,l-r);s-=k<len(h)and heappop(h)
 print(s)

Measured memory_bytes: 51492
#include <iostream>

using namespace std;

void swap(int &x, int &b) {
	int temp = x;
	x = b;
	b = temp;
}

int main()
{
	int A, B, C;
	cin >> A >> B >> C;

	swap(A, B);
	swap(A, C);

	cout << A << " " << B << " " << C << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define cmin(a,b) (a>(b)?a=(b),1:0)
#define cmax(a,b) (a<(b)?a=(b),1:0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
namespace io
{
	int F()
	{
		 int F=1,n=0;
		 char ch;
		 while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		 ch=='-'?F=0:n=ch-'0';
		 while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
		 return F?n:-n;
	}
	long long G()
	{
		 long long F=1,n=0;
		 char ch;
		 while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		 ch=='-'?F=0:n=ch-'0';
		 while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
		 return F?n:-n;
	}
}
int R(int l,int r)
{
	return (rand()<<15|rand())%(r-l+1)+l;
}
const int M=998244353;
char s[3333]; 
bool b[4444][4444];
int f[4444][4444];
int sub[5][2]={{'3',0},{'4','3'},{0,'4'}};
int main()
{
	scanf("%s",s+1);
	int n=strlen(s+1);
	int R=0,B=0;
	for(register int i=1;i<=n;++i)
	{
		if(s[i]=='0')R+=2;
		if(s[i]=='1')++R,++B;
		if(s[i]=='2')B+=2;
	}
	int x=0,y=0;
	while(1)
	{
		int pl=0;
		for(register int i=1;i<=n;++i)
			if(s[i]=='0'||s[i]=='2'){pl=i;break;}
		if(pl==0)break;
		if(s[pl]=='0')
		{
			b[x][y+pl]=1;
			s[1]=sub[s[1]-'0'][0];
			++x;
		}
		else
		{
			b[x+pl][y]=1;
			s[1]=sub[s[1]-'0'][1];
			++y;
		}
		for(register int i=1;i<n;++i)
		{
			if(s[i]=='3')
			{
				if(s[i+1]!='0'&&s[i+1]!='3')s[i]='1',s[i+1]=sub[s[i+1]-'0'][1];
					else s[i]='0',s[i+1]=sub[s[i+1]-'0'][0];
			}
			else
			{
				if(s[i+1]!='2'&&s[i+1]!='4')s[i]='1',s[i+1]=sub[s[i+1]-'0'][0];
					else s[i]='2',s[i+1]=sub[s[i+1]-'0'][1];
			}
		}
		if(!s[n])--n;
	}
	f[0][0]=1;
	for(register int i=0;i<=R;++i)
		for(register int j=0;j<=B;++j)
			if(b[i][j]==0)
			{
				if(i)f[i][j]=(f[i][j]+f[i-1][j])%M;
				if(j)f[i][j]=(f[i][j]+f[i][j-1])%M;
			}
	printf("%d\n",f[R][B]);
	return 0;
}


Measured memory_bytes: 88832
program ABC043C
    implicit none
    integer(8)::N,cost,result,i,j
    integer(8),allocatable,dimension(:)::A

    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)

    do i=minval(A),maxval(A)
        cost=0
        do j=1,N
            cost=cost+(i-A(j))**2
        end do

        if(i==minval(A))then
            result=cost
        else if(cost<result)then
            result=cost
        end if
    end do

    print'(i0)',result
end program ABC043C

Measured memory_bytes: 256
package main

import (
	"fmt"
)

func main() {
	var n int
	var s string
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s", &s)
	rs := []rune(s)

	red := make([]int, n, n)
	green := make([]int, n, n)
	blue := make([]int, n, n)

	if rs[n-1] == 'R' {
		red[n-1], green[n-1], blue[n-1] = 1, 0, 0
	} else if rs[n-1] == 'G' {
		red[n-1], green[n-1], blue[n-1] = 0, 1, 0
	} else {
		red[n-1], green[n-1], blue[n-1] = 0, 0, 1
	}
	for i := n - 2; i >= 0; i-- {
		if rs[i] == 'R' {
			red[i], green[i], blue[i] = red[i+1]+1, green[i+1], blue[i+1]
		} else if rs[i] == 'G' {
			red[i], green[i], blue[i] = red[i+1], green[i+1]+1, blue[i+1]
		} else {
			red[i], green[i], blue[i] = red[i+1], green[i+1], blue[i+1]+1
		}
	}

	count := 0
	for i := 0; i < n; i++ {
		x := rs[i]
		for j := i + 1; j < n; j++ {
			y := rs[j]
			if x == y {
				continue
			}
			if x != 'B' && y != 'B' {
				count += blue[j]
				if 2*j-i < n && rs[2*j-i] == 'B' {
					count--
				}
			} else if x != 'G' && y != 'G' {
				count += green[j]
				if 2*j-i < n && rs[2*j-i] == 'G' {
					count--
				}
			} else {
				count += red[j]
				if 2*j-i < n && rs[2*j-i] == 'R' {
					count--
				}
			}
		}
	}

	fmt.Printf("%d\n", count)
}


Measured memory_bytes: 2012
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
#define rep(i, a, b) for(int i = int(a); i <= int(b); i++)
#define N 5005
int P[N], C[N];
int main() {
	int n, k;
	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;
	rep(i, 1, n) {
		int p = i;
		ll cnt = 0, len = 0, allsum = 0, sum = 0;
		while(1) {
			++len;allsum += C[p];p = P[p];
			if(p == i)break;
		}
		cnt = 0, sum = 0;
		while(1) {
			++cnt;sum += C[p];
			if(cnt > k)break;
			ll num = (k - cnt) / len;
			ll scr = sum + max(0LL, allsum) * num;
			Ans = max(Ans, scr);
			
			p = P[p];if(p == i)break;
		}
		
	}
	cout << Ans << endl;
	return 0;
} 

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
ll gcd(ll a,ll b){ while(b^=a^=b^=a%=b); return a;}
int main(){
ll a;
while(cin>>a){

if(a==0)cout<<"1"<<endl;
else cout<<"0"<<endl;
}


	return 0;
}

Measured memory_bytes: 3632
#include <stdio.h>
#include <vector>
#include <cmath>

using namespace std;

int N;

class Circle {
public:
    Circle(long long int x, long long int y, long long int r) : x(x), y(y), r(r), done(0), min_cost(9e300) {}
    double get_dist(Circle &to) {
        double t = sqrt((x - to.x) * (x - to.x) + (y - to.y) * (y - to.y)) - r - to.r;
        if (t < 0) return 0.0;
        return t;
    }
    double update_min(Circle &from) {
        double cost = get_dist(from) + from.min_cost;
        if (cost < min_cost) min_cost = cost;
        return min_cost;
    }
    double x;
    double y;
    double r;
    bool done;
    double min_cost;
};

double dijk(vector<Circle>& v)
{
    int node = 0;
    while (1) {
        double min_v = 9.1e300;
        int min_idx;
        for (int i=1;i<N+2;i++) {
            if (v[i].done) continue;
            double t = v[i].update_min(v[node]);
            if (t < min_v) {
                min_idx = i;
                min_v = t;
            }
        }
        if (min_idx == 1) {
            return v[1].min_cost;
        }
        v[min_idx].done = true;
        node = min_idx;
    }
}

int main()
{
    long long int xs, ys, xt, yt;
    scanf("%lld %lld %lld %lld", &xs, &ys, &xt, &yt);

    scanf("%d", &N);
    vector<Circle> circles;
    circles.reserve(N+2);
    circles.emplace_back(xs, ys, 0.0);
    circles[0].done = true;
    circles[0].min_cost = 0.0;
    circles.emplace_back(xt, yt, 0.0);
    for (int i=0;i<N;i++) {
        long long int x, y, r;
        scanf("%lld %lld %lld", &x, &y, &r);
        circles.emplace_back(x, y, r);
    }
    printf("%.20f\n", dijk(circles));
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    set<pair<LL,int>> paths;
    set<pair<int,pair<LL,int>>> eds;
    DRII(n,m);
    VI res(n);
    REP(i,m){
        DRIII(l,r,c);
        l--;
        r--;
        eds.insert({l,{c,r}});
    }
    paths.insert({0,0});
    int curr=0;
    auto it=eds.begin();
    LL cc=0;
    while (curr<n-1 and !paths.empty())
    {
        res[curr]=cc;
        while (it!=eds.end() and it->F<=curr){
            paths.insert({cc+(it->S).F,(it->S).S});
            it++;
        }
        auto it2=paths.begin();
        while (it2!=paths.end() and it2->S<=curr) paths.erase(it2++);
        if (paths.empty()) continue;
        int next=it2->S;
        cc=it2->F;
        REPP(i,curr+1,next+1){
            res[i]=cc;
        }
        curr=next;
    }
    if (curr==n-1){
        printf("%lld\n",cc);
    }
    else{ printf("-1\n");}
}




Measured memory_bytes: 12416
#include <iostream>

void call(int n) {
    using std::cout;
    using std::endl;
    int i = 1;
    do {
        int x = i;
        if ( x % 3 == 0 ){
            cout << " " << i;
            continue;
        }
        do {
            if ( x % 10 == 3 ){
                cout << " " << i;
                break;
            }
            x /= 10;
        } while(x);
    } while(++i <= n);
    cout << endl;
}

int main() {
    int n;
    std::cin >> n;
    call(n);
}



Measured memory_bytes: 3116
# coding: utf-8
isgdprime=[1 for i in range(300001)]
gdprimes=[]
for i in range(6):
    isgdprime[i]=0
for i in range(300001):
    if isgdprime[i]==1 and (i%7==1 or i%7==6):
        gdprimes.append(i)
        j=i*2
        while j<300001:
            isgdprime[j]=0
            j+=i
    else:
        isgdprime[i]=0

while 1:
    n=int(input())
    if n==1:
        break
    print(n,end=':')
    for i in range(len(gdprimes)):
        if n%gdprimes[i]==0:
            print('',gdprimes[i],end='')
    print()


Measured memory_bytes: 9256
N, M = map(int, input().split())
dic = {}
if N == 1:
    print(1)
    exit()
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        if 2 not in dic:
            dic[2] = 1
        else:
            dic[2] += 1
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            if f not in dic:
                dic[f] = 1
            else:
                dic[f] += 1
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        if n not in dic:
            dic[n] = 1
        else:
            dic[n] += 1
        a.append(n)
    return a

n = 10 ** 9
k = 2 * 10 ** 5
mod = 10**9 + 7

modinv_table = [-1] * (k+1)
modinv_table[1] = 1
for i in range(2, k+1):
    modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

prime_factorize(M)
res = 1
for k, v in dic.items():
    if v == 1:
        res *= N % mod
    else:
        res *= binomial_coefficients((v + N - 1), (N - 1)) % mod
print(res % mod)


Measured memory_bytes: 74512
#include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007;
#define INF (1<<28)
#define int long long
#define double long double
#define floot10 cout<<fixed<<setprecision(10)
int dp[100100];

signed main(){
  int n,m,s,t;
  cin>>n>>m>>s>>t;
  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>> > q;
  int cost[100100];
  lp(i,n){
    cin>>cost[i+1];
  }
  vector<pair<int,int> > v;
  lp(i,m){
    int a,b;
    cin>>a>>b;
    if(a>t) continue;
    if(a<s) a=s;
    v.push_back({a,b});
  }
  sort(v.begin(),v.end());
  q.push({0,s});
  lp(i,v.size()){
    int fr=v[i].first;
    int to=v[i].second;
    while(1){
      if(q.top().second<fr) q.pop();
      else break;
    }
    q.push({q.top().first+cost[to],to});
  }
  while(1){
    if(q.top().second<t) q.pop();
    else break;
  }
  cout<<q.top().first<<endl;
  return 0;
}




Measured memory_bytes: 8204
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

void b_collectiong_balls(void) {
	int n, k;
	int* x;
	cin >> n >> k;
	x = (int*)calloc(n, sizeof(int));
	for (int i = 0; i < n; i++)cin >> x[i];
	int sum = 0;
	int sub_a, sub_b;
	for (int i = 0; i < n; i++) {
		sub_a = abs(x[i]);
		sub_b = abs(k - x[i]);
		if (sub_a < sub_b) sum += sub_a<<1;
		else               sum += sub_b<<1;
	}

	cout << sum << endl;

	free(x);
}

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

Measured memory_bytes: 3516