akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
const int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
typedef long long ll;
const int N=200005;
const int mod=1e9+7;
int n,m,a[N],b[N];
ll sk[N],sb[N];
using namespace std;

int calc(int a,int b)
{
	int ans=1,tmp=a;
	while (b)
	{
		if (b&1) ans=(ll)ans*tmp%mod;
		tmp=(ll)tmp*tmp%mod,b>>=1;
	}
	return ans;
}
inline int read()
{
	char c;int x;
	while (c=getchar(),c<'0' || c>'9');x=c^'0';
	while (c=getchar(),c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x;
}
void write(int x)
{
	if (x==0) return;
	write(x/10);
	putchar((x%10)+'0');
}
void add(int l,int r,int k,int b)
{
	b-=k*l;
	sk[l]+=k,sk[r+1]-=k;
	sb[l]+=b,sb[r+1]-=b;
}
void solve1(int x,int y)
{
	int cost=y-x;
	add(x+1,y,-1,cost);
	add(1,x,0,cost),add(y+1,m,0,cost);
}
void solve2(int x,int y)
{
	int cost=y+m-x;
	add(x+1,m,-1,cost),add(1,y,-1,y);
	add(y+1,x,0,cost);
}
int main()
{
	#ifdef Kay
		freopen ("code.in","r",stdin);
		freopen ("code.out","w",stdout);
	#endif
	scanf ("%d %d",&n,&m);
	int i;
	for (i=1;i<=n;i++) a[i]=read();
	for (i=1;i<n;i++)
	{
		if (a[i]<a[i+1]) solve1(a[i],a[i+1]);
		if (a[i]>a[i+1]) solve2(a[i],a[i+1]);
	}
	ll tmp=1ll<<60;
	for (i=1;i<=m;i++)
	{
		sk[i]+=sk[i-1],sb[i]+=sb[i-1];
		tmp=min(tmp,sk[i]*i+sb[i]);
	}
	printf ("%lld",tmp);
	return 0;
}


Measured memory_bytes: 3712
#include <bits/stdc++.h>
#define MOD 1000000007

using namespace std;

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

int main()
{
    int n;
    cin>>n;
    ll t;
    cin>>t;
    ll ar[n];
    for(int ctr1 = 0; ctr1 < n; ctr1++)
        cin>>ar[ctr1];
    ll first = -1, last = -1;
    ll rez = 0;
    for(int ctr1=0;ctr1<n;ctr1++) {
        if(first == -1) {
            first = ar[ctr1];
            last = ar[ctr1] + t;
        }
        else if(last < ar[ctr1]) {
            rez += last - first;
            first = ar[ctr1];
            last = ar[ctr1] + t;
        }
        else {
            last = ar[ctr1] + t;
        }
    }
    rez += last - first;
    cout<<rez;
    return 0;
}


Measured memory_bytes: 1792
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
al = list(map(int, input().split()))
MOD = 10**9+7

d = {}
for a in al:
    if a not in d:
        d[a] = 0
    d[a] += 1

res = 0
for i in range(n-1):
    for j in range(i+1,n):
        if al[i]>al[j]:
            res += 1
            res %= MOD
res = res*k
res %= MOD

for i in range(n):
    r = 0
    for key in d:
        if al[i]>key:
            r += d[key]
    res += r*k*(k-1)//2
    res %= MOD

print(res)


Measured memory_bytes: 3188
n = gets.chomp.to_i
a = gets.chomp.split.map(&:to_i)

f = []
n.times do |i|
  if i == 0
    f.push(a[i])
  else
    f.push(f[i-1].gcd(a[i]))
  end
end

e = []
b = a.reverse
n.times do |i|
  if i == 0
    e.push(b[i])
  else
    e.push(e[i-1].gcd(b[i]))
  end
end

gcds = []
n.times do |i|
  if i == 0
    gcds.push(e[n-2])
  elsif i == n - 1
    gcds.push(f[n-2])
  else
    gcds.push(f[i-1].gcd(e[n-i-2]))
  end
end
p gcds.max


Measured memory_bytes: 12940
def q4():
    N = int(input())
    D = []
    for i in range(N):
        l = [int(i) for i in input().split()]
        D.append({'A': l[0], 'B': l[1]})
    D.sort(key=lambda x: x['A'])
    D.sort(key=lambda x: x['B'])

    time = 0
    ans = 'Yes'
    for d in D:
        time += d['A']
        if time > d['B']:
            ans = 'No'
            break
    print(ans)


if __name__ == '__main__':
    q4()

Measured memory_bytes: 123224
using System;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;
        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }
            var res = line[index];
            index++;
            return res;
        }
        public int NextInt()
        {
            return int.Parse(Next());
        }
        public long NextLong()
        {
            return long.Parse(Next());
        }
        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }
        public int[] IntArray()
        {
            var array = Array();
            var result = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = int.Parse(array[i]);
            }

            return result;
        }
        public long[] LongArray()
        {
            var array = Array();
            var result = new long[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = long.Parse(array[i]);
            }

            return result;
        }
    }

    class Program
    {
        private int a, b;
        private void Scan()
        {
            Scanner sc = new Scanner();
            a = sc.NextInt();
            b = sc.NextInt();

        }
        public void Solve()
        {
            Scan();
            Console.WriteLine(a * b / 2);
        }

        static void Main(string[] args) => new Program().Solve();
    }
}


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

class Program {
    static void Main(string[] args) {
        //入力
        int N = int.Parse(Console.ReadLine());
        //string s = Console.ReadLine();
        //int[] H = new int[N];
        //for (int i = 0; i < N; i++) {
        //    H[i] = int.Parse(s.Split(' ')[i]);
        //}

        int[] H = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray(); ;


        //回答用フラグ
        bool flg = true;

        //検証
        for (int i = 0; i < N; i++) {
            //先頭の数字は問答無用で1つ減らす
            if (i == 0) {
                H[i] -= 1;
            } else if (H[i] - H[i - 1] <= -1) {
                flg = false;
                //ループを抜ける
                break;
            } else if (H[i] - H[i - 1] == 0) {
                //何もしない
            } else if (H[i] - H[i - 1] >= 1) {
                H[i] = H[i] - 1;
            }
        }
        //出力
        Console.WriteLine(flg? "Yes" : "No");
    }
}

