everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.com/contest/855/problem/B

my code : https://paste.ubuntu.com/25608466/

Before contest

Codeforces Round #517 (Div. 1, based on Technocup 2019 Elimination Round 2)

00:52:29

Register now »

Codeforces Round #517 (Div. 1, based on Technocup 2019 Elimination Round 2)

00:52:29

Register now »

*has extra registration

Before contest

Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

00:52:29

Register now »

Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

00:52:29

Register now »

*has extra registration

# | User | Rating |
---|---|---|

1 | tourist | 3581 |

2 | OO0OOO00O0OOO0O0…O | 3264 |

3 | mnbvmar | 3246 |

4 | Um_nik | 3194 |

5 | LHiC | 3190 |

6 | Petr | 3161 |

7 | V--o_o--V | 3133 |

8 | CongLingDanPaiSh…5 | 3116 |

9 | ko_osaga | 3115 |

10 | Benq | 3098 |

# | User | Contrib. |
---|---|---|

1 | Radewoosh | 187 |

2 | Errichto | 165 |

3 | rng_58 | 161 |

4 | tourist | 158 |

5 | Vovuh | 150 |

5 | Um_nik | 150 |

7 | Petr | 149 |

7 | Swistakk | 149 |

9 | 300iq | 148 |

10 | PikMike | 147 |

10 | neal | 147 |

everything seems okay , where is the problem ? can you explain ? problem link : http://codeforces.com/contest/855/problem/B

my code : https://paste.ubuntu.com/25608466/

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/21/2018 10:12:31 (d3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

consider this input: 3 1 1 -1 -1 100 1000

should output: 1000

your solution: -1

But why ? i was always taking the maximum answer greedily....!!!

1 ≤ i ≤ j ≤ k ≤ n.

ooo , may be got it , thank you.

As I told you before, greedy approach will not work here.

You can try Dynamic programming to solve this type of problem where greedy doesn't work. But for this there is a brute force solution.

for each number in the array, let's say that this is

a_{j}and we multiply it withq. So we are fixing the middle element every time. Now we have to finda_{i}anda_{k}[i≤jandj≤k] such thata_{i}*p+a_{k}*ris maximized. So we can easily get anO(n^{2}) solution. How to make itO(n)? Well the array is static. So we can pre-calculate the minimum and maximum in each suffix and prefix of the array and use them instead of running a loop each time to finda_{i}anda_{k}. This is the same solution described in the editorial btw.