fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. const long long MOD = 1000000007;
  8.  
  9. long long max_sum_after_operations(int n, int k, vector<int>& arr) {
  10. vector<long long> prefix_sum(n + 1, 0);
  11. for (int i = 0; i < n; ++i) {
  12. prefix_sum[i + 1] = prefix_sum[i] + arr[i];
  13. }
  14.  
  15. long long max_sum = numeric_limits<long long>::min();
  16. for (int i = 0; i < n; ++i) {
  17. for (int j = i + 1; j <= n; ++j) {
  18. long long subarray_sum = prefix_sum[j] - prefix_sum[i];
  19. max_sum = max(max_sum, subarray_sum);
  20. }
  21. }
  22.  
  23. max_sum += max_sum * (k - 1);
  24. return max_sum % MOD;
  25. }
  26.  
  27. int main() {
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30.  
  31. int t;
  32. cin >> t; // Number of test cases
  33. while (t--) {
  34. int n, k;
  35. cin >> n >> k; // Length of the array and number of operations
  36. vector<int> arr(n);
  37. for (int i = 0; i < n; ++i) {
  38. cin >> arr[i]; // Array elements
  39. }
  40. cout << max_sum_after_operations(n, k, arr) << endl; // Output result
  41. }
  42.  
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5276KB
stdin
12
2 2
-4 -7
3 3
2 2 8
1 7
7
5 1
4 -2 8 -12 9
7 4
8 14 -9 6 0 -1 3
7 100
5 3 -8 12 -5 -9 3
6 1000
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
2 1
1000000000 8
5 4
0 0 0 0 0
6 10
48973 757292 58277 -38574 27475 999984
7 1
-1000 1000 -1000 1000 -1000 1000 -1000
10 10050
408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045
stdout
-8
36
49
10
88
1200
-999993007
1
0
18534270
1000
518711043