akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

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

    int ma = -1;
    for (int i = 0; i < n; i++) {
        ma = max(ma, a[i]);
    }

    int k;
    int d = 1000000010;
    for (int i = 0; i < n; i++) {
        if (a[i] == ma) continue;
        if (a[i] <= ma / 2) {
            if (ma / 2 - a[i] < d) {
                k = a[i];
                d = ma / 2 - a[i];
            }
        } else {
            if (a[i] - (ma + 1) / 2 < d) {
                k = a[i];
                d = a[i] - (ma + 1) / 2;
            }
        }
    }
    cout << ma << " " << k << endl;
    return 0;
}

Measured memory_bytes: 1152
import sys
n, m = map(int, input().split())
s = input()
p = n
hist = []
while p>0:
  for i in range(max(0,p-m), p):
    if s[i]=='0':
      hist.append(p-i)
      p = i
      break
  else:
    print(-1)
    sys.exit()
print(*hist[::-1])

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

vector<int> vec;
long long cal(int n){
    if(vec[n]!=-1) return vec[n];
    if(n==0) return 0;
    if(n==1) return 1;
    return vec[n]=cal(n-1)+cal(n-2);
}

int main(void){
    int x;
    
    cin >> x;
    x=x+1;
   vec.assign(x+1,-1);
    
    cout << cal(x) <<endl;
}





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

int main(void){
    int a, b, c, x, answer;
    cin >> a >> b >> c;
    x = a-b;

    if(x < c){
        answer = c - x;
    }
    else if(x > c){
        answer = 0;       // To prevent the answer from being a negative integer.
    } 

    cout << answer << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int dp[20100];
int main(void){
	int n,w;
	scanf("%d %d",&n,&w);
	int i,j;
	for(i=0;i<=w;i++){
		dp[i]=-1;
	}
	dp[0]=0;
	for(i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		for(j=0;j<=w;j++){
			if(dp[j]>=0 && dp[j+b]<dp[j]+a && j+b<=w){
				dp[j+b]=dp[j]+a;
			}
		}
	}
	int max=0;
	for(i=0;i<=w;i++){
		if(dp[i]>max){
			max=dp[i];
		}
	}
	printf("%d\n",max);
	return 0;
}

Measured memory_bytes: 1064
#include <stdio.h>

main(){
  int sic, gic, sh, sm, gh, gm, h, time, ans;
  char a, b, c, d;
  int data[7][7]={0,300,500,600,700,1350,1650,6,0,350,450,600,1150,1500,13,7,0,250,400,1000,1350,18,12,5,0,250,850,1300,23,17,10,5,0,600,1150,43,37,30,25,20,0,500,58,52,45,40,35,15,0};

  while(1){
    scanf("%d", &sic);
    getchar();
    if(sic==0) break;

    scanf("%c%c %c%c", &a, &b, &c, &d);
    sh=(a-'0')*10+b-'0';
    sm=(c-'0')*10+d-'0';

    scanf("%d", &gic);
    getchar();

    scanf("%c%c %c%c", &a, &b, &c, &d);
    gh=(a-'0')*10+b-'0';
    gm=(c-'0')*10+d-'0';

    h=0;

    if(sic>gic){
      time=gic;
      gic=sic;
      sic=time;
    }
    sic--;
    gic--;
    time=sh*100+sm;
    if(1730<=time && time<=1930){
      if(data[gic][sic]<=40) h=1;
    }
    time=gh*100+gm;
    if(1730<=time && time<=1930){
      if(data[gic][sic]<=40) h=1;
    }

    ans=data[sic][gic];
    if(h==1){
      ans/=2;
      if(ans%50==25) ans+=25;
    }

    printf("%d\n", ans);


  }
  return 0;
}

Measured memory_bytes: 0
# coding: utf-8


def solve(*args: str) -> str:
    n = int(args[0])
    ret = 0
    for a in range(1, n):
        for b in range(a, n):
            if n <= a*b:
                break
            else:
                ret += 1 if a == b else 2

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))


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

int main ( ) {
    int A, B;
    int sum = 0, dev = 0;
    string op;

    cin >> A >> op >> B;
    if (op == "+") {
        sum = A + B;
        cout << sum << endl;
    } else {
        dev = A - B;
        cout << dev << endl;
    }
}

Measured memory_bytes: 256
function main(input) {
  var N = input[0]-0;
  var K = input[1]-0;
  var list = input[2].split(" ").map(v=>v-0);
  var ans = 0;
  list.forEach(v=>{
    ans += Math.min(v,K-v)*2;
  });
  console.log(ans);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8').trim().split("\n"))


Measured memory_bytes: 7496
//minpro2019_c.cpp
//Sat Feb  9 20:56:10 2019

#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;

int main(){
	ll k,a,b;
	cin >> k >> a >> b;

	if ((b-a)<=1){
		cout << k+1 << endl;
		return 0;
	}else{
		ll ans = 0;
		k=k-(a-1);
		ans = ans + k/2*(b-a);
		if (k%2==1){
			ans++;
		}
		cout << ans+a << endl;
		return 0;
	}

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll  long long
#define FASTINOUT ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const ll mod = 1e9+7;
const int N=2e5+9;
vector<pair<int,int>>v[20];
int main()
{
    FASTINOUT;
    int x,l,r,ans=0;
    cin>>x;
    int k;
    for (int i=1; i<=x; i++)
    {
        cin>>k;
        for (int j=0; j<k; j++)
        {
            cin>>l>>r;
            v[i].push_back({l,r});
        }
    }
    for (int i=0; i<(1<<x); i++)
    {
        int a[20]= {0};
        int cnt=0;
        for (int j=0; j<x; j++)
        {
            if (i&(1<<j))
            {
                a[j+1]=1;
                cnt++;
            }
        }
        bool op=true;
        for (int j=1; j<=x; j++)
        {
            if (a[j])
            {
                for (int kl=0; kl<v[j].size(); kl++)
                {
                    if (a[v[j][kl].first]!=v[j][kl].second)
                    {
                        op=false;
                        break;
                    }
                }
                if (!op)
                    break;
            }
        }
        if (op)
            ans=max(ans,cnt);
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
int main(){
	string s;
	cin>>s;
	ll n;
	cin>>n;
	int m=0;
	while(s[m]=='1'&&m!=s.size()){
		m++;
	}
	if(n<=m)
	printf("1");
	else
	printf("%c",s[m]);
}


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

int main() {
	int n;
	cin>>n;
string b;
cin>>b;
int c=1;
for(int i=1;i<n;i++){
    if(b[i-1]!=b[i]){
        c++;
    }
}
cout<<c;
	return 0;
}

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

int main(){
	while(true){
		int x, y;
		cin >> x >> y;
		
		if(x > y) swap(x, y);
		if( y == 0 ) break;
		
		cout << x << " " << y << "\n";
	}
	cout << flush;
	
	return 0;
}


Measured memory_bytes: 3020
#include <iostream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <queue>
#define INF 1e9+7
#define rep(i,n) for(int i=0;i<n;i++)
#define NO cout<<"NO"<<endl;
#define YES cout << "YES"<<endl;
#define No cout << "No"<<endl;
#define Yes cout << "Yes"<<endl;
#define all(a) a.begin(),a.end()
#define P pair<int,int>
#define space ' '
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

//(int)'a'は97 (int)'A'は65 (int)'1'は49
//おまじない

const int maxn = 100000;

int main(){
    
    ll ans=0;
    int n;
    int t[maxn];
    
    cin >> n;
    rep(i,n){
        cin >> t[i];
    }
    
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            ans+=t[i]*t[j];
        }
    }
    
    cout << ans<<endl;
    
    return 0;
}


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

using namespace std;

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

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int main(){
  int n,m;
  cin>>n>>m;
  vi a(n),b(n),c(m),d(m);
  REP(i,n)cin>>a[i]>>b[i];
  REP(i,m)cin>>c[i]>>d[i];
  REP(i,n){
    int id = -1, di = 1e9;
    REP(j,m){
      int dd = abs(a[i]-c[j]) + abs(b[i]-d[j]);
      if(dd<di){
        id=j;
        di = dd;
      }
    }
    printf("%d\n",id+1);
  }
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    ll L, R;
    cin >> L >> R;
    R = min(R, L + 2019);
    int ans = 1e7;
    for (ll i = L; i <= R; i++) {
        for (ll j = i+1; j <= R; j++) {
            int x = i * j % 2019;
            ans = min(ans, x);
        }        
    }
    cout << ans << '\n';
    return 0;
}

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

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

int main() {
  int R, G, B, N;
  cin >> R >> G >> B >> N;
  int ret = 0;
  for(int r = 0; r * R <= N; r++) {
    for(int g = 0; r * R + g * G <= N; g++) {
      int rest = N - r * R - g * G;
      if(rest % B == 0) ++ret;
    }
  }
  cout << ret << endl;
}









Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
bool chmin(ll & a, ll  b) { if (b < a) { a = b; return 1; } return 0; }
bool chmax(ll & a, ll b) { if (b > a) { a = b; return 1; } return 0; }
const ll INF = (1LL<<31)-1;
const ll MOD = 1000000007;
const ll MAX_N=500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
template <typename T,typename E>
struct SegmentTree{
  typedef function<T(T,T)> F;
  typedef function<T(T,E)> G;
  typedef function<E(E,E)> H;
  typedef function<E(E,ll)> P;
  ll n;
  F f;
  G g;
  H h;
  P p;
  T d1;
  E d0;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(ll n_,F f,G g,H h,T d1,E d0,
	      vector<T> v=vector<T>(),P p=[](E a,ll b){return a;}):
    f(f),g(g),h(h),d1(d1),d0(d0),p(p){
    init(n_);
    if(n_==(ll)v.size()) build(n_,v);
  }
  void init(ll n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1,d1);
    laz.clear();
    laz.resize(2*n-1,d0);
  }
  void build(ll n_, vector<T> v){
    for(ll i=0;i<n_;i++) dat[i+n-1]=v[i];
    for(ll i=n-2;i>=0;i--)
      dat[i]=f(dat[i*2+1],dat[i*2+2]);
  }
  inline void eval(ll len,ll k){
    if(laz[k]==d0) return;
    if(k*2+1<n*2-1){
      laz[k*2+1]=h(laz[k*2+1],laz[k]);
      laz[k*2+2]=h(laz[k*2+2],laz[k]);
    }
    dat[k]=g(dat[k],p(laz[k],len));
    laz[k]=d0;
  }
  T update(ll a,ll b,E x,ll k,ll l,ll r){
    eval(r-l,k);
    if(r<=a||b<=l) return dat[k];
    if(a<=l&&r<=b){
      laz[k]=h(laz[k],x);
      return g(dat[k],p(laz[k],r-l));
    }
    return dat[k]=f(update(a,b,x,k*2+1,l,(l+r)/2),
		    update(a,b,x,k*2+2,(l+r)/2,r));
  }
  T update(ll a,ll b,E x){
    return update(a,b,x,0,0,n);
  }
  T query(ll a,ll b,ll k,ll l,ll r){
    eval(r-l,k);
    if(r<=a||b<=l) return d1;
    if(a<=l&&r<=b) return dat[k];
    T vl=query(a,b,k*2+1,l,(l+r)/2);
    T vr=query(a,b,k*2+2,(l+r)/2,r);
    return f(vl,vr);
  }
  T query(ll a,ll b){
    return query(a,b,0,0,n);
  }
};
auto F = [](ll a, ll b) { return a + b; };
auto G = F;
auto H = F;
SegmentTree<ll, ll> seg(MAX_N, F, G, H, 0, 0, vl(MAX_N, 0), multiplies<ll>());
int main() { cin >> n >> q; 
while(q--){
    cin >> x;
    if(x==0){
        cin >> a >> b >> c;
        seg.update(a, b + 1, c);
    }
    else{
        cin >> a;
        cout << seg.query(a, a + 1)<<endl;
    }
}}




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

int main(){
    long N,M;
    cin>>N>>M;
    long X[M];
    rep(i, M) cin>>X[i];

    sort(X, X+M);
    if(N >= M){
        cout << 0 << endl;
        return 0;
    }
    vector<long> dis;
    long sum = 0;
    rep(i, M-1){
        dis.push_back(abs(X[i+1]-X[i]));
        sum += dis[i];
    }

    sort(dis.rbegin(), dis.rend());
    rep(i, N-1){
        sum -= dis[i];
    }

    cout << sum << endl;
}


Measured memory_bytes: 2168
#include <stdio.h>

inline void swap(int array[], int a, int b){
	int c = array[a];
	array[a] = array[b];
	array[b] = c;
}

int main(){
	int array[50];
	int w,n,a,b;

	for(int i=0 ; i<50 ; i++){
		array[i] = i;
	}
	
	scanf("%d", &w);
	scanf("%d", &n);
	for(int i=0 ; i<n ; i++){
		scanf("%d,%d", &a, &b);
		swap( array , a , b );
	}
	for(int i=1 ; i<=w ; i++){
		printf("%d\n", array[i]);
	}
}

Measured memory_bytes: 2552
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <cmath>
#include <limits>
#include <set>
#include <iomanip>
#include <queue>
#include <string>
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 200000000000000;
typedef long long ll;
double pi = 3.141592653589793;
//setprecision(20) <<

const int MOD = 1000000007;

long long gcd(long long a, long long b)
{
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(a % b, b);
}

long long lcm(long long a, long long b)
{
  return a / gcd(a, b) * b;
}
long myPow(long long x, long long n, long long m = MOD)
{
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}

class Point
{
public:
  long long x;
  long long y;
  char S;
};
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  if(N%2==1){
    cout << "No" << endl;
    return 0;
  }
    string s;
    cin >> s;
    if(s.substr(0, N / 2)==s.substr(N / 2, N / 2)){
      cout << "Yes" << endl;
    }else
      cout << "No" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <sstream>
#include <istream>
#include <cmath>
#include <cstdio>
#include <complex>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i=(int)a; i<(int)b; i++)
#define for_d(i, a, b) for (int i=(int)a-1; i>=b; i--)
#define for_r(i, a, b, c) for (int i=(int)a; i<(int)b; i += c)
#define for_dr(i, a, b, c) for (int i=(int)a-1; i>=b; i -= c)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }

typedef long long ll;
typedef double D;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }





struct Part {
	int no, pos;
	
	Part (int no_, int pos_) : no(no_), pos(pos_) {}
};

int MAX = 1000000;
vector<Part> segpart;	// winner : segpart[0]


void init(int n) {
	segpart.assign(3*n, Part(MAX+1, 0));
	for_(k, 0, n + 1) {
		int k_ = k + n - 1;
		segpart[k_].no = k;
	}
}

Part maxpart(Part p1, Part p2) {
	if (p1.pos < p2.pos) return p2;
	else if (p1.pos > p2.pos) return p1;
	
	// p1.pos == p2.pos
	if (p1.no > p2.no) return p2;
	return p1;
}

void add(int n, int k, int v) {
	k += n-1;
	segpart[k].pos += v;
	
	while (k > 0) {
		k = (k-1)/2;
		segpart[k] = maxpart(segpart[k*2+1], segpart[k*2+2]);
	}
}

int main() {
	int n, q; cin >> n >> q;
	
	init(n);
	
	for_(i, 0, q) {
		int a, v; cin >> a >> v;
		add(n, a, v);
		cout << segpart[0].no << " " << segpart[0].pos << endl;
	}
	
	return 0;	
}

Measured memory_bytes: 24544
k, n = map(int, input().split())
a = [int(x) for x in input().split()]
x = []

for i in range(1, len(a)):
  x.append(a[i] - a[i - 1])
if(a[0] == 0):
	x.append(k - a[-1])
else:	x.append(k - a[-1] + a[0])

print(sum(x) - max(x))

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

ll N;
vector<ll> a;

ll calc(bool sig){
  ll ans = 0LL;
  ll sum = 0LL;

  for(ll i = 0; i < N; i++){
    if(sig ^ (i%2)){
      if(sum + a[i] <= 0){
        ans += 1 - (sum + a[i]); sum = 1;
      }else{
        sum += a[i];
      }
    }else{
      if(sum + a[i] >= 0){
        ans += 1 + (sum + a[i]); sum = -1;
      }else{
        sum += a[i];
      }
    }
  }
  return ans;
}

int main(){
  cin >> N;
  a.resize(N);
  for(ll i = 0; i < N; i++){
    cin >> a[i];
  }
  cout << min(calc(1), calc(0));
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=2e5+9;
const double pi=acos(-1);
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    map<int,int>cnt;
    cin>>n;
    for(int x,i=2;i<=n;i++){ 
        cin>>x;
        cnt[x]++;
    }
    for(int i=1;i<=n;i++){ 
        cout<<cnt[i]<<'\n';
    }

    return 0;
}


Measured memory_bytes: 13028
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define gec getchar
#define puc putchar
#define File(S) (freopen(S".in","r",stdin),freopen(S".out","w",stdout))
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T&a){
	static char ch;
	static bool sign;
	for(ch=gec(),sign=0;ch<'0'||ch>'9';ch=gec())sign^=(ch=='-');
	for(a=0;ch>='0'&&ch<='9';ch=gec())a=((a+(a<<2))<<1)+(ch^'0');
	if(sign)a=-a;
}
template <typename T>
inline void print(T x){
	static int Buf[30],PS=0,BUF;
    if(x<0)puc('-'),x=-x;
    for(;x>=10;x=BUF){
    	BUF=(x/5)>>1;
    	Buf[++PS]=x-((BUF+(BUF<<2))<<1);
    }
    Buf[++PS]=x;
    while(PS)puc('0'^Buf[PS--]);
}
const int N=2e5+10;
ll Sum=0,S=0,Ans=0x7ffffffffffffff;
int n,a[N];
int main()
{
	read(n);
	for(int i=1;i<=n;i++)
		read(a[i]),Sum+=a[i];
	for(int i=1;i<n;i++){
		S+=a[i];
		if(abs((Sum-S)-S)<Ans)Ans=abs((Sum-S)-S);
	}
	printf("%lld",Ans);
	return 0;
}


Measured memory_bytes: 1024
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include<math.h>
#define ll long long int
#define lim 100010
#define mod 1e9+7
#define INF 1e9
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define ABS(x) ((x)>0?(x):-(x))
int main(void){
	int n;
	scanf("%d",&n);
	int keta=0;
	int tmp=n;
	while(tmp>0){
		tmp/=10;
		keta++;
	}
	int ans=0;
	for(int i=1;i<keta;i+=2){
		ans+=9*pow(10,(i-1));
	}
	for(int i=pow(10,(keta-1));i<=n;i++){
		if(keta%2)ans++;
	}
	
	printf("%d",ans);
	
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int32_t main()
{
	IOS;
	string s;
	cin >> s;
	if(s[2] == s[3] && s[4] == s[5])
		cout << "Yes";
	else
		cout << "No";
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)
#define cinv(n,v) rep(i,n) cin >> v[i]
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vchar;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
// const lint MOD = 998244353;
int x_pos[4] = {1,0,-1,0}, y_pos[4] = {0,1,0,-1};
int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}
char upper(char c){
	return c-0x20;
}
char lower(char c){
	return c+0x20;
}

int main(){
	int n,l; cin>>n>>l;
	vector<int> v(n);
	int m = INF;
	rep(i,n){
		v[i] = l+i;
		if (abs(m) > (abs(l+i))){
			m = l+i;
		}
	}
	cout << acc(v) - m << endl;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    int N;
    cin >> N;
    int X[N];
    for (int i = 0; i < N; ++i) cin >> X[i];

    // (x1 - P)^2 + ... + (xn - P)^2
    // = (x1^2 + ... + xn^2) + (P^2 * n) - 2 * P * (x1 + ... + xn)
    // = n * (P - sum1 / n)^2 - sum1 / n + sum2
    int sum1 = 0, sum2 = 0;
    for (int i = 0; i < N; ++i) {
        sum1 += X[i];
        sum2 += X[i] * X[i];
    }

    int fp = sum1 / N;
    int fSum = sum2 + (fp * fp * N) - 2 * fp * sum1;
    int cp = sum1 / N + 1;
    int cSum = sum2 + (cp * cp * N) - 2 * cp * sum1;

    if (fSum <= cSum) cout << fSum;
    else cout << cSum;

    return 0;
}


Measured memory_bytes: 256
import sys
range = xrange
input = raw_input

m,k = [int(x) for x in input().split()]

if (m==1 and k==1) or k>=2**m:
    print -1
    sys.exit()

if k==0:
    A = list(range(2**m))
    print ' '.join(str(a) for a in (A + A[::-1]))
    sys.exit()

A = [i for i in range(2**m) if i != k]

A = A + [k] + A[::-1] + [k]

print ' '.join(str(a) for a in A)


Measured memory_bytes: 64276
package main

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

// const abcd = "abcdefghijklnmopqrstuvwxyz"
// const ABCD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// var dy = [...]int{1, 1, 0, -1, -1, -1, 0, 1}
// var dx = [...]int{0, 1, 1, 1, 0, -1, -1, -1}

// var dx = [...]int{0, 1, 0, -1}
// var dy = [...]int{1, 0, -1, 0}

var inf int = 1e9

// ---------------------------------------------------------
func main() {
	log.SetFlags(log.Lshortfile)
	next := newScanner()
	a := next.Int()
	b := next.Int()
	c := next.Int()
	d := next.Int()
	ab := false
	cd := false
	ans := 0
	for i := 0; i < 101; i++ {
		if a == i {
			ab = true
		}
		if b == i {
			ab = false
		}
		if c == i {
			cd = true
		}
		if d == i {
			cd = false
		}
		if ab && cd {
			ans++
		}
	}

	fmt.Println(ans)
}

// ---------------------------------------------------------

// Pair is liked C++ pair
type Pair struct {
	a, b int
}

// Pairs is sorted by []Pair struct
type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	if p[i].a < p[j].a {
		return true
	} else if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return false
}

// -------------------------------
func in(c, a, z int) bool {
	return c >= a && c < z
}

func btoi(b bool) int {
	if b {
		return 1
	}
	return 0
}

func itob(a int) bool {
	if a == 0 {
		return false
	}
	return true
}

func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}

func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}

func minmax(a, b int) (int, int) {
	if a > b {
		return b, a
	}
	return a, b
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// ---------- buffered scanner -----------------------------------------
type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 10000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) Line() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) Int() int {
	v, _ := strconv.Atoi(s.next())
	return v
}
func (s *scanner) Int64() int64 {
	v, _ := strconv.ParseInt(s.next(), 10, 64)
	return v
}
func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef unordered_set<int> si;
typedef pair<int, int> pp;
typedef vector<pp> vpp;
typedef unsigned int i64;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define all(e) (e).begin(), (e).end()
#define pb push_back

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

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


template <typename T>
void printV(vector<T> A) {
  if (A.empty()) cerr << "EMTPY\n";
  else { 
    cerr << "[";
    for (int i = 0; i < A.size() -1 ; ++i) {
      cerr << A[i] << ", ";
    }
    if (A.size()) cerr << A.back() << "]\n";
  }
}


