akhauriyash/Code-Regression huggingface dataset:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}
int64_t rll() {
	long long n;
	scanf("%lld", &n);
	return n;
}

int main() {
	int n = ri();
	int64_t a[n];
	int64_t first = 0;
	for (int i = 0; i < n; i++) {
		a[i] = ri();
		if (i % 2) first -= a[i];
		else first += a[i];
	}
	int64_t res[n];
	res[0] = first;
	for (int i = 1; i < n; i++) {
		res[i] = a[i - 1] * 2 - res[i - 1];
	}
	for (int i = 0; i < n; i++) std::cout << res[i] << " ";
	std::cout << std::endl;
	return 0;
}


Measured memory_bytes: 2816
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin >> n >> m;

if (n == 1 && m!= 1)
{
    cout << m*(m-1)/2 << endl;
}

if (n!=1 && m == 1)
{
    cout << n*(n-1)/2 << endl;
}

else
{
    cout << n*(n-1)/2 + m*(m-1)/2 << endl;
}

}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  int x1,y1,x2,y2;
  cin>>x1>>y1>>x2>>y2;
  int x3=x2-(y2-y1),y3=x2-x1+y2;
  int x4=x3-(x2-x1),y4=y3-(y2-y1);
  cout<<x3<<" "<<y3<<" "<<x4<<" "<<y4;
  return 0;
}

Measured memory_bytes: 256
n = int(input())
D = sorted(list(map(int,input().split())))
m = int(input())
T = sorted(list(map(int,input().split())))

ans = 'YES'
di = -1
for t in T:
  while True:
    di += 1
    if di >= n:
      ans = 'NO'
      break    
    if D[di] == t:
      break
        
print(ans)    

Measured memory_bytes: 36820
let lcs x y =
  let m = String.length x in
  let n = String.length y in
  let dp = Array.make_matrix (m + 1) (n + 1) 0 in
  for i = 1 to m do
    for j = 1 to n do
      dp.(i).(j) <-
        if x.[i-1] = y.[j-1] then dp.(i-1).(j-1) + 1
        else if dp.(i-1).(j) > dp.(i).(j-1) then dp.(i-1).(j)
        else dp.(i).(j-1)
    done
  done;
  dp.(m).(n)

let () =
  let q = read_int () in
  for _ = 0 to q - 1 do
    let x = read_line () in
    let y = read_line () in
    lcs x y |> Printf.printf "%d\n"
  done

Measured memory_bytes: 18592
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
ex = 0
for i in range(1, n):
    if a[i]-a[i-1] == 1:
        ex += c[a[i-1]-1]

print(sum(b)+ex)


Measured memory_bytes: 38384
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

int main() {
    int N, M; cin >> N >> M;
    vector<vector<int> > adjList(N + 1);
    vector<int> inDegree(N + 1, 0);
    for(int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        adjList[a].push_back(b);
        inDegree[b]++;
    }
    vector<int> dist(N + 1, 0);
    queue<int> q;
    for(int i = 1; i <= N; i++) if(!inDegree[i]) q.push(i);
    while(!q.empty()) {
        int top = q.front(); q.pop();
        for(int i = 0; i < adjList[top].size(); i++) {
            if(--inDegree[adjList[top][i]] == 0) q.push(adjList[top][i]);
            dist[adjList[top][i]] = max(dist[adjList[top][i]], dist[top] + 1);
        }
    }
    int ret = 0;
    for(int i = 1; i <= N; i++) ret = max(ret, dist[i]);
    
    cout << ret << endl;
    
    return 0;
}

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

int main() {
  string a[3];rep(i,3) cin>>a[i];
  rep(i,3) {
    char c = a[i][0] + ('A' - 'a');
    cout<<c;
  }
}


Measured memory_bytes: 256
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main()
{
	int e[101][101];
	int R, C, r, c;
	int s, total;

	scanf("%d%d", &R, &C);
	for (r = 1; r <= R; r++) {
		for (c= 1; c <= C; c++) scanf("%d", &e[r][c]);
	}
	
	for (r = 1; r <= R; r++) {
		s = 0;
		for (c = 1; c <= C; c++) s += e[r][c];
		e[r][0] = s;
	}
	total = 0;
	for (c = 1; c <= C; c++) {
		s = 0;
		for (r = 1; r <= R; r++) s += e[r][c];
		e[0][c] = s;
		total += s;
	}
	for (r = 1; r <= R; r++) {
		for (c = 1; c <= C; c++) printf("%d ", e[r][c]);
		printf("%d\n", e[r][0]);
	}
	for (c = 1; c <= C; c++) printf("%d ", e[0][c]);
	printf("%d\n", total);
	return 0;
}


Measured memory_bytes: 2048
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
typedef long long ll;
const ll INF = 1LL<<62;
int main() {
	int N, M;
	cin >> N >> M;
	vector<string> S(N);
	for (auto& x : S) cin >> x;

	mcf_graph<int,int> G(2602);
	int s = 2600;
	int t = 2601;
	for (int i = 0; i < 2500; ++ i) G.add_edge(i, t, 1, 0);
	for (int i = 0; i < 100; ++ i) G.add_edge(s, 2500+i, 1, 0);

	int k = 2500;
	for (int i = 0; i < N; ++ i) for (int j = 0; j < M; ++ j) if (S[i][j] == 'o') {
		vector<vector<int>> dist(N, vector<int>(M, -1));
		queue<pair<int,int>> Q;
		dist[i][j] = 0;
		Q.push({i,j});
		while (!Q.empty()) {
			int ii = Q.front().first;
			int jj = Q.front().second;
			Q.pop();
			G.add_edge(k, ii*50+jj, 1, 1000-dist[ii][jj]);
			if (ii+1 < N && S[ii+1][jj] != '#' && dist[ii+1][jj] == -1) {
				dist[ii+1][jj] = dist[ii][jj] + 1;
				Q.push({ii+1, jj});
			}
			if (jj+1 < M && S[ii][jj+1] != '#' && dist[ii][jj+1] == -1) {
				dist[ii][jj+1] = dist[ii][jj] + 1;
				Q.push({ii, jj+1});
			}
		}
		++ k;
	}
	auto r = G.flow(s, t);
	cout << r.first*1000 - r.second << endl;
}


Measured memory_bytes: 7180
/*
 * WelcomeToAtCoder.cpp
 *
 *  Created on: 2020/02/19
 *      Author: black
 */

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <list>
#include <cstring>
#include <stdio.h>
using namespace std;

int numList[4];
char plcs[3];
string strtmp;
char ans[10];

bool calc(){
	int total = numList[0];
	strtmp += to_string(numList[0]);

	for(int i=0; i<3;++i){
		strtmp += plcs[i];
		strtmp += to_string(numList[i+1]);
		if(plcs[i] == '+'){
			total += numList[i+1];
		}
		else{
			total -= numList[i+1];
		}
	}

	if (total == 7){
		strcpy(ans, strtmp.c_str());
		strtmp = "";
		return true;
	}
	else{
		strtmp = "";
		return false;
	}
}

bool solve(char plc, int i){
	if (i == 3){
		return calc();
	}
	plcs[i] = plc;
	return solve('+', i+1) || solve('-', i+1);
}


int main(){
	int num;
	scanf("%d", &num);
	numList[0] = num/1000;
	num -= numList[0]*1000;
	numList[1] = num/100;
	num -= numList[1]*100;
	numList[2] = num/10;
	num -= numList[2]*10;
	numList[3] = num/1;

	if (solve('+', 0)){
		cout << ans << "=7" << endl;
		return 0;
	}
	else if(solve('-', 0)){
		cout << ans << "=7" << endl;
		return 0;
	}





}


Measured memory_bytes: 256
// ABC 048 A- AtCoder *** Contest

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s1, s2, s3;
  cin >> s1 >> s2 >> s3;

  char c;
  c = s2.at(0); // char型に代入

  cout << "A" << c << "C" << endl;
}


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

