您的位置:首页 > 其它

Codeforces 500C New Year Book Reading

2014-12-31 05:21 459 查看
C. New Year Book Reading

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is wi.

As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.

He lifts all the books above book x.

He pushes book x out of the stack.

He puts down the lifted books without changing their order.

After reading book x, he puts book x on the top of the stack.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010;

int n , m , w
, d
;
bool vis
;
int main()
{
while( cin >> n >> m ) {
for( int i = 1 ; i <= n ; ++i ) cin >> w[i] ;
for( int i = 1 ; i <= m ; ++i ) cin >> d[i] ;
LL ans = 0 ;
for( int i = 1 ; i <= m ; ++i ) {
memset( vis , false , sizeof vis );
for( int j = i - 1 ; j > 0 ; --j ){
if( d[i] == d[j] ) break ;
if( !vis[ d[j] ] ) { ans += w[ d[j] ] ; vis[ d[j] ] = true ; }
}
}
cout << ans << endl ;
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: