akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
using namespace std;
int main(){
    int i=0,ans=0;
    string a;
    cin >> a;
    while(a[i]!='\0'){
        if(a[i]=='+'){
            ans++;
        }else{
            ans--;
        }
        i++;
    }
    cout << ans << endl;
    return 0;
}

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

long long mod=1000000007,sz,dp[100009][2];
string str;

long long solve(long long idx,long long sta)
{

    if(idx==sz)return 1;
    else if(dp[idx][sta]!=-1)return dp[idx][sta];
    else
    {

        long long p=0,q=0,r=0,fr,sc;

        if(sta==1 || str[idx]=='1')q=1;

        for(long long lp=0;lp<=q;lp++)
        {
            for(long long tp=0;tp<=q;tp++)
            {
                if(lp!=1 || tp!=1)
                {

                    fr=sta; if(lp==0 && tp==0 && str[idx]=='1')fr=1;

                    p=(p+solve(idx+1,fr))%mod;

                }

            }
        }



        dp[idx][sta]=p;  return dp[idx][sta];


    }


}

int main()
{


        long long n,m,a,b,c,d,e,f,g,h,p,q,r,fr,sc,tr,tz,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX;
        long long x=0,y=0,cnt=0,res=0,ttl=0,ans=0,sum=0; long long flg=0,flag=1,na=0,as=1;
        vector<long long>u,v,w; vector< pair<long long,long long> >vct; vector<string>vst;
        set<long long>st,nt,tt; map<long long,long long>mp,nq,qr; string ttr,ntr;

        cin>>str;

        sz=str.size();

        memset(dp,-1,sizeof dp);
        ans=solve(0,0);

        printf("%lld\n",ans);





    return 0;
}




Measured memory_bytes: 13228
import numpy as np
n=int(input())
a=[]
b=[]
time=0
for i in range(n):
    x,y=map(int,input().split())
    a.append(x)
    b.append(y)
bsort=np.sort(b)
bsoeji=np.argsort(b)
for i in range(n):
    time=a[bsoeji[i]]+time
    if time>bsort[i]:
        print("No")
        exit()
print("Yes")

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

typedef long long ll;

const int N = 1e5 + 10;
ll h[N], dp[N];

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> h[i];
    for (int i = n - 2;  i >= 0; i--) {
        dp[i] = dp[i + 1] + abs(h[i] - h[i + 1]);
        if (i < n - 2)
            dp[i] = min(dp[i], dp[i + 2] + abs(h[i] - h[i + 2]));
    }
    cout <<  dp[0] << "\n";
    return 0;
}

Measured memory_bytes: 1792
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  s=S()
  T=S()
  ans=inf

  for i in range(len(s)-len(T)+1):
    _ans=0
    for j in range(len(T)):
      if s[i+j]!=T[j]:
        _ans+=1

    ans=min(ans,_ans)

  return ans

# main()
print(main())


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

int main() {
  int H, W;
  cin >> H >> W;

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;

  for (int i = 0; i < H; i++) {
    cout << '#';
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      cout << c;
    }
    cout << '#' << endl;
  }

  for (int i = 0; i < W + 2; i++) {
    cout << '#';
  }
  cout << endl;

  return 0;
}


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

int main(){
    int n,m,x,a,c0=0,cn=0;
    cin>>n>>m>>x;
    vector<bool> v(n,0);
    for(int i=0;i<m;i++){
        cin>>a;
        v[a]=1;
    }
    for(int i=1;i<n;i++){
        if(i<x) c0+=v[i];
        else if(i>x) cn+=v[i];
    }
    cout<<min(c0,cn)<<endl;
    return 0;
}

Measured memory_bytes: 256
#ifdef __APPLE__
#include <cassert>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <sstream>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cctype>
#include <list>
#include <cmath>
#include <limits>
#include <bitset>
#include <numeric>
#include <climits>
#include <complex>
#else
#include<bits/stdc++.h>
#endif // __APPLE__
using namespace std;
typedef pair<int,int>   II;
typedef vector<II>     VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int LL;
typedef  unsigned long long int ULL;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define SET(a,b) memset(a,b,sizeof(a))
#define LET(x,a) __typeof(a) x(a)
#define ALL(c) (c).begin(),(c).end()
#define si(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define sll(n) scanf("%lld",&n)
#define lldout(n) printf("%lld\n",n)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define REP(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define ROUNDOFFINT(d) d = (int)((double)d + 0.5)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1)	{
cerr << name << " : " << arg1 << std::endl;}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
     const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);}
#else
#define trace(...)
#endif
//A



int func1(int n, vector<int> &v)
{
    int i=0;
    
    int ans=0;
    for(;i<n;i++)
    {
        //trace(i);
        if (v[i]==v[i+1])
        {
            while(i+1<n && v[i]==v[i+1])
                i++;
            if(i==(n-1))
                ans++;
            else
                i--;
            continue;
        }
        if(v[i]<=v[i+1])
        {
            while(i+1<n && v[i]<=v[i+1])
                i++;
        }
        else
        {
            while(i+1<n && v[i]>=v[i+1])
                i++;
        }
        //trace(i,ans);
        ans++;
    }
    return ans;
}



int main()
{
	//fr;fw;
	fast_io;
    
    int n;
    cin>>n;
    VI v(n);
    REP(i,0,n)
    {
        cin>>v[i];
    }
    
    int ans1=func1(n,v);
    cout<<ans1<<endl;


	return 0;
}





Measured memory_bytes: 640
#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int (i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define yn {puts("Yes");}else {puts("No");}
#define printd(x) printf("%.12f\n", x);
template<class T> bool chmin(T& a, const T& b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
using ll = long long;
using P = pair<int, int>;


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

	int cntl = 0, cntr = 0;
	rep (i, n) {
		if (s[i] == ')') {
			if (cntr == 0) cntl++;
			else cntr--;
		}
		else {
			cntr++;
		}

	}
	
	string tmpc = "", tmpd = "";
	rep (i, cntl) tmpc.pb('(');
	rep (i, cntr) tmpd.pb(')');
	s = tmpc + s;
	s += tmpd;

	cout << s << endl;
	return 0;
}