Measured memory_bytes: 26204
#import sys
#inf = sys.maxsize

n = str(input())
#l = len(str(n))

'''
a = []
for i in range(l):
    if m[i] <= 5:
        a.append(0)
    elif m[i] > 5:
        a.append(1)

for i in range(l):
    if i < l-1 and a[i+1] == 1 and m[i] == 5:
        a[i] = 1

p = []
for i in range(l):
    if a[i] == 0:
        if i < l-1 and a[i+1] == 1:
            p.append(m[i]+1)
        else:
            p.append(m[i])
    elif a[i] == 1:
        p.append(0)

pp = ''
for i in range(l):
    pp += str(p[i])
if pp[0] == '0':
    pp = '1' + pp
pp = int(pp)
oo = pp - n
print(pp)
print(oo)
ppp = [int(str(pp)[i]) for i in range(len(str(pp)))]
ooo = [int(str(oo)[i]) for i in range(len(str(oo)))]

print(sum(ppp) + sum(ooo))
'''
dp = [0, 1] #dp[0]: ちょうど dp[1]: 1多い

for i in n:
    i = int(i)
    dp0 = dp[0]
    dp1 = dp[1]
    dp[0] = min(dp0 + i, dp1 + (10 - i))
    dp[1] = min(dp0 + i + 1, dp1 + (10 - i) - 1)
print(dp[0])


Measured memory_bytes: 5492
import sys
import heapq
from collections import deque

input = sys.stdin.readline

N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

A = [-i for i in A]
B = [-i for i in B]
C = [-i for i in C]
BD = [i for i in B]


heapq.heapify(A)
heapq.heapify(B)
heapq.heapify(C)
heapq.heapify(BD)

ai = [0]*N
bi = [0]*N
ci = [1]*N


for i in range(N):
    b=-heapq.heappop(B)
    cnt=False
    while not cnt:
        if len(C)==0:
            cnt=True
            break
        else:
            c=-heapq.heappop(C)
            if c>b:
                bi[i]+=1
            else:
                cnt=True
                heapq.heappush(C, -c)

for i in range(1,N):
    bi[i]+=bi[i-1]

bi = deque(bi)   
    
for i in range(N):
    a=-heapq.heappop(A)
    while len(BD)>0:
        b=-heapq.heappop(BD)
        if b>a:
            ai[i]+=bi.popleft()
        else:
            heapq.heappush(BD, -b)
            break
        
         
for i in range(1,N):
    ai[i]+=ai[i-1]
    
print(sum(ai))


