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
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <set>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>

using namespace std;
#define LONG long long
//#define LONG __int64

vector<LONG> vecClients;
vector<LONG> vecOvens;
vector<LONG> vecMulti;
map<LONG, LONG> mapOvenWait;
map<LONG, LONG, greater<LONG>> mapOvenQueue;

void Process()
{
   LONG lQueue         = 0LL;
   LONG lMultiplicator = 1LL;
   LONG distanceVoyager = 0LL;
   LONG shift           = 0LL;
   bool bMulti         = false;
   vector<LONG>::iterator itClient = vecClients.begin();
   vector<LONG>::iterator CLsentinel = vecClients.end();
   vector<LONG>::iterator itMulti = vecMulti.begin();
   map<LONG, LONG, greater<LONG>>::iterator itOven = mapOvenQueue.begin();
   map<LONG, LONG, greater<LONG>>::iterator OVsentinel = mapOvenQueue.end();

   while(itClient != CLsentinel)
   {
      itOven = mapOvenQueue.begin();
      if(*itMulti > 1L)
      {
         bMulti = true;
         lMultiplicator = (*itMulti * (1LL + *itMulti)) >> 1;
      }
      else
      {
         bMulti = false;
      }

      while(itOven != OVsentinel)
      {
         distanceVoyager = *itClient - (itOven->second + itOven->first);
         if(distanceVoyager > 0LL)
            shift = -itOven->first;
         else
            shift = itOven->second - *itClient;
         
         if(bMulti)
         {
            lQueue = (itOven->first * lMultiplicator) + (*itMulti * shift);
            itOven->second = *itMulti * itOven->first + shift;
            mapOvenWait[itOven->first] += lQueue;
            ++itOven;
         }
         else
         {
            lQueue = shift + itOven->first;

            if(lQueue > 0LL)
            {
               itOven->second = lQueue;
               mapOvenWait[itOven->first] += lQueue;
               ++itOven;
            }
            else
            {
               while(itOven != OVsentinel && itOven->second)
               {
                  itOven->second = 0LL;
                  ++itOven;
               }
               break;
            }
         }
      }
      ++itClient;
      ++itMulti;
   }
}

void Print()
{
   vector<LONG>::iterator ov = vecOvens.begin();
   vector<LONG>::iterator se = vecOvens.end();
   while(ov != se)
   {
      printf("%lld\n", mapOvenWait[*ov]);
      ++ov;
   }
}

int main() {

   long nClients = 0L;
   long nOvens   = 0L;
   LONG currOven = 0L;
   LONG prevClient = 0LL;
   LONG currClient = 0LL;
   LONG diff       = 0LL;

   scanf("%ld", &nClients); 
   scanf("%ld", &nOvens);

   vecClients.reserve(nClients);
   vecMulti.reserve(nClients);
   vecOvens.reserve(nOvens);
   long left = nClients;

   scanf("%lld", &prevClient);
   vecClients.push_back(prevClient);
   vecMulti.push_back(1L);
      
   while(--left) 
   {
      scanf("%lld", &currClient);
      if(currClient == prevClient)
      {
         vecMulti.back() += 1L;
      }
      else
      {
         vecClients.push_back(currClient - prevClient);
         vecMulti.push_back(1L);
         prevClient = currClient;
      }
   } 

   left = nOvens;
   do 
   {
      scanf("%lld", &currOven);
      vecOvens.push_back(currOven);
      mapOvenWait[currOven] = 0LL;
      mapOvenQueue[currOven] = 0LL; 
   } while (--left);

   Process();

   Print();
   
   return 0;
}