akhauriyash/Code-Regression huggingface dataset:
// Standard I/O
#include <iostream>
#include <sstream>
#include <cstdio>
// Standard Library
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
// Template Class
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
// Container Control
#include <algorithm>

using namespace std;

#define rep( i, n ) for( int i = 0; i < n; ++i )
#define irep( i, n ) for( int i = n-1; i >= 0; --i )
#define reep( i, s, n ) for ( int i = s; i < n; ++i )
#define ireep( i, n, s ) for ( int i = n-1; i >= s; --i )
#define foreach(itr, x) for( typeof(x.begin()) itr = x.begin(); itr != x.end(); ++itr)

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all( v ) v.begin(), v.end()
#define fs first
#define sc second
#define vc vector

// for visualizer.html
double SCALE = 1.0;
double OFFSET_X = 0.0;
double OFFSET_Y = 0.0;
#define LINE(x,y,a,b) cerr << "line(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(a) + OFFSET_X << ","										\
	<< SCALE*(b) + OFFSET_Y << ")" << endl;
#define CIRCLE(x,y,r) cerr << "circle(" << SCALE*(x) + OFFSET_X << ","	\
	<< SCALE*(y) + OFFSET_Y << ","										\
	<< SCALE*(r) << ")" << endl;

typedef long long ll;
typedef complex<double> Point;

typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector< vector<int> > vii;
typedef vector< vector<double> > vdd;

typedef vector<int>::iterator vi_itr;

const int IINF = 1 << 28;
const int MOD = 1e9+7;
const double INF = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1.0);

// Direction : L U R D
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1 };

ll calc ( ll n ) {
    ll ans = 0;

    while ( n > 0 ) {
        ans += n%10;
        n /= 10;
    }

    return ans;
}

int main()
{
    ll n;
    cin >> n;

    cout << (n % calc(n) == 0 ? "Yes" : "No") << endl;
}


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

const int MN = 100005;

int N, K, A[MN], Ans;
std::vector<int> G[MN];

int dep[MN], kpar[MN], per[MN], stk[MN], tp;
void DFS(int u) {
	stk[++tp] = u;
	if (dep[u] > K) kpar[u] = stk[tp - K + 1];
	for (int v : G[u]) dep[v] = dep[u] + 1, DFS(v);
	--tp;
}

int del[MN];
void Del(int u) {
	del[u] = 1;
	for (int v : G[u]) if (!del[v]) Del(v);
}

int main() {
	scanf("%d%d", &N, &K);
	for (int i = 1; i <= N; ++i) scanf("%d", &A[i]);
	if (A[1] != 1) A[1] = 1, Ans = 1;
	for (int i = 2; i <= N; ++i) G[A[i]].push_back(i);
	DFS(1);
	for (int i = 1; i <= N; ++i) per[i] = i;
	std::sort(per + 1, per + N + 1, [](int i, int j) { return dep[i] > dep[j]; });
	for (int i = 1; i <= N; ++i) if (kpar[per[i]] && !del[per[i]]) ++Ans, Del(kpar[per[i]]);
	printf("%d\n", Ans);
	return 0;
}


Measured memory_bytes: 15680
import math
a, b = map(int, input().split())

a_min, a_max = math.ceil(12.5 * a), math.floor(12.5 * (a + 1)) - 1
b_min, b_max = math.ceil(10 * b), math.floor(10 * (b + 1)) - 1
# print(a_min, a_max)
# print(b_min, b_max)

if a_min <= b_min <= a_max:
    print(b_min)
elif b_min <= a_min <= b_max:
    print(a_min)
else:
    print(-1)


Measured memory_bytes: 3060
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long a[100010];
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
            cin>>a[i];
        sort(a+1,a+1+n,cmp);
        long long x=0,y=0;
        for(int i=2;i<=n;i++)
        {
            if(a[i]==a[i-1]&&x==0){x=a[i];a[i]=0;}
            else if(a[i]!=0&&a[i]==a[i-1]){y=a[i];break;}
        }
        if(x==0||y==0)cout<<'0'<<endl;
        else cout<<x*y<<endl;

    }

}


Measured memory_bytes: 1024
import java.util.*;
import java.math.RoundingMode;
import java.math.BigDecimal;
 
 
 
public class Main{
	public static void main (String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long a[] = new long[n];
		for(int i = 0; i < n; i++) {
			a[i] = sc.nextLong();
		}
		Arrays.sort(a);
		long ans = a[n-1];
		int lim = n-2;
		int now = n-2;
		while(lim>0) {
			if(lim-2>=0) {
				ans+=(a[now]*2);
				lim-=2;
				now--;
			}
			else {
				ans+=a[now];
				lim--;
				now--;
			}
		}
		System.out.println(ans);
	}
}

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

int main(){
  int a, b;
  string s;
  cin >> a >> b >> s;
  bool flag = true;
  for(int i = 0; i < s.length() && flag; i++){
  	if(i == a && !(s[i] == '-')){
      flag = false;
    }else if(i != a && !(s[i] >= '0' && s[i] <= '9')){
    	flag = false;
    }
  }
  if(flag){
  	cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}

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

int main(){
    char S[3];
    rep(i, 3) cin >> S[i];

    if (( (S[0] == 'A')  && (S[1] == 'A')  && (S[2] == 'A') )||( (S[0] == 'B')  && (S[1] == 'B')  && (S[2] == 'B') )) cout << "No" << endl;
    else cout << "Yes" << endl;

    return 0;
}

Measured memory_bytes: 256
A, B = map(int,input().split())
ans = 0
if A <= B:
    ans += B
    if A <= B-1:
        ans += B-1
    else:
        ans += A
else:
    ans += A
    if A-1 <= B:
        ans += B
    else:
        ans += A-1
print(ans)

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <cmath>
#include <functional>
#include <set>
#include <string>
#include <stdlib.h>
#include <iomanip>
#include <utility>
 
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i = a;i<=b;i++)
#define ll long long int
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define iii pair<ii,int >
#define ff first
#define ss second
#define vi vector<int>
#define vc vector<char>
 
