#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std ;
const int N = 5e5, NS = 5e3 ;
int n, q, d[N + 1], dp[NS + 1][NS + 1], mx[NS + 1][NS + 1] ;
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
for(int i = 1 ; i <= n ; i++)
cin >> d[i] ;
cin >> q ;
if(n <= 5000)
{
for(int i = 1 ; i <= n ; i++)
for(int j = i ; j <= n ; j++)
mx[i][j] = max(mx[i][j - 1], d[j]) ;
for(int i = 1 ; i <= n - 2 ; i++)
dp[i][i + 2] = d[i] + d[i + 1] + d[i + 2] ;
for(int ln = 4 ; ln <= n ; ln++)
for(int j = 1 ; j <= n - ln + 1 ; j++)
dp[j][j + ln - 1] = max({dp[j][j + ln - 2], dp[j + 1][j + ln - 1], d[j] + d[j + ln - 1] + mx[j + 1][j + (ln - 1) / 2]}) ;
while(q--)
{
int l, r ;
cin >> l >> r ;
cout << dp[l][r] << '\n' ;
}
return 0 ;
}
if(n <= 100 && q <= 100)
{
while(q--)
{
int l, r, ans = 0 ;
cin >> l >> r ;
for(int b = l + 1 ; b < r ; b++)
for(int a = l ; a < b ; a++)
for(int c = b + (b - a) ; c <= r ; c++)
ans = max(ans, d[a] + d[b] + d[c]) ;
cout << ans << '\n' ;
}
return 0 ;
}
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1076 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1076 KB |
Output is correct |
8 |
Correct |
1 ms |
1092 KB |
Output is correct |
9 |
Correct |
1 ms |
1096 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1076 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1076 KB |
Output is correct |
8 |
Correct |
1 ms |
1092 KB |
Output is correct |
9 |
Correct |
1 ms |
1096 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
484 ms |
143156 KB |
Output is correct |
12 |
Correct |
483 ms |
143060 KB |
Output is correct |
13 |
Correct |
484 ms |
143092 KB |
Output is correct |
14 |
Correct |
477 ms |
143096 KB |
Output is correct |
15 |
Correct |
454 ms |
143096 KB |
Output is correct |
16 |
Correct |
483 ms |
142512 KB |
Output is correct |
17 |
Correct |
479 ms |
142524 KB |
Output is correct |
18 |
Correct |
481 ms |
142420 KB |
Output is correct |
19 |
Correct |
520 ms |
143044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
1108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1076 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1076 KB |
Output is correct |
8 |
Correct |
1 ms |
1092 KB |
Output is correct |
9 |
Correct |
1 ms |
1096 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
484 ms |
143156 KB |
Output is correct |
12 |
Correct |
483 ms |
143060 KB |
Output is correct |
13 |
Correct |
484 ms |
143092 KB |
Output is correct |
14 |
Correct |
477 ms |
143096 KB |
Output is correct |
15 |
Correct |
454 ms |
143096 KB |
Output is correct |
16 |
Correct |
483 ms |
142512 KB |
Output is correct |
17 |
Correct |
479 ms |
142524 KB |
Output is correct |
18 |
Correct |
481 ms |
142420 KB |
Output is correct |
19 |
Correct |
520 ms |
143044 KB |
Output is correct |
20 |
Incorrect |
14 ms |
1108 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |