akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;


struct Node{
    ll key;
    Node *parent,*left,*right;
};


struct Tree{
    Node *nil,*root;

    void init(){
        root=nil;
    }

    void add(ll key){
        Node *newnode;
        Node *p;
        newnode = (Node*)malloc(sizeof(Node));

        newnode->key = key;
        newnode->parent = newnode->right = newnode->left = nil;

        Node *pa = find_parent(key);
        if(pa == nil){
            root = newnode;
        }else if(key < pa->key){
            pa->left = newnode;
        }else{
            pa->right = newnode;
        }
    }

    Node* find_parent(int key){
        Node *pa=nil;
        Node *loc=root;
        while(loc != nil){
            pa = loc;
            if(key < loc->key){
                loc = loc->left;
            }else{
                loc = loc->right;
            }
        }
        return pa;
    }

    void find_x(int x){
        Node *p=root;
        bool flg = false;
        while(p != nil){
            if(x == p->key){
                flg = true;
                break;
            }else if(x < p->key){
                p = p->left;
            }else{
                p = p->right;
            }
        }
        if(flg)cout << "yes\n";
        else cout << "no\n";
    }

    void print_in(Node *r){
        if(r==nil)return;
        print_in(r->left);
        cout << " " << r->key;
        print_in(r->right);
    }

    void print_pre(Node *r){
        if(r==nil)return;
        cout << " " << r->key;
        print_pre(r->left);
        print_pre(r->right);
    }
    void print(){
        print_in(root);
        cout << endl;
        print_pre(root);
        cout << endl;
    }
};


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

    Tree tree;
    tree.init();
    int n;
    cin >> n;
    string s;
    int k;
    for(int i=0;i<n;i++){
        cin >> s;
        if(s=="insert"){
            cin >> k;
            tree.add(k);
        }else if(s=="find"){
            cin >> k;
            tree.find_x(k);
        }else{
            tree.print();
        }
    }
    return 0;
}



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

using namespace std;

const int maxn = 210;
bool mark[maxn];
int dis[maxn], n;
vector<int> adj[maxn];

void bfs(int u)
{
        memset(mark, false, sizeof mark);
        mark[u] = true;
        dis[u] = 1;
        queue<int> q({ u });
        while (!q.empty()) {
                int v = q.front();
                q.pop();

                for (int u : adj[v])
                        if (!mark[u]) {
                                mark[u] = true;
                                dis[u] = dis[v] + 1;
                                q.push(u);
                        }
        }
}

int main()
{
        ios::sync_with_stdio(false);
        cin >> n;

        vector<pair<int, int>> edge;
        for (int i = 1; i <= n; i++) {
                string s;
                cin >> s;
                for (int j = 1; j <= n; j++) {
                        if (s[j - 1] == '1')
                                adj[i].push_back(j), adj[j].push_back(i),
                                    edge.push_back({ i, j });
                }
        }

        bfs(1);

        for (auto e : edge) {
                if (dis[e.first] == dis[e.second])
                        return cout << -1, 0;
        }

        int ans = 0;
        for (int i = 1; i <= n; i++) {
                bfs(i);
                ans = max(ans, *max_element(dis, dis + n + 1));
        }

        cout << ans;

        return 0;
}


Measured memory_bytes: 1148
<?php

fscanf(STDIN, "%d", $n);

$a_arr = explode(' ', trim(fgets(STDIN)));

$tmp_arr = array_fill(0, $n, 0);

foreach ($a_arr as $a) {
    ++$tmp_arr[$a - 1];
}

foreach ($tmp_arr as $tmp) {
    echo $tmp, PHP_EOL;
}


Measured memory_bytes: 35960
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		int N = S.length()+1;
		

		List<Integer> l2 = new ArrayList<Integer>();
		for(int i=0;i<N;i++) l2.add(-1);
		
		List<Integer> memos = new ArrayList<Integer>(); 
		if(S.charAt(0)=='<') {
			l2.set(0,0);
			memos.add(0);
		}
		for(int i=1;i<N-1;i++) {
			if(S.charAt(i-1)=='>'&&S.charAt(i)=='<') {
				l2.set(i, 0);
				memos.add(i);
			}
		}
		if(S.charAt(N-2)=='>') {
			l2.set(N-1,0);
			memos.add(N-1);
		}
		
		int size = memos.size();

		for(int tmp:memos) {
			for(int i=tmp;i>0;i--) {
				int now = l2.get(i);
				if(S.charAt(i-1)=='>') l2.set(i-1,Math.max(now+1,l2.get(i-1)));else break;
			}
			for(int i=tmp;i<N-1;i++) {
				int now = l2.get(i);
				if(S.charAt(i)=='<') l2.set(i+1,Math.max(now+1,l2.get(i+1)));else break;	
			}
		}

		long ans = 0;
		for(int tmp:l2) {
			ans += tmp;
		}
	
		System.out.println(ans);
		
	}
}


Measured memory_bytes: 49192
def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    c_lst = list(map(int, input().split()))
    ans = 0

    for a in a_lst:
        ans += b_lst[a - 1]

    for i in range(n - 1):
        if a_lst[i] + 1 == a_lst[i + 1]:
            ans += c_lst[a_lst[i] - 1]

    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 61964
import fractions
A, B, C, D = map(int, input().split())
A = A-1
f=fractions.gcd(C,D)
f2=C*D//f

Aans = A - (A//C + A//D - A//f2)
Bans = B - (B//C + B//D - B//f2)
print(Bans- Aans)

Measured memory_bytes: 5344
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <bitset>
#include <limits>
#include <algorithm>

#define MOD_BIG 1000000007
#define MOD_BIG2 998244353
using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using ll = long long;
using std::vector;

template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) {
    vec.resize(len);
    for (auto &v : vec) ndarray(v, args...);
}

//////////////////////////////////////
using P = std::pair<int, int>;
struct UDGraph {
    int size;
    vector<vector<P>> g;
    UDGraph(int N) : size(N+1), g(N+1) { }
    void add_edge(int from, int to, int weight) {
        g[from].push_back(P(to, weight));
        g[to].push_back(P(from, weight));
    }
    int bfs(int s) {
        int res = 0;
        vector<int> d(size, -1);
        d[s] = 1;
        std::queue<int> que;
        que.push(s);
        while(!que.empty()) {
            auto to = que.front();
            que.pop();
            for (auto e: g[to]) {
                if (d[e.first] != -1) {
                    if (d[e.first] % 2 != (d[to] + 1) % 2) {
                        return -1;
                    }
                    continue;
                }
                d[e.first] = d[to] + 1;
                que.push(e.first);
                
                res = std::max(res, d[e.first]);
            }
        }
        return res;
    }
};

//////////////////////////////////////

int main() {
    
    int N;
    cin >> N;
    UDGraph g(N);
    
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            char c;
            cin >> c;
            if (c == '1') {
                g.add_edge(i+1, j+1, 1);
            }
        }
    }
    
    int ans = 0;
    for (int i = 0; i < N; i++) {
        auto res = g.bfs(i+1);
        if (res == -1) {
            cout << -1 << endl;
            return 0;
        }
        ans = std::max(ans, res);
    }
    cout << ans << endl;
    
    return 0;
}


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

const int INF=1000000 ;

int main(){
    int n,m;
    cin >> n >> m;
    int c[m+1];
    for(int i=1;i<=m;i++){
        cin >> c[i];
    }
    int dp[m+1][n+1];
    for(int i=0;i<=m;i++){
        for(int j=0;j<=n;j++){
            dp[i][j]=INF;
        }
    }
    dp[0][0]=0;
    for(int i=1;i<=m;i++){
        for(int j=0;j<=n;j++){
            if(j-c[i]>=0){
                dp[i][j]=min(dp[i-1][j],dp[i][j-c[i]]+1);
            }
            else{
                dp[i][j]=dp[i-1][j];
            }
        }
    }
    cout << dp[m][n] << endl;
    return 0;
}

Measured memory_bytes: 7204
package main

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

// input scanner
var sc = bufio.NewScanner(os.Stdin)

const (
	initialBufSize = 10e4
	maxBufSize     = 10e6
)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextInt64() int64 {
	a, _ := strconv.ParseInt(next(), 10, 64)
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

// util
func max(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n > ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func min(l []int) (int, int) {
	idx := 0
	ans := l[idx]
	for i, n := range l {
		if n < ans {
			idx = i
			ans = n
		}
	}
	return idx, ans
}

func count(h int64) int64 {
	if h == 1 {
		return 1
	}
	return 1 + 2*count(h/2)
}

// main
func main() {
	buf := make([]byte, initialBufSize)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	h := nextInt64()
	fmt.Println(count(h))
}


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

typedef long long  LL;
const int N=200010,mo=924844033,g=5;
struct edge{int s,t,n;}e[2*N];
int n,h[N],a[N],siz[N],fac[N],ifac[N],A[1<<19],B[1<<19],W[1<<19];

int qpow(int a,int b)
{
	int x=a;  a=1;
	while (b)
		{
			if (b&1)  a=1LL*a*x%mo;
			x=1LL*x*x%mo,b>>=1;
		}
	return a;
}

void NTT(int *a,int n,int op)
{
	for (int i=1,j,p,t; i<n; i++)
		{
			for (j=0,p=i,t=1; t<n; j<<=1,j|=p&1,p>>=1,t<<=1);
			if (i<j)  swap(a[i],a[j]);
		}
	int *w=(op>0?W:W+n/2),u,v;
	for (int len=2; len<=n; len<<=1)
		for (int i=0; i<n; i+=len)
			for (int j=0; j<(len>>1); j++)
				{
					u=a[i+j],v=1LL*a[i+j+(len>>1)]*w[n/len*j]%mo;
					a[i+j]=(u+v)%mo,a[i+j+(len>>1)]=(u+mo-v)%mo;
				}
	if (op==-1)
		{
			int inv=qpow(n,mo-2);
			for (int i=0; i<n; i++)  a[i]=1LL*a[i]*inv%mo;
		}
}

void FFT()
{
	int N=1;
	while (N<=2*n)  N<<=1;
	W[0]=1,W[1]=qpow(g,(mo-1)/N);
	for (int i=2; i<N; i++)  W[i]=1LL*W[i-1]*W[1]%mo;
	reverse(W+N/2+1,W+N),W[N/2]=1;
	NTT(A,N,1),NTT(B,N,1);
	for (int i=0; i<N; i++)  A[i]=1LL*A[i]*B[i]%mo;
	NTT(A,N,-1);
}

void dfs(int x)
{
	siz[x]=1;
	for (int i=h[x]; i; i=e[i].n)
		if (!siz[e[i].t])  dfs(e[i].t),siz[x]+=siz[e[i].t];
	if (siz[x]!=n)  a[siz[x]]--,a[n-siz[x]]--;
}

void work()
{
	scanf("%d",&n);
	for (int i=1,u,v,tot=0; i<n; i++)
		{
			scanf("%d %d",&u,&v);
			e[++tot]=(edge){u,v,h[u]},h[u]=tot;
			e[++tot]=(edge){v,u,h[v]},h[v]=tot;
		}
	dfs(rand()%n+1),a[n]=n,fac[0]=1;
	for (int i=1; i<=n; i++)  fac[i]=1LL*fac[i-1]*i%mo;
	ifac[n]=qpow(fac[n],mo-2);
	for (int i=n; i; i--)  ifac[i-1]=1LL*ifac[i]*i%mo;
	for (int i=1; i<=n; i++)  A[i]=1LL*(a[i]+mo)*fac[i]%mo;
	for (int i=1; i<=n; i++)  B[i]=ifac[n-i];
	FFT();
	for (int i=1; i<=n; i++)
		printf("%d\n",int(1LL*A[n+i]*ifac[i]%mo));
}

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


Measured memory_bytes: 21888
n,k=map(int,input().split())
v=list(map(int,input().split()))
ans=0
for i in range(min(n+1,k+1)):
    for j in range(min(n-i+1,k-i+1)):
        a=v[:i]+v[n-j:]
        a.sort(reverse=True)
        for _ in range(k-i-j):
            if a and a[-1]<0:
                a.pop()
        ans=max(ans,sum(a))
print(ans)



Measured memory_bytes: 3064
#include <bits/stdc++.h>
#pragma GCC optimize("O999")
//示例：#define ll long long
//unsigned long long ;
const int INF=0x3f3f3f3f;
const int maxn=50000005;
using namespace std;

int main(){
	//freopen(".in", "r",stdin );
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	int k, n, maxv=0;
	cin >> k >> n;
	int a[n+1], b[n+1];
	cin >> a[1];
	for(int i=2; i<=n; i++){
		cin >> a[i];
		b[i]=a[i]-a[i-1];
	}
	b[1]=k-a[n]+a[1];
	for(int i=1; i<=n; i++)
		maxv=max(b[i],maxv);
	cout<<k-maxv<<endl;
	return 0;
}

Measured memory_bytes: 1920
#include <iostream>

using namespace std;

#define int long long

const int N = 2e5 + 10;

int32_t main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);    
    int ans = 8;
    int x;
    cin >> x;
    for (int i = 400; i < 2000; i += 200)
    {
        if (x >= i && x < i + 200)
        {
            cout << ans;
            break;
        }
        --ans;
    }
    return 0;
}

