fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void write_output(unsigned long long int);
  7. int max_coins_monsters(vector<unsigned long long int>, int);
  8. int main()
  9. {
  10. unsigned long long int N, T, X;
  11. cin >> N;
  12. for (int i = 0; i < N; i++)
  13. {
  14. cin >> T;
  15. switch (T)
  16. {
  17. case 0: write_output(0);
  18. break;
  19. case 1:
  20. unsigned long long int n;
  21. cin >> n;
  22. write_output(n);
  23. break;
  24. default:
  25. vector<unsigned long long int> pTest(T);
  26. for (int k = 0; k < T; k++){ cin >> pTest[k]; }
  27. X = max_coins_monsters(pTest, T);
  28. write_output(X);
  29. }
  30. }
  31. return 0;
  32. }
  33. void write_output(unsigned long long int value)
  34. {
  35. static int nr = 1;
  36. cout << "Case " << nr++ << ": " << value << endl;
  37. }
  38. int max_coins_monsters(vector<unsigned long long int> p, int nr)
  39. {
  40. unsigned long long int max{ 0 };
  41. int x;
  42. unsigned long long int tab[] = {0, 0, 0};
  43. for (auto elem:p)
  44. {
  45. (tab[0] > tab[1]) ?
  46. x = tab[0] + elem:
  47. x = tab[1] + elem;
  48. tab[0] = tab[1];
  49. tab[1] = tab[2];
  50. tab[2] = x;
  51. }
  52. for (int i = 0; i < 3; i++)
  53. {
  54. if (tab[i] > max) { max = tab[i]; }
  55. }
  56. return max;
  57. }
Runtime error #stdin #stdout #stderr 0s 4200KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc