akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
using namespace std;

template<class T>
void in(vector<T>& V) {
	for (auto itr = V.begin(); itr != V.end(); itr++)cin >> *itr;
	return;
}

template<class T>
void out(vector<T>& V) {
	for (T& itr : V)cout << itr << endl;
	return;
}

int main() {
	int N;
	cin >> N;
	int K;
	cin >> K;
	int ans = 0;
	for (int i = 0; i < N; i++) {
		int h;
		cin >> h;
		if (h >= K)ans++;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.rbegin(), a.rend());
  vector<vector<long long>> dp(n + 1, vector<long long>(n + 1));
  for (int i = 1; i <= n; i++) {
    int val = a[i - 1].first;
    int pos = a[i - 1].second;
    for (int x = 0; x < i; x++) {
      int y = i - 1 - x;
      dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + 1LL * val * abs(pos - x));
      dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + 1LL * val * abs(n - 1 - y - pos));
    }
  }
  long long ans = 0;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, dp[i][n - i]);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 34616
parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)

function main()
	n = readline() |> parseInt
	w = readline() |> split |> parseMap
	for i in 2:n
		w[i] =w[i]+w[i-1]
	end
	t = w[n]
	for i in 1:n
		t = min(t,abs(w[n]-w[i]-w[i]))
	end
	println(t)
end

main()

Measured memory_bytes: 113668
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<math.h>
#include<numeric>
#include<cassert>
#include<random>
#include<deque>
#include<chrono>
#include<unordered_map>
#include<list>
#include<fstream>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
const ll E=1e18+7;
#define F first
#define S second
#define MK make_pair
const ll MOD=1000000007;

vector<vector<ll>> mk_c(){
    ll mx=60;
    vector<vector<ll>> c(mx+1,vector<ll>(mx+1,0));
    for(ll i=0;i<=mx;i++){
        c[i][i]=1;
        c[i][0]=1;
        for(ll t=1;t<i;t++){
            c[i][t]=c[i-1][t-1]+c[i-1][t];
        }
    }
    return c;
}


int main(){
    vector<vector<ll>> c=mk_c();
    ll n,p;
    cin>>n>>p;
    ll g=0,k=0;
    for(int i=0;i<n;i++){
        ll b;
        cin>>b;
        if(b%2==0){g++;}
        else{k++;}
    }
    g=(ll)1<<g;
    ll K=0;
    if(p==1){
        for(int i=1;i<=k;i+=2){
            K+=c[k][i];
        }
    }
    else{
        for(int i=0;i<=k;i+=2){
            K+=c[k][i];
        }
    }
    cout<<g*K<<endl;
    
    return 0;
}


Measured memory_bytes: 256
N,x = map(int,input().split())
l = list(map(int,input().split()))
l.sort()
for i in range(len(l)):
    sum_l = sum(l[:i+1])
    if sum_l > x :
        print(i)
        break
if sum(l) == x:
    print(N)
elif sum(l) < x:
    print(N-1)


Measured memory_bytes: 2940
#include <cstdio>
#include <algorithm>
static const int MAXN = 1e5 + 4;
static const int LOGN = 17;
static const int MAXQ = 1e5 + 2;

int n, x[MAXN];
int l, q;

int jump[LOGN][MAXN];

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) scanf("%d", &x[i]);
    scanf("%d", &l);

    for (int i = 0; i < n; ++i)
        jump[0][i] = std::upper_bound(x, x + n, x[i] + l) - &x[0] - 1;
    for (int j = 1; j < LOGN; ++j)
        for (int i = 0; i < n; ++i)
            jump[j][i] = (jump[j - 1][i] == n ? n : jump[j - 1][jump[j - 1][i]]);

    scanf("%d", &q);
    int u, v;
    for (int i = 0; i < q; ++i) {
        scanf("%d%d", &u, &v); --u, --v;
        if (u > v) std::swap(u, v);
        int ans = 0;
        for (int j = LOGN - 1; j >= 0; --j) if (jump[j][u] < v) {
            ans += (1 << j);
            u = jump[j][u];
        }
        printf("%d\n", ans + 1);
    }

    return 0;
}


Measured memory_bytes: 7808
#include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int main(){
    int v, e;
    cin >> v >> e;

    ll dist[v][v];//dist[i][j]:iとjを結ぶ辺の距離、
    rep(i, v)rep(j, v)dist[i][j] = INF;

    rep(i, e){
        int s, t, d;
        cin >> s >> t >> d;

        dist[s][t] = d;
    }

    ll dp[(1<<v)][v];//dp[r][i]:集合r(立っているbitの番号の辺が訪問済)から頂点iへ行くときの最短距離
    rep(i, (1<<v))rep(j, v)dp[i][j] = INF;

    dp[0][0] = 0;
    rep(r, (1<<v)){
        rep(i, v){
            if(r&(1<<i)){
                rep(j, v)dp[r][i] = min(dp[r][i], dp[r-(1<<i)][j]+dist[j][i]);
            }
        }
    }

    ll ans = dp[(1<<v)-1][0];
    if(ans >= INF)cout << -1 << endl;
    else cout << ans << endl;
}


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

using namespace std ;

const int MAX = 5e5 + 10 ;

int arr[MAX] , last[MAX] , prv[MAX] ;
vector<int>tree[4 * MAX] ;

int n , q ;

void build(int node , int l , int r)
{
	if(l == r)
	{
		tree[node].push_back(prv[l]) ;
		return ;
	}
	int mid = (l + r) >> 1 ;
	build(node << 1 , l , mid) ;
	build(node << 1 | 1 , mid+1 , r) ;
	merge(tree[node << 1].begin() , tree[node << 1].end() , tree[node << 1 | 1].begin() , tree[node << 1 | 1] .end() , back_inserter(tree[node])) ;
}

