akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin


def print_time_lefts(seconds):
    h, remain = divmod(seconds, 3600)
    m, s = divmod(remain, 60)
    print('{:02d}:{:02d}:{:02d}'.format(h, m, s))

for line in stdin:
    h, m, s = (int(s) for s in line.split())
    if h == -1 and m == -1 and s == -1:
        break
    std_time_lefts = 7200 - h * 3600 - m * 60 - s
    print_time_lefts(std_time_lefts)
    print_time_lefts(std_time_lefts * 3)

Measured memory_bytes: 4256
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n;
int dp[3100][3100];
int a[3100];

main(){
  cin>>n;
  FOR(i,1,n+1){
    cin>>a[i];
  }

  FOR(r,1,n+1){
    RFOR(l,r,1){
      dp[r][l]=max(a[l]-dp[r][l+1],a[r]-dp[r-1][l]);
    }
  }

  cout<<dp[n][1]<<endl;

}


Measured memory_bytes: 72320
#include <iostream>
#include <vector>

using namespace std;

int N, M;
bool e[10][10];
int ans = 0;
int p[10];

void dfs(int pos, int mask) {
		if (pos == N) {
				for (int i = 0; i < N-1; i++) {
						if (p[0] != 0) return;
						else if (!e[p[i]][p[i+1]]) return;
				}
				ans++;
				return;
		}

		for (int i = 0; i < N; i++) {
				if (mask & (1<<i)) {
						p[pos] = i;
						dfs(pos+1, (mask^(1<<i)));
				}
		}
}

int main() {
		cin >> N >> M;
		for (int i = 0; i < M; i++) {
				int a, b;
				cin >> a >> b;
				a--; b--;
				e[a][b] = true;
				e[b][a] = true;
		}

		dfs(0, (1<<N) - 1);
		cout << ans << endl;
		return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000

int main() {
    int N, K;
    cin >> N >> K;
    vector<ll> A(N);

    rep(i, N) { cin >> A[i]; }

    ll res = INF;
    for (int i = 0; i + K - 1 < N; i++) {
        ll left = A[i], right = A[i + K - 1];
        res = min(res, min(abs(left), abs(right)) + right - left);
    }

    cout << res << endl;
}

Measured memory_bytes: 1024
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelfCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = Console.ReadLine().Split(' ');
            var a = int.Parse(input[0]);
            var b = int.Parse(input[1]);
            var ans = Math.Max(2 * a - 1, Math.Max(2 * b - 1, a + b));
            Console.WriteLine(ans); 
        }
    }
}


Measured memory_bytes: 13140
#include <bits/stdc++.h>
using namespace std;
 
#define rep for(int i = 0;i<n;i++)
const int MOD = 1000000007;
 
int main(){
    int n;cin >> n;
    int p[n];rep{
        cin >> p[i];
    }
    int dp[n] = {};
    for(int i = 1;i<n;i++){
        if(i>1){
            dp[i] = min(dp[i-1] + abs(p[i]-p[i-1]),dp[i-2]+abs(p[i]-p[i-2]));
        }else{
            dp[i] = abs(p[i]-p[i-1]);
        }
    }
    cout << dp[n-1] << endl;
}

Measured memory_bytes: 1024
using System.Text;
using System;

public class Hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var n = int.Parse(line[0]);
        var k = int.Parse(line[1]);
        var q = int.Parse(line[2]);
        var a = new int[n + 1];
        for (int i = 0; i < q; i++)
        {
            var t = int.Parse(Console.ReadLine().Trim()) - 1;
            if (t == 0) { a[1]++; a[n]--; }
            else if (t == n - 1) { a[0]++; a[n - 1]--; }
            else
            {
                a[0]++; a[t]--;
                a[t + 1]++; a[n]--;
            }
        }
        getAns(a, n, k);
    }
    static void getAns(int[] a, int n, int k)
    {
        var sb = new StringBuilder();
        var w = 0;
        for (int i = 0; i < n; i++)
        {
            w += a[i];
            if (k - w > 0) sb.Append("Yes\n");
            else sb.Append("No\n");
        }
        Console.WriteLine(sb);
    }
}


Measured memory_bytes: 18144
from heapq import heappop, heappush


def main():
    x, y, z, k = map(int, input().split())
    aa = sorted(map(int, input().split()))
    bb = sorted(map(int, input().split()))
    cc = sorted(map(int, input().split()))
    v = aa[-1] + bb[-1] + cc[-1]
    t = (len(aa) - 1, len(bb) - 1, len(cc) - 1)
    q = [(-v, t)]
    vs = []
    ts = set()
    while len(vs) < k:
        nv, t = heappop(q)
        if t in ts:
            continue
        vs.append(-nv)
        ts.add(t)
        if t[0] > 0:
            heappush(q, (-(aa[t[0] - 1] + bb[t[1]] + cc[t[2]]), (t[0] - 1, t[1], t[2])))
        if t[1] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1] - 1] + cc[t[2]]), (t[0], t[1] - 1, t[2])))
        if t[2] > 0:
            heappush(q, (-(aa[t[0]] + bb[t[1]] + cc[t[2] - 1]), (t[0], t[1], t[2] - 1)))
    for v in vs:
        print(v)


if __name__ == "__main__":
    main()


Measured memory_bytes: 4724
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <stdlib.h>
#include <set>
typedef long long ll;
using namespace std;

int main() {
	string s;
	int w;
	cin>>s;
	s.erase(4,1);
	s.erase(6,1);
	w=stoi(s);
	if(w>20190430){
		cout<<"TBD"<<endl;
	}else{
		cout<<"Heisei"<<endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
//個別に期待値だして累積和比較
int main(){
  ll N,K; cin>>N>>K;
  vector<ll> A(N,0);
  vector<long double> B(N+2,0);
  //  1 2 3 4
  //0 1 3 6 10
  for(int i=0; i<N; i++){
    long double p,q; cin>>p;
    A[i]=p;
    q=(1+p)*p/2/p;
    B[i+1]=B[i]+q;
  }
  long double ans=0;
  for(int i=K; i<=N; i++) ans=max(B[i]-B[i-K], ans);
  cout<<fixed<<setprecision(10)<<ans<<endl;
}

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

int a[500];
int main()
{
	int m, n, k, t;
	cin >> m;
 n=0;
	if(m==0)
	printf("0\n");


	while(m != 0){
	k = m % (-2);
	t = m / (-2);
	if(k < 0){
	k = k-(-2);
	t++;
	}
	m = t;
	a[n++]=k;
}
	for(int i=n-1;i>=0;i--){
	cout << a[i];
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ALL(obj) obj.begin(),obj.end()
#define pb(obj) push_back(obj)
#define REP(i,N) for(ll i=0;i<N;i++)
typedef long long ll;
ll const MOD=1000000007;
const int MAX = 5000;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
   fac[0] = fac[1] = 1;
   finv[0] = finv[1] = 1;
   inv[1] = 1;
   for (int i = 2; i < MAX; i++){
       fac[i] = fac[i - 1] * i % MOD;
       inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
       finv[i] = finv[i - 1] * inv[i] % MOD;
   }
}
// 二項係数計算
long long COM(int n, int k){
   if (n < k) return 0;
   if (n < 0 || k < 0) return 0;
   return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    COMinit();
    int N,K;
    cin>>N>>K;
    ll sub;
    for(int i=1;i<=K;i++){
        cout<<COM(K-1,i-1)*COM(N-K-(i-1)+i,i)%MOD<<endl;
    }

}

Measured memory_bytes: 384
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define SORT(v,n) sort(v,v+n)
#define rep(i,n) FOR(i,0,n)
#define EPS (1e-7)
#define INF (1e9)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef priority_queue<int> PQI;
typedef priority_queue<int,vector<int>,greater<int> > PQSI;
typedef priority_queue<ll> PQL;
typedef priority_queue<ll,vector<ll>,greater<ll> > PQSL;

const int MOD = 1000000007;
struct mint { int n; mint(int n_ = 0) : n(n_) {} };
mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
istream &operator>>(istream &i, mint &a) { return i >> a.n; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }

vector<mint> F_{1, 1}, R_{1, 1}, I_{0, 1};
void check_fact(int n) {
  for (int i = I_.size(); i <= n; i++) {
    I_.push_back(I_[MOD % i] * (MOD - MOD / i));
    F_.push_back(F_[i - 1] * i);
    R_.push_back(R_[i - 1] * I_[i]);
  }
}

mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[n]; }
mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; }
mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; }
mint C(int n, int r) { return F(n) * R(n - r) * R(r); }
mint P(int n, int r) { return F(n) * R(n - r); }
mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); }

template<typename T>
T gcd(T x, T y){
    if (y == 0) return x;
    else return gcd(y, x % y);
}

template<typename T>
T lcm(T x, T y){
    return (x / gcd(x, y)) * y;
}

template<typename T>
T ceil(T x, T y){
    return (x + (y - 1)) / y;
}

int N;
int A[200000];
int B[200000];

int main(void){
    cin >> N;
    rep(i,N) scanf("%d",&A[i]);
    rep(i,N) B[i] = A[i];

    sort(A,A+N);
    int max1 = A[N-1];
    int max2 = A[N-2];


    rep(i,N){
        if(B[i] == max1) cout << max2 << endl;
        else cout << max1 << endl;
    }

    return 0;
}



















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

int main(void){
    
    int N;
    long long K;
    cin >> N >> K;
    int A;
    map<int,int> mpA;
    for(int i = 0; i < N;i++){
        cin >> A;
        A--;
        mpA[i] = A;
    }
    
    vector<int> vec;
    map<int,int> mpF;
    int mrk;
    map<int,int> mpC;
    
    int cont = 0;
    int fr = 0;
    int to = mpA[0];
    vec.push_back(0);
    mpF[0]++;
    mpC[0] = 0;
    while(true){
        
        if(mpF[to]!=0){
            //cout << to <<endl;
            mrk = to;
            break;
        }
        vec.push_back(to);
        mpF[to]++;
        cont++;
        mpC[to] = cont;
        
        fr = to; 
        to = mpA[to];
        
    }
    

    
    // cout << mrk <<endl;
    // cout << mpC[mrk] <<endl;
    // cout << cont <<endl;
    vector<int> V = vec;

    vec.erase(vec.begin(),vec.begin()+mpC[mrk]);
    
    
    //cout << (K- mpC[mrk] )%vec.size() <<endl;
    
    if(K > mpC[mrk] )cout << vec[(K- mpC[mrk] )%vec.size()] + 1  <<endl;
    else cout << V[K] + 1  <<endl;
    
    // for(auto u:vec){
    //     cout << u + 1;
    // }
    
    
    
    
    
}


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

using namespace std;

#define int long long
#define tie() ios_base :: sync_with_stdio(0); cin.tie(0) ; cout.tie(0)
#define err(x) cerr << #x << " is : " << x << endl
#define mem(a , v)   memset(a , v , sizeof a)
#define all(v) v.begin() , v.end()

const int N = 1e5 + 20;
const int mod = 1e9 + 7;

int32_t main()
{
      tie();
      int t = 1;
      //cin >> t;
      while(t--)
      {
            string s;
            cin >> s;
            set <char> st;
            for(char &x : s)
                  st.insert(x);
            if(st.size() == 2)
                  cout << "Yes" << endl;
            else
                  cout << "No" << endl;
      }
      return 0;
}


Measured memory_bytes: 256
use std::str::FromStr;
use std::io::stdin;
use std::cmp::max;

fn main() {
    let (n, w) = {
        let line = read_vec::<usize>();
        (line[0], line[1])
    };
    let mut vec = Vec::new();
    for _ in 0..n {
        let line = read_vec::<usize>();
        vec.push((line[0], line[1]));
    }
    let mut dp = Vec::new();
    for i in 0..(n + 1) {
        dp.push(Vec::new());
        for _ in 0..(w + 1) { dp[i].push(0 as usize); }
    }
    for i in 0..n {
        let (weight, value) = vec[i];
        for j in 0..(w + 1) {
            if (j as i64) - (weight as i64) >= 0 {
                dp[i + 1][j] = max(dp[i][j], dp[i][j - weight] + value);
            } else {
                dp[i + 1][j] = dp[i][j];
            }
        }
    }
    println!("{}", dp.last().unwrap().last().unwrap());
}

fn read_vec<T: FromStr>() -> Vec<T> {
    let mut line = String::new();
    stdin().read_line(&mut line).ok();
    line.trim().split_whitespace()
        .map(|i| i.parse().ok().unwrap()).collect()
}

Measured memory_bytes: 209148
#include <iostream>

using namespace std;

int main(){
    unsigned long long N, M;
    cin >> N >> M;
    if(N > M){
        swap(N, M);
    }

    unsigned long long ans = 0;
    if(N == 1 && M == 1){
        ans = 1;
    }else if(N == 1 && M > 1){
        ans = M-2;
    }else{
        ans = (N-2)*(M-2);
    }

    cout << ans << endl;


    return 0;
}

Measured memory_bytes: 256
#include <iostream>
// #include <cassert>
#include <vector>
#include <algorithm>
#include <climits>

// #include <stack>
// #include <queue>
// #include <deque>
// #include <set>
// #include <map>
// #include <string>
// #include <unordered_set>
// #include <unordered_map>

// #include <tuple>
// #include <cmath>
// #include <numeric>

using namespace std;
typedef long long ll;
typedef pair<long,long> pll;
typedef pair<int,int> pii;
typedef pair<int,long> pil;
typedef pair<long,int> pli;
typedef vector<vector<long long>> vvll;
typedef vector<vector<long>> vvl;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;

#define emplace_back pb
const char el = '\n';

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

    long long a,b,c; cin>>a>>b>>c;

    long long rhs = c-a-b;
    if (rhs<=0) {
        cout<<"No"<<el;
        return 0;
    }

    rhs *= rhs;
    long long lhs = 4*a*b;

    if (lhs<rhs) {
        cout<<"Yes";
    } else {
        cout<<"No";
    } cout<<el;

}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
 
int
main()
{
    int n;
    scanf("%d", &n);
 
    int i;
    int result;
    int a_score = 0;
    int b_score = 0;
    char a_card[100];
    char b_card[100];
    for (i = 0; i < n; i++)
    {
        scanf("%s %s", a_card, b_card);
        result = strcmp(a_card, b_card);
        if (result == 0)
        {
            a_score += 1;
            b_score += 1;
        }
        if (result > 0)
        {
            a_score += 3;
        }
        if (result < 0)
        {
            b_score += 3;
        }
    }
    printf("%d %d\n", a_score, b_score);
 
    return 0;
}


Measured memory_bytes: 2068
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

int main() {
  int a, b;
  cin >> a >> b;
  if (b < a) swap(a, b);
  if ((b - a) % 2) {
    cout << "IMPOSSIBLE" << endl;
  } else {
    cout << (a + b) / 2 << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int H, W; cin >> H >> W;
	vector<string>v(H);
	rep(i, 0, H)cin >> v[i];
	map<char, int>m;
	rep(i, 0, H)rep(j, 0, W) {
		m[v[i][j]]++;
	}

	bool f(H % 2 == 0), g(W % 2 == 0);
	bool h = true;
	if (f&&g) {

		for (auto &x : m)if (x.second % 4 != 0)h = false;
	}
	else if (f || g) {
		int sum(0);
		for (auto &x : m) {
			sum += x.second % 4;
			if (x.second % 2 == 1)h = false;
		}
		if (sum > H&&f)h = false;
		else if (sum > W&&g)h = false;
	}
	else {
		int sum(0);
		int cnt(0);
		for (auto &x : m) {
			sum += x.second % 4;
			if (x.second % 2 == 1)cnt++;
		}
		if (cnt != 1 || sum > H + W - 1)h = false;
	}

	cout << (h ? "Yes" : "No") << endl;
	return 0;
}

Measured memory_bytes: 256
S=str(input())
T=str(input())
s=[str(c) for c in S]
t=[str(c) for c in T]
flag=0
for i in range(len(s)):
  if s[i]!=t[i]:
    flag=1
if flag==0:
  if len(s)+1==len(t):
    print("Yes")
  else:
    print("No")
else:
  print("No")

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

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

const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;

vector<lint> divisor(lint n) {
	vector<lint> ret;
	for (lint i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			ret.emplace_back(i);
			if (i * i != n) ret.emplace_back(n / i);
		}
	}
	sort(ret.begin(), ret.end(), greater<lint>());
	return ret;
}

