akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(void){
    int n,m,x = 0;
    vector<int>fee;
    cin >> n >> m >> x;
    for (int i = 0; i < m; i++)
    {
        int buf = 0;
        cin >> buf;
        fee.push_back(buf);
    }
    
    int countz = 0;
    for (int i = x; i > 0; i--)
    {
        auto resz = find(fee.begin(),fee.end(),i);
        if(resz != fee.end()){
            countz++;
        }
    }

    int countn = 0;
    for (int i = x; i < n; i++)
    {
        auto resn = find(fee.begin(),fee.end(),i);
        if(resn != fee.end()){
            countn++;
        }
    }
    
    if(countz <= countn){
        cout << countz << endl;
    }else{
        cout << countn << endl;
    }
    
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(i = 0; i < n; i++)
//2D::rep(i,j)rep(j,j)cin>>bin[i][j];
//vector<int> a(n)
#define Graph vector<vector<int>>
#define A1 cout<<"No"<<endl
#define A2 cout<<"Yes"<<endl
#define A3 cout<<ans<<
#define AA(i) cout<<i<<endl;
const int U=2e5+5;
const ll MOD=1000000007;
const int INF = 1001001001;

struct UnionFind {
    vector<int> r;
 
    UnionFind(int N) {
        r = vector<int>(N, -1);
    }
 
    int root(int x) {
        if (r[x] < 0) return x;
        return r[x] = root(r[x]);
    }
 
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (r[x] > r[y]) swap(x, y);
        r[x] += r[y];
        r[y] = x;
        return true;
    }
 
    int size(int x) {
        return -r[root(x)];
    }
};
 
 
int main() {
    int N, M;
    cin >> N >> M;
 
    UnionFind UF(N);
    for (int i = 0; i < M; i++)
    {
        int A, B;
        cin >> A >> B;
        A -= 1; B -= 1;
        UF.unite(A, B);
    }
 
    int ans = 0;
    for (int i = 0; i < N; i++)
    {
        if(UF.r[i]<0) ans++;
    }
 
    cout << ans-1 << endl;
}

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


int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int a,b;
  cin>>a>>b;
  if((b-1)%(a-1)==0){
    cout<<(b-1)/(a-1);
  }else{
    cout<<(b-1)/(a-1)+1;
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
int N;
LL dp[4][1000011];
LL MOD=1e9+7;

int main(){
	cin>>N;
	dp[0][0]=1;
	dp[1][0]=0;
	dp[2][0]=0;
	dp[3][0]=0;
	REP(i,N){
		dp[0][i+1]=8*dp[0][i];
		dp[1][i+1]=9*dp[1][i]+dp[0][i];
		dp[2][i+1]=9*dp[2][i]+dp[0][i];
		dp[3][i+1]=10*dp[3][i]+dp[1][i]+dp[2][i];
		dp[0][i+1]%=MOD;
		dp[1][i+1]%=MOD;
		dp[2][i+1]%=MOD;
		dp[3][i+1]%=MOD;
	}
	cout<<dp[3][N]<<endl;
	return 0;
}

Measured memory_bytes: 34624
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#include <queue>
#include <math.h>
using namespace std;
#define N 10000000000000

int main(){
    long n;
    cin>>n;
    long num[n];
    long maxnum=0;
    for (int i=0; i<n; i++){
        cin>>num[i];
    }
    for (int i=0; i<n; i++){
        if (maxnum<num[i]){
            maxnum=num[i];
        }
    }
    int count=0;
    for (int i=0; i<n; i++){
        if (num[i]==maxnum){
            count+=1;
        }
    }
    if (count==1){
        int second=0;
        for (int i=0; i<n; i++){
            if (second<num[i] && num[i]!=maxnum){
                second=num[i];
            }
        }
        for (int i=0; i<n; i++){
            if (num[i]==maxnum){
                cout<<second<<endl;
            }
            else{
                cout<<maxnum<<endl;
            }
        }
    }
    else{
        for (int i=0; i<n; i++){
            cout<<maxnum<<endl;
        }
    }
}

Measured memory_bytes: 3200
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>

using namespace std;

typedef long long ll;

typedef std::pair<int, int> ipair;
bool lessPair(const ipair& l, const ipair& r){return l.second < r.second;}
bool morePair(const ipair& l, const ipair& r){return l.second > r.second;}

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;
}

const ll MOD = 1e9 + 7;
// const long long INF = 1LL<<60;
void add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; }
void sub(long long &a, long long b) { a -= b; if (a < 0) a += MOD; }
void mul(long long &a, long long b) { a *= b; a %= MOD; }
ll llmin(ll a, ll b) { if (a < b) return a; else return b; }
ll llmax(ll a, ll b) { if (a < b) return b; else return a; }
ll llabs(ll a) { if (a >= 0) return a; else return - a; }
ll llmodpow(ll a, ll n) {
   if (n == 0) return 1;
   ll tmp = llmodpow(a, n / 2);
   mul(tmp, tmp);
   if (n & 1) mul(tmp, a);
   return tmp;
}

int main() {
   int N;
   cin >> N;
   int A[N], B[N];
   for (int i = 0; i < N; i++) {
      cin >> A[i];
      B[i] = A[i];
   }
   sort(A, A + N);
   int MAX = A[N - 1];
   for (int i = 0; i < N; i++) {
      if (B[i] == A[N - 1]) cout << A[N - 2] << endl;
      else cout << MAX << endl;
   }

   return 0;
}


Measured memory_bytes: 3200
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define dunk(a) cout << (a) << endl
using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vector<int> t(n + 1), x(n + 1), y(n + 1);
	t[0] = 0;
	x[0] = 0;
	y[0] = 0;
	for (int i = 1; i < n + 1; ++i) cin >> t[i] >> x[i] >> y[i];

	if (n == 1) {
		int a = x[1] + y[1];
		if (a > t[1]) puts("No");
		else puts("Yes");
		return 0;
	}

	for (int i = 1; i < n + 1; ++i) {
		int a = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
		if (a > t[i] - t[i - 1] || a % 2 != (t[i] - t[i - 1])%2) {
			puts("No");
			return 0;
		}
	}

	puts("Yes");

	return 0;
}

