akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <iostream> //入出力
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <math.h> //算術演算子
#include <vector>
#include <cstdlib>
#include <queue>
#include <stack>
#include <list>
#include <iomanip> //小数点以下を表示させる(setprecision())
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll; //64bit型

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define debug(x) cerr << x << " " \
                      << "(L:" << __LINE__ << ")" << '\n'
#define PRINT(V)                                   \
        {                                          \
                for (int i = 0; i < V.size(); i++) \
                {                                  \
                        cout << V[i] << " ";       \
                }                                  \
                cout << endl;                      \
        }

int gcd(int a, int b)
{
        return b ? gcd(b, a % b) : a;
}
string trans(int N)
{
        string S;
        while (N != 0)
        {
                if (N % 2 == 1)
                {
                        N--;
                        S = '1' + S;
                }
                else
                {
                        S = '0' + S;
                }
                N /= 2;
        }
        return S;
}

int main() //input->compute->output
{
        string S;
        cin >> S;

        for (int i = S.size() - 1; i >= 2; i--)
        {
                string M = S.substr(0, i);
                if (M.size() % 2 == 1)
                {
                        continue;
                }
                else
                {
                        string H = M.substr(0, M.size() / 2);
                        if (M == H + H)
                        {
                                cout << M.size() << endl;
                                break;
                        }
                }
        }

        return 0;
}


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

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

const int N = 1e5 + 1;

int p[N];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", p + i);
    }
    int res = 0;
    int i = 0;
    while (i < n) {
        int c = 0;
        while (i < n && p[i] != i + 1) ++i;
        while (i < n && p[i] == i + 1) {
            ++i;
            ++c;
        };
        res += (c + 1) >> 1;
    }
    printf("%d\n", res);
    return 0;
}


Measured memory_bytes: 512
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial
sys.setrecursionlimit(200000)
input = sys.stdin.readline

A, B, C, D = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

if B <= C:
    print(0)
    exit()

B = min(B, D)
C = max(A, C)

print(max(0, B - C))

Measured memory_bytes: 3316
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<bitset>
#include<queue>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) FOR(i,0,n)
typedef long long ll;
int main(){
	int cnt =0;
	int n;
	char k;
	cin >> n;
	int a[4] = {};
	rep(i,n){
		cin >> k;
		if (k=='P') a[0]++;
		else if (k=='W') a[1]++;
		else if (k=='G') a[2]++;
		else a[3]++;
	}
	rep(i,4){
		if (a[i]) cnt++;
	}
	if (cnt == 4) cout << "Four" << endl;
	else cout << "Three" << endl;

	return 0;

}


Measured memory_bytes: 256
N,M=list(map(int,input().split()))
g=[[] for _ in range(N+1)]#部屋mにつながっている部屋一覧
for m in range(M):
    a,b=list(map(int,input().split()))
    if a<=N and b<=N:
        g[a].append(b)
        g[b].append(a)
queue=[1]
d=[None]*(N+1)
d[0]=0
d[1]=0
while queue:
    v=queue.pop(0)
    for i in g[v]:
        if d[i] is None:
            d[i]=v
            queue.append(i)
for i in range(N+1):
    if d[i] is None:
        print('No')
        exit()
print('Yes')
for i in range(2,N+1):
    print(d[i])


Measured memory_bytes: 33904
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long int				int32;
typedef unsigned long int		uint32;
typedef long long int			int64;
typedef unsigned long long int  uint64;
typedef pair<int, int>			PII;
typedef vector<int>				VI;
typedef vector<long long>		VLL;
typedef vector<string>			VS;
typedef vector<PII>				VII;
typedef vector<VI>				VVI;

#define MEM(a, b)				memset(a, (b), sizeof(a))
#define calctime				std::cout << setprecision(5) << fixed << "Time to completed program: " << (double)(clock() - start)/(CLOCKS_PER_SEC) << "s.\n";
#define FOR(i, j, k, in)		for (int i = (j); i < (k); i += (in))
#define FORW(i, j, k, in)		for (int i = (j); i <= (k); i += (in))
#define RFOR(i, j, k, in)		for (int i = (j); i >= (k); i -= (in))
#define RFORW(i, j, k, in)		for (int i = (j); i > (k); i -= (in))
#define aint64(cont)			cont.begin(), cont.end()
#define raint64(cont)			cont.end(), cont.begin()
#define sz(cont)				int((cont).size())
#define PB						push_back
#define MP						make_pair
#define EPS						1e-9
#define MOD						1000000007
#define PI						3.1415926535897932384626433832795
#define Ff						first
#define Ss						second
#define FastIO					ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL);
#define fileIO(name)			ifstream iFs(name".inp"); ofstream oFs(name".out");
#define fileio(name)			ifstream cin(name".inp"); ofstream cout(name".out");
#define endl					'\n'

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

