akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0,i##_len=int(n);i<i##_len;++i)
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define All(x) (x).begin(),(x).end()
#define rAll(x) (x).rbegin(),(x).rend()
using namespace std;
using ll = long long;

int main(){
    int N;cin>>N;
    vector<ll> A(N);
    REP(i,N) cin>>A[i];
    sort(rAll(A));
    ll ans = A[0];
    rep(i,1,N/2) ans+=2*A[i];
    if(N&1) ans+=A[N/2];
    cout<<ans<<endl;
}


Measured memory_bytes: 4844
n,x,y=map(int,input().split())
x-=1
y-=1
a=[]
for i in range(n):
  a.append(0)
for i in range(n-1):
  for j in range(n-i):
    j=n-1-j
    p=min(abs(j-i),abs(x-i)+abs(y-j)+1,abs(y-i)+abs(x-j)+1)
    a[p]+=1
for i in range(n-1):
  print(str(a[i+1]))

Measured memory_bytes: 3444
///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>

#define                           int                         long long
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);




using namespace std;

const int inf=1e18;
const int mod=1e9+7;
const int M=100009;
inline int bigmod(int B,int P){int R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}


 main()

{
    fast
    int s,w;
    cin>>s>>w;
    if(w>=s)
    {
        cout<<"unsafe"<<endl;
    }
    else cout<<"safe"<<endl;

    return 0;

}
















Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... U> void assign(V<T>& v, int n, const U&... u) { v.resize(n); for (auto&& i : v) assign(i, u...); }

const int inf = 1e9;
struct edge { int to, cost; edge(int to, int cost) : to(to), cost(cost) {} };

template<class T> V<T> dijkstra(int s, const VV<edge>& g) {
  V<T> d(g.size(), inf);
  using P = pair<T, int>;
  priority_queue<P, V<P>, greater<P> > pq;
  pq.emplace(d[s] = 0, s);
  while (!pq.empty()) {
    T dv; int v; tie(dv, v) = pq.top(); pq.pop();
    if (dv > d[v]) continue;
    for (auto&& e : g[v]) {
      if (d[e.to] <= d[v] + e.cost) continue;
      pq.emplace(d[e.to] = d[v] + e.cost, e.to);
    } 
  }
  return d;
}

int main() {
  cin.tie(NULL); ios::sync_with_stdio(false);
  int n; cin >> n;
  VV<edge> g(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b; cin >> a >> b, a--, b--;
    g[a].emplace_back(b, 1);
    g[b].emplace_back(a, 1);
  }
  int k; cin >> k;
  for (int i = 0; i < k; i++) {
    int v, p; cin >> v >> p, v--;
    g[n].emplace_back(v, p);
    g[v].emplace_back(n, p);
  }
  auto d = dijkstra<int>(n, g);
  bool ok = true;
  for (int i = 0; i < n; i++) for (auto&& e : g[i]) if (e.to != n) {
    if (abs(d[i] - d[e.to]) != 1) ok = false;
  }
  for (auto&& e : g[n]) if (d[e.to] != e.cost) ok = false;
  if (ok) {
    cout << "Yes" << '\n';
    for (int i = 0; i < n; i++) cout << d[i] << '\n';
  } else cout << "No" << '\n';
}

Measured memory_bytes: 10356
# frozen_string_literal: true

n, m, q = gets.split.map(&:to_i)
abcd = q.times.map { gets.split.map(&:to_i) }
puts (1..m).to_a.repeated_combination(n).map { |f| abcd.map { |e| f[e[1] - 1] - f[e[0] - 1] == e[2] ? e[3] : 0}.sum }.max


Measured memory_bytes: 16312
import qualified Data.ByteString.Char8 as BS
import Control.Monad
import Data.Char
import Data.List

getVals::IO [Int]
getVals = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

main::IO()
main = do
    a:[] <- getVals
    print $ 1-a

Measured memory_bytes: 3772
#include <bits/stdc++.h>
#define int long long
#define INF 1e+18
using namespace std;

signed main(){
	int n;
	while(cin >> n,n){
		int a[1010],mi = INF;
		for(int i = 0;i < n;i++) cin >> a[i];
		for(int i = 0;i < n;i++){
			for(int j = i + 1;j < n;j++) mi = min(mi,abs(a[i] - a[j]));
		}
		cout << mi << endl;
	}
}


Measured memory_bytes: 3092
N, Y = map(int,input().split())
for i in range(N+1):
    for j in range(N+1-i):
        if  10000 * (N - i - j)+ 5000 * j + 1000 * i == Y:
            print(N - i - j,j,i)
            exit()
print("-1 -1 -1")

Measured memory_bytes: 2940
n,w=map(int,input().split())
W=[]
V=[]
for _ in range(n):
    x,y=map(int,input().split())
    W.append(x)
    V.append(y)
dp=[[0]*(w+1) for _ in range(n+1)]

for i in range(n):
    for j in range(w+1):
        if W[i]>j:
            dp[i+1][j]=dp[i][j]
        else:
            dp[i+1][j]=max(dp[i][j],dp[i][j-W[i]]+V[i])

print(dp[n][w])

Measured memory_bytes: 146376
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define c(P)        cout<<P<<"\n"
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define F           first
#define S           second
#define sz(a)       (ll)(a.size())
#define cr(P)       cout<<P<<"\n";return;
#define mem(a,val)  memset(a,val,sizeof(a))
#define fr(i,x,n)   for(ll i=x;i<n;i++)
#define frr(i,x,n)  for(ll i=n-1;i>=x;i--)
using namespace std;


const ll N   =  500005;
const ll mod = 1e9 + 7;
ll dp[2005][2005];
vector<pii>v;
ll f(ll idx,ll i,ll j){
	if(idx==sz(v))
		return 0;
	ll &ans = dp[idx][i];
	if(ans!=-1)
		return ans;
	ans= max(abs(i-v[idx].S)*v[idx].F + f(idx+1,i+1,j),
		abs(j-v[idx].S)*v[idx].F + f(idx+1,i,j-1));
	return ans;
}


