akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>

using namespace std;

int a[2'009];

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  int cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (a[j] > a[i]) {
        ++cnt1;
      }
    }
    for (int j = 0; j < n; ++j) {
      if (a[j] > a[i]) {
        ++cnt2;
      }
    }
  }
  const int mod = 1'000'000'007;
  long long ans = 0;
  ans += (long long)cnt1 * m % mod;
  ans += (long long)(m - 1) * m / 2 % mod * cnt2 % mod;
  ans %= mod;
  printf("%d\n", (int)ans);
}


Measured memory_bytes: 128
# coding: utf-8
class Case02

  def initialize
  end

  def show
    p self.instance_variables
        .map{ |sym| [sym, self.instance_variable_get(sym)] }
        .to_h
  end
  
  # 実行する
  def execute
    read
    solve
    output
  end

  def read
    @n = gets.chomp.to_i
    @a = gets.chomp.split.map(&:to_i)
    @b = gets.chomp.split.map(&:to_i)
    @c = gets.chomp.split.map(&:to_i)
  end

  def solve
    @result = 0
    1.upto(@n) do |i|
      x = @a[i-1]
      @result += @b[x-1]
      if i > 1 && x == (@a[i-2] + 1) then
        @result += @c[x-2]
      end
    end
  end
  
  def output
    puts @result
  end

end

Case02.new.execute if $0 == __FILE__



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

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

    int one_max_index = -1;
    char next_char;
    for(int i=0; i<S.size(); i++){
        char c = S[i];
        if(c!='1'){
            next_char = S[i];
            break;
        }
        one_max_index += 1;
    }

    if(K-1 <= one_max_index){
        cout << '1' << endl;
    }else{
        cout << next_char << endl;
    }

    return 0;
}

Measured memory_bytes: 3572
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define per(i, n, s) for (int i = (n-1); i >= (int)(s); i--)
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<x<<endl
using namespace std;
using ll = long long;
const ll MOD = 1e9+7;
const ll LINF = LLONG_MAX;
const int INF = INT_MAX;

int main(){
    int a,b,c,d,e,f; cin>>a>>b>>c>>d>>e>>f;
    vector<int> sug;
    rep(i,0,3001){
        rep(j,0,3001){
            sug.push_back(c*i+d*j);
        }
    }
    sort(all(sug));
    double maxi=0;
    int ans_sugar=0;
    int ans_eki=0;
    rep(i,0,30){
        rep(j,0,30){
            if(i==0 and j==0) continue;
            int water=100*a*i+100*b*j;
            int limit=f-water;
            int solve=water/100*e;
            if(limit<=0) continue;
            
            // debug(solve);
            int sugar=*(upper_bound(all(sug),min(limit,solve))-1);
            // debug(sugar);
            if(sugar/(water+sugar+0.0)>=maxi){
                maxi=sugar/(water+sugar+0.0);
                ans_sugar=sugar;
                ans_eki=sugar+water;
                // debug(maxi);
            }

        }
    }
    cout<<ans_eki<<" "<<ans_sugar<<endl;
}

Measured memory_bytes: 67808
#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <set>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int a[1000][1000];
int hh[1000];
int ww[1000];

int main(){
  const int h = getInt();
  const int w = getInt();

  REP(i,h) REP(j,w){
    a[i][j] = getInt();
    hh[i] += a[i][j];
    ww[j] += a[i][j];
  }

  REP(i,h) REP(j,w)
    printf("%d%c", (3 * a[i][j] + hh[i] + ww[j] + 1) % 2, j == w - 1 ? '\n' : ' ');

  return 0;
}

Measured memory_bytes: 4944
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template<class T>inline bool chmax(T &a, const T &b){if(a < b){a = b; return 1;}return 0;}
template<class T>inline bool chmin(T &a, const T &b){if(a > b){a = b; return 1;}return 0;}
typedef long long ll;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, K; cin >> N >> K;
  ll ans = 0;
  for(ll i = K+1; i <= N; i++){
    if(K != 0){
      ans += (i-K)*((N+1)/i) + max(0LL, (N+1)%i-K);
    }else{
      ans += (i-K)*((N+1)/i) + max(0LL, (N+1)%i-K) - 1;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h> 
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(vec) vec.begin(), vec.end()
#define tmax(x,y,z) max((x),max((y),(z)))
#define tmin(x,y,z) min((x),min((y),(z)))
typedef long long ll;ll MM = 1000000000;ll mod = MM + 7; ll MMM=9223372036854775807;//2^63 -1
ll GCD(ll x, ll y) {if (y == 0) return x;else return GCD(y, x % y);}
ll LCM(ll x, ll y) { return x / GCD(x, y) * y;}
//cout << fixed << setprecision(10);

int main(){
  ll n, m, ans=0;
  cin >> n >> m;
  set<ll> v;
  ll a;
  rep(i,n){
    cin >> a;
    v.insert(a);
  }
  
  if(v.size() != 1){
    ll ma = *rbegin(v);
    v.erase(ma);
    ll next = *rbegin(v);
    v.erase(next);
    ll i = ma/2;
    while(i <= m){
      bool t = true; 
      if((i - next/2) % next != 0) t = false; 
      if(t){
        for(auto val : v){
          if((i - val/2) % val != 0){
            t = false;   
            break;
          }
        }
      }
    if(t) ans++;
    i += ma;
    }
  }
 ///////////////////////////////// 
  else{
    ll ma = *rbegin(v);
    v.erase(ma); 
    ll i = ma/2;
    while(i <= m){
      bool t=true;    
      for(auto val : v){
        if((i - val/2) % val != 0){
          t = false;   
          break;
        }
      } 
      if(t) ans++;
      i += ma;
    }
  }
    
  cout<<ans<<endl; 
}

Measured memory_bytes: 4992
#include <iostream>
using namespace std;
void ABC120(void);
void ABC121(void);
void ABC122(void);
void ABC123(void);
void ABC124(void);
void ABC125(void);
void ABC126(void);
void ABC127(void);
void ABC128(void);
void ABC129(void);

int main(void){
    ABC120();
}

void ABC120(){
    int a,b,c;
    cin>>a>>b>>c;
    if(b>=a*c) cout<<c<<endl;
    else cout<<b/a<<endl;
}

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

using namespace std;

typedef long long int ll;

void readVec(vector<ll>& v, int n)
{
	for(int i = 0; i < n; ++i)
		cin >> v[i];
}

int main()
{
	ll n, m, k; cin >> n >> m >> k;

	vector<ll> a(n);
	vector<ll> b(m);

	readVec(a, n);
	readVec(b, m);

	for(int i = 1; i < m; ++i)
	{
		b[i] += b[i-1];
		//cout << b[i] << endl;
	}	

	int count = upper_bound(b.begin(), b.end(), k) - b.begin();
	assert(count >= 0);
	for(int i = 0; i < n; ++i)
	{
		k -= a[i];
		if(k >= 0)
		{
			int picksFromB = upper_bound(b.begin(), b.end(), k) - b.begin();
			assert(picksFromB >= 0);
			count = max(count, i+1+picksFromB);
		}
		else
		{
			break;
		}
	}

	cout << count;
	return 0;
}

Measured memory_bytes: 6432
n=int(input())
a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=int(input())
print(5 + int(max((n-0.1)//a,(n-0.1)//b,(n-0.1)//c,(n-0.1)//d,(n-0.1)//e)))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define FOR(i,a,n) for(int i=a,i##_len=(int)(n);i<i##_len;++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i=(int)(n);i>=0;--i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)   // c++14
#define SZ(c) ((int)c.size())
#define EXIST(c,x) (c.find(x)!=end(c))
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : "\n"); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
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;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
    }
} before_main_function;
//------------------------------------------------------------------------------

signed main() {
    int H,W;
    cin>>H>>W;
    REP(i,H) {
        REP(j,W) {
            string s;
            cin>>s;
            if(s=="snuke") {
                cout<<(char)('A'+j)<<i+1<<endl;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#define lol(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;

using namespace std;
typedef pair<ll,ll> P;
#define N 2010
ll n;
ll Beta(string A,string B,ll b){
    reverse(A.begin(),A.end());
    reverse(B.begin(),B.end());
    
    ll x[N],y[N],bef;
    lol(i,n)x[i]=y[i]=0;
    
    bef=1e9;
    lol(i,2*n){
	ll I=i%n;
	if(A[I]=='1'&&B[I]=='0'){
	    x[I]=max(x[I],i-bef);
	}
	if(B[I]=='1')bef=i+b;
    }
    
    bef=-1e9;
    for(ll i=2*n;~i;i--){
	ll I=i%n;
	if(A[I]=='1'&&B[I]=='0'){
	    y[I]=max(y[I],bef-i);
	}
	if(B[I]=='1')bef=i;
    }
    
    vector<P> v;
    lol(i,n){
	if(x[i] or y[i])
	v.push_back(make_pair(x[i],y[i]));
    }
    sort(v.begin(),v.end(),greater<P>());
    
    ll res=1e9;
    ll maxi=0;
    lol(i,v.size()){
	res=min(res,v[i].first+maxi);
	maxi=max(maxi,v[i].second);
    }
    res=min(res,maxi);
    
    res=2*res+b;
    lol(i,n)res+=(A[i]!=B[i]);
    return res;
}
string Rshift(string x){
    string res="";
    res.push_back(x[x.size()-1]);
    res+=x.substr(0,x.size()-1);
    return res;
}
ll Alpha(string A,string B){
    ll ans=1e9;
    for(ll b=0;b<=n;b++){
	ans=min(ans,Beta(A,B,b));
	B=Rshift(B);
    }
    return ans;
}
bool Cand(string a,string b){
    bool zeroa=1,zerob=1;
    lol(i,n){
	if(a[i]!='0')zeroa=0;
	if(b[i]!='0')zerob=0;
    }
    if(zerob==1){
	if(zeroa)cout<<0<<endl;
	else cout<<-1<<endl;
	return 1;
    }
    return 0;
}
int main(){
    string a,b;cin>>a>>b;
    n=a.size();
    if(Cand(a,b))return 0;
    ll ans=Alpha(a,b);
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    ans=min(ans,Alpha(a,b));
    cout<<ans<<endl;
    return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> B(N - 1);
  for (int i = 0; i < N -1; i++) {
    cin >> B.at(i);
  }
  int Amax;
  Amax = B.at(0) + B.at(N - 2);
  for (int i = 0; i < N - 2; i++) {
    Amax += min(B.at(i), B.at(i + 1));
  }
  cout << Amax << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int count=0;
  string S,T;
  cin>>S>>T;
  for(int i=0;i<S.size();i++){
  if(S.at(i)!=T.at(i)){
  count++;
  }
  }
cout<<count<<endl;
}

Measured memory_bytes: 3884
#include<stdio.h>
int main (void){
  long a[5],tmp;
  int k,l;
  for(k=1;k<=5;k++){
    scanf("%ld",&a[k-1]);
  }
  for(l=1;l<=120;l++){
    for(k=1;k<=4;k++){
      if(a[k-1]<a[k]){
        tmp = a[k-1];
        a[k-1] = a[k];
        a[k] = tmp;
      }
    }
    if(a[0]>=a[1]&&a[1]>=a[2]&&a[2]>=a[3]&&a[3]>=a[4]) break;
  }
  printf("%ld",a[0]);
  for(k=2;k<=5;k++){
    printf(" %ld",a[k-1]);
  }
  printf("\n");
  return 0;
}

Measured memory_bytes: 600
#include <bits/stdc++.h>
  
using namespace std;
  
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
  
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
  
#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update> 
  
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 
  
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
  
const int inf = int(1e9);
const int mod = inf + 7;
const int N = 1e6 + 555; // TL MB
const double PI = acos(-1.0);
 
void mul(ll &x, ll y){
	x *= y;
	if(x >= mod) x %= mod;
}
 
void add(ll &x, ll y){
	x += y;
	if(x >= mod) x -= mod;
	if(x < 0) x += mod;
}
ll bp(ll a, ll n){
	ll r = 1;
	while(n){
		if(n & 1) mul(r, a);
		mul(a, a);
		n >>= 1;
	}
	return r;
}


void solve()
{
	string s;
	cin >> s;
	int cnt = 0, mx = 0;
	vector<char> v = {'A', 'C', 'G', 'T'};
	rep(i, sz(s)){
		bool ok = 0;
		rep(j, sz(v)){
			if(v[j] == s[i]){
				ok = 1;
			}
		}
		cnt += (ok ? 1 : -cnt);
		mx = max(mx, cnt);
	}
	cout << mx;
}


 
 
int main()
{
    Red;
  	// int T; cin >> T; while(T--)
    solve();
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<set>
#include<cstring>
#include<iomanip> //cout << fixed << setprecision(15) << x << endl;

using namespace std;
typedef long long ll;
const ll INF = 1e9 + 6;
const ll MOD = 1e9 + 7;
const ll LLINF = 1LL<<60;
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
/* -- template -- */

int N;

void dfs(string s, char prev) {
    if(s.size() == N) {
        cout << s << endl;
        return;
    }
    for(char c = 'a'; c <= prev + 1; ++c) {
        dfs(s + c, max(c, prev));
    }
}

int main() {
    cin >> N;
    dfs("a", 'a');
}


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

using namespace std;

int main(void)
{
    int M, H, X, Y, R;

    cin >> M >> H >> X >> Y >> R;

    if(0 <= X-R && X+R <= M && 0<= Y-R && Y+R <= H)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;



   return 0;
}



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

int main(){
    long long int n, k;
    cin >> n >> k;

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

    long long int ans = 0;
    for(int d=39; d>=-1; d--){
        if(d >= 0 && !(k & (1LL<<d))) continue;

        // if (d-th bit of k) == 1
        long long int test = 0;
        for(int i=39; i>=0; i--){
            int count0 = 0, count1 = 0;

            for(int j=0; j<n; j++){
                if(a[j] & (1LL<<i)) count1 += 1;
                else count0 += 1;
            }
            //cout << count0 << " " << count1 << endl;

            if(i > d){
                if(k & (1LL<<i)) test += (1LL<<i) * count0;
                else test += (1LL<<i) * count1;
            }
            if(i == d){
                test += (1LL<<d) * count1;
            }
            if(i < d){
                test += (1LL<<i) * max(count0, count1);
            }
        }
        ans = max(ans, test);
    }
    cout << ans << endl;
    return 0;
}

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

vector<int> to[200005];
vector<int> ans;
void dfs(int v,int p=-1){
    for(int u : to[v]){
        if(u == p) continue;
        ans[u] += ans[v];
        dfs(u,v);
    }
}

int main(){
    int n,q;
    cin >> n >> q;
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        --a; --b;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    ans.resize(n);
    rep(i,q){
        int p,x;
        cin >> p >> x;
        --p;
        ans[p] += x;
    }
    dfs(0);
    rep(i,n){
        cout << ans[i] << " " ;
    }
    cout << endl;
}

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

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

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

using ll = long long;

using namespace std;

struct Camel {
  ll k, x;
  bool operator<(const Camel& c) const { return x < c.x; }
};

void solve() {
  in(int, n);
  ll ans = 0;
  vector<Camel> lc, rc;
  rep(i, n) {
    in(ll, k, l, r);
    if (l >= r) {
      ans += r;
      lc.push_back({k, l - r});
    } else {
      ans += l;
      rc.push_back({k, r - l});
    }
  }
  auto calc = [&ans](const vector<Camel>& cs) {
    multiset<Camel> m;
    for (const auto& c : cs) {
      if (m.size() == c.k) {
        auto it = m.begin();
        if (it->x >= c.x) {
          continue;
        }
        ans -= it->x;
        m.erase(it);
      }
      assert(m.size() < c.k);
      ans += c.x;
      m.insert(c);
    }
  };

  sort(all(lc), [](const Camel& a, const Camel& b) { return a.k < b.k; });
  calc(lc);

  for (auto& c : rc) {
    c.k = n - c.k;
  }
  sort(all(rc), [](const Camel& a, const Camel& b) { return a.k < b.k; });
  calc(rc);

  out(ans);
}

int main() {
  in(int, t);
  while (t--) solve();
}


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

int main() {
  // ここにプログラムを追記
  int a, b , y;
  double x ;
  cin >> a >> b ;
  x = ( a + b ) / 2.0 ;
  y = ( a + b ) / 2  ;
  y = y + (x - y) * 2  ;
cout << y  << endl;
}

Measured memory_bytes: 256
S = input()
YYMM = 0
MMYY = 0
start = S[0:2]
end = S[2:4]
tmp1 = int(start)
tmp2 = int(end)
if(tmp2 <= 12 and tmp2 != 0):
    YYMM = 1
if(tmp1 <= 12 and tmp1 != 0):
    MMYY = 1
if(YYMM == 1 and MMYY == 1):
    print("AMBIGUOUS")
elif(YYMM == 1):
    print("YYMM")
elif(MMYY == 1):
    print("MMYY")
else:
    print("NA")

Measured memory_bytes: 9196
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), i##_max = (b); i < i##_max; ++i)
#define RFOR(i, a, b) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()

#define pb push_back
#define mp make_pair
#define fst first
#define snd second

using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<i64>;
using vd = vector<double>;
using pi = pair<int, int>;
using pl = pair<i64, i64>;
using pd = pair<double, double>;

constexpr int INF = 1 << 30;
constexpr int MOD = 1000000007;

template <typename T>
void print(T &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) cout << " ";
    cout << *it;
  }
  cout << endl;
}

int main() {
  int n, wmax, ans = -1;
  cin >> n >> wmax;
  vi V(n), W(n);
  vector<vi> C(n + 1, vi(wmax + 1, 0));
  REP(i, n) cin >> V[i] >> W[i];
  REP(i, n) {
    C[i + 1] = C[i];
    REP(w, wmax - W[i] + 1) {
      auto new_w = w + W[i];
      C[i + 1][new_w] = max(C[i + 1][new_w], C[i][w] + V[i]);
      ans = max(ans, C[i + 1][new_w]);
    }
  }

  cout << ans << endl;
}


Measured memory_bytes: 6812
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = (int)(j);i <= (int)(k);i ++)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define fix cout<<fixed<<setprecision(20);
const int maxn=(int)1e6+5;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,k,c;cin>>n>>k>>c;
    string s;
    cin>>s;
    int len=s.length();
    vector<int>L,R;
    int cnt=c+1;
    rep(i,0,len-1){
        if(s[i]=='o'&&cnt>c)L.push_back(i),cnt=0,debug(i);
        cnt++;
        if(L.size()==k)break;
    }
    cnt=c+1;
    for(int i=len-1;i>=0;i--){
        if(s[i]=='o'&&cnt>c)R.push_back(i),cnt=0,debug(i);
        cnt++;
        if(R.size()==k)break;
    }
    reverse(R.begin(),R.end());
    int ans=0;
    rep(i,0,L.size()-1)if(R[i]==L[i])cout<<L[i]+1<<endl;
}

Measured memory_bytes: 3468
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    grid = [list(map(int, input().split())) for _ in range(n)]
    flg = [[True] * n for _ in range(n)]

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if grid[i][k] + grid[k][j] < grid[i][j]:
                    print(-1)
                    exit()
                elif grid[i][k] + grid[k][j] == grid[i][j] and i != k and j != k:
                    flg[i][j] = False

    res = 0
    for i in range(n):
        for j in range(n):
            if flg[i][j]:
                res += grid[i][j]
    print(res // 2)


if __name__ == '__main__':
    resolve()


Measured memory_bytes: 75168
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;


const int maxn = 1e6+5;
const int inf = 0x3f3f3f3f;
int n;
struct Edge
{
    int to, next, w;
}edges[maxn];
int head[maxn], dis[maxn], ne = 1;
bool done[maxn];
int ans[maxn];
struct HeapNode
{
    int d, u;
    HeapNode(int d, int u) : d(d), u(u){}
    bool operator < (const HeapNode &rhs) const {
          return d > rhs.d;
    }
};
void add(int u, int v, int w)
{
    edges[ne] = {v, head[u], w}; head[u] = ne++;
}
void init(int n)
{
    for(int i = 1; i <= n; i++) {
        dis[i] = inf;
        done[i] = false;
    }
}
void dijkstra(int s) { //s:起始点
		priority_queue<HeapNode> Q;
		init(n);
		dis[s] = 0;
		Q.push(HeapNode(0, s));
		while (Q.size()) {
			HeapNode x = Q.top();
			Q.pop();
			int u = x.u;
			if (done[u]) continue;
			done[u] = true;
			for (int i = head[u]; i; i = edges[i].next) {
				int id = edges[i].to;
				if (dis[id] > dis[u] + edges[i].w&&!done[id]) {
					dis[id] = dis[u] + edges[i].w;
					Q.push(HeapNode(dis[id], id));
				}
			}
		}
}
int main(){
    int x, y; cin >> n >> x >> y;
    for(int i = 1; i < n; i++) {
        add(i, i+1, 1);
        add(i+1, i, 1);
    }
    add(x, y, 1);
    add(y, x, 1);
    for(int i = 1; i < n; i++) {
        dijkstra(i);
        for(int j = i+1; j <= n; j++) {
            ans[dis[j]]++;
        }
    }
    for(int i = 1; i < n; i++) {
        cout << ans[i] << endl;
    }
}


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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == count + 1) {
      count++;
    }
  }
  cout << (count == 0 ? -1 : n - count) << endl;
}


Measured memory_bytes: 1024
import numpy as np
N,M,Q=map(int, input().split())
a=np.array([[0]*(N+1) for i in range(N+1)])
for i in range(M):
  l,r=map(int, input().split())
  a[l][r]+=1
a=a.cumsum(axis=0).cumsum(axis=1)
for i in range(Q):
  p,q=map(int, input().split())
  print(a[q][q]-a[p-1][q]-a[q][p-1]+a[p-1][p-1])

Measured memory_bytes: 33268
#include <iostream>
#include <map>
#include <functional>
#include <cstring>
using namespace std;

void alpha_loop(function<void(int)> f)
{
    for (int a = 'a'; a <= 'z'; a++)
    {
        f(a);
    }
}

int main(void)
{
    map<int, int> m;
    alpha_loop([&](char c) { m[c] = 0; });
    while (1)
    {
        char c;
        cin >> c;
        if (!cin) break;
        if (isalpha(c))
        {
            m[tolower(c)]++;
        }
    }
    alpha_loop([&](char c) { cout << c << " : " << m[c] << endl; });


    return 0;
}

Measured memory_bytes: 3128
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;

int main(){
    int n, k;
    cin >> n >> k;
    int cnt=0;
    rep(i, n){
        int h;
        cin >> h;
        if(h>=k){
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 3636
fun main(args: Array<String>) {
    val N = nextInt()
    val A = listOfInt()
    val C = IntArray(N + 1)
    var total = 0L
    A.forEach { a ->
        total += C[a]
        C[a]++
    }
    A.forEach { a ->
        println(total - (C[a] - 1)) // - nC2 + (n-1)C2 = -(n-1)
    }
}

fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun nextInt(offset: Int = 0) = nextInt() + offset
fun nextLong() = java.lang.Long.parseLong(next())
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { Integer.parseInt(it) }

Measured memory_bytes: 97912
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

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

__attribute__ ((constructor))
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> dis(n, vector<int>(n, inf));
    vector<int> to[n];
    map<pair<int, int>, int> mii;
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        mii[{a, b}] = c;
        mii[{b, a}] = c;
        dis[b][a] = c;
        dis[a][b] = c;
    }
    rep(k,n){
        rep(i,n){
            rep(j,n){
                chmin(dis[i][j],dis[i][k]+dis[k][j]);
            }
        }
    }
    int cnt = 0;
    for(auto u : mii){
        // cout << u.first.first<<" "<<u.first.second<<" "<<u.second<<" "<< dis[u.first.first][u.first.second] <<endl;
        if(dis[u.first.first][u.first.second] < u.second)
            cnt++;
    }
    cout << cnt/2 << endl;
    return 0;
}

Measured memory_bytes: 3776
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

int main() {
	IOS;
	int n;
	cin >> n;
	vector<int> a(n);
	REP(i, n) {
		cin >> a[i];
	}
	REP(i, n) {
		if (a[i] % 2 == 0) {
			if (a[i] % 3>0 && a[i] % 5>0) {
				put("DENIED");
				return 0;
			}
		}
	}
	put("APPROVED");
	return 0;
}

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

int main()
{
   
    set<string> s;
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        string x;
        cin>>x;
        s.insert(x);
    }
    cout<<s.size();
    return 0;
}




