Codeforces Round 909 (Div. 3) 1899B - 250 Thousand Tons of TNT Solution
Problem Link: https://codeforces.com/problemset/problem/1899/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- vector<int > a(n) ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- vector<int>divisor ;
- for ( int i = 1 ; i * i <= n ; i++){
- if ( n % i == 0){
- divisor.push_back(i) ;
- if( i * i != n){
- divisor.push_back(n/i) ;
- }
- }
- }
- sort(divisor.begin(),divisor.end()) ;
- int ans = 0 ;
- for ( auto size : divisor){
- int cnt = 0 ;
- int mn = LLONG_MAX ;
- int mx = 0 ;
- int tem_sum = 0 ;
- for ( int i = 0 ; i < n ; i++){
- tem_sum += a[i] ;
- cnt++ ;
- if ( cnt == size){
- mn = min(mn,tem_sum) ;
- mx = max(mx,tem_sum) ;
- cnt = 0 ;
- tem_sum = 0 ;
- }
- ans = max(ans,mx-mn) ;
- }
- }
- cout << ans << '\n' ;
- }
- return 0 ;
- }
No comments