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

int main(){
    vector<vector<int>> a(3,vector<int>(3));
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
    cin>>a[i][j];
      }
    }
    int N;
    cin>>N;
    vector<int> b(N);
    for(int i=0;i<N;i++){
      cin>>b[i];
    }
    vector<vector<int>> c(3,vector<int>(3));
    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){

    c[i][j]=0;
      }
    }

    for(int i=0;i<3;i++){
      for(int j=0;j<3;j++){
   for(int k=0;k<N;k++){
        if(a[i][j]==b[k]){
          c[i][j]=1;
        }
    }
      }
    }
  
    bool f = false;
    for(int i=0;i<3;i++){
        if(c[i][0]==1&&c[i][1]==1&&c[i][2]==1)
        f=true;
    }
    for(int i=0;i<3;i++){
        if(c[0][i]==1&&c[1][i]==1&&c[2][i]==1)
        f=true;
    }
    if(c[0][0]==1&&c[1][1]==1&&c[2][2]==1) {
      f=true;
    }
    if(c[0][2]==1&&c[1][1]==1&&c[2][0]==1) {
      f=true;
    }
    if(f){
      cout<<"Yes"<<endl;
    }
    else {
      cout<<"No"<<endl;
    }
    
}


Measured memory_bytes: 256
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int a=scan.nextInt();
		String str=scan.next();
		int b=scan.nextInt();
		char ch=str.charAt(b-1);
		String ans="";
		for(int i=0;i<str.length();i++) {
			char chl=str.charAt(i);
			if(!(chl==ch)) {
				ans+="*";
			}
			else {
				ans+=chl;
			}
		}
		System.out.println(ans);
	}
}


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

int main(){
    int n,m;
    cin >> n >> m;
    vector<bool> a(n+1, true);
    vector<long long int> ans(n+1);
    int save;
    for(int i = 0; i < m; i++){
        cin >> save;
        a.at(save) = false;
    }
    ans.at(0) = 1;
    if(a.at(1)) ans.at(1) = 1;
    for(int i = 2; i < n+1; i++){
        if(a.at(i)) ans.at(i) = (ans.at(i-1) + ans.at(i-2)) % 1000000007;
    }
    cout << ans.at(n) << endl;
}

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

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n,i;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    if(n%1000==0)
    {
        cout<<0;
        return 0;
    }
    else
    {
        cout<<1000-n%1000;
        return 0;
    }
    return 0;
}


Measured memory_bytes: 3632
n, m = map(int, input().split())
tree = [0]+[i+1 for i in range(n)]
size = [0] + [1 for i in range(n)]
def find(c):
    pc = tree[c]
    if pc == c:
        return pc
    else:
        return find(pc)

for _ in range(m):
    a, b = map(int, input().split())
    pa, pb = find(a), find(b)
    if pa!=pb:
        if size[pa] < size[pb]:
            tree[pa] = pb
            size[pa], size[pb] = size[pa]+size[pb], size[pa]+size[pb]
        else:
            tree[pb] = pa
            size[pa], size[pb] = size[pa]+size[pb], size[pa]+size[pb]
            
print(max(size))

Measured memory_bytes: 19920
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <fstream>
#include <bitset>
#include <map>
#include <queue>
#include <complex>
#include <assert.h>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <stack>
#include <numeric>

typedef long long ll;
typedef long double ld;

#define pb push_back
#define mp make_pair

using namespace std;

int h, w;

int xd[8] = {0,0,-1,-1,-1,1,1,1};
int yd[8] = {1,-1,0,1,-1,0,1,-1};
bool in(int x, int y){
	return 0 <= x && x < h && 0 <= y && y < w;
}


int main(){
	
	cin >> h >> w;
	vector<vector<char> > a(h);
	for(int i = 0; i<h; i++){	
		string s;
		cin >> s;
		a[i].resize(w);
		for(int j = 0; j < w; j++) a[i][j] = s[j];
	}
	for(int i = 0; i<h; i++){
		for(int j = 0; j < w; j++){
			int ct = 0;
			for(int d = 0; d < 8; d++){
				if(in(i + xd[d], j + yd[d])){
					if(a[i + xd[d]][j + yd[d]] == '#'){
						ct++;
					}
				}
			}
			if(a[i][j] != '#'){
				a[i][j] = (char)(ct + '0');
			}
		}
	}

	for(int i = 0; i<h; i++){
		for(int j = 0; j < w; j++){
			cout << a[i][j];
		}
		cout << endl;
	}
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14 100兆
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

int main(){
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  bool t = true;
  while(A > 0 && C > 0){
    if(t){
      C -= B;
      t = false;
    }
    else{
      A -= D;
      t = true;
    }
  }
  if(A > 0){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}

Measured memory_bytes: 3600
//
// Created by 伊東麻沙美 on 2019/10/22.
//

#include<iostream>
#include<algorithm>
using namespace std;


int main(){

    int a;
    string s;
    cin>>a>>s;

    if(a>=3200){
        cout<<s<<endl;
    }else{
        std::cout<<"red"<<endl;
    }

    //cin>>a>>b>>x;
    //scanf("%d%s%d",&i,&x,&j);


       // printf("%d",i+j);
       //cout<<i+j<<endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>

int main(void)
{
  using namespace std;
  long R, G, B, N;
  cin >> R >> G >> B >> N;
  long ans = 0;
  for (int r = 0; r <= N/R+1; r++) {
    for (int g = 0;  g <= N/G + 1; g++) {
      long b = N - r*R - g*G;
      b = long(b / B);
      if ( b < 0 ) continue;
      if (r*R + g*G + b*B == N)
        ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll mo = 1000000007;
const ll INF = 1LL << 60; //MAX 9223372036854775807
ll ans = 0;
vector< int > z_algorithm(const string &s) {
  vector< int > prefix(s.size());
  for(int i = 1, j = 0; i < s.size(); i++) {
    if(i + prefix[i - j] < j + prefix[j]) {
      prefix[i] = prefix[i - j];
    } else {
      int k = max(0, j + prefix[j] - i);
      while(i + k < s.size() && s[k] == s[i + k]) ++k;
      prefix[i] = k;
      j = i;
    }
  }
  prefix[0] = (int) s.size();
  return prefix;
}

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

string ss=s;
ll num=0,num2=0;
while(ss.size()<t.size()){
  ss=ss+ss;
}
ss=ss+ss;
ss=ss+ss;
{vector< int > v=z_algorithm(t+'|'+ss);
ll te=0;
assert(v.size()==t.size()+ss.size()+1);
ll i=t.size();
bool see[v.size()]={};
while(i<t.size()+ss.size()+1){
  if(see[i]){i++;continue;}
  if(v[i]==t.size()){
    ll j=i;
    while(j<t.size()+ss.size()+1 && v[j]==t.size()){te++;see[j]=1;j+=t.size();num=max(num,te);}
    i++;
  }
  else {te=0;see[i]=1;i++;}
}
}

ss+=ss;
{vector< int > v=z_algorithm(t+'|'+ss);
ll te=0;
assert(v.size()==t.size()+ss.size()+1);
ll i=t.size();
bool see[v.size()]={};
while(i<t.size()+ss.size()+1){
  if(see[i]){i++;continue;}
  if(v[i]==t.size()){
    ll j=i;
    while(j<t.size()+ss.size()+1 && v[j]==t.size()){te++;see[j]=1;j+=t.size();num2=max(num2,te);}
    i++;
  }
  else {te=0;see[i]=1;i++;}
}
}

if(num==num2)cout << num << endl;
else cout << -1 << endl;

return 0;
}


Measured memory_bytes: 59764
#include<stdio.h>
#include<math.h>

#define N 100000

int main(){
  int a,b[N],i,j,count=0;

  scanf("%d",&a);

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

  
  for(j=0;j<a;j++){
    if(b[j]==2){
      count++;
      continue;
    }
    if(b[j]<2||b[j]%2==0) continue;

    i=3;
    while(i<=sqrt(b[j])){
      if(b[j]%i==0) {
	i+=2;
	count--;
	break;
      }
    

      else {
	i+=2;
	
      }
    }
    count++;
  }
  printf("%d\n",count);
  
  return 0;
}





Measured memory_bytes: 2044
//ええなにそれ・・・割り算ってllよりdoubleの方が速いとでも？
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}
inline ll getll() {
	ll A = 0;
	if (ci - cn + 24 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}

int tmp[200001];
void pakuri_sort(int N, int A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}
void pakuri_sort2(int N, int A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		for (int i = (1 << b) - 2; i >= 0; i--) kazu[i] += kazu[i + 1];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		for (int i = (1 << b) - 2; i >= 0; i--) kazu2[i] += kazu2[i + 1];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}

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


	ll N = getint(), K = getll();
	int A[200000], F[200000];
	rep(i, N) A[i] = getint();
	rep(i, N) F[i] = getint();
	pakuri_sort(N, A);
	pakuri_sort2(N, F);

	ll L = -1, R = 1e12;
	while (L + 1 < R) {
		ll M = (L + R) / 2;

		ll kazu = 0;
		rep(i, N) {
			kazu += max(0, A[i] - (int)((double)M / F[i]));
			if (kazu > K) break;
		}
		if (kazu > K) L = M;
		else R = M;
	}

	printf("%lld", R);


	Would you please return 0;
}

Measured memory_bytes: 2688
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(){

    int N;
    cin >> N;

    vector <int> n;
    map <int, int> m;

    for (int i = 0; i < N; i++){
        int a;
        cin >> a;
        if (m[a] == 0){
            n.push_back(a);
        }
        m[a] ++;
    }

    int ans = 0;
    for (int i = 0; i < n.size(); i++){
        int a = n[i];
        if (m[a] > a){
            ans += m[a] - a;
        }else if(m[a] < a){
            ans += m[a];
        }
    }
    
    cout << ans << endl;
    

    return 0;
}

Measured memory_bytes: 5112
#include <stdio.h>

int main(void)
{
	int N, i, j, v, k;
	int A[99];
	scanf("%d", &N);
	for(i=0; i<N; i++){
		scanf("%d", &A[i]);
	}
	
	for(i=0;i<N-1;i++){
		printf("%d ",A[i]);
	}
	printf("%d\n", A[N-1]);
	
	for(i=1; i<=N-1; i++){
		v=A[i];
		j=i-1;
		
		while (j>=0 && A[j]>v){
			A[j+1]=A[j];
			j--;
		
			A[j+1]=v;
		}
			for(k=0; k<N-1; k++){
				printf("%d ",A[k]);
			}
			printf("%d\n", A[N-1]);
	}
	
		
	return 0;
}


Measured memory_bytes: 2040
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        String str = sc. nextLine();
        
        char []str_t_char = str.toCharArray();
        char []ACGT = {'A', 'C', 'G', 'T'};
        
        int ans = 0;
        
        for (int i = 0 ; i < str_t_char.length; i++){
            int count = 0;
            for (int iter = i; iter < str_t_char.length; iter++){
                boolean flag = false;
                for (int k = 0; k < ACGT.length; k++){
                    if (str_t_char[iter] == ACGT[k]){
                        flag = true;
                        break;
                    } 
                }
                
                if (flag){
                    count++;
                    ans = Math.max(ans, count);
                }else {
                    break;
                }
            } 
        }
        System.out.println(ans);
    }
}


Measured memory_bytes: 22740
import std.stdio, std.string, std.array, std.algorithm, std.conv, std.typecons, std.numeric, std.math;

alias R = Tuple!(long, "x1", long, "y1", long, "x2", long, "y2");

void main()
{
    auto N = readln.chomp.to!int;
    R[] rs;
    long[] xs, ys;
    foreach (_; 0..N) {
        auto xy = readln.split.to!(long[]);
        xs ~= xy[0];
        xs ~= xy[2];
        ys ~= xy[1];
        ys ~= xy[3];
        rs ~= R(xy[0], xy[1], xy[2], xy[3]);
    }
    sort(xs);
    xs = xs.uniq().array();
    sort(ys);
    ys = ys.uniq().array();
    long[long] xm, ym;
    foreach (i, x; xs) xm[x] = i.to!long;
    foreach (i, y; ys) ym[y] = i.to!long;
    foreach (ref r; rs) {
        r.x1 = xm[r.x1];
        r.y1 = ym[r.y1];
        r.x2 = xm[r.x2];
        r.y2 = ym[r.y2];
    }
    auto MAP = new int[][](ys.length, xs.length);
    foreach (r; rs) {
        MAP[r.y1][r.x1] += 1;
        MAP[r.y2][r.x2] += 1;
        MAP[r.y1][r.x2] -= 1;
        MAP[r.y2][r.x1] -= 1;
    }
    foreach (i; 0..ys.length) {
        foreach (j; 0..xs.length-1) {
            MAP[i][j+1] += MAP[i][j];
        }
    }
    foreach (j; 0..xs.length) {
        foreach (i; 0..ys.length-1) {
            MAP[i+1][j] += MAP[i][j];
        }
    }
    long r;
    foreach (i; 0..ys.length-1) {
        foreach (j; 0..xs.length-1) {
            if (MAP[i][j] > 0) {
                r += (ys[i+1] - ys[i]) * (xs[j+1] - xs[j]);
            }
        }
    }
    writeln(r);
}


Measured memory_bytes: 67844
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>

using namespace std;
int main(void)
{
	int n,sum,A;
	cin>>n;
	sum=n*800;
	A=n/15;
	A*=200;
	cout<<sum-A<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
long mod = 1e9+7;
int main(){
    long n,ans,j=0,flag=0;
    cin >> n;
    char a[n];
    rep(i,n) cin >> a[i];
    if(a[0]==a[1]){
        ans = 6;
        j = 2;
        flag = 0;
    } 
    else{
        ans = 3;
        j = 1;
        flag = 1;
    }
    for(int i=j;i<n;i++){
        if(i<n&&a[i]==a[i+1]){
            if(flag==0) ans *= 3;
            else ans *= 2;
            flag = 0;
            i++;
        }
        else{
            if(flag==1) ans *= 2;
            flag = 1;
        } 
        ans %= mod;
    }
    cout << ans << endl;
}

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

using namespace std;

int N,K;

int board[2005][2005];

int getBlackNum(int x1,int y1,int x2,int y2)
{
	return board[x2][y2] - board[x1][y2] - board[x2][y1] + board[x1][y1];
}

int main()
{
	scanf("%d %d",&N,&K);
	char ch;
	int x,y;
	for(int i=0 ; i<N ; i++)
	{
		scanf("%d %d %c",&x,&y,&ch);
		if(ch == 'W')x += K;
		x %= K*2;
		y %= K*2;
		board[x+1][y+1]++;
	}
	for(int i=1 ; i<=2*K ; i++)
	{
		for(int j=1 ; j<=2*K ; j++)
		{
			board[i][j] += board[i-1][j] + board[i][j-1] - board[i-1][j-1];
		}
	}
	int Max = 0;
	for(int i=1; i<=K ; i++)
	{
		for(int j=1 ; j<=K ; j++)
		{
			int mid = getBlackNum(0,0,i,j) + getBlackNum(0,j+K,i,2*K) 
					+ getBlackNum(i+K,0,2*K,j) + getBlackNum(i+K,j+K,2*K,2*K)
					+ getBlackNum(i,j,i+K,j+K);
			Max = max(Max,max(mid,N-mid));
		}
	}
	printf("%d\n",Max);
	return 0;
}

Measured memory_bytes: 15872
import queue

N = int(input())
H = list(map(int, input().split()))

q = queue.Queue()
q.put([H,0])

num = 0
while not q.empty():
    data = q.get()
    if len(data[0]) == 0:
        continue
    if len(data[0]) == 1:
        num += data[0][0]-data[1]
        continue
    min_index = data[0].index(min(data[0]))
    num += data[0][min_index]-data[1]
    tmp = data[0][min_index]

    tmp_data = []
    for d in data[0]:
        if d == data[0][min_index]:
            q.put([tmp_data,tmp])
            tmp_data = []
        else:
            tmp_data.append(d)
    q.put([tmp_data,tmp])

print(num)

Measured memory_bytes: 4208
function Main(input) {
  input = input.split("\n");
  const n = parseInt(input[0].split(" ")[0], 10);
  const A = input[1].split(" ").map(a => parseInt(a, 10));
  const B = input[2].split(" ").map(a => parseInt(a, 10));

  var count = 0;
  for (var i = 0; i < n; i++) {
    if (A[i] >= B[i]) {
      count += B[i];
      // console.log("a")
    } else {
      if ( A[i] - B[i] + A[i+1] > 0) {
        count += B[i];
        A[i+1] -= B[i] - A[i];
      // console.log("b")
      } else {
        count += A[i] + A[i+1];
        A[i+1] = 0;
      // console.log("c")
      }
    }
    // console.log(count)
  }
  console.log(count);

}


// *出力
// number
// console.log('%d', );
// string
// console.log('%s', );

Main(require("fs").readFileSync("/dev/stdin", "utf8"));


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

int main(){
  int N;
  string S;
  cin>>N>>S;
  vector<int> count(N+1,0);
  count.at(0)=0;
  for(int i=1;i<=N;i++){
    if(S.at(i-1)=='W'){
      count.at(i)++;
    }
    count.at(i)+=count.at(i-1);
  }

  int ans=N;
  for(int i=1;i<=N;i++){
    ans=min(ans,count.at(i-1)+(N-i)-(count.at(N)-count.at(i)));
  }
  cout<<ans<<endl;

}


Measured memory_bytes: 1796
#include<stdio.h>
#define BLACK 5
#define GRAY 4
#define WHITE 3
void visit(int);
int d[1000],f[1000],color[1000],time=0,n;
int a[1000][1000];
int main(){
  int i,j,b,num,st,u;
  scanf("%d",&n);

  for(i=1;i<=n;i++){
    for(j=1;j<=n;j++){
      a[i][j]=0;
    }
  }
  for(i=0;i<n;i++){
    scanf("%d %d",&num,&st);
    for(j=0;j<st;j++){
      scanf("%d",&b);
      a[num][b]=1;
    }
  }
  for(u=0;u<n;u++){
    color[u] =  WHITE;
  }
  time = 0;
  for(u=0;u<n;u++){
    if(color[u] == WHITE)
      visit(u);
  }
  for(i=0;i<n;i++){
    printf("%d %d %d\n",i+1,d[i],f[i]);
  }
  return 0;
}
void visit(int u){
  int v;
  color[u] = GRAY; // White vertex u has just been discovered                  
  d[u] = ++time;
  for(v=0;v<=n;v++){ // Explore edge (u,v)                                     
    if(a[u+1][v+1]==1){
      if(color[v] == WHITE){
        visit(v);
      }
    }
  }
  color[u] = BLACK; // Blacken u; it is finished                               
  f[u] = ++time;
}

Measured memory_bytes: 992
from collections import deque
import sys
input = sys.stdin.readline

ldyx = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]


while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    MAP = [list(map(int, input().split())) for _ in range(h)]
    D = [[-1 for _ in range(w)] for _ in range(h)]    

    count = 1
    for j in range(h):
        for i in range(w):
            if D[j][i] == -1 and MAP[j][i] == 1:
                D[j][i] = count
                queue = deque([(j, i)])

                while queue:
                    (ny, nx) = queue.pop()
                    d = D[ny][nx]

                    # all neighbors
                    for (dy, dx) in ldyx:
                        new_y, new_x = ny + dy, nx + dx
                        if 0 <= new_x < w and 0 <= new_y < h:
                            if MAP[new_y][new_x] == 1:
                                if D[new_y][new_x] > -1:
                                    continue
                                D[new_y][new_x] = d
                                queue.append((new_y, new_x))

                # increase island number
                count += 1

    mv = 0
    for row in D:
        mv = max(mv, max(row))
    print(mv)


Measured memory_bytes: 6172
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cout << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int long long

void solve() {
	string s; int k;
    cin >> s >> k;
    int n = (int)s.size();
    int i = 0;
    while(k > 0 && i < n) {
        if(i == n - 1) {
            k %= 26;
            int now = s[i] - 'a';
            (now += k) %= 26;
            s[i] = 'a' + now;
            k = 0;
            break;
        }
        else if(s[i] == 'a') {++i; continue;}
        else {
            int c = 26 - (s[i] - 'a');
            if(c <= k) {
                k -= c;
                s[i] = 'a';
            }
            ++ i;
        }
    }
    cout << s << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init();
	solve();
	//cout << "finish" << endl;
    return 0;
}

Measured memory_bytes: 592
#include <inttypes.h>
#include <stdio.h>

#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long llong;

static int popcnt(uint64_t x) {
  int count = 0;
  for (int i = 0; i < 64; ++i) {
    if (x & 1)
      ++count;
    x >>= 1;
  }
  return count;
}

void CHonestOrUnkind2(std::istream& in, std::ostream& out) {
  int n;
  in >> n;

  vector<pair<int, int>> v[15];
  for (int i = 0; i < n; ++i) {
    int a;
    in >> a;
    int x, y;
    for (int j = 0; j < a; ++j) {
      in >> x >> y;
      v[i].push_back({x - 1, y});
    }
  }

  int max_honest = 0;
  uint64_t bits = 1U << n;
  // test all combinations
  for (uint64_t b = 1; b < bits; ++b) {
    // iterate check for each person
    bool found_contra = false;
    for (int i = 0; !found_contra && i < n; ++i) {
      if (b & (1U << i)) {
        // test testimonies
        for (int j = 0; j < v[i].size(); ++j) {
          const int y = v[i][j].second;
          const int x = v[i][j].first;
          if ((y && !(b & (1U << x))) ||
              (!y && (b & (1U << x)))) {
            found_contra = true;
            break;
          }
        }
      }
    }
    if (found_contra == false)
      max_honest = max(max_honest, popcnt(b));
  }
  cout << max_honest << endl;
}

int main(int argc, char** argv) { CHonestOrUnkind2(cin, cout); return 0; }


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

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define repf(i, f, l) for(int i = f; i < (int)l; ++i)
#define repit(it, t) for(__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)
#define endl "\n"
#define pb emplace_back
#define lb lower_bound
#define int long long
#define ul unsigned long
#define ull unsigned long long
#define ll long long
#define INF 1000000007
#define MOD 1000000007
#define fs first
#define sd second
#define DBG0(x) { cout << #x << ": " << x << "\t"; }
#define DBG(x) {DBG0(x); cout << endl;}
#define DBG2(x, y) {DBG0(x); DBG(y);}
#define DBG3(x, y, z) {DBG0(x); DBG2(y, z);}
#define DBG4(w, x, y, z) {DBG0(w); DBG3(x, y, z);}

typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef vector<bool> vbl;
typedef pair<int, int> pii;

signed main(void){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int last = 0;
    int count = 0;
    int ans = 0;
    rep(i, n) {
        int c;
        cin >> c;
        if (c == last) {
            count++;
        } else {
            ans += count / 2;
            count = 1;
        }
        last = c;
    }
    cout << ans + count / 2 << endl;
    return 0;

}


Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
using namespace std;

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

int main() {
  int n, a, b; scanf("%d%d%d", &n, &a, &b);
  printf("%d %d\n", min<int>(a, b), max<int>(a+b-n, 0));
  return 0;
}


Measured memory_bytes: 128
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,unsafe-math-optimizations")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#include <assert.h>
#include <immintrin.h>
#include <emmintrin.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD=1000000007;
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;
#ifdef ONLINE_JUDGE
#define assert(...) /* nothing */
#endif
#define DEBUG_CAT
#ifdef DEBUG_CAT
#define dbg(...)   printf( __VA_ARGS__ )
#else 
#define dbg(...)   /****nothing****/
#endif
const int mn=701;
bitset<mn> g[mn];
int a[mn];
queue<int> q[2];
void fail() {
	printf("-1\n"); exit(0);
}
int c[2];
void psh(int x, int t) {
	//printf("x:%d assigned t:%d\n",x,t);
	if (a[x]!=-1) {
		if (a[x]!=t) fail();
		return;
	}
	a[x]=t;
	c[t]++;
	q[t].push(x);
}
ll c2(ll x) {
	return (x*(x-1))/2;
}
bitset<mn> knap[2];
int main()
{
  int n,m; scanf("%d%d",&n,&m);
  for (int i=0;i<m;i++) {
  	int x,y; scanf("%d%d",&x,&y); --x;--y;
  	g[x][y]=true; g[y][x]=true;
  }
  memset(a,-1,sizeof a);
  int cur=0,nxt=1;
  knap[cur][0]=true;
  for (int init=0;init<n;init++) {
  	if (a[init]!=-1) continue;
  	memset(c,0,sizeof c);
	  psh(init,0);
	  while(!(q[0].empty()&&q[1].empty())) {
	  	for (int t=0;t<2;t++) {
		  	if (!q[t].empty()) {
		  		int x=q[t].front(); q[t].pop();
		  		for (int y=0;y<n;y++) {
		  			if (y!=x&&!g[x][y]) {
		  				psh(y,t^1);
		  			}
		  		}
		  	}
		  }
	  }
	  knap[nxt]=knap[cur]<<c[0];
	  knap[nxt]|=knap[cur]<<c[1];
	  swap(cur,nxt);
	}
	ll ans=1e9;
	for (int x=0;x<=n;x++) {
		if (knap[cur][x]) {
			ll cand=c2(x)+c2(n-x);
			chkmin(ans,cand);
		}
	}
  printf("%lld\n",ans);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i , n) for (int i = 0; i < (int)(n); i++)
const int INF = 1<<29;
using namespace std;
using ll = long long;
int main(){
    ll a , b , x;
    cin >> a >> b >> x;
    if(a % x == 0){
        cout << b / x - a / x + 1 << endl;;
    }
    else{
        cout << b / x - a / x << endl;;
    }
}


Measured memory_bytes: 3620
using System;
using System.Collections.Generic;

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {
            int A = int.Parse(Console.ReadLine());
            int B = int.Parse(Console.ReadLine());

            var list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);

            list.Remove(A);
            list.Remove(B);

            foreach(int i in list)
            {
                Console.WriteLine(i);
            }

        }
    }
}