Measured memory_bytes: 19168
#include <bits/stdc++.h>
#define ll long long
#define WEIGHTSUM weight + i
#define VALUESUM maxValue[i] + value
using namespace std;
bitset <100005> foundWeight;
ll maxValue[100005];
 
int main() {
	ll n, w, weight, value, result = 0;
 
	cin >> n >> w;
	for (int i = 1; i <= n; i++) {
		cin >> weight >> value;
 
		for (int i = w; i >= 1; i--)
			if (foundWeight[i] && WEIGHTSUM <= w) {
				maxValue[WEIGHTSUM] = max(maxValue[WEIGHTSUM], VALUESUM);
				foundWeight[WEIGHTSUM] = 1;
				result = max(result, VALUESUM);
			}
 
		maxValue[weight] = max(maxValue[weight], value);
		foundWeight[weight] = 1;
		result = max(result, value);
	}
 
    cout << result;
 
	return 0;
}

Measured memory_bytes: 4280
data = [0 for _ in range(100)]

while True:
  try:
    input_data = int(input())
  except EOFError:
    break
  data[input_data-1] += 1

max_piv = max(data)
for i in range(100):
  if data[i] == max_piv:
    print(i+1)


Measured memory_bytes: 5604
def solve(n, h):
    for i in range(n-1):
        if h[i] < h[i+1]:
            h[i+1] -= 1
        elif h[i] == h[i+1]:
            pass
        else:
            return "No"
    return "Yes"