int main() {

	int N, M;
	cin >> N >> M;

	auto divs = divisor(M);

	int ans;
	for (auto& ele : divs) {
		if (M / ele >= N) {
			ans = ele; break;
		}
	}

	cout << ans << "\n";

	return 0;

}

Measured memory_bytes: 256
char m[1001*1002];
v[1001*1002];
q[1001*1002],d,e;
h,w,p[10],x;
main(n,i,j,c,t){
	scanf("%d%d%d",&h,&w,&n);
	w++;
	for(i=0;i<h*w;i++){
		c=getchar();
		if(c>='1'&&c<='9')
			p[c-'0']=w+i,c='.';
		if(c=='S')
			p[0]=w+i,c='.';
		m[w+i]=c;
	}
	t=0;
	for(i=0;i<n;i++){
		memset(v,0,4*(h+2)*w);
		d=e=0;
		v[q[e++]=p[i]]=1;
		for(;!v[p[i+1]];){
			x=q[d++];
			if(m[x-1]=='.'&&v[x-1]==0)
				v[q[e++]=x-1]=v[x]+1;
			if(m[x+1]=='.'&&v[x+1]==0)
				v[q[e++]=x+1]=v[x]+1;
			if(m[x-w]=='.'&&v[x-w]==0)
				v[q[e++]=x-w]=v[x]+1;
			if(m[x+w]=='.'&&v[x+w]==0)
				v[q[e++]=x+w]=v[x]+1;
		}
		t+=v[p[i+1]]-1;
	}
	{int n;for(n=100000;n--;)getchar();}//delay(0.05s)
	x=!printf("%d\n",t);
}

Measured memory_bytes: 5636
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int a[222222];
int L[222222];

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(a[i]>i)
		{
			cout<<-1<<'\n'; return 0;
		}
	}
	for(int i=0;i<n;i++)
	{
		L[i] = i-a[i];
	}
	ll ans = 0;
	for(int i=0;i+1<n;i++)
	{
		if(L[i]>L[i+1])
		{
			cout<<-1<<'\n'; return 0;
		}
	}
	int curpref = int(1e9);
	for(int i=n-1;i>=0;i--)
	{
		if(L[i]==curpref) continue;
		ans += i - L[i];
		curpref = L[i];
	}
	cout<<ans<<'\n';
}	