int query(int node , int l , int r , int from , int to)
{
	if(from > r || to < l)
		return 0 ;
	if(l >= from && r <= to)
	{
		int x = lower_bound(tree[node].begin() , tree[node].end() , from) - tree[node].begin() ;
		return x ;
	}
	int mid = (l + r) >> 1 ;
	int a = query(node << 1 , l , mid , from , to) ;
	int b = query(node << 1 | 1 , mid+1 , r , from , to) ;
	return a+b ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>q ;
	for(int i = 1 ; i <= n ; ++i)
	{
		cin>>arr[i] ;
		prv[i] = last[arr[i]] ;
		last[arr[i]] = i ;
	}
	build(1 , 1 , n) ;
	while(q--)
	{
		int l , r ;
		cin>>l>>r ;
		cout<<query(1 , 1 , n , l , r)<<"\n" ;
	}
	return 0 ;
}

Measured memory_bytes: 121332
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define printYes() cout << "Yes" << endl;
#define printNo() cout << "No" << endl;
#define printVector(v) rep(i, v.size()) cout << v[i] << endl;

int main()
{
  int R, G;
  cin >> R >> G;
  int ans = 2 * G - R;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<functional>
#include<map>
#include<cstdlib>
#include<complex>
#include<numeric>
#include<set>
#include<deque>
#include<queue>

using namespace std;
using Int = long long;
Int INF = 1LL << 60;
const Int MOD = 1000000000 + 7;

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Int x;
	cin >> x;

	vector<Int> p{ 100,101,102,103,104,105 };
	vector<Int> dp(2e6);//dp[i]:i円を作れるなら1
	dp[0] = 1;

	for (Int i = 0; i < 2e5; i++) {
		if (dp[i]==0) {
			continue;
		}
		for (Int j = 0; j < 6; j++) {
			dp[i + p[j]] = 1;
		}
	}

	if (dp[x]) {
		cout << 1 << "\n";
	}
	else {
		cout << 0 << "\n";
	}

}


Measured memory_bytes: 15872
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,aa,bb,i,j,ans,f[5001][5001],a[5001],la;
int main(){
	scanf("%lld%lld%lld",&n,&aa,&bb);
	for(i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(i=0;i<=n;i++)
	 for(j=0;j<=n;j++)f[i][j]=1e16;
	f[0][0]=0;
	for(i=1;i<=n;i++)
	 for(j=0;j<=n;j++){
	 	la=f[i-1][j];
	 	if(la==1e16)continue;
	 	if(a[i]>j){
	 		f[i][j]=min(f[i][j],la+aa);
	 		f[i][a[i]]=min(f[i][a[i]],la);
	 	}
	 	 else f[i][j]=min(f[i][j],la+bb);
	 }
	 ans=1e16;
	 for(i=0;i<=n;i++)ans=min(ans,f[n][i]);
	 printf("%lld",ans);
}

Measured memory_bytes: 195712
n= int(input())
a,b = map(int,input().split())
p = input().split()
p =[int(p) for p in p]

q1 = sum(x <= a for x in p) 
q2= sum((b >= x > a)  for x in p) 
q3= sum(x > b  for x in p) 

q =[q1,q2,q3]

print(min(q))

Measured memory_bytes: 3064
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        var n = int.Parse(Console.ReadLine());
        var s = Console.ReadLine();
        var x = s.Replace("ABC", "");
        Console.WriteLine((s.Length - x.Length) / 3);
    }
}

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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)

int main()
{
    int D;
    cin >> D;
    vector<int> c(26);
    vector<vector<int>> s(D, vector<int>(26));
    vector<vector<pint>> ss(D, vector<pint>(26));
    REP(i, 26)
    cin >> c[i];
    REP(i, D)
    REP(j, 26){
        cin >> s[i][j];
        ss[i][j].first = s[i][j];
        ss[i][j].second = j;
    }
    REP(i,D)sort(ALL(ss[i]),greater<pint>());
    lint sat = 0;
    vector<int> las(26, -1), con(D);

    vector<int> t(D);
    REP(i, D)
    {
        t[i] = (i) % 26 + 1;
        //cout << (i) % 26 + 1 << "\n";
    }
    REP(i, D)
    {
        int tt = t[i] - 1;
        con[i] = tt;
        las[tt] = i;
        sat += s[i][tt];
        REP(j, 26)
        sat -= c[j] * (i - las[j]);
    }
    REP(i, D)
    {
        t[i] = ss[i][0].second + 1;
        lint sat2 = 0;
        vector<int> las2(26, -1);
        REP(i, D)
        {
            int tt = t[i] - 1;
            con[i] = tt;
            las2[tt] = i;
            sat2 += s[i][tt];
            REP(j, 26)
            sat2 -= c[j] * (i - las2[j]);
        }
        if(sat2>sat){
            sat = sat2;
        }else{
            t[i] = (i) % 26 + 1;
        }
    }
    REP(i, D)
    {
        cout << t[i] << "\n";
    }
    /*

    REP(i, D)
    {
        int tt = t[i] - 1;
        con[i] = tt;
        las[tt] = i;
        sat += s[i][tt];
        REP(j, 26)
        sat -= c[j] * (i - las[j]);
    }
    cout << sat << "\n";
    REP(i, m)
    {
        int d, q;

        t[d - 1] = q;
        lint sat2 = 0;
        vector<int> las2(26, -1);
        REP(i, D)
        {
            int tt = t[i] - 1;
            con[i] = tt;
            las2[tt] = i;
            sat2 += s[i][tt];
            REP(j, 26)
            sat2 -= c[j] * (i - las2[j]);
        }
        cout << sat2 << "\n";
    }
    */
    return 0;
}