n = int(input())
h = list(map(int, input().split()))
print(solve(n, h))

Measured memory_bytes: 14396
// AOJ
// ITTP1_7_B How many ways?

#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <functional>
#include <random>
#include <vector>
#include <chrono>

using namespace std;
//using namespace std::chrono;


int main()
{
	//steady_clock::time_point start = steady_clock::now();
	int n_temp,x_temp;
	//string parts[4] = {"dream", "dreamer", "erase", "eraser"};
	//vector<string> StringParts(parts, parts + sizeof(parts)/sizeof(parts[0]));
	vector<int> n, x;

	int id = -1;
	do {
		cin >> n_temp >> x_temp;
		n.push_back(n_temp);
		x.push_back(x_temp);
		id++;
	} while (n[id] != 0 || x[id] != 0);

	// 制約確認
	/*
	for (vector<int>::size_type i=0; i<N-1; i++) {
		if (n[i] < 3 || 1000 < n[i]) {
			cout << "n[" << i << "]は条件を満たしていません!\n";
				return 0;
		} 

		if (x[i] < 0 || 300 < x[i]) {
			cout << "x[" << i << "]は条件を満たしていません!\n";
			return 0;
		}
	}
	*/

	// 計算
	int i,j,k,N,X;
	int cnt = 0;
	for (int ite=0; ite<n.size()-1; ite++) {
		N = n[ite];
		X = x[ite];
		//cout << "N,X=" << N << "," << X << endl;
		for (i=1; i<=N-2; i++) {
			for (j=i+1; j<=N-1; j++) {
				for (k=j+1; k<=N; k++) {
					//cout << "(i,j,k)=" << i << "," << j << "," << k << endl;
					if (i+j+k == X) cnt++;
				}
			}
		}
		cout << cnt << endl;
		cnt = 0;
	}

	//計算時間集計
	//steady_clock::time_point end = steady_clock::now();
    //steady_clock::duration d = end - start;
	//cout << "計算時間は" << duration_cast<milliseconds>(d).count() << "ミリ秒です。\n";
}



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

