akhauriyash/Code-Regression huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = B[A[0]-1]
for i in range(1, N):
    ans += B[A[i]-1]
    if A[i]-1 == A[i-1]:
        ans += C[A[i]-2]

print(ans)


Measured memory_bytes: 3064
N = int(input())
A = [int(input()) for _ in range(N)]
m = -1
m2 = -1
for a in A:
    if a >= m:
        m2 = m
        m = a
    elif a < m and a >= m2:
        m2 = a
for i in range(N):
    if A[i] != m:
        print(m)
    else:
        print(m2)


Measured memory_bytes: 12532
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;

#ifdef INPUT
#include <sstream>
stringstream input(R"(
11009 11011
)");
#define cin input
#endif

int main()
{
	int a, b;
	cin >> a >> b;
	int n=0;

	for (int i=a; i<=b; i++)
	{
		string j = to_string(i);
		if (j[0]==j[4] && j[1]==j[3])
			n++;
	}	
	cout << n << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <deque>
#include <map>
#include <bits/stdc++.h>
using namespace std;

int main(void){
    
    int A, B;
    
    cin >> A >> B;

    if(max(A,B) <= 9 && min(A,B) >= 1){
        cout << A*B << endl;
    }
    else{
        cout << -1 << endl;
    }
    
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

const long long INF = 1LL << 60;

//各桁の和を出力
long long Sum(long long n) {
  long long m = 0;
  while (n) {
    m += n % 10;
    n /= 10;
  }
  return m;
}

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

long long fac[MAX], finv[MAX], inv[MAX];

void Comuse() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

#define comuse Comuse()
// nCk(comuseを使え)
long long combi(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// pCk(comuseを使え)
long long perm(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] % MOD) % MOD;
}

//最小公倍数(aとbの)
long long lcm(long long a, long long b) {
  long long n;
  n = a / __gcd(a, b) * b;
  return n;
}

// auto A = div(n)
vector<long long> div(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

//(aのn乗をmodで割ったあまり)
long long modpow(long long a, long long n, long long mod) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * a % mod;
    }

    a = a * a % mod;

    n >>= 1;
  }

  return ans;
}

void solve();

const int MAX_N = 131072;
// segment tree
int NN;
int seg[MAX_N * 2 - 1];
void seguse() {
  for (int i = 0; i < 2 * NN - 1; i++) {
    seg[i] = INT_MAX;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;
  solve();
}

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

string S;
void solve() {
  cin >> S;
  if(S[2] == S[3] && S[4] == S[5]){
    cout << "Yes" <<endl;
  } else{
    cout << "No" <<endl;
  }
}

Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
  int N=0,M=0,i=0,x=0;
  
  scanf("%d %d",&N,&M);
  
  for(i=0;i<M;i++)
  {
    scanf("%d",&x);
    N=N-x;
  }
  
  if(N<0)
  {
  printf("-1");
  }
  
  else
  {
  printf("%d",N);
  }
  
  return 0;
  
}

Measured memory_bytes: 1728
#include<iostream>
using namespace std;
int main()
{
  int n, x, t, a, b;
  cin >> n >> x >> t;
  a=n/x;
  if (n%x==0)
  {
    b=a;
  }else
  {
    b=a+1;
  }
  
  
  cout<<b*t<<endl;
}

Measured memory_bytes: 3588
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

#define int long long

