Advertisement
paranid5

Bin

Feb 14th, 2021
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct
  6. {
  7.     int first_;
  8.     int second_;
  9.     int third_;
  10. } triple;
  11.  
  12. int cmp(const void* a, const void* b)
  13. {
  14.     const triple f = *(triple*)a;
  15.     const triple s = *(triple*)b;
  16.  
  17.     if (f.third_ < s.third_) return -1;
  18.     if (f.third_ > s.third_) return 1;
  19.     return 0;
  20. }
  21.  
  22. int main()
  23. {
  24.     FILE* in = fopen("vase.in", "r");
  25.     FILE* out = fopen("vase.out", "w");
  26.    
  27.     int n = 0, t = 0;
  28.     fscanf(in, "%d%d", &n, &t);
  29.  
  30.     triple* arr = malloc(n * sizeof(triple));
  31.  
  32.     int64_t sum = 0, minus = 0;
  33.  
  34.     for (triple* p = arr; p != arr + n; p++)
  35.     {
  36.         int f = 0, s = 0;
  37.         fscanf(in, "%d%d", &f, &s);
  38.  
  39.         p->first_ = f;
  40.         p->second_ = !f ? 0 : s;
  41.         p->third_ = !s || !f ? t + 2 : f / s + (!(f % s) ? 0 : 1);
  42.  
  43.         sum += f;
  44.         minus += p->second_;
  45.     }
  46.  
  47.     qsort(arr, n, sizeof(triple), cmp);
  48.  
  49.     int move = 0;
  50.     fprintf(out, "%lld\n", sum);
  51.  
  52.     for (int i = 1; i <= t; i++)
  53.     {
  54.         if (sum)
  55.         {
  56.             while (move < n && i == arr[move].third_)
  57.             {
  58.                 sum -= !(arr[move].first_ % arr[move].second_) ? arr[move].second_ : arr[move].first_ % arr[move].second_;
  59.                 minus -= arr[move].second_;
  60.                 move++;
  61.             }
  62.  
  63.             sum -= minus;
  64.         }
  65.  
  66.         fprintf(out, "%lld\n", sum);
  67.     }
  68.  
  69.     free(arr);
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement