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
#include <stdio.h>
#include <iostream>
#include <algorithm>


using namespace std;


typedef long long int  lli;


static const int MAX_SIZE = 200002;


struct Buff
{
    lli m_a[MAX_SIZE];
    lli m_t[MAX_SIZE];
    int m_iSize;

    Buff() : m_iSize(0) {}
};


struct Oven
{
    int m_iTime;
    lli m_iResult;
};


static int iPeople, iOvens, iBuff;

static Buff aBuffs[2];

static Oven aOvens[MAX_SIZE];

static int aOvensOrder[MAX_SIZE];


static bool CompareOvens(int lhs, int rhs)
{
    return aOvens[lhs].m_iTime > aOvens[rhs].m_iTime;
}


static void ReadData()
{
    scanf("%d", &iPeople);
    scanf("%d", &iOvens);

    Buff & buff = aBuffs[0];

    iBuff = 0;

    for (int i = 0; i < iPeople; ++i)
    {
        lli iEnter;

        scanf("%lld", &iEnter);

        buff.m_a[buff.m_iSize++] = iEnter;
    }

    for (int i = 0; i < iOvens; ++i)
    {
        int d;

        scanf("%d", &d);

        aOvens[i].m_iTime = d;

        aOvensOrder[i] = i;
    }

    sort(aOvensOrder, aOvensOrder + iOvens, CompareOvens);
}


static void Solve11(int iSrcBuff, int iOven)
{
    lli    iFreeOvenStartTime = 0;
    lli    iTotal = 0;
    lli    iArrival, iWait;
    Buff & srcBuff = aBuffs[iSrcBuff];
    Buff & dstBuff = aBuffs[1 - iSrcBuff];
    int    d = aOvens[iOven].m_iTime;

    dstBuff.m_iSize = 0;

    for (int i = 0; i < srcBuff.m_iSize; ++i)
    {
        iArrival = srcBuff.m_a[i];

        if (iFreeOvenStartTime + d <= iArrival)
        {
            if (dstBuff.m_iSize > 0 && dstBuff.m_t[dstBuff.m_iSize - 1] == 0)
            {
                dstBuff.m_iSize--;
            }

            iFreeOvenStartTime = iArrival;

            dstBuff.m_t[dstBuff.m_iSize]   = 0;
            dstBuff.m_a[dstBuff.m_iSize++] = iArrival;
            continue;
        }

        iFreeOvenStartTime += d;

        iWait = iFreeOvenStartTime - iArrival;

        iTotal += iWait;

        dstBuff.m_t[dstBuff.m_iSize]   = iWait;
        dstBuff.m_a[dstBuff.m_iSize++] = iArrival;
    }

    aOvens[iOven].m_iResult = iTotal;
}


static void Solve()
{
    lli iPreviousResult;
    int iPreviousTime;

    for (int i = 0; i < iOvens; ++i)
    {
        int iOrder = aOvensOrder[i];

        if (i > 0 && aOvens[iOrder].m_iTime == iPreviousTime)
        {
            aOvens[iOrder].m_iResult = iPreviousResult;
            continue;
        }

        Solve11(iBuff, iOrder);

        iBuff = 1 - iBuff;

        iPreviousResult = aOvens[iOrder].m_iResult;
        iPreviousTime   = aOvens[iOrder].m_iTime;
    }
}


static void WriteResults()
{
    for (int i = 0; i < iOvens; ++i)
    {
        printf("%d", aOvens[i].m_iResult);

        if (i < iOvens - 1)
        {
            printf("\n");
        }
    }
}


int main(int argc, char * argv[])
{
    ReadData();

    Solve();

    WriteResults();

	return 0;
}