int main() {
	FastIO;

	//fileio("input")

	int n, k = 0; cin >> n >> k;
	int64 dp[100000 + 6] = { };
	int a[100000 + 6] = {}; FOR(i, 0, n, 1) cin >> a[i], dp[i] = LLONG_MAX;
	dp[0] = 0;
	FOR(i, 0, n - 1, 1) {
		FORW(j, 1, k, 1) {
			if (i + j > n) break;
			dp[i + j] = min(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
		}
	}

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

	//calctime
	return 0;
}

Measured memory_bytes: 4792
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll,ll>;

int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
string s;
cin >> s;
ll ans = 1145141919810;
bool all_same = true;
rep(i,s.size()){
    if(s[i] != s[0])
        all_same = false;
}
if(all_same){
    cout << 0 << endl;
    return 0;
}
for(ll chara = 97; chara < 123; chara++){
    string tmp = s;
    ll cnt = 0;
    char target = char(chara);
    for(;;) {
        for (ll i = 0; i < tmp.size(); i++) {
            if(i == tmp.size() - 1){
                tmp = tmp.substr(0,tmp.size() - 1);
            }else{
                if(tmp[i] == target || tmp[i + 1] == target)
                    tmp[i] = target;
            }
        }
        cnt++;
        ll good = 0;
        for(ll i = 0; i < tmp.size(); i++){
            if(tmp[i] == target)
                good++;
        }
        if(good == tmp.size()) {
            ans = min(cnt,ans);
            break;
        }
    }
    }
cout << ans << endl;
}

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

int main() {
  string s;
  cin>>s;
  vector<bool> a(4,false);
  for(int i=0;i<s.size();i++){
    if(s[i]=='N') a[0]=true;
    if(s[i]=='S') a[1]=true;
    if(s[i]=='W') a[2]=true;
    if(s[i]=='E') a[3]=true;
  }
  if(a[0]!=a[1]||a[2]!=a[3]) cout<<"No"<<endl;
  else cout<<"Yes"<<endl;
}


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

int main()
{
	char arr[3][3];
	for(int i = 0; i < 3; i++)
		for(int j = 0; j < 3; j++)
			cin >> arr[i][j];
	cout << arr[0][0] << arr[1][1] << arr[2][2];
}

Measured memory_bytes: 3484
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define downque(que)  priority_queue<ll> que;
#define upque(que) priority_queue<ll, vector<int>, greater<int>> que;
typedef long long ll;
const int MOD = 1e9+7;



int main(){
 ll x,y; cin>>x>>y;
 ll count=0;
 while(x<=y){
     count++;
     x*=2;
 }
 cout<<count<<endl;
}

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

#define FOR(i,l,r) for(int i = int(l);i < int(r);i++)
template<typename T> bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; }
template<typename T> bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; }
typedef long long ll;

int N;
const int MAX_N = 1000,MAX_X = 5017;
vector<int> edge [MAX_N];
int A [MAX_N];
deque<bool> dp [MAX_N];
int mn [MAX_N];

bool dfs(int curr)
{
	dp [curr].assign(MAX_X,false);
	dp [curr] [0] = true;
	bool res = true,isLeaf = true;
	int sum = 0;
	for(const auto& it : edge [curr]){
		res &= dfs(it);
		deque<bool> _dp = dp [curr];
		fill(dp [curr].begin(),dp [curr].end(),false);
		for(int i = MAX_X - 1;i >= 0;i--) if(_dp [i]){
			dp [curr] [min(MAX_X - 1,i + mn [it])] = true;
			dp [curr] [min(MAX_X - 1,i + A [it])] = true;
		}
		sum += mn [it] + A [it];
		isLeaf = false;
	}
	if(isLeaf){
		mn [curr] = 0;
	}
	else{
		bool ok = false;
		for(int i = MAX_X - 1;i >= 0;i--) if(dp [curr] [i] && A [curr] >= i){
			mn [curr] = sum - i;
			ok = true;
			break;
		}
		res &= ok;
	}
	return res;
}

int main()
{
	scanf("%d",&N);
	FOR(i,1,N){
		int u;
		scanf("%d",&u);
		u--;
		edge [u].push_back(i);
	}
	FOR(i,0,N){
		scanf("%d",&A [i]);
	}

	puts(dfs(0) ? "POSSIBLE" : "IMPOSSIBLE");

	return 0;
}


Measured memory_bytes: 6528
#include <iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
using ll = long long;
#define INF 1e9
int main() {
	ll N;
	cin >> N;
	string S;
	ll count[5] = { 0 };
	for (int i = 0; i < N; ++i) {
		cin >> S;
		if (S[0] == 'M')
			count[0]++;
		else if (S[0] == 'A')
			count[1]++;
		else if (S[0] == 'R')
			count[2]++;
		else if (S[0] == 'C')
			count[3]++;
		else if (S[0] == 'H')
			count[4]++;
	}
	ll ans = 0;
		for (int i = 0; i < 3; ++i) {
			for (int j = i + 1; j < 4; ++j) {
				for (int k = j + 1; k < 5;++k) {
					ans += count[i] * count[j] * count[k];
				}
			}
		}

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

Measured memory_bytes: 256
from collections import deque

N = int(input())
li = input().split()
ans = deque()
if N % 2 == 0:
    for i in range(N):
        if i % 2 == 0:
            ans.append(li[i])
        elif i % 2 == 1:
            ans.appendleft(li[i])
else:
    for i in range(N):
        if i % 2 == 0:
            ans.appendleft(li[i])
        elif i % 2 == 1:
            ans.append(li[i])
for i in range(N):
    print(ans[i],end=' ')

Measured memory_bytes: 21316
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;

inline int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    } while('0' <= ch && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    } return x * f;
}