Measured memory_bytes: 256
N = int(input())
l = [list(map(int, input().split())) for i in range(N)]
t, x, y = 0, 0, 0
 
for i in range(N):
  if l[i][0]-t >= abs(l[i][1]-x)+abs(l[i][2]-y) and (l[i][0]-t-abs(l[i][1]-x)+abs(l[i][2]-y)) % 2 == 0:
      t, x, y = l[i][0], l[i][1], l[i][2]
  else:
    print("No")
    break
else:
  print("Yes")

Measured memory_bytes: 27156
#include <stdio.h>  
#include <algorithm>  
#include <assert.h>
#include <bitset>
#include <cmath>  
#include <complex>  
#include <deque>  
#include <functional>  
#include <iostream>  
#include <limits.h>  
#include <map>  
#include <math.h>  
#include <queue>  
#include <deque>
#include <set>  
#include <stdlib.h>  
#include <string.h>  
#include <string>  
#include <time.h>  
#include <unordered_map>  
#include <unordered_set>  
#include <vector>  
#include <chrono>
#include <random>
#include <time.h>
#include <fstream>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define endl "\n"
#define all(c) begin(c),end(c)
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}
void print(vec v){for(auto e:v)cout<<e<<" ";cout<<endl;}
void print(vecll v){for(auto e:v)cout<<e<<" ";cout<<endl;}
void print(map<int,int> mp){for(auto e:mp)cout<<e.first<<" "<<e.second<<endl;cout<<endl;}


#define INF 1e9+7
#define LLINF 1e18+7
const ll MOD=1e9+7;
const int N=500000;

bool ask(vec a){
    cout<<"? ";
    for(auto e:a)cout<<e<<" ";
    cout<<endl;
    cout.flush();
    string res; cin>>res;
    return (res=="Red");
}
main(){
    int n=in();
    vec a;
    rep2(i,1,n)a.pb(i);
    int defo=ask(a);
    // if(n==1){
    //     cout<<"! ";
    //     cout<<(defo?"R":"B");
    //     cout<<endl;
    //     cout.flush();
    //     return 0;
    // }
    vec ans(n*2+1);
    vec st;
    int l,r;
    l=1,r=n+1;
    while(l<r-1){
        int mid=(l+r)/2;
        vec v;
        rep2(i,mid,mid+n-1)v.pb(i);
        bool now=ask(v);
        if(now!=defo)r=mid;
        else l=mid;
    }
    l=r;
    r=l+n-2;
    rep2(i,l,r)st.pb(i);
    rep2(i,1,n*2){
        if(l<=i&&i<=r)continue;
        vec now=st;
        now.pb(i);
        ans[i]=ask(now);
    }
    bool red=0,blue=0;
    vec w;
    rep2(i,1,n*2){
        if(l<=i&&i<=r)continue;
        if(ans[i]==1){
            if(red) w.pb(i);
            red=1;
        }
        else{
            if(blue)w.pb(i);
            blue=1;
        }
    }
    rep2(i,l,r){
        vec ww=w;
        ww.pb(i);
        ans[i]=ask(ww);
    }
    cout<<"! ";
    rep2(i,1,n*2){
        cout<<(ans[i]?"R":"B");
    }
    cout<<endl;
    cout.flush();
    return 0;
}


Measured memory_bytes: 724
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N,K;
  cin >> N >> K;
  vector<int> ls(N);
  for(int i = 0; i < N; i++){
    cin >> ls.at(i);
  }
  
  sort(ls.begin(), ls.end());
  
  int ans = 0;
  for(int i = 0;  i < K;i++){
    ans += ls.at(N-1-i);
  }
  cout << ans << endl;
  
  return 0;
}

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

using namespace std;
typedef long long ll;
const int 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 main() {
    ll N, K; cin >> N >> K;
    ll ans = 0;
    for(ll i = K; i <= N + 1; ++i) {
        ll maxv = N * (N + 1) / 2 - (N - i + 1) * (N - i) / 2;
        ll minv = i * (i - 1) / 2;
        ans = (ans + maxv - minv + 1 + MOD) % MOD;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3580
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
#define pb push_back
#define mp make_pair
const int mod=1e9+7;

void solve()
{
  int n,m,x;
  cin>>n>>m;
  vector<int> V;
  ll sum=0;
  for(int i=0;i<n;i++)
  {
    cin>>x;
    sum+=x;
    V.pb(x);
  }
  int cnt=0;
  int q=4*m;
  for(int i=0;i<n;i++)
  {
    if(V[i]*q>=sum)
    cnt++;
  }
  if(cnt>=m)
  cout<<"Yes";
  else
  cout<<"No";

}

int main()
{
  fastio
  solve();
}


Measured memory_bytes: 256
using System;
using System.IO;
using System.Linq;

class Solve {
    static void Main(string[] args) {
        var arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int N = arr[0], M = arr[1];
        var brk = new int[M];
        for (int i = 0; i < M; i++) {
            brk[i] = int.Parse(Console.ReadLine());
        }
        var dp = new long[N + 1];
        dp[0] = 1;
        var j = 0;
        if (brk.Any() && brk[j] == 1) {
            j++;
        }
        else dp[1] = 1;
        for (int i = 2; i <= N; i++) {
            if (j < M && i == brk[j]) {
                j++;
                continue;
            }
            dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007;
        }
        var answer = dp[^1];
        var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(writer);
        Console.WriteLine(answer);
        Console.Out.Flush();
    }
}

Measured memory_bytes: 31100
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define AC(x,t) accumulate(ALL(x),t)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int main()
{   int n;
    cin >> n;
    cout << "ABC" << n << endl;
    return 0;
}

Measured memory_bytes: 256
def bubbleSort(A,N):
    flag = 1
    count = 0
    while flag:
        flag = 0
        for j in range(N-1,0,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                flag = 1
                count += 1
    return A,count

N = int(input())
A = list(map(int, input().split()))
A,count = bubbleSort(A,N)
print(" ".join(map(str,A)))
print("%d"%(count))



Measured memory_bytes: 5600
from decimal import Decimal, ROUND_HALF_UP
N = int(input())
Xlist = list(map(int, input().split()))
sumX = 0
for x in Xlist:
  sumX += x
average = Decimal(sumX/N).quantize(Decimal('1'),ROUND_HALF_UP)
answer = 0
for x in Xlist:
  answer += (x-average)**2
print(answer)

Measured memory_bytes: 77396
#include<cstdio>
int d[500050],f[500050],p[500050],n,q,i=1;main(){for(scanf("%d%d",&n,p);i<=n;++i)scanf("%d",d+i),p[i]=(p[i-1]>d[i]?p[i-1]-d[i]:(d[i]<p[i-1]*2?d[i]-p[i-1]:p[i-1]));for(f[n+1]=1;n;--n)f[n]=(d[n]>=f[n+1]*2?f[n+1]:d[n]+f[n+1]);for(scanf("%d",&q);q--;){scanf("%d",&i);puts(p[i-1]>=f[i+1]?"YES":"NO");}}

Measured memory_bytes: 7680
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SORT(i) sort((i).begin(), (i).end())
#define INF 2000000000
constexpr ll mod = 1e9 + 7;
 
int main(){
    int N;
    cin >> N;
    int ans = 0;
    for (int i = 1; i <= N;i+=2){
        int cnt = 0;
        for (int j = 1; j <= N;++j){
            if(i % j == 0){
                ++cnt;
            }
        }
        if(cnt == 8){
            ++ans;
        }
    }
    cout << ans << "\n";

    return 0;
}

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

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

  while (n--){
    int a, e = 10000000, maxN = 0, minN = 0;
    int num[8];

    cin >> a;

    for (int i = 0; i < 8; i++){
      num[i] = (a / e) % 10;
      e /= 10;
    }

    sort(num, num + 8);

    e = 10000000;
    for (int i = 0; i < 8; i++){
      minN += (num[i] * e);
      e /= 10;
    }

    sort(num, num + 8, greater<int>());

    e = 10000000;
    for (int i = 0; i < 8; i++){
      maxN += (num[i] * e);
      e /= 10;
    }

    cout << maxN - minN << endl;
  }
}

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

int main() {
  int a, b; cin >> a >> b;
  int ans = 0;
  for (int i = a; i <= b; ++i) {
    string str_num = to_string(i);
    string rev_num = str_num;
    reverse(str_num.begin(), str_num.end());
    
    if (str_num == rev_num) ++ans;
  }
  cout << ans << endl;
  
  return 0;
}

Measured memory_bytes: 256
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template<typename T> bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tlll;
typedef tuple<int,int,int> tiii;
const ll mod=1e9+7;
const int inf=1<<30;

int main(){
  int a,b,c,d,e; cin >> a >> b >> c >> d >> e;
  ll ans=c*60+d-(a*60+b)-e;
  cout << ans << endl;
}

Measured memory_bytes: 3636
N,M =map(int,input().split())
lis =[]
for i in range(M):
    S =[int(i) for i in input().split()]
    k =S.pop(0)
    foo = 0
    for i in S:
        foo += 2**(i-1)
    lis.append(foo)

ans = 0
p = [int(i)for i in input().split()]
for i in range(2**N):
    sta =True
    for n,j in enumerate(lis):
        if bin(i&j)[2:].count("1")%2 !=p[n]:
            sta = False
    if sta:
        ans += 1
print(ans)

Measured memory_bytes: 3064
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<iterator>
#define ll long long
#define mod 1000000007
using namespace std;
ll dp[100][100001];
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,w;
	cin>>n>>w;
	vector< pair<ll,ll> > items;
	ll i,j;
	for(i=0; i<n; i++)
	{
		int weight, value;
		cin>>weight>>value;
		items.push_back(make_pair(weight,value));
	}
	for(i=0; i<=w; i++)
	{
		if(items[0].first>i)
		{
			dp[0][i]=0;
		}
		else
		{
			dp[0][i]=items[0].second;
		}
	}
	for(i=1; i<items.size(); i++)
	{
		for(j=0; j<=w; j++)
		{
			if(items[i].first>j)
			{
				dp[i][j]=dp[i-1][j];
			}
			else
			{
				dp[i][j]=max(dp[i-1][j],items[i].second+dp[i-1][j-items[i].first]);
			}
		}
	}
	cout<<dp[items.size()-1][w]<<"\n";
	
	return 0;
}


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

double q;
double f(double x) {
    return x - ((x*x*x - q) / (3 * x * x));
}

int main() {
    while (cin >> q, q != -1){
        double x = q/2;
        while (fabs(x*x*x - q) > 1e-5 * q) {
            x = f(x);
        }
        printf("%.10lf\n", x);
    }
}



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

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

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

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    while (A.at(i) % 2 == 0){
      A.at(i) /= 2;
      cnt++;
    }
  }

  cout << cnt << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using ll = long long int;
using vec = vector<ll>;
using P = pair<int,int>;
using LP = pair<ll,int>;
const ll INF = 1e18+7;

// 未解決
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  si--; sj--; ti--; tj--;
  vector<string> c(h);
  rep(i,h) {
    cin >> c[i];
  }

  auto toId = [&](int i, int j, int v) {
    return (i*w+j)*4+v;
  };
  auto modCeilK = [&](ll x) {
    return (x+k-1)/k*k;
  };

  // start dijkstra
  int di[] = {1, 0, -1, 0};
  int dj[] = {0, 1, 0, -1};
  priority_queue<LP, vector<LP>, greater<LP> > q;
  vector<ll> dist(h*w*4, INF);
  auto push = [&](int i, int j, int v, ll x) {
    int id = toId(i,j,v);
    if (dist[id] <= x) return;
    dist[id] = x;
    q.emplace(x, id);
  };
  rep(v,4) push(si,sj,v,0);

  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();

    if (dist[id] != d) continue;

    int v = id % 4;
    int j = id / 4 % w;
    int i = id / 4 / w;

    rep(nv, 4) {
      push(i,j,nv,modCeilK(d));
      int ni = i + di[v], nj = j + dj[v];
      if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == '.') {
        push(ni,nj,v,d+1);
      }
    }
  }
  // end dijkstra

  ll ans = INF;
  rep(v,4) {
    int id = toId(ti,tj,v);
    ans = min(ans, dist[id]);
  }
  if (ans == INF) cout << -1 << endl;
  else cout << modCeilK(ans)/k << endl;
}