void solve(){

  ll x=0,y=0,c=1,ans=0;
  ll n,m,k,l= 0,r,i;
  cin>>n;
  mem(dp,-1LL);

  for (ll i = 0; i < n; ++i){
  	cin>>x;
  	v.pb({x,i});
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  c(f(0,0,n-1));
}

signed main(){
     
  ios_base::sync_with_stdio(false);   
  cin.tie(NULL);
  //int T;cin >> T;while (T--)
  solve();
  return 0;
}


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

int main() 
{
    int a, b;
    cin >> a >> b;

    int r = a+b;
    if(r%2 == 1) {
        cout << "IMPOSSIBLE" << endl;
        return 0;
    }
    cout << r/2 << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

int main()
{
	int N, K;
	cin >> N >> K;
	int ans = 0;
	while (N > 0) {
		ans++;
		N /= K;
	}
	cout << ans << endl;
}


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

int length(char*str) {
	int a = strlen(str);
	return a;
}

int main() {
	char x[200020];
	cin >> x;
	int ans = length(x), s = 0;
	for (int i = 0; i < length(x); i++) {
	    if (x[i] == 'S') s++;
	    else {
	        if (s > 0) {
	            ans -= 2;
	            s--;
	        }
	    }
	}
	cout << ans;
	return 0;
}


Measured memory_bytes: 384
a = gets.split.map(&:to_i)
(0..a.size - 2).reverse_each { |i| 
  (0..i).each { |j| 
    a[j], a[j+1] = a[j+1], a[j] if a[j] < a[j+1]
  }
}
puts a.join(' ')

Measured memory_bytes: 5224
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
#define INF 100000000
#define rep(i,a,b) for(int (i)=a;(i)<(int)(b);(i)++)

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

    cout <<2*N*M_PI  << endl;
}

Measured memory_bytes: 3788
n, c, k, *T = map(int, open(0).read().split())
T.sort()
ans = s = 0
while s < n:
    t = T[s] + k
    X = [T[s]]
    for i in range(s+1, min(n, s+c)):
        if T[i] <= t:
            X.append(T[i])
        else:
            break
    s += len(X)
    ans += 1
print(ans)

Measured memory_bytes: 20032
#gcdは3.4.3だとfractionsにある
from fractions import gcd

from math import factorial as f

from math import ceil,floor,sqrt

from sys import exit

from copy import deepcopy
import numpy as np

import bisect
import re


ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))



def main():
    n = ii()
    b = li()
    b = [100000000]+b+[100000000]
    ans = 0

    for i in range(n):
        ans+=min(b[i],b[i+1])

    print(ans)

main()


Measured memory_bytes: 22824
#include <bits/stdc++.h>
#define rep2(i,m,n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using ll = long long;
template <typename T>
T sq(T x){
  return x * x;
}

signed main (){
string S1,S2,S3;
cin >> S1;
cin >> S2;
cin >> S3;


char next = S1.at(0);
S1.erase(0,1);
// cout << next << S1.at(0) <<endl;
for(int i = 0; i < 300;i++){

// cout  << "next :" << next << endl;
  if(next == 'a'){
if(S1.empty()){
  cout << "A" <<endl;
return 0;
}
next = S1.at(0);
S1.erase(0,1);
}else if(next == 'b'){
if(S2.empty()){
  cout << "B" <<endl;
return 0;
}
next = S2.at(0);
S2.erase(0,1);
}else if(next == 'c'){
if(S3.empty()){
  cout << "C" <<endl;
return 0;
}
next = S3.at(0);
S3.erase(0,1);
}
 
}

return 0;
}


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

using namespace std;


struct Node
{
  int key, lazy;
  Node *l, *r;
} *root = nullptr;

Node pool[2000001];

template< typename T >
struct SkewHeap
{
  bool rev;

  int ptr;

  SkewHeap(bool rev = false) : rev(rev) {}

  Node *propagate(Node *t)
  {
    if(t->lazy != 0) {
      if(t->l) t->l->lazy += t->lazy;
      if(t->r) t->r->lazy += t->lazy;
      t->key += t->lazy;
      t->lazy = 0;
    }
    return t;
  }

  Node *merge(Node *x, Node *y)
  {
    if(!x || !y) return x ? x : y;
    propagate(x), propagate(y);
    if((x->key > y->key) ^ rev) swap(x, y);
    x->r = merge(y, x->r);
    swap(x->l, x->r);
    return x;
  }

  Node *push(const T &key)
  {
    pool[ptr].key = key;
    root = merge(root, &pool[ptr++]);
  }

  T top()
  {
    return propagate(root)->key;
  }

  void pop()
  {
    propagate(root);
    auto *temp = root;
    root = merge(root->l, root->r);
    //  delete temp;
  }

  bool empty() const
  {
    return !root;
  }

  void add(const T &lazy)
  {
    if(root) {
      root->lazy += lazy;
      propagate(root);
    }
  }

};

int main()
{
  char buff[100];
  int key;

  SkewHeap< int > que(true);
  for(;;) {
    scanf("%s", buff);
    if(buff[0] == 'e' && buff[1] == 'n') break;
    if(buff[0] == 'i') {
      scanf("%d", &key);
      que.push(key);
    } else {
      printf("%d\n", que.top());
      que.pop();
    }
  }
}


Measured memory_bytes: 38368
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long MOD = 1e9 + 7;
const long long INF = 1LL << 60;
using namespace std;

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

int main() {
  char c;
  cin >> c;
  cout << char(c + 1) << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i,x,y)  for(int i=x;i<y;++i)