deque<char> s;


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    string ss;
    cin >> ss;
    int len = ss.size();
    for(int i = 0; i < len; i ++)
      s.push_back(ss[i]);
    int q;
    cin >> q;
    int op, f;
    char c;
    int k = 1;
    int cnt = 0;
    while(q --){
      cin >> op;
      if(op == 1){
        k = -k;
        cnt ++;
      }
      else{
        cin >> f >> c;
        if(k == 1){
          if(f == 1)  s.push_front(c);
          else        s.push_back(c);
        }
        else if(k == -1){
          if(f == 1)  s.push_back(c);
          else        s.push_front(c);
        }
      }
    }
    if(cnt & 1){
      reverse(s.begin(), s.end());
      for(auto c : s)
        cout << c;
    }
    else
    for(auto c : s)
      cout << c;
    return 0;
}

/*
cbd
10
1
1
1
2 1 a
1
2 1 b
1
1
2 2 a
1
*/


Measured memory_bytes: 976
import numpy as np
n, m = map(int, input().split())

k = [[] for i in range(4)]

for i in range(m):
    s, c = map(int, input().split())
    if s == 1:
        k[1].append(c)
    elif s == 2:
        k[2].append(c)
    else:
        k[3].append(c)

ans = 0
for x in range(1, n+1):
    if len(k[x]) == 0:
        if x == 1 and x != n:
            ans += 10**(n-x)
    elif len(k[x]) == 1 or np.std(k[x]) == 0:
        if k[x][0] == 0 and x == 1 and x != n:
            print(-1)
            exit()
        else:
            ans += (10**(n-x))*k[x][0]
    else:
        print(-1)
        exit()

print(ans)

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

#define ll long long int

using namespace std;

int main(void){
    ll n, h, w;
    ll cnt=0;
    ll a, b;
    cin>>n>>h>>w;
    for(ll i=0; i<n; i++){
        cin>>a>>b;
        if(a < h)continue;
        if(b < w)continue;
        cnt++;
    }

    cout<<cnt<<endl;

    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/integer/common_factor.hpp>
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

using std::string;
using std::vector;
using std::set;
using std::multiset;
using std::unordered_set;
using std::map;
using std::multimap;
using std::unordered_map;
using std::pair;
using std::cin;
using std::cout;

typedef uintmax_t    ull;
typedef intmax_t     ll;
typedef uint64_t     ul;
typedef uint32_t     ui;
typedef uint8_t      uc;

typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000>> cpp_dec_float_1000;

constexpr char CRLF  = '\n';
constexpr char SPACE = ' ';
constexpr char VECTOR_COUT_SEPARATOR = SPACE;
constexpr ll INF = 1000'000'007;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; }
template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; }

void solve(void)
{
  int N; cin >> N;
  set<int> A{};
  for (int i = 0; i < N; ++i) {
    int a; cin >> a;
    auto it = A.find(a);
    if (it == A.end()) {
      A.emplace(a);
    } else {
      A.erase(it);
    }
  }
  auto ans = A.size();
  cout << ans << CRLF;
  return;
}

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

  solve();

  return 0;
}


Measured memory_bytes: 4992
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             998244353
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define vpii            vector<pair<int,int> >
#define umii            unordered_map<int,int>
#define lli             list<int>             
#define gcd(m,n)        __gcd(m,n)   
#define mib             map<int,bool>
 
void fast()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#ifndef ONLINE_JUDGE
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
//#endif
}
 
//////////////////////////////code//////////////////////////////

int32_t main()
{
  fast();
  int n;
  cin>>n;
  
  vector<vi>a(n+1,vi(3));
  vector<vi>dp(n+1,vi(3,0));
  
  for(int i=0;i<n;i++)
   for(int j=0;j<3;j++)
     cin>>a[i][j];
     
   for(int i=0;i<n;i++)
    dp[0][i]=a[0][i];
    
   for(int i=1;i<n;i++)
    for(int j=0;j<3;j++)
     for(int k=0;k<3;k++)
      if(j!=k)
       dp[i][j]=max(a[i][j]+dp[i-1][k],dp[i][j]);
       
    int ans=0;
    for(int i=0;i<3;i++)
     ans =max(dp[n-1][i],ans);
     
     cout<<ans;
  
  

}

Measured memory_bytes: 11264
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
/*		char[] s = stdIn.next().toCharArray();
		
		int sum = 0;
		for(char a: s) {
			if(a == '1') {
				sum++;
			}
		}
		System.out.println(sum);*/
		int cnt = 0;
		String[] s = stdIn.next().split(""); //splitを使う。toCharArray()を使う。substring()を使う方法がある
		for(int i = 0; i < s.length; i++) {
			if(s[i].equals("1")) {
				cnt++;
			}
		}
		System.out.println(cnt);
		
		
	}

}


Measured memory_bytes: 20692
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;

int main()
{
	int a, b, c, d;
	cin >> a >> b >> c >> d;

	if (a + b == c + d) cout << "Balanced" << endl;
	else if (a + b > c + d) cout << "Left" << endl;
	else cout << "Right" << endl;
}


Measured memory_bytes: 256
# N, A, B = gets.split(" ").map(&:to_i)
# S = gets.chomp
N = gets.to_i

ok = false
(1..9).each do |x|
    (1..9).each do |y|
        if x*y == N
            ok = true
        end
    end
end

puts ok ? "Yes" : "No"


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

int main() {
	// your code goes here
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
    
    int n,i;
    long long c;
    
    cin>>n;
    cin>>c;
    
    long long x;
	long long v;
	
	
	map<long long ,long long>mp;
	mp[0]=0;
	for(i=1;i<=n;i++)
	{
	    cin>>x>>v;
	    mp[x]=v;
	}
	
	long long a[n+1],b[n+1],ax[n+1],bx[n+1];
	ax[0]=0;
	bx[n]=0;
	long long j,k;
	j=0;k=0;
	i=0;
	for(auto d:mp)
	{
	    j=j+d.second;
	    k=max(k,j-d.first);
	    
	    a[i]=k;
	    if(i!=0)
	    {
	        if(a[i]!=a[i-1])
	        ax[i]=d.first;
	        else
	        ax[i]=ax[i-1];
	    }
	    
//	    cout<<i<<" : "<<a[i]<<endl;
	
	    i++;
	}
	
	j=0;v=0;i=n;
    mp[c]=0;
//    cout<<endl;
	for(auto d=mp.rbegin();d!=mp.rend();d++)
	{
	    if(d->first != 0)
	    {
	        j=j+d->second;
	        v=max(v,j-c+d->first);
	        b[i]=v;
	        if(i!=n)
	        {
	            if(b[i]!=b[i+1])
	            bx[i]=c-d->first;
	            else
	            bx[i]=bx[i+1];
	        }
//	        cout<<i<<" : "<<b[i]<<endl;
	    
	        i--;
	    }
	    
	}
	v=0;
//	cout<<endl;
	for(i=0;i<=n;i++)
	{
	    v=max(v,a[i]+b[i]-min(ax[i],bx[i]));
	    
//	    cout<<i<<" : "<<v<<endl;
	}
	
	cout<<v;
	return 0;
	
}


Measured memory_bytes: 9600
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vp;
typedef pair<ll,ll> Pair;
#define rep(i,n) for(ll i = 0; i < ll(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define inputv(v,n) rep(i,n){\
    ll x;\
    cin>>x;\
    v.push_back(x);\
    }

const ll INF=99999999999999;
const ll MOD=1000000007;
 
int main() {
  ll n;
  vl a;
  cin >> n;
  inputv(a,n);
  ll sum = 0;
  ll num = 0;
  rep(i,n){
    if(a[i] < 0) num++;
    sum += abs(a[i]);
  }
  if(num%2 == 0){
    cout << sum;
    return 0;
  }
  ll mx = INF;
  rep(i,n){
    mx = min(mx,abs(a[i]));
  }
  cout << sum-2*mx;

 return 0;
}

Measured memory_bytes: 1400
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cmath>

using namespace std;

long long int h;
long long int ans=0;
long long int n=0;

int main()
{
    cin>>h;
    while(h)
    {
        n++;
        h/=2;
    }
    ans=pow(2,n)-1;
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
A, B, M = map(int, input().split())

a = list(map(int, input().split()))
b = list(map(int, input().split()))

#c = [input().split() for l in range(M)]

ans = min(a) + min(b)

for i in range(M):
	x,y,c = map(int, input().split())
	ans = min(ans, a[x-1]+b[y-1]-c)

print(ans)

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

using namespace std;

#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mk make_pair
ll n, k, s;

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> k >> s;

    for(int i = 1; i <= k; ++i){
        cout << s << ' ';
    }
    for(int i = k + 1; i <= n; ++i){
        cout << 999999999 << ' ';
    }

    return 0;
}


Measured memory_bytes: 1280
N = int(input())
h = list(map(int,input().split()))
def g(ls):
    try:
        return ls.index(0)
    except:
        return -1
def f(ls):
    if ls==[]:
        return 0
    if len(ls) == 1:
        return ls[0]
    m = min(ls)
    ls = [x-m for x in ls]
    ans = m
    while ls:
        i = g(ls)
        if i == -1:
            ans += f(ls)
            break
        ans += f(ls[:i])
        ls = ls[i+1:]
    return ans
print(f(h))

Measured memory_bytes: 9268
#include <bits/stdc++.h>
using namespace std;
int main () {
  int N,M;
  cin >> N >> M;
  int datax, datay;
  cin >> datax >> datay;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if (datax < a) datax = a;
  }
  for (int i = 0; i < M; i++) {
    int a;
    cin >> a;
    if (datay > a) datay = a;
  }
  if (datax < datay) cout << "No War" << endl;
  else cout << "War" << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k; cin >> n >> k;
  vector<ll> H(n); rep(i, n) cin >> H[i];
  sort(all(H));
  if(k>=n) cout << 0 << "\n";
  else     cout << accumulate(H.begin(), H.begin()+n-k, 0LL) << "\n";
  return 0;
}


Measured memory_bytes: 4760
#include<bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
using namespace std;

//ループマクロ
#define FOR(i , a , b)  for(int i = (a) ; i < (b) ; i++)
#define REP(i , n)      for(int i = 0 ; i < n ; i++)
#define REPR(i , n)     for(int i = n ; 0 <= i ; i--)

//入出力
#define SCOUT(x) cout << (x) << " "
#define VECCIN(x) for(auto& ele : (x) ) cin >> ele
#define VECOUT(x) for(auto& ele : (x) ) cout << ele << " "; cout << endl;

//その他
#define ALL(obj) (obj).begin() , (obj).end() 
#define P pair<int , int>
#define V vector<int>
#define M map<int, int>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int , V , greater<int>>

