akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <deque>
#include <stack>
#include <iomanip>
#include <cmath>


using namespace std;


typedef long long ll;
typedef long double ld;


const ll MOD = 1000000007ll;


ll modSum(ll a, ll b) {
  return ((a % MOD) + (b % MOD)) % MOD;
}


ll modSubtract(ll a, ll b) {
  return (((a % MOD) - (b % MOD)) + MOD + MOD) % MOD;
}


ll modProd(ll a, ll b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}


ll getPowMod(ll x, ll e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPowMod(x, e/2);
    return modProd(tmp, tmp);
  } else {
    ll tmp = getPowMod(x, e-1);
    return modProd(tmp, x);
  }
}


ll getPow(ll x, ll e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPow(x, e/2);
    return tmp * tmp;
  } else {
    ll tmp = getPow(x, e-1);
    return tmp * x;
  }
}


ll getInverse(ll x) {
  return getPowMod(x, MOD-2);
}


bool isEven(ll x) {
  ll tmp = ((x % 2) + 2) % 2;
  return tmp == 0;
}


ll getSumOfDigitsInBase(ll n, ll b) {
  ll ret = 0;
  while (n > 0) {
    ret += n % b;
    n /= b;
  }
  return ret;
}


vector<int> getKMP(string &s) {
  int len = (int)s.size();
  vector<int> ret (len, 0);
  for (int i = 1 ; i < len ; i++) {
    int at = ret[i-1];
    while (at > 0 && s[i] != s[at]) {
      at = ret[at-1];
    }
    if (s[i] == s[at]) {
      at++;
    }
    ret[i] = at;
  }
  return ret;
}


string getSubstring(string &s, int from, int to) {
  int l = to-from+1;
  if (l <= 0) {
    return "";
  }
  return s.substr(from, l);
}

ll gcd(ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0;
        y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}














string s;


void input() {
  cin >> s;
}


void preprocess() {
}


void solve() {
  int ans = 0;
  int cnt = 0;
  for (char ch : s) {
    if (ch == 'g') {
      if (cnt > 0) {
        ans++;
        cnt--;
      } else {
        cnt++;
      }
    } else {
      if (cnt > 0) {
        cnt--;
      } else {
        ans--;
        cnt++;
      }
    }
  }
  cout << ans;
}


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

  int T;
//  cin >> T;
  T = 1;

  for (int i = 0 ; i < T ; i++) {
    input();
    preprocess();
    solve();
  }

  return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m;
  cin>>n>>m;
  vector<vector<int>> km(m);
  for(int i=0;i<m;i++){
      int k;
        cin>>k;
    km[i].resize(k);
    for(int j=0;j<k;j++){
        cin>>km[i][j];
        km[i][j]--;
    }
  }
  vector<int> p(m);
  for(int i=0;i<m;i++){
    cin>>p[i];
  }
  int cmb = 0;
  for(int i=0;i<(1<<n);i++){
       // cout<<"adfa"<<endl;

        bool cortCombi = true;
    for(int j=0;j<m;j++){
         int cnt = 0;
        for(int swi:km[j]){
            if((i>>swi)&1){
               cnt++;
            }
        }
        cnt%=2;
        if(cnt!=p[j]){
            cortCombi = false;
        }
    }
    if(cortCombi){
            cmb++;
        }
  }
  cout<<cmb<<endl;

  return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

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

	int N;
	cin >> N;
	vi A(N), B(N);
	for(int i = 0; i < N; ++i) cin >> A[i] >> B[i];
	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	if(N % 2 == 0) cout << 1 + B[N/2] + B[N/2-1] - A[N/2] - A[N/2-1] << "\n";
	else cout << 1 + B[N/2] - A[N/2] << "\n";

	return 0;
}

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

using namespace std;

int solve(string s)
{
    const int M = 1e9 + 7;
    int n = (int) s.size();
    vector <int> go(n);
    for (int i = n - 1; i >= 0; i--)
    {
        if (i == n - 1 || s[i] == s[i + 1])
        {
            go[i] = i + 1;
        }
        else
        {
            go[i] = go[i + 1];
        }
    }
    if (go[0] == n)
    {
        return 1;
    }
    vector <int> dp(n + 1);
    vector <int> pref(n);
    int sm = 0;
    for (int i = 0; i < n; i++)
    {
        sm += (s[i] - 'a' + 1);
        if (sm >= 3) sm -= 3;
        pref[i] = sm;
    }
    vector <vector <int> > g(n + 1, vector <int> (3, n));
    for (int i = n - 1; i >= 0; i--)
    {
        g[i] = g[i + 1];
        g[i][pref[i]] = i;
    }
    int ans = 0;
    dp[0] = 1;
    for (int i = 0; i < n; i++)
    {
        dp[i + 1] += dp[i];
        if (dp[i + 1] >= M) dp[i + 1] -= M;
        int cur = (s[i] == 'a' ? 2 : 1);
        int ret = go[i];
        int need = ((i ? pref[i - 1] : 0) + cur) % 3;
        int where = g[ret][need];
        if (where + 1 <= n)
        {
            dp[where + 1] += dp[i];
            if (dp[where + 1] >= M) dp[where + 1] -= M;
        }
        if (i != 0 && pref[i - 1] == pref[n - 1])
        {
            ans += dp[i];
            if (ans >= M) ans -= M;
        }
    }
    ans += dp[n];
    if (ans >= M) ans -= M;
    return ans;
}

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif // ONPC
    string s;
    while (cin >> s)
    {
        cout << solve(s) << '\n';
    }
}


Measured memory_bytes: 7040
def resolve():
    N = int(input())
    if N < 3:
        print(0)
        return
    L = list(sorted([int(i) for i in input().split()]))
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if L[i] == L[j]:
                continue
            for k in range(j + 1, N):
                if L[j] == L[k]:
                    continue
                if L[i] + L[j] <= L[k]:
                    break
                ans += 1
    print(ans)

