/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
#include <bits/stdc++.h>
#define i64 long long
#define ld long double
#define bit(n,i) ((n>>i)&1)
#define mp make_pair
#define pii pair<int,int>
#define sz(x) (int)((x).size())
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FOD(i,a,b) for(int i=a; i>=b; i--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define __sumairu__ main()
#define i_love_flandre_scarlet void seggs()
#define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
#define brute(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".ans","w",stdout);}
#define TIME (1.0*clock()/CLOCKS_PER_SEC)
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ai(n) array<int,n>
using namespace std;
const int MOD=998244353;
//const int MOD=1e9+7;
const int inf=1e9;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
///check the limits, dummy
const int N=3e5+5;
#define int long long
int n,k;
int a[N];
i64 pre[N];
namespace sub1
{
bool check(){return n<=2000&&k<=2000;}
i64 t[2003][2003*4];
void up(int c,int id,int l,int r,int p,i64 v)
{
if(p>r||p<l)return;
if(l==r)
{
t[c][id]=v;
return;
}
int m=(l+r)/2;
up(c,id*2,l,m,p,v);
up(c,id*2+1,m+1,r,p,v);
t[c][id]=max(t[c][id*2],t[c][id*2+1]);
}
i64 get(int c,int id,int l,int r,int x,int y)
{
if(x>r||y<l)return -1e18;
if(x<=l&&r<=y)return t[c][id];
int m=(l+r)/2;
return max(get(c,id*2,l,m,x,y),get(c,id*2+1,m+1,r,x,y));
}
void solve()
{
vector<vector<i64>>dp(n+1,vector<i64>(k+1));
/**
dp[i][j]: up to i, picked j segments
dp[i][j] = dp[k][j-1]+sum(k+1,i) = ...+t[i]-t[k]
(take i)dp[i][j] = t[i] + dp[k][j-1] - t[k] (k<i)
(not take i)dp[i][j] = dp[k][j]
*/
memset(t,-60,sizeof t);
i64 ans=0;
if(k==1)ans=max(ans,1ll*a[1]);
FOR(i,0,n)up(0,1,0,n,i,0-pre[i]);
vector<vector<i64>>preMx(k+1,vector<i64>(n+1));
FOR(i,1,n)
{
FOD(j,min(k,i),1)
{
dp[i][j]=max(dp[i][j],pre[i]+get(j-1,1,0,n,0,i-1));
dp[i][j]=max(dp[i][j],preMx[j][i-1]);
up(j,1,0,n,i,dp[i][j]-pre[i]);
preMx[j][i]=max(preMx[j][i-1],dp[i][j]);
}
ans=max(ans,dp[i][k]);
}
// FOR(i,1,n)
// {
// FOR(j,1,k)if(dp[i][j]!=-1e18)
// {
// cout<<i<<' '<<j<<' '<<dp[i][j]<<'\n';
// }
// }
cout<<ans;
exit(0);
}
}
namespace sub2
{
bool check(){return 1;}
pii dp[N];
pii check(int x)
{
memset(dp,0,sizeof(dp));
pii ans{};
FOR(i,0,n-1)
{
pii tmp{dp[i].first-pre[i], dp[i].second};
if(tmp.fi^ans.fi?tmp.fi>ans.fi:tmp.se<ans.se)ans=tmp;
pii tmp2{ans.fi-x+pre[i+1],ans.se+1};
dp[i+1]=(tmp2.fi^dp[i].fi?tmp2.fi>dp[i].fi:tmp2.se<dp[i].se)?tmp2:dp[i];
}
return dp[n];
}
void solve()
{
FOR(i,1,n)a[i]+=a[i-1];
int l=0,r=3e14;
while(l<r)
{
int mid=(l+r)/2;
if(check(mid).se<=k)r=mid;
else l=mid+1;
}
pii ans=check(l);
cout<<ans.fi+k*l;
exit(0);
}
}
i_love_flandre_scarlet
{
cin>>n>>k;
FOR(i,1,n)
{
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}
// if(*min_element(a+1,a+n+1)>=0)
// {
// i64 ans=0;
// FOR(i,1,n)ans+=a[i];
// cout<<ans;
// return;
// }
//
// int cnt=0;
// FOR(i,1,n)cnt+=(a[i]<0);
// if(cnt==1)
// {
// i64 L=0,R=0;
// FOR(i,1,n)if(a[i]<0)
// {
// L=pre[i-1];
// R=pre[n]-pre[i];
// }
// if(k>=2)cout<<L+R;
// else cout<<max(L,R);
// return;
// }
if(sub2::check())sub2::solve();
if(sub1::check())sub1::solve();
}
__sumairu__
{
FAST
//file("ぺぽよ⋆PEPOYO");
// freopen("feas.inp","r",stdin);
// freopen("feas.out","w",stdout);
int t=1;//cin>>t;
while(t--)seggs();
//cerr<<"\nTime elapsed: "<<TIME<<" s.\n";
}
/**
6 1
1 -2 3 -1 5 -6
6 2
1 2 3 -10 5 6
6 4
-1 -2 -1 0 -5 -1
*/
Compilation message
feast.cpp: In function 'std::pair<long long int, long long int> sub2::check(long long int)':
feast.cpp:156:31: warning: 'void* memset(void*, int, size_t)' clearing an object of type 'struct std::pair<long long int, long long int>' with no trivial copy-assignment; use assignment instead [-Wclass-memaccess]
156 | memset(dp,0,sizeof(dp));
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from feast.cpp:43:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<long long int, long long int>' declared here
211 | struct pair
| ^~~~
feast.cpp: At global scope:
feast.cpp:56:21: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
56 | #define __sumairu__ main()
| ^~~~
feast.cpp:224:1: note: in expansion of macro '__sumairu__'
224 | __sumairu__
| ^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
9692 KB |
Output is correct |
2 |
Correct |
59 ms |
9808 KB |
Output is correct |
3 |
Correct |
69 ms |
9836 KB |
Output is correct |
4 |
Correct |
59 ms |
9812 KB |
Output is correct |
5 |
Correct |
58 ms |
9820 KB |
Output is correct |
6 |
Correct |
58 ms |
9556 KB |
Output is correct |
7 |
Correct |
58 ms |
9560 KB |
Output is correct |
8 |
Correct |
60 ms |
9816 KB |
Output is correct |
9 |
Correct |
58 ms |
9560 KB |
Output is correct |
10 |
Correct |
59 ms |
9792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
9560 KB |
Output is correct |
2 |
Correct |
64 ms |
9720 KB |
Output is correct |
3 |
Correct |
47 ms |
10844 KB |
Output is correct |
4 |
Correct |
49 ms |
10840 KB |
Output is correct |
5 |
Correct |
58 ms |
12380 KB |
Output is correct |
6 |
Correct |
47 ms |
10896 KB |
Output is correct |
7 |
Correct |
50 ms |
10976 KB |
Output is correct |
8 |
Correct |
60 ms |
12636 KB |
Output is correct |
9 |
Correct |
61 ms |
12628 KB |
Output is correct |
10 |
Correct |
50 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
9820 KB |
Output is correct |
2 |
Correct |
56 ms |
9564 KB |
Output is correct |
3 |
Correct |
74 ms |
9820 KB |
Output is correct |
4 |
Correct |
52 ms |
9636 KB |
Output is correct |
5 |
Correct |
52 ms |
9808 KB |
Output is correct |
6 |
Correct |
52 ms |
9812 KB |
Output is correct |
7 |
Correct |
52 ms |
9660 KB |
Output is correct |
8 |
Correct |
53 ms |
9820 KB |
Output is correct |
9 |
Correct |
52 ms |
9816 KB |
Output is correct |
10 |
Correct |
53 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7000 KB |
Output is correct |
2 |
Correct |
7 ms |
7000 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Correct |
8 ms |
7220 KB |
Output is correct |
5 |
Correct |
7 ms |
7004 KB |
Output is correct |
6 |
Correct |
7 ms |
7000 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
7 ms |
7004 KB |
Output is correct |
9 |
Correct |
8 ms |
7220 KB |
Output is correct |
10 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7000 KB |
Output is correct |
2 |
Correct |
7 ms |
7000 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Correct |
8 ms |
7220 KB |
Output is correct |
5 |
Correct |
7 ms |
7004 KB |
Output is correct |
6 |
Correct |
7 ms |
7000 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
7 ms |
7004 KB |
Output is correct |
9 |
Correct |
8 ms |
7220 KB |
Output is correct |
10 |
Correct |
6 ms |
7004 KB |
Output is correct |
11 |
Correct |
7 ms |
7000 KB |
Output is correct |
12 |
Correct |
7 ms |
7164 KB |
Output is correct |
13 |
Correct |
6 ms |
7004 KB |
Output is correct |
14 |
Correct |
7 ms |
7004 KB |
Output is correct |
15 |
Correct |
6 ms |
7004 KB |
Output is correct |
16 |
Correct |
8 ms |
7004 KB |
Output is correct |
17 |
Correct |
7 ms |
7100 KB |
Output is correct |
18 |
Correct |
7 ms |
7224 KB |
Output is correct |
19 |
Correct |
7 ms |
7004 KB |
Output is correct |
20 |
Correct |
7 ms |
7224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7000 KB |
Output is correct |
2 |
Correct |
7 ms |
7000 KB |
Output is correct |
3 |
Correct |
6 ms |
7004 KB |
Output is correct |
4 |
Correct |
8 ms |
7220 KB |
Output is correct |
5 |
Correct |
7 ms |
7004 KB |
Output is correct |
6 |
Correct |
7 ms |
7000 KB |
Output is correct |
7 |
Correct |
6 ms |
7180 KB |
Output is correct |
8 |
Correct |
7 ms |
7004 KB |
Output is correct |
9 |
Correct |
8 ms |
7220 KB |
Output is correct |
10 |
Correct |
6 ms |
7004 KB |
Output is correct |
11 |
Correct |
7 ms |
7000 KB |
Output is correct |
12 |
Correct |
7 ms |
7164 KB |
Output is correct |
13 |
Correct |
6 ms |
7004 KB |
Output is correct |
14 |
Correct |
7 ms |
7004 KB |
Output is correct |
15 |
Correct |
6 ms |
7004 KB |
Output is correct |
16 |
Correct |
8 ms |
7004 KB |
Output is correct |
17 |
Correct |
7 ms |
7100 KB |
Output is correct |
18 |
Correct |
7 ms |
7224 KB |
Output is correct |
19 |
Correct |
7 ms |
7004 KB |
Output is correct |
20 |
Correct |
7 ms |
7224 KB |
Output is correct |
21 |
Correct |
8 ms |
7004 KB |
Output is correct |
22 |
Correct |
8 ms |
7048 KB |
Output is correct |
23 |
Correct |
7 ms |
7004 KB |
Output is correct |
24 |
Correct |
6 ms |
7004 KB |
Output is correct |
25 |
Correct |
7 ms |
7004 KB |
Output is correct |
26 |
Correct |
7 ms |
7004 KB |
Output is correct |
27 |
Correct |
7 ms |
7240 KB |
Output is correct |
28 |
Correct |
7 ms |
7004 KB |
Output is correct |
29 |
Correct |
7 ms |
7004 KB |
Output is correct |
30 |
Correct |
7 ms |
7240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
9692 KB |
Output is correct |
2 |
Correct |
59 ms |
9808 KB |
Output is correct |
3 |
Correct |
69 ms |
9836 KB |
Output is correct |
4 |
Correct |
59 ms |
9812 KB |
Output is correct |
5 |
Correct |
58 ms |
9820 KB |
Output is correct |
6 |
Correct |
58 ms |
9556 KB |
Output is correct |
7 |
Correct |
58 ms |
9560 KB |
Output is correct |
8 |
Correct |
60 ms |
9816 KB |
Output is correct |
9 |
Correct |
58 ms |
9560 KB |
Output is correct |
10 |
Correct |
59 ms |
9792 KB |
Output is correct |
11 |
Correct |
43 ms |
9560 KB |
Output is correct |
12 |
Correct |
64 ms |
9720 KB |
Output is correct |
13 |
Correct |
47 ms |
10844 KB |
Output is correct |
14 |
Correct |
49 ms |
10840 KB |
Output is correct |
15 |
Correct |
58 ms |
12380 KB |
Output is correct |
16 |
Correct |
47 ms |
10896 KB |
Output is correct |
17 |
Correct |
50 ms |
10976 KB |
Output is correct |
18 |
Correct |
60 ms |
12636 KB |
Output is correct |
19 |
Correct |
61 ms |
12628 KB |
Output is correct |
20 |
Correct |
50 ms |
10844 KB |
Output is correct |
21 |
Correct |
52 ms |
9820 KB |
Output is correct |
22 |
Correct |
56 ms |
9564 KB |
Output is correct |
23 |
Correct |
74 ms |
9820 KB |
Output is correct |
24 |
Correct |
52 ms |
9636 KB |
Output is correct |
25 |
Correct |
52 ms |
9808 KB |
Output is correct |
26 |
Correct |
52 ms |
9812 KB |
Output is correct |
27 |
Correct |
52 ms |
9660 KB |
Output is correct |
28 |
Correct |
53 ms |
9820 KB |
Output is correct |
29 |
Correct |
52 ms |
9816 KB |
Output is correct |
30 |
Correct |
53 ms |
9820 KB |
Output is correct |
31 |
Correct |
7 ms |
7000 KB |
Output is correct |
32 |
Correct |
7 ms |
7000 KB |
Output is correct |
33 |
Correct |
6 ms |
7004 KB |
Output is correct |
34 |
Correct |
8 ms |
7220 KB |
Output is correct |
35 |
Correct |
7 ms |
7004 KB |
Output is correct |
36 |
Correct |
7 ms |
7000 KB |
Output is correct |
37 |
Correct |
6 ms |
7180 KB |
Output is correct |
38 |
Correct |
7 ms |
7004 KB |
Output is correct |
39 |
Correct |
8 ms |
7220 KB |
Output is correct |
40 |
Correct |
6 ms |
7004 KB |
Output is correct |
41 |
Correct |
7 ms |
7000 KB |
Output is correct |
42 |
Correct |
7 ms |
7164 KB |
Output is correct |
43 |
Correct |
6 ms |
7004 KB |
Output is correct |
44 |
Correct |
7 ms |
7004 KB |
Output is correct |
45 |
Correct |
6 ms |
7004 KB |
Output is correct |
46 |
Correct |
8 ms |
7004 KB |
Output is correct |
47 |
Correct |
7 ms |
7100 KB |
Output is correct |
48 |
Correct |
7 ms |
7224 KB |
Output is correct |
49 |
Correct |
7 ms |
7004 KB |
Output is correct |
50 |
Correct |
7 ms |
7224 KB |
Output is correct |
51 |
Correct |
8 ms |
7004 KB |
Output is correct |
52 |
Correct |
8 ms |
7048 KB |
Output is correct |
53 |
Correct |
7 ms |
7004 KB |
Output is correct |
54 |
Correct |
6 ms |
7004 KB |
Output is correct |
55 |
Correct |
7 ms |
7004 KB |
Output is correct |
56 |
Correct |
7 ms |
7004 KB |
Output is correct |
57 |
Correct |
7 ms |
7240 KB |
Output is correct |
58 |
Correct |
7 ms |
7004 KB |
Output is correct |
59 |
Correct |
7 ms |
7004 KB |
Output is correct |
60 |
Correct |
7 ms |
7240 KB |
Output is correct |
61 |
Correct |
92 ms |
12768 KB |
Output is correct |
62 |
Correct |
98 ms |
12664 KB |
Output is correct |
63 |
Correct |
63 ms |
12628 KB |
Output is correct |
64 |
Correct |
87 ms |
12832 KB |
Output is correct |
65 |
Correct |
79 ms |
12624 KB |
Output is correct |
66 |
Correct |
79 ms |
12624 KB |
Output is correct |
67 |
Correct |
77 ms |
12624 KB |
Output is correct |
68 |
Correct |
86 ms |
12628 KB |
Output is correct |
69 |
Correct |
56 ms |
12636 KB |
Output is correct |
70 |
Correct |
71 ms |
12472 KB |
Output is correct |