signed main(){
	int n;
	cin >> n;

	vector<int> a(n);
	cin >> a;

	sort(all(a), greater<int>());

	vector<vector<int>> v(n);
	rep(i,n){
		rep(j,n){
			if(v[j].empty() or v[j].back() % a[i] == 0){
				v[j].emplace_back(a[i]);
				break;
			}
		}
	}

	int ans = 0;
	rep(i,n){
		if(v[i].empty()) break;
		int k = v[i].front() - 1;
		rep(j,v[i].size()){
			ans += k % v[i][j];
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 384
#include "bits/stdc++.h"
using namespace std;

/* macro */
#define rep(i,a,b) for(int i=a;i<b;i++)
#define revrep(i,a,b) for(int i = a; i > b; i--)
#define int long long
#define exist(s,e) ((s).find(e)!=(s).end())
#define all(v) (v).begin(), (v).end()
#define each(s,itr) for(auto (itr) = s.begin(); (itr) != s.end(); (itr)++)
#define sum(v) accumulate(all(v), (0LL))
#define isin(a, b, c) (b <= a && a <= c)
#define println cout << "\n";
#define sz(v) (int)v.size()


/* alias */
template<class T> using vec = vector<T>;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef tuple<int, int, int> ti;

/* constant */
const int inf = 1LL << 62;
const int mod = 1e9 + 7;
const int dx[8]={1,0,-1,0,-1,1,-1,1};
const int dy[8]={0,1,0,-1,-1,-1,1,1};
const string alphabet = "abcdefghijklmnopqrstuvwxyz";

/* io_method */
int input(){int tmp;cin >> tmp;return tmp;}
string raw_input(){string tmp;cin >> tmp;return tmp;}
template<class T> void printx(T n){cout << n;}
template<class T, class U> void printx(pair<T, U> p){cout << "(" << p.first << "," << p.second << ")";}
template<class T, class U, class V> void printx(tuple<T, U, V> t){cout << "{" << get<0>(t) << "," << get<1>(t) <<"," << get<2>(t) << "}" << endl;}
template<class T> void printx(vector<T> v){cout << "{";rep(i,0,v.size()){printx(v[i]);if(i != v.size()-1)printx(",");}cout << "}";}
template<class T> void print(T n){printx(n);cout << endl;}
template<class T> void print(set<T> s){cout << "{";each(s, e){if(e != s.begin()) printx(",");printx(*e);}cout << "}" << endl;}
template<class T, class U> void print(map<T, U> mp){cout << "{";each(mp, e){cout << "(" << e -> first << "," << e -> second << ")";}cout << "}" << endl;}


/* general_method */
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>T cut(T &a, int l, int r){return T(a.begin()+l, a.begin()+r);}

/* main */

int n, m;
vi arr;

void in(){
    cin >> n >> m;
	rep(i,0,m) arr.push_back(input());

}


void solve(){
	if(n >= m){
		print(0);
		return ;
	}


    sort(all(arr));
	vi tmp;

	rep(i,1,m) tmp.push_back(arr[i] - arr[i - 1]);

	sort(all(tmp));

	vi c = cut(tmp, 0, m - (n));

	print(sum(c));


}

signed main(){

    cin.tie(0);
    ios::sync_with_stdio(false);
    in();
    solve();
    
}


Measured memory_bytes: 2548
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n;
    cin>>n;
    ll sum=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=i;j<=n;j+=i)
            sum+=j;
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 3636
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
long long a, b, x;

long long f(long long n){
    if(n >= 0){
        return n/x + 1;
    }
    else{
        return 0;
    }
}

int main(){
    
    long long A, B;

    cin >> a >> b >> x;

    printf("%lld\n", f(b) - f(a - 1));

    return 0;
    
}


Measured memory_bytes: 256
N = int(input())
B_s = list(map(int, input().split()))
answer = B_s[0]
for i in range(len(B_s)-1):
    answer += B_s[i] if B_s[i] < B_s[i+1] else B_s[i+1]
answer += B_s[-1]
print(answer)


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

using namespace std;


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

    int H,W,K;
    cin>>H>>W>>K;


    vector<string> board(H);
    for(int c=0;c<H;c++) cin>>board[c];


    int compteur = 0;
    for(int c=0;c<H;c++) for(int c2=0;c2<W;c2++) {
        if(board[c][c2]=='#') compteur++;
    }
    long long res = 0;
    for(int c=0;c<(1<<H);c++) {
        int compteurCopie = compteur;
        vector<string> copie(H);
        copy(board.begin(),board.end(),copie.begin());

        for(int c2=0;c2<H;c2++) {
            if((1<<c2) & c) {
                for(int c3=0;c3<W;c3++) {
                    if(board[c2][c3] == '#') compteurCopie--;
                    copie[c2][c3] = 'R';
                }
            }
        }



        vector<int> dp(100,0);
        dp[0]=1;
        for(int c2=0;c2<W;c2++) {
            int current = 0;
            for(int c3=0;c3<H;c3++) {
                if(copie[c3][c2] == '#') {
                    current++;
                }
            }
            for(int c=99;c>=0;c--) {
                if(c+current < 100) dp[c+current]+=dp[c];
            }
        }
        res += dp[K];
    }
    cout<<res;
}


Measured memory_bytes: 3632
#include <iostream>

using namespace std;

int main()
{
	int N, A, B, C;

	while (true)
	{
		cin >> N;

		if (N == 0) { break; }

		for (int i = 0; i < N; i++)
		{
			cin >> A >> B >> C;

			if (A == 100 || B == 100 || C == 100)
			{
				cout << "A" << endl;
			}
			else if (A + B >= 180)
			{
				cout << "A" << endl;
			}
			else if (A + B + C >= 240)
			{
				cout << "A" << endl;
			}
			else if (A + B + C >= 210)
			{
				cout << "B" << endl;
			}
			else if (A + B + C >= 150 && (A >= 80 || B >= 80))
			{
				cout << "B" << endl;
			}
			else
			{
				cout << "C" << endl;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 2984
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cout << min(a,b) + min(c,d) << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<limits>
#include<sstream> 
#include<unordered_map>      

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;
typedef vector<int> vi;
typedef vector<ll> vl;

const int inf = numeric_limits<int>::max() >> 2;
const ll linf = numeric_limits<ll>::max() >> 2;
const ull ulinf = numeric_limits<ull>::max() >> 2;
const double pi = acos(-1);
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const int dx8[8]={-1,0,1,-1,1,-1,0,1};
const int dy8[8]={-1,-1,-1,0,0,1,1,1};

#define p_queue(i) priority_queue<i> 
#define rp_queue(i) priority_queue<i, vector<i>, greater<i>> 
#define umap(i,j) unordered_map<i,j>
#define P(p) cout<<(p)<<endl
#define PS(p) cout<<(p)<< " "
#define IN cin >> 
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i = (m-1); i >= (int)(n); i--)
#define inrep(n,a) for(int i = 0; i < (int)(n); i++)cin >> a[i];
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define rsort(a,b,c) sort(a, b, greater<c>())
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v,c) sort(v.begin(), v.end(), greater<c>()) 
#define ft first
#define sd second
#define pb push_back
#define it insert
#define sz(x) ((int)(x).size())
#define lb(a,n,k) (lower_bound(a,a+n,k) - a) 
#define vlb(a,k)  (lower_bound(a.begin(),a.end(),k) - a.begin())
#define ub(a,n,k) (upper_bound(a,a+n,k) - a) 
#define vub(a,k)  (upper_bound(a.begin(),a.end(),k) - a.begin())
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

vector<string> split(const string &str, char sep){
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while( getline(ss, buffer, sep) ) {
        v.push_back(buffer);
    }
    return v;
}

//ll ncr[100][100];
//ll nCr(int n, int r){if(n==r) ret ncr[n][r] = 1; if(r==0) ret ncr[n][r] = 1; if(r==1) ret ncr[n][r] = n;if(ncr[n][r]) ret ncr[n][r]; ret ncr[n][r] = nCr(n-1,r) + nCr(n-1,r-1);}

//ll npr[100][100]={};
//ll nPr(int n,int r){if(npr[n][r])ret npr[n][r];if(r==0)ret npr[n][r] = 1;if(r==1)ret npr[n][r] = n;ret npr[n][r] = n * nPr(n-1,r-1);}

//ll nHr(int n,int r){ret nCr(n+r-1,r);}

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

int n;
int main(){
  
    cin >> n;
    int p[n];
    inrep(n,p);

    int ans = 0;

    rep(i,0,n-2){
	int a[3];
	rep(j,0,3){
	    a[j] = p[i+j];
	}

	sort(a,a+3);
	if(a[1] == p[i+1])ans++;
    }
    P(ans);
    
    return  0;
}


Measured memory_bytes: 256
def main ():
  N = int(input())
  A = list(map(int,input(). split ()))
  
  if 0 in A:
    print(0)
    return
  
  prod = 1
  for a in A:
    prod *= a
    if prod > 1000000000000000000:
      print(-1)
      return
  print(prod)
main ()

Measured memory_bytes: 21696
#include <bits/stdc++.h>
#define ll long long 
#define maxn 500005
#define INF 0x3f3f3f3f
using namespace std;

int n;
int a[maxn];
struct Node{
    int x, y;
    Node():x(0), y(0){}
    Node(int xx, int yy): x(xx), y(yy){}
};
vector<Node> ans;
bool vis[maxn];
int main(){
    scanf("%d", &n);
    int da = 0, xiao = 0;
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        if(a[i] >= 0) da++;
        else
            xiao++;
    }
    if(!xiao){
        sort(a + 1, a + 1 + n);
        int p = a[1];
        for(int i = 2; i <= n - 1; i++){
            ans.push_back(Node(p, a[i]));
            p -= a[i];
        }
        ans.push_back(Node(a[n], p));
        printf("%d\n", a[n] - p);
    }
    else if(!da){
        sort(a + 1, a + 1 + n);
        int p = a[n];
        for(int i = n - 1; i >= 2; i--){
            ans.push_back(Node(p, a[i]));
            p -= a[i];
        }
        ans.push_back(Node(p, a[1]));
        printf("%d\n", p - a[1]);
    }
    else{
        int fu = INF, z = INF;
        sort(a + 1, a + 1 + n);
        memset(vis, false, sizeof(vis));
        for(int i = 1; i <= n; i++){
            if(a[i] < 0 && fu == INF){
                fu = a[i];
                vis[i] = true;
                break;
            }
        }
        for(int i = n; i >= 1; i--){
            if(a[i] >= 0 && z == INF){
                z = a[i];
                vis[i] = true;
                break;
            }
        }
        for(int i = 1; i <= n; i++){
            if(!vis[i]){
                if(a[i] >= 0){
                    ans.push_back(Node(fu, a[i]));
                    fu -= a[i];
                }
                else{
                    ans.push_back(Node(z, a[i]));
                    z -= a[i];
                }
            }
        }
        ans.push_back(Node(z, fu));
        printf("%d\n", z - fu);       
    }
    for (int i = 0; i < ans.size(); i++) {
        printf("%d %d\n", ans[i].x, ans[i].y);
    }
    return 0;
}

Measured memory_bytes: 3700
n,k=map(int,input().split())
mod=10**9+7
a=list(map(int,input().split()))
ans=0
for i in range(n):
    l1=0
    l2=0
    for j in range(n):
        if i<j and a[i]>a[j]:
            l1+=1
        if a[i]>a[j]:
            l2+=1
    ans+=l1*k%mod+l2*(k-1)*k//2%mod
    ans%=mod

print(ans)


Measured memory_bytes: 41964
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll,ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node*, Node*>
#define uid uniform_int_distribution
#define FILE ifstream in("C:\\Users\\BOG\\Desktop\\ICPC\\b1.txt");ofstream out("C:\\Users\\BOG\\Desktop\\ICPC\\b1_out.txt");
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int main(){
    fast;
    ll a; cin>>a;
    cout<<a+a*a+a*a*a;
}

Measured memory_bytes: 3616
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;

class Main {
    static final int MOD = 1000000007;
    static final int INF = 1 << 30;
    static final int ALF = 26;

    Main() {
        Scanner sc = new Scanner(System.in);
        long ans = 0;
        int N = sc.nextInt();
        System.out.println(N>=30?"Yes":"No");
    }

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


Measured memory_bytes: 35700
#include <bits/stdc++.h>
using namespace std;
#define REP(i,x,n) for(int i = x; i < n; i++)
#define MAX 100
#define INF 1000000001

#define ad(x) int i = 100;
typedef long long ll;
// enum {
//   AST_INT,
//   AST_SYM,
//   AST_STR,
//   AST_FUNCALL,
// };



// double winProb(int Rp, int Rq) {
//     return 1/(1+pow(10, (Rq-Rp)/400.));
// }

// int R[1<<10];
// double dp[12][1<<10];
int n;
string s;

// int w[30010];
// int e[30010];

int main() {
    cin >> n >> s;
    vector<int> w(n);
    vector<int> e(n);
    REP(i,0,n) {
        if (s[i] == 'W') {w[i] = 1;}
        else {e[i] = 1;}
    }
    REP(i,1,n) {
        w[i] += w[i-1];
        e[i] += e[i-1];
    }
    
    int ans = INF;
    REP(i,0,n) {
        if (i == 0) {ans = min(ans,e[n-1]);}
        else if (i == n - 1) {ans = min(ans,w[i-1]);}
        else {ans = min(ans,w[i-1] + (e[n-1] - e[i]));}
    }

    cout << ans << endl;
}

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

#define sz(x) int(x.size())
#define Task "abc"
#define For(i, a, b) for(int i = (a), _b = (b); i <= b; ++i)
#define All(x) (x).begin(), (x).end()
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;

const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;

int n, m, q;
int root_a[maxn], root_b[maxn];

int getroot(int u, int root[]){
  return u == root[u] ? u : (root[u] = getroot(root[u], root) );
}

int main(){
  ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
//  freopen(Task".inp", "r", stdin); freopen(Task".out", "w", stdout);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; ++i){
    root_a[i] = i; root_b[i] = i;
  }

  for (int i = 0; i < m; ++i){
    int u, v; cin >> u >> v;
    int ru = getroot(u, root_a), rv = getroot(v, root_a);
    if (ru == rv) continue;
    root_a[rv] = ru;
  }

  for (int i = 0; i < q; ++i){
    int u, v; cin >> u >> v;
    int ru = getroot(u, root_b), rv = getroot(v, root_b);
    if (ru == rv) continue;
    root_b[rv] = ru;
  }
  map<pair<int,int> , int> myMap;
  for (int u = 1; u <= n; ++u){
    int ru = getroot(u, root_a), rv = getroot(u, root_b);
    myMap[mp(ru, rv)]++;
  }
  for (int u = 1; u <= n; ++u){
    cout << myMap[mp(root_a[u], root_b[u])] <<' ';
  }
}



Measured memory_bytes: 13568
def solve():
    n, m = map(int, input().split())
    A = sorted(map(int, input().split()))
    BC = []
    for _ in range(m):
        b, c = map(int, input().split())
        BC.append([c, b])
    BC.sort(reverse=True)
    i = 0
    flg = False
    for c, b in BC:
        for a in range(i, min(i + b, n)):
            if A[a] < c:
                A[a] = c
            else:
                flg = True
                break
        if flg:
            break
        i += b
    print(sum(A))
solve()


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

int main(){
    char pattern[8][8];
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            cin >> pattern[i][j];
        }
    }
    
    cout << 90 << endl;
    for(int i=0;i<8;i++){
        for(int j=7;j>=0;j--){
            cout << pattern[j][i];
        }
        cout << endl;
    }
    
    cout << 180 << endl;
    for(int i=7;i>=0;i--){
        for(int j=7;j>=0;j--){
            cout << pattern[i][j];
        }
        cout << endl;
    }
    
    cout << 270 << endl;
    for(int i=7;i>=0;i--){
        for(int j=0;j<8;j++){
            cout << pattern[j][i];
        }
        cout << endl;
    }
}

