akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

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

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    ll h, w;
    cin >> h >> w;

    // ll ans = h * w;
    if (h % 3 == 0 or w % 3 == 0) {
        cout << 0 << endl;
        return 0;
    }

    ll ans = min(h, w);

    rep(y, h) {
        ll s1 = y * w;
        ll s2 = (h - y) * w;
        ll s3 = w / 2 * (h - y);
        s2 -= s3;
        ll smax = max(max(s1,s2),s3);
        ll smin = min(min(s1,s2),s3);
        chmin(ans, smax - smin);
    }

    rep(x, w) {
        ll s1 = x * h;
        ll s2 = h * (w - x);
        ll s3 = h / 2 * (w - x);
        s2 -= s3;
        ll smax = max(max(s1,s2),s3);
        ll smin = min(min(s1,s2),s3);
        chmin(ans, smax - smin);
    }

    cout << ans << endl;
    return 0;
}


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

int suitIndex(char c)
{ return "00230001000000000000000000"[c-'A'] - '0'; }

int main()
{
    int n; cin >> n;
    bool cards[52] = {0};

    char s;
    int card;
    while(n-- && cin >> s >> card)
        cards[suitIndex(s)*13 + card-1] = true;

    for(card=0; card<52; ++card)
        if(!cards[card]) 
            cout << "SHCD"[card/13] << ' ' << card%13+1 << '\n';

    return 0;
}




Measured memory_bytes: 3080
#include <bits/stdc++.h>
    using namespace std;
     
    int main() {
      cin.tie(0);
      ios::sync_with_stdio(false);
      
      int n, d;
      cin >> n >> d;
      if(n % (d * 2 + 1) == 0) {
        cout << n / (d * 2 + 1) << endl;
      }else {
        cout << (n / (d * 2 + 1)) + 1 << endl;
      }
    }

Measured memory_bytes: 256
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(x, y):
   return (x * y) // gcd(x, y)

M, N = map(int, input().split())
S = input()
T = input()

MA = [S[0]]
NA = [T[0]]

L = lcm(M, N)

m = L/M
n = L/N

for i in range(M):
   if i % n == 0:
       MA.append(S[i])

for i in range(N):
   if i % m == 0:
       NA.append(T[i])

if MA == NA:
   print(L)
else:
   print("-1")

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

namespace ABC065_B_Trained
{
	internal class Program
	{
		public static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			List<int> aList = new List<int>(n);
			for(int i = 0; i < n; i++)
				aList.Add(int.Parse(Console.ReadLine()));

			int index = 1;
			int count = 0;
			//解説スライドを見て書いたコード
			while (count <= n)
			{
				if (index == 2) break;
				
				index = aList[index - 1];
				count++;
			}
			
			if(index == 2) Console.WriteLine(count);
			else Console.WriteLine(-1);
			
			/*以下自分で書いたコード
			List<int> pushedButtonIndex = new List<int>();
			while (index != 2)
			{
				if (pushedButtonIndex.Any(value => value == index))
				{
					Console.WriteLine(-1);
					return;
				}
				
				pushedButtonIndex.Add(index);
				index = aList[index - 1];
				count++;
			}
			
			Console.WriteLine(count);*/
		}
	}
}

Measured memory_bytes: 15328
#include <bits/stdc++.h>
#define DUMP(x)  cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define IFOR(i, m, n) for(int i = n - 1; i >= m; i-- )
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define FOREACH(x,a) for(auto& (x) : (a) )
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
const ll INF = 1e18;
/* テンプレートここまで */
void yes(){
  cout << "Yes" << endl;
  exit(0);
}

void no(){
  cout << "No" << endl;
  exit(0);
}

int main() {
  string n; cin >> n;
  if(n[1] == n[2] && (n[0] == n[1] || n[2] == n[3])) yes();
  else no();
}

Measured memory_bytes: 256
from heapq import *

def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    n, m = read_int_array()
    nums = read_int_array()

    heapify(nums)
    ops = [None]*m
    for i in range(m):
        b, c = read_int_array()
        ops[i] = (c, b)
    ops.sort(reverse=True)

    done = []
    for c, b in ops:
        while b and nums and nums[0] < c:
            heappop(nums)
            done.append(c)
            b -= 1
        if not nums or nums[0] >= c:
            break
    write(sum(nums) + sum(done))

main()


Measured memory_bytes: 78208
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <cstdint>
#include <cmath>
using namespace std;

bool prime(int x)
{
	if ( (x % 2) == 0 ) {
		return false;
	}
	for( int i = 3; i <= sqrt(x); i += 2 ) {
		if ( (x % i) == 0 ) {
			return false;
		}
	}
	return true;
}

int main()
{
	int n;

	cin >> n;

	for( int i = 11; ; i += 5 ) {
		if( prime(i) == true ) {
			cout << i;
			n--;
			if( n > 0 ) {
				cout << " ";
			}
			else {
				cout << endl;
				break;
			}
		}
	}

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <queue>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)