#define For(i,x,y)  for(int i=x;i<=y;++i)
#define FOr(i,x,y)  for(int i=x;i>=y;--i)
#define pi acos(-1)
#define mk make_pair<ll,ll>
#define pa pair<ll,ll>
#define lf else if
#define max(x,y)    ((x)<(y)?(y):(x))
#define min(x,y)    ((x)<(y)?(x):(y))
#define sqr(x)      ((x)*(x))
#define abs(x)      ((x)>0?(x):-(x))
#define Mul(x,y)    (x=1LL*(x)*(y)%mod)
#define Add(x,y)    (x=(x+(y))%mod)
#define Max(x,y)    ((x)=((x)<(y)?(y):(x)))
#define Min(x,y)    ((x)=((x)>(y)?(y):(x)))
#define E(x)        return writeln(x),0
#define p(x)        printf("~%lld~\n",x)
#define pp(x,y)     printf("~~%lld %lld~~\n",x,y)
#define ppp(x,y,z)  printf("~~~%lld %lld %lld~~~\n",x,y,z)
#define pppp(a,b,c,d)	printf("~~~%d %d %d %d\n",a,b,c,d)
#define f_in(x)     freopen(x".in","r",stdin)
#define f_out(x)    freopen(x".out","w",stdout)
#define open(x)     f_in(x),f_out(x)
#define fi first
#define se second
typedef complex<double> E;
namespace SHENZHEBEI{
#ifdef LOCAL
    struct _{_(){freopen("cf.in","r",stdin);}}_;
#endif
#define NEG 1
    inline char gc(){   return getchar();   }
#if NEG
    inline ll read(){    ll x=0,f=1; char ch=gc();   for (;!isdigit(ch);ch=gc()) if (ch=='-') f=-1;  for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x*f; }
    inline void write(ll x){    if (x<0) putchar('-'),x=-x;  if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#else
    inline ll read(){    ll x=0; char ch=gc();   for (;!isdigit(ch);ch=gc());    for (;isdigit(ch);ch=gc())  x=x*10-48+ch;   return x;   }
    inline void write(ll x){    if (x>=10)   write(x/10);    putchar(x%10+'0');  }
#endif
    inline char readchar(){ char ch=gc();   for(;isspace(ch);ch=gc());  return ch;  }
    inline ll readstr(char *s){ char ch=gc();   int cur=0;  for(;isspace(ch);ch=gc());      for(;!isspace(ch);ch=gc())  s[cur++]=ch;    s[cur]='\0';    return cur; }
    inline void writeln(ll x){  write(x);   puts("");   }        
    inline ld getreal(){    static ld lbc;  scanf("%lf",&lbc);  return lbc; }
}using namespace SHENZHEBEI;
ll n,ans;
ll solve(ll a,ll b){return a%b?solve(a*(b-1)/b,b):a/b;}
ll Dec(ll a,ll b){return a-a*(b-1)/b;}
ll Solve(ll a,ll b){
	for(;a%b;){
		ll l=1,r=1e9,ans=0,dec=Dec(a,b);
		for(;l<=r;){
			ll mid=(l+r)>>1;
			if (a-mid*dec>=0&&Dec(a-mid*dec,b)==dec)ans=mid,l=mid+1;
			else	r=mid-1;
		}a-=dec*(ans+1);
	}return a/b;
}
int main(){
	n=read();
	For(i,1,n){ll x=read(),y=read();ans^=Solve(x,y);}
	puts(ans?"Takahashi":"Aoki");
}

Measured memory_bytes: 256
#include <stdio.h>
char n[]="AKIHABARA";
int main()
{
    char s[100];
    int si=0,ni=0;
    scanf("%s",s);
    while(s[si]&&n[ni])
    {
        if(s[si]==n[ni])
        {
            si++;
            ni++;
        }
        else
        {
            if(n[ni]=='A')
            {
                ni++;
            }
            else
            {
                break;
            }
        }
    }
    if(n[ni]=='A')
    {
        ni++;
    }
    if(!s[si]&&!n[ni])
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()

using namespace std;
using ll = long long int;
const int inf = 1001001000;
const long long int Inf = 1001001001001001000;

void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

pair<int,int> f(int a){
	pair<int, int> t;
	t.second = a % 10;
	while(a){
		t.first = a;
		a /= 10;
	}
	return t;
}

int main()
{
	int n;
	cin >> n;
	map<pair<int, int>, int> mp;
	rep(i,n){
		pair<int, int> m = f(i + 1);
		mp[m]++;
	}
	long long int ans = 0;
	rep(i, n)
	{
		pair<int, int> m = f(i + 1);
		ans += mp[pair<int,int>(m.second,m.first)];
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
#define MOD (1000000007ll)
#define l_ength size
#define EPS (1e-10)

ll g[44444];

int main(void){
	ll i,j,n,t[123],v[123],ans=0ll;
	cin >> n;
	t[0] = 0ll;
	for(i=1ll; i<=n; ++i){
		cin >> t[i];
		t[i] *= 2;
		t[i] += t[i-1];
	}
	for(i=0ll; i<n; ++i){
		cin >> v[i];
		v[i] *= 2;
	}
	for(i=0ll; i<=t[n]; ++i){
		g[i] = min(i,t[n]-i);
		for(j=0ll; j<n; ++j){
			if(i <= t[j]){
				g[i] = min(g[i],v[j]+t[j]-i);
			}else if(t[j+1] <= i){
				g[i] = min(g[i],v[j]+i-t[j+1]);
			}else{
				g[i] = min(g[i],v[j]);
			}
		}
	}
	for(i=0ll; i<t[n]; ++i){
		ans += g[i]+g[i+1];
	}
	printf("%.5lf\n",((double)(ans)/8));
	return 0;
}


Measured memory_bytes: 512
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        int ans;
        if (N < 10) ans = N;
        else if (10 <= N && N < 100) ans = 9;
        else if (100 <= N && N < 1000) ans = 9 + (N - 99);
        else if (1000 <= N && N < 10000) ans = 909;
        else if (10000 <= N && N < 100000) ans = 909 + (N - 9999);
        else ans = 90909;
        System.out.println(ans);
    }
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
//cout << setprecision(20) << 

int main() {
    ll N; cin >> N;
    map<ll, ll> L;
    map<ll, ll> R;
    ll ans = 0;
    for (int i = 1; i <= N; ++i) {
        ll A; cin >> A;
        if (!L.count(i + A)) L[i + A] = 0;
        L[i + A]++;
        if (!R.count(i - A)) R[i - A] = 0;
        R[i - A]++;
    }

    for (auto i: L) {
        ans += R[i.first] * i.second;
    }

    cout << ans << endl;
}

Measured memory_bytes: 37920
#include<bits/stdc++.h>
#define int long long
using namespace std;


signed main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, k;
	cin >> n >> k;
	for (int i = k - n + 1; i <= k + n - 1; i++)cout << i << " ";
	return 0;
}

Measured memory_bytes: 256
#include <string.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <istream>
#define INF 2147483647
#define LLINF 9223372036854775807
typedef long long ll;
using namespace std;
int main(){
	string s;
	cin>>s;
	vector<int> v;
	for(int i=0;i<s.size();i++){
		v.push_back((int)(s[i]-'0'));
	}
	ll cnt=0;
	int last=0;
	for(int i=v.size()-1;i>=0;i--){
		v[i]+=last;
		last=v[i]/10;
		v[i]%=10;
		int sum=0;
		for(int j=i-1;j>=0;j--){
			if(v[j]<5)break;
			else sum++;
			if(sum>=6-v[i])break;
		}
		if(v[i]<5||(sum<6-v[i])||(v[i]==5&&i==0))cnt+=v[i];
		else cnt+=10-v[i],last++;
	}
	cnt+=last;
	cout<<cnt<<endl;
	return 0;
}

Measured memory_bytes: 7420
n = int(input())
s = 'abcdefghij'


def dfs(ans, ref):
    if len(ans) == n:
        print(ans)
    else:
        l = [s[i] for i in range(ref + 1)]
        for x in l:
            if x == l[ref]:
                dfs(ans + x, ref + 1)
            else:
                dfs(ans + x, ref)


dfs('', 0)


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

using namespace std;
#define F first
#define S second
typedef long long lli;
const int MX = 100005 , infi =1000000 , mod = 1000000007;
typedef pair<int,int> pii;

lli n,m , vals[MX][2] , dp[102][MX];

lli super(int i , lli rem)
{
    if(rem<0)return -1e11;
    if(i>=n)return 0;
    lli &ret = dp[i][rem];
    if(ret!=-1)return ret;
    ret = max( super(i+1,rem),super(i+1,rem-vals[i][0])+vals[i][1] );
    ret = max(ret,0ll);
    return ret;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=0;i<102;i++)
        for(int j=0;j<MX;j++)
            dp[i][j]=dp[i][j]=-1;
    for(int i=0;i<n;i++)
        cin>>vals[i][0]>>vals[i][1];
    cout<<super(0,m);
    return 0;
}


Measured memory_bytes: 80128
def is_prime?(num)
  if ! num.kind_of?(Integer) || num <= 1
    raise
  elsif [2, 3, 5, 7].include?(num)
    true
  elsif num.even?
    false
  else
    sqrt = (num**0.5).ceil
    flag = true
    3.step(sqrt, 2).each do |d|
      if num % d == 0
        flag = false
        break
      end
    end
    flag
  end
end

nums = readlines.map(&:to_i)
n = nums.shift
count = 0
nums.each do |num|
  count += 1 if is_prime?(num)
end
puts count



Measured memory_bytes: 7284
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
const ll mo = 1e10 +7;
const int M = 100+50;
const ll mod = 998244353;
bool cmp(int a,int b){
	return a>b;
}
int main() {
	int A,B,K;
	scanf("%d %d %d",&A,&B,&K);
	int mark[N],i,j=0;
	for(i=1;i<=min(A,B);i++){
		if(A%i==0 && B%i==0)
			mark[j++]=i;
	}
	sort(mark,mark+j,cmp);
	printf("%d\n",mark[K-1]);
    return 0;
}

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

#define int long long

using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int k, a, b;
  cin >> k >> a >> b;
  int res = k + 1;
  if (a < b && k > a) {
    int t = k - (a - 1);
    int m = t / 2;
    int c = a + m * (b - a) + (t % 2 == 1);
    res = max(res, c);
  }
  cout << res << "\n";
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>  
#include<set>
#include<iterator>
#include<list>
#include<utility>
#include<cstdlib>
#include<queue>
#include<stack>
#include<bitset>
//#include<bits/stdc++.h>

#define PI  2*acos(0.0)
#define pb push_back


using namespace std;
using ll = long long;
using lli = long long int;
using ld = long double;


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



string s;
cin>>s;
if(s[s.size() - 1]=='s')
{
	cout<<s<<"es"<<"\n";
}

else
{
	cout<<s<<"s"<<"\n";
}










	
	
