akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}


int main() {
	int h, w,ansh=151,answ=151;
	int diag;
	while (cin>>h>>w,h) {
		diag = h*h + w*w;
		//cout << "diag " << diag << endl;
		for (int i = 1; i <= 150; i++) {
			for (int j = 1; j <= 150; j++) {
				if (i >= j) continue;
				if (i == h&&j == w) continue;
				//cout << "# " << i << " " << j <<" "<< i*i + j*j << endl;
				if (i*i + j*j > diag|| (i*i + j*j == diag&&i>h)) {
					if (i*i + j*j <ansh*ansh + answ*answ) {
						ansh = i;
						answ = j;
						//cout << "check1 " << i << " " << j << endl;
					}
					else if ((i*i + j*j == ansh*ansh + answ*answ)&&i<ansh) {
						ansh = i;
						answ = j;
						//cout << "check2 " << i << " " << j << endl;
					}
				}
			}
		}
		cout << ansh << " " << answ << endl;
		ansh = 151; 
		answ = 151;
	}
}



Measured memory_bytes: 3012
#include<bits/stdc++.h>
using namespace std;
int main()
{
  char str[1024];
  int s=700,i;
  scanf("%s",str);
  for(i=0;i<3;i++)
  {
  	if(str[i]=='o')
  	   s+=100;
  }
  printf("%d",s);
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct hi{
	ll a;int b;
} cnt[200005];
int id[200005],id2[200005],p[200005]
,n,k,l,i,j,m;

int find(int x){
	while (x!=id[x]){
		id[x]=id[id[x]];
		x=id[x];
	}
	return x;
}

int find2(int x){
	while (x!=id2[x]){
		id2[x]=id2[id2[x]];
		x=id2[x];
	}
	return x;
}

void connect(int x,int y){
	x=find(x);y=find(y);
	if (x!=y) id[x]=y;
}

void connect2(int x,int y){
	x=find2(x);y=find2(y);
	if (x!=y) id2[x]=y;
}

bool cmp(hi a,hi b){
	return a.a<b.a;
}

int main(){
	scanf("%d%d%d",&n,&k,&l);
	for (i=1;i<=n;i++){
		id[i]=i;id2[i]=i;
	}
	for (i=1;i<=k;i++){
		scanf("%d%d",&j,&m);
		connect(j,m);
	}
	for (i=1;i<=l;i++){
		scanf("%d%d",&j,&m);
		connect2(j,m);
	}
	for (i=1;i<=n;i++){
		id[i]=find(id[i]);id2[i]=find2(id2[i]);
		cnt[i].a=ll(id[i])*200001+ll(id2[i]);cnt[i].b=i;
	}
	sort(cnt+1,cnt+n+1,cmp);
	j=0;k=0;
	for (i=1;i<=n;i++){
		if (cnt[i].a!=cnt[i-1].a) j++;
		p[j]++;
		id[cnt[i].b]=j;
	}
	for (i=1;i<n;i++) printf("%d ",p[id[i]]);
	printf("%d\n",p[id[n]]);
}

Measured memory_bytes: 9108
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define RREP(i,a,b) for(int i=(a);i>=(b);i--)
#define pq priotity_queue
typedef long long ll; typedef long double ld;
using namespace std;
const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};
const ld PI=abs(acos(-1)); 
int n,m,a,b,c,d;
string s,w;

int main(){
	cin >> s;
	cin >> w;
	
	if(s[0]==w[2] and s[1]==w[1] and s[2]==w[0]) cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}


Measured memory_bytes: 256
#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() {
    ll a,b,c,d;
    cin >> a >> b >> c >> d;
    ll mx = -1001001001001001001;
   	mx = max(mx, a*d);
    mx = max(mx, b*c);
    mx = max(mx, a*c);
    mx = max(mx, b*d);
    cout << mx << endl;
    return 0;
}

Measured memory_bytes: 3632
w, h, n = map(int, input().split())
list = [list(map(int, input().split())) for i in range(n)]

x_max = w
x_min = 0
y_max = h
y_min = 0


for list in list:
    if list[2] == 1:
        x_min = max(list[0], x_min)
    elif list[2] == 2:
        x_max = min(list[0], x_max)
    elif list[2] == 3:
        y_min = max(list[1], y_min)
    else:
        y_max = min(list[1], y_max)
 
ans = max((x_max - x_min), 0) * max((y_max - y_min), 0)
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
string divide[4]={"dream","dreamer","erase","eraser"};
int main()
{
  int n,cnt=0;cin>>n;
  string s;cin>>s;
  for(int i=0;i<n-2;i++){
    if(s.substr(i,3)=="ABC")cnt++;
  }
  cout<<cnt<<endl;
}

Measured memory_bytes: 256
/* 
	Author: ankrypt
*/

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

using namespace std;
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define MOD 1000000007
#define ll long long int
#define u64 unsigned long long int
#define ff first
#define ss second
#define all(x) x.begin(), x.end() 
#define ub upper_bound
#define lb lower_bound
#define MAX_INT 200005
#define fi(a, b, c, d) for(int a = b; a < c; a += d)
#define fl(a, b, c, d) for(ll a = b; a < c; a += d)
#define fei(a, b, c, d) for(int a = b; a <= c; a += d)
#define fel(a, b, c, d) for(ll a = b; a <= c; a += d)
#define quit exit(0)

ll N, M, A, B, C;
ll arr[MAX_INT], vis[MAX_INT], col[MAX_INT];
vector<ll> adj[MAX_INT];
ll startingHere = -1;
ll tot;
ll cycle[MAX_INT];
ll cnt = 0;
ll beforeStart;
std::vector<ll> V, cycles;

void dfs1(int node) {
	vis[node] = 1;
	col[node] = -1;
	// cout << node << "hi\n";
 	fi(i, 0, adj[node].size(), 1) {
		if(!vis[adj[node][i]]) {
			dfs1(adj[node][i]);
		}
		else {
			if(col[adj[node][i]] == -1) {
				startingHere = adj[node][i];
			}
		}
	}
	col[node] = 1;
}
void dfs2(int node) {
	vis[node] = 1;
	cnt++;
	// cout << node << " " << cnt <<"tell\n";
	cycle[node] = 1;
	cycles.push_back(node);
	fi(i, 0, adj[node].size(), 1) {
		if(adj[node][i] == startingHere) {
			tot = cnt;
			return;
		}
		else if(!vis[adj[node][i]]) {
			dfs2(adj[node][i]);
		}
	}
}

void dfs3(int node) {
	vis[node] = 1;
	if(cycle[node]) {
		return;
	}
	beforeStart++;
	V.push_back(node);
	fi(i, 0, adj[node].size(), 1) {
		if(cycle[adj[node][i]]) {
			return;
		}
		else if(!vis[adj[node][i]]) {
			dfs3(adj[node][i]);
		}
	}
}


int main() {
	scanf("%lld %lld", &N, &M);
	fi(i, 0, N, 1) {
		scanf("%lld", &arr[i]);
		int A = i;
		int B = arr[i];
		B--;
		adj[A].push_back(B);
		// adj[B].push_back(A);
	}
	dfs1(0);
	// cout << startingHere << " starts";
	memset(vis, 0, sizeof(vis));
	dfs2(startingHere);
	dfs3(0);
	// fi(i, 0, V.size(), 1) {
	// 	cout << V[i] << " yeah";
	// }
	// cout << "\n";
	// fi(i, 0, cycles.size(), 1) {
	// 	cout << cycles[i] << " this";
	// }
	// cout << "\n";
	// cout << beforeStart << " ";
	if(M < beforeStart) {
		cout << V[M] + 1 << "\n";
		quit;
	}
	M -= beforeStart;
	ll sz = cycles.size();
	cout << cycles[M % sz] + 1 << " \n";

	// cout << tot << " that\n";


	return 0;
}
/*
	Powered by Buggy Plugin
*/




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

const int N = 21;
const int MOD = 1e9 + 7;

int can[N][N];

void add(int &a, int b)
{
	a += b;
	if (a >= MOD)
		a -= MOD;
}

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

	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			cin >> can[i][j];
	}
	vector<int> dp(1 << n);
	dp[0] = 1;

	for (int mask = 0; mask < (1 << n) - 1; mask++)
	{
		int a = __builtin_popcount(mask);
		for (int j = 0; j < n; j++)
		{
			if (!(mask & (1 << j)) && can[a][j])
			{
				add(dp[mask ^ (1 << j)], dp[mask]);
			}
		}
	}

	cout << dp[(1 << n) - 1] << "\n";
}



Measured memory_bytes: 8448
#include <iostream>

using namespace std;

typedef long long ll;
#define x first
#define y second
#define mp make_pair

const int MAXN = (int)2e5;
ll n, k, a[MAXN + 5];

int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> k >> n;
for(int i = 0 ; i < n ; ++i){
	cin >> a[i];
}
ll sol = k;
for(int i = 0 ; i < n ; ++i){
	int drugi = (i + 1) % n;
	ll dist = (a[drugi] - a[i] + k) % k;
	dist = k - dist;
	sol = min(sol, dist);
	drugi = (i - 1 + n) % n;
	dist = (a[i] - a[drugi] + k) % k;
	dist = k - dist;
	sol = min(sol, dist);
}
cout << sol << endl;

return 0;
}



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



int main()
{
  string s,t;
  cin >> s >> t;
 
  if(s.size()!=t.size())
    cout << "No\n";
  else
  {
    string temp=s+s;
   // cout << temp << endl;
    if(temp.find(t) != string::npos){
    //  cout << temp.find(t) << endl;
      cout << "Yes\n";
    }
    else
      cout << "No\n";
  }
  return 0;
}



Measured memory_bytes: 384
#include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
 
using namespace std;
 
struct P{int c,r;};
struct State{
  int t;
  int pos, remain;
  bool operator < (const State& st) const {
    return t > st.t;
  }
};
 
const int MAX = 5005;
const int INF = (1<<25);
int T[MAX][MAX];
vector<int> Edge[MAX];
P price[MAX];
int N,K;
 
void input(){
  scanf("%d %d",&N,&K);
  for(int i = 0; i < N; i++) scanf("%d %d",&price[i].c, &price[i].r);
  for(int i = 0; i < K; i++){
    int a,b;
    scanf("%d %d",&a, &b);
    a--;b--;
    Edge[a].push_back(b);
    Edge[b].push_back(a);
  }
}
 
int solve(){
  fill(T[0], T[0]+MAX*MAX, INF);
  
  T[0][0] = 0;
  priority_queue<State> Q;
  Q.push((State){0,0,0});
  while(!Q.empty()){
    const State now = Q.top();
    Q.pop();
    
    if(now.t > T[now.pos][now.remain]) continue;
    
    if(now.pos == N-1) return now.t;
    if(now.remain > 0){
      for(int i = 0; i < (int)Edge[now.pos].size(); i++){
	State nex = now;
	nex.pos = Edge[now.pos][i];
	nex.remain--;
	
	if(T[nex.pos][nex.remain] > nex.t){
	  T[nex.pos][nex.remain] = nex.t;
	  Q.push(nex);
	}
      }
    }
    
    State nex = now;
    nex.t += price[now.pos].c; 
    nex.remain = price[now.pos].r;
    if(T[nex.pos][nex.remain] > nex.t){
      T[nex.pos][nex.remain] = nex.t;
      Q.push(nex);
    }
  }
  return INF;
}
 
int main(){
  input();
  cout << solve() << endl;
  return 0;
}

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

int main() {
  int r;
  cin >> r;
  string ans;
  if ( r < 1200 ) {
    ans = "ABC";
  } else if ( r < 2800 ) {
    ans = "ARC";
  } else {
    ans = "AGC";
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <algorithm>

using namespace std;

int main() {
	int N;
	long long K,total;
	cin >> N >> K;

	map<int, long long> mp;
	total = 0;
	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		mp[a] += b;
	}

	for (auto&& var : mp) {
		total += var.second;
		if (total >= K) {
			cout << var.first << endl;
			break;
		}
	}

	return 0;
}


Measured memory_bytes: 4224
#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout<<"Case #"<<x<<": "
#define op1(x) cout<<"Scenario #"<<x<<": "
#define endl "\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = LLONG_MAX;
const int N = 18;


void solve(int t)
{
	string p, q;
	cin >> p >> q;

	ll ans = INF;

	for (int i = 0; i + q.length() <= p.length(); i++)
	{
		ll cnt = 0;

		for (int j = 0; j < q.length(); j++)
		{
			if (q[j] != p[i + j]) cnt++;
		}
		ans = min(ans, cnt);
	}

	cout << ans << endl;



}


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);





	int t = 1;
	//cin >> t;



	for (int i = 1; i <= t; i++)
	{
		solve(i);

	}
}