resolve()


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

int main(){
    while(1){
        int n, x, s = 0;
        cin >> n >> x;
        if(n == 0 && x == 0) break;
        for(int i = 1 ; i <= n ; i++){
            for(int j = i + 1 ; j <= n ; j++){
                for (int k = j + 1 ; k <= n ; k++){
                    if(i + j + k == x) s++;
                }
            }
        }
        cout << s << endl;
    }
    return 0;
}


Measured memory_bytes: 3092
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S, T;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;
ll V;
vec a(0);

bool ok(ll c){
    if(c == K - 1) return true;
    ll b = a[c] + M, rest = M * (V - K);
    if(rest <= 0) return b >= a[K-1];
    reps(i,K - 1,N){
        if(i == c) continue;
        rest -= min(M, b - a[i]);
    }
    return rest <= 0 && b >= a[K-1];
}
int main(){
    cin>>N>>M>>V>>K;
    rep(i,N) {
        cin>>A;
        a.push_back(A);
    }
    sort(ALL(a));
    reverse(ALL(a));
    ll lb = K - 1, ub = N;
    while(ub - lb > 1){
        ll cen = (ub + lb) /2;
        (ok(cen) ? lb : ub) = cen;
        //cout<<lb<<' '<<ub<<endl;
    }
    //rep(i,N) cout<<ord[i]<<' '<<a[i]<<endl;
    ll border = a[lb], ans = 0;
    //cout<<border<<endl;
    rep(i,N){
        ans += (a[i] >= border);
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 1400
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<string>
using namespace std;

int main(void) {
	int n;
	cin >> n;
	vector<int> p(n);
	int i;
	int count = 0;
	for (i = 0; i < n; i++)
	{
		cin >> p[i];
	}

	for (i = 1; i < n-1; i++)
	{
		if ((p[i - 1] < p[i] && p[i] < p[i+ 1])||(p[i+1]<p[i]&&p[i]<p[i-1]))
		{
			count++;
		}
	}

	cout << count << endl;
	return 0;
}

Measured memory_bytes: 256
s = int(input())
def f(n):
  if n%2 == 0:
    return n/2
  else:
    return 3*n+1
a = []
a.append(s)
for i in range(1,1000001):
  if f(a[i-1]) in a:
    print(i+1)
    break
  else:
    a.append(f(a[i-1]))
  

Measured memory_bytes: 2940
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>

#define FOR(idx, begin, end) for(int idx = (int)(begin); idx < (int)(end); ++idx)

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

using namespace std;
typedef long long lint;

const int MOD = 1000000007;
const int INF = 1111111111;
const double EPS = 1e-9;

int main() {

	cin.tie(0);

	string odd, even;
	cin >> odd >> even;

	FOR(i, 0, odd.size()) cout << odd[i] << (((i == odd.size() - 1) and (odd.size() != even.size())) ? ' ' : even[i]);

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define int ll
#define MOD (ll)(1e9+7)
using namespace std;
using ll=long long;
using pii=pair<int,int>;

int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }
int lcm(int a,int b) { return a/gcd(a,b)*b; }

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n; cin>>n;
	vector<int> v(n);
	for(auto &i:v) cin>>i;
	sort(all(v));
	cout<<v[n/2]-v[n/2-1]<<"\n";
	return 0;
}















Measured memory_bytes: 1024
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.Scanner;
import java.util.OptionalInt;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BBitterAlchemy solver = new BBitterAlchemy();
        solver.solve(1, in, out);
        out.close();
    }

    static class BBitterAlchemy {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt(), x = in.nextInt();
            int[] m = IntStream.range(0, n).map(i -> in.nextInt()).toArray();
            int min = Arrays.stream(m).min().orElse(0);
            int sum = Arrays.stream(m).sum();
            out.println((x - sum) / min + n);
        }

    }
}



Measured memory_bytes: 28756
import java.io.*;
import java.util.*;
class Main{
    public static void main(String[] args) throws Exception {
		hayami saori = new hayami();
		int N = saori.saori_hayami();
		HashMap<String,Integer> x = new HashMap<>();
		String str;
		int count = 0;
		int heigh = 1;
		for(int i = 0;i < N;i++){
			str = saori.nextHayami();
			if(x.containsKey(str)){
				count = x.get(str);
				count++;
				heigh = Math.max(heigh, count);
				x.put(str,count);
			} else {
				x.put(str, 1);
			}
		}
		ArrayList<String> ans =new ArrayList<>();
		for(Map.Entry<String, Integer> map : x.entrySet()) {
            if(map.getValue() == heigh){
				ans.add(map.getKey());
			}
		}
		Collections.sort(ans);
		for(String s : ans){
            System.out.println(s);
        }
		saori.close();
	}
}



