2.c 799 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BUFSZ 1024
  5. char buf[BUFSZ];
  6. int maxheap[3] = {0, 0, 0};
  7. void push(int val) {
  8. int minidx = 0;
  9. for (int i = 0; i < 3; i++) {
  10. if (maxheap[i] < maxheap[minidx]) {
  11. minidx = i;
  12. }
  13. }
  14. if (maxheap[minidx] < val) {
  15. maxheap[minidx] = val;
  16. }
  17. }
  18. int main() {
  19. int maxval = 0;
  20. int cur = 0;
  21. FILE* fp = fopen("input", "r");
  22. while (fgets(buf, BUFSZ, fp)) {
  23. int len = strlen(buf);
  24. char *end;
  25. if (len <= 1) {
  26. maxval = cur > maxval ? cur : maxval;
  27. push(cur);
  28. cur = 0;
  29. } else {
  30. cur += strtol(buf, &end, 10);
  31. }
  32. }
  33. printf("%d\n", maxheap[0] + maxheap[1] + maxheap[2]);
  34. return 0;
  35. }