akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<cstdlib>
using namespace std;
struct Node{
	int key;
	Node *right, *left, *parent;
};
Node *root, *NIL;

Node *treeMinimum(Node *x){
	while(x->left != NIL)
		x=x->left;
	return x;
}
Node *treeSuccessor(Node *x){
	if(x->right!=NIL)	return treeMinimum(x->right);
	Node *y=x->parent;
	while(y!=NIL && x==y->right){
		x=y;
		y=y->parent;
	}
	return y;
}
void treeDelete(Node *z){
	Node *y;
	Node *x;
	if(z->left==NIL || z->right==NIL)	y=z;
	else	y=treeSuccessor(z);
	if(y->left!=NIL){
		x=y->left;
	}else{
		x=y->right;
	}
	if(x!=NIL)
		x->parent=y->parent;
	if(y->parent==NIL)
		root=x;
	else{
		if(y==y->parent->left)
			y->parent->left=x;
		else
			y->parent->right=x;
	}
	if(y!=z)
		z->key=y->key;
	free(y);
}

Node *find(Node *u, int k){
	while(u!=NIL && k!=u->key){
		if(k<u->key)	u=u->left;
		else	u=u->right;
	}
	return u;
}
void insert(int k){
	Node *y=NIL;
	Node *x=root;
	Node *z;
	z=(Node *)malloc(sizeof(Node));
	z->key=k;
	z->left=NIL;
	z->right=NIL;

	while(x!=NIL){
		y=x;
		if(z->key < x->key){
			x=x->left;
		}
		else
			x=x->right;
	}
	z->parent=y;
	if(y==NIL){
		root=z;
	}else{
		if(z->key < y->key){
			y->left=z;
		}else{
			y->right=z;
		}
	}
}
void inorder(Node *u){
	if(u==NIL)	return ;
	inorder(u->left);
	cout<<" "<<u->key;
	inorder(u->right);
}
void preorder(Node *u){
	if(u==NIL)	return ;
	cout<<" "<<u->key;
	preorder(u->left);
	preorder(u->right);
}
int main(){
	int n, i, x;
	string com;
	cin>>n;
	while(n--){
		cin>>com;
		if(com=="insert"){
			cin>>x;
			insert(x);
		}
		else if(com=="print"){
			inorder(root);
			cout<<endl;
			preorder(root);
			cout<<endl;
		}
		else if(com=="find"){
			cin>>x;
			Node *t=find(root, x);
			if(t!=NIL)	cout<<"yes"<<endl;
			else	cout<<"no"<<endl;
		}
		else if(com=="delete"){
			cin>>x;
			treeDelete(find(root, x));
		}
	}
	return 0;
}


Measured memory_bytes: 21748
N,K = map(int, input().split())
h = [[] for _ in range(N)]
for i in range(N):
    h[i] = int(input())
    
h.sort(reverse=True)

min = h[0]-h[K-1]
for i in range(N-K+1):
    if min>h[i]-h[i+K-1]:
        min = h[i]-h[i+K-1]
        
print(min)

Measured memory_bytes: 10592
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

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

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

// Self settings
// clang-format off
#define MAX_N 100000
#define REP(i, N) for (int i = 0; i < (int)(N); ++i)
#define SLN(i,N) (i == N-1 ? "\n" : " ")
ll fact(ll n) { ll res = 1; for(ll i=2;i<=n;++i) res = res * i; return res;}
ll nCr(ll n, ll r) {return (fact(n)/fact(n-r)*fact(r)) ;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const ll MOD = 1e9+7;
const ll INF = 1LL << 60;
const int inf = 1 << 31;
// clang-format on

int N;
int d[MAX_N];

int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  REP(i, N) cin >> d[i];

  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      ans += d[i] * d[j];
    }
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 888
use proconio::{input, fastout};

#[fastout]
fn answer(links: std::vec::Vec<std::vec::Vec<bool>>, n: usize) -> std::vec::Vec<(usize,usize)> {
    let mut ret: std::vec::Vec<(usize,usize)> = Vec::new();
    for i in 0..n {
        for j in (i+1)..n {
            if links[i][j] {
                ret.push((i, j));
            }
        }
    }
    ret
}


fn main() {
    input!{
        (n, k): (usize, usize)
    };

    let mut cnt = (n-1) * (n-2) / 2;
    if k>cnt {
        println!("-1");
        return;
    }

    let mut links = vec![vec![false; n]; n];
    for j in 0..(n-1) {
        links[n-1][j] = true;
        links[j][n-1] = true;
    }

    'all: for i in 0..(n-1) {
        for j in (i+1)..(n-1) {
            if cnt==k {
                break 'all;
            } else {
                links[i][j] = true;
                links[j][i] = true;
                cnt -= 1;
            }   
        }
    }

    let edges = answer(links, n);
    println!("{}", edges.len());
    for (x, y) in edges {
        println!("{} {}", x+1, y+1);
    }
}


Measured memory_bytes: 2220
package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

func parseProblem() int {
	k, n := scanInt(sc), scanInt(sc)
	as := nextIntSlice(sc, n)
	sort.Sort(sort.IntSlice(as))
	r := k - as[len(as)-1] + as[0]
	for i := 0; i+1 < len(as); i++ {
		if cur := as[i+1] - as[i]; r < cur {
			r = cur
		}
	}
	return k - r
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return int(a)
}

func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextIntSlice(sc *bufio.Scanner, n int) (a []int) {

	a = make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt(sc)
	}
	return a
}
func resolve(n int) int {
	return n
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}

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

// Macros
#define F  first
#define S  second
#define PB push_back

// Rename Datatypes
using ll  = long long;
using ld  = long double;
using pii = pair<int,int>;

// Common Functions
ll fastpow(ll a, ll ex){ ll res = 1; while(ex){ if(ex & 1) res *= a; a *= a; ex >>= 1; } return res; }
ll fastpow(ll a, ll ex, ll m){ a %= m; ll res = 1; while(ex){ if(ex & 1) res = (a*res) % m; a = (a*a) % m; ex >>= 1; } return res; }
ll modinverse(ll a, ll m){ ll m0 = m, y = 0, x = 1; while (a > 1){ ll q = a / m; a %= m; swap(a,m); x -= q*y; swap(x,y); } if(x < 0) x += m0; return x; }

// HELPERS
//  Tedious Grid Movements
vector<pii> moves_grid = { {0,1} , {0,-1} , {1,0} , {-1,0} };
inline bool move_okay(int x, int y, int n, int m) { return 0 <= x and x < n and 0 <= y and y < m; }
//  Remember:
//   * vec.assign() GOOD; vec.resize() BAD
//   * comparator uses <

// Output, Common Containers
template <typename T> ostream& operator << (ostream& os, const vector<T>& v) { for (T i : v) os << i << ' '; return os; }
template <typename T> ostream& operator << (ostream& os, const set<T>& v)    { for (T i : v) os << i << ' '; return os; }
template <typename T, typename S> ostream& operator << (ostream& os, const pair<T,S>& v) { os << "(" << v.F << "=>" << v.S << ") "; return os; }
template <typename T, typename S> ostream& operator << (ostream& os, const map<T,S>& v)  { for (auto i : v) os << i << ' '; return os; }

// Debugging
#ifdef SANYAM1101
	#define trace(args...) do{ cerr << "Trace On! (Line: " << __LINE__ << ")\n"; d_f(string(#args), args); } while(0)
	void d_f(__attribute__((unused)) string str) {}
	template<typename Head, typename... Tails> void d_f(string str, Head H, Tails... T) { cerr << "\t" << str.substr(0, str.find(", ")) << " <=> [" << H << "]\n"; d_f(str.substr(str.find(", ") + 2), T...); }
#else
	#define trace(args...)
#endif


// CODE STARTS HERE

void solve(){
	int n;
	cin >> n;

	vector<ll> arr(n);
	for(auto& i: arr) cin >> i;
	sort(arr.begin(), arr.end(), greater<>());

	ll ans = arr.front();

	for(int ptr = 1, i = 3; i <= n; ++i){
		ans += arr[ptr];
		if((i & 1) == 0) ++ptr;
	}

	cout << ans << "\n";
}

signed main (void){
	// Fast_IO
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	// Set Fixed Notation (default sucks), High Precision for Floating-Point Output
	cout << fixed << setprecision(20);

	// Multiple TCs
	int tt = 1;
	// cin >> tt;
	while(tt--) solve();
}


Measured memory_bytes: 4780
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()


int main(){
    ll n;
    cin >> n;
    vector<ll> a(n);
    REP(i, n){
        cin >> a.at(i);
    }


    map<ll, ll> count;

    ll ans = 0;
    REP(i, n){
        ans += count[i - a.at(i)];
        count[i + a.at(i)]++;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 27368
# 入力
N, M = map(int, input().split())

# 番号を入力
A = [int(i) for i in range(N)]

B = [] # 台No. リスト

if N % 2 == 0:
    # N が偶数の場合

    center = N // 2
    c1 = center // 2
    c2 = center + c1
    # print(c1,c2)

    for i in range(1, M+1):
        # print(i)
        j = i // 2
        # print("j", j)
        if i % 2 == 1:
            B.append((c1-j, c1+j+1))
        else:
            B.append((c2-(j-1), c2 - (j-1) + 2*j ))

else:
    # N が奇数の場合

    center = N // 2 + 1
    # print(center)
    for i in range(1, M+1):
        B.append((center - i, center + i))

for i in B:
    print(i[0], i[1])

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

using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
ll a[MAXN];


int main() {
    int n;
    cin >> n;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        ll t;
        cin >> t;
        ans += t - 1;
    }
    cout << ans << endl;
    return 0;
}

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

int main(){
	int n,x,mi,ma;
	vector<int> vec;
	long long res,result = 10000000;
		cin >> n;
		for(int i = 0;i < n;i++){
			cin >> x;
			vec.push_back(x);
		}
		sort(vec.begin(),vec.end());
		mi = vec[0];
		ma = vec[n-1];
		for(int i = mi;i <= ma;i++){
			res = 0;
			for(int j = 0;j < n;j++){
			res +=	(vec[j] - i) * (vec[j] - i);
			}
			//cout << i << " " << res <<endl;
			result = min(result,res);
		}
		cout << result << endl;

	return 0;
}

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

int main(){

    int a, b;
    char op;

    cin >> a >> op >> b;
    while(op != '?'){
        if(op == '+') cout << a + b << endl;
        if(op == '-') cout << a - b << endl;
        if(op == '*') cout << a * b << endl;
        if(op == '/') cout << a / b << endl;
    cin >> a >> op >> b;
    }

    return 0;
}


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

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

Node *root, *NIL;

void insert(int a){
    Node *x = root;
    Node *y = NIL;
    Node *z;
    
    z = (Node *)malloc(sizeof(Node));
    z->key = a;
    z->right = NIL;
    z->left = NIL;
    
    while( x != NIL ){
        y = x;
        if( z->key < x->key) {
            x = x->left;
            
        }
        else {
            x = x->right;
        }
    }
    z->parent = y;
    
    if( y == NIL) {
        root = z;
        
    }
    else{
        if(z->key < y->key) {
            
            y->left = z;
            
        }
        else{
            y->right = z;
        } 
    }
}

void Pre(Node *u){
    if( u == NIL) return;
    printf(" %d", u->key);
    Pre(u->left);
    Pre(u->right);
}

void In(Node *u){
    if( u == NIL) return;
    In(u->left);
    printf(" %d", u->key);
    In(u->right);
}

void print(Node *u){
    In(u);
    printf("\n");
    Pre(u);
    printf("\n");
}

int main(){
    int n,x;
    string str;
    
    scanf("%d",&n);
    
    for(int i = 0; i < n; i++){
        cin >> str;
        if(str == "insert"){
            scanf("%d",&x);
            insert(x);
        }
        else if(str == "print") {
            print(root);
            
        }
    }
    
    return 0;
}    



Measured memory_bytes: 26732
#include <bits/stdc++.h>
using namespace std;
int main() {
    string S; cin >> S;
    vector<long long>A;
    for (long long i = 1; i < S.size(); i++) {
        if (S.at(i) != S.at(i - 1)) {
            A.push_back(i);
        }
    }
    bool check = true; //true...right,false...left
    vector<pair<long long, long long>>ans(S.size());
    for (long long i = 0; i < S.size(); i++) {
            auto it = upper_bound(A.begin(), A.end(), i);
            if (S.at(i) == 'R') check = true;
            else check = false;
        if (check) {
            ans.at(i).first = *it;
            ans.at(i).second = (ans.at(i).first - i) % 2;
        }
        else {
            long long pos = it - A.begin();
            ans.at(i).first = A.at(pos - 1);
            ans.at(i).second = (i - ans.at(i).first) % 2;
        }
    }
    vector<long long>B(S.size(), 0);
    for (long long i = 0; i < S.size(); i++) {
        B.at(ans.at(i).first - ans.at(i).second)++;
    }
    for (long long i = 0; i < S.size(); i++) {
        cout << B.at(i);
        if (i != S.size() - 1) cout <<  ' ';
    }
    cout << endl;
}

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

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long double pi=acos(-1);
#define x  first
#define y  second
#define read(n,a) for(int i=0;i<n;i++)cin>>a[i];
#define rep(z,n)  for(int i=z;i<n;i++)

ll a[200002]={};
//int b[102]={};
ll n,m;

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    ///freopen("ss.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
       ld d,t,s;
       cin>>d>>t>>s;
       cout<<(d/t <=s? "Yes":"No");
    }
    return 0;
}