Measured memory_bytes: 22976
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define per(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define INF 2e9
#define LINF 1e18
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int cnt = 0;
    rep(i, n) {
        while (a[i]%2 == 0) {
            cnt++;
            a[i] /= 2;
        }
    }
    cout << cnt << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef set<int> si;
typedef set<ll> sl;
typedef pair<int,int> pii;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define per(i,a,b) for(int i=(b)-1; i>=(a); --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define S second

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	vl cnt(n+1);
	rep(i,0,m) {
		int a, b;
		cin >> a >> b;
		cnt[a]++;
		cnt[b]++;
	}
	rep(i,0,n+1) {
		if(cnt[i]%2) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}


Measured memory_bytes: 1024
#include<iostream>
#include<string>
using namespace std;
int main(){
	int n;
	string s;
	cin >> n >> s;
	for (int i = 0;i < s.size();i++) {
		 int x = s[i] - 'A';
		 x = (x + n) % 26;
		 s[i] = 'A' + x;
		 cout << s[i];
		
	}
	cout << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define chmin(a,b) if((a)>(b)) (a)=(b);
#define chmax(a,b) if((a)<(b)) (a)=(b);
#define vi vector<int>
#define pii pair<int,int>
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define pb push_back
#define pf push_front
int gcd(int a,int b){/*a>=0,b>=0,￢(a=b=0)*/
  while(min(a,b)>0){if(a<b)swap(a,b);a%=b;}return max(a,b);
}
int dx[]={1,0,-1,0,1,-1,-1,1};
int dy[]={0,1,0,-1,1,1,-1,-1};
const int MOD = 1e9+7;
const long long INF = 1e18+10;

/*--------------------------------------------------------------------*/


signed main(){
	string s,t;
	cin>>s>>t;
	cout<<t<<s<<endl;



	return 0;
}



Measured memory_bytes: 3636
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main{
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int r, c, sum, sumOfAll;
		int[][] sheet;
		String[] size = in.readLine().split(" ");
		r = Integer.parseInt(size[0]);
		c = Integer.parseInt(size[1]);
		StringBuilder sb = new StringBuilder((r + 1) * (c + 1));
		sheet = new int[r][c];
		for (int i = 0; i < r; i += 1) {
			sum = 0;
			String[] array = in.readLine().split(" ");
			for (int j = 0; j < c; j += 1) {
				sheet[i][j] = Integer.parseInt(array[j]);
				sb.append(sheet[i][j]).append(" ");
				sum += sheet[i][j];
			}
			sb.append(sum + System.getProperty("line.separator"));
		}
		in = null;
		sumOfAll = 0;
		for (int m = 0; m < c; m += 1) {
			sum = 0;
			for (int n = 0; n < r; n += 1) {
				sum += sheet[n][m];
			}
			sumOfAll += sum;
			sb.append(sum).append(" ");
		}
		sb.append(sumOfAll);
		System.out.println(sb);
	}
}

Measured memory_bytes: 21352
#include<stdio.h>

int main(){
    int N; //N種類2 <= N <= 20
    int a[20];
    int b[50];
    int c[50];
    int i = 1;

    int Satisfied = 0;

    scanf("%d", &N);
    
    a[0] = -1;
    b[0] = -1;
    c[0] = -1;

    //input
    for(i = 1; i <= N; i++){
        scanf("%d", &a[i]);
    }

    for(i = 1; i <= N; i++ ){
        scanf("%d", &b[i]);
    }

    for(i = 1; i < N; i++ ){
        scanf("%d", &c[i]);
    }

    for(i = 1; i <= N; i++){
        Satisfied += b[a[i]];
        if(a[i] == a[i - 1] + 1){
            Satisfied += c[a[i] - 1];
        }
    }

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

    return 0;

}


Measured memory_bytes: 128
object Main extends App {
  val in = io.StdIn.readLine().split(" ").map(_.toInt)
  val A = in(0)
  val B = in(1)
  if (A % 3 == 0 || B % 3 == 0 || (A + B) % 3 == 0)
    println("Possible")
  else
    println("Impossible")

}

Measured memory_bytes: 25412
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <tuple>
#include <cstring>
#include <map>
#include <iomanip>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tlll tuple<ll, ll, ll>

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

    int b[100010] = {};
    int a = 0, w = 0;
    for (int i = 0; i < m; i++) {
        int p;
        string s;
        cin >> p >> s;

        if (s == "AC") {
            if (b[p] != -1) {
                a++;  w += b[p];
                b[p] = -1;
            }
        }
        else {
            if (b[p] != -1) b[p]++;
        }

    }
    cout << a _ w << endl;

}


Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed
#define endl '\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;

void solve(istream& cin, ostream& cout) {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  int bad = 0;
  char now = s[0];
  REP(i, n) {
    if (i == 0) continue;
    if (now != s[i]) bad++;
    now = s[i];
  }
  int ans = n - 1 - bad;
  if (2 * k <= bad) ans += 2 * k;
  else
    ans = n - 1;
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
}
#endif


Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

typedef long long ll;
#define mfor(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,n)  mfor(i,0,n)
#define all(x) (x).begin(),(x).end()

bool comp(const vector<int> &x, const vector<int> &y){
    return x[2] > y[2];
}

typedef pair<ll, ll> P;

bool pairComp(const P& x, const P& y) {
	return x.first < y.first;
}


int main(void){

	string s;
	cin >> s;

	int red = 0;
	int blue = 0;
	rep(i, s.size()) {
		if (s[i] == '0') {
			red++;
		}else {
			blue++;
		}
	}
	int ans = min(red, blue) * 2;

	cout << ans <<endl;

  return 0;
}


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

int main(){
    string s;
    cin >> s;
    int n = s.length();
    vector<vector<ll>> dp(n+1, vector<ll>(2, 0));
    dp[0][0] = 1;
    rep(i, n) {
        if ( s[i] == '0' ) {
            dp[i+1][0] = ( dp[i+1][0] + dp[i][0] ) % mod;
            dp[i+1][1] = ( dp[i+1][1] + dp[i][1] ) % mod; 
        }
        else {
            dp[i+1][1] = ( dp[i+1][1] + ( dp[i][0] + dp[i][1] ) % mod ) % mod;
        }

        if ( s[i] == '0') {
            dp[i+1][1] = ( dp[i+1][1] + dp[i][1] * 2 % mod ) % mod;
        }
        else {
            dp[i+1][0] = ( dp[i+1][0] + dp[i][0] * 2 % mod ) % mod;
            dp[i+1][1] = ( dp[i+1][1] + dp[i][1] * 2 % mod ) % mod;
        }
    }
    ll ans = ( dp[n][0] + dp[n][1] ) % mod;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 5760
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define LINT long long
const LINT MOD = 1000000007; //nより大きい素数であってくれ

class COMB{
private:
  LINT n;
  vector<LINT> fac;
  vector<LINT> finv;
  vector<LINT> inv;

public:
  COMB(LINT N)
  : fac(vector<LINT>(N+1, 1)), finv(vector<LINT>(N+1, 1)),
    inv(vector<LINT>(N+1, 1)), n(N)
  {
    COMB::init();
  }

  // テーブルを作る前処理
  void init() {
    for (int i = 2; i < n+1; i++){
        COMB::fac[i] = COMB::fac[i - 1] * i % MOD;
        COMB::inv[i] = MOD - COMB::inv[MOD%i] * (MOD / i) % MOD;
        COMB::finv[i] = COMB::finv[i - 1] * COMB::inv[i] % MOD;
    }
  }

  // 二項係数計算
  //1<= k <= n <=10^7くらいまで
  LINT com(int n, int k){
      if (n < k) return 0;
      if (n < 0 || k < 0) return 0;
      return COMB::fac[n] * (COMB::finv[k] * COMB::finv[n - k] % MOD) % MOD;
  }
};

int main(){
  LINT H, W, A, B;
  cin>>H>>W>>A>>B;

  COMB cm(200000);
  LINT ans=0;
  for(LINT i=0; i<H-A; ++i){
    ans += cm.com(i+B-1, i) * cm.com(H-1-i+W-B-1, H-1-i);
    ans %= MOD;
  }
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 4992
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define int long long
using namespace std;
typedef vector<int>   vint;
typedef pair<int,int> pint;

signed main()
{   
    int N; cin>>N;
    map<int,int> cnt;
    REP(i,N){
        int a; cin>>a;
        cnt[a]++;
    }
    int ans=0;
    REP(i,100000){
        ans=max(ans,cnt[i-1]+cnt[i]+cnt[i+1]);
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 6528
#include<bits/stdc++.h>
        #define int long long
        using namespace std;
        typedef pair<int,int> P;
         
        signed main(){
          vector<int> v;
          int t=3;
          while(v.size()<1000){
            bool f=1;
            for(int i=2;i*i<=t;i++)if(t%i==0)f=0;
            if(f)v.push_back(t);
            t++;
          }
          int d1[500][500],d2[500][500],c=0;
          for(int i=-498;i<500;i+=2){
            for(int j=-i;j+i<500;j++){
              if(0<=j&&j<500&&0<=j+i&&j+i<500)d1[j][j+i]=v[c];
            }
            c++;
          }
         
          for(int i=0;i<1000;i+=2){
            for(int j=1000;i-j<500;j--){
              if(0<=j&&j<500&&0<=i-j&&i-j<500)d2[j][i-j]=v[c];
            }
            c++;
          }
         
          int n;
          cin>>n;
          
          int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
          for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
              if(j)cout<<" ";
              if((i+j)%2){
        	int res=1;
        	set<int> s;
        	for(int k=0;k<4;k++){
        	  int nx=j+dx[k],ny=i+dy[k];
        	  if(nx<0||ny<0||nx>=500||ny>=500)continue;
        	  s.insert(d1[ny][nx]);
        	  s.insert(d2[ny][nx]);
        	}
        	for(auto z:s){res*=z;}
        	cout<<res+1;
              }
              else cout<<d1[i][j]*d2[i][j];
            }
            cout<<endl;
          }
          return 0;
        }

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

int main(){
    int k;
    cin >> k;
    ll sev = 7;
    for(int i = 1; i <= k; i++){
        if(sev % k == 0){
            cout << i << endl;
            return 0;
        }
        sev = (10 * sev + 7) % k;
    }
    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
#define INF 1000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

bool dbgflag = false;

int bfs(int i, int j, vector<string>&field) {
  int h = field.size(), w = field[0].size();
  vector<string> copyfield(h);
  for (int i = 0; i < h; i++) copyfield[i] = field[i];
  vector<vector<int>> dist(h, vector<int>(w));

  queue<pii> que;
  que.push(pii(i, j));
  field[i][j] = '#';
  dist[i][j] = 0;
  vector<int> dx = {-1, 0, 0, 1}, dy = {0, 1, -1, 0};

  while (!que.empty()) {
    pii p = que.front();
    int px = p.second, py = p.first;
    que.pop();
    for (int k = 0; k < 4; k++) {
      int x = px + dx[k], y = py + dy[k];
      if ((0 <= x) && (x < w) && (0 <= y) && (y < h) && (field[y][x] == '.')) {
        dist[y][x] = dist[py][px] + 1;
        field[y][x] = '#';
        que.push(pii(y, x));
      }
    }
  }
  int ret = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      ret = max(ret, dist[i][j]);
    }
    field[i] = copyfield[i];
  }

  return ret;
}

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

  int h, w;
  cin >> h >> w;
  vector<string> field(h);
  //vector<vector<int>> dist(h, vector<int>(w));
  for (int i = 0; i < h; i++) cin >> field[i];

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == '.') {
        ans = max(ans, bfs(i, j, field));
      }
    }
  }

  cout << ans << endl;
}

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

