akhauriyash/Code-Regression huggingface dataset:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans)
if __name__ == '__main__':
    main()

Measured memory_bytes: 8720
#include <stdio.h>
#include <string.h>

int main()
{
    int a, b, k;
    scanf("%d%d%d", &a, &b, &k);
    for (int i=1; i<=k; i++) {
        if (i%2 != 0) {
            if (a%2 != 0) a --;
            b += a/2;
            a /= 2;
        } else {
            if (b%2 != 0) b --;
            a += b/2;
            b /= 2;
        }
    }
    printf("%d %d\n", a, b);
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main(){

  vector <int> a(12);
  for(int i=0;i<12;i++) cin>>a[i];
  sort(a.begin(),a.end());

  int r=0;
  for(int i=0;i<12;i+=4) r+=(a[i]==a[i+1] && a[i]==a[i+2] && a[i]==a[i+3]);

  cout<<(r==3?"yes":"no")<<endl;

  return 0;

}



Measured memory_bytes: 3132
import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        int n = sc.nextInt();
        int k = sc.nextInt();
        long[] a = new long[100010];
        long[] b = new long[100010];
        long[] c = new long[100010];
        for (int i = 1;i <= n;i++) {
            a[i] = sc.nextLong();
            b[i] = b[i-1] + a[i];
        }
        
        long sum = 0;
        for (int i = 1;i <= n;i++) {
            sum += a[i]>0?a[i]:0;
            c[i] = sum;
        }
        
        long ret = 0;
        long t, u;
        for (int i = 1;i <= n-k+1;i++) {
            u = Math.max(b[i+k-1]-b[i-1],0);
            t = c[i-1]+u+c[n]-c[i+k-1];
            ret = Math.max(ret,t);
        }
        
        System.out.println(ret);
    }
}


Measured memory_bytes: 49828
#include<bits/stdc++.h>
#define mins(a,b) a=min(a,b)
#define maxs(a,b) a=max(a,b)
 template < typename T > std::string to_string( const T& n )
  {
    std::ostringstream stm ;
    stm << n ;
    return stm.str() ;
  }
using namespace std;

const long long INF=9000000000000000;
int main(){
  int n,a,b,aa=0,bb=1;
  cin>>n>>a>>b;
  char m[n];
  for(int i=0;i<n;i++){
    cin>>m[i];
    if(m[i]=='c')
      cout<<"No"<<endl;
    else if(m[i]=='a'){
      if(aa<a+b){
        cout<<"Yes"<<endl;
        aa++;
      }
      else
        cout<<"No"<<endl;
    }
    else{
      if(aa<a+b and b>=bb){
        cout<<"Yes"<<endl;
        aa++;
        bb++;
      }
      else{
        bb++;
        cout<<"No"<<endl;
      }
    }
  }
}

Measured memory_bytes: 768
#include<iostream> 
#include<iomanip> 
#include<vector> 
#include<map> 
#include<set> 
#include<algorithm> 
#include<numeric> 
#include<limits> 
#include<bitset> 
#include<functional> 
#include<type_traits> 
#include<queue> 
#include<stack> 
#include<array> 
#include<random> 
#include<utility> 
#include<cstdlib> 
#include<ctime>
#define _LIBCPP_DEBUG 0
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i = 0; i < (n); ++i)	
using namespace std;

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


int main()
{
	int X, Y;
	string board[50];

	cin >> X >> Y;

	rep(i, X) {
		cin >> board[i];
	}

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

	rep(i, X) {
		rep(j, Y) {
			if (board[i][j] == '.') {
				int cnt = 0;
				rep(k, 8) {
					int x = i + dx[k];
					int y = j + dy[k];
					if (X >= 0 && x < X && y >= 0 && y < Y && board[x][y] == '#') {
						cnt++;
					}
				}
				board[i][j] = char('0' + cnt);
			}
		}
	}
	rep(i, X) {
		cout << board[i] << endl;
	}
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main(){
	ll n; cin>>n;
  	ll a[n];
  	ll freq[200005] = {0};
  	for(ll i=0;i<n;i++){
    	cin>>a[i];
      	freq[a[i]]++;
    }
    ll ans=0;
    for(ll j=0;j<200005;j++){
        	ans += (freq[j]*(freq[j] - 1))/2;
        }
  	for(ll i=0;i<n;i++){
        ll s = a[i];
        ll k = ans - (freq[s]*(freq[s]-1))/2 + (freq[s]-1)*(freq[s]-2)/2;
        cout << k << endl;
    }
    
    return 0;
}


Measured memory_bytes: 5760
#include<iostream>
main(){int n,a,b,c,d,M[22][22],i,j,k;std::cin>>n>>k;for(i=0;i++<n;)for(j=0;j++<n;)M[i][j]=1<<29;for(;scanf("%d,%d,%d,%d",&a,&b,&c,&d),k+1;)if(k--){M[a][b]=c;M[b][a]=d;}for(k=0;k++<n;)for(i=0;i++<n;)for(j=0;j++<n;)M[i][j]=std::min(M[i][j],M[i][k]+M[k][j]);printf("%d\n",c-d-M[a][b]-M[b][a]);}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++) 
const ll INF = 1LL<<60;
const ll mod = (int)1e9 + 7;