Measured memory_bytes: 44180
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, M = map(int, input().split())
box = [1]*N
red = [False]*N
red[0] = True


def update(fr, to):
    if box[fr] == 1:
        red[to] = red[fr] | red[to]
        box[fr] -= 1
        red[fr] = False
        box[to] += 1
    else:
        red[to] = red[fr] | red[to]
        box[fr] -= 1
        box[to] += 1


for _ in range(M):
    a, b = map(lambda x: int(x)-1, input().split())
    update(a, b)

ans = sum(red)
print(ans)


Measured memory_bytes: 4596
#include<bits/stdc++.h>
using namespace std;
#define maxn 2005
int n,m,q;
char s[maxn][maxn];
int sum[maxn][maxn];
int eup[maxn][maxn];
int eleft[maxn][maxn];
main()
{
    int i,j,r1,r2,c1,c2,ans;
    scanf("%d%d%d",&n,&m,&q);
    for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf(" %c",&s[i][j]);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
            eup[i][j] = eup[i-1][j] + eup[i][j-1] - eup[i-1][j-1];
            eleft[i][j] = eleft[i-1][j] + eleft[i][j-1] - eleft[i-1][j-1];
            if(s[i][j]=='1') sum[i][j]++;
            if(s[i][j]=='1' && s[i-1][j]=='1') eup[i][j]++;
            if(s[i][j]=='1' && s[i][j-1]=='1') eleft[i][j]++;
        }
    }
    while(q--)
    {
        scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
        ans = (sum[r2][c2] - sum[r1-1][c2] - sum[r2][c1-1] + sum[r1-1][c1-1]);
        ans -= (eup[r2][c2] - eup[r1][c2] - eup[r2][c1-1] + eup[r1][c1-1]);
        ans -= (eleft[r2][c2] - eleft[r1-1][c2] - eleft[r2][c1] + eleft[r1-1][c1]);
        printf("%d\n",ans);
    }
}


Measured memory_bytes: 52864
N=int(input())

if N%2:
    print(N*(N-1)//2-N//2)
    for i in range(1,N+1):
        for j in range(1,i):
            if i+j==N:
                continue
            print(i,j)
else:
    print(N*(N-1)//2-N//2)
    for i in range(1,N+1):
        for j in range(1,i):
            if i+j==N+1:
                continue
            print(i,j)

Measured memory_bytes: 9228
#include <iostream>
#include <vector>
#include <numeric>
#include <cmath>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<double> a(N);
    for (int i = 0; i < N; i++) cin >> a[i];

    double avg = accumulate(a.begin(), a.end(), 0.0) / a.size();
    int ans;
    double dis = 10000;
    for (int i = 0; i < N; i++) {
        if (abs(a[i] - avg) < abs(dis - avg)) {
            ans = i;
            dis = a[i];
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 888
#include<bits/stdc++.h>
using namespace std;
int main()
{
	bool check=false;
	int cake=4,dnut=7,n=0;
	int pc=0,qd=0;
	cin>>n;
	pc=(n-(n%4))/cake;
	qd=(n-(n%7))/dnut;
	for(int i=0;i<=pc;i++)
		for(int j=0;j<=qd;j++)
		{
			if((i*cake+j*dnut)==n)
			{
				check=true;
				break;
			}
		}
	
	if(check==true)
		cout<<"Yes";
	else
		cout<<"No";
	return 0;
}

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

string s, t;
bool f;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	cin >> s >> t;
	for(int i=0; i<s.size(); ++i){
		s = s[s.size()-1] + s.substr(0, s.size()-1);
		if(s == t) f = true;
	}
	cout << (f ? "Yes" : "No") << "\n";
}

Measured memory_bytes: 256
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <iomanip>
#include <map>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;

int k, s;

int main() {
    cin >> k >> s;
    
    int ans = 0;
    for (int a = 0; a <= k; a++) {
        for (int b = 0; b <= k; b++) {
            int r = s - a - b;
            if (r >= 0 && r <= k) ans++;
        }
    }
    
    cout << ans << endl;
}


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

using namespace std;
typedef long long int ll;

int main(void)

{
    ll n, k, i, a=0;
    cin>>n>>k;
    ll ara[n];
    for(i = 0; i < n; i++){
        cin>>ara[i];
    }
    sort(ara, ara+n);
    if(k >= n)
        a = 0;
    else{
        for(i = 0; i < n-k; i++)
            a += ara[i];
    }
    cout<<a<<"\n";
    return 0;
}


Measured memory_bytes: 1792
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();

        StringBuilder stb = new StringBuilder();
        for(int i = 0 ; i < s.length() ; i++){
            stb.append('x');
        }
        System.out.println(stb.toString());
    }
}

Measured memory_bytes: 22996
#include<iostream>
#include<string>
#include<string.h>
#include<map>
#include<set>  
#include<vector> 
#include<stdio.h> 
#include<math.h> 
#include<algorithm>
#include<queue> 
using namespace std;
int a[100000],b[100000];
int n,h,m;
int main()  
{cin>>n>>h;
 for (int i=0;i<=n-1;i++)
 cin>>a[i]>>b[i];
 sort(a,a+n);
 sort (b,b+n);
 for (int i=n-1;i>=0;i--){
 if (a[n-1]<b[i])
 h-=b[i];
 else
 h-=a[n-1];
 
 if (h<=0){
 cout<<n-i;
 system("pause");
 return 0;}}
   
 while(h>0){
 h-=a[n-1];
 m++;
 if (h<=0){
 cout<<m+n;}} 
 system("pause");
 return 0;
}        

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

int a[103];

int main(){

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

    /*for(int i=0; i<n; ++i){
        printf("%d ", a[i]);
    }
    puts("");*/

    scanf("%d", &m);
    for(int i=0; i<m; ++i){
        scanf("%d%d", &p, &x);
        if(x<a[p-1]){
            printf("%d\n", sum-(a[p-1]-x));
        }
        else{
            printf("%d\n", sum+(x-a[p-1]));
        }
    }




    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <functional>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

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

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

int main(void){
    string s;
    cin >> s;
    int r = 0;
    for (char c : s){
        r = (r + c - '0') % 9;
    }
    cout << (r == 0 ? "Yes" : "No") << endl;
    return 0;
}


Measured memory_bytes: 3724
#include<bits/stdc++.h>
using namespace std;
int main()
{
	std::ios::sync_with_stdio(false);
	int n,a,b,sum=0,x;
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
		x=i/10000+(i/1000)%10+(i/100)%10+(i/10)%10+i%10;
		//cout<<i/10000<<" "<<(i/1000)%10<<" "<<(i/100)%10<<" "<<(i/10)%10<<" "<<i%10<<endl;
		if(x>=a&&x<=b)
		sum+=i;
	}
	cout<<sum<<endl;
	return 0;
}



Measured memory_bytes: 256
#include<stdio.h>
int main()
{
  int x,y;
  scanf("%d%d",&x,&y);
  while(1){
  if(x==0&&y==0)
    break;
  else if(x<y)
    printf("%d %d\n",x,y);
  else
    printf("%d %d\n",y,x);
  scanf("%d%d",&x,&y);
  }
  return 0;
}


Measured memory_bytes: 1976
#include<iostream>
#include<cmath>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
        int W,H,x,y,r;
        scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
        if((x+r)>W ||(x-r)<0 || (y-r)<0 || (y+r)>H){
                printf("No\n");
        }else{
                printf("Yes\n");
        }
        return 0;
}

