|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st; |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 16 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 17 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 18 | + |
| 19 | + // Additional field |
| 20 | + static long[] A; |
| 21 | + static int N; |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + |
| 25 | + ready(); |
| 26 | + solve(); |
| 27 | + |
| 28 | + bwEnd(); |
| 29 | + } |
| 30 | + |
| 31 | + static void ready() throws Exception{ |
| 32 | + |
| 33 | + N = Integer.parseInt(br.readLine()); |
| 34 | + A = new long[N]; |
| 35 | + nextLine(); |
| 36 | + for(int i=0;i<N;i++) A[i] = nextLong(); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + static void solve() throws Exception{ |
| 41 | + |
| 42 | + bw.write(find(0,N-1) + "\n"); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + static long find(int s, int e) { |
| 47 | + if(s == e) return A[s]; |
| 48 | + int m=(s+e-1)/2; |
| 49 | + long left = A[s], right = A[e]; |
| 50 | + for(int i=s+1;i<=m;i++) left = gcd(left, A[i]); |
| 51 | + for(int i=m+1;i<e;i++) right = gcd(right, A[i]); |
| 52 | + return Math.max(left + find(m+1,e), right + find(s,m)); |
| 53 | + } |
| 54 | + |
| 55 | + static long gcd(long a, long b) { |
| 56 | + if(a < b) { |
| 57 | + long temp = a; |
| 58 | + a = b; |
| 59 | + b = temp; |
| 60 | + } |
| 61 | + return a%b!=0 ? gcd(b,a%b) : b; |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +``` |
0 commit comments