template <typename T>
void printVV(vector<vector<T>> A) {
  cerr << "[";
  for (auto v : A) {
    printV<T>(v);
  }
  cerr << "]";
}

template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << ": " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&& ...args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n; cin >> n;
  int v = n % 10;
  if (v == 3) {
    cout << "bon\n";
  }
  else if (v == 0 || v == 1 || v == 6 || v == 8) {
    cout << "pon\n";
  }
  else cout << "hon\n";
  return 0;
}



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


using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vll=vector<ll>;
using vvll=vector<vll>;


#define MOD (1000000000+7)


int main(void)
{
	int n, m;

	while(scanf("%d%d", &n, &m)==2)
	{
		vi a(n+1), b(m+1);
		vi c(n*m);
		ll ans=1LL;

		for(int i=0;i<n;i++) scanf("%d", &a[i]);
		for(int i=0;i<m;i++) scanf("%d", &b[i]);

		sort(a.rbegin(), a.rend());
		sort(b.rbegin(), b.rend());

		for(int i=1;i<n;i++) if(a[i]==a[i-1]) ans=0;
		for(int i=1;i<m;i++) if(b[i]==b[i-1]) ans=0;

		int ap=0, bp=0;

		for(int i=m*n;i>0;i--)
		{
			for(;i<a[ap] && ap<n;ap++);
			for(;i<b[bp] && bp<m;bp++);

			if(i==a[ap])
			{
				if(i!=b[bp])
				{
					ans=(ans*bp)%MOD;
//					printf("*%d", bp+1);
				}
				else
				{
//					printf("*1");
				}
			}
			else if(i==b[bp])
			{
				ans=(ans*ap)%MOD;
//				printf("*%d", ap+1);
			}
			else
			{
				ans=(ans*((ap*bp)-(m*n-i)))%MOD;
//				printf("*(%d-%d)", (ap*bp), (m*n-i));
			}
			if(ans<=0) ans=0;
		}
		printf("%lld\n", ans);
	}

	return 0;
}



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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

const int MAX_N = 110000;

int dp[MAX_N]; // memo[i]がiについての答え

int main()
{
    int N;
    cin >> N;
    for (int i = 0; i < MAX_N; i++)
        dp[i] = N;
    dp[0] = 0;

    // 配るDP
    for (int n = 0; n <= N; n++) {
        for (int pow6 = 1; pow6 + n <= N; pow6 *= 6) {
            dp[n + pow6] = min(dp[n + pow6], dp[n] + 1);
        }
        for (int pow9 = 1; pow9 + n <= N; pow9 *= 9) {
            dp[n + pow9] = min(dp[n + pow9], dp[n] + 1);
        }
    }

    cout << dp[N] << endl;

    return 0;
}

Measured memory_bytes: 4076
#include <iostream>

using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int A, B, T;
	
	cin >> A >> B >> T;
	int tot = 0;
	int time = A;
	
	while(time < (T+0.5))
	{
		tot = tot + B;
		time = time + A;
	}
	cout << tot << endl;
	return 0;
}

Measured memory_bytes: 256
from sys import stdin
from collections import deque
n = int(stdin.readline())
d = [-1] * (n + 1)
def bfs(G):
    d[1] = 0
    queue = [1]
    dq = deque([1])
    while len(dq) > 0:
        v = dq.popleft()
        for c in G[v]:
            if d[c] < 0:
                d[c] = d[v] + 1
                dq.append(c)
    for i, x in enumerate(d[1:], start=1):
        print(i, x)
G = {}
for i in range(n):
    x = list(map(int, stdin.readline().split()))
    G[x[0]] = x[2:]
bfs(G)

Measured memory_bytes: 8080
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<int> vi;
#define FOR(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rep(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(),(x).end()
#define mset(a,x) memset(a,x,sizeof(a))
#define FASTIO cin.tie(0),ios::sync_with_stdio(0)

int n, K;
bool dp[5020][5020];

// 消しておk
bool valid(int c, vi& A) {
    if(c >= n) return false;
    FOR(s, max(0, K - A[c]), K) {
        if(dp[n - 1][s]) {
            // いらない
            return true;
        }
    }
    return false;
}

int main() {
    FASTIO;
    cin >> n >> K;
    vector<int> A(n);
    for(auto&& a : A) cin >> a;
    sort(ALL(A));

    ll lb = -1, ub = n + 1;
    while(ub - lb > 1) {
        int c = (lb + ub) / 2; // 省くカード
        vector<int> AA;
        rep(i, n) if(i != c) AA.emplace_back(A[i]);
        mset(dp, 0);
        dp[0][0] = 1;
        rep(i, n - 1) {
            rep(s, K + 1) {
                if(s - AA[i] >= 0)
                    dp[i + 1][s] |= dp[i][s] | dp[i][s - AA[i]];
                else
                    dp[i + 1][s] |= dp[i][s];
            }
        }

        if(valid(c, A))
            ub = c;
        else
            lb = c;

    }
    
    cout << min((int)ub, n) << endl;

    return 0;
}

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

int N;
std::vector<int> h;

int cost(int to) {
	if(to <= 0) {
		return 0;
	}
	static const int INVALID = -1;
	static std::vector<int> memo(N, INVALID);
	if(memo.at(to) != INVALID) {
		return memo.at(to);
	}

	int result;

	std::vector<int> list;
	for(int d : {2, 1}) {
		const int from = to - d;
		if(from < 0) {
			continue;
		}
		list.emplace_back(cost(from) + std::abs(h.at(from) - h.at(to)));
	}
	result = *std::min_element(list.begin(), list.end());

	memo.at(to) = result;
	return result;
}

int main() {
	std::cin >> N;
	h.resize(N);
	for(int i = 0; i < N; i++) {
		std::cin >> h.at(i);
	}

	const int result = cost(N - 1);

	std::cout << result << std::endl;

	return 0;
}

Measured memory_bytes: 7296
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;