Measured memory_bytes: 1136
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.B
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var s = ReadText();
            var firstAIndex = s.IndexOf("A");
            var rev = new string(s.Reverse().ToArray());
            var lastZIndex = rev.IndexOf("Z");

            return s.Length - lastZIndex - firstAIndex;
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


Measured memory_bytes: 28744
n, m = map(int, input().split())
sc = []
for i in range(m):
    sc.append(list(map(int,input().split())))
sc = sorted(sc, key=lambda x:x[0])
#print(sc)
ans = [0] * (n+1)
lis = [-1] * (n+1)
for i in range(m):
    if lis[sc[i][0]] == -1:
        lis[sc[i][0]] = sc[i][1]
    if lis[sc[i][0]] != -1 and lis[sc[i][0]] != sc[i][1]:
        print('-1')
        exit()
    if sc[i][0] == 1 and sc[i][1] == 0 and n > 1:
        print('-1')
        exit()
for i in range(m):
    ans[sc[i][0]] = sc[i][1]
del ans[0]
if ans[0] == 0 and n > 1:
    ans[0] = 1
print(''.join(map(str, ans)))

Measured memory_bytes: 3064
S = input()
if  S=="SUN":
  print(7)
elif S=="MON":
  print(6)
elif S=="TUE":
  print(5)
