fork download
  1. #include<iostream>
  2. #include <stdlib.h>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7.  
  8. {
  9. int n;
  10. int x[100];
  11. cin >> n;
  12. int j = 0;
  13. int i = 0;
  14. int count = 0;
  15. int count1 = 0;
  16. int count2 = 0;
  17. int a;
  18. int c = 0;
  19.  
  20. for (int k = 0; k < n; k++)
  21. {
  22. cin >> x[k];
  23. if (x[k + 1] == 0)
  24. {
  25. i = k + 1;
  26. //cout << i << endl;
  27. break;
  28. }
  29.  
  30. }
  31.  
  32. for (int k = 0; k < n; k++)
  33. {
  34. if (x[n-1] == 0)
  35. {
  36. j = n;
  37. //cout << j << endl;
  38. break;
  39.  
  40. }
  41. else
  42. {
  43. n = n - 1;
  44. if (x[n-1] == 0)
  45. {
  46. j = n;
  47. // cout << j << endl;
  48. break;
  49. }
  50. }
  51. }
  52. for (int m=i; i< j; i++)
  53. {
  54. if (x[0] == 1)
  55. {
  56. count1 = x[0];
  57.  
  58. }
  59.  
  60. if (x[n] == 1)
  61. {
  62. count2++;
  63. }
  64.  
  65. a = 1 - x[i];
  66. //cout << a << endl;
  67. if (a == 1)
  68. {
  69. count++;
  70. }
  71. }
  72. c = count1 + count2 + count;
  73. cout << c << endl;
  74.  
  75. }
Success #stdin #stdout 0s 3472KB
stdin
5
1
0
0
1
0
stdout
0