	return 0;
	
	
}

Measured memory_bytes: 3620
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<ctime>

using namespace std;
using ll = long long;

int f_dir[2][4] = {{-1, 0, 0, 1}, {0, -1, 1, 0}};
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w){
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  ll h, w; cin >> h >> w;
  if(h == 1){
    cout << 1 << endl;
    return 0;
  }
  if(w == 1){
    cout << 1 << endl;
    return 0;
  }
  if(h%2 == 0) cout << (h/2) * w << endl;
  else if(w % 2 == 0) cout << (w/2) * h << endl;
  else cout << (h/2 + h%2) * (w/2 + w%2) + (h/2) * (w/2) << endl;
  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}


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

int main(){
	int N, M, i, j, l, ans = 0;
	scanf("%d%d", &N, &M);
	vector<int> k(M), p(M);
	vector<vector<int>> s(M, vector<int>(0));
	for(i = 0; i < M; i++){
		scanf("%d", &k[i]);
		s[i].assign(k[i], 0);
		for(j = 0; j < k[i]; j++){
			scanf("%d", &s[i][j]);
			s[i][j]--;
		}
	}
	for(i = 0; i < M; i++){
		scanf("%d", &p[i]);
	}
	for(i = 0; i < (1 << N); i++){
		int f = 1;
		for(j = 0; j < M; j++){
			int sum = 0;
			for(l = 0; l < k[j]; l++){
				if((i | (1 << s[j][l])) == i) sum++;
			}
			if(sum % 2 != p[j]) f = 0;
		}
		ans += f;
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <limits>
#include <cmath>
#include <vector>
#include <algorithm>
int main(){
    int n;
    std::cin>>n;
    std::vector<int> x(n);
    for(int i = 0; i < n; i++)std::cin>>x[i];
    std::sort(x.begin(), x.end());
    long min = -1;
    long cur = 0;
    for(int i = 1; i < 100; i++){
        for(int j = 0; j < x.size(); j++){
            cur += std::pow(x[j] - i, 2);
        }
        if(min == -1)min = cur;
        if(cur < min)
            min = cur;
        cur = 0;
    }
    std::cout << min;
    return 0;
}

Measured memory_bytes: 256
import java.util.*; 

class Main{
public static void main(String[] args){
	Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int ans=0;
		
  		int[] s=new int[n];
		for(int c=0;c<n;c++){
    		s[c]=sc.nextInt();
          	ans+=s[c];
    	}
  		Arrays.sort(s);
  
  		if(ans%10==0){
  		
  		for(int d=0;d<n;d++){
        if((ans-s[d])%10!=0){
        	ans-=s[d];
            break;
        }
        }
        }
  		if(ans%10==0){
        ans=0;
        }
    	System.out.println(ans);
    }
}


Measured memory_bytes: 35940
#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define ld long double
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pi 3.14159265358979323846
#define mod 1000000007
int main()
{ 
    ll p;
    p=1;
    while(p--)
    {
        ll a,b,c,d,e;
        string A;
        cin>>A;
        if(A=="RRR") cout<<3<<endl;
        else if(A=="RRS" || A=="SRR") cout<<2<<endl;
        else if(A=="SSS") cout<<0<<endl;
        else cout<<1<<endl;
    }
}

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

int main(){
   string s;
   int q,k,u;
   char c;
   cin>>s>>q;
   int flag=0;
   while(q--){
    cin>>u;
    if(u==1){
        flag++;
    }
    else{
        cin>>k>>c;
        if(flag%2==0){
            if(k==1) s.insert(s.begin(),c);
            else s.insert(s.end(),c);
        }
        else{
            if(k==1) s.insert(s.end(),c);
            else s.insert(s.begin(),c);
        }
      }
   }
   if(flag%2==1) reverse(s.begin(),s.end());
   cout<<s;
   return 0;
}


Measured memory_bytes: 1028
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 222
inline int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    return x*f;
}
int n,mp[N][N],ans,num[N],d[N];
int col[N],vis[N];
char s[N];
queue<int> q;
bool Color(int u){
	for(int i=1;i<=n;i++){
		if(mp[u][i]){
			if(col[i]!=2){
				if(col[u]==col[i])return false;
			}
			else{
				col[i]=(col[u]^1);
				bool tmp=Color(i);
				if(!tmp)return false;
			}
		}
	}
	return true;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=n;j++){
			mp[i][j]=s[j]-'0';
			if(mp[i][j])d[i]++,d[j]++;
		}
		col[i]=2;
	}
	col[1]=0;
	if(!Color(1)){
		cout<<-1<<endl;
		return 0;
	}
	for(int p=1;p<=n;p++){
		q.push(p);
		memset(num,0,sizeof(num));
		num[p]=1;
		int tmp=0;
		while(!q.empty()){
			int u=q.front();
			q.pop();
			for(int i=1;i<=n;i++){
				if(!mp[u][i])continue;
				if(num[i])continue;
				num[i]=num[u]+1;
				tmp=max(tmp,num[i]);
				q.push(i);
			}
		}
		ans=max(ans,tmp);
	}
	cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int p[110],memo[1100];