elif S=="WED":
  print(4)
elif S=="THU":
  print(3)
elif S=="FRI":
  print(2)
else:
  print(1)

Measured memory_bytes: 2940
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll,ll>
#define vvll vector< vector<ll> >
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define tel(a){cout<<a<<"\n";}
#define tell(a,b){cout<<a<<" | "<<b<<"\n";}
#define telll(a,b,c){cout<<a<<" | "<<b<<" | "<<c<<"\n";}
#define teln(v,n){cout<<"v- ";rep(i,0,n)cout<<v[i]<<" ";cout<<"\n";}
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using o_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define M 1000010

int main(){
	IOS;
	
	ll a,b;cin>>a>>b;
	ll l= a*b/(__gcd(a,b));
	cout<<l;
	return 0;
}



Measured memory_bytes: 256
# coding: utf-8

s = input().split()

# 最後の一文字
saigo = []
for i in range(2):
    tmp = s[i][len(s[i]) - 1:]
    saigo.append(tmp)

# しりとり判定
if saigo[0] == s[1][0:1] and saigo[1] == s[2][0:1]:
    print ("YES")
else:
    print ("NO")

Measured memory_bytes: 3060
N = int(input())
s = input()
Sww = 0
Se = 0
for i in range(N) :
    if s[i] == 'E' :
        Se+=1