using std::vector;
using std::printf;
using std::cout;
using std::endl;
using std::string;
using std::cin;
using std::pair;
using std::sort;

#define i64t long long int
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

template<typename T>
void dump(vector<T> vec) {
    for (auto x : vec) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
}

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

const i64t MOD = 1000000007;

int main() {
    string S;
    cin >> S;

    if (S == "Sunny") {
        printf("Cloudy");
    }
    else if (S == "Cloudy") {
        printf("Rainy");
    }
    else {
        printf("Sunny");
    }
    
}

Measured memory_bytes: 256
#include <time.h>

#include <stdlib.h>
#include <cstdint>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <cstdint>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <bitset>

using namespace std;
const int64_t INF=10000000000000000;

int main(){
	string s;
	cin>>s;
	vector<int> a;
	a.reserve(s.size());
	for(int i1=0;i1<s.size();i1++){
		a.push_back(s[i1]-'a');
	}
	int64_t ans=INF;
	for(int i1=0;i1<=25;i1++){
		if(s.find_first_of((char)(i1+'a'))==string::npos){
			continue;
		}
		vector<bool> *prev=new vector<bool>();
		prev->reserve(s.size());
		for(int i2=0;i2<a.size();i2++){
			if(a[i2]==i1){
				prev->push_back(true);
			}else{
				prev->push_back(false);
			}
		}
		vector<bool> *current;
		int count=0;
		while(true){
			if(prev->size()==0){
				break;
			}
			bool finished=true;
			for(auto i3=prev->begin();i3!=prev->end();i3++){
				if(!(*i3)){
					finished=false;
					break;
				}
			}
			if(finished){

				if(ans>count){
					ans=count;
					break;
				}
			}else{
				count++;
			}
			current=new vector<bool>();
			current->reserve(prev->size()-1);
			for(int i3=0;i3<prev->size()-1;i3++){
				current->push_back(prev->at(i3)|prev->at(i3+1));
			}
			delete prev;
			prev=current;
		}


	}
	cout<<ans<<endl;





}

Measured memory_bytes: 256
#include <stdio.h>

int N, M;

int D[10001];
int P[10001];

void sort(void)
{
    int i, j;

    for(i = 1; i <= N - 1; i++){
        for(j = 2; j <= N; j++){
            if(P[j - 1] < P[j]){
                int temp = P[j - 1];
                P[j - 1] = P[j];
                P[j] = temp;
                temp = D[j - 1];
                D[j - 1] = D[j];
                D[j] = temp;
            }
        }
    }
}


int main(void)
{
    while(1){
        int i, j;
        int exp = 0;

        scanf("%d %d", &N, &M);

        if(N == 0 && M == 0)
            break;

        for(i = 1; i <= N; i++){
            scanf("%d %d", D + i, P + i);
        }

        sort();
        for(i = 1; i <= N; i++){
            if(D[i] <= M){
                M -= D[i];
            }else if(M > 0){
                exp += P[i] * (D[i] - M);
                M = 0;
            }else{
                exp += P[i] * D[i];
            }
        }
        printf("%d\n", exp);
    }
}

Measured memory_bytes: 676
s_list=list(input())

if len(s_list) % 2 == 1:
    index_max = (len(s_list) - 1)//2 - 1
else:
    index_max = len(s_list)//2 - 1

count = 0
for i in range(index_max + 1):
    if s_list[i] != s_list[-(i + 1)]:
        count += 1
print(count)