main()
{
    int a,b,c;
    cin >> a >> b >> c;
    for( int i = 0; i < a; i++ )cin >> p[i];
    int ans = 0;
    for( int i = 0; i < c; i++ )
    {
       for( int j = 0; j < a; j++ )
       {
           if( memo[( i + 1 ) * p[j] % b ] == 0 ) ans++;
           else memo[(i+1) * p[j] % b]--;
       } 
       if( i ) 
       {
           for( int j = 0; j < a; j++ )memo[( i + 1 ) * p[j] % b]++;
       }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3120
//39
#include<iostream>
#include<string>

using namespace std;

int main(){
  int n;
  cin>>n;
  while(n--){
    string s;
    cin>>s;
    int i;
    for(i=0;s.size()>1;i++){
      s[1]+=s[0]-'0';
      if(s[1]>'9'){
	s[0]='1';
	s[1]-=10;
      }else{
	s.erase(s.begin());
      }
    }
    cout<<((i%2)?"Fabre wins.":"Audrey wins.")<<endl;
  }
  return 0;
}

Measured memory_bytes: 1220
#include<iostream>
using namespace std;
int main(){
  int N,A[10000],count=0,save=0;
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>A[i];
    if(A[i]==1){
      count++;
      if(i==N-1){
        if(save<count){
          save=count;
          count=0;
        }
      }
      
    }
    else if(A[i]==0){
      if(save<count){
        save=count;
        count=0;
      }else
        count=0;
    }
  }
  cout<<save+1<<endl; 
}



Measured memory_bytes: 3104
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    m=int(input())
    dc=[list(map(int, input().split())) for _ in range(m)]
    k=s=0
    for d,c in dc:
        k+=c
        s+=d*c
    print((9*k+s-10)//9)

main()

Measured memory_bytes: 45388
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#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() {
  string s;
  cin >> s;
  int ans = 0;
  if (s == "RRR") {
    ans = 3;
  } else if (s == "RRS") {
    ans = 2;
  } else if (s == "SRR") {
    ans = 2;
  } else if (s == "SSS") {
    ans = 0;
  } else ans = 1;

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

Measured memory_bytes: 3596
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
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; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
ll solve(int n, int p, string s) {
	map<int, ll> mp;
	mp[0]++;
	ll mul = 1;
	V mod(n + 1, 0);
	rep(i, n) {
		int num = (s[i] - '0') * mul % p;
		mod[i + 1] = (mod[i] + num) % p;
		mp[mod[i + 1]]++;
		mul = mul * 10 % p;
	}
	ll ans = 0;
	for (auto c : mp) {
		ans += c.second * (c.second - 1) / 2;
	}
	return ans;
}
ll solve2(int n, int p, string s) {
	ll ans = 0;
	rep(i, n) {
		int digit = s[i] - '0';
		if (digit % p == 0) ans += n - i;
	}
	return ans;
}
int main() {
	int n, p;
	cin >> n >> p;
	string s;
	cin >> s;
	reverse(all(s));
	if (p == 2 || p == 5) cout << solve2(n, p, s) << endl;
	else cout << solve(n, p, s) << endl;
	return 0;
}

Measured memory_bytes: 2052
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;

LL dir,pos[200005];
LL n,l,t,rnk = 0;

int main(){
	ios::sync_with_stdio(false);
	cin >> n >> l >> t;
	for(LL i = 1;i <= n;i ++){
		cin >> pos[i] >> dir;
		if(dir == 1){
			pos[i] += t;
			rnk += pos[i] / l;
		}
		else{
			pos[i] -= t;
			rnk += (pos[i] >= 0 ? 0 : (pos[i] + 1 - l) / l);
		}
		pos[i] = (pos[i] % l + l) % l;
		rnk = (rnk % n + n) % n;
	}
	sort(pos + 1,pos + 1 + n);
	for(LL i = rnk + 1;i <= n;i ++) cout << pos[i] << endl;
	for(LL i = 1;i <= rnk;i ++) cout << pos[i] << endl;
    return 0;
}
//

Measured memory_bytes: 2048
#include <cstdio>
using namespace std;
bool d[33554432];
char k[128];

int c(char str[12]){
	int ans = str[0], i = 1;
	for(i;str[i] != '\0';i++){ans <<= 2;ans += k[str[i]];}
	return ans;
}

int main(){
	k['A'] = 0;k['C'] = 1;k['G'] = 2;k['T'] = 3;
	int n;scanf("%d", &n);
	char s[7], str[12];
	while(n--){
		scanf("%s %s", s, str);
		if(s[0] == 'i')d[c(str)] = true;
		else puts(d[c(str)] ? "yes" : "no");
	}
	return 0;
}

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

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dc[] = {1, 2, 3, 4};

const int MaxN = 2e3 + 23;

int a[MaxN][MaxN];
int dp[MaxN][MaxN][5];

int n, m, q;

void dfs(int x, int y, int c = 0)
{
    if(!a[x][y])
        return;
    a[x][y] = 0;
    ++dp[x][y][c];
    for(int i = 0; i < 4; ++i)
        dfs(x + dx[i], y + dy[i], dc[i]);
}

void precalc()
{
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            if(a[i][j])
                dfs(i, j);

    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            for(int k = 0; k <= 4; ++k)
                dp[i][j][k] += dp[i - 1][j][k] + dp[i][j - 1][k] - dp[i - 1][j - 1][k];
}

int get(int x, int y, int xx, int yy, int k)
{
    if(y > yy || x > xx)
        return 0;
    return dp[xx][yy][k] - dp[x - 1][yy][k] - dp[xx][y - 1][k] + dp[x - 1][y - 1][k];
}

int solve(int x, int y, int xx, int yy)
{
    return get(x, y, xx, yy, 0) + get(x, y, x, yy, 4) + get(xx, y, xx, yy, 1) + get(x, y, xx, y, 3) + get(x, yy, xx, yy, 2);
}

int main()
{
    ios_base :: sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> q;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
        {
            char q;
            cin >> q;
            a[i][j] = q - '0';
        }
    precalc();
    while(q --> 0)
    {
        int x, y, xx, yy;
        cin >> x >> y >> xx >> yy;
        cout << solve(x, y, xx, yy) << '\n';
    }
    return 0;
}


Measured memory_bytes: 97792
#include<stdio.h>
#include<stdlib.h>
#define WHITE 0
#define GRAY  1
#define BLACK 2

int n,M[100][100];
int color[100];
int time;
int d[100],f[100];

void visit(int u){
  int v;
  color[u] = GRAY;
  d[u] = ++time;
  for(v=0;v<n;v++){
    if(M[u][v] == 0) continue;
    if(color[v] == WHITE) visit(v);
  }
  color[u] = BLACK;
  f[u] = ++time;
}

void dfs(){
  int u;

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

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

  scanf("%d",&n);
  for(i=0;i<n;i++)
    for(j=0;j<n;j++)
      M[i][j]=0;
	
  for(i=0;i<n;i++){
    scanf("%d%d",&u,&k);
    for(j=0;j<k;j++){
      scanf("%d",&v);
      M[u-1][v-1] = 1;
    }
  }
  
  dfs();
  for(i=0;i<n;i++) printf("%d %d %d\n",i+1,d[i],f[i]);
  
  return 0;
}


Measured memory_bytes: 2112
L,R = map(int,input().split())
ans = 2019

for i in range(L,R+1):
    for j in range(L,R+1):
        if i == j:
            pass
        else:
            x = (i*j)%2019
            if x < ans:
                ans = x
            
            if x == 0:
                print (x)
                exit ()
print (ans)


Measured memory_bytes: 2940
#pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
typedef  unsigned long long int ull;
typedef pair<ll,ll> P;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int h,w;
bool usedb[500][500];
bool visited[500][500];
V<int> dx={-1,0,1,0},dy={0,-1,0,1};
V<string> s;
ll dfs(int x,int y,ll &b){
    ll cnt=0;
    if(s[x][y]=='.')cnt++;
    if(s[x][y]=='#'){b++;usedb[x][y]=false;}
   for(int i=0;i<4;i++){
       int nx=dx[i]+x,ny=dy[i]+y;
       if(nx<0||nx>=h||ny<0||ny>=w)continue;
       if(visited[nx][ny])continue;
       if(s[x][y]==s[nx][ny])continue;
       visited[nx][ny]=true;
       cnt+=dfs(nx,ny,b);
   }
   return cnt;
}
int main(){
cin>>h>>w;
s.resize(h);
for(int i=0;i<h;i++)cin>>s[i];
for(int i=0;i<h;i++){
    for(int j=0;j<w;j++)if(s[i][j]=='#')usedb[i][j]=true;
}
ll ans=0;
for(int i=0;i<h;i++){
    for(int j=0;j<w;j++)if(usedb[i][j]){
        visited[i][j]=true;
        usedb[i][j]=false;
        ll cnt=0;
        ll times=dfs(i,j,cnt);
        ans+=times*cnt;
    }
}
cout<<ans<<"\n";
}



Measured memory_bytes: 20736
// code by Juan Torres
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		boolean coffee = s.charAt(2) == s.charAt(3) && s.charAt(4) == s.charAt(5);
		System.out.println(coffee ? "Yes" : "No");
	}
}