sum = N+10
for i in range(N) :
    if s[i] == 'E' :
        Se-=1
    sum1 = Sww + Se
    sum = min(sum1,sum)
    if s[i] == 'W' :
        Sww+=1
print(sum)

Measured memory_bytes: 3676
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	string a,b;
	cin>>a>>b;
	if(a[0]==b[2] && a[2]==b[0] && a[1]==b[1]){
		cout<<"YES"<<endl;
	}
	
	else cout<<"NO"<<endl;
	
}

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

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

#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 pb push_back
#define fill(x, y) memset(x, y, sizeof(x))
#define even(x) x % 2 == 0
#define odd(x) x % 2 != 0
#define all(x) x.begin(), x.end()
#define pcnt __builtin_popcount
#define buli(x) __builtin_popcountll(x)
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define IN1(x) ll x; cin >> x;
#define OUT1(x) cout << x << endl;

// these functions return the position of result of Binary Search.
#define LB(s, t, x) (int) (lower_bound(s, t, x) - s)
#define UB(s, t, x) (int) (upper_bound(s, t, x) - s)

ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[] = { 1, -1, 0, 0, 1, 1, -1, -1 }, dy8[] = { 0, 0, -1, 1, -1, 1, -1, 1 };

template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts>
auto make_v(size_t a, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); }
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); }
// auto dp = make_v<int>(4, h, w);
// fill_v(dp, 0);

const ll INF_LL = (1ll << 60);
const int INF_INT = (int)1e9;
const ll MOD_CONST = (ll)(1e9 + 7);

template <typename T>
vector<T> pows(int n) {
	vector<T> ret;
	T x = 1;
	while (ret.size() < n) {
		ret.push_back(x);
		x *= 10;
	}
	return ret;
}

template <class T>
bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T>
bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

inline tuple<ll, ll> rotate45(tuple<ll, ll> point) { ll x = get<0>(point), y = get<1>(point); return tuple<ll, ll>(x + y, x - y); }
inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; }

template <typename T>
void out(T o) { cout << o << endl; }
template <typename T>
void out(vector<T> &out) { REP(i, (int)out.size()) { cout << out[i]; if (i == (int)out.size() - 1) cout << endl; else cout << " "; } }



int main(void)
{
	cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20);

	IN1(N); IN1(K);
	
	ll k0 = 0, k02 = 0;
	REP(i, N) {
		ll n = i + 1;
		if (n % K == 0) k0++;
		if (n % K == (K/2)) k02++;
	}

	ll ans = 0;
	if (even(K)) ans = k0 * k0 * k0 + k02 * k02 * k02;
	else ans = k0 * k0 * k0;
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
n = gets.to_i
blue_cards = []
n.times { blue_cards << gets.chomp.to_s }
m = gets.to_i
red_cards = []
m.times { red_cards << gets.chomp.to_s }
kinds = (blue_cards + red_cards).uniq
results = kinds.map { |kind| blue_cards.count(kind) - red_cards.count(kind) }
results << 0
puts results.max


Measured memory_bytes: 1788
#include <cstdio>

using namespace std;

int get(int y, int m, int d)
{
	int ret = 0;
	ret += (y - 1) * (20 + 19) * 5 + (y - 1) / 3 * 5;
	
	for (int i = 1; i < m; i++){
		ret += 19 + (i % 2 || y % 3 == 0);
	}
	
	return (ret + d);
}

int main()
{
	int T;
	
	scanf("%d", &T);
	
	while (T--){
		int y, m, d;
		scanf("%d %d %d", &y, &m, &d);
		printf("%d\n", get(1000, 1, 1) - get(y, m, d));
	}
	
	return (0);
}

Measured memory_bytes: 1024
macro_rules! read_line {
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim_right().to_owned()
    }};
}

macro_rules! parse_next {
    ($ws:expr) => {
        $ws.next().unwrap().parse().unwrap();
    };
}

fn main() {
    let (n, m, c) = {
        let line = read_line!();
        let mut ws = line.split_whitespace();
        let n: i32 = parse_next!(ws);
        let m: i32 = parse_next!(ws);
        let c: i32 = parse_next!(ws);
        (n, m, c)
    };
    let b: Vec<i32> = read_line!()
        .split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();

    let mut ans = 0;

    for _ in 0..n {
        let a: Vec<i32> = read_line!()
            .split_whitespace()
            .map(|x| x.parse().unwrap())
            .collect();
        let w: i32 = (0..m as usize).map(|mi| a[mi] * b[mi]).sum();
        if w + c > 0 {
            ans += 1;
        }
    }

    println!("{}", ans);
}

Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, m; cin >> n >> m;
	multiset<int> v;
	for(int i = 0; i < n; i++){
		int x; cin >> x;
		v.insert(x);
	}
	for(int i = 0; i < m; i++){
		int val = *prev(v.end()); v.erase(prev(v.end()));
		val /= 2;
		v.insert(val);
	}
	long long ans = 0;
	for(auto it : v) ans += it;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;
 
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }
 
