• Source
    1. #include<cstdio>
    2. #include<stack>
    3. #include<iostream>
    4.  
    5. using namespace std;
    6.  
    7. int arr[1003];
    8.  
    9. stack<int>st;
    10.  
    11. int main()
    12. {
    13. int n,tag,i,a;
    14.  
    15. while(scanf("%d",&n)&&n)
    16. {
    17. while(scanf("%d",&arr[1])==1)
    18. {
    19.  
    20. if(arr[1]==0)
    21. {
    22. break;
    23. }
    24.  
    25. for(i=2; i<=n; i++)
    26. {
    27. scanf("%d",&arr[i]);
    28. }
    29.  
    30. i=1;
    31.  
    32. tag=0;
    33. bool toggle=0;
    34. a=arr[1];
    35.  
    36. st.push(++tag);
    37.  
    38. for(;;)
    39. {
    40. if(st.empty())
    41. {
    42. st.push(++tag);
    43. if(tag==n)
    44. {
    45. break;
    46. }
    47. }
    48.  
    49. else if(st.top()==a)
    50. {
    51. st.pop();
    52. a=arr[++i];
    53. toggle=1;
    54. }
    55. else if(st.top()!=a)
    56. {
    57. st.push(++tag);
    58. if(tag==n)
    59. {
    60. break;
    61. }
    62. }
    63. }
    64.  
    65. if(toggle==0)
    66. {
    67. i=1;
    68. }
    69.  
    70. for(;;)
    71. {
    72. if(st.empty()) break;
    73.  
    74. if(st.top()==arr[i++])
    75. {
    76. st.pop();
    77. }
    78. else
    79. {
    80. break;
    81. }
    82. }
    83. if(st.empty())
    84. {
    85. puts("Yes");
    86. }
    87. else
    88. {
    89. puts("No");
    90. }
    91. while(!st.empty())
    92. {
    93. st.pop();
    94. }
    95. }
    96. puts("");
    97.  
    98. }
    99.  
    100. return 0;
    101. }