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

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
const ll INF = (1ll << 50);

ll dp[101][502][101];
ll n;
ll p[101];


int main(){
  while(cin >> n){
    if(n == 0) return 0;
    for(ll i = 0;i < n;i++) cin >> p[i];
    for(ll i = 0;i <= 100;i++){
      for(ll j = 0;j <= 501;j++){
        for(ll k = 0;k <= 100;k++){
          dp[i][j][k] = INF;
        }
      }
    }
    dp[0][0][0] = 0;
    for(ll i = 0;i < n;i++){
      for(ll j = 0;j <= 500;j++){
        for(ll k = 0;k <= 100;k++){
          dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k]);
          if(p[i]%1000 <= 500 && p[i]%1000 > 0){
            dp[i+1][j+(p[i]/1000)+1][k+1] = min(dp[i+1][j+(p[i]/1000)+1][k+1], dp[i][j][k]+p[i]);
          }else if(p[i]%1000 > 500){
            ll nokori = (p[i]%1000) - 500;
            ll kozeni = j*1000-dp[i][j][k]-500*k;
            if(kozeni >= nokori){
              dp[i+1][j+(p[i]/1000)+1][k+1] = min(dp[i+1][j+(p[i]/1000)+1][k+1], dp[i][j][k]+p[i]);
            }else{
              dp[i+1][j+(p[i]/1000)+1][k] = min(dp[i+1][j+(p[i]/1000)+1][k], dp[i][j][k]+p[i]);
            }
          }else{
            ll kozeni = j*1000-dp[i][j][k]-500*k;
            if(kozeni >= 500){
              dp[i+1][j+(p[i]/1000)][k+1] = min(dp[i+1][j+(p[i]/1000)][k+1], dp[i][j][k]+p[i]);

            }else{
              // dp[i+1][j+(p[i]/1000)][k] = min(dp[i+1][j+(p[i]/1000)][k], dp[i][j][k]);

            }
          }
        }
      }
    }
    // for(ll i = 0;i <= n;i++){
    //   for(ll j = 0;j <= 5;j++){
    //     for(ll k = 0;k <= 5;k++){
    //       cerr << dp[i][j][k] << " ";
    //     }
    //     cerr << endl;
    //   }
    //   cerr << endl;
    // }
    ll ans = 0;
    ll cost = INF;
    ll i = n;

    for(ll j = 0;j <= 500;j++){
      for(ll k = 0;k <= 100;k++){
        if(dp[i][j][k] != INF){
          if(ans < k){
            ans = k;
            cost = dp[i][j][k];
            // cerr << j << " " << k << endl;
          }else if(ans == k){
            cost = min(cost, dp[i][j][k]);
          }
        }
      }
    }
    cout << ans  << " " << cost << endl;

  }
}



Measured memory_bytes: 43092
#include "bits/stdc++.h"
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define INF 1e9
using ll = std::int64_t;

template <class T>bool chmax(T &a, const T &b);
template <class T>bool chmin(T &a, const T &b);
//最大公約数
std::int64_t gcd(std::int64_t a, std::int64_t b);
std::int64_t gcd_s(std::int64_t aa, std::int64_t bb);
//ラングレス圧縮
std::vector<std::pair<char, std::int64_t>> rle(std::string str);
//素数判定
bool is_prime(std::int64_t n);
// 約数の列挙
std::vector<std::int64_t> divisor(std::int64_t n);
// 素因数分解
std::map<std::int64_t,std::int64_t> prime_factor(std::int64_t n);

//10^9 + 7
#define yojo 1000000007
#define MAX_NUM 200001

void Main()
{
  int n;
  cin >> n;
  vector<int> w(n);
  for (auto &&i : w)
  {
    cin >> i;
  }
  
  int ans = INF;
  for (int t = 0; t < n; t++)
  {
    int sum1 = accumulate(w.begin(), w.begin() + t, 0);
    int sum2 = accumulate(w.begin() + t, w.end(), 0);
    chmin(ans, abs(sum1-sum2));
  }
  
  std::cout << ans << std::endl;
  return;
}

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

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

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

//最大公約数
std::int64_t gcd(std::int64_t a, std::int64_t b)
{
  // a >= b && b != 0
  std::int64_t c;
  do
  {
    c = a % b;
    a = b;
    b = c;
  } while (c);

  return a;
}

//最大公約数安全版
std::int64_t gcd_s(std::int64_t aa, std::int64_t bb)
{
  std::int64_t a = max(aa, bb);
  std::int64_t b = min(aa, bb);
  std::int64_t c;
  if (b == 0)
  {
    return -1;
  }
  do
  {
    c = a % b;
    a = b;
    b = c;
  } while (c);

  return a;
}

//ラングレス圧縮
std::vector<std::pair<char, std::int64_t>> rle(std::string str)
{
  std::vector<std::pair<char, std::int64_t>> data;
  char now = str[0];
  std::int64_t count = 0;
  for (std::int64_t i = 0; i < str.size() + 1; i++)
  {
    if (i == str.size())
    {
      data.push_back(make_pair(str[i - 1], count));
      break;
    }
    else if (now != str[i])
    {
      data.push_back(make_pair(now, count));
      now = str[i];
      count = 0;
    }
    count++;
  }

  return data;
}

//素数判定
bool is_prime(std::int64_t n)
{
  for (std::int64_t i = 0; i * i <= n; i++)
  {
    if (n % i == 0)
    {
      return false;
    }
  }
  
  return n != 1;
}

//約数の列挙
std::vector<std::int64_t> divisor(std::int64_t n)
{
  std::vector<std::int64_t> res;
  for (int i = 0; i < n; i++)
  {
    if(n % i == 0){
      res.push_back(i);
      if (i != n / i) res.push_back(n / i);
    }
  }
  return res;
}

// 素因数分解
std::map<std::int64_t, std::int64_t> prime_factor(std::int64_t n)
{
  std::map<std::int64_t, std::int64_t> res;
  for (int i = 2; i * i <= n; i++)
  {
    while (n % i == 0)
    {
      ++res[i];
      n /= i;
    }
  }

  if (n != 1)
  {
    res[n] = 1;
  }
  
  return res;
}

Measured memory_bytes: 256
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let n: i32 = lines.next().unwrap().unwrap().parse().unwrap();

    let ans = if n % 2 == 0 {
        0.5
    } else {
        (n + 1) as f32 / (2 * n) as f32
    };
    println!("{}", ans);
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <typename T>
lint largest_rectangle(vector<T> height) {
    vector<int> st;
    height.push_back(0);
    int n = height.size();
    vector<int> left(n);
    lint ret = 0;
    for (int i = 0; i < n; i++) {
        while (!st.empty() && height[st.back()] >= height[i]) {
            ret = max(ret, (lint)(i - left[st.back()]) * height[st.back()]);
            st.pop_back();
        }
        left[i] = st.empty() ? -1 : st.back();
        st.emplace_back(i);
    }
    return ret;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (int i = 0; i < h; ++i) {
        cin >> s[i];
    }
    vector<vector<int>> a(h, vector<int>(w, 0));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (s[i][j] == '#')
                a[i][j] = 1;
        }
    }
    vector<vector<int>> b(h - 1, vector<int>(w - 1, 0));
    for (int i = 0; i < h - 1; ++i) {
        for (int j = 0; j < w - 1; ++j) {
            b[i][j] = 1 ^ a[i][j] ^ a[i + 1][j] ^ a[i][j + 1] ^ a[i + 1][j + 1];
        }
    }
    for (int i = 0; i < h - 1; ++i) {
        for (int j = 0; j < w - 1; ++j) {
            if (b[i][j] == 0)
                continue;
            if (i == 0) {
                if (b[i][j] == 1)
                    b[i][j] = 2;
            } else {
                if (b[i - 1][j] == 0 && b[i][j] == 1)
                    b[i][j] = 2;
                b[i][j] += b[i - 1][j];
            }
        }
    }
    lint ret = max(h, w);
    for (int i = 0; i < h - 1; ++i) {
        ret = max(ret, largest_rectangle(b[i]));
    }
    cout << ret << "\n";
    return 0;
}

Measured memory_bytes: 36096
from itertools import product
N, M = map(int, input().split())
C = [list(map(int, input().split())) for i in range(N)]

P = product((1, -1), repeat=3)
ans = 0
for px, py, pz in P:
    tmp = []
    for x, y, z in C:
        tmp.append(x * px + y * py + z * pz)
    tmp.sort(reverse=True)
    ans = max(ans, sum(tmp[:M]))
print(ans)


Measured memory_bytes: 3384
#include<bits/stdc++.h>
using namespace std;
int main() {
  long n,x,y,i,k,c;
  cin>>n>>x>>y;
  vector<vector<int>> a(n+1,vector<int>(n+1));
  for(k=1;k<=n-1;k++) {
    c=0;
    for(i=1;i<=n-k;i++) {
      if(a.at(i).at(i+k)==0) {
        a.at(i).at(i+k)=1;
        c++;
      }
    }
    for(i=0;i<=k-1;i++) {
      if (1<=x-i && x-i<y+k-1-i && y+k-1-i<=n) {
        if(a.at(x-i).at(y+k-1-i)==0) {
          a.at(x-i).at(y+k-1-i)=1;
          c++;
        }
      }
      if (1<=x-i && x-i<y-(k-1-i) && 1<=y-(k-1-i)) {
        if(a.at(x-i).at(y-(k-1-i))==0) {
          a.at(x-i).at(y-(k-1-i))=1;
          c++;
        }
      }
      if (x+i<=n && x+i<y+k-1-i && y+k-1-i<=n) {
        if(a.at(x+i).at(y+k-1-i)==0) {
          a.at(x+i).at(y+k-1-i)=1;
          c++;
        }
      }
      if (x+i<=n && x+i<y-(k-1-i) && 1<=y-(k-1-i)) {
        if(a.at(x+i).at(y-(k-1-i))==0) {
          a.at(x+i).at(y-(k-1-i))=1;
          c++;
        }
      }
    }
    cout<<c<<endl;
  }
}

Measured memory_bytes: 16000
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip> 
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>

using namespace std;