int P[210100];
int main(){
    int N;
    cin >> N;
    REP(i,N){
        int p;
        cin >> p;
        p--;
        P[p] = i;
    }

    int last = N+1;
    int num = 0;
    int max_num = 0;
    REP(i,N){
        int p = P[i]; 
        if(p > last)
            num++;
        else{
            max_num = max(max_num, num);
            num = 0;
        }
        last = p;
    }
    max_num = max(max_num, num);

    cout << N - max_num - 1 << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <algorithm>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <list>
#define INF 10000000000ll
#define MOD 1000000007ll
#define EPS 1e-10
#define REP(i,m) for(long long i=0; i<m; i++)
#define FOR(i,n,m) for(long long i=n; i<m; i++)
#define DUMP(a) for(long long dump=0; dump<(ll)a.size(); dump++) { cout<<a[dump]; if(dump!=(ll)a.size()-1) cout<<" "; else cout<<endl; }
#define ALL(v) v.begin(),v.end()
#define pb push_back
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef long double ld;
using namespace std;

int main(){
	int n;
	cin >> n;
	int t[n][10];
	int p[n][11];
	REP(i, n){
		REP(j, 10){
			cin >> t[i][j];
		}
	}
	REP(i, n){
		REP(j, 11){
			cin >> p[i][j];
		}
	}
	ll max = (-1) * INF;
	FOR(i, 1, 1024){
		int num[n] = { };
		ll tmp = 0;
		REP(j, n){
			REP(k, 10){
				if((i >> k) % 2 == 1){
					if(t[j][k] == 1) num[j]++;
				}
			}
			tmp += p[j][num[j]];
		}
		if(max < tmp) max = tmp;
	}
	cout << max << endl;
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
 
#define REP(i,n) for(auto i=0;i<n;++i)
#define REPR(i,n) for(auto i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(auto i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define pb push_back
#define pf push_front
#define MP make_pair
#define SV(n,v) {int tmp;for(int i=0;i<n;++i){scanf("%d",&tmp);v.push_back(tmp);}}
 
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
 
using namespace std;
 
typedef pair<int,int> pii;
 
int main(){
  int a, b, c;
  cin >> a >> b >> c;

  cout << a * b / 2 << endl;
  return 0;
}
 



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
using namespace std;

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

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

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

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

struct edge {
	ll ind;
	ll to;
	ll d;
};


///////////////////////////

ll d(ll a, ll x) {
	if (a < 0) { return 0; }
	return a / x +1;
}

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

	ll N = s.size();

	ll ans;

	if (s[0] != s[N - 1]) { ans = N % 2; }
	else { ans = (N - 1) % 2; }

	if (ans == 1) { cout << "First"; }
	else { cout << "Second"; }




	system("PAUSE");
}

Measured memory_bytes: 760
import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new Abc153(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Abc153(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val h = ni()
    val n = ni()

    val a = na(n).sum
    if(h > a) {
      out.println("No")
    } else out.println("Yes")
  }
}


Measured memory_bytes: 38032
#pragma once
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <numeric>
#include <iomanip>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 510000;
const int MOD = 1000000007;

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;
}
struct edge {
	int t;
	edge(int _t) : t(_t) {}
};
bool use[100010];


vector<edge> te[100010];
vector<edge> fe[100010];
int t[64];
int d[100100];
ll rec(int num) {
	if (num == 0) return 0;
	return rec(num - 1) + num;
}

void Solve() {
	int n;
	cin >> n;
	int t[105];
	t[0] = 0;
	int res = 0;
	for (size_t i = 1; i <= n; i++)
	{
		cin >> t[i];
		res += max(0, t[i] - t[i - 1]);
	}

	cout << res << endl;
}


int main() {
	Solve();
	return 0;
}



Measured memory_bytes: 6016
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <cmath>
#include <numeric>
#include <tuple>

using namespace std;

int main() {
	int64_t n;
	cin >> n;
	vector<int64_t> vec;
	vector<int64_t> lastvec;
	vec.push_back(0);
	lastvec.push_back(0);
	bool end = false;
	for (int i = 0; i < 11; ++i) {
		if (end) break;
		vector<int64_t> nvec;
		for (int64_t item : lastvec) {
			int64_t v;
			v = item * 10 + 3;
			nvec.push_back(v);
			v = item * 10 + 5;
			nvec.push_back(v);
			v = item * 10 + 7;
			nvec.push_back(v);
			if (v > n) end = true;
		}
		lastvec = nvec;
		for (int64_t item : nvec) {
			vec.push_back(item);
		}
	}
	int sum = 0;
	for (int64_t item : vec) {
		if (item > n) continue;
		int s[3];
		s[0] = s[1] = s[2] = 0;
		while (item > 0) {
			if (item % 10 == 3) s[0]++;
			if (item % 10 == 5) s[1]++;
			if (item % 10 == 7) s[2]++;
			item /= 10;
		}
		if (s[0] > 0 && s[1] > 0 && s[2] > 0) {
			sum++;
		}
	}
	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 2312
//****************************//
/**********  main *************/
//****************************//
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, n) for(i = 0; i < n; i++)
#define FOR(i, a, b) for(i = a; i < b; i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((ll)(x).size())

double pi = 3.141592653589793;

int gcd(int a, int b) {
    if(a % b == 0) {
        return (b);
    } else {
        return (gcd(b, a % b));
    }
}

int main() {

    std::cin.tie(0);
   char s;
   cin >> s;

   if (isupper(s))
   {
       cout << "A" << endl;
   }
   else
   {
       cout << "a" << endl;
   }


    return 0;
}


Measured memory_bytes: 3632
n,c = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
left_info = info[::-1]

right_ruiseki = [0]*(n+1)
left_ruiseki = [0]*(n+1)
ou_right_ruiseki = [0]*(n+1)
ou_left_ruiseki = [0]*(n+1)

for i in range(n):
  right_ruiseki[i+1] = right_ruiseki[i] + info[i+1][1] - info[i+1][0] + info[i][0]
  left_ruiseki[i+1] = left_ruiseki[i] + left_info[i+1][1] + left_info[i+1][0] - left_info[i][0]
  ou_right_ruiseki[i+1] = ou_right_ruiseki[i] + info[i+1][1] - 2*info[i+1][0] + 2*info[i][0]
  ou_left_ruiseki[i+1] = ou_left_ruiseki[i] + left_info[i+1][1] + 2*left_info[i+1][0] - 2*left_info[i][0]