class hayami implements Closeable {
	private final InputStream in = System.in;
	private final byte[] hayami = new byte[1024];
	private int Hayami = 0;
	private int saori = 0;
	private boolean HayamiSaori() {
		if (Hayami < saori) {
			return true;
		}else{
			Hayami = 0;
			try {
				saori = in.read(hayami);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (saori <= 0) {
				return false;
			}
		}
		return true;
	}
	private int SaoriHayami() { 
		if (HayamiSaori()) {
            return hayami[Hayami++];
         }else{
             return -1;
         }
	}
	private static boolean hayami_saori(int hayami) { 
		return 33 <= hayami && hayami <= 126;
	}
	public boolean hayamisaori() { 
		while(HayamiSaori() && !hayami_saori(hayami[Hayami])) Hayami++; return HayamiSaori();
	}
	public String nextHayami() {
		if (!hayamisaori()) throw new NoSuchElementException();
		StringBuilder hayamin = new StringBuilder();
		int saori = SaoriHayami();
		while(hayami_saori(saori)) {
			hayamin.appendCodePoint(saori);
			saori = SaoriHayami();
		}
		return hayamin.toString();
	}
	public long saorihayami() {//nextLong
		if (!hayamisaori()) throw new NoSuchElementException();
		long hayami = 0;
		boolean misao = false;
		int saori = SaoriHayami();
		if (saori == '-') {
			misao = true;
			saori = SaoriHayami();
		}
		if (saori < '0' || '9' < saori) {
			throw new NumberFormatException();
		}
		while(true){
			if ('0' <= saori && saori <= '9') {
				hayami *= 10;
				hayami += saori - '0';
			}else if(saori == -1 || !hayami_saori(saori)){
				return misao ? -hayami : hayami;
			}else{
				throw new NumberFormatException();
			}
			saori = SaoriHayami();
		}
	}
	public int saori_hayami() {//nextInt
		long hayami = saorihayami();
		if (hayami < Integer.MIN_VALUE || hayami > Integer.MAX_VALUE) throw new NumberFormatException();
		return (int) hayami;
	}
	public double Hayamin() { //nextDouble
		return Double.parseDouble(nextHayami());
	}
	public void close() {
		try {
			in.close();
		} catch (IOException e) {
		}
    }
    
}

Measured memory_bytes: 75700
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace LogicSummoner
{
    class Program
    {
        static void Main(string[] args)
        {
            var line = GetStrings();
            var stack = new Stack<int>();

            foreach (var item in line)
            {
                if (item == "+")
                {
                    var b = stack.Pop();
                    var a = stack.Pop();
                    stack.Push(a + b);
                }
                else if (item == "-")
                {
                    var b = stack.Pop();
                    var a = stack.Pop();
                    stack.Push(a - b);
                }
                else if (item == "*")
                {
                    var b = stack.Pop();
                    var a = stack.Pop();
                    stack.Push(a * b);
                }
                else
                {
                    stack.Push(int.Parse(item));
                }
            }

            System.Console.WriteLine(stack.First());
        }

        //static StreamReader Console = new StreamReader("input.txt");

        static int insertionSort(int[] A, int n, int g, int cnt)
        {
            for (int i = g; i < n; i++)
            {
                var v = A[i];
                var j = i - g;
                while (j >= 0 && A[j] > v)
                {
                    A[j + g] = A[j];
                    j = j - g;
                    cnt++;
                }
                A[j + g] = v;
            }
            return cnt;
        }

        static int shellSort(int[] A, int n, int[] m)
        {
            var cnt = 0;
            for (int i = 0; i < m.Length; i++)
            {
                cnt = insertionSort(A, n, m[i], cnt);
            }
            return cnt;
        }

        static int GetNumber()
        {
            return int.Parse(Console.ReadLine().Trim());
        }

        static double GetDounble()
        {
            return double.Parse(Console.ReadLine().Trim());
        }

        static int[] GetNumbersInt(int n = 0)
        {
            return Console.ReadLine().Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToArray();
        }

        static long[] GetNumbersLong(int n = 0)
        {
            return Console.ReadLine().Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => long.Parse(x)).ToArray();
        }

        static double[] GetNumbersDouble(int n = 0)
        {
            return Console.ReadLine().Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(x => double.Parse(x)).ToArray();
        }

        static string[] GetStrings(int n = 0)
        {
            return Console.ReadLine().Trim().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
        }
    }
}

Measured memory_bytes: 10856
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

ans = 0
for d in range(60):
    zero = 0
    one = 0
    for a in A:
        if ((1 << d) & a) != 0:
            one += 1
        else:
            zero += 1

    ans += one * zero * (1 << d)
    ans %= MOD

print(ans)


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

int main() {
	int x;
	cin >> x;
	if (x < 1200) {
		cout << "ABC" << endl;
	}
	else {
		cout << "ARC" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long n;int con=1;cin>>n;
    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)con=i;
    }
    if(con==1)cout<<n-1;
    else cout<<(con-1)+(n/con-1);
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
#include <cstdio>
#include <tuple>
#include <string>
#include <vector> 
#include <set>
#include <string.h>
#include <map>
#include <cmath>
#include <deque>
using ll = long long;
using Graph = vector<vector<int>>;
#include <queue>
#include <stack>
#include <bitset>
#include <stdlib.h>


//動的計画
//最小化問題
// template<class T>
// bool chmin(T& a,T b){if(a >  b) {a = b;return true;}return false;}
template<class T> 
bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int MAX_INDEX = 100010;
ll dp[MAX_INDEX];
const ll INF = 1LL << 60;
int main(){
    
    int N;
    int K;
    cin >> N >> K;

    long long h[MAX_INDEX];
    for(int i=0;i < N;i++) cin >> h[i];

    //初期化
    for(int i = 0;i < MAX_INDEX;i++) dp[i] = INF;

    //初期条件
    dp[0] =  0;

    for(int i = 0;i< N;i++){
        for(int j = 1;j <= K;j++){
            //k個目の点からiに移動した場合。最短なら更新
             chmin(dp[i + j],abs(h[i + j] - h[i]) + dp[i]);

        }

        
    
    }

    cout << dp[N - 1] << endl;

    return 0;

}

Measured memory_bytes: 1792
import java.io.*;
import java.math.*;
import java.util.*;

class Main {
    private static final Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int[] nums = Arrays.stream(sc.nextLine().split(" +")).mapToInt(Integer::parseInt).toArray();
        Arrays.sort(nums);
        System.out.println(nums[0]+nums[1]+10*nums[2]);
        sc.close();
    }
}


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

int main() {
  int N;
  cin >> N;
  vector<int> V(N);
  vector<int> C(N);
  rep(i, N) cin >> V[i];
  rep(i, N) cin >> C[i];
  int ans = 0;
  rep(i, N) {
    if (V[i] <= C[i]) continue;
    ans += V[i] - C[i];
  }
  cout << ans << endl;
}

Measured memory_bytes: 3624
a,b,c,x,y = map(int, input().split())

costs = 0

