//#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <unistd.h>
#define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL)
#define all(v) (v).begin(),(v).end()
using namespace std;

typedef long long Long;
typedef long double Double;
typedef unsigned long long ULong;
typedef pair<Long, Long> Pair;

const int N = 3e5;
const Long INF = 1e18;

struct FenwickTree {
  Long tree[N + 1];
  void Clear(int n) {
    for (int i = 0; i <= n; i++) tree[i] = 0;
  }
  void Update(int i, Long delta) {
    while (i <= N) {
      tree[i] += delta;
      i += (i & -i);
    }
  }
  Long Query(int i) {
    Long sum = 0;
    while (i > 0) {
      sum += tree[i];
      i -= (i & -i);
    }
    return sum;
  }
} ft;


void Solve(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]++;
  }
  Long ans = 0;
  vector<Long> cnt(n + 1, 0);
  for (int i = n - 1; i >= 0; i--) {
    cnt[a[i]] = ft.Query(a[i]);
    ans += cnt[a[i]];
    ft.Update(a[i] + 1, 1);
  }

  ft.Clear(n);
  for (int i = 0; i < n; i++) {
    cout << ans << '\n';
    ans -= (cnt[a[i]] + ft.Query(a[i]));
    ans += (n - a[i]);
    ft.Update(a[i] + 1, 1);
  }
} 

int main(void) {
  FAST_IO;
  int t = 1;
  //cin >> t;
  while (t--) Solve();
  return 0;
}

  #include<bits/stdc++.h> 
  #define fp(x,a,b) for(int x = a; x < b; x++)
  #define fn(x,a,b) for(int x = a; x > b; x--)
  #define f(x, m) for(auto x : m)
  #define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
  #define pb push_back
  #define pii pair<int,int>
  #define pll pair<ll, ll>
  #define vi vector<int>
  #define vl vector<ll>
  #define vii vector<pair<int ,int>>
  #define vll vector<pair<ll ,ll>>
  #define all(v) v.begin(),v.end()
  #define sor(a) sort( a.begin(), a.end() )
  #define ros(a) sort( a.rbegin(), a.rend())
  #define prec(n) fixed<<setprecision(n)
  #define ff first
  #define ss second
  // #define tt third
  #define print(x) for(auto it:x) cout<<it<<" ";
  #define debug(x) cerr << #x << " is " << x << endl;
  #define w(x) while(x--)
  #define r(x) scanf("%d", &x)
  #define rl(x) scanf("%ld", &x) 
  #define rll(x) scanf("%lld", &x)
  typedef long long ll;
  using namespace std;
  // mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
  // template <typename TYPE> void readint(TYPE &x){
  // x=0; int f=1; char c;
  // for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
  // for(;isdigit(c);c=getchar())x=x*10+c-'0';
  // x*=f;
  // }
  #define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  void err(istream_iterator<string> it) {}
  template<typename T, typename... Args>
  void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
  }  
  template<typename... T>
  void rd(T& ... args){
    ((cin >> args), ...);
  }
  template<typename... T>
  void ps(T&& ... args){
    ((cout << args << ' '), ...);
    cout<<"\n";
  }
  const ll MOD = 1e9 + 7, MAX = 1e5 + 5;
  const char nl = '\n';
  const ll INF = 2e9 + 5;
  int n;
  ll k, ans;
  ll a[MAX];
  void solve(){
    rd(n, k);
    int t = 0;
   while(n--){
    cin >> a[t++];
     if(a[t] > k) t--;
   }
   // ps(t, k);
   int x = t / 2;
   vl v1, v2;
   fp(i, 0, 1 << x){
    ll ga = 0LL;
   
    fp(j, 0, x){
      if(i & (1<<j)) ga += a[j];
      
    } 
    v1.pb(ga);
  } 
  // print(v1);
   fp(i, 0, 1 << (t - x)){
    ll ga = 0LL;
   
    fp(j, 0, t - x){
      if(i & (1<<j)) ga += a[j+x];
      
    } 
    v2.pb(ga);
  } 
   sor(v2);
  // print(v2);
  f(it, v1){
    auto xd = upper_bound(all(v2), k - it);
    if(xd != v2.begin()){
    xd--;
    if(*xd + it <= k) ans = max(*xd + it, ans);
   }
  }
  ps(ans);
  }
  int main(){
  cpu();
  // int __;
  //  cin>>__;
  //  while(__--){
  //   solve();
  //   ps(" ");
  //  }

  solve();
  // cout<<nl;

  return 0;
  }