#print(right_ruiseki,left_ruiseki,ou_right_ruiseki,ou_left_ruiseki)

for i in range(n):
  right_ruiseki[i+1]=max(right_ruiseki[i],right_ruiseki[i+1])
  left_ruiseki[i+1]=max(left_ruiseki[i],left_ruiseki[i+1])

#print(right_ruiseki,left_ruiseki,ou_right_ruiseki,ou_left_ruiseki)
ans = 0

for i in range(n+1):
  tmp1 = ou_right_ruiseki[i]+left_ruiseki[n-i]
  tmp2 = ou_left_ruiseki[i]+right_ruiseki[n-i]
  ans = max(tmp1,tmp2,ans)
print(ans)

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

int main() {
  int n;
  cin >> n;
  map<long long, long long> p;
  for (int i = 2; i <= n; i++) {
    int tmp = i;
    int j = 2;
    while (true) {
      if (tmp == 1) {
        break;
      }
      while (true) {
        if (tmp%j != 0) {
          break;
        }
        tmp /= j;
        p[j]++;
      }
      j++;
    }
  }
  long long ans = 1;
  for (auto i = p.begin(); i != p.end(); i++) {
    int tmp = i->second;
    ans *= tmp + 1;
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}

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

ui MOD = 1e9 + 7;

int main()
{

    int w, h;
    cin >> h >> w;
    char grid[h + 1][w + 1];
    ui dp[h + 1][w + 1];
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= h; i++)
        for (int j = 1; j <= w; j++)
            cin >> grid[i][j];

    for (int i = 1; i <= h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            if (grid[i][j] == '#')
                dp[i][j] = 0;
            else if (i == 1 && j == 1)
                dp[i][j] = 1;
            else if (i == 1)
                dp[i][j] = dp[i][j - 1];
            else if (j == 1)
                dp[i][j] = dp[i - 1][j];
            else        
                dp[i][j] = (dp[i - 1][j]%MOD + dp[i][j - 1]%MOD)%MOD;
    
        }
    }

    cout << dp[h][w];
    return 0;
}


Measured memory_bytes: 5120
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class Doit {
    static IEnumerable<int> rep(int a, int b) {
        return Enumerable.Range(a, b);
    }
    
    static void Main() {
	var s = ReadLine();

	var n = (from x in s
		 where x == 'p'
		 select x).Count();

	WriteLine("{0}", (s.Count() / 2) - n);
    }
}


Measured memory_bytes: 3288
#include <cmath>
#include <cstdio>

#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <numeric>
#include <string>
#include <array>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;

// Union Find begin

class UnionFind {
 private:
  vector<ll> _par;
  vector<ll> _rank;
 public:
  explicit UnionFind(ll n);
  ll find(ll x);
  void unite(ll x, ll y);
  bool isSame(ll x, ll y);
};

UnionFind::UnionFind(ll n) {
  for (ll i = 0; i < n; ++i) {
    _par.push_back(i);
    _rank.push_back(0);
  }
}

ll UnionFind::find(ll x) {
  if (_par.at(static_cast<unsigned long>(x)) == x) return x;
  else return _par.at(static_cast<unsigned long>(x)) = find(_par.at(static_cast<unsigned long>(x)));
}

void UnionFind::unite(ll x, ll y) {
  x = find(x);
  y = find(y);
  if (x == y) return;

  if (_rank.at(static_cast<unsigned long>(x)) < _rank.at(static_cast<unsigned long>(y)))
    _par.at(static_cast<unsigned long>(x)) = y;
  else {
    _par.at(static_cast<unsigned long>(y)) = x;
    if (_rank.at(static_cast<unsigned long>(x)) == _rank.at(static_cast<unsigned long>(y)))
      ++_rank.at(static_cast<unsigned long>(x));
  }

}

bool UnionFind::isSame(ll x, ll y) {
  return find(x) == find(y);
}

// Union Find end

int main() {
  ios::sync_with_stdio(false);
  ll n, k, l;
  cin >> n >> k >> l;

  ll p, q, r, s;
  UnionFind ufl(n), ufd(n);
  rep(i, k) {
    cin >> p >> q;
    ufl.unite(p-1, q-1);
  }
  rep(i, l) {
    cin >> r >> s;
    ufd.unite(r-1, s-1);
  }
  map<pair<ll ,ll>, ll> cnt;
  rep(i, n)
    cnt[make_pair(ufl.find(i), ufd.find(i))] += 1;

  rep(i, n)
    cout << cnt[make_pair(ufl.find(i), ufd.find(i))] << " ";


  cout << endl;


  return 0;
}

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

ll mod=1e9+7;

ll mpow(ll x, ll n) {
  ll ans=1;
  while(n>0) {
    if(n&1) {
      ans=ans*x%mod;
    }
    x=x*x%mod;
    n>>=1;
  }
  return ans;
}

vector<ll> tfact(1e7+1,-1);
ll fact(ll n) {
  if(tfact[n]<0) {
    if(n==0) {
      tfact[n]=1;
    }
    else {
      tfact[n]=fact(n-1)*n%mod;
    }
  }
  return tfact[n];
}

int main() {
  ll n;
  cin >> n;
  vll x(n);
  for(ll i;i<n;i++) {
    cin >> x[i];
  }
  vll back(n);
  for(int i=0;i<n;i++) {
    back[i]=mpow(i+1,mod-2);
  }
  vll sback(n);
  for(int i=0;i<n;i++) {
    if(i==0) {
      sback[i]=back[i];
    }
    else {
      sback[i]=(sback[i-1]+back[i])%mod;
    }
  }
  ll ans=0;
  for(int i=0;i<n-1;i++) {
    ans=(ans+(x[i+1]-x[i])*sback[i]%mod)%mod;
  }
  ans=ans*fact(n-1)%mod;
  cout << ans << endl;
}