Measured memory_bytes: 3784
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def func(A):
    N = len(A)
    tmp = N // 2
    T = [0 for _ in range(N)]
    for i in range(0, Math.round_up(N, 2)):
        T[tmp] = A[i]
        if i % 2 == 0:
            tmp -= (i + 1) * 2
        else:
            tmp += (i + 1) * 2
    tmp = N // 2 - 1
    for i in range(0, N // 2):
        T[tmp] = A[N - i - 1]
        if i % 2 == 0:
            tmp += (i + 1) * 2
        else:
            tmp -= (i + 1) * 2
    res = 0
    for i in range(N - 1):
        res += abs(T[i + 1] - T[i])
    return res


def solve():
    N = Scanner.int()
    A = [0 for _ in range(N)]
    for i in range(N):
        A[i] = Scanner.int()
    A.sort()
    ans = func(A)
    A.sort(reverse=True)
    ans = max(ans, func(A))
    print(ans)


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()


Measured memory_bytes: 10436
import java.util.Scanner;
import java.util.stream.Stream;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long[] items = Stream.of(sc.nextLine().split(" ", 0)).mapToLong(Long::parseLong).toArray();
		sc.close();
		Main main = new Main();
		if (items[0] > items[1]) {
			long let = items[0];
			items[0] = items[1];
			items[1] = let;
		}
		long ans = items[0] / main.GCD(items[0], items[1]) * items[1];
		System.out.println(ans);
	}

	public long GCD(long num1, long num2) {
		if (num2 == 0) {
			return num1;
		} else {
			return GCD(num2, num1 % num2);
		}
	}


}

Measured memory_bytes: 29008
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
#define reps(i,j,n) for(int i = j ; i < n ; ++i)
#define rep(i, n) reps(i,0,n)
#define fr first
#define sc second
#define SQR(x) ((x)*(x))
#define INF (1 << 28)
typedef pair< int , int > Pt;

int main(){
  int Q;
  int N,X,Y;
  Pt Natume;
  int x[1000],y[1000];
  vector< vector<int> > set;

  cin >> Q;
  while(Q--){
    //Input
    cin >> N >> X >> Y;
    cin >> Natume.sc >> Natume.fr;
    set.resize(N);
    rep(i,N) cin >> x[i] >> y[i];
    rep(i,N) rep(j,N){
      if(SQR(x[i] - x[j]) + SQR(y[i] - y[j]) <= 2500){
        set[i].push_back(j);
      }
    }
    //end

    queue< Pt > que;
    int Say[1000],cnt = 0,flg = 1,END = 1000,ans = 0;
    fill_n(Say,1000,-INF);
    rep(i,N){
      if(SQR(x[i] - Natume.sc) + SQR(y[i] - Natume.fr) <= 100){
        Say[i] = X;
        que.push(Pt(X,i));
      }
    }
    while(!que.empty()){
      Pt p = que.front(); que.pop();
      int now = p.sc , time = p.fr;
      cnt++;
      if(cnt > END){ flg--; break; }
      rep(i,set[now].size()){
        int e = set[now][i];
        if(time - Say[e] > Y){
          Say[e] = X + time;
          que.push(Pt(Say[e],e));
        }
      }
    }
    rep(i,N) ans = max(ans,Say[i]);

    //Output
    if(!flg) cout << "You're always welcome!" << endl;
    else cout << ans << endl;
    //end

    set.clear();
    }
}

Measured memory_bytes: 5228
#include <iostream>

using namespace std;

int main()
{
	int w, h, x, y, r;
	cin >> w >> h >> x >> y >> r;

	if (0 <= x - r && x + r <= w && 0 <= y - r && y + r <= h)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
}


Measured memory_bytes: 3104
import Foundation

let input = readLine()!.components(separatedBy: " ").map({Double($0)!})

let A = input[0]
let B = input[1]
let H = input[2]
let M = input[3]

/*

何をする問題か
角度の差を求めて距離を出す

*/

//時針は1時間で何度動くか？
//30度
let HourAnglePerH = 360.0/12
//時針は1分で何度動くか？
//0.5度
let HourAnglePerM = 360.0/(12*60)
//時針の角度は？
let HourAngle = HourAnglePerH*H + HourAnglePerM*M

//分針は1分で何度動くか？
//6度
let MinuteAnglePerM = 360.0/60
//分針の角度は？
let MinuteAngle = MinuteAnglePerM*M

//分針と時針の角度の差は？
//差が180を超える時、360からそれを引く。
var angleDiff: CGFloat = CGFloat(abs(HourAngle-MinuteAngle))
if angleDiff > 180 {
    angleDiff = 360-angleDiff
}

//角度の差と針の長さから距離を求める
//a2 = b2+c2-2bc cosA

var cosValue:Double
cosValue = Double(cos(CGFloat.pi/180.0*angleDiff))
let temp:Double = Double((A*A))+Double((B*B))-(Double(2*A*B)*cosValue)

print(sqrt(temp))

Measured memory_bytes: 14692
#include <stdio.h>

void insertion(int [], int, int);

int cnt = 0;

int main(){
  int i, n, m = 10, A[1000000], G[] = {10000, 5000, 2500, 1000, 500, 100, 50, 10, 4, 1}, m_cnt = 0;

  scanf("%d", &n);

  for(i=0; i<n; i++) scanf("%d", &A[i]);

  for(i=0; i<m; i++)
    if(G[i] <= n){
      insertion(A, n, G[i]);
      m_cnt++;
    }

  printf("%d\n", m_cnt);
  for(i=0; i<m-1; i++) if(G[i] <= n) printf("%d ", G[i]);
  printf("%d\n%d\n", G[i], cnt);

  for(i=0; i<n; i++) printf("%d\n", A[i]);
  
  return 0;
}