int main() {
  int H,W;
  cin >> H >> W;
  char c[H+2][W+2];
  queue<pair<int,int>> que;
  queue<pair<int,int>> newq;
  for(int i=0;i<H+2;i++){
      c[i][0]='o';
      c[i][W+1]='o';
  }
  for(int i=0;i<W+2;i++){
      c[0][i]='o';
      c[H+1][i]='o';
  }
  for(int i=1;i<H+1;i++){
    for(int j=1;j<W+1;j++){
      cin >> c[i][j];
      if(c[i][j]=='#'){
        c[i][j]='o';
        que.push(pair<int,int>(i,j));
    	}
    }
  }
  int x=-1;
  bool nomove=true;
  while(nomove){
    x++;
    nomove=false;
    queue<pair<int,int>> emq;
    while(!que.empty()){
      pair<int,int> pe;
      int p,q;
      pe=que.front();
      que.pop();
      p=pe.first;
      q=pe.second;
      if(c[p][q-1]=='.'){
        c[p][q-1]='o';
        newq.push(pair<int,int>(p,q-1));
      }
      if(c[p][q+1]=='.'){
        c[p][q+1]='o';
        newq.push(pair<int,int>(p,q+1));
      }
      if(c[p-1][q]=='.'){
        c[p-1][q]='o';
        newq.push(pair<int,int>(p-1,q));
      }
      if(c[p+1][q]=='.'){
        c[p+1][q]='o';
        newq.push(pair<int,int>(p+1,q));
      }
    }
    swap(que,newq);
    swap(newq,emq);
    if(!que.empty())nomove=true;
  }
  cout << x << endl;
}

/*



 */



Measured memory_bytes: 9360
#include<bits/stdc++.h>
 
#define Vsort(a) sort(a.begin(), a.end())
#define Vreverse(a) reverse(a.begin(), a.end())
#define Srep(n) for(int i = 0; i < (n); i++)
#define Lrep(i,a,n) for(int i = (a); i < (n); i++)
#define Brep(n) for(int bit = 0; bit < (1<<n); bit++)
#define rep2nd(n,m) Srep(n) Lrep(j,0,m)
#define vi vector<int>
#define vi64 vector<int64_t>
#define vvi vector<vector<int>>
#define vvi64 vector<vector<int64_t>>
 
using namespace std;

int main(){
  int x,a,b;
  cin >> x >> a >> b;
  if(a >= b) cout << "delicious" << endl;
  else if(x >= (b - a)) cout << "safe" << endl;
  else cout << "dangerous" << endl;
}


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

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


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

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll,ll> P;

struct UF
{
	vector<int> par; // 親のインデックスを記憶する配列
	vector<int> sz; // サイズを記憶する。
	vector<int> rank;
	// 初期化
	UF(int n):par(n),sz(n),rank(n){
		for(int i = 0; i < n; i++){
			par[i] = i;sz[i] = 1;rank[i] = 0;
		}
	}
	// 親を求める
	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	// xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		if(rank[x] < rank[y]){
			par[x] = y;
			sz[y] += sz[x];
		}
		else{
			par[y] = x;
			sz[x] += sz[y];
			if(rank[x] == rank[y])rank[x]++;
		}
	}
	// xとyが同じ集合ならtrue
	bool same(int x, int y) { return find(x) == find(y); }
	// 素の集合のサイズを求める
	int size(int n){return sz[find(n)];}
};

bool used[111111];

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

	ll n, m;cin >> n >> m;
	UF uf(n);

	REP(i, m){
		int a, b, c;cin >> a >> b >> c;
		a--;b--;
		uf.unite(a, b);
	}

	int ans = 0;
	REP(i, n){
		if(!used[uf.find(i)]){
			ans++;
			used[uf.find(i)] = true;
		}
	}

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 1536
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

class Main{

    static class BIT{
        int data[];
        int N;
        void init(int n){
            N=n+1;
            data = new int[N];
        }
        void add(int i,int x){
            for(;i<N;i+=(i&-i))data[i]+=x;
        }
        int sum(int i){
            int res=0;
            for(; i>0;i-=(i&-i))res+=data[i];
            return res;
        }
    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N  = scan.nextInt();
        int[] a = new int[2*N];
        int[] Bindex = new int[N+1];
        int[] Windex = new int[N+1];
        for(int i=0;i<2*N;++i){
            String s = scan.next();
            a[i] = scan.nextInt() * (s.equals("B") ? 1:-1);
            if(s.equals("B"))Bindex[a[i]]=i;
            else Windex[-a[i]]=i;
        }

        int[][] Wsum = new int[2*N][N+2];
        int[][] Bsum = new int[2*N][N+2];
        for(int j=1;j<=N;++j){
            for(int i=1;i<2*N;++i){
                Wsum[i][j]=Wsum[i-1][j] + (a[i-1]<0&&Math.abs(a[i-1])<=j ? 1:0);
                Bsum[i][j]=Bsum[i-1][j] + (a[i-1]>0&&a[i-1]<=j ? 1:0);
            }
        }
        for(int i=1;i<2*N;++i){
            Wsum[i][N+1]=N;
            Bsum[i][N+1]=N;
        }


        int[][] dp = new int[N+1][N+1];
        for(int[] d : dp)Arrays.fill(d, Integer.MAX_VALUE/2);
        dp[0][0]=0;
        for(int i=0;i<=N;++i){
            for(int j=0;j<=N;++j){
                if(i>0)dp[i][j]=Math.min(dp[i][j], dp[i-1][j] + (Bindex[i] - Bsum[Bindex[i]][i] - Wsum[Bindex[i]][j]));
                if(j>0)dp[i][j]=Math.min(dp[i][j], dp[i][j-1] + (Windex[j] - Bsum[Windex[j]][i] - Wsum[Windex[j]][j]));
            }
        }
        System.out.println(dp[N][N]);

    }
}

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