Measured memory_bytes: 3608
from collections import Counter
N = int(input())
A = list(map(int,input().split()))

e = A[0::2]
cme = Counter(e).most_common()
e1 = cme[0][0]
e1_num = cme[0][1]
e2_num = 0
if len(cme) > 1:
  e2 = cme[1][0]
  e2_num = cme[1][1]

o = A[1::2]
cmo = Counter(o).most_common()
o1 = cmo[0][0]
o1_num = cmo[0][1]
o2_num = 0
if len(cmo) > 1:
  o2 = cmo[1][0]
  o2_num = cmo[1][1]

if e1 != o1:
  print(N - e1_num - o1_num)
else:
  print(min(N - e1_num - o2_num, N - e2_num - o1_num))

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

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define aut(r,v) __typeof(v) r = (v)
#define each(it,o) for(aut(it,(o).begin()); it!=(o).end(); ++it)
#define reach(it,o) for(aut(it,(o).rbegin()); it!=(o).rend(); ++it)
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T1, typename T2> ostream& operator<<(ostream& o, const pair<T1, T2>& p)
{return o<<"("<<p.first<<", "<<p.second<<")";}
template<typename T>string join(const vector<T>&v, string del=", ")
{stringstream s;rep(i,v.size())s<<del<<v[i];return s.str().substr(del.size());}
template<typename T>ostream& operator<<(ostream& o, const vector<T>&v)
{if(v.size())o<<"["<<join(v)<<"]";return o;}
template<typename T>ostream& operator<<(ostream& o, const vector<vector<T> >&vv)
{int l=vv.size();if(l){o<<endl;rep(i,l){o<<(i==0?"[ ":",\n  ")<<vv[i]<<(i==l-1?" ]":"");}}return o;}
template<typename T>ostream& operator<<(ostream& o, const set<T>& st)
{vector<T> v(st.begin(),st.end());o<<"{ "<<join(v)<<" }";return o;}
template<typename T1, typename T2>ostream& operator<<(ostream& o, const map<T1, T2>& m)
{each(p,m){o<<(p==m.begin()?"{ ":",\n  ")<<*p<<(p==--m.end()?" }":"");}return o;}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const ll INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    int n; cin >> n;
    int x, y; cin >> x >> y;
    x--; y--;

    vvi d(n, vi(n));
    REP(i,0,n){
        REP(j,i+1,n){
            d[i][j] = j - i;
        }
    }
    int diff = y - x - 1;
    REP(i,0,n){
        REP(j,i+1,n){
            d[i][j] = min(d[i][j], abs(x - i) + 1 + abs(j - y));
        }
    }

    vi ans(n-1);
    REP(i,0,n) REP(j,i+1,n){
        ans[d[i][j]-1] += 1;
    }
    rep(i,n-1) cout << ans[i] << endl;
    return 0;
}

Measured memory_bytes: 16000
from itertools import combinations_with_replacement
N, M, Q = map(int, input().split())
lists = []
for i in range(Q):
  lists.append(list(map(int, input().split())))

l = [i+1 for i in range(M)]

ans = []

for v in combinations_with_replacement(l, N):
  count = 0
  for i in range(Q):
    a, b, c, d = lists[i]
    if v[b-1] - v[a-1] == c:
      count += d
    ans.append(count)
print(max(ans))

Measured memory_bytes: 73840
#include<bits/stdc++.h>
using namespace std;
int main()
{   
   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
	string s;
	cin >> s;
	for(int i  = 0;i<19;i++)
	{

		if(i==5||i==13)
		{
			cout <<" ";
		}
		else
		{
			cout << s[i];
		}
	}
}

Measured memory_bytes: 256
package main

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

var sc = bufio.NewScanner(os.Stdin)