void insertion(int A[], int n, int g){
  int i, j, v;

  for(i=g; i<n; i++){
    v = A[i];
    j = i - g;
    while(j >= 0 && A[j] > v){
      A[j+g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j+g] = v;
  }
}

Measured memory_bytes: 4504
#include<iostream>
#include<cmath>
#include<algorithm>
#include <cassert>
#include<string>
#include<vector>
#include <utility>
#include <math.h>
#define INF 1000000000;
using namespace std;
char mas[200500];
int main() {
	int a = 0,b=0,e=0;
	int c[30] = { 0 }, d[30] = { 0 };
	cin >> a;
	for (int i = 0; i < a; i++) {
		cin >> c[i];
	}
	for (int i = 0; i < a; i++) {
		cin >> d[i];
	}
	for (int i = 0; i < a; i++) {
		if (c[i] >= d[i]) {
			e += c[i];
			e -= d[i];
		}
	}
	cout << e << endl;
	return 0;
}

Measured memory_bytes: 256
#%%
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))

ans = 0
tmp = 0

for i in range(N):
    ans += (A[i]+tmp)//2
    if tmp == 1 and A[i] == 0:
        tmp = 0
    else:
        tmp = (A[i]+tmp)%2

print(ans)

Measured memory_bytes: 7072
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
    ll a, b;
    bool operator < (ii o) const {
        return tie(a, b) < tie(o.a, o.b);
    }
};
struct iii {
    ll a, b, c;
    bool operator < (iii o) const {
        return tie(a, b, c) < tie(o.a, o.b, o.c);
    }
};

const ll MOD = 1000000007;
const ll INF = 2000000000;
const ll MAXN = 1000005;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    
    ll N;
    cin >> N;
    vector<ii> v(N);
    for (ll i = 0; i < N; i++) {
        cin >> v[i].a >> v[i].b;
    }
    
    ll plusMin = INF, plusMax = -INF, minusMin = INF, minusMax = -INF;
    ll ans = -INF;
    for (auto i: v) {
        ll plus = i.a + i.b;
        ll minus = i.a - i.b;
        ans = max(ans, plus - plusMin);
        ans = max(ans, plusMax - plus);
        ans = max(ans, minus - minusMin);
        ans = max(ans, minusMax - minus);
        
        plusMin = min(plusMin, plus);
        plusMax = max(plusMax, plus);
        minusMin = min(minusMin, minus);
        minusMax = max(minusMax, minus);
    }
    cout << ans << "\n";
}


Measured memory_bytes: 6364
#include <bits/stdc++.h>
#define repp(i,l,r)for(long long i=(l);i<(r);i++)
#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define per(i,n) for (long long i = (n); i >= 0; --i)
const int INF = 2147483647;//int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//ミョ(-ω- ?)
int main() {
    int n;
    cin >> n;
    cout << n*(n+1)/2 << endl;
    return 0;
}

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

const int MAXN = 112345;

vector <pair <int, int> > g[MAXN];
map <pair <int, int>, vector <int> > gc;
map <pair <int, int>, int> dist;

int main () {

	int n, m;
	cin >> n >> m;

	for (int i = 0; i < m; ++i) {
		int x, y, c;
		scanf ("%d %d %d", &x, &y, &c);
		
		g[x].push_back (make_pair (y, c));
		g[y].push_back (make_pair (x, c));
		gc[make_pair (x, c)].push_back (y);
		gc[make_pair (y, c)].push_back (x);
	}

	deque <pair <int, int> > q;
	q.push_front (make_pair (1, -1));

	while (!q.empty()) {
		int x = q.front().first;
		int c = q.front().second;
		int d = dist[make_pair (x, c)];

		q.pop_front();

		if (c == -1) {
			for (int i = 0; i < g[x].size(); ++i) {
				int y = g[x][i].first;
				c = g[x][i].second;
				pair <int, int> p = make_pair (y, c);
				if ( !dist.count (p) ) {
					dist[ p ] = d;
					q.push_front ( p );
				}
			}
		} else {
			vector <int> & adj = gc[ make_pair (x, c) ];
			for (int i = 0; i < adj.size(); ++i) {
				int y = adj[i];
				pair <int, int> p = make_pair (y, c);
				if ( !dist.count (p) ) {
					dist[p] = d;
					q.push_front (p);
				}
			}
			pair <int, int> p = make_pair (x, -1);
			if (!dist.count (p)) {
				dist[p] = d + 1;
				q.push_back (p);
			}
		}
	}

	if (!dist.count (make_pair (n, -1)))
		cout << -1 << endl;
	else
		cout << dist[make_pair (n, -1)] << endl;

	return 0;
}

Measured memory_bytes: 83584
#include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
typedef vector<string> vstring;
#define _GLIBCXX_DEBUG
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
const int INF = 1 << 30;
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,-1,1};
#define stp(x)  setprecision(x)
const long double PI=3.14159265358979323846264338327950;
int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);  
  int q;
  cin>>q;
  rep(i,q){
    int c,a,n;
    int count=0;
    cin>>c>>a>>n;
    while(n>0){
      if(c<=0 || a<=0){
        break;
      }
      c--; 
      a--;
      n--;
      count++;
    }

    while(a>0){
      if(c<=1){
        break;
      }
      a--;
      c-=2;
      count++;
    }
    while(c>=3){
      c-=3;
      count++;
    }
    cout<<count<<'\n';
  }

  return(0);
}


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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;

int main() {
    int h, n;
    cin >> h >> n;
    vector<ll>dp(h+10,inf);
    vector<int>a(n),b(n);
    int p =0;
    rep(i, 0, n){
        cin >> a[i] >> b[i];
        p = max(p, a[i]);
    }
    dp[0]=0;
    rep(i, 0, n){
        rep(j, 0, h+1){
            chmin(dp[min(j+a[i], h)],dp[j]+b[i]);
        }
    }
    cout << dp[h] << endl;
}

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

