akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <functional>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

int main(void){
    int x, y, z;
    cin >> x >> y >> z;
    cout << z << " " << x << " " << y << endl;

    return 0;
}


Measured memory_bytes: 256
use std::cmp;

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let mut v: Vec<usize> = s.trim().split_whitespace().map(|x| x.parse().unwrap()).collect();
    let mut d: usize = std::usize::MAX;

    for _ in 0..2 {
        for i in 1..v[0]/2+1 {
            let x = i * v[1];

            let mut y = ((v[0] - i) / 2) * v[1];
            let mut z = ((v[0] - i) / 2 + (v[0] - i) % 2) * v[1];
            d = cmp::min(d, cmp::max(x, cmp::max(y, z))-cmp::min(x, cmp::min(x, y)));

            y = v[1] / 2 * (v[0] - i);
            z = (v[1] / 2 + v[1] % 2) * (v[0] - i);
            d = cmp::min(d, cmp::max(x, cmp::max(y, z))-cmp::min(x, cmp::min(x, y)));
        }
        v.reverse();
    }
    println!("{}", d);
}



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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
    int n; cin >> n;
    
    map<ll, ll> mp;
    ll tot = 0;
    
    rep(i, n) {
        ll a; cin >> a;
        mp[a]++;
        tot += a;
    }
    
    int q; cin >> q;
    while(q--){
        ll b, c; cin >> b >> c;
        tot += (c-b) * mp[b];
        mp[c] += mp[b];
        mp[b] = 0;
        cout << tot << endl;
    }

    return;
}

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
	return 0;
}

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

int main(){
	vector<int> l(4,0);
	vector<int> r(4,0);
	
	double a,b;
	while(cin>>a>>b){
		if(a>=1.1){
			l[0]++;
		}else if(a>=0.6){
			l[1]++;
		}else if(a>=0.2){
			l[2]++;
		}else{
			l[3]++;
		}
		
		if(b>=1.1){
			r[0]++;
		}else if(b>=0.6){
			r[1]++;
		}else if(b>=0.2){
			r[2]++;
		}else{
			r[3]++;
		}
	}
	
	for(int i=0;i<4;i++)cout<<l[i]<<" "<<r[i]<<endl;
	
	return 0;
}

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

const int N=2e5+5;

int a[N];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	if(n&1){
		for(int i=n;i>=1;i-=2) printf("%d ",a[i]);
		for(int i=2;i<=n-1;i+=2) printf("%d ",a[i]);
	} 
	else{
		for(int i=n;i>=2;i-=2) printf("%d ",a[i]);
		for(int i=1;i<=n-1;i+=2) printf("%d ",a[i]);
	}
	return 0;
}

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


int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  int i = 0;
  int res = 0;
  while (i <= n - 1) {
    res += a.at(i);
    if (i + 1 <= n - 1) {
     res -= a.at(i + 1);
    }
    i += 2;
  }
  cout << res << endl;
}

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

int main()
{
    ll n,w;
    cin>>n>>w;
    vector<ll> dp(w+1);
    for(ll i=0;i<n;i++)
    {
        ll weight,value;
        cin>>weight>>value;
        for(ll weight_already=w-weight;weight_already>=0;weight_already--)
        {
            dp[weight_already+weight]=max(dp[weight_already+weight],dp[weight_already]+value);
        }
    }
    ll ans=0;
    for(ll i=0;i<=w;i++)
    {
        ans=max(ans,dp[i]);
    }
    cout<<ans<<"\n";
}

Measured memory_bytes: 1024
// Marcin Knapik
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

//#include <ext/pb_ds/assoc_container.hpp>
// Including tree_order_statistics_node_update
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
 
typedef long double             ld;
typedef long long               ll;
typedef unsigned long long      ull;
typedef pair<ll, ll>            pll;
typedef pair<int, int>          ii;
typedef vector<int>             vi;
typedef vector<ii>              vii;
typedef vector<vi>              vvi;
typedef vector<vector<ii>>      vvii;
typedef vector< ll >            vll;
typedef vector< pll >           vpll;
 
#define boost                   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a)                   (int)(a).size()
#define pb                      push_back
#define all(c)                  (c).begin(), (c).end()
#define rall(c)                 (c).rbegin(), (c).rend()
#define REP(i, a, b)            for (int i = (a); i <= (b); i++)
#define FOR(i, a)               for (int i = 0; i < (a); i++)
#define TRAV(i, n)              for(auto&i:n)
#define beg(x)                  (*(x.begin()))
#define re(x)                   int x; cin >>x;   
#define f                       first
#define s                       second
 
template <class T> inline bool setmin(T &a, T b){if (a > b)return a = b, 1;return 0;}
template <class T> inline bool setmax(T &a, T b){if (a < b)return a = b, 1;return 0;}
 
template<class T> inline T fast(T a,T b,T mod) {ll res = 1; while(b){if(b&1) res = (res*a)%mod;a = (a*a)%mod;b >>= 1;}return res;}
template<class T> inline T russian(T a, T b, T mod) {ll res = 0; while(b){if(b&1) res = (res + a)%mod; a = (a+a)%mod; b>>=1;}return res;}
template <class T> istream &operator>>(istream &os, vector<T> &container){for (auto &u : container)os >> u;return os;}
template <class T> ostream &operator<<(ostream &os, const vector<T> &container){for (auto &u : container)os << u << " ";return os;}
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }
 
const ll INF = 1e9 + 7;
const ll mod = 998244353;
const ll BIG_INF = 1e18 + 7;
const ll N = 1e6+7;
const int LOG = 17;

ll n, m, k, q;
ll pref[N];