//var dp [103][100010]int

func main() {
	scanInit()

	n := nextInt()
	k := nextInt()
	w := make([]int, n+2)
	v := make([]int, n+2)
	for i := 0; i < n; i++ {
		w[i] = nextInt()
		v[i] = nextInt()
	}

	dp := makemat(n+2, k+2) // 余裕+2

	for i := 1; i <= n+1; i++ {
		for j := 0; j <= k; j++ {
			if j >= w[i-1] {
				take := dp[i-1][j-w[i-1]] + v[i-1]
				notake := dp[i-1][j]
				dp[i][j] = max(take, notake)
			} else {
				dp[i][j] = dp[i-1][j]
			}
		}
	}

	fmt.Println(dp[n][k])
	//fmt.Println(dp)
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func makemat(a, b int) [][]int {
	dp := make([][]int, a)
	for i := 0; i < a; i++ {
		dp[i] = make([]int, b)
		for j := 0; j < b; j++ {
			dp[i][j] = 0
		}
	}
	return dp
}

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


Measured memory_bytes: 84592
#include<stdio.h>
int main(void){
    int N,p,b,i;
    scanf("%d",&N);
    for(i = 0; i < 11; i++){
        p = 1000*i;
        b = p - N;
        if(b >= 0){
            break;
        }
    }
    printf("%d",b);
}

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

int main()
{
    int n, x, t;
    cin >> n >> x >> t;
    int f;
    if(n%x==0){
        f = n/x;
    }
    else{
        f = n/x+1;
    }
    cout << f*t << '\n';
    return 0;
}

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

using namespace std;
#define ll long long int
#define mod 1000000007
#define vec vector<long long int>
#define fi first
#define se second
#define pb push_back
int main()
{
    string a, b;
    cin >> a >> b;
    int n = a.length();
    int m = b.length();
    int mi = 0;
    for (int i = 0; i < n - m + 1; i++)
    {
        int p = 0;

        for (int j = i; j < i + m; j++)
        {
            if (a[j] == b[j - i])
            {
                p++;
            }
        }
        mi = max(p, mi);
    }
    cout << m - mi << endl;
}

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

ll calc(ll A){
    ll cnt1 = (A+1)/2;
    ll ans = cnt1 % 2;
    if(A%2 == 0)ans ^=A;
    return ans;
}

int main(){
    ll A, B;
    cin >> A >> B;
    ll ans = calc(B) ^ calc(A-1);
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <stdio.h>
#include <vector>
#include <algorithm> // sort
#include <map>
using namespace std;

int main(){
  int n,m;
  cin>>n>>m;
  int l=0;
  int r=n;
  int x,y;
  for(int i=0;i<m;i++){
    cin>>x>>y;
    l=max(x,l);
    r=min(r,y);
  }
  cout<<max(0,r-l+1)<<endl;
}


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

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; }

inline int read() {
    int x(0), sgn(1); char ch(getchar());
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
    for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
    return x * sgn;
}

inline char read_opt() {
	char ch(getchar());
	for (; ch != '+' && ch != '-'; ch = getchar());
	return ch;
}

void File() {
#ifdef zjp_shadow
	freopen ("E.in", "r", stdin);
	freopen ("E.out", "w", stdout);
#endif
}

const int N = 1e5 + 1e3;
const long long inf = 1e15;

long long f[N][3];

int main () {

	File();

	int n = read();
	f[1][0] = read(); f[1][1] = f[1][2] = - inf;

	For (i, 2, n) {
		char opt = read_opt(); int x = read();
		if (opt == '+') {
			f[i][0] = max(f[i - 1][0], f[i - 1][1]) + x;
			f[i][1] = f[i - 1][1] - x;
			f[i][2] = f[i - 1][2] + x;
		} else {
			f[i][0] = - inf;
			f[i][1] = max(f[i - 1][0], f[i - 1][1]) - x;
			f[i][2] = max(f[i - 1][1], f[i - 1][2]) + x;
		}
	}
	printf ("%lld\n", max({f[n][0], f[n][1], f[n][2]}));

	return 0;

}


Measured memory_bytes: 2560
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#define REP0(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define REP1(i,n) for((i)=1;(i)<=(int)(n);(i)++)
#define ll long long
using namespace std;

int i,j,k;
const ll MOD = 1000000000 + 7;

int main(){
    string s,t,ans;
    ans="Yes";
    cin>>s>>t;
    REP0(i,s.size()){
        if(s[i]!=t[i]){
            ans="No";
            break;
        }
    }
    

    cout<<ans<<'\n';
    return 0;
}

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

#define rep(i, s, n) for(int i = s; i < n; ++i)
#define rrep(i, n) for(int i = n - 1; i > -1; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
const long long INF = 1000000000;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<int, int> P;

int main() {
    ll n, k;
    cin >> n >> k;
    vector<P> a(n + 1);
    int tmp;
    rep(i, 1, n + 1){
        cin >> tmp;
        a[i].first = tmp;
        a[i].second = -1;
    }
    int idx = 1, cnt = 0, next, loop, before_loop;
    while(1){
        next = a[idx].first;
        if(a[idx].second != -1){
            loop = cnt - a[idx].second;
            before_loop = a[idx].second;
            break;
        }
        a[idx].second = cnt;
        cnt++;
        if(cnt == k){
            cout << next << endl;
            return 0;
        }
        idx = next;
    }

    k -= before_loop;
    k %= loop;
    idx = 1;
    rep(i, 0, k + before_loop){
        idx = a[idx].first;
    }
//    cout << "bloop " << before_loop << endl;
//    cout << "loop: " << loop << endl;
//    cout << "k " << k << endl;
    cout << idx << endl;
}


Measured memory_bytes: 4852
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <list>
#include <utility>
#include <tuple>
#include <cstdio>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <cmath>
#define _GLIBCXX_DEBUG
using namespace std;
const long long INF = 1LL << 60;
typedef long long ll;
int main() {
    int N;
    cin >> N;
    vector<string> W(N);
    bool frg = false;
    for(int i=0;i<N;i++){
        cin >> W[i];
        if(i!=0){
            int size= W[i-1].size();
            if(W[i][0]!=W[i-1][size-1]){
                cout << "No" << endl;
                break;
            }
            for(int j=0;j<i;j++){
                if(W[i]==W[j]){
                    frg = true;
                    cout << "No" << endl;
                    break;
                }
            }
        }
        if(frg){
            break;
        }
        if(i==N-1){
            cout << "Yes" << endl;
        }
    }
}

Measured memory_bytes: 3636
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

vector<ll> fx(10000005,2);

int main() {
    ll n;
    cin >> n;
    fx[0] = 0; fx[1] = 1;
    for(int i = 2;i <= n / 2 + 1;i++) {
        for(int j = 2;i * j <= n;j++) {
            fx[i*j]++;
        }
    }
    ll ans = 0;
    for(int i = 1;i <= n;i++) ans = ans + i * fx[i];
    cout << ans << endl;
}


Measured memory_bytes: 81224
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <utility>
#include <functional>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <numeric>
#include <complex>

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()
const int INF = (1<<30);
const ll INFL = (1LL<<62);
const ll MOD = 1000000007;
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> ll mod_pow(ll a, ll n, T mod) { mod=(ll)mod; ll res=1, p=a%mod; while(n){ if(n&1) res=res*p%mod; p=p*p%mod; n>>=1; } return res; }
template<int mod>
struct ModInt {
    using ll = long long; ll x;
    ModInt() : x(0) {}
    ModInt(int y) : x(y >= 0 ? ll(y % mod) : (ll)((mod - (-y) % mod))) {}
    ModInt(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
    ModInt &operator+=(const ModInt &rhs) { if((x += rhs.x) >= mod) x -= mod; return *this; }
    ModInt &operator-=(const ModInt &rhs) { if((x += mod - rhs.x) >= mod) x -= mod; return *this; }
    ModInt &operator*=(const ModInt &rhs) { x = x * rhs.x % mod; return *this; }
    ModInt &operator/=(const ModInt &rhs) { *this *= rhs.inverse(); return *this; }
    ModInt operator-() const { return ModInt(-x); }
    ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; }
    ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; }
    ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; }
    ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; }
    ModInt operator==(const ModInt &rhs) const { return x == rhs.x; }
    ModInt operator!=(const ModInt &rhs) const { return x != rhs.x; }
    friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.x; }
    friend istream &operator>>(istream &is, ModInt &rhs) { ll t; is >> t; rhs = ModInt<mod> (t); return (is); }