Measured memory_bytes: 1792
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main() {
  string s;
  getline(cin, s);
  for(int i = 0; i < s.size(); i++) {
    if('a' <= s[i] && s[i] <= 'z') {
      printf("%c", s[i]+('A'-'a'));
    } else {
      printf("%c", s[i]);
    }
  }
  cout << endl;
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#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++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 1000000009
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

int main(){
  ll N, A, B;
  cin >> N >> A >> B;
  cout << N/(A+B)*A + min(N%(A+B), A) << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define dbg(x) cout<<#x<<": "<<(x)<<"\n"

using namespace std;

const int MAX_N = 114514;
int n, m;

struct edge {
    int u, v, cost;
    edge() {}
    edge(int a, int b, int c) {
        u=a; v=b; cost=c;
    }
};

vector<edge> es;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}


//
// Union-Find
//
int par[MAX_N], _rank[MAX_N];

void init(int n) {
    for (int i = 0; i < n; i++) {
        par[i] = i; _rank[i] = 0;
    }
}

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

void unite(int x, int y) {
    x = find(x); y = find(y);
    if(x == y) return ;

    if(_rank[x] < _rank[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if(_rank[x] == _rank[y]) _rank[x]++;
    }
}

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


int kruskal() {
    sort(es.begin(), es.end(), comp);
    init(n);
    int res = 0;
    rep(i,m) {
        edge e = es[i];
        if(!same(e.u, e.v)) {
            unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

void solve() {
    es.clear();
    cin>>m;
    rep(i,m) {
        int a,b,d;
        scanf("%d,%d,%d",&a,&b,&d);
        es.push_back(edge(a, b, d/100 - 1));
    }
    int res = kruskal();
    cout << res << endl;
}

int main() {
    while(cin>>n, n) solve();
    return 0;
}

Measured memory_bytes: 3220
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  long long n, a;
  scanf("%ld", &n);
  vector<int> v(n);
  for(int i = 0; i < n; ++i) scanf("%d", &v[i]);
  vector<vector<long long>> dp(n+1, vector<long long>(4, -1e15));
  dp[0][0] = 0; dp[1][0] = v[0]; dp[2][1] = v[1]; if(n > 2) dp[3][2] = v[2];
  for(int i = 3; i <= n; ++i){
    if(i & 1){
      dp[i][0] = dp[i-2][0] + v[i-1];
      dp[i][2] = max(dp[i][2], max(dp[i-2][2] + v[i-1], dp[i-3][1] + v[i-1]));
      if(i > 3) dp[i][2] = max(dp[i][2], dp[i-4][0] + v[i-1]);
    }
    else dp[i][1] = max(dp[i-2][1] + v[i-1], dp[i-3][0] + v[i-1]);
  }
  if(n % 2 == 0) a = max(dp[n][1], dp[n-1][0]);
  else a = max(dp[n][2], max(dp[n-1][1], dp[n-2][0]));
  printf("%ld\n", a);
  return 0;
}

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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
 
Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

Int dp[101][100001];

int main() {
    Int n, k;
    cin >> n >> k;
    vector<Int> a(n);
    for (Int i = 0; i < n; i++) {
        cin >> a[i];
    }
    dp[0][0] = 1;
    for (Int i = 0; i < n; i++) {
        for (Int j = 0; j <= k; j++) {
            dp[i + 1][j] += dp[i][j];
            dp[i + 1][j] %= mod;
            if (j + a[i] + 1 <= k) {
                dp[i + 1][j + a[i] + 1] = (dp[i + 1][j + a[i] + 1] - dp[i][j] + mod) % mod;
            }
        }
        for (Int j = 0; j < k; j++) {
            dp[i + 1][j + 1] += dp[i + 1][j];
            dp[i + 1][j + 1] %= mod;
        }
    }
    dump(dp[n][k]);
    return 0;
}

Measured memory_bytes: 78336
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <bitset>
#define INF	100000000
#define pb	push_back
#define fi first
#define sec second
#define SS stringstream
using namespace std;
typedef pair<int, int> P;
typedef long long int	ll;

vector<double> vec;

int main(){
	double temp;
	while(cin >> temp) vec.pb(temp);
	sort(vec.begin(), vec.end());
	cout << vec[vec.size()-1]-vec[0] << endl;
}

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

int main(){
	/*input*/
	long int n;	cin >> n;
	vector<long int>x(n);
	for(long int i = 0; i < n; i++){
		cin >> x[i];
	}
	/*calculation*/
	vector<long int>tmp(n);
	tmp = x;
	sort(tmp.begin(), tmp.end());
	int left, right;
	left = tmp[n/2 - 1];
	right = tmp[n/2];
	/*output*/
	for(long int i = 0; i < n; i++){
		if(x[i] <= left){
			cout << right << endl;
		}else{
			cout << left << endl;
		}
	}
}


Measured memory_bytes: 5376
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
#define rep(i,n) for (ll i=0;i<(n);++i)
#define rep2(i,a,b) for (ll i=(a);i<(b);++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(),(v).end()
const ll MOD=1e9+7;
//const ll MOD=998244353;
const ll INF=1e9;
const ll IINF=1e18;
const double EPS=1e-8;
const double pi=acos(-1);

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;
}

template<typename T,bool Mono>
struct ConvexHullTrick{
    typedef function<bool(T,T)> C;
    #define F first
    #define S second
    using Line=pair<T,T>;
    C cmp;
    deque<Line> lines;
    ConvexHullTrick(C cmp=[](T l,T r){return l>=r;})
        :cmp(cmp){}
    bool check(Line l1,Line l2,Line l3){
        if (l1<l3) swap(l1,l3);
        return (l3.S-l2.S)*(l2.F-l1.F)>=(l2.S-l1.S)*(l3.F-l2.F);
    }
    void add(T a,T b){
        Line line(a,b);
        while(lines.size()>=2&&check(*(lines.end()-2)
        ,lines.back(),line)) lines.pop_back();
        lines.emplace_back(line);
    }
    T f(int i,T x){
        return lines[i].F*x+lines[i].S;
    }
    T f(Line line,T x){
        return line.F*x+line.S;
    }
    T query(T x){
        if (Mono){
            while(lines.size()>=2
            &&cmp(f(0,x),f(1,x))) lines.pop_front();
            return f(0,x);
        } else {
            int lb=-1,ub=lines.size()-1;
            while(ub-lb>1){
                int mid=(lb+ub)>>1;
                (cmp(f(mid,x),f(mid+1,x))?lb:ub)=mid;
            }
            return f(ub,x);
        }
    }
    #undef F
    #undef S
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N,C; cin >> N >> C;
    vector<ll> h(N);
    rep(i,N) cin >> h[i];
    ConvexHullTrick<ll,true> CHT;
    vector<ll> dp(N,0);
    rep(i,N){
        if (i) dp[i]=CHT.query(h[i])+h[i]*h[i]+C;
        CHT.add(-2*h[i],h[i]*h[i]+dp[i]);
    }
    cout << dp[N-1] << endl;
}

Measured memory_bytes: 5632
#include <iostream>
 
using namespace std;
 
int main()
{
    long long a, b, c, k;
    long long sum = 0;
 
    cin >> a >> b >> c >> k;
 
    if (a >= k)
    {
        sum += k;
    }
    else if (a == 0 && b >= k)
    {
        sum = 0;
    }
    else if (a == 0 && b == 0 && c >= k)
    {
        sum -= k;
    }
    else
    {
        sum = a - (k - a - b);
    }
 
    /*
    while(true){
        f(a>0)
        {
            sum += k - a;
            if(a)
        }else if(a==0 && b > 0)
        {
            b--;
        }
        else if(b == 0 && c > 0)
        {
            sum--;
            c--;
        }
    }*/
 
    cout << sum << endl;
}

Measured memory_bytes: 3604
#include<stdio.h>
int main(void)
{
	int d, m1, m2, m3, i;
	m1=0;
	m2=0;
	m3=0;
	for(i=0;i<10;i++){
	 scanf("%d",&d);
	 if(d>m1){
	  m3=m2;
	  m2=m1;
	  m1=d;
	 }else if(d>m2){
	  m3=m2;
	  m2=d;
	 }else if(d>m3){
	  m3=d;
	 }
	}
	printf("%d\n%d\n%d\n", m1, m2, m3);
	return 0;
}

Measured memory_bytes: 600
#include<iostream>
#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<time.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<algorithm>
typedef long long ll;
const int N = 1e7;
using namespace std;

int main()
{
 	ll a[200005];
	ll num[200005]; //记录每次的abs(差) 
    int n;
    cin >> n;
    ll sum = 0;//记录sum 
    
    for(int i = 0;i < n;i++)
    {
        cin >> a[i];
        sum += a[i];
    }
     
    ll s = sum - a[0];//表示Snuke开始 
    ll r = a[0];//表示Raccoon开始 
    num[0] = abs(s-r);//记录第一次 
    
    for (int i = 1;i < n-1;i++)
    {
        r += a[i];//每次多拿一张 
        s -= a[i];//每次少拿一张 
        num[i] = abs(s-r);//记录每次 
    }
    sort(num,num+n-1);//找到最小 
    cout<< num[0] << endl;
    return 0;
}


Measured memory_bytes: 3456
#include<bits/stdc++.h>
using namespace std;
int main() {

	std::ios::sync_with_stdio(0);
	std::cin.tie(NULL);
	std::cout.tie(0);

	string s;
	cin >> s;
	for (int i = 0; i < s.size(); ++i) {
		if (s[i] != 'D' || s[i] != 'U') {
			if (s[i] == 'R' && ((i + 1) % 2 == 0)) return cout << "No" << '\n', 0;
			if (s[i] == 'L' && ((i + 1) % 2 == 1)) return cout << "No" << '\n', 0;
		}
	}
	cout << "Yes" << '\n';
	return 0;
}

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

int main()
{
    vector<int>mon;
    int num;
    cin>>num;
    int tmp;
    for(int i=0;i<num;i++)
    {
        cin>>tmp;
        mon.push_back(tmp);
    }
    sort(mon.begin(),mon.end());
    int sum=0;
    for(int i=0;i<num-1;i++)
    {
        sum += mon[i];
    }
    sum += mon[num-1]/2;
    cout<<sum<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(){

	int L;

	scanf("%d",&L);

	int L_dummy=L;
	int N=1;
	int M=0;

	while(L_dummy!=1){

		if(L_dummy%2==0){
			M+=2;
			N++;
			L_dummy/=2;
		}
		else{
			M++;
			L_dummy--;
		}
	}

	printf("%d %d\n",N,M);

	int i=1;

	while(L!=1){
		if(L%2==0){
			printf("%d %d %d\n",i,i+1,0);
			printf("%d %d %d\n",i,i+1,L/2);
			i++;
			L/=2;
		}
		else{
			L--;
			printf("%d %d %d\n",i,N,L);
		}
	}

	return 0;
}

Measured memory_bytes: 128
s = input()

arr = []
amari = [0 for i in range(2019)]
amari[0] +=1
ans = 0
cnt = 0
keta = 1
for i in range(len(s)):
    cnt = (cnt + int(s[-(i+1)]) * keta) % 2019
    arr.append(cnt)
    keta =keta * 10 % 2019

for i in range(len(arr)):
    amari[arr[i]] +=1    
    
for i in amari:
    ans = ans + i * (i-1) // 2
print(ans)

Measured memory_bytes: 76344
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<queue>
using namespace std;
int cnt[1001];
int n,m;
int a,b;
int main()
{
cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>a>>b;
		cnt[a]++;;
		cnt[b]++;
	}
	for(int i=1;i<=n;i++)
	{
		cout<<cnt[i]<<endl;
	}
	return 0;
 }


Measured memory_bytes: 256
//love yjl forever
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const ll INF=4611686018427387904,N=5000+5;
int n;
struct node{
    int x,y;
}a[N];
bool cmp(node x,node y)
{
    return x.x+x.y<y.x+y.y;
}
ll dp[N][N];
int main()
{
/*  freopen("","r",stdin);
    freopen("","w",stdout);*/
    int i,j;
    cin>>n;
    for(i=1;i<=n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
    }
    sort(a+1,a+n+1,cmp);

    for(i=0;i<=n;i++)
        for(j=0;j<=n;j++)
            dp[i][j]=INF;

    dp[0][0]=0;
    for(i=1;i<=n;i++)
        for(j=1;j<=i;j++)
        {
            dp[i][j]=dp[i-1][j];
            if(a[i].x>=dp[i-1][j-1])
                dp[i][j]=min(dp[i][j],dp[i-1][j-1]+a[i].y);
        }

    for(i=n;i>=0;i--)
        if(dp[n][i]<INF)
        {
            cout<<i;
            return 0;
        }
}

Measured memory_bytes: 195840
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {
  public static void main(String[] args)throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int m;
		
		LinkedList list = new LinkedList();
		
		for(int i = 0; i < n; i++){
			String[] meirei = br.readLine().split(" ");
			
			if(meirei[0].equals("deleteFirst")){
				list.removeFirst();
			}else if(meirei[0].equals("deleteLast")){
				list.removeLast();
			}else if(meirei[0].equals("insert") || meirei[0].equals("delete")){
				m = Integer.parseInt(meirei[1]);
				if(meirei[0].equals("insert")){
					list.insert(m);
				}else{
					list.remove(m);
				}
			}
		}
		System.out.println(list.toString());
	}
}

class Room{
	Room prev; //????????¨?±????????????????
	Room next; //?¬??????¨?±????????????????
	int data; //??¨?±?????????????
	
	public Room(int data){
		this.data = data;
		prev = null;
		next = null;
	}

}

class LinkedList {
	Room head;
	Room leg;
	
	public LinkedList(){
		head = new Room(0);
		head.next = head;
		head.prev = head;
		leg = head;
	}
	
	//??¨?±?x?????´??????data????????\
	void insert(int data){
		Room x = new Room(data);
		x.prev = head;
		x.next = head.next;
		head.next.prev = x;
		head.next = x;
		leg = head.prev;
	}
	
	public void removeFirst(){
		Room x = head.next;
		x.next.prev = x.prev;
		x.prev.next = x.next;
		leg = head.prev;
	}
	
	public void removeLast(){
		leg.next.prev = leg.prev;
		leg.prev.next = leg.next;
		leg = head.prev;
	}
	
	public void remove(int num){
		Room p = head.next;
		while(p != head){
			if(p.data == num){
				p.prev.next = p.next;
				p.next.prev = p.prev;
				break;
			}
			p = p.next;
		}
		leg = head.prev;
	}

	public String toString(){
		StringBuilder st = new StringBuilder();
		for(Room p=head.next; p != head; p = p.next){
			if(p.next != head){
				st.append(p.data + " ");
			}else{
				st.append(p.data);
			}
		}
		return st.toString();
	}
} 

Measured memory_bytes: 189172
import itertools
N,M,X=map(int,input().split())
CA=[list(map(int,input().split())) for _ in range(N)]
ll=list(itertools.product(range(2),repeat=N))
ans=10**10
for l in ll:
    t=0
    a=[0]*M
    for i in range(N):
        if l[i]==1:
            t+=CA[i][0]
            for j in range(M):
                a[j]+=CA[i][j+1]
    for i in range(M):
        if a[i]<X:
            break
    else:
        ans=min(ans,t)
if ans==10**10:
    print(-1)
else:
    print(ans)

Measured memory_bytes: 9648
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static int MOD = 1000000007;
	static int[] x4 = { -1, -1, 1, 1 };
	static int[] y4 = { -1, 1, -1, 1 };

	static class TaskX {
		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int x = in.nextInt(), y = in.nextInt();

			long ans  = Long.MAX_VALUE;

			if (-x <= y) {
				ans = Math.min(ans, y+x+1);
			}
			if (-x <= -y) {
				ans = Math.min(ans, -y+x+2);
			}
			if (x <= -y) {
				ans = Math.min(ans, -y-x+1);
			}
			if (x <= y) {
				ans = Math.min(ans, y-x);
			}

			out.println(ans);

		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}

	}

}


