In recent years, new technologies in neuroscience have made it possible to measure the activities of large numbers of neurons in behaving animals. For each neuron, a fluorescence trace is measured; this can be seen as a first-order approximation of the neuron's activity over time. Determining the exact time at which a neuron spikes on the basis of its fluorescence trace is an important open problem in the field of computational neuroscience. Recently, a convex optimization problem involving an l1 penalty was proposed for this task. In this paper, we slightly modify that recent proposal by replacing the l1 penalty with an l0 penalty. In stark contrast to the conventional wisdom that l0 optimization problems are computationally intractable, we show that by drawing connections between this optimization problem and changepoint detection techniques, the l0 problem can be efficiently solved for the global optimum using an extremely simple and efficient dynamic programming algorithm. Our proposal leads to substantially better results than the previous l1 proposal.