int main() {
  int a, b, x;
  string str;
  cin >> a;
  long long arr[a];
  for (int i = 0; i < a; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + a);
  cout << abs(arr[0] - arr[a - 1]) << endl;
  return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader
import java.io.InputStreamReader

fun main(args: Array<String>) {
    val rs = BufferedReader(InputStreamReader(System.`in`))
            .readText()
            .trim()
            .split(regex = """\s+""".toRegex())
            .map { it.toInt() }
            .drop(n = 1)
            .toIntArray()

    // If we have `scan`, we can do better

    var maxR = Int.MIN_VALUE
    var minR = rs[0]

    for (r in rs.slice(1 until rs.size)) {
        maxR = Math.max(maxR, r - minR)
        minR = Math.min(minR, r)
    }

    println(maxR)
}


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

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

template<typename Functor>
struct functor_traits{
	template<typename C,typename Ret,typename Arg,typename... Args>
		static Arg helper(Ret(C::*)(Arg,Args...));
	template<typename C,typename Ret,typename Arg,typename... Args>
		static Arg helper(Ret(C::*)(Arg,Args...)const);
	//using first_argument_type=decltype(helper(&Functor::operator()));
	typedef decltype(helper(&Functor::operator())) first_argument_type;
};
template<typename Compare,typename T=typename functor_traits<Compare>::first_argument_type>
priority_queue<T,vector<T>,Compare> make_priority_queue(Compare comp){
	return priority_queue<T,vector<T>,Compare>(move(comp));
}

int main()
{
	const int di[]={-1,0,1,0},dj[]={0,-1,0,1};
	
	for(int h,w,n;cin>>h>>w>>n && h|w|n;){
		string t; cin>>t;
		vs grid(h);
		rep(i,h) cin>>grid[i];
		
		vi ds(n+1);
		rep(i,n){
			if(t[i]=='L') ds[i+1]=(ds[i]+1)%4;
			if(t[i]=='R') ds[i+1]=(ds[i]+3)%4;
		}
		vvi next(n+1,vi(4));
		{
			vi tmp(4,INF);
			per(i,n+1){
				tmp[ds[i]]=i;
				rep(j,4) next[i][j]=tmp[j];
			}
		}
		
		// (i,j,d,p)
		auto pq=make_priority_queue(
			[](tuple<int,int,int,int> a,tuple<int,int,int,int> b){return get<3>(a)>get<3>(b);}
		);
		rep(i,h) rep(j,w) if(grid[i][j]=='S') pq.emplace(i,j,0,0);
		vvi vis(h,vi(w));
		bool res=false;
		while(pq.size()){
			int i,j,d,p; tie(i,j,d,p)=pq.top(); pq.pop();
			if(i<0 || h<=i || j<0 || w<=j || p==INF || grid[i][j]=='#' || (vis[i][j]>>d&1)) continue;
			vis[i][j]|=1<<d;
			if(grid[i][j]=='G'){
				res=true;
				break;
			}
			rep(k,4) pq.emplace(i+di[k],j+dj[k],k,next[p][k]);
		}
		cout<<(res?"Yes":"No")<<endl;
	}
}

Measured memory_bytes: 14860
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool div10min( int a, int b )
{
	if( a%10 && b%10 )
		return a % 10 > b % 10;
	return a % 10 < b % 10;
}

int main()
{
	int n = 9;
	vector<int> v( 5 );
	int ans = 0;
	for( int i = 0; i < 5; i++ )
	{
		cin >> v[i];
	}
	
	sort( v.begin(), v.end(), div10min );

	for( int i = 0; i < 5; i++ )
	{
		if( ans % 10 )
		{
			ans += 10 - ans % 10;
		}

		ans += v[i];
	}

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

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
int N;
vector<int>G[100001];
bool used[100001];
int ub[100001];
int lb[100001];
int dp[100001];
void dfs2(int x,int now){
	//cout<<x<<" x"<<endl;
	if(x==0){
		if(ub[x]!=-1){
			dp[x]=ub[x];
		}else{
			dp[x]=1;
		}
	}else{
		if(lb[x]<=now-1&&now-1<=ub[x]){
			dp[x]=now-1;
		}else{
			dp[x]=now+1;
		}
	}
	used[x]=true;
	REP(i,G[x].size()){
		if(used[G[x][i]]==false){
			dfs2(G[x][i],dp[x]);
		}
	}
}

bool dfs(int x){
	if(used[x]==true)return true;
	used[x]=true;
	int mi=-10000000;
	int ma=100000000;
	bool odd=false;
	bool even=false;
	REP(i,G[x].size()){
		if(used[G[x][i]]==false){
			if(dfs(G[x][i])==false){
				return false;
			}
			//cout<<x<<" "<<G[x][i]<<" "<<lb[G[x][i]]<<" "<<ub[G[x][i]]<<endl;
			if(lb[G[x][i]]!=-10000000){
				if(lb[G[x][i]]%2==0){
					even=true;
				}else{
					odd=true;
				}
				
				if(mi==-10000000){
					mi=lb[G[x][i]]-1;
					ma=ub[G[x][i]]+1;
				}else{
					mi=max(lb[G[x][i]]-1,mi);
					ma=min(ub[G[x][i]]+1,ma);
				}
			}
		}
	}
	if(mi>ma)return false;
	//cout<<x<<" "<<ub[x]<<" "<<lb[x]<<"aa"<<mi<<" "<<ma<<endl;
	if(even==true&&odd==true)return false;
	if(ub[x]==-10000000){
		//cout<<"a"<<endl;
		if(mi!=-10000000){
			ub[x]=ma;
			lb[x]=mi;
		}
		return true;
	}
	if(mi==-10000000){
		return true;
	}
	if(mi<=lb[x]&&ub[x]<=ma){
		if(even==true&&lb[x]%2==1){
			return true;
		}
		if(odd==true &&lb[x]%2==0){
			return true;
		}
		if(even==false&&odd==false){
			return true;
		}
		return false;
	}else{
		return false;
	}
}

int main(){
	cin>>N;
	REP(i,N-1){
		int a;
		int b;
		cin>>a>>b;
		a--;
		b--;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	int K;
	cin>>K;
	REP(i,N){
		used[i]=false;
		ub[i]=-10000000;
		lb[i]=-10000000;
	}
	REP(i,K){
		int a,b;
		cin>>a>>b;
		a--;
		ub[a]=b;
		lb[a]=b;
	}

	if(dfs(0)==true){
		cout<<"Yes"<<endl;
		REP(i,N){
			used[i]=false;
		}
		dfs2(0,0);
		REP(i,N){
			cout<<dp[i]<<endl;
		}
	}else{
		cout<<"No"<<endl;
	}
	/*
	REP(i,N){
		cout<<"ub[i] "<<ub[i]<<" lb[i] "<<lb[i]<<endl;
	}*/
}

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

int main(){
  string l="qazwsxedcrfvtgb";
  string r="yhnujmikolp";
  while(1){
    string s;
    cin>>s;
    if(s[0]=='#')break;
    int sta=0;
    for(int i=0;i<l.size();i++){
      if(s[0]==l[i])sta=1;
    }
    int ans=0;
    for(int j=0;j<s.size();j++){
      if(sta==0){
        for(int i=0;i<l.size();i++){
          if(s[j]==l[i]){
            sta=1;
            ans++;
          }
        }
      }else{
        for(int i=0;i<r.size();i++){
          if(s[j]==r[i]){
            sta=0;
            ans++;
          }
        }
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 3156
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <stack>
#include <queue>
using namespace std;
using ll = long long;
using P = pair<int, ll>;

int main()
{
    int x, y;
    cin >> x >> y;
    int a = 4 * x - y;
    int b = y - 2 * x;
    if (a >= 0 && b >= 0 && a % 2 == 0 && b % 2 == 0)
    {
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
    return 0;
}


Measured memory_bytes: 3588
#include<iostream>
#include<vector>
#include<algorithm>
#define N 5

using namespace std;

void RockWin(vector<int>&);
void ScissorsWin(vector<int>&);
void PaperWin(vector<int>&);
void Draw(vector<int>&);

int main(){
  int i, x;
  vector<int> data;
  vector<int>::iterator rock, scissors, paper;

  while(1){
    cin >> x;
    if(x == 0) break;
    data.push_back(x);
    for(i=1; i<N; ++i){
      cin >> x;
      data.push_back(x);
    }

    rock = find(data.begin(), data.end(), 1);
    scissors = find(data.begin(), data.end(), 2);
    paper = find(data.begin(), data.end(), 3);

    if(rock != data.end() && scissors != data.end() && paper == data.end())
      RockWin(data);
    else if(rock == data.end() && scissors != data.end() && paper != data.end())
      ScissorsWin(data);
    else if(rock != data.end() && scissors == data.end() && paper != data.end())
      PaperWin(data);
    else
      Draw(data);

    for(i=0; i<data.size(); ++i)
      cout << data[i] << endl;

    data.clear();
  }

  return 0;
}

void RockWin(vector<int>& data){
  int i;
  for(i=0; i<data.size(); ++i){
    if(data[i] == 1) data[i] = 1;
    else data[i] = 2;
  }
}

void ScissorsWin(vector<int>& data){
  int i;
  for(i=0; i<data.size(); ++i){
    if(data[i] == 2) data[i] = 1;
    else data[i] = 2;
  }
}

void PaperWin(vector<int>& data){
  int i;
  for(i=0; i<data.size(); ++i){
    if(data[i] == 3) data[i] = 1;
    else data[i] = 2;
  }
}

void Draw(vector<int>& data){
  int i;
  for(i=0; i<data.size(); ++i)
    data[i] = 3;
}

Measured memory_bytes: 0
from copy import deepcopy
from collections import deque
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
Sum = deque([0])
for i in range(N):
    Sum.append(Sum[-1] + A[i])
Asum = deque([])
for i in range(N):
    for j in range(i+1, N+1):
        Asum.append(Sum[j] - Sum[i])

Search = deepcopy(Asum)
x = 0
for i in reversed(range(1, 41)):
    new = deque([])
    for s in Search:
        if (x + 2 ** (i-1)) & s == x + 2 ** (i-1):
            new.append(s)
    if len(new) >= K:
        Search = new
        x += 2 ** (i-1)
print(x)

Measured memory_bytes: 35948
#include<bits/stdc++.h>
using namespace std;
int read(){int x;scanf("%d",&x);return x;} 
int main(){
	int A=read(),B=read(),C=read();
	int S=A+B+C;
	int i=0;
	while(true){
		if(i>=A&&i>=B&&i>=C&&(3*i-A-B-C)%2==0){ 
			printf("%d\n",(3*i-A-B-C)/2);
			return 0; 
		} 
		i++;
	}
	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;

int main(){
    double n,m,d;
    cin >> n >> m >> d;
    
    double cnt=0;
    if(d==0) cnt=n;
    else cnt=2*n-2*d;

    printf("%.10lf\n",(m-1.0)*cnt/(n*n));
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

const int maxn = 3e5, mod = 998244353;
int n, r, g, b, rg, rb, gb, rgb;
char s[maxn + 3];

void settings() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}

int main() {
	settings();
	scanf("%d %s", &n, s + 1);
	int ans = 1;
	for (int i = 1; i <= 3 * n; i++) {
		if (s[i] == 'R') {
			if (gb) {
				ans = 1ll * ans * gb % mod;
				gb--, rgb++;
			} else if (g) {
				ans = 1ll * ans * g % mod;
				g--, rg++;
			} else if (b) {
				ans = 1ll * ans * b % mod;
				b--, rb++;
			} else {
				r++;
			}
		} else if (s[i] == 'G') {
			if (rb) {
				ans = 1ll * ans * rb % mod;
				rb--, rgb++;
			} else if (r) {
				ans = 1ll * ans * r % mod;
				r--, rg++;
			} else if (b) {
				ans = 1ll * ans * b % mod;
				b--, gb++;
			} else {
				g++;
			}
		} else {
			if (rg) {
				ans = 1ll * ans * rg % mod;
				rg--, rgb++;
			} else if (r) {
				ans = 1ll * ans * r % mod;
				r--, rb++;
			} else if (g) {
				ans = 1ll * ans * g % mod;
				g--, gb++;
			} else {
				b++;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		ans = 1ll * ans * i % mod;
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 512
from collections import deque
import copy
n,m = map(int,input().split())
Map = [[] for i in range(n)]

for i in range(m):
    a,b = map(int,input().split())
    Map[a-1].append(b-1)
    Map[b-1].append(a-1)
visit = [False for i in range(n)]
ans = 0
q = deque()
q.append((0,visit))
while q:
    index,visit = q.popleft()
    Visit = copy.deepcopy(visit)
    if not Visit[index]:
        Visit[index] = True
    else:
        continue
    if all(Visit):
        ans += 1
        continue
    for i in Map[index]:
        q.append((i,Visit))
print(ans)

Measured memory_bytes: 6624
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;

ll a[100010];
int n, k;
ll ans = 10000000000;

int main()
{
	cin >> n >> k;
	for (int i = 0; i < n; i++)cin >> a[i];
	sort(a, a + n);
  	k--;
	for (int i = 0; i + k < n; i++)ans = min(ans, a[i + k] - a[i]);
	cout << ans << endl;
}

Measured memory_bytes: 1024
N, M = map(int, input().split())

WA_count = 0
AC_count = 0
Penal = 0
d = {}
d_W = {}

for i in range(1, M+1):
    p, S = list(map(str, input().split()))
    if S == "WA":
        if p not in d_W.keys():
            d_W[p] = 1
        else:
            d_W[p] += 1
    else:
        if p not in d_W.keys():
            d[p] = 0
        elif p not in d.keys():
            d[p] = d_W[p]

    # if S == "WA":
    #     WA_count += 1
    # else:
    #     if p not in d.keys():
    #         d[p] = WA_count
    #         WA_count = 0
    #     else:
    #         WA_count = 0
        # cor.append(p)
        # if AC_count != set(cor):
        #     AC_count = set(cor)
        #     Penal += WA_count
        #     WA_count = 0
        # else:
        #     WA_count = 0

print(len(d), sum(d.values()))


Measured memory_bytes: 17224
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define PI 3.141592
#define LL_INF 9000000000000000000
#define LL_MINF -9000000000000000000
#define INT_INF 2000000000
#define INT_MINF -2000000000
#define all(a) (a).begin(), (a).end()
#define fi first
#define se second

int main(){
    int X;
    cin >> X;
    int max_n = 0;
    for(int b = 1; b <= 1000; b++){
        for(int p = 2; p <= 10; p++){
            int n = pow(b, p);
            if(n <= X) max_n = max(max_n, n);
        }
    }
    cout << max_n << endl;
}


Measured memory_bytes: 4124
import math

def isPrime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True

X = int(input())
i = X
while True:
    if isPrime(i):
        break
    i+=1
print(i)

Measured memory_bytes: 2940
from operator import itemgetter
n = int(input())
v = [int(x) for x in input().split()]
li = [[[0 for _ in range(2)] for _ in range(10**5)] for _ in range(2)]
for index,i in enumerate(v):
    li[index%2][i-1][0]+=1
    li[index%2][i-1][1]=i

li[0].sort(key=itemgetter(0),reverse=True)
li[1].sort(key=itemgetter(0),reverse=True)

if(li[0][0][1]!=li[1][0][1]):
    print(n-li[0][0][0]-li[1][0][0])
else:
    print(min(n-li[0][0][0]-li[1][1][0],n-li[0][1][0]-li[1][0][0]))


Measured memory_bytes: 31844
#include <stdio.h>

int main( void ) {
	int i, n, sum, elmNum[ 9 ] = { 5, 3, 3, 3, 3, 3, 4, 3, 4 };
	char ch[ 9 ][ 6 ] = { ".,!? ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }, key, tmp;

	scanf( "%d%*c", &n );

	for ( i = 0; i < n; i++ ) {
		while ( 1 ) {
			scanf( "%c", &key );

			if ( key == '\n' )
				break;
			else if ( key == '0' )
				continue;

			for ( sum = 0; scanf( "%c", &tmp ), tmp != '0'; sum++ ) ;
			putchar( ch[ key - '1' ][ sum % elmNum[ key - '1' ] ] );
		}

		putchar( '\n' );
	}

	return 0;
}

Measured memory_bytes: 568
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

L = [1 for i in range(100010)]
L[0] = 0
L[1] = 0
plist = []
for i in range(100010):
	if L[i] == 1:
		plist.append(i)
		c = 2
		while True:
			if i * c <= 100009:
				L[i * c] = 0
				c += 1
			else:
				break

#処理内容
def main():
	X = int(input())
	for i in plist:
		if X <= i:
			print(i)
			break

if __name__ == '__main__':
	main()

Measured memory_bytes: 44784
#include <iostream>
#include <string>
#include <queue>
#include <math.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    ll jun[n];
    FOR(i,1, n+1){
        ll tmp=0;
        cin >> tmp;
        jun[tmp] = i;
    }
    FOR(i,1, n+1){
        cout << jun[i] << " ";
    }
    cout << "\n";
    return 0;
}


Measured memory_bytes: 1664
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

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

    vi L(2*N);

    rep(i, 2*N) cin >> L[i];

    sort(all(L));

    int ans = 0;

    for (int i = 0; i < N; ++i) ans += L[i * 2];

    cout << ans << endl;
}


Measured memory_bytes: 256
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

class UnionFind:
    def __init__(self, node:int) -> None:
        self.n = node
        self.par = [i for i in range(self.n)]
        self.rank = [0 for i in range(self.n)]
        self.elem = [1 for i in range(self.n)]
        
    def find(self, x:int) -> int:
        if x == self.par[x]:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
        
    def unite(self, x:int, y:int) -> bool:
        if self.isSame(x,y):
            #print("x and y has already united")
            return False
        
        rx = self.find(x)
        ry = self.find(y)
        
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = self.par[ry]
            self.elem[ry] += self.elem[rx]
            self.elem[rx] = 0
        else:
            self.par[ry] = self.par[rx]
            self.elem[rx] += self.elem[ry]
            self.elem[ry] = 0
            
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1
                
        return True
                
    def isSame(self, x:int, y:int) -> bool:
        return self.find(x) == self.find(y)
    
n,m = li()
ab = []

uf = UnionFind(n)
ans = [n*(n-1)//2]
cur = n*(n-1)//2

for _ in range(m):
    ab.append(list(li_()))
    
for _ in range(m):
    a,b = ab.pop()
    if uf.find(a) == uf.find(b):
        ans.append(cur)
        
    else:
        ra = uf.find(a)
        rb = uf.find(b)
        cur -= uf.elem[ra]*uf.elem[rb]
        
        ans.append(cur)
        
        uf.unite(ra, rb)
        
for ansi in ans[-2::-1]:
    print(ansi)

Measured memory_bytes: 33332
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll mod=1e9+7 ;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}





int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s,t;
    int n;
    cin>>n;
    vector<ll>v;
    for(int i=0;i<n;i++){
        ll a;
        cin>>a;
        v.push_back(a);
    }
    int res=0;
    int cnt=0;
    for(int i=n-1;i>=1;i--){
        if(v[i]<=v[i-1]){cnt++;continue;}
        else{res=max(res,cnt);cnt=0;}
    }
    res=max(res,cnt);
    cout<<res<<endl;
    return 0;
    }


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

#define rep(i,a,b) for(int i=a;i<b;i++)
#define int long long
const int inf = 100100100100100009;
const int mod = 1000000007;


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

    int n, v;
    cin >> n >> v;

    int coins[4][n];
    rep(i,0,4) rep(j,0,n) cin >> coins[i][j];

    vector<int> ab;
    vector<int> cd;

    

    rep(i,0,n){
        rep(j,0,n){
            ab.push_back(coins[0][i] + coins[1][j]);
            cd.push_back(coins[2][i] + coins[3][j]);
        }
    }

    sort(ab.begin(), ab.end());
    sort(cd.begin(), cd.end());

    int ans = 0;
    for(auto e: ab){
        ans += upper_bound(cd.begin(), cd.end(), v - e) - lower_bound(cd.begin(), cd.end(), v - e);
    }

    cout << ans << endl;

}


Measured memory_bytes: 18860
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
 
// ===============================================================
 
using namespace std;
using ll = long long;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using vs = vector<string>;
using vc = vector<char>;
using vcc = vector<vector<char>>;
using vm = vector<short>;
using vmm = vector<vector<short>>;
using pii = pair<int, int>;
using psi = pair<string, int>;
using ld = long double;
using ull = unsigned long long;
 
// ===============================================================
 
ll gcd(ll a, ll b) //最大公約数
{
	if (a % b == 0)
	{
		return(b);
	}
	else
	{
		return(gcd(b, a % b));
	}
}
 
ll lcm(ll a, ll b) //最小公倍数
{
	return a * b / gcd(a, b);
}
 
ll box(double a) //doubleの切り捨て
{
	ll b = a;
	return b;
}
 
ll fff(double a) //doubleの四捨五入
{
	ll b = a + 0.5;
	return b;
}
 
ll sum(ll n) { //整数sまでの合計
	if (n == 0) {
		return 0;
	}
 
	int s = sum(n - 1);
	return s + n;
}
 
bool prime(ll num)//素数判定、primeならtrue,違うならfalse
{
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false;
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}
 
	// 素数である
	return true;
}
 
// ===============================================================
 
int main() {
    ll x;
	string a;
	ll b;
	cin >> x >> b >> a;
	a[b - 1] += 32;
	cout << a << endl;
}

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

dsum = 0

for i in range(n-1):
  for j in range(i+1, n):
    d = ( (xy[i][0] - xy[j][0])**2 + (xy[i][1] - xy[j][1])**2 )**0.5
    dsum += d

dave = dsum / (n*(n-1) / 2)

print(dave*(n-1))

Measured memory_bytes: 9432
#include<iostream>
using namespace std;
int main(){
  int n;
  cin>>n;
  long sum = 0;
  for(int i=1;i<=n;i++){
    if(i%3!=0 && i%5 !=0){
      sum+=i;
    }
  }
    cout<<sum;
  
  return 0;
}
  


Measured memory_bytes: 3636
s = input()
s1 = int(s[0]+s[1])
s2 = int(s[2]+s[3])

if 1<=s1<=12 and 0<=s2<=99:
    if 1<=s2<=12:
        print("AMBIGUOUS")
    else:
        print("MMYY")
else:
    if 0<=s1<=99 and 1<=s2<=12:
        print("YYMM")
    else:
        print("NA")

    

Measured memory_bytes: 38384
#include <cstdio>
#include <algorithm>

using namespace std;

int cnt[200001];
int n,k;

long long get(int a,int b,int c) {
	return 1ll*cnt[a]*cnt[b]*cnt[c];
}
int main() {
	scanf("%d%d",&n,&k);
	int val=n%k,c=n/k;
	for(int i=0;i<k;i++) cnt[i] = c;
	for(int i=1;i<=val;i++) cnt[i]++;

	long long ans = get(0,0,0);
	if(k%2==0) {
		int t=k/2;
		ans += get(t,t,t);
	}
	printf("%lld\n",ans);
	

	return 0;
}


Measured memory_bytes: 896
fun main(args:Array<String>) {
  val (a,b) = readLine()!!.split(" ").map(String::toInt)
  val aa = readLine()!!
  val h = """\d""".repeat(a)
  val t = """\d""".repeat(b)
  when((h+"-"+t).toRegex().matchEntire(aa)){
     null -> "No"
     else -> "Yes"
  }.let(::println)
}

Measured memory_bytes: 36068
object Main {
  var mul_limit = 12000
  var depth_limit = 7
  def dfs(p: Int, q: Int, depth: Int, sub: Int, mul: Int): Int = {
    p match {
      case 0 => 1
      case _ =>
        var ret = 0
        for{
          i <- (sub to mul_limit / mul).takeWhile(x =>
            (depth_limit - depth) * q >= p * x).dropWhile(x =>
            p * x < q)
        } ret += dfs(p * i - q, q * i, depth + 1, i, mul * i)
        ret
    }
  }
  def main(args:Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    var p, q = sc.nextInt
    mul_limit = sc.nextInt
    depth_limit = sc.nextInt
    while(p != 0) {
      println(dfs(p, q, 0, 1, 1))
      p = sc.nextInt
      q = sc.nextInt
      mul_limit = sc.nextInt
      depth_limit = sc.nextInt
    }
  }
}

Measured memory_bytes: 188408
// Dice01.c
#include<stdio.h>

struct Dice {
  int number[6];
};

void setLabel( struct Dice *dice, int index, int num) {
  dice->number[index] = num;
}

void setNumber( struct Dice *dice, int n0, int n1, int n2, int n3, int n4, int n5) {
  dice->number[0] = n0;
  dice->number[1] = n1;
  dice->number[2] = n2;
  dice->number[3] = n3;
  dice->number[4] = n4;
  dice->number[5] = n5;
}

void roll( struct Dice *dice, char dst) {
  int work[6];

  for ( int i = 0; i < 6; i++) work[i] = dice->number[i];
  switch ( dst) {
  case 'E':
    setNumber( dice, work[3], work[1], work[0], work[5], work[4], work[2]);
    break;
  case 'N':
    setNumber( dice, work[1], work[5], work[2], work[3], work[0], work[4]);
    break;
  case 'S':
    setNumber( dice, work[4], work[0], work[2], work[3], work[5], work[1]);
    break;
  case 'W':
    setNumber( dice, work[2], work[1], work[5], work[0], work[4], work[3]);
    break;
  }
}

int returnPeek( struct Dice *dice) {
  return dice->number[0];
}

int main( void) {
  struct Dice dice;
  int label;
  char ch;

  for ( int i = 0; i < 6; i++) {
    scanf( "%d", &label);
    setLabel( &dice, i, label);
  }
  getchar();
  while ( ( ch = (char)getchar()) != '\n') {
    roll( &dice, ch);
  }

  printf( "%d\n", returnPeek( &dice));

  return 0;
}



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

#define ll long long

int main(){
	ll n,x;
	cin >> n;
	stack<ll>st;
	ll ans = 0;
	for(ll i = 0;i < n;i++){
		cin >> x;
		while(!st.empty() && st.top() < x){
			st.pop();
		}
		if(!st.empty()){
			ans += abs(st.top() - x);
			st.push(x + abs(st.top() - x));
		}
		else st.push(x);
	}
	cout<<ans;
}

Measured memory_bytes: 5244
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <stdio.h>
#include <tuple>
#include <bitset>
#include <cfloat>
#include <fstream>
#include <limits.h>
#include <list>
#include <math.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <numeric>
#include <utility>
#include <sstream>

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

using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORR(i,a,b)for(ll i=(a);i<=(b);i++)
#define repR(i,n) for(ll i=n;i>=0;i--)
#define P pair<ll, ll>
#define sz(x) (ll)x.size()
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define VE vector<ll>
#define COUT(x) cout<<(x)<<endl
#define MA map<ll,ll>
#define SE set<ll>
#define PQ priority_queue<ll>
#define PQR priority_queue<ll,VE,greater<ll>>
#define COUT(x) cout<<(x)<<endl
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (b == 0) return a;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}
bool prime(ll n) {
	for (ll i = 2; i <= sqrt(n); i++) {
		if (n%i == 0)return false;
	}
	return n != 1;
}


const long long MOD = 1000000007;
const long long INF = 1e17;
const long long LINF = 1LL << 50;
#define int long long
int dist[310][310];
int dist2[310][310];
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	rep(i, 310) {
		rep(j, 310) {
			dist[i][j] = INF;
			dist2[i][j] = INF;
		}
	}
	rep(i, 310) {
		dist[i][i] = 0;
		dist2[i][i] = 0;
	}
	int n, m, l; cin >> n >> m >> l;
	VE c(m);
	vector<VE>G(n);
	rep(i, m) {
		int a, b; cin >> a >> b;
		a--; b--;
		G[a].push_back(b);
		G[b].push_back(a);
		cin >> c[i];
		chmin(dist[a][b], c[i]);
		chmin(dist[b][a], c[i]);
	}
	rep(k, n) {
		rep(i, n) {
			rep(j, n) {
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	rep(i, n) {
		rep(j, n) {
			if (i == j)continue;
			if (dist[i][j] <= l) {
				chmin(dist2[i][j], (int)1);
			}
		}
	}
	rep(k, n) {
		rep(i, n) {
			rep(j, n) {
				dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]);
			}
		}
	}
	int q; cin >> q;
	rep(i, q) {
		int s, t; cin >> s >> t;
		s--; t--;
		if (dist2[t][s] < INF)cout << dist2[t][s] - 1 << endl;
		else cout << -1 << endl;
	}
	return 0;
}


Measured memory_bytes: 3584
S = str(input())
n = len(S)
sgn = [0]*n
for i in range(n):
    if (i%2) == 1:
        if S[i] == "L" or S[i] == "U" or S[i] == "D":
            sgn[i] = 0
        else:
            sgn[i] = 1
    if (i%2) == 0:
        if S[i] == "R" or S[i] == "U" or S[i] == "D":
            sgn[i] = 0
        else:
            sgn[i] = 1
if sum(sgn) == 0:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3064
#include<iostream>
#include<vector>
using namespace std;
int main()
{
  long long int n,x=0,i=0,p;
  cin>>n;
  vector<long long int> V(n);
  for(i=0;i<n;i++)
  {
    cin>>V[i];
    x^=V[i];
  }
  for(i=0;i<n;i++)
  {
    p=x^V[i];
    cout<<p<<" ";
  }
}

Measured memory_bytes: 4680
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    string s;
    cin >>s;
    int n = s.size();

    vector<int> ans(n);
    int x=0;
    while(x<n){
        int r=x;
        while(s[r]=='R') ++r;
        int lidx = r;

        int l=r;
        while(l<n && s[l]=='L') ++l;

        int m = 0;
        for(int i=lidx-1; i>=x; --i){
            ++ans[lidx-1+m];
            m = !m;
        }
        m = 1;
        for(int i=lidx; i<l; ++i){
            ++ans[lidx-1+m];
            m = !m;
        }

        x=l;
    }

    rep(i,n) cout << ans[i] << " \n"[i==n-1];
    return 0;
}


Measured memory_bytes: 1024
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN = 2e5 + 5;

int A[MAXN + 5], B[MAXN + 5];

int N, M;
int main()
{
	scanf("%d", &N);
	for(int i = 1; i <= N; i++) scanf("%d", &A[i]);
	sort(A + 1, A + 1 + N);
	scanf("%d", &M);
	for(int i = 1; i <= M; i++) scanf("%d", &B[i]);
	sort(B + 1, B + 1 + M);
	int tail = 1;
	for(int i = 1; i <= N; i++){
		if(tail <= M && A[i] == B[tail]) tail++;
	}
	if(tail > M) printf("YES\n");
	else printf("NO\n");
}

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

#define int long long int 

int32_t main()
{
    int n;
    cin>>n;
    int a[n+2],ans=0;
    a[0]=0;
    a[n+1]=0;
    for(int i=1;i<=n;i++)
     {
     cin>>a[i];
      ans+=abs(a[i]-a[i-1]);
     }
     ans+=abs(a[n]);
    
     
    int diff[n+2];
   
    diff[0]=0;
    diff[n+1]=0;
    for(int i=1;i<=n;i++)
       diff[i]=abs(a[i]-a[i-1])+abs(a[i+1]-a[i]);
    //     for(auto i:diff)
    // cout<<i<<" ";
    // cout<<endl;
    // cout<<ans<<endl;
    for(int i=1;i<n;i++)
     cout<<ans-diff[i]+abs(a[i+1]-a[i-1])<<endl;
     cout<<ans-abs(a[n-1]-a[n])-abs(a[n])+abs(a[n-1])<<endl;
    
    
}

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
 
Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

Int dp[3001][3001];

int main() {
    Int n;
    cin >> n;
    vector<Int> a(n);
    for (Int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (Int i = 0; i < n; i++) {
        dp[i][i] = a[i];
    }
    for (Int i = 0; i + 1 < n; i++) {
        if (n % 2 == 0) {
            dp[i][i + 1] = max(a[i], a[i + 1]) - min(a[i], a[i + 1]);
        } else {
            dp[i][i + 1] = min(a[i], a[i + 1]) - max(a[i], a[i + 1]);
        }
    }
    for (Int len = 2; len < n; len++) {
        for (Int i = 0; i + len < n; i++) {         
            if ((n + len) % 2 == 1) {
                Int a = dp[i + 1][i + len] + dp[i][i];
                Int b = dp[i][i + len - 1] + dp[i + len][i + len];
                dp[i][i + len] = max(a, b);
            } else {
                Int a = dp[i + 1][i + len] - dp[i][i];
                Int b = dp[i][i + len - 1] - dp[i + len][i + len];
                dp[i][i + len] = min(a, b);
            }
            //cerr << dp[i][i + len] << " ";
        }
        //cerr << endl;
    }
    dump(dp[0][n - 1]);
    return 0;
}

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

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

struct BIT
{
  vector<int> data;
  BIT(int n):data(n+1, 0){}
  int sum(int k, int P)
  {
    int ret = 0;
    while(k > 0) {
      ret = (ret + data[k]) % P;
      k -= k & -k;
    }
    return ret;
  }
  void add(int k, int x, int P)
  {
    while(k < data.size()) {
      data[k] = (data[k] + x) % P;
      k += k & -k;
    }
  }
};

vector<string> lex;
vector<int> idx;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int A, B, P;
  while(cin >> A >> B >> P, A) {
    int n = B - A;
    BIT bit(n+1);
    lex.clear(); lex.resize(n+1);
    idx.clear(); idx.resize(n+1);
    for(int i = A; i <= B; i++) lex[i-A] = to_string(i);
    sort(all(lex));
    for(int i = 0; i <= n; i++) idx[stoi(lex[i])-A] = i;
    for(int i = 0; i <= n; i++) {
      int sum = bit.sum(idx[i], P);
      bit.add(idx[i]+1, (sum + 1) % P, P);
    }      
    cout << bit.sum(n+1, P) << endl;
  }
  
  return 0;
}

Measured memory_bytes: 10056