akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int  long long int
#define pb push_back
#define fe first
#define lb lower_bound 
#define ub upper_bound
#define pii pair<pair<int,int>,pair<int,int> >
#define se second
#define endl "\n"
#define pi pair<int, int>
#define mi          map<int,int>
#define mii         map<pi,int>
#define vi vector<int> 
#define vvi vector<vi>
#define bs   binary_search
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i<=b;i++)	
#define all(c) (c).begin(),(c).end()
#define sz(x)       (int)x.size() 
#define PI 3.14159265358979323846

 const int N=2e5+5;

 int mod= 1e9+7;
 int dx[4]={0,0,+1,-1};
 int dy[4]={+1,-1,0,0};  
 
 
int po(int a, int b) {
	a %= mod;
	int res = 1;
	while (b > 0) {
		if (b & 1)
			res = (res * a) % mod;
		a =(a*a) % mod;
		b >>= 1;
	}
	 return res;	
   }
  

   
     void solve() {
	       int n,k;
	       cin>>n>>k;
	       vi v(n,0);
	       rep(i,0,n) cin>>v[i];
	       sort(all(v));
	       
	       int L=-1e18,R=1e18;
	       while(R-L>1) {
			      int MID=(L+R)>>1,cnt=0;
			      rep(i,0,n) {
					      int l=i,r=n;
					    
					     if(v[i]<0) {
					      while(r-l>1 ) {
 							      int mid=(l+r)>>1;
 							      if(v[i]*v[mid]<=MID) r=mid;
 							      else l=mid; 
							  }
							  cnt+=(n-r);
						  }
						  
						  else {
							   while(r-l>1) {
								       int mid=(l+r)>>1;
								       if(v[i]*v[mid]<=MID) l=mid;
								       else r=mid;
								   }
								   cnt+=(l-i);						  
							  }			      
					  }
					  
					  if(cnt<k) L=MID;
						else R=MID;
			   }
			   
			   cout<<R<<endl;  
	       	  		   		   
	 }

    
int32_t main() {
	cool;
	int t=1;
	//cin>>t;
	while(t--)
	solve();


return 0;	   
}


Measured memory_bytes: 1792
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#include <iostream>
#include <math.h>
#include <string>
#include <string.h>
using namespace std;

int main()
{
	string str[7]
	{
		"SUN",
		"MON",
		"TUE",
		"WED",
		"THU",
		"FRI",
		"SAT"
	};

	string S;
	cin >> S;

	rep(i,7)
		if (S == str[i])
		{
			cout << 7 - i;
			break;
		}

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define nl cout<<"\n"
#define pb push_back
#define S second
#define F first
typedef long long int ll;


int main(){
	ios_base :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	//~ freopen("input.txt", "r", stdin); 
    //~ freopen("output.txt", "w", stdout); 
	int n;
	string s;
	cin>>n>>s;
	if(s.length()<=n){
		cout<<s<<endl;
	}
	else{
		string k=s.substr(0,n);
		cout<<k;
		for(int i=0;i<3;i++) cout<<'.';
	}
	return 0;
}


Measured memory_bytes: 3628
#include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

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

#define mod 1000000007LL
#define eps 0.0001
#define pi acos(-1)
#define INF 10000000000000000

#define rep(i, n) for(int i = 0;i < n;++i)
#define rep1(i, n) for(int i = 1;i < n;++i)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define chmin

#define lset(h, n) memset(h, n, sizeof(h));
    

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    // ---------------------
    // コーディング開始
    // ---------------------
    ll n,m;
    cin >> n >> m;
    if(n*2 >= m){
        //cが少ない
        pri(m/2);
        return 0;
    }
    ll let2 = m-n*2;
    pri(let2/4+n);
    
    return 0;
}



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

using namespace std;

long long attacks(int H, int N, vector<int>& A, vector<int>& B) {
	int copy = H;
	H += *max_element(A.begin(), A.end());
	vector<long long> prev, dp(H + 1, numeric_limits<int>::max());
	
	// Dynamic Programming
	dp[0] = 0;
	for (int i = 1; i <= N; i++) {
		prev = dp;
		for (int h = 1; h <= H; h++) {
			if (A[i - 1] > h)
				dp[h] = prev[h];
			else
				dp[h] = min(dp[h - A[i - 1]] + B[i - 1], prev[h]);
		}
	}
	
	return *min_element(dp.begin() + copy, dp.end());
}

int main() {
	int H, N;
	cin >> H >> N;
	vector<int> A(N), B(N);
	for (int i = 0; i < N; i++)
		cin >> A[i] >> B[i];
	cout << attacks(H, N, A, B);
	return 0;
}

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

long long int a, b, c, d;

int main(){
  cin >> a >> b >> c >> d;
  long long int range = b - a + 1;
  long long int p = b / c - (a - 1) / c;
  long long int q = b / d - (a - 1) / d;
  long long lcm = c / __gcd(c, d) * d;
  long long int r = b / lcm - (a - 1) / lcm;
  cerr << range <<" "<< p <<" "<< q <<" "<< r<<endl;
  cout << range -p - q + r << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}