private:
    ModInt inverse() const { ll a = x, u = 1LL, v = 0LL, t, b = (ll)mod; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return  ModInt(u); }
};
using mint = ModInt<MOD>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a, b;
    cin >> a >> b;
    if(a & 1 and b & 1) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 892
#include <bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()),((v).end())
#define ll long long
#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
double const EPS = 1e-9, PI = acos(-1);
const int N = 1e6 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;
const ll INF = 1e18;
using namespace std;

vector<int> adj[N];

int main() {
	fastIO;
#ifdef LOCAL
//	freopen("input.in", "rt", stdin);
#endif
	int n, a;
	cin >> n;
	for (int i = 2; i <= n; ++i) {
		cin >> a;
		adj[a].push_back(i);
	}
	for (int i = 1; i <= n; ++i) {
		cout << adj[i].size() << '\n';
	}
	return 0;
}


Measured memory_bytes: 33216
#include<iostream>
using namespace std;
int main(){
  int n;cin>>n;
  int mx=0;
  int sm=0;
  for(int i=0;i<n;++i){
    int buf;cin>>buf;
    sm+=buf;
    mx=max(mx,buf);
  }
  if(mx<sm-mx){
    cout<<"Yes"<<endl;
  }
  else{
    cout<<"No"<<endl;
  }
  return 0;
}


Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define P pair<int,int>
#define F first
#define S second
#define mod 1000000007
#define INF 9000000000000000000
signed main(void){
    int n,a,b;cin>>n>>a>>b;
    cout<<min(a*n,b)<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long long int W[1000000];
long long int B[1000000];
void dfs(int a, int b,vector<int> V[], bool vis[])
{
  W[a] = 1;
  B[a] = 1;
  for(int i = 0;i<V[a].size();i++)
  {
    long long int c = V[a][i];
    if(vis[c] == false)
    {
      vis[c] = true;
      dfs(c,b,V,vis);
      W[a]  = (W[a]*(W[c]+B[c]))%1000000007;
      B[a] = (B[a]*(W[c]))%1000000007;
    }
  }
}
int main()
{
   int N;
   cin>>N;
   vector<int> V[N];
   bool vis[N];
   vis[0] = false;
   for(int i = 0 ;i<N-1;i++)
   {
     vis[i+1] = false;
     int a,b;
     cin>>a>>b;
     V[a-1].push_back(b-1);
     V[b-1].push_back(a-1);
   }
   vis[0] = true;
   dfs(0,0,V,vis);
   cout<<(W[0]+B[0])%1000000007;

}


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

typedef unsigned long long ll;

const int MN = 200010;

int N, X;
int A[MN];
ll psum[MN];

int f(int cnt) {
    if(cnt == 1) return 5;
    else return 2 * cnt + 1;
}
ll calc(int l, int r) {
    return psum[r] - (l? psum[l - 1] : 0);
}

int main() {
    scanf("%d %d", &N, &X);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
    }
    for(int i = 0; i < N; i++) {
        psum[i] = A[i];
        if(i) psum[i] += psum[i - 1];
    }

    ll ans = 1e19;
    for(int i = 1; i <= N; i++) {
        ll sum = 0;
        int cnt = 1;
        for(int j = N - 1; j >= 0; j -= i) {
            sum += calc(max(0, j - i + 1), j) * f(cnt);
            cnt++;
        }
        ans = min(ans, sum + i * X);
    }
    cout << ans + 1LL * N * X;
}


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