#define MAXN 110

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

	long long n, i, j, k, best, s, a, b;
	cin >> n;
	long long f[MAXN][5][2], p[MAXN][11];
	vector<long long> ct;
	for (i = 0; i < n; i++) {
		for (j = 0; j < 5; j++) cin >> f[i][j][0] >> f[i][j][1];
	}
	for (i = 0; i < n; i++) {
		for (j = 0; j < 11; j++) cin >> p[i][j]; 
	}

	best = -1LL << 60;
	for (i = 1; i < 1024; i++) {
		ct.assign(n, 0);
		for (j = 0; j < 10; j++) {
			if ((1 << j) & i) {
				a = j % 5; b = j / 5;
				for (k = 0; k < n; k++) {
					if (f[k][a][b] == 1)ct[k]++;
				}
			}
		}
		s = 0;
		for (k = 0; k < n; k++) {
			s += p[k][ct[k]];
		}
		best = max(best, s);
	}
	cout << best << "\n";
	


	return 0;
}


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


int main() {
int a[10],i;	
for(int i=0; i<10; i++) cin>>a[i];
sort(a, a+10);
for(i=9; i>6; i--)
cout<<a[i]<<endl;
// your code goes here
	return 0;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}
template<class T,class E,class F,class G,class H>
struct LazySegmentTree{
	F f;
	G g;
	H h;

	T ti;
	E ei;

	vector<T>dat;
	vector<E>put;
	int sz;
	LazySegmentTree(const T &ti,const E &ei,const F &f,const G &g,const H &h):ti(ti),ei(ei),f(f),g(g),h(h){}

	void build(const vector<T>&v){
		assert(v.size());
		sz=1;
		while(sz<v.size())sz<<=1;
		dat.resize(sz<<1,ti);
		put.resize(sz<<1,ei);
		for(int i=0;i<v.size();i++)dat[sz-1+i]=v[i];
		for(int i=sz-2;i>=0;i--)dat[i]=f(dat[i*2+1],dat[i*2+2]);
	}
	inline void push(int k,int l,int r){
		dat[k]=g(dat[k],put[k],r-l);
		if(k<sz-1){
			put[k*2+1]=h(put[k*2+1],put[k]);
			put[k*2+2]=h(put[k*2+2],put[k]);
		}
		put[k]=ei;
	}

	void modify(int a,int b,E x,int k,int l,int r){
		push(k,l,r);
		if(r<=a||b<=l)return;
		if(a<=l&&r<=b){
			put[k]=x;
			push(k,l,r);
			return;
		}
		modify(a,b,x,k*2+1,l,(l+r)/2);
		modify(a,b,x,k*2+2,(l+r)/2,r);
		dat[k]=f(dat[k*2+1],dat[k*2+2]);
	}