int main()
{
	string word;
	
	while(cin>>word)
	{
		int count,county;
		for(int i=0;(i=word.find("JOI",i))!=word.npos; i++)
		{
		    count++;
	        
		}
		cout<<count<<endl;
		count=0;
		for(int i=0;(i=word.find("IOI",i))!=word.npos; i++)
		{
			county++;
			
		}
		cout<<county<<endl;
		county=0;
	}
	return 0;
}

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

int main()
{
	const int max_r = 100, max_c = 100;
	int a[max_r][max_c] = { 0 };
	int r, c;
	int SUM_ = 0;
	//input
	cin >> r >> c;
	for (int y = 0; y < r; y++) {
		for (int x = 0; x < c; x++) {
			cin >> a[y][x];
			SUM_ += a[y][x];
		}
	}

	//output
	for (int i = 0; i < r; i++) {
		int sum_r = 0;
		for (int j = 0; j < c; j++) {
			cout << a[i][j] << " ";
			sum_r += a[i][j];
		}
		cout << sum_r << endl;
	}
	for (int i = 0; i < c; i++) {
		int sum_c = 0;
		for (int j = 0; j < r; j++) {
			sum_c += a[j][i];
		}
		cout << sum_c << " ";
	}
	cout << SUM_ << endl;
    return 0;
}

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

const int mod = 1e9 + 7;

ll int power(int x, int y) {
    ll int res = 1;
    for(int i = 1; i <= y; i++) {
        res = (res * x) % mod;
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    ll int s = (power(10, n) - 2 * power(9, n) + power(8, n)) % mod; 
    cout << (s + mod) % mod;
}

Measured memory_bytes: 3552
import java.util.Scanner;

public class Main {
    //Double Factorial
    //这是第一次做 E 吧废物, 这次 D 简单人均 20 分钟 6 道呵呵
    //先看看能不能看懂题目吧
    //又是整型溢出的情况, 如何计算可以算出多少个 0 呢
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long v = sc.nextLong();
        System.out.println(helper(v));
        sc.close();
    }

    static long helper(long v) {
        if (v % 2 == 1) return 0;
        long count = 0;
        for (long i = 10; i <= v; i *= 5) {
            count += v / i;
        }
        return count;
    }
}


Measured memory_bytes: 23252
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>  
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000000000000000000000001
#define LONG_INF 10000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD  998244353
#define REP(i,n) for(long long i = 0;i < n;++i)
int main(){
	while (true) {
		long long n;
		cin >> n;
		if (n == 0) return 0;
		vector<pair<int, int>> gogo;
		int m;
		cin >> m;
		int base_x = 0;
		int base_y = 0;
		cin >> base_x >> base_y;
		REP(i, m-1) {
			int a, b;
			cin >> a >> b;
			if (a != base_x) {
				gogo.push_back(make_pair(a - base_x, 0));
			}
			else {
				gogo.push_back(make_pair(b - base_y, 1));
			}
			base_x = a;
			base_y = b;
		}
		if (gogo[0].second == 1) {
			//moving y
			//spin it!
			for (int q = 0; q < gogo.size(); ++q) {
				if (gogo[q].second == 0) {
					gogo[q].second = 1;
				}
				else {
					gogo[q].second = 0;
					gogo[q].first *= -1;
				}
			}
		}
		if (gogo[0].first < 0) {
			for (int q = 0; q < gogo.size(); ++q) {
				gogo[q].first *= -1;
			}
		}
		REP(tea, n) {
			vector<pair<int,int>> compared;
			int m;
			cin >> m;
			int base_x, base_y;
			cin >> base_x >> base_y;
			REP(i, m-1) {
				int a, b;
				cin >> a >> b;
				if (a != base_x) {
					compared.push_back(make_pair(a - base_x, 0));
				}
				else {
					compared.push_back(make_pair(b - base_y, 1));
				}
				base_x = a;
				base_y = b;
			}
			if (compared[0].second == 1) {
				//moving y
				//spin it!
				for (int q = 0; q < compared.size(); ++q) {
					if (compared[q].second == 0) {
						compared[q].second = 1;
					}
					else {
						compared[q].second = 0;
						compared[q].first *= -1;
					}
				}
			}
			if (compared[0].first < 0) {
				for (int q = 0; q < compared.size(); ++q) {
					compared[q].first *= -1;
				}
			}
			if (compared == gogo) {
				cout << tea + 1 << endl;
				continue;
			}
			REP(q, compared.size()) {
				compared[q].first *= -1;
			}
			reverse(compared.begin(), compared.end());
			if (compared[0].second == 1) {
				//moving y
				//spin it!
				for (int q = 0; q < compared.size(); ++q) {
					if (compared[q].second == 0) {
						compared[q].second = 1;
					}
					else {
						compared[q].second = 0;
						compared[q].first *= -1;
					}
				}
			}
			if (compared[0].first < 0) {
				for (int q = 0; q < compared.size(); ++q) {
					compared[q].first *= -1;
				}
			}
			if (compared == gogo) {
				cout << tea + 1 << endl;
				continue;
			}
		}
		cout << "+++++" << endl;
	}
}


Measured memory_bytes: 3068
def main():
    n = int(input())
    m1 = input().split()
    ps = [int(m1[0]), int(m1[1])]
    for _ in range(n - 1):
        ps.append(int(input().split()[1]))
    ms = [[float("inf") for _ in range(n)] for __ in range(n)]
    # 範囲 0 - n
    for i in range(n):
        ms[i][i] = 0

    for length in range(1, n):
        # 00 11 22 33 44 ...         nn
        # 01 12 23 34 45 ...      n-1,n
        # 02 13 24 35 46 ... n-2,n
        # ...
        # 0n
        for start in range(0, n - length):
            end = start + length
            # 仕切りpartition
            m_start_end = float("inf")
            for partition in range(start, end):
                m_start_end = min(m_start_end, ms[start][partition] + ms[partition + 1]
                                  [end] + ps[start] * ps[partition + 1] * ps[end + 1])
            ms[start][end] = m_start_end
    print(ms[0][n - 1])


if __name__ == "__main__":
    main()



Measured memory_bytes: 5932
/* bit DP, O(n*2^(2n)) */

#include<cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int sw[1024];
	rep(S,1024){
		sw[S]=0;
		if(S&1) sw[S]^=3;
		for(int i=1;i<9;i++) if(S&(1<<i)) sw[S]^=(7<<(i-1));
		if(S&512) sw[S]^=768;
	}

	int T; scanf("%d",&T);
	while(T--){
		int cell[10]={};
		rep(i,10){
			cell[i]=0;
			rep(j,10){
				int tmp; scanf("%d",&tmp);
				cell[i]|=(tmp<<(9-j));
			}
		}

		static int dp[11][1024];
		// dp[r][stat] : rsÚÌXCb`ðµûstat(bit\»)Åµ½Æ«ÉAr-1sÚª·×Ä0ÉÈéæ¤Èr-1sÚÌXCb`Ìµû
		rep(S,1024) dp[0][S]=0;
		rep(r,10){
			rep(S,1024){
				dp[r+1][S]=-1;
				rep(B,1024){
					if(dp[r][B]==-1) continue;
					if((cell[r]^dp[r][B]^sw[B]^S)==0) dp[r+1][S]=B;
				}
			}
		}

		int ans[10];
		for(int i=0,S=dp[10-i][0];i<10;i++,S=dp[10-i][S]) ans[i]=S;
		for(int i=9;i>=0;i--){
			rep(k,10) printf("%d%c",!!(ans[i]&(1<<(9-k))),k<9?' ':'\n');
		}
	}

	return 0;
}

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = x ; i < n ; ++i)
#define rep(i, n) for(int i = 0 ; i < n ; ++i)
#define repr(i, n) for(int i = n - 1 ; i >= 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())

const int IINF = 1e9 + 10;
const long long LLINF = (long long)1e18 + 10;
const long long MOD = (long long)1e9 + 7;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

lint calc(lint l, lint r, lint m, lint val){
    lint res = (r - l) * (r - l - 1LL) / 2LL;
    res -= (m - l) * (m - l - 1LL) / 2LL;
    res -= (r - m) * (r - m - 1LL) / 2LL;
    res *= val;
    return res;
}

int main(){
    int n;
    cin >> n;
    vector<int> rev(n + 1);
    rep(i, n){
        int a;
        cin >> a;
        rev[a] = i + 1;
    }
    lint ans = 0;
    set<int> st;
    st.insert(0);
    st.insert(n + 1);
    REP(i, 1, n + 1){
        auto it1 = st.upper_bound(rev[i]);
        auto it2 = it1--;
        ans += calc(*it1, *it2, rev[i], i);
        st.insert(rev[i]);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 10368
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
typedef vector<int> vint;
#define pb push_back
#define N 50

int a[N][N];
//dfr = distance from root,node = group of node ,
void solve(vint dfr,vint node,vint &ans){
  if (node.size() == 1 && dfr[node[0]] == 0)return;
  int n = node.size();
  int gid[n];
  rep(i,n)gid[i] = node[i];

  int cnt = 0;
  rep(i,n){
    if ( gid[i] != node[i])continue;
    vint nextgroup;
    nextgroup.pb(node[i]);
    REP(j,i+1,n){
      if ( gid[j] == node[j] && dfr[node[i]] + dfr[node[j]] > a[node[i]][node[j]]){
	nextgroup.pb(node[j]);
	gid[j] = node[i];
	dfr[node[j]]--;
      }
    }
    dfr[node[i]]--;
    solve(dfr,nextgroup,ans);
    cnt++;
  }
  ans.pb(cnt+1);
}


main(){
  int n;
  while(cin>>n && n){
    vint node,dfr,ans;
    dfr.push_back(-1);//dummy
    rep(i,n)rep(j,n)cin>>a[i][j];
    REP(i,1,n)node.pb(i),dfr.pb(a[0][i]-1);
    solve(dfr,node,ans);
    sort(ans.begin(),ans.end());
    rep(i,ans.size()){
      if (i)cout << " ";
      cout << ans[i];
    }
    cout << endl;
  }
}

Measured memory_bytes: 0
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int x;
    int y;
} point_t;
point_t a[300*300];
long b[300*300];

inline long dist(const point_t a, const point_t b){
    return labs(a.x-b.x)+labs(a.y-b.y);
}

int main(void){
    long h, w, d;
    scanf("%ld %ld %ld\n", &h, &w, &d);
    long t;
    for(long i=0; i<h; i++){
        for(long j=0; j<w; j++){
            scanf("%ld", &t);
            a[t-1].x = j;
            a[t-1].y = i;
        }
    }
    const long n = h*w-d;
    for(long i=0; i<n; i++){
        b[i+d] = b[i] + dist(a[i], a[i+d]);
    }
    long q;
    scanf("%ld", &q);
    long l, r;
    for(long i=0; i<q; i++){
        scanf("%ld %ld\n", &l, &r);
        printf("%ld\n", b[r-1]-b[l-1]);
    }
    return 0;
}


Measured memory_bytes: 2432
n=int(input())
b=[0 for i in range(100002)]
a=list(map(int,input().split()))
a.sort()
for i in range(n):
    b[a[i]]+=1
ans=0
for i in range(1,100001):
    tmp=b[i-1]+b[i]+b[i+1]
    if tmp>ans:
        ans=tmp
print(ans)

Measured memory_bytes: 55664
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<long long, long long>
#define ff first
#define ss second

main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int N; cin >> N; int a[3][N];
    for(int i = 0; i < N; i++)
        cin >> a[0][i] >> a[1][i] >> a[2][i];
    int DP[3][N];
    DP[0][0] = a[0][0];
    DP[1][0] = a[1][0];
    DP[2][0] = a[2][0];
    for(int i = 1; i < N; i++){
        DP[0][i] = max(DP[1][i-1], DP[2][i-1]) + a[0][i];
        DP[1][i] = max(DP[0][i-1], DP[2][i-1]) + a[1][i];
        DP[2][i] = max(DP[0][i-1], DP[1][i-1]) + a[2][i];
    }
    cout << max({DP[0][N-1], DP[1][N-1], DP[2][N-1]});
}

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