while x>0 and y>0:
  if a+b>2*c:
    costs += 2*c
  else:
    costs += a+b
  x -= 1
  y -= 1
  
if x>0:
  if a<=2*c:
    costs += x*a
  else:
    costs += x*2*c
    
if y>0:
  if b<=2*c:
    costs += y*b
  else:
    costs += y*2*c
  
print(costs)

Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <cctype>
#include <cmath>
#define rep(i, m, n) for(int i = m;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define ll long long
#define ull unsigned long long
#define push(a) push_back(a)
#define pop(a) pop_back(a)
#define debug(x) cout<<#x<<": "<<x<<endl
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

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

int main(){
  string w;
  int a[27]={};
  bool flag=true;
  cin>>w;
  rep(i,0,w.size()){
    a[w[i]-'a']++;
  }

  rep(i,0,27){
    if(a[i]%2!=0)flag=false;
  }
  if(flag){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,r;
    cin>>a>>b>>c>>d>>r;
    if(a-c<r||c<r||d<r)
        cout<<"No"<<endl;
    else if(b-d<r)
        cout<<"No"<<endl;
    else
        cout<<"Yes"<<endl;
    return 0;
}



Measured memory_bytes: 3100
#include <iostream>

int main(){
    int a,b,c,d;
    std::cin >> a >> b >> c >> d;
    std::string ans="Yes";
    
    int j=a;
    
    for(int i=c-b;i>0;i-=b){
        j-=d;
        if(j<=0){
            ans="No";
        }
    }
    
    std::cout << ans << std::endl;
    return 0;
}

Measured memory_bytes: 3624
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
class UnionFind {
public:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : a(x, -1), siz(x) {}

  int root(int x) {
    return a[x] < 0 ? x : a[x] = root(a[x]);
  }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    siz--;
    if (a[x] > a[y]) swap(x, y);
    a[x] += a[y];
    a[y] =x;
  }

  bool same(int x, int y) {
    return root(x) == root(y);
  }

  int size(int x) {
    return -a[root(x)];
  }
};
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    uf.unite(u, v);
  }
  vector<int> root;
  vector<vector<int>> forest(n);
  for (int i = 0; i < n; i++) {
    if (forest[uf.root(i)].empty()) root.push_back(uf.root(i));
    forest[uf.root(i)].push_back(a[i]);
  }
  if (n + 2 < (int) root.size() * 2) {
    cout << "Impossible" << endl;
    return 0;
  }
  if (m == n - 1) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  for (int i : root) {
    sort(forest[i].rbegin(), forest[i].rend());
    ans += forest[i].back();
    forest[i].pop_back();
  }
  vector<int> rem;
  for (int i : root) {
    if (forest[i].empty()) continue;
    for (int j : forest[i]) {
      rem.push_back(j);
    }
  }
  sort(rem.begin(), rem.end());
  int need = (n - 1) - m;
  if ((int) root.size() == 0) ans = 0;
  for (int i = 0; i < (need * 2) - (int) root.size(); i++) {
    ans += rem[i];
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  long X, A = 1, P;
  cin >> X;
  
  int i = 0;
  while(true) {
    i++;
    P = pow(2, i);
    if (P <= X) {
      A += P;
    } else {
      break;
    }
  }
  
  cout << A << endl;
}


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

using namespace std;


int main() {
	int N, K;
	cin >> N >> K;
	vector<int> h(N+1);
	for(int i = 1; i <= N; i++) {
		cin >> h[i];
	}

	vector<int> c(N+1, 0);

	c[0] = -1;
	c[1] = 0;

	int m;
	for(int i = 2; i <= N; i++) {
		c[i] = INT_MAX;
		for(int j = i-1; j >= i-K and j > 0; j--) {
			c[i] = min(c[i], c[j] + abs(h[i] - h[j]));
		}
	}
	
	cout << c[N] << endl;

	return 0;
}

Measured memory_bytes: 1024
S=input()
l=len(S)
mod=10**9+7
dp=[[0 for j in range(13)] for i in range(l+1)]
dp[0][0]=1

for i in range(l):
  if S[i]=='?':
    for k in range(13): 
      for j in range(10):      
        dp[i+1][(k*10+j)%13]+=dp[i][k]%mod
  else:
    for k in range(13):
      dp[i+1][(k*10 + int(S[i]))%13]+=dp[i][k]%mod
      
print(dp[-1][5]%mod)

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

const int INF = 1001001001;
const int NV=100100;

int main() {
  int64_t a,b,c,ab,n,ans;
  bool h=true;
  string str;
  cin>>a>>b>>c;
  ab=a*b;
  n=1;
  while(h){
    if(n*n<=4*ab){
      n+=1;
    }
    else{
      h=false;
      n-=1;
      break;
    }
  }
  ans=c-a-b-n;
  
  if(ans>0){
    str="Yes";
  }
  else{
    str="No";
  }
//  cout<<ans<<","<<n<<endl;
  cout<<str<<endl;
}

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

using namespace std;

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

#define int ll
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define in(x, a, b) a <= x && x < b
const int inf = 1000000001;
const ll INF = 2e18;
//const ll MOD = 1000000007;
//const ll mod = 1000000009;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p <<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };


ll n, MOD;
const int MAXN = 3010;
vl ways(MAXN);
vll ways2(MAXN, vl(MAXN));

vll secondStirling(MAXN, vl(MAXN, 0));
vl fact(MAXN);
vl rfact(MAXN);

ll mod_pow(ll x, ll p, ll M = MOD) {
	ll a = 1;
	while (p) {
		if (p % 2)
			a = a*x%M;
		x = x*x%M;
		p /= 2;
	}
	return a;
}

ll mod_inverse(ll a, ll M = MOD) {
	return mod_pow(a, M - 2, M);
}