int main() {
  int a,b,c;
  cin >> a >> b >> c;
  int ans = 1;
  if(b != a){
    ans++;
  }
  
  if(c != a && b != c){
    ans++;
  }
  
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

// 約数の列挙O(√n)
vector<ll> enum_divisors(ll n){
    vector<ll> res;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            res.push_back(i);
            // 重複しないならばiの相方であるN/iもpush
            if(i!=n/i) res.push_back(n/i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

// 桁数を返す
int cnt_digits(ll X){
    int cnt=0;
    while(X>0){
        X/=10;
        cnt++;
    }
    return cnt;
}

int main(){
    ll n;
    cin>>n;
    auto ed=enum_divisors(n);

    int ans=INF;
    for(auto a : ed){
        ll b=n/a;
        int tmp=max(cnt_digits(a),cnt_digits(b));
        ans=min(ans,tmp);
        //cout<<ans<<endl;
    }

    cout<<ans<<endl;
}


Measured memory_bytes: 3576
package main

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

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func gojo(a, b int) int {
	if a < b {
		a, b = b, a
	}
	if b < 1 {
		return -1
	}

	if a%b == 0 {
		return b
	}

	return gojo(b, a%b)
}

func main() {
	ins := strings.Split(readLine(), " ")

	a, _ := strconv.Atoi(ins[0])
	b, _ := strconv.Atoi(ins[1])
	c, _ := strconv.Atoi(ins[2])
	d, _ := strconv.Atoi(ins[3])

	a--

	ca := a / c
	da := a / d
	cda := a / (c * d / gojo(c, d))

	cb := b / c
	db := b / d
	cdb := b / (c * d / gojo(c, d))

	l := b - (cb + db) + cdb
	r := a - (ca + da) + cda

	fmt.Println(l - r)
}


Measured memory_bytes: 640
h,w,k = map(int, input().split())
c = [list(input()) for i in range(h)]
d = [[0 if j == "." else 1 for j in i] for i in c]
ans = 0
for i in range(2 ** h):
    a = [0] * h
    for j in range(h):
        if (i >> j) & 1:
            a[j] = 1
    for I in range(2 ** w):
        b = [0] * w
        kuro = 0
        for J in range(w):
            if (I >> J) & 1:
                b[J] = 1
        for k1,v1 in enumerate(d):
            if a[k1]:
                continue
            for k2,v2 in enumerate(v1):
                if b[k2]:
                    continue
                kuro += v2
        if kuro == k:
            ans += 1
print(ans)

Measured memory_bytes: 68348
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef pair<int, int> P;
const int c_INF = 111111111;
const int c_mINF = -111111111;
const int c_YET = -1;
const int c_Dx[4] = { 0,0,1,-1 };
const int c_Dy[4] = { 1,-1,0,0 };
const int c_div = 1000000007; //10^9 + 7


int main() {


	int D,E;
	scanf("%d%d", &D, &E);

	printf("%d", D + E / 2);

}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);  
const ld EPS = 0.0000000001;

vector<vector<ll>> g;
int h, w, n;
vector<pair<ll, ll>> a;

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

void dfs(int x, int y, int c=0){
    
    g[x][y] = a[c].second;
    a[c].first -= 1;

    for(int i=0; i<4; i++){
        int nx=x+dx[i], ny=y+dy[i];
        if(nx<0 || ny<0 ||h<=nx || w<=ny) continue;
        if(0<g[nx][ny]) continue;
        if(a[c].first==0) c += 1;
        dfs(nx, ny, c);
    }
}

int main(){
    cin >> h >> w >> n;
    g.assign(h, vector<ll>(w, -1));
    a.resize(n);

    for(int i=0; i<n; i++){
        cin >> a.at(i).first;
        a.at(i).second = i+1;
    }
    sort(a.begin(), a.end(), greater<pair<ll, ll>>());
    dfs(0, 0);

    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++) cout << g[i][j] << ' ';
        cout << endl;
    }
    
}

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

int main(){
  int n;
  double t,a;
  cin >> n >> t >> a;
  
  double min=300,tmp;
  int id;
  rep(i,n){
    cin >> tmp;
    tmp = t - tmp*0.006;
    if(abs(a-tmp) < min){
      min = abs(a-tmp);
      id = i+1;
    }
  }
  
  cout << id << endl;
  
  return 0;

}


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



int main(void)
{


    int t[5];
    cin>>t[0]>>t[1]>>t[2]>>t[3]>>t[4];
    for(int i=0;i<5;i++)
    {
        if(t[i]==0)
        {
            cout<<i+1<<endl;
            return 0;
        }
    }





    return 0;
}



Measured memory_bytes: 3524
N = int(input())
_L = list(map(int,input().split()))
L = [(v,i) for i,v in enumerate(_L)]
L = sorted(L,key=lambda x:(-x[0]))

dp = [[0]*(N+1) for _ in range(N+1)]

ans=0
for x in range(N):#前から
    for y in range(N):#後ろから
        cur = x+y
        if  cur==N:
            ans = max(ans,dp[x][y])
            break
        val,ind = L[cur]
        dp[x+1][y] = max(dp[x+1][y],dp[x][y]+val*abs(ind-x))
        dp[x][y+1] = max(dp[x][y+1],dp[x][y]+val*abs(N-y-1-ind))