Measured memory_bytes: 1408
#

import sys
input=sys.stdin.readline

MOD=10**9+7
kl = [0]*(10**6+1)

            
def main():
    N=int(input())
    A=list(map(int,input().split()))
    if N==1:
        print(1)
        exit()
    for j in range(N):
        n=A[j]
        for i in range(2,int((n)**0.5)+1):
            if n%i==0:
                cnt=0
                while(n%i==0):
                    n//=i
                    cnt+=1
                if kl[i]<cnt:
                    kl[i]=cnt
        if n!=1:
            if kl[n]<1:
                kl[n]=1
                
    lcm=1
    for i in range(2,10**6+1):
        lcm*=pow(i, kl[i], MOD)

    modsum=0
    for i in range(N):
        modsum+=pow(A[i],MOD-2,MOD)

    ans = lcm*modsum%MOD
    print(ans)
    
    
    
if __name__=="__main__":
    main()


Measured memory_bytes: 58204
fun main(args: Array<String>) {
    val A = readLine()!!.toInt()
    val B = readLine()!!.toInt()
    val C = readLine()!!.toInt()
    val X = readLine()!!.toInt()
    var pattern = 0
    for (a in 0..A) {
        for (b in 0..B) {
            for (c in 0..C) {
                val total = 500*a + 100*b + 50*c
                if (total == X) pattern++
            }
        }
    }
    println(pattern)
}

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

int main() {
    string S;
    cin >> S;
    int N = S.size();
    string rs = S;
    reverse(all(rs));
    bool ans = true;
    if(S != rs) ans = false;

    string s2 = S.substr(0,(N-1)/2);
    string rs2 = s2;
    reverse(all(rs2));
    if(s2 != rs2) ans = false;

    string s3 = S.substr((N+3)/2-1);
    string rs3 = s3;
    reverse(all(rs3));
    if(s3 != rs3) ans = false;
    
    cout << (ans?"Yes":"No") << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  vector<bool> S(13, false);
  vector<bool> H(13, false);
  vector<bool> C(13, false);
  vector<bool> D(13, false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++){
    char s;
    int r;
    cin >> s >> r;
    if (s == 'S'){
      S[r - 1] = true;
    }
    if (s == 'H'){
      H[r - 1] = true;
    }
    if (s == 'C'){
      C[r - 1] = true;
    }
    if (s == 'D'){
      D[r - 1] = true;
    }
  }
  for (int r = 1; r <= 13; r++){
    if (S[r - 1] == false){
      cout << "S " << r << endl;
    }
  }
  for (int r = 1; r <= 13; r++){
    if (H[r - 1] == false){
      cout << "H " << r << endl;
    }
  }
  for (int r = 1; r <= 13; r++){
    if (C[r - 1] == false){
      cout << "C " << r << endl;
    }
  }
  for (int r = 1; r <= 13; r++){
    if (D[r - 1] == false){
      cout << "D " << r << endl;
    }
  }
}


Measured memory_bytes: 3100
# coding: utf-8
import sys
import decimal
input = sys.stdin.readline
decimal.getcontext().prec = 13
dec = decimal.Decimal

s = input().split(" ")
d = int(s[0])
k = int(s[1])

sums = 0
for i in range(d):
    x = i + 1
    j = 0
    while x < k: 
        x = x * 2
        j = j+1
    sums = sums + dec(1)/dec(d) * (dec(1) /dec(2) ** dec(j))

print(sums)
#0.145833333333

Measured memory_bytes: 97108
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <bitset>
using namespace std;

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

int dx4[4]={1,0,-1,0};
int dy4[4]={0,-1,0,1};
int dx8[8]={1,0,-1,1,-1,1,0,-1};
int dy8[8]={1,1,1,0,0,-1,-1,-1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;

const int INF = 1e9;
const int MAX_V = 1e6+1;
const ll mod = 1000000007;

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

int a, b;

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

  cin >> a >> b;
  cout << (((a * b) % 2) ? "Yes" : "No") << endl;
}


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

void show(int A[], int N)
{
    for (int i = 0; i < N; i++) {
        if (i == N - 1)
            cout << A[i] << endl;
        else
            cout << A[i] << ' ';
    }
}

void insertionSort(int A[], int N)
{
    show(A, N);
    for (int i = 1; i < N; i++) {
        int j = i - 1, v = A[i];
        while (j >= 0 && A[j] > v) {
            A[j + 1] = A[j];
            j--;
        }
        A[j + 1] = v;
        show(A, N);
    }
}

int main()
{
    int *arr, n;

    cin >> n;
    arr = new int[n];

    for (int i = 0; i < n; i++)
        cin >> arr[i];

    insertionSort(arr, n);

    delete[] arr;  
}


Measured memory_bytes: 3104
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

char solve(map<char, string>& mp) {
  char target = 'a';
  while (!mp[target].empty()) {
    char next_target = *(mp[target].end() - 1);
    mp[target].pop_back();
    target = next_target;
  }
  return toupper(target);
}

int main() {
  map<char, string> mp;
  REP(i, 3) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    mp.insert(make_pair('a' + i, s));
  }
  cout << solve(mp) << endl;
}


Measured memory_bytes: 256
x1,y1,x2,y2 = map(int,input().split())
x3 = x2 + (y1 - y2)
y3 = y2 + (x2 - x1)
x4 = x3 + (y2 - y3)
y4 = y3 + (x3 - x2)
print(x3,y3,x4,y4)

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

int main(){
  string n;
  cin >> n;
  for(int i=0;i<n.size();i++){
    if(n.at(i)=='1'){
      n.at(i)='9';
    }else if(n.at(i)=='9'){
      n.at(i)='1';
    }
  }
  cout << n << endl;
  return 0;
}


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

using namespace std;