Measured memory_bytes: 83840
N = int(input())
cake = 4
donut = 7

for i in range((100//cake) + 1):
  is_break = False
  for j in range((100//donut) + 1):
    num = cake * i + donut * j
    if N == num:
      print('Yes')
      is_break = True
      break
  if is_break:
    break
else:
  print('No')

Measured memory_bytes: 3060
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
template < typename T > std::string to_string(const T& n)
{
	std::ostringstream stm;
	stm << n;
	return stm.str();
}
int main()
{
	int n, b = 0, c = 0;
	string Istr, Ostr;
	char ch;
	cin >> n;
	do {
		cin >> Istr;
		while (n--) {
			for (int i = 0; i < (int)Istr.size();) {
				ch = Istr[i];
				for (int j = i; i < (int)Istr.size(); j++) {
					if (Istr[i] == Istr[j]) {
						b++;
					}
					else
					{
						i = j;
						break;
					}
				}
				Ostr += to_string(b);
				Ostr.push_back(ch);
				b = 0;
			}
			Istr.clear();
			Istr = Ostr;
			Ostr.clear();
		}
		cout << Istr << endl;
		cin >> n;
	} while (n != 0);
	return 0;
}

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

// DAG
vector<bool> visited;
vector<int> dp;

int DFS(const vector<vector<int>> &graph, int v){
    if(visited[v] == true) return dp[v];
    
    visited[v] = true;
    for(auto next_v : graph[v]){
        if(DFS(graph, next_v) == -1) return -1;

        dp[v] = max(dp[v], DFS(graph, next_v) + 1);
    }
    return dp[v] = max(0, dp[v]);// if no next_v (i.e. source) dp[v] = 0
}

int main(){
    int n;
    cin >> n;
    int V = n * (n-1) / 2;
    visited.assign(V,false);
    dp.assign(V,-1);

    vector<vector<int>> a(n, vector<int>(n-1));
    for(int i=0; i<n; i++){
        for(int j=0; j<n-1; j++){
            cin >> a[i][j];
            a[i][j] -= 1;
        }
    }

    vector<vector<int>> matchID(n, vector<int>(n));
    int id = 0;
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            matchID[i][j] = id;
            matchID[j][i] = id;
            id += 1;
        }
    }

    vector<vector<int>> graph(V);
    for(int i=0; i<n; i++){
        for(int j=1; j<n-1; j++){
            int s = matchID[i][a[i][j-1]];
            int t = matchID[i][a[i][j]];
            //cout << s << " " << t << endl;
            graph[t].push_back(s);// arrow inversed
        }
    }

    int ans = 0;
    for(int i=0; i<V; i++){
        int depth = DFS(graph, i);
        if(depth == -1){
            cout << -1 << endl;
            return 0;
        }
        ans = max(ans, depth);
    }
    cout << ans + 1 << endl;
    return 0;
}

Measured memory_bytes: 37504
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
 
const int MAX_SIZE = 1010;
const int HALF = MAX_SIZE / 2;
const int MAX_M = 1000000;
 
int field[MAX_SIZE][MAX_SIZE];
pair<int, int> dp[MAX_SIZE][MAX_SIZE];
bool is_prime[MAX_M + 1];

inline void sieve(int n) {
	fill(is_prime, is_prime + n, true);
    is_prime[0] = is_prime[1] = false;
    for(int i = 2; i < n; ++i) {
        if(is_prime[i]) {
            for(long long j = static_cast<long long>(i) * i; j < n; j += i)
                is_prime[j] = false;
        }
    }
}
 
inline void make_field() {
    const int INF = 0xfffffff;
    fill(field[0], field[0] + MAX_SIZE * MAX_SIZE, INF);

	int x = HALF, y = HALF, d = 0;
    for(int num = 1; num <= MAX_M; ++num) {
        field[y][x] = num;
        const int left = (d + 1) % 4;
        if(field[y + dy[left]][x + dx[left]] == INF)
            d = left;
 
        x += dx[d];
        y += dy[d];
    }
}

inline pair<int, int> solve(const int &m, const int &n) {
	int x, y;
	int min_x = MAX_SIZE, max_x = -1, max_y = -1;
	for(int i = 0; i < MAX_SIZE; ++i) {
		for(int j = 0; j < MAX_SIZE; ++j) {
			if(field[i][j] <= m) {
				min_x = min(min_x, j);
				max_x = max(max_x, j);
				max_y = max(max_y, i);
 
				if(field[i][j] == n) {
					x = j;
					y = i;
				}
			}
		}
	}
 
	fill(dp[y], dp[y] + (max_y - y + 1) * MAX_SIZE, pair<int, int>(-1, -1));
	dp[y][x] = make_pair(0, 0);
	for(; y <= max_y; ++y) {
		for(; x <= max_x; ++x) {
			const int num = field[y][x];
			if(num > m || dp[y][x].first == -1)
				continue;
 
			if(is_prime[num]) {
				++dp[y][x].first;
				dp[y][x].second = num;
			}
 
			for(int k = -1; k <= 1; ++k) {
				dp[y + 1][x + k] = max(dp[y + 1][x + k], dp[y][x]);
			}
		}
		x = min_x;
	}

	return *max_element(dp[max_y] + min_x, dp[max_y] + max_x + 1);
}
 
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

	sieve(MAX_M + 1);
    make_field();
 
    for(int m, n; cin >> m >> n && m;) {
		const pair<int, int> ans = solve(m, n);
		cout << ans.first << " " << ans.second << endl;
    }
 
    return EXIT_SUCCESS;
}

Measured memory_bytes: 14124
def solve():
    A,B, M = map(int, input().split())
    a_kind = list(map(int, input().split()))
    b_kind = list(map(int, input().split()))
    Coupon =  [list(map(int, input().split())) for _ in range(M)]
    ans = min(a_kind) + min(b_kind)

    for x,y,disc in Coupon:
        ans = min(ans, a_kind[x-1] + b_kind[y-1] - disc)
    print(ans)



if __name__ == '__main__':
    solve()


Measured memory_bytes: 38564
#include<bits/stdc++.h>
using namespace std;
vector<int> ga[100010];
int indeg[100010];
int a,b;
int ancest[100010];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=(n-1+m);i++){
		cin>>a>>b;
		ga[a].push_back(b);
		indeg[b]++;
	}
	stack<int> S;
	for(int i=1;i<=n;i++)
	   if(indeg[i]==0){
	   	  S.push(i);
	   	  ancest[i]=0;
	   }
	while(!S.empty()){
		int curr=S.top();
		S.pop();
		for(int i=0;i<ga[curr].size();i++)
		{
			indeg[ga[curr][i]]--;
			if(indeg[ga[curr][i]]==0){
				S.push(ga[curr][i]);
				ancest[ga[curr][i]]=curr;
			}
		}
	}
	for(int i=1;i<=n;i++)
	   cout<<ancest[i]<<endl;
}

