akhauriyash/Code-Regression huggingface dataset:
S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])

Measured memory_bytes: 4616
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int p[3];
    for(int i=0;i<4;i++){
        cin>>p[i];
    }
    sort (p,p+3);
    int a=0;
   a=p[0]+p[1];
    cout<<a<<endl;
    return 0;
}


Measured memory_bytes: 384
#include<stdio.h>
int main() {

	int n, k, i,j,w,c=0,p[1000];

	scanf("%d%d", &n, &k);

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

	for (i = 0; i < n - 1; i++) {
		for (j = i + 1; j < n; j++) {
			if (p[i] > p[j]) {
				w = p[i];
				p[i] = p[j];
				p[j] = w;
			}
		}
	}

	for (i = 0; i < k; i++) {
		c += p[i];
	}

	printf("%d", c);


	return 0;

}

Measured memory_bytes: 2156
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define REP(i,a,b) for(int i=int(a);i<(int)b;++i)
#define Graph vector<vecctor<ll>>
#define pairs vector<pair<ll,ll>>
#define pb push_back



using namespace std;
using ll = long long;

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







int main(void)
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    string t;
    cin >> t;
    rep(i,t.size()){
        if(t[i] == '?'){
            t[i] = 'D';
        }
    }

    cout << t << endl;
    
    return 0;
}


Measured memory_bytes: 3732
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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 <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int X,Y,A,B,C; cin >> X >>Y>>A>>B>>C;
    vector<PII> ap;
    REP(i,A){
        int p; cin >> p;
        ap.eb(p,0);
    }
    REP(i,B){
        int p; cin >> p;
        ap.eb(p,1);
    }
    REP(i,C){
        int p; cin >> p;
        ap.eb(p,2);
    }
    sort(ap.begin(),ap.end());
    reverse(ap.begin(),ap.end());
    int nx = 0, ny = 0, nz = 0;
    LL ans = 0;

    for(auto p : ap){
        if(p.second == 0){
            if(nx < X){
                nx++;
                ans += p.first;
            }
        }else if(p.second == 1){
            if(ny < Y){
                ny++;
                ans += p.first;
            }
        }else{
            nz++;
            ans += p.first;
        }

        if(nx + ny + nz == X+Y) break;
    }
    cout << ans << endl;




}


Measured memory_bytes: 6384
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
typedef long long lng;
#define UP(i,a,b) for(int i=(a); i<=(b); ++i)
#define DOWN(i,b,a) for(int i=(b); i>=(a); --i)
#define REP(i,n) UP(i,0,n-1)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end()) //NOTE: <a> must be sorted in advance

lng power(lng b, lng n) {lng sol=1; while(n>0) {if(n&1) {sol=sol*b;} n>>=1; b*= b;} return sol;} //calculate b^n

const int MOD = 1000000007; //10^9+7


/*********** variables ************/

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

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

    UP(c, 'a', 'z') {
        int cnt = 0;
        REP(i, W.size()) {
            if(W[i]==c) cnt++;
        }
        if(cnt&1) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  	string n;
  	cin >> n;
  	int k, l;
  	cin >> k;
  	l = n.size();
  
  	int dp[105][5][2] = {};
  	dp[0][0][0] = 1;
  	int d;
  	for(int i=0; i<l; i++){
    	d = n.at(i) - '0';
    		for(int j = 0; j <= k; j++){
      			for(int smaller = 0; smaller < 2; smaller++){
        			for(int x = 0; x <= (smaller ? 9 : d); x++){
          				dp[i+1][x ? j+1 : j][smaller || x < d] += dp[i][j][smaller];
        			}
      			}
    		}
  	}
  	cout << dp[l][k][0] + dp[l][k][1] << endl;
}

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

int CheckMax(int addR, int subR, int mulR);

int main() {
	int A, B;
	int addR, subR, mulR;
	
	cin >> A;
	cin >> B;
	
	addR = A + B;
	subR = A - B;
	mulR = A * B;
	
	cout << CheckMax(addR, subR, mulR) << endl;
	return 0;
}

int CheckMax(int addR, int subR, int mulR){
	if (addR > subR && addR > mulR){
		return addR;
	}
	if (subR > mulR){
		return subR;
	}
	return mulR;
}

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

int main() {
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
  string S;
  cin>>S;
  int a=0;
  int b=0;
  rep(i,3){
    if(S.at(i)=='A')
      a=1;
    if(S[i]=='B')
      b=1;
  }
  if(a*b==1)
    cout<<"Yes";
  else
    cout<<"No";
}

Measured memory_bytes: 256
#include <cstdio> 
char s[5];
int main ()
{
    scanf("%s",s);
    int a=s[0]-'0',b=s[1]-'0',c=s[2]-'0',d=s[3]-'0'; 
    if(a-b-c-d==7)printf("%d-%d-%d-%d=7\n",a,b,c,d);
    else if(a-b-c+d==7)printf("%d-%d-%d+%d=7\n ",a,b,c,d); 
    else if(a-b+c-d==7)printf("%d-%d+%d-%d=7\n",a,b,c,d); 
    else if(a-b+c+d==7)printf("%d-%d+%d+%d=7\n",a,b,c,d); 
    else if(a+b-c-d==7)printf("%d+%d-%d-%d=7\n",a,b,c,d); 
    else if(a+b-c+d==7)printf("%d+%d-%d+%d=7\n",a,b,c,d); 
    else if(a+b+c-d==7)printf("%d+%d+%d-%d=7\n",a,b,c,d); 
    else if(a+b+c+d==7)printf("%d+%d+%d+%d=7\n",a,b,c,d);
}


Measured memory_bytes: 1732
N = int(input())
A = list(map(int, input().split()))

cnt = [0 for _ in range(8)]
over = 0

for a in A:
    if a < 3200:
        ind = a//400
        cnt[ind] = 1
    else:
        over += 1


ans = sum(cnt)

ans_min = max(1,ans)
ans_max = ans + over
# if over != 0:
#     ans_min = max(ans,1)
#     ans_max = min(8,ans+over
# else:
#     ans_min = ans
#     ans_max = ans
print(ans_min, ans_max)


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define int long long

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


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

int32_t main(){
	int n;
	cin >> n;
	vector<int> vec(n+1);
	for(int i = 1; i <= n; i++){
		cin >> vec[i];
	}
	vector<int> ans;
	vector<int> build(n+1);
	for(int i = n; i >= 1; i--){
		int sum = build[i];
		for(int j = i+i; j <= n; j+=i){
			sum += build[j]; 
		}
		if(sum % 2 != vec[i]){
			build[i] += 1;
			ans.push_back(i);
		}
	}
	bool exist = true;
	for(int i = 1; i <= n && exist == true; i++){
		int sum = 0;
		for(int j = i; j <= n; j+=i){
			sum += build[j];
		}
		exist &= (sum % 2 == vec[i]);
	}
	if(exist == false){
		cout << "-1" << endl;
	}else{
		cout << ans.size() << endl;
		for(int a : ans){
			cout << a << " ";
		}
	}
}

Measured memory_bytes: 4980
#include <cstdio>
#include <cstring>
#define int long long
#define rep(i,a,b) for(register int i=(a);i<=(b);++i)
#define per(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
inline void chkmax(int &x,int y){
	x<y?(x=y):0;
}
inline void chkmin(int &x,int y){
	x>y?(x=y):0;
}
const int N=5005;
int dp[N][N],a[N];
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar()){
		if(ch=='-'){
			f=-1;
		}
	}
	for(;ch>='0'&&ch<='9';ch=getchar()){
		x=(x<<1)+(x<<3)+(ch^48);
	}
	return x*f;
}
signed main() {
	int n=read(),A=read(),B=read(),ans=1ll<<60;
	rep(i,1,n){
		a[i]=read();
	}
	memset(dp,0x3f,sizeof(dp));
	dp[0][0]=0;
	for(int i=1;i<=n;i++){
		for(int j=0;j<=n;j++){
			if(a[i]>j){
				chkmin(dp[i][j],dp[i-1][j]+A);
				chkmin(dp[i][a[i]],dp[i-1][j]);
			}
			else{
				chkmin(dp[i][j],dp[i-1][j]+B);
			}
		}
	}
	rep(i,0,n){
		chkmin(ans,dp[n][i]);
	}
	printf("%lld\n",ans);
	return 0;
}


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

using namespace std;

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

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 113
#define MAXK 100013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, K;
int arr[MAXN];
ll dp[MAXN][MAXK]; //the # of ways where guy i ends at candy j

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	FOR(i, 0, N) cin >> arr[i];
	dp[0][0] = 1;
	FOR(i, 1, N + 1)
	{
		ll x = arr[i - 1];
		ll sum = 0;
		FOR(j, 0, K + 1)
		{
			sum += dp[i - 1][j];
			if (j > x) sum -= dp[i - 1][j - x - 1];
			sum %= INF; if (sum < 0) sum += INF;
			dp[i][j] = sum;
		}
	}
	cout << dp[N][K] << '\n';
	return 0;
}