int main() {
  int a,b,c;
  scanf("%d%d%d", &a,&b,&c);
  
  int m = max({a,b,c});

  int sus = 3*m-a-b-c;

  if(sus%2==0) cout << sus/2 << endl;
  else cout << (sus+3)/2 << endl;

}



Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <set>
#define ll long long
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    long long a,s;
    cin>>a;
    s = a+a*a+a*a*a;
    cout<<s<<endl;

    return 0;
}

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

using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')
    cerr<<*sdbg++;
  cerr<<"="<<h<<","; 
  _dbg(sdbg+1, a...);
}

template<class T> ostream & operator<<(ostream & os, vector<T> V){
  os<<"[";
  for(auto vv: V) os << vv <<",";
  return os << "]";
}
template<class L, class R> ostream & operator <<(ostream & os, pair<L,R> P){
  return os <<"("<<P.first <<","<<P.second <<")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif
const int N = 305, M = 998244353;
int n, k;
char word[N];
int dp[N][N][N];

void add(int &w, int u) {
    w += u;
    if (w >= M) w -= M;
}

int main() {
    
    scanf("%s", &word[1]);
    scanf("%d", &k);
    
    n = strlen(word + 1);
    int cur = 0;
    int totalOnes = 0;
    vector<int> s;
    s.push_back(0);
    for (int i = 1; i <= n; i++) {
        word[i] -= '0';
        if (word[i] == 0) {
            s.push_back(cur);
            cur = 0;
        } else {
            totalOnes++;
            cur++;
        }
    }
    s.push_back(cur);
    vector<int> prefSum;
    for (int i = 0; i < s.size(); i++) {
        if (i == 0)
        prefSum.push_back(s[i]);
        else
        prefSum.push_back(prefSum.back() + s[i]);
    }
    
    k = min(k, totalOnes);
    int m = (int)s.size() - 1;
    
    dp[0][0][0] = 1;
    for (int i = 1; i <= m; i++) {
        for (int l = 0; l <= k; l++) {
            for (int j = prefSum[i]; j <= totalOnes; j++) {
                for (int p = j; p >= prefSum[i - 1]; p--) {
                    int myVal = j - p;
                    if (myVal <= s[i]) {
                        add(dp[i][l][j], dp[i - 1][l][p]);
                    } else {
                        if (l - myVal + s[i] < 0) {
                            break;
                        }
                        add(dp[i][l][j], dp[i - 1][l - myVal + s[i]][p]);
                    }
                }
            }
        }
    }
    
    int ans = 0;
    for (int i = 0; i <= k; i++) {
        add(ans, dp[m][i][prefSum.back()]);
    }
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 31076
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define ep emplace_back
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}





int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s,t;cin >> s >> t;
	int n = s.size(), m = t.size();
	vector<pii> cnts(n + 1), cntt(m + 1);
	rep(i,n){
		if(s.at(i) == 'A') cnts.at(i + 1).first++;
		else cnts.at(i + 1).second++;
		cnts.at(i + 1).first += cnts.at(i).first;
		cnts.at(i + 1).second += cnts.at(i).second;
	}
	rep(i,m){
		if(t.at(i) == 'A') cntt.at(i + 1).first++;
		else cntt.at(i + 1).second++;
		cntt.at(i + 1).first += cntt.at(i).first;
		cntt.at(i + 1).second += cntt.at(i).second;
	}
	int q;cin >> q;
	while(q--){
		int a,b,c,d;cin >> a >> b >> c >> d;
		int k1 = cnts.at(b).first - cnts.at(a - 1).first, k2 = cnts.at(b).second - cnts.at(a - 1).second;
		int k3 = cntt.at(d).first - cntt.at(c - 1).first, k4 = cntt.at(d).second - cntt.at(c - 1).second;
		if((k1 % 3 - k2 % 3 + 3) % 3 == (k3 % 3 - k4 % 3 + 3) % 3) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}


Measured memory_bytes: 2512
n = int(input())
p = []
a,b = [int(x) for x in input().split()]
p.append(a)
p.append(b)
for i in range(1, n):
    c,d = [int(x) for x in input().split()]
    p.append(d)
# end input
# create dp table
dp = []
for x in range(n+1):
    dp.append([0] * (n+1))

for l in range(2, n+1):
    for i in range(1, n-l+2):
        j = i + l -1
        dp[i][j] = 9999999999

        for k in range(i, j):
            tmp = dp[i][k] + dp[k+1][j] + (p[i-1] * p[k] * p[j])
            if dp[i][j] > tmp:
                dp[i][j] = tmp
#print(dp)
print(dp[1][n])

Measured memory_bytes: 7820