Measured memory_bytes: 1152
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <map>
#include <iomanip>
#include <limits.h>
#include <queue>

using namespace std;    
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const bool DEBUG = false;


int main(){

    ll A, B, C, D, E, F;
    cin >> A >> B >> C >> D >> E >> F;

    double max_ratio = -1.0;
    ll water = 0;
    ll sugar = 0;
    for(ll a = 0; true; a ++){
        if( 100 * A * a > F) break;
        for(ll b = 0; true; b++){
            if( 100 * A * a + 100 * B * b > F) break;
            for(ll c = 0; true; c++){
                if( 100 * A * a + 100 * B * b + C * c > F) break;
                for(ll d = 0; true; d++){
                    if( 100 * A * a + 100 * B * b + C * c + D * d > F) break;
                    ll weight_water = 100 * A * a + 100 * B * b; 
                    ll weight_sugar = C * c + D * d; 
                    ll weight_all   = weight_water + weight_sugar;
                    ll max_sugar = weight_water / 100 * E;
                    if(weight_sugar > max_sugar ) break;
                    double ratio = 100.0 * weight_sugar / (double)(weight_sugar + weight_water);
                    if(ratio > max_ratio){
                        max_ratio = ratio;
                        water = weight_water;
                        sugar = weight_sugar;
                    }

                }
            }
        }
    }

    cout << water + sugar << " " << sugar << endl;
}

Measured memory_bytes: 256
using LIB;
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        io.r<int>();
        double[][] d = io.r<double>(2, ' ');
        double d1 = 0;
        double d2 = 0;
        double d3 = 0;
        double cd = double.MinValue;
        int cnt = d[0].Count();
        for (int i = 0; i < cnt; i++)
        {
            double b = Math.Abs(d[0][i] - d[1][i]);
            d1 += b;
            d2 += Math.Pow(b, 2);
            d3 += Math.Pow(b, 3);
            if (cd < b) { cd = b; }
        }
        d2 = Math.Pow(d2, (double)1 / 2);
        d3 = Math.Pow(d3, (double)1 / 3);
        io.w(d1);
        io.w(d2);
        io.w(d3);
        io.w(cd);
        io.wflush();
    }
}

namespace LIB
{
    public class io
    {
        private const int WMAX = 1000;
        private static StringBuilder S = new StringBuilder();
        public static T r<T>() { return util.parse<T>(r()); }
        public static T[] r<T>(char s = ' ') { return r().Split(s).Select(util.parse<T>).ToArray(); }
        public static T[] r<T>(int l) { T[] r = new T[l]; for (int i = 0; i < l; i++) { r[i] = r<T>(); } return r; }
        public static T[][] r<T>(int l, char s = ' ') { T[][] r = new T[l][]; for (int i = 0; i < l; i++) { r[i] = r<T>(s); } return r; }
        private static string r() { return Console.ReadLine(); }
        public static void w(object v, bool lf = true) { S.Append(util.parse<string>(v)); if (lf == true) { S.Append('\n'); } if (S.Length >= WMAX) { wflush(); } }
        public static void wflush() { Console.Write(S.ToString()); S.Length = 0; }
    }
    public class util
    {
        public static T parse<T>(object value) { return (T)(Convert.ChangeType(value, typeof(T))); }
    }
    public class memo<Key, Result>
    {
        private Dictionary<Key, Result> R;
        public memo() { R = new Dictionary<Key, Result>(); }
        public Result exec(Key k, Func<Key, Result> f) { Result r; if (R.ContainsKey(k)) { r = R[k]; } else { r = f(k); R.Add(k, r); } return r; }
    }
}

Measured memory_bytes: 10604
n, a, b, c = [int(it) for it in input().split()]
ls = [int(input()) for it in range(n)]
def dfs(cur, tmpA, tmpB, tmpC):
    if cur == n:
        return abs(a - tmpA) + abs(b - tmpB) + abs(c - tmpC) if tmpA > 0 and tmpB > 0 and tmpC else 10**20
    return min(
        dfs(cur + 1, tmpA + ls[cur], tmpB, tmpC) + (10 if tmpA > 0 else 0),
        dfs(cur + 1, tmpA, tmpB + ls[cur], tmpC) + (10 if tmpB > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC + ls[cur]) + (10 if tmpC > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC)
    )