Measured memory_bytes: 79360
#include <iostream>
#include <math.h>
#include <algorithm>
#include <functional> 
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 
#define rep(i,n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

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

    int top = atoi(S.substr(0,2).c_str());
    int btm = atoi(S.substr(2,2).c_str());
    string ans;

    if (1 <= top && top <= 12) {
        if (1 <= btm && btm <= 12) {
            ans = "AMBIGUOUS";
        } else {
            ans = "MMYY";
        }
    } else {
        if (1 <= btm && btm <= 12) {
            ans = "YYMM";
        } else {
            ans = "NA";
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
import bisect

n = int(input())
l = [int(_) for _ in input().split()]

l.sort()

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a, b = l[i], l[j]
        k = bisect.bisect_right(l, a+b-1)
        ans += k - j - 1
print(ans)

Measured memory_bytes: 3188
class City:
    def __init__(self,id,y):
        self.id = id
        self.year = y


def main():
    n,m = map(int,input().split())
    pre_lst = [[] for _ in range(n)]
    city_tags = [0 for _ in range(m)]

    for i in range(m):
        p,y = map(int,input().split())
        pre_lst[p-1].append(City(i,y))

    for pre_num,pre in enumerate(pre_lst):
        if len(pre) <= 0:
            continue
        pre.sort(key = lambda x:x.year)
        counter = 0
        for city in pre:
            city_tags[city.id] = str(pre_num+1).zfill(6) + str(counter+1).zfill(6)
            counter += 1
    for tag in city_tags:
        print(tag)


if __name__ == '__main__':
    main()


Measured memory_bytes: 46216
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i))
#define mat(type, c, m, n) vector<vector<type>> c(m, vector<type>(n));for(auto& r:c)for(auto& i:r)cin>>i;
#define rep(i,a,n) for(int i=(a), i##_len=(n); i<i##_len; ++i)
#define rrep(i,a,n) for(int i=(a), i##_len=(n); i>i##_len; --i)
#define each(x,y) for(auto &(x):(y))
#define var(type, ...)type __VA_ARGS__;Scan(__VA_ARGS__);
template<typename T> void Scan(T& t) { cin >> t; }
template<typename First, typename...Rest>void Scan(First& first, Rest&...rest) { cin >> first; Scan(rest...); }
#define vec(type, c, n) vector<type> c(n);for(auto& i:c) cin>>i;
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
#define em emplace
#define pb pop_back
#define fi first
#define se second
#define get(a, i) get<i>(a)
#define bit_count __builtin_popcount
#define mt make_tuple
inline string zero(int a,string s,char c='0'){return string(a-len(s),c)+s;}
inline string zero(int a,int t,char c='0'){return zero(a, to_string(t), c);}
inline string zero(int a,ll t,char c='0'){return zero(a, to_string(t), c);}
#define mini min_element
#define maxi max_element
#define sum accumulate
#define chmax(a,b) if(a<b)a=b
#define chmin(a,b) if(a>b)a=b
template <class T> inline void print(T t){cout << t << '\n';}
template <class H, class... T> inline void print(H h, T... t){cout << h << " ";print(t...);}
const constexpr ll mod = 1000000007;
#define MAX 1001
int h, w;
string MAP[MAX], s;
ll v[MAX][MAX];
void solve() {
	cin >> h >> w;
	MAP[0] = string(MAX, '#');
	rep(i, 1, h + 1) {
		cin >> s;
		MAP[i] = '#' + s;
	}
	v[0][1] = 1;
	rep(i, 1, h + 1) {
		rep(j, 1, w + 1) {
			if (MAP[i][j] == '.') {
				v[i][j] += (v[i - 1][j] + v[i][j - 1]) % mod;
			}
		}
	}
	print(v[h][w] % mod);
}
int main() {
	solve();
	return 0;
}


Measured memory_bytes: 9088
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <numeric>
#include <sstream>
#include <string>
using namespace std;
#define out(X) cerr << #X << ": " << (X) << endl
#define SZ(X) ((int)(X.size()))
#define REP(I,N) for (int I = 0; I < (N); ++I)
#define FOR(I,L,H) for (int I = (L); I < (H); ++I)
#define MP(X,Y) make_pair((X),(Y))
#define PB push_back
#define ALL(X) X.begin(), X.end()
template <typename T> inline bool checkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline bool checkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
typedef long long lint;

const int MAXN = 1000000 + 10;

int n, m, num1[MAXN], num2[MAXN], st1[MAXN][20], st2[MAXN][20];

int min(int num[], int p1, int p2) {
    return num[p1] < num[p2] ? p1 : p2;
}