Measured memory_bytes: 3616
l = input()
len_l = len(l)

ans = 0

MOD = 1000000007
def pow_mod(x, y):
    if y == 0: return 1
    ans = 1
    while y > 1:
        if y % 2 != 0: 
            ans *= x
            ans %= MOD
        x *= x
        x %= MOD
        y //= 2
    return ans * x % MOD

keisuu = 1
for i, b in enumerate(l):
  if b == "1":
    ans += (keisuu * pow_mod(3, len_l - i - 1)) % MOD
    ans %= MOD
    keisuu *= 2
    keisuu %= MOD

ans += keisuu
ans %= MOD

print(ans)

Measured memory_bytes: 3188
import numpy as np
import copy


def dist2(y, z):
    return np.sum((y - z)**2)


def main():
    L, R = map(int, input().split())

    m = R // 2019
    if L <= 2019 * m and 2019 * m <= R:
        ans = 0
    else:
        L -= 2019 * m
        R -= 2019 * m
        # assert L > 0
        # assert L < 2019
        # assert R > 0
        # assert R < 2019
        ans = 2018
        for i in range(L, R+1):
            for j in range(i+1, R+1):
                ans = min([ans, i * j % 2019])

    print(ans)


if __name__ == '__main__':
    main()


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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
    ost<<"{"<<p.first<<","<<p.second<<"}";
    return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
    ost<<"{";
    for(int i=0;i<v.size();i++){
        if(i)ost<<",";
        ost<<v[i];
    }
    ost<<"}";
    return ost;
}

inline int topbit(unsigned long long x){
    return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
    return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
    return __builtin_parity(x);
}


signed main(){
	int N;
	cin>>N;
	int ans=0;
	for(int i=0;i<N;i++)ans+=(i+1)*(N-i);

	rep(i,N-1){
		int a,b;
		cin>>a>>b;
		a--;b--;
		if(a>b)swap(a,b);
		ans-=(a+1)*(N-b);
	}

	cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 3588
from sys import stdin

N = stdin.readline().rstrip()
H_list = stdin.readline().rstrip().split()
H_list_int = list(map(lambda x: int(x), H_list))  # strリストをintリストへ

ables = 1
max = H_list_int.pop(0)

for i in H_list_int:
    if max <= i:
        ables += 1
        max = i

print(ables)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;

int main(){
    string s;
    cin >> s;
    int cnt = 0;
    ll ans = 0;
    rep(i,s.size()-1){
        if(s[i]=='A'){
            cnt ++;
        }
        else if(s[i]=='B'&&s[i+1]=='C'){
            ans += cnt;
            ++i;
        }
        else{
            cnt = 0;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a,b,x;
    cin>>a>>b>>x;
    long long ans = 0;
    
    ans = b/x - a/x;

    if(a%x == 0){
        ans++;
    }

    cout<<ans<<endl;
    return 0;
}

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


#define endl '\n'

int solve(vector<int> &stones, unsigned int k) {

    if(stones.size() <= k) {
        return abs(stones[0]-stones[stones.size()-1]);
    }
    
    vector<int> dp(stones.size(), INT_MAX);
    
    dp[0] = 0;

    for(size_t i = 1; i < k; ++i) {
        dp[i] = abs(stones[0]-stones[i]);
    }

    for(size_t i = k; i < stones.size(); ++i) {
        dp[i] = INT_MAX;

        for(size_t j = 1; j <= k; ++j) {
            dp[i] = min(dp[i], dp[i-j]+abs(stones[i]-stones[i-j]));
        }
         
    }

    // for(size_t i = 0; i < stones.size(); ++i) {
    //     cout<<dp[i]<<endl;
    // }

    return dp[stones.size()-1];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int m, k, h_i;

    cin>>m>>k;

    vector<int> stones;


    stones.reserve(m+1);

    for(int i = 0; i < m; ++i) {
        cin>>h_i;
        stones.push_back(h_i);
    }

    cout<<solve(stones, k)<<endl;

    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define N 400
using namespace std;
typedef long long LL;
struct tile
{
	vector<int> values;
	tile(){}
	tile(int b[])
	{
		vector<int> rots[4];
		for(int i = 0; i < 4; i++)
			for(int offset = 0; offset < 4; offset++)
				rots[i].push_back(b[(i+offset)%4]);
		values = *min_element(rots,rots+4);
	}
	bool operator <(const tile& other)const{ return this->values < other.values; }
	bool operator <=(const tile& other)const{ return this->values <=other.values; }
	bool operator ==(const tile& other)const{ return this->values ==other.values; }
	void print()const
	{
		for(const int i : values)
			cout<<i<<" ";
		cout<<endl;
	}
	int orbit()const
	{
		if(values[0]==values[1]&&values[1]==values[2]&&values[2]==values[3]) return 4;
		if(values[0]==values[2]&&values[1]==values[3]) return 2;
		else return 1;
	}
};

map<tile,LL> freq;
vector<tile> tiles;

LL P(LL n, LL k)
{
	if(k <= 0 || k > n) return 0;
	LL ans = 1;
	for(LL j = 0; j < k; j++)
		ans *= (n-j);
	return ans;
}
tile make(int a, int b, int c, int d)
{
	int arr[4] = {a,b,c,d};
	return tile(arr);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n; cin >> n;
	LL total = 0;
	for(int j = 0; j < n; j++)
	{
		int arr[4]; 
		for(int i = 0; i < 4; i++) cin >> arr[i];
		
		tile a(arr);
		for(const auto& b : tiles)
		{
			//a.print();
			//b.print();
			freq[b]--;
			for(int offset = 0; offset < 4; offset++)
			{
				map<tile,LL> needed;
				needed[make( a.values[1], a.values[0], b.values[(1+offset)%4], b.values[(0+offset)%4] )]++;
				needed[make( a.values[2], a.values[1], b.values[(0+offset)%4], b.values[(3+offset)%4] )]++;
				needed[make( a.values[3], a.values[2], b.values[(3+offset)%4], b.values[(2+offset)%4] )]++;
				needed[make( a.values[0], a.values[3], b.values[(2+offset)%4], b.values[(1+offset)%4] )]++;

				LL sub_ans = 1;
				for(const auto& pattern : needed)
				{
					sub_ans *= P(freq[pattern.first],pattern.second);
					for(int i = 0; i < pattern.second; i++)
						sub_ans *= pattern.first.orbit();
				}
				total += sub_ans;
			}
			freq[b]++;
		}
		freq[a]++;		
		tiles.push_back(a);
	}
	cout<<total<<endl;

	return 0;
}

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

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

constexpr ll INF = TEN(18);
const int MX = 3010;

ll dp[MX][MX][4];
ll v[MX][MX];

int main() {
    int R, C, K;
    cin >> R >> C >> K;
    rep(i, K) {
        int r, c, val;
        cin >> r >> c >> val;
        --r, --c;
        v[r][c] = val;
    }

    rep(i, MX) rep(j, MX) rep(k, 4) dp[i][j][k] = -INF;
    dp[0][0][0] = 0;

    rep(i, R) {
        rep(j, C) {
            for (int k = 2; k >= 0; --k) {
                if (dp[i][j][k] >= 0) {
                    chmax(dp[i][j][k + 1], dp[i][j][k] + v[i][j]);
                }
            }
            rep(k, 4) {
                if (dp[i][j][k] >= 0) {
                   
                        chmax(dp[i + 1][j][0], dp[i][j][k]);
                    
                    
                        chmax(dp[i][j + 1][k], dp[i][j][k]);
                    
                }
            }
        }
    }

    ll ans = -INF;
    rep(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 357168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

void push(int);
int pop(void);

int top=0;
int t[MAX];

int main(){
  int a,b;
  char s[100];

  while(scanf("%s",s) != EOF){
    if(s[0]=='+'){
      a=pop();
      b=pop();
      push(b+a);
    }else if(s[0]=='-'){
      a=pop();
      b=pop();
      push(b-a);
    }else if(s[0]=='*'){
      a=pop();
      b=pop();
      push(b*a);
    }else if(s[0]>=48 && s[0]<58){
      push(atoi(s));
    }else continue;
  }
 
  printf("%d\n",pop());
  return 0;
}

void push(int x){
  top++;
  t[top]=x;
}

int pop(void){
  top--;
  return t[top+1];
}



Measured memory_bytes: 2104
N,K = map(int,input().split())

ans = ""

while True:
  r = str(N % K)
  ans += r
  N = N // K
  if N < K:
    ans += str(N % K)
    break

ans = ans[::-1]
if ans[0] == "0":
  ans = ans[1:]  

print(len(ans))

Measured memory_bytes: 3060
#include<stdio.h>

#define MAX 101

#define WHITE 0
#define GRAY 1
#define BLACK 2



int n,color[MAX],time,m[MAX][MAX],f[MAX],d[MAX];


void visit(int u){
  int i;
  color[u]=GRAY;
  d[u]=++time;
  for(i=1;i<=n;i++){
    if(m[u][i]==GRAY && color[i]==WHITE){
      visit(i);
    }
  }
  color[u]=BLACK;
  f[u]=++time;
}


void DFS(){
  int u;
  for(u=1;u<=n;u++){
    color[u]=WHITE;
  }
  time=0;
  for(u=1;u<=n;u++){
    if(color[u]==WHITE){
      visit(u);
    }
  }
}


int main(){
  int i,j,u,k,x;

  
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      m[i][j]=0;
    }
  }

  for(i=1;i<=n;i++){
    scanf("%d%d",&u,&k);
    for(j=1;j<=k;j++){
      scanf("%d",&x);
      m[u][x]=1;
    }
  }
  
  DFS();

  for(i=1;i<=n;i++){
    printf("%d %d %d\n",i,d[i],f[i]);
  }

  return 0;
}



Measured memory_bytes: 2104
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n[10],m[10],i;
  for(i=0;i<10;i++){
    cin>>n[i];
  }
  for(i=0;i<10;i++){
    cin>>m[i];
  }
  sort(n,n+10);
  sort(m,m+10);
  reverse(n,n+10);
  reverse(m,m+10);
  cout<<n[0]+n[1]+n[2]<<" "<<m[0]+m[1]+m[2]<<endl;
  return 0;
}

Measured memory_bytes: 3100
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
using LP=pair<ll,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353;

const ll INF=1e18;
vector<int> nex={1,-1,0,0},ney={0,0,1,-1};

int main() {
    int H,W,K;
    cin>>H>>W>>K;
    int xs,ys,xg,yg;
    cin>>xs>>ys>>xg>>yg;
    --xs;--ys;--xg;--yg;
    vector<vector<int>> rake(H,vector<int>(W));
    rep(i,H)rep(j,W){
        char c;
        cin>>c;
        if(c=='@'){
            rake.at(i).at(j)=1;
        }
    }
    vector<ll> d(H*W*4,INF);
    auto toid=[&](int x,int y,int muki){
        return (x*W+y)*4+muki;
    };
    auto upK=[&](ll x){
        return (x+K-1)/K*K;
    };
    priority_queue<LP,vector<LP>,greater<LP>> que;
    que.push({0,toid(xs,ys,0)});
    d[toid(xs,ys,0)]=0;
    while(!que.empty()){
        LP now=que.top();
        que.pop();
        if(d[now.second]!=now.first)continue;
        int x=now.second/4/W,y=now.second/4%W,muki=now.second%4;
        
        int newx=x+nex.at(muki),newy=y+ney.at(muki);
        if(0<=newx && newx<H && 0<=newy && newy<W){
            if(!rake.at(newx).at(newy)){
                if(d[toid(newx,newy,muki)]>now.first+1){
                    d[toid(newx,newy,muki)]=now.first+1;
                    que.push({d[toid(newx,newy,muki)],toid(newx,newy,muki)});
                }
            }
        }

        rep(newmuki,4){
            if(d[toid(x,y,newmuki)]>upK(now.first)){
                d[toid(x,y,newmuki)]=upK(now.first);
                que.push({d[toid(x,y,newmuki)],toid(x,y,newmuki)});
            }
        }
    }
    ll ans=d[toid(xg,yg,3)];
    rep(i,3){
        chmin(ans,d[toid(xg,yg,i)]);
    }
    if(ans==INF){cout<<-1<<endl;}
    else{
        cout<<upK(ans)/K<<endl;
    }

    /*rep(i,H){
        rep(j,W){
            cout<<"("<<d[toid(i,j,0)]<<" "<<d[toid(i,j,1)]<<" "<<d[toid(i,j,2)]<<" "<<d[toid(i,j,3)]<<")";
        }
        cout<<endl;
    }*/
}

Measured memory_bytes: 46664
# include<iostream>
# include<cstring>
using namespace std;

class cycle {
    public:
    int top,b,c,d,e,f,nothing;
    
    cycle (int aa, int bb, int cc, int dd, int ee, int ff) {
        top = aa;
        b = bb;
        c = cc;
        d = dd;
        e = ee;
        f = ff;
    }
    
    int move_east () {
        nothing = top;
        top = d;
        d = f;
        f = c;
        c = nothing;
        return top;
    }
    int move_west () {
        nothing = top;
        top = c;
        c = f;
        f = d;
        d = nothing;
        return top;
    }
    int move_north () {
        nothing = top;
        top = b;
        b = f;
        f = e;
        e = nothing;
        return top;
    }
    int move_south ()  {
        nothing = top;
        top = e;
        e = f;
        f = b;
        b = nothing;
        return top;
    }
};

int main() {
    int aa, bb, cc, dd, ee, ff, top;
    string dir;
    cin >> aa >> bb >> cc >> dd >> ee >> ff >> dir;
    cycle yili(aa, bb, cc, dd, ee, ff);
    for (int i=0;i<dir.size();i++) {
        if (dir[i] == 'E') top = yili.move_east();
        else if (dir[i] == 'W') top = yili.move_west();
        else if (dir[i] == 'N') top = yili.move_north();
        else if (dir[i] == 'S') top = yili.move_south();
    }
    cout << top << endl;
}


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

int main() {
    int x, y, z, k;
    cin >> x >> y >> z >> k;
    vector<long long> a(x), b(y), c(z);
    for(int i = 0; i < x; i++)
    {
        cin >> a.at(i);
    }
    for(int i = 0; i < y; i++)
    {
        cin >> b.at(i);
    }
    for(int i = 0; i < z; i++)
    {
        cin >> c.at(i);
    }
    
    vector<long long> ab(x * y);
    for(int i = 0; i < x; i++)
    {
        for(int j = 0; j < y; j++)
        {
            ab.at(i * y + j) = a.at(i) + b.at(j);
            
        }
        
    }

    sort(ab.begin(), ab.end());
    vector<long long> abc(min((x * y), k) * z);
    for(int i = 0; i < min((x * y), k); i++)
    {
        for(int j = 0; j < z; j++)
        {
            abc.at(i * z + j) = ab.at((int)ab.size() - 1 - i) + c.at(j);
        }
        
    }
    
    sort(abc.begin(), abc.end());

    for(int i = 0; i < k; i++)
    {
        cout << abc.at((int)abc.size() - 1 - i) << endl;
    }
    
}

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

int main() {
 int A, B;
  string op;
 cin >> A >> op >> B;
  // cout << op;
  if (op == "+") {
   cout << A + B << endl;
 }
  else if (op == "-") {
    cout << A - B << endl;
  }
  
  
}  




Measured memory_bytes: 256
/*
 * WelcomeToAtCoder.cpp
 *
 *  Created on: 2020/02/19
 *      Author: black
 */

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

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

	    if (n[n.length()-1] == '3'){
	    	cout << "bon" << endl;
	    }
	    else if((n[n.length()-1] == '0') || (n[n.length()-1] == '1') || (n[n.length()-1] == '6') || (n[n.length()-1] == '8')){
	    	cout << "pon" << endl;
	    }
	    else{
	    	cout << "hon" << endl;
	    }

	    return 0;
}


Measured memory_bytes: 3040
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long int;
const int MOD = 1e9 + 7;

int main() {
    int n, k; cin >> n >> k;
    vector<int> bnd{};
    int boundary = 1;
    int div = n;
    while(boundary <= n){
        bnd.push_back(boundary);
        if(n / boundary != n / (boundary+1)){
            boundary++;
            div = n / boundary;
        } 
        else{
            if(div <= 0){ break;}
            boundary = n / div + 1; 
            div--;
        }
    }
    bnd.push_back(n+1);
    int length = (int)bnd.size() - 1;
    vector<vector<ll>> dp(k, vector<ll>(length, 0));
    for(int i=0; i<length; i++){
        dp[0][i] = bnd[i+1] - bnd[i];
    }
    for(int i=0; i<k-1; i++){
        vector<ll> cumsum(length+1, 0);
        for(int j=0; j<length; j++){
            cumsum[j+1] = cumsum[j] + dp[i][j];
            cumsum[j+1] %= MOD;
        }
        for(int j=0; j<length; j++){
            dp[i+1][j] += dp[0][j] * cumsum[length - j]; 
            dp[i+1][j] %= MOD;
        }
    }

    // Debug
    // for(auto& line:dp){
    //     for(auto& v:line){
    //         cout << v << " ";
    //     }
    //     cout << endl;
    // }
    ll ans = 0;
    for(auto& v:dp[k-1]){
        ans += v;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 50684
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif 

using namespace std;

#define MAXN ((int)2e5+5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define TRACE(x) if(DEBUG) cerr << #x << " = " << x << endl;
#define TRACE2(x,y) if(DEBUG) cerr << #x << " = " << x << " | " << #y << " = " << y << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pii pair<int,int>
#define mid ((l+r)/2)

int L , R;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	cin >> L >> R;
	if(R - L >= 2018)
		cout << 0 << endl;
	else
	{
		int ans = 2020;
		for (int i = L; i <= R; ++i)
			for (int j = i+1; j <= R; ++j)
			{
				int t = (i%2019) * (j%2019);
				ans = min(ans, t%2019);
			}
		cout << ans << endl;
	}

}

Measured memory_bytes: 256
N, L = [int(_) for _ in raw_input().split(" ")]
ret =  [int(_) for _ in raw_input().split(" ")]
index = 0
for i in range(1, len(ret)):
  s = ret[i] + ret[i-1]
  if  L <= s:
    index = i+1
    break

if index == 0:
  print "Impossible"
else:
  print "Possible"
  for i in range(len(ret)-1, index-1, -1):
    print i
  for i in range(1, index):
    print i


Measured memory_bytes: 11828
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

struct Node {
	int x, y;
};

int h, w, k;
char r[810][810];
int s, t;
int d[810][810];
int ans;
int dx[] = { 0, 0, 1, -1 },
	dy[] = { 1, -1, 0, 0 };

void Bfs() {
	queue<Node> q;
	q.push((Node){ s, t });
	d[s][t] = 0;
	while (!q.empty()) {
		Node t = q.front(); q.pop();
		for (int i = 0; i < 4; ++i) {
			int xx = t.x + dx[i],
				yy = t.y + dy[i];
			if (xx < 1 || xx > h || yy < 1 || yy > w || d[xx][yy] != 0x3f3f3f3f || r[xx][yy] == '#') continue;
			d[xx][yy] = d[t.x][t.y] + 1;
			q.push((Node){ xx, yy });
		}
	}
}

int main() {
	scanf("%d%d%d", &h, &w, &k);
	for (int i = 1; i <= h; ++i) {
		scanf("%s", r[i] + 1);
	}
	for (int i = 1; i <= h; ++i) {
		for (int j = 1; j <= w; ++j) {
			if (r[i][j] == 'S') {
				s = i; t = j;
				break;
			}
		}
	}
	memset(d, 0x3f, sizeof(d));
	Bfs();
	ans = 0x3f3f3f3f;
	for (int i = 1; i <= h; ++i) {
		for (int j = 1; j <= w; ++j) {
			if (d[i][j] > k) continue;
			int dis = 0x3f3f3f3f;
			dis = min(dis, i - 1);
			dis = min(dis, j - 1);
			dis = min(dis, h - i);
			dis = min(dis, w - j);
			ans = min(ans, (dis + k - 1) / k + 1);
		}
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 3456
// https://atcoder.jp/contests/abc111/tasks/arc103_a

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;

typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

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

int main() {
    int n, t, sum;
    // 偶数と奇数の2つ用意
    map<int, int> mp[2];
    vector<pair<int, int>> vc[2];
    scanf("%d", &n);
    sum = n;

    REP(i, n) {
        scanf("%d", &t);
        if (mp[i % 2].count(t) == 0) {
            mp[i % 2][t] = 1;
        } else {
            mp[i % 2][t]++;
        }
    }

    // 偶数と奇数の（値と出現回数）をソートして、最大の出現回数を除いて加算する。
    // ただし、偶数と奇数の最大出現回数の値が同じ場合は、回数をみて判断する。
    REP(i, 2) {
        FOREACH(x, mp[i]) vc[i].push_back(make_pair(x.second, x.first));
        // 降順ソート
        sort(ALLR(vc[i]));
    }

    // 最大の出現回数の値が同じ場合
    if (vc[0][0].second == vc[1][0].second) {
        // 奇数の1番目＋偶数の2番目 と　奇数の2番目と偶数の1番目の出現回数を比較
        if (vc[0][0].first + vc[1][1].first > vc[0][1].first + vc[1][0].first) {
            sum -= vc[0][0].first;
            sum -= vc[1][1].first;
        } else {
            sum -= vc[0][1].first;
            sum -= vc[1][0].first;
        }
    } else {
        sum -= vc[0][0].first;
        sum -= vc[1][0].first;
    }

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

    return 0;
}


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

int main() {
    string S;
    cin >> S;
    int N = S.length();
    vector<int> ans(N+1, 0);
    for (int i = 0; i < N; i++) {
        if (S[i] == '<') {
            ans[i+1] = ans[i] + 1;
        }
    }
    
    reverse(S.begin(), S.end());
    reverse(ans.begin(), ans.end());
    for (int i = 0; i < N; i++) {
        if (S[i] == '>') {
            ans[i+1] = max(ans[i+1], ans[i] + 1);
        }
    }
    //reverse(ans.begin(), ans.end());
    long long sum = 0;
    for (auto a : ans) {
        sum += a;
      //  cout << a << " ";
    }
    cout << sum << endl;
    return 0;
}

Measured memory_bytes: 2820
package main

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


func main() {
	var n,m int
	fmt.Scan(&n, &m)

	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	uf := newUnionFind(n+10)
	for i := 0; i < m; i++ {
		sc.Scan()
		a, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		b, _ := strconv.Atoi(sc.Text())
		uf.unite(a, b)
	}
	cnt:= 0
	for i := 1 ; i <= n; i++ {
		if !uf.same(i, n) {
			uf.unite(i,n)
			cnt++
		}
	}
	fmt.Println(cnt)

}

type UnionFind struct {
	par []int
}

func newUnionFind(N int) *UnionFind {
	u := new(UnionFind)
	u.par = make([]int, N)
	for i := range u.par {
		u.par[i] = -1
	}
	return u
}

// xの所属するグループを返す
func (u UnionFind) root(x int) int {
	if u.par[x] < 0 {
		return x
	}
	u.par[x] = u.root(u.par[x])
	return u.par[x]
}
// xの所属するグループ と yの所属するグループ をJoinする
func (u UnionFind) unite(x, y int) {
	xr := u.root(x)
	yr := u.root(y)
	if xr == yr {
		return
	}
	u.par[yr] += u.par[xr]
	u.par[xr] = yr
}
// xとyが同じグループに所属するかどうかを返す
func (u UnionFind) same(x, y int) bool {
	return u.root(x) == u.root(y)
}
// xの所属するグループの木の大きさを返す
func (u UnionFind) size(x int) int {
	return -u.par[u.root(x)]
}


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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define ALL(x) x.begin(),x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9+7;
const ll INF = 1e18;
const int MAX = 2e6;

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

struct edge {int to, cost;}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問状況

int n, m; // 変数
int sub[MAX];

int main() {
// input
    cin >> n;
    memset(sub, 0, sizeof(sub));
    rep(i, n+1) {
        int boss;
        if (i > 1) {
            cin >> boss;
            sub[boss]++;
        }
    }
// solve
// output
    rep(i, n+1) {
        if (i > 0) cout << sub[i] << "\n";
    }
}

Measured memory_bytes: 11452
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void push(int);
int pop(void);

int top,S[1000];

int main(){
  int x;
  char s[100];
  int n,m;

  top = 0;

  while(scanf("%s",s) != EOF ){
    if ( s[0] == '+' ){
      n = pop();
      m = pop();
      push(n+m); 

    } else if ( s[0] == '-' ){
      m = pop();
      n = pop();
      push(n-m);
     
    } else if ( s[0] == '*' ){
      n = pop();
      m = pop();
      push(n*m);

    } else {//オペランド（数字）であればスタックに値を積む
      x = atoi(s);
      push(x);
    }
  }
  printf("%d\n",pop());
  
  return 0;
}

void push(int x){//top を 1 つ増やし、S[top] に x を代入
  top++;
  S[top] = x;
}

int pop(void){//S[top] の値を返し、top を１つ減らす
  top--;
  return S[top+1];
}




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

int n, p[N], f[N], ans;

int main() {// freopen("_in.txt","r",stdin);
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",p+i);
	for (int i=1;i<=n;i++) f[p[i]] = f[p[i]-1] + 1;
	for (int i=1;i<=n;i++) ans = max(ans,f[i]);
	printf("%d\n",n - ans);
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=200010;
const ll mod=1e9+7;

vector<int> v[mx];
vector<int> g(mx,-1);
  
void dfs(int n, int p, int dep){
  g[n] = dep%2;
  for(auto to:v[n])if(to!=p){ dfs(to,n,dep+1); }
}

int main() {
  int n;
  cin >> n;
  rep(i,n-1){
    int a,b; cin >> a >> b; a--; b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(0,-1,0);

  int nb = count(g.begin(),g.end(),0);
  int nw = count(g.begin(),g.end(),1);
  if(nb>nw){ swap(nb,nw); rep(i,n){g[i]=1-g[i];} }
  
  vector<int> ans(n);
  if(nb<=n/3){
    int bcnt=3,wcnt=1;
    rep(i,n){
      if(g[i]==0){ 
        ans[i]=bcnt; bcnt+=3; 
      } else {
        ans[i]=wcnt; wcnt+=(wcnt%3==1?1:2);
      }
    }
    rep(i,n)if(ans[i]>n){ ans[i]=bcnt; bcnt+=3; }

  } else {
    int bcnt=1,wcnt=2;
    rep(i,n){
      if(g[i]==0){ 
        ans[i]=bcnt; bcnt+=3; 
      } else {
        ans[i]=wcnt; wcnt+=3;
      }
    }
    int cnt=3;
    rep(i,n)if(ans[i]>n){ ans[i]=cnt; cnt+=3; }
  }
  rep(i,n){ cout << ans[i] << " "; }
  cout << endl;
  return 0;
}


Measured memory_bytes: 19200
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
B = iil()
C = iil()

ans = sum(B)

for i in range(1,n):
    if A[i-1] == A[i] - 1:
        ans += C[A[i-1]-1]

print(ans)

Measured memory_bytes: 3064
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef vector<string> svec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;

void Z_algorithm(const string &s, vector<int> &a) {
	int sz = s.size();
	a.resize(sz);
	a[0] = sz;
	int i = 1, j = 0;
	while (i < sz) {
		while (i + j < sz&&s[j] == s[i + j])++j;
		a[i] = j;
		if (j == 0) { ++i; continue; }
		int k = 1;
		while (i + k < sz&&k + a[k] < j)a[i + k] = a[k], ++k;
		i += k; j -= k;
	}
}

int gcd(int a, int b) {
	if (a < b)swap(a, b);
	while (b) {
		int r = a % b; a = b; b = r;
	}
	return a;
}
void solve() {
	string s, t; cin >> s >> t;
	int n = s.length(), m = t.length();
	int g = gcd(n, m);
	{
		bool is_inf = true;
		rep(i, g) {
			char memo = s[i];
			for (int j = i; j < n; j += g) {
				if (s[j] != memo)is_inf = false;
			}
		}
		rep(i, g) {
			char memo = t[i];
			for (int j = i; j < m; j += g) {
				if (t[j] != memo)is_inf = false;
			}
		}
		if (is_inf) {
			string ss = s.substr(0, g);
			string tt = t.substr(0, g);
			vector<int> num;
			ss += tt; ss += tt;
			Z_algorithm(ss, num);
			Rep(i, g, 2 * g) {
				if (num[i] >= g) {
					cout << -1 << endl; return;
				}
			}
		}
	}
	int sz = 10;
	int num = (n + m - 1) / m; num *= sz;
	string z;
	rep(i, num)z += t;
	rep(i, sz)z += s;
	vector<int> a;
	Z_algorithm(z, a);
	int sta = num * m;
	int ans = 0;
	rep(i, n) {
		int c = a[sta + i];
		c = min(c, sta);
		ans = max(ans, c / m);
	}
	cout << ans << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//cout << pow(1/6000.0, 1.0 / 25) << endl;
	//init();
	solve();
	//cout << "finish" << endl;
	//stop
	return 0;
}


Measured memory_bytes: 76120
/**
 *     author: qodjf
 *     created: 04.21.2020 21:54:50
 */
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

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

  int N, P;
  string s;
  cin >> N >> P;
  cin >> s;

  vector<int> dp(N + 1, 0);
  int x = 1;
  for (int i = N - 1; i >= 0; i--) {
    dp[i] = ((s[i] - '0') * x + dp[i + 1]) % P;
    x = (x * 10) % P;
  }
  ll ans = 0;
  if (P == 2 || P == 5) {
    for (int i = 0; i < N; i++) {
      if ((s[i] - '0') % P == 0) {
        ans += i + 1;
      }
    }
  } else {
    vector<int> cnt(P, 0);
    for (int i = 0; i <= N; i++) {
      ans += cnt[dp[i]];
      cnt[dp[i]]++;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 1428
from itertools import product
n_str = input()
n = int(n_str)
l = len(n_str)
e_list = ["3", "5", "7"]
ans = 0
for i in range(3, l + 1):
    for temp in list(product(e_list, repeat=i)):
        if "3" in temp and "5" in temp and "7" in temp:
            num = int("".join(temp))
            if num <= n:
                ans += 1
print(ans)

Measured memory_bytes: 11428
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define ins insert
#define rs (x<<1|1) 
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define cle(x) ((x).clear())
#define lowbit(x) ((x)&(-x))
#define SZ(x) (int((x).size()))
#define ms(x,y) memset(x,y,sizeof (x))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)]) 
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
template<class T>inline void read(T&num){
	num=0;T f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
	num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
const double pi=acos(-1);	
inline void judge(){
	freopen("input.txt","r",stdin);
} 
//********************************head*************************************
const int maxn=2e5+5,mod=1e9+7;
int n;
int x[maxn],v[maxn],q[maxn],maxi[maxn],mini[maxn],c[maxn];
pa seg[maxn];
void M(int x,int num){
	for(;x<=n;x+=lowbit(x))c[x]=(c[x]+num)%mod;
}
int Q(int x){
	if(x<0)return 0;
	int res=1;
	for(;x;x-=lowbit(x))res=(res+c[x])%mod;
	return res;
}
bool cmp(int a,int b){
	return v[a]<v[b];
}
bool cmp2(pa a,pa b){
	return a.w2<b.w2||(a.w2==b.w2&&a.w1<b.w1);
}
int main(){
	read(n);
	rep(i,1,n)read(x[i]),read(v[i]);
	rep(i,1,n)q[i]=i;
	sort(q+1,q+n+1,cmp);
	rep(i,1,n)q[i]=x[q[i]];
	maxi[1]=q[1];rep(i,2,n)maxi[i]=max(maxi[i-1],q[i]);
	mini[n]=q[n];per(i,n-1,1)mini[i]=min(mini[i+1],q[i]);
	rep(i,1,n){
		int l=1,r=i,ans=r;
		while(l<=r){
			if(maxi[mid]>=q[i])ans=mid,r=mid-1;
			else l=mid+1;
		}
		seg[i].w1=ans;
		l=i,r=n,ans=l;
		while(l<=r){
			if(mini[mid]<=q[i])ans=mid,l=mid+1;
			else r=mid-1;
		}
		seg[i].w2=ans;
	}
	sort(seg+1,seg+n+1,cmp2);
	rep(i,1,n){
		int tmp=(Q(seg[i].w2)-Q(seg[i].w1-2)+mod)%mod;
		M(seg[i].w2,tmp);
	}
	printf("%d\n",(Q(n)-Q(n-1)+mod)%mod);
	return 0;
}

Measured memory_bytes: 6528
import math 
def main():
    X = int(input())
    a = math.ceil(X/105)
    b = 105*a-X
    i = 0
    while 1:
        if b>5:
            b-=5
            i+=1 
        else:
            if b==0:
                break
            else:
                i+=1
                break
    if a<i:
        print(0)
    else:
        print(1)

                
if __name__ == "__main__":
    main()

Measured memory_bytes: 38384
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long d = (long)sc.nextInt();
    int ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long x = (long)sc.nextInt();
      long y = (long)sc.nextInt();
      if ((x * x) + (y * y) <= (d * d)) {
        ans++;
      }
    }
    System.out.println(ans+"");
    sc.close();
  }
}


Measured memory_bytes: 59724
#include <bits/stdc++.h>
using namespace std;
int main(){
	int N; cin >> N; 
	long long int L[100010] = {}; 
	long long int A[100010] = {}; 
	for(int i = 0; i < N; i++){
		cin >> A[i]; 
		if(i % 2 == 0) L[0] += A[i]; 
		else L[0] -= A[i]; 
	}
	A[N] = A[0]; 
	cout << L[0] << " "; 
	for(int i = 1; i < N; i++){
		L[i] = 2 * A[i-1] - L[i-1]; 
		cout << L[i] << " "; 
	}
	cout << endl; 
}

Measured memory_bytes: 2816
#include<iostream>
using namespace std;
main() {
	string s,u="[<++++++++++>-]>",c=">+++++[<++++++>-]>+++++[<++++++++>-]>+++++"+u+"++++++"+u+"+++++++"+u+"++++++++"+u+"+++++++++"+u+"++++++++++"+u+"+++++++++++"+u+"++++++++++++"+u+"<<<<<<<<<<<";
	int p=0,t[10]={},i,j,a,b,d;
	for(cin>>s,i=0;i<s.length();++i){
		a=(s[i]/10)-3,b=a-p,p=a,d=s[i]-(a+3)*10;
		for(j=b>0?b:-b;j--;)c+=(b>=0?">":"<");
		for(j=d;j--;)c+="+";
		c+=".";
		for(j=d;j--;)c+="-";
	}
	cout<<c<<endl;
}

Measured memory_bytes: 1248
import java.util.*;

class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int n;
		HashMap<String,String> hm = new HashMap<String,String>();
		while(true){
			n = scan.nextInt();
			if(n == 0){
				break;
			}
			for(int i = 0;i < n;i++){
				hm.put(scan.next(),scan.next());
			}
			n = scan.nextInt();
			for(int i = 0;i < n;i++){
				String s = scan.next();
				String t = hm.get(s);
				if(t == null){
					System.out.print(s);
				}else{
					System.out.print(t);
				}
			}
			System.out.println();
			hm.clear();
		}
	}
}

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

int main() {
	int n, k;
	int l[50];
	int res=0;
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		cin >> l[i];
	}
	sort(l, l + n);
	for (int i=n-k; n>i; ++i) {
		res += l[i];
	}
	cout << res;
}

Measured memory_bytes: 256
N = int(input())
D = list2 = [[0 for i in range(2)] for j in range(N)]

for i in range(N):
    D[i][0],D[i][1] = map(int, input().split())

ans = "No"
for i in range(N-2):
    if D[i][0] == D[i][1]:
        if D[i+1][0] == D[i+1][1]:
            if D[i + 2][0] == D[i + 2][1]:
                ans = "Yes"
print(ans)

Measured memory_bytes: 9196