Measured memory_bytes: 13152
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();

        int[] A = new int[N+1];
        int[] B = new int[N];

        for(int i=0; i<N+1; i++){
            A[i] = sc.nextInt();
        }

        for(int i=0; i<N; i++){
            B[i] = sc.nextInt();
        }

        long destroyed = 0;

        for(int i=0; i<N; i++){
            int j = N-1 - i;

            if(i > j){
                break;
            }

            if(i == j){
                destroyed += destroy1(A, B, i);
                break;
            }

            destroyed += destroy1(A, B, i);
            destroyed += destroy2(A, B, j);
        }

        System.out.println(destroyed);

    }

    public static int destroy1(int[] A, int[] B, int i){
        int destroyed = 0;

        int killable = B[i];
        if(A[i] < killable){
            destroyed += A[i];
            killable -= A[i];
            A[i] = 0;
            if(A[i+1] < killable){
                destroyed += A[i+1];
                A[i+1] = 0;
            }else{
                destroyed += killable;
                A[i+1] -= killable;
            }
        }else {
            destroyed += killable;
            A[i] -= killable;
        }

        return destroyed;
    }

    public static int destroy2(int[] A, int[] B, int i){
        int destroyed = 0;

        int killable = B[i];
        if(A[i+1] < killable){
            destroyed += A[i+1];
            killable -= A[i+1];
            A[i+1] = 0;
            if(A[i] < killable){
                destroyed += A[i];
                A[i] = 0;
            }else{
                destroyed += killable;
                A[i] -= killable;
            }
        }else {
            destroyed += killable;
            A[i+1] -= killable;
        }

        return destroyed;
    }
}

Measured memory_bytes: 87112
import sys
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
if a[1]-a[0]==b[1]-b[0]==c[1]-c[0]:
    if a[1]-a[2]==b[1]-b[2]==c[1]-c[2]:
        print("Yes")
        sys.exit()
print("No")



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

using namespace std;

const int INF = 1e9 + 9;

int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int N, K;
  cin >> N >> K;
  vector < int > a(N), dp(N, INF);
  for (auto &x : a)
    cin >> x;
  dp[0] = 0;
  for (int i = 0; i < N; ++i)
    for (int j = i + 1; j <= i + K && j < N; ++j)
        dp[j] = min (dp[j], dp[i] + abs (a[i] - a[j]));
  cout << dp[N - 1];
}


Measured memory_bytes: 3900
#include <iostream>
#include <cstdlib>
using namespace std;
string str;
int K;
int l=0;
void solve(){
	if(str[l] != '1'){
		cout << str[l] << endl;
	} else if(l < K-1){
		l++;
		solve();
	} else{
		cout << str[l] << endl;
	}
}
int main()
{
	cin >> str;
	cin >> K;
	solve();
}


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

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)

class UnionFind {
public:
    vector <int> par;
    vector <int> siz;

    UnionFind(int sz_): par(sz_), siz(sz_, 1LL) {
        for (int i = 0; i < sz_; ++i) par[i] = i;
    }

    void init(int sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (int i = 0; i < sz_; ++i) par[i] = i;
    }

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

    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;

        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

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

signed main() {
    int N, K, L;

    cin >> N >> K >> L;
    UnionFind road(N);
    UnionFind train(N);

    REP(i, K) {
        int p, q;
        cin >> p >> q;
        p--; q--;
        road.merge(p, q);
    }

    REP(i, L) {
        int r, s;
        cin >> r >> s;
        r--; s--;
        train.merge(r, s);
    }

    vector<pair<int, int>> l;
    REP(i, N) {
        l.push_back(make_pair(road.root(i), train.root(i)));
    }
    sort(ALL(l));

    REP(i, N) {
        int ans = 0;
        pair<int, int> p = make_pair(road.root(i), train.root(i));

        vector<pair<int, int>>::iterator left = lower_bound(ALL(l), p);
        vector<pair<int, int>>::iterator right = upper_bound(ALL(l), p);

        ans = distance(left, right);
        cout << ans << " ";
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 5492
n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    if l[i] == l[j]:
      continue
    for k in range(j+1,n):
      if l[i] == l[k] or l[j] == l[k]:
        continue
      if l[i]+l[j] > l[k] and l[j]+l[k] > l[i] and l[k]+l[i] > l[j]:
        ans += 1
print(ans)

Measured memory_bytes: 9228
#include <iostream>
#include <iomanip>
#include <climits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <math.h>
#include <complex>

using namespace std;

#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e15
#define _INFLL (long long)-1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef complex<double> cdouble;
typedef vector<complex<double>> vcd;
typedef vector<vector<complex<double>>> vvcd;

/*******************************************************/

int main() {
  string s; cin >> s;
  int min_k = s.length();
  Loop(i, s.length() - 1) {
    if (s[i] != s[i + 1]) {
      int k = max((int)s.length() - 1 - i, i + 1);
      min_k = min(k, min_k);
    }
  }
  cout << min_k << endl;
}

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

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { std::cout << #a << " = " << a << "\n"; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
template<class T> istream& operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) ist >> e; return ist; }
template<class T> istream& operator<< (istream& ist, pair<T, T>& p) { return ist >> p.first >> p.second; }
template<class T> ostream& operator<< (ostream& ost, pair<T, T>& p) { return ost << p.first << ", " << p.second; }

typedef long long ll;
int64_t const inf = std::numeric_limits<int64_t>::max();

using P = pair<int64_t, int64_t>;

int N, K;

auto in_left_cornered_rect(int x, int y, int lx, int ly) {
  return lx <= x && ly <= y;
}

auto count_points_in_rect(vector<P> const& vs, map<P, int>& counts, int i, int j) {
  assert(vs[i].first < vs[j].first && vs[i].second < vs[j].second);
  auto lt = vs[i];
  auto rt = P{vs[j].first, vs[i].second};
  auto lb = P{vs[i].first, vs[j].second};
  auto rb = vs[j];
  if (counts.find(rt) == counts.end()) return 0;
  if (counts.find(lb) == counts.end()) return 0;
  return counts[lt] - counts[rt] - counts[lb] + counts[rb];
}

int main() {
  cin >> N >> K;
  vector<P> in;
  rep(i, N) {
    int x, y; cin >> x >> y;
    in.emplace_back(x, y);
  }

  // 点の座標集合の直積
  vector<P> vs = in;
  rep(i, N) rep(j, N) {
    vs.emplace_back(min(in[i].first, in[j].first),     min(in[i].second, in[j].second));
    vs.emplace_back(max(in[i].first, in[j].first) + 1, min(in[i].second, in[j].second));
    vs.emplace_back(min(in[i].first, in[j].first),     max(in[i].second, in[j].second) + 1);
    vs.emplace_back(max(in[i].first, in[j].first) + 1, max(in[i].second, in[j].second) + 1);
  }
  sort(vs.begin(), vs.end());
  vs.erase(unique(vs.begin(), vs.end()), vs.end());

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

  map<P, int> counts;
  for (auto const& corner: vs) {
    rep(i, N) {
      counts[corner] += !!in_left_cornered_rect(in[i].first, in[i].second, corner.first, corner.second);
    }
  }

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

  int64_t min_rect = inf;
  rep(i, vs.size()) rep(j, vs.size()) {
    if (!(vs[i].first < vs[j].first && vs[i].second < vs[j].second)) continue;
    if (count_points_in_rect(vs, counts, i, j) >= K) {
      auto W = vs[j].first - vs[i].first - 1;
      assert(W >= 0);
      auto H = vs[j].second - vs[i].second - 1;
      assert(H >= 0);
      minimize(min_rect, (int64_t)W * H);
    }
  }
  cout << min_rect << "\n";
}


Measured memory_bytes: 576
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"

using ll=long long;
using vi=std::vector<int>;
using vvi=std::vector<vi>;
using vll=std::vector<ll>;
using vvll=std::vector<vll>;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}

template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
  s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}

inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
template<typename T>
inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}

using namespace std;

int main() {
  int n_a, n_b, m;
  scan(n_a, n_b, m);
  vi a(n_a);
  scan(a);
  vi b(n_b);
  scan(b);
  int min_a = *min_element(all(a));
  int min_b = *min_element(all(b));
  int ans = min_a + min_b;
  rep (i, m) {
    int x, y, c;
    scan(x, y, c);
    --x; --y;
    ans = min(ans, a[x] + b[y] - c);
  }
  print(ans);
  return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/* clang-format off */
#define MOD 1000000007
#define INF 1000000000
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define __DEBUG__
#ifdef __DEBUG__
#define CH_P(a) cout <<"check_point("<<#a<<")" << "\n";
#define DEBUG(x) cout<<#x<<":"<<x<<"\n"
#define DEBUGS(v) cout << #v << ":";for(auto x:v){cout<<x<<" ";}cout<<"\n"
#endif
#ifndef __DEBUG__
#define CH_P(a) 
#define DEBUG(x) 
#define DEBUGS(v) 
#endif
#define TIMER_S(start) chrono::system_clock::time_point start = chrono::system_clock::now();
#define TIMER_E(end) chrono::system_clock::time_point end = chrono::system_clock::now();
#define TIME(start, end) cout << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count()) << "ms" << "\n";
/* clang-format on */

ll d;
vector<ll> c(26);
vector<vector<ll>> s(365, vector<ll>(26));
vector<ll> t(365);
vector<ll> last(26, -1);

ll score(ll type, ll day, ll sc) {
  ll s_now = sc + s[day][type];
  REP(i, 26) { s_now -= c[i] * (day - last[i]); }
  return s_now;
}

ll sim(vector<ll> yotei) {
  ll ss = 0;
  REP(i, d) {
    last[yotei[i]] = i;
    ss = score(yotei[i], i, ss);
  }
  return ss;
}

int main() {
  cin >> d;
  REP(i, 26) { cin >> c[i]; }
  REP(i, d) {
    REP(j, 26) { cin >> s[i][j]; }
  }

  REP(i, d) {
    ll tmp = 0, m = 0;
    REP(j, 26) {
      if (m < s[i][j]) {
        m = s[i][j];
        tmp = j;
      }
    }
    t[i] = tmp;
  }

  REP(j, 300) {
    REP(i, d) {
      ll ss = sim(t), tmp = t[i];
      t[i] = rand() % 26;
      if (sim(t) < ss) t[i] = tmp;
    }
  }
  // DEBUGS(t);
  // cout << sim(t) << "\n";
  REP(i, d) { cout << t[i]+1 << "\n"; }
  return 0;
}


Measured memory_bytes: 3680
N, M = map(int, input().split())
L = [list(map(int, input().split())) for i in range(M)]

l0 = L[0][0]
l1 = L[0][1]
for i in L:
    if l0 < i[0]:
        l0 = i[0]

    if l1 > i[1]:
        l1 = i[1]

if l1 < l0:
    print(0)
else:
    print(l1 - l0 + 1)


Measured memory_bytes: 27344
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma GCC optimize(2)
#define LL long long
using namespace std;
int n,k,ls;LL ans;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		while(ls<i*2)ls+=k;int val=ls-i,tot;
		tot=(n-val)/k+1;if(val*2%k)continue;
		if(val!=i)ans+=1ll*tot*3+1ll*(tot-1)*tot/2*6;
		else ans+=1ll+1ll*(tot-1)*2*3+1ll*(tot-1)*(tot-2)/2*6;
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 256
h=int(input())
#2^x<=h<2^(x+1)の時、攻撃する回数は2^0+2^1+...+2^xとなる
#xを求める
x=0
while True:
    if 2**x<=h and h<2**(x+1):
        break
    x+=1
#初項1,公比2,項数x+1の等比数列の和を求めたら終わる
print(2**(x+1)-1)
        

Measured memory_bytes: 9104
/*
　　　  ∧＿∧　やあ
　　 （´・ω・｀)　　　　　/　　　　　ようこそ、バーボンハウスへ。
　　 ／∇y:::::＼　　　 [￣]　　　　　このテキーラはサービスだから、まず飲んで落ち着いて欲しい。
　　 |:⊃:|:::::|　　　|──|
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|　うん、「また」なんだ。済まない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|￣　  仏の顔もって言うしね、謝って許してもらおうとも思っていない。
￣￣￣￣￣￣￣￣￣￣￣￣￣￣／|
　　　　∇　∇　∇　∇　　　／.／|　　　でも、この提出を見たとき、君は、きっと言葉では言い表せない
　　　　┴　┴　┴　┴　／ ／　  |　　　「ときめき」みたいなものを感じてくれたと思う。
￣￣￣￣￣￣￣￣￣￣|／　　  |　　　殺伐としたコンテストの中で、そういう気持ちを忘れないで欲しい
￣￣￣￣￣￣￣￣￣￣　　　　 |　　　そう思って、この提出を投げたんだ。
　　　(⊆⊇)　(⊆⊇)　(⊆⊇)　　|
　    ||　　 ||　　||　　|　　　 じゃあ、判定を聞こうか。
　　.／|＼　／|＼ ／|＼
*/

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <cmath>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define fst first
#define snd second
#define ALL(obj) (obj).begin(),(obj).end()
#define debug(x) cerr << #x << " -> " << x << " (line:" << __LINE__ << ")" << '\n';
#define debugpair(x, y) cerr << "(" << #x << ", " << #y << ") -> (" << x << ", " << y << ") (line:" << __LINE__ << ")" << '\n';
typedef long long lint;
typedef priority_queue<lint> p_que;
typedef priority_queue<lint, vector<lint>, greater<lint>()> p_que_rev;
const lint INF = INT_MAX;
const lint LINF = LLONG_MAX;
const lint MOD = 1000000000 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
const lint di[]{0, -1, 0, 1, -1, -1, 1, 1};
const lint dj[]{1, 0, -1, 0, 1, -1, -1, 1};

struct UnionFind
{
    vector<int> data;
    UnionFind() {}

    // initialize
    UnionFind(int size) : data(size, -1) {}

    void unite_set(int x, int y){
        x = root(x);
        y = root(y);
        bool is_unite = (x != y);
        
        if (is_unite){
            if (data[y] < data[x]){
                swap(x, y);
            }
            data[x] += data[y];
            data[y] = x;
        }
    }

    bool is_same(int x, int y){
        return root(x) == root(y);
    }

    int root(int x){
        return (data[x] < 0) ? x : data[x] = root(data[x]);
    }

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

int main()
{
    int n, q;
    int c, a, b;
    cin >> n >> q;
    struct UnionFind d(n);
    vector<int> data;
    // query
    for (int i=0; i<q; ++i){
        cin >> c >> a >> b;
        // same
        if(c == 1){
            cout << d.is_same(a, b) << endl;
        }
        // unite
        else {
            d.unite_set(a, b);
        }
    }
}


Measured memory_bytes: 3088
# ducational DP Contest B-Frog 2
    
N, K = map(int, input().split())
h = list(map(int, input().split())) + [0]*K

dp = [float('inf') for _ in range(N+K)]
dp[0] = 0

for i in range(N):
    for j in range(1, K+1):
        dp[i+j] = min(dp[i+j], dp[i] + abs(h[i+j] - h[i]))
        
print(dp[N-1])

Measured memory_bytes: 57568