Measured memory_bytes: 3740
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
	int lim = 1000000;
	int dp[lim],odp[lim];
	for (int i = 0; i < lim; ++i) {
		dp[i] = i;
		odp[i] = i;
	}

	int num = 4;
	for (int now = 2; num < lim; ++now) {
		for (int i = num; i < lim; ++i) {
			dp[i] = min(dp[i],dp[i-num]+1);
		}
		if (num%2 == 1) {
			for (int i = num; i < lim; ++i) {
				odp[i] = min(odp[i],odp[i-num]+1);
			}
		}
		num = ((now+1)*(now+2)*(now+3))/6;
	}

	int n;
	while (cin >> n, n) {
		cout << dp[n] << " " << odp[n] << endl;
	}

	return 0;
}

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

int main() {
  	int N;
  	int a[100];
  	cin >> N;
  	for(int i = 0; i < N; i++){
    	cin >> a[i];
    }
  	sort(a, a+N);
  	int total = 0;
  	for(int i = 1; i < N; i++){
    	total += (a[i] - a[i - 1]);
    }
  	cout << total << endl;
  	return 0;
}

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

typedef long long ll;
const ll INF = ll(1) << 54;
typedef pair<int, int> P;

void solve(vector< vector<ll> >& d)
{
    int n = d.size();
    for(int k=0;k<n;++k)
    {
        for(int j=0;j<n;++j)
        {
            if(d[k][j] >= INF) continue;
            for(int i=0;i<n;++i)
            {
                if(d[i][k] >= INF) continue;
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

bool is_negative_cycle(const vector< vector<ll> >& d)
{
    int n = d.size();
    for(int i=0;i<n;++i)
    {
        if(d[i][i] < 0) return true;
    }
    return false;
}

int main()
{
    int N, M, s, t, d;
    cin >> N >> M;
    vector< vector<ll> > graph(N, vector<ll>(N, INF));
    for(int i=0;i<N;++i) graph[i][i] = 0;
    for(int i=0;i<M;++i)
    {
        cin >> s >> t >> d;
        graph[s][t] = d;
    }

    solve(graph);
    if(is_negative_cycle(graph))
    {
        cout << "NEGATIVE CYCLE" << endl;
    }
    else
    {
        for(int i=0;i<N;++i)
        {
            for(int j=0;j<N;++j)
            {
                if(j) cout << " ";
                if(graph[i][j] == INF) cout << "INF";
                else cout << graph[i][j];
            }
            cout << endl;
        }
    }
}



Measured memory_bytes: 3236
#include <iostream>
//#include <cstring>
//#include <cctype>
//#include <ctime>
using namespace std;


int main(){
    char word[10];
    string str;
    int     count = 0;
    cin >> word;
    while(1){
        cin >> str;
        if(str == "END_OF_TEXT")break;
        for( int i = 0; i < str.size(); i++){
            str[i] = tolower(str[i]);
        }
        if( str == word) count++;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 1188
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;

typedef long long ll;
#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 999999999

int main(){
  int N;
  double s;
  cin >> N;
  if(N%2==0){
    s=1.0/2.0;
    cout <<s<<endl;
  }
  else if (N%2==1){
    double z=N;
    s=((N/2)+1)/z;
    cout << s;
  }
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;
 
#define max(x, y) ((x)>(y)?(x):(y))
#define min(x, y) ((x)<(y)?(x):(y))
 
Int n, x;
Int ans[2160], a[2160], res = 1LL<<60;
int main(){
  cin >> n >> x;
  for(int i = 0;i < n;i++)cin >> a[i];
  for(int i = 0;i <= n;i++){
    Int tmp = x * i;
    for(int j = 0;j < n;j++){
      tmp += a[j];
    }
    res = min(tmp, res);
    Int z = a[n-1];
    for(int j = n-1;j > 0;j--){
      a[j] = min(a[j], a[j-1]);
    }
    a[0] = min(a[0], z);
  }
  cout << res << endl;
  return 0;
}

Measured memory_bytes: 256
package main

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

var sc *bufio.Scanner

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func minInt(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n := nextInt()

	ans := 0
	bPrev := -1
	for i := 0; i < n-1; i++ {
		b := nextInt()
		switch {
		case i == 0:
			ans += b
		default:
			ans += minInt(b, bPrev)
		}
		bPrev = b
	}

	fmt.Println(ans + bPrev)
}


Measured memory_bytes: 640
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>

using namespace std;
int gcd(int x,int y){
  if (x%y==0){
    return y;
  }else{
    int a,b;
    a=y;
    b=x%y;
    return gcd(a,b);
  }
}
int main(void){
  int N;
  cin >>N;
  int A[N];
  for (int i=0;i<N;i++) cin >>A[i];
  int Gcd;
  for (int i=0;i<N-1;i++){
  if (i==0){
    Gcd=A[0];
  }
  Gcd=gcd(Gcd,A[i+1]);
  }
  cout <<Gcd<<endl;
  return 0;
}

Measured memory_bytes: 640
def solver(n, s, g, amida):
    for a in amida:
        a = list(map(int, list(a)))
        r, l = 0, 0
        if s != 1:
            l = a[s-2]
        if s != n:
            r = a[s-1]

        if r == 1:
            s += 1
        elif l == 1:
            s -= 1

    return True if s == g else False

while True:
    n = int(input())
    if n == 0: break
    s = int(input())
    g = int(input())
    d = int(input())
    amida = [input() for j in range(d)]
    if solver(n, s, g, amida):
        print(0)
    else:
        ans = None
        for i in range(d):
            for j in range(n-1):
                a = amida[i]
                if a[j] == '0':
                    a = a[:j] + '1' + a[j+1:]
                    if '11' in a:
                        continue
                    else:
                        if solver(n, s, g, amida[:i] + [a] + amida[i+1:]):
                            ans = str(i+1) + ' ' + str(j+1)
                            break
            if ans: break
        if ans:
            print(ans)
        else:
            print(1)

Measured memory_bytes: 6772
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <functional>
#include <limits.h>
#include <set>
#include <map>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i,N) for (int i=0;i<N;i++)
#define loop(i,N,M) for(int i=N;i<M;i++)

//変数
ll N;

//解法
void solve() {

	ll ans = LLONG_MAX;

	rep(i,N+1) {

		ll val6 = i;
		ll val9 = N - i;
		ll cnt = 0;

		while (val6 > 0) cnt += (val6 % 6), val6 /= 6;
		while (val9 > 0) cnt += (val9 % 9), val9 /= 9;

		ans = min(ans, cnt);

	}
	cout << ans;

	return;
}

int main() {
	//入力
	cin >> N;
	solve();
	cout << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define pai 3.14159265358979323846
const int INF = 1001001001;
typedef long long ll;
int A[3][3], N;
bool punched[3][3];
bool ok[3][3];
int a[3][3],n;
using Graph = vector<vector<int>>;
vector<vector<int>> field;
vector<bool> seen;

const int MOD = 1000000007;
typedef pair<int,int> P;

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

}
int lcm(int a,int b){
	return a /gcd(a,b) * b;
}






int main() {
   int a[3];
   rep (i,3) cin >> a[i];
   sort (a,a+3);
   bool ok = false;
   if ((a[0]==a[1]&&a[1]!=a[2])||(a[0]!=a[1]&&a[1]==a[2])){
       ok = true;
   }
   if (ok) cout << "Yes" << endl;
   else cout << "No" << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define N 100
 
int main(){
    long ans=0;
    long n;
    cin>>n;
    long d[10][10];
    for (int i=0; i<10; i++){
        for (int j=0; j<10; j++) d[i][j]=0;
    }
    for (long i=1; i<=n; i++){
        vector<int> num;
        long nn=i;
        while(nn){
            num.push_back(nn%10);
            nn/=10;
        }
        int a=num[0];
        int b=num.back();
        if (a==0 || b==0) continue;
        d[a][b]+=1;
    }
    for (int i=1; i<=9; i++){
        for (int j=1; j<=9; j++){
            ans+=d[i][j]*d[j][i];
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
# encoding: utf-8
N, M = map(int, input().split())

if N == M == 1: ans = 1
elif N == 1: ans = M - 2 # 2 corners are omitted
elif M == 1: ans = N - 2 # 2 corners are omitted
else: ans = (N - 2) * (M - 2) # corners (4 flips) and edges (6 flips) are omitted

print(ans)


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

void fnInput(string& rsLine)
{
  int nSize;
  cin >> nSize;
  cin >> rsLine;
}

int fnAttention(const string& cnrsLine)
{
  int nCumlSum = 0;
  for (int i = 1; i < cnrsLine.size(); i++)
    if (cnrsLine[i] == 'E')  nCumlSum++;

  int nMinSum = nCumlSum;
  for (int i = 1; i < cnrsLine.size(); i++)
  {
    if (cnrsLine[i - 1] == 'W')  nCumlSum++;
    if (cnrsLine[i]     == 'E')  nCumlSum--;

    nMinSum = min(nMinSum, nCumlSum);
  }
  return nMinSum;
}
    
int main()
{
  string sLine;
  
  fnInput(sLine);
  cout << fnAttention(sLine) << endl;

  return 0;
}

Measured memory_bytes: 900
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=500500,oo=1e9+10,mod=1e9+7;
struct L{
	int k,b,id;
}a[maxn];
struct QJ{
	int l,r;
}b[maxn];
inline bool cmpk(const L &x,const L &y){return x.k<y.k;}
inline bool cmpb(const L &x,const L &y){return x.b<y.b;}
inline bool operator<(const QJ &x,const QJ &y){return x.r!=y.r?x.r<y.r:x.l<y.l;}
int n;
long long f[maxn],s[maxn];
inline char getc()
{
	static char buf[1048576],*p1=buf,*p2=buf;
	if(p1!=p2)return *p1++;
	p2=(p1=buf)+fread(buf,1,1048576,stdin);
	if(p1!=p2)return *p1++;
	return EOF;
}
inline int rd()
{
	int ans=0,f=1;char ch=getc();
	while((ch<'0'||ch>'9')&&(ch!='-'))ch=getc();
	if(ch=='-'){f=-1;ch=getc();}
	while('0'<=ch&&ch<='9'){ans=ans*10+ch-'0';ch=getc();}
	return ans*f;
}
int main()
{
	std::ios::sync_with_stdio(false);
	n=rd();
	for(int i=1;i<=n;i++)
	{	a[i].b=rd();
		a[i].k=rd();
	}
	sort(a+1,a+n+1,cmpk);
	for(int i=1;i<=n;i++)
		a[i].id=i;
	sort(a+1,a+n+1,cmpb);
	int in=oo;
	for(int i=n;i>=1;i--)
	{	in=min(in,a[i].id);
		b[a[i].id].l=in;
	}
	int ax=0;
	for(int i=1;i<=n;i++)
	{	ax=max(ax,a[i].id);
		b[a[i].id].r=ax;
	}
	sort(b+1,b+n+1);
	/*cerr<<"b: "<<endl;
	for(int i=1;i<=n;i++)
		cerr<<b[i].l<<" "<<b[i].r<<endl;
	cerr<<endl;*/
	/*int p=0;
	f[0]=1;
	s[0]=1;
	for(int i=1;i<=n;i++)
	{	while(p<b[i].r-1)
		{	++p;
			s[p]=(s[p-1]+f[p])%mod;
		}
		(f[b[i].r]+=f[b[i].r]+s[b[i].r-1]-(b[i].l>=2?s[b[i].l-2]:0))%=mod;
	}
	cout<<f[n]<<endl;*/
	int p=1,ans=0;
	for(int i=1;i<=n;i++)
	{	while(b[p].r<b[i].l-1) 
			p++;
		f[i]=(s[i-1]-s[p-1]+mod)%mod;
		if(b[i].l==1)
			f[i]=(f[i]+1)%mod;
		s[i]=(s[i-1]+f[i])%mod;
		if(b[i].r==n)
			ans=(ans+f[i])%mod;
		//cerr<<A[i].l<<" "<<A[i].r<<endl;
	}
	cout<<ans<<endl;
	return 0;
}
/*
3
2 5
6 1
3 7
*/
/*
考虑污染第i条河产生的贡献，对于第j条河，如果bj<bi,kj>ki，j和i一定会在之后相交
对于所有的x，kj>kx>ki，不论bx的值，第x条河最终一定会被i或j污染
将所有直线按k值排序，找到ri表示kj>ki且bj<bi的最大的j
li表示kj<ki且bj>bi的最小的j
如果第i条河被污染区间li-ri中所有的河都会被污染并且区间外的河不会被污染
原问题化为给定n个区间，问有多少种挑选出一些区间的方案使得全集被覆盖
f[i]表示恰好覆盖1-i的方案数
将所有区间按r排序，依次更新
*/

Measured memory_bytes: 16384
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,area,counter=0;
    int flage=0;
    char cf,cs;
    cin>>n>>m;
    char af[n][n];
    char as[m][m];
    area=m*m;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            cin>>af[i][j];
    for(int k=0;k<m;k++)
        for(int l=0;l<m;l++)
            cin>>as[k][l];
        for(int z=0;z<n;z++)
            for(int x=0;x<n;x++){
                cf=af[z][x];
            	cs=as[0][0];
                     if(cf==cs){
                        int a=z;
                        for(int h=0;h<m;h++){
                            int b=x;
                            for(int g=0;g<m;g++){
                                cf=af[a][b];
                                cs=as[h][g];
                                if(cf==cs)
                                    counter+=1;
                                b++;
                            }
                        a++;
                        }
                    }
                    if(counter==area)
                        flage=1;
                    counter=0;
        }
        if(flage==1)
            printf("Yes");
        else
            printf("No");
    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bits/stdint-uintn.h>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;

template <typename T> inline constexpr bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline constexpr bool chmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <typename ite>
void print_ite(ite start, ite end, char sp = ' ', char en = '\n') {
  for (auto i = start; i != end; i++)
    cout << *i << sp;
  cout << en;
}
template <typename ite> void scan_ite(ite start, ite end) {
  for (auto i = start; i != end; i++)
    cin >> (*i);
}
inline void Yes(bool condition) { cout << (condition ? "Yes\n" : "No\n"); }

int main() {
  string st;
  cin >> st;
  int n = st.length();
  vector<uint16_t> s(n);
  for (int i = 0; i < n; i++) {
    s[i] = st[i] - '0';
  }
  vector<vector<uint16_t>> dp(n, vector<uint16_t>(2019, 0));
  for (int i = 0; i < n - 1; i++) {
    dp[i][s[i]] += 1;
    for (int j = 0; j < 2019; j++) {
      int next = 10 * j + s[i + 1];
      next %= 2019;
      dp[i + 1][next] += dp[i][j];
    }
  }
  dp[n - 1][s[n - 1]] += 1;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}


Measured memory_bytes: 798752
N=int(input())
t=0
x=0
y=0
ans=True
for i in range(N):
  ta,xa,ya=map(int,input().split())
  res1=abs(xa-x)
  res2=abs(ya-y)
  if res1+res2>(ta-t):
    ans=False
    break
  else:
    if ((ta-t)-(res1+res2))%2==1:
      ans=False
      break
  t=ta
  x=xa
  y=ya
if ans:
  print("Yes")
else:
  print("No")

Measured memory_bytes: 3064
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
struct temp
{
    int xian,id,year;
    temp(int x=0,int y=0,int z=0) {xian=x;id=y;year=z;}
}a[100000+5];
bool cmp(temp a,temp b){
    if(a.xian!=b.xian)
        return a.xian<b.xian;
    else
       return a.year<b.year;
}
pair<int,int> p[100000+5];
int n,m;
int t1,t2;
int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d",&t1,&t2);
        a[i].id=i;
        a[i].xian=t1;
        a[i].year=t2;
    }
    sort(a+1,a+m+1,cmp);
    int no=0;
    for(int i=1;i<=m;i++){
        if(i>1&&a[i].xian!=a[i-1].xian)
            no=1;
        else
            no++;
        p[a[i].id]=make_pair(a[i].xian,no);
    }
    for(int i=1;i<=m;i++)
        printf("%06d%06d\n",p[i].first,p[i].second);

}

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

Measured memory_bytes: 600
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dl double long
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rrep(i, m, n) for(ll i = (ll)(m); i > (ll)(n); --i)
const ll MOD = 1000000007;
const double pi = 3.14159265358979323846;

const int INF = 2000000000;

void marge(pair<char, int> a[], int left, int mid, int right){
    int n1 = mid - left;
    int n2 = right - mid;
    vector<pair<char, int>> l,r;
    rep(i, 0, n1) l.push_back(a[left+i]);
    rep(i, 0, n2) r.push_back(a[mid+i]);
    l.push_back(make_pair('S', INF));
    r.push_back(make_pair('S', INF));
    int i = 0, j = 0;
    rep(k, left, right){
        if(l[i].second <= r[j].second) a[k] = l[i++];
        else a[k] = r[j++];
    }
    return;
}

void margesort(pair<char, int> a[], int left, int right){
    if(left + 1 < right){
        int mid = (left + right) / 2;
        margesort(a, left, mid);
        margesort(a, mid, right);
        marge(a, left, mid, right);
    }
    return;
}

int Partition(pair<char, int> *a, int p, int r){
    pair<char, int> x = a[r];
    int i = p - 1;
    rep(j, p, r){
        if(a[j].second <= x.second){
            i = i+1;
            swap(a[i], a[j]);
        }
    }
    swap(a[i+1], a[r]);
    return i+1;
}

void quicksort(pair<char, int> *a, int p, int r){
    if(p < r){
        int q = Partition(a, p, r);
        quicksort(a, p, q-1);
        quicksort(a, q+1, r);
    }
}

int main(void){
    int n;
    pair<char, int> a[100000], b[100000];
    bool isstable = true;
    cin >> n;
    rep(i, 0, n) {
        cin >> a[i].first >> a[i].second;
        b[i] = a[i];
    }
    quicksort(a, 0, n-1);
    margesort(b, 0, n);
    
    rep(i, 0, n){
        if(a[i] != b[i]){
            isstable = false;
            break;
        }
    }
    cout << (isstable? "Stable":"Not stable") << endl;
    rep(i, 0, n){
        cout << a[i].first << ' ' << a[i].second << endl;
    }
    return 0;
}


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

char data[10][6] = {
    "*",
    ".,!? ",
    "abc",
    "def",
    "ghi",
    "jkl",
    "mno",
    "pqrs",
    "tuv",
    "wxyz"
};

int data_len[10] = {1,5,3,3,3,3,3,4,3,4};

char out[256][2048],answer[256][2048];

int main(void){
    int k,c;
    scanf("%d",&c);
    for(k=0;k<c;k++){
      int i,j=0,count=-1,length,num,flag=0;
      char input[2048],input_num[2];
      char now;
      scanf("%s",input);
      length = strlen(input);
      input_num[1]='\0';
      now = input[0];
      for(i=0;;i++){
        if(input[i]=='\0') break;
        if(now==input[i]) count++;
        else{
          input_num[0] = now;
          out[k][j] = data[atoi(input_num)][count%data_len[atoi(input_num)]];
          j++;
          count = -1;
          now = input[i];
          i--;
        }
      }
      j = 0;
      for(i=0;i<strlen(out[k]);i++){
        if(out[k][i]!='*'){
          answer[k][j] = out[k][i];
          j++;
      }
      }
    }
    for(k=0;k<c;k++){
        printf("%s\n",answer[k]);
    }
    return 0;
}


Measured memory_bytes: 3000
#include<stdio.h>
#define N 100
#define WHITE 0
#define GRAY 1
#define BLACK 2

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

void dfs(){
  int u;
  for(u=0;u<n;u++)color[u]=WHITE;
  tt=0;
  for(u=0;u<n;u++){
    if(color[u]==WHITE)dfs_visit(u);
  }
  for(u=0;u<n;u++){
    printf("%d %d %d\n",u+1,d[u],f[u]);
  }
}

int main(){
  int u,v,k,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);
    u--;
    for(j=0;j<k;j++){
      scanf("%d",&v);
      v--;
      M[u][v]=1;
    }
  }
  dfs();
  return 0;
}



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

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;

const ll mod = (ll)1e9 + 7;
const ll mxN = 200200;




int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.precision(20);
	cout << fixed;

	ll n,d,x,ans=0;
	cin>>n>>d>>x;
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		ans+=(d+x-1)/x;
	}
	cout<<ans+x;
	
		

}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

struct Node{
    int key;
    struct Node *left;
    struct Node *right;
    struct Node *parent;
};
struct Node *head,*NIL;

int Datanum = 0;
int count = 0;
int exist = 0;

void Insert(int in){
    struct Node *y = NIL;
    struct Node *x = head;
    struct Node *z = malloc(sizeof(struct Node));
    z->key = in;
    z->left = NIL;
    z->right = NIL;

    while(x != NIL){
        y = x;
        if(z->key < x->key) x=x->left;
        else x = x->right;
    }
    z->parent = y;

    if(y==NIL){
        head = z;
    }else if(z->key < y->key){
        y->left = z;
    }else{
        y->right = z;
    }
    
}

void Find(int in,struct Node *ele){
    if(ele == NIL || exist==1)return;
    if(ele->key == in) exist=1;
    else{
        if(in>ele->key) Find(in,ele->right);
        else Find(in,ele->left);
    }
}

void PrintPreorder(struct Node *u){
    if(u == NIL) return;
    printf(" %d",u->key);
    PrintPreorder(u->left);
    PrintPreorder(u->right);
}

void PrintInorder(struct Node *u){
    if(u == NIL) return;
    PrintInorder(u->left);
    printf(" %d",u->key);
    PrintInorder(u->right);
}

int main(){
    int n,i,innum;
    char order[7];

    scanf("%d",&n);
    for(i=0; i<n; i++){
        scanf("%s",order);
        if(order[0] == 'i'){
            scanf("%d",&innum);
            Insert(innum);
            Datanum++;
        }else if(order[0] == 'p'){
            PrintInorder(head);
            printf("\n");
            count = 0;
            PrintPreorder(head);
            printf("\n");
            count = 0;
        }else if(order[0] == 'f'){
            scanf("%d",&innum);
            Find(innum,head);
            if(exist) printf("yes\n");
            else printf("no\n");
            exist = 0;
        }else{
            printf("Invalid Order!!\n");
            exit(1);
        }
    }

    return 0;
}


Measured memory_bytes: 20480
#include <stdio.h>
#include <math.h>

int main(){
   int n,d,i,j,k,x[10][10],l,ls,ans;
    
   scanf("%d %d",&n,&d);
   for (i=0;i<n;i++)
      for (j=0;j<d;j++) scanf("%d",&x[i][j]);
   for (ans=i=0;i<n;i++)
      for (j=i+1;j<n;j++) {
         for (l=k=0;k<d;k++) l+=(x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
         ls=sqrt(l);
         if (ls*ls==l) ans++;
      }
   printf("%d\n",ans);
   return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
bool flag[110];
void solve()
{
	int n,k;
	int cnt=0;
	cin>>n>>k;
	for (int i=1;i<=k;i++)
	{
		int d;
		cin>>d;
		while (d--) 
		{
			int m;
			cin>>m;
			flag[m]=true;
		}
	 } 
	 for (int i=1;i<=n;i++)
	 	if (!flag[i])
	 		cnt++;
	cout<<cnt;
}
int main() 
{
	ios::sync_with_stdio(false);
	solve();
	return 0;
}

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

using namespace std;

int main()
{
    while (true) {
        int x;
        int y;
        int s;
        scanf("%d %d %d", &x, &y, &s);
        if (x == 0 && y == 0 && s == 0) {
            break;
        }

        auto f = [](int n, int y) { return n * (100 + y) / 100; };
        int r = -1;
        for (int i = 1; i < s; ++i) {
            for (int j = 1; j < s; ++j) {
                if (f(i, x) + f(j, x) == s) {
                    r = max(r, f(i, y) + f(j, y));
                }
            }
        }
        printf("%d\n", r);
    }
}

Measured memory_bytes: 2576
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define REP(i, n) for(int i=1; i<=(int)(n); i++)

using namespace std;

int dp[12 + 1][12 + 1];
int C[12];
int A[12][12];
int N, M, X;

int main(){
    bool able = true;
    cin >> N >> M >> X;
    rep(i, N) {
        cin >> C[i];
        rep(j, M) {
            cin >> A[i][j];
        }
    }

    int ans = 1000*1000*1000;
    rep(i, 1 << N) {
        int sum[12] = {0};
        int cost = 0;
        rep(j, N) {
            if ((i >> j) & 1) {
                cost += C[j];
                rep(k, M) {
                    sum[k] += A[j][k];
                }
            } else {
                continue;
            }
        }
        bool ok = true;
        rep(j, M) {
            if (sum[j] < X) {
                ok = false;
            }
        }
        if (ok) ans = min(ans, cost);
    }
    if (ans == 1000*1000*1000) {
        cout << -1 << endl;
        return 0;
    } else {
        cout << ans << endl;
    }
    
    return 0;

}



Measured memory_bytes: 3200
#include <cstdio>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
int main(){
		int N,K,mnum,mm,pnum,pm;
		cin >> N >> K;
		int s[N];
		for(int i=0; i<N ;i++){
				cin >> s[i];
		}
		int min=10E9;
		for(int i=0;i<N-K+1;i++){
				int d;
				d=fmin(fabs(s[i]),fabs(s[K+i-1]))+fabs(s[K+i-1]-s[i]);
				min = fmin(min,d);
		}
		cout << min << endl;
}



Measured memory_bytes: 640
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=3010;
ll f[maxn][maxn][4];
ll a[maxn][maxn];

int main()
{
	int r,c,k;cin>>r>>c>>k;
	while(k--){
		int x,y,z;cin>>x>>y>>z;
		a[x][y]=z;
	}
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			for(int t=1;t<=3;t++){
				f[i][j][t]=max(f[i-1][j][3]+a[i][j],max(f[i][j-1][t],f[i][j-1][t-1]+a[i][j]));
			}
		}
	}
	cout<<f[r][c][3];
	return 0;	
} 

Measured memory_bytes: 355756
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

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 (b<a) { a=b; return 1; } return 0; }

void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
//--------------------------------------------------------------------------------//

int main() {
    init();
    ll N;
    cin >> N;
    vl A(N), B(N);
    vc<pair<ll, ll>> C(N);
    rep(i,N){
        ll a, b;
        cin >> a >> b;
        A[i] = a, B[i] = b, C[i] = {a + b, i};
    }
    sort(all(C));
    reverse(all(C));

    ll ans = 0;
    rep(i,N){
        ll sum, k;
        tie(sum, k) = C[i];
        if(i%2==0){
            ans += A[k];
        }else{
            ans -= B[k];
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3456
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(v) v.begin(), v.end()

using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
const double PI = 2 * acos(0);
int dp[212121];

int main() {
	int ans = 0;
	LL N;
	scanf("%lld", &N);
	for (int i = 1; i <= sqrt(N); i++) {
		if (N % i == 0) {
			LL x = N / i;
			if (i > 1) {
				LL tmp = N;
				while (tmp % i == 0) tmp /= i;
				if (tmp % i == 1) ans++;
			}
			if (i != x) {
				LL tmp = N;
				while (tmp % x == 0) tmp /= x;
				if (tmp % x == 1) ans++;
			}
		}
	}
	LL selisih = N - 1;
	LL x = selisih;
	int faktor = 1;
	for (int i = 2; i <= sqrt(selisih); i++) {
		if (x == 1) break;
		int cnt = 0;
		while (x % i == 0) x /= i, cnt++;
		faktor *= (cnt + 1);
	}
	if (x > 1) faktor *= 2;
	ans += (faktor - 1);
	printf("%d\n", ans);
}

Measured memory_bytes: 384
/* 
Hey there stalker....
 
█▀▀▄░░░░░░░░░░░▄▀▀█
░█░░░▀▄░▄▄▄▄▄░▄▀░░░█
░░▀▄░░░▀░░░░░▀░░░▄▀
░░░░▌░▄▄░░░▄▄░▐▀▀
░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█ 
░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█
▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█
█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀
░▀▄░░▀░░▀▀▀░░▀░░░▄█▀
░░░█░░░░░░░░░░░▄▀▄░▀▄
░░░█░░░░░░░░░▄▀█░░█░░█
░░░█░░░░░░░░░░░█▄█░░▄▀
░░░█░░░░░░░░░░░████▀
░░░▀▄▄▀▀▄▄▀▀▄▄▄█▀
 
Like what you see?
*/
#include <bits/stdc++.h>
using namespace std;
 
#define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define lli long long int
#define p_b push_back
#define m_p make_pair
#define fs first
#define sc second
#define all(x) x.begin(),x.end()
#define fr(i,a,n) for(lli i=a; i<n; i++)
#define rep(i,a,n) for(lli i=a; i>=n; i--)
const lli mod = 1e9 + 7;
/*****************************************************/
lli n,w,wt[105],v[105],dp[105][100005]={0};
int main()
{
    fastIO
    cin>>n>>w;
    lli i,j,valmax=0;
    for(i=1;i<=n;i++)
        cin>>wt[i]>>v[i];
    for(i=1;i<=n;i++)
    {
        for(j=0;j<100003;j++)
        {
            if(j==0)
                dp[i][j]=0;
            if(v[i]>j)
                dp[i][j]=dp[i-1][j];
            if(v[i]==j)
            {
                dp[i][j]=wt[i];
                if(dp[i-1][j]!=0)
                    dp[i][j]=min(dp[i][j],dp[i-1][j]);
            }
            if(v[i]<j)
            {
                if(dp[i-1][j-v[i]]==0)
                    dp[i][j]=dp[i-1][j];
                else
                {
                    dp[i][j]=wt[i]+dp[i-1][j-v[i]];
                    if(dp[i][j]>w)
                        dp[i][j]=dp[i-1][j];
                    else if(dp[i-1][j]!=0)
                        dp[i][j]=min(dp[i][j],dp[i-1][j]);
                }
            }
            if(dp[i][j]!=0)
                valmax=max(valmax,j);
        }
    }
    cout<<valmax;
    //cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
    return 0;
}

Measured memory_bytes: 79360
#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("-1"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define bn(x) ((1<<x)-1)
#define newline puts("")
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;



int main() {
  int n;
  scanf("%d",&n);
  vl a(n);
  cin>>a;
  rep(i,n-1) a[i+1] += a[i];
  rep(i,n) if (i&1) a[i] = -a[i];
  ll ans = LINF;
  rep(ti,2) {
    ll now = 0, t = 0;
    rep(i,n) {
      ll x = max(a[i]+t+1,0ll);
      now += x;
      t = x-t;
    }
    rep(i,n) a[i] = -a[i];
    mins(ans,now);
  }
  cout<<ans<<endl;
  return 0;
}






















Measured memory_bytes: 1024
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
#include <set>
#include <deque>

using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define all(i,n) (i),(i+n)

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;

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

  cin >> n;
  vector<ll> a(n + 1);
  vector<ll> memo(n + 1);

  REP(i, n + 1) cin >> a[i];
  memo[n] = a[n];
  RREP(i, n) {
    memo[i] = memo[i + 1] + a[i];
  }

  ll node = 1;
  REP(i, n + 1) {
    if(node <= 0) {
      cout << -1 << endl;
      return 0;
    }

    if(node < memo[i]) memo[i] = node;
    if(memo[i] < node) node = memo[i];

    node = 2 * (node - a[i]);
    if(i == n && node != 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  ll ans = 0;
  REP(i, n + 1) ans += memo[i];

  cout << ans << endl;
}


Measured memory_bytes: 4748