bool field[5][5][5];

inline bool inField(int x,int y,int z){
  if(0 > x || x >= 5) return false;
  if(0 > y || y >= 5) return false;
  if(0 > z || z >= 5) return false;
  return true;
}

int getNum(int x,int y,int z,bool f){
  int res = 0;
  for(int zz = z-1 ; zz <= z+1 ; zz++){
    for(int yy = y-1 ; yy <= y+1 ; yy++){
      for(int xx = x-1 ; xx <= x+1 ; xx++){
        if(xx == x && yy == y && zz == z) continue;
        if(!inField(xx,yy,zz)) continue;
        if(field[yy][xx][zz] == f){
          res++;
        }
      }
    }
  }
  return res;
}

int main(){
  int N,Tc = 0;
  bool space = false;
  while(cin >> N, N > 0){
    if(space) cout << endl;
    else space = true;
    for(int z = 0 ; z < 5 ; z++){
      string str;
      for(int y = 0 ; y < 5 ; y++){
        cin >> str;
        for(int x = 0 ; x < 5 ; x++){
          field[y][x][z] = str[x]-'0';
        }
      }
    }
    int M[2],a[27],b[27];
    cin >> M[0];
    for(int i = 0 ; i < M[0] ; i++){
      cin >> a[i];
    }
    cin >> M[1];
    for(int i = 0 ; i < M[1] ; i++){
      cin >> b[i];
    }
    while(N--){
      bool next[5][5][5];
      for(int z = 0 ; z < 5 ; z++){
        for(int y = 0 ; y < 5 ; y++){
          for(int x = 0 ; x < 5 ; x++){
            int num;
            bool f = field[y][x][z];
            if(f){
              num = getNum(x,y,z,true);
              bool ok = false;
              for(int i = 0 ; i < M[1] ; i++){
                if(num == b[i]){
                  ok = true;
                  break;
                }
              }
              if(!ok){
                f = !f;
              }
            }else{
              num = getNum(x,y,z,true);
              bool ok = false;
              for(int i = 0 ; i < M[0] ; i++){
                if(num == a[i]){
                  ok = true;
                  break;
                }
              }
              if(ok){
                f = !f;
              }
            }
            next[y][x][z] = f;
          }
        }
      }
      for(int z = 0 ; z < 5 ; z++){
        for(int y = 0 ; y < 5 ; y++){
          for(int x = 0 ; x < 5 ; x++){
            field[y][x][z] = next[y][x][z];
          }
        }
      }
    }
    cout << "Case " << ++Tc << ":" << endl;
    for(int z = 0 ; z < 5 ; z++){
      for(int y = 0 ; y < 5 ; y++){
        for(int x = 0 ; x < 5 ; x++){
          cout << field[y][x][z];
        }
        cout << endl;
      }
      if(z != 4){
        cout << endl;
      }
    }
  }
  return 0;
}

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

	public static void main(String[] args) {
		Scanner st=new Scanner(System.in);
		int a=st.nextInt();
		String s=st.next();
		if(a>=3200)
			System.out.println(s);
		else {
			s="red";
			System.out.println(s);
		}
	}

}


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

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i < (n); i++)
#define all(a) begin(a),end(a)
template<class T> istream& operator >> (istream& s, vector<T>& v)
{ for (T& x: v) { s >> x; } return s;}
template<class T> void cout_h(const vector<T>& c, int n=INT_MAX)
{ rep(i, min(n, (int)c.size())) cout << c[i] << (i==c.size()-1?"\n":" "); }
template<class T> inline bool chmin(T& a, T b) {return a>b?a=b,true:false;}
template<class T> inline bool chmax(T& a, T b) {return a<b?a=b,true:false;}
#define D1(a) cout<<#a<<":"<<a<<"\n"
#define D2(a,b) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<endl 
#define D3(a,b,c) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c <<endl
#define D4(a,b,c,d) cout<<#a<<":"<<a<<","<<#b<<":"<<b<<","<<#c<<":"<<c<<","<<#d<<":"<<d<<endl 

int main()
{
    int N, M, K; cin >> N >> M >> K;

    rep(i, N+1) rep(j, M+1) {
        // i行とj列だけボタンを押した場合の黒いマス
        int black = i * M + j * ((N-i) - i);
        if (black == K) {
            cout <<"Yes"<<endl;
            return 0;
        }
    }
    cout << "No" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll          long long
#define vl          vector<ll>
#define vi          vector<int>
#define pi          pair<int,int>
#define pl          pair<ll,ll>
#define all(a)      a.begin(),a.end()
#define mem(a,x)    memset(a,x,sizeof(a))
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define trace(x)     cerr<<#x<<":"<<x<<endl;
#define trace2(x,y) cerr<<#x<<":"<<x<<"|"<<#y<<":"<<y<<endl;
#define trace3(x,y,z) cerr<<#x<<":"<<x<<"|"<<#y<<":"<<y<<"|"<<#z<<":"<<z<<endl;
#define fast_io     std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)

int main(){
	fast_io;
	int n;
	cin>>n;
	// vector<string> a;
	int cnt=0;
	map<string, int> m;
	for(int i=0;i<n;i++){
		string t;
		cin>>t;
		if(!m[t])cnt++;
		m[t]++;
	}
	cout<<cnt;
	
	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<int> vec(3);
  for (int i = 0; i < 3; i++) {
  	cin >> vec[i];
  }
  sort(vec.begin(), vec.end());
  cout << vec[0] + vec[1] << '\n';
  return 0;
}

Measured memory_bytes: 256
/**
 * Created by Wangzhe on 2020/1/12 19:56.
 * Description: 
 **/


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define PI acos(-1.0)
#define eps 1e-12
#define fi first
#define se second
#define MEM(a, b) memset((a),(b),sizeof(a))
#define mod(x) ((x)%MOD)
#define pii pair<int,int>
#define wz cout<<"-----"<<endl;

const int INF_INT = 2147483647;
const ll INF_LL = 9223372036854775807LL;
const ull INF_ULL = 18446744073709551615Ull;
const ll P = 92540646808111039LL;

const ll maxn = 1e5 + 10, MOD = 1e9 + 7;

int n,m;
int Move[4][2] = {1,0,-1,0,0,1,0,-1};
int step[22][22];
int f[22][22];
char mp[22][22];

int res;

void init() {

}

void bfs(int x, int y) {
    if (mp[x][y] == '#') return ;
    memset(step, 0, sizeof step);
    memset(f, 0, sizeof f);
    queue<pair<int,int>> q;
    f[x][y] = 1;
    q.push(make_pair(x, y));
    while (q.size()) {
        pii u = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int xx = u.first + Move[i][0];
            int yy = u.second + Move[i][1];
            if (xx < 0 || xx >= n || yy < 0 || yy >= m || f[xx][yy] || mp[xx][yy] == '#' || step[xx][yy] != 0) continue;
            step[xx][yy] = step[u.first][u.second] + 1;
            f[xx][yy] = 1;
            q.push(make_pair(xx, yy));
        }
    }

}
void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> mp[i];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            bfs(i,j);
            for (int ii = 0; ii < n; ii++) {
                for (int jj = 0; jj < m; jj++) {
                    res = max(res, step[ii][jj]);
                }
            }
        }
    }
    cout << res << endl;
}