Measured memory_bytes: 23764
#include <stdio.h>

#define MAX 100

int main(void){
  int n,x;
  int sum_c=0;
  int i;
  int p,q,r;
  int ary[MAX];

  for(i=0;i<MAX;i++){
    ary[i]=i+1;
  }
  
  while(1){
    sum_c=0;
    scanf("%d",&n);
    scanf("%d",&x);

    if(n==0&&x==0){
      break;
    }

    for(p=1;p<=n-2;p++){
      for(q=p+1;q<=n-1;q++){
        for(r=q+1;r<=n;r++){
          if((p+q+r)==x){
            sum_c++;
          }
        }
      }
    }
    printf("%d\n",sum_c);
  }
  return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
using namespace std;
#define loop(i,b,e) for(int i=b;i<=e;i++)
#define loop2(i,e,b) for(int i=e;i>=b;i--)
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6 + 5;
const ll mod = 1e9 + 7;
const int oo = 1e9;

int a, b;

int main() {
	scanf("%d%d",&a,&b);
	int ans = a - 1;
	if (b >= a) ans++;
	printf("%d",ans);
}

Measured memory_bytes: 256
// C - Numbers on a Circle
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main(int argc, char* argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  set<pair<int, int>, greater<pair<int, int>>> s;
  for (int i = 0; i < n; i++)
    s.insert(make_pair(b[i], i));

  long long ans = 0;
  while (!s.empty()) {
    int i = s.begin()->second;
    s.erase(s.begin());
    int d = b[i] - a[i], t = b[(i + 1) % n] + b[(i - 1 + n) % n];
    if (d != 0 && d < t) {
      ans = -1;
      break;
    }
    ans += d / t;
    b[i] -= d / t * t;
    if (b[i] != a[i])
      s.insert(make_pair(b[i], i));
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 11264
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int inputValue(){
    int a;
    cin >> a;
    return a;
};

void inputArray(int * p, int a){
    REP(i, a){
        cin >> p[i];
    }
};

void inputVector(vector<int> * p, int a){
    REP(i, a){
        int input;
        cin >> input;
        p -> push_back(input);
    }
}

int main()
{
  int N = inputValue();
  int A[100];
  int B[100];
  inputArray(B, N-1);

  A[0] = B[0];
  A[N-1] = B[N-2];
  FOR(i, 1, N-1){
    A[i] = min(B[i-1],B[i]);
  }
  int ans = 0;
  REP(i, N){
      ans += A[i];
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
package main

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

type pair struct {
	l, r int
}

const max = 100000

func main() {
	q := getInt()
	ps := make([]pair, q)
	for i := range ps {
		ps[i].l = getInt() - 1
		ps[i].r = getInt()
	}

	primes := furui(max)
	like2017s := make([]int, max+1)
	for i, p := range primes {
		if p == 0 {
			continue
		}

		if f := (i + 1) / 2; primes[f] == 1 {
			like2017s[i] = 1
		}
	}

	s := make([]int, max+1)
	for i := 1; i <= max; i++ {
		s[i] = s[i-1] + like2017s[i]
	}

	for _, p := range ps {
		res := s[p.r] - s[p.l]
		fmt.Println(res)
	}
}

func furui(n int) []int {
	ns := make([]int, n+1)
	for i := range ns {
		ns[i] = 1
	}
	ns[0], ns[1] = 0, 0

	for i := 2; i*i <= n; i++ {
		if ns[i] == 1 {
			for j := i * 2; j <= n; j += i {
				ns[j] = 0
			}
		}
	}
	return ns
}

// -----------------------------------------

var sc = bufio.NewScanner(os.Stdin)

func init() {
	// buf := 200001
	// sc.Buffer(make([]byte, buf), buf)
	sc.Split(bufio.ScanWords)
}

func getInt() int {
	i, err := strconv.Atoi(getString())
	if err != nil {
		panic(err)
	}
	return i
}

func getString() string {
	sc.Scan()
	s := sc.Text()
	return s
}


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

using namespace std;

int main(){
    long H;
    cin >> H;
    long i = 1;
    long result = 0;
    while(H>0){
        H /= 2;
        result += i;
        i *= 2;
    }
    cout << result << endl;

}

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

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

int MOD=1e9+7;
ll INF=1e18;

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

int main(){
    string base; cin>>base;
    int basen=base.size();
    int n; cin>>n;
    int ans=0;
    for(int i=0; i<n; i++){
        string s; cin>>s;
        s+=s;
        //cout << s << endl;
        
        int sn=s.size();
        for(int j=0; j<=sn-basen; j++){
            int sum=0;
            for(int k=0; k<basen; k++){
                if(base[k] == s[k+j]) sum++;
            }
            if(sum == basen) {
                ans++;
                break;
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 3216
<?php
fscanf(STDIN, "%d%d", $N, $Q);
$map = array_fill(1, $N, 0);
for($i = 0; $i < $Q; $i++){
    fscanf(STDIN, "%d%d", $a, $b);
    $map[$a]++;
    $map[$b]++;
}
$flag = true;
foreach($map as $key => $count){
    if($count % 2 == 1)$flag = false;
}
echo $flag ? "YES": "NO","\n";

Measured memory_bytes: 8824
function Main(input) {
    if(input.split("").indexOf("7") >= 0) {
      console.log("Yes");
    } else {
      console.log("No");
    }
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define MOD 1000000007

typedef long long int ll;

const ll INF=(ll)1e18;

int main(){
  string a,b;
  cin >> a >> b;

  if(a==b){
    cout << "EQUAL" << endl;
    return 0;
  }

  if(a.size() == b.size()){
    REP(i,a.size()){
      if(a[i]>b[i]){
        cout << "GREATER" << endl;
        return 0;
      }else if(a[i]<b[i]){
        cout << "LESS" << endl;
        return 0;
      }
    }

  }else{
    if(a.size()>b.size()){
      cout << "GREATER" << endl;
    }else{
      cout << "LESS" << endl;
    }
  }
}



Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <iostream>
#include <math.h>

using namespace std;

int sosuu(int n);

int main(){
    int n=0;
    int num=0;
    int a[30][2]={0};
    int max=0;
    while(scanf("%d",&n)!=EOF){
        if(max<n){
            max=n;
        }
        a[num][0]=n;
        num++;
    }
    
    int b[max];
    int c[max];
    int so=0;
    for(int i=0; i<=max; i++){
        b[i]=i;
    }
    b[1]=0;
    
    for(int i=2; i<sqrt(max); i++){
        if(b[i]!=0){
            for(int j=i+1; j<=max; j++){
                if(j%i==0){
                    b[j]=0;
                }
        }
        }
    }
    
    for(int i=0; i<=max; i++){
        if(b[i]!=0){
            c[so]=i;
            so++;
        }
    }
    
    
//    for(int j=0; j<num; j++){
//        cout<<a[j][0]<<"\n";
//    }
//    
//    for(int j=0; j<=so; j++){
//        cout<<c[j]<<"\n";
//    }
    
    
    
    
    for(int i=0; i<num; i++){
        for(int j=0; j<so; j++){
            if(c[j]<=a[i][0]){
                a[i][1]++;
            }else{
                j=so;
            }
        }
    }

    
    for(int j=0; j<num; j++){
        cout<<a[j][1]<<"\n";
    }
    
}

Measured memory_bytes: 5416
#include <iostream>
#include <queue>
#define pii pair<int,int>
#define ppi pair<pii,int>
#define fst first
#define snd second
const int dr[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
using namespace std;
queue<ppi> q;int n,m,mx=0;string mp[1001]={};bool bt[1001][1001]={};
bool isv(pii dt) {return dt.fst>=0&&dt.fst<n&&dt.snd>=0&&dt.snd<m;}
int main()
{
	cin>>n>>m;
	for (int i=0;i<n;i++) 
	{
		cin>>mp[i];
		for (int j=0;j<m;j++)
		{
			if (mp[i][j]=='#') {q.push({{i,j},0});}
		}
	}
	while (!q.empty())
	{
		ppi dt = q.front();q.pop();
		if (!bt[dt.fst.fst][dt.fst.snd])
		{
			bt[dt.fst.fst][dt.fst.snd] = 1;
			mx = dt.snd;
			for (int i=0;i<4;i++)
			{
				if (isv({dt.fst.fst+dr[i][0],dt.fst.snd+dr[i][1]})) {q.push({{dt.fst.fst+dr[i][0],dt.fst.snd+dr[i][1]},dt.snd+1});}
			}
		}
	}
	cout<<mx<<"\n";
	return 0;
}

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

typedef long long ll;

int N;
char S[200020] = {0};
int sum[100] = {0};
ll ans = 1;

int cha(char c)
{
    return c - 'a';
}

int main()
{
    scanf("%s",S);
    N = strlen(S);
    for(int i = 0; i < N; i++)sum[cha(S[i])]++;
    for(int i = 0; i < N; i++)
    {
        sum[cha(S[i])]--;
        ans += (ll)(N - i - 1 - sum[cha(S[i])]);
    }
    printf("%lld\n",ans);
    return 0;
}


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

int main(){
  int a[2];
  for(;;){
    cin >> a[0] >> a[1];
    sort(a,a+2);
    if(a[0] == 0 && a[1] == 0) break;
    cout << a[0] << " " << a[1] << endl;
  }
  return 0;
}

Measured memory_bytes: 3056
import fractions
N=int(input())
a = list(map(int, input().split()))

#------------最小公倍数---------------------
lcm = a[0]
for i in range(1, N):
    lcm = lcm * a[i] // fractions.gcd(lcm, a[i])
#------------------------------------------
ans=0
for j in a:
    ans += lcm // j
print(ans % 1000000007)

Measured memory_bytes: 6112
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int N, K, Q;
    cin >> N >> K >> Q;
    vector<int> a(Q);
    vector<int> point(N, K-Q);
    for(int i = 0; i < Q; i++){
        cin >> a[i];
    }
    for(int i = 0; i < Q; i++){
        point[a[i]-1]++;
    }
    for(int i = 0; i < N; i++){
        if(point[i] > 0)    cout << "Yes" << endl;
        else                cout << "No" << endl;
    }
}

Measured memory_bytes: 1408
S = gets.chomp

n = S.size
ans = [0] * n
r, l, j, k = nil, nil, nil, -1
(0 ... n).each do |i|
  case S[i, 2]
  when 'RL'
    j = i
    r = j - k
  when 'LR', 'L'
    k = i
    l = i - j
    ans[j] = (r - 1)/2 + 1 + l/2
    ans[j + 1] = r/2 + 1 + (l - 1)/2
  end
end
puts ans.join(' ')

Measured memory_bytes: 3196
// <head>
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    const int INF = 1002003004;
    const ll LINF = 1002003004005006007ll;
    struct preprocess{preprocess()
    {cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
// </head>

// <library>
    
// </library>

const int MX_N = 1005;
vector<vector<int>> to(MX_N);
vector<int> x(MX_N);
const int MX = 5005;
bool flag = true;

int dfs(int u){
    if(to[u].size()==0){
        return x[u];
    }
    set<int> can;
    int res=0;
    for(int v:to[u]){
        int s = dfs(v);
        if(s==-1) return -1;
        res+=s;
        if(can.empty()){
            can.emplace(x[v]);
            can.emplace(s-x[v]);
        } else {
            set<int> cana;
            for(int i:can){
                cana.emplace(min(MX,i+x[v]));
                cana.emplace(min(MX,i+s-x[v]));
            }
            can=cana;
        }
    }
    auto it = can.upper_bound(x[u]);
    if(it == can.begin()){
        flag=false;
        return -1;
    }
    res+=x[u]-*(--it);
    return res;
}
int main(){
    int n; cin >> n;
    for (int i = 1; i < n; i++){
        int p;cin >> p;
        p--;
        to[p].emplace_back(i);
    }
    for (int i = 0; i < n; i++){
        cin >> x[i];
    }
    dfs(0);
    if(flag) cout << "POSSIBLE" << '\n';
    else cout << "IMPOSSIBLE" << '\n';
    
    

}

Measured memory_bytes: 768