P3817 小A的糖果


// Problem: P3817 小A的糖果
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3817
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include 

using namespace std;

template
void debugVector(const T &a) {
    cout << "[ ";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << a[i] << (i == a.size() - 1 ? " " : ", ");
    }
    cout << "]" << endl;
}

template
void debugMatrix2(const T &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        debugVector(a[i]);
    }
}

template
using matrix2 = vector>;

template
vector> getMatrix2(size_t n, size_t m, T init = T()) {
    return vector>(n, vector(m, init));
}

template
using matrix3 = vector>>;

template
vector>> getMatrix3(size_t x, size_t y, size_t z, T init = T()) {
    return vector>>(x, vector>(y, vector(z, init)));
}

vector genBigInteger(string a) {
    vector res;
    for (int i = a.size() - 1; i >= 0; --i) {
        res.push_back(a[i] - '0');
    }
    return res;
}

void printBigInteger(vector a) {
    for (size_t i = a.size() - 1; i >= 0; --i) {
        cout << a[i];
    }
}

vector addBigInteger(vector a, vector b) {
    vector res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i) {
        if (i < a.size()) pre += a[i];
        if (i < b.size()) pre += b[i];
        res.push_back(pre % 10);
        pre /= 10;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, x;
    cin >> n >> x;
    vector info(n);
    
    for (int i = 0; i < n; ++i) {
        cin >> info[i];
    }
    
    long long cnt = 0;
    for (int i = 1; i < n; ++i) {
        if (info[i] + info[i - 1] > x) {
            long long t = info[i] + info[i - 1] - x;
            if (info[i] < t) {
                info[i] = 0;
            } else {
                info[i] -= t;
            }
            cnt += t;
        }
    }
    cout << cnt << endl;
    return 0;
}

相关