int main() {
    init();
    solve();
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    cin>>n;
    set<int> st;
    for(int i=0;i<n;i++){
        int d;
        cin>>d;
        st.insert(d);
    }
    cout<<st.size()<<endl;
    return 0;
}


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

using namespace std;

int main(void) {

	long double a, b, c;
	cin >> a >> b >> c;
	
	if (a + b + 2 * sqrtl(a*b) < c) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	

	return 0;
}

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

#define fir first
#define sec second
using namespace std;
typedef long long LL;
typedef unsigned long long u64;

template <typename T> inline bool cmin(T & a, const T & b) { return a > b ? a = b, 1 : 0;}
template <typename T> inline bool cmax(T & a, const T & b) { return a < b ? a = b, 1 : 0;}
int inp() {
	int x = 0, f = 1;char ch;
	for(ch = getchar(); !isdigit(ch); ch = getchar())
		if(ch == '-') f = -1;
	for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
	return x * f;
}

const int MaxN = 1001234, P = 998244353;
int n, m, fac[MaxN], rfac[MaxN];

int exp(int a, int n) {
	int res = 1;
	for(; n; n >>= 1) {
		if(n & 1)
			res = (LL) res * a % P;
		a = (LL) a * a % P; 
	}
	return res;
}

int C(int n, int m) {
	return n < 0 || m < 0 || n < m ? 0 : (LL) fac[n] * rfac[m] % P * rfac[n - m] % P;
}

int main() {
	int i;
	
	fac[0] = 1;
	for(i = 1; i < MaxN; i++) fac[i] = (LL) fac[i - 1] * i % P;
	rfac[MaxN - 1] = exp(fac[MaxN - 1], P - 2);
	for(i = MaxN - 1; i; i--) rfac[i - 1] = (LL) rfac[i] * i % P;
	
	n = inp(); m = inp(); 
	if(n < m) swap(n, m);
	int ans = 0;
	
	for(i = 1; i <= m; i++) {
		int wayA = C(i + i, i);
		int wayB = C(n - i + m - i, n - i);
		ans += (LL) wayA * wayB % P;
		ans %= P; 
	}
	
	ans = (LL) ans * exp(C(n + m, n), P - 2) % P;
	ans = (LL) ans * (P + 1) / 2 % P;
	
	cout << (ans + n) % P;
	return 0;
}




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

double p[3000];
double dp[3000][3000];

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

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

    dp[0][0]=(1-p[0]);
    dp[0][1]=p[0];
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<3000;j++)
        {
            dp[i][j]+=(1-p[i])*dp[i-1][j];
            if(j>0)
                dp[i][j]+=p[i]*dp[i-1][j-1];
        }
    }
    double ans=0;

    for(int i=n/2+1;i<3000;i++)
    {
        ans+=dp[n-1][i];
    }
    cout<<setprecision(9);
    cout<<fixed;

    cout << ans;
    return 0;
}


Measured memory_bytes: 74076
#include<iostream>

int main(){
 	int n, ans=0;
  	std::cin >> n;
 	while(n!=2 && n!= 4 &&  n!=1){
     	ans++;
		if(n%2==1){
          	n*=3;n++;
        }
  		else{
          	n/=2;
        }
    }
  	std::cout << ans + 4;
}


Measured memory_bytes: 384
# coding: utf-8

import sys
import math
import collections
import itertools
from inspect import currentframe
INF = 10 ** 10
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def gcd(x, y) : return y if x % y == 0 else gcd(y, x % y)
def lcm(x, y) : return (x * y) // gcd(x, y)
def I() : return int(input())
def MI() : return map(int, input().split())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def chkprint(*args) : names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}; print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

N, A, B = MI()

if A == 0:
    print(0)
else:
    ans = N // (A + B) * A
    tmp = N % (A + B)
    if tmp >= A:
        ans += A
    else:
        ans += tmp

    print(ans)


Measured memory_bytes: 43504
#include <bits/stdc++.h>
using namespace std;
const int N=605;

int n,ans;
char a[N][N];
long long h[N][N],v[N][N],p[N];

long long H(int i, int j, int k){
	return h[i+k][j+n-1]-h[i+k][j+k]*p[n-k-1];
}

