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
#include <iostream>
#include <list>
#include <vector>
#include <set>

using namespace std;

long long GlobalCount = 0;

struct block
{
    long long position = 0;
    long long count = 0;
    long long affectNextOn = 0;

    bool operator<(const block& other) const
    {
        if (affectNextOn == other.affectNextOn)
            return position < other.position;

        return affectNextOn < other.affectNextOn;
    }
};

long long GetBlockWeight(long long n)
{
    return ((n + 1) * n) / 2;
}

long long CalculateNewCount(long long first, long long second)
{
    long long sum = first + second + 1;
    long long firstWeight = GetBlockWeight(first);
    long long secondWeight = GetBlockWeight(second);
    long long sumWeight = GetBlockWeight(sum);

    GlobalCount += sumWeight - firstWeight - secondWeight;

    return sum;
}

struct iteratorCmp
{
    bool operator()(const list<block>::iterator& lhs, const list<block>::iterator& rhs) const {
        return (*lhs) < (*rhs);
    }
};

set<list<block>::iterator, iteratorCmp> executionSet;

long long CalculateAffectNext(long long iteration, block& current, block& next)
{
    long long distance = next.position - current.position;

    if ((distance % (current.count + 1)) == 0)
        return distance / (current.count + 1);

    return distance / (current.count + 1) + 1;
}

long long furnanceCost[1000011];

bool IsCurrentAffectNext(long long iteration, block& current, block& next)
{
    return current.position + (current.count + 1) * iteration >= next.position;
}

long long GetMergeCost(long long iteration, block& current, block& next)
{
    return (next.count + 1) * (current.position + (current.count + 1) * iteration - next.position);;
}

int main()
{
    int n = 0;
    int m = 0;

    cin >> n;
    cin >> m;


    //wczytywanie klientów

    list<block> blocks(n);
    for (auto& item : blocks)
    {
        cin >> item.position;
    }

    //add first element
    blocks.push_front(block());

    //wczytywanie pieców

    vector<long long> furnances(m);
    long long maxFurnance = 0;
    for (auto& furnance : furnances)
    {
        cin >> furnance;
        if (furnance > maxFurnance)
            maxFurnance = furnance;
    }

    //join 0 distance
    long long iteration = 0;
    furnanceCost[iteration] = 0;


    for (auto it = blocks.begin(); it != blocks.end(); it++)
    {
        auto next = it;
        while (++next != blocks.end() && it->position == next->position)
        {
            it->count = CalculateNewCount(it->count, next->count);
            blocks.erase(next);
            next = it;
        }

        if (next == blocks.end())
            break;

        it->affectNextOn = CalculateAffectNext(iteration, *it, *next);
        executionSet.insert(it);
    }

    for (iteration = 1; iteration <= maxFurnance; iteration++)
    {
        furnanceCost[iteration] = furnanceCost[iteration - 1] + GlobalCount;

        for (auto it = executionSet.begin(); it != executionSet.end() && (*it)->affectNextOn == iteration; it = executionSet.begin())
        {
            auto current = *it;
            auto next = *it;

            executionSet.erase(current);

            while (++next != blocks.end() && IsCurrentAffectNext(iteration, *current, *next))
            {
                //remove next from execution Plan
                executionSet.erase(next);

                //add cost of merging two elements
                furnanceCost[iteration] += GetMergeCost(iteration, *current, *next);

                //calculate count of all elements
                current->count = CalculateNewCount(current->count, next->count);

                //remove next
                blocks.erase(next);
                next = current;
            }

            if (next == blocks.end())
                continue;

            current->affectNextOn = CalculateAffectNext(iteration, *current, *next);
            executionSet.insert(current);
        }
    }

    for (auto& furnance : furnances)
    {
        cout << furnanceCost[furnance] << endl;
    }

    return 0;
}