You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
# define int long long
# define endl "\n"
signed main ( ) {
int n , m , ans = 0 ;
scanf ( " %lld%lld " , & n , & m ) ;
int p [ m + 1 ] ; // 用于记录经过站点的顺序
int t [ n + 1 ] = { } ; // 用于记录站点之间的路径经过的次数
int a [ n + 1 ] , b [ n + 1 ] , c [ n + 1 ] , x , y ; // 用于记录每段路径所花的费用
for ( int i = 1 ; i < = m ; i + + )
scanf ( " %lld " , & p [ i ] ) ;
for ( int i = 1 ; i < = n - 1 ; i + + )
scanf ( " %lld%lld%lld " , & a [ i ] , & b [ i ] , & c [ i ] ) ;
for ( int i = 1 ; i < = m - 1 ; i + + ) { // 所有的区间都以较小的点排在前面, 例如: 2-1, 5-3都用1-2, 3-5表示, 且每一段都用前面较小的点作为标记! ! ! !
if ( p [ i ] > p [ i + 1 ] ) {
x = p [ i + 1 ] ;
y = p [ i ] ;
} else {
x = p [ i ] ;
y = p [ i + 1 ] ;
}
t [ x ] + + ;
t [ y ] - - ;
}
for ( int i = 1 ; i < = n ; i + + ) { // 求前缀和
t [ i ] + = t [ i - 1 ] ;
}
for ( int i = 1 ; i < = n - 1 ; i + + )
ans + = min ( a [ i ] * t [ i ] , ( b [ i ] * t [ i ] + c [ i ] ) ) ; // 求总的最小就是把每一段的最小相加
printf ( " %lld " , ans ) ;
}