long long V(int i, int j, int k){
	return v[i+n-1][j+k]-v[i+k][j+k]*p[n-k-1];
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	p[0]=1;
	for (int i=1;i<N;i++){
		p[i]=p[i-1]*42073;
	}
	cin >> n;
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			cin >> a[i][j];
			a[i][j+n]=a[i+n][j]=a[i+n][j+n]=a[i][j];
		}
	}
	for (int i=1;i<=n*2;i++){
		for (int j=1;j<=n*2;j++){
			h[i][j]=h[i][j-1]*42073+a[i][j];
		}
	}
	for (int i=1;i<=n*2;i++){
		for (int j=1;j<=n*2;j++){
			v[j][i]=v[j-1][i]*42073+a[j][i];
		}
	}
	for (int i=1;i<=n;i++){
		for (int j=1;j<=n;j++){
			int ok=1;
			for (int k=0;k<n;k++){
				ok&=H(i,j,k)==V(i,j,k);
			}
			ans+=ok;
		}
	}
	cout << ans << "\n";
}

Measured memory_bytes: 6400
from math import sqrt
n,d =(int(i) for i in input().split())
x = [[int(i) for i in input().split()] for i in range(n)]

tmp = []
sum = 0
for i in range(n):
    for j in range(n):
        if j != i:
            for a in range(d):
                sum += abs(x[i][a]-x[j][a])**2
            tmp.append(sum)
            sum = 0
ans = 0
for i in tmp:
    if sqrt(i).is_integer(): ans += 1
print(ans//2)


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

const int N=1e5+10;

struct Edge{ int to,next; }way[N];

int n,num[N],tot;

void Build(int a,int b) { way[++tot]=(Edge){b,num[a]}; num[a]=tot; }

void Init() {
	scanf("%d",&n);
	int a;
	for (int i=2;i<=n;++i) {
		scanf("%d",&a);
		Build(a,i);
	}
}

int val[N],value[N];
int Dfs(int x) {
	for (int i=num[x];i;i=way[i].next) {
		int v=way[i].to;
		Dfs(v);
	}
	int top=0;
	for (int i=num[x];i;i=way[i].next) {
		int v=way[i].to;
		val[++top]=value[v];
	}
	std::sort(val+1,val+top+1);
	int ans=0;
	for (int i=1;i<=top;) {
		int p=i; while (val[p]==val[i] && i<=top) ++i;
		ans=std::max(ans,val[p])+i-p;
	}
	value[x]=ans;
	return ans;
}

void Solve() {
	printf("%d\n",Dfs(1));
}

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

Measured memory_bytes: 6528
MOD=10**9+7
UPPERLIMIT=2*(10**5)+1
MODMUL=[1, 1]+[0]*(UPPERLIMIT-1)
for i in range(2, UPPERLIMIT+1):
  MODMUL[i]=MODMUL[i-1]*i%MOD
MODDIV=[1]*UPPERLIMIT+[pow(MODMUL[-1], MOD-2, MOD)]
for i in range(UPPERLIMIT, 0, -1):
  MODDIV[i-1]=MODDIV[i]*i%MOD

H, W, A, B=map(int, input().split())

ans=(((MODMUL[H+W-2]*MODDIV[H-1])%MOD)*MODDIV[W-1])%MOD

x=[MODMUL[H-A+B+i-1]*MODDIV[B-1]*MODDIV[H-A+i]%MOD for i in range(A)]
y=[MODMUL[W+A-B-i-1]*MODDIV[W-B]*MODDIV[A-i-1]%MOD for i in range(A)]
for i in range(A-1, 0, -1):
  x[i]-=x[i-1]

for i in range(A):
  ans-=(x[i]*y[i]%MOD)
  ans%=MOD
  
print(ans)

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

typedef long long ll; // -9,223,372,036,854,775,808 から 9,223,372,036,854,775,807

template <class T> using vec = std::vector<T>;
template <class T> using vec2 = vec<vec<T>>;
template <class T> using vec3 = vec<vec<vec<T>>>;

#define DEBUG

void solve(long long N, long long K){
    ll res = K;
    for(int i=0; i < N-1; i++){
        res *= K-1;
    }
    cout << res << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long K;
    scanf("%lld",&K);
    solve(N, K);
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int sum_days = 0;
        for (int i = 0; i<m;i++){
            sum_days += sc.nextInt();
        }
            int res = n - sum_days;        
        if(res<0){
            res = -1;
        }
        System.out.println(res);
    }
}

Measured memory_bytes: 46068
#include <cstdio>
#include <stack>
#include <set>
#include <cmath>
#include <map>
#include <time.h>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <memory.h>
#include <cstdlib>
#include <queue>
#include <iomanip>
#define P pair<int, int>
#define LL long long
#define LD long double
#define PLL pair<LL, LL>
#define mset(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI acos(-1.0)
#define random(x) rand() % x
using namespace std;
const int inf = 0x3f3f3f3f;
const LL __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 5;
const int Mod = 1e9 + 7;

LL dp[105][5][5][5];
bool C(int x, int y, int k ){
    if (x == 1 && y == 3 && k == 2)
        return false;
    if (x == 3 && y == 1 && k == 2)
        return false;
    if (x == 1 && y == 2 && k == 3)
        return false;
    return true;
}
int main()
{
    int n;
    cin >> n;
    for(int x = 1; x <= 4; x++){
        for(int y = 1; y <= 4; y++)
            for(int k = 1; k <= 4; k++)
            if(C(x, y, k))
                dp[3][x][y][k] = 1;
    }
    for(int i = 4; i <= n; i++){
        for(int x = 1; x <= 4; x++){
            for(int y = 1; y <= 4; y++){
                for(int  z = 1; z <= 4; z++){
                    for(int k = 1; k <= 4; k++){
                        if(!C(y, z, k))    continue;
                        if(x == 1 && z == 3 && k == 2)  continue;
                        if(x == 1 && y == 3 && k == 2)  continue;
                        dp[i][y][z][k] += dp[i-1][x][y][z];
                        dp[i][y][z][k] %= Mod;
                    }
                }
            }
        }
    }
    LL ret = 0;
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= 4; j++){
            for(int t = 1; t <= 4; t++){
            if(!C(i, j, t)) continue;
            ret = (ret+dp[n][i][j][t]) % Mod;
            }
        }
    }
    cout << ret << endl;
    return 0;
}

