fork(1) download
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7.  
  8. /**
  9.  *
  10.  * @author RYMA
  11.  */
  12. import java.io.BufferedOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.PrintWriter;
  16. import java.util.InputMismatchException;
  17. import java.util.*;
  18.  
  19. public class Main {
  20. public static void main(String[] args) {
  21. new Main();
  22. }
  23. public int min(int a,int b){
  24. return (a>b)? b:a;
  25. }
  26. public int min(int a,int b,int c){
  27. int x = min(a,b);
  28. return min(x,c);
  29. }
  30. public Main() {
  31. FasterScanner sc = new FasterScanner(System.in);
  32. FastPrinter printer = new FastPrinter();
  33. int N = sc.nextInt();
  34. int a[] = new int[N];
  35. for(int i=0;i<N;++i){
  36. a[i]=sc.nextInt();
  37. }
  38. if(N<=3){
  39. Arrays.sort(a);
  40. System.out.println(a[0]);
  41. }
  42. else{
  43. int b[] = new int[N];
  44. b[0]=a[0];
  45. b[1]=a[1];
  46. b[2]=a[2];
  47. for(int i=3;i<N;++i){
  48. int x = b[i-1]>b[i-2] ? b[i-2]:b[i-1];
  49. int y = b[i-3]>x ? x : b[i-3];
  50. b[i]= y+a[i];
  51. }
  52. int x = b[N-1]>b[N-2] ? b[N-2]:b[N-1];
  53. int y = b[N-3]>x ? x : b[N-3];
  54. System.out.println(y);
  55. }
  56. printer.close();
  57. sc.close();
  58. }
  59.  
  60. public class FasterScanner {
  61. private InputStream mIs;
  62. private byte[] buf = new byte[1024];
  63. private int curChar;
  64. private int numChars;
  65.  
  66. public FasterScanner() {
  67. this(System.in);
  68. }
  69.  
  70. public FasterScanner(InputStream is) {
  71. mIs = is;
  72. }
  73.  
  74. public int read() {
  75. if (numChars == -1)
  76. throw new InputMismatchException();
  77. if (curChar >= numChars) {
  78. curChar = 0;
  79. try {
  80. numChars = mIs.read(buf);
  81. } catch (IOException e) {
  82. throw new InputMismatchException();
  83. }
  84. if (numChars <= 0)
  85. return -1;
  86. }
  87. return buf[curChar++];
  88. }
  89.  
  90. public String nextLine() {
  91. int c = read();
  92. while (isSpaceChar(c))
  93. c = read();
  94. StringBuilder res = new StringBuilder();
  95. do {
  96. res.appendCodePoint(c);
  97. c = read();
  98. } while (!isEndOfLine(c));
  99. return res.toString();
  100. }
  101.  
  102. public String nextString() {
  103. int c = read();
  104. while (isSpaceChar(c))
  105. c = read();
  106. StringBuilder res = new StringBuilder();
  107. do {
  108. res.appendCodePoint(c);
  109. c = read();
  110. } while (!isSpaceChar(c));
  111. return res.toString();
  112. }
  113.  
  114. public long nextLong() {
  115. int c = read();
  116. while (isSpaceChar(c))
  117. c = read();
  118. int sgn = 1;
  119. if (c == '-') {
  120. sgn = -1;
  121. c = read();
  122. }
  123. long res = 0;
  124. do {
  125. if (c < '0' || c > '9')
  126. throw new InputMismatchException();
  127. res *= 10;
  128. res += c - '0';
  129. c = read();
  130. } while (!isSpaceChar(c));
  131. return res * sgn;
  132. }
  133.  
  134. public int nextInt() {
  135. int c = read();
  136. while (isSpaceChar(c))
  137. c = read();
  138. int sgn = 1;
  139. if (c == '-') {
  140. sgn = -1;
  141. c = read();
  142. }
  143. int res = 0;
  144. do {
  145. if (c < '0' || c > '9')
  146. throw new InputMismatchException();
  147. res *= 10;
  148. res += c - '0';
  149. c = read();
  150. } while (!isSpaceChar(c));
  151. return res * sgn;
  152. }
  153.  
  154. public boolean isSpaceChar(int c) {
  155. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  156. }
  157.  
  158. public boolean isEndOfLine(int c) {
  159. return c == '\n' || c == '\r' || c == -1;
  160. }
  161.  
  162. public void close() {
  163. try {
  164. mIs.close();
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. }
  170.  
  171. class FastPrinter {
  172. PrintWriter writer;
  173.  
  174. /**
  175. * Initialize the writer.
  176. */
  177. public FastPrinter() {
  178. writer = new PrintWriter(new BufferedOutputStream(System.out));
  179. }
  180.  
  181. /**
  182. * Print the given String and add a newline.
  183. *
  184. * @param string
  185. * The given String.
  186. */
  187. public void println(String string) {
  188. writer.println(string);
  189. }
  190.  
  191. /**
  192. * Print the given String.
  193. *
  194. * @param string
  195. * The given String.
  196. */
  197. public void print(String string) {
  198. writer.print(string);
  199. }
  200.  
  201. public void close() {
  202. writer.close();
  203. }
  204. }
  205. }
Success #stdin #stdout 0.04s 4386816KB
stdin
2
1 2
stdout
1