fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int stasiun_cnt, robot_cnt;
  5. if (scanf("%d %d", &stasiun_cnt, &robot_cnt) != 2) return 0;
  6.  
  7. long long harga_s[105];
  8. for (int x = 1; x <= stasiun_cnt; x++) {
  9. scanf("%lld", &harga_s[x]);
  10. }
  11.  
  12. long long bobot_r[2005];
  13. for (int x = 1; x <= robot_cnt; x++) {
  14. scanf("%lld", &bobot_r[x]);
  15. }
  16.  
  17. int slot_isi[105] = {0};
  18. int status_posisi[2005] = {0};
  19.  
  20. int antrean[2005];
  21. int f_idx = 0, r_idx = 0;
  22.  
  23. long long kas_bengkel = 0;
  24.  
  25. int total_kejadian = 2 * robot_cnt;
  26. for (int ev = 0; ev < total_kejadian; ev++) {
  27. int log_ev;
  28. scanf("%d", &log_ev);
  29.  
  30. if (log_ev > 0) {
  31. int cur_r = log_ev;
  32.  
  33. int target_slot = -1;
  34. for (int s = 1; s <= stasiun_cnt; s++) {
  35. if (slot_isi[s] == 0) {
  36. target_slot = s;
  37. break;
  38. }
  39. }
  40.  
  41. if (target_slot != -1) {
  42. slot_isi[target_slot] = cur_r;
  43. status_posisi[cur_r] = target_slot;
  44. kas_bengkel += bobot_r[cur_r] * harga_s[target_slot];
  45. } else {
  46. antrean[r_idx++] = cur_r;
  47. }
  48.  
  49. } else {
  50. int cur_r = -log_ev;
  51.  
  52. int released_slot = status_posisi[cur_r];
  53. slot_isi[released_slot] = 0;
  54. status_posisi[cur_r] = 0;
  55.  
  56. if (f_idx < r_idx) {
  57. int next_target = -1;
  58. for (int s = 1; s <= stasiun_cnt; s++) {
  59. if (slot_isi[s] == 0) {
  60. next_target = s;
  61. break;
  62. }
  63. }
  64.  
  65. if (next_target != -1) {
  66. int next_r = antrean[f_idx++];
  67. slot_isi[next_target] = next_r;
  68. status_posisi[next_r] = next_target;
  69. kas_bengkel += bobot_r[next_r] * harga_s[next_target];
  70. }
  71. }
  72. }
  73. }
  74.  
  75. printf("%lld\n", kas_bengkel);
  76. return 0;
  77. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Standard output is empty