int main(void) {

	ios::sync_with_stdio(false);	cin.tie(0);
	int r;	cin >> r;
	SCOUT(3 * r * r) << endl;

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
char s[N];
vector<int> L,R;
int main() {
    //freopen("in.txt","r",stdin);
    int n,k,c;
    scanf("%d%d%d%s",&n,&k,&c,s+1);
    for(int i=1;i<=n;) {
        if(s[i]=='o') L.push_back(i),i+=c;
        i++;
        if(L.size()==k) break;
    }
    for(int i=n;i>=1;) {
        if(s[i]=='o') R.push_back(i),i-=c;
        i--;
        if(R.size()==k) break;
    }
    for(int i=0;i<k;i++) {
        if(L[i]==R[k-i-1]) printf("%d\n",L[i]);
    }
    return 0;
}

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

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893
#define prev PREV
#define next NEXT
                         
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }
template<class T> T gcd(T a, T b) { re a ? gcd (b % a, a) : b; }
template<class T> T sqr(T a) { re a * a; }
template<class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

#define filename ""

int n;
int m;
string s;

int main () {
	cin >> s;
	if (sz (s) >= 4 && s.substr (0, 4) == "YAKI") printf ("Yes\n"); else printf ("No\n");
	return 0;
}


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

int main() {
  char b;
  cin >> b;

  switch (b) {
    case 'A':
      cout << 'T' << endl;
      break;
    case 'T':
      cout << 'A' << endl;
      break;
    case 'C':
      cout << 'G' << endl;
      break;
    case 'G':
      cout << 'C' << endl;
      break;
  }

  return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
template <typename T>
inline T read() {
    T X = 0, w = 0;
    char ch = 0;
    while (!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
const int MAXN = 200005;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
int a[MAXN];
bool vis[MAXN];
int dis[MAXN];
int loopA, loopB;
int V;
int d2[MAXN];
void dfs(int u, int d) {
    vis[u] = true;
    dis[u] = d;
    d2[d] = u;
    if (vis[a[u]]) {
        loopB = d + 1 - dis[a[u]];
        V = a[u];
        return;
    }
    dfs(a[u], d + 1);
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    ll k;
    scanf("%d%lld", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    dfs(1, 0);
    loopA = dis[V];
    if (k < loopA) {
        printf("%d", d2[k]);
    } else {
        k -= loopA;
        k %= loopB;
        k += loopA;
        printf("%d", d2[k]);
    }
    return 0;
}

Measured memory_bytes: 6244
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>

using namespace std;
using ll = long long;


int main(){
    int l;
    cin >> l;
    cout.precision(12);
    cout << (long double) l*l*l/27 << endl;
}

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

const int MOD = 1000000007;

//long long
using ll = long long;

//出力系
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
 
// begin() end()
#define all(x) (x).begin(),(x).end()

//for
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

//最大公約数 
unsigned gcd(unsigned a, unsigned b) {
  if(a < b) return gcd(b, a);
  unsigned r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}

 int main(){
string X, Y;
cin >> X >> Y;

if(X > Y) print('>');
else if(X == Y) print('=');
else if(X < Y) print('<');
 }

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<limits>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define REP(i,n) FOR(i,0,n)

int N;
int pre_a = 0, cur_a;

int main() {
  scanf("%d", &N);

  int count = 0;
  REP(i,N) {
    scanf("%d", &cur_a);
    if (pre_a == cur_a) {
      ++count;
      pre_a = 0;
    }
    else pre_a = cur_a;
  }

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

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

int main() {
  int N;
  cin >> N;
  
  int ans = 1;
  while (ans*2 <= N) {
    ans *= 2;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
int main() {
	int n, r, l, ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> l >> r;
		ans += r - l + 1;
	}
	cout << ans << endl;
	return 0;
}


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

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S,T; cin >> S >> T;
    int s=S.size(),t=T.size();
    vector<vector<ll>> pos(26);
    rep(i,s) pos[S[i]-'a'].emplace_back(i);
    rep(i,26){
        int p=pos[i].size();
        rep(j,p) pos[i].emplace_back(pos[i][j]+s);
    }
    ll now=-1;
    rep(i,t){
        int n=T[i]-'a';
        if (pos[n].empty()){
            cout << -1 << endl;
            return 0;
        }
        ll next=*upper_bound(all(pos[n]),(now<0LL?-1:now%s));
        now=(now<0?0:now/s*s)+next;
    }
    cout << now+1 << endl;
}

Measured memory_bytes: 2756
import java.io.PrintWriter;
import java.util.*;

public class Main {
    void run() {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        long MOD = (long) 1e9 + 7;


        int s = sc.nextInt();
        Combination combi = new Combination(2100, MOD);

        if (s < 3) {
            System.out.println(0);
            return;
        }
        long ans = 1;
        for (int i = 2; i <= s / 3; i++) {
//            debug(i, combi.nck(s - i * 3 + i - 1, i - 1));
            ans = (ans + combi.nck(s - i * 3 + i - 1, i - 1) % MOD) % MOD;
        }

        System.out.println(ans);

        // out.println("Hello World");
        out.flush();
    }

    public class Combination {
        int n;
        long mod;
        long[] facts;

        public Combination(int n, long mod) {
            this.n = n;
            this.mod = mod;
            facts = new long[n];
            facts[0] = 1;
            for (int i = 1; i < n; i++) {
                facts[i] = facts[i - 1] * i % mod;
            }
        }

        long modpow(long a, long b) {
            if (b == 0) return 1;
            else if (b == 1) return a;

            long x = modpow(a, b / 2);
            return b % 2 == 0 ? x * x % mod : x * (x * a % mod) % mod;
        }

        long inv(long n) {
            return modpow(n, mod - 2);
        }

        public long nck(int n, int k) {
            if (n < k) return 0;
            return facts[n] * (inv(facts[n - k]) * inv(facts[k]) % mod) % mod;
        }
    }


    void debug(Object... os) {
        System.err.println(Arrays.deepToString(os));
    }

    public static void main(String[] args) {
        new Main().run();
    }
}


Measured memory_bytes: 35796
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string S;
  cin >> S;
  if (S == "RRR") {
    cout << 3 << "\n";
  }
  else if ((S.at(0) == 'R' && S.at(1) == 'R') || (S.at(1) == 'R' && S.at(2) == 'R')) {
    cout << 2 << "\n";
  }
  else if (S.at(0) != 'R' && S.at(1) != 'R' && S.at(2) != 'R') {
    cout << 0 << "\n";
  }
  else {
    cout << 1 << "\n";
  }
}


Measured memory_bytes: 3612
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<numeric>
#include<list>
using namespace std;

typedef stack<int> IntStack;
typedef queue<int> IntQueue;

const double pi = 3.141592653589793;


//最大公約数
template<class T> T gcd(T a, T b)
{
	while (true)
	{
		T r;
		if (a%b == 0) return b;
		else
		{
			r = a % b;
			a = b;
			b = r;
		}
	}
}
//最小公倍数
template<class T> T lcm(T a, T b)
{
	return a / gcd(a, b)*b;
}
//文字0~9をint型0~9に変換
int char_to_int(char c)
{
	if (c == '0') return 0;
	if (c == '1') return 1;
	if (c == '2') return 2;
	if (c == '3') return 3;
	if (c == '4') return 4;
	if (c == '5') return 5;
	if (c == '6') return 6;
	if (c == '7') return 7;
	if (c == '8') return 8;
	if (c == '9') return 9;
	else return -1;
}
//int型0~9を文字0~9に変換
char int_to_char(int c) {
	if (c == 0) return '0';
	if (c == 1) return '1';
	if (c == 2) return '2';
	if (c == 3) return '3';
	if (c == 4) return '4';
	if (c == 5) return '5';
	if (c == 6) return '6';
	if (c == 7) return '7';
	if (c == 8) return '8';
	if (c == 9) return '9';
	else return'x';
}
//a^b mod MODを計算
long long int ModPower(long long int a, long long int b, long long int MOD)
{
	if (b == 0) return 1;
	else if (b % 2 == 0)
	{
		long long int d = ModPower(a, b / 2, MOD);
		return (d*d) % MOD;
	}
	else
	{
		return (a*ModPower(a, b - 1, MOD)) % MOD;
	}
}
//nCr mod MODを計算
long long int ModCombi(long long int n, long long int r, long long int MOD)
{
	if (n == 0 || r == 0) return 1;

	long long int Kn = 1, Kr = 1, Knr = 1;

	long long int temp = 1;
	for (long long int i = 1; i <= n; i++)
	{
		temp *= i;
		temp %= MOD;
		if (i == n) Kn = temp;
		if (i == r) Kr = temp;
		if (i == n - r) Knr = temp;
	}

	long long int x = ModPower(Kr, MOD - 2, MOD);
	long long int y = ModPower(Knr, MOD - 2, MOD);

	long long int ans;
	ans = Kn * x;
	ans %= MOD;
	ans *= y;
	ans %= MOD;

	return ans;
}
//int型10進数をord桁のstring型2進数に変換する
string binary(int x, int ord)
{
	int y;
	string ans = "";
	for (int i = 0; i < ord; i++)
	{
		y = x / (int)pow(2, ord - 1 - i);
		y %= 2;
		if (y == 0) ans += "0";
		else ans += "1";
	}

	return ans;
}
//ord桁のstring型2進数をint型10進数に変換する
int rep_binary(string x, int ord)
{
	int res = 0;
	for (int i = 0; i < ord; i++)
	{
		if (x[i] == '1') res += (int)pow(2, ord - 1 - i);
	}
	return res;
}


/*ここまでテンプレ*//*ここまでテンプレ*//*ここまでテンプレ*//*ここまでテンプレ*/



int main()
{
	string S;
	cin >> S;
	int l = S.length();
	int ans = 0;
	for (int i = 0; i < l/2; i++)
	{
		if (S[i] != S[l - 1 - i]) ans++;
	}

	cout << ans << endl;

	return 0;
}

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

int main() {
  int n, x = 0, mxm = 0, i;
  string s;
  cin >> n >> s;
  
  for(i = 0; i < n; i++){
    if(s.at(i) == 'I'){
      x++;
    }
    else{
      x--;
    }
    mxm = max(x, mxm);
  }
  
  cout << mxm << endl;
}


Measured memory_bytes: 256
/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
       return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}
void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}


int main()
{

   fastread();

   ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r,ans=0;

      bool flag=false;

      string str;



      cin>>a>>b>>c;

      if((a+b+c)>=22)cout<<"bust"<<endl;

      else cout<<"win"<<endl;











}



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

int maxtime(int x){
  return (x+9)/10*10;
}

int remtime(int x){
  return maxtime(x)-x;
}

int main(){
  int a,b,c,d,e;
  cin >>a >>b >>c >>d >>e;
  int sum = maxtime(a)+maxtime(b)+maxtime(c)+maxtime(d)+maxtime(e);
  sum -= max({remtime(a), remtime(b), remtime(c), remtime(d), remtime(e)});
  cout << sum << endl;
  return 0;
}

Measured memory_bytes: 256
n, a, b = map(int, input().split())
mod = pow(10, 9) + 7
 
def comb(N, x):
    X = 1
    for i in range(N-x+1, N+1):
        X = X * i % mod
    Y = 1
    for j in range(1, x+1):
        Y = Y * j % mod
    Ym = pow(Y, mod-2, mod)
    return X * Ym

a = comb(n, a)
b = comb(n, b)
 
print((pow(2, n, mod) -1 -a -b) % mod)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std ;
int main()
{ long long int n,i,j,x,y,m,k,t ;
  cin>>n ;
    m=0 ;
    x=0 ;
    queue<string> q ;
    long long int arr[10+n] ;
       k=0 ;
    for(i=1;i<=9;i++)
    {  q.push(to_string(i)) ;
       arr[k++]=i ;
    }



      while(1)
      { if(x==1)
         break ;
         long long int size=q.size() ;
         while(size--)
         { string s1=q.front() ;
           q.pop() ;
           if(s1[s1.length()-1]!='0')
           { q.push(s1+to_string(s1[s1.length()-1]-'0'-1));
              arr[k++]=stoll(s1+to_string(s1[s1.length()-1]-'0'-1)) ;
               if(k>=n)
              { x=1 ;
                 break ; }
           }  q.push(s1+to_string(s1[s1.length()-1]-'0'));
              arr[k++]=stoll(s1+to_string(s1[s1.length()-1]-'0')) ;
              if(k>=n)
             {  x=1 ;
               break ; }
          if(s1[s1.length()-1]!='9')
           { q.push(s1+to_string(s1[s1.length()-1]-'0'+1));
              arr[k++]=stoll(s1+to_string(s1[s1.length()-1]-'0'+1)) ;
              if(k>=n)
             {  x=1 ;
               break ; }
           } } } string s2="" ;

     sort(arr,arr+n) ;
       cout<<arr[n-1]<<"\n" ;
   return 0 ; }


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

int main(void) {
  string s;
  long long k;
  cin >> s >> k;
  int ix = -1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != '1') break;
    else ix = i;
  }
  // cerr << ix << endl;
  if (ix == -1) {
    cout << s[0] << endl;
  } else if (k - 1 <= ix) {
    cout << 1 << endl;
  } else {
    cout << s[ix + 1] << endl;
  }

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define GET_REP(_1, _2, _3, NAME, ...) NAME
#define rep(...) GET_REP(__VA_ARGS__, irep, _rep)(__VA_ARGS__)
#define rep1(...) GET_REP(__VA_ARGS__, irep1, _rep1)(__VA_ARGS__)
#define _rep(i, n) irep (i, 0, n)
#define _rep1(i, n) irep1(i, 1, n)
#define irep(i, a, n) for (int i = a; i < (int)(n); ++i)
#define irep1(i, a, n) for (int i = a; i <= (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = (int)(n); i >= 1; --i)
#define allrep(X, x) for (auto &&X : x)
#define all(x) begin(x), end(x)
#ifdef LOCAL
  #include "../../Lib/cout_container.hpp"
  #define debug(x) cout << #x " => " << (x) << endl
#else
  #define debug(x) 0
#endif
using lint = long long;
constexpr int    INF  = 1 << 29;
constexpr lint   INFL = 1LL << 61;
constexpr int    MOD  = (int)1e9 + 7;
constexpr double EPS  = 1e-9;
using namespace std;
namespace { struct INIT { INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } INIT; }

int main(void) {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<int> l(n);
  rep (i, n) cin >> l[i];
  int limit = 1, mini = INT_MAX;
  rep (i, n) limit *= 4;
  vector<int> target{a, b, c};
  rep (bit, limit) {
    int b = bit, mp = -30;
    vector<int> sum(3);
    rep (i, n) {
      int d = b % 4;
      b /= 4;
      if (d < 3) {
        sum[d] += l[i];
        mp += 10;
      }
    }
    rep (i, 3) {
      if (sum[i] == 0) {
        mp = INT_MAX;
        break;
      }
      mp += abs(target[i] - sum[i]);
    }
    mini = min(mini, mp);
  }
  cout << mini << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>

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

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;


void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

//mod計算用のクラス
class ModInt{
public:
    long long int x;
    long long int mod;
	ModInt(){}
    ModInt(long long int _x, long long int _mod = 1000000007){
        this->x = _x % _mod;
        this->mod = _mod;
    }
	ModInt operator+(ModInt other){
		return ModInt(this->x + (other.x % this->mod),this->mod);
	}
	ModInt operator-(ModInt other){
		long long int res = this->x - (other.x % this->mod);
		if (res < 0) return ModInt(res + this->mod, this->mod);
		return ModInt(res);
	}
	ModInt operator*(ModInt other){
		return ModInt(this->x * (other.x % this->mod),this->mod);
	}
	ModInt operator/(ModInt other){
		return ModInt(*this * other.inv());
	}
	ModInt pow(long long int n){
		ModInt res(1, this->mod), a(this->x, this->mod);
		while (n){
			if (n & 1){
				res = res * a;
			}
			n >>= 1;
			a = a * a;
		}
		return res;
	}
	ModInt inv(){
		return ModInt(this->pow(this->mod - 2));
	}
	void disp(){
		cout << this->x << endl;
	}
};
ostream& operator<<(ostream& stream, const ModInt m){
	stream << to_string(m.x);
	return stream;
}

//最大公約数を返す関数.
long long int gcd(long long int x, long long int y){
    if (x < y) swap(x,y);
    while (y > 0){
        long long int r = x % y;
        x = y;
        y = r;
    }
    return x;
}

int main() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	long long int r = 0, g = 0,b = 0;
	rep(i,n){
		if(s[i]=='R')
			r++;
		if(s[i]=='B')
			b++;
		if(s[i]=='G')
			g++;
	}
	long long int ans = r * g * b;
	for (int k = 1; k <= n;k++){
		rep(i,n){
			if (i+2*k < n){
				if(s[i] != s[i+k] && s[i+k] != s[i+2*k] && s[i+2*k] != s[i]){
					ans--;
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3600
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
  int k,t;
  int f=-1,g=1000,x=-1;
  cin >> k >> t;
  vector<int> a(t,0);
  for(int i=0;i<t;i++){
    cin >> a[i];
  }
  while(k!=0){
    g=0;
    f=-1;
    for(int i=0;i<t;i++){
      if(x==i||a[i]==0)continue;
      g=max(g,a[i]);
      if(g==a[i])f=i;
    }
    if(f==-1)break;
    k--;
    x=f;
    a[f]--;
  }
  cout << k << endl;
  return 0;
}


Measured memory_bytes: 256
import fractions


def func():
    N, init = map(int, input().split())
    x = list(map(int, input().split()))

    x.append(init)
    x.sort()

    ans = x[1] - x[0]
    for i in range(N):
        ans = fractions.gcd(ans, x[i+1] - x[i])

    print(ans)


if __name__ == '__main__':
    func()


Measured memory_bytes: 16240
#include <iostream>
#include <string>

using namespace std;


struct {
	unsigned int num;
	char s;
}typedef card;

// Variables for mergesort
const unsigned int MAX = 100000;
card L[MAX / 2 + 2];
card R[MAX / 2 + 2];
const int SENTINEL = 2000000000;

// Merge
void Merge(card A[], int left, int mid, int right)
{
	int n1 = mid - left;
	int n2 = right - mid;
	for (int i = 0; i < n1; i++)
		L[i] = A[left + i];
	for (int i = 0; i < n2; i++)
		R[i] = A[mid + i];
	L[n1].num = SENTINEL;
	R[n2].num = SENTINEL;

	int i = 0;
	int j = 0;
	for (int k = left; k < right; k++) {
		if (L[i].num <= R[j].num) {
			A[k] = L[i];
			i++;
		}
		else {
			A[k] = R[j];
			j++;
		}
	}
}

// Merge sort
void Mergesort(card A[], int left, int right)
{
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		Mergesort(A, left, mid);
		Mergesort(A, mid, right);
		Merge(A, left, mid, right);
	}

}

// Exchange
void exchange(card A[], int i, int j)
{
	card tmp = A[i];
	A[i] = A[j];
	A[j] = tmp;
}

// Partition
int partition(card A[], int l, int r)
{
	unsigned int threshold = A[r].num;
	int i = l - 1;
	for (int j = l; j < r; j++) {
		if (A[j].num <= threshold) {
			i++;
			// Exchange
			exchange(A, i, j);
		}
	}
	i++;
	// Insert threshold
	exchange(A, i, r);
	return i;
}

// Quicksort
void quicksort(card A[], int l, int r)
{
	if (l >= r)
		return;
	int p = partition(A, l, r);
	quicksort(A, l, p - 1);
	quicksort(A, p + 1, r);
}

// MAIN FUNCTION
int main(int argc, char** argv)
{
	// Input
	card A[100000];
	card B[100000];
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> A[i].s >> A[i].num;
		B[i] = A[i];
	}

	// Quicksort
	quicksort(A, 0, n - 1);

	// Mergesort
	Mergesort(B, 0, n);

	// Check if quicksort is stable
	bool stable = true;
	for (int i = 0; i < n; i++) 
		if (A[i].s != B[i].s)
			stable = false;
	
	// Print all elements
	if (stable)
		cout << "Stable" << endl;
	else
		cout << "Not stable" << endl;

	for (int i = 0; i < n; i++) {
		cout << A[i].s << " " << A[i].num << endl;
	}

	return 0;
}

Measured memory_bytes: 5428
import java.util.*;
class Main{
	public static void main(String[] args){
    	Scanner scan = new Scanner(System.in);
      	int n = scan.nextInt();
      	int k = scan.nextInt();
      	String word = scan.next();
            
      	String ans = word.substring(0,k-1) + word.substring(k-1,k).toLowerCase() + word.substring(k,n);
       System.out.println(ans);
    }
}

Measured memory_bytes: 21204
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int, int>;
const int INF (1<<30);

vector<int> dist;
vector<vector<int>> edge;
queue<P> q;
int N, M;

void bfs() {
}

int main() {
  cin >> N >> M;
  dist.resize(N);
  edge.resize(N);

  rep(i, N) dist[i] = -1;
  dist[0] = 0;

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  for (int x : edge[0]) {
    q.push(make_pair(0,x));
  }
  while (!q.empty()) {
    P p = q.front();
    q.pop();

    if (dist[p.second] != -1) continue;

    dist[p.second] = p.first;

    for (int x: edge[p.second]) {
      q.push(make_pair(p.second, x));
    }
  }

  cout << "Yes" << endl;
  for (int i = 1; i < N; i++) {
    cout << dist[i]+1 << endl;
  }


  return 0;
}


Measured memory_bytes: 12316