int N, M, pointing[MAXN], par[MAXN], order[MAXN];
vector<int> edge[MAXN], urut;
bool visit[MAXN];

void topo(int now) {
	visit[now] = 1;
	for (auto i: edge[now]) {
		if (!visit[i]) topo(i);
	}
	urut.push_back(now);
	order[now] = urut.size();
}

void dfs(int now) {
	for (auto i: edge[now]) {
		if (par[i] != 0) continue;
		par[i] = now;
		dfs(i);
	}
}

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	
	cin >> N >> M;
	for (int i = 0; i < N+M-1; i++) {
		int x, y;
		cin >> x >> y;
		edge[x].push_back(y);
		pointing[y]++;
	}
	
	for (int i = 1; i <= N; i++) {
		if (!visit[i]) topo(i);
	}
	
	for (int i = 1; i <= N; i++) {
		sort(edge[i].begin(), edge[i].end(), [](int a, int b) {
			return order[a] > order[b];
		});
		//~ for (auto j: edge[i]) {
			//~ cerr << j << " ";
		//~ }
		//~ cerr << endl;
	}
	
	int root = urut[N-1];
	par[root] = 0;
	dfs(root);
	for (int i = 1; i <= N; i++) {
		cout << par[i] << "\n";
	}
}


Measured memory_bytes: 11256
import bisect
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
A.sort()
C.sort()
B.sort()

for i in B:
    a = bisect.bisect_left(A,i)
    c = bisect.bisect_right(C,i)
    ans += a*(N-c)

print(ans)


Measured memory_bytes: 23232
def main():
    import numpy as np
    n,m=map(int,input().split())
    A=list(map(int,input().split()))
    A.sort()
    A=np.array(A)

    def cnt_shake(x): #握手の組であって合計パワーがx以上となる組みの数
        return n**2-np.searchsorted(A,x-A).sum()
  
    right=2*(10**5)+1
    left=-1
    while right-left>1:
        mid=(left+right)//2
        if cnt_shake(mid)<m:
            right=mid
        else:
            left=mid
    
    C=n-np.searchsorted(A,right-A)
    B=np.cumsum(A[::-1])
    cnt=C.sum()
    print(np.sum(A*C)+left*(m-cnt)+B[C[np.where(C>0)]-1].sum())
if __name__=='__main__':
    main()

Measured memory_bytes: 23376
import java.util.Scanner;

public class Main {

    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {

	String A = scan.next();
	String B = scan.next();
	String C = scan.next();

	String ans = "NO";
	if(A.charAt(A.length()-1) == B.charAt(0) && B.charAt(B.length()-1) == C.charAt(0))
	    ans = "YES";
	System.out.println(ans);
	scan.close();
    }
}


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

int main(){
    int a[5];
    int mini=10;
    for(int i=0;i<5;i++) cin>>a[i];
    for(int i=0;i<5;i++) {
        if(a[i]%10!=0) mini=min(mini, a[i]%10);
    }
    for(int i=0;i<5;i++) {
        if(mini==a[i]%10) {
            swap(a[i], a[4]);
            break;
        }
    }

    int ans=0;
    for(int i=0;i<5;i++) {
        if(i!=4&&a[i]%10!=0) {
            a[i]=a[i]+(10-a[i]%10);
        }
        ans+=a[i];
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
require 'bigdecimal'
require 'bigdecimal/util' 
N = gets.to_d
A = gets.to_d
B = gets.to_d
C = gets.to_d
D = gets.to_d
E = gets.to_d

puts sprintf("%d", (N/([A,B,C,D,E].min)).ceil+4)

Measured memory_bytes: 1916
#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

int num[8];
double runtime[8];
int num3[18];
double runtime3[18];


void bubble_sort1(void){

  for( int i = 0 ; i < 7 ; i++ ){
    for( int j = 7 ; j > i ; j-- ){
      if( runtime[j - 1] > runtime[j] ){

	double temp = runtime[j - 1];
	runtime[j - 1] = runtime[j];
	runtime[j] = temp;

	temp = num[j - 1];
	num[j - 1] = num[j];
	num[j] = temp;

      }
    }
  }

}


void bubble_sort2(void){

  for( int i = 0 ; i < 17 ; i++ ){
    for( int j = 17 ; j > i ; j-- ){
      if( runtime3[j - 1] > runtime3[j] ){

	double temp = runtime3[j - 1];
	runtime3[j - 1] = runtime3[j];
	runtime3[j] = temp;

	temp = num3[j - 1];
	num3[j - 1] = num3[j];
	num3[j] = temp;

      }
    }
  }

}

int main(void){

  int n = 3;
  int I = 0;

  while(n--){

    for( int i = 0 ; i < 8 ; i++ ){
      scanf("%d %lf",&num[i],&runtime[i]);
    }

    bubble_sort1();

    //cout << "---" << endl;
    cout << num[0] << " " << runtime[0] << endl;
    cout << num[1] << " " << runtime[1] << endl;
    // cout << "---" << endl;
 
    for( int i = 2 ; i < 8 ; i++ ){
      num3[I] = num[i];
      runtime3[I] = runtime[i];
      I++;
    }

  }
    bubble_sort2();

    cout << num3[0] << " " << runtime3[0] << endl;
    cout << num3[1] << " " << runtime3[1] << endl;
  return 0;
}

Measured memory_bytes: 1292
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <functional>
using namespace std;

typedef long long ll;

int n, m, s, t;
vector<pair<ll, int>> edg[100003];
ll d[100003], dps[100003], dpt[100003];
ll mod = 1e9 + 7;

int main() {
	cin >> n >> m >> s >> t;
	--s; --t;
	for (int i = 0; i < m; ++i) {
		int u, v; ll dis; cin >> u >> v >> dis;
		--u; --v;
		edg[u].push_back({ dis, v });
		edg[v].push_back({ dis, u });
	}
	for (int i = 0; i < n; ++i) d[i] = 1e18;
	d[s] = 0;
	priority_queue<pair<ll, int>> que;
	for (int i = 0; i < (int)edg[s].size(); ++i) que.push({ -edg[s][i].first, edg[s][i].second });
	while (!que.empty()) {
		ll dis = -que.top().first; int pos = que.top().second; que.pop();
		if (dis >= d[pos]) continue;
		d[pos] = dis;
		for (int i = 0; i < (int)edg[pos].size(); ++i) {
			que.push({ -dis - edg[pos][i].first, edg[pos][i].second });
		}
	}

	priority_queue<pair<ll, int>> que2;
	dps[s] = 1;
	for (int i = 0; i < n; ++i) que2.push({ -d[i], i });
	while (!que2.empty()) {
		int pos = que2.top().second; que2.pop();
		for (int i = 0; i < (int)edg[pos].size(); ++i) {
			ll w = edg[pos][i].first; int to = edg[pos][i].second;
			if (d[pos] + w != d[to]) continue;
			dps[to] += dps[pos];
			dps[to] %= mod;
		}
	}
	dpt[t] = 1;
	for (int i = 0; i < n; ++i) que2.push({ d[i], i });
	while (!que2.empty()) {
		int pos = que2.top().second; que2.pop();
		for (int i = 0; i < (int)edg[pos].size(); ++i) {
			ll w = edg[pos][i].first; int to = edg[pos][i].second;
			if (d[pos] - w != d[to]) continue;
			dpt[to] += dpt[pos];
			dpt[to] %= mod;
		}
	}

	ll ans = 0;
	for (int i = 0; i < n; ++i) {
		if (2*d[i] == d[t]) {
			ans += (dps[i] * dpt[i] % mod)*(dps[i] * dpt[i] % mod) % mod;
			ans %= mod;
		}
		for (int j = 0; j < (int)edg[i].size(); ++j) {
			ll w = edg[i][j].first; int to = edg[i][j].second;
			if (d[i] + w != d[to]) continue;
			if (2*d[i] < d[t] && d[t] < 2*d[to]) {
				ans += (dps[i] * dpt[to] % mod)*(dps[i] * dpt[to] % mod) % mod;
				ans %= mod;
			}
		}
	}
	ans = (dps[t] * dps[t] % mod - ans + mod) % mod;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 20716
def d():
    nmax, mmax = 40, 10
    n, ma, mb = map(int, input().split())
    a, b, c = [], [], []
    for i in range(n):
        a_, b_, c_ = map(int, input().split())
        a.append(a_)
        b.append(b_)
        c.append(c_)

    dp = [[[float('inf') for _ in range(n*mmax+1)] for _ in range(n*mmax+1) ] for _ in range(n+1)]
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(n*mmax+1):
            for cb in range(n*mmax+1):
                if dp[i][ca][cb] == float('inf'):
                    continue
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
                if ca + a[i] < n*mmax+1 and cb + b[i] < n*mmax+1:
                    dp[i+1][ca + a[i]][cb + b[i]] = min(dp[i+1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i])

    ans = float('inf')
    for ca in range(1, n*mmax+1):
        for cb in range(1, n*mmax+1):
            if ma * cb == mb * ca:
                ans = min(ans, dp[n][ca][cb])

    if ans == float('inf'):
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    d()


Measured memory_bytes: 122460
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  vector<int> A(100);
  A[0] = a;
  REP(i, 99) { A[i + 1] = A[i] + a * (i+1); }
  REP(i, 99) for (int j = 1; j + i < 100;j++){
      if((A[i+j]-A[i])%b==c){
        puts("YES");
        return 0;
      }
  }
  puts("NO");
}

Measured memory_bytes: 256
S = list(input())
T = list(input())
ls = len(S)
lt = len(T)
res = 1000
for i in range(ls - lt + 1):
  temp = 0
  for j in range(lt):
    if S[i+j] != T[j]:
      temp += 1
  if res > temp:
    res = temp
print(res)

Measured memory_bytes: 65384
#include <iostream>
#define REP(i, a, n) for(int i = (a); i <= (n); i++)
using namespace std;

const int M = 1000000;
int A, D, N;
bool p[M + 1];

int main(void) {
  REP(i, 1, M) p[i] = true;
  p[1] = false;
  REP(i, 2, M) if(p[i]) for(int j = i * 2; j <= M; j += i) p[j] = false;

  while(cin >> A >> D >> N, A) {
    int v = A;
    for(int cnt = 0; cnt < N; v += D) {
      if(p[v]) cnt++;
      if(cnt == N) break;
    }
    cout << v << endl;
  }

  return 0;
}

Measured memory_bytes: 4044
#include <iostream>
using namespace std;
int main(void){
    int a,b,c;
    cin>>a>>b>>c;
    if(a==1 && b==0 && c==0){
        cout<<"Close"<<endl;
    }
    else if(a==0 && b==1 && c==0){
        cout<<"Close"<<endl;
    }
    else if(a==1 && b==1 && c==0){
        cout<<"Open"<<endl;
    }
    else if(a==0 && b==0 && c==1){
        cout<<"Open"<<endl;
    }
    else if(a==0 && b==0 && c==0){
        cout<<"Close"<<endl;
    }
   
        return 0;
    
}



Measured memory_bytes: 3060
#include <bits/stdc++.h>
//#pragma GCC optimize(2)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i = (a);i <= (b);i++)
#define per(i,a,b) for(int i = (a);i >= (b);i--)
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const double eps = 1e-8;
const double pi = acos(-1.0);	
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a * b / gcd(a,b);}
ll qw(ll x,ll y){ll res = 1;while(y){if(y & 1) res *= x;x = x * x;y >>= 1;}return res;}
ll poww(ll x,ll y,ll p){ll ans = 1;while(y){if(y & 1) ans = ans * x % p;x = x * x % p;y >>= 1;}return ans % p;}
bool isp(ll x){if(x < 2) return 0; for(ll i = 2;i * i <= x;i++){if(x % i == 0) return 0;}return 1;}
ll n,a[maxn],ans;
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	rep(i,0,n-1) cin >> a[i];
	sort(a,a+n,greater<ll>());
	ans += a[0];
	int sum = 1;
	rep(i,1,n-1){
		if(sum == n-1) break;
		if(sum + 1 <= n-1) sum += 1,ans += a[i];
		if(sum + 1 <= n-1) sum += 1,ans += a[i];
	}
	cout << ans << endl;
	return 0;
}
//do smt instead of nothing! never give up!


Measured memory_bytes: 5176
import Data.List
--import Data.Array
import Data.Array.Unboxed
import Data.Array.MArray
import Data.Array.ST
import Control.Monad
import Control.Monad.ST

--type UArray Int Int = Haps
type DP s = STUArray s (Int,Int) Int

--i>=2
dp_i :: DP s -> Int -> ST s ()
dp_i dparr i = do
  [aj,bj,cj] <- forM [1..3] $ \j -> readArray dparr (i-1,j)
  [a,b,c]    <- forM [1..3] $ \j -> readArray dparr (i,j)
  writeArray dparr (i,1) $ (max bj cj)+a
  writeArray dparr (i,2) $ (max aj cj)+b
  writeArray dparr (i,3) $ (max bj aj)+c
solve :: [Int] -> Int -> Int
solve haps n = runST $ do
    dparr <- newListArray ((1,1),(n,3)) haps
    forM_ [2..n] $ dp_i dparr
    (forM [1..3] $ \j -> readArray dparr (n,j)) >>= return.maximum

main :: IO ()
main = do
  n <- read <$> getLine
  haps <- replicateM n $ map read <$> words <$> getLine :: IO [[Int]]
  putStrLn $ show $ solve (concat haps) n


Measured memory_bytes: 109820
S = input()

alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Now = 0
ans = 0
for s in S:
    Next = alpha.find(s)
    # print(s, Now, Next)

    if(Now < Next):
        pass
    else:
        ans += 1
    Now = Next
print(ans)


Measured memory_bytes: 5556
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<utility>
#include<map>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1001001001;
const double PI = acos(-1);

ll pow_mod(ll p, ll n, ll m) {
	if (n == 0) return 1;
	else if (n == 1) return p % m;
	else if (n % 2 == 1) return p * pow_mod(p, n - 1, m) % m;
	else {
		ll tmp = pow_mod(p, n / 2, m);
		return (tmp * tmp) % m;
	}
}

ll gcd(ll x, ll y) {
	if (y == 0) {
		return x;
	}
	return gcd(y, x % y);
}

ll com_mod(ll n, ll k, ll m) {
	if (k == 0) return 1;
	else {
		ll x = 1;
		ll y = 1;
		for (ll i = 0; i < k; i++) {
			x = (x * (n - i)) % m;
			y = (y * (i + 1)) % m;
		}
		ll res = (x * pow_mod(y, m - 2, m)) % m;
		return res;
	}
}

int main() {
	int n; cin >> n;
	n %= 10;
	if (n == 2 || n == 4 || n == 5 || n == 7 || n == 9) cout << "hon" << endl;
	else if (n == 0 || n == 1 || n == 6 || n == 8) cout << "pon" << endl;
	else cout << "bon" << endl;
	return 0;
}

Measured memory_bytes: 3636
import std.stdio;
import std.string;
import std.array;
import std.conv;

uint figure(uint num) {
  if(num == 0) return 0;
  else return figure(num / 10) + 1;
}

void main() {
  string[][] inputs;
  while(true) {
    auto s = readln();
    if(stdin.eof()) break;
    else inputs ~= split(s);
  }

  foreach(input; inputs) {
    uint a = to!uint(input[0]);
    uint b = to!uint(input[1]);

    writeln(figure(a + b));  
  }
}

Measured memory_bytes: 960
#include<bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll inf=1e9+7;
const ll mod=998244353;
int main(){
    while(1){
    ll sum;cin>>sum;
    if(sum==0)break;
    ll a=0;
    rep(i,9){
        ll b;cin>>b;
        sum-=b;
    }
    cout<<sum<<endl;
    }
}


Measured memory_bytes: 3016
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define rep(i,a,b) for (int i = a; i < b; ++i)
#define IOS ios::sync_with_stdio(false); cin.tie(NULL);
#ifndef LOCAL
#define dbg(...) ;
#define endl '\n'
#endif

const int inf = 1e15;
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;



signed main(){
    IOS;
    int n;
    cin >> n;
    cout << fixed << setprecision(10) << n*n*n/3.0/3.0/3.0;
    return 0;
}

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

int main()
{
	unsigned long long n;
	string s;
	cin >> n;
	//Ascii from 97 to 122 (+96)
	while (true)
	{
		if (n % 26 == 0)
		{
			s += 'z';
			n /= 26;
			n--;
		}
		else
		{
			s += (char)((n % 26) + 96);
			n = n / 26;
		}
		if (n == 0) break;
	}
	reverse(s.begin(), s.end());
	cout << s << endl;

	ios::sync_with_stdio(false);
	cin.tie();
	cout.tie();
	return 0;
}

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

namespace ABC061_C
{
    class Program
    {
        static void Main(string[] args)
        {
            var NK = Console.ReadLine().Split().Select(long.Parse).ToArray();
            var N = NK[0];
            var K = NK[1];

            var ar = new int[N][];
            for (int i = 0; i < N; i++)
            {
                ar[i] = Console.ReadLine().Split().Select(int.Parse).ToArray();
            }

            ar = ar.OrderBy(x => x[0]).ToArray();

            long n = 0;
            for (int i = 0; i < N; i++)
            {
                n += ar[i][1];
                if (n >= K)
                {
                    Console.WriteLine(ar[i][0]);
                    break;
                }
            }
            
        }
    }
}


Measured memory_bytes: 28220