Measured memory_bytes: 7040
import java.util.*;
public class Main{
  static double xp,yp,z,x1,y1;
  public static double[] w(double[] x, double[] y) {
	  x[2] = -x[2];
	  y[2] = -y[2];
	  z = (x[0] * y[1] )- (x[1] * y[0]);
	  x1 = +(y[1] * x[2]) - (x[1] * y[2]);
	  y1 = +(x[0] * y[2]) - (y[0] * x[2]);
	  return new double[] { (x1 / z), (y1 / z) };
	}
public static void main(String [] args){
	  Scanner cs = new Scanner(System.in);
	  double[] x = new double[4];
	  double[] y = new double[4];
	  while (cs.hasNextDouble()) {  {
	      for (int i = 0; i < 3; i++) {
	        x[i] = cs.nextDouble();
	        y[i] = cs.nextDouble();
	      }
	       xp = cs.nextDouble();
	       yp = cs.nextDouble();
	      for (int i = 0; i < 3; i++) {
	        x[i] -= xp;
	        y[i] -= yp;
	      }
	    }
	    double[] X = w(x, y);
	    if (X[0] >= 0 && X[1] >= 0) System.out.println("YES");
	   else System.out.println("NO");
	    }
	  }
	
	}

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

ll a,b,c,a1,b1,c1,k;

int main(){
	cin>>a>>b>>c>>k;
	if(k%2==0){
		cout<<a-b<<endl;
	}
	else{
		a1=a;b1=b;c1=c;
		a=b1+c1;b=a1+c1;c=a1+b1;
		cout<<a-b<<endl;
	}
}

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

struct islands {
  int t;
  int w;

  bool operator>(const islands &left) const {
    return w > left.w;
  }
};

int main() {
  int n, i;
  vector<islands> island;

  while(1) {
    cin >> n;
    if(n == 0)  break;

    for(i = 0; i < n; i++) {
      islands I;
      cin >> I.t >> I.w;
      island.push_back(I);
    }

    sort(island.begin(), island.end(), greater<islands>());

    int sum = 0;
    int flag = 0;
    for(i = n-1; i >= 0; i--) {
      sum += island[i].t;
      if(island[i].w < sum) {
        cout << "No" << endl;
        flag = 1;
        break;
      }
    }
    if(flag != 1)
      cout << "Yes" << endl;

    island.clear();
  }
  return 0;
}


Measured memory_bytes: 3072
# -*- coding: utf-8 -*-
import sys

def main():
    N,K = list(map(int, sys.stdin.readline().split()))
    A_list = list(map(int, sys.stdin.readline().split()))


    A_list.sort(key=lambda x: -abs(x))
    mod = 10**9 + 7

    L_plus  = R_plus  = -1 # the Index of plus value
    L_minus = R_minus = -1 # the Index of minus value
    # L : between index 0 to (K-1)
    # R : between index K to end


    for i in range(K-1, -1, -1):
        if (L_plus == -1) and (A_list[i] >= 0):
            L_plus = i
        if (L_minus == -1) and (A_list[i] < 0):
            L_minus = i
        if (L_plus != -1) and (L_minus != -1):
            break
    
    for i in range(K,N):
        if (R_plus == -1) and (A_list[i] >= 0):
            R_plus = i
        if (R_minus == -1) and (A_list[i] < 0):
            R_minus = i
        if (R_plus != -1) and (R_minus != -1):
            break


    cnt_minus = 0

    for i in range(K):
        if A_list[i] < 0:
            cnt_minus += 1


    if cnt_minus % 2 == 0:
        target = A_list[:K]
    
    else: # cnt_minus % 2 != 0
        if (R_plus == -1) and \
           ( (L_plus == -1) or (R_minus == -1) ) :
        
            calc1 = calc2 = 0
        
        elif (R_plus != -1) and \
           ( (L_plus == -1) or (R_minus == -1) ) :
        
            calc1 = 1
            calc2 = 0
        
        elif (R_plus == -1) and \
           ( (L_plus != -1) and (R_minus != -1) ) :
        
            calc1 = 0
            calc2 = 1
        
        elif (R_plus != -1) and \
           ( (L_plus != -1) and (R_minus != -1) ) :
            
            calc1 = A_list[L_plus] * A_list[R_plus]
            calc2 = A_list[L_minus] * A_list[R_minus]


        if calc1 == calc2:
            target = A_list[N-K:N]
        
        elif calc1 > calc2:
            A_list[L_minus], A_list[R_plus] = A_list[R_plus], A_list[L_minus]
            target = A_list[:K]

        elif calc1 < calc2:
            A_list[L_plus], A_list[R_minus] = A_list[R_minus], A_list[L_plus]
            target = A_list[:K]


    ans = 1

    for i in range(len(target)):
        ans *= target[i]
        ans %= mod
    
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 31416
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <bitset>
#include<vector>
#include<utility>
#include<fstream>
#include<queue>
#include <iomanip>
#include<numeric>
#include<set>
using namespace std;
typedef long long ll;
#define rep(i,k) for(i=0;i<k;i++)
#define P pair<ll,ll>
ll mod = 1000000007;
P p[200005];