	inline void modify(int a,int b,E x){
		return modify(a,b,x,0,0,sz);
	}
	T query(int a,int b,int k,int l,int r){
		push(k,l,r);
		if(r<=a||b<=l)return ti;
		if(a<=l&&r<=b)return dat[k];
		return f(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
	}
	inline T query(int a,int b){
		return query(a,b,0,0,sz);
	}
};

signed main(){
	int ti=0;
	int ei=-1001001001;
	auto f=[&](int a,int b){return a+b;};
	auto g=[&](int a,int b,int l){
		if(b==ei)return a;
		return b*l;
	};
	auto h=[&](int a,int b){
		if(b==ei)return a;
		return b;
	};
	LazySegmentTree<int,int,decltype(f),decltype(g),decltype(h)>seg(ti,ei,f,g,h);

	int N,Q;
	scanf("%lld%lld",&N,&Q);
	seg.build(vint(N,0));
	while(Q--){
		int p,s,t,x;
		scanf("%lld%lld%lld",&p,&s,&t);
		t++;
		if(p==0){
			scanf("%lld",&x);
			seg.modify(s,t,x);
		}
		else{
			printf("%lld\n",seg.query(s,t));
		}
	}
	return 0;
}


Measured memory_bytes: 7864
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>	
#include <cmath>
#include <string>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout<<s<<endl
using namespace std;
typedef long long ll;
string Yes(bool b) { if (b) { return "Yes"; } else { return "No"; } }
string YES(bool b) { if (b) { return "YES"; } else { return "NO"; } }
//================================================

bool primejudge(const ll x) {
	if (x == 2)return true;
	if (x < 2 || x % 2 == 0)return false;
	for (int i = 3; i * i <= x; i += 2)
		if (x % i == 0)return false;
	return true;
}

int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;


int main()
{
	ll n;
	cin >> n;
	int flag=1;
	for (int i = sqrt(n); i>0; i--)
	{
		if (n % i == 0)
		{
			flag = i; break;
		}
	}
	cout << flag + n / flag - 2 << endl;

	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstring>/*The header <string> and
<string.h> does not contain the useful
function 'memset' and 'memcpy'.*/
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<utility>
#include<algorithm>
#include<functional>
#include<ctime>
#include<cmath>
#include<cstdlib>
#include<cstdio>
using namespace std;
int ans=10000000,N,W[110];
int main()
{
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>W[i];
	for(int T=0;T<N-1;T++)
	{
		int S1=0,S2=0;
		for(int i=0;i<=T;i++)
			S1+=W[i];
		for(int i=T+1;i<N;i++)
			S2+=W[i];
		ans=min(ans,abs(S1-S2));
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
#include<string.h>
#include<set>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include <cstdio>
#include <cstdlib>
#include<stack>
#include<vector>
int n,m;
int dp[55][55];
int a[55];
int b[55];
struct lll
{
    int x,y;
}TM1[55];

struct qqq
{
    int len=1;
    int s[55];
}TM[55];
int flag=0;
void dfs(int x,int jieshu)
{
    if(flag)
        return ;
    a[x]=1;
    //cout<<x<<"_"<<jieshu<<endl;
    if(x==jieshu)
    {
        flag=1;
        return ;
    }
    for(int i=1;i<TM[x].len;i++)
    {
        if(a[TM[x].s[i]]==0&&TM[x].s[i]!=-1)
        {
            dfs(TM[x].s[i],jieshu);
        }
    }
}
int main()
{
    cin>>n>>m;
    int x,y;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        TM1[i].x=x;
        TM1[i].y=y;
        TM[x].s[TM[x].len++]=y;
        TM[y].s[TM[y].len++]=x;
        //cout<<TM[x].len<<"_"<<endl;
    }
    int sum=0;
    for(int i=1;i<=m;i++)
    {
        x=TM1[i].x;
        y=TM1[i].y;
        flag=0;
        int t=0;
        memset(a,0,sizeof(a));
         for(int j=1;j<=TM[x].len;j++)
         {
             //cout<<TM[x].s[j]<<"********"<<y<<endl;
             if(TM[x].s[j]==y)
             {

                 TM[x].s[j]=-1;
                 //cout<<TM[x].s[j]<<"********"<<endl;
                 t=j;
                 break;
             }
         }
         dfs(x,y);
         TM[x].s[t]=y;
         if(!flag)
            {
                sum++;
            }
    }
    cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.*;

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


Measured memory_bytes: 51740
#include<cstdio>

#define MAX 100

int num[MAX], N;

bool solve(int i, int m)
{
    if (0 == m) return true;
    if (i >= N) return false;
    return ((solve(i + 1, m)) || (solve(i + 1, m - num[i])));
}

int main()
{
    int i, j, k;
    scanf("%d", &N);
    for (i = 0; i < N; i++) {
        scanf("%d", &num[i]);
    }
    scanf("%d", &k);
    for (i = 0; i < k; i++) {
        scanf("%d", &j);
        if (solve(0, j)) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

Measured memory_bytes: 2604
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
vector<char>NUM(10);
	NUM = { '0','1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
*/



int main() {
	int N, M;
	cin >> N >> M;
	vector<int>T(M);
	rep(i, N) {
		int K;
		cin >> K;
		rep(j, K) {
			int F;
			cin >> F;
			T[F - 1]++;
		}
	}
	int cnt = 0;
	rep(i, M) {
		if (T[i] == N) {
			cnt++;
		}
	}
	cout << cnt << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    string s,t;
    ll a,b,c,d,e,i,j,k=99999999,l=0;
    cin>>a>>b;
  for(i=1;i<=a;i++)
  {
   cin>>c>>d;
   if(d<=b)
        {l=1;
        if(c<k){k=c;e=i;}}
  }
    if(l==0)cout<<"TLE\n";
 else cout<<k<<endl;
 }

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define MOD (1e9+7)
void bulk(){}template<class H,class...T>void bulk(H&h,T&...t){cin>>h;bulk(t...);}
#define get(...) int __VA_ARGS__; bulk(__VA_ARGS__);


int main(){
    get(n);
	vvi p(2,vi(n+1,0));
	rep(i,n)cin>>p[0][i+1];
	rep(i,n)cin>>p[1][i+1];
	partial_sum(all(p[0]),p[0].begin());
	partial_sum(all(p[1]),p[1].begin());
	int ans=-1;
	rep(i,n){
	    ans=max(ans,p[0][i+1]-p[0][0]+p[1][n]-p[1][i]);
	}
	cout<<ans<<endl;
	return 0;
}


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

Ans = [0] * (10**5 + 1)

for a in A:
    Ans[a] += 1

Q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(Q)]

ans = sum(A)
for b, c in BC:
    x = Ans[b]
    Ans[b] = 0
    Ans[c] += x
    ans += (c-b) * x
    print(ans)


Measured memory_bytes: 90824
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int> >
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf 1000000000000000000

using namespace std;

const int mod=1e9+7;

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

void run(){
    int t;
    cin>>t;
    int h=t/30;
    int m=(t-h*30)*2;
    cout<<h<<" "<<m<<endl;
}


Measured memory_bytes: 3212
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
ll ara[200005];
ll n,k;
bool func(ll x)
{
    ll s=0;
    for(ll i=1;i<=n;i++)
    {
       if(ara[i]<=x)
       {
           continue;
       }
       if(x==0)
       {
           return 0;
       }
       ll d=ara[i]/x;
       /*if((d-1)!=0)
       {
           double d1=(double)ara[i]/(d-1);
           ll a=round(d1);
           if(a<=x)
           {
               ans+=(d-1);
           }
       }*/
       s+=d;
    }
    if(s<=k)return 1;
    return 0;
}
main()
{
    ll i,r=-1;
    cin>>n>>k;
    for(i=1;i<=n;i++)
    {
        cin>>ara[i];
        r=max(r,ara[i]);
    }
    ll l=0;
    while(l<r)
    {
       ll mid=(l+r)/2;
       if(func(mid)==1)
       {
           r=mid;
       }
       else
       {
           l=mid+1;
       }
    }
    cout<<l<<endl;
}


Measured memory_bytes: 5128
N,Q = map(int,input().split())
s = input()
t = [0,0]
for i in range(1,N):
    if s[i-1:i+1] == "AC":
        t.append(t[i]+1)
    else:
        t.append(t[i])
for i in range(Q):
    l,r = map(int,input().split())
    print(t[r]-t[l])

Measured memory_bytes: 6176
#include <iostream>
#include <vector>
using namespace std;
int d[10][10] = {},c[10][10] = {};
int main(){
	int i,j,k;
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			if(i!=j) d[i][j] = 100;
		}
	}
	for(i=0;i<3;i++){
		int a,b;
		cin >> a >> b;
		a--; b--;
		d[a][b] = 1;  d[b][a] = 1;
	}
	
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			for(k=0;k<4;k++){
				d[j][k] = min(d[j][k],d[j][i] + d[i][k]);
			}
		}
	}
	int mx = 0;
	for(i=0;i<4;i++){
		for(j=0;j<4;j++){
			mx = max(mx,d[i][j]);
		}
	}
	if(mx==3){
		cout << "YES" << endl;
	}else{
		cout << "NO" << endl;
	}
}
		

Measured memory_bytes: 256
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    S = input()
    key = "keyence"
    if key in S:
        print("YES")
        return

    for i in range(len(S) - 1):
        for j in range(i + 1, len(S)):
            if S[:i] + S[j:] == key:
                print("YES")
                return

    print("NO")
    return


if __name__ == "__main__":
    main()


Measured memory_bytes: 63944
#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

char myf(char x){
    if(x=='A') return 'a';
    if(x=='B') return 'b';
    if(x=='C') return 'c';
}

signed main(){
    int N,K;
    string S;
    cin>>N>>K>>S;
    S[K-1]=myf(S[K-1]);
    cout<<S<<endl;
}

Measured memory_bytes: 256
/**
 * B - Balance
 * https://atcoder.jp/contests/abc129/tasks/abc129_b
 **/
#include<iostream>
#include<algorithm>
#include<numeric>

using namespace std;

const int MAX_N = 100;

int N;
int w[MAX_N+1];
int t_diff[MAX_N+1];

void solve() {
    int all_sum = accumulate(w, w+N, 0);
    for (int i = 0; i < N-1; i++) {
        int t_sum = accumulate(w, w+i+1, 0);
        t_diff[i] = abs(all_sum - 2 * t_sum);
    }
    sort(t_diff, t_diff+N-1);
    cout << t_diff[0] << endl;
}

int main() {
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> w[i];
    }
    solve();
}

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

using namespace std;

using ll = long long;

vector<ll> fact;
vector<ll> ifact;
const int mod = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll ret = (n & 1) ? x : 1;
  ll half = mod_pow(x, n / 2, mod);
  ret *= half * half % mod;
  return ret % mod;
}

void init() {
  fact.resize(200001);
  ifact.resize(200001);
  fact[0] = 1, ifact[0] = 1;
  for(int i = 1; i <= 200000; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    ifact[i] = mod_pow(fact[i], mod - 2, mod);
  }
}

ll comb(ll n, ll k) {
  return fact[n] * ifact[n - k] % mod * ifact[k] % mod;
}

int main() {
  init();
  ll N, M, K;
  cin >> N >> M >> K;
  ll sum = 0;
  for(ll d = 1; d <= N; d++) {
    sum += (d * (N - d)) % mod * M % mod * M % mod;
    sum %= mod;
  }
  for(ll d = 1; d <= M; d++) {
    sum += (d * (M - d)) % mod * N % mod * N % mod;
    sum %= mod;
  }
  cout << sum * comb(N * M - 2, K - 2) % mod << endl;
}


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

int main(){
  int N;
  cin>>N;
  vector<string>s(N);
  vector<int>t(N);
  string X;
   for(int i=0;i<N;i++){
    cin>>s.at(i)>>t.at(i);
   }
  cin>>X;
  
  int ans=0;
  for(int i=0;i<N;i++){
    if(s.at(i)==X){
      for(int j=i+1;j<N;j++){
        ans+=t.at(j);
      }
      break;
    }
  }
  cout<<ans<<endl;
}
        

Measured memory_bytes: 3580
n = int(input())
s = [input() for i in range(n)]
x = [51]*26
for i in s:
	for j in range(97,123):
		if i.count(chr(j))<x[j-97]:
			x[j-97] = i.count(chr(j))
answer = ""
for i in range(26):
	answer += chr(i+97)*x[i]
print(answer)

Measured memory_bytes: 3064
using System;
using System.Linq;
using System.Collections.Generic;

namespace b
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = Console.ReadLine().ToCharArray();
            var h = new HashSet<char>(s);

            var flag = true;
            foreach(var c in h){
                var count = s.Count(x => x == c);
                if(count % 2 != 0){
                    flag = false;
                    break;
                }
            }

            Console.WriteLine(flag ? "Yes" : "No");
        }
    }
}


Measured memory_bytes: 13280
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>//sort,reverse
#include<utility>//pair
#include<queue>//queue,priority_queue
#include<deque>//double_ended queue
#include<map>//map
using namespace std;
typedef long long LL; 
typedef pair<LL,LL> P;//Pでpair<-,->を表す。
const LL MAX=2000010;
const LL MOD=1000000007;
typedef map<LL,LL>::iterator Itr;

int main()
{ 
    LL x;
    cin>>x;
    LL a=100;
    LL ans=0;
    while(a<x){
        a=a+a/100;
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 3608
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author KharYusuf
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CTypicalStairs solver = new CTypicalStairs();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTypicalStairs {
        public void solve(int testNumber, FastReader s, PrintWriter w) {
            int n = s.nextInt(), m = s.nextInt();
            long dp[] = new long[n + 1], mod = (long) 1e9 + 7;
            boolean broken[] = new boolean[n + 1];
            dp[0] = 1;
            while (m-- > 0) {
                broken[s.nextInt()] = true;
            }
            for (int i = 1; i <= n; i++) {
                if (i - 1 >= 0 && !broken[i - 1]) {
                    dp[i] = (dp[i] + dp[i - 1]) % mod;
                }
                if (i - 2 >= 0 && !broken[i - 2]) {
                    dp[i] = (dp[i] + dp[i - 2]) % mod;
                }
            }
            w.println(dp[n]);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        public int nextInt() {

            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {

            if (filter != null)
                return filter.isSpaceChar(c);

            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}



Measured memory_bytes: 23380
// https://atcoder.jp/contests/abc149/tasks/abc149_c

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
typedef long long ll;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<ll> v);
template <typename T>
void drop(T a);

const ll INF = 1e18;

void solve() {
  ll X;
  cin >> X;

  auto is_prime = [&](ll a) -> bool {
    for (ll div = 2; div * div <= a; div++) {
      if (a % div == 0) return false;
    }

    return true;
  };

  repf(num, X, 1000 * X) {
    if (is_prime(num)) drop(num);
  }
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

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

  try {
    solve();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

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

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

void print_ints(vector<ll> v) {
  rep(i, v.size()) {
    if (i > 0) {
      cout << " ";
    }

    cout << v[i];
  }

  cout << endl;
}

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int n;
vector<P> v;
vector<int> a;


void f(int x,int y){
  rep(i,a.size()){
    if(abs(x)>abs(y)){
      if(x>0){
        x-=a[i];
        cout<<'R';
      }else{
        x+=a[i];
        cout<<'L';
      }
    }else{
      if(y>0){
        y-=a[i];
        cout<<'U';
      }else{
        y+=a[i];
        cout<<'D';
      }
    }
  }
  cout<<endl;
//  cout<<x<<' '<<y<<endl;
}


main(){
  cin>>n;
  rep(i,n){
    int x,y;
    cin>>x>>y;
    v.PB(P(x,y));
  }

  int r=2;
  rep(i,38){
    a.PB(r);
    r*=2;
  }

  int b=(abs(v[0].F)+abs(v[0].S))%2;

  rep(i,n){
    if((abs(v[i].F)+abs(v[i].S))%2!=b){
      cout<<-1<<endl;
      return 0;
    }
  }

  a.PB(1);
  a.PB(1);

  if((abs(v[0].F)+abs(v[0].S))%2)
    a.erase(a.end()-1);

  sort(all(a));
  reverse(all(a));

  cout<<a.size()<<endl;
  rep(i,a.size()){
    if(i)cout<<' ';
    cout<<a[i];
  }
  cout<<endl;

  rep(i,n){
    f(v[i].F,v[i].S);
  }

}



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

int main() {
  int n;
  cin >> n;
  vector<P> vec(n);
  rep(i,n) {
    cin >> vec.at(i).first;
    vec.at(i).second = i;
  }
  sort(vec.begin(), vec.end());
  rep(i,n) {
    cout << vec.at(i).second+1;
    if(i < n-1) cout << " ";
    else cout << endl;
  }
  return 0;
}

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

using namespace std;

const long long MOD = 1000000007LL;
long long GCD(long long a, long long b){return b == 0 ? a : GCD(b, a % b);}

int main(){

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

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    string a;
    cin >> a;

    int r = 0, p = 0, tot = 0;

    for(auto i : a){
        if(i == 'p'){
            if(r >= p + 1){
                ++p;
            } else {
                ++r;
                --tot;
            }
        } else {
            if(r >= p + 1){
                ++tot;
                ++p;
            } else {
                ++r;
            }
        }
    }

    cout << tot;

    return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <map>
#include <utility>
#include <numeric>
#include <typeinfo>

using namespace std;

int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for(int i=0;i<N;i++)  cin >> a.at(i);

  sort(a.begin(),a.end());
  int ans=0;
  int count=1;
  for(int i=0;i<N;i++){
    if(i<N-1 && a.at(i)==a.at(i+1)){
      count++;
      continue;
    }
    if(count>=a.at(i))  ans+=count-a.at(i);
    else ans+=count;
    count=1;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 640
#include <stdio.h>

int main(){
	int countTable[256] = {0};
	char ch;

	while((ch = getchar()) != EOF){
				countTable[ch]++;
	}
	for(int i = 97; i <=122;i++)printf("%c : %d\n",i,countTable[i]+countTable[i-32]);
}


Measured memory_bytes: 2620
/**
 *   
 *   Date: 30 March 2020
 *   Time: 13:42:55
**/
#pragma GCC optimize("O3")
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cmath>
#include <cstring>
#include<queue>
#include<stack>
#include<climits>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>
#define endl "\n"
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vec;
typedef vector<vector<ll> > matrix;
ll const mod=1000000007;
ll dp[102][100100];
int main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL);
	ll n,w,s=0,d;
	cin>>n>>w;
	matrix v1(n+2,vector<ll>(3));
	for(ll i=1;i<=n;i++)
	{
		cin>>v1[i][0]>>v1[i][1];
		s+=v1[i][1];
	}
	for(ll i=0;i<=n;i++)
	{
		for(ll j=0;j<=s;j++)
			dp[i][j]=INT_MAX;
	}
	//dp[1][v1[1][1]]=v1[1][0];
	dp[0][0]=0;
	for(ll i=1;i<=n;i++)
	{
		for(ll j=0;j<=s;j++)
		{
			// if(i == 0 || j == 0)
			// {
			// 	dp[i][j]=0;
			// 	continue;
			// }
			//dp[i][j]=dp[i-1][j];
			dp[i][j]=dp[i-1][j];
			if(j-v1[i][1] >= 0 && dp[i-1][j-v1[i][1]]+v1[i][0] <= w)
				dp[i][j]=min(dp[i][j],dp[i-1][j-v1[i][1]]+v1[i][0]);
		}
	}
	d=INT_MAX;
	for(ll i=s;i>=0;i--)
	{
		if(dp[n][i] != INT_MAX)
		{
			cout<<i<<endl;
			return 0;

		}
	}
	cout<<d<<endl;

return 0;
}

Measured memory_bytes: 79232
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include <cmath>
#include <limits>
#include <utility>
#include <map>
#include <iomanip>
#include <queue>
#include <cctype>
#include <stack>
#include <random>
#include <list>
#include <numeric>
#include <chrono>
#include <set>


#define rep(i,p,q) for(ll i = p;i < q;i++)
#define ll long long

using namespace std;

vector<vector<ll>> graph;
vector<ll> query;
vector<ll> ans;
vector<ll> visited;


const long long MAX_N = 1 << 17;

ll ecd(ll a, ll b) {
	if (a < b) swap(a, b);
	if (b == 0) return a;
	else {
		return ecd(b, a % b);
	}
}

ll pow(ll a, ll b) {
	vector<ll> d = vector<ll>((ll)(log(b)) + 2);
	d[0] = a;
	rep(i, 1, d.size()) d[i] = d[i - 1] * d[i - 1];
	ll ret = 1;
	rep(i, 0, log(b) + 1) {
		if (b & 1 << i) ret *= d[i];
	}
	return ret;
}

class UnionFind
{

private:
	std::vector<long long> par;
	std::vector<long long> rank;

public:
	UnionFind(long long n) {
		par = std::vector<long long>(n);
		rank = std::vector<long long>(n);
		for (long long i = 0; i < n; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	};

	long long find(long long x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	void unite(long long x, long long y) {
		x = find(x);
		y = find(y);
		if (x == y)return;

		if (rank[x] < rank[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (rank[x] == rank[y])rank[x]++;
		}

	}

	bool same(long long x, long long y) {
		return find(x) == find(y);
	}

	long long count() {
		long long ret = 0;
		for (long long i = 0; i < par.size(); i++) {
			if (i == par[i])ret++;
		}
		return ret;
	}
};

int main(void) {
	ll n, m;
	cin >> n >> m;
	vector<ll> p = vector<ll>(n);
	rep(i, 0, n) cin >> p[i];
	UnionFind uf = UnionFind(n + 1);
	rep(i, 0, m) {
		ll x, y;
		cin >> x >> y;
		x;
		y;
		uf.unite(x, y);
	}
	ll ans = 0;
	rep(i, 0, n) {
		if (uf.same(i + 1, p[i])) ans++;
	}
	cout << ans << endl;
}

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

int main(){
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) cin >> a[i];
    deque<int> deq;
    for(int i = 0; i < n; i++){
        if(i % 2 == 0) deq.push_back(a[i]);
        else deq.push_front(a[i]);
    }
    if(n % 2){
        while(!deq.empty()){
            cout << deq.back() << (deq.size() != 1 ? " " : "\n");
            deq.pop_back();
        }
    }else{
        while(!deq.empty()){
            cout << deq.front() << (deq.size() != 1 ? " " : "\n");
            deq.pop_front();
        }
    }
    return 0;
}

Measured memory_bytes: 3840
#define r(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
#define f first
#define endl "\n"
#define s second
#define pb push_back
#define ms(a,b) memset(a,b,sizeof(a))
#define fori(a,b) for(int i=a;i<b;i++)
#define forj(a,b) for(int j=a;j<b;j++)
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
char _;

ll n;
ll nums[81];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cin>>n;
	ll money = 1000;
	ll stocks=0;
	fori(0,n) cin>>nums[i];
	nums[n] = -1e9;
	for(int i=0;i<n;i++){
		if(nums[i]<nums[i+1]){
			stocks+=money/nums[i];
			money%=nums[i];
		}
		else if(nums[i]>nums[i+1]){
			money+=stocks*nums[i];
			stocks=0;
		}
	}
	cout << money << endl;

	return 0;
}


Measured memory_bytes: 3676
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

bool iseven(int n) { return (n % 2) == 0; }

int main() {
  int n, cn = 0;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  for (int i = 0;; ++i) {
    if (i == n) {
      i = 0;
      cn++;
    }
    if (a[i] % 2 == 0) {
      a[i] /= 2;
    } else {
      break;
    }
  }
  cout << cn << endl;
  return 0;
}


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

public class Main{
    static MyReader in = new MyReader();

    public static void main(String[] args){       
        ModOp.init();
        int H, W, A, B; {int[] a = in.ii(); H = a[0]; W = a[1]; A = a[2]; B = a[3];}
        long ans = 0;
        final int c1 = B-1, c2 = H+W-B-2, c3 = W-B-1;
        for(int i = H-A-1; i > -1; i--){
            ans = (ans + ModOp.com(i+c1, c1) * ModOp.com(c2-i, c3)) % ModOp.MOD;
        }
        out.println(ans);
    }

    static class ModOp {
        final static long MOD = 1_000_000_007;
        final static int MAX = 200_000;
        static long[] fact;
        static long[] factInv;
        static long[] inv;

        static long pow(long a, long b){
            long ret = 1;
            while(b > 0){
                if(b%2 == 1){
                    ret = ret*a%MOD;
                }
                a = a*a%MOD;
                b /= 2;
            }
            return ret;
        }

        static long mul(int s, int e){
            long ret = 1;
            for(int i = s; i <= e; i++){
                ret = ret*i%MOD;
            }
            return ret;
        }

        static long fact(int n){
            return mul(1, n);
        }

        static long nCr(int n, int r){
            return mul(n-r+1, n) * pow(fact(r), MOD-2) % MOD;
        }

        static void init() {
            fact = new long[MAX+1];
            factInv = new long[MAX+1];
            inv = new long[MAX+1];
            fact[0] = fact[1] = factInv[0] = factInv[1] = inv[1] = 1;
            for (int i = 2; i <= MAX; i++){
                fact[i] = fact[i - 1] * i % MOD;
                inv[i] = MOD - inv[(int)(MOD%i)] * (MOD / i) % MOD;
                factInv[i] = factInv[i - 1] * inv[i] % MOD;
            }
        }

        static long com(int n, int k){
            return n < k || n < 0 || k < 0 ? 0 : 
                   fact[n] * (factInv[k] * factInv[n - k] % MOD) % MOD;
        }

    }

    static class MyReader extends BufferedReader{
        MyReader(){
            super(new InputStreamReader(System.in));
        }

        String s(){
            try{return readLine();}catch(IOException e){return "";}
        }

        String[] ss(){
            return s().split(" ");
        }

        int i(){
            return Integer.parseInt(s());
        }

        int[] ii(){
            String[] ss = ss();
            int[] ii = new int[ss.length];
            for(int j = 0; j < ss.length; j++) ii[j] = Integer.parseInt(ss[j]);
            return ii;
        }

        ArrayList<Integer> li(){
            String[] ss = ss();
            ArrayList<Integer> li = new ArrayList<>();
            for(int j = 0; j < ss.length; j++) li.add(Integer.parseInt(ss[j]));
            return li;
        }

        long l(){
            return Long.parseLong(s());
        }

        long[] ll(){
            String[] ss = ss();
            long[] ll = new long[ss.length];
            for(int j = 0; j < ss.length; j++) ll[j] = Long.parseLong(ss[j]);
            return ll;
        }
    }
}


Measured memory_bytes: 25940
using System.Collections.Generic;
using System.Linq;
using System;
class Program
{
	static void Main()
    {
        int[] inputs = Console.ReadLine().Split(' ')
                              .Select(elem => int.Parse(elem))
                              .ToArray();

        bool IsAOn = false;
        bool IsBOn = false;
        int count = 0;
        for (int i = 0; i <= 100; i++)
        {
            if (i == inputs[0])
                IsAOn = true;

            if (i == inputs[1])
                IsAOn = false;

            if (i == inputs[2])
                IsBOn = true;

            if (i == inputs[3])
                IsBOn = false;

            if (IsAOn && IsBOn)
                count++;
        }
        Console.WriteLine(count.ToString());
	}
}

Measured memory_bytes: 13268
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int N = sc.nextInt();
		if(N%1000 != 0) {

			System.out.println(1000-N%1000);
		}else {

			System.out.println(0);
		}
	}

	static class FastScanner {
		private BufferedReader reader = null;
	    private StringTokenizer tokenizer = null;
	    public FastScanner(InputStream in) {
	        reader = new BufferedReader(new InputStreamReader(in));
	        tokenizer = null;
	    }

	    public String next() {
	        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
	            try {
	                tokenizer = new StringTokenizer(reader.readLine());
	            } catch (IOException e) {
	                throw new RuntimeException(e);
	            }
	        }
	        return tokenizer.nextToken();
	    }

	    public String nextLine() {
	        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
	            try {
	                return reader.readLine();
	            } catch (IOException e) {
	                throw new RuntimeException(e);
	            }
	        }

	        return tokenizer.nextToken("\n");
	    }

	    public long nextLong() {
	        return Long.parseLong(next());
	    }

	    public int nextInt() {
	        return Integer.parseInt(next());
	    }

	    public double nextDouble() {
	         return Double.parseDouble(next());
	     }

	    public int[] nextIntArray(int n) {
	        int[] a = new int[n];
	        for (int i = 0; i < n; i++)
	            a[i] = nextInt();
	        return a;
	    }

	    public long[] nextLongArray(int n) {
	        long[] a = new long[n];
	        for (int i = 0; i < n; i++)
	            a[i] = nextLong();
	        return a;
	    }
	}
}


Measured memory_bytes: 32788