Measured memory_bytes: 3060
N,Q =map(int,input().strip().split())
#=int(input().strip())
#=list(map(int,input().strip().split()))
S = input()

count = [0]
for i in range(0,N - 1):
    if S[i] == "A" and S[i + 1] == "C":
        count.append(count[i] + 1)
    else:
        count.append(count[i])


for i in range(0,Q):
    l,r = map(int,input().strip().split())
    if r > len(count):
        r = len(count)
    if l == 0:
        l += 1
    print(count[r - 1] - count[l - 1])



Measured memory_bytes: 6100
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if((a+b)>(c+d))cout<<"Left"<<endl;
    if((a+b)==(c+d)) cout<<"Balanced"<<endl;
    if((a+b)<(c+d))cout<<"Right"<<endl;
    return 0;
}


Measured memory_bytes: 256
def solve(a,i,b,c):
	return  b == sorted(b) and c == sorted(c) if i == 10 else solve(a,i+1,b+[a[i]],c) or solve(a,i+1,b,c+[a[i]])
	
for roop in range(int(raw_input())):
	print "YES" if solve(map(int,raw_input().split()),0,[],[]) else "NO"

Measured memory_bytes: 4216
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		int K = Integer.parseInt(sc.next());
		Integer[] l = new Integer[N];
		for (int i = 0; i < N; i++) {
			l[i] = Integer.parseInt(sc.next());
		}

		int result = 0;
        Arrays.sort(l, Comparator.reverseOrder());
		for (int i = 0; i < K; i++) {
			result += l[i];
		}
		System.out.println(result);
	}
}

Measured memory_bytes: 35624
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <ctype.h>
#include <map>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <cctype>
#include <queue>
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define REP1(j,n) for(int j=0;j<(int)n;++j)
#define REPD(i,n) for(int i=n;i>=0;--i)
#define REPD1(j,n) for(int j=n;j=>0;--j)
#define REPS(i,n) for(int i=1;i<=(int)n;++i)
#define REPS1(j,n) for(int j=1;j<=(int)n;++j)
#define rep(i,a,n) for(int i=a;i<=(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define DescSort(a) sort(a.begin(),a.end(),std::greater<int>())

using namespace std;
typedef long long ll;
const ll INF=1e9+7;

int main(void) {
	int a, b;
	cin >> a >> b;
	int eval = abs(a - b);
	if (eval % 2 == 0) {
		cout << max(a,b)-eval/2 << endl;
	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define rep(i,n) for(int i=0;i<n;i++)
int gcd(int a, int b){
    int r=1;
    while(r!=0){
        r=a%b;
        a=b;
        b=r;
    }
    return a;
}
void revStr(char* str){
    int size = strlen(str);
    int i,j;
    char tmp = {0};
    for(i = 0, j = size - 1; i < size / 2; i++, j--){
        tmp = str[i];
        str[i] = str[j];
        str[j] = tmp;
    }return;
}
struct data{
    int a,b;
};

//降順
int compare_int(const void *a, const void *b)
{
    return *(int*)b - *(int*)a;
}
#define MAXSIZE 200000
int list[MAXSIZE];
int tail;

void Init(void){
    tail=0;
}
int Top(void){
    return 0;
}

int Next(int pos){
    if(pos<tail-1){
        return pos+1;
    }else{
        return -1;
    }
}
int Count(void){
    return tail;
}

int Item(int pos){
    if((pos>=0)&&(pos<tail)){
        return list[pos];
    }else{
        return 0;
    }
}
void Add(int value){
    if(tail<MAXSIZE){
        list[tail]=value;
        tail++;
    }
}
void Delete(int pos){
    if((pos>=0)&&(pos<tail)){
        int i;
        for(i=pos;i<tail-1;i++){
            list[i]=list[i+1];
        }
        tail--;
    }
}

int main(){
    long long N,M;
    scanf("%lld%lld",&N,&M);
    long long cnt=0;
    if(N==1 && M==1){
        cnt=1;
    }else if(N==1){
        cnt=M-2;
    }else if(M==1){
        cnt=N-2;
    }else if(M==2 || N==2){
        cnt=0;
    }else{
        cnt=(M-2)*(N-2);
    }
    printf("%lld",cnt);
}


Measured memory_bytes: 128
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Main{

	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<String> array = new ArrayList<String>();

		try {
			String str = null;
			while ((str = br.readLine()) != null) {
				array.add(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String[] data = array.get(0).split(" ");
		int N = Integer.parseInt(data[0]);
		int count = Integer.parseInt(data[1]);
		
		int[] fisher = new int[N + 1];
		
		int maxNumber = 1;
		for (int i = 1; i <= count; i++) {
			data = array.get(i).split(" ");
			int id = Integer.parseInt(data[0]);
			int fishes = Integer.parseInt(data[1]);
		
			fisher[id] += fishes;
			if(maxNumber == id) {
				if(fishes < 0) {
					maxNumber = 1;
					for(int j = 0; j < fisher.length; j++) {
						if(fisher[j] > fisher[maxNumber]) {
							maxNumber = j;
						}
					}
				}
			} else if(fisher[id] > fisher[maxNumber] || (fisher[id] == fisher[maxNumber] && id < maxNumber)){
				maxNumber = id;
			}
			System.out.println(maxNumber + " " + fisher[maxNumber]);
		}

		/*
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

		for (int i = 1; i <= N; i++) {
			map.put(i, 0);
		}
		int maxNumber = 1;
		for (int i = 1; i <= count; i++) {
			data = array.get(i).split(" ");
			int id = Integer.parseInt(data[0]);
			int fishes = Integer.parseInt(data[1]);
			map.put(id, fishes + map.get(id));

			if (maxNumber == id) {
				if (fishes < 0) {
					maxNumber = 1;
					for (int j : map.keySet()) {
						if (map.get(j) > map.get(maxNumber)) {
							maxNumber = j;
						}
					}
				}
			} else if (map.get(id) > map.get(maxNumber)
					|| ((map.get(id) == map.get(maxNumber) && id < maxNumber))) {
				maxNumber = id;
			}
			System.out.println(maxNumber + " " + map.get(maxNumber));
		}*/
	}

}

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

using namespace std;

#define ll long long int

ll n,ara[100005],dp[100005];

ll Abs(ll x)
{
    if(x<0) return -x;

    return x;
}

ll func(ll indx)
{
    if(indx>n)
    {
        return 1e12;
    }

    if(indx==n) return 0;

    if(dp[indx]!=-1) return dp[indx];

    ll ans1=1e13,ans2=1e13,i=indx;

    if(i==n-1)
    {
        ans1=Abs(ara[i+1]-ara[i])+func(indx+1);
    }

    else
    {
        ans1=Abs(ara[i+1]-ara[i])+func(indx+1);

        ans2=Abs(ara[i+2]-ara[i])+func(indx+2);
    }

    return dp[indx]=min(ans1,ans2);
}

int main()
{
    ll i,j;

    scanf("%lld",&n);

    for(i=1; i<=n; i++)
    {
        scanf("%lld",&ara[i]);
    }

    memset(dp, -1, sizeof dp);

    ll ans=func(1);

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

    return 0;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18+7)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x > 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x) {
	if (x == 0)return 1;
	return kai(x - 1) * x % mod;
}
int mod_pow(int x, int y, int mod_) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % mod_;
		}
		x = x * x % mod_;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x)* mod_pow(kai(x - y), mod - 2, mod) % mod * mod_pow(kai(y), mod - 2, mod) % mod;
}
/*--------Library Zone!--------*/


