First decomposition of the quality factor for the current a [i], the quality factor is assumed that the current x, this location to meet the divisible k, there are two possibilities, either it is a rearwardly x% k th transfer, or after a transfer it to a kx% k.
For each of the a [i] is satisfied, because only affect the next position, so that the next position of a [i + 1] Effect count a [i] is generated, and later issues a new sub (prohibition nesting dolls).
A quality factor for each of x, for all i positions all requirements min (a [i], xa [i]) and, the minimum value of the sum of all prime factors of i as the answer.
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 long long a[1000007]; 5 int main(){ 6 ios::sync_with_stdio(false); 7 cin.tie(NULL); 8 cout.tie(NULL); 9 long long n; 10 cin>>n; 11 long long sum=0; 12 for(long long i=1;i<=n;++i){ 13 cin>>a[i]; 14 sum+=a[i]; 15 } 16 if(sum<=1){ 17 cout<<-1; 18 return 0; 19 } 20 long long ans=1e18; 21 for(long long i=2;i*i<=sum;++i){ 22 long long flag=0; 23 while(sum%i==0){ 24 sum/=i; 25 flag=1; 26 } 27 if(flag){ 28 long long pre=0,summ=0; 29 for(long long j=1;j<=n;++j){ 30 pre=(a[j]+pre)%i; 31 summ+=min(pre,i-pre); 32 } 33 ans=min(ans,summ); 34 } 35 } 36 if (sum> 1 ) { 37 along long pre = 0 , summ = 0 ; 38 for ( long along j = 1 ; j <= n; ++ j) { 39 pre = (a [j] + pre)% sum; 40 summ = min + (pre, sum- pre); 41 } 42 years = min (years summ); 43 } 44 cout << years; 45 return 0 ; 46 }