print(ans)


Measured memory_bytes: 100708
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

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

#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()

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

	string S;
	cin >> S;
	int w;
	cin >> w;
	REP(i, (S.size() + w - 1) / w){
		cout << S[i * w];
	}
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
std::vector<ll> IsPrime;

void sieve(ll max){
    // if(max+1 > IsPrime.size()){     // resizeで要素数が減らないように
        IsPrime.resize(max+1,-1); // IsPrimeに必要な要素数を確保
    // } 
    IsPrime[0] = -1; // 0は素数ではない
    IsPrime[1] = -1; // 1は素数ではない

    for(ll i=2; i<=max; ++i){
        if(IsPrime[i]==-1){
            for(ll j=2; i*j<=max; ++j) // (max以下の)iの倍数は
                if(IsPrime[i*j]==-1)IsPrime[i*j] = i;      // 素数ではない
            IsPrime[i]=i;
        }
    }
}
int main() {
    ll n;
    std::cin >> n;
    sieve(1e6+20);
    vector<ll> a(n);
    ll gg=0;
    vector<ll> cnt(1e6+20,0);
    for (ll i = 0; i < n; i++) {
        std::cin >> a[i];
        ll now = a[i];
        if(i==0)gg = a[i];
        if(i!=0)gg = gcd(gg,a[i]);
        set<ll> el;
        while(IsPrime[now]!=-1){
            el.insert(IsPrime[now]);
            now /= IsPrime[now];
        }
        for (auto e : el) {
            cnt[e]++;
        }
    }
    bool ok = true;
    bool ok2= true;
    for (ll i = 1; i < 1e6+10; i++) {
        if(cnt[i]>1){
            ok = false;
        }
        if(cnt[i]==n){
            ok2 = false;
        }
    }
    
    if(ok){
        std::cout << "pairwise coprime" << std::endl;
    }else if(ok2){
        std::cout << "setwise coprime" << std::endl;
    }else{
        std::cout << "not coprime" << std::endl;
    }
}


Measured memory_bytes: 26732
// segment tree/BIT number of unique element in a range
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int mx=500010;
int arr[mx],n,m,last[mx];
pair<int,int> que[mx];
vector<int> g[mx];
int BIT[mx];
void update(int pos,int val)
{
	for(;pos<=mx;pos+=pos&(-pos))BIT[pos]+=val;
}
int query(int pos)
{
	int sum=0;
	for(;pos>0;pos-=pos&(-pos))sum+=BIT[pos];
	return sum;
}
map<pair<int,int>,int> mp;

int main(){
	fast;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>arr[i];
	}
	for(int i=0;i<m;i++){
		cin>>que[i].x>>que[i].y;
		g[que[i].y].push_back(que[i].x);
	}
	for(int i=1;i<=n;i++){
		if(last[arr[i]]){
			update(last[arr[i]],-1);
		}
		last[arr[i]]=i;
		update(i,1);
		for(auto x:g[i]){
			mp[{x,i}]=(query(i)-query(x-1));
		}
	}
	for(int i=0;i<m;i++){
		cout<<mp[que[i]]<<endl;
	}
}


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

using namespace std;

int main(int argc, const char *argv[]) {
  string s, t;
  cin >> s >> t;
  int ans = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] != t[i]) {
      ans++;
    }
  }

  cout << ans << '\n';
  return 0;
}

Measured memory_bytes: 3920
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(),(x).end()
#define dlog(x) cerr<<#x<<'='<<x<<endl
#define f first
#define s second
#define endl '\n'
#define sq(x) (x)*(x)*1ll
//typedef tree<int,null_type,less_equal<int>,rb_tree_tag,
//      tree_order_statistics_node_update> indexed_multiset;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//      tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef pair<int,int> pii;



