题解:P12006 【MX-X10-T2】[LSOT-4] 网易云

· · 题解

原题传送门

思路

我们可以开一个桶数组记录一下每首歌听的次数,然后遍历一遍,依次让每首歌清零,答案就为每首歌的数量乘上组合值,再更新下首歌的数量。

代码

#include<bits/stdc++.h>
#define int long long
#define inf 0x7f7f7f7f
using namespace std;
const int maxn = 100010;
inline int read() {
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int n , m , a[maxn] , tong[maxn];
signed main() {
    n = read();
    m = read();
    for(int i = 1; i < n; i++) a[i] = read();
    for(int i = 1; i <= m; i++) {
        int u = read() , v = read();
        tong[u] += v;
    }
    int ans = 0;
    for(int i = 1; i < n; i++) {
        ans += tong[i] * a[i];
        tong[i + 1] -= tong[i];
        tong[i] -= tong[i];
    }
    for(int i = 1; i <= n; i++) {
        if(tong[i]) {
            cout << "Impossible" << endl;
            return 0;
        }
    }
    cout << ans << endl;
    return 0;
}