using namespace std;
int partsum[105],partsum1[105],dp[105];
int main()
{
    int N;
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
    {
        int x;
        scanf("%d",&x);
        partsum[i]=partsum[i-1]+x;
    }
    for(int i=1;i<=N;i++)
    {
        int x;
        scanf("%d",&x);
        partsum1[i]=partsum1[i-1]+x;
    }
    int ans=-1;
    for(int i=1;i<=N;i++)
    {
        ans=max(ans,partsum1[N]-partsum1[i-1]+partsum[i]);
    }
    printf("%d",ans);
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int main(){
	scanf("%d%d%d",&n,&a,&b);
	if(a>b) return puts("0"),0;
	if(n==1&&a!=b) return puts("0"),0;
	long long mn=a*1LL*(n-1)+b,mx=a+(n-1)*1LL*b;
	cout<<mx-mn+1<<endl;
	return 0;
}

Measured memory_bytes: 256
a,b,k=map(int,input().split())
for i in range(k//2):
  if a%2==1:
    a-=1
  b+=a//2
  a//=2
  if b%2==1:
    b-=1
  a+=b//2
  b//=2
if k%2==1:
  if a%2==1:
    a-=1
  b+=a//2
  a//=2
print(a,b)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

int V, E;
const int MAX_V = 100;

struct edge { int to, cap, rev; };

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap){
    G[from].push_back((edge){to, cap, (int)G[to].size()});
    G[to].push_back((edge){from, 0, (int)G[from].size()-1});
}

int dfs(int v, int t, int f){
    if (v == t) return f;
    used[v] = 1;
    rep(i, G[v].size()){
        edge &e = G[v][i];
        if (!used[e.to] && e.cap>0){
            int d = dfs(e.to, t, min(f, e.cap));
            if (d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t){
    int flow = 0;
    while(1){
        memset(used, 0, sizeof(used));
        int f = dfs(s, t, INF);
        if (f == 0) return flow;
        flow += f;
    }
}


int main(){
    cin >> V >> E;

    rep(i, E){
        int u, v, c;
        cin >> u >> v >> c;
        add_edge(u, v, c);
    }

    int ans = max_flow(0, V-1);

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 3204
from collections import defaultdict


def main():
    _ = input()
    S = input()
    S_idx = defaultdict(list)
    num_combs = 0
    for i in range(len(S)):
        S_idx[S[i]].append(i)

    for i in range(10):
        for j in range(10):
            for k in range(10):
                if not ((len(S_idx[str(i)]) == 0) or (len(S_idx[str(j)]) == 0) or (len(S_idx[str(k)]) == 0)):
                    s1_idx = S_idx[str(i)][0]
                    if not s1_idx >= S_idx[str(j)][-1]:
                        s2_idx = binary_search(S_idx[str(j)], s1_idx)
                        if not (s2_idx == -1 or s2_idx >= S_idx[str(k)][-1]):
                            num_combs += 1
    print(num_combs)


def binary_search(A, x):
    ok = len(A)
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if A[mid] > x:
            ok = mid
        else:
            ng = mid
    if ok < len(A):
        return A[ok]
    else:
        return -1


if __name__ == '__main__':
    main()

Measured memory_bytes: 74224
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip> //setprecision
#include <limits>
using namespace std;
int main(){
    long long n,T;
    cin >> n >> T;
    vector<int> t(n);
    long long sum=0;
    for(long long i=0; i<n; ++i){
        cin >> t[i];
        if(i !=0 && t[i]-t[i-1]<T) sum+=(t[i]-t[i-1]);
        else if(i!=0) sum+=T;
    }
    cout << sum+T << endl;
}

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(),(a).end()

int main(){
    string S;
    cin>>S;
    int l=S.length()-1;
    for(;l>1;l--){
        if(l%2)continue;
        string A=S.substr(0,l/2);
        string B=S.substr(l/2,l/2);
        if(A==B){
            cout<<l<<endl;
            return 0;
        }
    }
    cout<<0<<endl;
    return 0;
}
//a=97,z=122

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <bitset>

using namespace std;
typedef long long ll;

int main() {
	ll N; cin >> N;
	if (N % 2 == 1) {
		cout << 0 << endl;
	}
	else {
		ll c = 0;
		N /= 2;
		while (N > 0) {
			c += N / 5;
			N /= 5;
		}
		cout << c << endl;
	}
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#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 drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//グリッド：（典型）dp,dfs,bfs,最短経路,その他
int main(){
    string s;cin>>s;
    ll k;cin>>k;
    ll n=(ll)s.size();
    vl num(n);
    rep(i,n){
        num[i]=(ll)(s[i]-'a');
    }
    rep(i,n){
        if(num[i]==0) continue;
        int need=26-num[i];
        if(need<=k){
            num[i]=0;
            k-=need;
        }
    }
    num[n-1]=(num[n-1]+k)%26;
    string ans="";
    rep(i,n){
        ans+=(char)(num[i]+'a');
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1408
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  string s;
  cin >> s;
  int n = (int) s.length();
  cout << ((n % 2 == 0) ^ (s[0] == s.back()) ? "Second" : "First") << endl;
  return 0;	
}

Measured memory_bytes: 512
n = int(input())
A = list(map(int,input().split()))
c,c2,c4 = 0,0,0
for a in A:
  if a%4==0:c4+=1
  elif a%2==0:c2+=1
  else:c+=1
f = False
if c4>=c:f=True
if c4 == c-1 and c2==0:f=True
if f:print("Yes")
else:print("No")
        


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

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

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

class NODE{
public:
	int x1;
	int y1;
	int x2;
	int y2;
	int depth;
};

int main(){
	int X,Y,tx,ty,kx,ky;
	bool data[52][52];
	while(cin >> X >> Y , X){
		cin >> tx >> ty >> kx >> ky;
		rep(i,52)rep(j,52)data[i][j] = 1;
		rep(i,Y)rep(j,X){
			cin >> data[1+i][1+j];
		}
		queue<NODE> Q;
		NODE t = {tx,ty,kx,ky,0};
		Q.push(t);
		bool done[52][52][52][52] = {0};
		while(Q.size()){
			NODE q = Q.front();Q.pop();
			if(q.depth >= 100)continue;
			if( done[q.x1][q.y1][q.x2][q.y2] )continue;
			done[q.x1][q.y1][q.x2][q.y2] = true;
			
			if(q.x1 == q.x2 && q.y1 == q.y2){
				cout << q.depth << endl;
				goto end;
			}
			rep(i,4){
				NODE next;
				next.x1 = q.x1 + dx[i];
				next.y1 = q.y1 + dy[i];
				next.x2 = q.x2 - dx[i];
				next.y2 = q.y2 - dy[i];
				next.depth = q.depth + 1;
				if(data[next.y1][next.x1] == 1){
					next.x1 = q.x1;
					next.y1 = q.y1;
				}
				if(data[next.y2][next.x2] == 1){
					next.x2 = q.x2;
					next.y2 = q.y2;
				}
				Q.push(next);
			}
		}
		cout << "NA" << endl;
		end:;	
	}
}

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

namespace _517_LongestSteps
{
	class Program
	{
		static void Main ( string[] args )
		{
			while (true)
			{
				//input
				int[] orderNumber = Console.ReadLine ().Split (' ').Select (str => int.Parse (str)).ToArray ();

				bool isEnd = orderNumber.All (item => item == 0);
				if (isEnd)
				{
					break;
				}

				int dealCardCount = orderNumber[1];
				int[] cardArray = new int[dealCardCount];
				for (int lp = 0; lp < cardArray.Length; lp++)
				{
					cardArray[lp] = int.Parse (Console.ReadLine ());
				}

				//process
				int whiteCardCount = cardArray.Count (item => item == 0);
				cardArray = cardArray.Where (item => item != 0).OrderBy (item => item).ToArray ();

				List<ChainNumber> list = new List<ChainNumber> (cardArray.Length / 10);

				int previousItem = cardArray[0];
				int count = 1;

				//????´?????????¨??????
				for (int lp = 1; lp < cardArray.Length; lp++)
				{
					//????????????
					int item = cardArray[lp];

					//???????????¨????????????= n + 1??§????????°
					if (previousItem + 1 == item)
					{
						//????????????????¢???????
						count++;
					}
					//!= n + 1??§????????°
					else
					{
						//?????¨?????? - ???????????§?¨???????????????????????????¢???????????????????????????
						int spaceCount = item - previousItem - 1;

						//????´??????°??????
						ChainNumber number = new ChainNumber (count, spaceCount);
						list.Add (number);

						//????????????
						count = 1;
					}

					previousItem = item;

					//????????????
					if (lp + 1 == cardArray.Length)
					{
						ChainNumber number = new ChainNumber (count, 0);
						list.Add (number);
					}
				}

				int loop = 0;
				int maxLength = 0;

				//?????¨??????????´?????????¨??????????????§?????????????±???????
				while (loop < list.Count)
				{
					//?????????
					int length = 0;
					int instanceWhiteCardCount = whiteCardCount;
					bool isPlus = false;

					//????´??????????????????????????´?????¶?????????????????????§
					while (loop < list.Count)
					{
						length += list[loop].Length;

						int needWhiteCardCount = list[loop].SpaceCount;

						//????????????????????????
						loop++;

						//????´?????¶?????????°
						if (needWhiteCardCount <= instanceWhiteCardCount)
						{
							//?¶????
							instanceWhiteCardCount -= needWhiteCardCount;
							length += needWhiteCardCount;

							isPlus = true;
						}
						else
						{
							//????????????????´?????????°?????´???
							if (isPlus)
							{
								loop--;
							}

							//?????????
							break;
						}
					}

					//????´?????????£???????????°
					if (0 < instanceWhiteCardCount)
					{
						length += instanceWhiteCardCount;
					}

					//??????
					if (maxLength < length)
					{
						maxLength = length;
					}
				}

				//output
				Console.WriteLine (maxLength);

			}
		}

		class ChainNumber
		{
			public ChainNumber ( int length,int spaceCount )
			{
				this.Length = length;
				this.SpaceCount = spaceCount;
			}

			public int Length { private set; get; }
			public int SpaceCount { private set; get; }
		}
	}
}

Measured memory_bytes: 13392
def count_cont(s)
    bef = ''
    cnt = 0
    s.each_char do |c|
        if c == bef
            cnt += 1
            bef = 'Z'
        else
            bef = c
        end
    end
    #puts cnt
    cnt
end

def aa
    s = gets.chomp
    k = gets.chomp.to_i

    if s.length == 1
        puts (k/2.0).floor
        exit
    end

    if s.split("").uniq.length == 1
        if s.length%2 == 0
            puts s.length/2 * k
        else
            if k == 1
                puts s.length/2
            elsif k%2 == 0
                puts k/2 * s.length
            else
                puts k/2 * s.length + s.length/2
            end
        end
        exit
    end

    cnt1 = count_cont(s)
    if k == 1
        puts cnt1
    elsif k%2 == 0
        cnt2 = count_cont(s+s)
        puts (k-1)*(cnt2-cnt1) + cnt1
    else
        cnt2 = count_cont(s+s)
        puts (k-1)*(cnt2-cnt1) + cnt1
    end
end

aa

Measured memory_bytes: 1788
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
# mod = 10 ** 9 + 7
mod = 998244353


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


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)


def main():
    N, S = read_values()
    A = read_list()

    dp = [(0, 0, 0) for _ in range(S + 1)]
    dp[0] = (1, 1, 1)

    for a in A:
        for s in range(S, -1, -1):
            d1 = dp[s][0]
            d2 = (dp[s][1] + (0 if s - a < 0 else dp[s - a][1]) + d1) % mod
            d3 = (dp[s][2] + (0 if s - a < 0 else dp[s - a][1]) + d2) % mod
            dp[s] = (d1, d2, d3)

    print((dp[S][2] - dp[S][1]) % mod)


if __name__ == "__main__":
    main()


Measured memory_bytes: 57308
// May this submission get accepted!

#pragma GCC optimize ("O3")
#pragma GCC target ("tune=native")
#pragma GCC target ("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define UNIQUE(v) do { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); } while (false)
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) {if (a > b) {a = b; return true;} return false; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) ((q) ? "Yes" : "No")
#define YES(q) ((q) ? "YES" : "NO")
#define Possible(q) ((q) ? "Possible" : "Impossible")
#define POSSIBLE(q) ((q) ? "POSSIBLE" : "IMPOSSIBLE")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) do { cerr << "[DEBUG] " #q ": ["; REP(i, (q).size()) { cerr << (q)[i] << (i == i_len-1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl; } while (false)
template<class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; }
template<class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
#define DANCE_ long
#define ROBOT_ unsigned
#define HUMAN_ signed
#define CHOKUDAI_ const
using  ll = DANCE_ HUMAN_ DANCE_;
using ull = DANCE_ ROBOT_ DANCE_;
using cll = DANCE_ DANCE_ CHOKUDAI_;
using  ld = long double;
using namespace std;

// モジュール


// 処理内容
int main() {
    
    ll n, k; cin >> n >> k;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    // dp[i][j] := jの番で石がi個の山が回ってきたとき先手(0)必勝かどうか
    vector<vector<bool>> dp(k+1, vector<bool>(2, false));

    // 動かせなくなったら負け
    REP(i, k) {
        dp[i][0] = false;
        dp[i][1] = true;
    }

    // 遷移
    REP(i, k+1) {
        REP(j, 2) {
            for (ll x : a) {
                if (i-x >= 0 && dp[i-x][!j] == !j) {
                    dp[i][j] = !j;
                }
            }
        }
    }

    cout << (dp[k][0] ? "First" : "Second") << endl;
    
}

Measured memory_bytes: 7296
import java.util.*;
class Main{
  public static void main(String[] args) {
    Scanner reader=new Scanner(System.in);
    long N=reader.nextLong();
    long K=reader.nextLong();
    long sum=0;
    final Long INF =(long)Math.pow(10, 9) + 7;
    for(long i=K;i<=N+1;i++)
    {
      sum+=(i*(N-i+1)+1);
      
    }
System.out.println(sum%INF);
  }
}


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


int main() {
    int A,B,C;
    cin>>A>>B>>C;
    int count =0;
    vector<int>data={A,B,C};
    sort(data.begin(),data.end());
    while(data[2]!=data[1]){
        data[1] +=1;
        data[0]+=1;
        count ++;
    }
    while(data[0]!=data[1]){
    data[0]+=2;
    count ++;
    if(data[1]==data[0]){
        cout<<count <<endl;
        return 0;
    }
    else if(data[0]>data[1]){
        cout<< count +1<<endl;
        return 0;
    }
    }
    cout<<count<<endl;
}

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

using namespace std;

int main(){
  int A, B;
  string S, T, U;
  cin >> S >> T;
  cin >> A >> B;
  cin >> U;
  if (U == S) A--;
  else B--;
  cout << A << " " << B << endl;
  return 0;
}

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

int main(void) {
	int N, d[101];
	cin >> N;

	int counter = 0;
	for (int i = 0; i < N; i++) {
		int tmp;
		cin >> tmp;
		if (d[tmp] != tmp) {
			d[tmp] = tmp;
			counter++;
		}
	}

	cout << counter << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
    int n,k;
    cin >> n >> k;
    int m = n+1-k;
    int h[n], a[m];
    rep(i, n) {
        cin >> h[i];
    }

    sort(h, h+n);

    rep(i, m) {
        a[i] = h[i+k-1] - h[i];
    }

    sort(a, a+m);

    cout << a[0];

    cout << endl;
    return 0;
}

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

int main()
{
    // スペース区切りの整数の入力
    int N, M;
    priority_queue<int> que;
    cin >> N >> M;
    int A[N];

    // 文字列の入力
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
        que.push(A[i]);
    }

    for (int k = 0; k < M; k++)
    {
        int max_price = que.top();
        que.push(max_price / 2);
        que.pop();
    }

    long long answer = 0;
    while (!que.empty())
    {
        answer += que.top();
        que.pop();
    }

    cout << answer << endl;
    return 0;
}



Measured memory_bytes: 1148
#include<iostream>
#include<array>
#include<algorithm>

using namespace std;

int rec(const array<int,16> &a,int d){
  if(count(begin(a),end(a),0)==16)return 0;
  if(d==0)return 9;
  int m=9;
  for(int i=0;i<16;i++){
    auto n=a;
    n[i]++;
    if(n[i]>=5){
      n[i]=0;
      int u,r,d,l;
      u=r=d=l=1<<i;
      for(;;){
	u=u>>4&0xFFFF;
	r=r<<1&0xEEEE;
	d=d<<4&0xFFFF;
	l=l>>1&0x7777;
	int b=u|r|d|l;
	if(!b)break;
	for(int i=0;i<16;i++){
	  if(b>>i&1&&n[i]){
	    n[i]+=(u>>i&1)+(r>>i&1)+(d>>i&1)+(l>>i&1);
	    if(n[i]>=5){
	      n[i]=0;
	      u|=1<<i;
	      r|=1<<i;
	      d|=1<<i;
	      l|=1<<i;
	    }else{
	      u&=~(1<<i);
	      r&=~(1<<i);
	      d&=~(1<<i);
	      l&=~(1<<i);
	    }	      
	  }
	}
      }
    }
    m=min(m,rec(n,d-1));
  }
  return m+1;
}

int main(){
  array<int,16> a;
  for(int i=0;i<16;i++){
    cin>>a[i];
  }
  auto r=rec(a,5);
  cout<<((r<=5)?r:-1)<<endl;
}

Measured memory_bytes: 1160
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>
#include <utility>

using namespace std;

using Weight=long long;
static const Weight INF=1LL<<57;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

bool has_cycle(const Graph &g) {
    size_t V=g.size();
    for (size_t i=0; i<V; ++i) {
        vector<bool> visited(V);
        stack<size_t> s; s.push(i);
        while (!s.empty()) {
            size_t v=s.top(); s.pop();
            visited[v] = true;

            for (const Edge &e: g[v]) {
                if (e.dst == i) return true;
                if (!visited[e.dst])
                    s.push(e.dst);
            }
        }
    }

    return false;
}

int main() {
    size_t V, E;
    scanf("%zu %zu", &V, &E);

    Graph g(V);
    for (size_t i=0; i<E; ++i) {
        size_t s, t;
        scanf("%zu %zu", &s, &t);

        connect(g, s, t, 1);
    }

    printf("%d\n", has_cycle(g));

    return 0;
}

Measured memory_bytes: 2772
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;
typedef long long ll;

ll x[26];

int main(){
  string s;
  cin >> s;
  fill(x, x+26, 0);

  ll len = s.length();
  ll su = len * (len-1) / 2 + 1;
  for(int i = 0; i < len; i++){
    x[s[i] - 'a']++;
  }
  for(int i = 0; i < 26; i++){
    su -= x[i] * (x[i] - 1) / 2;
  }

  cout << su << endl;

  return 0;
}


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

vector<long> ans;

void dfs(long n, int d) {
  if (d == 11) return;
  ans.push_back(n);
  int end = n % 10;
  int mi = max(0, end - 1);
  int mx = min(9, end + 1);
  for (int i = mi; i <= mx; i++) dfs(n * 10 + i, d + 1);
}

int main() {
  int K;
  cin >> K;
  for (int i = 1; i <= 9; i++) dfs(i, 1);
  sort(ans.begin(), ans.end());
  cout << ans.at(K - 1) << "\n";
}

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

# define pb push_back
# define fr first
# define sc second
# define mk make_pair

using namespace std;

const int inf = 1e9 + 7;
const int N = 1e6 + 5;

typedef long long ll;

int n, a[N], x[N], ans;

int main()
{
      cin >> n;

      for(int i = 1; i <= n; i ++)
      {
            cin >> a[i];
            x[a[i]] ++;
      }

      for(int i = 1; i <= 1e5; i ++)
      {
            ans = max(ans, x[i] + x[i - 1] + x[i + 1]);
      }

      cout << ans << endl;
}


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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ODD(x) (((x)&1)==0?(0):(1))
#define sign(x) ((x > 0) - (x < 0))
std::mt19937_64  generator(std::chrono::system_clock::now().time_since_epoch().count());

inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return a ? gcd(b%a, a): b; }
ll lcm(ll a, ll b) { return a / gcd(a,b) * b; }


void READ(){
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
    freopen ("in.txt", "r", stdin);
#endif
}

const int NMAX = 2500;
vii G[NMAX],RG[NMAX];
ll dist[NMAX];
int NN;
bool visited[2][NMAX];


void add_edge(int a, int b, int w = 0) {
    G[a].emplace_back(b, w);
    RG[b].emplace_back(a,w);
}

/// BELMAN FORD //
/// find shortest path from s to all other nodes,
/// there can be negative weights and negative cycles
/// use with sparse graphs O(V*E); if modified using Adj Matrix O(V^3)
void bellman_ford(int s) {
    dist[s] = 0;
    FOR(i,NN-1) {
        FOR(j,NN) {
            if(dist[j] != INFL) {
                for(auto it : G[j]) {
                    if (visited[0][it.first] && visited[1][it.first])
                        dist[it.first] = min(dist[it.first], dist[j] + it.second);
                }
            }
        }
    }
}
bool hasNegCycle() {
    FOR(j,NN) {
        if(dist[j] != INFL) {
            for(auto it : G[j]) {
                if (visited[0][it.first] && visited[1][it.first] && dist[it.first] > dist[j] + it.second) return true;
            }
        }
    }
    return false;
}

void dfs0(int node=0){
    visited[0][node]=1;
    for(ii c:G[node]){
        if (!visited[0][c.first])
            dfs0(c.first);
    }
}

void dfs1(int node=NN-1){
    visited[1][node]=1;
    for(ii c:RG[node]){
        if (!visited[1][c.first])
            dfs1(c.first);
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
  //  freopen("in.txt","r",stdin);
    int m,p;
    cin >> NN>>m>>p;
    fill(dist, dist + NN, INFL);
    FOR(i,m){
        int u,v,c;cin>>u>>v>>c;
        add_edge(u-1,v-1,p-c);
    }
    memset(visited,false,sizeof(visited));
    dfs0();
    dfs1();
    bellman_ford(0);
    cout << (hasNegCycle()?-1:max(0LL,-dist[NN-1]));

}


Measured memory_bytes: 768
#include<stdio.h>
#include<algorithm>
using namespace std;
int d[6];
int v[30];
int main(){
	int a,b;
	while(scanf("%d%d",&a,&b),b){
		v[0]=a;
		int i=0; 
		bool ok=false;
		while(!ok){
			int c=v[i];
			for(int j=0;j<b;j++){
				d[j]=c%10;
				c/=10;
			}
			i++;
			std::sort(d,d+b);
			int p=0;
			int q=0;
			for(int j=0;j<b;j++){
				p*=10;q*=10;
				p+=d[j];
				q+=d[b-1-j];
			}
			v[i]=q-p;
			for(int j=0;j<i;j++)if(v[j]==v[i]){
				printf("%d %d %d\n",j,v[j],i-j);
				ok=true;
				break;
			}
		}
	}
}

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

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

#define all(x) (x).begin(),(x).end()

bool check(string s) {
  for (int i = 1; i < s.size(); i++) {
    if (s[i] != '9') return 0;
  }
  return 1;
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  string s;
  cin >> s;
  int ans = (s[0] - '0' - 1) + (s.size() - 1) * 9;
  if (check(s)) ans++;
  cout << ans << '\n';
}


Measured memory_bytes: 3664
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
	int n;
	cin >> n;
	int k;
	cin >> k;
	vector<ll> vec(n+1);
	for(int i=0; i<n; i++)
	{
		cin >> vec[i];
	}
	vector<ll> dp(n);
	dp[0] = 0;
	// dp[1] = abs(vec[1]-vec[0]);
	for(int i=1;i<k;i++)
	{
		ll val=INT_MAX;
		for(int j=0;j<i;j++)
			val = min(val, abs(vec[i]-vec[j]) + dp[j]);
		dp[i] = val;
	}
	for(int i=k;i<n;i++)
	{
		ll val = INT_MAX;
		for(int j=i-k;j<i;j++)
			val = min(val, abs(vec[i]-vec[j]) + dp[j]);
		
		dp[i] = val;
	}

	// int ans = findans(vec,n-1, dp);
	// for(int i=0;i<n;i++)
	// 	cout << dp[i] << " ";
	// cout << endl;
	cout << dp[n-1] << endl;
	return 0;
}

Measured memory_bytes: 1792
#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<math.h>
#include<algorithm>
#include<iomanip>
#include<bitset>
using namespace std;

int main() {

	long long N;
	cin >> N;

	long long sum = N * (N + 1) / 2;

	long long val3 = N / 3;
	long long sum3 = 3 * val3 * (val3 + 1) / 2;

	long long val5 = N / 5;
	long long sum5 = 5 * val5 * (val5 + 1) / 2;

	long long val15 = N / 15;
	long long sum15 = 15 * val15 * (val15 + 1) / 2;

	long long ans = sum - sum3 - sum5 + sum15;

	cout << ans << endl;

	return 0;
}



Measured memory_bytes: 3616
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
int main() {
  int a,b;
  cin >> a >> b;
  if(a>9||b>9) {
    cout << -1 << endl;
  }
  else {
    cout << a*b << endl;
  }
}

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

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >>d;
  cout << min(a, b) + min(c, d) << endl;
  return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#define MAX 1000000

int p[MAX];

void prime_number(int p[]){
	int i,j;
	for(i=0;i<MAX;i++)p[i]=0;
	p[0]=1;
	p[1]=1;
	//ツ素ツ青板：0
	for(i=2;i<=MAX/2;i++){//ツ倍ツ青板づ債渉慊つュ
		for(j=2;i*j<=MAX;j++){
			if(p[i*j]==0){
				p[i*j]=1;
			}
		}
	}
	return;
}
int main(void){
	int i,j;
	int n,cnt;

	prime_number(p);
	while(scanf("%d",&n)!=EOF){
		cnt=0;
		for(i=2;i<=n;i++){
			if(p[i]==0){
				cnt++;
			}
		}
		printf("%d\n",cnt);	
	}
	return 0;
}

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

constexpr unsigned int MAX_N = 50'000;
unsigned int A[MAX_N + 1];

int main(void){
    memset(A, -1, MAX_N * 4 + 4); A[0] = 0;
    unsigned int n, m; scanf("%u%u", &n, &m);
    while(m--){
        unsigned int v; scanf("%u", &v); if(v > n) continue;
        for(unsigned int i = v, j = 0; i <= n; ++i, ++j){
            if(A[j] == -1) continue;
            if(A[i] > A[j] + 1) A[i] = A[j] + 1;
        }
    }
    printf("%u\n", A[n]);

    return 0;
}


Measured memory_bytes: 2952
#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
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; }
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define DEBUG(...)
#endif

int main(int argc, const char * argv[]) {
  std::cout << std::fixed << std::setprecision(15);

  int N, M; cin >> N >> M;
  vector<int> v(N);
  rep(i, M){
    int a, b; cin >> a >> b;
    a--; b--;
    v[a]++;
    v[b]++;
  }

  bool ans = true;
  rep(i, N){
    if(v[i] % 2 == 1) ans = false;
  }

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


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

int main() {

  int a;
  int ans = 0;
  for(int i=0; i<5; i++) {
    cin >> a;
    if( a < 40 ) ans += 40;
    else ans += a;
  }

  cout << ans / 5 << endl;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
using namespace         std;
#define                 int long long
#define                 pb push_back
#define                 mp make_pair
#define st(arr,a)       memset(arr,a,sizeof arr)
#define                 nl cout<<endl
#define                 endl '\n'              
#define bitcount(x)     __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i,a,b)     for(int i=a;i<=b;i++)
#define rfor(i,a,b)     for(int i=a;i>=b;i--)
#define all(x)          x.begin() , x.end()
#define pi pair<int,int>
#define X first
#define Y second
#define N 1014159
#define  vi vector<int>
#define  v  vector
using namespace __gnu_pbds;  
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update> oset; 
gp_hash_table<int, int> table;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {
    int operator()(int x) const { return x ^ RANDOM; }
};


int arr[N];
int n,l;
int nex[N][20];

void solve()
{
    cin>>n;
    forn(i,1,n)
    {
        cin>>arr[i];
    }
    
    int last = n;
    cin>>l;
    rfor(i,n-1,0)
    {
        while(arr[last] - arr[i] > l)
        {
            last--;
        }
        nex[i][0] = last;
    }
    nex[n][0] = n;
    
    ///forn(i,1,n)cout<<nex[i][0]<<" ";nl;
    
    
    forn(i,1,17)forn(j,0,n)nex[j][i] = nex[nex[j][i-1]][i-1];
    
    int q;
    cin>>q;
    while(q--)
    {
        int a,b;
        cin>>a>>b;
        if(a>b)swap(a,b);
        int now = a;
        int ans = 1;
        rfor(i,17,0)
        {
            if(nex[now][i]<b)
            {
                now = nex[now][i];
                ans+=1LL<<i;
            }
        }
        cout<<ans<<"\n";
        
    }
    
    
    
}


signed main() 
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); 
    int t = 1;
    //cin>>t;
    while(t--)
    {
        solve();
    }



    return 0;

}

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

Measured memory_bytes: 19328
//void _(){
//    int n;
//    cin >> n;
//    vi arr(n+1);
//    cin >> arr;
//    map<int,int> cnt;
//    for(int x : arr)
//        ++cnt[x];
//    int dbl = 1;
//    while(cnt[dbl] == 1)
//        ++dbl;
//    int l = 0;
//    int r = n;
//    while(arr[l] != dbl) ++l;
//    while(arr[r] != dbl) --r;
//    vl ans(n+2);
//    Factorial f(n+5);
//    for(int s = 1; s <= n+1; ++s){
//        ans[s] += f.comb(n+1,s);
//        int extra = n+1 - (r-l+1);
//        if(s >= 1 && s-1 <= extra)
//            ans[s] -= f.comb(extra,s-1);
//        ans[s] = (ans[s]%MOD+MOD)%MOD;
//    }
//    print(vl(ans.begin()+1,ans.end()),"\n");
//}
//
#include <map>
#include <iomanip>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
const ll MOD = 1e9 + 7;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
T1 T ext_euclid(T a, T b, T c, T d){
    if(c == T(0))
        return b;
    return ext_euclid(c, d, a % c, b - a / c * d);
}
T1 T mod_inverse(T x, T mod = T(MOD)){
    return (ext_euclid(mod, T(0), x, T(1)) + mod) % mod;
}
ll mod_inverse(int x, ll mod = MOD){
    return mod_inverse<ll>(x,mod);
}
struct Factorial{
    vl fact,ifact;
    ll comb(ll n, ll m){
        assert(n >= m);
        assert(n < sz(fact));
        return fact[n]*ifact[m]%MOD*ifact[n-m]%MOD;
    }
    Factorial(int n){
        fact.resize(n+1);
        ifact.resize(n+1);
        fact[0] = 1;
        for(int i = 1; i <= n; ++i)
            fact[i] = fact[i-1]*i%MOD;
        ifact[n] = mod_inverse(fact[n]);
        for(int i = n-1; i >= 0; --i)
            ifact[i] = (i+1)*ifact[i+1]%MOD;
    }
};
T1 ostream& print(T b, T e, string sep = " ", ostream& stream = cout){
    for(T it = b; it != e; ++it){
        stream << *it;
        if(it + 1 != e)
            stream << sep;
    }
    return stream;
}
T1 void print(vector<T> arr, string sep = " "){
    if(arr.empty()){
        return;
    }
    print(arr.begin(), arr.end(), sep);
    cout << '\n';
}
void _(){
    int n;
    cin >> n;
    vi arr(n+1);
    cin >> arr;
    map<int,int> cnt;
    for(int x : arr)
        ++cnt[x];
    int dbl = 1;
    while(cnt[dbl] == 1)
        ++dbl;
    int l = 0;
    int r = n;
    while(arr[l] != dbl) ++l;
    while(arr[r] != dbl) --r;
    vl ans(n+2);
    Factorial f(n+5);
    for(int s = 1; s <= n+1; ++s){
        ans[s] += f.comb(n+1,s);
        int extra = n+1 - (r-l+1);
        if(s >= 1 && s-1 <= extra)
            ans[s] -= f.comb(extra,s-1);
        ans[s] = (ans[s]%MOD+MOD)%MOD;
    }
    print(vl(ans.begin()+1,ans.end()),"\n");
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        _();
}


Measured memory_bytes: 9472
#include <bits/stdc++.h>
#define mp make_pair
#define sqr(x) (x)*(x)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn=100007;
const ll inf=0x3f3f3f3f;
const int mod=998244353;
#define rep(i,x,y) for(auto i=(x);i<=(y);++i)
#define dep(i,x,y) for(auto i=(x);i>=(y);--i)
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int a[maxn],b[maxn];
int main(){
    int n=read(),m=read();
    rep(i,1,n) a[i]=read(),b[i]=1; 
    rep(i,1,m){
        int u=read(),v=read();
        b[u]&=a[v]<a[u];
        b[v]&=a[u]<a[v];
    }
    int ans=0;
    rep(i,1,n) ans+=b[i];
    cout<<ans;
    return 0;
}

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

int main()
{
    int N;
    char S[10000];
    cin >> N >> S;

    int count = strlen(S);

    for (int i=0; i<count; i++)
    {
        S[i] = S[i] + N;

        if(S[i] > 0x5A)
            S[i] = S[i] -26;
    }
    
    cout << S;

    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
using namespace std;
long long DIVISOR = 1000000007;

long long own_product(int n){
    long long acc = 1;
    for (long long i=1;i<n+1;i++){
        acc = acc % DIVISOR;
        acc *= i;
    }
    return acc % DIVISOR;
}

int main(){
    int n, m;
    cin >> n >> m;
    int bigger, smaller;
    if (n >= m){
        bigger = n;
        smaller = m;
    } else {
        bigger = m;
        smaller = n;
    }
    if (bigger - smaller >= 2){
        cout << 0 << endl;
    } else if (bigger - smaller == 1) {
        cout << (own_product(bigger) * own_product(smaller)) % DIVISOR << endl;
    } else {
        cout << 2 *(own_product(bigger) * own_product(smaller)) % DIVISOR << endl;
    }
    return 0;
    
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<numeric>
#include<queue>
#include<stack>
#include<cstring>
#include<limits>
#include<functional>
#include<unordered_set>
#include<iomanip>
#include<cassert>
#define rep(i,a) for(int i=(int)0;i<(int)a;++i)
#define pb push_back
#define eb emplace_back
using ll=long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 50;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
using namespace std;

void solve(){
    string s;
    cin>>s;
    int n=s.size();
    string t;
    set<string>ss;
    ss.insert("dream");
    ss.insert("dreamer");
    ss.insert("erase");
    ss.insert("eraser");
    for(int i=n-1;i>=0;--i){
        if(s[i]=='m'||s[i]=='e'){
            if(i-4<0){
                cout<<"NO\n";
                return;
            }
            t=s.substr(i-4,5);
            if(!ss.count(t)){
                cout<<"NO\n";
                return;
            }
            i-=4;
        }
        else if(s[i]=='r'){
            if(i-5<0){
                cout<<"NO\n";
                return;
            }
            if(s[i-5]=='e'){
            t=s.substr(i-5,6);
            if(!ss.count(t)){
                cout<<"NO\n";
                return;
            }
            i-=5;
            }
            else {
                if(i-6<0){
                cout<<"NO\n";
                return;
            }
            t=s.substr(i-6,7);
            if(!ss.count(t)){
                cout<<"NO\n";
                return;
            }
            i-=6;
            }
        }
    }
    cout<<"YES\n";
}
 
signed main(){
	ios::sync_with_stdio(false);
    cin.tie(0);
	cout<<fixed<<setprecision(15);
	solve();
	return 0;
}


Measured memory_bytes: 512