fork download
  1. #include <stdio.h>
  2.  
  3. int antre[2005];
  4.  
  5. int main () {
  6. // n statsiun, m robot
  7. // n baris berikutnya : r tarif
  8. // m baris berikutnya : w berat robot
  9. // 2m baris berikutnya : robot ke i datang (i > 0) atau robot ke i pergi (i < 0)
  10. int n, m; scanf("%d %d", &n, &m);
  11. int r[105], w[2005];
  12. for(int i = 0; i < n; i++) scanf("%d", &r[i]);
  13. for(int i = 1; i <= m; i++) scanf("%d", &w[i]);
  14. //extra variables
  15. //ans = money, station stores robot idx, or -1 if empty
  16. //parkir where robots at
  17. long long ans = 0;
  18. int station[105];
  19. int parkir[105];
  20. for(int i = 0; i < n; i++) station[i] = -1;
  21. //manual queue (antre)
  22. //left defines the next robot that deserves an exit
  23. //right defines where the next robot will be stored (in terms of index)
  24. //if left == right queue is empty
  25. int left = 0;
  26. int right = 0;
  27.  
  28. for(int ite = 1; ite <= 2 * m; ite++) {
  29. int x; scanf("%d", &x);
  30. if(x > 0) {
  31. int find = -1;
  32. for(int i = 0; i < n; i++) {
  33. if(station[i] == -1) {
  34. station[i] = x;
  35. parkir[x] = i;
  36. find = 1;
  37. break;
  38. }
  39. }
  40. if(find == -1) {
  41. antre[right] = x;
  42. right++;
  43. }
  44. } else { //count ans here
  45. //robot leaving
  46. x *= -1;
  47. ans = ans + (long long)r[parkir[x]] * w[x];
  48. // printf("%d : %d | %d | %d\n", x, parkir[x], r[parkir[x]], w[x]);
  49. station[parkir[x]] = -1;
  50. //if there exist a robot waiting
  51. if(left < right) {
  52. int cur = antre[left];
  53. // printf("Added : %d", cur);
  54. left++;
  55. parkir[cur] = parkir[x];
  56. station[parkir[x]] = cur;
  57. }
  58. }
  59. // for(int i = 0; i < n; i++) {
  60. // printf("%d | ", station[i]);
  61. // }
  62. // printf("\n");
  63. }
  64. printf("%d", ans);
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5308KB
stdin
2 4
5
2
100
500
1000
2000
3
1
2
4
-1
-3
-2
-4
stdout
16200