void set_fact(ll n, ll M = MOD) {
	fact[0] = fact[1] = rfact[0] = rfact[1] = 1;
	for (ll i = 2; i <= n; i++) {
		fact[i] = i * fact[i - 1] % M;
		rfact[i] = mod_inverse(fact[i], M);
	}
}

ll nCr(ll n, ll r, ll M = MOD) {
	ll ret = fact[n];
	ret = (ret*rfact[r]) % M;
	ret = (ret*rfact[n - r]) % M;
	return ret;
}



void setSecondStirling(ll n, ll M = MOD) {
	secondStirling[0][0] = 1;
	rep1(i, n) {
		rep(j, i + 1) {
			if (j == i) {
				secondStirling[i][j] = 1;
			}
			else if (j == 0) {
				secondStirling[i][j] = 1;
			}
			else {
				secondStirling[i][j] = (secondStirling[i - 1][j - 1] + (ll)(j + 1)*secondStirling[i - 1][j] % M) % M;
			}
		}
	}
}

vl memo1(10000000, -1), memo2(10000000, -1);

signed main() {
	cin >> n >> MOD;
	setSecondStirling(n);
	set_fact(n);
	rep(i, n + 1) {
		rep(j, i + 1) {
			ll temp = secondStirling[i][j];
			if (memo1[(n - i)*j] == -1) {
				memo1[(n - i)*j] = mod_pow(2, (n - i)*j);
			}
			temp *= memo1[(n - i)*j];
			temp %= MOD;
			if (memo2[n - i] == -1) {
				memo2[n - i] = mod_pow(2, n - i, MOD - 1);
			}
			temp *= mod_pow(2, memo2[n - i]);
			temp %= MOD;
			ways2[i][j] = temp;
		}
		//cout << endl;
	}
	rep(i, n + 1) {
		//DEBUG(i);
		rep(j, i + 1) {
			ways[i] += ways2[i][j];
			ways[i] %= MOD;
		}
		//DEBUG(ways[i]);
	}
	ll ans = 0;
	ll fugou = 1;
	rep(i, n + 1) {
		ans += fugou*nCr(n, i) % MOD *ways[i] % MOD;
		ans %= MOD;
		fugou *= -1;
	}
	while (ans < 0) {
		ans += MOD;
	}
	cout << ans << endl;
}

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

const int maxn=500004, sq=707;
int n, q, a[maxn], cnt[maxn];
pair<pair<int,int>,int>query[maxn];
int ans[maxn];

bool cmp(pair<pair<int,int>,int>aa, pair<pair<int,int>,int>bb){
	if (aa.first.first/sq<bb.first.first/sq) return true;
	if (aa.first.first/sq>bb.first.first/sq) return false;
	return aa.first.second<bb.first.second;
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> n >> q;
	for (int i=0;i<n;i++) cin >> a[i];
	for (int i=0;i<q;i++){
		cin >> query[i].first.first >> query[i].first.second;
		query[i].second=i;
	}
	sort(query,query+q,cmp);
	memset(cnt,0,sizeof cnt);
	cnt[a[0]]=1;
	int l=0, r=0, tmp=1;
	for (int i=0;i<q;i++){
		int ll=query[i].first.first-1, rr=query[i].first.second-1;
		if (r<rr){
			for (int j=r+1;j<=rr;j++){
				if (cnt[a[j]]==0) tmp++;
				cnt[a[j]]++;
			}
		}
		else if (r>rr){
			for (int j=r;j>rr;j--){
				cnt[a[j]]--;
				if (cnt[a[j]]==0) tmp--;
			}
		}
		if (l<ll){
			for (int j=l;j<ll;j++){
				cnt[a[j]]--;
				if (cnt[a[j]]==0) tmp--;
			}
		}
		else if (l>ll){
			for (int j=l-1;j>=ll;j--){
				if (cnt[a[j]]==0) tmp++;
				cnt[a[j]]++;
			}
		}
		r=rr;
		l=ll;
//		cout << "(l,r)=(" << l+1 << "," << r+1 << "), ans=" << tmp << "\n";
		ans[query[i].second]=tmp;
	}
	for (int i=0;i<q;i++) cout << ans[i] << "\n";
}

Measured memory_bytes: 15464
parseInt(x) = parse(Int,x)

function main()
	k,s = map(parseInt, split(readline()))
	if s <= k
		println(div((s+1)*(s+2),2))
	elseif s <= 2*k
		println((k+1)*(k+1)-div((s-k)*(s-k+1),2)-div((2*k-s)*(2*k-s+1),2))
	else
		println(div((3*k-s+1)*(3*k-s+2),2))
	end
end

main()

Measured memory_bytes: 109020
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mod (long long)(1e9+7)
int dp[2001][2001];
int fac[2001*2001],ifac[2001*2001];
int fastpow(int x,int y){
    int res=1;
    while(y){
        if(y&1)res=res*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return res;
}
void init(int top){
    fac[0]=1;
    for(int i=1;i<=top;++i)fac[i]=(fac[i-1]*i)%mod;
    ifac[0]=ifac[1]=1;
    ifac[top]=fastpow(fac[top],mod-2);
    for(int i=top-1;i>1;--i)ifac[i]=(ifac[i+1]*(i+1))%mod;
}
int C(int n,int m){
    return ((fac[n]*ifac[m])%mod*ifac[n-m])%mod;
}
signed main(){
    int n,k;
    scanf("%d%d",&n,&k);
    if(k==1){
        puts("1");
        exit(0);
    }
    init(n*k);
    k--;
    dp[0][0]=1;
    for(int i=0;i<=n;++i){
        for(int j=i;j<=n;++j){
            if(i)dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;
            if(j)dp[i][j]=(dp[i][j]+(dp[i][j-1]*C(i+(j-1)*k+k-1,k-1))%mod)%mod;
        }
    }
    printf("%lld\n",(dp[n][n]*fac[n])%mod);
}


Measured memory_bytes: 92928
#include <stdio.h>
#include <math.h>
int n;
int x[10],y[10];
int visit[10];
int cnt;
double dis;
double dist(int a,int b){
	return sqrt((double)((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])));
}
double f(int x,int y){
	printf("%d %d\n",x,y);
	double res = 0;
	if(y==n){
		cnt++;
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(visit[i]==0){
			visit[i] = 1;
			res+= dist(x,i)+f(i,y+1);
			visit[i] = 0;
		}
	}
	return res;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d %d",&x[i],&y[i]);
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			dis+=dist(i,j)*2;
		}
	}
	printf("%lf\n",dis/(double)n);
}