int main(){
  int n, k;
  cin >> n >> k;
  vector<int> pn(n), pt(n);
  for(int i=0; i<n; ++i){
    cin >> pn[i];
    --pn[i];
  }
  for(int i=0; i<n; ++i) cin >> pt[i];
  ll ans = -1001001001;
  for(int i=0; i<n; ++i){
    int pos = pn[i], cnt = 1;
    ll nans = pt[pos];
    while(cnt < k && pos != i){
      ans = max(ans, nans);
      pos = pn[pos];
      nans += pt[pos];
      ++cnt;
    }
    ans = max(ans, nans);
    if(nans <= 0 || k == cnt) continue;
    if(k/cnt != 1){
      nans *= k/cnt -1;
      cnt = (k/cnt -1) * cnt;
    }
    while(cnt < k){
      ans = max(ans, nans);
      pos = pn[pos];
      nans += pt[pos];
      ++cnt;
    }
    ans = max(ans, nans);
  }
  cout << ans << endl;
}


Measured memory_bytes: 3668
def get_num(n, m):
    ans = 0
    for x in range(min(n, m), (m + 2) / 3, -1):
        for y in range(x - 1, 1, -1):
            if x + 2 * y - 1 < m:
                break
            for z in range(y - 1, 0, -1):
                s = x + y + z
                if s == m:
                    ans += 1
                    break
    return ans


while True:
    [n, x] = [int(m) for m in raw_input().split()]
    if [n, x] == [0, 0]:
        break
    print(get_num(n, x))

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

#define FOR(i, a, b)  for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << "\n"
#define _ << " _ " <<
#define ll long long

using namespace std;

string s;
int n;
int num[30];

int main() {
  ios_base::sync_with_stdio(false);

  cin >> s;
  n = s.size();
  REP(i, n) {
    num[s[i] - 'a']++;
  }

  ll out = (ll) n * (ll) (n - 1) / 2LL;
  REP(i, 30) {
    ll tr = (ll) num[i] * (ll) (num[i] - 1) / 2LL;
    out -= tr;
  }
  cout << out + 1LL << endl;

  return 0;
}


Measured memory_bytes: 720
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <map>

using namespace std;

using ll = long long;
using P = pair<ll,ll>;

#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = ((ll)(x) - 1); i >= 0; i--)

#define _upgrade ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

//ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

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

const ll mod = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.14159265358979323846;


// ****************************************CODE***************************************//




const int V = 100005;
//こうやって初期化できる
vector<int> to[V*2];
bool visited[V*2];

//左側と右側の頂点の個数を求めるために２つの配列を用意してチェック
vector<int> cnt;
void dfs(int v){
        if(visited[v]) return;
        visited[v] = true;
        //このテクニックさすがすぬけさん
        cnt[v/V]++;
        //全てのvから張られている頂点をなめる
        for(int u : to[v]) dfs(u);
}

int main(){
        _upgrade;
        int n;
        cin >> n;
        for(int i = 0; i < n; i++)
        {
                int x, y;
                cin >> x >> y;
                y += V;
                //無向グラフの作成
                to[x].push_back(y);
                to[y].push_back(x);
        }

        ll ans = 0;
        for(int i = 0; i < V*2; i++)
        {
                if(visited[i]) continue;
                //初期化
                cnt = vector<int>(2);
                dfs(i);
                //キャストしなければならない
                ans += (ll)cnt[0]*cnt[1];
        }
        //全体の辺から張られてる
        ans -= n;
        cout << ans << endl;
        return 0;
}


Measured memory_bytes: 10240
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <iterator>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define MOD 1000000007
using namespace std;


int main(int argc, char* argv[])
{
    long a, b,c;
    scanf("%ld %ld %ld", &a, &b, &c);
    long k;
    scanf("%ld", &k);
    long max = MAX(MAX(a,b),c);
    long tmp = (max<<k)-max;
    printf("%ld\n", a+b+c+tmp);

    return 0;
}


Measured memory_bytes: 128
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String a = scan.next();
        int ans=0;
        if(a.equals("MON"))ans=6;
        if(a.equals("TUE"))ans=5;
        if(a.equals("WED"))ans=4;
        if(a.equals("THU"))ans=3;
        if(a.equals("FRI"))ans=2;
        if(a.equals("SAT"))ans=1;
        if(a.equals("SUN"))ans=7;
        System.out.println(ans);
    }
}


Measured memory_bytes: 23636
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long int ll;
#define pb push_back
#define mp make_pair;


int main(){
	int n,a[100],b[100],c[100],i;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];

	for(i=0;i<n;i++)
		cin>>b[i];
	
	for(i=0;i<n;i++)
		c[i]=i+1;
	int ai=0,bi=0,count=0;
	do { 
         count++;
         int val=1;
         for(i=0;i<n;i++){
         	if(a[i]!=c[i]){
         		val=0;
         	}
         }
         if(val)
         	ai=count;
         val=1;
         for(i=0;i<n;i++){
         	if(b[i]!=c[i]){
         		val=0;
         	}
         }
         if(val)
         	bi=count;
    } while (next_permutation(c, c + n));
    int ans = abs(ai-bi);
    printf("%d\n", ans);
	return 0;	
}


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

#define for_(i,a,b) for(int i=(a);i<(b);++i)
#define size_of(a) (int)(a).size()
#define allof(a) (a).begin(),(a).end()

int N, T, S, A[3030], B[3030];
int dp[3030][3030];

void maxUpdate(int& a, int b) {
	a = max(a, b);
}

int main() {
	cin >> N >> T >> S;
	for_(i,0,N) cin >> A[i] >> B[i];
	
	memset(dp, 0, sizeof(dp));
	
	for_(i,0,N) for_(j,0,T+1) {
		maxUpdate(dp[i + 1][j], dp[i][j]);
		int nj = (j < S && S < j + B[i]) ? S + B[i] : j + B[i];
		if (nj <= T) maxUpdate(dp[i + 1][nj], dp[i][j] + A[i]);
	}
	
	int ans = 0;
	for_(i,0,T+1) maxUpdate(ans, dp[N][i]);
	cout << ans << endl;
}

Measured memory_bytes: 37056
using System;
using System.Linq;

namespace AtCoder.Abcdef
{
    public class QuestionB
    {
        public static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            //Input string
            string s = Console.ReadLine();
            string t = Console.ReadLine();

            int res = 0;
            foreach (var item in s.Zip(t))
            {
                if (item.First != item.Second) res++;
            }
            Console.WriteLine(res);

            Console.Out.Flush();
        }
    }
}


Measured memory_bytes: 27804
MOD = 10**9 + 7

n,m = (int(num) for num in input().split())
s = [int(num) for num in input().split()]
t = [int(num) for num in input().split()]

dp = [[1]*(m+1) for _ in range(n+1)]

for i in range(1,n+1):
    for j in range(1,m+1):
        dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1]
        if s[i-1] == t[j-1]:
            dp[i][j] += dp[i-1][j-1]
        dp[i][j] = dp[i][j] % MOD
        
        
print(dp[n][m])


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

string s;
int c;
int N;

string f() {
	string ret;
	int p = 0;
	while(c < N && s[c] != ']') {
		if(s[c] == '[') {
			c++;
			string res = f();
			reverse(res.begin(), res.end());
			ret += res;
			assert(s[c] == ']');
			c++;
		}
		else if(s[c] == '+') {
			p++;
			c++;
		}
		else if(s[c] == '-') {
			p--;
			c++;
		}
		else {
			if(s[c] == '?') {
				ret += "A";
			}
			else {
				while(p < 0) p += 26;
				ret.push_back('A' + (s[c] - 'A' + p) % 26);
			}
			p = 0;
			c++;
		}
	}
	return ret;
}

void solve() {
	N = s.size();
	c = 0;
	cout << f() << endl;
}

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

	while(cin >> s, s != ".") {
		solve();
	}
}

Measured memory_bytes: 3228
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

int d[2005][2005];
int main() {
   cin.tie(0);
   ios_base::sync_with_stdio(0);

   int n, x, y;
   cin >> n >> x >> y;

   rep(i, 1, n + 1) {
      rep(j, 1, i) d[i][j] = min(i - j, abs(x - j) + abs(y - i) + 1);
   }
   vector<int> ans(n);
   rep(i, 1, n + 1) rep(j, 1, i) ans[d[i][j]]++;
   rep(i, 1, n) cout << ans[i] << endl;
   return 0;
}

Measured memory_bytes: 15104
#include <iostream>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <string>
#include <set>
#include <iomanip>
#define REP(i,n) for (int i=0;i<(n);++i)
using namespace std;

int main(){
    int n, y;
    cin >> n >> y;

    for(int i = 0; i <= n; ++i){
        for(int j = 0; j <= n - i; ++j){
            int k = n - i - j;
            int num = 10000 * i + 5000 * j + 1000 * k;
                if(num == y){
                    cout << i << ' ' << j << ' ' << k << endl;
                    return 0;
                }
        }
    }
    cout << "-1 -1 -1" << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n;cin>>n;
  vector<int>po;
  po.pb(0);
  int ans=0;
  for(int i=1;i<=n;i++){
    int a;cin>>a;
    po.pb(a);
    ans+=abs(po[i]-po[i-1]);
  }
  ans+=abs(po[po.size()-1]);
  po.pb(0);
  for(int i=1;i<=n;i++){
    if(po[i-1]<=po[i]&&po[i]<=po[i+1])
      cout<<ans<<endl;
    else
      cout<<ans-abs(po[i+1]-po[i])-abs(po[i]-po[i-1])+abs(po[i+1]-po[i-1])<<endl;;
  }
  return 0;
}


Measured memory_bytes: 1784
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	var N, K int
	scanner.Scan()
	fmt.Sscanf(scanner.Text(), "%d %d", &N, &K)

	if N%K == 0 {
		fmt.Println(0)
	} else {
		fmt.Println(1)
	}

}


Measured memory_bytes: 640
#include<stdio.h>

int main(){
		int n,i;
		long min,max,t;
		scanf("%d",&n);
		for(i=0;i<n;i++){
				scanf("%d",&t);
				if(i==0){
						min=t;
						max=t;
				}
				else{
						if(t<min)min=t;
						if(t>max)max=t;
				}
		}
		printf("%d\n",max-min);

		return 0;
}


Measured memory_bytes: 128
while True:
  n,x = map(int, input().split())
  if n ==0 and x ==0:
    break
  
  ans =0
  for a in range(1,n+1):
    for b in range(1,n+1):
      if b <= a:
          continue
      c = x-(a+b)
      if c >b and c <=n:
         ans +=1
  print('%d'%(ans))


Measured memory_bytes: 5592
S="Z"+input()+"Z";i=j=c=0
while i<len(S)or j<len(S):
    if S[i]==S[~j]:i+=1;j+=1
    elif S[i]=="x":c+=1;i+=1
    elif S[~j]=="x":j+=1
    else:print(-1);quit()
print(c)

Measured memory_bytes: 3316
package main

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

type IntListForSort []int

func (s IntListForSort) Len() int           { return len(s) }
func (s IntListForSort) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s IntListForSort) Less(i, j int) bool { return s[i] < s[j] }


func sortIntList(intList []int) {
	sort.Sort(IntListForSort(intList))
}

type IntListForSortDesc []int

func (s IntListForSortDesc) Len() int           { return len(s) }
func (s IntListForSortDesc) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s IntListForSortDesc) Less(i, j int) bool { return s[i] > s[j] }

func sortIntListDesc(intList []int) {
	sort.Sort(IntListForSortDesc(intList))
}

// ASC前提
// 見つからない場合は前提じゃない
func binarySearchInt(intList []int, key int, iMin int, iMax int) int {
	if iMax < iMin {
		return -1
	} else {
		iMid := iMin + (iMax - iMin) / 2
		if intList[iMid] > key {
			return binarySearchInt(intList, key, iMin, iMid - 1)
		} else if intList[iMid] < key {
			return binarySearchInt(intList, key, iMid + 1, iMax)
		} else {
			return iMid
		}
	}
}

func input() string {
	var s string
	_, _ = fmt.Scan(&s)
	return s
}

func inputLn() string {
	var s string
	_, _ = fmt.Scanln(&s)
	return s
}

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

func convListStrToInt(strList []string) []int {
	intList := make([]int, len(strList))
	for k, v := range strList {
		intV, _ := strconv.Atoi(v)
		intList[k] = intV
	}
	return intList
}

func convListStrToFloat(strList []string) []float64 {
	intList := make([]float64, len(strList))
	for k, v := range strList {
		intV, _ := strconv.Atoi(v)
		intList[k] = float64(intV)
	}
	return intList
}

func main() {
	n, _ := strconv.Atoi(inputLn())
	aList := make([]int, n)
	for i := 0; i < n; i++ {
		a, _ := strconv.Atoi(inputLn())
		aList[i] = a
	}
	aListClone := make([]int, n)
	_ = copy(aListClone, aList)
	sortIntList(aListClone)
	for _, a := range aList {
		tgIndex := binarySearchInt(aListClone, a, 0, len(aListClone)-1)
		if tgIndex == n-1 {
			fmt.Println(aListClone[n-2])
		} else {
			fmt.Println(aListClone[n-1])
		}
	}
}


Measured memory_bytes: 9856
#include<iostream>
#include<queue>
#include<vector>

using namespace std;

typedef pair<int,int> P;
const int MAX = 21;
int M[MAX][MAX];
int sx,sy,gx,gy;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

struct State{
  int x,y,t;
  vector<P> V;
  State(){}
  State(int x, int y,int t):x(x),y(y),t(t){}
};

int w,h;

void input(){
  for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++){
      cin >> M[i][j];
      if(M[i][j] == 2){
	sx = j;
	sy = i;
      }
      if(M[i][j] == 3){
	gx = j;
	gy = i;
      }
    }
}

bool isInside(int x, int y){ return 0<=x&&x<w&&0<=y&&y<h;}

bool isBroken(const vector<P>& V, int x, int y){
  for(int i = 0; i < (int)V.size(); i++)
    if(V[i].first == x && V[i].second == y) return true;
  return false;
}

P Move(State s, int d){

  int x = s.x;
  int y = s.y;

  while(isInside(x,y)){
    if(x == gx && y == gy) return P(x,y);
    if(M[y][x] == 1 && !isBroken(s.V,x,y)) return P(x-dx[d],y-dy[d]);
    x+=dx[d];
    y+=dy[d];
  }
  return P(-1,-1);
}

void print(vector<P> V){
  cout << "print   " << endl;
  for(int i = 0; i < (int)V.size(); i++)
    cout << V[i].first << " " << V[i].second << endl;
}
int solve(){

  queue<State> Q;
  Q.push(State(sx,sy,0));

  while(!Q.empty()){
    State now  = Q.front();
    Q.pop();

    if(now.t > 10) break;
    if(now.x == gx && now.y == gy) return now.t;

    //  cout <<  now.x << " " << now.y << " " << now.t << endl;

    //   if(now.x == 3 && now.y == 0 && h == 1) print(now.V);
    for(int i = 0; i < 4; i++){

      int checkX = now.x+dx[i];
      int checkY = now.y+dy[i];

      // cout << "check " << checkX << " " << checkY << endl;
      if(!isInside(checkX, checkY)) continue;
      if(M[checkY][checkX] == 1 && !isBroken(now.V,checkX,checkY)) continue;
				   
      P p = Move(now, i);

      //   cout << "from " << now.x << " " << now.y << " " << now.t << endl;
      //cout << "to " << p.first << " " << p.second << endl;
      if(p == P(-1,-1)) continue;
      State nex = now;
      nex.x = p.first;
      nex.y = p.second;
      nex.t++;

      p.first += dx[i];
      p.second += dy[i];
      nex.V.push_back(p);
      Q.push(nex);
    }
  }
  return -1;
}

int main(){
  while(cin >> w >> h && w+h){
    input();
    cout << solve() << endl;
  }
  return 0;
}

Measured memory_bytes: 32100
from collections import deque
import heapq
N,M = map(int,input().split())
A = list(map(int,input().split()))
G = {i:[] for i in range(N)}
for _ in range(M):
    x,y = map(int,input().split())
    G[x].append(y)
    G[y].append(x)
F = []
hist = [0 for _ in range(N)]
for i in range(N):
    T = []
    if hist[i]==0:
        que = deque([i])
        hist[i]=1
        heapq.heappush(T,A[i])
        while que:
            x = que.popleft()
            for y in G[x]:
                if hist[y]==0:
                    que.append(y)
                    hist[y]=1
                    heapq.heappush(T,A[y])
        F.append(T)
K = len(F)
if K>N//2+1:        
    cnt = -1
elif K==1:
    cnt = 0
else:
    cnt = 0
    for i in range(K):
        cnt += heapq.heappop(F[i])
    F1 = []
    for T in F:
        F1 += T
    F1 = sorted(F1)
    for i in range(K-2):
        cnt += F1[i]
if cnt<0:
    print("Impossible")
else:
    print(cnt)

Measured memory_bytes: 101144
// motatoes cp snippet
#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define ll long long

#define forr(a,b,v) for(int v=a; v<=b; v++)
#define ford(a,b,v) for(int v=a; v>=b; v--)

// https://www.topcoder.com/community/competitive-programming/tutorials/power-up-c-with-the-standard-template-library-part-1/#map
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef pair< int,int > ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())


string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

// get default value from map type
template<class K, class V>
V get(map<K,V> m, K k, V def) {
    if (m.find(k) != m.end()) {
        return m[k];
    } else {
        return def;
    }
}

// <geometry>
const double PI = acos(-1.0);

double toDegreeFromMinutes(double minutes) {
  return (minutes/60);
}

double toRadians(double degree) {
  return (degree * PI/180.0);
}

double toDegree(double radian) {
  if (radian < 0) radian += 2 * PI;
  return (radian * 180 / PI);  
}

double fixAngle(double A) {
  return A > 1 ? 1 : (A < -1 ? -1 : A);
}

// sin(A)/a = sin(B)/b = sin(C)/c
double getSide_a_bAB(double b, double A, double B) {
  return (sin(A)*b)/sin(B);
}

double getAngle_A_abB(double a, double b, double B) {
  return asin( fixAngle(  (a*sin(B))/b ));  
}

// a^2 = b^2 + c^2 - 2*b*c*cos(A)
double getAngle_A_abc(double a, double b, double c) {
  return acos( fixAngle(  (b*b*c*c-a*a) / (2*b*c)  ) );  
}
// </geometry>


#define INF 1e9+5

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

    int n;
    cin >> n;
    int REW[n][3];
    for (int i=0; i<n; i++) {
        for (int j=0; j<3; j++) {
            cin >> REW[i][j];
        }
    }   
    vector< vector<ll> > dp(n+5, vector<ll>(3, 0));
    dp[1][0] = REW[0][0];
    dp[1][1] = REW[0][1];
    dp[1][2] = REW[0][2];


    for (int i=2; i<=n; i++) {
        dp[i][0] = REW[i-1][0] + max(dp[i-1][1], dp[i-1][2]);
        dp[i][1] = REW[i-1][1] + max(dp[i-1][0], dp[i-1][2]);
        dp[i][2] = REW[i-1][2] + max(dp[i-1][0], dp[i-1][1]);
    }

    ll mx;

    cout << max(dp[n][0], max(dp[n][1], dp[n][2]));

    return 0;
}



Measured memory_bytes: 6912
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int N = input.nextInt();
		int W = input.nextInt();
		long[][] dp = new long[N+1][W+1];
		int[][] items = new int[N][2]; //(weight,value)
		for (int i = 0; i < N; i++) {
			items[i][0] = input.nextInt();
			items[i][1] = input.nextInt();
		}
		int STARTWEIGHT = items[0][0];
		for (int i = STARTWEIGHT; i <= W; i++) {
			dp[1][i] = items[0][1];
		}
		for (int item = 2; item <= N; item++) {
			for (int weight = 1; weight <= W; weight++) {
				int curW = items[item-1][0]; //Current weight
				int curV = items[item-1][1]; //Current value
				dp[item][weight]=dp[item-1][weight];
				if (weight>=curW) {
					dp[item][weight]=Math.max(dp[item][weight], curV);
					dp[item][weight]=Math.max(dp[item-1][weight-curW]+curV, dp[item][weight]);
				} 
			}
		} 
		/*for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= W; j++) {
				System.out.print(dp[i][j]+" ");
			}
			System.out.print("\n");
		}*/
		System.out.println(dp[N][W]);
	}
}

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

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

  ll x,y;
  for(ll i=1;i*i<=n;i++){
    if(n%i==0){
      x=i;
      y=n/i;
    }
  }
  cout<<x+y-2<<endl;
  
  return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public static void Main( )
    {
        var input = Console.ReadLine( ).Split( ).Select( int.Parse ).ToArray( );
        int A = input[ 0 ];
        int B = input[ 1 ];
        long result = 1L * A / GCD( A, B ) * B;
        Console.WriteLine( result );
    }
    public static int GCD( int a, int b )
    {
        if ( b == 0 ) return a;
        return GCD( b, a % b );
    }


}

Measured memory_bytes: 28228
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(n);
    rep(i, n) cin >> a[i] >> b[i];
    vector<int> c(m), d(m);
    rep(i, m) cin >> c[i] >> d[i];

    rep(i, n){
        int ans = m;
        int di = abs(a[i] - c[m - 1]) + abs(b[i] - d[m - 1]);
        for(int j = m - 2; j >= 0; j--){
            int dist = abs(a[i] - c[j]) + abs(b[i] - d[j]);
            if(dist <= di){
                di = dist;
                ans = j + 1;
            }
        }
        cout << ans << endl;
    }
    
}

Measured memory_bytes: 256
#!/usr/bin/env python3

def solve_short(S: str):
    print(2*min(S.count('0'), S.count('1')))

def solve(S: str):
    stack = []
    N = len(S)
    for i, s in enumerate(S):
        if i == 0:
            stack.append(s)
        else:
            if len(stack) == 0:
                stack.append(s)
            else:
                if s != stack[-1]:
                    tmp = stack.pop()
                else:
                    stack.append(s)
    print(N - len(stack))

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    S = str(input())
    solve_short(S)

if __name__ == '__main__':
    main()


Measured memory_bytes: 3188