print(dfs(0, 0, 0, 0))


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

int main() {
  int N;
  cin >> N;
  vector<int> D(N);
  for (int i=0; i<N; i++){
    cin >> D[i];
  }
  sort (D.begin(), D.end());
  int count = 1;
  for (int i=0; i<N-1; i++){
    if(D[i]!=D[i+1]){
    	count++;
    }
  }
  cout << count << endl;
}

Measured memory_bytes: 3628
#!/usr/bin/env python3
import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n = I()
s = list(S())

w = 0
r = n - 1
ans = 0
while True:
    for i in range(w, n):
        if s[i] == "W":
            w = i
            break
    else:
        w = -1
    for i in range(r, 0, -1):
        if s[i] == "R":
            r = i
            break
        r += 1
    else:
        r = -1
    if w == -1 or r == -1:
        print(ans)
        exit()
    else:
        if r < w:
            print(ans)
            exit()
        ans += 1
        s[w] = "R"
        s[r] = "W"
        w += 1
        r -= 1


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

int main(){
    int r, g, b, n; 
    vector<int> input(3);
    cin >> input[0] >> input[1] >> input[2] >> n;
    sort(input.begin(), input.end());
    r = input[2]; g = input[1]; b = input[0];
    int ans =0;
    
    int r_n = n/r; 
    int g_n = n/g; 
    int b_n = n/b;

    for(int i=0; i<=r_n; i++){
        for(int j=0; j<=g_n; j++){
            if( (n-r*i-g*j)%b==0 && (n-r*i-g*j)/b >=0)
                ans++ ;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3600
#!/usr/bin/rdmd
//domestic 2007 A in D-lang

import std.cstream;

int main(/*char [][]args*/){
  int i, n, s, ma, mi, sum;
  while(true){
    ma=0; mi=1000; sum=0;
    din.readf(&n);
    if(n==0)return 0;
    for(i=0;i<n;i++){
      din.readf(&s);
      ma=ma>s?ma:s;
      mi=mi<s?mi:s;
      sum+=s;
    }
    dout.writefln("%d",(sum-ma-mi)/(n-2));
  }
}

Measured memory_bytes: 1172
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int intsort(const void *a, const void *b){
	return *(int*)a - *(int*)b;
	/*qsort(a,aの配列数,sizeof(int),intsort);*/
}

int longsort(const void *a, const void *b){
	long *A=(long *)a;
	long *B=(long *)b;
	if(*A>*B) return -1;
	if(*A<*B) return 1;
	return 0;
}

int main(){
	int N,cnt=0,i,back=0,now;
	scanf("%d",&N);

	for(i=0;i<N;i++){
		scanf("%d",&now);
		if(back<=now){
			cnt++;
			back=now;
		}
	}

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

Measured memory_bytes: 128
#include <stdio.h>
long long t[2], a[2], b[2];
long long w, x, y, z, e, f;
long long ans;
int main() {
	scanf("%lld%lld", t, t + 1);
	scanf("%lld%lld", a, a + 1);
	scanf("%lld%lld", b, b + 1);
	x = t[0]; y = t[1];
	w = b[0] - a[0];
	z = b[1] - a[1];
	e = w * x + z * y;
	if (e == 0) {
		printf("infinity\n");
		return 0;
	}
	if (e > 0) {
		w = -w;
		z = -z;
		e = -e;
	}
	if (w < 0) {
		printf("0\n");
		return 0;
	}
	f = w * x;
	e = -e;
	ans = f / e * 2;
	if (f % e)ans++;
	printf("%lld\n", ans);
}

Measured memory_bytes: 128
import java.util.ArrayList;
		import java.util.Collection;
		import java.util.Collections;
		import java.util.List;
		import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		List<Integer> l = new ArrayList<>();
		List<Integer> r = new ArrayList<>();
		for (int i = 0; i < m; i++) {
			l.add(sc.nextInt());
			r.add(sc.nextInt());
		}
		int min = Collections.max(l);
		int max = Collections.min(r);
		int num = Math.min(max, n) + 1 - Math.max(min, 1);
		if (num < 0) {
			num = 0;
		}
		System.out.println(num);
	}
}


Measured memory_bytes: 87680
package main

import "fmt"

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_6_&Alang=jp>
 */

func init() {
}

func main() {
	var r, c int
	fmt.Scanf("%d %d", &r, &c)
	sheet := make([][]int, r+1)
	for i := 0; i <= r; i++ {
		sheet[i] = make([]int, c+1)
	}

	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			fmt.Scanf("%d", &sheet[i][j])
			sheet[r][j] += sheet[i][j]
			sheet[i][c] += sheet[i][j]
			sheet[r][c] += sheet[i][j]
		}
	}

	for i := 0; i <= r; i++ {
		for j := 0; j <= c; j++ {
			fmt.Print(sheet[i][j])
			if j != c{
				fmt.Print(" ")
			}
		}
		fmt.Println("")
	}
}

Measured memory_bytes: 1824
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#define int long long
using namespace std;

typedef pair<int, int> Pii;

void readline(int N, vector<int> &a){
    string s;
    for(int i = 0; i < N - 1; i++){
        getline(cin, s, ' ');
        a[i] = atoi(s.c_str());
    }
    getline(cin, s, '\n');
    a[N - 1] = atoi(s.c_str());
}
void readline(int N, vector<double> &a){
    string s;
    for(int i = 0; i < N - 1; i++){
        getline(cin, s, ' ');
        a[i] = atof(s.c_str());
    }
    getline(cin, s, '\n');
    a[N - 1] = atof(s.c_str());
}