using namespace std;
const int MOD = 1000000007;
const int MAXN = (int)1e5 + 5;
vi g[MAXN];
ll dp1[MAXN]; // white
ll dp2[MAXN]; // black

void dfs(int node,int p){
	FOR(i,g[node].size()){
		int child = g[node][i];
		if(child == p)continue;
		dfs(child,node);
	}
	if(g[node].size() == 1 and p != -1){
		dp1[node] = dp2[node] = 1;
	}else{
		dp1[node] = 1;
		dp2[node] = 1;
		FOR(i,g[node].size()){
			int child = g[node][i];
			if(child == p)continue;
			dp1[node] *= dp1[child] + dp2[child];
			dp2[node] *= dp1[child];
			dp1[node] %= MOD;
			dp2[node] %= MOD;
		}
	}
}

int main(){
	int n;
	cin >> n;
	if(n == 1){
		cout << 2 << endl;
		return 0;
	}
	FOR(i,n-1){
		int a,b;
		cin >> a >> b;
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(0,-1);
	FOR(i,n){
		//cout << dp1[i] << " " << dp2[i] << endl; 
	}
	// cout << endl;
	
	cout << (dp1[0] + dp2[0])%MOD << endl;

	return 0;
}

Measured memory_bytes: 12800
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;

int main() {
	while(1) {
		int n, p;
		cin >> n >> p;
		if(n==0 && p==0) break;
		int player[n];
		for(int i=0; i<n; i++) player[i] = 0;

		int i=0;
		int num = p;
		while(1) {
			i = i%n;
			if(num!=0) {
				player[i]++;
				num--;
				if(player[i] == p) {
					cout << i << endl;
					break;
				}
			}
			else {
				int temp = player[i];
				player[i] = 0;
				num += temp; 
			}
			i++;
		}
	}
	return 0;
}

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

struct edge {
  int a;
  int b;
  int weight;
};
int *ufp, *ufr;
struct edge *ed;

void qsort1(int, int);
int UF_find(int);
void UF_union(int, int);
int Kruskal(int, int);

int main(void)
{
  int i, j, v, e, s, t, w, cost;
  scanf("%d %d", &v, &e);
  ufp = (int*)malloc(sizeof(int) * v);
  ufr = (int*)malloc(sizeof(int) * v);
  ed = (struct edge*)malloc(sizeof(struct edge) * e);
  for (i = 0; i < v; i++) {
    ufp[i] = i;
    ufr[i] = 0;
  }
  for (i = 0; i < e; i++) {
    scanf("%d %d %d", &s, &t, &w);
    ed[i].weight = w;
    ed[i].a = s;
    ed[i].b = t;
  }
  cost = Kruskal(v, e);
  printf("%d\n", cost);
  free(ufp);
  free(ufr);
  free(ed);
}

void qsort1(int left, int right)
{
  int pivot = ed[(left + right) / 2].weight, i = left, j = right;
  if (i >= j) return;
  do {
    while (ed[i].weight < pivot) i++;
    while (ed[j].weight > pivot) j--;
    if (i <= j) {
      struct edge temp = ed[i];
      ed[i] = ed[j];
      ed[j] = temp;
      i++;
      j--;
    }
  } while (i <= j);
  qsort1(left, j);
  qsort1(i, right);
}

int UF_find(int a)
{
  if (ufp[a] == a) return a;
  else {
    ufp[a] = UF_find(ufp[a]);
    return ufp[a];
  }
}

void UF_union(int a, int b)
{
  if (ufr[a] < ufr[b]) ufp[a] = b;
  else {
    ufp[b] = a;
    if (ufr[a] == ufr[b]) ufr[a]++;
  }
}

int Kruskal(int size, int esize)
{
  int i, selected = 0, cost = 0;
  qsort1(0, esize - 1);
  for (i = 0; i < esize; i++) {
    int ea = UF_find(ed[i].a), eb = UF_find(ed[i].b);
    if (ea != eb) {
      UF_union(ea, eb);
      cost += ed[i].weight;
      selected++;
    }
    if (selected == size - 1) break;
  }
  return cost;
}


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

int count_divisor(int n)
{
    int count = 0;
    for (int i = 1; i <= n; ++i) {
        if (n % i == 0) count++;
    }
    return count;
}

int main()
{
    int n, count = 0;
    cin >> n;
    for (int i = n; i >= 1; --i) {
        if (i % 2 == 0) continue;
        if (count_divisor(i) == 8) count++;
    }
    cout << count << endl;
    return 0;
}


Measured memory_bytes: 256
A, B, C = map(int, input().split())
K = int(input())

ans = False
while K >= 0:
    if A >= B:
        B = B * 2
        K -= 1
    elif B >= C:
        C = C * 2
        K -= 1
    else:
        ans = True
        break

print('Yes' if ans else 'No')


Measured memory_bytes: 9164
N,K = map(int,input().split())
R,S,P = map(int,input().split())
T = list(input())
dic = {'r':P,'s':R,'p':S}
ans  = 0
for i in range(K):
    ans += dic[T[i]]
for i in range(K,N):
    if T[i-K] != T[i]:
        ans += dic[T[i]]
    else:
        T[i] = 'e'
print(ans)

Measured memory_bytes: 9940
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int s = in.nextInt();
		int[] mgp = new int[101];
		while (n != 0 || s != 0) {
			Arrays.fill(mgp, 0);
			int asize = 0;
			for (int i = 0; i < n; i++) {
				int pw = in.nextInt();
				mgp[pw]++;
				if (pw >= s) {
					asize++;
				}
			}
			long pear = 0;
			for (int i = 1; i <= 100; i++) {
				for (int j = i; j <= 100; j++) {
					if (i + j > s && mgp[i] != 0 && mgp[j] != 0) {
						int p = mgp[i] * mgp[j];
						if (i == j) {
							p -= mgp[i];
							p /= 2;
						}
						pear += p;
					}
				}
			}
			System.out.println(pear);
			n = in.nextInt();
			s = in.nextInt();
		}
	}
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)