ll i, b[100005], c, k, j, n, m, z = 0, mi = 0;
ll a[100005] = {};
vector<ll> v[30];
string s;
char ca[10000];
priority_queue<ll> q;
int main() {
	cin >> n;
	rep(i, n) {
		cin >> b[i];
		if (i % 2 == 0)mi += 2 * b[i];
		z += b[i];
	}
	a[0] = mi - z;
	rep(i, n - 1)a[i + 1] = 2 * b[i] - a[i];
	rep(i,n)cout << a[i]<<" ";

	return 0;
}

Measured memory_bytes: 2944
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define REPS(i,n) for(int i=1;i<=(int)(n);i++)
using namespace std;
using ll = long long;
using ld = long double;

int main(void) {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    REP (i, H) {
        cin >> S[i];
    }

    int ans = 0;
    // スタート地点を決める
    REP (si, H) {
        REP (sj, W) {
            // スタート地点が壁の中になる場合を除外
            if (S[si][sj] == '#') {
                continue;
            }

            // BFS
            vector<vector<int>> dist(H, vector<int> (W, -1));
            queue<pair<int, int>> que;
            dist[si][sj] = 0;
            que.push(make_pair(si, sj));

            while (!que.empty()) {
                pair<int, int> v = que.front();
                que.pop();

                // vから左下上右に移動
                int di[4] = {0, 1, -1, 0};
                int dj[4] = {-1, 0, 0, 1};
                REP (k, 4) {
                    int ni = v.first + di[k];
                    int nj = v.second + dj[k];
                    // 迷路の範囲外を参照してしまう場合を除外
                    if ((ni < 0 || H <= ni) || (nj < 0 || W <= nj)) {
                        continue;
                    }
                    // 道が存在して、かつ未訪問ならば、距離を設定してキューに追加
                    if (S[ni][nj] == '.' && dist[ni][nj] == -1) {
                        dist[ni][nj] = dist[v.first][v.second] + 1;
                        que.push(make_pair(ni, nj));
                    }
                }
            }

            // スタート地点からの距離が最大になるゴール地点を調べる
            REP (gi, H) {
                REP (gj, W) {
                    ans = max(ans, dist[gi][gj]);
                }
            }
        }
    }

    cout << ans << endl;
}


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

using namespace std;

int main(void){

  int a,b,n;
  while( cin >> a >> b >> n ){

    int s = 0;
    for( int i = 0 ; i < n ; i++ ){
     a = a % b;
     a *= 10; 
     s = s + a / b;
    }
    cout << s << endl;
  }
  return 0;
}

Measured memory_bytes: 1160
#include "bits/stdc++.h"
using namespace std;
multiset<int> S; 
multiset<int>::iterator it;

int main(){
	int N , Num , i;
	int left , right;
	scanf( "%d" , &N );

	for( i = 1 ; i <= N ; ++i ){
		scanf( "%d" , &Num );
		S.insert( Num );
	}

	if( N == 1 ){
		puts( "0" );
		return 0;
	}

	left = *S.begin();
	it = S.end();
	--it;
	right = *it;
	S.erase( S.begin() );
	S.erase( it );
	long long Ans = right - left;
	int f , b;

	while( int(S.size()) > 1 ){
		f = *S.begin();
		it = S.end();
		--it;
		b = *it;

		if( abs( left - f ) > abs( left - b ) ){
			if( abs( right - f ) > abs( right - b ) ){
				S.erase( S.begin() );

				if( abs( left - f ) > abs( right - f ) ){
					Ans = Ans + abs( left - f );
					left = f;
				}
				else{
					Ans = Ans + abs( right - f );
					right = f;
				}
			}
			else{
				if( abs( left - f ) > abs( right - b ) ){
					S.erase( S.begin() );
					Ans = Ans + abs( left - f );
					left = f;
				}
				else{
					S.erase( it );
					Ans = Ans + abs( right - b );
					right = b;
				}
			}
		}
		else{
			if( abs( right - f ) > abs( right - b ) ){
				if( abs( left - b ) > abs( right - f ) ){
					S.erase( it );
					Ans = Ans + abs( left - b );
					left = b;
				}
				else{
					S.erase( S.begin() );
					Ans = Ans + abs( right - f );
					right = f;
				}
			}
			else{
				S.erase( it );

				if( abs( left - b ) > abs( right - b ) ){
					Ans = Ans + abs( left - b );
					left = b;
				}
				else{
					Ans = Ans + abs( right - b );
					right = b;
				}
			}
		}
	}

	if( !S.empty() ){
		if( abs( *S.begin() - left ) > abs( *S.begin() - right ) ) 
			Ans = Ans + (abs( *S.begin() - left ));
		else Ans = Ans + abs( *S.begin() - right );
	}

	printf( "%lld\n" , Ans );
	return 0;
}

Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const double PI = 3.14159265358979323846;

int main() {
  vector<int> time(3);
  vector<int> subsc = {0, 1, 2};
  rep(i, 3) cin >> time[i];
  int cmp = 100000000;
  do {
    int sum = 0;
    rep(i, 2) sum += time[subsc[i]];
    cmp = min(sum, cmp);
  }while(next_permutation(subsc.begin(), subsc.end()));
  
  cout << cmp << endl;
  return 0;
}
    

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

int main() {
  int n, m; cin >> n >> m;
  vector<map<int, int>> city(n + 1);
  for (int i = 0; i < m; i++) {
    int p, y; cin >> p >> y;
    city[p].emplace(y, i);
  }
  
  vector<string> answers(m);
  
  for (int i = 0; i <= n; i++) {
    if (!city[i].empty()) {
      int cnt = 1;
      string pname = to_string(i);
      while (pname.size() < 6) pname = "0" + pname;
      for (auto it = city[i].begin(); it != city[i].end(); it++) {
        string num = to_string(cnt);
        while (num.size() < 6) num = "0" + num;
        answers[(*it).second] = pname + num;
        cnt++;
      }
    }
  }
  
  for (string ans : answers) cout << ans << endl;
}

Measured memory_bytes: 16384
#include "bits/stdc++.h"
#define ll long long
#define rep2(i,a,b) for(int i=(a);i<=(b);++i)
#define rep(i,n) rep2(i,0,n-1)
int MOD=1000000000+7;
int INF=1e6;
 
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");

main(){
    int a[60][60]={};
    int h,w;
    cin>>h>>w;
    rep2(i,1,w){
        rep2(j,1,h){
            char c;
            cin>>c;
            if(c=='#') a[j][i]=1;
        }
    }
    rep2(i,1,w){
        rep2(j,1,h){
            if(a[j][i]==1)
            {
                if(!(a[j+1][i]||a[j-1][i]||a[j][i+1]||a[j][i-1])) {cout<<"No"; return 0;}
        }
        }
    }

    cout<<"Yes";
}


Measured memory_bytes: 256
def main():
    n, k = map(int, input().split())
    i = 0
    s = []
    for _ in range(k):
        d = int(input())
        v = map(int, input().split())
        s += list(v)

    ans = n - len(set(s))
    print(ans)


if __name__ == '__main__':
    main()


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

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;

#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i<n; i++)
#define ALL(a) begin(a), end(a)

typedef pair<int,int> Edge;
int N,M,R;
vector<int> r;
vector<vector<Edge>> E;


int main (void) {
    cin>>N>>M>>R;
    REP(i,R) {
        int rr;
        cin>>rr;
        r.pb(rr-1);
    }
    E.resize(N);
    REP(i,M) {
        int a,b,c;
        cin>>a>>b>>c;
        E[a-1].pb(Edge(c,b-1));
        E[b-1].pb(Edge(c,a-1));
    }


    vector<vector<ll>> cost(R,vector<ll>(N,INF));

    REP(i,R) {
        priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
        pq.push(Edge(0,r[i]));
        vector<int> used(N,false);
        cost[i][r[i]] = 0;

        while(pq.size()) {
            Edge e = pq.top();
            pq.pop();
            int to = e.second, co = e.first;
            if (used[to]) continue;
            used[to] = true;
            REP(j,E[to].size()) {
                Edge t = E[to][j];
                if (cost[i][t.second] > cost[i][to] + t.first) {
                    cost[i][t.second] = cost[i][to] + t.first;
                    pq.push(Edge(cost[i][t.second],t.second));
                }
            }
        }
    }

    ll ans = INF;
    map<int,int> adpt;
    REP(i,R) {
        adpt[r[i]] = i;
    }
    sort(ALL(r));
    do {
        ll dist = 0;
        for (int i=1;i<R;i++) {
            int from = adpt[r[i-1]] , to = r[i];
            dist += cost[from][to];
        }
        ans = min(dist,ans);
    } while (next_permutation(ALL(r)));
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 768
n, k, *h = map(int, open(0).read().split())
dp = [0] * n
for i in range(1, n):
  dp[i] = min(dp[i-a] + abs(h[i] - h[i-a]) for a in range(1, min(i, k) + 1))
print(dp[n - 1])

Measured memory_bytes: 88216
import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		double N = sc.nextDouble();
		double cnt = 0;
		for(int i = 1; i <= N; i++) {
			if(i % 2 == 1) {
				cnt++;
			}
		}
		System.out.println(cnt / N);
	}
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define VSORT(v) sort(v.begin(), v.end())
#define DVSORT(v) sort(v.begin(), v.end(),greater<int>())
#define SORT(v, n) sort(v, v+n)
#define DSORT(v,n) sort(v, v+n,greater<int>())
#define vi vector<int>
#define pb push_back
#define P pair<int,int>


template <class T> void chmin(T&a, const T&b) { a = min(a,b); }
template <class T> void chmax(T&a, const T&b) { a = max(a,b); }

const double EPS =1e-9;
const long INF =999999999;
const long MOD =1000000007; //10^9+7
#define PI 3.14159265258979

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