int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,k;cin >> n >> k;
	vector<ll> x(n),y(n);
	rep(i,n) cin >> x.at(i) >> y.at(i);
	ll ans = 4e18;
	rep(x1,n){
		for(int x2 = x1 + 1; x2 < n; x2++){
			rep(y1,n){
				for(int y2 = y1 + 1; y2 < n; y2++){
					ll xmin = min(x.at(x1), x.at(x2)), xmax = max(x.at(x1), x.at(x2)), ymin = min(y.at(y1), y.at(y2)), ymax = max(y.at(y1), y.at(y2));
					int cnt = 0;
					rep(i,n){
						if(x.at(i) >= xmin && x.at(i) <= xmax && y.at(i) >= ymin && y.at(i) <= ymax) cnt++;
					}
					if(cnt >= k) ans = min(ans, (xmax - xmin) * (ymax - ymin));
				}
			}
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void) {
  int n, i, d;
  scanf("%d", &n);
  char s[n + 20], ans[n + 20];
  scanf("%s", s);
  scanf("%d", &d);
  for(i = 0; i < n && d > 0; ++i) {
    ans[i] = '1';
    if(s[i] == '0') --d;
  }
  if(!d) {
   for(; i < n; ++i) ans[i] = s[i];
   ans[n] = 0;
  } else {
    for(i = n - 1; i >= 0 && d > 0; --i) {
      if(s[i] == '1') ans[i] = '0', d--;
    }
    ans[n] = 0;
  }
  /*for(i = 0; i < n; ++i) printf("%d ", ans[i]);
  printf("\n");
  for(i = 0; i < n; ++i) printf("%d ", s[i]);
  printf("\n");*/
  printf("%s\n", ans);
  return 0;
}


Measured memory_bytes: 2668
using System;
using System.Web;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Numerics;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using static System.Math;
 

class P
{
    static void Main()
    {
        string s = Console.ReadLine();
        string t = Console.ReadLine();
        for (int i = 0; i < s.Length; i++)
        {
            t = t.Last() + t.Substring(0, s.Length - 1);
            if(s == t)
            {
                Console.WriteLine("Yes");
                return;
            }
        }
        Console.WriteLine("No");
    }
}


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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for(auto i = k; i != n; i++)
#define rrep(i, n) RREP(i, n, 0)
#define RREP(i, n, k) for(auto i = n; i != k; i--)
#define all(x, r) (x), (x)+(r)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " " << x << endl
#define exst(x, data) (data).find(x) != (data).end()
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define np next_permutation
#define b32 bitset<32>

using ll = long long;
using Pii = pair<int,int>;
using Tiii = tuple<int, int, int>;
template<class T>using V = vector<T>;

const long long MOD = 1000000000 +7;
ll dp[101][10];

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

    int H, W, K; cin >> H >> W >> K;

    dp[0][1] = 1;
    REP(h,1,H+1) {
        rep(p,pow(2,W-1)) {
            // 正当性チェック
            bool ok = true;
            REP(i,1,W)
                if((p>>i)&1 && (p>>(i-1))&1) {
                    ok = false; break;
                }
            if(!ok) continue;

            REP(k,1,W+1) {
                if(k>1 && (p>>(k-2))&1) {
                    dp[h][k] = (dp[h][k]+dp[h-1][k-1]) % MOD;
                    // cout << h << " " << p << " " << k << " L" << endl;
                } else if((p>>(k-1))&1) {
                    dp[h][k] = (dp[h][k]+dp[h-1][k+1]) % MOD;
                    // cout << h << " " << p << " " << k << " R" << endl;
                } else {
                    dp[h][k] = (dp[h][k]+dp[h-1][k]) % MOD;
                    // cout << h << " " << p << " " << k << " C" << endl;
                }
            }
        }
    }
    cout << dp[H][K] << endl;

    // rep(h,H+1) {
    //     rep(w,W+1) {
    //         cout << dp[h][w] << " ";
    //     } cout << endl;
    // }
    return 0;
}


Measured memory_bytes: 256
from itertools import product
from collections import defaultdict

def solve(S):
    def helper(s):
        n = len(s)
        d = defaultdict(int)
        for p in product((True,False),repeat=n):
            t = [-1]*(n+1)
            t[-1] = sum(p)
            i = 0
            j = n-1
            for c,f in zip(s,p):
                if f:
                    t[i] = c
                    i += 1
                else:
                    t[j] = c
                    j -= 1
            d[tuple(t)] += 1
        return d

    S = [ord(c)-ord('a') for c in S]
    n = len(S)//2
    d1 = helper(S[:n])
    d2 = helper(S[:n-1:-1])
    res = 0
    for k,c in d1.items():
        res += c*d2[k]

    return res


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


Measured memory_bytes: 451968
#include<stdio.h>
#include<stdlib.h>
#define N 2000000
int n=1,h=1;
int parent(int i){
  return i/2;

}
int left(int i){
  return 2*i;

}

int right(int i){
  return 2*i+1;

}

void maxHeapify(int* A,int i){
  int l = left(i),r = right(i),largest,tmp;;
  if(l<=n && A[l]>A[i])largest = l;
  else largest = i;
  if(r<=n&&A[r]>A[largest])largest=r;
  if(largest != i){
    tmp=A[i];
    A[i]=A[largest];
    A[largest]=tmp;
    maxHeapify(A,largest);
  }

}
void buildMaxHeap(int* A){
  int i;
  for(i=n/2;i>=1;i--){
    maxHeapify(A,i);
  }
  
}


void heapIncreaseKey(int* A,int i,int key){
  int tmp; 
  if(key<A[i])return;
  A[i]=key;
  while(i>1 && A[parent(i)]<A[i] ){
    tmp=A[i];
    A[i]=A[parent(i)];
    A[parent(i)]=tmp;
    i=parent(i);
  }
}

void maxHeapInsert(int* A,int key){
  n++;
  A[n]=-500001;
  heapIncreaseKey(A,n,key);
}


int heapExtractMax(int* A){
  int max;

  if (n < 1)return ;
  max=A[1];
  A[1]=A[n];
  n--;
  maxHeapify(A,1);
  return max;
}



int main(){
  int heap[N];
  int i;
  char c[8];

  while(1){
    scanf("%s",c);
    if(c[0]=='e'){
      if(c[1]=='n')break;
      else printf("%d\n",heapExtractMax(heap));
    }
    if(c[0]=='i'){
      scanf("%d",&i);
      maxHeapInsert(heap,i);
    }
    
  }
  
  return 0;
}

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