int n;
vector<int>G[10005];
int a[10005], cnt[100005];
int k[100005];
signed main() {
	cin >> n;
	rep(i, n - 1) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b); G[b].push_back(a);
		cnt[a]++; cnt[b]++;
	}
	rep(i, n)cin >> a[i];
	sort(a, a + n);
	int ans = 0;
	rep(_, n) {
		int min_ = inf, memo;
		rep(i, n) {
			if (min_ > cnt[i]) {
				min_ = cnt[i]; memo = i;
			}
		}
		k[memo] = a[_];
		cnt[memo] = inf;
		ans += min_ * a[_];
		rep(i, G[memo].size()) {
			cnt[G[memo][i]]--;
		}
	}
	cout << ans << endl;
	rep(i, n)cout << k[i] << " ";
	cout << endl;
}


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

int main() {
  int n;
  cin >> n;
  string s;
  int arr[4] = {};
  while (n--) {
    cin >> s;
    if (s == "AC") arr[0]++;
    else if (s == "WA") arr[1]++;
    else if (s == "TLE") arr[2]++;
    else arr[3]++;
  }
  cout << "AC x " << arr[0] << '\n';
  cout << "WA x " << arr[1] << '\n';
  cout << "TLE x " << arr[2] << '\n';
  cout << "RE x " << arr[3] << '\n';
  return 0;
}

Measured memory_bytes: 3628
#include<bits/stdc++.h>
using namespace std;
int main(){
  char a[105],b[105];
  char str[205];
  
  scanf("%s",str);
  str[strlen(str)-1]='\0';
  
  int ans=0;
  
  while(1){
    int len=strlen(str);
    int i;
    for(i=0;i<len/2;i++){
      a[i]=str[i];
    }
    a[i]='\0';
    for(i=len/2;i<len;i++){
      b[i-(len/2)]=str[i];
    }
    b[i-(len/2)]='\0';

    if(strcmp(a,b)==0)
      ans=len;
    if(ans)
      break;
    str[len-1]='\0';
  }
  
  cout << ans << endl;
  
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for(int i=0;i<n;i++) cin >> a[i];
  map<int, ll> mp;
  for(int i=0;i<n;i++) mp[a[i]]++;
  ll total = 0;
  for(auto it=mp.begin();it!=mp.end();it++) {
    total += it->second * (it->second - 1) / 2;
  }
  for(int i=0;i<n;i++) {
    int v = mp[a[i]];
    cout << total - (v * (v-1) / 2) + (v-1) * (v - 2) / 2 << endl;
  }
  return 0;
}


Measured memory_bytes: 10240
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <map>
#define REP(i,n) for(ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

int main(){
  ll n,i;
  cin >> n >> i;
  cout << n-i+1 << endl;
  return 0;
}


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

int main(){
int A, B;
  cin>>A>>B;
  if(A==B){cout<<A+B<<endl;}
  else if(A+A-1>B+B-1){cout<<A+A-1<<endl;}
  else{cout<<B+B-1<<endl;}
  
}


Measured memory_bytes: 256
#include <iostream>
#include <cassert>
#include <climits>
#include <bitset>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>

using namespace std;

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

long long int INF = 1e18;
long long int mod = 998244353;
double Pi = 3.1415926535897932384626;

vector<ll> G[500005];
vector<P> tree[500010];
priority_queue <ll> pql;
priority_queue <P> pqp;
//big priority queue
priority_queue <ll,vector<ll>,greater<ll> > pqls;
priority_queue <P,vector<P>,greater<P> > pqps;
//small priority queue
//top pop

int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
char dir[] = "RULD";
ll bit[500005];
//↓,→,↑,←

#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define ps(x) cout<<fixed<<setprecision(15)<<x<<endl;
#define pu(x) cout<<x;
#define re(i,n) for(i=0;i<n;i++);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define deba(x) cout<< #x << " = " << x <<endl

ll rui(ll abc,ll bed){
    //abcのbed乗を計算する
    if(bed==0){return 1;}
    else{
        ll ced = rui(abc,bed/2);
        ced *= ced;
        ced %= mod;
        if(bed%2==1){ced*=abc; ced%=mod;}
        return ced;
    }
}
ll gcd(ll number1,ll number2){
	if(number1 > number2){
		swap(number1,number2);
	}
	if(number1 == 0 || number1 == number2){
		return number2;
	}else{
		return gcd(number2 % number1,number1);
	}
}