int main(){
    int N, K;
    cin >> N >> K;
    vector<pair<ll, int> > DT(N);
    REP(i,N){
        ll t, d;
        cin >> t >> d;
        t--;
        DT[i] = make_pair(d, t);
    }

    sort(DT.begin(), DT.end(), greater<pair<ll, int> >());

    ll sum = 0;
    set<int> cls;
    map<int, int> m;
    for(int i = 0; i < K; i++){
        pair<ll, int> p = DT[i];
        sum += p.first;
        int c = p.second;
        m[c]++;
        if(cls.find(c) == cls.end())
            cls.insert(c);
    }
    sum += cls.size()*cls.size();

    ll tmp = sum;
    int midx = K-1;
    for(int i = K; i < N; i++){
        int mc = DT[midx].second;
        while(m[mc] <= 1){
            midx--;
            if(midx < 0) goto fin;
            mc = DT[midx].second;
        }
        pair<ll, int> p = DT[i];
        ll t = p.first; 
        int c = p.second;
        if(cls.find(c) != cls.end()) continue;
        ll mm = DT[midx].first;
        int cls_size = cls.size();
        //if(-mm + t + 2*cls_size + 1 >= 0){
        if(-mm + t + 2*cls_size + 1 >= 0){
            sum = tmp + -mm + t + 2*cls_size + 1;
        }
        //cout << -mm + t + 2*cls_size + 1 << endl;
        tmp += -mm + t + 2*cls_size + 1;
        cls.insert(c);
        m[c]++;
        m[mc]--;
        midx--;
        //}
    }

fin:
    sum = max(sum, tmp);
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 7808
import numpy as np

h, w, d = map(int, input().strip().split(" "))
a = [[int(i) for i in input().strip().split(" ")] for _ in range(h)]
q = int(input().strip())
lr = [input().strip().split(" ") for _ in range(q)]
rs = [None for _ in range(h * w)]
cs = [None for _ in range(h * w)]
for _r, _a in enumerate(a):
    for _c, __a in enumerate(_a):
        rs[__a - 1] = _r
        cs[__a - 1] = _c
rs_d = [rs[i::d] for i in range(d)]
cs_d = [cs[i::d] for i in range(d)]
costs = [
    np.cumsum(
        np.abs(np.array(_rs[1:]) - np.array(_rs[:-1])) +
        np.abs(np.array(_cs[1:]) - np.array(_cs[:-1]))) for _rs, _cs in zip(rs_d, cs_d)
]
for l, r in lr:
    l = int(l) - 1
    r = int(r) - 1
    if l == r:
        print(0)
    elif l < d:
        print(costs[r % d][r // d - 1])
    else:
        print(costs[r % d][r // d - 1] - costs[l % d][l // d - 1])


Measured memory_bytes: 80720
#include<iostream>

using namespace std;


int main(void)
{
	int M[12] = {0};
	int N[12] = {0};
	int L = 0;
	
	while (true)
	{
		int ans = 0;

		cin >> L;
		if (L == 0)	{	return 0; }


		int i = 0;

		for (i = 0; i < 12; i++)
		{
			cin >> M[i] >> N[i];
		}

		for (i = 0; i < 12; i++)
		{
			if (L <= 0)
			{
				break;
			}
			ans++;
			L = L + N[i] - M[i];
		}

		if (i == 12 && L > 0)
		{
			cout << "NA" << endl;
			continue;
		}

		cout << ans << endl;
	}

	return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
using namespace std;
void IA(vector<int> &vec) {
    for (int i=0; i<vec.size(); i++) cin >> vec.at(i);
}
void PA(vector<int> vec) {
    for (int i=0; i<vec.size()-1; i++) cout << vec.at(i) << " ";
    cout << vec.at(vec.size()-1) << endl;
    return;
}

int main() {
    int n; cin >> n;
    vector<long long> l(90);
    l[0] = 2;
    l[1] = 1;
    for (int i=2; i<l.size(); i++) {
        l[i] = l[i-1] + l[i-2];
    }

    long long ans = l[n];
    cout << ans << endl;
}


Measured memory_bytes: 3628
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <limits>
#include <map>
#include <set>
#include <iomanip>
#include <cmath>
 
using namespace std;
 
typedef long long int ll;
typedef std::numeric_limits<double> dbl;
const long long int LL_INF=1LL<<60;
 
vector<string> split(const string& input, char delimiter)
{
    stringstream stream(input);
    
    string field;
    vector<string> result;
    while (getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}
 
//#define DEBUG
 
ll A, B, C, D, E, M, N, K, Q, W, H, T, X, Y;
 
int main()
{
    string str; cin >> str;
    vector<bool> in(26);
    for (auto& i : str) {
        if (in[i-'a']) {
             cout << "no" << endl;
             return 0;
        }
        in[i-'a'] = true;
    }
    cout << "yes" << endl;
 
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>  
using namespace std;
char s[200005];
int f[1<<26];
int main(){
	scanf("%s",&s);
	int n=strlen(s),td;
	for (int i=1;i<(1<<26);i++) f[i]=1000000000;
	for (int i=0,zt=0;i<n;i++){
		zt^=(1<<(s[i]-'a'));
		td=f[zt]+1;
		for (int j=0;j<26;j++) td=min(td,f[zt^(1<<j)]+1);
		f[zt]=min(f[zt],td);
	}
	printf("%d",td);
}

Measured memory_bytes: 262528
// Template from https://techracho.bpsinc.jp/jhonda/2019_08_05/78537

// input macro for competive-programming from https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, bytes) => {
        read_value!($iter, String).into_bytes()
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();

    println!("{}", solve(&s));
}

macro_rules! test {
    ($($input:expr => $output:expr),* $(,)*) => {
        #[test]
        fn solve_test() {
            $(
                assert_eq!(solve($input), $output);
            )*
        }
    };
}

test! {
r#"12"# => r#"Yes"#,
}


fn solve(src: &str) -> String {
    input! {
        source = src,
        n: usize,
    }

    if n % n.to_string().chars().map(|x| (x as u8 - '0' as u8) as usize).sum::<usize>() == 0 {
        "Yes"
    } else {
        "No"
    }.to_string()
}

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

using namespace std;
using ll = long long;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  vector<ll> ar(n);
  for(ll &x: ar) cin >> x;
  sort(ar.begin(), ar.end());
  ll res = 0;
  for(int i = 0; i < n - k; ++i) {
    res += ar[i];
  }
  cout << res << '\n';
  return 0;
}


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

using namespace std;
using int64 = long long;
using Int2D = tuple<int, int>;

constexpr int DEBUG = 0;

template<typename T>
vector<vector<T>> MakeVector2D(int d1, int d2, T default_value) {
  return vector<vector<T>>(d1, vector<T>(d2, default_value));
}

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

  int n;
  cin >> n;
  vector<Int2D> ps(n * n);
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int r = p / n;
    int c = p % n;
    ps[i] = {r, c};
  }

  vector<vector<int>> is_filled_m = MakeVector2D(n, n, 1);
  vector<vector<int>> cost_m = MakeVector2D(n, n, -1);
  for (int r = 0; r < n; r++) {
    for (int c = 0; c < n; c++) {
      int min_r = min(r, n - 1 - r);
      int min_c = min(c, n - 1 - c);
      cost_m[r][c] = min(min_r, min_c);
    }
  }

  int ans = 0;
  for (const auto& [r, c] : ps) {
    ans += cost_m[r][c];

    is_filled_m[r][c] = 0;
    stack<Int2D> stack({{r, c}});
    while (!stack.empty()) {
      const auto [sr, sc] = stack.top();
      stack.pop();
      for (const auto& [dr, dc] : vector<Int2D>({{1, 0}, {0, 1}, {-1, 0}, {0, -1}})) {
        int nr = sr + dr;
        int nc = sc + dc;
        if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
        if (cost_m[nr][nc] > cost_m[sr][sc] + is_filled_m[sr][sc]) {
          cost_m[nr][nc] = cost_m[sr][sc] + is_filled_m[sr][sc];
          stack.push({nr, nc});
        }
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 8012
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
ll gcd(ll a, ll b) {
    if(a < b) swap(a, b);
    if(b == 0) return a;
    if(a % b == 0) return b;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
struct SegmentTree {
private:
    int n;
    vector<ll> node;
 
public:
    SegmentTree() {
        int sz = 1e5 + 2;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 1);
        for(int i=0; i<sz; i++) node[i+n-1] = 1;
        for(int i=n-2; i>=0; i--) node[i] = lcm(node[2*i+1], node[2*i+2]);
    }
 
    void update(int x, int val) {
        x += (n - 1);
        node[x] = val;
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = lcm(node[2*x+1], node[2*x+2]);
        }
    }
    // hannkaikukann 
    ll getlcm(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        if(r <= a || b <= l) return 1;
        if(a <= l && r <= b) return node[k];
 
        ll vl = getlcm(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getlcm(a, b, 2*k+2, (l+r)/2, r);
        return lcm(vl, vr);
    }
};

ll N, Q;
ll p[100500];
ll period[100500];
ll field[100500][50];
ll sum[100500];
ll beki(ll a, ll b) {
    if(b == 0) return 1;
    ll ret = beki(a, b / 2);
    ret = ret * ret % mod;
    if(b & 1) ret = ret * a % mod;
    return ret;
}

void f(int index, int timer) {
    //cout << index << " " << timer << endl;
    period[index] = timer;
    field[index][timer] = p[field[index][timer-1]];
    sum[index] = (sum[index] + field[index][timer]) % mod;
    if(field[index][timer] == index) return;
    f(index, timer + 1);
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) {
        cin >> p[i];
        field[i][0] = i;
    }
    SegmentTree seg;
    for(int i = 1; i <= N; i++) {
        f(i, 1);
        //cerr << i << " " << period[i] << " " << sum[i] << endl;
        seg.update(i, period[i]);
    }
    for(int i = 1; i <= N; i++) {
        //cerr << i << " " << sum[i] << " ";
        sum[i] = sum[i] * beki(period[i], mod - 2) % mod;
        //cerr << sum[i] << endl;
    }
    ll sumsum[100500];
    sumsum[0] = 0;
    for(int i = 1; i <= N; i++) {
        sumsum[i] = (sumsum[i-1] + sum[i]) % mod;
    }
    while(Q--) {
        ll l, r;
        cin >> l >> r;
        ll TIME = seg.getlcm(l, r + 1) % mod;
        ll ans = (sumsum[r] - sumsum[l-1] + mod) * TIME % mod;
        cout << ans << "\n";
    }
    return 0;
}



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

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

const int maxn = 3e5 + 10;
int a[maxn], n;

int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
	freopen("in", "r", stdin);
#endif
	scanf("%d", &n);
	for(int i = 1; i <= 3*n; ++i) {
		scanf("%d", &a[i]);
	}
	priority_queue<int, vector<int>, greater<int> > Q; 
	ll sum = 0;
	vector<ll> ans1, ans2; 

	for(int i = 1; i <= n; i++) 
		Q.push(a[i]), sum += a[i];
	
	ans1.push_back(sum); 
	for(int i = n + 1; i <= 2 * n; ++i) {
		sum += a[i]; 
		Q.push(a[i]);
		sum -= Q.top(); 
		Q.pop();
		ans1.push_back(sum); 
	}
	priority_queue<int> q; sum = 0;
	for(int i = 3 * n; i > 2 * n; --i) 
		q.push(a[i]), sum += a[i]; 

	ans2.push_back(sum);
	for(int i = 2 * n; i > n; --i) {
		sum += a[i]; 
		q.push(a[i]);
		sum -= q.top(); 
		q.pop(); 
		ans2.push_back(sum);
	}

	reverse(ans2.begin(), ans2.end());
	ll Max = -1e18; 

	for(int i = 0; i < ans1.size(); i++) 
		Max = max(Max, ans1[i] - ans2[i]); 

	printf("%lld\n", Max);

}

Measured memory_bytes: 4852
n = int(input())
words = []
for i in range(n):
    words.append(input())
if len(words) != len(set(words)):
    print("No")
    exit()
prev = ""
for word in words:
    if prev != "":
        if prev[-1] != word[0]:
            print("No")
            exit()
    prev = word
print("Yes")

Measured memory_bytes: 3064
#include<bits/stdc++.h>
#define N 200005
#define LL long long
#define mod 1000000007
using namespace std;

struct team{
	int p,v;
	friend bool inline operator < (team a,team b){
		return a.v<b.v;
	}
}Tm[N];

struct T_node{
	int l,r;
	LL s,t1,t2;
};

struct sg{
	int l,r;
	friend bool inline operator < (sg a,sg b){
		if(a.r==b.r) return a.l<b.l;
		return a.r<b.r;
	}
}a[N];

int n,mx[N],mn[N],flag=1;

LL inline pw(LL x,LL y){
	LL ret=1,tmp=x;
	while(y){
		if(y&1) ret=ret*tmp%mod;
		y>>=1,tmp=tmp*tmp%mod;
	}
	return ret;
}

struct segment_tree{
	T_node p[N<<3];
	void inline pushdown(int x){
		if(p[x].t1==1&&p[x].t2==0) return;
		int T1=p[x].t1,T2=p[x].t2,ls=x<<1,rs=x<<1|1;
		p[x].t1=1,p[x].t2=0;
		(p[ls].s*=T1)%=mod,(p[ls].t1*=T1)%=mod,(p[ls].t2*=T1)%=mod;
		(p[ls].s+=1LL*T2*(p[ls].r-p[ls].l+1))%=mod,(p[ls].t2+=T2)%=mod;
		(p[rs].s*=T1)%=mod,(p[rs].t1*=T1)%=mod,(p[rs].t2*=T1)%=mod;
		(p[rs].s+=1LL*T2*(p[rs].r-p[rs].l+1))%=mod,(p[rs].t2+=T2)%=mod;
	}	
	void inline build(int x,int l,int r){
		p[x].l=l,p[x].r=r,p[x].t1=1;
		if(l==r) return;
		int mid=(l+r)>>1;
		build(x<<1,l,mid),build(x<<1|1,mid+1,r);	
	}
	void inline modify(int x,int l,int r,int opt,int v){
		int L=p[x].l,R=p[x].r;
		pushdown(x);
		if(l==L&&r==R){
			if(opt==2) (p[x].s+=1LL*v*(p[x].r-p[x].l+1))%=mod,(p[x].t2+=v)%=mod;
			else (p[x].s*=v)%=mod,(p[x].t1*=v)%=mod,(p[x].t2*=v)%=mod;
			return;
		}
		int mid=(L+R)>>1;
		if(r<=mid) modify(x<<1,l,r,opt,v);
		else if(l>mid) modify(x<<1|1,l,r,opt,v);
		else modify(x<<1,l,mid,opt,v),modify(x<<1|1,mid+1,r,opt,v);
		p[x].s=p[x<<1].s+p[x<<1|1].s;
	}
	LL inline query(int x,int pos){
		pushdown(x);
		if(p[x].l==p[x].r) return p[x].s;
		int mid=(p[x].l+p[x].r)>>1;
		if(pos<=mid) return query(x<<1,pos);
		return query(x<<1|1,pos);
	}
}T;

void inline solve(){
	sort(a+1,a+n+1),T.build(1,1,n);
	for(int i=1;i<=n;i++){
		int l=a[i].l,r=a[i].r;
		if(flag&&l==1) flag=0,T.modify(1,l,r,2,pw(2,i-1));
		else if(!flag){
			LL tmp=(l==1)?pw(2,i-1):T.query(1,l-1);
			T.modify(1,l,r,2,tmp);
			if(l!=1) T.modify(1,1,l-1,1,2);
		}
	}
	printf("%lld\n",T.query(1,n));
}

int inline ckmx(int p){
	int l=1,r=p,mid;
	while(l<r){
		mid=(l+r)>>1;
		if(mx[mid]>=Tm[p].p) r=mid;
		else l=mid+1;
	}
	return l;
}

int inline ckmn(int p){
	int l=p,r=n,mid;
	while(l+1<r){
		mid=(l+r)>>1;
		if(mn[mid]<=Tm[p].p) l=mid;
		else r=mid-1;
	}
	return (mn[r]<=Tm[p].p)?r:l; 
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d%d",&Tm[i].p,&Tm[i].v);
	sort(Tm+1,Tm+n+1),mx[0]=0,mn[n+1]=0x3f3f3f3f;
	for(int i=1;i<=n;i++) mx[i]=max(mx[i-1],Tm[i].p);
	for(int i=n;i;i--) mn[i]=min(mn[i+1],Tm[i].p);
	for(int i=1;i<=n;i++) a[i].l=ckmx(i),a[i].r=ckmn(i);
	solve();
}

Measured memory_bytes: 40576
#include<cstdio>
#include<cstring>
const int N=3069;
template<typename TP>
inline void read(TP &kk)
{
	TP ret=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
	kk=ret*f;
}
int n,a[N];
struct sumireko{int to,ne;}e[N<<1];
int he[N],ecnt;
void addline(int f,int t)
{
	e[++ecnt].to=t;
	e[ecnt].ne=he[f];
	he[f]=ecnt;
}
int dp[N];
void dfs(int x,int f)
{
	for(int i=he[x],t;i;i=e[i].ne)
	{
		t=e[i].to;
		if(t==f) continue;
		dfs(t,x);
		if(a[t]<a[x]&&(!dp[t])) dp[x]=1;
	}
}

int xi,yi;
signed main()
{
	read(n);
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1;i<n;i++)
	{
		read(xi),read(yi);
		addline(xi,yi),addline(yi,xi);
	}
	for(int i=1;i<=n;i++)
	{
		memset(dp,0,sizeof(dp));
		dfs(i,0);
		if(dp[i]) printf("%d ",i);
	}
	return 0;
}

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL<<60;
const int mod = 1000000007;

template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }

int N;
vector<ll> a;
vector<vector<ll>> dp;
vector<ll> S;

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

    cin >> N;
    a.resize(N);
    S.resize(N + 1);
    REP(i, N) cin >> a[i];
    dp.assign(N + 1, vector<ll>(N + 1, INF));

    for (int i = 0; i < N; i++) {
        dp[i][i] = 0;
    }

    S[0] = 0;
    for (int i = 0; i < N; i++) {
        S[i + 1] = S[i] + a[i];
    }

    for (int len = 1; len < N; len++) {
        for (int l = 0; l + len <= N; l++) {
            int r = l + len;
            for (int m = l; m < r; m++) {
                chmin(dp[l][r], dp[l][m] + dp[m + 1][r] + S[r + 1] - S[l]);
            }
        }
    }

    cout << dp[0][N - 1] << endl;
    
    return 0;
}

Measured memory_bytes: 1536
using System;

class project{
static void Main(){
  
  string [] a = Console.ReadLine().Split(' ');
  int n1 = int.Parse(a[0]);
  int n2 = int.Parse(a[1]);
  
  Console.WriteLine(Math.Max(Math.Max(n1+n2,n1-n2),n1*n2));
}

}

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

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<complex>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD =  (int)1e9 + 7;
const double EPS = 1e-9;

int N, K;
int A[200010];
int L[200010];
int P,T;

void dfs(int idx,int cnt)
{
	L[idx] = cnt;
	if (L[A[idx]] == -1)dfs(A[idx], cnt + 1);
	else
	{
		T = (cnt + 1 - L[A[idx]]);
		P = A[idx];
	}
}

void dfs2(int idx, int cnt)
{
	if (cnt == K)
	{
		cout << idx + 1 << endl;
	}
	else
	{
		dfs2(A[idx], cnt + 1);
	}
}

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

	cin >> N >> K;
	rep(i, 0, N)
	{
		cin >> A[i];
		A[i]--;
		L[i] = -1;
	}

	dfs(0,0);

	if (K >= L[P])
	{
		K = L[P] + (K - L[P]) % T;
	}

	dfs2(0, 0);

	return 0;
}

Measured memory_bytes: 6776
import java.util.Scanner;

public class Main {

    static int[] p;
    static int[] x;
    static int[] y;
    static int[] size;
    static int[] sizes;

    public static void main(String args[]){


        Scanner scanner = new Scanner(System.in);

        int n = Integer.parseInt(scanner.next());
        int q = Integer.parseInt(scanner.next());

        p = new int[n];
        x = new int[q];
        y = new int[q];
        size = new int[q];
        sizes = new int[n];



        for(int i = 0; i < n; i++){
            p[i] = i;
            sizes[i] = 0;
        }

        for(int i = 0; i < q; i++){
            x[i] = Integer.parseInt(scanner.next());
            y[i] = Integer.parseInt(scanner.next());
            size[i] = Integer.parseInt(scanner.next());
        }

        quick_sort(size, 0, q - 1);

        for(int i = 0; i < q; i++){
            unite(x[i], y[i], size[i]);
        }

        System.out.println(size(0));



    }

    static public int find(int x){
        if(x == p[x]) return x;
        return p[x] = find(p[x]);
    }

    static public void unite(int x, int y, int size){
        x = find(x);
        y = find(y);

        if(x == y) return;

        if(sizes[x] < sizes[y]){
            int tmp;
            tmp = x;
            x = y;
            y = tmp;
        }

        sizes[x] += size;

        p[y] = x;
        sizes[x] += sizes[y];
    }

    public static boolean same(int x, int y){
        if(find(x) == find(y)) return true;
        return false;
    }

    public static int size(int x){
        return sizes[find(x)];
    }


    public static void quick_sort(int[] d, int left, int right) {
        if (left >= right) {
            return;
        }
        int p = d[(left + right) / 2];
        int l = left, r = right, tmp;
        while (l <= r) {
            while (d[l] < p) {
                l++;
            }
            while (d[r] > p) {
                r--;
            }
            if (l <= r) {
                tmp = d[l];
                d[l] = d[r];
                d[r] = tmp;
                tmp = x[l];
                x[l] = x[r];
                x[r] = tmp;
                tmp = y[l];
                y[l] = y[r];
                y[r] = tmp;
                l++;
                r--;
            }
        }
        quick_sort(d, left, r);
        quick_sort(d, l, right);
    }


}



Measured memory_bytes: 79548
#include<cstdio>

using namespace std;

int main() {
    int nums[] = {1, 1, 1};
    for(int i = 0; i < 3; ++i) {
        int a = getchar() - 'a';
        --nums[a];
    }
    for(int i = 0; i < 3; ++i) {
        if(nums[i] != 0) {
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
}


Measured memory_bytes: 128
#include<stdio.h>
#include<iostream>

using namespace std;

int main(){
    int n;
    cin >> n;
    bool dp[2005];
    for (int i = 0; i < 2005; ++i) {
        dp[i] = false;
    }
    dp[0] = true;

    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        for (int j = 2000-a; j >= 0; --j) {
            if(dp[j]){
                dp[j+a] = true;
            }
        }
    }

    int q;
    cin >> q;

    for (int i = 0; i < q; ++i) {
        int m;
        cin >> m;

        cout << (dp[m] ? "yes" : "no") << endl;
    }


    return 0;
}


Measured memory_bytes: 3116
import sys

h,w = map(int, sys.stdin.readline().split())

a = [input() for _ in range(h)]

row = [False] * h
col = [False] * w

for i in range(h):
    for j in range(w):
        if a[i][j] == "#":
            row[i] = True
            col[j] = True

for i in range(h):
    if row[i]:
        for j in range(w):
            if col[j]:
                print(a[i][j], end="")
        print()


Measured memory_bytes: 4468
N=int(input())

a={}
a['AC']=0
a['WA']=0
a['TLE']=0
a['RE']=0
for n in range(N):
    s=input()
    a[s]+=1

print('AC x {}'.format(a['AC']))
print('WA x {}'.format(a['WA']))
print('TLE x {}'.format(a['TLE']))
print('RE x {}'.format(a['RE']))



Measured memory_bytes: 9208
#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define endl "\n"
using ll=long long;
using ld=long double;
using namespace std;
#define INF 1e9+7
#define MAX_N 210000
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll a[210000];
main(){
    ll n,m,k,tmp,ans=0;
    cin>>n;
    rep(i,0,n){
        cin>>a[i];
    }
    ll mm=a[0];
    rep(i,0,n){
        if(a[i]<=mm)ans++;
        mm=min(mm,a[i]);
    }
cout<<ans;
    
}


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

int ganjil[100005];
int genap[100005];

int main(){
	int n,x;
	scanf("%d",&n);
	memset(ganjil,0,sizeof(ganjil));
	memset(genap,0,sizeof(genap));
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(i&1) ganjil[x]++;
		else genap[x]++;
	}
	int maks=0, prev=0, gjl=0;
	for(int i=1;i<=100000;i++){
		if(ganjil[i]>=maks){
			prev=maks;
			maks=ganjil[i];
			gjl=i;
		}
		else if(ganjil[i]>prev){
			prev=ganjil[i];
		}
	}
	int maks2=0,prev2=0,gnp=0;
	for(int i=1;i<=100000;i++){
		if(genap[i]>=maks2){
			prev2=maks2;
			maks2=genap[i];
			gnp=i;
		}
		else if(genap[i]>prev2){
			prev2=genap[i];
		}
	}
//	printf("%d %d %d %d",maks,maks2,prev,prev2);
	int ans=0;
	if(gnp!=gjl){
		ans=n-maks2-maks;
	}
	else{
		if(maks==prev||maks2==prev2){
			ans=n-maks-maks2;
		}
		else if(maks-prev>maks2-prev2){
			ans=n-prev2-maks;
		}
		else{
			ans=n-prev-maks2;
		}
	}
	printf("%d\n",ans);
}

Measured memory_bytes: 1024
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()
KA = [LIST() for _ in range(N)]

S = set(KA[0][1:])
for i in range(1, N):
	S = S & set(KA[i][1:])

print(len(S))

Measured memory_bytes: 5460
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

ll n, m;

bool C(ll k){
    if (m < 2 * k) return false;
    return n + (m - 2 * k) / 2 >= k;
}

int main(){
    cin >> n >> m;

    ll ok = 0, ng = m;
    while(abs(ok - ng) > 1){
        ll mid = (ok + ng) / 2;
        if (C(mid)) ok = mid;
        else ng = mid;
    }

    cout << ok << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
int main()
{
	int N;
    std::cin >> N;
	std::vector<int> question(N);
	std::map<int, int> m;
	for (int i = 1; i < N; i++)
	{
		int A;
		std::cin >> A;
		question[i] = A;

		m[A]++;
	}
	for (int i = 0; i < N; i++)
	{
		int count = m[i + 1];
		std::cout << count << std::endl;
	}
}

Measured memory_bytes: 13508
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <queue>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using PP = pair<P, P>;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
const int MAX_N = 35;
const int dx[4][4] = {{1, 0, -1, 0}, {0, -1, 0, 1}, {0, 1, 0, -1}, {-1, 0, 1, 0}};
const int dy[4][4] = {{0, 1, 0, -1}, {1, 0, -1, 0}, {-1, 0, 1, 0}, {0, -1, 0, 1}};
int memo[4][4] = {{0, 1, 3, 2}, {1, 3, 2, 0}, {2, 0, 1, 3}, {3, 2, 0, 1}};
int w, h;
vector<int> c(4);
int table[MAX_N][MAX_N];
int solve()
{
    priority_queue<PP, vector<PP>, greater<PP>> que;
    int used[h][w][4];
    for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) for(int k = 0; k < 4; k++) used[i][j][k] = INF;
    used[0][0][0] = 0;
    que.push(PP(P(0, 0), P(0, 0)));
    while(que.size())
    {
        PP p = que.top(); que.pop();
        int cost = p.first.first;
        int dir = p.first.second;
        int y = p.second.first;
        int x = p.second.second;
        if(y == h - 1 and x == w - 1) return cost;
        if(used[y][x][dir] < cost) continue;
        for(int i = 0; i < 4; i++)
        {
            int ny = y + dy[dir][i];
            int nx = x + dx[dir][i];
            if(ny < 0 or nx < 0 or ny >= h or nx >= w) continue;
            int d = memo[dir][i];
            if(table[y][x] == i)
            {
                if(used[ny][nx][d] > used[y][x][dir])
                {
                    used[ny][nx][d] = used[y][x][dir];
                    que.push(PP(P(used[ny][nx][d], d), P(ny, nx)));
                }
            }
            else
            {
                if(used[ny][nx][d] > used[y][x][dir] + c[i])
                {
                    used[ny][nx][d] = used[y][x][dir] + c[i];
                    que.push(PP(P(used[ny][nx][d], d), P(ny, nx)));
                }
            }
        }
    }
    return -1;
}
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> w >> h, w or h)
    {
        memset(table, 0, sizeof(table));
        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++) cin >> table[i][j];
        }
        for(int i = 0; i < 4; i++) cin >> c[i];
        cout << solve() << endl;
    }
    return 0;
}




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

