fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int w[510],fl[510],st[510],od[510];
  5. int main()
  6. {
  7. int i,j,k=0,n,t,tt,m,sm=0;
  8. cin>>n>>m;
  9. for(i=1;i<=n;i++)
  10. cin>>w[i];
  11. for(i=0;i<m;i++)
  12. {
  13. cin>>od[i];
  14.  
  15. if(fl[od[i]]==0)
  16. st[k++]=od[i];
  17. fl[od[i]]=1;
  18. }
  19. /*if(n==500)
  20.   {
  21.   for(i=0;i<10;i++)
  22.   cout<<od[i]<<" ";
  23.   cout<<"\n";
  24.   }*/
  25. for(i=0;i<m;i++)
  26. {
  27. j=0;
  28.  
  29. while(j<k&&od[i]!=st[j])
  30. {
  31. sm+=w[st[j]];
  32. j++;
  33. }
  34. for(t=j;t>0;t--)
  35. st[t]=st[t-1];
  36. st[0]=od[i];
  37.  
  38. }
  39.  
  40. cout<<sm;
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 2696KB
stdin
3 3
10 20 30
1 2 3
stdout
40