Measured memory_bytes: 384
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <functional>
#include <queue>
#include <iostream>
#include <string.h>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <cstdint>
#include <climits>
#include <unordered_set>
#include <sstream>
#include <stack>

using namespace std;

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

int main()
{
    int n;
    cin >> n;
    vector<ll> vs(3 * n);
    for(int i = 0;i < 3 * n;i++)
    {
        cin >> vs[i];
    }
    vector<ll> fv(n+1);
    {
        priority_queue<ll, std::vector<ll>, std::greater<ll>> f;
        ll fsum = 0;
        for(int i = 0;i < n;i++)
        {
            f.push(vs[i]);
            fsum += vs[i];
        }
        fv[0] = fsum;
        for(int i = n;i < 2 * n;i++)
        {
            f.push(vs[i]);
            ll ft = f.top(); f.pop();
            fsum += vs[i] - ft;
            fv[i - n + 1] = fsum;
        }
    }
    vector<ll> sv(n+1);
    {
        priority_queue<ll, std::vector<ll>, std::less<ll>> f;
        ll fsum = 0;
        for(int i = 2 * n;i < 3 * n;i++)
        {
            f.push(vs[i]);
            fsum += vs[i];
        }
        sv[n] = fsum;
        for(int i = 2 * n - 1;i >= n;i--)
        {
            f.push(vs[i]);
            ll ft = f.top(); f.pop();
            fsum += vs[i] - ft;
            sv[i - n] = fsum;
        }
    }
    ll diff = -1e17;
    for(int i = 0;i < n+1;i++)
    {
        auto d = fv[i] - sv[i];
        diff = max(diff, d);
    }
    cout << diff << endl;
    return 0;
}

Measured memory_bytes: 5992
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <deque>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

bool flag;
double x1, yt1, x2, y2, x3, y3, xp, yp;
double v[3][2];

double inpr(double v1[2], double v2[2]) {
	double ret = v1[0] * v2[0] + v1[1] * v2[1];
	return ret;
}

int main() {
	x2 = -123456;
	cin >> x1 >> yt1 >> x2 >> y2 >> x3 >> y3 >> xp >> yp;

	while (x2 != -123456) {
		flag = true;
		v[0][0] = -(y2 - yt1);
		v[0][1] = x2 - x1;		
		
		v[1][0] = x3 - x1;
		v[1][1] = y3 - yt1;

		v[2][0] = xp - x1;
		v[2][1] = yp - yt1;

		if (inpr(v[0], v[1]) * inpr(v[0], v[2]) < 0) flag = false;
		
		v[0][0] = -(y3 - y2);
		v[0][1] = x3 - x2;		
		
		v[1][0] = x1 - x2;
		v[1][1] = yt1 - y2;

		v[2][0] = xp - x2;
		v[2][1] = yp - y2;

		if (inpr(v[0], v[1]) * inpr(v[0], v[2]) < 0) flag = false;
		
		v[0][0] = -(yt1 - y3);
		v[0][1] = x1 - x3;		
		
		v[1][0] = x2 - x3;
		v[1][1] = y2 - y3;

		v[2][0] = xp - x3;
		v[2][1] = yp - y3;

		if (inpr(v[0], v[1]) * inpr(v[0], v[2]) < 0) flag = false;

		if (flag) cout << "YES" << endl;
		else cout << "NO" << endl;

		x2 = -123456;
		cin >> x1 >> yt1 >> x2 >> y2 >> x3 >> y3 >> xp >> yp;
	}

	return 0;
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@*/
/*@                       @*/
/*@ debug output erased ? @*/
/*@                       @*/
/*@@@@@@@@@@@@@@@@@@@@@@@@@*/

Measured memory_bytes: 1216
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
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;
}
/////////////////////////////////////////////

class UnionFind {
public:
  vector <ll> par;
  vector <ll> siz;
  UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i) par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool issame(ll x, ll y) {
    return root(x) == root(y);
  }
  ll size(ll x) {
    return siz[root(x)];
  }
};

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  int N,M; cin >> N >> M;
  vector<int> p(N), x(M), y(M);
  rep(i,N) {
    cin >> p[i]; p[i]--;
  }
  rep(i,M) {
    cin >> x[i] >> y[i];
    x[i]--; y[i]--;
  }
  
  UnionFind uf(N);
  rep(i,M) uf.merge(x[i],y[i]);
  
  int ans = 0;
  rep(i,N) {
    if (uf.issame(i,p[i])) ans++;
  }

  cout << ans << endl;
}

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

namespace B___Sumo
{
    class Program
    {
        static void Main(string[] args)
        {
            var S = Console.ReadLine();

            Console.WriteLine(S.Count(x => x == 'o') + 15 - S.Length >= 8 ? "YES" : "NO");
        }
    }
}


Measured memory_bytes: 13268
a, b, k = map(int, input().split())

for p in range(k):
    if p % 2 == 0:
        if a % 2 != 0:
            a = a - 1
        b = int(b + a/2)
        a = int(a/2)
    else:
        if b % 2 != 0:
            b = b - 1
        a = int(a + b/2)
        b = int(b/2)

print(a, b)

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

#define PI 3.1415926535897932
#define MOD 1000000007
#define INF 1e9

#define REP(i, n) for(int i = 0; i < (n); i++)
#define REP1(i, n) for(int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO  cout << "NO"  << endl
#define No  cout << "No"  << endl

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;


void solve()
{
    int a, b; cin >> a >> b;
    string s1, s2;
    REP(i, b) s1.push_back('0'+a);
    REP(i, a) s2.push_back('0'+b);
    cout << min(s1, s2) << endl;
}
    
int main() 
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}

Measured memory_bytes: 256
#0025
while True:
    c = 0
    d = 0
    l = []
    m = []
    try:
        a1,a2,a3,a4 = map(int,input().split())
        b1,b2,b3,b4 = map(int,input().split())
    except:
        break
    l.extend([a1,a2,a3,a4])
    m.extend([b1,b2,b3,b4])

    for i in range(4):
        for k in range(4):
            if l[i] == m[k]:
                if i == k:
                    c += 1
                else:
                    d += 1
    print(c,d)


Measured memory_bytes: 5596
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000000000000009
//#define INF 9223372036854775807
typedef long long ll;
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define rollcall cout<<"I'm Sucu."<<endl;
#define YES(s) s?cout << "YES" << endl:cout << "NO" << endl
#define Yes(s) s?cout << "Yes" << endl:cout << "No" << endl
#define Taka(s) s?cout << "Takahashi" << endl:cout << "Aoki" << endl
#define out(s,t,u) s?cout << t << endl:cout << u << endl
#define int ll
#define Endl endl
//#define P pair<int,int>

signed main(){
    int N;
    cin >> N;
    if(N==0){
        cout << 0 << endl;
        return 0;
    }
    vector<int>f;
    while(N!=0){
        //cout << N << endl;
        if(N%2!=0){
            f.push_back(1);
            N-=1;
        }else{
            f.push_back(0);
        }
        N/=-2;
    }
    while(1){
        if(f[f.size()-1]==0){
            f.pop_back();
        }else{
            break;
        }
    }
    REP(i,f.size()){
        cout << f[f.size()-i-1];
    }
    cout << endl;
	return 0;
}

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

int N,P;

int dp[101][101];
int x[100],y[100];

int main(){
  memset(dp,-1,sizeof(dp));
  cin>>N>>P;
  for(int i=0;i<N;i++){
    cin>>x[i]>>y[i];
  }
  
  dp[0][0]=P;

  for(int i=0;i<N;i++){
    for(int j=0;j<=i;j++){
      
      int p=dp[i][j];
      if(p<0)continue;
      
      dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
      
      int last=0;
      
      for(int k=i;k<N;k++){
        int xx=max(0,x[k]-last);
        int cost=max(0,y[k]-xx);
        p-=cost;
        if(p<0)break;
        
        dp[ min(N,k+2) ][ j+k-i+1 ]=max(dp[ min(N,k+2) ][ j+k-i+1 ],p );
        last=cost;
      }
      
    }
  }

  int ans=0;
  for(int j=0;j<=N;j++)
    if(dp[N][j]!=-1)ans=j;
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 3136
/*
    written by Pankaj Kumar.
    country:-INDIA
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef set<pair<int,int>> spi;
typedef set<pair<ll,ll>> spl;
typedef vector<pair<int,int>> vpi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<pair<ll,ll>> vpl;
typedef vector<string> vs;
typedef map<int,int>mi;
typedef map<ll,ll> ml;
typedef set<string> ss;
typedef set<char>sc;
typedef set<int> si;
typedef set<ll> sl;
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
// define values.
#define mod 1e9+7LL
#define phi 1.618
/* Bit-Stuff */
#define get_set_bits(a) (__builtin_popcount(a))
#define get_set_bitsll(a) ( __builtin_popcountll(a))
#define get_trail_zero(a) (__builtin_ctz(a))
#define get_lead_zero(a) (__builtin_clz(a))
#define get_parity(a) (__builtin_parity(a))
/*  Abbrevations  */
#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"
// loops
#define loop(i,start,end) for(ll i=ll(start);i<ll(end);i++)
#define loop0(num) for(ll i=0;i<ll(num);i++)
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
// Some print
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define cc ll test;cin>>test;while(test--)
// sort
#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())
// function

ll power(ll x,   ll y, ll p)  
{  
    ll res = 1;      
    x = x % p;             
    while (y > 0)  
    {    
        if (y & 1)  
            res = (res*x) % p;   
        y = y>>1; 
        x = (x*x) % p;  
    }  
    return res;  
} 
/* ascii value 
A=65,Z=90,a=97,z=122 1=49
*/
/*  -----------------------------------------------------------------------------------*/
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);



int main()
{
    ll n;
    cin>>n;
    ll count=0;
    vl v(n,0);
    forin(v,n);
    for(ll i=1;i<n;i++)
    {
        if(v[i]<v[i-1])
        {
            count+=abs(v[i-1]-v[i]);
            v[i]=v[i-1];
        }
    }
    cout<<count<<endl;
}

Measured memory_bytes: 4848
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
    int w,s;
    long long v;
};
bool cmp(node x,node y)
{
    return (x.s+x.w)>(y.s+y.w);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int n,m,i,j,k;
    cin>>n;
    vector<node>arr(n);
    for(i=0;i<n;i++)
    {
        cin>>arr[i].w>>arr[i].s>>arr[i].v;
    }
    sort(arr.begin(),arr.end(),cmp);
    int sz=arr[0].s+arr[0].w;
    vector<long long>tmp(sz+1);
    vector<vector<long long> >dp(n+1,tmp);
    for(j=0;j<=sz;j++) dp[n][j]=0ll;
    for(i=n-1;i>=0;i--)
    {
        for(j=0;j<=sz;j++)
        {
            dp[i][j]=0ll;
            if(arr[i].w<=j) dp[i][j]=arr[i].v+dp[i+1][min(j-arr[i].w,arr[i].s)];
            dp[i][j]=max(dp[i][j],dp[i+1][j]);
        }
    }
    long long ans=arr[n-1].v;
    for(i=0;i<n-1;i++)
    {
        ans=max(ans,arr[i].v+dp[i+1][arr[i].s]);
    }
    cout<<ans;
}


Measured memory_bytes: 160640
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <sstream>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define MATH_PI 3.14159265358979323846
int main(){
    ios::sync_with_stdio(false);
    string sInput;
    getline(cin, sInput);
    register int i;
    for (i = 0; i < sInput.length(); ++i){
        if (sInput[i] != ',')
            cout << sInput[i];
        else
            cout << ' ';
    }
    cout << '\n';
endapp:
    //system("pause > nul");
    return 0;
}

Measured memory_bytes: 256
/*#### bits/stdc++.h */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iostream>
#include<string>
#include<sstream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<deque>
#include<queue>
#include<utility>
#include<algorithm>
using namespace std;
/*####*/