Measured memory_bytes: 19412
n,m = map(int, input().split())
start = set()
goal = set()
for i in range(m):
    a,b = map(int, input().split())
    if a == 1:
        start.add(b)
    elif  b == n:
        goal.add(a)

ans = start & goal
if len(ans) > 0:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

Measured memory_bytes: 19024
#define first f
#define second s
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
	int n;
	cin >> n;
	int ans = 0;
	vector <pair<int,int>> h(n);
	for (int i = 0; i < n; i++) {
		cin >> h[i].f;
		h[i].s = i + 1;
	}
	sort(h.begin(), h.end());
	for (int i = 0; i < n; i++) {
		cout << h[i].s<<' ';
	}
}

Measured memory_bytes: 1536
#include <iostream>
#include <algorithm>
#include <math.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
	int d,e;
	while(cin>>d>>e and d>0){
	 	double ans=1e9;
		rep(x,d+1){
			int y=d-x;
			double tmp=abs(sqrt(x*x+y*y)-e);
			if(tmp<ans) ans=tmp;
		}
		printf("%10f\n",ans);
	}
	return 0;
}



Measured memory_bytes: 3220
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 100;
const int MAX_K = 1E+05;

int N, K;

ll A[MAX_N + 1], dp[MAX_N + 1][MAX_K + 1];
ll cam[MAX_K + 1];

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

    cin >> N >> K;

    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }

    for (int i = 0; i <= N; i++) {
        dp[i][0] = 1;
    }
    for (int i = 1; i <= N; i++) {
        cam[0] = dp[i - 1][0];
        for (int j = 1; j <= K; j++) {
            cam[j] = (cam[j - 1] + dp[i - 1][j]) % MOD;
        }
        for (int j = 1; j <= K; j++) {
            dp[i][j] = cam[j];
            if (j > A[i]) {
                dp[i][j] = (dp[i][j] - cam[j - A[i] - 1] + MOD) % MOD;
            }
        }
    }

    cout << dp[N][K] << "\n";

    return 0;
}

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

struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};

int main(){
    long long N ,M; cin >> N >> M;
    vector<int> A(M),B(M);
    for( int i = 0; i < M; i++ ){
        cin >> A[i] >> B[i];
        A[i]--; B[i]--;
    }


    UnionFind Uni(N);


    vector<long long> ans(M);

    ans[M-1] = (N*(N-1))/2;
    for( int i = M - 1; i > 0; i-- ){
        if( Uni.same(A[i],B[i]) ){
            ans[i-1] = ans[i];
        } else {
            long long N1 = Uni.size(A[i]), N2 = Uni.size(B[i]);
            ans[i-1] = ans[i] - N1 * N2;
            Uni.unite( A[i], B[i] );
        }
    }

    for( int i = 0; i < M; i++ ){
        cout << ans[i] << endl;
    }
}

Measured memory_bytes: 3328
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class AizuRangeSumQuery {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int q = in.nextInt();

            int[] a = new int[n];
            int[] b = new int[4 * n];

            build(0, 0, n - 1, a, b);

            while (q-- != 0) {
                int o = in.nextInt();
                int l = in.nextInt() - 1;
                int r = in.nextInt() - 1;

                if (o == 0) {
                    update(0, 0, n - 1, l, r + 1, a, b);
                } else {
                    out.printf("%d\n", query(0, 0, n - 1, l, r, b));
                }
            }
        }

        private void build(int n, int s, int e, int[] a, int[] b) {
            if (s == e) {
                b[n] = a[s];
            } else {
                int m = (s + e) / 2;
                build(2 * n + 1, s, m, a, b);
                build(2 * n + 2, m + 1, e, a, b);

                b[n] = b[2 * n + 1] + b[2 * n + 2];
            }
        }

        private void update(int n, int s, int e, int idx, int val, int[] a, int[] b) {
            if (s == e) {
                a[idx] += val;
                b[n] += val;
            } else {
                int m = (s + e) / 2;
                if (s <= idx && idx <= m) {
                    update(2 * n + 1, s, m, idx, val, a, b);
                } else {
                    update(2 * n + 2, m + 1, e, idx, val, a, b);
                }

                b[n] = b[2 * n + 1] + b[2 * n + 2];
            }
        }

        private long query(int n, int s, int e, int l, int r, int[] b) {
            if (r < s || e < l) {
                return 0;
            } else if (l <= s && e <= r) {
                return b[n];
            } else {
                int m = (s + e) / 2;
                long s1 = query(2 * n + 1, s, m, l, r, b);
                long s2 = query(2 * n + 2, m + 1, e, l, r, b);

                return s1 + s2;
            }
        }

    }
}




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