Measured memory_bytes: 2164
import java.util.*;

public class Main {

    static  boolean[] visited;
    static boolean[][] adjMatrix;
    static int res;

    static {
        res = 0;
    }

    static boolean visitedAll() {
        for (boolean b : visited)
            if (!b)
                return false;
        return true;
    }

    static void dfsSolve(int vertex) {
        visited[vertex] = true;
        for (int nextVertex = 1; nextVertex < adjMatrix[vertex].length; ++nextVertex) {
            if (adjMatrix[vertex][nextVertex] && !visited[nextVertex]) {
                dfsSolve(nextVertex);
            }
        }
        if (visitedAll())
            res += 1;
        visited[vertex] = false;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        visited = new boolean[n + 1];
        visited[0] = true; // indexing from 1
        adjMatrix = new boolean[n + 1][n + 1];
        for (int i = 0; i < m; ++i) {
            int a = in.nextInt();
            int b = in.nextInt();
            adjMatrix[a][b] = true;
            adjMatrix[b][a] = true;
        }
        dfsSolve(1);
        System.out.println(res);
    }
}


Measured memory_bytes: 22100
#include <iostream>
#include <cstdio>
#include <algorithm>
#define F first
#define S second
#define PLL pair<ll, ll>

using namespace std;

typedef long long ll;

ll mn = 4e18;
ll n;
PLL a[100005];
ll ans[100005];

int main()
{
    scanf("%lld", &n);
    for(ll i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i].F);
        a[i].S = i;
    }
    sort(a + 1, a + n + 1, greater<PLL >());
    for(ll i = 1; i <= n; i++)
    {
        mn = min(mn, a[i].S);
        ans[mn] += i * (a[i].F - a[i + 1].F);
    }
    for(ll i = 1; i <= n; i++)
    {
        printf("%lld\n", ans[i]);
    }
    return 0;
}


Measured memory_bytes: 3456
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int dir[2][2]={
   0,1,
   1,0
};
char vp[10][10];int flag;
 int h,w;
 int vis[10][10];
struct node{
    int x,y;
};
queue<node>q;
void bfs()
{
    while(!q.empty())
    {
        int sum=0;
        node temp,k;
        temp=q.front();
        q.pop();
        if(temp.x==h&&temp.y==w)
            flag=1;
        for(int i=0;i<2;i++)
        {
            if(vp[temp.x+dir[i][0]][temp.y+dir[i][1]]=='#')
            {
                k.x=temp.x+dir[i][0];
                k.y=temp.y+dir[i][1];
                q.push(k);
                vis[k.x][k.y]=1;
                sum++;
            }
        }
        if(sum==2)
        {
            flag=0;
            break;
        }
    }

}
int main()
{
    scanf("%d %d ",&h,&w);
    node temp;
    temp.x=temp.y=1;
    vis[1][1]=1;
    q.push(temp);
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
            scanf("%c",&vp[i][j]);
        getchar();
    }
    bfs();
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            if(vp[i][j]=='#'&&vis[i][j]==0)
            {
                flag=0;
            }
        }
        if(flag==0)
            break;
    }
    if(flag==1)
    {
        printf("Possible");
    }

    else
        printf("Impossible");
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define ld long double
#define tc() ll t;cin>>t;while(t--)
#define inp(n) ll n;cin>>(n)
#define inp2(n,m) ll n,m;cin>>(n);cin>>(m)
#define inp3(n,m,g) ll n,m,g;cin>>(n);cin>>(m);cin>>(g)
#define inps(s) string s;cin>>(s)
#define fr(i,n) for(ll i=0;i<n;i++)
#define rf(n,i) for(ll i=n-1;i>=0;i--)
#define vi vector<int >
#define vl vector<ll>
#define vd vector<ld>
#define vs vector<string >
#define pii pair<int,int>
#define pci pair<char ,int>
#define pic pair<int ,char>
#define psi pair<string ,int>
#define mii map<int ,int >
#define mci map<char ,int>
#define msi map<string ,int>
#define si set<int >
#define ss set<string>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007

int main() {
    fast
    ll n;
    cin>>n;
    ll ans = 0;
    for(ll i=1;i<=n;i++)
    {
       ans+=n/i;
       if(n%i==0)
       {
           ans--;
       }
    }
    cout<<ans;
	return 0;
}


Measured memory_bytes: 3632
import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] list = new int[n];
        for (int i = 0; i < n; i++) {
            list[i] = sc.nextInt();
        }
        int ans = 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            min = Math.min(list[i], min);
            if (min < list[i]) {
                continue;
            }
            ans++;
        }
        System.out.println(ans);
    }
}

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

int calc(int n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
int main() {
  int n;
  cin>>n;
  if (n % calc(n) == 0) {
    cout<<"Yes"<<endl;
  } else {
    cout<<"No"<<endl;
  }

}

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

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  
  if (abs(a - c) <= d)
    cout << "Yes" << endl;
  else if (abs(a - b) <= d && abs(b - c) <= d)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

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

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n,ans=0;
	cin>>n;
	vl v(n);
	rep(i,n)cin>>v[i];
	ll r=-1;
	ll psum=0,xsum=0;
	rep(i,n){
		if(i!=0){
			psum-=v[i-1];
			xsum^=v[i-1];
		}
		while(r+1<n&&(psum+v[r+1])==(xsum^v[r+1])){
			psum+=v[r+1];
			xsum^=v[r+1];
			r++;
		}
		ans+=r-i+1;
	}
	cout<<ans<<endl;
}