int main() {
  long double a,b,w;
  ll ans;
  cin >> a>>b;
  w=a*b;
  ans=floor(w);
  cout <<ans<<endl;
}


Measured memory_bytes: 3796
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

import numpy as np

def main():
    n, m = map(int, input().split())
    a = np.array([int(i) for i in input().split()])
    a.sort()

    while True:
        temp = a[-1]//2
        a[-1] //= 2
        a.sort()
        ind = np.where(a==temp)[0][0]
        m -= 1
        if m == 0:
            print(np.sum(a))
            break
        elif n - ind < m:
            a[ind:] = a[ind:]//2
            a.sort()
            m = m - (n-ind)
        else:
            a[-m:] = a[-m:]//2
            print(np.sum(a))
            break

main()


Measured memory_bytes: 23148
import queue

INF = 123456789

def main():
    n,m = map(int,input().split())

    G = [[] for _ in range(n)]
    for _ in range(m):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append((b,c))
        G[b].append((a,c))

    used = set()
    for i in range(n):
        d = [INF for _ in range(n)]
        par = [-1 for _ in range(n)]

        # dijkstra
        que = queue.PriorityQueue()
        d[i] = 0
        que.put((0,i))
        while not que.empty():
            p = que.get()
            v = p[1]
            if d[v] < p[0]:
                continue
            for (to,cost) in G[v]:
                if d[to] > d[v]+cost:
                    d[to] = d[v]+cost
                    par[to] = v
                    que.put((d[to],to))

        for j in range(n):
            if i != j:
                a,b = j,par[j]
                if a>b:
                    a,b = b,a
                used.add((a,b))

    print(m - len(used))

