To optimally allocate its marketing mix across customers, a firm needs to consider the evolution of its customers over time. Changes in the marketing environment, as well as intrinsic changes in preferences or needs, may discretely shift customers into different buying-behavior states. The ability to identify the dynamics in customer behavior and its drivers presents an opportunity for the firm to influence the movement of customers to more favorable states of buying behavior. Accordingly, we address the following managerial questions in this research: (1) how can the firm dynamically segment its customer base? (2) what are the short- and long-term effects of marketing activities? and (3) how should the firm allocate and target its marketing resources to maximize long-term profitability? To address these questions we propose a non-homogeneous hidden Markov model that accounts for dynamics in customer behavior, the long-term impact of marketing actions, and customer heterogeneity. We capture dynamics in customer behavior by allowing customers to transition over time among a set of latent states of buying behavior. We develop a unique and flexible approach to capture the enduring effect of marketing actions by incorporating a non-stationary transition matrix that is dynamically affected by these actions. To optimally allocate marketing activities, we formulate a dynamic programming approach which takes into account the evolution of customers' behavior. We apply the model in the context of direct-to-physicians marketing in a major pharmaceutical company. The results suggest that physicians transition among three behavioral states over time, showing a high degree of dynamics. Furthermore, the direct-to-physician marketing activities have varying degrees of short- and long-term effects that depend on the physician's prescription-behavior state. Specifically, we find that (i) both detailing and sampling have mostly long-term effects; (ii) detailing and sampling have a total duration impact of approximately 10 and 5 months, respectively; (iii) detailing is most effective as an acquisition marketing tool, whereas sampling is most effective as a retention tool. Using a counterfactual analysis, the optimization results show that by applying our dynamic marketing allocation approach, the firm could increase the number of prescriptions and its profits by as much as 51% and 80%, respectively. Moreover, our analysis suggests that the pharmaceutical firm should decrease its current detailing and sampling efforts by 30% and 20%, respectively. The integrative framework we propose provides important marketing implications for managing customers and maximizing long-run profitability.