int main() {
    ll N,M,V,P;
    cin >> N >> M >>V >> P;
    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin >> A[i];
    }
    sort(A.rbegin(),A.rend());
    ll sum[N+1] = {};
    for(int i=0;i<N;i++){
        sum[i+1] += sum[i];
        sum[i+1] += A[i];
        if(i < P){
            continue;
        }
        if((i-P+1)*(A[i]+M) < (sum[i]-sum[P-1])+M*max(V-(P-1)-(N-1-i)-1,0LL) || A[i] +M < A[P-1]){
            // cerr << "sum=" <<sum[i] -sum[P-1] << endl;
            // cerr <<"i=" <<i <<"A[i]=" << A[i]<< "canNot" << endl;
            cout << i<<endl;
            return 0;
        }

        
    }
    cout << N <<endl;
    return 0;
}

Measured memory_bytes: 1792
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
 
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
 
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define  ff          first
#define  ss          second
#define  pb          push_back
#define  IOS         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define  lb          lower_bound
#define  ub          upper_bound
#define  D2(x)       { cerr << "[" << #x << ": "; for(auto it:x) cerr << it << " "; cerr << "]\n"; }
#define  all(x)      (x).begin(), (x).end()
#define  mem(a)      memset(a , 0 ,sizeof a)
#define  deb(x)      cout << #x << " - " << x << "\n";
#define  ps(x,y)     fixed<<setprecision(y)<<x
#define  setbit(x)   __builtin_popcount(x)
#define  deb2(x,y)   cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define  rep(i,a,b)  for(int i=a ; i<b ; ++i)
 
#define PI 3.141592653589793
const ll p = 5;
const ll INF = 1000000000;
const ll MAX_N = 2e5 + 2;

void solve(){
    int k; cin >> k;
    int res = 7,mod = 9*k;
    for(int i=1;i<=2000000;++i){
        res *= 10;
        res %= mod;
        if(res == 7) {cout << i; return;}
    }
    cout << "-1";
}

int main() {
    IOS;
    solve();
}

Measured memory_bytes: 3640
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(){
  string divide[4]={"dream","dreamer","erase","eraser"};
  string S;
  cin>>S;
  reverse(S.begin(),S.end());
  for(int i=0;i<4;i++) reverse(divide[i].begin(),divide[i].end());
  
  for(int i=0;i<S.length();){
    bool can2=false;
  	  for(int j=0;j<4;j++){
      	    string d=divide[j];
    		if(S.substr(i,d.size())==d){
        		can2=true;
        		i+=d.size();
        }
    }
      if(!can2){
      	cout<<"NO"<<endl;
        return 0;
      }
  }
  cout<<"YES"<<endl;
  return 0;
}

Measured memory_bytes: 512
let for_range s e step f =
  let rec loop i =
    if i <= e then begin f i;loop (i + step) end
  in loop s;;
 
let sieve num =
  let p = Array.init num (fun x -> x mod 2) in
  let rec loop i =
    if i*i < num then
      begin
        if p.(i) = 1 then for_range (i*i) num (i*2) (fun x -> p.(x) <- 0);
        loop (i+2)
      end
  in
  p.(0) <- 0; p.(1)<-(0);p.(2)<-1;loop 3;p
;;
 
let () =
  let p = sieve 50000 in
  let rec read () =
    let n = read_int () in
    if n = 0 then ()
    else
      begin
        let count = ref 0 in
        if n = 4 then count := 1
        else
          if n mod 2 = 1 then
            if p.(n-2) = 1 then incr count else ()
          else
            begin
              let m = n/2 in
              for_range 3 m 2 (fun i -> if p.(i)=1 && p.(n-i)=1 then incr count)
            end
        ;print_int !count;print_newline ();read ()
      end
  in read ()
;;

Measured memory_bytes: 4044
#coding=UTF-8

#ゴールに関係無い袋小路で永パされると、探索が収束しない
#かつ答えがinfでないということで 修正

#表にあるかを尋ねるのが重いので
#データ構造を変えて回避する
mozir=input()
hyo=mozir.split(' ')

N=int(hyo[0])
M=int(hyo[1])

#idx番目の都市に繋っている道とコストのタプルを持つリスト
tuuro=[]
# [] はコピーがしんどいものだったりするので
# 参照でごまかしたりしてくる
# なので1個づつ書いている
for idx in range(0,N,1):
    tuuro.append([])


A=[]
B=[]

for idx in range(0,M,1):
    mozir=input()
    hyo=mozir.split(' ')
    tuuro[int(hyo[0])-1].append((int(hyo[1])-1,int(hyo[2])))

#print(tuuro)
#普通の経路探索のようなことをする
#永パ
tmp_score=[None]*N #マイナス点のときもある
tmp_score[0]=0
tansaku=[0]
loopconut=0
while len(tansaku) > 0:
    loopconut=loopconut+1
    next_tansaku=[]
    for mono in tansaku:
        for deru in tuuro[mono]:
            kouho_ten=tmp_score[mono]+deru[1]
            if tmp_score[deru[0]] == None or kouho_ten > tmp_score[deru[0]]:
                tmp_score[deru[0]]=kouho_ten
                next_tansaku.append(deru[0])

    tansaku=list(set(next_tansaku))
    if loopconut==N-1:
        tmp_ans=tmp_score[N-1] #cannot be None
    elif loopconut==2*N-1:
        #period must be eqal or less than N
        if tmp_score[N-1] > tmp_ans:
            print('inf')
            break
        else:
            #関係の無い無限ループ
            print(tmp_score[N-1])
            break
else:
    print(tmp_score[N-1])


Measured memory_bytes: 3444
import sys
input = sys.stdin.readline

#dojkstra
import itertools
import queue
G=[]
#infの値に注意
inf=3*10**15
V, E, R=map(int,input().strip().split(' '))
rlis=list(map(int,input().strip().split(' ')))

cost=[[inf]*V for i in range(V)]
for i in range(E):
    fr, to, co=map(int,input().strip().split(' '))
    cost[fr-1][to-1]=co
    cost[to-1][fr-1]=co
def dijkstra(s):
    d=[inf]*V
    used=[False]*V
    d[s]=0
    while True:
        v=-1
        for u in range(V):
            if used[u]==False and(v==-1 or d[u]<d[v]):
                v=u
        if v==-1:
            break
        used[v]=True
        for u in range(V):
            d[u]=min(d[u],d[v]+cost[v][u])
    return d