if __name__ == '__main__':
    main()


Measured memory_bytes: 4204
# coding: utf-8
A = input()
B = input()

if len(A) > len(B):
    print('GREATER')
    exit()
elif len(A) < len(B):
    print('LESS')
    exit()
else:
    for i in range(len(A)):
        # print('i={} , A[i]={} , B[i]={}'.format(i, A[i], B[i]))
        if int(A[i]) > int(B[i]):
            print('GREATER')
            exit()
        elif int(A[i]) < int(B[i]):
            print('LESS')
            exit()
print('EQUAL')

Measured memory_bytes: 3064
import java.util.*;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int N;
	String P, Q;
	int result = 0;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		N = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < N; ++i) {
			strBuilder.append(tokens[i]);
		}
		P = strBuilder.toString();
		tokens = in.readLine().split(" ");
		strBuilder = new StringBuilder();
		for (int i = 0; i < N; ++i) {
			strBuilder.append(tokens[i]);
		}
		Q = strBuilder.toString();
	}

	List<StringBuffer> sub(String str) {
		if (str.length() == 1) {
			List<StringBuffer> list = new ArrayList<>();
			StringBuffer buf = new StringBuffer();
			buf.append(str.charAt(0));
			list.add(buf);
			return list;
		} else {
			List<StringBuffer> list = new ArrayList<>();
			for (int i = 0; i < str.length(); ++i) {
				Character c = str.charAt(i);
				StringBuffer s = new StringBuffer(str);
				s.deleteCharAt(i);
				List<StringBuffer> subList = sub(s.toString());
				for (StringBuffer buf : subList) {
					buf.insert(0, c);
				}
				list.addAll(subList);
			}
			return list;
		}
	}

	void calc() {
		List<StringBuffer> list = sub(P);
		List<String> strList = new ArrayList<>(list.size());
		for (StringBuffer buf : list) {
			strList.add(buf.toString());
		}
		Collections.sort(strList);
		int p = Collections.binarySearch(strList, P);
		int q = Collections.binarySearch(strList, Q);
		result = Math.abs(q - p);
	}

	void showResult() {
		System.out.println(result);
	}
}