Measured memory_bytes: 1792
def main():
    S = input()
    for bit in range(1 << 3):
        op = []
        for i in range(3):
            if bit & (1 << i):
                op.append("+")
            else:
                op.append("-")
        ans = int(S[0])
        for opc, s in zip(op, S[1:]):
            if opc == "+":
                ans += int(s)
            else:
                ans -= int(s)
        if ans == 7:
            for s, opc in zip(S, op):
                print(s+opc, end="")
            print(S[-1]+"=7")
            return


if __name__ == '__main__':
    main()


Measured memory_bytes: 38384
import java.util.*;
public class Main {
	public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int t = sc.nextInt();
        if ((n % x) > 0) {
            System.out.println(((n / x) + 1) * t);
        } else {
            System.out.println((n / x) * t);
        }
	}
}

Measured memory_bytes: 35656
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
int main() {
  ll x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  vector<ll> p(a), q(b), r(c + x + y);
  rep(i,a)
    cin >> p[i];
  rep(i,b)
    cin >> q[i];
  rep(i,c)
    cin >> r[i];
  sort(p.begin(), p.end(), greater<ll>());
  sort(q.begin(), q.end(), greater<ll>());
  rep(i,x)
    r[i + c] = p[i];
  rep(i,y)
    r[i + c + x] = q[i];
  sort(r.begin(), r.end(), greater<ll>());
  ll sum = 0;
  rep(i,x+y)
    sum += r[i];
  cout << sum;
}


Measured memory_bytes: 3968
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1000000005;
const long double EPS = 1e-10;

struct Edge {
  int f, t, c;

  Edge(int f, int t, int c): f(f), t(t), c(c) {}
};

int main() {
  int l;
  cin >> l;

  int res = 2, n = 1;
  while (res <= l) {
    res *= 2;
    n++;
  }

  vector<Edge> e;
  res /= 4;
  int c = res;
  FOR(i, 1, n) {
    e.push_back(Edge(i, i + 1, c));
    e.push_back(Edge(i, i + 1, 0));
    c /= 2;
  }

  l -= res * 2;
  c = res * 2;
  FOR(i, 2, n + 1) {
    if (l >= res) {
      e.push_back(Edge(1, i, c));
      l -= res;
      c += res;
    }
    res /= 2;
  }

  int m = e.size();
  cout << n << " " << m << endl;
  rep(i, m) {
    cout << e[i].f << " " << e[i].t << " " << e[i].c << endl;
  }


  
  return 0;
}

Measured memory_bytes: 256
while 1:
    n,x=map(int,input().split())
    if n==0: break
    cnt=0
    for i in range(1,n+1):
        for j in range(i+1,n+1):
            for k in range(j+1,n+1):
                if i+j+k==x:
                    cnt+=1
    print(cnt)


Measured memory_bytes: 5592
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
#define yorn(f) puts((f)?"Yes":"No")
ll a, b, c;
//ld ;
//string s;

int main() {
    cin >> a >> b >> c;
    bool flg = (c-a-b>0);
    if(flg){
        ll t1 = (c-a-b)*(c-a-b);
        ll t2 = 4*a*b;
    flg = (t1-t2>0);
    }
    yorn(flg);
    return 0;
}


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

int main( void ) {
    int n;
    string s = "No";

    cin >> n;

    int d1[n],d2[n];

    for(int i=0; i<n; i++){
        cin >> d1[i];
        cin >> d2[i];
    }

    for(int i=0; i<n-2; i++){
        if(d1[i] == d2[i] && d1[i+1] == d2[i+1] && d1[i+2] == d2[i+2]){
            s = "Yes";
        }
    }
    cout << s << endl;
    return 0;
}


Measured memory_bytes: 3620
#include<stdio.h>

int main(){
  int l, r;
  int min = 2018, tmp, tmp1;
  int i, j;

  scanf("%d %d",&l ,&r);

  for(i=l;i<r;i++){
    for(j=i+1;j<=r;j++){
      tmp = ((i%2019)*(j%2019))%2019;
      if(tmp < min){
        min = tmp;
        if(min == 0){
          printf("%d\n",min);
          return 0;
        }
      }
    }
  }

  printf("%d\n",min);
  return 0;
}


Measured memory_bytes: 128
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>

#define f first
#define s second
#define MAX_N 155
#define INF 1000000000

using namespace std;

typedef long long ll;

typedef pair<int,int> P;

int main(void){

  ll n,m,k,a,b;

  while(cin >> n >> m,n|m){
    vector<P>v[MAX_N];
    
    for(int i=1;i<=n;i++){
      cin >> k;
      for(int j=0;j<k;j++){
	cin >> a >> b;
	v[i].push_back(make_pair(a,b));
      }
    }

    ll dp[MAX_N][11][80];

    for(int i=0;i<MAX_N;i++)
      for(int j=0;j<11;j++)
	for(int k=0;k<80;k++)
	  dp[i][j][k]=INF;

    for(int i=0;i<11;i++)
      for(int j=0;j<80;j++)
	dp[0][i][j]=dp[1][i][j]=0;

    for(int i=2;i<=n;i++){
      for(int j=0;j<v[i].size();j++){
	for(int l=m;l>=0;l--){
	  for(int k=0;k<v[i-1].size();k++){
	    dp[i][j][l]=min(dp[i][j][l],dp[i-1][k][l]+(v[i][j].s+v[i-1][k].s)*abs(v[i][j].f-v[i-1][k].f));
	  }
	  if(l<m){
	    if(i==2)dp[i][j][l]=0;
	    for(int p=0;p<v[i-2].size();p++)
	      dp[i][j][l]=min(dp[i][j][l],dp[i-2][p][l+1]+(v[i][j].s+v[i-2][p].s)*abs(v[i][j].f-v[i-2][p].f));
	  }
	}
      }
    }
    
    ll ans=INF;
    /*
    for(int i=0;i<v[n].size();i++){
      for(int j=0;j<=m;j++){
      ans=min(ans,dp[n][i][j]);
      if(j>0)ans=min(ans,dp[n-1][i][j]);
      }
    }
    */
    for(int i=0;i<v[n].size();i++)ans=min(ans,dp[n][i][0]);
    if(m>=1)for(int i=0;i<v[n-1].size();i++)ans=min(ans,dp[n-1][i][1]);

    cout << ans << endl;
  }

  return 0;
}