ll i,j,k,ii,jj,r,l;
ll n,m,num,ans;
ll a,b,c,d,e,g,h,w;
ll x[500005],y[500005],z[500005];
ll l1[500005],l2[500005];
char s[5005];

ll bitsum(ll i){
	ll s = 0;
	while(i>0){
		//p("hoi");
		s += bit[i];
		i -= i & -i;
	}
	return s;
}
void bitadd(ll b,ll x){
	while(b <= n){
		//p("hoi");
		bit[b] += x;
		b += b & -b;
	}
}

int main(){
	cin >> n >> m;
	for(i = 0;i < m;i++){
		cin >> a >> b;
		x[a] ++;
		y[num] = a * 1000000000 + b;
		l1[num] = a;
		l2[num++] = b;
	}
	sort(y,y+num);
	for(i=1;i<=n + 5;i++){
		x[i] += x[i-1];
	}
	for(i=0;i<m;i++){
		a = l1[i];
		b = l2[i];
		c = lb(y,y+m,a * 1000000000 + b) - y;
		num = 1000000 * a + c - x[a - 1] + 1;
		d = num;
		while(d < 100000000000){
			pu(0);
			d *= 10;
		}
		p(num);
	}



	//p(ans);
	

    return 0;
}

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

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

  string str;
  int res = 0;
  cin >> str;
  REP(i, (int)str.size()) {
    if(str[i] == '1') {
      res++;
    }
  }
  cout << res << endl;
  return 0;
}

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

int main() {
	ll n, k, q; cin >> n >> k >> q;
	vector<ll> a(q); rep(i, 0, q) cin >> a[i];

	vector<ll> ans(n, k);
	rep(i, 0, q) ans[a[i] - 1] += 1;

	for (auto x : ans) {
		if (x <= q) cout << "No" << endl;
		else cout << "Yes" << endl;
	}
	
	return 0;
}

Measured memory_bytes: 2176
#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const long long INF = 1LL << 60;
// const int INF=1010101010;
using Graph = vector<vector<int>>;

int main() {
  int k;
  cin >> k;
  ll total = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= k; j++) {
      for (int t = 1; t <= k; t++) {
        total += __gcd(__gcd(i, j), t);
      }
    }
  }
  cout << total << endl;
}


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

using namespace std;

int main(){
	int n; cin >> n;
	vector<int> v(n);
	for(int i = 0; i < n; ++i) cin >> v[i];
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	cout << v.size() << endl;
}

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

int main(void) {
    int houseList[3][10][4] = {};
    int a, b, f, r, v, i;

    cin >> a;

    for( i = 0; i < a; i++ ) {
        cin >> b >> f >> r >> v;
        houseList[f - 1][r - 1][b - 1] += v;
    }

    int s, t, u;
    for(s = 0; s < 4; s++ ) {
        for(t = 0; t < 3; t++) {
            for(u = 0; u < 10; u++) {
                cout << " " << houseList[t][u][s];
            }
            cout << endl;
        }
        if( s < 3) {
            cout << "####################" << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 1164
#include<cstdio>

const int MAX_N = 200000;
int A[MAX_N];

int N;

void solve();

int main(){

    scanf("%d", &N);
    for(int i=0;i<N;i++){
        scanf("%d", &A[i]);
    }

    solve();

    return 0;
}

void solve(){

    long long ans = 0;

    for(int i=0; i < N - 1;i++){
        if(A[i] > A[i + 1]){
            ans += (A[i] - A[i + 1]);
            A[i + 1] = A[i];
        }
    }

    printf("%ld", ans);
}


Measured memory_bytes: 2508
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int r, c, n, fa[N];
long long wi[N], mn[N];
vector<pair<int, int> > buck[N];
int Find(int x) {
	if (x == fa[x]) return x;
	int rt = Find(fa[x]);
	wi[x] += wi[fa[x]];
	return fa[x] = rt;
}
bool Union(int x, int y, long long z) {
	int rx = Find(x), ry = Find(y);
	z = z - wi[x] + wi[y];
	x = rx, y = ry;
	if (x < y) swap(x, y), z = -z;
	if (x == y) {
		return !z;
	}
	fa[x] = y;
	wi[x] = z;
	return true;
}
int main() {
	scanf("%d%d%d", &r, &c, &n);
	for (int i = 1; i <= n; i++) {
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		buck[x].push_back(make_pair(y, z));
	}
	for (int i = 1; i <= c; i++) fa[i] = i, wi[i] = mn[i] = 0;
	for (int i = 1; i <= r; i++) {
		sort(buck[i].begin(), buck[i].end());
		for (int j = 1; j < (int)buck[i].size(); j++) {
			if (!Union(buck[i][j - 1].first, buck[i][j].first, buck[i][j - 1].second - buck[i][j].second)) {
				puts("No");
				return 0;
			}
		}
	}
	for (int i = 1; i <= c; i++) {
		Find(i);
	}
	for (int i = 1; i <= c; i++) {
		mn[fa[i]] = min(mn[fa[i]], wi[i]);
	}
	for (int i = 1; i <= r; i++) if (buck[i].size()) {
		int p = fa[buck[i][0].first];
		long long w = buck[i][0].second - wi[buck[i][0].first];
		if (w + mn[p] < 0) {
			puts("No");
			return 0;
		}
	}
	puts("Yes");
	return 0;
}

Measured memory_bytes: 11080
#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 19;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<n;i++)
#define Se second
#define Fi first

const int MX = 200005;
const int MM = 1000000007;

int pw2[MX];
vector<int> T[MX], lv[MX];
int P[MX][20], lev[MX];
int vst[MX], t = 0;

void dfs(int x){
	int cur = vst[x];
	for(int i = 1; i < 20; i++) P[cur][i] = P[P[cur][i-1]][i-1];
	for(int c : T[x]){
		vst[c] = ++t;
		P[t][0] = cur;
		lev[t] = lev[cur] + 1;
		dfs(c);
	}
}

void up(int &x, int d){
	if( d < 0 ) return;
	for(int i = 0; i < 20; i++) if( d&1<<i ) x = P[x][i];
}

int LCA(int a, int b){
	up(a, lev[a] - lev[b]);
	up(b, lev[b] - lev[a]);

	for(int i = 19; i >= 0; i--){
		if( P[a][i] != P[b][i] ) a = P[a][i], b = P[b][i];
	}
	return a == b ? a : P[a][0];
}

vector<int> G[MX];

ll pw(ll a, ll b = MM-2){
	ll r = 1;
	while(b){
		if( b&1 ) r = r*a % MM;
		a = a*a % MM; b /= 2;
	}
	return r;
}

pll get_dp(int x){
	if( G[x].size() == 0 ) return pll(1, 1);
	vector<pll> T;
	ll m = 1, total = 1;
	for(int c : G[x]){
		T.push_back(get_dp(c));
		m = m * T.back().first % MM;
		total = total * (T.back().first + T.back().second) % MM;
	}
	pll t = pll(0, 0);
	for(pll c : T){
		t.second = (t.second + m * pw(c.first) % MM * c.second) % MM;
	}
	t.first = (total - t.second + MM) % MM;
	return t;
}

ll solve(vector<int> &L){
	sort(L.begin(), L.end());

	vector<int> X;
	for(int i = 0; i < L.size(); i++){
		X.push_back(L[i]);
		if( i+1 < L.size() ) X.push_back(LCA(L[i], L[i+1]));
	}
	sort(X.begin(), X.end());
	X.erase(unique(X.begin(), X.end()), X.end());
	for(int i = 0; i+1 < X.size(); i++){
		G[LCA(X[i], X[i+1])].push_back(X[i+1]);
	}
	pll t = get_dp(X[0]);
	for(int c : X) G[c].clear();
	return t.second;
}

int main()
{
	pw2[0] = 1;
	for(int i = 1; i < MX; i++) pw2[i] = pw2[i-1] * 2 % MM;
	int N, a;
	scanf("%d", &N);
	for(int i = 1; i <= N; i++){
		scanf("%d", &a);
		T[a].push_back(i);
	}
	dfs(0);

	for(int i = 0; i <= N; i++) lv[lev[i]].push_back(i);

	ll ans = 0;
	for(int i = 0; i <= N; i++){
		if(lv[i].size()) ans = (ans + solve(lv[i]) * pw2[N+1-lv[i].size()]) % MM;
	}
	printf("%lld\n", ans);
}


Measured memory_bytes: 54144
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include<algorithm>
#include<sstream>
#include<iomanip>
#include<deque>
#include<list>


using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

const ll MOD_CONST = 1000000007;
const ll BIG_NUM = 1000000000000000000;
const int BIG_INT = 1000000000;



struct UnionFind {
	vector<int> p;
	vector<int> s;

	UnionFind(int n) :p(n), s(n,1){
		for (int i = 0; i < n;i++) {
			p[i] = i;
			
		}
	}

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

		p[x] = root(p[x]);
		return p[x];
	}

	bool same(int x, int y) {
		return root(p[x]) == root(p[y]);
	}

	void unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);

		if (rx != ry) {
			p[rx] = ry;
			s[ry] += s[rx];
		}
	}

	int size(int x) {
		return s[root(x)];
	}
};