void iostream_init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed);
    cout.precision(12);
}
 
class Knapsack {
    typedef long long LL;
    struct item {
        LL v, w;
    };
 
    LL W;
    vector<item> items;
    LL lv;
 
    LL solve_rec(size_t k, LL v, LL w) {
        if (w + items[k].w > W) return solve_rec(k+1, v, w);
        LL cv = v, cw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (cw + items[i].w <= W) {
                cw += items[i].w;
                cv += items[i].v;
            }
        }
        if (lv < cv) lv = cv;
        double fv = v, fw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (fw + items[i].w <= W) {
                fw += items[i].w;
                fv += items[i].v;
            } else {
                fv += items[i].v * (W - fw) / items[i].w;
                break;
            }
        }
        if (fv - lv < 1 || fv < lv) return lv;
        solve_rec(k+1, v+items[k].v, w+items[k].w);
        return solve_rec(k+1, v, w);
    }
 
    public:
    // 価値v, 重みwのアイテムを追加
    void add_item(LL v, LL w) {
        items.push_back({v, w});
    }
 
    // 重みの合計がW以下になるようにアイテムを選んだときの価値の和の最大値を返す．
    LL solve(LL W_) {
        if(items.empty()) return 0;
        W = W_;
        sort(items.begin(), items.end(), [](const item &a, const item &b) {
                return a.v * b.w > a.w * b.v;
                });
        lv = 0;
        return solve_rec(0, 0, 0);
    };
};
 
int main(){
    iostream_init();
    int N, W;
    cin >> N >> W;
    Knapsack dp;
    for (int i = 0; i < N; i++) {
        int w, v;
        cin >> w >> v;
        dp.add_item(v, w);
    }
 
    cout << dp.solve(W) << endl;
    return 0;
}


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

int main(){
    int n,a[100],count=0,i;
    cin>>n;
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    bool flag=true;
    while(flag){
        flag = false;
        for(i=n-1;i>0;i--){
            if(a[i]<a[i-1]){
                int z=a[i];
                a[i]=a[i-1];
                a[i-1]=z;
                flag=true;
                count++;
            }
        }
    }
    for(i=0;i<n;i++){
        cout <<a [i];
        if(i==n-1){
            cout<<endl;
        }else{
            cout<<' ';
        }
    }
    cout << count << endl;
    return 0;
}


Measured memory_bytes: 3088
import sys
import numpy as np
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall, shortest_path

input = sys.stdin.readline

def main():
    n, m, l = map(int, input().split())
    wf = [[0 for i in range(n)] for j in range(n)]
    for _ in range(m):
        a, b, c = map(int, input().split())
        wf[a-1][b-1] = wf[b-1][a-1] = c
    wf = np.array(wf)
    for i in range(n):
        wf[i][i] = 0
    wf = shortest_path(wf, method='FW')
    for i in range(n):
        for j in range(n):
            if wf[i][j] <= l:
                wf[i][j] = 1
            else:
                wf[i][j] = 301
    wf = shortest_path(wf, method='FW')
    q = int(input())
    ans = [None] * q
    for i in range(q):
        s, t = map(int, input().split())
        if wf[s-1][t-1] < 301:
            ans[i] = int(wf[s-1][t-1])
        else:
            ans[i] = 0
    for i in range(q):
        print(ans[i] - 1)

if __name__ == "__main__":
    main()

Measured memory_bytes: 18984
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

#include <algorithm>
#include <numeric>
#include <cmath>

#include <queue>
#include <set>

using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)

///////////////////////////////////////////////////
// -------------------- I/O -------------------- //
///////////////////////////////////////////////////
// https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords
template<typename T> void outputVec(const std::vector<T>& vec, const int precision = 0) {
	if (precision > 0) std::cout << std::fixed << std::setprecision(precision);
	for (auto it = vec.begin(); it != vec.end() - 1; ++it) { std::cout << *it << " "; }
	std::cout << *(vec.end() - 1) << "\n";
}
template<typename T> void outputVal(const T& res, const int precision = 0) {
	if (precision > 0) std::cout << std::fixed << std::setprecision(precision);
	std::cout << res << "\n";
}
template<typename T> T inputVal() { T a; std::cin >> a; return a; }
template<typename T> std::vector<T> inputVec(T N) {
	std::vector<T> vec(N);
	for (auto it = vec.begin(); it != vec.end(); ++it) { std::cin >> *it; }
	return vec;
}
template<typename T> void inputVec(std::vector<T>& vec) {
	for (auto it = vec.begin(); it != vec.end(); ++it) { std::cin >> *it; }
}

///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
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; }

///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////
void Main() {
	const int N = inputVal<int>();
	const int M = inputVal<int>();

	vector<vector<int> > jobs(M+1);

	rep(i, N) {
		int a, b; cin >> a >> b;
		if (a > M) continue;
		jobs[a].push_back(b);
	}

	ll money = 0;
	std::priority_queue<int> que;
	for (int i = 1; i <= M; ++i) {
		for (auto j : jobs[i]) { que.push(j); }
		if (!que.empty()) {
			money += que.top();
			que.pop();
		}
	}

	cout << money << endl;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout << std::fixed << std::setprecision(15);
	Main();

	double tmp;
	cin >> tmp;

	return 0;
}