/*#### modular_arithmetic */
class modular_arithmetic{
public:
	int mod, range;

	modular_arithmetic(){
		mod = 1000000007;
	}
	//No specific settings.
	void set_mod(int _mod){mod = _mod;}
	int add(int x,int y){return (x+y)%mod;}
	int sub(int x,int y){return (x-y+mod)%mod;}
	int mul(int x,int y){return (int)((long long int)x*y%mod);}
};
/*####*/
modular_arithmetic ma;

#define R 4005
int c[R][R],csum[R][R];
int c_sum(int x,int from, int to){
	if(from < 0)from = 0;
	if(to > x)to = x;
	if(from > to)return 0;
	return ma.sub(csum[x][to], from==0?0:csum[x][from-1]);
}
int calc(int red,int blue,int pos){
	int all = c[red+blue][red];
	int eat = red + blue - pos;
	int good = c_sum(eat, max(0, eat-blue), min(min(eat, blue),red));
	return ma.sub(all, good);
}
int mem[R][R];
int calc2(int red,int blue){
	int&res = mem[red][blue];
	if(res !=-1)return res;
	res = 0;
	if(red >= 1)
		res = ma.add(calc2(red-1,blue), calc(red, blue-1, red));
	return res;
}
int main(){
	memset(mem,-1,sizeof(mem));
	for(int i=0; i<R; i++)for(int j=0; j<=i; j++){
		c[i][j] = j==0||j==i?1:ma.add(c[i-1][j], c[i-1][j-1]);
		csum[i][j] = ma.add(c[i][j], j==0?0:csum[i][j-1]);
	}
	int A,B;
	scanf("%d%d",&A,&B);
	int res = 0;
	for(int i=0; i<A; i++){
		int red = A - i, blue = B - 1, pos = red;
		int eat = red + blue - pos;
		for(int j=0; j<=eat; j++){
			int red2 = j, blue2 = eat-j;
			if(red2 > red || blue2 < 0 || blue2 > blue)continue;
			int lef_red = red - red2, lef_blue = blue - blue2;
			if(lef_red <= 0 || lef_blue <= 0)continue;
			res = ma.add(res, ma.mul(c[eat][red2], calc2(lef_red, lef_blue)));
		}
	}
	printf("%d\n",ma.sub(c[A+B][A], res));
	return 0;
}

Measured memory_bytes: 188288
#include<stdio.h>

int main(void){
    int i;
    int x;
    int n;
    
    scanf("%d", &n);
    
    for(i = 1; i <= n; i++){
        x = i;
        if(x % 3 == 0){
            printf(" %d", i);
            continue;
        }
    do{
        if(x % 10 == 3){
            printf(" %d", i);
            break;
        }
        x /= 10;
    }while(x);
    }
    printf("\n");
    
    return 0;
}


Measured memory_bytes: 2140
from collections import Counter
S = input()
S = list(map(int,S))
N = len(S)

MOD = 2019
rs = [1]
for i in range(N):
    rs.append((10*rs[-1]) % MOD)

res = [0]
for i in range(N):
    s = S[-i-1]
    res.append((res[-1] + s * rs[i]) % MOD)

cand = Counter(res)
ans = 0
for i in cand.values():
    ans += i*(i-1)//2
print(ans)



Measured memory_bytes: 25480
#include <bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c(n-1);
  for(int i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  int sum = 0;
  for(int i = 0; i < n; i++)
  {
      int b;
      cin >> b;
      sum += b;
  }
  for(int i = 0; i < n-1; i++)
  {
    cin >> c[i];
  }
  for(int i = 0; i < n-1; i++)
  {
      if(a[i+1] - a[i] == 1)
      {
          sum += c[a[i]-1];
      }
  }
  cout << sum << endl;
}



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

int a,b;
int p1,p2;

int32_t main()
{
	 cin >> a >> b;
	
	 int resp = -1;
	 for(int i = 1; i <= 2000; i++)
	 {
	 	int x = i*0.08;
	 	int y = i*0.1;
	 	if(x == a and y == b)
	 	{
	 		resp = i;
	 		break;
	 	}
	 }
	 
	cout << resp << '\n';
}

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

int main() {
  int N=0, ans=0;
  cin >> N;
  vector<int> v_a(N);
  for(int i=0;i<v_a.size();i++) cin >> v_a.at(i);
  sort(v_a.begin(),v_a.end());
  reverse(v_a.begin(),v_a.end());
  for(int i=0;i<N;i++){
    if(i%2==0){
      ans += v_a.at(i);
    }
    else{
      ans -= v_a.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
using ll = long long;
using pint = pair<int, int>;

int main() {
	int N;
	cin >> N;
	vector<int> a(N);
	vector<bool> visit(N, false);
	for (int i = 0; i < N; ++i) {
		cin >> a[i];
	}

	int cur = a[0];
	int cnt = 1;
	visit[0] = true;
	for (; cnt <= N; ++cnt) {
		if (visit[cur-1]) {
			cnt = -1;
			break;
		}
		if (cur == 2) {
			break;
		}
		visit[cur - 1] = true;
		cur = a[cur - 1];
	}
	cout << cnt << endl;

	return 0;
}


Measured memory_bytes: 3784
#include <bits/stdc++.h>    
//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#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")
//using namespace __gnu_pbds; 
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long 
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);} 
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}

signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf    
    #ifndef ONLINE_JUDGE    
           freopen("input.txt","r",stdin);
           freopen("output.txt","w",stdout);
    #endif//*/
    int n;
    cin>>n;
    std::vector<int> v;
    while(n)
    {
        if(n%26)
        {
            v.pb(n%26);
            n/=26;
        }
        else
        {
            v.pb(26);
            n/=26;
            n--;
        }
    }
    reverse(all(v));
    for(auto x : v)
    {
        cout<<(char)('a'+x-1);
    }
    printclock;
    return 0;
}

Measured memory_bytes: 3840