int main () {
        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//        freopen("test_input.txt", "r", stdin);
        ll n, a, b; cin >> n >> a >> b;
        cout << min(a,n % (a + b)) + (n / (a + b)) * a;
        return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;


public class Main { 
       public static void main(String[] args) {
           int n; 
           String a;
           int i;
           int j;
           
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        String [] [] c = new String [4] [13];
        
        while(n!=0){
            n--;
            switch(a = sc.next().toUpperCase()){
                case "S":
                  i = 0;
                  j = sc.nextInt();
                  c [i] [j-1] = a+" "+j;
                break;
                case "H":
                    i = 1;
                    j = sc.nextInt();
                    c [i] [j-1] = a+ " " + j;
                break;
                case "C":
                    i = 2;
                    j = sc.nextInt();
                    c [i] [j-1] = a+ " " + j;
                break;   
                case "D":
                    i = 3;
                    j = sc.nextInt();
                    c [i] [j-1] = a+ " " + j;
                break;   
          }
        }
          for ( i = 0; i <4; i++){
            for (j = 0; j<13; j++){
                int k = j+1;
                if( c[i][j]== null){
                switch(i){
                    case 0:
                    System.out.println("S "+ k);
                    break;
                    case 1:
                    System.out.println("H "+ k);
                    break;
                    case 2:
                    System.out.println("C "+ k);
                    break;
                    case 3:
                    System.out.println("D "+ k);
                    break;
                }
                }
                }
          }
          }
}




Measured memory_bytes: 26204
n = int(input())
a_list = list(map(int, input().split()))

a_list = sorted(a_list, reverse = True)

# print(a_list)

Alice_Score = 0
Bob_Score = 0

for i in range(n):

    # 奇数(1, 3, 5…)枚目はAliceのターン
    if i % 2 == 0 :
        Alice_Score = Alice_Score + a_list[i]
    else :
        Bob_Score = Bob_Score + a_list[i]

ans = Alice_Score - Bob_Score

print(ans)

Measured memory_bytes: 9116
n,x = map(int,input().split())
a = list(map(int,input().split()))
ans = 0

if sum(a) ==x:
    print(n)
else:
    a.sort()
    for i in range(n):
        if x >= a[i]:
            ans += 1
            x -= a[i]
        else:
            break

    if ans == n:
        ans -= 1


    print(ans)

Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <list>
#include <numeric>
#include <stack>
#include <queue>
#include <tuple>
#include <bitset>
#include <map>
#include <math.h>
//　ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int,int> P;
const int inf = 1012345678;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

int gcd(int a, int b)
{
  if (a%b==0){
    return(b);
  }
  else{
    return(gcd(b,a%b));
  }
}


int a[1000000];

int main(){
  int N;
  cin >> N;
  rep(i,N) cin >> a[i];
  int res = a[0];
  rep(i,N){
    res = gcd(res,a[i]);
  }
  cout << res << endl;
}


Measured memory_bytes: 640
import java.util.*;
class Main{
    void run(){
	Scanner sc=new Scanner(System.in);
	int M=sc.nextInt();
	sc.nextLine();
	for(int attend=0;attend<M;attend++){
	    StringBuffer text=new StringBuffer(sc.nextLine());
	    int cur=0;
	    int n=sc.nextInt();
	    String command1;
	    String command2;
	    sc.nextLine();
	    for(int i=0;i<n;i++){
		String command=sc.nextLine();
		//System.out.println(command);
		for(int j=0;;j++){
		    if(command.charAt(j)==' '){
			command1=command.substring(0,j);
			command2=command.substring(j+1);
			break;
		    }
		}
		if("forward".equals(command1)){
		    if("char".equals(command2)){
			cur=cur<text.length()? cur+1:cur;    
		    }
		    else{
			for(;cur<text.length()&&text.charAt(cur)==' ';cur++);
			for(;cur<text.length()&&text.charAt(cur)!=' ';cur++);
		    }
		}
		if("backward".equals(command1)){
		    if("char".equals(command2)){
			cur=cur>0? cur-1:cur;    
		    }
		    else{
			for(;cur>0&&text.charAt(cur-1)==' ';cur--);
			for(;cur>0&&text.charAt(cur-1)!=' ';cur--);
		    }  
		}
		if("insert".equals(command1)){
		    text.insert(cur,command2.substring(1,command2.length()-1));
		    cur+=command2.length()-2;
		}		       
		if("delete".equals(command1)){
		    if("char".equals(command2)){
			if(cur!=text.length()){
			    text.deleteCharAt(cur);
			}
		    }
		    else{
			int temp=cur;
			for(;temp<text.length()&&text.charAt(temp)==' ';temp++);
			if(temp!=text.length()){
			    for(;temp<text.length()&&text.charAt(temp)!=' ';temp++);
			    text.delete(cur,temp);
			}
		    }
		}
		//print(text,cur);
	    }
	    print(text,cur);
	}
	
    }
    void print(StringBuffer text,int cur){
	    text.insert(cur,"^");
	    System.out.println(text);
	    text.deleteCharAt(cur);
    }
    public static void main(String[] args){
	new Main().run();
    }
}

Measured memory_bytes: 12000
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
bigger_a = 0
bigeer_b = 0
for a, b in zip(A, B):
    if a > b:
        bigger_a += a - b
    if b > a:
        bigeer_b += (b - a) // 2
ans = 'Yes' if bigeer_b >= bigger_a else 'No'
print(ans)

Measured memory_bytes: 4596
module.exports=function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){r(1)(r(2).readFileSync("/dev/stdin","UTF-8"))},function(e,t){e.exports=function(e){for(var t=e.split("\n"),r=Number(t[0]),n=t[1].split(" ").map(function(e){return Number(e)}),o=0,u=0;u<r;u++)n[u]%2==0&&(o+=1);var i=Math.pow(3,r)-Math.pow(2,o);console.log(i)}},function(e,t){e.exports=require("fs")}]);

Measured memory_bytes: 22076
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// const int INF = (1 << 30) - 1;
const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;

ll N, M;
vector<ll> a, b;
vector<ll> c;

const int MAX = 1 << 12;
ll dp[MAX];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  a.resize(M);
  b.resize(M);
  c.resize(M);
  for (int i = 0; i < M; i++) {
    cin >> a[i] >> b[i];
    for (int j = 0; j < b[i]; j++) {
      int ci;
      cin >> ci;
      c[i] |= (1 << (ci - 1));
    }
  }

  fill(dp, dp + MAX, LL_INF);
  dp[0] = 0;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < (1 << N); j++) {
      ll next = j | c[i];
      dp[next] = min(dp[next], dp[next]);
      dp[next] = min(dp[next], dp[j] + a[i]);
    }
  }

  int idx = (1 << N) - 1;
  if (dp[idx] == LL_INF) {
    cout << -1 << endl;
  } else {
    cout << dp[idx] << endl;
  }

  return 0;
}

Measured memory_bytes: 384
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
using namespace std;
typedef pair<double,string> pis;

int main(){
  int n;
  while(cin>>n,n){
    string str;
    vector<pis> v(n);
    for(int i=0;i<n;i++){
      int p,a,b,c,d,e,f,s,m;
      cin>>str>>p>>a>>b>>c>>d>>e>>f>>s>>m;
      v[i]=make_pair(-(double)(s*f*m-p)/(a+b+c+(d+e)*m),str);
    }
    sort(v.begin(),v.end());
    for(int i=0;i<(int)v.size();i++){
      cout<<v[i].second<<endl;
    }
    cout<<"#"<<endl;
  }
  return 0;
}

Measured memory_bytes: 1224