#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; template class SWAG{ private: struct node{ T val,sum; node(const T& val, const T& sum):val(val),sum(sum){} }; using F = function; stack front_stack,back_stack; F op; public: SWAG(const F op):op(op),front_stack(),back_stack(){}; void push(const T& x){ if(back_stack.empty()) back_stack.emplace(x,x); else{ T s{op(back_stack.top().sum,x)}; back_stack.emplace(x,s); } } void pop(){ if(front_stack.empty()){ front_stack.emplace(back_stack.top().val,back_stack.top().val); back_stack.pop(); while(!back_stack.empty()){ T s{op(back_stack.top().val,front_stack.top().sum)}; front_stack.emplace(back_stack.top().val,s); back_stack.pop(); } } front_stack.pop(); } T result(){ if(front_stack.empty()) return back_stack.top().sum; else if(back_stack.empty()) return front_stack.top().sum; else return op(front_stack.top().sum,back_stack.top().sum); } bool empty(){ return front_stack.empty() && back_stack.empty(); } }; using P = pair; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,K; cin >> N >> K; vec A(N); for(auto& x:A) cin >> x; auto op = [](int a,int b){return min(a,b);}; SWAG swag(op); map lsum,cnt; ll ans = 0; int r = 0; int l = 0; queue Q; int inf = 2e9; while(lswag.result()){ while(!Q.empty()){ int x = Q.front(); Q.pop(); lsum[A[x]] += x-1; cnt[A[x]]++; if(cnt.count(K-A[x])) ans += cnt[K-A[x]]*x-lsum[K-A[x]]; } } r++; }else break; } if(r==N){ while(!Q.empty()){ int x = Q.front(); Q.pop(); lsum[A[x]] += x-1; cnt[A[x]]++; if(cnt.count(K-A[x])) ans += cnt[K-A[x]]*x-lsum[K-A[x]]; } } while(!swag.empty()){ bool remove = A[l]==swag.result(); swag.pop(); lsum[A[l]] -= l-1; cnt[A[l]]--; if(cnt[A[l]]==0){ lsum.erase(A[l]); cnt.erase(A[l]); } l++; if(remove) break; } while(!Q.empty()){ int x = Q.front(); Q.pop(); lsum[A[x]] += x-1; cnt[A[x]]++; if(cnt.count(K-A[x])) ans += cnt[K-A[x]]*x-lsum[K-A[x]]; } } cout << ans << "\n"; }