1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;

namespace {

struct Client {
  int first;
  int count = 1;
  mutable Client* parent = this;

  explicit Client(int id): first{id}
  {
  }

  Client* find() const
  {
    if (parent != this) parent = parent->find();
    return parent;
  }

  friend Client* merge(Client* a, Client* b)
  {
    a = a->find();
    b = b->find();
    if (a == b) return a;
    if (a->count < b->count) swap(a, b);
    b->parent = a;
    a->first = min(a->first, b->first);
    a->count += b->count;
    return a;
  }

  friend ostream& operator<<(ostream& os, Client const& c)
  {
    auto cc = c.find();
    return os << cc->first << "…" << cc->first + cc->count - 1 << " (" << cc->waiting() << ")";
  }

  long long waiting() const
  {
    return static_cast<long long>(count - 1) * count / 2;
  }
};

struct Oven {
  int time;
  long long result;
};

constexpr auto inf = numeric_limits<long long>::max();

template <typename A, typename B>
constexpr typename common_type<A, B>::type div_up(A a, B b)
{
  return (a + b - 1) / b;
}

template <typename T> using pq = priority_queue<T, vector<T>, greater<T>>;

void solve(vector<long long> times, vector<Oven>& ovens_)
{
  times.insert(times.begin(), 0);

  int n = times.size();
  vector<Client> clients;
  clients.reserve(n);
  for (int i = 0; i < n; ++i) {
    clients.emplace_back(i);
  }

  vector<long long> slack(n, inf);
  pq<pair<long long, int>> slacks;

  auto set_slack = [&](int i, long long s)
  {
    if (slack[i] == s) return;
    slack[i] = s;
    slacks.push({s, i});
  };

  for (int i = 1; i < n; ++i) {
    set_slack(i, times[i] - times[i - 1]);
  }

  long long result = 0;
  auto update = [&](Client* c, int time) {
    int l = c->first;
    int r = c->first + c->count;
    if (r >= n) return;
    auto end = times[l] + static_cast<long long>(c->count - 1) * time;
    auto left = max(0LL, times[r] - time - end);
    set_slack(r, time + div_up(left, c->count));
  };

  auto update2 = [&](Client* c, int time) {
    int l = c->first;
    int r = c->first + c->count;
    if (r >= n) return;
    auto d = clients[r].find();
    auto end = times[l] + static_cast<long long>(c->count - 1) * time;
    auto left = times[r] - time - end;
    if (left < 0) {
      result += -left * d->count;
    }
  };

  vector<Oven*> ovens(ovens_.size());
  iota(ovens.begin(), ovens.end(), ovens_.data());
  sort(ovens.begin(), ovens.end(), [](Oven const* lhs, Oven const* rhs) { return lhs->time < rhs->time; });

  long long waiting = 0;
  int last_time = 0;

  auto jump_time = [&](int time)
  {
    if (time == last_time) return;
    result += waiting * (time - last_time);
    last_time = time;
  };

  for (Oven* o: ovens) {
    while (!slacks.empty() && slacks.top().first <= o->time) {
      auto t = slacks.top().first;
      auto i = slacks.top().second;
      slacks.pop();
      if (clients[i].find()->first != i || slack[i] != t) continue;
      jump_time(t);
      auto a = clients[i - 1].find();
      auto b = clients[i].find();
      update2(a, t);
      waiting -= a->waiting();
      waiting -= b->waiting();
      auto c = merge(a, b);
      update(c, t);
      waiting += c->waiting();
    }
    jump_time(o->time);
    o->result = result;
  }
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;

  vector<long long> times(n);
  for (auto& t: times) cin >> t;

  vector<Oven> ovens(m);
  for (auto& o: ovens) cin >> o.time;

  solve(move(times), ovens);

  for (auto const& o: ovens) {
    cout << o.result << '\n';
  }

  return 0;
}