Measured memory_bytes: 49068
s = input()
a = ''
b = ''
for i in range(len(s)):
  if i%2 == 0:
    a += '0'
    b += '1'
  else:
    a += '1'
    b += '0'
def distance(s, a):
  return format(int(s, 2) ^ int(a, 2), 'b').count('1')
print(min(distance(s, a),distance(s, b)))

Measured memory_bytes: 3444
#include <iostream>
using namespace std;
int main(){
  int n;
  cin>>n;
  int i,k;
  char j;
  int p[][13]={
    {1,2,3,4,5,6,7,8,9,10,11,12,13},
    {1,2,3,4,5,6,7,8,9,10,11,12,13},
    {1,2,3,4,5,6,7,8,9,10,11,12,13},
    {1,2,3,4,5,6,7,8,9,10,11,12,13},
  };
  for(i=1;i<=n;i++){
    cin>>j>>k;
    if(j=='S'){
      p[0][k-1]=0;
    }else if(j=='H'){
      p[1][k-1]=0;
    }else if(j=='C'){
      p[2][k-1]=0;
    }else if(j=='D'){
      p[3][k-1]=0;
    }
  }
  for(i=0;i<=3;i++){
    for(k=0;k<=12;k++){
      if(p[i][k]!=0){
	switch (i){
	case 0:
	  cout<<"S"<<" "<<p[0][k]<<endl;
	  break;
	case 1:
	  cout<<"H"<<" "<<p[1][k]<<endl;
	  break;
	case 2:
	  cout<<"C"<<" "<<p[2][k]<<endl;
	  break;
	case 3:
	  cout<<"D"<<" "<<p[3][k]<<endl;
	  break;
	}
      }
    }
  }
  return 0;
}