ll suma(int a, int b){
	return pref[b+1] - pref[a];
}

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

	cin >> n >> m >> k >> q;

	vll tab(n);
	for(auto&u:tab)
		cin >> u;
	sort(all(tab));

	for(int i = 0; i < n; i++){
		pref[i+1] = pref[i] + tab[i];
	}

	int ans = 0;
	for(int i = 0; i < n; i++){

		ll nast = upper_bound(all(tab), tab[i] + m) - tab.begin();
		if(n - nast >= q)
			continue;

		if(n - i <= q){
			ans++;
			continue;
		}
		ll ile_z_bomby = (q*m) + i*m;
		ll jeszcze = (n - q - i)*(tab[i]+m) - suma(i+1, n-q);

		if(ile_z_bomby + jeszcze >= m*k)
			ans++;
	}
	cout << ans << '\n';
}

Measured memory_bytes: 1792
#include <iostream>


using namespace std;

int main() {
    int D, N;
    cin >> D >> N;

    if (N == 100) ++N;

    if (D == 0) {
        cout << N << endl;
    } else if (D == 1) {
        cout << 100 * N << endl;
    } else {
        cout << 10000 * N << endl;
    }
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;
int main() {
  int x, n, l[110], d = 0, res = 1;
  cin >> n >> x;
  for (int i = 0; i < n; i++) cin >> l[i];

  for (int i = 2; i <= n + 1; i++) {
    if (d + l[i - 2] <= x) {
      res++;
      d = d + l[i - 2];
    } else
      break;
  }
  cout << res << endl;

  return 0;
}


Measured memory_bytes: 256
def digit_num(n)
    i = 0
    while n > 0
        i += 1
        n /= 10
    end
    return i
end

n = gets.chomp.to_i
sum = 0

(1..n).each do |i|
    sum += 1 if digit_num(i) % 2 == 1
    i += 1
end

puts sum

Measured memory_bytes: 1788
# 2019-11-16 21:01:15(JST)
import sys
# import collections
import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def dist(x1,y1,x2,y2):
    return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

def main():
    n = int(sys.stdin.readline().rstrip())
    x, y = [], []
    for _ in range(n):
        xi, yi = [int(x) for x in sys.stdin.readline().split()]
        x.append(xi)
        y.append(yi)

    ans = 0
    for p in itertools.permutations(range(n), n):
        d = 0
        for i in range(n-1):
            d += dist(x[p[i]], y[p[i]], x[p[i+1]], y[p[i+1]])
        ans += d
    
    ans /= math.factorial(n)
    print(ans)

  
if __name__ == "__main__":
    main()


Measured memory_bytes: 3064
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


struct ToUpper {
    char operator()(char c) { return toupper(c); }
};

int main()
{
	string str;
	getline(cin, str);

	transform(ALL(str), str.begin(), ToUpper());

	cout << str << endl;
}

Measured memory_bytes: 0
N = int(input())
a_lst = [int(x) for x in input().split()]

dict = {}

for a in a_lst:
    if not a - 1 in dict.keys():
        dict[a - 1] = 1
    else:
        dict[a - 1] += 1
    if not a in dict.keys():
        dict[a] = 1
    else:
        dict[a] += 1
    if not a + 1 in dict.keys():
        dict[a + 1] = 1
    else:
        dict[a + 1] += 1
val_lst = list(dict.values())
val_lst.sort(reverse=True)
print(val_lst[0])


Measured memory_bytes: 18796
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define rrep(i,a,n) for(ll i=n;i>=a;i--)
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF = 1e+17;
const ll MODcdf = 998244353;
int main(){
    ll n;cin>>n;
    ll a[n];
    rep(i,0,n)cin>>a[i];
    sort(a,a+n);
    ll sum=0;
    rep(i,0,n-1){
        sum+=a[i];
    }
    if(a[n-1]<sum)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
n = int(input())
a = []

for _ in range(n):
    a.append(int(input()) - 1)

cnt = 0
current = 0
s = set()
while True:
    current = a[current]
    if current in s:
        print(-1)
        exit(0)
    s.add(current)
    cnt += 1
    if current == 1:
        print(cnt)
        exit(0)


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

/************* Using *************/
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using graph_t = vector<vector<ll>>;

/*********** Constants ***********/
const auto INF  = static_cast<int>(1e10);
const auto INFL = static_cast<ll>(1e18);
const auto MOD  = static_cast<ll>(1e9 + 7);
const auto NIL  = nullptr;

/*********** Variables ***********/
ll M, D, ANS;

/************* Utils *************/

/*********** Functions ***********/
bool init() {
    cin >> M >> D;
    return true;
}

void solve() {
    for (ll m = 1; m <= M; m++) {
        for (ll d = 1; d <= D; d++) {
            ll d1, d2;
            d1 = d / 10; d2 = d % 10;
            if (d1 >= 2 && d2 >= 2 && d1 * d2 == m) {
                ANS++;
            }
        }
    }
}

void output() {
    cout << ANS << endl;
}

int main() {
    init();
    solve();
    output();
    return 0;
}

Measured memory_bytes: 764
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int main(){
     int a,b,c;
     cin>>a>>b;
     c = a * b;
     if(c&1)
     	 printf("Odd");
     else
     	 printf("Even");
}



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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

int main(){
    int n, ma, mb; cin >> n >> ma >> mb;
    vector<vector<vector<int> > > dp(n+1, vector<vector<int> >(500, vector<int>(500, 1e9)));
    dp[0][0][0] = 0;
    vector<vector<int>> lis(n, vector<int>(3));
    REP(i, 0, n){
        int a, b, c; cin >> a >> b >> c;
        lis[i][0] = a;
        lis[i][1] = b;
        lis[i][2] = c;
    }
    REP(i, 0, n){
        dp[i+1] = dp[i];
        for(int l = 499; l >= 0; l--){
            for(int k = 499; k >= 0; k--){
                if(dp[i][l][k] != 1e9){
                    chmin(dp[i+1][l+lis[i][0]][k+lis[i][1]], dp[i][l][k] + lis[i][2]);
                }
            }
        }
    }
    int ans = 1e9;
    REP(i, 1, 401){
        REP(l, 1, 401){
            if(i * mb == l * ma){
                chmin(ans, dp[n][i][l]);
            }
        }
    }
    if(ans == 1e9){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}

Measured memory_bytes: 45180
import java.util.Scanner;

public class Main {
    public static void main(String args[]) {
        try (Scanner in = new Scanner(System.in)) {
            int N = in.nextInt();
            String A = in.next();
            String B = in.next();
            String C = in.next();

            int count = 0;
            for (int i = 0; i < N; i++) {
                char a = A.charAt(i);
                char b = B.charAt(i);
                char c = C.charAt(i);
                boolean ab = a == b;
                boolean bc = b == c;
                boolean ca = c == a;
                if (ab) {
                    if (bc) {
                        continue;
                    } else {
                        count++;
                        continue;
                    }
                } else {
                    if (bc || ca) {
                        count++;
                        continue;
                    } else {
                        count++;
                        count++;
                        continue;
                    }
                }
            }

            System.out.println(count);
        }
    }
}


Measured memory_bytes: 21844
#include <bits/stdc++.h>
using namespace std;
long long int mod=1e9+7;
int main() {
	long long int h,w;
	cin>>h>>w;
    char tmp;
    vector<vector<long long int>>matrix(h,vector<long long int>(w,1));
    for(long long int i=0;i<h;i++){
        for(long long int j=0;j<w;j++){
            cin>>tmp;
            if(tmp=='#') matrix[i][j]=0;
        }
    }
    vector<vector<long long int>>dp(h,vector<long long int>(w,0));
    dp[0][0]=1;
    for(long long int i=0;i<h;i++){
        for(long long int j=0;j<w;j++){
            if(matrix[i][j] && !(i==0 &&  j==0)){
                dp[i][j]=((i>0 ? dp[i-1][j]%mod : 0) +(j>0 ? dp[i][j-1]%mod : 0))%mod;
            }
        }
    }
    cout<<dp[h-1][w-1]%mod<<endl;

    return 0;
}

Measured memory_bytes: 16000
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<functional>
using namespace std;
int main(void) {
    
  int a,b,c,k;
  cin>>a>>b>>c>>k;

    int sum = a+b+c;

    
        sum+=max(max(a,b),c)*pow(2,k);
    

    sum -= max(max(a,b),c);
    cout<<sum<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#define MAX_T 1300
using namespace std;
int main(){
  int n,m,r,q,a,b,c,d,ts,te,stu,cnt;
  cin >> n >> m;
  while(n!=0){
    vector<int> imos[10000];
    for(int i=0;i<m;i++){
      for(int j=0;j<MAX_T;j++){
        imos[i].push_back(0);
      }
    }
    cin >> r;
    for(int i=0;i<r;i++){
      cin >> a >> b >> c >> d;
      c--;
      if(d==1){
        imos[c][a]++;
      }else{
        imos[c][a]--;
      }
    }
    for(int i=0;i<m;i++){
      for(int j=1;j<MAX_T;j++){
        imos[i][j]+=imos[i][j-1];
      }
    }
//    for(int i=0;i<MAX_T;i++)cout << imos[0][i]<<"\n";
    cin >> q;
    for(int i=0;i<q;i++){
      cin >> ts >> te >> stu;
      stu--;
      cnt=0;
      for(int j=ts;j<te;j++){
        if(imos[stu][j]>0)cnt++;
      }
      cout << cnt << endl;
    }
    cin >> n >> m;
  }
}



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

int main(){
  string s;cin >> s;
  map<char,ll> m;
  rep(i,(ll)s.size()) m[s.at(i)]++;

  ll ans;
  if( s.size()%2 == 0 ) ans = (ll)s.size()/2;
  else ans = (ll)s.size()/2+1;

  for(auto p : m ){
    ll score = 0;
    char c = p.first;
    for(ll i=(ll)s.size()-1;i>=0;--i){
      if( s.at(i) == c ){
        ll cnt = 1;
        for(ll j=i+1;j<(ll)s.size();++j){
          if( s.at(j) == c ){
            --cnt;
            score = max(score,cnt);
            break;
          }else if( j == (ll)s.size()-1 ){
            score = max(score,cnt);
          }
          ++cnt;
        }
        cnt = 1;
        for(ll j=i-1;j>=0;--j){
          if( s.at(j) == c ){
            --cnt;
            score = max(score,cnt);
            break;
          }else if( j == 0 ){
            score = max(score,cnt);
          }
          ++cnt;
        }
      }
    }
    ans = min(ans,score);
  }
  cout << ans << endl;
  return 0;
}




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

#define int long long
const int MOD = 1e9 + 7;
int f[200001], c[200001];

int binpw(int ba, int exp){
    int ans = 1;
	while(exp != 0){
	    if(exp % 2 != 0) ans *= ba;
	    ans %= MOD;
	    ba = ba * ba % MOD;
	    exp /= 2;
	}
	return ans;
}

int C(int a, int b){
	int ans = (c[a + b] * f[a]) % MOD;
	return (ans * f[b]) % MOD;
}

signed main()
{
	int h, w, a, b;
	cin >> h >> w >> a >> b;
	f[0] = c[0] = 1;
	for(int i = 1; i <= h + w; i++){
		c[i] = (c[i - 1] * i) % MOD;
		f[i] = binpw(c[i], MOD - 2);
	}
	int ans = 0;
	for(int i = b + 1; i <= w; i++){
		ans += C(h - a - 1, i - 1) * C(a - 1, (w - i));
		ans %= MOD;
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 3328
import java.util.Scanner;

import static java.lang.Math.min;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int l = scanner.nextInt();
        int r = scanner.nextInt();
        int ml = l % 2019;
        int mr = r % 2019;

        if (r - l + 1 >= 2019) {
            System.out.println(0);
        } else if (ml > mr) {
            System.out.println(0);
        } else {
            int min=mr*mr;
            for (int i = ml; i <= mr; i++) {
                for (int j = i + 1; j <= mr ; j++) {
                    min=min(i*j%2019,min);
                }
            }
            System.out.println(min);
//            boolean flag = false;
//            for (int i = ml; i < mr; i++) {
//                if (i % 673 == 0) {
//                    if (mr - ml + 1 >= 3 || ml % 3 == 0 || mr % 3 == 0) {
//                        System.out.println(0);
//                        flag = true;
//                        break;
//                    }
//                }
//            }
//            if (!flag) {
//                System.out.println(ml * (ml + 1) % 2019);
//            }
        }
    }
}


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

int main() {
	ll n;
	string s;
	cin >> n >>s;
	ll ans;
	ll r = count(s.begin(), s.end(), 'R');
	ll g = count(s.begin(), s.end(), 'G');
	ll b = count(s.begin(), s.end(), 'B');
	ans = r * g * b;
	for (ll i = 0; i < n; ++i) {
		for (ll j = i + 1; j < n; ++j) {
			if ((j - i) % 2 == 1)continue;
			ll mid = i + (j - i) / 2;
			if (s[i] != s[j] && s[j] != s[mid] && s[mid] != s[i])--ans;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 3640
class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7
A.sort()

comb = Combination(N + 100)
ans = 0

for i, a in enumerate(A, start=1):
    ans += comb.ncr(i - 1, K - 1) * a
    ans -= comb.ncr(N - i, K - 1) * a
    ans %= MOD

print(ans)


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inN(A) cin>>n;for(i=0;i<n;++i)cin>>A[i];
#define rep(i,a,b) for(i=a;i<b;++i)
#define repn for(i=0;i<n;++i)
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define sd second
#define ft first
#define vi vector<int>
#define vpii vector<pair<int,int> >
#define TC //int T;cin>>T;while(T--)
#define n_max 200001
#define mod 1000000007


int main() {
    int a,b,p,i,j,n,A[n_max];
    TC
    {
    	cin>>n;
    	rep(i,0,n)cin>>A[i];
    	sort(A,A+n);
    	ll ans=A[n-1];j=n-2;
    	for(i=n-3;i>0;i-=2)ans+=2*A[j--];
    	if(i==0)ans+=A[j];
    	cout<<ans;
    }
}



Measured memory_bytes: 4340
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using V = std::vector<std::pair<int, int>>;

int solve(int n, int m, V& v)
{
  std::string num(n, '-');
  for (const auto& p : v) {
    const auto s = p.first - 1;
    const auto c = p.second;
    const auto c_char = std::to_string(c)[0];
    if (num[s] == '-') {
      num[s] = c_char;
    } else if (num[s] != c_char) {
      return -1;
    }
  }

  for (int i = 0; i < n; ++i) {
    if (num[i] == '-') {
      if (i >= 1) {
        num[i] = '0';
      } else if (n == 1) {
        num[i] = '0';
      } else {
        num[i] = '1';
      }
    }
  }
  if (num[0] == '0' && n >= 2) return -1;
  return std::stoi(num);
}

int main()
{
  int n, m;
  std::cin >> n >> m;
  V v(m);
  for (auto& p : v) {
    std::cin >> p.first >> p.second;
  }

  std::cout << std::setprecision(20);
  std::cout << solve(n, m, v) << std::endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main () {
    vector<int> V;
    int n, q, c = 0, T;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int t = 0; scanf("%d", &t);
        V.push_back(t);
    }
    scanf("%d", &q);
    for (int i = 0; i < q; i++) {
        scanf("%d", &T);
        if (binary_search(V.begin(), V.end(), T)) c++;
    }
    printf("%d\n", c);
    return 0;
}


Measured memory_bytes: 3400
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <map>
using namespace std;
 
 
int main() {
    int n;
    cin>>n;
    vector<vector<int> > a(2, vector<int>(n));
    for(int i=0; i<2; ++i) {
        for(int j=0; j<n; ++j) {
            cin>>a[i][j];
        }
    }

    int sum=0, sum2, max=-1;
    for(int i=0; i<n; ++i) sum+=a[0][i];
    for(int i=n-1; i>=0; --i) {
        sum2=sum;
        for(int j=i; j<n; ++j) {
            sum2+=a[1][j];
        }
        if(max<sum2) max=sum2;
        sum-=a[0][i];
    }
    cout<<max<<endl;
    
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;
vector<int> G[200010];
int d[200010];
void dfs(int s){
	for(int x:G[s]){
		if(d[x]!=-1) continue;
		d[x] = d[s] + 1;
		dfs(x);
	}
}
int main(){
	int i,n;
	cin >> n;
	for(i=0;i<n-1;i++){
		int a,b; cin >> a >> b;
		a--; b--;
		G[a].push_back(b); G[b].push_back(a);
	}
	for(i=0;i<n;i++) d[i] = -1;
	d[0] = 0;
	dfs(0);
	int mx = 0,y = -1;
	for(i=0;i<n;i++){
		if(d[i]>mx){
			mx = d[i];
			y = i;
		}
	}
	for(i=0;i<n;i++) d[i] = -1;
	d[y] = 0;
	dfs(y);
	mx = 0;
	for(i=0;i<n;i++){
		mx = max(mx,d[i]);
	}
	if(mx%3==1){
		cout << "Second" << endl;
	}else{
		cout << "First" << endl;
	}
}
	

Measured memory_bytes: 18176
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 50;


int main(void) {
	int N, M;
	cin >>  N >> M;

	const int NMAX = 1000;
	const int MMAX = 2000;
	int a[MMAX], b[MMAX];
	ll c[MMAX];

	for (int i = 0; i < M; ++i) {
		cin >> a[i] >> b[i] >> c[i];
		c[i] = -c[i];
	}

	ll dist[NMAX];

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

	dist[0] = 0;


	for (int loop = 0; loop < N - 1; ++ loop) {
		for (int i = 0; i < M; ++i) {
			if (dist[a[i] - 1] == INF) continue;

			if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
				dist[b[i] - 1] = dist[a[i] - 1] + c[i];
			}
		}
	}

	ll ans = dist[N - 1];


	bool negative[NMAX];

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

	for (int loop = 0; loop < N ; ++ loop) {
		for (int i = 0; i < M; ++i) {
			if (dist[a[i] - 1] == INF) continue;

			if (dist[b[i] - 1] > dist[a[i] - 1] + c[i]) {
				dist[b[i] - 1] = dist[a[i] - 1] + c[i];
				negative[b[i] - 1] = true;
			}

			if (negative[a[i] - 1] == true) {
				negative[b[i] - 1] = true;
			}
		}
	}


	if (negative[N - 1])
		cout << "inf" << endl;
	else
		cout << -ans << endl;

	return 0;
}

Measured memory_bytes: 256
mod=10**9+7

N,M=map(int,input().split())
a=[int(input()) for _ in range(M)]
dp=[1,1]
m=0
if M>0 and a[0]==1:
    dp[1]=0
    m+=1
for i in range(2,N+1):
    if m<M and i==a[m]:
        dp.append(0)
        m+=1
    else:
        dp.append((dp[-1]+dp[-2])%mod)
print(dp[-1])

Measured memory_bytes: 7848
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define inf 1e+9
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define msg(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
#define pri(a) cout << a << endl;

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


  //ll n,k,ans=0;
  //cin >> n >> k;
  string s;
  cin >> s;

  if(s.length()<2){
    pri("No");
    return 0;
  }
  else if(s.length()%2!=0){
    pri("No");
    return 0;
  }

  int count=0;
  rep(i,s.length()){
    if(s[i]=='h' && count==0){
      count++;
    }
    else if(s[i]=='i' && count==1){
      count--;
    }
    else{
      pri("No");
      return 0;
    }
  }
  pri("Yes");
}


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

#ifdef ENABLE_DEBUG
template <typename T>
void debug(T value) {
  cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                              \
  do {                                          \
    cerr << " \033[33m (L" << __LINE__ << ") "; \
    cerr << #__VA_ARGS__ << ":\033[0m ";        \
    debug(__VA_ARGS__);                         \
    cerr << endl;                               \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int a, b, c, d, e, f;
  cin >> a >> b >> c >> d >> e >> f;
  double max_density = -1.0;
  int ans_water = -1, ans_sugar = -1;
  for (int ta = 0; ta <= f; ta += 100 * a) {
    for (int tb = 0; ta + tb <= f; tb += 100 * b) {
      int water = ta + tb;
      int max_sugar = e * water / 100;
      for (int sc = 0; sc <= max_sugar && water + sc <= f; sc += c) {
        int weight = water + sc;
        for (int sd = 0; sc + sd <= max_sugar && weight + sd <= f; sd += d) {
          int sugar = sc + sd;
          if (water + sugar == 0) continue;
          double density = 100.0 * sugar / double(water + sugar);
          if (max_density < density) {
            max_density = density;
            ans_water = water;
            ans_sugar = sugar;
          }
        }
      }
    }
  }
  cout << (ans_water + ans_sugar) << " " << ans_sugar << endl;
  DEBUG(max_density);
}


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

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<pdd> vdd;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define mem(a,b) memset(a, b, sizeof(a) )
#define all(x) (x).begin(),(x).end()
#define INF 1000000000000
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

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

int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

int main(){
	int x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	int x3=x2-(y2-y1),y3=y2+(x2-x1);
	cout<<x3<<" "<<y3<<" ";
    int x4=x3-(y3-y2),y4=y3+(x3-x2);
    cout<<x4<<" "<<y4<<endl;
}

Measured memory_bytes: 256
n=int(input())
l=[list(map(int,input().split())) for _ in range(n)]
goukei=0
for i in range(n):
  for k in range(i+1,n):
    goukei+=((l[i][0]-l[k][0])**2+(l[i][1]-l[k][1])**2)**0.5
goukei*=2
print(goukei/n)

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

using namespace std;

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

    int min = 300000, ans = 0;
    for (int i = 0; i < n; i++) {
        if (min > p[i])min = p[i], ans++;
    }
    cout << ans << endl;

}


Measured memory_bytes: 1024
#include<iostream>
#include<string>
using namespace std;
int prime[200];
bool ans[1000001];
int main(){
	int count=0;
	for(int i=2;i<1000;i++){
		bool t=true;
		for(int j=0;j<count;j++){
			if(i%prime[j]==0)
				t=false;
		}
		if(t==true){
			prime[count]=i;
			count++;
		}
	}
	int n,x,m;
	while(true){
	cin>>n>>x;
	if(n==0&&x==0)
		break;
	ans[0]=true;
	for(int i=1;i<x;i++)
		ans[i]=false;
	for(int u=0;u<n;u++){
		cin>>m;
		for(int i=0;i<m;i++){
			bool t=false;
			for(int j=i;j<=x;j+=m){
				if(ans[j]==true)
					t=true;
				if(t==true)
					ans[j]=true;
			}
		}
	}
	int best=x;
	while(true){
		if(ans[best]==true){
			bool t=true;
			for(int i=0;i<count;i++){
				if(best%prime[i]==0)
					t=false;
			}
			if(t==true)
				break;
		}
		best--;
		if(best==1)
			break;
	}
	if(best==1)
		cout<<"NA"<<endl;
	else
		cout<<best<<endl;
	}
	return 0;
}

Measured memory_bytes: 2136
from collections import deque

S = input()
Q = int(input())
d = deque(S)
#print(d)

mod = 0 #反転が何回目か（先頭がどっちか）,偶数回の時0，奇数回の時1

for _ in range(Q):
    Query = input()
    if Query[0] == "1":
        mod = (1 + mod) % 2
    else:
        if Query[2] == "1":
            if mod == 0:
                d.appendleft(Query[4])
            else:
                d.append(Query[4])
        else:
            if mod == 0:
                d.append(Query[4])
            else:
                d.appendleft(Query[4])

S = ""
if mod==0:
    for _ in range(len(d)):
        S += d.popleft()
else:
    for _ in range(len(d)):
        S += d.pop()
print(S)

Measured memory_bytes: 6644
#include<stdio.h>

int main (void){
        int a = 0;
        long long int i = 0;
        scanf("%d",&a);
        i = 1;
        for(int k = 0;k < a; k++){
                i = i * (k + 1);
                i = i % 1000000007;
        }
        printf("%lld\n",i);
        return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;

struct UnionFind {
    vector<int> par, rank;

    UnionFind(int N) : par(N), rank(N) {
        rep(i, N) par[i] = i, rank[i] = 0;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx == ry) return;
        if (rank[rx] < rank[ry]) {
            par[rx] = ry;
        }
        else {
            par[ry] = rx;
            if (rank[rx] == rank[ry]) rank[rx]++;
        }
    }

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


int main()
{
    int N, M, K;
    cin >> N >> M >> K;

    UnionFind tree(N);
    vector<vector<int>> Friend(N), Block(N);

    rep(i, M) {
        int A, B;
        cin >> A >> B;
        A--; B--;
        tree.unite(A, B);
        Friend[A].push_back(B);
        Friend[B].push_back(A);
    }

    rep(i, K) {
        int C, D;
        cin >> C >> D;
        C--; D--;
        Block[C].push_back(D);
        Block[D].push_back(C);
    }

    vector<int> relate(N);

    rep(i, N) {
        relate[tree.root(i)]++;
    }
    rep(i, N) {
        int res = relate[tree.root(i)] - 1 - (int)Friend[i].size();
        int minus = 0;
        rep(j, Block[i].size()) {
            if (tree.same(i, Block[i][j])) minus++;
        }
        cout << res - minus << endl;
    }
    return 0;
}

Measured memory_bytes: 15388
#include<bits/stdc++.h>
using namespace std;
long long half(long long h) {
  if(h==1){
    long long count =1;
    return count;
  }else{
    h = h/2;
    return 2*half(h)+1;
  }
}
 
int main(){
  long long h;
  long long count=0;
  cin>>h;
  cout<<half(h);
  return 0;
}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)

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

struct edge {
	int to,cost;
	edge(int t,ll c):to(t),cost(c) {}
};

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

ll mypow(ll x,ll n,ll m) {
	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;
}

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

int a,b,c,d;

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

	cin>>a>>b>>c>>d;
	int s=max(a,c),t=min(b,d);
	if(t<=s) cout<<0<<endl;
	else cout<<t-s<<endl;

	return 0;
}


Measured memory_bytes: 256
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n-1):
    num = a[i]+a[i+1]-x
    if num > 0:
        a[i+1] = max(a[i+1]-num, 0)
        ans += num
print(ans)


Measured memory_bytes: 20024
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
#define M 1000000007
ll N;
int main(){
	cin >> N;
	ll power = 1;
	REP(i,N) power = (power*(i+1)) % M;
	cout << power%M << endl;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef string st;
typedef double db;
typedef vector<long long> vecll;
typedef vector<char> vechar;

int main(){
    ll N, T, A;
    cin >> N >> T >> A;
    vecll H(N);
    for(auto& i : H) cin >> i;
    ll ans = 0;
    for(ll i = 0; i < N; i++){
        if(abs(A - T + H.at(i) * 0.006) < abs(A - T + H.at(ans) * 0.006)) ans = i;
    }
    cout << ans + 1 << endl;
}

Measured memory_bytes: 256
// https://beta.atcoder.jp/contests/abc066

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;

int main() {
    deque<int> q;
    int n;
    cin >> n;
    bool even_is_front = n % 2 == 0;
    for (int i = 1; i <= n; ++i) {
        int a;
        cin >> a;
        if (even_is_front) {
            if (i % 2 == 0) q.push_front(a);
            else q.push_back(a);
        }
        else {
            if (i % 2 == 0) q.push_back(a);
            else q.push_front(a);
        }
    }
    for (int i = 0; i < n; ++i) {
        cout << q.front();
        q.pop_front();
        if (i != n - 1) cout << " ";
        else cout << endl;
    }
    return 0;
}

Measured memory_bytes: 2944
#include<bits/stdc++.h>
using namespace std;
int n,a[100005],dp[100005];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	memset(dp,0x3f3f3f3f,sizeof(dp));
	dp[1]=0;
	dp[2]=abs(a[2]-a[1]);
	for(int i=3;i<=n;i++){
		dp[i]=min(dp[i-2]+abs(a[i-2]-a[i]),dp[i-1]+abs(a[i-1]-a[i]));
	}
	cout<<dp[n];
}

Measured memory_bytes: 1024
#include <iostream>
#include <cmath>

using namespace std;

int Bsort(int *arr,int n) {
	bool flag = true;
	int i = 0;
	int count = 0;
	while (flag) {
		flag = false;
		for (int j=n-1; j>i; j--) {
			if (arr[j] < arr[j-1]) {
				int t = arr[j];
				arr[j] = arr[j-1];
				arr[j-1] = t;
				flag = true;
				count++;
			}
		}
		i++;
	}
	return count;
}
int Ssort(int *arr,int n) {
	int count = 0;
	for (int i=0; i<n-1; i++) {
		int k = i;
		for (int j=i+1; j<n; j++) {
			if (arr[k] > arr[j]) {
				k = j;
			}
		}
		if (k != i) {
			int t = arr[k];
			arr[k] = arr[i];
			arr[i] = t;
			count++;
		}
	}
	return count;
}

int main() {
	int n;
	cin >> n;
	int *arr = new int[n];
	for (int i=0; i<n; i++) {
		cin >> arr[i];
	}
	int c = Ssort(arr,n);
	for (int i=0; i<n; i++) {
		if (i) cout << " ";
		cout << arr[i];
	}
	cout << endl;
	cout << c << endl;
	return 0;
}


Measured memory_bytes: 3108
#include<iostream>
using namespace std;
#include<vector>
template<typename T>
struct weightedUF{
	int n;
	vector<int>parent,rank;
	vector<T>dweight;
	weightedUF(int n_=0,T defvalue=0):n(n_),parent(n_),rank(n_,1),dweight(n_,defvalue)
	{
		for(int i=0;i<n;i++)parent[i]=i;
	}
	int find(int a)
	{
		if(parent[a]==a)return a;
		else
		{
			int ar=find(parent[a]);
			dweight[a]+=dweight[parent[a]];
			return parent[a]=ar;
		}
	}
	T weight(int a)
	{
		find(a);
		return dweight[a];
	}
	bool same(int a,int b)
	{
		return find(a)==find(b);
	}
	bool unite(int a,int b,int w)//a+w==b
	{
		w+=weight(a);w-=weight(b);
		a=find(a);b=find(b);
		if(a==b)return false;
		if(rank[a]<rank[b])
		{
			parent[a]=b;
			dweight[a]=-w;
			rank[b]+=rank[a];
		}
		else
		{
			parent[b]=a;
			dweight[b]=w;
			rank[a]+=rank[b];
		}
		return true;
	}
	T diff(int a,int b)
	{
		return weight(b)-weight(a);
	}
};
main()
{
	int N,M;cin>>N>>M;
	weightedUF<int>UF(N);
	for(;M--;)
	{
		int L,R,D;cin>>L>>R>>D;
		L--,R--;
		if(UF.same(L,R))
		{
			if(UF.diff(L,R)!=D)
			{
				cout<<"No"<<endl;
				return 0;
			}
		}
		else
		{
			UF.unite(L,R,D);
		}
	}
	cout<<"Yes"<<endl;
}


Measured memory_bytes: 1536
#include<stdio.h>
int main()
{
  int K,S;
  int X,Y,Z;
  int con=0;
  
  scanf("%d %d",&K,&S);
  
  for(int i=0;i<=K;i++){
    for(int j=0;j<=K;j++){
      X=i;
      Y=j;
      Z=S-X-Y;
      if(Z<=K&&Z>=0) con++;
    }
  }
  printf("%d",con);
  
  return 0;
}

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

int main() {
  int N, K;
  long long S = 0;
  cin >> N >> K;
  vector<int> H(N);
  
  for (int i=0; i<N; i++) {
    cin >> H.at(i);
  }
  
  sort(H.begin(), H.end());
  
  for (int i=0; i<N-K; i++) {
    S += H.at(i);
  }
  
  cout << S <<endl;
    
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define speed ios_base::sync_with_stdio(false)
#define boost cin.tie(NULL)
#define booster cout.tie(NULL)
#define endl "\n"
typedef long long int lld;

#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define F1(a,n) for(int i=1;i<=n;i++){cin>>a[i];}

using namespace std;


int main(){
	
	speed;
	boost;
	
	lld N;
	cin>>N;
	
	unordered_map<lld,lld> mp;
	
	for(lld i=0;i<N;i++){
		lld c;
		cin>>c;
		mp[c]++;
	}
	
	lld sum=0;
	for(auto it= mp.begin();it != mp.end(); it++){
		if(it->second < it->first)
			sum+= (it->second);
		else if(it->second > it->first)
			sum+= (it->second - it->first);
		
	}
	
	cout<<sum;
	
	return 0;
}


Measured memory_bytes: 4260
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cassert>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define MAX 41000
using namespace std;

struct P
{
  int x,y,cost;
  P(int x=inf,int y=inf,int cost=inf):x(x),y(y),cost(cost){}
  bool operator < (const P& a)const
  {
    return cost > a.cost;
  }
};

int N;
bool exist[MAX][2];
string line;
int cost[4][3][3] = 
  {
    //{{(0,0)},{(0,1)},{(0,2)}},{{(1,0)},{(1,1)},{(1,2)}},{{(2,0)},{(2,1)},{(2,2)}}
    {{0,1,2},{1,0,1},{2,1,0}}, // N N
    {{3,2,2},{2,1,1},{2,1,1}}, // N Y
    {{1,1,2},{1,1,2},{2,2,3}}, // Y N
    {{3,2,2},{2,2,2},{2,2,3}}  // Y Y
  };

void makeExist()
{
  rep(i,line.size())
    {
      if(i < 2*N)
	{
	  //	  cout << "index : " << ceil(i/2.0) << endl;
	  if(line[i] == 'Y')
	    exist[(int)ceil(i/2.0)][0] = true;
	}
      else
	{
	  //	  cout << "index : " << ceil(i/2.0)-N << endl;
	  if(line[i] == 'Y')
	    exist[(int)ceil(i/2.0)-N][1] = true;
	}
    }
}

int getSP(int i)
{
  if(!exist[i][0] && !exist[i][1])return 0;
  if(!exist[i][0] &&  exist[i][1])return 1;
  if( exist[i][0] && !exist[i][1])return 2;
  if( exist[i][0] &&  exist[i][1])return 3;
  assert(false);
}

void compute()
{
  int dp[3][2];
  rep(i,3)rep(k,2)dp[i][k] = inf;
  dp[0][0] = 0;
  dp[1][0] = 1;
  dp[2][0] = 2;

  int ans = inf;

  rep(i,N+1)
    {
      rep(k,3)dp[k][(i+1)&1] = inf;
      rep(from,3)
	{
	  if(dp[from][i&1] == inf)continue;
	  rep(to,3)
	    {
	      dp[to][(i+1)&1] = min(dp[to][(i+1)&1],
				    dp[from][i&1] + cost[getSP(i)][from][to]+1);
	      //cout <<  "phase : " << i << " from : " << from << " dp[" << to << "][" << ((i+1)&1) << "] = " << dp[to][(i+1)&1] << " cost["<<getSP(i)<<"]["<<from<<"]["<<to<<"] = " << cost[getSP(i)][from][to] << endl;
	    }
	}
      if(i == N)
	{
	  ans = min(ans,dp[0][(i+1)&1]-1);
	  ans = min(ans,dp[1][(i+1)&1]);
	  ans = min(ans,dp[2][(i+1)&1]+1);
	}
    }
  cout << ans << endl;
}

int main()
{
  int T;
  while(cin >> T)
    {
      while(T--)
	{
	  cin >> N;
	  rep(i,N+1)rep(j,2)exist[i][j] = false;

	  cin >> line;
	  makeExist();

	  compute();

	}
    }
  return 0;
}

Measured memory_bytes: 1332
#include<iostream>
#include<algorithm>
using namespace std;
int a[26]={0},b[26]={0};
int main(){
	string s,t;
	int i,j,f=1;
	cin >> s >> t;
	for(i=0;i<s.size();i++){
		a[s[i]-'a']++;
	}
	for(i=0;i<t.size();i++){
		b[t[i]-'a']++;
	}
	sort(a,a+26);
	sort(b,b+26);
	for(i=0;i<26;i++){
		if(a[i]!=b[i]){
			f=0;
			break;
		}
	}
	if(f==0){
		cout <<"No\n";
	}
	else{
		cout << "Yes\n";
	} 
	return 0;
}


Measured memory_bytes: 776
# -*- coding: utf-8 -*-
"""
Created on Thu Aug  9 01:39:56 2018

@author: Yuki
"""

N = int(input())
plans = [[0, 0, 0]]
for i in range(N):
    plans.append([int(ii) for ii in input().split()])

Yes_flag = True
# 移動距離が遠すぎないかのチェック
for i in range(N):
    if (abs(plans[i+1][1] - plans[i][1]) + abs(plans[i+1][2] - plans[i][2])) > (plans[i+1][0] - plans[i][0]):
        Yes_flag = False
        break
for i in range(1, N+1):
    if plans[i][0] % 2 == 0:
        if (plans[i][1] + plans[i][2]) % 2 != 0:
            Yes_flag = False
            break
    else:
        if (plans[i][1] + plans[i][2]) % 2 == 0:
            Yes_flag = False
            break
if Yes_flag:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 21156
# 入力は以下の形式で標準入力から与えられる。
# N X
# m1
# m2
# :
# mN
n, x = gets.chomp.split(" ").map(&:to_i)

all_kind_sum = 0
array_m = []
n.times do |idx|
  array_m << gets.chomp.to_i
  all_kind_sum += array_m[idx]
end

# 赤木さんは、「お菓子の素」という粉のみを材料として 
# N 種類のドーナツを作ることができます。
# これらのドーナツは ドーナツ 1、ドーナツ 2、... ドーナツ N と呼ばれます。

# ドーナツ i (1 ≤ i ≤ N) を 1 個作るには、
# お菓子の素 miグラムを消費する必要があります。
# 0.5 個など整数でない個数のドーナツを作ることはできません。

# 赤木さんはお菓子の素を X グラム持っています。
# たくさんのドーナツを作ることにしました。

# 次の条件を守ることにしました。
# N 種類のドーナツそれぞれを少なくとも 1 個は作る。

# このとき、最大で何個のドーナツを作ることができるでしょうか？
# お菓子の素を使い切る必要はありません。
# この問題の制約のもとでは、条件を守ることは必ず可能です。

# 条件を守って作ることのできるドーナツの最大の個数を出力せよ。
rest_val = x - all_kind_sum

array_n = []
n.times do |idx|
  array_n << rest_val / array_m[idx]
end

array_n.sort!
p array_n.last + n

Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\n"
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

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

signed main() {
    cin >> N;
    vector<int> a(N);
    cin >> a;
    int m = 1000;
    REPS(i, N - 1) {
        dump(m, cnt);
        if (a[i - 1] < a[i]) {
            m = (m / a[i - 1]) * a[i] + m % a[i - 1];
        }
    }
    cout << m << endl;
}


Measured memory_bytes: 3608