dist=[None] * V
for i in rlis:
    dist[i-1]=dijkstra(i-1)

ans=inf
for rperm in itertools.permutations(rlis):
    a_tmp=0
    for j in range(R-1):
        a_tmp+=dist[rperm[j]-1][rperm[j+1]-1]
    ans=min(ans,a_tmp)

print(ans)


Measured memory_bytes: 4848
import java.util.*;

public class Main {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        StringBuilder s2 = new StringBuilder();
        for(int i = 0; i < s.length(); i++)
        	s2.append('x');

        System.out.println(s2.toString());
    }
}


Measured memory_bytes: 21972
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+5;
const int inf=0x3f3f3f3f;
long long prime[maxn],a[maxn];
long long inz[maxn],gs[maxn];
long long getprime(long long n)
{
    long long cnt=0;
    for(int i=2;i<n;i++)
    {
        if(a[i]==0) prime[cnt++]=i;
        for(int j=0;j<cnt&&prime[j]*i<n;j++)
        {
            a[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
    return cnt;
}
long long fj(long long n)
{
    long long k=0;
    long long cnt=getprime(10000000);
    for(int i=0;i<cnt;i++)
    {
        if(n%prime[i]==0)
        {
            while(n%prime[i]==0)
            {
                inz[k]=prime[i];
                gs[k]++;
                n/=prime[i];
            }
            k++;
        }
        if(n==1) break;
    }
    if(n!=1) {inz[k]=n;gs[k]=1;}
    k++;
    return k;
}
long long js(long long n)
{
	long long sum=0;
	for(int i=1;i<=n;i++)
	{
		sum+=i;
		if(sum>n)
			return i-1;
		else if(sum==n)
			return i;
	}
}
int main()
{
    long long n,ans=0;
	cin>>n;
    long long cnt=fj(n);
    for(int i=0;i<cnt;i++)
    {
        if(inz[i]!=0)
        { 
        	ans+=js(gs[i]);
        //	cout<<inz[i]<<'^'<<gs[i];
        } 
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 86948
#include <bits/stdc++.h>
using namespace std;
 
#define for_(i,a,b) for(int i=(a);i<(b);++i)
 
class UnionFind {
private:
    vector< int > data;
    int num_of_g;
     
public:
    UnionFind(int n) : data(n, -1), num_of_g(n) {}
     
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
         
        if (x == y) return 0;
         
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        --num_of_g;
         
        return 1;
    }
     
    bool sameSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int getNumOfG() { return num_of_g; }
};
 
int n, used[26], indeg[26], outdeg[26];
 
void solve() {
    memset(indeg, 0, sizeof(indeg));
    memset(outdeg, 0, sizeof(outdeg));
    fill(used, used + 26, 1);
     
    UnionFind uf(26);
     
    for_(i,0,n) {
        string s;
        cin >> s;
        int a = s[s.size() - 1] - 'a', b = s[0] - 'a';
        ++indeg[a];
        ++outdeg[b];
        uf.unionSet(a, b);
        used[a] = used[b] = 0;
    }
     
    bool ok = (uf.getNumOfG() - accumulate(used, used + 26, int())) == 1;
    for_(i,0,26) ok &= (indeg[i] == outdeg[i]);
    cout << (ok ? "OK" : "NG") << endl;
}
 
int main() { while (cin >> n, n) { solve(); } }

Measured memory_bytes: 1200
from collections import deque
N, Q = map(int, input().split())
ab = [list(map(int,input().split())) for _ in range(N-1)]
px = [list(map(int,input().split())) for _ in range(Q)]
tree = [[] for _ in range(N)]
ans = [0]*N
for a,b in ab:
  tree[a-1].append(b-1)
  tree[b-1].append(a-1)
  
for p,x in px:
  ans[p-1]+=x
  
q = deque([0])
judge = [False]*N
while q:
  a=q.pop()
  judge[a]=True
  for i in tree[a]:
    if judge[i]:
      continue
    ans[i]+=ans[a]
    q.append(i)
    
print(' '.join(map(str,ans)))

Measured memory_bytes: 153324
int a[100];
main(i,j,n,r,x){
  for(;scanf("%d",&n),n;printf("%d\n",r)){
    for(i=0;i<n;i++)
      scanf("%d",a+i);
    for(r=i=0;i<n;i++)
      for(j=0;j<n-1;j++)
        if(a[j]>a[j+1])x=a[j],a[j]=a[j+1],a[j+1]=x,r++;
  }
  return 0;
}

Measured memory_bytes: 600
package main

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

var sc = bufio.NewScanner(os.Stdin)
var c [500050]int

func main() {
	scanInit()

	s := nextStr()
	n := len(s) + 1

	for i := 0; i < n-1; i++ {
		if s[i] == byte('<') {
			c[i+1] = c[i] + 1
		}
	}
	//fmt.Println("DBG", c[:n]) //! DBG

	for i := n - 2; i >= 0; i-- {
		if s[i] == byte('>') {
			if c[i] == 0 {
				c[i] = c[i+1] + 1
				continue
			}
			if c[i] <= c[i+1] { //1つ前が小さすぎる場合
				c[i] = c[i+1] + 1
			}
		}
	}

	ans := 0
	for i := 0; i < n; i++ {
		ans += c[i]
	}

	//fmt.Println("DBG", c[:n]) //! DBG
	fmt.Println(ans)
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func chmin(target *int, cmp int) {
	if *target > cmp {
		*target = cmp
	}
	return
}

// ---- readfunc
func scanInit() {
	const cap = 600 * 1024 // default=64*1024
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}


Measured memory_bytes: 6756
import sys
N,M = map(int,input().split())
List =[]
for i in range(N):
    tmp_a,tmp_b = map(int,input().split())
    List.append([tmp_a,tmp_b])
List.sort()
sum_a,sum_b = 0,0
for i in range(N):
    if M-sum_b > List[i][1]:
        sum_a += List[i][0]*List[i][1]
        sum_b += List[i][1]
    else:
        sum_a += List[i][0]*(M-sum_b)
        sum_b += M-sum_b
        print(sum_a)
        sys.exit()

Measured memory_bytes: 20072
import java.util.*;
import java.lang.Math;
public class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int a = sc.nextInt();
      int b = sc.nextInt();
      int c = sc.nextInt();
      int d = sc.nextInt();
      int ab= Math.abs(a-b);
      int bc= Math.abs(b-c);
      int ac= Math.abs(a-c);
      if(ac<=d){
      	System.out.println("Yes");
      }else if(ab<=d&&bc<=d){
      	System.out.println("Yes");
      }else{
      	System.out.println("No");
      }
   }
}

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

using namespace std;
struct I{
    I(){
        ios::sync_with_stdio(false);cin.tie(0);
        //cout<<fixed<<setprecision(10);
    }
}init;

typedef long long LL;
const LL MOD=1e9+7;
    
int main(){
    int N;
    cin>>N;
    map<int,int> z;
    vector<int> A(N);
    for(auto &it:A){
        cin>>it;
        z[it]=0;
    }
    int cnt=0;
    for(auto &it:z)it.second=cnt++;
    for(auto &it:A)it=z[it];
    cnt=0;
    for(int i=0;i<N;i+=2){
        if(A[i]%2){
            cnt++;
        }
    }
    cout<<cnt<<endl;

    return 0;
}


Measured memory_bytes: 5376
n = int(input())   
s = [input() for i in range(n)]

a = 0
b = 0
ab = 0
ba = 0
for i in s:
  if i[0] == "B":
    b += 1
  if i[-1] == "A":
    a += 1
  if i[0] == "B" and i[-1] == "A":
    ba += 1
  ab += i.count("AB")
  
if ba != 0 and ba == b:
  aa = a - 1
else:
  aa = a
if ba != 0 and ba == a:
  bb = b - 1
else:
  bb = b
  
print(min(aa, bb) + ab)

Measured memory_bytes: 3700
N = int(input())
W = list(map(int, input().split()))

# 累積和 => O(N)
S = [0] * N
S[0] = W[0]
for i in range(N-1):
  S[i+1] = S[i] + W[i+1]
  
# S[0] = W[0]
# S[1] = W[0] + W[1]
# S[2] = W[0] + W[1] + W[2]
# S[N-1] = W[0] + W[1] + ... + W[N-1]

# S[i] = W[0] + ... + W[i]
# S[N-1] - S[i] = W[i+1] + ... + W[N-1]

m = 10**10
for i in range(N-1):
  m = min(m, abs(S[N-1] - 2*S[i]))

print(m)

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

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
 
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int n, x;
 
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif
	
	cin >> n;
	bool ok = true;
	forn(i, n) {
		cin >> x;
		if (x % 2 == 0) {
			if (x % 3 != 0 &&  x % 5 != 0) ok = false;
		}
	}
	cout << (ok ? "APPROVED" : "DENIED") << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int a,b;
  cin>>a>>b;
  
  int count=0;
  for(int i=a;i<=b;++i){
    int s=i%10;
    int t=(i/10000)%10;
    
    int n=(i/10)%10;
    int m=(i/1000)%10;
    
    if(s==t && n==m)
      ++count;
  }
  
  cout<<count<<endl;
}



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

#define For(i, j, k) for (int i = j; i <= k; i++)

using namespace std;

const int N = 2e5 + 10;

typedef long long LL;

int n, X;
LL sum[N];

int main() {

	scanf("%d%d", &n, &X);
	For(i, 1, n) scanf("%lld", &sum[i]), sum[i] += sum[i - 1];

	LL ans = 1ll << 60;
	For(i, 1, n) {
		LL cur = 1ll * (i + n) * X;
		for (int j = n, c = 0; j > 0; j -= i, ++c) {
			LL w = (!c ? 5 : 2 * c + 3) * (sum[j] - sum[max(j - i, 0)]);
			cur += w;
			if (cur >= ans) break;
		}
		ans = min(ans, cur);
	}
	printf("%lld\n", ans);

	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>
#include <stdlib.h>//random,NULL
#include <string.h>
#include <time.h>
#include <unistd.h>//sleep()
#include <math.h>
#include <stdbool.h>

#define rep(i,n) for(int i=0; i<((int)n); i++)
#define REP(i,n) for(int i=0; i<=((int)n); i++)
#define srep(i,a,n) for(int i=a; i<((int)n); i++)
#define SREP(i,a,n) for(int i=a; i<=((int)n); i++)
#define rrep(i,n) for(int i=n-1; i>=0; i--)
#define RREP(i,n) for(int i=n; i>=0; i--)
#define INF 1000000000
const int mod = INF+7;

int min(int a, int b){
  if(a>=b){
    return b;
  }
  else{
    return a;
  }
}

int main(){
  char buf[1024];
  int N,A,B;

  fgets(buf,sizeof(buf),stdin);
  sscanf(buf, "%d %d %d", &N, &A, &B);

  int ans=min(A*N, B);

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


return 0;
}


Measured memory_bytes: 128
line = readlines
len = line.length
i = 0

while i < len
    line[i] = line[i].chomp
    i += 1
end

N = line[0].to_i
S = line[1]

ans = 0
for i in 1..N do
  if i == 1 then
    ans += 1
  elsif S[i-1] != S[i-2] then
    ans += 1
  end
end

print ans

Measured memory_bytes: 1916
#if !defined(__clang__) && defined(__GNUC__)
#include <bits/stdc++.h>
#else
#include <cstdlib>
#include <climits>
#include <iostream>
#include <cstdint>
#include <vector>
#include <string>
#include <complex>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <regex>
#endif //  !defined(__clang__) && defined(__GNUG__)
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/math/common_factor_rt.hpp>

using namespace std;

int main()
{
    int N;
    std::cin >> N;

    std::string S;
    std::cin >> S;


    size_t max = 0;
    for (size_t i = 1; i < N-1; ++i) {
        std::set<char> L, R;
        for (size_t l = 0; l < i; ++l) {
            L.insert(S[l]);
        }
        for (size_t r = i; r < N; ++r) {
            R.insert(S[r]);
        }

        
        size_t count = 0;
        for (auto c: L) {
            auto itr = R.find(c);
            if (itr != R.end()) {
                count++;
            }
        }
        max = std::max(max, count);
    }

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

    return 0;
}


Measured memory_bytes: 256
// ------------------------------------
// Date:2018/ 2/24
// Problem:AGC 001 BBQEasy a.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i,a) for (auto&& i : a)
#define FOR(i,a,b) for(int i=(int)a;i<(int)b;++i)
#define RFOR(i,a,b) for(int i=(int)b-1;i>=(int)a;--i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()

using ll = long long;

#define MOD 1000000007

int main()
{
  int N,ans = 0;
  cin >> N;
  vector<int> L(2*N);
  REP(i,2*N) {
    cin >> L[i];
  }
  sort(ALL(L));
  REP(i,N) {
    ans += L[i*2];
  }
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 256
n = int(input())
s = len(str(n))

if s == 1:
    print(n)
elif s == 2:
    print(9)
elif s % 2 == 0:
    k = s//2
    sum = 0
    t = 1
    for i in range(1,k+1):
        sum += 9*t
        t *= 100
    print(sum)
        
else:
    k = (s-1)//2
    sum = 0
    t = 1
    for i in range(1,k+1):
        sum += 9*t
        t *= 100
    print(sum + n -10**(s -1) +1)

Measured memory_bytes: 3064
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}
fn found_nn(n: usize) -> Option<usize> {
    for nn in 1.. {
        if nn * (nn + 1) == 2 * n {
            return Some(nn);
        } else if nn * (nn + 1) > 2 * n {
            return None;
        }
    }
    unreachable!();
}

fn main() {
    let n = read::<usize>();
    if let Some(nn) = found_nn(n) {
        let mut groups = vec![vec![]; nn + 1];
        let mut cur = 1;
        for i in 0..groups.len() {
            for j in i + 1..groups.len() {
                groups[i].push(cur);
                groups[j].push(cur);
                cur += 1;
            }
        }
        println!("Yes");
        println!("{}", groups.len());
        for i in 0..groups.len() {
            print!("{} ", groups[i].len());
            for &elem in groups[i].iter() {
                print!("{} ", elem);
            }
            println!("");
        }
    } else {
        println!("No");
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}


Measured memory_bytes: 5500
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 *
 * @author saku
 */
public class Main {
    public static void main(String args[]) throws IOException{
        
       BufferedReader std = new BufferedReader(new InputStreamReader(System.in));
       String[] s = std.readLine().split(" ");
       int a = Integer.parseInt(s[0]);
       int b = Integer.parseInt(s[1]);
       
       if (a > b){
           System.out.println("a > b");
       } else if ( a < b){
           System.out.println("a < b");
       } else if (a == b ){
           System.out.println("a == b");
       }
        
    }     
}

Measured memory_bytes: 24232
#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

int bias = 300;
int main() {
  int n;
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  while(true) {
    cin >> n;
    if (n == 0) break;

    pair<int, int> nd[n-1];
    for (int i = 0; i < n-1; i++) {
      cin >> nd[i].first >> nd[i].second;
    }
    // sort(nd, nd+n-1);

    int px[n], py[n];
    px[0] = bias;
    py[0] = bias;
    for (int i = 1; i < n; i++) {
      px[i] = px[nd[i-1].first]+dx[nd[i-1].second];
      py[i] = py[nd[i-1].first]+dy[nd[i-1].second];
    }

    int min_x, min_y, max_x, max_y;
    min_x = min_y = 1000;
    max_x = max_y = -1000;
    for (int i = 0; i < n; i++) {
      min_x = min(min_x, px[i]);
      min_y = min(min_y, py[i]);
      max_x = max(max_x, px[i]);
      max_y = max(max_y, py[i]);
    }

    std::cout << max_x-min_x+1 << " " << max_y-min_y+1 << std::endl;
  }
}

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

int main(){
    int x;
    cin >> x;
    vector<ll> y;
    int i = 0;
    while(pow(i,5)<1e20){
        y.push_back(pow(i,5));
        i++;
    }
    for(int a=0; a<=2*y.size(); a++){
        for(int b=0; b<=2*y.size(); b++){
            ll a_=y[a%y.size()];
            ll b_=y[b%y.size()];
            if(a>=y.size()) a_*=-1;
            if(b>=y.size()) b_*=-1;
            if(a_-b_==x){
                int ans_a = a;
                int ans_b = b;
                if(a>=y.size()) ans_a=-(a-y.size());
                if(b>=y.size()) ans_b=-(b-y.size());
                cout << ans_a << " " << ans_b << endl;
                return 0;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 4356
/*

This Submission is to determine how many 120/240 min const. delivery point there are.

//info
120 req. steps <= 5
*/
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 1000000007
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)

unsigned long xor128() {
	static unsigned long x = time(NULL), y = 362436069, z = 521288629, w = 88675123;
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
double dot(complex<double> a, complex<double> b) {
	return a.real() * b.real() + a.imag() * b.imag();
}
double gyaku_dot(complex<double> a, complex<double> b) {
	return a.real() * b.imag() - a.imag() * b.real();
}
double leng(complex<double> a) {
	return sqrt(a.real() * a.real() + a.imag() * a.imag());
}
double angles(complex<double> a, complex<double> b) {
	double cosine = dot(a, b) / (leng(a) * leng(b));
	double sine = gyaku_dot(a, b) / (leng(a) * leng(b));
	double kaku = acos(min((double)1.0, max((double)-1.0, cosine)));
	if (sine <= 0) {
		kaku = 2 * Ma_PI - kaku;
	}
	return kaku;
}
vector<int> convex_hull(vector<complex<double>> a) {
	vector<int> ans;
	double now_minnest = a[0].real();
	int now_itr = 0;
	REP(i, a.size()) {
		if (now_minnest > a[i].real()) {
			now_minnest = a[i].real();
			now_itr = i;
		}
	}
	ans.push_back(now_itr);
	complex<double> ba(0, 1);
	while (true) {
		int now_go = 0;
		double now_min = 0;
		double now_length = 0;
		int starter = ans[ans.size() - 1];
		for (int i = 0; i < a.size(); ++i) {
			if (i != starter) {
				double goa = angles(ba, a[i] - a[starter]);
				if (goa - now_min >= eps||(abs(goa - now_min) <= eps&&(abs(a[i] - a[starter]) - now_length) >= eps)) {
					now_min = goa;
					now_go = i;
					now_length = abs(a[i] - a[starter]);
				}
			}
		}
		if (now_go == ans[0]) break;
		ans.push_back(now_go);
		ba = complex<double>(a[now_go] - a[starter]);
	}
	return ans;
}
int main() {
	string s, t;
	cin >> s >> t;
	int ans = 0;
	REP(i, s.length()) {
		if (s[i] == t[i])ans++;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f|=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int N=200005;
int n,a[N];
int x[N],y[N];
int check(int v){
	LL pw[233];
	int tot=0,s=0,tmp=n;
	while (tmp)
		s++,tmp/=v;
	pw[0]=1;
	for (int i=1;i<=s;i++)
		pw[i]=pw[i-1]*v;
	a[0]=y[0]=x[0]=0;
	for (int i=1;i<=n;i++){
		if (a[i]>a[i-1]){
			tot++;
			x[tot]=a[i];
			y[tot]=0;
		}
		else {
			while (tot>0&&x[tot-1]>=a[i])
				tot--;
			int len=x[tot]-a[i];
			while (len&&y[tot])
				y[tot]/=v,len--;
			x[tot]=a[i];
			y[tot]++;
			for (int j=tot;j>=1;j--){
				if (x[j]-x[j-1]<=s&&y[j]>=pw[x[j]-x[j-1]]){
					y[j-1]++;
					y[j]-=pw[x[j]-x[j-1]];
				}
				else
					break;
			}
		}
	}
	return y[0]==0;
}
int ck1(){
	for (int i=1;i<n;i++)
		if (a[i]>=a[i+1])
			return 0;
	return 1;
}
int main(){
	n=read();
	for (int i=1;i<=n;i++)
		a[i]=read();
	if (ck1())
		return puts("1"),0;
	int L=2,R=n,ans=n;
	while (L<=R){
		int mid=(L+R)>>1;
		if (check(mid))
			R=mid-1,ans=mid;
		else
			L=mid+1;
	}
	cout << ans;
	return 0;
}
/*
10
2 4 4 4 5 4 3 2 2 2
*/


Measured memory_bytes: 1536
N = int(input())
A = list(map(int, input().split(" ")))
B = list(map(int, input().split(" ")))
C = list(map(int, input().split(" ")))

ans = 0
prev = -1
for i in range(N):
    idx = A[i] - 1
    if (A[i] == prev + 1):
        ans += C[idx - 1]
    ans += B[idx]
    prev = A[i]
print(ans)

Measured memory_bytes: 38384
def 解():
    N = int(input())
    aH = [int(_) for _ in input().split()]

    #dp = [0]*3
    #dp[1] = abs(aH[0] - aH[1])
    dp={0:0,1:abs(aH[0]-aH[1]),2:0}
    for i in range(2,N):
        dp[i%3] = min(abs(aH[i]-aH[i-1])+dp[(i-1)%3],abs(aH[i]-aH[i-2]) + dp[(i-2)%3])
    print(dp[(N-1)%3])
解()


Measured memory_bytes: 13780
#include <stdio.h>
#include <stdlib.h>
#define rep(i,l,r)for(int i=(l);i<(r);i++)
#define min(p,q)((p)<(q)?(p):(q))

typedef struct tuple{int x,y;}T;
int xcmp(const void*p,const void*q){
	T *pp=(T*)p,*qq=(T*)q;
	if(pp->x<qq->x)return-1;
	if(pp->x>qq->x)return 1;
	if(pp->y<qq->y)return-1;
	if(pp->y>qq->y)return 1;
	return 0;
}
int ycmp(const void*p,const void*q){
	T *pp=(T*)p,*qq=(T*)q;
	if(pp->y<qq->y)return-1;
	if(pp->y>qq->y)return 1;
	if(pp->x<qq->x)return-1;
	if(pp->x>qq->x)return 1;
	return 0;
}

int cnt[4];
T temp[4][200010];

int hoge[800010],*aaa=hoge+400005;
int hoge2[800010],*bbb=hoge2+400005;

int main(){
	int n;
	scanf("%d",&n);
	rep(i,0,n){
		int x,y;
		char c;
		scanf("%d%d %c",&x,&y,&c);
		T*t;
		if(c=='U')t=temp[0]+cnt[0]++;
		if(c=='D')t=temp[1]+cnt[1]++;
		if(c=='R')t=temp[2]+cnt[2]++;
		if(c=='L')t=temp[3]+cnt[3]++;
		t->x=x;
		t->y=y;
	}
	int ans=1e9;
	
	//正面衝突1
	qsort(temp[0],cnt[0],sizeof(T),xcmp);
	qsort(temp[1],cnt[1],sizeof(T),xcmp);
	for(int l0=0,l1=0;l0<cnt[0];l0++){
		while(l1<cnt[1]&&xcmp(temp[0]+l0,temp[1]+l1)!=-1)l1++;
		if(l1<cnt[1]&&temp[1][l1].x==temp[0][l0].x)ans=min(ans,(temp[1][l1].y-temp[0][l0].y)*5);
	}
	//正面衝突2
	qsort(temp[2],cnt[2],sizeof(T),ycmp);
	qsort(temp[3],cnt[3],sizeof(T),ycmp);
	for(int l0=0,l1=0;l0<cnt[2];l0++){
		while(l1<cnt[3]&&ycmp(temp[2]+l0,temp[3]+l1)!=-1)l1++;
		if(l1<cnt[3]&&temp[3][l1].y==temp[2][l0].y)ans=min(ans,(temp[3][l1].x-temp[2][l0].x)*5);
	}
	
	//左から見て、反転して、もう1回左から見る
	rep(d,2,4){
		rep(i,0,800005)hoge[i]=hoge2[i]=0;
		aaa=hoge+400005,bbb=hoge2+400005;
		qsort(temp[0],cnt[0],sizeof(T),xcmp);
		qsort(temp[1],cnt[1],sizeof(T),xcmp);
		qsort(temp[d],cnt[d],sizeof(T),xcmp);
		for(int l0=0,l1=0,l2=0,x=0;x<=200000;x++){
			while(l2<cnt[d]&&temp[d][l2].x==x){
				aaa[temp[d][l2].y]=x+1;
				bbb[temp[d][l2].y]=x+1;
				l2++;
			}
			while(l0<cnt[0]&&temp[0][l0].x==x){
				if(aaa[temp[0][l0].y])ans=min(ans,(x+1-aaa[temp[0][l0].y])*10);
				l0++;
			}
			while(l1<cnt[1]&&temp[1][l1].x==x){
				if(bbb[temp[1][l1].y])ans=min(ans,(x+1-bbb[temp[1][l1].y])*10);
				l1++;
			}
			aaa++;
			bbb--;
		}
		rep(dd,0,4)rep(i,0,cnt[dd])temp[dd][i].x=200000-temp[dd][i].x;
	}
	
	if(ans==1e9)puts("SAFE");
	else printf("%d",ans);
}

Measured memory_bytes: 10968
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main() {
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	cout << fixed << setprecision(6) << sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) << endl;
	return 0;
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1<<(n))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
int const INF = 1001001001;

ll dp[2020][2020];

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


int N;
cin >> N;
vl A(N);
rep(i,N) cin >> A[i];

vector<pll> P(N);
rep(i,N) {
    P[i] = {A[i],i};
}
sort(P.rbegin(), P.rend());

for (int i = 0; i < N; ++i) {
    for (int j = 0; i + j < N; ++j) {
        chmax(dp[i+1][j], dp[i][j] + P[i+j].first * abs(P[i+j].second - i));
        chmax(dp[i][j+1], dp[i][j] + P[i+j].first * abs((N-1-j) - P[i+j].second));
        }
    }

ll ans = 0;
for(int i = 0; i <= N; i++) {
    chmax(ans, dp[i][N-i]);
}
cout << ans << endl;
return 0;
}

Measured memory_bytes: 26376
// Comment
//

#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(),(v).end()
#define one first
#define two second
using ll = long long;
using pi = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll * INF * INF;

const int MAX_N = 1e5 + 100;

int N;
ll M, X[MAX_N], C[MAX_N];
ll Sum[2][MAX_N], Max[2][MAX_N];
int main() {
	cin >> N >> M;
	X[0] = 0; X[N+1] = M;
	for(int i=1; i<=N; i++) scanf("%lld%lld", &X[i], &C[i]);
	for(int i=1; i<=N; i++) Sum[0][i] = Sum[0][i-1] + C[i] - (X[i] - X[i-1]);
	for(int i=N; i>=1; i--) Sum[1][i] = Sum[1][i+1] + C[i] - (X[i+1] - X[i]);
	for(int i=1; i<=N; i++) Max[0][i] = max(Sum[0][i], Max[0][i-1]);
	for(int i=N; i>=1; i--) Max[1][i] = max(Sum[1][i], Max[1][i+1]);

	ll ans = max(Max[0][N], Max[1][1]);
	for(int i=0; i<=N-1; i++) ans = max(ans, Sum[0][i] - (    X[i]) + Max[1][i+1]);
	for(int i=N; i>=1  ; i--) ans = max(ans, Sum[1][i] - (M - X[i]) + Max[0][i-1]);
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 8392
package main

import (
  "fmt"
  "unicode"
)

func main() {
  var n, k int
  var s string
  fmt.Scan(&n, &k, &s)
  runes := []rune(s)
  runes[k-1] = unicode.ToLower(runes[k-1])
  fmt.Println(string(runes))
}

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

int main() {
    int n;
    cin >> n;
    vector<int> data(n);

    int xordata = 0;

    for(int i = 0 ; i < n ; i++){
        cin >> data.at(i);
        xordata ^= data.at(i);
    }

    for(int i = 0 ; i < n - 1 ; i++){
        cout << (data.at(i) ^ xordata);
        cout << ' ';
    }
    cout << (data.at(n - 1) ^ xordata);
    cout << endl;
}


Measured memory_bytes: 3992
import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;

enum lim = 10^^5 + 1;

void main() {
    string s, t;
    scan(s);
    scan(t);

    auto N = s.length.to!int;
    auto M = t.length.to!int;

    auto dp = new int[][](N + 1, M + 1);
    foreach (i ; 1 .. N + 1) {
        foreach (j ; 1 .. M + 1) {
            chmax(dp[i][j], dp[i - 1][j], dp[i][j - 1]);
            if (s[i - 1] == t[j - 1]) {
                chmax(dp[i][j], dp[i - 1][j - 1] + 1);
            }
        }
    }

    int x = N, y = M;

    string ans;

    while (x > 0 && y > 0) {
        if (dp[x][y] == dp[x - 1][y]) {
            x--;
        }
        else if (dp[x][y] == dp[x][y - 1]) {
            y--;
        }
        else {
            x--, y--;
            ans ~= s[x];
        }
    }

    writeln(ans.retro);
}



void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x > arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;

    foreach (arg; args) {
        if (x < arg) {
            x = arg;
            isChanged = true;
        }
    }

    return isChanged;
}


Measured memory_bytes: 38268