using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;

constexpr int MAXC = 200000, MOD = 1000000007;

int dp[MAXC + 2];

int main() {
	int N;
	cin >> N;
	int prev = 1;
	for(int i = 1, prevc = -1; i <= N; i++) {
		int C;
		cin >> C;
		if(C != prevc) {
			prev = (dp[C] + prev) % MOD;
			dp[prevc = C] = prev;
		}
	}
	cout << prev << endl;
}


Measured memory_bytes: 1152
#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

int main()
{
	string S;
	cin >> S;
	for (int i = 0; i < S.size() - 1; i++) {
		if (S[i] == S[i + 1]) {
			cout << i + 1 << " " << i + 2 << endl;
			return 0;
		}
	}
	for (int i = 0; i < S.size() - 2; i++) {
		if (S[i] == S[i + 2]) {
			cout << i + 1 << " " << i + 3 << endl;
			return 0;
		}
	}
	cout << -1 << " " << -1 << endl;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  bool t=true;
  cin >> s;
  for(int i=0;i<s.size();i++){
    if(i%2==0 && s[i]=='h' && s[i+1]=='i')continue;
    else if(i%2==1)continue;
    else{
      t=false;
      cout << "No" << endl;
      break;
    }
  }
  if(t)cout << "Yes" << endl;
}

Measured memory_bytes: 3624
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC171_B
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array = Console.ReadLine().Split(' ');
            int N = int.Parse(array[0]);
            int K = int.Parse(array[1]);
            List<int> price_list = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
            price_list.Sort();

            int sum = price_list.Where((value, index) => index < K).Sum();

            Console.WriteLine(sum);
        }
    }
}


Measured memory_bytes: 28676
# -*- coding: utf-8 -*-
"""
Created on Wed Mar  4 19:06:03 2020

@author: Kanaru Sato
"""
import math

X = int(input())

target = X
flag = 0

if X == 2 or X == 3:
    flag = 1

while flag == 0:
    rootX = int(math.sqrt(X))
    for i in range(2, rootX+1):
        if X%i == 0:
            if X%2 == 0:
                X += 1
                break
            else:
                X += 2
                break
        elif i == rootX:
            flag = 1
            break
        else:
            continue

print(X)

Measured memory_bytes: 3064
N = int(input())
S = list(map(int,input().split(" ")))
S.sort()

while True:
  if len(S)>1:
    num1,num2 =S.pop(0),S.pop(0)
    value=(num1+num2)/2
    S.insert(0,value)
  else:
    print(S.pop())
    break

Measured memory_bytes: 3060
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define put(a) cout<<a<<"\n"
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
typedef long long ll;
typedef pair<int, int> P;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){if(a>b){a=b;return 1;}return 0;}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){if(a<b){a=b;return 1;}return 0;}

int main(){
    int n;
    cin >> n;
    vector<ll> a(n);
    REP(i,n){
        cin>>a[i];
    }
    ll res = LLONG_MAX;
    ll sum = 0;
    ll temp = 0;
    REP(i,n){
        sum += a[i];
        if(i%2==0){
            if(sum<=0){
                temp += 1-sum;
                sum = 1;
            }
        }else{
            if(sum>=0){
                temp += sum+1;
                sum = -1;
            }
        }
    }
    chmin(res,temp);
    sum = temp = 0;
    REP(i,n){
        sum += a[i];
        if(i%2==1){
            if(sum<=0){
                temp += 1-sum;
                sum = 1;
            }
        }else{
            if(sum>=0){
                temp += sum+1;
                sum = -1;
            }
        }
    }
    chmin(res,temp);
    put(res);
    
    return 0;
}


Measured memory_bytes: 1024
import java.util.Scanner;

public class Main {
    public static final int mod = 1_000_000_007;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] strings = scanner.nextLine().split(" ");
        int h = Integer.parseInt(strings[0]);
        int w = Integer.parseInt(strings[1]);
        long[] prev = new long[w + 1];
        long[] curr = new long[w + 1];
        prev[1] = 1;
        for (int i = 0; i < h; i++) {
            String row = scanner.nextLine();
            for (int j = 0; j < w; j++) {
                curr[j + 1] = row.charAt(j) == '.' ? (curr[j] + prev[j + 1]) % mod : 0;
            }
            long[] tmp = prev;
            prev = curr;
            curr = tmp;
        }
        System.out.println(prev[w]);
    }
}


Measured memory_bytes: 44456