Measured memory_bytes: 2292
// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define String std::string
#define fi first
#define se second
#define mp std::make_pair
#define Pair std::pair<int, int>
#define Map std::map< int, int >
#define Vector std::vector<int>
#define LEAD (lead && !i)
#define LIM (lim && i == buc[pos])
 
using std::cin;
using std::cout;
using std::endl;
 
typedef long long ll;
typedef unsigned long long ull;
 
const int N = 100000 + 5;
const int M = 1000 + 5;
const int K = 1e7 + 5;
const int MOD = 1e9 + 7;
const int inv2 = 500000004;
const int LOG = 40;
const ull BASE = 19260817;
const int INF = 0x3f3f3f3f;
const int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };
const int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };
// const double PI = acos(-1);
const double PI = 3.14159;

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
 
template<class T>
inline T readT() {
    T res = 0, f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
    for (; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + ch - '0';
    return res * f;
}
#define read readT<int>

namespace Comb{
const int N = 10 + 5;
inline void exgcd(ll a, ll b, ll &g, ll &x, ll &y) {
    if (!b) g = a, x = 1, y = 0;
    else exgcd(b, a % b, g, y, x), y -= x * (a / b);
}
 
inline ll inv(ll a) {
    ll g, res, tmp;
    exgcd(a, MOD, g, res, tmp);
    return ((res % MOD) + MOD) % MOD;
}
 
ll f[N], invFact[N];
 
inline void prepare() {
    f[0] = 1;
    int n = 200000;
    for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % MOD;
    invFact[n] = inv(f[n]);
    for (int i = n - 1; i >= 1; i--) invFact[i] = invFact[i + 1] * (i + 1) % MOD;
}
 
inline ll C(int n, int m) {
    if (!m) return 1;
    if (n == m) return 1;
    if (m > n) return 0;
    if (m < 0) return 0;
    return f[n] * invFact[m] % MOD * invFact[n - m] % MOD;
}
}

int n;
bool flag;
char s[N * 2];
std::stack<int> stk;

int main() {
    flag = true;
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'S') {
            stk.push(i);
        } else {
            if (!stk.empty() && s[stk.top()] == 'S') {
                stk.pop();
            } else {
                stk.push(i);
            }
        }
    }
    cout << stk.size() << endl;
    return 0;
}

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

int zet(int x){
  if(x < 0) x*= (-1);
  return x;
}

int main(){
  int N, L;
  cin >> N >> L;
  int sum = (L - 1)* N + N*(N+1)/2;
  int mini = 1000000;
  int x;
  for(int i = 1; i <= N; i++){
    mini = min(mini, zet(L - 1 + i));
    if(mini == zet(L - 1 + i))x = i;
  }
  cout << sum - L + 1 - x  << endl;
}
  
    

Measured memory_bytes: 3664
package main

import (
	"container/list"
	"fmt"
	"strings"
)

func main() {
	var s string
	var q int
	fmt.Scan(&s, &q)

	l := list.New()
	l.PushFront(s)

	d := 1
	var t, f int
	var c string
	for i := 0; i < q; i++ {
		fmt.Scan(&t)

		switch t {
		case 1:
			d *= -1
		case 2:
			fmt.Scan(&f, &c)

			if d == -1 {
				if f == 1 {
					f = 2
				} else {
					f = 1
				}
			}

			if f == 1 {
				l.PushFront(c)
			} else {
				l.PushBack(c)
			}
		}
	}

	ss := make([]string, l.Len())
	i := 0
	for e := l.Front(); e != nil; e = e.Next() {
		ss[i] = e.Value.(string)
		i++
	}

	ans := strings.Join(ss, "")
	if d == -1 {
		ans = reverse(ans)
	}

	fmt.Println(ans)
}

func reverse(s string) string {
	runes := []rune(s)
	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}


Measured memory_bytes: 24832
#include<cstdio>
#include<algorithm>
using namespace std;
int mod;
int mypow(int a,int b)
{
	int ans=1;
	while(b)
	{
		if(b&1)ans=1ll*ans*a%mod;
		a=1ll*a*a%mod;
		b>>=1;
	}
	return ans;
}
int n,pw2[6005],pw3[6005],ans=0,fac[6005],invfac[6005];
int main()
{
	scanf("%d%d",&n,&mod);n=3*n;
	pw2[0]=pw3[0]=1;
	pw2[1]=mypow(2,mod-2);
	pw3[1]=mypow(3,mod-2);fac[0]=fac[1]=1;
	for(int i=2;i<=n;i++)pw2[i]=1ll*pw2[i-1]*pw2[1]%mod,pw3[i]=1ll*pw3[i-1]*pw3[1]%mod,fac[i]=1ll*fac[i-1]*i%mod;
	invfac[n]=mypow(fac[n],mod-2);
	for(int i=n-1;i>=0;i--)invfac[i]=1ll*invfac[i+1]*(i+1)%mod;
	for(int i=0;i<=n/3;i++)
	{
		for(int j=0;i+j<=n/3;j++)
		{
			ans=(ans+1ll*pw2[i]*pw3[j]%mod*invfac[i]%mod*invfac[j]%mod*invfac[n-2*i-3*j])%mod;
		}
	}
	for(int i=1;i<=n;i++)ans=1ll*ans*i%mod;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 256