Measured memory_bytes: 3084
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}
// D
fn main() {
  let n: i32 = read();

  let s: String = read();
  let mut result: i64 = 0;
  let mut v: Vec<i32> = s.split(" ").map(|s| s.parse().unwrap()).collect();
  v.sort_by(|a, b| b.cmp(a));
  //println!("{:?}", v)
  let mut t = n - 1;
  for i in 0..n {
    let mut lim = 2;
    if i == 0 { lim = 1; }
    for _j in 0..lim {
      if t > 0 {
        result += v[i as usize] as i64;
        t-=1;
      }
    }
  }
  println!("{}", result);
  //println!("{}", calc_comfort(v));
}

Measured memory_bytes: 6276
def get_building(i, N):
    ans = [0]*N
    ans[0] = 1
    for ind in range(1, N):
        if i % 2 == 0:
            ans[ind] = 1
        i //= 2
    return ans

N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = -1
for i in range(2**(N-1)):
    see_building = get_building(i, N)

    counter = 1
    now_height = a[0]
    cost = 0
    for j in range(1, N):
        if see_building[j] == 1:
            counter += 1
            if a[j] <= now_height:
                cost += (now_height - a[j]) + 1
                now_height += 1
            else:
                now_height = a[j]
        else:
            now_height = max(now_height, a[j])
    
    if counter >= K:
        if ans == -1:
            ans = cost
        else:
            ans = min(ans, cost)

print(ans)


Measured memory_bytes: 3064
#include <iostream>
using namespace std;
int fact(int n)
{
    if(n<=1)
    return 1;
    else
    return n*fact(n-1);
}
int result(int e,int o)
{
   int odd= o*(o-1)/2;
   int even=e*(e-1)/2;

   return odd+even;
}



int main() {
   int even_no,odd_no;
   cin>>even_no>>odd_no;
   cout<<result(even_no,odd_no);
}

Measured memory_bytes: 3636