Measured memory_bytes: 5632
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i<(int)n; ++i)
#define forr(i, t, n) for (int i = t; i<n; ++i)
#define rmod(x, y) (((x%y)+y)%y)
#define pb push_back
#define emp emplace_back
#define ppb pop_back
#define len(v) int(v.size())
#define all(v) (v).begin(),(v).end()
#define ri(x) scanf("%d",&(x))
#define rii(x,y) scanf("%d %d",&(x),&(y))
#define riii(x,y,z) scanf("%d %d %d",&(x),&(y),&(z))
#define rl(x) scanf("%lld",&(x))
#define rll(x,y) scanf("%lld %lld",&(x),&(y))
#define rlll(x,y,z) scanf("%lld %lld %lld",&(x),&(y),&(z))
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
void fastio(){ios_base::sync_with_stdio(false); cin.tie(0);}

const long double PI = acos(-1);
const long long MOD = 1000000000 +7;
const int maxn = 1024;


int main() {
	int n; ri(n);
 	printf("%d\n", n*n);
	
	
	return 0;
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func sum(a []int) int {
	total := 0
	for _, x := range a {
		total = (total + x) % CYCLE
	}
	return total
}

func max(a int64, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func parseInt(s string) int {
	return int(parseInt64(s))
}

func parseInt64(s string) int64 {
	x, _ := strconv.ParseInt(s, 10, 64)
	return x
}

var reader *bufio.Reader

func readLine() string {
	s, _ := reader.ReadString('\n')
	return strings.TrimSuffix(s, "\n")
}

var CYCLE = 1000000007

var result [65536]int64
var a [16][16]int64
var n int

func total(mask int) int64 {
	var r int64
	for i := 0; i < n-1; i++ {
		for j := i + 1; j < n; j++ {
			if mask&(1<<uint(i)) != 0 && mask&(1<<uint(j)) != 0 {
				r += a[i][j]
			}
		}
	}
	return r
}

func solve(mask int) int64 {
	if mask == 0 {
		panic("mask must be non-zero")
	}

	if r := result[mask]; r >= 0 {
		// fmt.Printf("%b->%d\n", mask, r)
		return r
	}

	x := mask
	r := total(mask)
	for true {
		x = (x - 1) & mask
		if x == 0 {
			break
		}
		y := mask ^ x
		s := solve(x) + solve(y)
		if s > r {
			r = s
		}
	}
	result[mask] = r
	// fmt.Printf("%b->%d\n", mask, r)
	return r
}

func main() {
	reader = bufio.NewReader(os.Stdin)
	n = parseInt(readLine())

	for i := 0; i < 1<<uint(n); i++ {
		result[i] = -1
	}

	for i := 0; i < n; i++ {
		line := readLine()
		for j, token := range strings.Fields(line) {
			a[i][j] = parseInt64(token)
		}
	}

	for i := 0; i < n; i++ {
		ii := 1 << uint(i)
		for j := i; j < n; j++ {
			a := max(a[i][j], 0)
			result[ii|1<<uint(j)] = a
		}
	}

	fmt.Println(solve(1<<uint(n) - 1))
}


Measured memory_bytes: 1152
n, m = gets.split.map(&:to_i)
ab = m.times.map { gets.split.map(&:to_i) }
i_1 = ab.select{|a, b| a == 1}.map{|x|x[1]}
i_n = ab.select{|a, b| b == n}.map{|x|x[0]}
puts ((i_1 & i_n).size > 0 ? '' : 'IM') + 'POSSIBLE'

Measured memory_bytes: 25852
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<utility>
#include<cmath>
#include<assert.h>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<complex>
#include<bitset>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int A, B, C;

signed main() {
    cin >> A >> B >> C;
    if (A == B && B == C) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}


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

typedef pair<int, string> pis;

const string S[10] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
const int C[10] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
int N, M;
vector<pis> A, P;
vector<string> dp;

void input_init(void) {
	cin >> N >> M;
	A.resize(M);
	for (int i = 0; i < M; ++i) {
		int a;
		cin >> a;
		A[i] = make_pair(C[a], S[a]);
	}
	sort(A.begin(), A.end());
	int p = 0;
	P.push_back(A[0]);
	for (int i = 1; i < M; ++i) {
		if (A[i].first == A[i - 1].first) {
			P[p] = A[i];
		}
		else {
			P.push_back(A[i]);
			++p;
		}
	}
}

void solve(void) {
	int sz = max(N, 7);
	dp.resize(sz + 1, "0");
	for (int i = 0; i < (int)P.size(); ++i)
		dp[P[i].first] = P[i].second;
	for (int i = 2; i <= N; ++i) {
		for (int j = 0; j < (int)P.size(); ++j) {
			int pre = i - P[j].first;
			if (pre < 0) continue;
			if (dp[pre] == "0") continue;
			string n_str = P[j].second + dp[pre];
			string p_str = dp[i];
			if (n_str.length() > p_str.length())
				dp[i] = n_str;
			else if (n_str.length() == p_str.length()) {
				if (n_str[0] > p_str[0])
					dp[i] = n_str;
			}
		}
	}
}

int main(void) {
	input_init();
	solve();
	cout << dp[N] << '\n';
	return 0;
}


Measured memory_bytes: 29824
#include <iostream>

using namespace std;

int main() {
    int x, n;

    int ans = 0;
    while (1) {
        cin >> n >> x;
        if (n == 0 && x == 0)
            break;
        ans = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = i+1; j <= n; j++) {
                for (int k = j+1; k <= n; k++) {
                    if (x == i + j + k) {
                        ans++;
                    }
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 3112
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<unordered_map>
#include<array>
#include<map>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme cout<<-1<<endl;return 0
//ios::sync_with_stdio(false);
//<< setprecision(5)
const int mod=1e9+7;
const llint big=1e15+100;
const long double pai=3.141592653589793238462643383279502884197;
const long double ena=2.71828182845904523536;
const long double eps=1e-7;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
int main(void){
	string str;cin>>str;
	if(str[0]=='A'&&str[1]=='C'){cout<<"Yes"<<endl;}
	else if(str.size()>2&&str[1]=='A'&&str[2]=='C'){cout<<"Yes"<<endl;}
	else if(str.size()>3&&str[2]=='A'&&str[3]=='C'){cout<<"Yes"<<endl;}
	else if(str.size()>4&&str[3]=='A'&&str[4]=='C'){cout<<"Yes"<<endl;}
	else{cout<<"No"<<endl;}
}

Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int[] a = new int[T];
        for (int i = 0; i < T; i++) {
            a[i] = sc.nextInt();
        }
        System.out.println(IntStream.of(a).reduce(0,(right,left) -> right + left -1));
    }
}


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

int main(){
  
  int N, A, x;
  cin >> N;
  cin >> A;
  
  x = N - N / 500 * 500;
  
  if (x <= A){
    cout << "Yes" <<endl;
  }
  else{
    cout << "No" <<endl;
  }
  
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define pb push_back
#define int long long int
const int mod=1e9+7;
using namespace std;

signed main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int n; cin>>n;
    int v[n], p[n];
    for(int i=0; i<n; i++){
        cin>>v[i];
    }
    int s=0;
    for(int i=n-1; i >=0; i--){
        s =(s+v[i])%mod;

        p[i]=s;
    }
    int q=0;
    for(int i=0; i<n-1; i++){
        q =(q+((v[i]*(p[i+1]))%mod))%mod;
    }
    cout<<q<<endl;
}

Measured memory_bytes: 6748
n=int(input())
ans=0
for i in range(1,n+1):
    ans += i*(i+1)/2
for i in range(n-1):
    u,v=map(int,input().split())
    if u > v:
        u,v=v,u
    ans -= u*(n-v+1)
print(int(ans))

Measured memory_bytes: 74284
#include<bits/stdc++.h>
using namespace std;
 
#define int long long 
#define ll long long 
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define ff first
#define ss second
#define vi vector<ll>
#define vii vector<vector<ll>>
#define vpi vector<pii>
#define mod 1000000007
#define MOD 1000000007
#define full(a) a.begin(),a.end()
#define inf INT_MAX
#define minf INT_MIN
#define dd double
#define fori(x,n) for(ll i=x;i<n;i++)
#define modd 998244353
#define umap unordered_map<ll,ll>
#define mmap map<ll,ll>
#define mm 100000000
ll n,m;
pii D;
ll h[4]={1,-1,0,0};
ll v[4]={0,0,1,-1};
char a[1001][1001];

// 0-1 BFS

ll dijkstra(ll x,ll y){

  ll distance[n+1][m+1];
  fori(1,n+1)
  for(int j=1;j<=m;j++)distance[i][j]=1e9;

  distance[D.ff][D.ss]=0;
  deque<pii>s;
  s.push_back(D);

  while(!s.empty()){
    auto p = *(s.begin());
    pii node = p;
    ll node_dist = distance[node.ff][node.ss];

    s.erase(s.begin());

    //checking for edges of cost 0
    fori(0,4){
      ll row = node.ff+h[i],col=node.ss+v[i];
      if(row>=1 and row<=n and col>=1 and col<=m and a[row][col]=='.' and node_dist<distance[row][col]){
        distance[row][col]=node_dist;
        s.push_front({row,col});    // at same level
      }
    }
     
    //checking for edges of cost 1
    for(int row=node.ff-2;row<=node.ff+2;row++)
      for(int col=node.ss-2;col<=node.ss+2;col++){
         if(row>=1 and row<=n and col>=1 and col<=m and a[row][col]=='.' and node_dist+1<distance[row][col]){
         distance[row][col]=node_dist+1;
        s.push_back({row,col});   // at higher level
      }
    }
  }

  return distance[x][y]==1e9?-1:distance[x][y];
}





int32_t main() 
{ 
  
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif


    
    ll t;
    t=1;
    // cin>>t;
    while(t--){
      cin>>n>>m;
      cin>>D.ff>>D.ss;
      ll x,y;
      cin>>x>>y;

      fori(1,n+1)
      for(int j=1;j<=m;j++)cin>>a[i][j];

      cout<<dijkstra(x,y);

      
    }





return 0;
}     



Measured memory_bytes: 12596
#!/usr/bin/env python
import re

s = input()
STR = 'AKIHABARA'
if len(s) > len(STR):
    print('NO')
    exit()

regex = re.compile('^A?KIHA?BA?RA?')
mo = regex.search(s)
if mo == None:
    print('NO')
else:
    print('YES')


Measured memory_bytes: 9924