signed main(){

    int a, b;

    cin >> a >> b;

    int n = b - a - 1;
    int ans = n * (n + 1) / 2 - a;

    cout << ans;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#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);
	int n,t;cin >> n >> t;
	vector<int> a(n);
	rep(i,n) cin >> a.at(i);
	int r = 1, k = 0;
	map<int,int> m;
	rep(i,n){
		while(r < n && a.at(i) < a.at(r)){
			k = max(k, a.at(r) - a.at(i));
			r++;
		}
		if(k) m[k]++;
		i = r - 1;
		r++;
		k = 0;
	}
	int ans = 0, ma = 0;
	for(auto i : m){
		if(i.first > ma){
			ma = i.first;
			ans = i.second;
		}
	}
	cout << ans << endl;
}


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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n-1; i >= 0; i--)
#define ALL(x) x.begin(),x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9+7;
const ll INF = 1e18;
const int MAX = 1e6;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {int to, cost;}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問状況

int n, m; // 変数
double p[3100];
double dp[3100][3100];

int main() {
// input
    cin >> n;
    rep(i, n+1) {
        if (i > 0) cin >> p[i];
    }
// solve
    dp[0][0] = 1;
    rep(i, n+1) {
        rep(j, n+1) {
            if (i == 0 or j > i) continue;
            dp[i][j] = dp[i-1][j] * (1 - p[i]);
            if (j > 0) {
                dp[i][j] += dp[i-1][j-1] * p[i];
            }
        }
    }
// output
    double ans = 0;
    for (int i = n; i > n / 2; i--) {
        ans += dp[n][i];
    }
    printf("%.10f\n", ans);
}

Measured memory_bytes: 50212
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define db long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
 
#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define EB emplace_back
#define fi first
#define se second
 
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define UPD(x,y) (((x)+=(y))>=mo?x-=mo:233)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
 
#define LS3 k*2,l,mid
#define RS3 k*2+1,mid+1,r
#define LS5 k*2,l,mid,x,y
#define RS5 k*2+1,mid+1,r,x,y
#define GET pushdown(k);int mid=(l+r)/2
 
#define inf 2000000000
#define sqr(x) ((x)*(x))
using namespace std;
const int N=200005;
int p[N],t[N*4][2];
int n,Q,mx,a[N],vis[N],ans[N];
void change(int k,int l,int r,int p,int v0,int v1){
	if (l==r){
		t[k][0]=v0;
		t[k][1]=v1;
		return;
	}
	int mid=(l+r)/2;
	if (p<=mid) change(k*2,l,mid,p,v0,v1);
	else change(k*2+1,mid+1,r,p,v0,v1);
	t[k][0]=max(t[k*2][0],t[k*2+1][0]);
	t[k][1]=max(t[k*2][1],t[k*2+1][1]);
}
int ask(int fl,int k,int l,int r,int p){
	if (p<=l) return t[k][fl];
	int mid=(l+r)/2;
	if (p>mid) return ask(fl,k*2+1,mid+1,r,p);
	return max(ask(fl,k*2,l,mid,p),t[k*2+1][fl]);
}
bool check(int HX,int HY,int CX,int CY){
	int t=CX-CY+Q;
	if (t>=0&&ask(t&1,1,1,n,HY)>=t) return 1;
	t=CY-CX+Q;
	if (t>=0&&ask(t&1,1,1,n,HX)>=t) return 1;
	return 0;
}
int main(){
	scanf("%d",&n);
	For(i,1,n){
		scanf("%d",&a[i]);
		if (a[i]>mx) mx=a[i],vis[i]=1,++Q;
	}
	Rep(i,n,1){
		int f0=0,f1=0,t=ask(0,1,1,n,a[i]);
		vis[i]?f0=t+2:f1=t+1;
		t=ask(1,1,1,n,a[i]);
		vis[i]?(f1=(t?t+2:0)):(f0=(t?t+1:0));
		change(1,1,n,a[i],f0,f1);
	}
	bool fl=1;
	int HX=-(1<<30),HY=HX,CX=0,CY=0;
	For(i,1,n){
		int _HX=HX,_CX=CX;
		if (vis[i]) --Q;
		change(1,1,n,a[i],0,0);
		if (HX<a[i]) HX=a[i],++CX;
		if (check(HX,HY,CX,CY)) continue;
		HX=_HX; CX=_CX; ans[i]=1;
		if (HY<a[i]) HY=a[i],++CY;
		if (check(HX,HY,CX,CY)) continue;
		fl=0;
	}
	if (!fl) puts("-1");
	else For(i,1,n) putchar(ans[i]+'0');
}