int main(){
	int N,rate,counter=0;
	cin>>N;
	set<int> st;

	REP(i,N){
		cin>>rate;
		FOR(j,1,10){
			if(j==9) counter++;
			else if(rate<(400*j)){
				st.insert(j);
				break;
			}
		}
	}

	printf("%d %d\n",max(1,(int)st.size()),(int)st.size()+counter);
	return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e9;
#define sz              3000005
#define Fast            ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

int arr[10004];

int cal(int x, int y, int z){

    return (x*x)+(y*y)+(z*z)+(x*y)+(y*z)+(z*x);
}

int main(){
    Fast;
    int n, cnt=0;
    cin>>n;
    for(int i=1; i*i<=n; i++){
        for(int j=1; (i*i)+(j*j)+(i*j)<=n; j++){
            for(int k=1; cal(i,j,k)<=n; k++){
                arr[cal(i,j,k)]++;
            }
        }
    }
    for(int i=1; i<=n; i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}



Measured memory_bytes: 3600
#include<stdio.h>
int serch(int S[],int key,int n){
  int left,right,mid;
  left = 0;
  right = n;
  while(left < right){
    mid = (right + left) / 2;
    if( S[mid] == key)
	return 1;
    else if( key < S[mid])
      right = mid;
    else
      left = mid + 1;
  }

  return 0;
}



int main(){
  int n,i,S[100000],q,key,sum=0;
  scanf("%d", &n);
  for(i = 0; i < n; i++)
    scanf("%d", &S[i]);
  
  scanf("%d", &q);
  for(i = 0; i < q; i++){
    scanf("%d", &key);
    if(serch(S,key,n))
      sum++;
  }

  printf("%d\n", sum);
  return 0;
}



Measured memory_bytes: 2108
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>  
#include <stdbool.h>   
#define inf (INT_MAX-1)
#define INF 9223372036854775807;
#define PI 3.14159265358979323846;
#define EPS 1e-10
#define sq(n) ((n)*(n))
#define rep(i,n) for(i=0;i<n;i++)
#define rev(i,n) for(i=n-1;i>=0;i--)
/*
#define sort(a,n) qsort(a,n,sizeof(TYPE),cmp)
#define sort_r(a,n) qsort(a,n,sizeof(TYPE),cmp_r)
*/
#define chsort(s,n) qsort(s,n,sizeof(char),cmp)
#define chsort_r(s,n) qsort(s,n,sizeof(char),char_cmp_r)
#define TYPE long long
#define ll long long
#define MEMSET(a) memset(a,0,sizeof(a))
#define MEMSET_U(a) memset(a,-1,sizeof(a))
#define bool    _Bool
const int mod = (int)1e09 + 7;

//#define DEBUG1
//#define DEBUG2
//#define DEBUGF
#define DUMMY

int in(void) { int i; scanf("%d", &i); return i; }
long long llin(void) { long long i; scanf("%lld", &i); return i; }
double din(void) { double i; scanf("%lf", &i); return i; }
void chin(char s[]) { scanf("%s", s); }

void print(int a) { printf("%d\n", a); }
void llprint(long long a) { printf("%lld\n", a); }
void dprint(double a) { printf("%.10f\n", a); }
void print2(int a, int b) { printf("%d %d\n", a, b); }
int Max(int a, int b) { if (a > b) { return a; }return b; }
int Min(int a, int b) { if (a < b) { return a; }return b; }
long long llmax(long long a, long long b) { return a > b ? a : b; }
long long llmin(long long a, long long b) { return a < b ? a : b; }
double dmax(double a, double b) { return a > b ? a : b; }
double dmin(double a, double b) { return a < b ? a : b; }
//long long llmax(long long a, long long b) { return a > b ? a : b; }
//long long llmin(long long a, long long b) { return a < b ? a : b; }
//double dmax(double a, double b) { return a > b ? a : b; }
int cmp(const void *a, const void *b) { return *(TYPE *)a - *(TYPE *)b; }
int cmp_r(const void *a, const void *b) { return *(TYPE *)b - *(TYPE *)a; }
int char_cmp(const void *a, const void *b) { return strcmp((char *)a, (char *)b); }
int char_cmp_r(const void *a, const void *b) { return strcmp((char *)b, (char *)a); }
void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

int main() {
	long long int N, Y;
	long long int x, y, z;
	bool ok = false;
	scanf("%lld%lld", &N, &Y);

	for (x = 0; x <= N; x++) {
		for (y = 0; y <= N - x; y++) {
			z = N - x - y;
			if (10000 * x + 5000 * y + 1000 * z == Y) {
				ok = true;
			}
			if (ok == true) {
				break;
			}
		}
		if (ok == true) {
			break;
		}
	}
	if (ok == false) {
		x = -1;
		y = -1;
		z = -1;
	}
	printf("%lld %lld %lld\n", x, y, z);

#ifdef DEBUGF
	getch();
#endif
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define ll long long
stack<int>s;
struct node
{
    int t,x;
   bool operator < (const node& a)const
   {
       return x>a.x;
   }
}a[N];
int vis[N];
int main()
{

    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].t,&a[i].x);
    }
    sort(a+1,a+1+n);
    ll tp=0;
    ll sum=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(i<=k)
        {
            if(vis[a[i].t])
                s.push(a[i].x);
            else
                vis[a[i].t]=1,tp++;

            sum+=a[i].x;

            ans=max(ans,sum+tp*tp);
        }
        else
        {
            if(s.empty())break;
            if(vis[a[i].t])continue;

            int v=s.top();s.pop();


            vis[a[i].t]=1;
            tp++;
            sum=sum-v+a[i].x;
            ans=max(ans,sum+tp*tp);

        }
    }
        printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 1536
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <cstring>

using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define rep(i,n) for (ll i = 0; i < (n); ++i)

int n;

int main() {
    cin >> n;

    cout << 8 - (n-400)/200 << endl;

    return 0;
}


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

int main() {
  int n, input;
  vector<int> w;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> input;
    w.push_back(input);
  }

  int s1, s2, ans, total;
  total = 0;
  ans = 0;
  for (int i = 0; i < n; i++) {
    total += w[i];
  }

  for (int i = 1; i < n; i++) {
    s1 = 0;
    s2 = 0;
    for (int k = 0; k < n; k++) {
      if (k< i) {
        s2 += w[k];
      } else {
        s1 += w[k];
      }
    }
    if (i == 1) {
      ans = abs(s1 - s2);
    } else if (ans > abs(s1 - s2)) {
      ans = abs(s1 - s2);
    }
  }

  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256