void init(int num[], int st[][20], int n) {
    REP(i, n)
        st[i][0] = i;
    for (int j = 1; (1 << j) <= n; ++j)
        for (int i = 0; i + (1 << j) - 1 < n; ++i)
            st[i][j] = min(num, st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}

int rmq(int num[], int st[][20], int i, int j) {
    int k = (int)log2(j - i + 1);
    return num[min(num, st[i][k], st[j - (1 << k) + 1][k])];
}

int main() {
    int q;
    while (scanf("%d%d%d", &n, &m, &q) == 3 && n + m + q) {
        REP(i, n) {
            REP(j, m) {
                int pos1 = i * m + j,
                    pos2 = j * n + i;
                //out(pos1);
                scanf("%d", num1 + pos1);
                num2[pos2] = num1[pos1];
            }
        }
        if (q < 10) {
            REP(qq, q) {
                //out(i);
                int l1, r1, l2, r2, ans = INT_MAX;
                scanf("%d%d%d%d", &l1, &l2, &r1, &r2);
                for (int i = l1; i <= r1; ++i) {
                    for (int j = l2; j <= r2; ++j) {
                        int pos = i * m + j;
                        //out(pos);
                        checkmin(ans, num1[pos]);
                    }
                }
                printf("%d\n", ans);
            }
        } else {
            init(num1, st1, n * m);
            init(num2, st2, n * m);
            REP(qq, q) {
                int l1, r1, l2, r2, ans = INT_MAX;
                scanf("%d%d%d%d", &l1, &l2, &r1, &r2);
                if (r2 - l2 > r1 - l1) {
                    for (int i = l1; i <= r1; ++i) {
                        int pos1 = i * m + l2, pos2 = i * m + r2;
                        checkmin(ans, rmq(num1, st1, pos1, pos2));
                    }
                } else {
                    for (int i = l2; i <= r2; ++i) {
                        int pos1 = i * n + l1, pos2 = i * n + r1;
                        checkmin(ans, rmq(num2, st2, pos1, pos2));
                    }
                }
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

Measured memory_bytes: 0
//Binary Search Tree III
#include <iostream>

using namespace std;

class Node {
public:
    int key;
    Node *parent = nullptr, *left = nullptr, *right = nullptr;

    Node(int key): key(key) {};
};

class Tree {
public:
    Node *root = nullptr;

    void insert(int k) {
        Node *x = root, *y = nullptr;
        while(x != nullptr) {
            y = x;
            if(k <= x->key) x = x->left;
            else x = x->right;
        }

        Node *z = new Node(k);
        z->parent = y;
        if(y == nullptr) root = z;
        else if(k <= y->key) y->left = z;
        else y->right = z;
    };

    Node* find(int k) {
        Node *x = root;
        while(x != nullptr && x->key != k) {
            if(k < x->key) x = x->left;
            else x = x->right;
        }
        return x;
    };

    Node* getMinimum(Node *u) {
        while(u->left != nullptr) u = u->left;
        return u;
    }

    void deleteNode(int k) {
        Node *x = find(k), *y = nullptr, *z = nullptr;
        if(x->left == nullptr || x->right == nullptr) y = x;
        else {
            y = getMinimum(x->right);
            x->key = y->key;
        }

        if(y->left != nullptr) z = y->left;
        else z = y->right;

        if(z != nullptr) z->parent = y->parent;

        if(y->parent == nullptr) root = z;
        else if(y->parent->left == y) y->parent->left = z;
        else y->parent->right = z;

        delete y;
    };
};

void inOrder(Node *u) {
    if(u == nullptr) return;
    inOrder(u->left);
    cout << " " << u->key;
    inOrder(u->right);
}

void preOrder(Node *u) {
    if(u == nullptr) return;
    cout << " " << u->key;
    preOrder(u->left);
    preOrder(u->right);
}

int main() {
    Tree T;

    int n;
    cin >> n;
    for(int i=0; i<n; i++) {
        string opr;
        int key;
        cin >> opr;
        if(opr == "insert") {
            cin >> key;
            T.insert(key);
        }
        if(opr == "find") {
            cin >> key;
            if(T.find(key) != nullptr) cout << "yes" << endl;
            else cout << "no" << endl;
        }
        if(opr == "delete") {
            cin >> key;
            T.deleteNode(key);
        }
        if(opr == "print") {
            inOrder(T.root);
            cout << endl;
            preOrder(T.root);
            cout << endl;
        }
    }
}


Measured memory_bytes: 21900
n, m = map(int, raw_input().split())
s = raw_input()
t = raw_input()

def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)

L = n * m / gcd(n, m)

a = {}
for i in range(n):
    a[i * L / n] = s[i]

poss = True
for j in range(m):
    idx = j * L / m
    if idx in a:
        poss &= a[idx] == t[j]

if poss:
    print L
else:
    print -1


Measured memory_bytes: 43020
#include<iostream>
#include<vector>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> b(n-1);
  for(int i=0;i<n-1;i++){
    cin>>b[i];
  }
  int ans = b[0]+b[n-2];
  for(int i=0;i<n-2;i++){
    if(b[i]>b[i+1]){
      ans+=b[i+1];
    }else{
      ans+=b[i];
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
import bisect
N = int(input())
L_list = list(map(int,input().split()))
L_list.sort()
ans = 0
 
for i in range(N-2):
  for j in range(i+1,N-1):
    x = bisect.bisect_left(L_list,L_list[i]+L_list[j])
    ans += x-j-1
print(ans)

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

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

    bool flag = false;
    for (int i=0; i<b; ++i) {
        if ((a * (i + 1) % b == c)) flag = true;
    }

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n); for(int i = 0;i < (n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
long double pi = acos(-1);
const int INF = 1001001001;


// 最小公倍数を求める
int euqlid (int a, int b){
  int temp;
  temp = a % b;
  if(temp == 0)return b;
  return euqlid(b,temp);
}
ll conbination(ll a,ll b){
  ll u = 1;
  ll d = 1;
  while(b != 0){
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int strtoint(char s){
  return (int(s-'0'));
}
int fact(int n){
  if(n == 1)return 1;
  return n * fact(n-1);
  }


int main(){
  rep(i,9){
    char temp;
    cin >> temp;
    if(i == 0 or i == 4 or i == 8) cout << temp;
  }
  cout << endl;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int r,c;
    int sum=0;
    cin>>r>>c;
    int a[r+1][c+1];
    int b[r+1];
    int d[c+1];
    
    for (int i=1;i<=r;i++){
        b[i]=0;
    }
    
    for (int i=1;i<=c;i++){
        d[i]=0;
    }
    
    for (int i=1;i<=r;i++){
        for (int j=1;j<=c;j++){
            int sub;
            cin>>sub;
            a[i][j]=sub;
            sum+=a[i][j];
        }
    }
    
    for (int i=1;i<=r;i++){
        for (int j=1;j<=c;j++){
            b[i]+=a[i][j];
        }
    }
    
    for (int i=1;i<=c;i++){
        for (int j=1;j<=r;j++){
            d[i]+=a[j][i];
        }
    }
    
    for (int i=1;i<=r;i++){
        for (int j=1;j<=c;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<b[i]<<endl;
    }
    
    for (int i=1;i<=c;i++){
        cout<<d[i]<<" ";
    }cout<<sum<<endl;
    
    return 0;
}


Measured memory_bytes: 3092
import sys
sys.setrecursionlimit(10**9)

def main():
    n, k = map(int, input().split())
    p = list(map(int, input().split()))

    p.sort()
    ans = sum(p[:k])
    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)

Measured memory_bytes: 62304
#2
#入力
Q,H,S,D=map(int,input().split())
N=int(input())

#処理
#0.25を単位として個数に変換
N=N*4
#リットル単価を取得
q=Q*4
h=H*2
s=S
d=D/2
z=[q,h,s,d]
x=[0,0,0,0]
#print(z)
for i in range(4):
    m=max(z)
    j=z.index(m)
    z[j]=0
    x[i]=j
x.reverse()
#print(x)

y=0
for i in x:
    #print(N)
    if i==0:
        t=N
        N=0
        y+=Q*t
    elif i==1:
        t=N//2
        N-=t*2
        y+=H*t
    elif i==2:
        t=N//4
        N-=4*t
        y+=S*t
    else:
        t=N//8
        N-=8*t
        y+=D*t
print(y)




Measured memory_bytes: 3064
//Code by Anshuman Das Gupta

#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define MOD 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define loop(I,N) for(int i=I;i<N;i++)
#define sortvec(V) sort(V.begin(),V.end())
#define vec vector<int>
#define endl "\n"
using namespace std;
int fast_power(int base, int power) {
    int result = 1;
    while (power > 0) {

        if (power & 1 == 1) {
            result = (result * base) % MOD;
        }
        base = (base * base) % MOD;
        power = power >> 1;
    }
    return result % MOD;
}

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

int binsearch(int a[], int f, int r, int x) {
    while (f <= r) {
        int mid = (f + r) / 2;
        if (a[mid] == x)
            return mid;
        else if (a[mid] > x) {
            r = mid - 1;
        }
        else {
            f = mid + 1;
        }
    }
    return -1;
}
void sieve(int a[], int n) {
    a[0] = 0;
    a[1] = 0;
    for (int i = 2; i <= n; i++) {
        if (a[i] == 1) {
            for (int j = i + i; j <= n; j += i) {
                a[j] = 0;
            }
        }
    }
}
int maxl = 1e18;
int32_t main() {
#ifndef ONLINE_JUDGE
    //for getting input from input.txt
    freopen("input.txt", "r", stdin);
    //for writing output to output.txt
    freopen("out.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int x = n % 10;
        if (x == 2 || x == 4 || x == 5 || x == 7 || x == 9)
            cout << "hon\n";
        else if (x == 3)
            cout << "bon\n";
        else
            cout << "pon\n";
    }
    return 0;
}


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

#define rep(i,a,b)                for(ll i=a;i<b;i++)
#define re(i,b)                for(ll i=0;i<b;i++)
#define repr(i,n)                for(ll i=n-1;i>=0;i--)
#define ll long long
#define ld long double
#define llu long long unsigned
#define pll std::pair<ll, ll> 
#define ppll std::pair<ll, pll>
#define vll std::vector<ll> 
#define vpll std::vector<pll> 
#define vppll std::vector<ppll> 
#define mll std::map<ll, ll> 
#define mpll std::map<pll, ll>
#define mppll std::map<ppll, ll>
#define sll std::set<ll>
#define ff first
#define ss second
#define msll std::multiset<ll>
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define srt(x) sort(all(x))
#define rsrt(x) sort(allr(x))
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define s(yy) ll yy;cin>>yy
#define mod 1e9+7
#define maxlong 1e18+5


/*
###################################################### 
#                                                    #
#                     @author                        #
#                  Parth Lathiya                     #
#      https://www.cse.iitb.ac.in/~parthiitb/        #
#                                                    #
###################################################### 
*/


int main()
{

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	#ifdef PARTH_LATHIYA_HOME

		freopen("A_in.txt","r",stdin);
	
		ll ttt,bkkk;
		cin>>ttt;
		bkkk=ttt;
		while(ttt--) {
			cout<<"Testcase - "<<bkkk-ttt<<"\n";
	#endif

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

	// std::vector<int> v[100];
	// std::vector<int> temp(100,4);
	// rep(i,0,100)
	// 	v[i] = temp;

	s(n);

	ll a[n],pr[n];
	re(i,n)
		cin>>a[i];
	pr[0]=a[0];
	rep(i,1,n)
		pr[i]=pr[i-1]+a[i];
	ll ans=maxlong;
	// cout<<pr[0]<<pr[1];
	re(i,n-1)	
		ans=min(ans,abs(pr[i]-pr[n-1]+pr[i]));
	cout<<ans;

//--------------------------------------------------------------------------------------
	
	#ifdef PARTH_LATHIYA_HOME
		cout<<"\n"; }
	#endif

	return 0;
}

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

int main() {
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
  int64_t N,K;
  cin>>N>>K;
  int64_t a,b,c;
  a=N/K;
  b=a+1;
  cout<<min(N-K*a,K*b-N);
}

Measured memory_bytes: 256
N,M = map(int,input().split())
edge = [0]*M
for i in range(M):
    a,b,c=map(int,input().split())
    a -= 1
    b -= 1
    c *= -1
    edge[i] = (a,b,c)

inf = float('inf')
V = N
E = M
d = [inf]*V
d[0] = 0
for i in range(V):
    update = False
    for j in range(E):
        From,To,cost = edge[j]
        if d[From] != inf and d[To]>d[From]+cost:
            d[To]=d[From]+cost
            update = True
    if not update:
        break

ans = d[N-1]
for j in range(E):
    From,To,cost = edge[j]
    if d[From] != inf and d[To]>d[From]+cost:
        d[To]=d[From]+cost

if d[N-1]!=ans:
    print('inf')
else:
    print(-ans)

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

using namespace std;
int cnt[40];
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i=0;i<n;i++)
    {
        int k;
        cin >> k;
        for (int j=0;j<k;j++)
        {
            int x;
            cin >> x;
            cnt[x]++;
        }
    }
    int ans=0;
    for (int i=1;i<=m;i++)
    {
        if (cnt[i]==n) ans++;
    }
    cout << ans <<endl;
    return 0;
}

Measured memory_bytes: 256
N, X = list(map(int, input().split()))
a, p = [1], [1]
for i in range(N):
    a.append(a[i] * 2 + 3)
    p.append(p[i] * 2 + 1)


def check(n, x):
    if n == 0:
        if x <= 0:
            return 0
        else:
            return 1

    if x <= 1 + a[n - 1]:
        return check(n - 1, x - 1)

    return p[n - 1] + 1 + check(n - 1, x - 2 - a[n - 1])


def main():
    print(check(N, X))


if __name__ == '__main__':
    main()


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


int main() {
    int n,q,a,b,p,x;
    cin >> n >> q;
    vector<vector<int>> G(n);
    int c[200010]={0};
    for (int i=0;i<n-1;i++) {
        cin >> a >> b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int j=0;j<q;j++) {
        cin >> p >> x;
        c[p-1]+=x;
    }
    vector<bool> flag(n); 
    queue<int> qu;
    int ans[200010]={0};
    qu.push(0);
    ans[0]=c[0];
    while (qu.size()>0) {
        int now = qu.front();
        qu.pop();
        for (int i=0;i<G[now].size();i++) {
            int next = G[now][i];
            if (flag[next]!=true) {
                ans[next]=c[next]+ans[now];
                qu.push(G[now][i]);
            }
        }
        flag[now]=true;
    }
    for (int i=0;i<n;i++) {
        cout << ans[i];
        if (i!=n-1) cout << ' ';
        else cout << endl;
    }
}

Measured memory_bytes: 16372
#include<bits/stdc++.h>
using namespace         std;
#define                 int long long
#define                 pb push_back
#define                 mp make_pair
#define st(arr,a)       memset(arr,a,sizeof arr)
#define                 nl cout<<endl
#define                 real signed
#define                 endl '\n'              
#define bitcount(x)     __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i,a,b)     for(int i=a;i<=b;i++)
#define rfor(i,a,b)     for(int i=a;i>=b;i--)
#define all(x)          x.begin() , x.end()
#define pi pair<int,int>
#define X first
#define Y second
#define N 314159
#define  vi vector<int>
#define  v  vector

signed main() 
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 
    int a,b,k;
    cin>>a>>b>>k;
    //multiset<int>dc;
    int ans;
    rfor(i,100,1)
    {
        if(a%i==0 and b%i==0)
        {
            k--;
            if(k==0)
            {
                ans=i;
                break;
            }
        }
    }
    cout<<ans;

     return 0;

}

//       g++ -g c.cpp ; ./a.exe

Measured memory_bytes: 256
import sys

def main():
    n_s = list(map(int, input().split()))
    m_s = list(map(int, input().split()))

    timetable = {}
    for h, m in zip(n_s[1::2], n_s[2::2]):
        timetable['{0:2d}:{1:02d}'.format(h, m)] = '{0}:{1:02d}'.format(h, m)
    for k, g in zip(m_s[1::2], m_s[2::2]):
        timetable['{0:2d}:{1:02d}'.format(k, g)] = '{0}:{1:02d}'.format(k, g)

    keys = list(timetable.keys())
    keys.sort()
    print(" ".join([timetable[key] for key in keys]))

    return 0

if __name__ == '__main__':
    sys.exit(main())

Measured memory_bytes: 7756
#include <stdio.h>

int main() {
	int N;
	scanf("%d",&N);
	int V[N],C[N],dif[N];
	for (int i = 0; i < N; i++) {
		scanf("%d",&V[i]);
	}
	for (int i = 0; i < N; i++) {
		scanf("%d",&C[i]);
	}
	for (int i = 0; i < N; i++) {
		dif[i] = V[i] - C[i];
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {
		if (dif[i] > 0) ans += dif[i];
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 128
/* Great things never come from comfort zones,
"whatever the mind of a man can conceive and believe,it can achieve." */
 
#include <bits/stdc++.h>
#define ll long long
#define scf(n) scanf("%d",&n)
#define lscf(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define pb push_back
#define pf push_front
#define mp make_pair
#define F first
#define S second
#define all(x)     x.begin(),x.end()
#define tc   int tt; scf(tt); while(tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define gcd __gcd
#define bitcount(n) __builtin_popcount(n)
typedef double dd;
using namespace std;
const ll mod =1e9+7;
const int N = 1e6+7;

void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B) 
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

bool m[N]={0};


int main()
{

    bool OK=1;

    string s;
    cin>>s;



    rep(i,1,s.size())
    {
        if(s[i]==s[i-1])
        {pri(i),prin(i+1),OK=0;
        break;}

        else if(i>1&&s[i]==s[i-2])
        {pri(i-1),prin(i+1),OK=0;
        break;}




    }


    if(OK)
      pri(-1),prin(-1);

  }

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

int f(int n){
    int ans=0;
    while(n){
        ans+=n%10;
        n/=10;
    }
    return ans;
}

int main(){
    int n,m;
    cin>>n;
    if(n%f(n)==0){
        cout<<"Yes\n";
    }else{
        cout<<"No\n";
    }
}


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

typedef long long ll;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}


int main() {
  ll n, m, ans1 = 1, ans2 = 1;
  cin >> n >> m;

  if(abs(n - m) > 1) {
    cout << 0 << endl;
    return 0;
  }

  rep(i, 1, n + 1) {
    ans1 = ans1 * i % 1000000007;
  }
  rep(i, 1, m + 1) {
    ans2 = ans2 * i % 1000000007;
  }

  cout << (ans1 * ans2 * (abs(n - m) == 0 ? 2 : 1)) % 1000000007 << endl;
}


Measured memory_bytes: 256
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
 
    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        k: i32,
        s: i32,
    }

    let mut count = 0;
    for i in 0..k+1 {
        for j in 0..k+1 {
            let tmp = s-i-j;
            if tmp >= 0 && tmp <= k {
                count = count + 1;
            }
        }
    }

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


Measured memory_bytes: 4352
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
 
int ascSort(const void* v1, const void* v2) {
    const int _v1 = *((const int*)v1);
    const int _v2 = *((const int*)v2);
 
    if ( _v1 < _v2 ) {
        return 1;
    } else if ( _v1 > _v2 ) {
        return -1;
    } else {
        return 0;
    }
}

int main() {
	int n,i;
	cin >> n;
	int a[101];
	for(i=0;i<n;i++){
		cin >> a[i];
	}
	int min=1000,max=0;
	for(i=0;i<n;i++){
		if(min>a[i]){
			min = a[i];
		}
		if(max<=a[i]){
			max = a[i];
		}
	}
	cout << max-min << endl;
	return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
broken = set([int(input()) for _ in range(m)]) # ここが'list'だとPypyでも通らない

def ptn_num(x): # 'x'stepまでのパターン数
    dp = [0] * (x+1) # DP枠_including step'0'
    if 1 in broken: # in case, step'1' broken 
        dp[:2] = 1, 0
    else:
        dp[:2] = 1, 1 # Usually

    for i in range(2, x+1):
        if i not in broken: # 階段が壊れてない場合...
            dp[i] = (dp[i-1] + dp[i-2]) % (10**9+7) # -1段目と-2段目の合計
    print(dp[-1])

ptn_num(n)


Measured memory_bytes: 13192
// -*- coding:utf-8-unix -*-

use proconio::input;

fn main() {
    input! {
        n: usize,
    }
    let mut vsp = vec![];
    for i in 0..n {
        input! {
            s: String,
            p: u8,
        }
        vsp.push((s, p, i + 1));
    }
    vsp.sort_by(|a, b| {
        if a.0 != b.0 {
            a.cmp(b)
        } else {
            b.1.cmp(&a.1)
        }
    });
    for i in 0..n {
        println!("{}", vsp[i].2);
    }
}


Measured memory_bytes: 2188
package main

import (
	"fmt"
)

func main() {

	var A, B int
	fmt.Scan(&A, &B)


	for ans := 1; ans <= 10000; ans++ {
		if (ans*108-((ans*108)%100)-ans*100 == A*100) && (ans*110-((ans*110)%100)-ans*100 == B*100) {
			fmt.Println(ans)
			return
		}
	}
	fmt.Println(-1)
}


Measured memory_bytes: 512
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) { return a.second < b.second; }

int N;
vector<pair<int, int>> A;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    A[i] = make_pair(i + 1, a);
  }

  sort(A.begin(), A.end(), cmp);
  for (int i = 0; i < N; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << A[i].first;
  }
  cout << endl;

  return 0;
}

Measured memory_bytes: 1664
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//
//#include <immintrin.h>
//#include <emmintrin.h>

#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define LL long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=998244353;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
inline int getplc(int x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
template <typename T>
// erro
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); errl(_it, args); }

void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}
void Solve();
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

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

void Solve(){
  int n;
  cin>>n;
  cout<<(1000-n%1000)%1000<<endl;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
#define ll long long
#define FLASH ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) --> {4,3,2,1} // rep(it, end(v), begin(v)) --> *it

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<ll> v(n);
    rep(i, 0, n)
        cin >> v[i];
    sort(begin(v), end(v));
    ll ans = 0;
    rep(mid, n, 0)
        rep(low, mid, 0)
        {
            auto itr = lower_bound(begin(v), end(v), v[low] + v[mid]);
            ll ng = distance(itr, end(v));
            ans += (n - 1 - mid) - ng;
        }
    cout << ans;
    return 0;
}

Measured memory_bytes: 3608
#include <bits/stdc++.h> 
using namespace std; 
#define ll long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define all(v) v.begin(),v.end()
#define INF ll(1e18)
#define MOD 1000000007

bool isPrime(int n) 
{  
    if (n <= 1) return false;  
    for (int i = 2; i < n; i++) 
        if (n % i == 0) return false; 
     return true; 
} 

int main()
{
 
      
  ll t=0,k=0,n;
  cin>>n;
  ll a[n];
  for(ll i=0;i<n;i++)
    cin>>a[i];
  sort(a,a+n);
  k+=a[n-1];
  ll j=n-2;
  ll p=0;
  n-=2;
  while(n--)
  {
    if(p%2==0)
    {
      k+=a[j];
      p++;
    }
    else
    {
      k+=a[j];
      p++;
      j--;
    }
  }
  cout<<k<<endl;
}


Measured memory_bytes: 5176
package main

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

var sc = bufio.NewScanner((os.Stdin))

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	l := nextLine()
	i, e := strconv.Atoi(l)
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	S := nextLine()

	fmt.Printf("%d\n", ABC066B(S))
}

// ABC066B ...
func ABC066B(S string) int {
	for i := len(S) - 2; i > 0; i-- {
		if i%2 == 0 {
			continue
		}

		sl, sr := S[0:i/2+1], S[i/2+1:i+1]
		if sl == sr {
			return i + 1
		}
	}

	return -1
}


Measured memory_bytes: 1772
function Main(N) {
	N = N.split(" ");
	var flag = true;
	if(N[0]==N[1]&&N[1]!=N[2]) flag = false;
	else if(N[0]==N[2]&&N[2]!=N[1]) flag = false;
	else if(N[1]==N[2]&&N[1]!=N[0]) flag = false;
	if(flag) console.log("No");
	else console.log("Yes");
}Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());


Measured memory_bytes: 21064
import * as fs from 'fs';

namespace AtCoder.abc116 {

    export class Program {
        /**
         * in
         *
         * out
         *
         */
        static main(inp: string): void {
            const inpList: number[] = inp.split(' ').map(x => parseInt(x, 10)).sort((a, b) => {
                return a - b
            });
            console.log(inpList[0] * inpList[1] / 2);
        }
    }
}

AtCoder.abc116.Program.main(fs.readFileSync('/dev/stdin', 'utf8').trim());


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

int main() {
  int temp;
  int sum = 0;
  for (int i = 0; i < 3; i++) {
    cin >> temp;
    sum += temp;
  }
  if (sum >= 22) {
    cout << "bust" << endl;
  } else {
    cout << "win" << endl;
  }
}

Measured memory_bytes: 256
#include<stdio.h>


int main()
{
	int a,b,max=0;
	int z, x, c;
	scanf("%d %d", &a,&b);
	z = a + b;
	x = a - b ;
	c = a*b;
	 max = z;
	if (x > max) max = x;
	if (c > max) max = c;


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

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

int main() {
    int n; cin >> n;
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        int a; cin >> a;
        if (a % 2 == 1) cnt--;
        if (a % 4 == 0) cnt++;
    }
    string ans = "No";
    if ((n % 2 == 0 && cnt >= 0) || (n % 2 == 1 && cnt >= -1)) ans = "Yes";
    cout << ans << endl;
}

Measured memory_bytes: 256
// Last Change: 05/11/2019 21:39:59.
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

namespace NS {
void LoopUntilZeroInpput() {
  int hogegegege = 0;
  while (cin >> hogegegege && hogegegege != 0) {
  }
}
} // namespace NS

int main() {
  ll N;
  cin >> N;

  ll ans = 0;
  ll bi, af, bothAB;
  bi = af = bothAB = 0;

  for (int j = 0; j < N; ++j) {
    string e;
    cin >> e;

    for (int i = 1; i < e.size(); ++i) {
      if (e[i - 1] == 'A' and e[i] == 'B') {
        ++ans;
      }
    }

    if (e[0] == 'B') {
      ++bi;
    }
    if (e[e.size() - 1] == 'A') {
      ++af;
      if (e[0] == 'B') {
        ++bothAB;
      }
    }
  }

  if (af != 0 and af == bi and af == bothAB) {
    ans += af - 1;
  } else {
    ans += min(af, bi);
  }

  cout << ans << endl;

  //NS::LoopUntilZeroInpput();
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void){
	int s1,s2,sum,No,i;
	char shop[6]={'A','B','C','D','E'};
	while(scanf("%d %d",&s1,&s2),s1>0 && s2>0){
		for(i=0;i<5;i++){
			if(sum<s1+s2){
				sum=s1+s2;
				No=i;
			}
			if(i<4)scanf("%d %d",&s1,&s2);
		}
		printf("%c %d\n",shop[No],sum);
		sum=0;
	}
	return 0;
}

Measured memory_bytes: 600