Measured memory_bytes: 8320
#include <iostream>
using namespace std;
int main() {
  int n, k, s, ans, t, u;
  while (cin >> n >> k >> s, n) {
    int A[k];
    if (k == 1) {
      if (n >= s) cout << 1 << endl;
      else cout << 0 << endl;
      continue;
    }
    for (int i = 0; i < k-1; ++i) A[i] = i+1;
    A[k-1] = s - k*(k-1)/2;
    ans = 0;
    t = k-2;
    while (A[k-1] > A[k-2]) {
      if (A[k-1] <= n) ++ans;
      ++A[t];
      --A[t+1];
      if (A[t] >= A[t+1]) {
	while (t > 0) {
	  --t;
	  ++A[t];
	  u = 0;
	  for (int i = 0; i <= t; ++i) u += A[i];
	  for (int i = t+1; i < k-1; ++i) {
	    A[i] = A[i-1]+1;
	    u += A[i];
	  }
	  A[k-1] = s - u;
	  if (A[k-1] > A[k-2]) {
	    t = k-2;
	    break;
	  }
	}
	if (!t) break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Measured memory_bytes: 3072
/* AOJ 0087
 *
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0087
 *
 */
#include <stdio.h>
#include <assert.h>

#define DEBUG 0

double gstack[100];
int gtop;

void push(double v){
    assert(gtop<100);
    if(DEBUG)
        printf("pushing %f\n", v);
    gstack[gtop] = v;
    gtop +=1;
}

double pop(void){
    assert(gtop>0);
    gtop -=1;
    if(DEBUG)
        printf("popping %f\n", gstack[gtop]);
    return gstack[gtop];
}

int is_num(char c){
    return ('0' <= c) && (c<='9');
}

int tokenize(char* p){
    int len;
    switch (*p){
        case '\0':
        case '\r':
        case '\n':
            return 0;
        case ' ':
        case '*':
        case '+':
        case '/':
            return 1;
        case '-':
            if(is_num(*(p+1))){
                len = 0;
                while(is_num(*(p+len+1))){
                    len++;
                }
                return len+1; /*sign*/
            }else{
                return 1;
            }
            break;
        default: /*0-9*/
            len = 0;
            while(is_num(*(p+len))){
                len++;
            }
            return len;
            break;
    }
    return 0;
}

int read_val(char* s, int len){
    int i, r;
    int sign;
    r = 0;
    if(*s == '-'){
        sign = -1;
        i=1;
    }else{
        sign = 1;
        i=0;
    }
    while(i<len){
        r *= 10;
        r += (*(s+i) - '0');
        i++;
    }
    return sign*r;
}


int main(){
    char buf[83];
    char* p;
    int len;
    double a, b;

    gtop=0;
    while(fgets(buf, 82, stdin)){
        p = buf;
        while((len = tokenize(p))){
            switch(*p){
                case ' ':
                    break;
                case '*':
                    b = pop();
                    a = pop();
                    push(a*b);
                    break;
                case '+':
                    b = pop();
                    a = pop();
                    push(a+b);
                    break;
                case '/':
                    b = pop();
                    a = pop();
                    push(a/b);
                    break;
                case '-':
                    if(len == 1){
                        b = pop();
                        a = pop();
                        push(a-b);
                        break;
                    }
                    /* fall through */
                default:
                    push(read_val(p, len));
                    break;
            }
            p+=len;
        }
        assert(gtop==1);
        printf("%f\n",pop());
        assert(gtop==0);
    }
    return 0;
}

Measured memory_bytes: 0
"""
AtCoder Beginner Contest 125  C - GCD on Blackboard

配列中の数字を一つ好きな値に書き換えて最大公約数を計算するときの最大値を求める
・書き換える→取り除くと考えてよい。
・全体のGCD = GCD(前半のGCD, 後半のGCD) のように、分割したり順番を入れ替えたりしても結果は変わらない。
なので、左からの累積GCDと、右からの累積GCDをあらかじめとっておくことで、要素を一個除いたときの全体のGCDも求められる。　

区間に対する処理 -> セグメント木いけるやん
"""
class SegmentTree:
    # セグメント木として初期化する配列、単位元、[a,b)に実行する関数
    def __init__(self, lst, ide_ele, segfunc):
        self.ide_ele = ide_ele
        self.func = segfunc
        # lstのサイズより大きい2^xで最小の値
        self.size = 2**(len(lst) - 1).bit_length()
        self.seg_lst = [self.ide_ele] * 2 * self.size

        """
        値を詰めていく
        ノードiの親：(i-1) // 2
        ノードiの子：2*i+1 と　2*i+2

        """
        for i in range(len(lst)):
            self.seg_lst[i + self.size - 1] = lst[i]

        # クエリが実行されたときに取り出せるように処理をしておく
        # この辺は何がしたいかによって自分で関数用意する
        for i in range(self.size-2, -1, -1):
            # 子から親に向かって結果を吸い上げるような形
            self.seg_lst[i] = self.func(self.seg_lst[2*i + 1], self.seg_lst[2*i + 2])

    def update(self, i, num):
        # 一番下の子の部分を更新するため、配列中のindexからseg_lstのindexに変える
        i += self.size - 1
        self.seg_lst[i] = num
        while i:
            # 親に更新を伝播
            i = (i - 1) // 2
            self.seg_lst[i] = self.func(self.seg_lst[2*i + 1], self.seg_lst[2*i + 2])

    def query(self, l, r):
        # 範囲がおかしい場合
        if r <= l:
            return self.ide_ele

        # seg_lst内でのindexに直す
        l += self.size - 1
        # 
        r += self.size - 2
        ret = self.ide_ele
        # 区間が閉じ切る = 同じ親のところに戻るまで
        while r - l > 1:
            # lの下位1ビットが立っていない。たぶんだが、あるノードの左右の子の左が範囲に入ってるかどうかの判断で、入ってないときか？
            if l & 1 == 0:
                # 一番初めは単位減であるretとlについてfuncをかけることになるので、やはり左の子が範囲に入ってないとき！
                ret = self.func(ret, self.seg_lst[l])
            # こっちは右の子が入ってるかどうかで、入っている場合の処理だと思う
            if r & 1 == 1:
                # 一番最初は単位元とrの処理なので、右側が範囲に入ってない時。
                ret = self.func(ret, self.seg_lst[r])
                r -= 1

            # それぞれの親に移動というか、一個上のdepthに移動
            l = l // 2
            r = (r - 1) // 2

        # retには下から積み上げてきたself.funcの結果が入ってるので、たどり着いた親の所で同じく処理を実行して戻す
        if l == r:
            ret = self.func(ret, self.seg_lst[l])
        else:
            ret = self.func(self.func(ret, self.seg_lst[l]), self.seg_lst[r])
        
        return ret

    
from fractions import gcd
N = int(input())
A = list(map(int, input().split()))

seg_tree = SegmentTree(A, 0, gcd)

ans = -1

for i in range(N):
    # 要素iを除いたときの全体のGCD
    ans = max(ans, gcd(seg_tree.query(0, i), seg_tree.query(i+1, N)))

print(ans)

Measured memory_bytes: 90464
// Create by JJL
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <set>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;

using namespace std;
int main(){
    IOS;
    /* START CODE */
    LL  x,k,d;
    cin>>x>>k>>d;
    if(x<0)x=-x;
    LL tk=x/d;
    if(tk>=k){
        cout<<x-k*d<<endl;
    }
    else {
        k-=tk;
        if(k%2==0) cout<<x%d<<endl;
        else cout<<d-x%d<<endl;
    }
    return 0;
}


Measured memory_bytes: 3176
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
    int N=0,A=0,B=0;
    cin>> N>>A>>B;
    int tmp = N*A;
    if(B<tmp){
        cout<<B<<endl;
    }else{
        cout<<tmp<<endl;
    }
    return 0;
}

Measured memory_bytes: 888