int main() {
	int n, k, l;
	cin >> n >> k >> l;
	UnionFind ro(n);
	UnionFind ra(n);

	for (int i = 0; i < k;i++) {
		int p, q;
		
		cin >> p >> q;
		p--;
		q--;
		ro.unite(p, q);
	}

	for (int i = 0; i < l;i++) {
		int r, s;
		
		cin >> r >> s;
		r--;
		s--;
		ra.unite(r, s);
	}

	map<pii,int> m;

	for (int i = 0;i < n;i++) {
		m[make_pair(ro.root(i), ra.root(i))]++;
	}

	for (int i = 0;i < n;i++) {
		cout << m[make_pair(ro.root(i), ra.root(i))] << " ";
	}
}


Measured memory_bytes: 15104
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define rSort(a) 		sort(a.rbegin(),a.rend())
#define Sort(a) 		sort(a.begin(),a.end())
#define Reverse(a) 		reverse(a.begin(),a.end())
#define Sum(a)  		accumulate(a.begin(), a.end(), 0)
#define REP(i, n) 		for(UI i = 0;i < n;i++)
#define REPR(i, n) 		for(UI i = n;i >= 0;i--)
#define FOR(i, m, n) 	for(UI i = m;i < n;i++)
#define pb(i) 			push_back(i)
#define SWAP(x,y) 		((x!=y)&&(x^=y,y^=x,x^=y))

#define MOD  			(LL)1e9 + 7;
#define INF  			(LL)1 << 62;

int main(int argc, char* argv[]) {

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

	LL N;
	LL ans=0;

	cin >> N;

	vector<LL> A(N);
	vector<LL> B(N);
	vector<LL> C(N-1);

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

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

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

	for(int i=0;i<N;i++){
		ans+=B[A[i]-1];
	}

	for(int i=0;i<N-1;i++){
		if(A[i] + 1 == A[i+1]){
			ans+=C[A[i]-1];
		}
	}

	cout << ans << endl;

	return 0;
}


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

int main(){
  int n;
  cin >> n;
  vector<int> v;
  int i = 0, j;
  while(i < n){
    cin >> j;
    v.push_back(j);
    i++;
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  int ans = v.size();
  if((n - ans)%2 != 0) ans -= 1;
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 892
#include<bits/stdc++.h>
using namespace std;
int main()
{
	 int t,n,a,ans=-1;
	 double mind=10000.0;
	 cin>>n>>t>>a;
	 for(int i=1;i<=n;i++)
	 {
	 	 int x;
	 	 cin>>x;
	 	 double aver=t-0.006*x;
	 	 if(fabs(aver-a)<mind)
	 	 {
	 	 	mind=fabs(aver-a);
	 	 	ans=i;
		  }
		 // cout<<ans<<" "<<mind<<endl;
	 }
	 cout<<ans;
}

Measured memory_bytes: 256