洛谷 P3406 海底高铁
#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
LL n,m,ans=0;
LL a,b,c;
LL p[100005];
LL s[100005];//每段铁路要走多少次
inline LL read()
{
char c;
LL x=0,f=1;
do
{
c=getchar();
if(c=='-')
f=-1;
}while(c<'0' || c>'9');
do
{
x=x*10+c-'0';
c=getchar();
}while(c>='0' && c<='9');
return f*x;
}
int main()
{
n=read();
m=read();
for(int i=0;i<m;++i)
{
p[i]=read();
}
for(int i=0;i<m-1;++i)//共m-1段铁路
{
s[max(p[i],p[i+1])]--;//铁路尾
s[min(p[i],p[i+1])]++;//铁路头
//差分
}
for(int i=1;i<=n;++i)//前缀和
{
s[i]+=s[i-1];
}
for(int i=1;i<n;++i)
{
a=read();
b=read();
c=read();
ans+=min(a*s[i],b*s[i]+c);
}
printf("%lld\n",ans);
return 0;
}